mirror of
https://github.com/hanzoai/pki.git
synced 2026-11-03 18:08:14 +00:00
114 lines
3.4 KiB
Go
114 lines
3.4 KiB
Go
package crypto11
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/effective-security/xlog"
|
|
"github.com/effective-security/xpki/certutil"
|
|
"github.com/miekg/pkcs11"
|
|
)
|
|
|
|
// CurrentSlotID returns current slot ID
|
|
func (lib *PKCS11Lib) CurrentSlotID() uint {
|
|
return lib.Slot.id
|
|
}
|
|
|
|
// TokensInfo returns list of tokens
|
|
func (lib *PKCS11Lib) TokensInfo() ([]*SlotTokenInfo, error) {
|
|
if err := lib.enter(); err != nil {
|
|
return nil, err
|
|
}
|
|
defer lib.exit()
|
|
|
|
list := []*SlotTokenInfo{}
|
|
slots, err := lib.Ctx.GetSlotList(true)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
logger.KV(xlog.TRACE, "slots", len(slots))
|
|
|
|
for _, slotID := range slots {
|
|
si, err := lib.Ctx.GetSlotInfo(slotID)
|
|
if err != nil {
|
|
return nil, errors.WithMessagef(err, "GetSlotInfo: %d", slotID)
|
|
}
|
|
ti, err := lib.Ctx.GetTokenInfo(slotID)
|
|
if err != nil {
|
|
logger.KV(xlog.ERROR, "reason", "GetTokenInfo", "slotID", slotID, "ManufacturerID", si.ManufacturerID, "SlotDescription", si.SlotDescription, "err", err)
|
|
} else if ti.SerialNumber != "" || ti.Label != "" {
|
|
list = append(list, &SlotTokenInfo{
|
|
id: slotID,
|
|
description: si.SlotDescription,
|
|
label: ti.Label,
|
|
manufacturer: strings.TrimSpace(ti.ManufacturerID),
|
|
model: strings.TrimSpace(ti.Model),
|
|
serial: ti.SerialNumber,
|
|
flags: ti.Flags,
|
|
})
|
|
|
|
}
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
// DestroyKeyPairOnSlot destroys key pair
|
|
func (lib *PKCS11Lib) DestroyKeyPairOnSlot(slotID uint, keyID string) error {
|
|
return lib.withSession(slotID, func(session pkcs11.SessionHandle) error {
|
|
return lib.destroyKeyPairOnSession(session, slotID, keyID)
|
|
})
|
|
}
|
|
|
|
func (lib *PKCS11Lib) destroyKeyPairOnSession(session pkcs11.SessionHandle, slotID uint, keyID string) error {
|
|
// A missing half of the pair is tolerated; any other lookup failure
|
|
// (session or token error) is returned before anything is destroyed,
|
|
// so a transient error can not delete only one object of the pair.
|
|
privHandle, err := lib.findKey(session, keyID, "", pkcs11.CKO_PRIVATE_KEY, ^uint(0))
|
|
if err != nil && !errors.Is(err, errKeyNotFound) {
|
|
return errors.WithMessagef(err, "find private key: slot=%d, key=%s", slotID, keyID)
|
|
}
|
|
pubHandle, err := lib.findKey(session, keyID, "", pkcs11.CKO_PUBLIC_KEY, ^uint(0))
|
|
if err != nil && !errors.Is(err, errKeyNotFound) {
|
|
return errors.WithMessagef(err, "find public key: slot=%d, key=%s", slotID, keyID)
|
|
}
|
|
if privHandle == 0 && pubHandle == 0 {
|
|
return errors.WithMessagef(errKeyNotFound, "slot=%d, key=%s", slotID, keyID)
|
|
}
|
|
|
|
if privHandle != 0 {
|
|
err = lib.Ctx.DestroyObject(session, privHandle)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
|
|
if pubHandle != 0 {
|
|
err = lib.Ctx.DestroyObject(session, pubHandle)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// getPublicKeyPEM retrieves public key for the specified key on session
|
|
func (lib *PKCS11Lib) getPublicKeyPEM(session pkcs11.SessionHandle, slotID uint, keyID string) (string, error) {
|
|
priv, err := lib.findKeyPairOnSession(session, slotID, keyID, "")
|
|
if err != nil {
|
|
return "", errors.WithMessagef(err, "unable to find key: slot=%d, key=%s", slotID, keyID)
|
|
}
|
|
|
|
pub, err := ConvertToPublic(priv)
|
|
if err != nil {
|
|
return "", errors.WithStack(err)
|
|
}
|
|
|
|
pemKey, err := certutil.EncodePublicKeyToPEM(pub)
|
|
if err != nil {
|
|
return "", errors.WithStack(err)
|
|
}
|
|
|
|
return string(pemKey), nil
|
|
}
|