-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_rsa.go
254 lines (219 loc) · 7.7 KB
/
context_rsa.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
/*
#include "pkcs11go.h"
*/
import "C"
import (
"crypto"
"crypto/rsa"
"encoding/binary"
"fmt"
"io"
"reflect"
"github.com/niclabs/dtcnode/v3/message"
"github.com/niclabs/tcrsa"
)
type SignContextRSA struct {
dtc *DTC
randSrc io.Reader
keyMeta *tcrsa.KeyMeta // Key Metainfo used in signing.
mechanism *Mechanism // Mechanism used to sign in a Sign session.
keyID string // Key ID used in signing.
data []byte // Data to sign.
initialized bool // // True if the user executed a Sign method and it has not finished yet.
}
type VerifyContextRSA struct {
dtc *DTC
randSrc io.Reader
keyMeta *tcrsa.KeyMeta // Key Metainfo used in sign verification.
mechanism *Mechanism // Mechanism used to verify a signature in a Verify session.
keyID string // Key ID used in sign verification.
data []byte // Data to verify.
initialized bool // True if the user executed a Verify method and it has not finished yet.
}
func (context *SignContextRSA) Initialized() bool {
return context.initialized
}
func (context *SignContextRSA) Init(metaBytes []byte) (err error) {
context.keyMeta, err = message.DecodeRSAKeyMeta(metaBytes)
context.initialized = true
return
}
func (context *SignContextRSA) SignatureLength() int {
return context.keyMeta.PublicKey.Size()
}
func (context *SignContextRSA) Update(data []byte) error {
context.data = append(context.data, data...)
return nil
}
func (context *SignContextRSA) Final() ([]byte, error) {
prepared, err := context.mechanism.Prepare(
context.randSrc,
context.keyMeta.PublicKey.Size(),
context.data,
)
if err != nil {
return nil, err
}
signature, err := context.dtc.RSASignData(context.keyID, context.keyMeta, prepared)
if err != nil {
return nil, err
}
if err = verifyRSA(
context.mechanism,
context.keyMeta.PublicKey,
context.data,
signature,
); err != nil {
return nil, err
}
return signature, nil
}
func (context *VerifyContextRSA) Initialized() bool {
return context.initialized
}
func (context *VerifyContextRSA) Init(metaBytes []byte) (err error) {
context.keyMeta, err = message.DecodeRSAKeyMeta(metaBytes)
context.initialized = true
return
}
func (context *VerifyContextRSA) Length() int {
return context.keyMeta.PublicKey.Size()
}
func (context *VerifyContextRSA) Update(data []byte) error {
context.data = append(context.data, data...)
return nil
}
func (context *VerifyContextRSA) Final(signature []byte) error {
return verifyRSA(
context.mechanism,
context.keyMeta.PublicKey,
context.data,
signature,
)
}
func verifyRSA(mechanism *Mechanism, pubKey crypto.PublicKey, data []byte, signature []byte) (err error) {
var hash []byte
hashType, err := mechanism.GetHashType()
if err != nil {
return
}
rsaPK, ok := pubKey.(*rsa.PublicKey)
if !ok {
return NewError("verifyRSA", "public key invalid for this type of signature", C.CKR_ARGUMENTS_BAD)
}
switch mechanism.Type {
case C.CKM_RSA_PKCS, C.CKM_MD5_RSA_PKCS, C.CKM_SHA1_RSA_PKCS, C.CKM_SHA256_RSA_PKCS, C.CKM_SHA384_RSA_PKCS, C.CKM_SHA512_RSA_PKCS:
if hashType == crypto.Hash(0) {
hash = data
} else {
hashFunc := hashType.New()
_, err = hashFunc.Write(data)
if err != nil {
return
}
hash = hashFunc.Sum(nil)
}
if err = rsa.VerifyPKCS1v15(rsaPK, hashType, hash, signature); err != nil {
return NewError("verifyRSA", "invalid signature", C.CKR_SIGNATURE_INVALID)
}
case C.CKM_SHA1_RSA_PKCS_PSS, C.CKM_SHA256_RSA_PKCS_PSS, C.CKM_SHA384_RSA_PKCS_PSS, C.CKM_SHA512_RSA_PKCS_PSS:
hashFunc := hashType.New()
_, err = hashFunc.Write(data)
if err != nil {
return
}
hash = hashFunc.Sum(nil)
if err = rsa.VerifyPSS(rsaPK, hashType, hash, signature, nil); err != nil {
return NewError("verifyRSA", "invalid signature", C.CKR_SIGNATURE_INVALID)
}
default:
err = NewError("verifyRSA", "mechanism not supported yet for verifying", C.CKR_MECHANISM_INVALID)
return
}
return
}
func createRSAPublicKey(keyID string, pkAttrs Attributes, keyMeta *tcrsa.KeyMeta) (Attributes, error) {
eBytes := make([]byte, reflect.TypeOf(keyMeta.PublicKey.E).Size())
binary.BigEndian.PutUint64(eBytes, uint64(keyMeta.PublicKey.E)) // Exponent is BigNumber
encodedKeyMeta, err := message.EncodeRSAKeyMeta(keyMeta)
if err != nil {
return nil, NewError("Session.createRSAPublicKey", fmt.Sprintf("%s", err.Error()), C.CKR_ARGUMENTS_BAD)
}
// This fields are defined in SoftHSM implementation
pkAttrs.SetIfUndefined(
&Attribute{C.CKA_CLASS, ulongToArr(C.CKO_PUBLIC_KEY)},
&Attribute{C.CKA_KEY_TYPE, ulongToArr(C.CKK_RSA)},
&Attribute{C.CKA_KEY_GEN_MECHANISM, ulongToArr(C.CKM_RSA_PKCS_KEY_PAIR_GEN)},
&Attribute{C.CKA_LOCAL, ulongToArr(C.CK_TRUE)},
// This fields are our defaults
&Attribute{C.CKA_LABEL, nil},
&Attribute{C.CKA_ID, nil},
&Attribute{C.CKA_SUBJECT, nil},
&Attribute{C.CKA_PRIVATE, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_MODIFIABLE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_TOKEN, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_DERIVE, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_ENCRYPT, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_VERIFY, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_VERIFY_RECOVER, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_WRAP, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_TRUSTED, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_START_DATE, make([]byte, 8)},
&Attribute{C.CKA_END_DATE, make([]byte, 8)},
&Attribute{C.CKA_MODULUS_BITS, nil},
&Attribute{C.CKA_PUBLIC_EXPONENT, eBytes},
)
pkAttrs.Set(
// E and N from PK
&Attribute{C.CKA_MODULUS, keyMeta.PublicKey.N.Bytes()},
// Custom Fields
&Attribute{AttrTypeKeyHandler, []byte(keyID)},
&Attribute{AttrTypeKeyMeta, encodedKeyMeta},
)
return pkAttrs, nil
}
func createRSAPrivateKey(keyID string, skAttrs Attributes, keyMeta *tcrsa.KeyMeta) (Attributes, error) {
eBytes := make([]byte, reflect.TypeOf(keyMeta.PublicKey.E).Size())
binary.BigEndian.PutUint64(eBytes, uint64(keyMeta.PublicKey.E))
encodedKeyMeta, err := message.EncodeRSAKeyMeta(keyMeta)
if err != nil {
return nil, NewError("Session.createRSAPrivateKey", fmt.Sprintf("%s", err.Error()), C.CKR_ARGUMENTS_BAD)
}
// This fields are defined in SoftHSM implementation
skAttrs.SetIfUndefined(
&Attribute{C.CKA_CLASS, ulongToArr(C.CKO_PRIVATE_KEY)},
&Attribute{C.CKA_KEY_TYPE, ulongToArr(C.CKK_RSA)},
&Attribute{C.CKA_KEY_GEN_MECHANISM, ulongToArr(C.CKM_RSA_PKCS_KEY_PAIR_GEN)},
&Attribute{C.CKA_LOCAL, ulongToArr(C.CK_TRUE)},
// This fields are our defaults
&Attribute{C.CKA_LABEL, nil},
&Attribute{C.CKA_ID, nil},
&Attribute{C.CKA_SUBJECT, nil},
&Attribute{C.CKA_PRIVATE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_MODIFIABLE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_TOKEN, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_DERIVE, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_WRAP_WITH_TRUSTED, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_ALWAYS_AUTHENTICATE, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_SENSITIVE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_ALWAYS_SENSITIVE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_DECRYPT, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_SIGN, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_SIGN_RECOVER, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_UNWRAP, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_EXTRACTABLE, ulongToArr(C.CK_FALSE)},
&Attribute{C.CKA_NEVER_EXTRACTABLE, ulongToArr(C.CK_TRUE)},
&Attribute{C.CKA_START_DATE, make([]byte, 8)},
&Attribute{C.CKA_END_DATE, make([]byte, 8)},
&Attribute{C.CKA_PUBLIC_EXPONENT, eBytes},
)
skAttrs.Set(
// E and N from PK
&Attribute{C.CKA_MODULUS, keyMeta.PublicKey.N.Bytes()},
// Custom Fields
&Attribute{AttrTypeKeyHandler, []byte(keyID)},
&Attribute{AttrTypeKeyMeta, encodedKeyMeta},
)
return skAttrs, nil
}