-
Notifications
You must be signed in to change notification settings - Fork 41
/
certs.go
124 lines (115 loc) · 3.51 KB
/
certs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package p11token
import (
"crypto/sha1"
"crypto/x509"
"errors"
"fmt"
"github.com/miekg/pkcs11"
"github.com/sassoftware/relic/v8/signers/sigerrors"
)
var newCertAttrs = []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, false),
pkcs11.NewAttribute(pkcs11.CKA_CERTIFICATE_TYPE, pkcs11.CKC_X_509),
}
func (tk *Token) ImportCertificate(cert *x509.Certificate, labelBase string) error {
fingerprint := sha1.Sum(cert.Raw)
if labelBase == "" {
return errors.New("label is required")
}
// make a label from the private key label plus the certificate fingerprint
label := fmt.Sprintf("%s_chain_%x", labelBase, fingerprint[:8])
tk.mutex.Lock()
defer tk.mutex.Unlock()
if err := tk.certExists(label); err != nil {
return err
}
keyID := makeKeyID()
if keyID == nil {
return errors.New("failed to make key ID")
}
attrs := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_ID, keyID),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
pkcs11.NewAttribute(pkcs11.CKA_SUBJECT, cert.RawSubject),
pkcs11.NewAttribute(pkcs11.CKA_ISSUER, cert.RawIssuer),
pkcs11.NewAttribute(pkcs11.CKA_VALUE, cert.Raw),
}
attrs = append(attrs, newCertAttrs...)
_, err := tk.ctx.CreateObject(tk.sh, attrs)
return err
}
func (tk *Token) certExists(label string) error {
attrs := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
}
objects, err := tk.findObject(attrs)
if err != nil {
return err
} else if len(objects) != 0 {
return sigerrors.ErrExist
} else {
return nil
}
}
func (key *Key) ImportCertificate(cert *x509.Certificate) error {
keyID, handle, err := key.findCertificate()
if err != nil {
return err
} else if handle != 0 {
return sigerrors.ErrExist
}
label := key.getLabel()
if label == "" {
return errors.New("label is required")
}
attrs := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_ID, keyID),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
pkcs11.NewAttribute(pkcs11.CKA_SUBJECT, cert.RawSubject),
pkcs11.NewAttribute(pkcs11.CKA_ISSUER, cert.RawIssuer),
pkcs11.NewAttribute(pkcs11.CKA_VALUE, cert.Raw),
}
attrs = append(attrs, newCertAttrs...)
key.token.mutex.Lock()
defer key.token.mutex.Unlock()
_, err = key.token.ctx.CreateObject(key.token.sh, attrs)
return err
}
func (key *Key) findCertificate() (keyID []byte, handle pkcs11.ObjectHandle, err error) {
keyID = key.GetID()
if len(keyID) == 0 {
return nil, 0, errors.New("no keyID")
}
key.token.mutex.Lock()
defer key.token.mutex.Unlock()
attrs := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE),
pkcs11.NewAttribute(pkcs11.CKA_ID, keyID),
}
objects, err := key.token.findObject(attrs)
if err != nil {
return nil, 0, err
}
if len(objects) == 0 {
return keyID, 0, nil
}
return keyID, objects[0], nil
}