本文整理匯總了Golang中github.com/miekg/pkcs11.Ctx類的典型用法代碼示例。如果您正苦於以下問題:Golang Ctx類的具體用法?Golang Ctx怎麽用?Golang Ctx使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Ctx類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getPublicKey
// Get the public key matching a private key
// TODO: Add support for non-RSA keys, switching on CKA_KEY_TYPE
func getPublicKey(module *pkcs11.Ctx, session pkcs11.SessionHandle, privateKeyHandle pkcs11.ObjectHandle) (rsa.PublicKey, error) {
var noKey rsa.PublicKey
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_MODULUS, nil),
pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, nil),
}
attr, err := module.GetAttributeValue(session, privateKeyHandle, template)
if err != nil {
return noKey, err
}
n := big.NewInt(0)
e := int(0)
gotModulus, gotExponent := false, false
for _, a := range attr {
if a.Type == pkcs11.CKA_MODULUS {
n.SetBytes(a.Value)
gotModulus = true
} else if a.Type == pkcs11.CKA_PUBLIC_EXPONENT {
bigE := big.NewInt(0)
bigE.SetBytes(a.Value)
e = int(bigE.Int64())
gotExponent = true
}
}
if !gotModulus || !gotExponent {
return noKey, errors.New("public key missing either modulus or exponent")
}
return rsa.PublicKey{
N: n,
E: e,
}, nil
}
示例2: makeSigner
func makeSigner(context *pkcs11.Ctx) (*signer, error) {
slot, err := getSlot(context, *tokenLabel)
if err != nil {
return nil, err
}
session, err := context.OpenSession(slot, pkcs11.CKF_SERIAL_SESSION)
if err != nil {
return nil, err
}
if err = context.Login(session, pkcs11.CKU_USER, *pin); err != nil {
context.CloseSession(session)
return nil, err
}
privateKey, err := getPrivateKey(context, session, *privateKeyLabel)
if err != nil {
context.CloseSession(session)
return nil, err
}
return &signer{context, session, privateKey}, nil
}
示例3: getPrivateKey
func (ps *Key) getPrivateKey(module *pkcs11.Ctx, session pkcs11.SessionHandle, label string) (pkcs11.ObjectHandle, error) {
var noHandle pkcs11.ObjectHandle
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PRIVATE_KEY),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
}
if err := module.FindObjectsInit(session, template); err != nil {
return noHandle, err
}
objs, _, err := module.FindObjects(session, 2)
if err != nil {
return noHandle, err
}
if err = module.FindObjectsFinal(session); err != nil {
return noHandle, err
}
if len(objs) == 0 {
return noHandle, fmt.Errorf("private key not found")
}
privateKeyHandle := objs[0]
// Check whether the key has the CKA_ALWAYS_AUTHENTICATE attribute.
// If so, fail: we don't want to have to re-authenticate for each sign
// operation.
attributes, err := module.GetAttributeValue(session, privateKeyHandle, []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_ALWAYS_AUTHENTICATE, false),
})
if err != nil {
return noHandle, err
}
for _, attribute := range attributes {
if len(attribute.Value) > 0 && attribute.Value[0] == 1 {
ps.alwaysAuthenticate = true
}
}
return privateKeyHandle, nil
}
示例4: getSlot
func getSlot(p *pkcs11.Ctx, label string) (uint, error) {
slots, err := p.GetSlotList(true)
if err != nil {
return 0, err
}
for _, slot := range slots {
_, err := p.GetSlotInfo(slot)
if err != nil {
return 0, err
}
tokenInfo, err := p.GetTokenInfo(slot)
if err != nil {
return 0, err
}
if tokenInfo.Label == label {
return slot, nil
}
}
return 0, fmt.Errorf("Slot not found: %s", label)
}
示例5: getPrivateKey
func getPrivateKey(context *pkcs11.Ctx, session pkcs11.SessionHandle, label string) (pkcs11.ObjectHandle, error) {
var noKey pkcs11.ObjectHandle
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PRIVATE_KEY),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
}
if err := context.FindObjectsInit(session, template); err != nil {
return noKey, err
}
objs, _, err := context.FindObjects(session, 2)
if err != nil {
return noKey, err
}
if err = context.FindObjectsFinal(session); err != nil {
return noKey, err
}
if len(objs) == 0 {
err = fmt.Errorf("private key not found")
return noKey, err
}
return objs[0], nil
}
示例6: cleanup
func cleanup(ctx *pkcs11.Ctx, session pkcs11.SessionHandle) {
ctx.Destroy()
ctx.Finalize()
ctx.CloseSession(session)
ctx.Logout(session)
}