1ff31d440239f41fb0e86b589efca489fe707a58
[openssl.git] / ssl / ssl_asn1.c
1 /* ssl/ssl_asn1.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include "ssl_locl.h"
62 #include <openssl/asn1_mac.h>
63 #include <openssl/objects.h>
64 #include <openssl/x509.h>
65
66 typedef struct ssl_session_asn1_st
67         {
68         ASN1_INTEGER version;
69         ASN1_INTEGER ssl_version;
70         ASN1_OCTET_STRING cipher;
71         ASN1_OCTET_STRING master_key;
72         ASN1_OCTET_STRING session_id;
73         ASN1_OCTET_STRING session_id_context;
74         ASN1_OCTET_STRING key_arg;
75 #ifndef OPENSSL_NO_KRB5
76         ASN1_OCTET_STRING krb5_princ;
77 #endif /* OPENSSL_NO_KRB5 */
78         ASN1_INTEGER time;
79         ASN1_INTEGER timeout;
80         ASN1_INTEGER verify_result;
81 #ifndef OPENSSL_NO_TLSEXT
82         ASN1_OCTET_STRING tlsext_hostname;
83 #endif /* OPENSSL_NO_TLSEXT */
84         } SSL_SESSION_ASN1;
85
86 int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
87         {
88 #define LSIZE2 (sizeof(long)*2)
89         int v1=0,v2=0,v3=0,v4=0,v5=0,v6=0;
90         unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2];
91         unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2];
92         long l;
93         SSL_SESSION_ASN1 a;
94         M_ASN1_I2D_vars(in);
95
96         if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
97                 return(0);
98
99         /* Note that I cheat in the following 2 assignments.  I know
100          * that if the ASN1_INTEGER passed to ASN1_INTEGER_set
101          * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed.
102          * This is a bit evil but makes things simple, no dynamic allocation
103          * to clean up :-) */
104         a.version.length=LSIZE2;
105         a.version.type=V_ASN1_INTEGER;
106         a.version.data=ibuf1;
107         ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION);
108
109         a.ssl_version.length=LSIZE2;
110         a.ssl_version.type=V_ASN1_INTEGER;
111         a.ssl_version.data=ibuf2;
112         ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version);
113
114         a.cipher.type=V_ASN1_OCTET_STRING;
115         a.cipher.data=buf;
116
117         if (in->cipher == NULL)
118                 l=in->cipher_id;
119         else
120                 l=in->cipher->id;
121         if (in->ssl_version == SSL2_VERSION)
122                 {
123                 a.cipher.length=3;
124                 buf[0]=((unsigned char)(l>>16L))&0xff;
125                 buf[1]=((unsigned char)(l>> 8L))&0xff;
126                 buf[2]=((unsigned char)(l     ))&0xff;
127                 }
128         else
129                 {
130                 a.cipher.length=2;
131                 buf[0]=((unsigned char)(l>>8L))&0xff;
132                 buf[1]=((unsigned char)(l    ))&0xff;
133                 }
134
135         a.master_key.length=in->master_key_length;
136         a.master_key.type=V_ASN1_OCTET_STRING;
137         a.master_key.data=in->master_key;
138
139         a.session_id.length=in->session_id_length;
140         a.session_id.type=V_ASN1_OCTET_STRING;
141         a.session_id.data=in->session_id;
142
143         a.session_id_context.length=in->sid_ctx_length;
144         a.session_id_context.type=V_ASN1_OCTET_STRING;
145         a.session_id_context.data=in->sid_ctx;
146
147         a.key_arg.length=in->key_arg_length;
148         a.key_arg.type=V_ASN1_OCTET_STRING;
149         a.key_arg.data=in->key_arg;
150
151 #ifndef OPENSSL_NO_KRB5
152         if (in->krb5_client_princ_len)
153                 {
154                 a.krb5_princ.length=in->krb5_client_princ_len;
155                 a.krb5_princ.type=V_ASN1_OCTET_STRING;
156                 a.krb5_princ.data=in->krb5_client_princ;
157                 }
158 #endif /* OPENSSL_NO_KRB5 */
159  
160         if (in->time != 0L)
161                 {
162                 a.time.length=LSIZE2;
163                 a.time.type=V_ASN1_INTEGER;
164                 a.time.data=ibuf3;
165                 ASN1_INTEGER_set(&(a.time),in->time);
166                 }
167
168         if (in->timeout != 0L)
169                 {
170                 a.timeout.length=LSIZE2;
171                 a.timeout.type=V_ASN1_INTEGER;
172                 a.timeout.data=ibuf4;
173                 ASN1_INTEGER_set(&(a.timeout),in->timeout);
174                 }
175
176         if (in->verify_result != X509_V_OK)
177                 {
178                 a.verify_result.length=LSIZE2;
179                 a.verify_result.type=V_ASN1_INTEGER;
180                 a.verify_result.data=ibuf5;
181                 ASN1_INTEGER_set(&a.verify_result,in->verify_result);
182                 }
183
184 #ifndef OPENSSL_NO_TLSEXT
185         if (in->tlsext_hostname)
186                 {
187                 a.tlsext_hostname.length=strlen(in->tlsext_hostname);
188                 a.tlsext_hostname.type=V_ASN1_OCTET_STRING;
189                 a.tlsext_hostname.data=(unsigned char *)in->tlsext_hostname;
190                 }                
191 #endif /* OPENSSL_NO_TLSEXT */
192
193         M_ASN1_I2D_len(&(a.version),            i2d_ASN1_INTEGER);
194         M_ASN1_I2D_len(&(a.ssl_version),        i2d_ASN1_INTEGER);
195         M_ASN1_I2D_len(&(a.cipher),             i2d_ASN1_OCTET_STRING);
196         M_ASN1_I2D_len(&(a.session_id),         i2d_ASN1_OCTET_STRING);
197         M_ASN1_I2D_len(&(a.master_key),         i2d_ASN1_OCTET_STRING);
198 #ifndef OPENSSL_NO_KRB5
199         if (in->krb5_client_princ_len)
200                 M_ASN1_I2D_len(&(a.krb5_princ), i2d_ASN1_OCTET_STRING);
201 #endif /* OPENSSL_NO_KRB5 */
202         if (in->key_arg_length > 0)
203                 M_ASN1_I2D_len_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING);
204         if (in->time != 0L)
205                 M_ASN1_I2D_len_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
206         if (in->timeout != 0L)
207                 M_ASN1_I2D_len_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
208         if (in->peer != NULL)
209                 M_ASN1_I2D_len_EXP_opt(in->peer,i2d_X509,3,v3);
210         M_ASN1_I2D_len_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,v4);
211         if (in->verify_result != X509_V_OK)
212                 M_ASN1_I2D_len_EXP_opt(&(a.verify_result),i2d_ASN1_INTEGER,5,v5);
213
214 #ifndef OPENSSL_NO_TLSEXT
215         if (in->tlsext_hostname)
216                 M_ASN1_I2D_len_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
217 #endif /* OPENSSL_NO_TLSEXT */
218         M_ASN1_I2D_seq_total();
219
220         M_ASN1_I2D_put(&(a.version),            i2d_ASN1_INTEGER);
221         M_ASN1_I2D_put(&(a.ssl_version),        i2d_ASN1_INTEGER);
222         M_ASN1_I2D_put(&(a.cipher),             i2d_ASN1_OCTET_STRING);
223         M_ASN1_I2D_put(&(a.session_id),         i2d_ASN1_OCTET_STRING);
224         M_ASN1_I2D_put(&(a.master_key),         i2d_ASN1_OCTET_STRING);
225 #ifndef OPENSSL_NO_KRB5
226         if (in->krb5_client_princ_len)
227                 M_ASN1_I2D_put(&(a.krb5_princ), i2d_ASN1_OCTET_STRING);
228 #endif /* OPENSSL_NO_KRB5 */
229         if (in->key_arg_length > 0)
230                 M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0);
231         if (in->time != 0L)
232                 M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
233         if (in->timeout != 0L)
234                 M_ASN1_I2D_put_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
235         if (in->peer != NULL)
236                 M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3);
237         M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,
238                                v4);
239         if (in->verify_result != X509_V_OK)
240                 M_ASN1_I2D_put_EXP_opt(&a.verify_result,i2d_ASN1_INTEGER,5,v5);
241 #ifndef OPENSSL_NO_TLSEXT
242         if (in->tlsext_hostname)
243                 M_ASN1_I2D_put_EXP_opt(&(a.tlsext_hostname), i2d_ASN1_OCTET_STRING,6,v6);
244 #endif /* OPENSSL_NO_TLSEXT */
245         M_ASN1_I2D_finish();
246         }
247
248 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
249              long length)
250         {
251         int version,ssl_version=0,i;
252         long id;
253         ASN1_INTEGER ai,*aip;
254         ASN1_OCTET_STRING os,*osp;
255         M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new);
256
257         aip= &ai;
258         osp= &os;
259
260         M_ASN1_D2I_Init();
261         M_ASN1_D2I_start_sequence();
262
263         ai.data=NULL; ai.length=0;
264         M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
265         version=(int)ASN1_INTEGER_get(aip);
266         if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
267
268         /* we don't care about the version right now :-) */
269         M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
270         ssl_version=(int)ASN1_INTEGER_get(aip);
271         ret->ssl_version=ssl_version;
272         if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
273
274         os.data=NULL; os.length=0;
275         M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
276         if (ssl_version == SSL2_VERSION)
277                 {
278                 if (os.length != 3)
279                         {
280                         c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
281                         goto err;
282                         }
283                 id=0x02000000L|
284                         ((unsigned long)os.data[0]<<16L)|
285                         ((unsigned long)os.data[1]<< 8L)|
286                          (unsigned long)os.data[2];
287                 }
288         else if ((ssl_version>>8) == SSL3_VERSION_MAJOR)
289                 {
290                 if (os.length != 2)
291                         {
292                         c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
293                         goto err;
294                         }
295                 id=0x03000000L|
296                         ((unsigned long)os.data[0]<<8L)|
297                          (unsigned long)os.data[1];
298                 }
299         else
300                 {
301                 SSLerr(SSL_F_D2I_SSL_SESSION,SSL_R_UNKNOWN_SSL_VERSION);
302                 return(NULL);
303                 }
304         
305         ret->cipher=NULL;
306         ret->cipher_id=id;
307
308         M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
309         if ((ssl_version>>8) == SSL3_VERSION_MAJOR)
310                 i=SSL3_MAX_SSL_SESSION_ID_LENGTH;
311         else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */
312                 i=SSL2_MAX_SSL_SESSION_ID_LENGTH;
313
314         if (os.length > i)
315                 os.length = i;
316         if (os.length > (int)sizeof(ret->session_id)) /* can't happen */
317                 os.length = sizeof(ret->session_id);
318
319         ret->session_id_length=os.length;
320         OPENSSL_assert(os.length <= (int)sizeof(ret->session_id));
321         memcpy(ret->session_id,os.data,os.length);
322
323         M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
324         if (ret->master_key_length > SSL_MAX_MASTER_KEY_LENGTH)
325                 ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
326         else
327                 ret->master_key_length=os.length;
328         memcpy(ret->master_key,os.data,ret->master_key_length);
329
330         os.length=0;
331
332 #ifndef OPENSSL_NO_KRB5
333         os.length=0;
334         M_ASN1_D2I_get_opt(osp,d2i_ASN1_OCTET_STRING,V_ASN1_OCTET_STRING);
335         if (os.data)
336                 {
337                 if (os.length > SSL_MAX_KRB5_PRINCIPAL_LENGTH)
338                         ret->krb5_client_princ_len=0;
339                 else
340                         ret->krb5_client_princ_len=os.length;
341                 memcpy(ret->krb5_client_princ,os.data,ret->krb5_client_princ_len);
342                 OPENSSL_free(os.data);
343                 os.data = NULL;
344                 os.length = 0;
345                 }
346         else
347                 ret->krb5_client_princ_len=0;
348 #endif /* OPENSSL_NO_KRB5 */
349
350         M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING);
351         if (os.length > SSL_MAX_KEY_ARG_LENGTH)
352                 ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH;
353         else
354                 ret->key_arg_length=os.length;
355         memcpy(ret->key_arg,os.data,ret->key_arg_length);
356         if (os.data != NULL) OPENSSL_free(os.data);
357
358         ai.length=0;
359         M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1);
360         if (ai.data != NULL)
361                 {
362                 ret->time=ASN1_INTEGER_get(aip);
363                 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
364                 }
365         else
366                 ret->time=(unsigned long)time(NULL);
367
368         ai.length=0;
369         M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2);
370         if (ai.data != NULL)
371                 {
372                 ret->timeout=ASN1_INTEGER_get(aip);
373                 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
374                 }
375         else
376                 ret->timeout=3;
377
378         if (ret->peer != NULL)
379                 {
380                 X509_free(ret->peer);
381                 ret->peer=NULL;
382                 }
383         M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3);
384
385         os.length=0;
386         os.data=NULL;
387         M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4);
388
389         if(os.data != NULL)
390             {
391             if (os.length > SSL_MAX_SID_CTX_LENGTH)
392                 {
393                 ret->sid_ctx_length=os.length;
394                 SSLerr(SSL_F_D2I_SSL_SESSION,SSL_R_BAD_LENGTH);
395                 }
396             else
397                 {
398                 ret->sid_ctx_length=os.length;
399                 memcpy(ret->sid_ctx,os.data,os.length);
400                 }
401             OPENSSL_free(os.data); os.data=NULL; os.length=0;
402             }
403         else
404             ret->sid_ctx_length=0;
405
406         ai.length=0;
407         M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5);
408         if (ai.data != NULL)
409                 {
410                 ret->verify_result=ASN1_INTEGER_get(aip);
411                 OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
412                 }
413         else
414                 ret->verify_result=X509_V_OK;
415
416 #ifndef OPENSSL_NO_TLSEXT
417         os.length=0;
418         os.data=NULL;
419         M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,6);
420         if (os.data)
421                 {
422                 ret->tlsext_hostname = BUF_strndup((char *)os.data, os.length);
423                 OPENSSL_free(os.data);
424                 os.data = NULL;
425                 os.length = 0;
426                 }
427         else
428                 ret->tlsext_hostname=NULL;
429
430 #endif /* OPENSSL_NO_TLSEXT */
431
432         M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION);
433         }