Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/evp/evp.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/evp.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/curve25519.h>
10 #include <openssl/dsa.h>
11 #include <openssl/ec.h>
12 #include <openssl/err.h>
13 #include <openssl/mem.h>
14 #include <openssl/nid.h>
15 #include <openssl/rsa.h>
16 #include <openssl/thread.h>
17
18 #include "../../evp_extra/internal.h"
19 #include "../../pem/internal.h"
20 #include "../../console/internal.h"
21 #include "../../internal.h"
22 #include "../kem/internal.h"
23 #include "../pqdsa/internal.h"
24 #include "internal.h"
25
26
27 // Node depends on |EVP_R_NOT_XOF_OR_INVALID_LENGTH|.
28 //
29 // TODO(davidben): Fix Node to not touch the error queue itself and remove this.
30 OPENSSL_DECLARE_ERROR_REASON(EVP, NOT_XOF_OR_INVALID_LENGTH)
31
32 // The HPKE module uses the EVP error namespace, but it lives in another
33 // directory.
34 OPENSSL_DECLARE_ERROR_REASON(EVP, EMPTY_PSK)
35
36 EVP_PKEY *EVP_PKEY_new(void) {
37 EVP_PKEY *ret;
38
39 ret = OPENSSL_zalloc(sizeof(EVP_PKEY));
40 if (ret == NULL) {
41 return NULL;
42 }
43
44 ret->type = EVP_PKEY_NONE;
45 ret->references = 1;
46
47 return ret;
48 }
49
50 static void free_it(EVP_PKEY *pkey) {
51 if (pkey->ameth && pkey->ameth->pkey_free) {
52 pkey->ameth->pkey_free(pkey);
53 }
54 pkey->pkey.ptr = NULL;
55 pkey->type = EVP_PKEY_NONE;
56 pkey->ameth = NULL;
57 }
58
59 void EVP_PKEY_free(EVP_PKEY *pkey) {
60 SET_DIT_AUTO_RESET;
61 if (pkey == NULL) {
62 return;
63 }
64
65 if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
66 return;
67 }
68
69 free_it(pkey);
70 OPENSSL_free(pkey);
71 }
72
73 int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
74 SET_DIT_AUTO_RESET;
75 CRYPTO_refcount_inc(&pkey->references);
76 return 1;
77 }
78
79 int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
80 SET_DIT_AUTO_RESET;
81 if (pkey->ameth && pkey->ameth->pkey_opaque) {
82 return pkey->ameth->pkey_opaque(pkey);
83 }
84 return 0;
85 }
86
87 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
88 SET_DIT_AUTO_RESET;
89 if (a->type != b->type) {
90 return -1;
91 }
92
93 if (a->ameth) {
94 int ret;
95 // Compare parameters if the algorithm has them
96 if (a->ameth->param_cmp) {
97 ret = a->ameth->param_cmp(a, b);
98 if (ret <= 0) {
99 return ret;
100 }
101 }
102
103 if (a->ameth->pub_cmp) {
104 return a->ameth->pub_cmp(a, b);
105 }
106 }
107
108 return -2;
109 }
110
111 char *EVP_get_pw_prompt(void) {
112 return (char*)"Enter PEM passphrase:";
113 }
114
115 int EVP_read_pw_string(char *buf, int length, const char *prompt, int verify) {
116 return EVP_read_pw_string_min(buf, 0, length, prompt, verify);
117 }
118
119 int EVP_read_pw_string_min(char *buf, int min_length, int length,
120 const char *prompt, int verify) {
121 int ret = -1;
122 char verify_buf[1024];
123
124 if (!buf || min_length < 0 || min_length >= length) {
125 return -1;
126 }
127
128 if (prompt == NULL) {
129 prompt = EVP_get_pw_prompt();
130 }
131
132 // Proactively zeroize |buf| and verify_buf
133 OPENSSL_cleanse(buf, length);
134 OPENSSL_cleanse(verify_buf, sizeof(verify_buf));
135
136 // acquire write lock
137 openssl_console_acquire_mutex();
138
139 if (!openssl_console_open()) {
140 goto err;
141 }
142
143 // Write initial password prompt
144 if (!openssl_console_write(prompt)) {
145 goto err;
146 }
147
148 // Read password with echo disabled. Returns 0 on success.
149 // While |buf| and |length| are user-provided and can be arbitrarily large,
150 // passwords exceeding 1024 characters will be rejected with AWS-LC. OpenSSL
151 // handles this by silently truncating |length| before reading the password.
152 ret = openssl_console_read(buf, min_length, length, 0);
153 if (ret != 0) {
154 OPENSSL_cleanse(buf, length);
155 OPENSSL_PUT_ERROR(PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
156 goto err;
157 }
158
159 if (verify) {
160 openssl_console_write("Verifying - ");
161 openssl_console_write(prompt);
162
163 ret = openssl_console_read(verify_buf, min_length, sizeof(verify_buf), 0);
164
165 if (ret == 0) {
166 if (strncmp(buf, verify_buf, length) != 0) {
167 openssl_console_write("Verify failure\n");
168 ret = -1;
169 }
170 } else {
171 OPENSSL_PUT_ERROR(PEM, PEM_R_PROBLEMS_GETTING_PASSWORD);
172 goto err;
173 }
174 }
175
176 openssl_console_close();
177
178 err:
179 openssl_console_release_mutex();
180 OPENSSL_cleanse(verify_buf, sizeof(verify_buf));
181 return ret;
182 }
183
184 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
185 SET_DIT_AUTO_RESET;
186 if (to->type == EVP_PKEY_NONE) {
187 // TODO(crbug.com/42290409): This shouldn't leave |to| in a half-empty state
188 // on error. The complexity here largely comes from parameterless DSA keys,
189 // which we no longer support, so this function can probably be trimmed
190 // down.
191 evp_pkey_set0(to, from->ameth, NULL);
192 } else if (to->type != from->type) {
193 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
194 return 0;
195 }
196
197 if (EVP_PKEY_missing_parameters(from)) {
198 OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
199 return 0;
200 }
201
202 // Once set, parameters may not change.
203 if (!EVP_PKEY_missing_parameters(to)) {
204 if (EVP_PKEY_cmp_parameters(to, from) == 1) {
205 return 1;
206 }
207 OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
208 return 0;
209 }
210
211 if (from->ameth && from->ameth->param_copy) {
212 return from->ameth->param_copy(to, from);
213 }
214
215 // TODO(https://crbug.com/boringssl/536): If the algorithm takes no
216 // parameters, copying them should vacuously succeed.
217 return 0;
218 }
219
220 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
221 SET_DIT_AUTO_RESET;
222 if (pkey->ameth && pkey->ameth->param_missing) {
223 return pkey->ameth->param_missing(pkey);
224 }
225 return 0;
226 }
227
228 int EVP_PKEY_size(const EVP_PKEY *pkey) {
229 SET_DIT_AUTO_RESET;
230 if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
231 return pkey->ameth->pkey_size(pkey);
232 }
233 return 0;
234 }
235
236 int EVP_PKEY_bits(const EVP_PKEY *pkey) {
237 SET_DIT_AUTO_RESET;
238 if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
239 return pkey->ameth->pkey_bits(pkey);
240 }
241 return 0;
242 }
243
244 int EVP_PKEY_id(const EVP_PKEY *pkey) {
245 SET_DIT_AUTO_RESET;
246 return pkey->type;
247 }
248
249 int EVP_PKEY_pqdsa_get_type(const EVP_PKEY *pkey) {
250 SET_DIT_AUTO_RESET;
251 if (pkey->type != EVP_PKEY_PQDSA) {
252 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_PQDSA_KEY);
253 return 0;
254 }
255 if (!pkey->pkey.pqdsa_key || !pkey->pkey.pqdsa_key->pqdsa) {
256 return 0;
257 }
258 return pkey->pkey.pqdsa_key->pqdsa->nid;
259 }
260
261 int EVP_PKEY_kem_get_type(const EVP_PKEY *pkey) {
262 SET_DIT_AUTO_RESET;
263 if (pkey->type != EVP_PKEY_KEM) {
264 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_KEM_KEY);
265 return 0;
266 }
267 if (!pkey->pkey.kem_key || !pkey->pkey.kem_key->kem) {
268 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_PARAMETERS_SET);
269 return 0;
270 }
271 return pkey->pkey.kem_key->kem->nid;
272 }
273
274 int EVP_MD_get_pkey_type(const EVP_MD *md) {
275 if (md) {
276 int sig_nid = 0;
277 if (OBJ_find_sigid_by_algs(&sig_nid, md->type, NID_rsaEncryption)) {
278 return sig_nid;
279 }
280 }
281 return 0;
282 }
283
284 int EVP_MD_pkey_type(const EVP_MD *md){
285 return EVP_MD_get_pkey_type(md);
286 }
287
288 const char *EVP_MD_get0_name(const EVP_MD *md) {
289 if (md != NULL) {
290 return OBJ_nid2sn(EVP_MD_nid(md));
291 }
292 return NULL;
293 }
294
295 const char *EVP_MD_name(const EVP_MD *md) {
296 return EVP_MD_get0_name(md);
297 }
298
299 // evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
300 // should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
301 // unknown.
302 static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
303
304 const EVP_PKEY_ASN1_METHOD *const *methods = AWSLC_non_fips_pkey_evp_asn1_methods();
305 for (size_t i = 0; i < ASN1_EVP_PKEY_METHODS; i++) {
306 if (methods[i]->pkey_id == nid) {
307 return methods[i];
308 }
309 }
310
311 return NULL;
312 }
313
314 void evp_pkey_set0(EVP_PKEY *pkey, const EVP_PKEY_ASN1_METHOD *method,
315 void *pkey_data) {
316 free_it(pkey);
317 pkey->ameth = method;
318 pkey->type = method ? method->pkey_id : EVP_PKEY_NONE;
319 pkey->pkey.ptr = pkey_data;
320 }
321
322 static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len) {
323 if (pkey && pkey->pkey.ptr) {
324 // This isn't strictly necessary, but historically |EVP_PKEY_set_type| would
325 // clear |pkey| even if |evp_pkey_asn1_find| failed, so we preserve that
326 // behavior.
327 evp_pkey_set0(pkey, NULL, NULL);
328 }
329
330 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
331
332 if (str != NULL) {
333 ameth = EVP_PKEY_asn1_find_str(NULL, str, len);
334 } else {
335 ameth = evp_pkey_asn1_find(type);
336 }
337
338 if (ameth == NULL) {
339 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
340 ERR_add_error_dataf("algorithm %d", type);
341 return 0;
342 }
343
344 if (pkey) {
345 evp_pkey_set0(pkey, ameth, NULL);
346 }
347
348 return 1;
349 }
350
351 int EVP_PKEY_type(int nid) {
352 // In OpenSSL, this was used to map between type aliases. BoringSSL supports
353 // no type aliases, so this function is just the identity.
354 return nid;
355 }
356
357 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *engine, const uint8_t *mac_key,
358 size_t mac_key_len) {
359 SET_DIT_AUTO_RESET;
360 // Only |EVP_PKEY_HMAC| is supported as of now.
361 if (type != EVP_PKEY_HMAC) {
362 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
363 return NULL;
364 }
365 // NULL |mac_key| will result in a complete zero-key being used, but in that
366 // case, the length must be zero.
367 if (mac_key == NULL && mac_key_len > 0) {
368 return NULL;
369 }
370
371 EVP_PKEY *ret = EVP_PKEY_new();
372 if (ret == NULL) {
373 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
374 return NULL;
375 }
376
377 HMAC_KEY *key = HMAC_KEY_new();
378 if(key == NULL) {
379 goto err;
380 }
381 key->key = OPENSSL_memdup(mac_key, mac_key_len);
382 if (key->key == NULL && mac_key_len > 0) {
383 OPENSSL_free(key);
384 goto err;
385 }
386 key->key_len = mac_key_len;
387
388 if(!EVP_PKEY_assign(ret, EVP_PKEY_HMAC, key)) {
389 OPENSSL_free(key);
390 goto err;
391 }
392 return ret;
393
394 err:
395 OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP);
396 EVP_PKEY_free(ret);
397 return NULL;
398 }
399
400 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
401 SET_DIT_AUTO_RESET;
402 if (EVP_PKEY_assign_RSA(pkey, key)) {
403 RSA_up_ref(key);
404 return 1;
405 }
406 return 0;
407 }
408
409 int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
410 SET_DIT_AUTO_RESET;
411 if (key == NULL) {
412 return 0;
413 }
414 const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(EVP_PKEY_RSA);
415 assert(meth != NULL);
416 evp_pkey_set0(pkey, meth, key);
417 return 1;
418 }
419
420 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
421 SET_DIT_AUTO_RESET;
422 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
423 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
424 return NULL;
425 }
426 return pkey->pkey.rsa;
427 }
428
429 RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
430 SET_DIT_AUTO_RESET;
431 RSA *rsa = EVP_PKEY_get0_RSA(pkey);
432 if (rsa != NULL) {
433 RSA_up_ref(rsa);
434 }
435 return rsa;
436 }
437
438 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
439 SET_DIT_AUTO_RESET;
440 if (EVP_PKEY_assign_DSA(pkey, key)) {
441 DSA_up_ref(key);
442 return 1;
443 }
444 return 0;
445 }
446
447 int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
448 SET_DIT_AUTO_RESET;
449 if (key == NULL) {
450 return 0;
451 }
452 const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(EVP_PKEY_DSA);
453 assert(meth != NULL);
454 evp_pkey_set0(pkey, meth, key);
455 return 1;
456 }
457
458 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
459 SET_DIT_AUTO_RESET;
460 if (pkey->type != EVP_PKEY_DSA) {
461 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
462 return NULL;
463 }
464 return pkey->pkey.dsa;
465 }
466
467 DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) {
468 SET_DIT_AUTO_RESET;
469 DSA *dsa = EVP_PKEY_get0_DSA(pkey);
470 if (dsa != NULL) {
471 DSA_up_ref(dsa);
472 }
473 return dsa;
474 }
475
476 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
477 SET_DIT_AUTO_RESET;
478 if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
479 EC_KEY_up_ref(key);
480 return 1;
481 }
482 return 0;
483 }
484
485 int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
486 SET_DIT_AUTO_RESET;
487 if (key == NULL) {
488 return 0;
489 }
490 const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(EVP_PKEY_EC);
491 assert(meth != NULL);
492 evp_pkey_set0(pkey, meth, key);
493 return 1;
494 }
495
496 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
497 SET_DIT_AUTO_RESET;
498 if (pkey->type != EVP_PKEY_EC) {
499 OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_EC_KEY_KEY);
500 return NULL;
501 }
502 return pkey->pkey.ec;
503 }
504
505 EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) {
506 SET_DIT_AUTO_RESET;
507 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
508 if (ec_key != NULL) {
509 EC_KEY_up_ref(ec_key);
510 }
511 return ec_key;
512 }
513
514 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
515 // This function can only be used to assign RSA, DSA, EC, and DH keys. Other
516 // key types have internal representations which are not exposed through the
517 // public API.
518 SET_DIT_AUTO_RESET;
519 switch (type) {
520 case EVP_PKEY_RSA:
521 return EVP_PKEY_assign_RSA(pkey, key);
522 case EVP_PKEY_DSA:
523 return EVP_PKEY_assign_DSA(pkey, key);
524 case EVP_PKEY_EC:
525 return EVP_PKEY_assign_EC_KEY(pkey, key);
526 case EVP_PKEY_DH:
527 return EVP_PKEY_assign_DH(pkey, key);
528 default:
529 if (!EVP_PKEY_set_type(pkey, type)) {
530 return 0;
531 }
532 pkey->pkey.ptr = key;
533 return key != NULL;
534 }
535 }
536
537 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
538 SET_DIT_AUTO_RESET;
539 return pkey_set_type(pkey, type, NULL, -1);
540 }
541
542 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) {
543 SET_DIT_AUTO_RESET;
544 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
545 }
546
547 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
548 const uint8_t *in, size_t len) {
549 SET_DIT_AUTO_RESET;
550 // To avoid pulling in all key types, look for specifically the key types that
551 // support |set_priv_raw|.
552 const EVP_PKEY_ASN1_METHOD *method;
553 switch (type) {
554 case EVP_PKEY_X25519:
555 method = &x25519_asn1_meth;
556 break;
557 case EVP_PKEY_ED25519:
558 method = &ed25519_asn1_meth;
559 break;
560 case EVP_PKEY_ED25519PH:
561 method = &ed25519ph_asn1_meth;
562 break;
563 case EVP_PKEY_HMAC:
564 method = &hmac_asn1_meth;
565 break;
566 default:
567 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
568 return 0;
569 }
570
571 EVP_PKEY *ret = EVP_PKEY_new();
572 if (ret == NULL) {
573 goto err;
574 }
575 evp_pkey_set0(ret, method, NULL);
576
577 if (!method->set_priv_raw(ret, in, len, NULL, 0)) {
578 goto err;
579 }
580
581 return ret;
582
583 err:
584 EVP_PKEY_free(ret);
585 return NULL;
586 }
587
588 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
589 const uint8_t *in, size_t len) {
590 // To avoid pulling in all key types, look for specifically the key types that
591 // support |set_pub_raw|.
592 const EVP_PKEY_ASN1_METHOD *method;
593 switch (type) {
594 case EVP_PKEY_X25519:
595 method = &x25519_asn1_meth;
596 break;
597 case EVP_PKEY_ED25519:
598 method = &ed25519_asn1_meth;
599 break;
600 case EVP_PKEY_ED25519PH:
601 method = &ed25519ph_asn1_meth;
602 break;
603 default:
604 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
605 return 0;
606 }
607
608 EVP_PKEY *ret = EVP_PKEY_new();
609 if (ret == NULL) {
610 goto err;
611 }
612 evp_pkey_set0(ret, method, NULL);
613
614 if (!method->set_pub_raw(ret, in, len)) {
615 goto err;
616 }
617
618 return ret;
619
620 err:
621 EVP_PKEY_free(ret);
622 return NULL;
623 }
624
625 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, uint8_t *out,
626 size_t *out_len) {
627 SET_DIT_AUTO_RESET;
628 if (pkey == NULL ||
629 pkey->ameth == NULL ||
630 pkey->ameth->get_priv_raw == NULL) {
631 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
632 return 0;
633 }
634
635 return pkey->ameth->get_priv_raw(pkey, out, out_len);
636 }
637
638 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, uint8_t *out,
639 size_t *out_len) {
640 SET_DIT_AUTO_RESET;
641 if (pkey == NULL ||
642 pkey->ameth == NULL ||
643 pkey->ameth->get_pub_raw == NULL) {
644 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
645 return 0;
646 }
647
648 return pkey->ameth->get_pub_raw(pkey, out, out_len);
649 }
650
651 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
652 SET_DIT_AUTO_RESET;
653 if (a->type != b->type) {
654 return -1;
655 }
656 if (a->ameth && a->ameth->param_cmp) {
657 return a->ameth->param_cmp(a, b);
658 }
659 // TODO(https://crbug.com/boringssl/536): If the algorithm doesn't use
660 // parameters, they should compare as vacuously equal.
661 return -2;
662 }
663
664 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
665 SET_DIT_AUTO_RESET;
666 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
667 (void *)md);
668 }
669
670 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
671 SET_DIT_AUTO_RESET;
672 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
673 0, (void *)out_md);
674 }
675
676 int EVP_PKEY_CTX_set1_signature_context_string(EVP_PKEY_CTX *ctx,
677 const uint8_t *context,
678 size_t context_len) {
679 EVP_PKEY_CTX_SIGNATURE_CONTEXT_PARAMS params = {context, context_len};
680 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
681 EVP_PKEY_CTRL_SIGNING_CONTEXT, 0, &params);
682 }
683
684 int EVP_PKEY_CTX_set_signature_context(EVP_PKEY_CTX *ctx,
685 const uint8_t *context,
686 size_t context_len) {
687 return EVP_PKEY_CTX_set1_signature_context_string(ctx, context, context_len);
688 }
689
690 int EVP_PKEY_CTX_get0_signature_context(EVP_PKEY_CTX *ctx,
691 const uint8_t **context,
692 size_t *context_len) {
693 GUARD_PTR(context);
694 GUARD_PTR(context_len);
695 EVP_PKEY_CTX_SIGNATURE_CONTEXT_PARAMS params = {NULL, 0};
696 if (!EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
697 EVP_PKEY_CTRL_GET_SIGNING_CONTEXT, 0, &params)) {
698 return 0;
699 }
700 *context = params.context;
701 *context_len = params.context_len;
702 return 1;
703 }
704
705
706 void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
707 SET_DIT_AUTO_RESET;
708 GUARD_PTR(pkey);
709 switch (pkey->type) {
710 case EVP_PKEY_RSA:
711 case EVP_PKEY_RSA_PSS:
712 case EVP_PKEY_DSA:
713 case EVP_PKEY_EC:
714 case EVP_PKEY_DH:
715 return pkey->pkey.ptr;
716 default:
717 return NULL;
718 }
719 }
720
721 void OpenSSL_add_all_algorithms(void) {}
722
723 void OPENSSL_add_all_algorithms_conf(void) {}
724
725 void OpenSSL_add_all_ciphers(void) {}
726
727 void OpenSSL_add_all_digests(void) {}
728
729 void EVP_cleanup(void) {}
730
731 int EVP_PKEY_base_id(const EVP_PKEY *pkey) {
732 SET_DIT_AUTO_RESET;
733 // OpenSSL has two notions of key type because it supports multiple OIDs for
734 // the same algorithm: NID_rsa vs NID_rsaEncryption and five distinct spelling
735 // of DSA. We do not support these, so the base ID is simply the ID.
736 return EVP_PKEY_id(pkey);
737 }
738
739 static int evp_pkey_tls_encodedpoint_ec_curve_supported(const EC_KEY *ec_key) {
740
741 int ret = 0;
742 int curve_nid = 0;
743 const EC_GROUP *ec_key_group = NULL;
744
745 if (NULL == ec_key) {
746 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
747 goto err;
748 }
749
750 ec_key_group = EC_KEY_get0_group(ec_key);
751 if (NULL == ec_key_group) {
752 OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
753 goto err;
754 }
755
756 curve_nid = EC_GROUP_get_curve_name(ec_key_group);
757 if ((NID_secp224r1 != curve_nid) &&
758 (NID_X9_62_prime256v1 != curve_nid) &&
759 (NID_secp384r1 != curve_nid) &&
760 (NID_secp521r1 != curve_nid)) {
761 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
762 goto err;
763 }
764
765 ret = 1;
766
767 err:
768 return ret;
769 }
770
771 static int evp_pkey_set1_tls_encodedpoint_ec_key(EVP_PKEY *pkey,
772 const uint8_t *in,
773 size_t len) {
774 int ret = 0;
775 EC_KEY *ec_key = NULL;
776 const EC_GROUP *ec_key_group = NULL;
777 EC_POINT *ec_point = NULL;
778
779 if ((NULL == pkey) || (NULL == in)) {
780 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
781 goto err;
782 }
783
784 if (1 > len) {
785 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS);
786 goto err;
787 }
788
789 if (EVP_PKEY_EC != pkey->type) {
790 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
791 goto err;
792 }
793
794 // This function is TLS-specific. Only support TLS EC point representation,
795 // which must be uncompressed
796 // (https://tools.ietf.org/html/rfc8422#section-5.4.1)
797 // TLS wire-encoding format for supported NIST curves are:
798 // compression || x-coordinate || y-coordinate
799 // where:
800 // compression = 0x04 if uncompressed
801 // compression = 0x02/0x03 if compressed (depending on y-coordinate parity)
802 if (POINT_CONVERSION_UNCOMPRESSED != in[0]) {
803 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
804 goto err;
805 }
806
807 ec_key = EVP_PKEY_get0_EC_KEY(pkey);
808 if (NULL == ec_key) {
809 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
810 goto err;
811 }
812
813 if (0 == evp_pkey_tls_encodedpoint_ec_curve_supported(ec_key)) {
814 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
815 goto err;
816 }
817
818 ec_key_group = EC_KEY_get0_group(ec_key);
819 if (NULL == ec_key_group) {
820 OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
821 goto err;
822 }
823
824 ec_point = EC_POINT_new(ec_key_group);
825 if (NULL == ec_point) {
826 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
827 goto err;
828 }
829
830 if (0 == EC_POINT_oct2point(ec_key_group, ec_point, in, len, NULL)) {
831 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
832 goto err;
833 }
834
835 if (0 == EC_KEY_set_public_key(ec_key, (const EC_POINT *) ec_point)) {
836 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
837 goto err;
838 }
839
840 ret = 1;
841
842 err:
843 EC_POINT_free(ec_point);
844 return ret;
845 }
846
847 static int evp_pkey_set1_tls_encodedpoint_x25519(EVP_PKEY *pkey,
848 const uint8_t *in,
849 size_t len) {
850 int ret = 0;
851
852 if ((NULL == pkey) || (NULL == in)) {
853 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
854 goto err;
855 }
856
857 if (EVP_PKEY_X25519 != pkey->type) {
858 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
859 goto err;
860 }
861
862 if (1 > len) {
863 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PARAMETERS);
864 goto err;
865 }
866
867 if ((NULL == pkey->ameth) || (NULL == pkey->ameth->set_pub_raw)) {
868 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
869 goto err;
870 }
871
872 if (0 == pkey->ameth->set_pub_raw(pkey, in, len)) {
873 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
874 goto err;
875 }
876
877 ret = 1;
878
879 err:
880 return ret;
881 }
882
883 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
884 size_t len) {
885 SET_DIT_AUTO_RESET;
886 if (NULL == pkey) {
887 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
888 goto err;
889 }
890
891 switch (pkey->type) {
892 case EVP_PKEY_X25519:
893 return evp_pkey_set1_tls_encodedpoint_x25519(pkey, in, len);
894 case EVP_PKEY_EC:
895 return evp_pkey_set1_tls_encodedpoint_ec_key(pkey, in, len);
896 default:
897 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
898 goto err;
899 }
900
901 err:
902 return 0;
903 }
904
905 static size_t evp_pkey_get1_tls_encodedpoint_ec_key(const EVP_PKEY *pkey,
906 uint8_t **out_ptr) {
907
908 size_t ret = 0;
909 const EC_KEY *ec_key = NULL;
910
911 if ((NULL == pkey) || (NULL == out_ptr)) {
912 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
913 goto err;
914 }
915
916 if (EVP_PKEY_EC != pkey->type) {
917 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
918 goto err;
919 }
920
921 ec_key = EVP_PKEY_get0_EC_KEY(pkey);
922 if (NULL == ec_key) {
923 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
924 goto err;
925 }
926
927 if (0 == evp_pkey_tls_encodedpoint_ec_curve_supported(ec_key)) {
928 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
929 goto err;
930 }
931
932 // This function is TLS-specific. Only support TLS EC point representation,
933 // which must be uncompressed
934 // (https://tools.ietf.org/html/rfc8422#section-5.4.1)
935 if (POINT_CONVERSION_UNCOMPRESSED != EC_KEY_get_conv_form(ec_key)) {
936 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
937 goto err;
938 }
939
940 // Returns the length of |*out_ptr|
941 ret = EC_KEY_key2buf(ec_key, POINT_CONVERSION_UNCOMPRESSED, out_ptr, NULL);
942 if (0 == ret) {
943 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
944 goto err;
945 }
946
947 err:
948 return ret;
949 }
950
951 static size_t evp_pkey_get1_tls_encodedpoint_x25519(const EVP_PKEY *pkey,
952 uint8_t **out_ptr) {
953
954 size_t ret = 0;
955 size_t out_len = 0;
956
957 if ((NULL == pkey) || (NULL == out_ptr)) {
958 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
959 return 0;
960 }
961
962 if (EVP_PKEY_X25519 != pkey->type) {
963 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
964 return 0;
965 }
966
967 if ((NULL == pkey->ameth) || (NULL == pkey->ameth->get_pub_raw)) {
968 OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
969 return 0;
970 }
971
972 out_len = X25519_SHARED_KEY_LEN;
973 *out_ptr = OPENSSL_malloc(X25519_SHARED_KEY_LEN);
974 if (NULL == *out_ptr) {
975 return 0;
976 }
977
978 if (0 == pkey->ameth->get_pub_raw(pkey, *out_ptr, &out_len)) {
979 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
980 goto err;
981 }
982
983 if (X25519_SHARED_KEY_LEN != out_len) {
984 OPENSSL_PUT_ERROR(EVP, ERR_R_EVP_LIB);
985 goto err;
986 }
987
988 ret = X25519_SHARED_KEY_LEN;
989
990 err:
991 if (0 == ret) {
992 OPENSSL_free(*out_ptr);
993 *out_ptr = NULL;
994 }
995 return ret;
996 }
997
998 size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
999 SET_DIT_AUTO_RESET;
1000 if (NULL == pkey) {
1001 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
1002 goto err;
1003 }
1004
1005 switch (pkey->type) {
1006 case EVP_PKEY_X25519:
1007 return evp_pkey_get1_tls_encodedpoint_x25519(pkey, out_ptr);
1008 case EVP_PKEY_EC:
1009 return evp_pkey_get1_tls_encodedpoint_ec_key(pkey, out_ptr);
1010 default:
1011 OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
1012 goto err;
1013 }
1014
1015 err:
1016 return 0;
1017 }