]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/ssl_rsa.c
Ensure legacy_asn1_ctrl_to_param can handle MDs not in the OBJ database
[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 static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
119 {
120 size_t i;
121
122 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
123 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
124 return 0;
125 }
126
127 if (c->pkeys[i].x509 != NULL) {
128 EVP_PKEY *pktmp;
129 pktmp = X509_get0_pubkey(c->pkeys[i].x509);
130 if (pktmp == NULL) {
131 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
132 return 0;
133 }
134 /*
135 * The return code from EVP_PKEY_copy_parameters is deliberately
136 * ignored. Some EVP_PKEY types cannot do this.
137 */
138 EVP_PKEY_copy_parameters(pktmp, pkey);
139 ERR_clear_error();
140
141 if (!X509_check_private_key(c->pkeys[i].x509, pkey)) {
142 X509_free(c->pkeys[i].x509);
143 c->pkeys[i].x509 = NULL;
144 return 0;
145 }
146 }
147
148 EVP_PKEY_free(c->pkeys[i].privatekey);
149 EVP_PKEY_up_ref(pkey);
150 c->pkeys[i].privatekey = pkey;
151 c->key = &c->pkeys[i];
152 return 1;
153 }
154
155 int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey)
156 {
157 int ret;
158
159 if (pkey == NULL) {
160 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
161 return 0;
162 }
163 ret = ssl_set_pkey(ssl->cert, pkey);
164 return ret;
165 }
166
167 int SSL_use_PrivateKey_file(SSL *ssl, const char *file, int type)
168 {
169 int j, ret = 0;
170 BIO *in;
171 EVP_PKEY *pkey = NULL;
172
173 in = BIO_new(BIO_s_file());
174 if (in == NULL) {
175 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
176 goto end;
177 }
178
179 if (BIO_read_filename(in, file) <= 0) {
180 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
181 goto end;
182 }
183 if (type == SSL_FILETYPE_PEM) {
184 j = ERR_R_PEM_LIB;
185 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
186 ssl->default_passwd_callback,
187 ssl->default_passwd_callback_userdata,
188 ssl->ctx->libctx,
189 ssl->ctx->propq);
190 } else if (type == SSL_FILETYPE_ASN1) {
191 j = ERR_R_ASN1_LIB;
192 pkey = d2i_PrivateKey_ex_bio(in, NULL, ssl->ctx->libctx,
193 ssl->ctx->propq);
194 } else {
195 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
196 goto end;
197 }
198 if (pkey == NULL) {
199 ERR_raise(ERR_LIB_SSL, j);
200 goto end;
201 }
202 ret = SSL_use_PrivateKey(ssl, pkey);
203 EVP_PKEY_free(pkey);
204 end:
205 BIO_free(in);
206 return ret;
207 }
208
209 int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const unsigned char *d,
210 long len)
211 {
212 int ret;
213 const unsigned char *p;
214 EVP_PKEY *pkey;
215
216 p = d;
217 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ssl->ctx->libctx,
218 ssl->ctx->propq)) == NULL) {
219 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
220 return 0;
221 }
222
223 ret = SSL_use_PrivateKey(ssl, pkey);
224 EVP_PKEY_free(pkey);
225 return ret;
226 }
227
228 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x)
229 {
230 int rv;
231 if (x == NULL) {
232 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
233 return 0;
234 }
235
236 rv = ssl_security_cert(NULL, ctx, x, 0, 1);
237 if (rv != 1) {
238 ERR_raise(ERR_LIB_SSL, rv);
239 return 0;
240 }
241 return ssl_set_cert(ctx->cert, x);
242 }
243
244 static int ssl_set_cert(CERT *c, X509 *x)
245 {
246 EVP_PKEY *pkey;
247 size_t i;
248
249 pkey = X509_get0_pubkey(x);
250 if (pkey == NULL) {
251 ERR_raise(ERR_LIB_SSL, SSL_R_X509_LIB);
252 return 0;
253 }
254
255 if (ssl_cert_lookup_by_pkey(pkey, &i) == NULL) {
256 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
257 return 0;
258 }
259 #ifndef OPENSSL_NO_EC
260 if (i == SSL_PKEY_ECC && !EVP_PKEY_can_sign(pkey)) {
261 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
262 return 0;
263 }
264 #endif
265 if (c->pkeys[i].privatekey != NULL) {
266 /*
267 * The return code from EVP_PKEY_copy_parameters is deliberately
268 * ignored. Some EVP_PKEY types cannot do this.
269 */
270 EVP_PKEY_copy_parameters(pkey, c->pkeys[i].privatekey);
271 ERR_clear_error();
272
273 if (!X509_check_private_key(x, c->pkeys[i].privatekey)) {
274 /*
275 * don't fail for a cert/key mismatch, just free current private
276 * key (when switching to a different cert & key, first this
277 * function should be used, then ssl_set_pkey
278 */
279 EVP_PKEY_free(c->pkeys[i].privatekey);
280 c->pkeys[i].privatekey = NULL;
281 /* clear error queue */
282 ERR_clear_error();
283 }
284 }
285
286 X509_free(c->pkeys[i].x509);
287 X509_up_ref(x);
288 c->pkeys[i].x509 = x;
289 c->key = &(c->pkeys[i]);
290
291 return 1;
292 }
293
294 int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type)
295 {
296 int j = SSL_R_BAD_VALUE;
297 BIO *in;
298 int ret = 0;
299 X509 *x = NULL, *cert = NULL;
300
301 in = BIO_new(BIO_s_file());
302 if (in == NULL) {
303 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
304 goto end;
305 }
306
307 if (BIO_read_filename(in, file) <= 0) {
308 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
309 goto end;
310 }
311 if (type != SSL_FILETYPE_ASN1 && type != SSL_FILETYPE_PEM) {
312 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
313 goto end;
314 }
315 x = X509_new_ex(ctx->libctx, ctx->propq);
316 if (x == NULL) {
317 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
318 goto end;
319 }
320 if (type == SSL_FILETYPE_ASN1) {
321 j = ERR_R_ASN1_LIB;
322 cert = d2i_X509_bio(in, &x);
323 } else if (type == SSL_FILETYPE_PEM) {
324 j = ERR_R_PEM_LIB;
325 cert = PEM_read_bio_X509(in, &x, ctx->default_passwd_callback,
326 ctx->default_passwd_callback_userdata);
327 }
328 if (cert == NULL) {
329 ERR_raise(ERR_LIB_SSL, j);
330 goto end;
331 }
332
333 ret = SSL_CTX_use_certificate(ctx, x);
334 end:
335 X509_free(x);
336 BIO_free(in);
337 return ret;
338 }
339
340 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const unsigned char *d)
341 {
342 X509 *x;
343 int ret;
344
345 x = X509_new_ex(ctx->libctx, ctx->propq);
346 if (x == NULL) {
347 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
348 return 0;
349 }
350
351 if (d2i_X509(&x, &d, (long)len) == NULL) {
352 X509_free(x);
353 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
354 return 0;
355 }
356
357 ret = SSL_CTX_use_certificate(ctx, x);
358 X509_free(x);
359 return ret;
360 }
361
362 int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey)
363 {
364 if (pkey == NULL) {
365 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
366 return 0;
367 }
368 return ssl_set_pkey(ctx->cert, pkey);
369 }
370
371 int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type)
372 {
373 int j, ret = 0;
374 BIO *in;
375 EVP_PKEY *pkey = NULL;
376
377 in = BIO_new(BIO_s_file());
378 if (in == NULL) {
379 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
380 goto end;
381 }
382
383 if (BIO_read_filename(in, file) <= 0) {
384 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
385 goto end;
386 }
387 if (type == SSL_FILETYPE_PEM) {
388 j = ERR_R_PEM_LIB;
389 pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
390 ctx->default_passwd_callback,
391 ctx->default_passwd_callback_userdata,
392 ctx->libctx, ctx->propq);
393 } else if (type == SSL_FILETYPE_ASN1) {
394 j = ERR_R_ASN1_LIB;
395 pkey = d2i_PrivateKey_ex_bio(in, NULL, ctx->libctx, ctx->propq);
396 } else {
397 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
398 goto end;
399 }
400 if (pkey == NULL) {
401 ERR_raise(ERR_LIB_SSL, j);
402 goto end;
403 }
404 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
405 EVP_PKEY_free(pkey);
406 end:
407 BIO_free(in);
408 return ret;
409 }
410
411 int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx,
412 const unsigned char *d, long len)
413 {
414 int ret;
415 const unsigned char *p;
416 EVP_PKEY *pkey;
417
418 p = d;
419 if ((pkey = d2i_PrivateKey_ex(type, NULL, &p, (long)len, ctx->libctx,
420 ctx->propq)) == NULL) {
421 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
422 return 0;
423 }
424
425 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
426 EVP_PKEY_free(pkey);
427 return ret;
428 }
429
430 /*
431 * Read a file that contains our certificate in "PEM" format, possibly
432 * followed by a sequence of CA certificates that should be sent to the peer
433 * in the Certificate message.
434 */
435 static int use_certificate_chain_file(SSL_CTX *ctx, SSL *ssl, const char *file)
436 {
437 BIO *in;
438 int ret = 0;
439 X509 *x = NULL;
440 pem_password_cb *passwd_callback;
441 void *passwd_callback_userdata;
442 SSL_CTX *real_ctx = (ssl == NULL) ? ctx : ssl->ctx;
443
444 ERR_clear_error(); /* clear error stack for
445 * SSL_CTX_use_certificate() */
446
447 if (ctx != NULL) {
448 passwd_callback = ctx->default_passwd_callback;
449 passwd_callback_userdata = ctx->default_passwd_callback_userdata;
450 } else {
451 passwd_callback = ssl->default_passwd_callback;
452 passwd_callback_userdata = ssl->default_passwd_callback_userdata;
453 }
454
455 in = BIO_new(BIO_s_file());
456 if (in == NULL) {
457 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
458 goto end;
459 }
460
461 if (BIO_read_filename(in, file) <= 0) {
462 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
463 goto end;
464 }
465
466 x = X509_new_ex(real_ctx->libctx, real_ctx->propq);
467 if (x == NULL) {
468 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
469 goto end;
470 }
471 if (PEM_read_bio_X509_AUX(in, &x, passwd_callback,
472 passwd_callback_userdata) == NULL) {
473 ERR_raise(ERR_LIB_SSL, ERR_R_PEM_LIB);
474 goto end;
475 }
476
477 if (ctx)
478 ret = SSL_CTX_use_certificate(ctx, x);
479 else
480 ret = SSL_use_certificate(ssl, x);
481
482 if (ERR_peek_error() != 0)
483 ret = 0; /* Key/certificate mismatch doesn't imply
484 * ret==0 ... */
485 if (ret) {
486 /*
487 * If we could set up our certificate, now proceed to the CA
488 * certificates.
489 */
490 X509 *ca;
491 int r;
492 unsigned long err;
493
494 if (ctx)
495 r = SSL_CTX_clear_chain_certs(ctx);
496 else
497 r = SSL_clear_chain_certs(ssl);
498
499 if (r == 0) {
500 ret = 0;
501 goto end;
502 }
503
504 while (1) {
505 ca = X509_new_ex(real_ctx->libctx, real_ctx->propq);
506 if (ca == NULL) {
507 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
508 goto end;
509 }
510 if (PEM_read_bio_X509(in, &ca, passwd_callback,
511 passwd_callback_userdata) != NULL) {
512 if (ctx)
513 r = SSL_CTX_add0_chain_cert(ctx, ca);
514 else
515 r = SSL_add0_chain_cert(ssl, ca);
516 /*
517 * Note that we must not free ca if it was successfully added to
518 * the chain (while we must free the main certificate, since its
519 * reference count is increased by SSL_CTX_use_certificate).
520 */
521 if (!r) {
522 X509_free(ca);
523 ret = 0;
524 goto end;
525 }
526 } else {
527 X509_free(ca);
528 break;
529 }
530 }
531 /* When the while loop ends, it's usually just EOF. */
532 err = ERR_peek_last_error();
533 if (ERR_GET_LIB(err) == ERR_LIB_PEM
534 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)
535 ERR_clear_error();
536 else
537 ret = 0; /* some real error */
538 }
539
540 end:
541 X509_free(x);
542 BIO_free(in);
543 return ret;
544 }
545
546 int SSL_CTX_use_certificate_chain_file(SSL_CTX *ctx, const char *file)
547 {
548 return use_certificate_chain_file(ctx, NULL, file);
549 }
550
551 int SSL_use_certificate_chain_file(SSL *ssl, const char *file)
552 {
553 return use_certificate_chain_file(NULL, ssl, file);
554 }
555
556 static int serverinfo_find_extension(const unsigned char *serverinfo,
557 size_t serverinfo_length,
558 unsigned int extension_type,
559 const unsigned char **extension_data,
560 size_t *extension_length)
561 {
562 PACKET pkt, data;
563
564 *extension_data = NULL;
565 *extension_length = 0;
566 if (serverinfo == NULL || serverinfo_length == 0)
567 return -1;
568
569 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
570 return -1;
571
572 for (;;) {
573 unsigned int type = 0;
574 unsigned long context = 0;
575
576 /* end of serverinfo */
577 if (PACKET_remaining(&pkt) == 0)
578 return 0; /* Extension not found */
579
580 if (!PACKET_get_net_4(&pkt, &context)
581 || !PACKET_get_net_2(&pkt, &type)
582 || !PACKET_get_length_prefixed_2(&pkt, &data))
583 return -1;
584
585 if (type == extension_type) {
586 *extension_data = PACKET_data(&data);
587 *extension_length = PACKET_remaining(&data);;
588 return 1; /* Success */
589 }
590 }
591 /* Unreachable */
592 }
593
594 static int serverinfoex_srv_parse_cb(SSL *s, unsigned int ext_type,
595 unsigned int context,
596 const unsigned char *in,
597 size_t inlen, X509 *x, size_t chainidx,
598 int *al, void *arg)
599 {
600
601 if (inlen != 0) {
602 *al = SSL_AD_DECODE_ERROR;
603 return 0;
604 }
605
606 return 1;
607 }
608
609 static int serverinfo_srv_parse_cb(SSL *s, unsigned int ext_type,
610 const unsigned char *in,
611 size_t inlen, int *al, void *arg)
612 {
613 return serverinfoex_srv_parse_cb(s, ext_type, 0, in, inlen, NULL, 0, al,
614 arg);
615 }
616
617 static int serverinfoex_srv_add_cb(SSL *s, unsigned int ext_type,
618 unsigned int context,
619 const unsigned char **out,
620 size_t *outlen, X509 *x, size_t chainidx,
621 int *al, void *arg)
622 {
623 const unsigned char *serverinfo = NULL;
624 size_t serverinfo_length = 0;
625
626 /* We only support extensions for the first Certificate */
627 if ((context & SSL_EXT_TLS1_3_CERTIFICATE) != 0 && chainidx > 0)
628 return 0;
629
630 /* Is there serverinfo data for the chosen server cert? */
631 if ((ssl_get_server_cert_serverinfo(s, &serverinfo,
632 &serverinfo_length)) != 0) {
633 /* Find the relevant extension from the serverinfo */
634 int retval = serverinfo_find_extension(serverinfo, serverinfo_length,
635 ext_type, out, outlen);
636 if (retval == -1) {
637 *al = SSL_AD_INTERNAL_ERROR;
638 return -1; /* Error */
639 }
640 if (retval == 0)
641 return 0; /* No extension found, don't send extension */
642 return 1; /* Send extension */
643 }
644 return 0; /* No serverinfo data found, don't send
645 * extension */
646 }
647
648 static int serverinfo_srv_add_cb(SSL *s, unsigned int ext_type,
649 const unsigned char **out, size_t *outlen,
650 int *al, void *arg)
651 {
652 return serverinfoex_srv_add_cb(s, ext_type, 0, out, outlen, NULL, 0, al,
653 arg);
654 }
655
656 /*
657 * With a NULL context, this function just checks that the serverinfo data
658 * parses correctly. With a non-NULL context, it registers callbacks for
659 * the included extensions.
660 */
661 static int serverinfo_process_buffer(unsigned int version,
662 const unsigned char *serverinfo,
663 size_t serverinfo_length, SSL_CTX *ctx)
664 {
665 PACKET pkt;
666
667 if (serverinfo == NULL || serverinfo_length == 0)
668 return 0;
669
670 if (version != SSL_SERVERINFOV1 && version != SSL_SERVERINFOV2)
671 return 0;
672
673 if (!PACKET_buf_init(&pkt, serverinfo, serverinfo_length))
674 return 0;
675
676 while (PACKET_remaining(&pkt)) {
677 unsigned long context = 0;
678 unsigned int ext_type = 0;
679 PACKET data;
680
681 if ((version == SSL_SERVERINFOV2 && !PACKET_get_net_4(&pkt, &context))
682 || !PACKET_get_net_2(&pkt, &ext_type)
683 || !PACKET_get_length_prefixed_2(&pkt, &data))
684 return 0;
685
686 if (ctx == NULL)
687 continue;
688
689 /*
690 * The old style custom extensions API could be set separately for
691 * server/client, i.e. you could set one custom extension for a client,
692 * and *for the same extension in the same SSL_CTX* you could set a
693 * custom extension for the server as well. It seems quite weird to be
694 * setting a custom extension for both client and server in a single
695 * SSL_CTX - but theoretically possible. This isn't possible in the
696 * new API. Therefore, if we have V1 serverinfo we use the old API. We
697 * also use the old API even if we have V2 serverinfo but the context
698 * looks like an old style <= TLSv1.2 extension.
699 */
700 if (version == SSL_SERVERINFOV1 || context == SYNTHV1CONTEXT) {
701 if (!SSL_CTX_add_server_custom_ext(ctx, ext_type,
702 serverinfo_srv_add_cb,
703 NULL, NULL,
704 serverinfo_srv_parse_cb,
705 NULL))
706 return 0;
707 } else {
708 if (!SSL_CTX_add_custom_ext(ctx, ext_type, context,
709 serverinfoex_srv_add_cb,
710 NULL, NULL,
711 serverinfoex_srv_parse_cb,
712 NULL))
713 return 0;
714 }
715 }
716
717 return 1;
718 }
719
720 int SSL_CTX_use_serverinfo_ex(SSL_CTX *ctx, unsigned int version,
721 const unsigned char *serverinfo,
722 size_t serverinfo_length)
723 {
724 unsigned char *new_serverinfo;
725
726 if (ctx == NULL || serverinfo == NULL || serverinfo_length == 0) {
727 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
728 return 0;
729 }
730 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
731 NULL)) {
732 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
733 return 0;
734 }
735 if (ctx->cert->key == NULL) {
736 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
737 return 0;
738 }
739 new_serverinfo = OPENSSL_realloc(ctx->cert->key->serverinfo,
740 serverinfo_length);
741 if (new_serverinfo == NULL) {
742 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
743 return 0;
744 }
745 ctx->cert->key->serverinfo = new_serverinfo;
746 memcpy(ctx->cert->key->serverinfo, serverinfo, serverinfo_length);
747 ctx->cert->key->serverinfo_length = serverinfo_length;
748
749 /*
750 * Now that the serverinfo is validated and stored, go ahead and
751 * register callbacks.
752 */
753 if (!serverinfo_process_buffer(version, serverinfo, serverinfo_length,
754 ctx)) {
755 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SERVERINFO_DATA);
756 return 0;
757 }
758 return 1;
759 }
760
761 int SSL_CTX_use_serverinfo(SSL_CTX *ctx, const unsigned char *serverinfo,
762 size_t serverinfo_length)
763 {
764 return SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV1, serverinfo,
765 serverinfo_length);
766 }
767
768 int SSL_CTX_use_serverinfo_file(SSL_CTX *ctx, const char *file)
769 {
770 unsigned char *serverinfo = NULL;
771 unsigned char *tmp;
772 size_t serverinfo_length = 0;
773 unsigned char *extension = 0;
774 long extension_length = 0;
775 char *name = NULL;
776 char *header = NULL;
777 static const char namePrefix1[] = "SERVERINFO FOR ";
778 static const char namePrefix2[] = "SERVERINFOV2 FOR ";
779 unsigned int name_len;
780 int ret = 0;
781 BIO *bin = NULL;
782 size_t num_extensions = 0, contextoff = 0;
783
784 if (ctx == NULL || file == NULL) {
785 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
786 goto end;
787 }
788
789 bin = BIO_new(BIO_s_file());
790 if (bin == NULL) {
791 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
792 goto end;
793 }
794 if (BIO_read_filename(bin, file) <= 0) {
795 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
796 goto end;
797 }
798
799 for (num_extensions = 0;; num_extensions++) {
800 unsigned int version;
801
802 if (PEM_read_bio(bin, &name, &header, &extension, &extension_length)
803 == 0) {
804 /*
805 * There must be at least one extension in this file
806 */
807 if (num_extensions == 0) {
808 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PEM_EXTENSIONS);
809 goto end;
810 } else /* End of file, we're done */
811 break;
812 }
813 /* Check that PEM name starts with "BEGIN SERVERINFO FOR " */
814 name_len = strlen(name);
815 if (name_len < sizeof(namePrefix1) - 1) {
816 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
817 goto end;
818 }
819 if (strncmp(name, namePrefix1, sizeof(namePrefix1) - 1) == 0) {
820 version = SSL_SERVERINFOV1;
821 } else {
822 if (name_len < sizeof(namePrefix2) - 1) {
823 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_TOO_SHORT);
824 goto end;
825 }
826 if (strncmp(name, namePrefix2, sizeof(namePrefix2) - 1) != 0) {
827 ERR_raise(ERR_LIB_SSL, SSL_R_PEM_NAME_BAD_PREFIX);
828 goto end;
829 }
830 version = SSL_SERVERINFOV2;
831 }
832 /*
833 * Check that the decoded PEM data is plausible (valid length field)
834 */
835 if (version == SSL_SERVERINFOV1) {
836 /* 4 byte header: 2 bytes type, 2 bytes len */
837 if (extension_length < 4
838 || (extension[2] << 8) + extension[3]
839 != extension_length - 4) {
840 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
841 goto end;
842 }
843 /*
844 * File does not have a context value so we must take account of
845 * this later.
846 */
847 contextoff = 4;
848 } else {
849 /* 8 byte header: 4 bytes context, 2 bytes type, 2 bytes len */
850 if (extension_length < 8
851 || (extension[6] << 8) + extension[7]
852 != extension_length - 8) {
853 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA);
854 goto end;
855 }
856 }
857 /* Append the decoded extension to the serverinfo buffer */
858 tmp = OPENSSL_realloc(serverinfo, serverinfo_length + extension_length
859 + contextoff);
860 if (tmp == NULL) {
861 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
862 goto end;
863 }
864 serverinfo = tmp;
865 if (contextoff > 0) {
866 unsigned char *sinfo = serverinfo + serverinfo_length;
867
868 /* We know this only uses the last 2 bytes */
869 sinfo[0] = 0;
870 sinfo[1] = 0;
871 sinfo[2] = (SYNTHV1CONTEXT >> 8) & 0xff;
872 sinfo[3] = SYNTHV1CONTEXT & 0xff;
873 }
874 memcpy(serverinfo + serverinfo_length + contextoff,
875 extension, extension_length);
876 serverinfo_length += extension_length + contextoff;
877
878 OPENSSL_free(name);
879 name = NULL;
880 OPENSSL_free(header);
881 header = NULL;
882 OPENSSL_free(extension);
883 extension = NULL;
884 }
885
886 ret = SSL_CTX_use_serverinfo_ex(ctx, SSL_SERVERINFOV2, serverinfo,
887 serverinfo_length);
888 end:
889 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
890 OPENSSL_free(name);
891 OPENSSL_free(header);
892 OPENSSL_free(extension);
893 OPENSSL_free(serverinfo);
894 BIO_free(bin);
895 return ret;
896 }
897
898 static int ssl_set_cert_and_key(SSL *ssl, SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
899 STACK_OF(X509) *chain, int override)
900 {
901 int ret = 0;
902 size_t i;
903 int j;
904 int rv;
905 CERT *c = ssl != NULL ? ssl->cert : ctx->cert;
906 STACK_OF(X509) *dup_chain = NULL;
907 EVP_PKEY *pubkey = NULL;
908
909 /* Do all security checks before anything else */
910 rv = ssl_security_cert(ssl, ctx, x509, 0, 1);
911 if (rv != 1) {
912 ERR_raise(ERR_LIB_SSL, rv);
913 goto out;
914 }
915 for (j = 0; j < sk_X509_num(chain); j++) {
916 rv = ssl_security_cert(ssl, ctx, sk_X509_value(chain, j), 0, 0);
917 if (rv != 1) {
918 ERR_raise(ERR_LIB_SSL, rv);
919 goto out;
920 }
921 }
922
923 pubkey = X509_get_pubkey(x509); /* bumps reference */
924 if (pubkey == NULL)
925 goto out;
926 if (privatekey == NULL) {
927 privatekey = pubkey;
928 } else {
929 /* For RSA, which has no parameters, missing returns 0 */
930 if (EVP_PKEY_missing_parameters(privatekey)) {
931 if (EVP_PKEY_missing_parameters(pubkey)) {
932 /* nobody has parameters? - error */
933 ERR_raise(ERR_LIB_SSL, SSL_R_MISSING_PARAMETERS);
934 goto out;
935 } else {
936 /* copy to privatekey from pubkey */
937 EVP_PKEY_copy_parameters(privatekey, pubkey);
938 }
939 } else if (EVP_PKEY_missing_parameters(pubkey)) {
940 /* copy to pubkey from privatekey */
941 EVP_PKEY_copy_parameters(pubkey, privatekey);
942 } /* else both have parameters */
943
944 /* check that key <-> cert match */
945 if (EVP_PKEY_eq(pubkey, privatekey) != 1) {
946 ERR_raise(ERR_LIB_SSL, SSL_R_PRIVATE_KEY_MISMATCH);
947 goto out;
948 }
949 }
950 if (ssl_cert_lookup_by_pkey(pubkey, &i) == NULL) {
951 ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
952 goto out;
953 }
954
955 if (!override && (c->pkeys[i].x509 != NULL
956 || c->pkeys[i].privatekey != NULL
957 || c->pkeys[i].chain != NULL)) {
958 /* No override, and something already there */
959 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_REPLACING_CERTIFICATE);
960 goto out;
961 }
962
963 if (chain != NULL) {
964 dup_chain = X509_chain_up_ref(chain);
965 if (dup_chain == NULL) {
966 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
967 goto out;
968 }
969 }
970
971 sk_X509_pop_free(c->pkeys[i].chain, X509_free);
972 c->pkeys[i].chain = dup_chain;
973
974 X509_free(c->pkeys[i].x509);
975 X509_up_ref(x509);
976 c->pkeys[i].x509 = x509;
977
978 EVP_PKEY_free(c->pkeys[i].privatekey);
979 EVP_PKEY_up_ref(privatekey);
980 c->pkeys[i].privatekey = privatekey;
981
982 c->key = &(c->pkeys[i]);
983
984 ret = 1;
985 out:
986 EVP_PKEY_free(pubkey);
987 return ret;
988 }
989
990 int SSL_use_cert_and_key(SSL *ssl, X509 *x509, EVP_PKEY *privatekey,
991 STACK_OF(X509) *chain, int override)
992 {
993 return ssl_set_cert_and_key(ssl, NULL, x509, privatekey, chain, override);
994 }
995
996 int SSL_CTX_use_cert_and_key(SSL_CTX *ctx, X509 *x509, EVP_PKEY *privatekey,
997 STACK_OF(X509) *chain, int override)
998 {
999 return ssl_set_cert_and_key(NULL, ctx, x509, privatekey, chain, override);
1000 }