Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
| 114 | |
David Benjamin | 443a1f6 | 2015-09-04 15:05:05 -0400 | [diff] [blame^] | 115 | #include <openssl/ssl.h> |
| 116 | |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 117 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | #include <openssl/bn.h> |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 120 | #include <openssl/buf.h> |
David Benjamin | 680ca96 | 2015-06-18 12:37:23 -0400 | [diff] [blame] | 121 | #include <openssl/ec_key.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | #include <openssl/dh.h> |
| 123 | #include <openssl/err.h> |
| 124 | #include <openssl/mem.h> |
David Benjamin | 680ca96 | 2015-06-18 12:37:23 -0400 | [diff] [blame] | 125 | #include <openssl/x509.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | #include <openssl/x509v3.h> |
| 127 | |
| 128 | #include "../crypto/dh/internal.h" |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 129 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 130 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 133 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 134 | /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the |
| 135 | * reserved app_data slot. Before ex_data was introduced, app_data was used. |
| 136 | * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data| |
| 137 | * works. */ |
| 138 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 139 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 141 | CERT *ssl_cert_new(void) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 142 | CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 143 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 144 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 145 | return NULL; |
| 146 | } |
| 147 | memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | return ret; |
| 150 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 152 | CERT *ssl_cert_dup(CERT *cert) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 153 | CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 154 | if (ret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 158 | memset(ret, 0, sizeof(CERT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 160 | ret->mask_k = cert->mask_k; |
| 161 | ret->mask_a = cert->mask_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | if (cert->dh_tmp != NULL) { |
| 164 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 165 | if (ret->dh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 166 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 167 | goto err; |
| 168 | } |
| 169 | if (cert->dh_tmp->priv_key) { |
| 170 | BIGNUM *b = BN_dup(cert->dh_tmp->priv_key); |
| 171 | if (!b) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 172 | OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 173 | goto err; |
| 174 | } |
| 175 | ret->dh_tmp->priv_key = b; |
| 176 | } |
| 177 | if (cert->dh_tmp->pub_key) { |
| 178 | BIGNUM *b = BN_dup(cert->dh_tmp->pub_key); |
| 179 | if (!b) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 180 | OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 181 | goto err; |
| 182 | } |
| 183 | ret->dh_tmp->pub_key = b; |
| 184 | } |
| 185 | } |
| 186 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 188 | ret->ecdh_nid = cert->ecdh_nid; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 189 | ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 191 | if (cert->x509 != NULL) { |
| 192 | ret->x509 = X509_up_ref(cert->x509); |
| 193 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 195 | if (cert->privatekey != NULL) { |
| 196 | ret->privatekey = EVP_PKEY_up_ref(cert->privatekey); |
| 197 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 199 | if (cert->chain) { |
| 200 | ret->chain = X509_chain_up_ref(cert->chain); |
| 201 | if (!ret->chain) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 202 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 203 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 207 | ret->cert_cb = cert->cert_cb; |
| 208 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 209 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 210 | return ret; |
| 211 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 212 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 213 | ssl_cert_free(ret); |
| 214 | return NULL; |
| 215 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
| 217 | /* Free up and clear all certificates and chains */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 218 | void ssl_cert_clear_certs(CERT *cert) { |
| 219 | if (cert == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 223 | X509_free(cert->x509); |
| 224 | cert->x509 = NULL; |
| 225 | EVP_PKEY_free(cert->privatekey); |
| 226 | cert->privatekey = NULL; |
| 227 | sk_X509_pop_free(cert->chain, X509_free); |
| 228 | cert->chain = NULL; |
David Benjamin | 71d2e54 | 2015-07-06 00:30:25 -0400 | [diff] [blame] | 229 | cert->key_method = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 230 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | void ssl_cert_free(CERT *c) { |
| 233 | if (c == NULL) { |
| 234 | return; |
| 235 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 237 | DH_free(c->dh_tmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 239 | ssl_cert_clear_certs(c); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 240 | OPENSSL_free(c->peer_sigalgs); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 241 | OPENSSL_free(c->shared_sigalgs); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 242 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 243 | OPENSSL_free(c); |
| 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 246 | int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 247 | sk_X509_pop_free(cert->chain, X509_free); |
| 248 | cert->chain = chain; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 252 | int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 253 | STACK_OF(X509) *dchain; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 254 | if (chain == NULL) { |
| 255 | return ssl_cert_set0_chain(cert, NULL); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | dchain = X509_chain_up_ref(chain); |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 259 | if (dchain == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 260 | return 0; |
| 261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 263 | if (!ssl_cert_set0_chain(cert, dchain)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 264 | sk_X509_pop_free(dchain, X509_free); |
| 265 | return 0; |
| 266 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | return 1; |
| 269 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 271 | int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 272 | if (cert->chain == NULL) { |
| 273 | cert->chain = sk_X509_new_null(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 274 | } |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 275 | if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 279 | return 1; |
| 280 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 282 | int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 283 | if (!ssl_cert_add0_chain_cert(cert, x509)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | return 0; |
| 285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 287 | X509_up_ref(x509); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 288 | return 1; |
| 289 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 292 | c->cert_cb = cb; |
| 293 | c->cert_cb_arg = arg; |
| 294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 296 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | X509 *x; |
| 298 | int i; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 299 | X509_STORE_CTX ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 300 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | if (sk == NULL || sk_X509_num(sk) == 0) { |
| 302 | return 0; |
| 303 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 305 | x = sk_X509_value(sk, 0); |
David Benjamin | d27441a | 2015-08-09 11:05:17 -0400 | [diff] [blame] | 306 | if (!X509_STORE_CTX_init(&ctx, s->ctx->cert_store, x, sk)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 307 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 312 | /* We need to inherit the verify parameters. These can be determined by the |
| 313 | * context: if its a server it will verify SSL client certificates or vice |
| 314 | * versa. */ |
| 315 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 317 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
| 318 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | if (s->verify_callback) { |
| 321 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); |
| 322 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 324 | if (s->ctx->app_verify_callback != NULL) { |
| 325 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
| 326 | } else { |
| 327 | i = X509_verify_cert(&ctx); |
| 328 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | s->verify_result = ctx.error; |
| 331 | X509_STORE_CTX_cleanup(&ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 332 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | return i; |
| 334 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 336 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 337 | STACK_OF(X509_NAME) *name_list) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 338 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 339 | *ca_list = name_list; |
| 340 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 341 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 342 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 343 | size_t i; |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 344 | STACK_OF(X509_NAME) *ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 345 | X509_NAME *name; |
| 346 | |
| 347 | ret = sk_X509_NAME_new_null(); |
| 348 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 349 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
| 350 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
| 351 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 352 | return NULL; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 359 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 360 | set_client_CA_list(&(s->client_CA), name_list); |
| 361 | } |
| 362 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 363 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 364 | set_client_CA_list(&(ctx->client_CA), name_list); |
| 365 | } |
| 366 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 367 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 368 | return ctx->client_CA; |
| 369 | } |
| 370 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 371 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 372 | if (s->server) { |
| 373 | if (s->client_CA != NULL) { |
| 374 | return s->client_CA; |
| 375 | } else { |
| 376 | return s->ctx->client_CA; |
| 377 | } |
| 378 | } else { |
| 379 | if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) { |
| 380 | return s->s3->tmp.ca_names; |
| 381 | } else { |
| 382 | return NULL; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 387 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 388 | X509_NAME *name; |
| 389 | |
| 390 | if (x == NULL) { |
| 391 | return 0; |
| 392 | } |
| 393 | if (*sk == NULL) { |
| 394 | *sk = sk_X509_NAME_new_null(); |
| 395 | if (*sk == NULL) { |
| 396 | return 0; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | name = X509_NAME_dup(X509_get_subject_name(x)); |
| 401 | if (name == NULL) { |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | if (!sk_X509_NAME_push(*sk, name)) { |
| 406 | X509_NAME_free(name); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | int SSL_add_client_CA(SSL *ssl, X509 *x) { |
| 414 | return add_client_CA(&(ssl->client_CA), x); |
| 415 | } |
| 416 | |
| 417 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) { |
| 418 | return add_client_CA(&(ctx->client_CA), x); |
| 419 | } |
| 420 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | /* Add a certificate to a BUF_MEM structure */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 422 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) { |
| 423 | int n; |
| 424 | uint8_t *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 426 | n = i2d_X509(x, NULL); |
| 427 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 428 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | p = (uint8_t *)&(buf->data[*l]); |
| 432 | l2n3(n, p); |
| 433 | i2d_X509(x, &p); |
| 434 | *l += n + 3; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 436 | return 1; |
| 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 439 | /* Add certificate chain to internal SSL BUF_MEM structure. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 440 | int ssl_add_cert_chain(SSL *ssl, unsigned long *l) { |
| 441 | CERT *cert = ssl->cert; |
| 442 | BUF_MEM *buf = ssl->init_buf; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 443 | int no_chain = 0; |
| 444 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 446 | X509 *x = cert->x509; |
| 447 | STACK_OF(X509) *chain = cert->chain; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 449 | if (x == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 450 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 451 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 454 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | no_chain = 1; |
| 456 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 458 | if (no_chain) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 459 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 460 | return 0; |
| 461 | } |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 462 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 463 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 464 | x = sk_X509_value(chain, i); |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 465 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 466 | return 0; |
| 467 | } |
| 468 | } |
| 469 | } else { |
| 470 | X509_STORE_CTX xs_ctx; |
| 471 | |
David Benjamin | d27441a | 2015-08-09 11:05:17 -0400 | [diff] [blame] | 472 | if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 473 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
David Benjamin | 605641e | 2015-05-03 15:14:04 -0400 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | X509_verify_cert(&xs_ctx); |
| 477 | /* Don't leave errors in the queue */ |
| 478 | ERR_clear_error(); |
| 479 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 480 | x = sk_X509_value(xs_ctx.chain, i); |
| 481 | |
| 482 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 483 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 484 | return 0; |
| 485 | } |
| 486 | } |
| 487 | X509_STORE_CTX_cleanup(&xs_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | return 1; |
| 491 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | |
David Benjamin | 11c0f8e | 2015-07-06 00:18:15 -0400 | [diff] [blame] | 493 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 494 | return ssl_cert_set0_chain(ctx->cert, chain); |
| 495 | } |
| 496 | |
| 497 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 498 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 499 | } |
| 500 | |
| 501 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 502 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 503 | } |
| 504 | |
| 505 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 506 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 507 | } |
| 508 | |
| 509 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 510 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 511 | } |
| 512 | |
| 513 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 514 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 515 | } |
| 516 | |
| 517 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 518 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 519 | } |
| 520 | |
| 521 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 522 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 523 | } |
| 524 | |
| 525 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 526 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 527 | } |
| 528 | |
| 529 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 530 | return SSL_CTX_set0_chain(ctx, NULL); |
| 531 | } |
| 532 | |
| 533 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 534 | return SSL_CTX_clear_chain_certs(ctx); |
| 535 | } |
| 536 | |
| 537 | int SSL_clear_chain_certs(SSL *ssl) { |
| 538 | return SSL_set0_chain(ssl, NULL); |
| 539 | } |
| 540 | |
| 541 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 542 | *out_chain = ctx->cert->chain; |
| 543 | return 1; |
| 544 | } |
| 545 | |
| 546 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 547 | STACK_OF(X509) **out_chain) { |
| 548 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 549 | } |
| 550 | |
| 551 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 552 | *out_chain = ssl->cert->chain; |
| 553 | return 1; |
| 554 | } |