blob: 36e4579a9b4a27f0cf1cced40925484d848df814 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
96OPENSSL_DECLARE_ERROR_REASON(SSL, CIPHER_CODE_WRONG_LENGTH);
97OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_SSL_VERSION);
98OPENSSL_DECLARE_ERROR_REASON(SSL, BAD_LENGTH);
99OPENSSL_DECLARE_ERROR_FUNCTION(SSL, D2I_SSL_SESSION);
100
101
102typedef 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;
115#ifndef OPENSSL_NO_TLSEXT
116 ASN1_OCTET_STRING tlsext_hostname;
117 ASN1_INTEGER tlsext_tick_lifetime;
118 ASN1_OCTET_STRING tlsext_tick;
119#endif /* OPENSSL_NO_TLSEXT */
120#ifndef OPENSSL_NO_PSK
121 ASN1_OCTET_STRING psk_identity_hint;
122 ASN1_OCTET_STRING psk_identity;
123#endif /* OPENSSL_NO_PSK */
Adam Langley75872532014-06-20 12:00:00 -0700124 ASN1_OCTET_STRING peer_sha256;
Adam Langley1258b6a2014-06-20 12:00:00 -0700125 ASN1_OCTET_STRING original_handshake_hash;
Adam Langley95c29f32014-06-20 12:00:00 -0700126 } SSL_SESSION_ASN1;
127
128int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
129 {
130#define LSIZE2 (sizeof(long)*2)
Adam Langley1258b6a2014-06-20 12:00:00 -0700131 int v1=0,v2=0,v3=0,v4=0,v5=0,v7=0,v8=0,v13=0,v14=0;
Adam Langley95c29f32014-06-20 12:00:00 -0700132 unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2];
133 unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2];
134#ifndef OPENSSL_NO_TLSEXT
135 int v6=0,v9=0,v10=0;
136 unsigned char ibuf6[LSIZE2];
137#endif
138 long l;
139 SSL_SESSION_ASN1 a;
140 M_ASN1_I2D_vars(in);
141
142 if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
143 return(0);
144
145 /* Note that I cheat in the following 2 assignments. I know
146 * that if the ASN1_INTEGER passed to ASN1_INTEGER_set
147 * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed.
148 * This is a bit evil but makes things simple, no dynamic allocation
149 * to clean up :-) */
150 a.version.length=LSIZE2;
151 a.version.type=V_ASN1_INTEGER;
152 a.version.data=ibuf1;
153 ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION);
154
155 a.ssl_version.length=LSIZE2;
156 a.ssl_version.type=V_ASN1_INTEGER;
157 a.ssl_version.data=ibuf2;
158 ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version);
159
160 a.cipher.type=V_ASN1_OCTET_STRING;
161 a.cipher.data=buf;
162
163 if (in->cipher == NULL)
164 l=in->cipher_id;
165 else
166 l=in->cipher->id;
167 if (in->ssl_version == SSL2_VERSION)
168 {
169 a.cipher.length=3;
170 buf[0]=((unsigned char)(l>>16L))&0xff;
171 buf[1]=((unsigned char)(l>> 8L))&0xff;
172 buf[2]=((unsigned char)(l ))&0xff;
173 }
174 else
175 {
176 a.cipher.length=2;
177 buf[0]=((unsigned char)(l>>8L))&0xff;
178 buf[1]=((unsigned char)(l ))&0xff;
179 }
180
181
182 a.master_key.length=in->master_key_length;
183 a.master_key.type=V_ASN1_OCTET_STRING;
184 a.master_key.data=in->master_key;
185
186 a.session_id.length=in->session_id_length;
187 a.session_id.type=V_ASN1_OCTET_STRING;
188 a.session_id.data=in->session_id;
189
190 a.session_id_context.length=in->sid_ctx_length;
191 a.session_id_context.type=V_ASN1_OCTET_STRING;
192 a.session_id_context.data=in->sid_ctx;
193
194 a.key_arg.length=in->key_arg_length;
195 a.key_arg.type=V_ASN1_OCTET_STRING;
196 a.key_arg.data=in->key_arg;
197
198 if (in->time != 0L)
199 {
200 a.time.length=LSIZE2;
201 a.time.type=V_ASN1_INTEGER;
202 a.time.data=ibuf3;
203 ASN1_INTEGER_set(&(a.time),in->time);
204 }
205
206 if (in->timeout != 0L)
207 {
208 a.timeout.length=LSIZE2;
209 a.timeout.type=V_ASN1_INTEGER;
210 a.timeout.data=ibuf4;
211 ASN1_INTEGER_set(&(a.timeout),in->timeout);
212 }
213
214 if (in->verify_result != X509_V_OK)
215 {
216 a.verify_result.length=LSIZE2;
217 a.verify_result.type=V_ASN1_INTEGER;
218 a.verify_result.data=ibuf5;
219 ASN1_INTEGER_set(&a.verify_result,in->verify_result);
220 }
221
222#ifndef OPENSSL_NO_TLSEXT
223 if (in->tlsext_hostname)
224 {
225 a.tlsext_hostname.length=strlen(in->tlsext_hostname);
226 a.tlsext_hostname.type=V_ASN1_OCTET_STRING;
227 a.tlsext_hostname.data=(unsigned char *)in->tlsext_hostname;
228 }
229 if (in->tlsext_tick)
230 {
231 a.tlsext_tick.length= in->tlsext_ticklen;
232 a.tlsext_tick.type=V_ASN1_OCTET_STRING;
233 a.tlsext_tick.data=(unsigned char *)in->tlsext_tick;
234 }
235 if (in->tlsext_tick_lifetime_hint > 0)
236 {
237 a.tlsext_tick_lifetime.length=LSIZE2;
238 a.tlsext_tick_lifetime.type=V_ASN1_INTEGER;
239 a.tlsext_tick_lifetime.data=ibuf6;
240 ASN1_INTEGER_set(&a.tlsext_tick_lifetime,in->tlsext_tick_lifetime_hint);
241 }
242#endif /* OPENSSL_NO_TLSEXT */
243#ifndef OPENSSL_NO_PSK
244 if (in->psk_identity_hint)
245 {
246 a.psk_identity_hint.length=strlen(in->psk_identity_hint);
247 a.psk_identity_hint.type=V_ASN1_OCTET_STRING;
248 a.psk_identity_hint.data=(unsigned char *)(in->psk_identity_hint);
249 }
250 if (in->psk_identity)
251 {
252 a.psk_identity.length=strlen(in->psk_identity);
253 a.psk_identity.type=V_ASN1_OCTET_STRING;
254 a.psk_identity.data=(unsigned char *)(in->psk_identity);
255 }
Adam Langley75872532014-06-20 12:00:00 -0700256
257 if (in->peer_sha256_valid)
258 {
259 a.peer_sha256.length = sizeof(in->peer_sha256);
260 a.peer_sha256.type = V_ASN1_OCTET_STRING;
261 a.peer_sha256.data = in->peer_sha256;
262 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700263
264 if (in->original_handshake_hash_len > 0)
265 {
266 a.original_handshake_hash.length = in->original_handshake_hash_len;
267 a.original_handshake_hash.type = V_ASN1_OCTET_STRING;
268 a.original_handshake_hash.data = in->original_handshake_hash;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270#endif /* OPENSSL_NO_PSK */
271
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 Langley75872532014-06-20 12:00:00 -0700283 if (in->peer != NULL && in->peer_sha256_valid == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700284 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
289#ifndef OPENSSL_NO_TLSEXT
290 if (in->tlsext_tick_lifetime_hint > 0)
291 M_ASN1_I2D_len_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9);
292 if (in->tlsext_tick)
293 M_ASN1_I2D_len_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10);
294 if (in->tlsext_hostname)
295 M_ASN1_I2D_len_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
296#endif /* OPENSSL_NO_TLSEXT */
297#ifndef OPENSSL_NO_PSK
298 if (in->psk_identity_hint)
299 M_ASN1_I2D_len_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7);
300 if (in->psk_identity)
301 M_ASN1_I2D_len_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8);
302#endif /* OPENSSL_NO_PSK */
Adam Langley75872532014-06-20 12:00:00 -0700303 if (in->peer_sha256_valid)
304 M_ASN1_I2D_len_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13);
Adam Langley1258b6a2014-06-20 12:00:00 -0700305 if (in->original_handshake_hash_len > 0)
306 M_ASN1_I2D_len_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14);
Adam Langley95c29f32014-06-20 12:00:00 -0700307
308 M_ASN1_I2D_seq_total();
309
310 M_ASN1_I2D_put(&(a.version), i2d_ASN1_INTEGER);
311 M_ASN1_I2D_put(&(a.ssl_version), i2d_ASN1_INTEGER);
312 M_ASN1_I2D_put(&(a.cipher), i2d_ASN1_OCTET_STRING);
313 M_ASN1_I2D_put(&(a.session_id), i2d_ASN1_OCTET_STRING);
314 M_ASN1_I2D_put(&(a.master_key), i2d_ASN1_OCTET_STRING);
315 if (in->key_arg_length > 0)
316 M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0);
317 if (in->time != 0L)
318 M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
319 if (in->timeout != 0L)
320 M_ASN1_I2D_put_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
Adam Langley75872532014-06-20 12:00:00 -0700321 if (in->peer != NULL && in->peer_sha256_valid == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700322 M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3);
323 M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,
324 v4);
325 if (in->verify_result != X509_V_OK)
326 M_ASN1_I2D_put_EXP_opt(&a.verify_result,i2d_ASN1_INTEGER,5,v5);
327#ifndef OPENSSL_NO_TLSEXT
328 if (in->tlsext_hostname)
329 M_ASN1_I2D_put_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
330#endif /* OPENSSL_NO_TLSEXT */
331#ifndef OPENSSL_NO_PSK
332 if (in->psk_identity_hint)
333 M_ASN1_I2D_put_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7);
334 if (in->psk_identity)
335 M_ASN1_I2D_put_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8);
336#endif /* OPENSSL_NO_PSK */
337#ifndef OPENSSL_NO_TLSEXT
338 if (in->tlsext_tick_lifetime_hint > 0)
339 M_ASN1_I2D_put_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9);
340 if (in->tlsext_tick)
341 M_ASN1_I2D_put_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10);
342#endif /* OPENSSL_NO_TLSEXT */
Adam Langley75872532014-06-20 12:00:00 -0700343 if (in->peer_sha256_valid)
344 M_ASN1_I2D_put_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13);
Adam Langley1258b6a2014-06-20 12:00:00 -0700345 if (in->original_handshake_hash_len > 0)
346 M_ASN1_I2D_put_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14);
347
Adam Langley95c29f32014-06-20 12:00:00 -0700348 M_ASN1_I2D_finish();
349 }
350
351SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
352 long length)
353 {
354 int ssl_version=0,i;
355 long id;
356 ASN1_INTEGER ai,*aip;
357 ASN1_OCTET_STRING os,*osp;
358 M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new);
359
360 aip= &ai;
361 osp= &os;
362
363 M_ASN1_D2I_Init();
364 M_ASN1_D2I_start_sequence();
365
366 ai.data=NULL; ai.length=0;
367 M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
368 if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
369
370 /* we don't care about the version right now :-) */
371 M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
372 ssl_version=(int)ASN1_INTEGER_get(aip);
373 ret->ssl_version=ssl_version;
374 if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
375
376 os.data=NULL; os.length=0;
377 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
378 if (ssl_version == SSL2_VERSION)
379 {
380 if (os.length != 3)
381 {
382 c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
383 goto err;
384 }
385 id=0x02000000L|
386 ((unsigned long)os.data[0]<<16L)|
387 ((unsigned long)os.data[1]<< 8L)|
388 (unsigned long)os.data[2];
389 }
390 else if ((ssl_version>>8) >= SSL3_VERSION_MAJOR)
391 {
392 if (os.length != 2)
393 {
394 c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
395 goto err;
396 }
397 id=0x03000000L|
398 ((unsigned long)os.data[0]<<8L)|
399 (unsigned long)os.data[1];
400 }
401 else
402 {
403 c.error=SSL_R_UNKNOWN_SSL_VERSION;
404 goto err;
405 }
406
407 ret->cipher=NULL;
408 ret->cipher_id=id;
409
410 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
411 if ((ssl_version>>8) >= SSL3_VERSION_MAJOR)
412 i=SSL3_MAX_SSL_SESSION_ID_LENGTH;
413 else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */
414 i=SSL2_MAX_SSL_SESSION_ID_LENGTH;
415
416 if (os.length > i)
417 os.length = i;
418 if (os.length > (int)sizeof(ret->session_id)) /* can't happen */
419 os.length = sizeof(ret->session_id);
420
421 ret->session_id_length=os.length;
422 assert(os.length <= (int)sizeof(ret->session_id));
423 memcpy(ret->session_id,os.data,os.length);
424
425 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
426 if (os.length > SSL_MAX_MASTER_KEY_LENGTH)
427 ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
428 else
429 ret->master_key_length=os.length;
430 memcpy(ret->master_key,os.data,ret->master_key_length);
431
432 os.length=0;
433
434 M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING);
435 if (os.length > SSL_MAX_KEY_ARG_LENGTH)
436 ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH;
437 else
438 ret->key_arg_length=os.length;
439 memcpy(ret->key_arg,os.data,ret->key_arg_length);
440 if (os.data != NULL) OPENSSL_free(os.data);
441
442 ai.length=0;
443 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1);
444 if (ai.data != NULL)
445 {
446 ret->time=ASN1_INTEGER_get(aip);
447 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
448 }
449 else
450 ret->time=(unsigned long)time(NULL);
451
452 ai.length=0;
453 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2);
454 if (ai.data != NULL)
455 {
456 ret->timeout=ASN1_INTEGER_get(aip);
457 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
458 }
459 else
460 ret->timeout=3;
461
462 if (ret->peer != NULL)
463 {
464 X509_free(ret->peer);
465 ret->peer=NULL;
466 }
467 M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3);
468
469 os.length=0;
470 os.data=NULL;
471 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4);
472
473 if(os.data != NULL)
474 {
475 if (os.length > SSL_MAX_SID_CTX_LENGTH)
476 {
477 c.error=SSL_R_BAD_LENGTH;
478 goto err;
479 }
480 else
481 {
482 ret->sid_ctx_length=os.length;
483 memcpy(ret->sid_ctx,os.data,os.length);
484 }
485 OPENSSL_free(os.data); os.data=NULL; os.length=0;
486 }
487 else
488 ret->sid_ctx_length=0;
489
490 ai.length=0;
491 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5);
492 if (ai.data != NULL)
493 {
494 ret->verify_result=ASN1_INTEGER_get(aip);
495 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
496 }
497 else
498 ret->verify_result=X509_V_OK;
499
500#ifndef OPENSSL_NO_TLSEXT
501 os.length=0;
502 os.data=NULL;
503 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,6);
504 if (os.data)
505 {
506 ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length);
507 OPENSSL_free(os.data);
508 os.data = NULL;
509 os.length = 0;
510 }
511 else
512 ret->tlsext_hostname=NULL;
513#endif /* OPENSSL_NO_TLSEXT */
514
515#ifndef OPENSSL_NO_PSK
516 os.length=0;
517 os.data=NULL;
518 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,7);
519 if (os.data)
520 {
521 ret->psk_identity_hint = BUF_strndup((char *)os.data, os.length);
522 OPENSSL_free(os.data);
523 os.data = NULL;
524 os.length = 0;
525 }
526 else
527 ret->psk_identity_hint=NULL;
528
529 os.length=0;
530 os.data=NULL;
531 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,8);
532 if (os.data)
533 {
534 ret->psk_identity = BUF_strndup((char *)os.data, os.length);
535 OPENSSL_free(os.data);
536 os.data = NULL;
537 os.length = 0;
538 }
539 else
540 ret->psk_identity=NULL;
541#endif /* OPENSSL_NO_PSK */
542
543#ifndef OPENSSL_NO_TLSEXT
544 ai.length=0;
545 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,9);
546 if (ai.data != NULL)
547 {
548 ret->tlsext_tick_lifetime_hint=ASN1_INTEGER_get(aip);
549 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
550 }
551 else if (ret->tlsext_ticklen && ret->session_id_length)
552 ret->tlsext_tick_lifetime_hint = -1;
553 else
554 ret->tlsext_tick_lifetime_hint=0;
555 os.length=0;
556 os.data=NULL;
557 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,10);
558 if (os.data)
559 {
560 ret->tlsext_tick = os.data;
561 ret->tlsext_ticklen = os.length;
562 os.data = NULL;
563 os.length = 0;
564 }
565 else
566 ret->tlsext_tick=NULL;
567#endif /* OPENSSL_NO_TLSEXT */
568
Adam Langley75872532014-06-20 12:00:00 -0700569 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 Langley1258b6a2014-06-20 12:00:00 -0700580 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
Adam Langley95c29f32014-06-20 12:00:00 -0700591 M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION);
592 }