155714Skris/* ssl/ssl_asn1.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296341Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
58238405Sjkim/* ====================================================================
59238405Sjkim * Copyright 2005 Nokia. All rights reserved.
60238405Sjkim *
61238405Sjkim * The portions of the attached software ("Contribution") is developed by
62238405Sjkim * Nokia Corporation and is licensed pursuant to the OpenSSL open source
63238405Sjkim * license.
64238405Sjkim *
65238405Sjkim * The Contribution, originally written by Mika Kousa and Pasi Eronen of
66238405Sjkim * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
67238405Sjkim * support (see RFC 4279) to OpenSSL.
68238405Sjkim *
69238405Sjkim * No patent licenses or other rights except those expressly stated in
70238405Sjkim * the OpenSSL open source license shall be deemed granted or received
71238405Sjkim * expressly, by implication, estoppel, or otherwise.
72238405Sjkim *
73238405Sjkim * No assurances are provided by Nokia that the Contribution does not
74238405Sjkim * infringe the patent or other intellectual property rights of any third
75238405Sjkim * party or that the license provides you with all the necessary rights
76238405Sjkim * to make use of the Contribution.
77238405Sjkim *
78238405Sjkim * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
79238405Sjkim * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
80238405Sjkim * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
81238405Sjkim * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
82238405Sjkim * OTHERWISE.
83238405Sjkim */
8455714Skris
8555714Skris#include <stdio.h>
8655714Skris#include <stdlib.h>
87109998Smarkm#include "ssl_locl.h"
8855714Skris#include <openssl/asn1_mac.h>
8955714Skris#include <openssl/objects.h>
9059191Skris#include <openssl/x509.h>
9155714Skris
92296341Sdelphijtypedef struct ssl_session_asn1_st {
93296341Sdelphij    ASN1_INTEGER version;
94296341Sdelphij    ASN1_INTEGER ssl_version;
95296341Sdelphij    ASN1_OCTET_STRING cipher;
96296341Sdelphij    ASN1_OCTET_STRING comp_id;
97296341Sdelphij    ASN1_OCTET_STRING master_key;
98296341Sdelphij    ASN1_OCTET_STRING session_id;
99296341Sdelphij    ASN1_OCTET_STRING session_id_context;
100296341Sdelphij    ASN1_OCTET_STRING key_arg;
101109998Smarkm#ifndef OPENSSL_NO_KRB5
102296341Sdelphij    ASN1_OCTET_STRING krb5_princ;
103296341Sdelphij#endif                          /* OPENSSL_NO_KRB5 */
104296341Sdelphij    ASN1_INTEGER time;
105296341Sdelphij    ASN1_INTEGER timeout;
106296341Sdelphij    ASN1_INTEGER verify_result;
107194206Ssimon#ifndef OPENSSL_NO_TLSEXT
108296341Sdelphij    ASN1_OCTET_STRING tlsext_hostname;
109296341Sdelphij    ASN1_INTEGER tlsext_tick_lifetime;
110296341Sdelphij    ASN1_OCTET_STRING tlsext_tick;
111296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
112238405Sjkim#ifndef OPENSSL_NO_PSK
113296341Sdelphij    ASN1_OCTET_STRING psk_identity_hint;
114296341Sdelphij    ASN1_OCTET_STRING psk_identity;
115296341Sdelphij#endif                          /* OPENSSL_NO_PSK */
116238405Sjkim#ifndef OPENSSL_NO_SRP
117296341Sdelphij    ASN1_OCTET_STRING srp_username;
118296341Sdelphij#endif                          /* OPENSSL_NO_SRP */
119296341Sdelphij} SSL_SESSION_ASN1;
12055714Skris
12155714Skrisint i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
122296341Sdelphij{
12355714Skris#define LSIZE2 (sizeof(long)*2)
124296341Sdelphij    int v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v7 = 0, v8 = 0;
125296341Sdelphij    unsigned char buf[4], ibuf1[LSIZE2], ibuf2[LSIZE2];
126296341Sdelphij    unsigned char ibuf3[LSIZE2], ibuf4[LSIZE2], ibuf5[LSIZE2];
127194206Ssimon#ifndef OPENSSL_NO_TLSEXT
128296341Sdelphij    int v6 = 0, v9 = 0, v10 = 0;
129296341Sdelphij    unsigned char ibuf6[LSIZE2];
130194206Ssimon#endif
131205128Ssimon#ifndef OPENSSL_NO_COMP
132296341Sdelphij    unsigned char cbuf;
133296341Sdelphij    int v11 = 0;
134205128Ssimon#endif
135238405Sjkim#ifndef OPENSSL_NO_SRP
136296341Sdelphij    int v12 = 0;
137238405Sjkim#endif
138296341Sdelphij    long l;
139296341Sdelphij    SSL_SESSION_ASN1 a;
140296341Sdelphij    M_ASN1_I2D_vars(in);
14155714Skris
142296341Sdelphij    if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
143296341Sdelphij        return (0);
14455714Skris
145296341Sdelphij    /*
146296341Sdelphij     * Note that I cheat in the following 2 assignments.  I know that if the
147296341Sdelphij     * ASN1_INTEGER passed to ASN1_INTEGER_set is > sizeof(long)+1, the
148296341Sdelphij     * buffer will not be re-OPENSSL_malloc()ed. This is a bit evil but makes
149296341Sdelphij     * things simple, no dynamic allocation to clean up :-)
150296341Sdelphij     */
151296341Sdelphij    a.version.length = LSIZE2;
152296341Sdelphij    a.version.type = V_ASN1_INTEGER;
153296341Sdelphij    a.version.data = ibuf1;
154296341Sdelphij    ASN1_INTEGER_set(&(a.version), SSL_SESSION_ASN1_VERSION);
15555714Skris
156296341Sdelphij    a.ssl_version.length = LSIZE2;
157296341Sdelphij    a.ssl_version.type = V_ASN1_INTEGER;
158296341Sdelphij    a.ssl_version.data = ibuf2;
159296341Sdelphij    ASN1_INTEGER_set(&(a.ssl_version), in->ssl_version);
16055714Skris
161296341Sdelphij    a.cipher.type = V_ASN1_OCTET_STRING;
162296341Sdelphij    a.cipher.data = buf;
16355714Skris
164296341Sdelphij    if (in->cipher == NULL)
165296341Sdelphij        l = in->cipher_id;
166296341Sdelphij    else
167296341Sdelphij        l = in->cipher->id;
168296341Sdelphij    if (in->ssl_version == SSL2_VERSION) {
169296341Sdelphij        a.cipher.length = 3;
170296341Sdelphij        buf[0] = ((unsigned char)(l >> 16L)) & 0xff;
171296341Sdelphij        buf[1] = ((unsigned char)(l >> 8L)) & 0xff;
172296341Sdelphij        buf[2] = ((unsigned char)(l)) & 0xff;
173296341Sdelphij    } else {
174296341Sdelphij        a.cipher.length = 2;
175296341Sdelphij        buf[0] = ((unsigned char)(l >> 8L)) & 0xff;
176296341Sdelphij        buf[1] = ((unsigned char)(l)) & 0xff;
177296341Sdelphij    }
17855714Skris
179205128Ssimon#ifndef OPENSSL_NO_COMP
180296341Sdelphij    if (in->compress_meth) {
181296341Sdelphij        cbuf = (unsigned char)in->compress_meth;
182296341Sdelphij        a.comp_id.length = 1;
183296341Sdelphij        a.comp_id.type = V_ASN1_OCTET_STRING;
184296341Sdelphij        a.comp_id.data = &cbuf;
185296341Sdelphij    }
186205128Ssimon#endif
187205128Ssimon
188296341Sdelphij    a.master_key.length = in->master_key_length;
189296341Sdelphij    a.master_key.type = V_ASN1_OCTET_STRING;
190296341Sdelphij    a.master_key.data = in->master_key;
19155714Skris
192296341Sdelphij    a.session_id.length = in->session_id_length;
193296341Sdelphij    a.session_id.type = V_ASN1_OCTET_STRING;
194296341Sdelphij    a.session_id.data = in->session_id;
19555714Skris
196296341Sdelphij    a.session_id_context.length = in->sid_ctx_length;
197296341Sdelphij    a.session_id_context.type = V_ASN1_OCTET_STRING;
198296341Sdelphij    a.session_id_context.data = in->sid_ctx;
19955714Skris
200296341Sdelphij    a.key_arg.length = in->key_arg_length;
201296341Sdelphij    a.key_arg.type = V_ASN1_OCTET_STRING;
202296341Sdelphij    a.key_arg.data = in->key_arg;
20355714Skris
204109998Smarkm#ifndef OPENSSL_NO_KRB5
205296341Sdelphij    if (in->krb5_client_princ_len) {
206296341Sdelphij        a.krb5_princ.length = in->krb5_client_princ_len;
207296341Sdelphij        a.krb5_princ.type = V_ASN1_OCTET_STRING;
208296341Sdelphij        a.krb5_princ.data = in->krb5_client_princ;
209296341Sdelphij    }
210296341Sdelphij#endif                          /* OPENSSL_NO_KRB5 */
211238405Sjkim
212296341Sdelphij    if (in->time != 0L) {
213296341Sdelphij        a.time.length = LSIZE2;
214296341Sdelphij        a.time.type = V_ASN1_INTEGER;
215296341Sdelphij        a.time.data = ibuf3;
216296341Sdelphij        ASN1_INTEGER_set(&(a.time), in->time);
217296341Sdelphij    }
21855714Skris
219296341Sdelphij    if (in->timeout != 0L) {
220296341Sdelphij        a.timeout.length = LSIZE2;
221296341Sdelphij        a.timeout.type = V_ASN1_INTEGER;
222296341Sdelphij        a.timeout.data = ibuf4;
223296341Sdelphij        ASN1_INTEGER_set(&(a.timeout), in->timeout);
224296341Sdelphij    }
22555714Skris
226296341Sdelphij    if (in->verify_result != X509_V_OK) {
227296341Sdelphij        a.verify_result.length = LSIZE2;
228296341Sdelphij        a.verify_result.type = V_ASN1_INTEGER;
229296341Sdelphij        a.verify_result.data = ibuf5;
230296341Sdelphij        ASN1_INTEGER_set(&a.verify_result, in->verify_result);
231296341Sdelphij    }
232194206Ssimon#ifndef OPENSSL_NO_TLSEXT
233296341Sdelphij    if (in->tlsext_hostname) {
234296341Sdelphij        a.tlsext_hostname.length = strlen(in->tlsext_hostname);
235296341Sdelphij        a.tlsext_hostname.type = V_ASN1_OCTET_STRING;
236296341Sdelphij        a.tlsext_hostname.data = (unsigned char *)in->tlsext_hostname;
237296341Sdelphij    }
238296341Sdelphij    if (in->tlsext_tick) {
239296341Sdelphij        a.tlsext_tick.length = in->tlsext_ticklen;
240296341Sdelphij        a.tlsext_tick.type = V_ASN1_OCTET_STRING;
241296341Sdelphij        a.tlsext_tick.data = (unsigned char *)in->tlsext_tick;
242296341Sdelphij    }
243296341Sdelphij    if (in->tlsext_tick_lifetime_hint > 0) {
244296341Sdelphij        a.tlsext_tick_lifetime.length = LSIZE2;
245296341Sdelphij        a.tlsext_tick_lifetime.type = V_ASN1_INTEGER;
246296341Sdelphij        a.tlsext_tick_lifetime.data = ibuf6;
247296341Sdelphij        ASN1_INTEGER_set(&a.tlsext_tick_lifetime,
248296341Sdelphij                         in->tlsext_tick_lifetime_hint);
249296341Sdelphij    }
250296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
251238405Sjkim#ifndef OPENSSL_NO_PSK
252296341Sdelphij    if (in->psk_identity_hint) {
253296341Sdelphij        a.psk_identity_hint.length = strlen(in->psk_identity_hint);
254296341Sdelphij        a.psk_identity_hint.type = V_ASN1_OCTET_STRING;
255296341Sdelphij        a.psk_identity_hint.data = (unsigned char *)(in->psk_identity_hint);
256296341Sdelphij    }
257296341Sdelphij    if (in->psk_identity) {
258296341Sdelphij        a.psk_identity.length = strlen(in->psk_identity);
259296341Sdelphij        a.psk_identity.type = V_ASN1_OCTET_STRING;
260296341Sdelphij        a.psk_identity.data = (unsigned char *)(in->psk_identity);
261296341Sdelphij    }
262296341Sdelphij#endif                          /* OPENSSL_NO_PSK */
263238405Sjkim#ifndef OPENSSL_NO_SRP
264296341Sdelphij    if (in->srp_username) {
265296341Sdelphij        a.srp_username.length = strlen(in->srp_username);
266296341Sdelphij        a.srp_username.type = V_ASN1_OCTET_STRING;
267296341Sdelphij        a.srp_username.data = (unsigned char *)(in->srp_username);
268296341Sdelphij    }
269296341Sdelphij#endif                          /* OPENSSL_NO_SRP */
270238405Sjkim
271296341Sdelphij    M_ASN1_I2D_len(&(a.version), i2d_ASN1_INTEGER);
272296341Sdelphij    M_ASN1_I2D_len(&(a.ssl_version), i2d_ASN1_INTEGER);
273296341Sdelphij    M_ASN1_I2D_len(&(a.cipher), i2d_ASN1_OCTET_STRING);
274296341Sdelphij    M_ASN1_I2D_len(&(a.session_id), i2d_ASN1_OCTET_STRING);
275296341Sdelphij    M_ASN1_I2D_len(&(a.master_key), i2d_ASN1_OCTET_STRING);
276109998Smarkm#ifndef OPENSSL_NO_KRB5
277296341Sdelphij    if (in->krb5_client_princ_len)
278296341Sdelphij        M_ASN1_I2D_len(&(a.krb5_princ), i2d_ASN1_OCTET_STRING);
279296341Sdelphij#endif                          /* OPENSSL_NO_KRB5 */
280296341Sdelphij    if (in->key_arg_length > 0)
281296341Sdelphij        M_ASN1_I2D_len_IMP_opt(&(a.key_arg), i2d_ASN1_OCTET_STRING);
282296341Sdelphij    if (in->time != 0L)
283296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.time), i2d_ASN1_INTEGER, 1, v1);
284296341Sdelphij    if (in->timeout != 0L)
285296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.timeout), i2d_ASN1_INTEGER, 2, v2);
286296341Sdelphij    if (in->peer != NULL)
287296341Sdelphij        M_ASN1_I2D_len_EXP_opt(in->peer, i2d_X509, 3, v3);
288296341Sdelphij    M_ASN1_I2D_len_EXP_opt(&a.session_id_context, i2d_ASN1_OCTET_STRING, 4,
289296341Sdelphij                           v4);
290296341Sdelphij    if (in->verify_result != X509_V_OK)
291296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.verify_result), i2d_ASN1_INTEGER, 5, v5);
29255714Skris
293194206Ssimon#ifndef OPENSSL_NO_TLSEXT
294296341Sdelphij    if (in->tlsext_tick_lifetime_hint > 0)
295296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER, 9,
296296341Sdelphij                               v9);
297296341Sdelphij    if (in->tlsext_tick)
298296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING, 10,
299296341Sdelphij                               v10);
300296341Sdelphij    if (in->tlsext_hostname)
301296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING, 6,
302296341Sdelphij                               v6);
303296341Sdelphij# ifndef OPENSSL_NO_COMP
304296341Sdelphij    if (in->compress_meth)
305296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.comp_id), i2d_ASN1_OCTET_STRING, 11, v11);
306296341Sdelphij# endif
307296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
308238405Sjkim#ifndef OPENSSL_NO_PSK
309296341Sdelphij    if (in->psk_identity_hint)
310296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,
311296341Sdelphij                               7, v7);
312296341Sdelphij    if (in->psk_identity)
313296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING, 8,
314296341Sdelphij                               v8);
315296341Sdelphij#endif                          /* OPENSSL_NO_PSK */
316238405Sjkim#ifndef OPENSSL_NO_SRP
317296341Sdelphij    if (in->srp_username)
318296341Sdelphij        M_ASN1_I2D_len_EXP_opt(&(a.srp_username), i2d_ASN1_OCTET_STRING, 12,
319296341Sdelphij                               v12);
320296341Sdelphij#endif                          /* OPENSSL_NO_SRP */
321238405Sjkim
322296341Sdelphij    M_ASN1_I2D_seq_total();
32355714Skris
324296341Sdelphij    M_ASN1_I2D_put(&(a.version), i2d_ASN1_INTEGER);
325296341Sdelphij    M_ASN1_I2D_put(&(a.ssl_version), i2d_ASN1_INTEGER);
326296341Sdelphij    M_ASN1_I2D_put(&(a.cipher), i2d_ASN1_OCTET_STRING);
327296341Sdelphij    M_ASN1_I2D_put(&(a.session_id), i2d_ASN1_OCTET_STRING);
328296341Sdelphij    M_ASN1_I2D_put(&(a.master_key), i2d_ASN1_OCTET_STRING);
329109998Smarkm#ifndef OPENSSL_NO_KRB5
330296341Sdelphij    if (in->krb5_client_princ_len)
331296341Sdelphij        M_ASN1_I2D_put(&(a.krb5_princ), i2d_ASN1_OCTET_STRING);
332296341Sdelphij#endif                          /* OPENSSL_NO_KRB5 */
333296341Sdelphij    if (in->key_arg_length > 0)
334296341Sdelphij        M_ASN1_I2D_put_IMP_opt(&(a.key_arg), i2d_ASN1_OCTET_STRING, 0);
335296341Sdelphij    if (in->time != 0L)
336296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.time), i2d_ASN1_INTEGER, 1, v1);
337296341Sdelphij    if (in->timeout != 0L)
338296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.timeout), i2d_ASN1_INTEGER, 2, v2);
339296341Sdelphij    if (in->peer != NULL)
340296341Sdelphij        M_ASN1_I2D_put_EXP_opt(in->peer, i2d_X509, 3, v3);
341296341Sdelphij    M_ASN1_I2D_put_EXP_opt(&a.session_id_context, i2d_ASN1_OCTET_STRING, 4,
342296341Sdelphij                           v4);
343296341Sdelphij    if (in->verify_result != X509_V_OK)
344296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&a.verify_result, i2d_ASN1_INTEGER, 5, v5);
345194206Ssimon#ifndef OPENSSL_NO_TLSEXT
346296341Sdelphij    if (in->tlsext_hostname)
347296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING, 6,
348296341Sdelphij                               v6);
349296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
350238405Sjkim#ifndef OPENSSL_NO_PSK
351296341Sdelphij    if (in->psk_identity_hint)
352296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.psk_identity_hint), i2d_ASN1_OCTET_STRING,
353296341Sdelphij                               7, v7);
354296341Sdelphij    if (in->psk_identity)
355296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.psk_identity), i2d_ASN1_OCTET_STRING, 8,
356296341Sdelphij                               v8);
357296341Sdelphij#endif                          /* OPENSSL_NO_PSK */
358238405Sjkim#ifndef OPENSSL_NO_TLSEXT
359296341Sdelphij    if (in->tlsext_tick_lifetime_hint > 0)
360296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&a.tlsext_tick_lifetime, i2d_ASN1_INTEGER, 9,
361296341Sdelphij                               v9);
362296341Sdelphij    if (in->tlsext_tick)
363296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.tlsext_tick), i2d_ASN1_OCTET_STRING, 10,
364296341Sdelphij                               v10);
365296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
366205128Ssimon#ifndef OPENSSL_NO_COMP
367296341Sdelphij    if (in->compress_meth)
368296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.comp_id), i2d_ASN1_OCTET_STRING, 11, v11);
369205128Ssimon#endif
370238405Sjkim#ifndef OPENSSL_NO_SRP
371296341Sdelphij    if (in->srp_username)
372296341Sdelphij        M_ASN1_I2D_put_EXP_opt(&(a.srp_username), i2d_ASN1_OCTET_STRING, 12,
373296341Sdelphij                               v12);
374296341Sdelphij#endif                          /* OPENSSL_NO_SRP */
375296341Sdelphij    M_ASN1_I2D_finish();
376296341Sdelphij}
37755714Skris
378160814SsimonSSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
379296341Sdelphij                             long length)
380296341Sdelphij{
381296341Sdelphij    int ssl_version = 0, i;
382296341Sdelphij    long id;
383296341Sdelphij    ASN1_INTEGER ai, *aip;
384296341Sdelphij    ASN1_OCTET_STRING os, *osp;
385296341Sdelphij    M_ASN1_D2I_vars(a, SSL_SESSION *, SSL_SESSION_new);
38655714Skris
387296341Sdelphij    aip = &ai;
388296341Sdelphij    osp = &os;
38955714Skris
390296341Sdelphij    M_ASN1_D2I_Init();
391296341Sdelphij    M_ASN1_D2I_start_sequence();
39255714Skris
393296341Sdelphij    ai.data = NULL;
394296341Sdelphij    ai.length = 0;
395296341Sdelphij    M_ASN1_D2I_get_x(ASN1_INTEGER, aip, d2i_ASN1_INTEGER);
396296341Sdelphij    if (ai.data != NULL) {
397296341Sdelphij        OPENSSL_free(ai.data);
398296341Sdelphij        ai.data = NULL;
399296341Sdelphij        ai.length = 0;
400296341Sdelphij    }
40155714Skris
402296341Sdelphij    /* we don't care about the version right now :-) */
403296341Sdelphij    M_ASN1_D2I_get_x(ASN1_INTEGER, aip, d2i_ASN1_INTEGER);
404296341Sdelphij    ssl_version = (int)ASN1_INTEGER_get(aip);
405296341Sdelphij    ret->ssl_version = ssl_version;
406296341Sdelphij    if (ai.data != NULL) {
407296341Sdelphij        OPENSSL_free(ai.data);
408296341Sdelphij        ai.data = NULL;
409296341Sdelphij        ai.length = 0;
410296341Sdelphij    }
41155714Skris
412296341Sdelphij    os.data = NULL;
413296341Sdelphij    os.length = 0;
414296341Sdelphij    M_ASN1_D2I_get_x(ASN1_OCTET_STRING, osp, d2i_ASN1_OCTET_STRING);
415296341Sdelphij    if (ssl_version == SSL2_VERSION) {
416296341Sdelphij        if (os.length != 3) {
417296341Sdelphij            c.error = SSL_R_CIPHER_CODE_WRONG_LENGTH;
418296341Sdelphij            c.line = __LINE__;
419296341Sdelphij            goto err;
420296341Sdelphij        }
421296341Sdelphij        id = 0x02000000L |
422296341Sdelphij            ((unsigned long)os.data[0] << 16L) |
423296341Sdelphij            ((unsigned long)os.data[1] << 8L) | (unsigned long)os.data[2];
424296341Sdelphij    } else if ((ssl_version >> 8) == SSL3_VERSION_MAJOR
425296341Sdelphij        || (ssl_version >> 8) == DTLS1_VERSION_MAJOR
426296341Sdelphij        || ssl_version == DTLS1_BAD_VER) {
427296341Sdelphij        if (os.length != 2) {
428296341Sdelphij            c.error = SSL_R_CIPHER_CODE_WRONG_LENGTH;
429296341Sdelphij            c.line = __LINE__;
430296341Sdelphij            goto err;
431296341Sdelphij        }
432296341Sdelphij        id = 0x03000000L |
433296341Sdelphij            ((unsigned long)os.data[0] << 8L) | (unsigned long)os.data[1];
434296341Sdelphij    } else {
435296341Sdelphij        c.error = SSL_R_UNKNOWN_SSL_VERSION;
436296341Sdelphij        c.line = __LINE__;
437296341Sdelphij        goto err;
438296341Sdelphij    }
43955714Skris
440296341Sdelphij    ret->cipher = NULL;
441296341Sdelphij    ret->cipher_id = id;
44255714Skris
443296341Sdelphij    M_ASN1_D2I_get_x(ASN1_OCTET_STRING, osp, d2i_ASN1_OCTET_STRING);
444296341Sdelphij    if ((ssl_version >> 8) >= SSL3_VERSION_MAJOR)
445296341Sdelphij        i = SSL3_MAX_SSL_SESSION_ID_LENGTH;
446296341Sdelphij    else                        /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */
447296341Sdelphij        i = SSL2_MAX_SSL_SESSION_ID_LENGTH;
44855714Skris
449296341Sdelphij    if (os.length > i)
450296341Sdelphij        os.length = i;
451296341Sdelphij    if (os.length > (int)sizeof(ret->session_id)) /* can't happen */
452296341Sdelphij        os.length = sizeof(ret->session_id);
45355714Skris
454296341Sdelphij    ret->session_id_length = os.length;
455296341Sdelphij    OPENSSL_assert(os.length <= (int)sizeof(ret->session_id));
456296341Sdelphij    memcpy(ret->session_id, os.data, os.length);
45755714Skris
458296341Sdelphij    M_ASN1_D2I_get_x(ASN1_OCTET_STRING, osp, d2i_ASN1_OCTET_STRING);
459296341Sdelphij    if (os.length > SSL_MAX_MASTER_KEY_LENGTH)
460296341Sdelphij        ret->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
461296341Sdelphij    else
462296341Sdelphij        ret->master_key_length = os.length;
463296341Sdelphij    memcpy(ret->master_key, os.data, ret->master_key_length);
464109998Smarkm
465296341Sdelphij    os.length = 0;
466296341Sdelphij
467109998Smarkm#ifndef OPENSSL_NO_KRB5
468296341Sdelphij    os.length = 0;
469296341Sdelphij    M_ASN1_D2I_get_opt(osp, d2i_ASN1_OCTET_STRING, V_ASN1_OCTET_STRING);
470296341Sdelphij    if (os.data) {
471296341Sdelphij        if (os.length > SSL_MAX_KRB5_PRINCIPAL_LENGTH)
472296341Sdelphij            ret->krb5_client_princ_len = 0;
473296341Sdelphij        else
474296341Sdelphij            ret->krb5_client_princ_len = os.length;
475296341Sdelphij        memcpy(ret->krb5_client_princ, os.data, ret->krb5_client_princ_len);
476296341Sdelphij        OPENSSL_free(os.data);
477296341Sdelphij        os.data = NULL;
478296341Sdelphij        os.length = 0;
479296341Sdelphij    } else
480296341Sdelphij        ret->krb5_client_princ_len = 0;
481296341Sdelphij#endif                          /* OPENSSL_NO_KRB5 */
482109998Smarkm
483296341Sdelphij    M_ASN1_D2I_get_IMP_opt(osp, d2i_ASN1_OCTET_STRING, 0,
484296341Sdelphij                           V_ASN1_OCTET_STRING);
485296341Sdelphij    if (os.length > SSL_MAX_KEY_ARG_LENGTH)
486296341Sdelphij        ret->key_arg_length = SSL_MAX_KEY_ARG_LENGTH;
487296341Sdelphij    else
488296341Sdelphij        ret->key_arg_length = os.length;
489296341Sdelphij    memcpy(ret->key_arg, os.data, ret->key_arg_length);
490296341Sdelphij    if (os.data != NULL)
491296341Sdelphij        OPENSSL_free(os.data);
49255714Skris
493296341Sdelphij    ai.length = 0;
494296341Sdelphij    M_ASN1_D2I_get_EXP_opt(aip, d2i_ASN1_INTEGER, 1);
495296341Sdelphij    if (ai.data != NULL) {
496296341Sdelphij        ret->time = ASN1_INTEGER_get(aip);
497296341Sdelphij        OPENSSL_free(ai.data);
498296341Sdelphij        ai.data = NULL;
499296341Sdelphij        ai.length = 0;
500296341Sdelphij    } else
501296341Sdelphij        ret->time = (unsigned long)time(NULL);
50255714Skris
503296341Sdelphij    ai.length = 0;
504296341Sdelphij    M_ASN1_D2I_get_EXP_opt(aip, d2i_ASN1_INTEGER, 2);
505296341Sdelphij    if (ai.data != NULL) {
506296341Sdelphij        ret->timeout = ASN1_INTEGER_get(aip);
507296341Sdelphij        OPENSSL_free(ai.data);
508296341Sdelphij        ai.data = NULL;
509296341Sdelphij        ai.length = 0;
510296341Sdelphij    } else
511296341Sdelphij        ret->timeout = 3;
51255714Skris
513296341Sdelphij    if (ret->peer != NULL) {
514296341Sdelphij        X509_free(ret->peer);
515296341Sdelphij        ret->peer = NULL;
516296341Sdelphij    }
517296341Sdelphij    M_ASN1_D2I_get_EXP_opt(ret->peer, d2i_X509, 3);
51855714Skris
519296341Sdelphij    os.length = 0;
520296341Sdelphij    os.data = NULL;
521296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 4);
52255714Skris
523296341Sdelphij    if (os.data != NULL) {
524296341Sdelphij        if (os.length > SSL_MAX_SID_CTX_LENGTH) {
525296341Sdelphij            c.error = SSL_R_BAD_LENGTH;
526296341Sdelphij            c.line = __LINE__;
527296341Sdelphij            goto err;
528296341Sdelphij        } else {
529296341Sdelphij            ret->sid_ctx_length = os.length;
530296341Sdelphij            memcpy(ret->sid_ctx, os.data, os.length);
531296341Sdelphij        }
532296341Sdelphij        OPENSSL_free(os.data);
533296341Sdelphij        os.data = NULL;
534296341Sdelphij        os.length = 0;
535296341Sdelphij    } else
536296341Sdelphij        ret->sid_ctx_length = 0;
53755714Skris
538296341Sdelphij    ai.length = 0;
539296341Sdelphij    M_ASN1_D2I_get_EXP_opt(aip, d2i_ASN1_INTEGER, 5);
540296341Sdelphij    if (ai.data != NULL) {
541296341Sdelphij        ret->verify_result = ASN1_INTEGER_get(aip);
542296341Sdelphij        OPENSSL_free(ai.data);
543296341Sdelphij        ai.data = NULL;
544296341Sdelphij        ai.length = 0;
545296341Sdelphij    } else
546296341Sdelphij        ret->verify_result = X509_V_OK;
54759191Skris
548194206Ssimon#ifndef OPENSSL_NO_TLSEXT
549296341Sdelphij    os.length = 0;
550296341Sdelphij    os.data = NULL;
551296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 6);
552296341Sdelphij    if (os.data) {
553296341Sdelphij        ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length);
554296341Sdelphij        OPENSSL_free(os.data);
555296341Sdelphij        os.data = NULL;
556296341Sdelphij        os.length = 0;
557296341Sdelphij    } else
558296341Sdelphij        ret->tlsext_hostname = NULL;
559296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
560238405Sjkim
561238405Sjkim#ifndef OPENSSL_NO_PSK
562296341Sdelphij    os.length = 0;
563296341Sdelphij    os.data = NULL;
564296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 7);
565296341Sdelphij    if (os.data) {
566296341Sdelphij        ret->psk_identity_hint = BUF_strndup((char *)os.data, os.length);
567296341Sdelphij        OPENSSL_free(os.data);
568296341Sdelphij        os.data = NULL;
569296341Sdelphij        os.length = 0;
570296341Sdelphij    } else
571296341Sdelphij        ret->psk_identity_hint = NULL;
572238405Sjkim
573296341Sdelphij    os.length = 0;
574296341Sdelphij    os.data = NULL;
575296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 8);
576296341Sdelphij    if (os.data) {
577296341Sdelphij        ret->psk_identity = BUF_strndup((char *)os.data, os.length);
578296341Sdelphij        OPENSSL_free(os.data);
579296341Sdelphij        os.data = NULL;
580296341Sdelphij        os.length = 0;
581296341Sdelphij    } else
582296341Sdelphij        ret->psk_identity = NULL;
583296341Sdelphij#endif                          /* OPENSSL_NO_PSK */
584238405Sjkim
585238405Sjkim#ifndef OPENSSL_NO_TLSEXT
586296341Sdelphij    ai.length = 0;
587296341Sdelphij    M_ASN1_D2I_get_EXP_opt(aip, d2i_ASN1_INTEGER, 9);
588296341Sdelphij    if (ai.data != NULL) {
589296341Sdelphij        ret->tlsext_tick_lifetime_hint = ASN1_INTEGER_get(aip);
590296341Sdelphij        OPENSSL_free(ai.data);
591296341Sdelphij        ai.data = NULL;
592296341Sdelphij        ai.length = 0;
593296341Sdelphij    } else if (ret->tlsext_ticklen && ret->session_id_length)
594296341Sdelphij        ret->tlsext_tick_lifetime_hint = -1;
595296341Sdelphij    else
596296341Sdelphij        ret->tlsext_tick_lifetime_hint = 0;
597296341Sdelphij    os.length = 0;
598296341Sdelphij    os.data = NULL;
599296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 10);
600296341Sdelphij    if (os.data) {
601296341Sdelphij        ret->tlsext_tick = os.data;
602296341Sdelphij        ret->tlsext_ticklen = os.length;
603296341Sdelphij        os.data = NULL;
604296341Sdelphij        os.length = 0;
605296341Sdelphij    } else
606296341Sdelphij        ret->tlsext_tick = NULL;
607296341Sdelphij#endif                          /* OPENSSL_NO_TLSEXT */
608205128Ssimon#ifndef OPENSSL_NO_COMP
609296341Sdelphij    os.length = 0;
610296341Sdelphij    os.data = NULL;
611296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 11);
612296341Sdelphij    if (os.data) {
613296341Sdelphij        ret->compress_meth = os.data[0];
614296341Sdelphij        OPENSSL_free(os.data);
615296341Sdelphij        os.data = NULL;
616296341Sdelphij    }
617205128Ssimon#endif
618194206Ssimon
619238405Sjkim#ifndef OPENSSL_NO_SRP
620296341Sdelphij    os.length = 0;
621296341Sdelphij    os.data = NULL;
622296341Sdelphij    M_ASN1_D2I_get_EXP_opt(osp, d2i_ASN1_OCTET_STRING, 12);
623296341Sdelphij    if (os.data) {
624296341Sdelphij        ret->srp_username = BUF_strndup((char *)os.data, os.length);
625296341Sdelphij        OPENSSL_free(os.data);
626296341Sdelphij        os.data = NULL;
627296341Sdelphij        os.length = 0;
628296341Sdelphij    } else
629296341Sdelphij        ret->srp_username = NULL;
630296341Sdelphij#endif                          /* OPENSSL_NO_SRP */
631238405Sjkim
632296341Sdelphij    M_ASN1_D2I_Finish(a, SSL_SESSION_free, SSL_F_D2I_SSL_SESSION);
633296341Sdelphij}
634