| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package p11token |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto" |
| 22 | "crypto/rand" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "io" |
| 26 | |
| 27 | "github.com/miekg/pkcs11" |
| 28 | |
| 29 | "github.com/sassoftware/relic/v7/config" |
| 30 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 31 | "github.com/sassoftware/relic/v7/token" |
| 32 | ) |
| 33 | |
| 34 | type Key struct { |
| 35 | PgpCertificate string |
| 36 | X509Certificate string |
| 37 | token *Token |
| 38 | keyConf *config.KeyConfig |
| 39 | keyType uint |
| 40 | pub pkcs11.ObjectHandle |
| 41 | priv pkcs11.ObjectHandle |
| 42 | pubParsed crypto.PublicKey |
| 43 | } |
| 44 | |
| 45 | func (token *Token) GetKey(ctx context.Context, keyName string) (token.Key, error) { |
| 46 | token.mutex.Lock() |
| 47 | defer token.mutex.Unlock() |
| 48 | keyConf, err := token.config.GetKey(keyName) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | return token.getKey(keyConf, keyName) |
| 53 | } |
| 54 | |
| 55 | func (token *Token) getKey(keyConf *config.KeyConfig, keyName string) (*Key, error) { |
| 56 | var err error |
| 57 | key := &Key{ |
| 58 | token: token, |
| 59 | keyConf: keyConf, |
| 60 | PgpCertificate: keyConf.PgpCertificate, |
| 61 | X509Certificate: keyConf.X509Certificate, |
| 62 | } |
| 63 | key.priv, err = token.findKey(keyConf, pkcs11.CKO_PRIVATE_KEY) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | key.pub, err = token.findKey(keyConf, pkcs11.CKO_PUBLIC_KEY) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | keyTypeBlob := token.getAttribute(key.priv, pkcs11.CKA_KEY_TYPE) |
| 72 | if len(keyTypeBlob) == 0 { |
| 73 | return nil, errors.New("private key: CKA_KEY_TYPE is missing") |
| 74 | } |
| 75 | key.keyType, err = getUlong(keyTypeBlob) |
| 76 | if err != nil { |
| 77 | return nil, fmt.Errorf("private key: CKA_KEY_TYPE: %w", err) |
| 78 | } |
| 79 | switch key.keyType { |
| 80 | case CKK_RSA: |
| 81 | key.pubParsed, err = key.toRsaKey() |
| 82 | case CKK_ECDSA: |
| 83 | key.pubParsed, err = key.toEcdsaKey() |
| 84 | default: |
| 85 | return nil, errors.New("Unsupported key type") |
| 86 | } |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | return key, nil |
| 91 | } |
| 92 | |
| 93 | func (token *Token) findKey(keyConf *config.KeyConfig, class uint) (pkcs11.ObjectHandle, error) { |
| 94 | attrs := []*pkcs11.Attribute{ |
| 95 | pkcs11.NewAttribute(pkcs11.CKA_CLASS, class), |
| 96 | } |
| 97 | if keyConf.Label != "" { |
| 98 | attrs = append(attrs, pkcs11.NewAttribute(pkcs11.CKA_LABEL, keyConf.Label)) |
| 99 | } |
| 100 | if keyConf.ID != "" { |
| 101 | keyID, err := parseKeyID(keyConf.ID) |
| 102 | if err != nil { |
| 103 | return 0, err |
| 104 | } |
| 105 | attrs = append(attrs, pkcs11.NewAttribute(pkcs11.CKA_ID, keyID)) |
| 106 | } |
| 107 | objects, err := token.findObject(attrs) |
| 108 | if err != nil { |
| 109 | return 0, err |
| 110 | } else if len(objects) > 1 { |
| 111 | return 0, errors.New("Multiple token objects with the specified attributes") |
| 112 | } else if len(objects) == 0 { |
| 113 | return 0, sigerrors.KeyNotFoundError{} |
| 114 | } |
| 115 | return objects[0], nil |
| 116 | } |
| 117 | |
| 118 | func (key *Key) Config() *config.KeyConfig { |
| 119 | return key.keyConf |
| 120 | } |
| 121 | |
| 122 | func (key *Key) Certificate() []byte { |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | func (key *Key) Public() crypto.PublicKey { |
| 127 | return key.pubParsed |
| 128 | } |
| 129 | |
| 130 | func (key *Key) getLabel() string { |
| 131 | key.token.mutex.Lock() |
| 132 | defer key.token.mutex.Unlock() |
| 133 | return string(key.token.getAttribute(key.priv, pkcs11.CKA_LABEL)) |
| 134 | } |
| 135 | |
| 136 | func (key *Key) GetID() []byte { |
| 137 | key.token.mutex.Lock() |
| 138 | defer key.token.mutex.Unlock() |
| 139 | return key.token.getAttribute(key.priv, pkcs11.CKA_ID) |
| 140 | } |
| 141 | |
| 142 | func (key *Key) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 143 | key.token.mutex.Lock() |
| 144 | defer key.token.mutex.Unlock() |
| 145 | switch key.keyType { |
| 146 | case CKK_RSA: |
| 147 | return key.signRSA(digest, opts) |
| 148 | case CKK_ECDSA: |
| 149 | return key.signECDSA(digest) |
| 150 | default: |
| 151 | return nil, errors.New("Unsupported key type") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func (key *Key) SignContext(ctx context.Context, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 156 | return key.Sign(rand.Reader, digest, opts) |
| 157 | } |