Adam Langley | d1515a3 | 2017-01-23 16:46:16 -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 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
| 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 143 | #include <assert.h> |
| 144 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame^] | 145 | #include <openssl/asn1.h> |
| 146 | #include <openssl/bytestring.h> |
| 147 | #include <openssl/err.h> |
| 148 | #include <openssl/pem.h> |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 149 | #include <openssl/stack.h> |
| 150 | #include <openssl/x509.h> |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 151 | #include <openssl/x509v3.h> |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 152 | #include <openssl/x509_vfy.h> |
| 153 | |
| 154 | #include "internal.h" |
| 155 | |
| 156 | |
| 157 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 158 | if (ssl == NULL) { |
| 159 | return NULL; |
| 160 | } |
| 161 | SSL_SESSION *session = SSL_get_session(ssl); |
| 162 | if (session == NULL || session->x509_peer == NULL) { |
| 163 | return NULL; |
| 164 | } |
| 165 | X509_up_ref(session->x509_peer); |
| 166 | return session->x509_peer; |
| 167 | } |
| 168 | |
| 169 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 170 | if (ssl == NULL) { |
| 171 | return NULL; |
| 172 | } |
| 173 | SSL_SESSION *session = SSL_get_session(ssl); |
| 174 | if (session == NULL || |
| 175 | session->x509_chain == NULL) { |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | if (!ssl->server) { |
| 180 | return session->x509_chain; |
| 181 | } |
| 182 | |
| 183 | /* OpenSSL historically didn't include the leaf certificate in the returned |
| 184 | * certificate chain, but only for servers. */ |
| 185 | if (session->x509_chain_without_leaf == NULL) { |
| 186 | session->x509_chain_without_leaf = sk_X509_new_null(); |
| 187 | if (session->x509_chain_without_leaf == NULL) { |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) { |
| 192 | X509 *cert = sk_X509_value(session->x509_chain, i); |
| 193 | if (!sk_X509_push(session->x509_chain_without_leaf, cert)) { |
| 194 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 195 | session->x509_chain_without_leaf = NULL; |
| 196 | return NULL; |
| 197 | } |
| 198 | X509_up_ref(cert); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return session->x509_chain_without_leaf; |
| 203 | } |
| 204 | |
| 205 | STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 206 | SSL_SESSION *session = SSL_get_session(ssl); |
| 207 | if (session == NULL) { |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | return session->x509_chain; |
| 212 | } |
| 213 | |
| 214 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 215 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 216 | } |
| 217 | |
| 218 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 219 | return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose); |
| 220 | } |
| 221 | |
| 222 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 223 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 224 | } |
| 225 | |
| 226 | int SSL_set_trust(SSL *ssl, int trust) { |
| 227 | return X509_VERIFY_PARAM_set_trust(ssl->param, trust); |
| 228 | } |
| 229 | |
| 230 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 231 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 232 | } |
| 233 | |
| 234 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 235 | return X509_VERIFY_PARAM_set1(ssl->param, param); |
| 236 | } |
| 237 | |
| 238 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; } |
| 239 | |
| 240 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; } |
| 241 | |
| 242 | int SSL_get_verify_depth(const SSL *ssl) { |
| 243 | return X509_VERIFY_PARAM_get_depth(ssl->param); |
| 244 | } |
| 245 | |
| 246 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 247 | return ssl->verify_callback; |
| 248 | } |
| 249 | |
| 250 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; } |
| 251 | |
| 252 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 253 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 254 | } |
| 255 | |
| 256 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 257 | int ok, X509_STORE_CTX *store_ctx) { |
| 258 | return ctx->default_verify_callback; |
| 259 | } |
| 260 | |
| 261 | void SSL_set_verify(SSL *ssl, int mode, |
| 262 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 263 | ssl->verify_mode = mode; |
| 264 | if (callback != NULL) { |
| 265 | ssl->verify_callback = callback; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 270 | X509_VERIFY_PARAM_set_depth(ssl->param, depth); |
| 271 | } |
| 272 | |
| 273 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
| 274 | int (*cb)(X509_STORE_CTX *store_ctx, |
| 275 | void *arg), |
| 276 | void *arg) { |
| 277 | ctx->app_verify_callback = cb; |
| 278 | ctx->app_verify_arg = arg; |
| 279 | } |
| 280 | |
| 281 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 282 | int (*cb)(int, X509_STORE_CTX *)) { |
| 283 | ctx->verify_mode = mode; |
| 284 | ctx->default_verify_callback = cb; |
| 285 | } |
| 286 | |
| 287 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 288 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 289 | } |
| 290 | |
Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 291 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 292 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 293 | } |
| 294 | |
| 295 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 296 | const char *ca_dir) { |
| 297 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 298 | } |
| 299 | |
| 300 | void SSL_set_verify_result(SSL *ssl, long result) { |
| 301 | if (result != X509_V_OK) { |
| 302 | abort(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | long SSL_get_verify_result(const SSL *ssl) { |
| 307 | SSL_SESSION *session = SSL_get_session(ssl); |
| 308 | if (session == NULL) { |
| 309 | return X509_V_ERR_INVALID_CALL; |
| 310 | } |
| 311 | return session->verify_result; |
| 312 | } |
| 313 | |
| 314 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 315 | return ctx->cert_store; |
| 316 | } |
| 317 | |
| 318 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 319 | X509_STORE_free(ctx->cert_store); |
| 320 | ctx->cert_store = store; |
| 321 | } |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 322 | |
| 323 | static void ssl_crypto_x509_flush_cached_leaf(CERT *cert) { |
| 324 | X509_free(cert->x509_leaf); |
| 325 | cert->x509_leaf = NULL; |
| 326 | } |
| 327 | |
| 328 | static void ssl_crypto_x509_flush_cached_chain(CERT *cert) { |
| 329 | sk_X509_pop_free(cert->x509_chain, X509_free); |
| 330 | cert->x509_chain = NULL; |
| 331 | } |
| 332 | |
| 333 | static void ssl_crypto_x509_clear(CERT *cert) { |
| 334 | ssl_crypto_x509_flush_cached_leaf(cert); |
| 335 | ssl_crypto_x509_flush_cached_chain(cert); |
| 336 | |
| 337 | X509_free(cert->x509_stash); |
| 338 | cert->x509_stash = NULL; |
| 339 | } |
| 340 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame^] | 341 | static int ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) { |
| 342 | STACK_OF(X509) *chain = NULL; |
| 343 | const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs); |
| 344 | |
| 345 | if (num_certs > 0) { |
| 346 | chain = sk_X509_new_null(); |
| 347 | if (chain == NULL) { |
| 348 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 349 | goto err; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | X509 *leaf = NULL; |
| 354 | for (size_t i = 0; i < num_certs; i++) { |
| 355 | X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i)); |
| 356 | if (x509 == NULL) { |
| 357 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 358 | goto err; |
| 359 | } |
| 360 | if (!sk_X509_push(chain, x509)) { |
| 361 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 362 | X509_free(x509); |
| 363 | goto err; |
| 364 | } |
| 365 | if (i == 0) { |
| 366 | leaf = x509; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | sk_X509_pop_free(sess->x509_chain, X509_free); |
| 371 | sess->x509_chain = chain; |
| 372 | sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free); |
| 373 | sess->x509_chain_without_leaf = NULL; |
| 374 | |
| 375 | X509_free(sess->x509_peer); |
| 376 | if (leaf != NULL) { |
| 377 | X509_up_ref(leaf); |
| 378 | } |
| 379 | sess->x509_peer = leaf; |
| 380 | |
| 381 | return 1; |
| 382 | |
| 383 | err: |
| 384 | sk_X509_pop_free(chain, X509_free); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session, |
| 389 | const SSL_SESSION *session) { |
| 390 | if (session->x509_peer != NULL) { |
| 391 | X509_up_ref(session->x509_peer); |
| 392 | new_session->x509_peer = session->x509_peer; |
| 393 | } |
| 394 | if (session->x509_chain != NULL) { |
| 395 | new_session->x509_chain = X509_chain_up_ref(session->x509_chain); |
| 396 | if (new_session->x509_chain == NULL) { |
| 397 | return 0; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | static void ssl_crypto_x509_session_clear(SSL_SESSION *session) { |
| 405 | X509_free(session->x509_peer); |
| 406 | session->x509_peer = NULL; |
| 407 | sk_X509_pop_free(session->x509_chain, X509_free); |
| 408 | session->x509_chain = NULL; |
| 409 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 410 | session->x509_chain_without_leaf = NULL; |
| 411 | } |
| 412 | |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 413 | const SSL_X509_METHOD ssl_crypto_x509_method = { |
| 414 | ssl_crypto_x509_clear, |
| 415 | ssl_crypto_x509_flush_cached_chain, |
| 416 | ssl_crypto_x509_flush_cached_leaf, |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame^] | 417 | ssl_crypto_x509_session_cache_objects, |
| 418 | ssl_crypto_x509_session_dup, |
| 419 | ssl_crypto_x509_session_clear, |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised |
| 423 | * contents of |x509|. */ |
| 424 | static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) { |
| 425 | uint8_t *buf = NULL; |
| 426 | int cert_len = i2d_X509(x509, &buf); |
| 427 | if (cert_len <= 0) { |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL); |
| 432 | OPENSSL_free(buf); |
| 433 | |
| 434 | return buffer; |
| 435 | } |
| 436 | |
| 437 | static int ssl_use_certificate(CERT *cert, X509 *x) { |
| 438 | if (x == NULL) { |
| 439 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | CRYPTO_BUFFER *buffer = x509_to_buffer(x); |
| 444 | if (buffer == NULL) { |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | const int ok = ssl_set_cert(cert, buffer); |
| 449 | CRYPTO_BUFFER_free(buffer); |
| 450 | return ok; |
| 451 | } |
| 452 | |
| 453 | int SSL_use_certificate(SSL *ssl, X509 *x) { |
| 454 | return ssl_use_certificate(ssl->cert, x); |
| 455 | } |
| 456 | |
| 457 | int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) { |
| 458 | return ssl_use_certificate(ctx->cert, x); |
| 459 | } |
| 460 | |
| 461 | /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the |
| 462 | * first element of |cert->chain|. */ |
| 463 | static int ssl_cert_cache_leaf_cert(CERT *cert) { |
| 464 | assert(cert->x509_method); |
| 465 | |
| 466 | if (cert->x509_leaf != NULL || |
| 467 | cert->chain == NULL) { |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 472 | if (!leaf) { |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | cert->x509_leaf = X509_parse_from_buffer(leaf); |
| 477 | return cert->x509_leaf != NULL; |
| 478 | } |
| 479 | |
| 480 | static X509 *ssl_cert_get0_leaf(CERT *cert) { |
| 481 | if (cert->x509_leaf == NULL && |
| 482 | !ssl_cert_cache_leaf_cert(cert)) { |
| 483 | return NULL; |
| 484 | } |
| 485 | |
| 486 | return cert->x509_leaf; |
| 487 | } |
| 488 | |
| 489 | X509 *SSL_get_certificate(const SSL *ssl) { |
| 490 | return ssl_cert_get0_leaf(ssl->cert); |
| 491 | } |
| 492 | |
| 493 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 494 | return ssl_cert_get0_leaf(ctx->cert); |
| 495 | } |
| 496 | |
| 497 | /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */ |
| 498 | static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) { |
| 499 | STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null(); |
| 500 | if (chain == NULL) { |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | if (!sk_CRYPTO_BUFFER_push(chain, NULL)) { |
| 505 | sk_CRYPTO_BUFFER_free(chain); |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | return chain; |
| 510 | } |
| 511 | |
| 512 | /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised |
| 513 | * forms of elements of |chain|. It returns one on success or zero on error, in |
| 514 | * which case no change to |cert->chain| is made. It preverses the existing |
| 515 | * leaf from |cert->chain|, if any. */ |
| 516 | static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 517 | STACK_OF(CRYPTO_BUFFER) *new_chain = NULL; |
| 518 | |
| 519 | if (cert->chain != NULL) { |
| 520 | new_chain = sk_CRYPTO_BUFFER_new_null(); |
| 521 | if (new_chain == NULL) { |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0); |
| 526 | if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) { |
| 527 | goto err; |
| 528 | } |
| 529 | /* |leaf| might be NULL if it's a “leafless” chain. */ |
| 530 | if (leaf != NULL) { |
| 531 | CRYPTO_BUFFER_up_ref(leaf); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | for (size_t i = 0; i < sk_X509_num(chain); i++) { |
| 536 | if (new_chain == NULL) { |
| 537 | new_chain = new_leafless_chain(); |
| 538 | if (new_chain == NULL) { |
| 539 | goto err; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i)); |
| 544 | if (buffer == NULL || |
| 545 | !sk_CRYPTO_BUFFER_push(new_chain, buffer)) { |
| 546 | CRYPTO_BUFFER_free(buffer); |
| 547 | goto err; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free); |
| 552 | cert->chain = new_chain; |
| 553 | |
| 554 | return 1; |
| 555 | |
| 556 | err: |
| 557 | sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 562 | if (!ssl_cert_set_chain(cert, chain)) { |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | sk_X509_pop_free(chain, X509_free); |
| 567 | ssl_crypto_x509_flush_cached_chain(cert); |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) { |
| 572 | if (!ssl_cert_set_chain(cert, chain)) { |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | ssl_crypto_x509_flush_cached_chain(cert); |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | static int ssl_cert_append_cert(CERT *cert, X509 *x509) { |
| 581 | assert(cert->x509_method); |
| 582 | |
| 583 | CRYPTO_BUFFER *buffer = x509_to_buffer(x509); |
| 584 | if (buffer == NULL) { |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | if (cert->chain != NULL) { |
| 589 | if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 590 | CRYPTO_BUFFER_free(buffer); |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | cert->chain = new_leafless_chain(); |
| 598 | if (cert->chain == NULL || |
| 599 | !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) { |
| 600 | CRYPTO_BUFFER_free(buffer); |
| 601 | sk_CRYPTO_BUFFER_free(cert->chain); |
| 602 | cert->chain = NULL; |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | return 1; |
| 607 | } |
| 608 | |
| 609 | static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) { |
| 610 | if (!ssl_cert_append_cert(cert, x509)) { |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | X509_free(cert->x509_stash); |
| 615 | cert->x509_stash = x509; |
| 616 | ssl_crypto_x509_flush_cached_chain(cert); |
| 617 | return 1; |
| 618 | } |
| 619 | |
| 620 | static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) { |
| 621 | if (!ssl_cert_append_cert(cert, x509)) { |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | ssl_crypto_x509_flush_cached_chain(cert); |
| 626 | return 1; |
| 627 | } |
| 628 | |
| 629 | int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 630 | return ssl_cert_set0_chain(ctx->cert, chain); |
| 631 | } |
| 632 | |
| 633 | int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) { |
| 634 | return ssl_cert_set1_chain(ctx->cert, chain); |
| 635 | } |
| 636 | |
| 637 | int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 638 | return ssl_cert_set0_chain(ssl->cert, chain); |
| 639 | } |
| 640 | |
| 641 | int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) { |
| 642 | return ssl_cert_set1_chain(ssl->cert, chain); |
| 643 | } |
| 644 | |
| 645 | int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 646 | return ssl_cert_add0_chain_cert(ctx->cert, x509); |
| 647 | } |
| 648 | |
| 649 | int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 650 | return ssl_cert_add1_chain_cert(ctx->cert, x509); |
| 651 | } |
| 652 | |
| 653 | int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) { |
| 654 | return SSL_CTX_add0_chain_cert(ctx, x509); |
| 655 | } |
| 656 | |
| 657 | int SSL_add0_chain_cert(SSL *ssl, X509 *x509) { |
| 658 | return ssl_cert_add0_chain_cert(ssl->cert, x509); |
| 659 | } |
| 660 | |
| 661 | int SSL_add1_chain_cert(SSL *ssl, X509 *x509) { |
| 662 | return ssl_cert_add1_chain_cert(ssl->cert, x509); |
| 663 | } |
| 664 | |
| 665 | int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) { |
| 666 | return SSL_CTX_set0_chain(ctx, NULL); |
| 667 | } |
| 668 | |
| 669 | int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) { |
| 670 | return SSL_CTX_clear_chain_certs(ctx); |
| 671 | } |
| 672 | |
| 673 | int SSL_clear_chain_certs(SSL *ssl) { |
| 674 | return SSL_set0_chain(ssl, NULL); |
| 675 | } |
| 676 | |
| 677 | int ssl_auto_chain_if_needed(SSL *ssl) { |
| 678 | /* Only build a chain if there are no intermediates configured and the feature |
| 679 | * isn't disabled. */ |
| 680 | if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || |
| 681 | !ssl_has_certificate(ssl) || |
| 682 | ssl->cert->chain == NULL || |
| 683 | sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) { |
| 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | X509 *leaf = |
| 688 | X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0)); |
| 689 | if (!leaf) { |
| 690 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | X509_STORE_CTX ctx; |
| 695 | if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) { |
| 696 | X509_free(leaf); |
| 697 | OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* Attempt to build a chain, ignoring the result. */ |
| 702 | X509_verify_cert(&ctx); |
| 703 | X509_free(leaf); |
| 704 | ERR_clear_error(); |
| 705 | |
| 706 | /* Remove the leaf from the generated chain. */ |
| 707 | X509_free(sk_X509_shift(ctx.chain)); |
| 708 | |
| 709 | const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain); |
| 710 | X509_STORE_CTX_cleanup(&ctx); |
| 711 | if (!ok) { |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | ssl_crypto_x509_flush_cached_chain(ssl->cert); |
| 716 | |
| 717 | return 1; |
| 718 | } |
| 719 | |
| 720 | /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of |
| 721 | * |cert->chain|. */ |
| 722 | static int ssl_cert_cache_chain_certs(CERT *cert) { |
| 723 | assert(cert->x509_method); |
| 724 | |
| 725 | if (cert->x509_chain != NULL || |
| 726 | cert->chain == NULL || |
| 727 | sk_CRYPTO_BUFFER_num(cert->chain) < 2) { |
| 728 | return 1; |
| 729 | } |
| 730 | |
| 731 | STACK_OF(X509) *chain = sk_X509_new_null(); |
| 732 | if (chain == NULL) { |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) { |
| 737 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i); |
| 738 | X509 *x509 = X509_parse_from_buffer(buffer); |
| 739 | if (x509 == NULL || |
| 740 | !sk_X509_push(chain, x509)) { |
| 741 | X509_free(x509); |
| 742 | goto err; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | cert->x509_chain = chain; |
| 747 | return 1; |
| 748 | |
| 749 | err: |
| 750 | sk_X509_pop_free(chain, X509_free); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) { |
| 755 | if (!ssl_cert_cache_chain_certs(ctx->cert)) { |
| 756 | *out_chain = NULL; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | *out_chain = ctx->cert->x509_chain; |
| 761 | return 1; |
| 762 | } |
| 763 | |
| 764 | int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx, |
| 765 | STACK_OF(X509) **out_chain) { |
| 766 | return SSL_CTX_get0_chain_certs(ctx, out_chain); |
| 767 | } |
| 768 | |
| 769 | int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) { |
| 770 | if (!ssl_cert_cache_chain_certs(ssl->cert)) { |
| 771 | *out_chain = NULL; |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | *out_chain = ssl->cert->x509_chain; |
| 776 | return 1; |
| 777 | } |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame^] | 778 | |
| 779 | static SSL_SESSION *ssl_session_new_with_crypto_x509(void) { |
| 780 | return ssl_session_new(&ssl_crypto_x509_method); |
| 781 | } |
| 782 | |
| 783 | SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) { |
| 784 | return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509, |
| 785 | d2i_SSL_SESSION, bio, out); |
| 786 | } |
| 787 | |
| 788 | int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) { |
| 789 | return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session); |
| 790 | } |
| 791 | |
| 792 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |
| 793 | |
| 794 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 795 | if (length < 0) { |
| 796 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | CBS cbs; |
| 801 | CBS_init(&cbs, *pp, length); |
| 802 | |
| 803 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method, |
| 804 | NULL /* no buffer pool */); |
| 805 | if (ret == NULL) { |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | if (a) { |
| 810 | SSL_SESSION_free(*a); |
| 811 | *a = ret; |
| 812 | } |
| 813 | *pp = CBS_data(&cbs); |
| 814 | return ret; |
| 815 | } |