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