douhuan4699 2017-12-19 15:32 采纳率: 100%
浏览 177
已采纳

如何通过模板获取PKCS11 PublicKey

I have a GO Application, that stores it's certificate to a HSM via opencryptoki (pkcs11) in a softwaretoken. But it's not a go question, more a general pkcs11 question I think.

I set my certificates and privatekey like this:

    certTemplate := []*pkcs11.Attribute{
        pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE),
        pkcs11.NewAttribute(pkcs11.CKA_CERTIFICATE_TYPE, pkcs11.CKC_X_509),
        pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
        pkcs11.NewAttribute(pkcs11.CKA_VALUE, certBytes),
        pkcs11.NewAttribute(pkcs11.CKA_SUBJECT, template.SubjectKeyId),
        pkcs11.NewAttribute(pkcs11.CKA_ID, pkcs11KeyID),
    }

    privateKeyTemplate := []*pkcs11.Attribute{
        pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PRIVATE_KEY),
        pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKK_ECDSA),
        pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true), 
        pkcs11.NewAttribute(pkcs11.CKA_ID, pkcs11KeyID),
        pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, []byte{0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07}),
        pkcs11.NewAttribute(pkcs11.CKA_VALUE, ecdsaPrivKeyD),
    }

ctx.CreateObject(session, certTemplate)
ctx.CreateObject(session, privateKeyTemplate)

This works perfectly. I can also get the Certificate via

findTemplate := []*pkcs11.Attribute{
    pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
    pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE),
    pkcs11.NewAttribute(pkcs11.CKA_CERTIFICATE_TYPE, pkcs11.CKC_X_509),
}

ctx.FindObjectsInit(session, findTemplate); 
objs, b, err := ctx.FindObjects(session, numSlots)

But I cannot get my public key, as I expected

findTemplate := []*pkcs11.Attribute{
        pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
        pkcs11.NewAttribute(pkcs11.CKA_ID, pkcs11KeyID),
        pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PUBLIC_KEY),
    }
ctx.FindObjectsInit(session, findTemplate); 
obj, _, err := ctx.FindObjects(session, 1)

There is no error, just that there are no keys in the hsm store.

  • 写回答

2条回答 默认 最新

  • drju37335 2017-12-20 01:39
    关注

    Although the public key is part of the certificate, the CKO_PUBLIC_KEY object may not be separately available, and there isn't a CKA_PUBLIC attribute either for certificates.

    To get to the public key you'll have to retrieve the CKA_VALUE of the certificate and parse it using your favorite X.509v3 certificate parser. The result will undoubtedly return either the encoded public key value or a public key as structure in your favorite runtime.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题