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 2005 Nokia. All rights reserved. |
| 59 | * |
| 60 | * The portions of the attached software ("Contribution") is developed by |
| 61 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 62 | * license. |
| 63 | * |
| 64 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 65 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 66 | * support (see RFC 4279) to OpenSSL. |
| 67 | * |
| 68 | * No patent licenses or other rights except those expressly stated in |
| 69 | * the OpenSSL open source license shall be deemed granted or received |
| 70 | * expressly, by implication, estoppel, or otherwise. |
| 71 | * |
| 72 | * No assurances are provided by Nokia that the Contribution does not |
| 73 | * infringe the patent or other intellectual property rights of any third |
| 74 | * party or that the license provides you with all the necessary rights |
| 75 | * to make use of the Contribution. |
| 76 | * |
| 77 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 78 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 79 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 80 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 81 | * OTHERWISE. */ |
| 82 | |
| 83 | #include <assert.h> |
| 84 | #include <stdio.h> |
| 85 | #include <stdlib.h> |
| 86 | |
| 87 | #include <openssl/asn1.h> |
| 88 | #include <openssl/asn1_mac.h> |
| 89 | #include <openssl/err.h> |
| 90 | #include <openssl/mem.h> |
| 91 | #include <openssl/obj.h> |
| 92 | #include <openssl/x509.h> |
| 93 | |
| 94 | #include "ssl_locl.h" |
| 95 | |
| 96 | OPENSSL_DECLARE_ERROR_REASON(SSL, CIPHER_CODE_WRONG_LENGTH); |
| 97 | OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_SSL_VERSION); |
| 98 | OPENSSL_DECLARE_ERROR_REASON(SSL, BAD_LENGTH); |
| 99 | OPENSSL_DECLARE_ERROR_FUNCTION(SSL, D2I_SSL_SESSION); |
| 100 | |
| 101 | |
| 102 | typedef struct ssl_session_asn1_st |
| 103 | { |
| 104 | ASN1_INTEGER version; |
| 105 | ASN1_INTEGER ssl_version; |
| 106 | ASN1_OCTET_STRING cipher; |
| 107 | ASN1_OCTET_STRING comp_id; |
| 108 | ASN1_OCTET_STRING master_key; |
| 109 | ASN1_OCTET_STRING session_id; |
| 110 | ASN1_OCTET_STRING session_id_context; |
| 111 | ASN1_OCTET_STRING key_arg; |
| 112 | ASN1_INTEGER time; |
| 113 | ASN1_INTEGER timeout; |
| 114 | ASN1_INTEGER verify_result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | ASN1_OCTET_STRING tlsext_hostname; |
| 116 | ASN1_INTEGER tlsext_tick_lifetime; |
| 117 | ASN1_OCTET_STRING tlsext_tick; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | ASN1_OCTET_STRING psk_identity_hint; |
| 119 | ASN1_OCTET_STRING psk_identity; |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | ASN1_OCTET_STRING peer_sha256; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | ASN1_OCTET_STRING original_handshake_hash; |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 122 | ASN1_OCTET_STRING tlsext_signed_cert_timestamp_list; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 123 | ASN1_OCTET_STRING ocsp_response; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | } SSL_SESSION_ASN1; |
| 125 | |
| 126 | int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp) |
| 127 | { |
| 128 | #define LSIZE2 (sizeof(long)*2) |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 129 | int v1=0,v2=0,v3=0,v4=0,v5=0,v7=0,v8=0,v13=0,v14=0,v15=0,v16=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2]; |
| 131 | unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | int v6=0,v9=0,v10=0; |
| 133 | unsigned char ibuf6[LSIZE2]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | long l; |
| 135 | SSL_SESSION_ASN1 a; |
| 136 | M_ASN1_I2D_vars(in); |
| 137 | |
| 138 | if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0))) |
| 139 | return(0); |
| 140 | |
| 141 | /* Note that I cheat in the following 2 assignments. I know |
| 142 | * that if the ASN1_INTEGER passed to ASN1_INTEGER_set |
| 143 | * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed. |
| 144 | * This is a bit evil but makes things simple, no dynamic allocation |
| 145 | * to clean up :-) */ |
| 146 | a.version.length=LSIZE2; |
| 147 | a.version.type=V_ASN1_INTEGER; |
| 148 | a.version.data=ibuf1; |
| 149 | ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION); |
| 150 | |
| 151 | a.ssl_version.length=LSIZE2; |
| 152 | a.ssl_version.type=V_ASN1_INTEGER; |
| 153 | a.ssl_version.data=ibuf2; |
| 154 | ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version); |
| 155 | |
| 156 | a.cipher.type=V_ASN1_OCTET_STRING; |
| 157 | a.cipher.data=buf; |
| 158 | |
| 159 | if (in->cipher == NULL) |
| 160 | l=in->cipher_id; |
| 161 | else |
| 162 | l=in->cipher->id; |
| 163 | if (in->ssl_version == SSL2_VERSION) |
| 164 | { |
| 165 | a.cipher.length=3; |
| 166 | buf[0]=((unsigned char)(l>>16L))&0xff; |
| 167 | buf[1]=((unsigned char)(l>> 8L))&0xff; |
| 168 | buf[2]=((unsigned char)(l ))&0xff; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | a.cipher.length=2; |
| 173 | buf[0]=((unsigned char)(l>>8L))&0xff; |
| 174 | buf[1]=((unsigned char)(l ))&0xff; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | a.master_key.length=in->master_key_length; |
| 179 | a.master_key.type=V_ASN1_OCTET_STRING; |
| 180 | a.master_key.data=in->master_key; |
| 181 | |
| 182 | a.session_id.length=in->session_id_length; |
| 183 | a.session_id.type=V_ASN1_OCTET_STRING; |
| 184 | a.session_id.data=in->session_id; |
| 185 | |
| 186 | a.session_id_context.length=in->sid_ctx_length; |
| 187 | a.session_id_context.type=V_ASN1_OCTET_STRING; |
| 188 | a.session_id_context.data=in->sid_ctx; |
| 189 | |
| 190 | a.key_arg.length=in->key_arg_length; |
| 191 | a.key_arg.type=V_ASN1_OCTET_STRING; |
| 192 | a.key_arg.data=in->key_arg; |
| 193 | |
| 194 | if (in->time != 0L) |
| 195 | { |
| 196 | a.time.length=LSIZE2; |
| 197 | a.time.type=V_ASN1_INTEGER; |
| 198 | a.time.data=ibuf3; |
| 199 | ASN1_INTEGER_set(&(a.time),in->time); |
| 200 | } |
| 201 | |
| 202 | if (in->timeout != 0L) |
| 203 | { |
| 204 | a.timeout.length=LSIZE2; |
| 205 | a.timeout.type=V_ASN1_INTEGER; |
| 206 | a.timeout.data=ibuf4; |
| 207 | ASN1_INTEGER_set(&(a.timeout),in->timeout); |
| 208 | } |
| 209 | |
| 210 | if (in->verify_result != X509_V_OK) |
| 211 | { |
| 212 | a.verify_result.length=LSIZE2; |
| 213 | a.verify_result.type=V_ASN1_INTEGER; |
| 214 | a.verify_result.data=ibuf5; |
| 215 | ASN1_INTEGER_set(&a.verify_result,in->verify_result); |
| 216 | } |
| 217 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | if (in->tlsext_hostname) |
| 219 | { |
| 220 | a.tlsext_hostname.length=strlen(in->tlsext_hostname); |
| 221 | a.tlsext_hostname.type=V_ASN1_OCTET_STRING; |
| 222 | a.tlsext_hostname.data=(unsigned char *)in->tlsext_hostname; |
| 223 | } |
| 224 | if (in->tlsext_tick) |
| 225 | { |
| 226 | a.tlsext_tick.length= in->tlsext_ticklen; |
| 227 | a.tlsext_tick.type=V_ASN1_OCTET_STRING; |
| 228 | a.tlsext_tick.data=(unsigned char *)in->tlsext_tick; |
| 229 | } |
| 230 | if (in->tlsext_tick_lifetime_hint > 0) |
| 231 | { |
| 232 | a.tlsext_tick_lifetime.length=LSIZE2; |
| 233 | a.tlsext_tick_lifetime.type=V_ASN1_INTEGER; |
| 234 | a.tlsext_tick_lifetime.data=ibuf6; |
| 235 | ASN1_INTEGER_set(&a.tlsext_tick_lifetime,in->tlsext_tick_lifetime_hint); |
| 236 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | if (in->psk_identity_hint) |
| 238 | { |
| 239 | a.psk_identity_hint.length=strlen(in->psk_identity_hint); |
| 240 | a.psk_identity_hint.type=V_ASN1_OCTET_STRING; |
| 241 | a.psk_identity_hint.data=(unsigned char *)(in->psk_identity_hint); |
| 242 | } |
| 243 | if (in->psk_identity) |
| 244 | { |
| 245 | a.psk_identity.length=strlen(in->psk_identity); |
| 246 | a.psk_identity.type=V_ASN1_OCTET_STRING; |
| 247 | a.psk_identity.data=(unsigned char *)(in->psk_identity); |
| 248 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | |
| 250 | if (in->peer_sha256_valid) |
| 251 | { |
| 252 | a.peer_sha256.length = sizeof(in->peer_sha256); |
| 253 | a.peer_sha256.type = V_ASN1_OCTET_STRING; |
| 254 | a.peer_sha256.data = in->peer_sha256; |
| 255 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
| 257 | if (in->original_handshake_hash_len > 0) |
| 258 | { |
| 259 | a.original_handshake_hash.length = in->original_handshake_hash_len; |
| 260 | a.original_handshake_hash.type = V_ASN1_OCTET_STRING; |
| 261 | a.original_handshake_hash.data = in->original_handshake_hash; |
| 262 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 264 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 265 | { |
| 266 | a.tlsext_signed_cert_timestamp_list.length = |
| 267 | in->tlsext_signed_cert_timestamp_list_length; |
| 268 | a.tlsext_signed_cert_timestamp_list.type = V_ASN1_OCTET_STRING; |
| 269 | a.tlsext_signed_cert_timestamp_list.data = |
| 270 | in->tlsext_signed_cert_timestamp_list; |
| 271 | } |
| 272 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 273 | if (in->ocsp_response_length > 0) |
| 274 | { |
| 275 | a.ocsp_response.length = in->ocsp_response_length; |
| 276 | a.ocsp_response.type = V_ASN1_OCTET_STRING; |
| 277 | a.ocsp_response.data = in->ocsp_response; |
| 278 | } |
| 279 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | M_ASN1_I2D_len(&(a.version), i2d_ASN1_INTEGER); |
| 281 | M_ASN1_I2D_len(&(a.ssl_version), i2d_ASN1_INTEGER); |
| 282 | M_ASN1_I2D_len(&(a.cipher), i2d_ASN1_OCTET_STRING); |
| 283 | M_ASN1_I2D_len(&(a.session_id), i2d_ASN1_OCTET_STRING); |
| 284 | M_ASN1_I2D_len(&(a.master_key), i2d_ASN1_OCTET_STRING); |
| 285 | if (in->key_arg_length > 0) |
| 286 | M_ASN1_I2D_len_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING); |
| 287 | if (in->time != 0L) |
| 288 | M_ASN1_I2D_len_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1); |
| 289 | if (in->timeout != 0L) |
| 290 | M_ASN1_I2D_len_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | if (in->peer != NULL && in->peer_sha256_valid == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | M_ASN1_I2D_len_EXP_opt(in->peer,i2d_X509,3,v3); |
| 293 | M_ASN1_I2D_len_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,v4); |
| 294 | if (in->verify_result != X509_V_OK) |
| 295 | M_ASN1_I2D_len_EXP_opt(&(a.verify_result),i2d_ASN1_INTEGER,5,v5); |
| 296 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | if (in->tlsext_tick_lifetime_hint > 0) |
| 298 | M_ASN1_I2D_len_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9); |
| 299 | if (in->tlsext_tick) |
| 300 | M_ASN1_I2D_len_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10); |
| 301 | if (in->tlsext_hostname) |
| 302 | M_ASN1_I2D_len_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | if (in->psk_identity_hint) |
| 304 | M_ASN1_I2D_len_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7); |
| 305 | if (in->psk_identity) |
| 306 | M_ASN1_I2D_len_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | if (in->peer_sha256_valid) |
| 308 | M_ASN1_I2D_len_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | if (in->original_handshake_hash_len > 0) |
| 310 | M_ASN1_I2D_len_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14); |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 311 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 312 | M_ASN1_I2D_len_EXP_opt(&(a.tlsext_signed_cert_timestamp_list), |
| 313 | i2d_ASN1_OCTET_STRING, 15, v15); |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 314 | if (in->ocsp_response_length > 0) |
| 315 | M_ASN1_I2D_len_EXP_opt(&(a.ocsp_response), i2d_ASN1_OCTET_STRING, 16, v16); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
| 317 | M_ASN1_I2D_seq_total(); |
| 318 | |
| 319 | M_ASN1_I2D_put(&(a.version), i2d_ASN1_INTEGER); |
| 320 | M_ASN1_I2D_put(&(a.ssl_version), i2d_ASN1_INTEGER); |
| 321 | M_ASN1_I2D_put(&(a.cipher), i2d_ASN1_OCTET_STRING); |
| 322 | M_ASN1_I2D_put(&(a.session_id), i2d_ASN1_OCTET_STRING); |
| 323 | M_ASN1_I2D_put(&(a.master_key), i2d_ASN1_OCTET_STRING); |
| 324 | if (in->key_arg_length > 0) |
| 325 | M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0); |
| 326 | if (in->time != 0L) |
| 327 | M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1); |
| 328 | if (in->timeout != 0L) |
| 329 | M_ASN1_I2D_put_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | if (in->peer != NULL && in->peer_sha256_valid == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3); |
| 332 | M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4, |
| 333 | v4); |
| 334 | if (in->verify_result != X509_V_OK) |
| 335 | M_ASN1_I2D_put_EXP_opt(&a.verify_result,i2d_ASN1_INTEGER,5,v5); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | if (in->tlsext_hostname) |
| 337 | M_ASN1_I2D_put_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | if (in->psk_identity_hint) |
| 339 | M_ASN1_I2D_put_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7); |
| 340 | if (in->psk_identity) |
| 341 | M_ASN1_I2D_put_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 342 | if (in->tlsext_tick_lifetime_hint > 0) |
| 343 | M_ASN1_I2D_put_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9); |
| 344 | if (in->tlsext_tick) |
| 345 | M_ASN1_I2D_put_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | if (in->peer_sha256_valid) |
| 347 | M_ASN1_I2D_put_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | if (in->original_handshake_hash_len > 0) |
| 349 | M_ASN1_I2D_put_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14); |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 350 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 351 | M_ASN1_I2D_put_EXP_opt(&(a.tlsext_signed_cert_timestamp_list), |
| 352 | i2d_ASN1_OCTET_STRING, 15, v15); |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 353 | if (in->ocsp_response > 0) |
| 354 | M_ASN1_I2D_put_EXP_opt(&(a.ocsp_response), i2d_ASN1_OCTET_STRING, 16, v16); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | M_ASN1_I2D_finish(); |
| 357 | } |
| 358 | |
| 359 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp, |
| 360 | long length) |
| 361 | { |
| 362 | int ssl_version=0,i; |
| 363 | long id; |
| 364 | ASN1_INTEGER ai,*aip; |
| 365 | ASN1_OCTET_STRING os,*osp; |
| 366 | M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new); |
| 367 | |
| 368 | aip= &ai; |
| 369 | osp= &os; |
| 370 | |
| 371 | M_ASN1_D2I_Init(); |
| 372 | M_ASN1_D2I_start_sequence(); |
| 373 | |
| 374 | ai.data=NULL; ai.length=0; |
| 375 | M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER); |
| 376 | if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; } |
| 377 | |
| 378 | /* we don't care about the version right now :-) */ |
| 379 | M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER); |
| 380 | ssl_version=(int)ASN1_INTEGER_get(aip); |
| 381 | ret->ssl_version=ssl_version; |
| 382 | if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; } |
| 383 | |
| 384 | os.data=NULL; os.length=0; |
| 385 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 386 | if (ssl_version == SSL2_VERSION) |
| 387 | { |
| 388 | if (os.length != 3) |
| 389 | { |
| 390 | c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | goto err; |
| 393 | } |
| 394 | id=0x02000000L| |
| 395 | ((unsigned long)os.data[0]<<16L)| |
| 396 | ((unsigned long)os.data[1]<< 8L)| |
| 397 | (unsigned long)os.data[2]; |
| 398 | } |
| 399 | else if ((ssl_version>>8) >= SSL3_VERSION_MAJOR) |
| 400 | { |
| 401 | if (os.length != 2) |
| 402 | { |
| 403 | c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 404 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | goto err; |
| 406 | } |
| 407 | id=0x03000000L| |
| 408 | ((unsigned long)os.data[0]<<8L)| |
| 409 | (unsigned long)os.data[1]; |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | c.error=SSL_R_UNKNOWN_SSL_VERSION; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | goto err; |
| 416 | } |
| 417 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | ret->cipher_id=id; |
Adam Langley | 82b7da2 | 2014-08-13 12:28:02 -0700 | [diff] [blame] | 419 | ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff); |
| 420 | if (ret->cipher == NULL) |
| 421 | { |
| 422 | c.error=SSL_R_UNSUPPORTED_CIPHER; |
| 423 | c.line = __LINE__; |
| 424 | goto err; |
| 425 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
| 427 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 428 | if ((ssl_version>>8) >= SSL3_VERSION_MAJOR) |
| 429 | i=SSL3_MAX_SSL_SESSION_ID_LENGTH; |
| 430 | else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */ |
| 431 | i=SSL2_MAX_SSL_SESSION_ID_LENGTH; |
| 432 | |
| 433 | if (os.length > i) |
| 434 | os.length = i; |
| 435 | if (os.length > (int)sizeof(ret->session_id)) /* can't happen */ |
| 436 | os.length = sizeof(ret->session_id); |
| 437 | |
| 438 | ret->session_id_length=os.length; |
| 439 | assert(os.length <= (int)sizeof(ret->session_id)); |
| 440 | memcpy(ret->session_id,os.data,os.length); |
| 441 | |
| 442 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 443 | if (os.length > SSL_MAX_MASTER_KEY_LENGTH) |
| 444 | ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; |
| 445 | else |
| 446 | ret->master_key_length=os.length; |
| 447 | memcpy(ret->master_key,os.data,ret->master_key_length); |
| 448 | |
| 449 | os.length=0; |
| 450 | |
| 451 | M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING); |
| 452 | if (os.length > SSL_MAX_KEY_ARG_LENGTH) |
| 453 | ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH; |
| 454 | else |
| 455 | ret->key_arg_length=os.length; |
| 456 | memcpy(ret->key_arg,os.data,ret->key_arg_length); |
| 457 | if (os.data != NULL) OPENSSL_free(os.data); |
| 458 | |
| 459 | ai.length=0; |
| 460 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1); |
| 461 | if (ai.data != NULL) |
| 462 | { |
| 463 | ret->time=ASN1_INTEGER_get(aip); |
| 464 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 465 | } |
| 466 | else |
| 467 | ret->time=(unsigned long)time(NULL); |
| 468 | |
| 469 | ai.length=0; |
| 470 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2); |
| 471 | if (ai.data != NULL) |
| 472 | { |
| 473 | ret->timeout=ASN1_INTEGER_get(aip); |
| 474 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 475 | } |
| 476 | else |
| 477 | ret->timeout=3; |
| 478 | |
| 479 | if (ret->peer != NULL) |
| 480 | { |
| 481 | X509_free(ret->peer); |
| 482 | ret->peer=NULL; |
| 483 | } |
| 484 | M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3); |
| 485 | |
| 486 | os.length=0; |
| 487 | os.data=NULL; |
| 488 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4); |
| 489 | |
| 490 | if(os.data != NULL) |
| 491 | { |
| 492 | if (os.length > SSL_MAX_SID_CTX_LENGTH) |
| 493 | { |
| 494 | c.error=SSL_R_BAD_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | goto err; |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | ret->sid_ctx_length=os.length; |
| 501 | memcpy(ret->sid_ctx,os.data,os.length); |
| 502 | } |
| 503 | OPENSSL_free(os.data); os.data=NULL; os.length=0; |
| 504 | } |
| 505 | else |
| 506 | ret->sid_ctx_length=0; |
| 507 | |
| 508 | ai.length=0; |
| 509 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5); |
| 510 | if (ai.data != NULL) |
| 511 | { |
| 512 | ret->verify_result=ASN1_INTEGER_get(aip); |
| 513 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 514 | } |
| 515 | else |
| 516 | ret->verify_result=X509_V_OK; |
| 517 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | os.length=0; |
| 519 | os.data=NULL; |
| 520 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,6); |
| 521 | if (os.data) |
| 522 | { |
| 523 | ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length); |
| 524 | OPENSSL_free(os.data); |
| 525 | os.data = NULL; |
| 526 | os.length = 0; |
| 527 | } |
| 528 | else |
| 529 | ret->tlsext_hostname=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 531 | os.length=0; |
| 532 | os.data=NULL; |
| 533 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,7); |
| 534 | if (os.data) |
| 535 | { |
| 536 | ret->psk_identity_hint = BUF_strndup((char *)os.data, os.length); |
| 537 | OPENSSL_free(os.data); |
| 538 | os.data = NULL; |
| 539 | os.length = 0; |
| 540 | } |
| 541 | else |
| 542 | ret->psk_identity_hint=NULL; |
| 543 | |
| 544 | os.length=0; |
| 545 | os.data=NULL; |
| 546 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,8); |
| 547 | if (os.data) |
| 548 | { |
| 549 | ret->psk_identity = BUF_strndup((char *)os.data, os.length); |
| 550 | OPENSSL_free(os.data); |
| 551 | os.data = NULL; |
| 552 | os.length = 0; |
| 553 | } |
| 554 | else |
| 555 | ret->psk_identity=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | ai.length=0; |
| 558 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,9); |
| 559 | if (ai.data != NULL) |
| 560 | { |
| 561 | ret->tlsext_tick_lifetime_hint=ASN1_INTEGER_get(aip); |
| 562 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 563 | } |
| 564 | else if (ret->tlsext_ticklen && ret->session_id_length) |
| 565 | ret->tlsext_tick_lifetime_hint = -1; |
| 566 | else |
| 567 | ret->tlsext_tick_lifetime_hint=0; |
| 568 | os.length=0; |
| 569 | os.data=NULL; |
| 570 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,10); |
| 571 | if (os.data) |
| 572 | { |
| 573 | ret->tlsext_tick = os.data; |
| 574 | ret->tlsext_ticklen = os.length; |
| 575 | os.data = NULL; |
| 576 | os.length = 0; |
| 577 | } |
| 578 | else |
| 579 | ret->tlsext_tick=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 580 | |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 581 | os.length=0; |
| 582 | os.data=NULL; |
| 583 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,13); |
| 584 | if (os.data && os.length == sizeof(ret->peer_sha256)) |
| 585 | { |
| 586 | memcpy(ret->peer_sha256, os.data, sizeof(ret->peer_sha256)); |
| 587 | ret->peer_sha256_valid = 1; |
| 588 | OPENSSL_free(os.data); |
| 589 | os.data = NULL; |
| 590 | } |
| 591 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 592 | os.length=0; |
| 593 | os.data=NULL; |
| 594 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,14); |
| 595 | if (os.data && os.length < (int)sizeof(ret->original_handshake_hash)) |
| 596 | { |
| 597 | memcpy(ret->original_handshake_hash, os.data, os.length); |
| 598 | ret->original_handshake_hash_len = os.length; |
| 599 | OPENSSL_free(os.data); |
| 600 | os.data = NULL; |
| 601 | } |
| 602 | |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 603 | os.length = 0; |
| 604 | os.data = NULL; |
| 605 | M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 15); |
| 606 | if (os.data) |
| 607 | { |
| 608 | if (ret->tlsext_signed_cert_timestamp_list) |
| 609 | OPENSSL_free(ret->tlsext_signed_cert_timestamp_list); |
| 610 | ret->tlsext_signed_cert_timestamp_list = os.data; |
| 611 | ret->tlsext_signed_cert_timestamp_list_length = os.length; |
| 612 | os.data = NULL; |
| 613 | } |
| 614 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 615 | os.length = 0; |
| 616 | os.data = NULL; |
| 617 | M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 16); |
| 618 | if (os.data) |
| 619 | { |
| 620 | if (ret->ocsp_response) |
| 621 | OPENSSL_free(ret->ocsp_response); |
| 622 | ret->ocsp_response = os.data; |
| 623 | ret->ocsp_response_length = os.length; |
| 624 | os.data = NULL; |
| 625 | } |
| 626 | |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 627 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION); |
| 629 | } |