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