os/ossrv/ssl/libssl/src/ssl_asn1.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libssl/src/ssl_asn1.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,398 @@
     1.4 +/* ssl/ssl_asn1.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include <stdlib.h>
    1.64 +#include "ssl_locl.h"
    1.65 +#include <openssl/asn1_mac.h>
    1.66 +#include <openssl/objects.h>
    1.67 +#include <openssl/x509.h>
    1.68 +
    1.69 +typedef struct ssl_session_asn1_st
    1.70 +	{
    1.71 +	ASN1_INTEGER version;
    1.72 +	ASN1_INTEGER ssl_version;
    1.73 +	ASN1_OCTET_STRING cipher;
    1.74 +	ASN1_OCTET_STRING master_key;
    1.75 +	ASN1_OCTET_STRING session_id;
    1.76 +	ASN1_OCTET_STRING session_id_context;
    1.77 +	ASN1_OCTET_STRING key_arg;
    1.78 +#ifndef OPENSSL_NO_KRB5
    1.79 +        ASN1_OCTET_STRING krb5_princ;
    1.80 +#endif /* OPENSSL_NO_KRB5 */
    1.81 +	ASN1_INTEGER time;
    1.82 +	ASN1_INTEGER timeout;
    1.83 +	ASN1_INTEGER verify_result;
    1.84 +	} SSL_SESSION_ASN1;
    1.85 +
    1.86 +EXPORT_C int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
    1.87 +	{
    1.88 +#define LSIZE2 (sizeof(long)*2)
    1.89 +	int v1=0,v2=0,v3=0,v4=0,v5=0;
    1.90 +	unsigned char buf[4],ibuf1[LSIZE2],ibuf2[LSIZE2];
    1.91 +	unsigned char ibuf3[LSIZE2],ibuf4[LSIZE2],ibuf5[LSIZE2];
    1.92 +	long l;
    1.93 +	SSL_SESSION_ASN1 a;
    1.94 +	M_ASN1_I2D_vars(in);
    1.95 +
    1.96 +	if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
    1.97 +		return(0);
    1.98 +
    1.99 +	/* Note that I cheat in the following 2 assignments.  I know
   1.100 +	 * that if the ASN1_INTEGER passed to ASN1_INTEGER_set
   1.101 +	 * is > sizeof(long)+1, the buffer will not be re-OPENSSL_malloc()ed.
   1.102 +	 * This is a bit evil but makes things simple, no dynamic allocation
   1.103 +	 * to clean up :-) */
   1.104 +	a.version.length=LSIZE2;
   1.105 +	a.version.type=V_ASN1_INTEGER;
   1.106 +	a.version.data=ibuf1;
   1.107 +	ASN1_INTEGER_set(&(a.version),SSL_SESSION_ASN1_VERSION);
   1.108 +
   1.109 +	a.ssl_version.length=LSIZE2;
   1.110 +	a.ssl_version.type=V_ASN1_INTEGER;
   1.111 +	a.ssl_version.data=ibuf2;
   1.112 +	ASN1_INTEGER_set(&(a.ssl_version),in->ssl_version);
   1.113 +
   1.114 +	a.cipher.type=V_ASN1_OCTET_STRING;
   1.115 +	a.cipher.data=buf;
   1.116 +
   1.117 +	if (in->cipher == NULL)
   1.118 +		l=in->cipher_id;
   1.119 +	else
   1.120 +		l=in->cipher->id;
   1.121 +	if (in->ssl_version == SSL2_VERSION)
   1.122 +		{
   1.123 +		a.cipher.length=3;
   1.124 +		buf[0]=((unsigned char)(l>>16L))&0xff;
   1.125 +		buf[1]=((unsigned char)(l>> 8L))&0xff;
   1.126 +		buf[2]=((unsigned char)(l     ))&0xff;
   1.127 +		}
   1.128 +	else
   1.129 +		{
   1.130 +		a.cipher.length=2;
   1.131 +		buf[0]=((unsigned char)(l>>8L))&0xff;
   1.132 +		buf[1]=((unsigned char)(l    ))&0xff;
   1.133 +		}
   1.134 +
   1.135 +	a.master_key.length=in->master_key_length;
   1.136 +	a.master_key.type=V_ASN1_OCTET_STRING;
   1.137 +	a.master_key.data=in->master_key;
   1.138 +
   1.139 +	a.session_id.length=in->session_id_length;
   1.140 +	a.session_id.type=V_ASN1_OCTET_STRING;
   1.141 +	a.session_id.data=in->session_id;
   1.142 +
   1.143 +	a.session_id_context.length=in->sid_ctx_length;
   1.144 +	a.session_id_context.type=V_ASN1_OCTET_STRING;
   1.145 +	a.session_id_context.data=in->sid_ctx;
   1.146 +
   1.147 +	a.key_arg.length=in->key_arg_length;
   1.148 +	a.key_arg.type=V_ASN1_OCTET_STRING;
   1.149 +	a.key_arg.data=in->key_arg;
   1.150 +
   1.151 +#ifndef OPENSSL_NO_KRB5
   1.152 +	if (in->krb5_client_princ_len)
   1.153 +		{
   1.154 +		a.krb5_princ.length=in->krb5_client_princ_len;
   1.155 +		a.krb5_princ.type=V_ASN1_OCTET_STRING;
   1.156 +		a.krb5_princ.data=in->krb5_client_princ;
   1.157 +		}
   1.158 +#endif /* OPENSSL_NO_KRB5 */
   1.159 + 
   1.160 +	if (in->time != 0L)
   1.161 +		{
   1.162 +		a.time.length=LSIZE2;
   1.163 +		a.time.type=V_ASN1_INTEGER;
   1.164 +		a.time.data=ibuf3;
   1.165 +		ASN1_INTEGER_set(&(a.time),in->time);
   1.166 +		}
   1.167 +
   1.168 +	if (in->timeout != 0L)
   1.169 +		{
   1.170 +		a.timeout.length=LSIZE2;
   1.171 +		a.timeout.type=V_ASN1_INTEGER;
   1.172 +		a.timeout.data=ibuf4;
   1.173 +		ASN1_INTEGER_set(&(a.timeout),in->timeout);
   1.174 +		}
   1.175 +
   1.176 +	if (in->verify_result != X509_V_OK)
   1.177 +		{
   1.178 +		a.verify_result.length=LSIZE2;
   1.179 +		a.verify_result.type=V_ASN1_INTEGER;
   1.180 +		a.verify_result.data=ibuf5;
   1.181 +		ASN1_INTEGER_set(&a.verify_result,in->verify_result);
   1.182 +		}
   1.183 +
   1.184 +	M_ASN1_I2D_len(&(a.version),		i2d_ASN1_INTEGER);
   1.185 +	M_ASN1_I2D_len(&(a.ssl_version),	i2d_ASN1_INTEGER);
   1.186 +	M_ASN1_I2D_len(&(a.cipher),		i2d_ASN1_OCTET_STRING);
   1.187 +	M_ASN1_I2D_len(&(a.session_id),		i2d_ASN1_OCTET_STRING);
   1.188 +	M_ASN1_I2D_len(&(a.master_key),		i2d_ASN1_OCTET_STRING);
   1.189 +#ifndef OPENSSL_NO_KRB5
   1.190 +	if (in->krb5_client_princ_len)
   1.191 +        	M_ASN1_I2D_len(&(a.krb5_princ),	i2d_ASN1_OCTET_STRING);
   1.192 +#endif /* OPENSSL_NO_KRB5 */
   1.193 +	if (in->key_arg_length > 0)
   1.194 +		M_ASN1_I2D_len_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING);
   1.195 +	if (in->time != 0L)
   1.196 +		M_ASN1_I2D_len_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
   1.197 +	if (in->timeout != 0L)
   1.198 +		M_ASN1_I2D_len_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
   1.199 +	if (in->peer != NULL)
   1.200 +		M_ASN1_I2D_len_EXP_opt(in->peer,i2d_X509,3,v3);
   1.201 +	M_ASN1_I2D_len_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,v4);
   1.202 +	if (in->verify_result != X509_V_OK)
   1.203 +		M_ASN1_I2D_len_EXP_opt(&(a.verify_result),i2d_ASN1_INTEGER,5,v5);
   1.204 +
   1.205 +	M_ASN1_I2D_seq_total();
   1.206 +
   1.207 +	M_ASN1_I2D_put(&(a.version),		i2d_ASN1_INTEGER);
   1.208 +	M_ASN1_I2D_put(&(a.ssl_version),	i2d_ASN1_INTEGER);
   1.209 +	M_ASN1_I2D_put(&(a.cipher),		i2d_ASN1_OCTET_STRING);
   1.210 +	M_ASN1_I2D_put(&(a.session_id),		i2d_ASN1_OCTET_STRING);
   1.211 +	M_ASN1_I2D_put(&(a.master_key),		i2d_ASN1_OCTET_STRING);
   1.212 +#ifndef OPENSSL_NO_KRB5
   1.213 +	if (in->krb5_client_princ_len)
   1.214 +        	M_ASN1_I2D_put(&(a.krb5_princ),	i2d_ASN1_OCTET_STRING);
   1.215 +#endif /* OPENSSL_NO_KRB5 */
   1.216 +	if (in->key_arg_length > 0)
   1.217 +		M_ASN1_I2D_put_IMP_opt(&(a.key_arg),i2d_ASN1_OCTET_STRING,0);
   1.218 +	if (in->time != 0L)
   1.219 +		M_ASN1_I2D_put_EXP_opt(&(a.time),i2d_ASN1_INTEGER,1,v1);
   1.220 +	if (in->timeout != 0L)
   1.221 +		M_ASN1_I2D_put_EXP_opt(&(a.timeout),i2d_ASN1_INTEGER,2,v2);
   1.222 +	if (in->peer != NULL)
   1.223 +		M_ASN1_I2D_put_EXP_opt(in->peer,i2d_X509,3,v3);
   1.224 +	M_ASN1_I2D_put_EXP_opt(&a.session_id_context,i2d_ASN1_OCTET_STRING,4,
   1.225 +			       v4);
   1.226 +	if (in->verify_result != X509_V_OK)
   1.227 +		M_ASN1_I2D_put_EXP_opt(&a.verify_result,i2d_ASN1_INTEGER,5,v5);
   1.228 +	M_ASN1_I2D_finish();
   1.229 +	}
   1.230 +
   1.231 +EXPORT_C SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
   1.232 +	     long length)
   1.233 +	{
   1.234 +	int version,ssl_version=0,i;
   1.235 +	long id;
   1.236 +	ASN1_INTEGER ai,*aip;
   1.237 +	ASN1_OCTET_STRING os,*osp;
   1.238 +	M_ASN1_D2I_vars(a,SSL_SESSION *,SSL_SESSION_new);
   1.239 +
   1.240 +	aip= &ai;
   1.241 +	osp= &os;
   1.242 +
   1.243 +	M_ASN1_D2I_Init();
   1.244 +	M_ASN1_D2I_start_sequence();
   1.245 +
   1.246 +	ai.data=NULL; ai.length=0;
   1.247 +	M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
   1.248 +	version=(int)ASN1_INTEGER_get(aip);
   1.249 +	if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
   1.250 +
   1.251 +	/* we don't care about the version right now :-) */
   1.252 +	M_ASN1_D2I_get_x(ASN1_INTEGER,aip,d2i_ASN1_INTEGER);
   1.253 +	ssl_version=(int)ASN1_INTEGER_get(aip);
   1.254 +	ret->ssl_version=ssl_version;
   1.255 +	if (ai.data != NULL) { OPENSSL_free(ai.data); ai.data=NULL; ai.length=0; }
   1.256 +
   1.257 +	os.data=NULL; os.length=0;
   1.258 +	M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
   1.259 +	if (ssl_version == SSL2_VERSION)
   1.260 +		{
   1.261 +		if (os.length != 3)
   1.262 +			{
   1.263 +			c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
   1.264 +			goto err;
   1.265 +			}
   1.266 +		id=0x02000000L|
   1.267 +			((unsigned long)os.data[0]<<16L)|
   1.268 +			((unsigned long)os.data[1]<< 8L)|
   1.269 +			 (unsigned long)os.data[2];
   1.270 +		}
   1.271 +	else if ((ssl_version>>8) == SSL3_VERSION_MAJOR)
   1.272 +		{
   1.273 +		if (os.length != 2)
   1.274 +			{
   1.275 +			c.error=SSL_R_CIPHER_CODE_WRONG_LENGTH;
   1.276 +			goto err;
   1.277 +			}
   1.278 +		id=0x03000000L|
   1.279 +			((unsigned long)os.data[0]<<8L)|
   1.280 +			 (unsigned long)os.data[1];
   1.281 +		}
   1.282 +	else
   1.283 +		{
   1.284 +		SSLerr(SSL_F_D2I_SSL_SESSION,SSL_R_UNKNOWN_SSL_VERSION);
   1.285 +		return(NULL);
   1.286 +		}
   1.287 +	
   1.288 +	ret->cipher=NULL;
   1.289 +	ret->cipher_id=id;
   1.290 +
   1.291 +	M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
   1.292 +	if ((ssl_version>>8) == SSL3_VERSION_MAJOR)
   1.293 +		i=SSL3_MAX_SSL_SESSION_ID_LENGTH;
   1.294 +	else /* if (ssl_version>>8 == SSL2_VERSION_MAJOR) */
   1.295 +		i=SSL2_MAX_SSL_SESSION_ID_LENGTH;
   1.296 +
   1.297 +	if (os.length > i)
   1.298 +		os.length = i;
   1.299 +	if (os.length > (int)sizeof(ret->session_id)) /* can't happen */
   1.300 +		os.length = sizeof(ret->session_id);
   1.301 +
   1.302 +	ret->session_id_length=os.length;
   1.303 +	OPENSSL_assert(os.length <= (int)sizeof(ret->session_id));
   1.304 +	memcpy(ret->session_id,os.data,os.length);
   1.305 +
   1.306 +	M_ASN1_D2I_get_x(ASN1_OCTET_STRING,osp,d2i_ASN1_OCTET_STRING);
   1.307 +	if (ret->master_key_length > SSL_MAX_MASTER_KEY_LENGTH)
   1.308 +		ret->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
   1.309 +	else
   1.310 +		ret->master_key_length=os.length;
   1.311 +	memcpy(ret->master_key,os.data,ret->master_key_length);
   1.312 +
   1.313 +	os.length=0;
   1.314 +
   1.315 +#ifndef OPENSSL_NO_KRB5
   1.316 +	os.length=0;
   1.317 +	M_ASN1_D2I_get_opt(osp,d2i_ASN1_OCTET_STRING,V_ASN1_OCTET_STRING);
   1.318 +	if (os.data)
   1.319 +		{
   1.320 +        	if (os.length > SSL_MAX_KRB5_PRINCIPAL_LENGTH)
   1.321 +            		ret->krb5_client_princ_len=0;
   1.322 +		else
   1.323 +			ret->krb5_client_princ_len=os.length;
   1.324 +		memcpy(ret->krb5_client_princ,os.data,ret->krb5_client_princ_len);
   1.325 +		OPENSSL_free(os.data);
   1.326 +		os.data = NULL;
   1.327 +		os.length = 0;
   1.328 +		}
   1.329 +	else
   1.330 +		ret->krb5_client_princ_len=0;
   1.331 +#endif /* OPENSSL_NO_KRB5 */
   1.332 +
   1.333 +	M_ASN1_D2I_get_IMP_opt(osp,d2i_ASN1_OCTET_STRING,0,V_ASN1_OCTET_STRING);
   1.334 +	if (os.length > SSL_MAX_KEY_ARG_LENGTH)
   1.335 +		ret->key_arg_length=SSL_MAX_KEY_ARG_LENGTH;
   1.336 +	else
   1.337 +		ret->key_arg_length=os.length;
   1.338 +	memcpy(ret->key_arg,os.data,ret->key_arg_length);
   1.339 +	if (os.data != NULL) OPENSSL_free(os.data);
   1.340 +
   1.341 +	ai.length=0;
   1.342 +	M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,1);
   1.343 +	if (ai.data != NULL)
   1.344 +		{
   1.345 +		ret->time=ASN1_INTEGER_get(aip);
   1.346 +		OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
   1.347 +		}
   1.348 +	else
   1.349 +		ret->time=(unsigned long)time(NULL);
   1.350 +
   1.351 +	ai.length=0;
   1.352 +	M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,2);
   1.353 +	if (ai.data != NULL)
   1.354 +		{
   1.355 +		ret->timeout=ASN1_INTEGER_get(aip);
   1.356 +		OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
   1.357 +		}
   1.358 +	else
   1.359 +		ret->timeout=3;
   1.360 +
   1.361 +	if (ret->peer != NULL)
   1.362 +		{
   1.363 +		X509_free(ret->peer);
   1.364 +		ret->peer=NULL;
   1.365 +		}
   1.366 +	M_ASN1_D2I_get_EXP_opt(ret->peer,d2i_X509,3);
   1.367 +
   1.368 +	os.length=0;
   1.369 +	os.data=NULL;
   1.370 +	M_ASN1_D2I_get_EXP_opt(osp,d2i_ASN1_OCTET_STRING,4);
   1.371 +
   1.372 +	if(os.data != NULL)
   1.373 +	    {
   1.374 +	    if (os.length > SSL_MAX_SID_CTX_LENGTH)
   1.375 +		{
   1.376 +		ret->sid_ctx_length=os.length;
   1.377 +		SSLerr(SSL_F_D2I_SSL_SESSION,SSL_R_BAD_LENGTH);
   1.378 +		}
   1.379 +	    else
   1.380 +		{
   1.381 +		ret->sid_ctx_length=os.length;
   1.382 +		memcpy(ret->sid_ctx,os.data,os.length);
   1.383 +		}
   1.384 +	    OPENSSL_free(os.data); os.data=NULL; os.length=0;
   1.385 +	    }
   1.386 +	else
   1.387 +	    ret->sid_ctx_length=0;
   1.388 +
   1.389 +	ai.length=0;
   1.390 +	M_ASN1_D2I_get_EXP_opt(aip,d2i_ASN1_INTEGER,5);
   1.391 +	if (ai.data != NULL)
   1.392 +		{
   1.393 +		ret->verify_result=ASN1_INTEGER_get(aip);
   1.394 +		OPENSSL_free(ai.data); ai.data=NULL; ai.length=0;
   1.395 +		}
   1.396 +	else
   1.397 +		ret->verify_result=X509_V_OK;
   1.398 +
   1.399 +
   1.400 +	M_ASN1_D2I_Finish(a,SSL_SESSION_free,SSL_F_D2I_SSL_SESSION);
   1.401 +	}