| 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/ecdsa" |
| 22 | "errors" |
| 23 | |
| 24 | "github.com/miekg/pkcs11" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 27 | ) |
| 28 | |
| 29 | // Convert token ECDSA public key to *ecdsa.PublicKey |
| 30 | func (key *Key) toEcdsaKey() (crypto.PublicKey, error) { |
| 31 | ecparams := key.token.getAttribute(key.pub, pkcs11.CKA_EC_PARAMS) |
| 32 | ecpoint := key.token.getAttribute(key.pub, pkcs11.CKA_EC_POINT) |
| 33 | if len(ecparams) == 0 || len(ecpoint) == 0 { |
| 34 | return nil, errors.New("Unable to retrieve ECDSA public key") |
| 35 | } |
| 36 | curve, err := x509tools.CurveByDer(ecparams) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | x, y := x509tools.DerToPoint(curve.Curve, ecpoint) |
| 41 | if x == nil || y == nil { |
| 42 | return nil, errors.New("Invalid elliptic curve point") |
| 43 | } |
| 44 | eckey := &ecdsa.PublicKey{Curve: curve.Curve, X: x, Y: y} |
| 45 | return eckey, nil |
| 46 | } |
| 47 | |
| 48 | // Sign a digest using token ECDSA private key |
| 49 | func (key *Key) signECDSA(digest []byte) (der []byte, err error) { |
| 50 | mech := pkcs11.NewMechanism(pkcs11.CKM_ECDSA, nil) |
| 51 | err = key.token.ctx.SignInit(key.token.sh, []*pkcs11.Mechanism{mech}, key.priv) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | sig, err := key.token.ctx.Sign(key.token.sh, digest) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | parsed, err := x509tools.UnpackEcdsaSignature(sig) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return parsed.Marshal(), nil |
| 64 | } |
| 65 | |
| 66 | // Generate ECDSA-specific public and private key attributes from a PrivateKey |
| 67 | func ecdsaImportAttrs(priv *ecdsa.PrivateKey) (pubAttrs, privAttrs []*pkcs11.Attribute, err error) { |
| 68 | curve, err := x509tools.CurveByCurve(priv.Curve) |
| 69 | if err != nil { |
| 70 | return nil, nil, err |
| 71 | } |
| 72 | pubAttrs = []*pkcs11.Attribute{ |
| 73 | pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, curve.ToDer()), |
| 74 | pkcs11.NewAttribute(pkcs11.CKA_EC_POINT, x509tools.PointToDer(&priv.PublicKey)), |
| 75 | } |
| 76 | privAttrs = []*pkcs11.Attribute{ |
| 77 | pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, curve.ToDer()), |
| 78 | pkcs11.NewAttribute(pkcs11.CKA_VALUE, priv.D.Bytes()), |
| 79 | } |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | // Generate ECDSA-specific public attributes to generate an ECSDA key in the token |
| 84 | func ecdsaGenerateAttrs(bits uint) ([]*pkcs11.Attribute, *pkcs11.Mechanism, error) { |
| 85 | curve, err := x509tools.CurveByBits(bits) |
| 86 | if err != nil { |
| 87 | return nil, nil, err |
| 88 | } |
| 89 | pubAttrs := []*pkcs11.Attribute{pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, curve.ToDer())} |
| 90 | mech := pkcs11.NewMechanism(pkcs11.CKM_EC_KEY_PAIR_GEN, nil) |
| 91 | return pubAttrs, mech, nil |
| 92 | } |