]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/ssl_rsa.c
Convert all {NAME}err() in ssl/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / ssl / ssl_rsa.c
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include "ssl_local.h"
12 #include "internal/packet.h"
13 #include <openssl/bio.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pem.h>
19
20 static int ssl_set_cert(CERT *c, X509 *x509);
21 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
22
23 #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
24 | SSL_EXT_CLIENT_HELLO \
25 | SSL_EXT_TLS1_2_SERVER_HELLO \
26 | SSL_EXT_IGNORE_ON_RESUMPTION)
27
28 int SSL_use_certificate(SSL *ssl, X509 *x)
29 {
30 int rv;
31 if (x == NULL) {
32 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
33 return 0;
34 }
35
36 rv = ssl_security_cert(ssl, NULL, x, 0, 1);
37 if (rv != 1) {
38 ERR_raise(ERR_LIB_SSL, rv);
39 return 0;
40 }
41
42 return ssl_set_cert(ssl->cert, x);
43 }
44
45 int SSL_use_certificate_file(SSL *ssl, const char *file, int type)
46 {
47 int j;
48 BIO *in;
49 int ret = 0;
50 X509 *cert = NULL, *x = NULL;
51
52 in = BIO_new(BIO_s_file());
53 if (in == NULL) {
54 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
55 goto end;
56 }
57
58 if (BIO_read_filename(in, file) <= 0) {
59 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
60 goto end;
61 }
62
63 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
64 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
65 goto end;
66 }
67 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
68 if (x == NULL) {
69 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
70 goto end;
71 }
72 if (type == SSL_FILETYPE_ASN1) {
73 j = ERR_R_ASN1_LIB;
74 cert = d2i_X509_bio(in, &x);
75 } else if (type == SSL_FILETYPE_PEM) {
76 j = ERR_R_PEM_LIB;
77 cert = PEM_read_bio_X509(in, &x, ssl->default_passwd_callback,
78 ssl->default_passwd_callback_userdata);
79 } else {
80 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
81 goto end;
82 }
83
84 if (cert == NULL) {
85 ERR_raise(ERR_LIB_SSL, j);
86 goto end;
87 }
88
89 ret = SSL_use_certificate(ssl, x);
90 end:
91 X509_free(x);
92 BIO_free(in);
93 return ret;
94 }
95
96 int SSL_use_certificate_ASN1(SSL *ssl, const unsigned char *d, int len)
97 {
98 X509 *x;
99 int ret;
100
101 x = X509_new_ex(ssl->ctx->libctx, ssl->ctx->propq);
102 if (x == NULL) {
103 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
104 return 0;
105 }
106
107 if (d2i_X509(&x, &d, (long)len)== NULL) {
108 X509_free(x);
109 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
110 return 0;
111 }
112
113 ret = SSL_use_certificate(ssl, x);
114 X509_free(x);
115 return ret;
116 }
117
118 #ifndef OPENSSL_NO_RSA
119 int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
120 {
121 EVP_PKEY *pkey;
122 int ret;
123
124 if (rsa == NULL) {
125 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
126 return 0;
127 }
128 if ((pkey = EVP_PKEY_new()) == NULL) {
129 ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
130 return 0;
131 }
132
133 RSA_up_ref(rsa);
134 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
135 RSA_free(rsa);
136 EVP_PKEY_free(pkey);
137 return 0;
138 }
139
140 ret = ssl_set_pkey(ssl->cert, pkey);
141 EVP_PKEY_free(pkey);
142 return ret;
143 }
144 #endif
145
146 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
147 {
148 size_t i;
149
150 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
151 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
152 return 0;
153 }
154
155 if (c->pkeys[i].x509 != NULL) {
156 EVP_PKEY *pktmp;
157 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
158 if (pktmp == NULL) {
159 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
160 return 0;
161 }
162 /*
163 * The return code from EVP_PKEY_copy_parameters is deliberately
164 * ignored. Some EVP_PKEY types cannot do this.
165 */
166 EVP_PKEY_copy_parameters(pktmp, pkey);
167 ERR_clear_error();
168
169 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
170 X509_free(c->pkeys[i].x509);
171 c->pkeys[i].x509 = NULL;
172 return 0;
173 }
174 }
175
176 EVP_PKEY_free(c->pkeys[i].privatekey);
177 EVP_PKEY_up_ref(pkey);
178 c->pkeys[i].privatekey = pkey;
179 c->key = &c->pkeys[i];
180 return 1;
181 }
182
183 #ifndef OPENSSL_NO_RSA
184 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
185 {
186 int j, ret = 0;
187 BIO *in;
188 RSA *rsa = NULL;
189
190 in = BIO_new(BIO_s_file());
191 if (in == NULL) {
192 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
193 goto end;
194 }
195
196 if (BIO_read_filename(in, file) <= 0) {
197 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
198 goto end;
199 }
200 if (type == SSL_FILETYPE_ASN1) {
201 j = ERR_R_ASN1_LIB;
202 rsa = d2i_RSAPrivateKey_bio(in, NULL);
203 } else if (type == SSL_FILETYPE_PEM) {
204 j = ERR_R_PEM_LIB;
205 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
206 ssl->default_passwd_callback,
207 ssl->default_passwd_callback_userdata);
208 } else {
209 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
210 goto end;
211 }
212 if (rsa == NULL) {
213 ERR_raise(ERR_LIB_SSL, j);
214 goto end;
215 }
216 ret = SSL_use_RSAPrivateKey(ssl, rsa);
217 RSA_free(rsa);
218 end:
219 BIO_free(in);
220 return ret;
221 }
222
223 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
224 {
225 int ret;
226 const unsigned char *p;
227 RSA *rsa;
228
229 p = d;
230 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
231 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
232 return 0;
233 }
234
235 ret = SSL_use_RSAPrivateKey(ssl, rsa);
236 RSA_free(rsa);
237 return ret;
238 }
239 #endif /* !OPENSSL_NO_RSA */
240
241 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
242 {
243 int ret;
244
245 if (pkey == NULL) {
246 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
247 return 0;
248 }
249 ret = ssl_set_pkey(ssl->cert, pkey);
250 return ret;
251 }
252
253 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
254 {
255 int j, ret = 0;
256 BIO *in;
257 EVP_PKEY *pkey = NULL;
258
259 in = BIO_new(BIO_s_file());
260 if (in == NULL) {
261 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
262 goto end;
263 }
264
265 if (BIO_read_filename(in, file) <= 0) {
266 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
267 goto end;
268 }
269 if (type == SSL_FILETYPE_PEM) {
270 j = ERR_R_PEM_LIB;
271 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
272 ssl->default_passwd_callback,
273 ssl->default_passwd_callback_userdata,
274 ssl->ctx->libctx,
275 ssl->ctx->propq);
276 } else if (type == SSL_FILETYPE_ASN1) {
277 j = ERR_R_ASN1_LIB;
278 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
279 ssl->ctx->propq);
280 } else {
281 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
282 goto end;
283 }
284 if (pkey == NULL) {
285 ERR_raise(ERR_LIB_SSL, j);
286 goto end;
287 }
288 ret = SSL_use_PrivateKey(ssl, pkey);
289 EVP_PKEY_free(pkey);
290 end:
291 BIO_free(in);
292 return ret;
293 }
294
295 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
296 long len)
297 {
298 int ret;
299 const unsigned char *p;
300 EVP_PKEY *pkey;
301
302 p = d;
303 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
304 ssl->ctx->propq)) == NULL) {
305 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
306 return 0;
307 }
308
309 ret = SSL_use_PrivateKey(ssl, pkey);
310 EVP_PKEY_free(pkey);
311 return ret;
312 }
313
314 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
315 {
316 int rv;
317 if (x == NULL) {
318 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
319 return 0;
320 }
321
322 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
323 if (rv != 1) {
324 ERR_raise(ERR_LIB_SSL, rv);
325 return 0;
326 }
327 return ssl_set_cert(ctx->cert, x);
328 }
329
330 static int ssl_set_cert(CERT *c, X509 *x)
331 {
332 EVP_PKEY *pkey;
333 size_t i;
334
335 pkey = X509_get0_pubkey(x);
336 if (pkey == NULL) {
337 ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
338 return 0;
339 }
340
341 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
342 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
343 return 0;
344 }
345 #ifndef OPENSSL_NO_EC
346 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
347 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
348 return 0;
349 }
350 #endif
351 if (c->pkeys[i].privatekey != NULL) {
352 /*
353 * The return code from EVP_PKEY_copy_parameters is deliberately
354 * ignored. Some EVP_PKEY types cannot do this.
355 */
356 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
357 ERR_clear_error();
358
359 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
360 /*
361 * don't fail for a cert/key mismatch, just free current private
362 * key (when switching to a different cert & key, first this
363 * function should be used, then ssl_set_pkey
364 */
365 EVP_PKEY_free(c->pkeys[i].privatekey);
366 c->pkeys[i].privatekey = NULL;
367 /* clear error queue */
368 ERR_clear_error();
369 }
370 }
371
372 X509_free(c->pkeys[i].x509);
373 X509_up_ref(x);
374 c->pkeys[i].x509 = x;
375 c->key = &(c->pkeys[i]);
376
377 return 1;
378 }
379
380 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
381 {
382 int j = SSL_R_BAD_VALUE;
383 BIO *in;
384 int ret = 0;
385 X509 *x = NULL, *cert = NULL;
386
387 in = BIO_new(BIO_s_file());
388 if (in == NULL) {
389 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
390 goto end;
391 }
392
393 if (BIO_read_filename(in, file) <= 0) {
394 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
395 goto end;
396 }
397 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
398 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
399 goto end;
400 }
401 x = X509_new_ex(ctx->libctx, ctx->propq);
402 if (x == NULL) {
403 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
404 goto end;
405 }
406 if (type == SSL_FILETYPE_ASN1) {
407 j = ERR_R_ASN1_LIB;
408 cert = d2i_X509_bio(in, &x);
409 } else if (type == SSL_FILETYPE_PEM) {
410 j = ERR_R_PEM_LIB;
411 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
412 ctx->default_passwd_callback_userdata);
413 }
414 if (cert == NULL) {
415 ERR_raise(ERR_LIB_SSL, j);
416 goto end;
417 }
418
419 ret = SSL_CTX_use_certificate(ctx, x);
420 end:
421 X509_free(x);
422 BIO_free(in);
423 return ret;
424 }
425
426 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
427 {
428 X509 *x;
429 int ret;
430
431 x = X509_new_ex(ctx->libctx, ctx->propq);
432 if (x == NULL) {
433 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
434 return 0;
435 }
436
437 if (d2i_X509(&x, &d, (long)len) == NULL) {
438 X509_free(x);
439 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
440 return 0;
441 }
442
443 ret = SSL_CTX_use_certificate(ctx, x);
444 X509_free(x);
445 return ret;
446 }
447
448 #ifndef OPENSSL_NO_RSA
449 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
450 {
451 int ret;
452 EVP_PKEY *pkey;
453
454 if (rsa == NULL) {
455 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
456 return 0;
457 }
458 if ((pkey = EVP_PKEY_new()) == NULL) {
459 ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
460 return 0;
461 }
462
463 RSA_up_ref(rsa);
464 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
465 RSA_free(rsa);
466 EVP_PKEY_free(pkey);
467 return 0;
468 }
469
470 ret = ssl_set_pkey(ctx->cert, pkey);
471 EVP_PKEY_free(pkey);
472 return ret;
473 }
474
475 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
476 {
477 int j, ret = 0;
478 BIO *in;
479 RSA *rsa = NULL;
480
481 in = BIO_new(BIO_s_file());
482 if (in == NULL) {
483 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
484 goto end;
485 }
486
487 if (BIO_read_filename(in, file) <= 0) {
488 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
489 goto end;
490 }
491 if (type == SSL_FILETYPE_ASN1) {
492 j = ERR_R_ASN1_LIB;
493 rsa = d2i_RSAPrivateKey_bio(in, NULL);
494 } else if (type == SSL_FILETYPE_PEM) {
495 j = ERR_R_PEM_LIB;
496 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
497 ctx->default_passwd_callback,
498 ctx->default_passwd_callback_userdata);
499 } else {
500 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
501 goto end;
502 }
503 if (rsa == NULL) {
504 ERR_raise(ERR_LIB_SSL, j);
505 goto end;
506 }
507 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
508 RSA_free(rsa);
509 end:
510 BIO_free(in);
511 return ret;
512 }
513
514 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
515 long len)
516 {
517 int ret;
518 const unsigned char *p;
519 RSA *rsa;
520
521 p = d;
522 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
523 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
524 return 0;
525 }
526
527 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
528 RSA_free(rsa);
529 return ret;
530 }
531 #endif /* !OPENSSL_NO_RSA */
532
533 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
534 {
535 if (pkey == NULL) {
536 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
537 return 0;
538 }
539 return ssl_set_pkey(ctx->cert, pkey);
540 }
541
542 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
543 {
544 int j, ret = 0;
545 BIO *in;
546 EVP_PKEY *pkey = NULL;
547
548 in = BIO_new(BIO_s_file());
549 if (in == NULL) {
550 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
551 goto end;
552 }
553
554 if (BIO_read_filename(in, file) <= 0) {
555 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
556 goto end;
557 }
558 if (type == SSL_FILETYPE_PEM) {
559 j = ERR_R_PEM_LIB;
560 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
561 ctx->default_passwd_callback,
562 ctx->default_passwd_callback_userdata,
563 ctx->libctx, ctx->propq);
564 } else if (type == SSL_FILETYPE_ASN1) {
565 j = ERR_R_ASN1_LIB;
566 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
567 } else {
568 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
569 goto end;
570 }
571 if (pkey == NULL) {
572 ERR_raise(ERR_LIB_SSL, j);
573 goto end;
574 }
575 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
576 EVP_PKEY_free(pkey);
577 end:
578 BIO_free(in);
579 return ret;
580 }
581
582 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
583 const unsigned char *d, long len)
584 {
585 int ret;
586 const unsigned char *p;
587 EVP_PKEY *pkey;
588
589 p = d;
590 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
591 ctx->propq)) == NULL) {
592 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
593 return 0;
594 }
595
596 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
597 EVP_PKEY_free(pkey);
598 return ret;
599 }
600
601 /*
602 * Read a file that contains our certificate in "PEM" format, possibly
603 * followed by a sequence of CA certificates that should be sent to the peer
604 * in the Certificate message.
605 */
606 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
607 {
608 BIO *in;
609 int ret = 0;
610 X509 *x = NULL;
611 pem_password_cb *passwd_callback;
612 void *passwd_callback_userdata;
613 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
614
615 ERR_clear_error(); /* clear error stack for
616 * SSL_CTX_use_certificate() */
617
618 if (ctx != NULL) {
619 passwd_callback = ctx->default_passwd_callback;
620 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
621 } else {
622 passwd_callback = ssl->default_passwd_callback;
623 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
624 }
625
626 in = BIO_new(BIO_s_file());
627 if (in == NULL) {
628 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
629 goto end;
630 }
631
632 if (BIO_read_filename(in, file) <= 0) {
633 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
634 goto end;
635 }
636
637 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
638 if (x == NULL) {
639 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
640 goto end;
641 }
642 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
643 passwd_callback_userdata) == NULL) {
644 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
645 goto end;
646 }
647
648 if (ctx)
649 ret = SSL_CTX_use_certificate(ctx, x);
650 else
651 ret = SSL_use_certificate(ssl, x);
652
653 if (ERR_peek_error() != 0)
654 ret = 0; /* Key/certificate mismatch doesn't imply
655 * ret==0 ... */
656 if (ret) {
657 /*
658 * If we could set up our certificate, now proceed to the CA
659 * certificates.
660 */
661 X509 *ca;
662 int r;
663 unsigned long err;
664
665 if (ctx)
666 r = SSL_CTX_clear_chain_certs(ctx);
667 else
668 r = SSL_clear_chain_certs(ssl);
669
670 if (r == 0) {
671 ret = 0;
672 goto end;
673 }
674
675 while (1) {
676 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
677 if (ca == NULL) {
678 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
679 goto end;
680 }
681 if (PEM_read_bio_X509(in, &ca, passwd_callback,
682 passwd_callback_userdata) != NULL) {
683 if (ctx)
684 r = SSL_CTX_add0_chain_cert(ctx, ca);
685 else
686 r = SSL_add0_chain_cert(ssl, ca);
687 /*
688 * Note that we must not free ca if it was successfully added to
689 * the chain (while we must free the main certificate, since its
690 * reference count is increased by SSL_CTX_use_certificate).
691 */
692 if (!r) {
693 X509_free(ca);
694 ret = 0;
695 goto end;
696 }
697 } else {
698 X509_free(ca);
699 break;
700 }
701 }
702 /* When the while loop ends, it's usually just EOF. */
703 err = ERR_peek_last_error();
704 if (ERR_GET_LIB(err) == ERR_LIB_PEM
705 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
706 ERR_clear_error();
707 else
708 ret = 0; /* some real error */
709 }
710
711 end:
712 X509_free(x);
713 BIO_free(in);
714 return ret;
715 }
716
717 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
718 {
719 return use_certificate_chain_file(ctx, NULL, file);
720 }
721
722 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
723 {
724 return use_certificate_chain_file(NULL, ssl, file);
725 }
726
727 static int serverinfo_find_extension(const unsigned char *serverinfo,
728 size_t serverinfo_length,
729 unsigned int extension_type,
730 const unsigned char **extension_data,
731 size_t *extension_length)
732 {
733 PACKET pkt, data;
734
735 *extension_data = NULL;
736 *extension_length = 0;
737 if (serverinfo == NULL || serverinfo_length == 0)
738 return -1;
739
740 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
741 return -1;
742
743 for (;;) {
744 unsigned int type = 0;
745 unsigned long context = 0;
746
747 /* end of serverinfo */
748 if (PACKET_remaining(&pkt) == 0)
749 return 0; /* Extension not found */
750
751 if (!PACKET_get_net_4(&pkt, &context)
752 || !PACKET_get_net_2(&pkt, &type)
753 || !PACKET_get_length_prefixed_2(&pkt, &data))
754 return -1;
755
756 if (type == extension_type) {
757 *extension_data = PACKET_data(&data);
758 *extension_length = PACKET_remaining(&data);;
759 return 1; /* Success */
760 }
761 }
762 /* Unreachable */
763 }
764
765 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
766 unsigned int context,
767 const unsigned char *in,
768 size_t inlen, X509 *x, size_t chainidx,
769 int *al, void *arg)
770 {
771
772 if (inlen != 0) {
773 *al = SSL_AD_DECODE_ERROR;
774 return 0;
775 }
776
777 return 1;
778 }
779
780 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
781 const unsigned char *in,
782 size_t inlen, int *al, void *arg)
783 {
784 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
785 arg);
786 }
787
788 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
789 unsigned int context,
790 const unsigned char **out,
791 size_t *outlen, X509 *x, size_t chainidx,
792 int *al, void *arg)
793 {
794 const unsigned char *serverinfo = NULL;
795 size_t serverinfo_length = 0;
796
797 /* We only support extensions for the first Certificate */
798 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
799 return 0;
800
801 /* Is there serverinfo data for the chosen server cert? */
802 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
803 &serverinfo_length)) != 0) {
804 /* Find the relevant extension from the serverinfo */
805 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
806 ext_type, out, outlen);
807 if (retval == -1) {
808 *al = SSL_AD_INTERNAL_ERROR;
809 return -1; /* Error */
810 }
811 if (retval == 0)
812 return 0; /* No extension found, don't send extension */
813 return 1; /* Send extension */
814 }
815 return 0; /* No serverinfo data found, don't send
816 * extension */
817 }
818
819 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
820 const unsigned char **out, size_t *outlen,
821 int *al, void *arg)
822 {
823 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
824 arg);
825 }
826
827 /*
828 * With a NULL context, this function just checks that the serverinfo data
829 * parses correctly. With a non-NULL context, it registers callbacks for
830 * the included extensions.
831 */
832 static int serverinfo_process_buffer(unsigned int version,
833 const unsigned char *serverinfo,
834 size_t serverinfo_length, SSL_CTX *ctx)
835 {
836 PACKET pkt;
837
838 if (serverinfo == NULL || serverinfo_length == 0)
839 return 0;
840
841 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
842 return 0;
843
844 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
845 return 0;
846
847 while (PACKET_remaining(&pkt)) {
848 unsigned long context = 0;
849 unsigned int ext_type = 0;
850 PACKET data;
851
852 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
853 || !PACKET_get_net_2(&pkt, &ext_type)
854 || !PACKET_get_length_prefixed_2(&pkt, &data))
855 return 0;
856
857 if (ctx == NULL)
858 continue;
859
860 /*
861 * The old style custom extensions API could be set separately for
862 * server/client, i.e. you could set one custom extension for a client,
863 * and *for the same extension in the same SSL_CTX* you could set a
864 * custom extension for the server as well. It seems quite weird to be
865 * setting a custom extension for both client and server in a single
866 * SSL_CTX - but theoretically possible. This isn't possible in the
867 * new API. Therefore, if we have V1 serverinfo we use the old API. We
868 * also use the old API even if we have V2 serverinfo but the context
869 * looks like an old style <= TLSv1.2 extension.
870 */
871 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
872 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
873 serverinfo_srv_add_cb,
874 NULL, NULL,
875 serverinfo_srv_parse_cb,
876 NULL))
877 return 0;
878 } else {
879 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
880 serverinfoex_srv_add_cb,
881 NULL, NULL,
882 serverinfoex_srv_parse_cb,
883 NULL))
884 return 0;
885 }
886 }
887
888 return 1;
889 }
890
891 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
892 const unsigned char *serverinfo,
893 size_t serverinfo_length)
894 {
895 unsigned char *new_serverinfo;
896
897 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
898 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
899 return 0;
900 }
901 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
902 NULL)) {
903 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
904 return 0;
905 }
906 if (ctx->cert->key == NULL) {
907 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
908 return 0;
909 }
910 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
911 serverinfo_length);
912 if (new_serverinfo == NULL) {
913 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
914 return 0;
915 }
916 ctx->cert->key->serverinfo = new_serverinfo;
917 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
918 ctx->cert->key->serverinfo_length = serverinfo_length;
919
920 /*
921 * Now that the serverinfo is validated and stored, go ahead and
922 * register callbacks.
923 */
924 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
925 ctx)) {
926 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
927 return 0;
928 }
929 return 1;
930 }
931
932 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
933 size_t serverinfo_length)
934 {
935 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
936 serverinfo_length);
937 }
938
939 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
940 {
941 unsigned char *serverinfo = NULL;
942 unsigned char *tmp;
943 size_t serverinfo_length = 0;
944 unsigned char *extension = 0;
945 long extension_length = 0;
946 char *name = NULL;
947 char *header = NULL;
948 static const char namePrefix1[] = "SERVERINFO FOR ";
949 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
950 unsigned int name_len;
951 int ret = 0;
952 BIO *bin = NULL;
953 size_t num_extensions = 0, contextoff = 0;
954
955 if (ctx == NULL || file == NULL) {
956 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
957 goto end;
958 }
959
960 bin = BIO_new(BIO_s_file());
961 if (bin == NULL) {
962 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
963 goto end;
964 }
965 if (BIO_read_filename(bin, file) <= 0) {
966 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
967 goto end;
968 }
969
970 for (num_extensions = 0;; num_extensions++) {
971 unsigned int version;
972
973 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
974 == 0) {
975 /*
976 * There must be at least one extension in this file
977 */
978 if (num_extensions == 0) {
979 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
980 goto end;
981 } else /* End of file, we're done */
982 break;
983 }
984 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
985 name_len = strlen(name);
986 if (name_len < sizeof(namePrefix1) - 1) {
987 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
988 goto end;
989 }
990 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
991 version = SSL_SERVERINFOV1;
992 } else {
993 if (name_len < sizeof(namePrefix2) - 1) {
994 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
995 goto end;
996 }
997 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
998 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
999 goto end;
1000 }
1001 version = SSL_SERVERINFOV2;
1002 }
1003 /*
1004 * Check that the decoded PEM data is plausible (valid length field)
1005 */
1006 if (version == SSL_SERVERINFOV1) {
1007 /* 4 byte header: 2 bytes type, 2 bytes len */
1008 if (extension_length < 4
1009 || (extension[2] << 8) + extension[3]
1010 != extension_length - 4) {
1011 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
1012 goto end;
1013 }
1014 /*
1015 * File does not have a context value so we must take account of
1016 * this later.
1017 */
1018 contextoff = 4;
1019 } else {
1020 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
1021 if (extension_length < 8
1022 || (extension[6] << 8) + extension[7]
1023 != extension_length - 8) {
1024 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
1025 goto end;
1026 }
1027 }
1028 /* Append the decoded extension to the serverinfo buffer */
1029 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
1030 + contextoff);
1031 if (tmp == NULL) {
1032 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1033 goto end;
1034 }
1035 serverinfo = tmp;
1036 if (contextoff > 0) {
1037 unsigned char *sinfo = serverinfo + serverinfo_length;
1038
1039 /* We know this only uses the last 2 bytes */
1040 sinfo[0] = 0;
1041 sinfo[1] = 0;
1042 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
1043 sinfo[3] = SYNTHV1CONTEXT & 0xff;
1044 }
1045 memcpy(serverinfo + serverinfo_length + contextoff,
1046 extension, extension_length);
1047 serverinfo_length += extension_length + contextoff;
1048
1049 OPENSSL_free(name);
1050 name = NULL;
1051 OPENSSL_free(header);
1052 header = NULL;
1053 OPENSSL_free(extension);
1054 extension = NULL;
1055 }
1056
1057 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
1058 serverinfo_length);
1059 end:
1060 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1061 OPENSSL_free(name);
1062 OPENSSL_free(header);
1063 OPENSSL_free(extension);
1064 OPENSSL_free(serverinfo);
1065 BIO_free(bin);
1066 return ret;
1067 }
1068
1069 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1070 STACK_OF(X509) *chain, int override)
1071 {
1072 int ret = 0;
1073 size_t i;
1074 int j;
1075 int rv;
1076 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
1077 STACK_OF(X509) *dup_chain = NULL;
1078 EVP_PKEY *pubkey = NULL;
1079
1080 /* Do all security checks before anything else */
1081 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
1082 if (rv != 1) {
1083 ERR_raise(ERR_LIB_SSL, rv);
1084 goto out;
1085 }
1086 for (j = 0; j < sk_X509_num(chain); j++) {
1087 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
1088 if (rv != 1) {
1089 ERR_raise(ERR_LIB_SSL, rv);
1090 goto out;
1091 }
1092 }
1093
1094 pubkey = X509_get_pubkey(x509); /* bumps reference */
1095 if (pubkey == NULL)
1096 goto out;
1097 if (privatekey == NULL) {
1098 privatekey = pubkey;
1099 } else {
1100 /* For RSA, which has no parameters, missing returns 0 */
1101 if (EVP_PKEY_missing_parameters(privatekey)) {
1102 if (EVP_PKEY_missing_parameters(pubkey)) {
1103 /* nobody has parameters? - error */
1104 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
1105 goto out;
1106 } else {
1107 /* copy to privatekey from pubkey */
1108 EVP_PKEY_copy_parameters(privatekey, pubkey);
1109 }
1110 } else if (EVP_PKEY_missing_parameters(pubkey)) {
1111 /* copy to pubkey from privatekey */
1112 EVP_PKEY_copy_parameters(pubkey, privatekey);
1113 } /* else both have parameters */
1114
1115 /* check that key <-> cert match */
1116 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
1117 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
1118 goto out;
1119 }
1120 }
1121 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
1122 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1123 goto out;
1124 }
1125
1126 if (!override && (c->pkeys[i].x509 != NULL
1127 || c->pkeys[i].privatekey != NULL
1128 || c->pkeys[i].chain != NULL)) {
1129 /* No override, and something already there */
1130 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
1131 goto out;
1132 }
1133
1134 if (chain != NULL) {
1135 dup_chain = X509_chain_up_ref(chain);
1136 if (dup_chain == NULL) {
1137 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
1138 goto out;
1139 }
1140 }
1141
1142 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
1143 c->pkeys[i].chain = dup_chain;
1144
1145 X509_free(c->pkeys[i].x509);
1146 X509_up_ref(x509);
1147 c->pkeys[i].x509 = x509;
1148
1149 EVP_PKEY_free(c->pkeys[i].privatekey);
1150 EVP_PKEY_up_ref(privatekey);
1151 c->pkeys[i].privatekey = privatekey;
1152
1153 c->key = &(c->pkeys[i]);
1154
1155 ret = 1;
1156 out:
1157 EVP_PKEY_free(pubkey);
1158 return ret;
1159 }
1160
1161 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
1162 STACK_OF(X509) *chain, int override)
1163 {
1164 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
1165 }
1166
1167 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
1168 STACK_OF(X509) *chain, int override)
1169 {
1170 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
1171 }