| 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/sha1" |
| 21 | "crypto/x509" |
| 22 | "errors" |
| 23 | "fmt" |
| 24 | |
| 25 | "github.com/miekg/pkcs11" |
| 26 | |
| 27 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 28 | ) |
| 29 | |
| 30 | var newCertAttrs = []*pkcs11.Attribute{ |
| 31 | pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE), |
| 32 | pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true), |
| 33 | pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, false), |
| 34 | pkcs11.NewAttribute(pkcs11.CKA_CERTIFICATE_TYPE, pkcs11.CKC_X_509), |
| 35 | } |
| 36 | |
| 37 | func (tk *Token) ImportCertificate(cert *x509.Certificate, labelBase string) error { |
| 38 | fingerprint := sha1.Sum(cert.Raw) |
| 39 | if labelBase == "" { |
| 40 | return errors.New("label is required") |
| 41 | } |
| 42 | // make a label from the private key label plus the certificate fingerprint |
| 43 | label := fmt.Sprintf("%s_chain_%x", labelBase, fingerprint[:8]) |
| 44 | tk.mutex.Lock() |
| 45 | defer tk.mutex.Unlock() |
| 46 | if err := tk.certExists(label); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | keyID := makeKeyID() |
| 50 | if keyID == nil { |
| 51 | return errors.New("failed to make key ID") |
| 52 | } |
| 53 | attrs := []*pkcs11.Attribute{ |
| 54 | pkcs11.NewAttribute(pkcs11.CKA_ID, keyID), |
| 55 | pkcs11.NewAttribute(pkcs11.CKA_LABEL, label), |
| 56 | pkcs11.NewAttribute(pkcs11.CKA_SUBJECT, cert.RawSubject), |
| 57 | pkcs11.NewAttribute(pkcs11.CKA_ISSUER, cert.RawIssuer), |
| 58 | pkcs11.NewAttribute(pkcs11.CKA_VALUE, cert.Raw), |
| 59 | } |
| 60 | attrs = append(attrs, newCertAttrs...) |
| 61 | _, err := tk.ctx.CreateObject(tk.sh, attrs) |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | func (tk *Token) certExists(label string) error { |
| 66 | attrs := []*pkcs11.Attribute{ |
| 67 | pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE), |
| 68 | pkcs11.NewAttribute(pkcs11.CKA_LABEL, label), |
| 69 | } |
| 70 | objects, err := tk.findObject(attrs) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } else if len(objects) != 0 { |
| 74 | return sigerrors.ErrExist |
| 75 | } else { |
| 76 | return nil |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func (key *Key) ImportCertificate(cert *x509.Certificate) error { |
| 81 | keyID, handle, err := key.findCertificate() |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } else if handle != 0 { |
| 85 | return sigerrors.ErrExist |
| 86 | } |
| 87 | label := key.getLabel() |
| 88 | if label == "" { |
| 89 | return errors.New("label is required") |
| 90 | } |
| 91 | attrs := []*pkcs11.Attribute{ |
| 92 | pkcs11.NewAttribute(pkcs11.CKA_ID, keyID), |
| 93 | pkcs11.NewAttribute(pkcs11.CKA_LABEL, label), |
| 94 | pkcs11.NewAttribute(pkcs11.CKA_SUBJECT, cert.RawSubject), |
| 95 | pkcs11.NewAttribute(pkcs11.CKA_ISSUER, cert.RawIssuer), |
| 96 | pkcs11.NewAttribute(pkcs11.CKA_VALUE, cert.Raw), |
| 97 | } |
| 98 | attrs = append(attrs, newCertAttrs...) |
| 99 | key.token.mutex.Lock() |
| 100 | defer key.token.mutex.Unlock() |
| 101 | _, err = key.token.ctx.CreateObject(key.token.sh, attrs) |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | func (key *Key) findCertificate() (keyID []byte, handle pkcs11.ObjectHandle, err error) { |
| 106 | keyID = key.GetID() |
| 107 | if len(keyID) == 0 { |
| 108 | return nil, 0, errors.New("no keyID") |
| 109 | } |
| 110 | key.token.mutex.Lock() |
| 111 | defer key.token.mutex.Unlock() |
| 112 | attrs := []*pkcs11.Attribute{ |
| 113 | pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE), |
| 114 | pkcs11.NewAttribute(pkcs11.CKA_ID, keyID), |
| 115 | } |
| 116 | objects, err := key.token.findObject(attrs) |
| 117 | if err != nil { |
| 118 | return nil, 0, err |
| 119 | } |
| 120 | if len(objects) == 0 { |
| 121 | return keyID, 0, nil |
| 122 | } |
| 123 | return keyID, objects[0], nil |
| 124 | } |