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