blob: 9f372f98f6c68d26ee3da7c6e4f804aa10ca87e8 [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;
Adam Langley95c29f32014-06-20 12:00:00 -0700115 ASN1_OCTET_STRING tlsext_hostname;
116 ASN1_INTEGER tlsext_tick_lifetime;
117 ASN1_OCTET_STRING tlsext_tick;
Adam Langley95c29f32014-06-20 12:00:00 -0700118 ASN1_OCTET_STRING psk_identity_hint;
119 ASN1_OCTET_STRING psk_identity;
Adam Langley75872532014-06-20 12:00:00 -0700120 ASN1_OCTET_STRING peer_sha256;
Adam Langley1258b6a2014-06-20 12:00:00 -0700121 ASN1_OCTET_STRING original_handshake_hash;
Adam Langley95c29f32014-06-20 12:00:00 -0700122 } SSL_SESSION_ASN1;
123
124int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
125 {
126#define LSIZE2 (sizeof(long)*2)
Adam Langley1258b6a2014-06-20 12:00:00 -0700127 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 -0700128 unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2];
129 unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2];
Adam Langley95c29f32014-06-20 12:00:00 -0700130 int v6=0,v9=0,v10=0;
131 unsigned char ibuf6[LSIZE2];
Adam Langley95c29f32014-06-20 12:00:00 -0700132 long l;
133 SSL_SESSION_ASN1 a;
134 M_ASN1_I2D_vars(in);
135
136 if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
137 return(0);
138
139 /* Note that I cheat in the following 2 assignments. I know
140 * that if the ASN1_INTEGER passed to ASN1_INTEGER_set
141 * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed.
142 * This is a bit evil but makes things simple, no dynamic allocation
143 * to clean up :-) */
144 a.version.length=LSIZE2;
145 a.version.type=V_ASN1_INTEGER;
146 a.version.data=ibuf1;
147 ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION);
148
149 a.ssl_version.length=LSIZE2;
150 a.ssl_version.type=V_ASN1_INTEGER;
151 a.ssl_version.data=ibuf2;
152 ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version);
153
154 a.cipher.type=V_ASN1_OCTET_STRING;
155 a.cipher.data=buf;
156
157 if (in->cipher == NULL)
158 l=in->cipher_id;
159 else
160 l=in->cipher->id;
161 if (in->ssl_version == SSL2_VERSION)
162 {
163 a.cipher.length=3;
164 buf[0]=((unsigned char)(l>>16L))&0xff;
165 buf[1]=((unsigned char)(l>> 8L))&0xff;
166 buf[2]=((unsigned char)(l ))&0xff;
167 }
168 else
169 {
170 a.cipher.length=2;
171 buf[0]=((unsigned char)(l>>8L))&0xff;
172 buf[1]=((unsigned char)(l ))&0xff;
173 }
174
175
176 a.master_key.length=in->master_key_length;
177 a.master_key.type=V_ASN1_OCTET_STRING;
178 a.master_key.data=in->master_key;
179
180 a.session_id.length=in->session_id_length;
181 a.session_id.type=V_ASN1_OCTET_STRING;
182 a.session_id.data=in->session_id;
183
184 a.session_id_context.length=in->sid_ctx_length;
185 a.session_id_context.type=V_ASN1_OCTET_STRING;
186 a.session_id_context.data=in->sid_ctx;
187
188 a.key_arg.length=in->key_arg_length;
189 a.key_arg.type=V_ASN1_OCTET_STRING;
190 a.key_arg.data=in->key_arg;
191
192 if (in->time != 0L)
193 {
194 a.time.length=LSIZE2;
195 a.time.type=V_ASN1_INTEGER;
196 a.time.data=ibuf3;
197 ASN1_INTEGER_set(&(a.time),in->time);
198 }
199
200 if (in->timeout != 0L)
201 {
202 a.timeout.length=LSIZE2;
203 a.timeout.type=V_ASN1_INTEGER;
204 a.timeout.data=ibuf4;
205 ASN1_INTEGER_set(&(a.timeout),in->timeout);
206 }
207
208 if (in->verify_result != X509_V_OK)
209 {
210 a.verify_result.length=LSIZE2;
211 a.verify_result.type=V_ASN1_INTEGER;
212 a.verify_result.data=ibuf5;
213 ASN1_INTEGER_set(&a.verify_result,in->verify_result);
214 }
215
Adam Langley95c29f32014-06-20 12:00:00 -0700216 if (in->tlsext_hostname)
217 {
218 a.tlsext_hostname.length=strlen(in->tlsext_hostname);
219 a.tlsext_hostname.type=V_ASN1_OCTET_STRING;
220 a.tlsext_hostname.data=(unsigned char *)in->tlsext_hostname;
221 }
222 if (in->tlsext_tick)
223 {
224 a.tlsext_tick.length= in->tlsext_ticklen;
225 a.tlsext_tick.type=V_ASN1_OCTET_STRING;
226 a.tlsext_tick.data=(unsigned char *)in->tlsext_tick;
227 }
228 if (in->tlsext_tick_lifetime_hint > 0)
229 {
230 a.tlsext_tick_lifetime.length=LSIZE2;
231 a.tlsext_tick_lifetime.type=V_ASN1_INTEGER;
232 a.tlsext_tick_lifetime.data=ibuf6;
233 ASN1_INTEGER_set(&a.tlsext_tick_lifetime,in->tlsext_tick_lifetime_hint);
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235 if (in->psk_identity_hint)
236 {
237 a.psk_identity_hint.length=strlen(in->psk_identity_hint);
238 a.psk_identity_hint.type=V_ASN1_OCTET_STRING;
239 a.psk_identity_hint.data=(unsigned char *)(in->psk_identity_hint);
240 }
241 if (in->psk_identity)
242 {
243 a.psk_identity.length=strlen(in->psk_identity);
244 a.psk_identity.type=V_ASN1_OCTET_STRING;
245 a.psk_identity.data=(unsigned char *)(in->psk_identity);
246 }
Adam Langley75872532014-06-20 12:00:00 -0700247
248 if (in->peer_sha256_valid)
249 {
250 a.peer_sha256.length = sizeof(in->peer_sha256);
251 a.peer_sha256.type = V_ASN1_OCTET_STRING;
252 a.peer_sha256.data = in->peer_sha256;
253 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700254
255 if (in->original_handshake_hash_len > 0)
256 {
257 a.original_handshake_hash.length = in->original_handshake_hash_len;
258 a.original_handshake_hash.type = V_ASN1_OCTET_STRING;
259 a.original_handshake_hash.data = in->original_handshake_hash;
260 }
Adam Langley95c29f32014-06-20 12:00:00 -0700261
262 M_ASN1_I2D_len(&(a.version), i2d_ASN1_INTEGER);
263 M_ASN1_I2D_len(&(a.ssl_version), i2d_ASN1_INTEGER);
264 M_ASN1_I2D_len(&(a.cipher), i2d_ASN1_OCTET_STRING);
265 M_ASN1_I2D_len(&(a.session_id), i2d_ASN1_OCTET_STRING);
266 M_ASN1_I2D_len(&(a.master_key), i2d_ASN1_OCTET_STRING);
267 if (in->key_arg_length > 0)
268 M_ASN1_I2D_len_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING);
269 if (in->time != 0L)
270 M_ASN1_I2D_len_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
271 if (in->timeout != 0L)
272 M_ASN1_I2D_len_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
Adam Langley75872532014-06-20 12:00:00 -0700273 if (in->peer != NULL && in->peer_sha256_valid == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700274 M_ASN1_I2D_len_EXP_opt(in->peer,i2d_X509,3,v3);
275 M_ASN1_I2D_len_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,v4);
276 if (in->verify_result != X509_V_OK)
277 M_ASN1_I2D_len_EXP_opt(&(a.verify_result),i2d_ASN1_INTEGER,5,v5);
278
Adam Langley95c29f32014-06-20 12:00:00 -0700279 if (in->tlsext_tick_lifetime_hint > 0)
280 M_ASN1_I2D_len_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9);
281 if (in->tlsext_tick)
282 M_ASN1_I2D_len_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10);
283 if (in->tlsext_hostname)
284 M_ASN1_I2D_len_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
Adam Langley95c29f32014-06-20 12:00:00 -0700285 if (in->psk_identity_hint)
286 M_ASN1_I2D_len_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7);
287 if (in->psk_identity)
288 M_ASN1_I2D_len_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8);
Adam Langley75872532014-06-20 12:00:00 -0700289 if (in->peer_sha256_valid)
290 M_ASN1_I2D_len_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13);
Adam Langley1258b6a2014-06-20 12:00:00 -0700291 if (in->original_handshake_hash_len > 0)
292 M_ASN1_I2D_len_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14);
Adam Langley95c29f32014-06-20 12:00:00 -0700293
294 M_ASN1_I2D_seq_total();
295
296 M_ASN1_I2D_put(&(a.version), i2d_ASN1_INTEGER);
297 M_ASN1_I2D_put(&(a.ssl_version), i2d_ASN1_INTEGER);
298 M_ASN1_I2D_put(&(a.cipher), i2d_ASN1_OCTET_STRING);
299 M_ASN1_I2D_put(&(a.session_id), i2d_ASN1_OCTET_STRING);
300 M_ASN1_I2D_put(&(a.master_key), i2d_ASN1_OCTET_STRING);
301 if (in->key_arg_length > 0)
302 M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0);
303 if (in->time != 0L)
304 M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
305 if (in->timeout != 0L)
306 M_ASN1_I2D_put_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
Adam Langley75872532014-06-20 12:00:00 -0700307 if (in->peer != NULL && in->peer_sha256_valid == 0)
Adam Langley95c29f32014-06-20 12:00:00 -0700308 M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3);
309 M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,
310 v4);
311 if (in->verify_result != X509_V_OK)
312 M_ASN1_I2D_put_EXP_opt(&a.verify_result,i2d_ASN1_INTEGER,5,v5);
Adam Langley95c29f32014-06-20 12:00:00 -0700313 if (in->tlsext_hostname)
314 M_ASN1_I2D_put_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
Adam Langley95c29f32014-06-20 12:00:00 -0700315 if (in->psk_identity_hint)
316 M_ASN1_I2D_put_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,7,v7);
317 if (in->psk_identity)
318 M_ASN1_I2D_put_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING,8,v8);
Adam Langley95c29f32014-06-20 12:00:00 -0700319 if (in->tlsext_tick_lifetime_hint > 0)
320 M_ASN1_I2D_put_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER,9,v9);
321 if (in->tlsext_tick)
322 M_ASN1_I2D_put_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING,10,v10);
Adam Langley75872532014-06-20 12:00:00 -0700323 if (in->peer_sha256_valid)
324 M_ASN1_I2D_put_EXP_opt(&(a.peer_sha256),i2d_ASN1_OCTET_STRING,13,v13);
Adam Langley1258b6a2014-06-20 12:00:00 -0700325 if (in->original_handshake_hash_len > 0)
326 M_ASN1_I2D_put_EXP_opt(&(a.original_handshake_hash),i2d_ASN1_OCTET_STRING,14,v14);
327
Adam Langley95c29f32014-06-20 12:00:00 -0700328 M_ASN1_I2D_finish();
329 }
330
331SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
332 long length)
333 {
334 int ssl_version=0,i;
335 long id;
336 ASN1_INTEGER ai,*aip;
337 ASN1_OCTET_STRING os,*osp;
338 M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new);
339
340 aip= &ai;
341 osp= &os;
342
343 M_ASN1_D2I_Init();
344 M_ASN1_D2I_start_sequence();
345
346 ai.data=NULL; ai.length=0;
347 M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
348 if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
349
350 /* we don't care about the version right now :-) */
351 M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
352 ssl_version=(int)ASN1_INTEGER_get(aip);
353 ret->ssl_version=ssl_version;
354 if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
355
356 os.data=NULL; os.length=0;
357 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
358 if (ssl_version == SSL2_VERSION)
359 {
360 if (os.length != 3)
361 {
362 c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
Adam Langleyf77452c2014-06-20 12:00:00 -0700363 c.line=__LINE__;
Adam Langley95c29f32014-06-20 12:00:00 -0700364 goto err;
365 }
366 id=0x02000000L|
367 ((unsigned long)os.data[0]<<16L)|
368 ((unsigned long)os.data[1]<< 8L)|
369 (unsigned long)os.data[2];
370 }
371 else if ((ssl_version>>8) >= SSL3_VERSION_MAJOR)
372 {
373 if (os.length != 2)
374 {
375 c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
Adam Langleyf77452c2014-06-20 12:00:00 -0700376 c.line=__LINE__;
Adam Langley95c29f32014-06-20 12:00:00 -0700377 goto err;
378 }
379 id=0x03000000L|
380 ((unsigned long)os.data[0]<<8L)|
381 (unsigned long)os.data[1];
382 }
383 else
384 {
385 c.error=SSL_R_UNKNOWN_SSL_VERSION;
Adam Langleyf77452c2014-06-20 12:00:00 -0700386 c.line=__LINE__;
Adam Langley95c29f32014-06-20 12:00:00 -0700387 goto err;
388 }
389
Adam Langley95c29f32014-06-20 12:00:00 -0700390 ret->cipher_id=id;
Adam Langley82b7da22014-08-13 12:28:02 -0700391 ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff);
392 if (ret->cipher == NULL)
393 {
394 c.error=SSL_R_UNSUPPORTED_CIPHER;
395 c.line = __LINE__;
396 goto err;
397 }
Adam Langley95c29f32014-06-20 12:00:00 -0700398
399 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
400 if ((ssl_version>>8) >= SSL3_VERSION_MAJOR)
401 i=SSL3_MAX_SSL_SESSION_ID_LENGTH;
402 else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */
403 i=SSL2_MAX_SSL_SESSION_ID_LENGTH;
404
405 if (os.length > i)
406 os.length = i;
407 if (os.length > (int)sizeof(ret->session_id)) /* can't happen */
408 os.length = sizeof(ret->session_id);
409
410 ret->session_id_length=os.length;
411 assert(os.length <= (int)sizeof(ret->session_id));
412 memcpy(ret->session_id,os.data,os.length);
413
414 M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
415 if (os.length > SSL_MAX_MASTER_KEY_LENGTH)
416 ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
417 else
418 ret->master_key_length=os.length;
419 memcpy(ret->master_key,os.data,ret->master_key_length);
420
421 os.length=0;
422
423 M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING);
424 if (os.length > SSL_MAX_KEY_ARG_LENGTH)
425 ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH;
426 else
427 ret->key_arg_length=os.length;
428 memcpy(ret->key_arg,os.data,ret->key_arg_length);
429 if (os.data != NULL) OPENSSL_free(os.data);
430
431 ai.length=0;
432 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1);
433 if (ai.data != NULL)
434 {
435 ret->time=ASN1_INTEGER_get(aip);
436 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
437 }
438 else
439 ret->time=(unsigned long)time(NULL);
440
441 ai.length=0;
442 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2);
443 if (ai.data != NULL)
444 {
445 ret->timeout=ASN1_INTEGER_get(aip);
446 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
447 }
448 else
449 ret->timeout=3;
450
451 if (ret->peer != NULL)
452 {
453 X509_free(ret->peer);
454 ret->peer=NULL;
455 }
456 M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3);
457
458 os.length=0;
459 os.data=NULL;
460 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4);
461
462 if(os.data != NULL)
463 {
464 if (os.length > SSL_MAX_SID_CTX_LENGTH)
465 {
466 c.error=SSL_R_BAD_LENGTH;
Adam Langleyf77452c2014-06-20 12:00:00 -0700467 c.line=__LINE__;
Adam Langley95c29f32014-06-20 12:00:00 -0700468 goto err;
469 }
470 else
471 {
472 ret->sid_ctx_length=os.length;
473 memcpy(ret->sid_ctx,os.data,os.length);
474 }
475 OPENSSL_free(os.data); os.data=NULL; os.length=0;
476 }
477 else
478 ret->sid_ctx_length=0;
479
480 ai.length=0;
481 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5);
482 if (ai.data != NULL)
483 {
484 ret->verify_result=ASN1_INTEGER_get(aip);
485 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
486 }
487 else
488 ret->verify_result=X509_V_OK;
489
Adam Langley95c29f32014-06-20 12:00:00 -0700490 os.length=0;
491 os.data=NULL;
492 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,6);
493 if (os.data)
494 {
495 ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length);
496 OPENSSL_free(os.data);
497 os.data = NULL;
498 os.length = 0;
499 }
500 else
501 ret->tlsext_hostname=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langley95c29f32014-06-20 12:00:00 -0700503 os.length=0;
504 os.data=NULL;
505 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,7);
506 if (os.data)
507 {
508 ret->psk_identity_hint = BUF_strndup((char *)os.data, os.length);
509 OPENSSL_free(os.data);
510 os.data = NULL;
511 os.length = 0;
512 }
513 else
514 ret->psk_identity_hint=NULL;
515
516 os.length=0;
517 os.data=NULL;
518 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,8);
519 if (os.data)
520 {
521 ret->psk_identity = 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=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langley95c29f32014-06-20 12:00:00 -0700529 ai.length=0;
530 M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,9);
531 if (ai.data != NULL)
532 {
533 ret->tlsext_tick_lifetime_hint=ASN1_INTEGER_get(aip);
534 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
535 }
536 else if (ret->tlsext_ticklen && ret->session_id_length)
537 ret->tlsext_tick_lifetime_hint = -1;
538 else
539 ret->tlsext_tick_lifetime_hint=0;
540 os.length=0;
541 os.data=NULL;
542 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,10);
543 if (os.data)
544 {
545 ret->tlsext_tick = os.data;
546 ret->tlsext_ticklen = os.length;
547 os.data = NULL;
548 os.length = 0;
549 }
550 else
551 ret->tlsext_tick=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700552
Adam Langley75872532014-06-20 12:00:00 -0700553 os.length=0;
554 os.data=NULL;
555 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,13);
556 if (os.data && os.length == sizeof(ret->peer_sha256))
557 {
558 memcpy(ret->peer_sha256, os.data, sizeof(ret->peer_sha256));
559 ret->peer_sha256_valid = 1;
560 OPENSSL_free(os.data);
561 os.data = NULL;
562 }
563
Adam Langley1258b6a2014-06-20 12:00:00 -0700564 os.length=0;
565 os.data=NULL;
566 M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,14);
567 if (os.data && os.length < (int)sizeof(ret->original_handshake_hash))
568 {
569 memcpy(ret->original_handshake_hash, os.data, os.length);
570 ret->original_handshake_hash_len = os.length;
571 OPENSSL_free(os.data);
572 os.data = NULL;
573 }
574
Adam Langley95c29f32014-06-20 12:00:00 -0700575 M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION);
576 }