-
Notifications
You must be signed in to change notification settings - Fork 13
/
evp.go
580 lines (551 loc) · 18.1 KB
/
evp.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//go:build !cmd_go_bootstrap
package openssl
// #include "goopenssl.h"
import "C"
import (
"crypto"
"errors"
"hash"
"strconv"
"sync"
"unsafe"
)
var (
keyTypeRSA = C.CString("RSA")
keyTypeEC = C.CString("EC")
keyTypeED25519 = C.CString("ED25519")
)
// cacheMD is a cache of crypto.Hash to GO_EVP_MD_PTR.
var cacheMD sync.Map
// hashFuncHash calls fn() and returns its result.
// If fn() panics, the panic is recovered and returned as an error.
// This is used to avoid aborting the program when calling
// an unsupported hash function. It is the caller's responsibility
// to check the returned value.
func hashFuncHash(fn func() hash.Hash) (h hash.Hash, err error) {
defer func() {
r := recover()
if r == nil {
return
}
h = nil
switch e := r.(type) {
case error:
err = e
case string:
err = errors.New(e)
default:
err = errors.New("unsupported panic")
}
}()
return fn(), nil
}
// hashToMD converts a hash.Hash implementation from this package to a GO_EVP_MD_PTR.
func hashToMD(h hash.Hash) C.GO_EVP_MD_PTR {
if h, ok := h.(*evpHash); ok {
return h.alg.md
}
return nil
}
// hashFuncToMD converts a hash.Hash function to a GO_EVP_MD_PTR.
// See [hashFuncHash] for details on error handling.
func hashFuncToMD(fn func() hash.Hash) (C.GO_EVP_MD_PTR, error) {
h, err := hashFuncHash(fn)
if err != nil {
return nil, err
}
md := hashToMD(h)
if md == nil {
return nil, errors.New("unsupported hash function")
}
return md, nil
}
type hashAlgorithm struct {
md C.GO_EVP_MD_PTR
ch crypto.Hash
size int
blockSize int
marshallable bool
magic string
marshalledSize int
}
// loadHash converts a crypto.Hash to a EVP_MD.
func loadHash(ch crypto.Hash) *hashAlgorithm {
if v, ok := cacheMD.Load(ch); ok {
return v.(*hashAlgorithm)
}
var hash hashAlgorithm
switch ch {
case crypto.RIPEMD160:
hash.md = C.go_openssl_EVP_ripemd160()
case crypto.MD4:
hash.md = C.go_openssl_EVP_md4()
case crypto.MD5:
hash.md = C.go_openssl_EVP_md5()
hash.magic = md5Magic
hash.marshalledSize = md5MarshaledSize
case crypto.MD5SHA1:
if vMajor == 1 && vMinor == 0 {
// OpenSSL 1.0.2 does not support MD5SHA1.
hash.md = nil
} else {
hash.md = C.go_openssl_EVP_md5_sha1()
}
case crypto.SHA1:
hash.md = C.go_openssl_EVP_sha1()
hash.magic = sha1Magic
hash.marshalledSize = sha1MarshaledSize
case crypto.SHA224:
hash.md = C.go_openssl_EVP_sha224()
hash.magic = magic224
hash.marshalledSize = marshaledSize256
case crypto.SHA256:
hash.md = C.go_openssl_EVP_sha256()
hash.magic = magic256
hash.marshalledSize = marshaledSize256
case crypto.SHA384:
hash.md = C.go_openssl_EVP_sha384()
hash.magic = magic384
hash.marshalledSize = marshaledSize512
case crypto.SHA512:
hash.md = C.go_openssl_EVP_sha512()
hash.magic = magic512
hash.marshalledSize = marshaledSize512
case crypto.SHA512_224:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha512_224()
hash.magic = magic512_224
hash.marshalledSize = marshaledSize512
}
case crypto.SHA512_256:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha512_256()
hash.magic = magic512_256
hash.marshalledSize = marshaledSize512
}
case crypto.SHA3_224:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha3_224()
}
case crypto.SHA3_256:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha3_256()
}
case crypto.SHA3_384:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha3_384()
}
case crypto.SHA3_512:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha3_512()
}
}
if hash.md == nil {
cacheMD.Store(ch, (*hashAlgorithm)(nil))
return nil
}
hash.ch = ch
hash.size = int(C.go_openssl_EVP_MD_get_size(hash.md))
hash.blockSize = int(C.go_openssl_EVP_MD_get_block_size(hash.md))
if vMajor == 3 {
// On OpenSSL 3, directly operating on a EVP_MD object
// not created by EVP_MD_fetch has negative performance
// implications, as digest operations will have
// to fetch it on every call. Better to just fetch it once here.
md := C.go_openssl_EVP_MD_fetch(nil, C.go_openssl_EVP_MD_get0_name(hash.md), nil)
// Don't overwrite md in case it can't be fetched, as the md may still be used
// outside of EVP_MD_CTX, for example to sign and verify RSA signatures.
if md != nil {
hash.md = md
}
}
hash.marshallable = hash.magic != "" && isHashMarshallable(hash.md)
cacheMD.Store(ch, &hash)
return &hash
}
// generateEVPPKey generates a new EVP_PKEY with the given id and properties.
func generateEVPPKey(id C.int, bits int, curve string) (C.GO_EVP_PKEY_PTR, error) {
if bits != 0 && curve != "" {
return nil, fail("incorrect generateEVPPKey parameters")
}
var pkey C.GO_EVP_PKEY_PTR
switch vMajor {
case 1:
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(id, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_id")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_keygen_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen_init")
}
if bits != 0 {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, id, -1, C.GO_EVP_PKEY_CTRL_RSA_KEYGEN_BITS, C.int(bits), nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl")
}
}
if curve != "" {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, id, -1, C.GO_EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, curveNID(curve), nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl")
}
}
if C.go_openssl_EVP_PKEY_keygen(ctx, &pkey) != 1 {
return nil, newOpenSSLError("EVP_PKEY_keygen")
}
case 3:
switch id {
case C.GO_EVP_PKEY_RSA:
pkey = C.go_openssl_EVP_PKEY_Q_keygen_RSA(nil, nil, keyTypeRSA, C.size_t(bits))
case C.GO_EVP_PKEY_EC:
pkey = C.go_openssl_EVP_PKEY_Q_keygen_EC(nil, nil, keyTypeEC, C.go_openssl_OBJ_nid2sn(curveNID(curve)))
case C.GO_EVP_PKEY_ED25519:
pkey = C.go_openssl_EVP_PKEY_Q_keygen(nil, nil, keyTypeED25519)
default:
panic("unsupported key type '" + strconv.Itoa(int(id)) + "'")
}
if pkey == nil {
return nil, newOpenSSLError("EVP_PKEY_Q_keygen")
}
default:
panic(errUnsupportedVersion())
}
return pkey, nil
}
type withKeyFunc func(func(C.GO_EVP_PKEY_PTR) C.int) C.int
type initFunc func(C.GO_EVP_PKEY_CTX_PTR) error
type cryptFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, *C.size_t, *C.uchar, C.size_t) error
type verifyFunc func(C.GO_EVP_PKEY_CTX_PTR, *C.uchar, C.size_t, *C.uchar, C.size_t) error
func setupEVP(withKey withKeyFunc, padding C.int,
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc) (_ C.GO_EVP_PKEY_CTX_PTR, err error) {
var ctx C.GO_EVP_PKEY_CTX_PTR
withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
ctx = C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
return 1
})
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new failed")
}
defer func() {
if err != nil {
if ctx != nil {
C.go_openssl_EVP_PKEY_CTX_free(ctx)
ctx = nil
}
}
}()
if err := init(ctx); err != nil {
return nil, err
}
if padding == 0 {
return ctx, nil
}
// Each padding type has its own requirements in terms of when to apply the padding,
// so it can't be just set at this point.
setPadding := func() error {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_PADDING, padding, nil) != 1 {
return newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
return nil
}
switch padding {
case C.GO_RSA_PKCS1_OAEP_PADDING:
md := hashToMD(h)
if md == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
var mgfMD C.GO_EVP_MD_PTR
if mgfHash != nil {
// mgfHash is optional, but if it is set it must match a supported hash function.
mgfMD = hashToMD(mgfHash)
if mgfMD == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
}
// setPadding must happen before setting EVP_PKEY_CTRL_RSA_OAEP_MD.
if err := setPadding(); err != nil {
return nil, err
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_OAEP_MD, 0, unsafe.Pointer(md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
if mgfHash != nil {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_MGF1_MD, 0, unsafe.Pointer(mgfMD)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
// ctx takes ownership of label, so malloc a copy for OpenSSL to free.
// OpenSSL does not take ownership of the label if the length is zero,
// so better avoid the allocation.
var clabel *C.uchar
if len(label) > 0 {
clabel = (*C.uchar)(cryptoMalloc(len(label)))
copy((*[1 << 30]byte)(unsafe.Pointer(clabel))[:len(label)], label)
var err error
if vMajor == 3 {
ret := C.go_openssl_EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, unsafe.Pointer(clabel), C.int(len(label)))
if ret != 1 {
err = newOpenSSLError("EVP_PKEY_CTX_set0_rsa_oaep_label failed")
}
} else {
ret := C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_OAEP_LABEL, C.int(len(label)), unsafe.Pointer(clabel))
if ret != 1 {
err = newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
if err != nil {
cryptoFree(unsafe.Pointer(clabel))
return nil, err
}
}
case C.GO_RSA_PKCS1_PSS_PADDING:
alg := loadHash(ch)
if alg == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_MD, 0, unsafe.Pointer(alg.md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
// setPadding must happen after setting EVP_PKEY_CTRL_MD.
if err := setPadding(); err != nil {
return nil, err
}
if saltLen != 0 {
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, C.GO_EVP_PKEY_RSA, -1, C.GO_EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltLen, nil) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
}
case C.GO_RSA_PKCS1_PADDING:
if ch != 0 {
// We support unhashed messages.
alg := loadHash(ch)
if alg == nil {
return nil, errors.New("crypto/rsa: unsupported hash function")
}
if C.go_openssl_EVP_PKEY_CTX_ctrl(ctx, -1, -1, C.GO_EVP_PKEY_CTRL_MD, 0, unsafe.Pointer(alg.md)) != 1 {
return nil, newOpenSSLError("EVP_PKEY_CTX_ctrl failed")
}
if err := setPadding(); err != nil {
return nil, err
}
}
default:
if err := setPadding(); err != nil {
return nil, err
}
}
return ctx, nil
}
func cryptEVP(withKey withKeyFunc, padding C.int,
h, mgfHash hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc, crypt cryptFunc, in []byte) ([]byte, error) {
ctx, err := setupEVP(withKey, padding, h, mgfHash, label, saltLen, ch, init)
if err != nil {
return nil, err
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
pkeySize := withKey(func(pkey C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_PKEY_get_size(pkey)
})
outLen := C.size_t(pkeySize)
out := make([]byte, pkeySize)
if err := crypt(ctx, base(out), &outLen, base(in), C.size_t(len(in))); err != nil {
return nil, err
}
// The size returned by EVP_PKEY_get_size() is only preliminary and not exact,
// so the final contents of the out buffer may be smaller.
return out[:outLen], nil
}
func verifyEVP(withKey withKeyFunc, padding C.int,
h hash.Hash, label []byte, saltLen C.int, ch crypto.Hash,
init initFunc, verify verifyFunc,
sig, in []byte) error {
ctx, err := setupEVP(withKey, padding, h, nil, label, saltLen, ch, init)
if err != nil {
return err
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
return verify(ctx, base(sig), C.size_t(len(sig)), base(in), C.size_t(len(in)))
}
func evpEncrypt(withKey withKeyFunc, padding C.int, h, mgfHash hash.Hash, label, msg []byte) ([]byte, error) {
encryptInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_encrypt_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_encrypt_init failed")
}
return nil
}
encrypt := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_encrypt(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_encrypt failed")
}
return nil
}
return cryptEVP(withKey, padding, h, mgfHash, label, 0, 0, encryptInit, encrypt, msg)
}
func evpDecrypt(withKey withKeyFunc, padding C.int, h, mgfHash hash.Hash, label, msg []byte) ([]byte, error) {
decryptInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_decrypt_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_decrypt_init failed")
}
return nil
}
decrypt := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_decrypt(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_decrypt failed")
}
return nil
}
return cryptEVP(withKey, padding, h, mgfHash, label, 0, 0, decryptInit, decrypt, msg)
}
func evpSign(withKey withKeyFunc, padding C.int, saltLen C.int, h crypto.Hash, hashed []byte) ([]byte, error) {
signtInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_sign_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_sign_init failed")
}
return nil
}
sign := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen *C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_sign(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_sign failed")
}
return nil
}
return cryptEVP(withKey, padding, nil, nil, nil, saltLen, h, signtInit, sign, hashed)
}
func evpVerify(withKey withKeyFunc, padding C.int, saltLen C.int, h crypto.Hash, sig, hashed []byte) error {
verifyInit := func(ctx C.GO_EVP_PKEY_CTX_PTR) error {
if ret := C.go_openssl_EVP_PKEY_verify_init(ctx); ret != 1 {
return newOpenSSLError("EVP_PKEY_verify_init failed")
}
return nil
}
verify := func(ctx C.GO_EVP_PKEY_CTX_PTR, out *C.uchar, outLen C.size_t, in *C.uchar, inLen C.size_t) error {
if ret := C.go_openssl_EVP_PKEY_verify(ctx, out, outLen, in, inLen); ret != 1 {
return newOpenSSLError("EVP_PKEY_verify failed")
}
return nil
}
return verifyEVP(withKey, padding, nil, nil, saltLen, h, verifyInit, verify, sig, hashed)
}
func evpHashSign(withKey withKeyFunc, h crypto.Hash, msg []byte) ([]byte, error) {
alg := loadHash(h)
if alg == nil {
return nil, errors.New("unsupported hash function: " + strconv.Itoa(int(h)))
}
var out []byte
var outLen C.size_t
ctx := C.go_openssl_EVP_MD_CTX_new()
if ctx == nil {
return nil, newOpenSSLError("EVP_MD_CTX_new failed")
}
defer C.go_openssl_EVP_MD_CTX_free(ctx)
if withKey(func(key C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_DigestSignInit(ctx, nil, alg.md, nil, key)
}) != 1 {
return nil, newOpenSSLError("EVP_DigestSignInit failed")
}
if C.go_openssl_EVP_DigestUpdate(ctx, unsafe.Pointer(base(msg)), C.size_t(len(msg))) != 1 {
return nil, newOpenSSLError("EVP_DigestUpdate failed")
}
// Obtain the signature length
if C.go_openssl_EVP_DigestSignFinal(ctx, nil, &outLen) != 1 {
return nil, newOpenSSLError("EVP_DigestSignFinal failed")
}
out = make([]byte, outLen)
// Obtain the signature
if C.go_openssl_EVP_DigestSignFinal(ctx, base(out), &outLen) != 1 {
return nil, newOpenSSLError("EVP_DigestSignFinal failed")
}
return out[:outLen], nil
}
func evpHashVerify(withKey withKeyFunc, h crypto.Hash, msg, sig []byte) error {
alg := loadHash(h)
if alg == nil {
return errors.New("unsupported hash function: " + strconv.Itoa(int(h)))
}
ctx := C.go_openssl_EVP_MD_CTX_new()
if ctx == nil {
return newOpenSSLError("EVP_MD_CTX_new failed")
}
defer C.go_openssl_EVP_MD_CTX_free(ctx)
if withKey(func(key C.GO_EVP_PKEY_PTR) C.int {
return C.go_openssl_EVP_DigestVerifyInit(ctx, nil, alg.md, nil, key)
}) != 1 {
return newOpenSSLError("EVP_DigestVerifyInit failed")
}
if C.go_openssl_EVP_DigestUpdate(ctx, unsafe.Pointer(base(msg)), C.size_t(len(msg))) != 1 {
return newOpenSSLError("EVP_DigestUpdate failed")
}
if C.go_openssl_EVP_DigestVerifyFinal(ctx, base(sig), C.size_t(len(sig))) != 1 {
return newOpenSSLError("EVP_DigestVerifyFinal failed")
}
return nil
}
func newEVPPKEY(key C.GO_EC_KEY_PTR) (C.GO_EVP_PKEY_PTR, error) {
pkey := C.go_openssl_EVP_PKEY_new()
if pkey == nil {
return nil, newOpenSSLError("EVP_PKEY_new failed")
}
if C.go_openssl_EVP_PKEY_assign(pkey, C.GO_EVP_PKEY_EC, unsafe.Pointer(key)) != 1 {
C.go_openssl_EVP_PKEY_free(pkey)
return nil, newOpenSSLError("EVP_PKEY_assign failed")
}
return pkey, nil
}
// getECKey returns the EC_KEY from pkey.
// If pkey does not contain an EC_KEY it panics.
// The returned key should not be freed.
func getECKey(pkey C.GO_EVP_PKEY_PTR) (key C.GO_EC_KEY_PTR) {
if vMajor == 1 && vMinor == 0 {
if key0 := C.go_openssl_EVP_PKEY_get0(pkey); key0 != nil {
key = C.GO_EC_KEY_PTR(key0)
}
} else {
key = C.go_openssl_EVP_PKEY_get0_EC_KEY(pkey)
}
if key == nil {
panic("pkey does not contain an EC_KEY")
}
return key
}
func newEvpFromParams(id C.int, selection C.int, params C.GO_OSSL_PARAM_PTR) (C.GO_EVP_PKEY_PTR, error) {
ctx := C.go_openssl_EVP_PKEY_CTX_new_id(id, nil)
if ctx == nil {
return nil, newOpenSSLError("EVP_PKEY_CTX_new_id")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if C.go_openssl_EVP_PKEY_fromdata_init(ctx) != 1 {
return nil, newOpenSSLError("EVP_PKEY_fromdata_init")
}
var pkey C.GO_EVP_PKEY_PTR
if C.go_openssl_EVP_PKEY_fromdata(ctx, &pkey, selection, params) != 1 {
if vMajor == 3 && vMinor <= 2 {
// OpenSSL 3.0.1 and 3.0.2 have a bug where EVP_PKEY_fromdata
// does not free the internally allocated EVP_PKEY on error.
// See https://github.com/openssl/openssl/issues/17407.
C.go_openssl_EVP_PKEY_free(pkey)
}
return nil, newOpenSSLError("EVP_PKEY_fromdata")
}
return pkey, nil
}
func checkPkey(pkey C.GO_EVP_PKEY_PTR, isPrivate bool) error {
ctx := C.go_openssl_EVP_PKEY_CTX_new(pkey, nil)
if ctx == nil {
return newOpenSSLError("EVP_PKEY_CTX_new")
}
defer C.go_openssl_EVP_PKEY_CTX_free(ctx)
if isPrivate {
if C.go_openssl_EVP_PKEY_private_check(ctx) != 1 {
// Match upstream error message.
return errors.New("invalid private key")
}
} else {
// Upstream Go does a partial check here, so do we.
if C.go_openssl_EVP_PKEY_public_check_quick(ctx) != 1 {
// Match upstream error message.
return errors.New("invalid public key")
}
}
return nil
}