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; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | } SSL_SESSION_ASN1; |
| 124 | |
| 125 | int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp) |
| 126 | { |
| 127 | #define LSIZE2 (sizeof(long)*2) |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 128 | int v1=0,v2=0,v3=0,v4=0,v5=0,v7=0,v8=0,v13=0,v14=0,v15=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2]; |
| 130 | unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | int v6=0,v9=0,v10=0; |
| 132 | unsigned char ibuf6[LSIZE2]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | long l; |
| 134 | SSL_SESSION_ASN1 a; |
| 135 | M_ASN1_I2D_vars(in); |
| 136 | |
| 137 | if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0))) |
| 138 | return(0); |
| 139 | |
| 140 | /* Note that I cheat in the following 2 assignments. I know |
| 141 | * that if the ASN1_INTEGER passed to ASN1_INTEGER_set |
| 142 | * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed. |
| 143 | * This is a bit evil but makes things simple, no dynamic allocation |
| 144 | * to clean up :-) */ |
| 145 | a.version.length=LSIZE2; |
| 146 | a.version.type=V_ASN1_INTEGER; |
| 147 | a.version.data=ibuf1; |
| 148 | ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION); |
| 149 | |
| 150 | a.ssl_version.length=LSIZE2; |
| 151 | a.ssl_version.type=V_ASN1_INTEGER; |
| 152 | a.ssl_version.data=ibuf2; |
| 153 | ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version); |
| 154 | |
| 155 | a.cipher.type=V_ASN1_OCTET_STRING; |
| 156 | a.cipher.data=buf; |
| 157 | |
| 158 | if (in->cipher == NULL) |
| 159 | l=in->cipher_id; |
| 160 | else |
| 161 | l=in->cipher->id; |
| 162 | if (in->ssl_version == SSL2_VERSION) |
| 163 | { |
| 164 | a.cipher.length=3; |
| 165 | buf[0]=((unsigned char)(l>>16L))&0xff; |
| 166 | buf[1]=((unsigned char)(l>> 8L))&0xff; |
| 167 | buf[2]=((unsigned char)(l ))&0xff; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | a.cipher.length=2; |
| 172 | buf[0]=((unsigned char)(l>>8L))&0xff; |
| 173 | buf[1]=((unsigned char)(l ))&0xff; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | a.master_key.length=in->master_key_length; |
| 178 | a.master_key.type=V_ASN1_OCTET_STRING; |
| 179 | a.master_key.data=in->master_key; |
| 180 | |
| 181 | a.session_id.length=in->session_id_length; |
| 182 | a.session_id.type=V_ASN1_OCTET_STRING; |
| 183 | a.session_id.data=in->session_id; |
| 184 | |
| 185 | a.session_id_context.length=in->sid_ctx_length; |
| 186 | a.session_id_context.type=V_ASN1_OCTET_STRING; |
| 187 | a.session_id_context.data=in->sid_ctx; |
| 188 | |
| 189 | a.key_arg.length=in->key_arg_length; |
| 190 | a.key_arg.type=V_ASN1_OCTET_STRING; |
| 191 | a.key_arg.data=in->key_arg; |
| 192 | |
| 193 | if (in->time != 0L) |
| 194 | { |
| 195 | a.time.length=LSIZE2; |
| 196 | a.time.type=V_ASN1_INTEGER; |
| 197 | a.time.data=ibuf3; |
| 198 | ASN1_INTEGER_set(&(a.time),in->time); |
| 199 | } |
| 200 | |
| 201 | if (in->timeout != 0L) |
| 202 | { |
| 203 | a.timeout.length=LSIZE2; |
| 204 | a.timeout.type=V_ASN1_INTEGER; |
| 205 | a.timeout.data=ibuf4; |
| 206 | ASN1_INTEGER_set(&(a.timeout),in->timeout); |
| 207 | } |
| 208 | |
| 209 | if (in->verify_result != X509_V_OK) |
| 210 | { |
| 211 | a.verify_result.length=LSIZE2; |
| 212 | a.verify_result.type=V_ASN1_INTEGER; |
| 213 | a.verify_result.data=ibuf5; |
| 214 | ASN1_INTEGER_set(&a.verify_result,in->verify_result); |
| 215 | } |
| 216 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | if (in->tlsext_hostname) |
| 218 | { |
| 219 | a.tlsext_hostname.length=strlen(in->tlsext_hostname); |
| 220 | a.tlsext_hostname.type=V_ASN1_OCTET_STRING; |
| 221 | a.tlsext_hostname.data=(unsigned char *)in->tlsext_hostname; |
| 222 | } |
| 223 | if (in->tlsext_tick) |
| 224 | { |
| 225 | a.tlsext_tick.length= in->tlsext_ticklen; |
| 226 | a.tlsext_tick.type=V_ASN1_OCTET_STRING; |
| 227 | a.tlsext_tick.data=(unsigned char *)in->tlsext_tick; |
| 228 | } |
| 229 | if (in->tlsext_tick_lifetime_hint > 0) |
| 230 | { |
| 231 | a.tlsext_tick_lifetime.length=LSIZE2; |
| 232 | a.tlsext_tick_lifetime.type=V_ASN1_INTEGER; |
| 233 | a.tlsext_tick_lifetime.data=ibuf6; |
| 234 | ASN1_INTEGER_set(&a.tlsext_tick_lifetime,in->tlsext_tick_lifetime_hint); |
| 235 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | if (in->psk_identity_hint) |
| 237 | { |
| 238 | a.psk_identity_hint.length=strlen(in->psk_identity_hint); |
| 239 | a.psk_identity_hint.type=V_ASN1_OCTET_STRING; |
| 240 | a.psk_identity_hint.data=(unsigned char *)(in->psk_identity_hint); |
| 241 | } |
| 242 | if (in->psk_identity) |
| 243 | { |
| 244 | a.psk_identity.length=strlen(in->psk_identity); |
| 245 | a.psk_identity.type=V_ASN1_OCTET_STRING; |
| 246 | a.psk_identity.data=(unsigned char *)(in->psk_identity); |
| 247 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
| 249 | if (in->peer_sha256_valid) |
| 250 | { |
| 251 | a.peer_sha256.length = sizeof(in->peer_sha256); |
| 252 | a.peer_sha256.type = V_ASN1_OCTET_STRING; |
| 253 | a.peer_sha256.data = in->peer_sha256; |
| 254 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | |
| 256 | if (in->original_handshake_hash_len > 0) |
| 257 | { |
| 258 | a.original_handshake_hash.length = in->original_handshake_hash_len; |
| 259 | a.original_handshake_hash.type = V_ASN1_OCTET_STRING; |
| 260 | a.original_handshake_hash.data = in->original_handshake_hash; |
| 261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 263 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 264 | { |
| 265 | a.tlsext_signed_cert_timestamp_list.length = |
| 266 | in->tlsext_signed_cert_timestamp_list_length; |
| 267 | a.tlsext_signed_cert_timestamp_list.type = V_ASN1_OCTET_STRING; |
| 268 | a.tlsext_signed_cert_timestamp_list.data = |
| 269 | in->tlsext_signed_cert_timestamp_list; |
| 270 | } |
| 271 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | M_ASN1_I2D_len(&(a.version), i2d_ASN1_INTEGER); |
| 273 | M_ASN1_I2D_len(&(a.ssl_version), i2d_ASN1_INTEGER); |
| 274 | M_ASN1_I2D_len(&(a.cipher), i2d_ASN1_OCTET_STRING); |
| 275 | M_ASN1_I2D_len(&(a.session_id), i2d_ASN1_OCTET_STRING); |
| 276 | M_ASN1_I2D_len(&(a.master_key), i2d_ASN1_OCTET_STRING); |
| 277 | if (in->key_arg_length > 0) |
| 278 | M_ASN1_I2D_len_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING); |
| 279 | if (in->time != 0L) |
| 280 | M_ASN1_I2D_len_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1); |
| 281 | if (in->timeout != 0L) |
| 282 | 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] | 283 | if (in->peer != NULL && in->peer_sha256_valid == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | M_ASN1_I2D_len_EXP_opt(in->peer,i2d_X509,3,v3); |
| 285 | M_ASN1_I2D_len_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,v4); |
| 286 | if (in->verify_result != X509_V_OK) |
| 287 | M_ASN1_I2D_len_EXP_opt(&(a.verify_result),i2d_ASN1_INTEGER,5,v5); |
| 288 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | if (in->tlsext_tick_lifetime_hint > 0) |
| 290 | M_ASN1_I2D_len_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9); |
| 291 | if (in->tlsext_tick) |
| 292 | M_ASN1_I2D_len_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10); |
| 293 | if (in->tlsext_hostname) |
| 294 | 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] | 295 | if (in->psk_identity_hint) |
| 296 | M_ASN1_I2D_len_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7); |
| 297 | if (in->psk_identity) |
| 298 | 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] | 299 | if (in->peer_sha256_valid) |
| 300 | 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] | 301 | if (in->original_handshake_hash_len > 0) |
| 302 | 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] | 303 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 304 | M_ASN1_I2D_len_EXP_opt(&(a.tlsext_signed_cert_timestamp_list), |
| 305 | i2d_ASN1_OCTET_STRING, 15, v15); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | |
| 307 | M_ASN1_I2D_seq_total(); |
| 308 | |
| 309 | M_ASN1_I2D_put(&(a.version), i2d_ASN1_INTEGER); |
| 310 | M_ASN1_I2D_put(&(a.ssl_version), i2d_ASN1_INTEGER); |
| 311 | M_ASN1_I2D_put(&(a.cipher), i2d_ASN1_OCTET_STRING); |
| 312 | M_ASN1_I2D_put(&(a.session_id), i2d_ASN1_OCTET_STRING); |
| 313 | M_ASN1_I2D_put(&(a.master_key), i2d_ASN1_OCTET_STRING); |
| 314 | if (in->key_arg_length > 0) |
| 315 | M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0); |
| 316 | if (in->time != 0L) |
| 317 | M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1); |
| 318 | if (in->timeout != 0L) |
| 319 | 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] | 320 | if (in->peer != NULL && in->peer_sha256_valid == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3); |
| 322 | M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4, |
| 323 | v4); |
| 324 | if (in->verify_result != X509_V_OK) |
| 325 | 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] | 326 | if (in->tlsext_hostname) |
| 327 | 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] | 328 | if (in->psk_identity_hint) |
| 329 | M_ASN1_I2D_put_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7); |
| 330 | if (in->psk_identity) |
| 331 | 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] | 332 | if (in->tlsext_tick_lifetime_hint > 0) |
| 333 | M_ASN1_I2D_put_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9); |
| 334 | if (in->tlsext_tick) |
| 335 | 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] | 336 | if (in->peer_sha256_valid) |
| 337 | 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] | 338 | if (in->original_handshake_hash_len > 0) |
| 339 | 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] | 340 | if (in->tlsext_signed_cert_timestamp_list_length > 0) |
| 341 | M_ASN1_I2D_put_EXP_opt(&(a.tlsext_signed_cert_timestamp_list), |
| 342 | i2d_ASN1_OCTET_STRING, 15, v15); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 343 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | M_ASN1_I2D_finish(); |
| 345 | } |
| 346 | |
| 347 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp, |
| 348 | long length) |
| 349 | { |
| 350 | int ssl_version=0,i; |
| 351 | long id; |
| 352 | ASN1_INTEGER ai,*aip; |
| 353 | ASN1_OCTET_STRING os,*osp; |
| 354 | M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new); |
| 355 | |
| 356 | aip= &ai; |
| 357 | osp= &os; |
| 358 | |
| 359 | M_ASN1_D2I_Init(); |
| 360 | M_ASN1_D2I_start_sequence(); |
| 361 | |
| 362 | ai.data=NULL; ai.length=0; |
| 363 | M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER); |
| 364 | if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; } |
| 365 | |
| 366 | /* we don't care about the version right now :-) */ |
| 367 | M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER); |
| 368 | ssl_version=(int)ASN1_INTEGER_get(aip); |
| 369 | ret->ssl_version=ssl_version; |
| 370 | if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; } |
| 371 | |
| 372 | os.data=NULL; os.length=0; |
| 373 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 374 | if (ssl_version == SSL2_VERSION) |
| 375 | { |
| 376 | if (os.length != 3) |
| 377 | { |
| 378 | c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | goto err; |
| 381 | } |
| 382 | id=0x02000000L| |
| 383 | ((unsigned long)os.data[0]<<16L)| |
| 384 | ((unsigned long)os.data[1]<< 8L)| |
| 385 | (unsigned long)os.data[2]; |
| 386 | } |
| 387 | else if ((ssl_version>>8) >= SSL3_VERSION_MAJOR) |
| 388 | { |
| 389 | if (os.length != 2) |
| 390 | { |
| 391 | c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | goto err; |
| 394 | } |
| 395 | id=0x03000000L| |
| 396 | ((unsigned long)os.data[0]<<8L)| |
| 397 | (unsigned long)os.data[1]; |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | c.error=SSL_R_UNKNOWN_SSL_VERSION; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | goto err; |
| 404 | } |
| 405 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | ret->cipher_id=id; |
Adam Langley | 82b7da2 | 2014-08-13 12:28:02 -0700 | [diff] [blame] | 407 | ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff); |
| 408 | if (ret->cipher == NULL) |
| 409 | { |
| 410 | c.error=SSL_R_UNSUPPORTED_CIPHER; |
| 411 | c.line = __LINE__; |
| 412 | goto err; |
| 413 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
| 415 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 416 | if ((ssl_version>>8) >= SSL3_VERSION_MAJOR) |
| 417 | i=SSL3_MAX_SSL_SESSION_ID_LENGTH; |
| 418 | else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */ |
| 419 | i=SSL2_MAX_SSL_SESSION_ID_LENGTH; |
| 420 | |
| 421 | if (os.length > i) |
| 422 | os.length = i; |
| 423 | if (os.length > (int)sizeof(ret->session_id)) /* can't happen */ |
| 424 | os.length = sizeof(ret->session_id); |
| 425 | |
| 426 | ret->session_id_length=os.length; |
| 427 | assert(os.length <= (int)sizeof(ret->session_id)); |
| 428 | memcpy(ret->session_id,os.data,os.length); |
| 429 | |
| 430 | M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING); |
| 431 | if (os.length > SSL_MAX_MASTER_KEY_LENGTH) |
| 432 | ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; |
| 433 | else |
| 434 | ret->master_key_length=os.length; |
| 435 | memcpy(ret->master_key,os.data,ret->master_key_length); |
| 436 | |
| 437 | os.length=0; |
| 438 | |
| 439 | M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING); |
| 440 | if (os.length > SSL_MAX_KEY_ARG_LENGTH) |
| 441 | ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH; |
| 442 | else |
| 443 | ret->key_arg_length=os.length; |
| 444 | memcpy(ret->key_arg,os.data,ret->key_arg_length); |
| 445 | if (os.data != NULL) OPENSSL_free(os.data); |
| 446 | |
| 447 | ai.length=0; |
| 448 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1); |
| 449 | if (ai.data != NULL) |
| 450 | { |
| 451 | ret->time=ASN1_INTEGER_get(aip); |
| 452 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 453 | } |
| 454 | else |
| 455 | ret->time=(unsigned long)time(NULL); |
| 456 | |
| 457 | ai.length=0; |
| 458 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2); |
| 459 | if (ai.data != NULL) |
| 460 | { |
| 461 | ret->timeout=ASN1_INTEGER_get(aip); |
| 462 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 463 | } |
| 464 | else |
| 465 | ret->timeout=3; |
| 466 | |
| 467 | if (ret->peer != NULL) |
| 468 | { |
| 469 | X509_free(ret->peer); |
| 470 | ret->peer=NULL; |
| 471 | } |
| 472 | M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3); |
| 473 | |
| 474 | os.length=0; |
| 475 | os.data=NULL; |
| 476 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4); |
| 477 | |
| 478 | if(os.data != NULL) |
| 479 | { |
| 480 | if (os.length > SSL_MAX_SID_CTX_LENGTH) |
| 481 | { |
| 482 | c.error=SSL_R_BAD_LENGTH; |
Adam Langley | f77452c | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | c.line=__LINE__; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | goto err; |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | ret->sid_ctx_length=os.length; |
| 489 | memcpy(ret->sid_ctx,os.data,os.length); |
| 490 | } |
| 491 | OPENSSL_free(os.data); os.data=NULL; os.length=0; |
| 492 | } |
| 493 | else |
| 494 | ret->sid_ctx_length=0; |
| 495 | |
| 496 | ai.length=0; |
| 497 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5); |
| 498 | if (ai.data != NULL) |
| 499 | { |
| 500 | ret->verify_result=ASN1_INTEGER_get(aip); |
| 501 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 502 | } |
| 503 | else |
| 504 | ret->verify_result=X509_V_OK; |
| 505 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | os.length=0; |
| 507 | os.data=NULL; |
| 508 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,6); |
| 509 | if (os.data) |
| 510 | { |
| 511 | ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length); |
| 512 | OPENSSL_free(os.data); |
| 513 | os.data = NULL; |
| 514 | os.length = 0; |
| 515 | } |
| 516 | else |
| 517 | ret->tlsext_hostname=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | os.length=0; |
| 520 | os.data=NULL; |
| 521 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,7); |
| 522 | if (os.data) |
| 523 | { |
| 524 | ret->psk_identity_hint = BUF_strndup((char *)os.data, os.length); |
| 525 | OPENSSL_free(os.data); |
| 526 | os.data = NULL; |
| 527 | os.length = 0; |
| 528 | } |
| 529 | else |
| 530 | ret->psk_identity_hint=NULL; |
| 531 | |
| 532 | os.length=0; |
| 533 | os.data=NULL; |
| 534 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,8); |
| 535 | if (os.data) |
| 536 | { |
| 537 | ret->psk_identity = BUF_strndup((char *)os.data, os.length); |
| 538 | OPENSSL_free(os.data); |
| 539 | os.data = NULL; |
| 540 | os.length = 0; |
| 541 | } |
| 542 | else |
| 543 | ret->psk_identity=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | ai.length=0; |
| 546 | M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,9); |
| 547 | if (ai.data != NULL) |
| 548 | { |
| 549 | ret->tlsext_tick_lifetime_hint=ASN1_INTEGER_get(aip); |
| 550 | OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; |
| 551 | } |
| 552 | else if (ret->tlsext_ticklen && ret->session_id_length) |
| 553 | ret->tlsext_tick_lifetime_hint = -1; |
| 554 | else |
| 555 | ret->tlsext_tick_lifetime_hint=0; |
| 556 | os.length=0; |
| 557 | os.data=NULL; |
| 558 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,10); |
| 559 | if (os.data) |
| 560 | { |
| 561 | ret->tlsext_tick = os.data; |
| 562 | ret->tlsext_ticklen = os.length; |
| 563 | os.data = NULL; |
| 564 | os.length = 0; |
| 565 | } |
| 566 | else |
| 567 | ret->tlsext_tick=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | os.length=0; |
| 570 | os.data=NULL; |
| 571 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,13); |
| 572 | if (os.data && os.length == sizeof(ret->peer_sha256)) |
| 573 | { |
| 574 | memcpy(ret->peer_sha256, os.data, sizeof(ret->peer_sha256)); |
| 575 | ret->peer_sha256_valid = 1; |
| 576 | OPENSSL_free(os.data); |
| 577 | os.data = NULL; |
| 578 | } |
| 579 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 580 | os.length=0; |
| 581 | os.data=NULL; |
| 582 | M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,14); |
| 583 | if (os.data && os.length < (int)sizeof(ret->original_handshake_hash)) |
| 584 | { |
| 585 | memcpy(ret->original_handshake_hash, os.data, os.length); |
| 586 | ret->original_handshake_hash_len = os.length; |
| 587 | OPENSSL_free(os.data); |
| 588 | os.data = NULL; |
| 589 | } |
| 590 | |
Håvard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 591 | os.length = 0; |
| 592 | os.data = NULL; |
| 593 | M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 15); |
| 594 | if (os.data) |
| 595 | { |
| 596 | if (ret->tlsext_signed_cert_timestamp_list) |
| 597 | OPENSSL_free(ret->tlsext_signed_cert_timestamp_list); |
| 598 | ret->tlsext_signed_cert_timestamp_list = os.data; |
| 599 | ret->tlsext_signed_cert_timestamp_list_length = os.length; |
| 600 | os.data = NULL; |
| 601 | } |
| 602 | |
| 603 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 604 | M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION); |
| 605 | } |