No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ssl_asn1.c 19 KiB

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