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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度