/
keyconfig.go
236 lines (189 loc) · 6.1 KB
/
keyconfig.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package pkcs11client
import (
"crypto/elliptic"
"encoding/asn1"
"errors"
"github.com/miekg/pkcs11"
)
type KeyConfig struct {
// CKA_LABEL
Label string
// CKA_ID doesn't appear to work with SoftHSM
Id []byte
// for CKA_KEY_TYPE
Type uint
// CKA_MODULUS BITS only needed for key creation
KeyBits int
// The mechanism will be auto populated but it can be manually set
Mechanism []*pkcs11.Mechanism
}
type KeyConfigKeyPairTemplate struct {
keyConfig KeyConfig
keyTemplatePrivConfig KeyTemplatePrivConfig
keyTemplatePubConfig KeyTemplatePubConfig
keyTemplateSecurity KeyTemplateSecurity
}
type KeyTemplatePrivConfig struct {
// CKA_DECRYPT
IsDecrypt bool
// CKA_UNWRAP
IsUnwrap bool
// CKA_SIGN
IsSign bool
// CKA_DERIVE
IsDerive bool
}
type KeyTemplatePubConfig struct {
// CKA_ENCRYPT
IsEncrypt bool
// CKA_WRAP
IsWrap bool
// CKA_VERIFY
IsVerify bool
// CKA_PUBLIC_EXPONTENT
Exponent []byte
}
type KeyTemplateSecurity struct {
// CKA_TOKEN token or session object
IsToken bool
// CKA_PRIVATE requires elevated privileges to report the presence of a key object
IsPrivate bool
// CKA_SENSITIVE
IsSensitive bool
// CKA_ALWAYS_SENSITIVE
IsAlwaysSensitive bool
// CKA_MODIFIABLE
IsModifiable bool
// CKA_EXTRACTABLE
IsExtractable bool
// CKA_NEVER_EXTRACTABLE
IsNeverExtractable bool
}
// an ID and label are needed when creating a key, though when using a key either may be used
func (k *KeyConfig) checkNewKeyIntegrity() bool {
return k.KeyBits > 0 && (len(k.Id) > 0 || len(k.Label) > 0)
}
func (k *KeyConfig) appendKeyIdentity(attribs []*pkcs11.Attribute) (fullAttribs []*pkcs11.Attribute, err error) {
found := false
var extraAttribs []*pkcs11.Attribute
if len(k.Id) > 0 {
extraAttribs = append(extraAttribs, pkcs11.NewAttribute(pkcs11.CKA_ID, k.Id))
found = true
}
if len(k.Label) > 0 {
extraAttribs = append(extraAttribs, pkcs11.NewAttribute(pkcs11.CKA_LABEL, k.Label))
found = true
}
if !found {
return nil, errors.New("Provide a key id or label")
}
fullAttribs = append(attribs, extraAttribs...)
return
}
func (kp *KeyConfigKeyPairTemplate) appendKeyPairGenParams(attribs []*pkcs11.Attribute) (fullAttribs []*pkcs11.Attribute, err error) {
found := false
var extraAttribs []*pkcs11.Attribute
if kp.keyConfig.Type == pkcs11.CKK_RSA {
extraAttribs = append(extraAttribs, pkcs11.NewAttribute(pkcs11.CKA_MODULUS_BITS, kp.keyConfig.KeyBits),
pkcs11.NewAttribute(pkcs11.CKA_PUBLIC_EXPONENT, kp.keyTemplatePubConfig.Exponent))
found = true
} else if kp.keyConfig.Type == pkcs11.CKK_EC || kp.keyConfig.Type == pkcs11.CKK_ECDSA {
var curve elliptic.Curve
switch kp.keyConfig.KeyBits {
case 224:
curve = elliptic.P224()
case 256:
curve = elliptic.P256()
case 384:
curve = elliptic.P384()
case 521:
curve = elliptic.P521()
default:
return nil, errors.New(ERR_UNSUPPORTEDCURVESIZE)
}
if curveOID, err := asn1.Marshal(curveOIDs[curve.Params().Name]); err != nil {
return nil, errors.New(ERR_UNSUPPORTEDCURVESIZE)
} else {
extraAttribs = append(extraAttribs, pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, curveOID))
}
found = true
}
if !found {
return nil, errors.New(ERR_UNSUPPORTEDKEYTYPE)
}
fullAttribs = append(attribs, extraAttribs...)
return
}
func (kp *KeyConfigKeyPairTemplate) GenDefaultKeyPairPrivTemplateForSigning() {
kp.keyTemplatePrivConfig = KeyTemplatePrivConfig{
IsDecrypt: false,
IsUnwrap: false,
IsSign: true,
IsDerive: false,
}
}
func (kp *KeyConfigKeyPairTemplate) GenDefaultKeyPairPubTemplateForSigning() {
kp.keyTemplatePubConfig = KeyTemplatePubConfig{
IsEncrypt: false,
IsWrap: false,
IsVerify: true,
Exponent: []byte{1, 0, 1},
}
}
func GenKeyConfigKeyPairTemplate(keyConfig *KeyConfig) KeyConfigKeyPairTemplate {
kp := KeyConfigKeyPairTemplate{}
kp.keyConfig = *keyConfig
return kp
}
func (kp *KeyConfigKeyPairTemplate) GenDefaultKeyPairTemplateForSigning() {
kp.GenDefaultKeySecurityTemplate()
kp.GenDefaultKeyPairPrivTemplateForSigning()
kp.GenDefaultKeyPairPubTemplateForSigning()
}
func (kp *KeyConfigKeyPairTemplate) GenDefaultKeySecurityTemplate() {
kp.keyTemplateSecurity = KeyTemplateSecurity{
IsToken: true,
IsPrivate: true,
IsSensitive: true,
IsAlwaysSensitive: true,
IsModifiable: false,
IsExtractable: false,
IsNeverExtractable: true,
}
}
func (kp *KeyConfigKeyPairTemplate) GenKeyPairTemplateAttribs() (privAttribs []*pkcs11.Attribute, pubAttribs []*pkcs11.Attribute, err error) {
privAttribs = []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PRIVATE_KEY),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, kp.keyTemplateSecurity.IsToken),
pkcs11.NewAttribute(pkcs11.CKA_PRIVATE, kp.keyTemplateSecurity.IsPrivate),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, kp.keyTemplateSecurity.IsSensitive),
// pkcs11.NewAttribute(pkcs11.CKA_ALWAYS_SENSITIVE, kp.keyTemplateSecurity.IsAlwaysSensitive),
pkcs11.NewAttribute(pkcs11.CKA_DECRYPT, kp.keyTemplatePrivConfig.IsDecrypt),
pkcs11.NewAttribute(pkcs11.CKA_UNWRAP, kp.keyTemplatePrivConfig.IsUnwrap),
pkcs11.NewAttribute(pkcs11.CKA_SIGN, kp.keyTemplatePrivConfig.IsSign),
pkcs11.NewAttribute(pkcs11.CKA_DERIVE, kp.keyTemplatePrivConfig.IsDerive),
pkcs11.NewAttribute(pkcs11.CKA_MODIFIABLE, kp.keyTemplateSecurity.IsModifiable),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, kp.keyTemplateSecurity.IsExtractable),
}
privAttribs, err = (kp.keyConfig).appendKeyIdentity(privAttribs)
if err != nil {
return
}
pubAttribs = []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PUBLIC_KEY),
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, kp.keyConfig.Type),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, kp.keyTemplateSecurity.IsToken),
pkcs11.NewAttribute(pkcs11.CKA_ENCRYPT, kp.keyTemplatePubConfig.IsEncrypt),
pkcs11.NewAttribute(pkcs11.CKA_WRAP, kp.keyTemplatePubConfig.IsWrap),
pkcs11.NewAttribute(pkcs11.CKA_VERIFY, kp.keyTemplatePubConfig.IsVerify),
}
pubAttribs, err = (kp.keyConfig).appendKeyIdentity(pubAttribs)
if err != nil {
return
}
pubAttribs, err = kp.appendKeyPairGenParams(pubAttribs)
if err != nil {
return
}
return
}