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 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 149 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | #include <stdio.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 151 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 153 | #include <openssl/buf.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | #include <openssl/dh.h> |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 155 | #include <openssl/digest.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 156 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | #include <openssl/md5.h> |
| 158 | #include <openssl/mem.h> |
| 159 | #include <openssl/obj.h> |
| 160 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 161 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 163 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 164 | const SSL3_ENC_METHOD SSLv3_enc_data = { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 165 | ssl3_prf, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 166 | tls1_setup_key_block, |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 167 | tls1_generate_master_secret, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 168 | tls1_change_cipher_state, |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 169 | ssl3_final_finish_mac, |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 170 | ssl3_cert_verify_mac, |
| 171 | SSL3_MD_CLIENT_FINISHED_CONST, 4, |
| 172 | SSL3_MD_SERVER_FINISHED_CONST, 4, |
| 173 | ssl3_alert_code, |
David Benjamin | d6e95ee | 2015-05-03 15:34:35 -0400 | [diff] [blame] | 174 | tls1_export_keying_material, |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 175 | 0, |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 176 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 178 | int ssl3_supports_cipher(const SSL_CIPHER *cipher) { |
| 179 | return 1; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 182 | int ssl3_set_handshake_header(SSL *s, int htype, unsigned long len) { |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 183 | uint8_t *p = (uint8_t *)s->init_buf->data; |
| 184 | *(p++) = htype; |
| 185 | l2n3(len, p); |
| 186 | s->init_num = (int)len + SSL3_HM_HEADER_LENGTH; |
| 187 | s->init_off = 0; |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 188 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 189 | /* Add the message to the handshake hash. */ |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 190 | return ssl3_update_handshake_hash(s, (uint8_t *)s->init_buf->data, |
| 191 | s->init_num); |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 192 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 194 | int ssl3_handshake_write(SSL *s) { return ssl3_do_write(s, SSL3_RT_HANDSHAKE); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 195 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 196 | int ssl3_new(SSL *s) { |
| 197 | SSL3_STATE *s3; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 199 | s3 = OPENSSL_malloc(sizeof *s3); |
| 200 | if (s3 == NULL) { |
| 201 | goto err; |
| 202 | } |
| 203 | memset(s3, 0, sizeof *s3); |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 204 | |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 205 | EVP_MD_CTX_init(&s3->handshake_hash); |
| 206 | EVP_MD_CTX_init(&s3->handshake_md5); |
| 207 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 208 | s->s3 = s3; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 209 | |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 210 | /* Set the version to the highest supported version for TLS. This controls the |
| 211 | * initial state of |s->enc_method| and what the API reports as the version |
| 212 | * prior to negotiation. |
| 213 | * |
| 214 | * TODO(davidben): This is fragile and confusing. */ |
| 215 | s->version = TLS1_2_VERSION; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 216 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | err: |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 218 | return 0; |
| 219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 221 | void ssl3_free(SSL *s) { |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 222 | if (s == NULL || s->s3 == NULL) { |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 223 | return; |
| 224 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 225 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 226 | ssl3_cleanup_key_block(s); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 227 | ssl_read_buffer_clear(s); |
| 228 | ssl_write_buffer_clear(s); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 229 | DH_free(s->s3->tmp.dh); |
| 230 | EC_KEY_free(s->s3->tmp.ecdh); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 231 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 232 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
| 233 | OPENSSL_free(s->s3->tmp.certificate_types); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 234 | OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist); |
| 235 | OPENSSL_free(s->s3->tmp.peer_psk_identity_hint); |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 236 | DH_free(s->s3->tmp.peer_dh_tmp); |
| 237 | EC_KEY_free(s->s3->tmp.peer_ecdh_tmp); |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 238 | ssl3_free_handshake_buffer(s); |
| 239 | ssl3_free_handshake_hash(s); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 240 | OPENSSL_free(s->s3->alpn_selected); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 242 | OPENSSL_cleanse(s->s3, sizeof *s->s3); |
| 243 | OPENSSL_free(s->s3); |
| 244 | s->s3 = NULL; |
| 245 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | |
David Benjamin | c045469 | 2015-04-27 00:28:56 -0400 | [diff] [blame] | 247 | int SSL_session_reused(const SSL *ssl) { |
| 248 | return ssl->hit; |
| 249 | } |
| 250 | |
| 251 | int SSL_total_renegotiations(const SSL *ssl) { |
| 252 | return ssl->s3->total_renegotiations; |
| 253 | } |
| 254 | |
| 255 | int SSL_num_renegotiations(const SSL *ssl) { |
| 256 | return SSL_total_renegotiations(ssl); |
| 257 | } |
| 258 | |
| 259 | int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int SSL_need_rsa(const SSL *ssl) { |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { |
| 272 | return 1; |
| 273 | } |
| 274 | |
David Benjamin | 15a3b00 | 2015-04-28 00:05:35 -0400 | [diff] [blame] | 275 | int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { |
| 276 | DH_free(ctx->cert->dh_tmp); |
| 277 | ctx->cert->dh_tmp = DHparams_dup(dh); |
| 278 | if (ctx->cert->dh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 279 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
David Benjamin | 15a3b00 | 2015-04-28 00:05:35 -0400 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { |
| 286 | DH_free(ssl->cert->dh_tmp); |
| 287 | ssl->cert->dh_tmp = DHparams_dup(dh); |
| 288 | if (ssl->cert->dh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 289 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
David Benjamin | 15a3b00 | 2015-04-28 00:05:35 -0400 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) { |
| 296 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 297 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
David Benjamin | 15a3b00 | 2015-04-28 00:05:35 -0400 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | ctx->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) { |
| 305 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 306 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
David Benjamin | 15a3b00 | 2015-04-28 00:05:35 -0400 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | ssl->cert->ecdh_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 310 | return 1; |
| 311 | } |
| 312 | |
David Benjamin | c280758 | 2015-04-28 00:19:50 -0400 | [diff] [blame] | 313 | int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) { |
| 314 | ctx->tlsext_channel_id_enabled = 1; |
| 315 | return 1; |
| 316 | } |
| 317 | |
| 318 | int SSL_enable_tls_channel_id(SSL *ssl) { |
| 319 | ssl->tlsext_channel_id_enabled = 1; |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) { |
| 324 | ctx->tlsext_channel_id_enabled = 1; |
| 325 | if (EVP_PKEY_id(private_key) != EVP_PKEY_EC || |
| 326 | EVP_PKEY_bits(private_key) != 256) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 327 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
David Benjamin | c280758 | 2015-04-28 00:19:50 -0400 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
David Benjamin | 9a10f8f | 2015-05-05 22:22:40 -0400 | [diff] [blame] | 331 | ctx->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key); |
David Benjamin | c280758 | 2015-04-28 00:19:50 -0400 | [diff] [blame] | 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) { |
| 336 | ssl->tlsext_channel_id_enabled = 1; |
| 337 | if (EVP_PKEY_id(private_key) != EVP_PKEY_EC || |
| 338 | EVP_PKEY_bits(private_key) != 256) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 339 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
David Benjamin | c280758 | 2015-04-28 00:19:50 -0400 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | EVP_PKEY_free(ssl->tlsext_channel_id_private); |
David Benjamin | 9a10f8f | 2015-05-05 22:22:40 -0400 | [diff] [blame] | 343 | ssl->tlsext_channel_id_private = EVP_PKEY_up_ref(private_key); |
David Benjamin | c280758 | 2015-04-28 00:19:50 -0400 | [diff] [blame] | 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) { |
| 348 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 349 | return 0; |
| 350 | } |
| 351 | memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64); |
| 352 | return 64; |
| 353 | } |
| 354 | |
David Benjamin | 7133d42 | 2015-04-28 00:43:39 -0400 | [diff] [blame] | 355 | int SSL_set_tlsext_host_name(SSL *ssl, const char *name) { |
| 356 | OPENSSL_free(ssl->tlsext_hostname); |
| 357 | ssl->tlsext_hostname = NULL; |
| 358 | |
| 359 | if (name == NULL) { |
| 360 | return 1; |
| 361 | } |
| 362 | if (strlen(name) > TLSEXT_MAXLEN_host_name) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 363 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME); |
David Benjamin | 7133d42 | 2015-04-28 00:43:39 -0400 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | ssl->tlsext_hostname = BUF_strdup(name); |
| 367 | if (ssl->tlsext_hostname == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 368 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 7133d42 | 2015-04-28 00:43:39 -0400 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | return 1; |
| 372 | } |
| 373 | |
David Benjamin | 7591064 | 2015-08-09 10:42:33 -0400 | [diff] [blame] | 374 | size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) { |
| 375 | if (ssl->server || !ssl->s3->tmp.cert_req) { |
| 376 | *out_types = NULL; |
| 377 | return 0; |
| 378 | } |
| 379 | *out_types = ssl->s3->tmp.certificate_types; |
| 380 | return ssl->s3->tmp.num_certificate_types; |
| 381 | } |
| 382 | |
David Benjamin | fdb4cdd | 2015-08-09 11:13:23 -0400 | [diff] [blame] | 383 | int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) { |
| 384 | return tls1_set_curves(&ctx->tlsext_ellipticcurvelist, |
| 385 | &ctx->tlsext_ellipticcurvelist_length, curves, |
| 386 | curves_len); |
| 387 | } |
| 388 | |
| 389 | int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) { |
| 390 | return tls1_set_curves(&ssl->tlsext_ellipticcurvelist, |
| 391 | &ssl->tlsext_ellipticcurvelist_length, curves, |
| 392 | curves_len); |
| 393 | } |
| 394 | |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 395 | int SSL_CTX_set_tlsext_servername_callback( |
| 396 | SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) { |
| 397 | ctx->tlsext_servername_callback = callback; |
| 398 | return 1; |
| 399 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | |
David Benjamin | 7133d42 | 2015-04-28 00:43:39 -0400 | [diff] [blame] | 401 | int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) { |
| 402 | ctx->tlsext_servername_arg = arg; |
| 403 | return 1; |
| 404 | } |
| 405 | |
David Benjamin | 6cacac0 | 2015-06-16 13:29:54 -0400 | [diff] [blame] | 406 | int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) { |
| 407 | if (out == NULL) { |
| 408 | return 48; |
| 409 | } |
| 410 | if (len != 48) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 411 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
David Benjamin | 6cacac0 | 2015-06-16 13:29:54 -0400 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | uint8_t *out_bytes = out; |
| 415 | memcpy(out_bytes, ctx->tlsext_tick_key_name, 16); |
| 416 | memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16); |
| 417 | memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16); |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) { |
| 422 | if (in == NULL) { |
| 423 | return 48; |
| 424 | } |
| 425 | if (len != 48) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 426 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
David Benjamin | 6cacac0 | 2015-06-16 13:29:54 -0400 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | const uint8_t *in_bytes = in; |
| 430 | memcpy(ctx->tlsext_tick_key_name, in_bytes, 16); |
| 431 | memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16); |
| 432 | memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16); |
| 433 | return 1; |
| 434 | } |
| 435 | |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 436 | int SSL_CTX_set_tlsext_ticket_key_cb( |
| 437 | SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 438 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 439 | int encrypt)) { |
| 440 | ctx->tlsext_ticket_key_cb = callback; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 441 | return 1; |
| 442 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 444 | struct ssl_cipher_preference_list_st *ssl_get_cipher_preferences(SSL *s) { |
| 445 | if (s->cipher_list != NULL) { |
| 446 | return s->cipher_list; |
| 447 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 449 | if (s->version >= TLS1_1_VERSION && s->ctx != NULL && |
| 450 | s->ctx->cipher_list_tls11 != NULL) { |
| 451 | return s->ctx->cipher_list_tls11; |
| 452 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 454 | if (s->version >= TLS1_VERSION && s->ctx != NULL && |
| 455 | s->ctx->cipher_list_tls10 != NULL) { |
| 456 | return s->ctx->cipher_list_tls10; |
| 457 | } |
| 458 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 459 | if (s->ctx != NULL && s->ctx->cipher_list != NULL) { |
| 460 | return s->ctx->cipher_list; |
| 461 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 463 | return NULL; |
| 464 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 466 | const SSL_CIPHER *ssl3_choose_cipher( |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 467 | SSL *s, STACK_OF(SSL_CIPHER) *clnt, |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 468 | struct ssl_cipher_preference_list_st *server_pref) { |
| 469 | const SSL_CIPHER *c, *ret = NULL; |
| 470 | STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow; |
| 471 | size_t i; |
| 472 | int ok; |
| 473 | size_t cipher_index; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 474 | uint32_t alg_k, alg_a, mask_k, mask_a; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 475 | /* in_group_flags will either be NULL, or will point to an array of bytes |
| 476 | * which indicate equal-preference groups in the |prio| stack. See the |
| 477 | * comment about |in_group_flags| in the |ssl_cipher_preference_list_st| |
| 478 | * struct. */ |
| 479 | const uint8_t *in_group_flags; |
| 480 | /* group_min contains the minimal index so far found in a group, or -1 if no |
| 481 | * such value exists yet. */ |
| 482 | int group_min = -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 484 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 485 | prio = srvr; |
| 486 | in_group_flags = server_pref->in_group_flags; |
| 487 | allow = clnt; |
| 488 | } else { |
| 489 | prio = clnt; |
| 490 | in_group_flags = NULL; |
| 491 | allow = srvr; |
| 492 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 494 | ssl_get_compatible_server_ciphers(s, &mask_k, &mask_a); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 496 | for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) { |
| 497 | c = sk_SSL_CIPHER_value(prio, i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 499 | ok = 1; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 501 | /* Skip TLS v1.2 only ciphersuites if not supported */ |
| 502 | if ((c->algorithm_ssl & SSL_TLSV1_2) && !SSL_USE_TLS1_2_CIPHERS(s)) { |
| 503 | ok = 0; |
| 504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 506 | alg_k = c->algorithm_mkey; |
| 507 | alg_a = c->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 508 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 509 | ok = ok && (alg_k & mask_k) && (alg_a & mask_a); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 511 | if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) { |
| 512 | if (in_group_flags != NULL && in_group_flags[i] == 1) { |
| 513 | /* This element of |prio| is in a group. Update the minimum index found |
| 514 | * so far and continue looking. */ |
| 515 | if (group_min == -1 || (size_t)group_min > cipher_index) { |
| 516 | group_min = cipher_index; |
| 517 | } |
| 518 | } else { |
| 519 | if (group_min != -1 && (size_t)group_min < cipher_index) { |
| 520 | cipher_index = group_min; |
| 521 | } |
| 522 | ret = sk_SSL_CIPHER_value(allow, cipher_index); |
| 523 | break; |
| 524 | } |
| 525 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 527 | if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) { |
| 528 | /* We are about to leave a group, but we found a match in it, so that's |
| 529 | * our answer. */ |
| 530 | ret = sk_SSL_CIPHER_value(allow, group_min); |
| 531 | break; |
| 532 | } |
| 533 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 534 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 535 | return ret; |
| 536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 538 | int ssl3_get_req_cert_type(SSL *s, uint8_t *p) { |
| 539 | int ret = 0; |
| 540 | const uint8_t *sig; |
| 541 | size_t i, siglen; |
| 542 | int have_rsa_sign = 0; |
| 543 | int have_ecdsa_sign = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 545 | /* get configured sigalgs */ |
| 546 | siglen = tls12_get_psigalgs(s, &sig); |
| 547 | for (i = 0; i < siglen; i += 2, sig += 2) { |
| 548 | switch (sig[1]) { |
| 549 | case TLSEXT_signature_rsa: |
| 550 | have_rsa_sign = 1; |
| 551 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 553 | case TLSEXT_signature_ecdsa: |
| 554 | have_ecdsa_sign = 1; |
| 555 | break; |
| 556 | } |
| 557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 559 | if (have_rsa_sign) { |
| 560 | p[ret++] = SSL3_CT_RSA_SIGN; |
| 561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 563 | /* ECDSA certs can be used with RSA cipher suites as well so we don't need to |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 564 | * check for SSL_kECDH or SSL_kECDHE. */ |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 565 | if (s->version >= TLS1_VERSION && have_ecdsa_sign) { |
| 566 | p[ret++] = TLS_CT_ECDSA_SIGN; |
| 567 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 569 | return ret; |
| 570 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 571 | |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 572 | /* If we are using default SHA1+MD5 algorithms switch to new SHA256 PRF and |
| 573 | * handshake macs if required. */ |
David Benjamin | 5055c76 | 2015-08-04 09:24:14 -0400 | [diff] [blame] | 574 | uint32_t ssl_get_algorithm_prf(SSL *s) { |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 575 | uint32_t algorithm_prf = s->s3->tmp.new_cipher->algorithm_prf; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 576 | if (s->enc_method->enc_flags & SSL_ENC_FLAG_SHA256_PRF && |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 577 | algorithm_prf == SSL_HANDSHAKE_MAC_DEFAULT) { |
David Benjamin | 9a980ab | 2015-05-27 00:36:11 -0400 | [diff] [blame] | 578 | return SSL_HANDSHAKE_MAC_SHA256; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 579 | } |
David Benjamin | b088331 | 2015-08-06 09:54:13 -0400 | [diff] [blame] | 580 | return algorithm_prf; |
Adam Langley | be2900a | 2014-12-18 12:09:04 -0800 | [diff] [blame] | 581 | } |