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