| 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 | "crypto" |
| 21 | "crypto/rsa" |
| 22 | "errors" |
| 23 | "math" |
| 24 | "math/big" |
| 25 | |
| 26 | "github.com/miekg/pkcs11" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | ) |
| 30 | |
| 31 | // Convert token RSA public key to *rsa.PublicKey |
| 32 | func (key *Key) toRsaKey() (crypto.PublicKey, error) { |
| 33 | modulus := key.token.getAttribute(key.pub, pkcs11.CKA_MODULUS) |
| 34 | exponent := key.token.getAttribute(key.pub, pkcs11.CKA_PUBLIC_EXPONENT) |
| 35 | if len(modulus) == 0 || len(exponent) == 0 { |
| 36 | return nil, errors.New("unable to retrieve RSA public key") |
| 37 | } |
| 38 | n := new(big.Int).SetBytes(modulus) |
| 39 | e := new(big.Int).SetBytes(exponent) |
| 40 | eInt := e.Int64() |
| 41 | if !e.IsInt64() || eInt > math.MaxInt || eInt < 3 { |
| 42 | return nil, errors.New("RSA exponent is out of bounds") |
| 43 | } |
| 44 | return &rsa.PublicKey{N: n, E: int(eInt)}, nil |
| 45 | } |
| 46 | |
| 47 | func (key *Key) newPssMech(opts *rsa.PSSOptions) (*pkcs11.Mechanism, error) { |
| 48 | var hashAlg, mgfType uint |
| 49 | switch opts.Hash { |
| 50 | case crypto.SHA1: |
| 51 | hashAlg = pkcs11.CKM_SHA_1 |
| 52 | mgfType = pkcs11.CKG_MGF1_SHA1 |
| 53 | case crypto.SHA224: |
| 54 | hashAlg = pkcs11.CKM_SHA224 |
| 55 | mgfType = pkcs11.CKG_MGF1_SHA224 |
| 56 | case crypto.SHA256: |
| 57 | hashAlg = pkcs11.CKM_SHA256 |
| 58 | mgfType = pkcs11.CKG_MGF1_SHA256 |
| 59 | case crypto.SHA384: |
| 60 | hashAlg = pkcs11.CKM_SHA384 |
| 61 | mgfType = pkcs11.CKG_MGF1_SHA384 |
| 62 | case crypto.SHA512: |
| 63 | hashAlg = pkcs11.CKM_SHA512 |
| 64 | mgfType = pkcs11.CKG_MGF1_SHA512 |
| 65 | default: |
| 66 | return nil, errors.New("unsupported hash type for PSS") |
| 67 | } |
| 68 | saltLength := opts.SaltLength |
| 69 | switch saltLength { |
| 70 | case rsa.PSSSaltLengthAuto: |
| 71 | pub := key.pubParsed.(*rsa.PublicKey) |
| 72 | saltLength = (pub.N.BitLen()+7)/8 - 2 - opts.Hash.Size() |
| 73 | case rsa.PSSSaltLengthEqualsHash: |
| 74 | saltLength = opts.Hash.Size() |
| 75 | } |
| 76 | args := make([]byte, ulongSize*3) |
| 77 | putUlong(args, hashAlg) |
| 78 | putUlong(args[ulongSize:], mgfType) |
| 79 | putUlong(args[ulongSize*2:], uint(saltLength)) |
| 80 | return pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS_PSS, args), nil |
| 81 | } |
| 82 | |
| 83 | // Sign a digest using token RSA private key |
| 84 | func (key *Key) signRSA(digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 85 | var mech *pkcs11.Mechanism |
| 86 | if opts == nil || opts.HashFunc() == 0 { |
| 87 | return nil, errors.New("signer options are required") |
| 88 | } else if pss, ok := opts.(*rsa.PSSOptions); ok { |
| 89 | var err error |
| 90 | mech, err = key.newPssMech(pss) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | } else { |
| 95 | var ok bool |
| 96 | digest, ok = x509tools.MarshalDigest(opts.HashFunc(), digest) |
| 97 | if !ok { |
| 98 | return nil, errors.New("unsupported hash function") |
| 99 | } |
| 100 | mech = pkcs11.NewMechanism(pkcs11.CKM_RSA_PKCS, nil) |
| 101 | } |
| 102 | err := key.token.ctx.SignInit(key.token.sh, []*pkcs11.Mechanism{mech}, key.priv) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | return key.token.ctx.Sign(key.token.sh, digest) |
| 107 | } |
| 108 | |
| 109 | // Generate RSA-specific public and private key attributes from a PrivateKey |
| 110 | func rsaImportAttrs(priv *rsa.PrivateKey) (pubAttrs, privAttrs []*pkcs11.Attribute, err error) { |
| 111 | if len(priv.Primes) != 2 || priv.Precomputed.Dp == nil || priv.Precomputed.Dq == nil || priv.Precomputed.Qinv == nil { |
| 112 | // multi-prime keys and keys without the precomputed values are rare |
| 113 | // enough not to be interesting |
| 114 | return nil, nil, errors.New("unsupported RSA key") |
| 115 | } |
| 116 | pubAttrs = []*pkcs11.Attribute{ |
| 117 | pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, big.NewInt(int64(priv.E)).Bytes()), |
| 118 | pkcs11.NewAttribute(pkcs11.CKA_MODULUS, priv.N.Bytes()), |
| 119 | } |
| 120 | privAttrs = []*pkcs11.Attribute{ |
| 121 | pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, big.NewInt(int64(priv.E)).Bytes()), |
| 122 | pkcs11.NewAttribute(pkcs11.CKA_MODULUS, priv.N.Bytes()), |
| 123 | pkcs11.NewAttribute(pkcs11.CKA_PRIVATE_EXPONENT, priv.D.Bytes()), |
| 124 | pkcs11.NewAttribute(pkcs11.CKA_PRIME_1, priv.Primes[0].Bytes()), |
| 125 | pkcs11.NewAttribute(pkcs11.CKA_PRIME_2, priv.Primes[1].Bytes()), |
| 126 | pkcs11.NewAttribute(pkcs11.CKA_EXPONENT_1, priv.Precomputed.Dp.Bytes()), |
| 127 | pkcs11.NewAttribute(pkcs11.CKA_EXPONENT_2, priv.Precomputed.Dq.Bytes()), |
| 128 | pkcs11.NewAttribute(pkcs11.CKA_COEFFICIENT, priv.Precomputed.Qinv.Bytes()), |
| 129 | } |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | // Generate RSA-specific public attributes to generate an RSA key in the token |
| 134 | func rsaGenerateAttrs(bits uint) ([]*pkcs11.Attribute, *pkcs11.Mechanism, error) { |
| 135 | if bits < 1024 || bits > 4096 { |
| 136 | return nil, nil, errors.New("unsupported number of bits") |
| 137 | } |
| 138 | pubExponent := []byte{1, 0, 1} // 65537 |
| 139 | attrs := []*pkcs11.Attribute{ |
| 140 | pkcs11.NewAttribute(pkcs11.CKA_MODULUS_BITS, bits), |
| 141 | pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, pubExponent), |
| 142 | } |
| 143 | mech := pkcs11.NewMechanism(pkcs11.CKM_RSA_X9_31_KEY_PAIR_GEN, nil) |
| 144 | return attrs, mech, nil |
| 145 | } |