/
tinksigner.go
461 lines (391 loc) · 13.7 KB
/
tinksigner.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package tinkjwt
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"encoding/asn1"
"encoding/binary"
"errors"
"fmt"
"math/big"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/tink-crypto/tink-go/v2/keyset"
commonpb "github.com/tink-crypto/tink-go/v2/proto/common_go_proto"
ecdsapb "github.com/tink-crypto/tink-go/v2/proto/ecdsa_go_proto"
rsppb "github.com/tink-crypto/tink-go/v2/proto/rsa_ssa_pkcs1_go_proto"
rspsspb "github.com/tink-crypto/tink-go/v2/proto/rsa_ssa_pss_go_proto"
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
"github.com/tink-crypto/tink-go/v2/signature"
"google.golang.org/protobuf/proto"
)
const (
rsaPKCS1PrivateKeyTypeURL = "type.googleapis.com/google.crypto.tink.RsaSsaPkcs1PrivateKey"
rsaSSAPKCS1VerifierTypeURL = "type.googleapis.com/google.crypto.tink.RsaSsaPkcs1PublicKey"
rsaPSSPrivateKeyTypeURL = "type.googleapis.com/google.crypto.tink.RsaSsaPssPrivateKey"
rsaPSSPKCS1VerifierTypeURL = "type.googleapis.com/google.crypto.tink.RsaSsaPssPublicKey"
ecdsaVerifierTypeURL = "type.googleapis.com/google.crypto.tink.EcdsaPublicKey"
ecdsaPrivateKeyTypeURL = "type.googleapis.com/google.crypto.tink.EcdsaPrivateKey"
// https://github.com/google/tink/blob/master/go/core/cryptofmt/cryptofmt.go#L68
// NonRawPrefixSize is the prefix size of Tink and Legacy key types.
NonRawPrefixSize = 5
// TinkPrefixSize is the prefix size of Tink key types.
// The prefix starts with \x01 and followed by a 4-byte key id.
TinkPrefixSize = NonRawPrefixSize
// TinkStartByte is the first byte of the prefix of Tink key types.
TinkStartByte = byte(1)
// RawPrefixSize is the prefix size of Raw key types.
// Raw prefix is empty.
RawPrefixSize = 0
// RawPrefix is the empty prefix of Raw key types.
RawPrefix = ""
)
type TINKConfig struct {
Key *keyset.Handle
KeyID string // (optional) the keyID (eg, specify the 'kid' parameter; if not set, use the TINK primary keyID)
publicKeyFromTINK crypto.PublicKey // the public key as read from KeyHandleFile, KeyHandleNV
}
type tinkConfigKey struct{}
func (k *TINKConfig) GetKeyID() string {
return k.KeyID
}
func (k *TINKConfig) GetPublicKey() crypto.PublicKey {
return k.publicKeyFromTINK
}
var (
SigningMethodTINKRS256 *SigningMethodTINK
SigningMethodTINKPS256 *SigningMethodTINK
SigningMethodTINKES256 *SigningMethodTINK
errMissingConfig = errors.New("tinkjwt: missing configuration in provided context")
)
type SigningMethodTINK struct {
alg string
override jwt.SigningMethod
hasher crypto.Hash
}
func bytesToBigInt(v []byte) *big.Int {
return new(big.Int).SetBytes(v)
}
// https://github.com/google/tink/blob/master/go/core/cryptofmt/cryptofmt.go#L68
func createOutputPrefix(size int, startByte byte, keyID uint32) string {
prefix := make([]byte, size)
prefix[0] = startByte
binary.BigEndian.PutUint32(prefix[1:], keyID)
return string(prefix)
}
func NewTINKContext(parent context.Context, val *TINKConfig) (context.Context, error) {
// first check if a TPM is even involved in the picture here since we can verify w/o a TPM
if val.Key == nil {
return nil, fmt.Errorf("tinkjwt: tpm device or key not set")
}
// TODO: find a better way to manage the keyset
// for now, only use the primary key
for _, k := range val.Key.KeysetInfo().GetKeyInfo() {
if val.Key.KeysetInfo().PrimaryKeyId == k.KeyId {
if val.KeyID == "" {
val.KeyID = fmt.Sprint(k.KeyId)
}
if k.Status != tinkpb.KeyStatusType_ENABLED {
return nil, fmt.Errorf("key is not ENABLED %d", k.KeyId)
}
// look for the primary key in the keyset
if k.OutputPrefixType != tinkpb.OutputPrefixType_RAW && k.OutputPrefixType != tinkpb.OutputPrefixType_TINK {
return nil, fmt.Errorf("outputPrefix type must be either RAW or TINK; got %v", k.OutputPrefixType)
}
switch k.TypeUrl {
case rsaPKCS1PrivateKeyTypeURL, rsaPSSPrivateKeyTypeURL:
publicKeyHandle, err := val.Key.Public()
if err != nil {
return nil, fmt.Errorf("could not acquire public Keyhandle %v", err)
}
bbw := new(bytes.Buffer)
bw := keyset.NewBinaryWriter(bbw)
err = publicKeyHandle.WriteWithNoSecrets(bw)
if err != nil {
return nil, fmt.Errorf("could not write encrypted keyhandle %v", err)
}
tpb := &tinkpb.Keyset{}
err = proto.Unmarshal(bbw.Bytes(), tpb)
if err != nil {
return nil, fmt.Errorf("could not unmarshall keyhandle %v", err)
}
for _, kk := range tpb.Key {
if kk.KeyId == val.Key.KeysetInfo().PrimaryKeyId {
kserialized := kk.KeyData.Value
if k.TypeUrl == rsaPKCS1PrivateKeyTypeURL {
key := &rsppb.RsaSsaPkcs1PublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
pubKey := &rsa.PublicKey{
E: int(bytesToBigInt(key.GetE()).Int64()),
N: bytesToBigInt(key.GetN()),
}
val.publicKeyFromTINK = pubKey
} else if k.TypeUrl == rsaPSSPrivateKeyTypeURL {
key := &rspsspb.RsaSsaPssPublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
pubKey := &rsa.PublicKey{
E: int(bytesToBigInt(key.GetE()).Int64()),
N: bytesToBigInt(key.GetN()),
}
val.publicKeyFromTINK = pubKey
} else {
return nil, fmt.Errorf("error: unknown private key type")
}
}
}
case rsaSSAPKCS1VerifierTypeURL, rsaPSSPKCS1VerifierTypeURL:
bbw := new(bytes.Buffer)
bw := keyset.NewBinaryWriter(bbw)
err := val.Key.WriteWithNoSecrets(bw)
if err != nil {
return nil, fmt.Errorf("could not write encrypted keyhandle %v", err)
}
tpb := &tinkpb.Keyset{}
err = proto.Unmarshal(bbw.Bytes(), tpb)
if err != nil {
return nil, fmt.Errorf("could not unmarshall keyhandle %v", err)
}
for _, kk := range tpb.Key {
if kk.KeyId == val.Key.KeysetInfo().PrimaryKeyId {
kserialized := kk.KeyData.Value
if k.TypeUrl == rsaSSAPKCS1VerifierTypeURL {
key := &rsppb.RsaSsaPkcs1PublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
pubKey := &rsa.PublicKey{
E: int(bytesToBigInt(key.GetE()).Int64()),
N: bytesToBigInt(key.GetN()),
}
val.publicKeyFromTINK = pubKey
} else if k.TypeUrl == rsaPSSPKCS1VerifierTypeURL {
key := &rspsspb.RsaSsaPssPublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
pubKey := &rsa.PublicKey{
E: int(bytesToBigInt(key.GetE()).Int64()),
N: bytesToBigInt(key.GetN()),
}
val.publicKeyFromTINK = pubKey
} else {
return nil, fmt.Errorf("error: unknown public key type")
}
}
}
case ecdsaPrivateKeyTypeURL:
publicKeyHandle, err := val.Key.Public()
if err != nil {
return nil, fmt.Errorf("could not acquire public Keyhandle %v", err)
}
bbw := new(bytes.Buffer)
bw := keyset.NewBinaryWriter(bbw)
err = publicKeyHandle.WriteWithNoSecrets(bw)
if err != nil {
return nil, fmt.Errorf("could not write encrypted keyhandle %v", err)
}
tpb := &tinkpb.Keyset{}
err = proto.Unmarshal(bbw.Bytes(), tpb)
if err != nil {
return nil, fmt.Errorf("could not unmarshall keyhandle %v", err)
}
for _, kk := range tpb.Key {
if kk.KeyId == val.Key.KeysetInfo().PrimaryKeyId {
kserialized := kk.KeyData.Value
key := &ecdsapb.EcdsaPublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
if key.Params.GetCurve() == commonpb.EllipticCurveType_NIST_P256 {
pubKey := &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: bytesToBigInt(key.X),
Y: bytesToBigInt(key.Y),
}
val.publicKeyFromTINK = pubKey
}
} else {
return nil, fmt.Errorf("unsupported keytype %v", err)
}
}
case ecdsaVerifierTypeURL:
bbw := new(bytes.Buffer)
bw := keyset.NewBinaryWriter(bbw)
err := val.Key.WriteWithNoSecrets(bw)
if err != nil {
return nil, fmt.Errorf("could not write encrypted keyhandle %v", err)
}
tpb := &tinkpb.Keyset{}
err = proto.Unmarshal(bbw.Bytes(), tpb)
if err != nil {
return nil, fmt.Errorf("could not unmarshall keyhandle %v", err)
}
for _, kk := range tpb.Key {
if kk.KeyId == val.Key.KeysetInfo().PrimaryKeyId {
kserialized := kk.KeyData.Value
key := &ecdsapb.EcdsaPublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
if key.Params.GetCurve() == commonpb.EllipticCurveType_NIST_P256 {
pubKey := &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: bytesToBigInt(key.X),
Y: bytesToBigInt(key.Y),
}
val.publicKeyFromTINK = pubKey
} else {
return nil, fmt.Errorf("unsupported keytype %v", err)
}
}
}
default:
return nil, fmt.Errorf("tinkjwt: error extracting public key %s", k.TypeUrl)
}
return context.WithValue(parent, tinkConfigKey{}, val), nil
}
}
return nil, fmt.Errorf("tinkjwt: primary keyID in keyset not found")
}
func TINKFromContext(ctx context.Context) (*TINKConfig, bool) {
val, ok := ctx.Value(tinkConfigKey{}).(*TINKConfig)
return val, ok
}
func init() {
// RS256
SigningMethodTINKRS256 = &SigningMethodTINK{
"TINKRS256",
jwt.SigningMethodRS256,
crypto.SHA256,
}
jwt.RegisterSigningMethod(SigningMethodTINKRS256.Alg(), func() jwt.SigningMethod {
return SigningMethodTINKRS256
})
// PS256
SigningMethodTINKPS256 = &SigningMethodTINK{
"TINKPS256",
jwt.SigningMethodPS256,
crypto.SHA256,
}
jwt.RegisterSigningMethod(SigningMethodTINKPS256.Alg(), func() jwt.SigningMethod {
return SigningMethodTINKPS256
})
// ES256
SigningMethodTINKES256 = &SigningMethodTINK{
"TINKES256",
jwt.SigningMethodES256,
crypto.SHA256,
}
jwt.RegisterSigningMethod(SigningMethodTINKES256.Alg(), func() jwt.SigningMethod {
return SigningMethodTINKES256
})
}
// Alg will return the JWT header algorithm identifier this method is configured for.
func (s *SigningMethodTINK) Alg() string {
return s.alg
}
// Override will override the default JWT implementation of the signing function this Cloud KMS type implements.
func (s *SigningMethodTINK) Override() {
s.alg = s.override.Alg()
jwt.RegisterSigningMethod(s.alg, func() jwt.SigningMethod {
return s
})
}
func (s *SigningMethodTINK) Hash() crypto.Hash {
return s.hasher
}
func (s *SigningMethodTINK) Sign(signingString string, key interface{}) ([]byte, error) {
var ctx context.Context
switch k := key.(type) {
case context.Context:
ctx = k
default:
return nil, jwt.ErrInvalidKey
}
config, ok := TINKFromContext(ctx)
if !ok {
return nil, errMissingConfig
}
signer, err := signature.NewSigner(config.Key)
if err != nil {
return nil, fmt.Errorf("error getting signer %v", err)
}
ss, err := signer.Sign([]byte(signingString))
if err != nil {
return nil, fmt.Errorf("error signing %v", err)
}
for _, k := range config.Key.KeysetInfo().GetKeyInfo() {
if config.Key.KeysetInfo().PrimaryKeyId == k.KeyId {
// remove the TINK Prefix
if k.OutputPrefixType == tinkpb.OutputPrefixType_TINK {
pf := createOutputPrefix(TinkPrefixSize, TinkStartByte, config.Key.KeysetInfo().PrimaryKeyId)
ss = ss[len(pf):]
}
switch k.TypeUrl {
case rsaPKCS1PrivateKeyTypeURL, rsaPSSPrivateKeyTypeURL:
return ss, err
case ecdsaPrivateKeyTypeURL:
publicKeyHandle, err := config.Key.Public()
if err != nil {
return nil, fmt.Errorf("could not acquire public Keyhandle %v", err)
}
bbw := new(bytes.Buffer)
bw := keyset.NewBinaryWriter(bbw)
err = publicKeyHandle.WriteWithNoSecrets(bw)
if err != nil {
return nil, fmt.Errorf("could not write encrypted keyhandle %v", err)
}
tpb := &tinkpb.Keyset{}
err = proto.Unmarshal(bbw.Bytes(), tpb)
if err != nil {
return nil, fmt.Errorf("could not unmarshall keyhandle %v", err)
}
for _, kk := range tpb.Key {
kserialized := kk.KeyData.Value
key := &ecdsapb.EcdsaPublicKey{}
if err := proto.Unmarshal(kserialized, key); err != nil {
return nil, fmt.Errorf("could not write unmarshall publicKey %v", err)
}
if key.Params.Encoding == ecdsapb.EcdsaSignatureEncoding_IEEE_P1363 {
return ss, nil
}
if key.Params.GetCurve() == commonpb.EllipticCurveType_NIST_P256 {
curveBits := elliptic.P256().Params().BitSize
keyBytes := curveBits / 8
if curveBits%8 > 0 {
keyBytes += 1
}
out := make([]byte, 2*keyBytes)
var sigStruct struct{ R, S *big.Int }
_, err = asn1.Unmarshal(ss, &sigStruct)
if err != nil {
return nil, fmt.Errorf("tinkjwt: can't unmarshall ecc struct %v", err)
}
sigStruct.R.FillBytes(out[0:keyBytes])
sigStruct.S.FillBytes(out[keyBytes:])
return out, nil
} else {
return nil, fmt.Errorf("unsupported keytype %v", err)
}
}
default:
return nil, fmt.Errorf("tinkjwt: error extracting publcic key %s", k.TypeUrl)
}
}
}
return ss, err
}
func TINKVerfiyKeyfunc(ctx context.Context, config *TINKConfig) (jwt.Keyfunc, error) {
return func(token *jwt.Token) (interface{}, error) {
return config.publicKeyFromTINK, nil
}, nil
}
func (s *SigningMethodTINK) Verify(signingString string, signature []byte, key interface{}) error {
return s.override.Verify(signingString, []byte(signature), key)
}