Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 115 | #include <openssl/ssl.h> |
| 116 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 117 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | #include <openssl/bn.h> |
| 120 | #include <openssl/buf.h> |
Adam Langley | 56d2503 | 2015-06-23 16:20:13 -0700 | [diff] [blame] | 121 | #include <openssl/ec_key.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 122 | #include <openssl/dh.h> |
| 123 | #include <openssl/err.h> |
| 124 | #include <openssl/mem.h> |
Adam Langley | 56d2503 | 2015-06-23 16:20:13 -0700 | [diff] [blame] | 125 | #include <openssl/x509.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 126 | #include <openssl/x509v3.h> |
| 127 | |
| 128 | #include "../crypto/dh/internal.h" |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 129 | #include "../crypto/internal.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 130 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 131 | |
| 132 | |
| 133 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [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 | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | CERT *ssl_cert_new(void) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 142 | CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 143 | if (ret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 144 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 145 | return NULL; |
| 146 | } |
| 147 | memset(ret, 0, sizeof(CERT)); |
| 148 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | CERT *ssl_cert_dup(CERT *cert) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 153 | CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 154 | if (ret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 158 | memset(ret, 0, sizeof(CERT)); |
| 159 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | ret->mask_k = cert->mask_k; |
| 161 | ret->mask_a = cert->mask_a; |
| 162 | |
| 163 | if (cert->dh_tmp != NULL) { |
| 164 | ret->dh_tmp = DHparams_dup(cert->dh_tmp); |
| 165 | if (ret->dh_tmp == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 166 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | goto err; |
| 168 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | } |
| 170 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
| 171 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 172 | if (cert->x509 != NULL) { |
| 173 | ret->x509 = X509_up_ref(cert->x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 176 | if (cert->privatekey != NULL) { |
| 177 | ret->privatekey = EVP_PKEY_up_ref(cert->privatekey); |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 180 | if (cert->chain) { |
| 181 | ret->chain = X509_chain_up_ref(cert->chain); |
| 182 | if (!ret->chain) { |
| 183 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 184 | goto err; |
| 185 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 188 | ret->cert_cb = cert->cert_cb; |
| 189 | ret->cert_cb_arg = cert->cert_cb_arg; |
| 190 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 191 | return ret; |
| 192 | |
| 193 | err: |
| 194 | ssl_cert_free(ret); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | /* Free up and clear all certificates and chains */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 199 | void ssl_cert_clear_certs(CERT *cert) { |
| 200 | if (cert == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 204 | X509_free(cert->x509); |
| 205 | cert->x509 = NULL; |
| 206 | EVP_PKEY_free(cert->privatekey); |
| 207 | cert->privatekey = NULL; |
| 208 | sk_X509_pop_free(cert->chain, X509_free); |
| 209 | cert->chain = NULL; |
| 210 | cert->key_method = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void ssl_cert_free(CERT *c) { |
| 214 | if (c == NULL) { |
| 215 | return; |
| 216 | } |
| 217 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 218 | DH_free(c->dh_tmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 219 | |
| 220 | ssl_cert_clear_certs(c); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 221 | OPENSSL_free(c->peer_sigalgs); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 222 | OPENSSL_free(c->digest_nids); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | |
| 224 | OPENSSL_free(c); |
| 225 | } |
| 226 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 227 | int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 228 | sk_X509_pop_free(cert->chain, X509_free); |
| 229 | cert->chain = chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 230 | return 1; |
| 231 | } |
| 232 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 233 | int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 234 | STACK_OF(X509) *dchain; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 235 | if (chain == NULL) { |
| 236 | return ssl_cert_set0_chain(cert, NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | dchain = X509_chain_up_ref(chain); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 240 | if (dchain == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 244 | if (!ssl_cert_set0_chain(cert, dchain)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 245 | sk_X509_pop_free(dchain, X509_free); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | return 1; |
| 250 | } |
| 251 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 252 | int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 253 | if (cert->chain == NULL) { |
| 254 | cert->chain = sk_X509_new_null(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 256 | if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 263 | int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 264 | if (!ssl_cert_add0_chain_cert(cert, x509)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 268 | X509_up_ref(x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | return 1; |
| 270 | } |
| 271 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 272 | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 273 | c->cert_cb = cb; |
| 274 | c->cert_cb_arg = arg; |
| 275 | } |
| 276 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 277 | int ssl_verify_cert_chain(SSL *ssl, STACK_OF(X509) *cert_chain) { |
| 278 | if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) { |
| 279 | return 0; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 282 | X509 *leaf = sk_X509_value(cert_chain, 0); |
| 283 | int ret = 0; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 284 | X509_STORE_CTX ctx; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 285 | if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, cert_chain)) { |
| 286 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 287 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 288 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 289 | if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), |
| 290 | ssl)) { |
| 291 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 292 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 293 | |
| 294 | /* We need to inherit the verify parameters. These can be determined by the |
| 295 | * context: if its a server it will verify SSL client certificates or vice |
| 296 | * versa. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 297 | X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server"); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 298 | |
| 299 | /* Anything non-default in "param" should overwrite anything in the ctx. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 300 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 302 | if (ssl->verify_callback) { |
| 303 | X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 306 | if (ssl->ctx->app_verify_callback != NULL) { |
| 307 | ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 308 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 309 | ret = X509_verify_cert(&ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 312 | ssl->verify_result = ctx.error; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 313 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 314 | err: |
| 315 | X509_STORE_CTX_cleanup(&ctx); |
| 316 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 319 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
| 320 | STACK_OF(X509_NAME) *name_list) { |
| 321 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 322 | *ca_list = name_list; |
| 323 | } |
| 324 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 325 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) { |
| 326 | STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null(); |
| 327 | if (ret == NULL) { |
| 328 | return NULL; |
| 329 | } |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 330 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 331 | size_t i; |
| 332 | for (i = 0; i < sk_X509_NAME_num(list); i++) { |
| 333 | X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | if (name == NULL || !sk_X509_NAME_push(ret, name)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 335 | X509_NAME_free(name); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 336 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
| 337 | return NULL; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 344 | void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) { |
| 345 | set_client_CA_list(&ssl->client_CA, name_list); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 348 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 349 | set_client_CA_list(&ctx->client_CA, name_list); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 352 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 353 | return ctx->client_CA; |
| 354 | } |
| 355 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 356 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) { |
| 357 | /* For historical reasons, this function is used both to query configuration |
| 358 | * state on a server as well as handshake state on a client. However, whether |
| 359 | * |ssl| is a client or server is not known until explicitly configured with |
| 360 | * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an |
| 361 | * indeterminate mode and |ssl->server| is unset. */ |
| 362 | if (ssl->handshake_func != NULL && !ssl->server) { |
| 363 | return ssl->s3->tmp.ca_names; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 365 | |
| 366 | if (ssl->client_CA != NULL) { |
| 367 | return ssl->client_CA; |
| 368 | } |
| 369 | return ssl->ctx->client_CA; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 372 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 373 | X509_NAME *name; |
| 374 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 375 | if (x509 == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | if (*sk == NULL) { |
| 379 | *sk = sk_X509_NAME_new_null(); |
| 380 | if (*sk == NULL) { |
| 381 | return 0; |
| 382 | } |
| 383 | } |
| 384 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 385 | name = X509_NAME_dup(X509_get_subject_name(x509)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 386 | if (name == NULL) { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | if (!sk_X509_NAME_push(*sk, name)) { |
| 391 | X509_NAME_free(name); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return 1; |
| 396 | } |
| 397 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 398 | int SSL_add_client_CA(SSL *ssl, X509 *x509) { |
| 399 | return add_client_CA(&ssl->client_CA, x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 402 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) { |
| 403 | return add_client_CA(&ctx->client_CA, x509); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* Add a certificate to a BUF_MEM structure */ |
| 407 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) { |
| 408 | int n; |
| 409 | uint8_t *p; |
| 410 | |
| 411 | n = i2d_X509(x, NULL); |
| 412 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 413 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | p = (uint8_t *)&(buf->data[*l]); |
| 417 | l2n3(n, p); |
| 418 | i2d_X509(x, &p); |
| 419 | *l += n + 3; |
| 420 | |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | /* Add certificate chain to internal SSL BUF_MEM structure. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 425 | int ssl_add_cert_chain(SSL *ssl, unsigned long *l) { |
| 426 | CERT *cert = ssl->cert; |
| 427 | BUF_MEM *buf = ssl->init_buf; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 428 | int no_chain = 0; |
| 429 | size_t i; |
| 430 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 431 | X509 *x = cert->x509; |
| 432 | STACK_OF(X509) *chain = cert->chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 433 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 434 | if (x == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 435 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 436 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 439 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 440 | no_chain = 1; |
| 441 | } |
| 442 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 443 | if (no_chain) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 444 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 445 | return 0; |
| 446 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 447 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 448 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 449 | x = sk_X509_value(chain, i); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 450 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 451 | return 0; |
| 452 | } |
| 453 | } |
| 454 | } else { |
| 455 | X509_STORE_CTX xs_ctx; |
| 456 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 457 | if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) { |
| 458 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | X509_verify_cert(&xs_ctx); |
| 462 | /* Don't leave errors in the queue */ |
| 463 | ERR_clear_error(); |
| 464 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { |
| 465 | x = sk_X509_value(xs_ctx.chain, i); |
| 466 | |
| 467 | if (!ssl_add_cert_to_buf(buf, l, x)) { |
| 468 | X509_STORE_CTX_cleanup(&xs_ctx); |
| 469 | return 0; |
| 470 | } |
| 471 | } |
| 472 | X509_STORE_CTX_cleanup(&xs_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | return 1; |
| 476 | } |
| 477 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 478 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 479 | return ssl_cert_set0_chain(ctx->cert, chain); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 482 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 483 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 484 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 485 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 486 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 487 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 488 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 489 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 490 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 491 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 492 | } |
| 493 | |
| 494 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 495 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 496 | } |
| 497 | |
| 498 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 499 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 500 | } |
| 501 | |
| 502 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 503 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 504 | } |
| 505 | |
| 506 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 507 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 508 | } |
| 509 | |
| 510 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 511 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 512 | } |
| 513 | |
| 514 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 515 | return SSL_CTX_set0_chain(ctx, NULL); |
| 516 | } |
| 517 | |
| 518 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 519 | return SSL_CTX_clear_chain_certs(ctx); |
| 520 | } |
| 521 | |
| 522 | int SSL_clear_chain_certs(SSL *ssl) { |
| 523 | return SSL_set0_chain(ssl, NULL); |
| 524 | } |
| 525 | |
| 526 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 527 | *out_chain = ctx->cert->chain; |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 532 | STACK_OF(X509) **out_chain) { |
| 533 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 534 | } |
| 535 | |
| 536 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 537 | *out_chain = ssl->cert->chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 538 | return 1; |
| 539 | } |