ssl_asn1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. * Copyright 2005 Nokia. All rights reserved.
  60. *
  61. * The portions of the attached software ("Contribution") is developed by
  62. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  63. * license.
  64. *
  65. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  66. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  67. * support (see RFC 4279) to OpenSSL.
  68. *
  69. * No patent licenses or other rights except those expressly stated in
  70. * the OpenSSL open source license shall be deemed granted or received
  71. * expressly, by implication, estoppel, or otherwise.
  72. *
  73. * No assurances are provided by Nokia that the Contribution does not
  74. * infringe the patent or other intellectual property rights of any third
  75. * party or that the license provides you with all the necessary rights
  76. * to make use of the Contribution.
  77. *
  78. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  79. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  80. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  81. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  82. * OTHERWISE.
  83. */
  84. #include <stdio.h>
  85. #include <stdlib.h>
  86. #include "ssl_locl.h"
  87. #include <openssl/asn1t.h>
  88. #include <openssl/x509.h>
  89. typedef struct {
  90. long version;
  91. long ssl_version;
  92. ASN1_OCTET_STRING *cipher;
  93. ASN1_OCTET_STRING *comp_id;
  94. ASN1_OCTET_STRING *master_key;
  95. ASN1_OCTET_STRING *session_id;
  96. ASN1_OCTET_STRING *key_arg;
  97. long time;
  98. long timeout;
  99. X509 *peer;
  100. ASN1_OCTET_STRING *session_id_context;
  101. long verify_result;
  102. ASN1_OCTET_STRING *tlsext_hostname;
  103. long tlsext_tick_lifetime_hint;
  104. ASN1_OCTET_STRING *tlsext_tick;
  105. #ifndef OPENSSL_NO_PSK
  106. ASN1_OCTET_STRING *psk_identity_hint;
  107. ASN1_OCTET_STRING *psk_identity;
  108. #endif
  109. #ifndef OPENSSL_NO_SRP
  110. ASN1_OCTET_STRING *srp_username;
  111. #endif
  112. long flags;
  113. } SSL_SESSION_ASN1;
  114. ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
  115. ASN1_SIMPLE(SSL_SESSION_ASN1, version, LONG),
  116. ASN1_SIMPLE(SSL_SESSION_ASN1, ssl_version, LONG),
  117. ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
  118. ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
  119. ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
  120. ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
  121. ASN1_EXP_OPT(SSL_SESSION_ASN1, time, ZLONG, 1),
  122. ASN1_EXP_OPT(SSL_SESSION_ASN1, timeout, ZLONG, 2),
  123. ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
  124. ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
  125. ASN1_EXP_OPT(SSL_SESSION_ASN1, verify_result, ZLONG, 5),
  126. ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
  127. #ifndef OPENSSL_NO_PSK
  128. ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
  129. ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
  130. #endif
  131. ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZLONG, 9),
  132. ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
  133. ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
  134. #ifndef OPENSSL_NO_SRP
  135. ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
  136. #endif
  137. ASN1_EXP_OPT(SSL_SESSION_ASN1, flags, ZLONG, 13)
  138. } static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
  139. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
  140. /* Utility functions for i2d_SSL_SESSION */
  141. /* Initialise OCTET STRING from buffer and length */
  142. static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
  143. unsigned char *data, size_t len)
  144. {
  145. os->data = data;
  146. os->length = len;
  147. os->flags = 0;
  148. *dest = os;
  149. }
  150. /* Initialise OCTET STRING from string */
  151. static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
  152. char *data)
  153. {
  154. if (data != NULL)
  155. ssl_session_oinit(dest, os, (unsigned char *)data, strlen(data));
  156. else
  157. *dest = NULL;
  158. }
  159. int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
  160. {
  161. SSL_SESSION_ASN1 as;
  162. ASN1_OCTET_STRING cipher;
  163. unsigned char cipher_data[2];
  164. ASN1_OCTET_STRING master_key, session_id, sid_ctx;
  165. #ifndef OPENSSL_NO_COMP
  166. ASN1_OCTET_STRING comp_id;
  167. unsigned char comp_id_data;
  168. #endif
  169. ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
  170. #ifndef OPENSSL_NO_SRP
  171. ASN1_OCTET_STRING srp_username;
  172. #endif
  173. #ifndef OPENSSL_NO_PSK
  174. ASN1_OCTET_STRING psk_identity, psk_identity_hint;
  175. #endif
  176. long l;
  177. if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
  178. return 0;
  179. memset(&as, 0, sizeof(as));
  180. as.version = SSL_SESSION_ASN1_VERSION;
  181. as.ssl_version = in->ssl_version;
  182. if (in->cipher == NULL)
  183. l = in->cipher_id;
  184. else
  185. l = in->cipher->id;
  186. cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
  187. cipher_data[1] = ((unsigned char)(l)) & 0xff;
  188. ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
  189. #ifndef OPENSSL_NO_COMP
  190. if (in->compress_meth) {
  191. comp_id_data = (unsigned char)in->compress_meth;
  192. ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
  193. }
  194. #endif
  195. ssl_session_oinit(&as.master_key, &master_key,
  196. in->master_key, in->master_key_length);
  197. ssl_session_oinit(&as.session_id, &session_id,
  198. in->session_id, in->session_id_length);
  199. ssl_session_oinit(&as.session_id_context, &sid_ctx,
  200. in->sid_ctx, in->sid_ctx_length);
  201. as.time = in->time;
  202. as.timeout = in->timeout;
  203. as.verify_result = in->verify_result;
  204. as.peer = in->peer;
  205. ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
  206. in->tlsext_hostname);
  207. if (in->tlsext_tick) {
  208. ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
  209. in->tlsext_tick, in->tlsext_ticklen);
  210. }
  211. if (in->tlsext_tick_lifetime_hint > 0)
  212. as.tlsext_tick_lifetime_hint = in->tlsext_tick_lifetime_hint;
  213. #ifndef OPENSSL_NO_PSK
  214. ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
  215. in->psk_identity_hint);
  216. ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
  217. #endif /* OPENSSL_NO_PSK */
  218. #ifndef OPENSSL_NO_SRP
  219. ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
  220. #endif /* OPENSSL_NO_SRP */
  221. as.flags = in->flags;
  222. return i2d_SSL_SESSION_ASN1(&as, pp);
  223. }
  224. /* Utility functions for d2i_SSL_SESSION */
  225. /* OPENSSL_strndup an OCTET STRING */
  226. static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
  227. {
  228. OPENSSL_free(*pdst);
  229. *pdst = NULL;
  230. if (src == NULL)
  231. return 1;
  232. *pdst = OPENSSL_strndup((char *)src->data, src->length);
  233. if (*pdst == NULL)
  234. return 0;
  235. return 1;
  236. }
  237. /* Copy an OCTET STRING, return error if it exceeds maximum length */
  238. static int ssl_session_memcpy(unsigned char *dst, unsigned int *pdstlen,
  239. ASN1_OCTET_STRING *src, int maxlen)
  240. {
  241. if (src == NULL) {
  242. *pdstlen = 0;
  243. return 1;
  244. }
  245. if (src->length > maxlen)
  246. return 0;
  247. memcpy(dst, src->data, src->length);
  248. *pdstlen = src->length;
  249. return 1;
  250. }
  251. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
  252. long length)
  253. {
  254. long id;
  255. unsigned int tmpl;
  256. const unsigned char *p = *pp;
  257. SSL_SESSION_ASN1 *as = NULL;
  258. SSL_SESSION *ret = NULL;
  259. as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
  260. /* ASN.1 code returns suitable error */
  261. if (as == NULL)
  262. goto err;
  263. if (!a || !*a) {
  264. ret = SSL_SESSION_new();
  265. if (ret == NULL)
  266. goto err;
  267. } else {
  268. ret = *a;
  269. }
  270. if (as->version != SSL_SESSION_ASN1_VERSION) {
  271. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
  272. goto err;
  273. }
  274. if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
  275. && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
  276. && as->ssl_version != DTLS1_BAD_VER) {
  277. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
  278. goto err;
  279. }
  280. ret->ssl_version = (int)as->ssl_version;
  281. if (as->cipher->length != 2) {
  282. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  283. goto err;
  284. }
  285. p = as->cipher->data;
  286. id = 0x03000000L | ((unsigned long)p[0] << 8L) | (unsigned long)p[1];
  287. ret->cipher = NULL;
  288. ret->cipher_id = id;
  289. if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
  290. as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
  291. goto err;
  292. if (!ssl_session_memcpy(ret->master_key, &tmpl,
  293. as->master_key, SSL_MAX_MASTER_KEY_LENGTH))
  294. goto err;
  295. ret->master_key_length = tmpl;
  296. if (as->time != 0)
  297. ret->time = as->time;
  298. else
  299. ret->time = (unsigned long)time(NULL);
  300. if (as->timeout != 0)
  301. ret->timeout = as->timeout;
  302. else
  303. ret->timeout = 3;
  304. X509_free(ret->peer);
  305. ret->peer = as->peer;
  306. as->peer = NULL;
  307. if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
  308. as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
  309. goto err;
  310. /* NB: this defaults to zero which is X509_V_OK */
  311. ret->verify_result = as->verify_result;
  312. if (!ssl_session_strndup(&ret->tlsext_hostname, as->tlsext_hostname))
  313. goto err;
  314. #ifndef OPENSSL_NO_PSK
  315. if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
  316. goto err;
  317. if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
  318. goto err;
  319. #endif
  320. ret->tlsext_tick_lifetime_hint = as->tlsext_tick_lifetime_hint;
  321. if (as->tlsext_tick) {
  322. ret->tlsext_tick = as->tlsext_tick->data;
  323. ret->tlsext_ticklen = as->tlsext_tick->length;
  324. as->tlsext_tick->data = NULL;
  325. } else {
  326. ret->tlsext_tick = NULL;
  327. }
  328. #ifndef OPENSSL_NO_COMP
  329. if (as->comp_id) {
  330. if (as->comp_id->length != 1) {
  331. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_BAD_LENGTH);
  332. goto err;
  333. }
  334. ret->compress_meth = as->comp_id->data[0];
  335. } else {
  336. ret->compress_meth = 0;
  337. }
  338. #endif
  339. #ifndef OPENSSL_NO_SRP
  340. if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
  341. goto err;
  342. #endif /* OPENSSL_NO_SRP */
  343. /* Flags defaults to zero which is fine */
  344. ret->flags = as->flags;
  345. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  346. if ((a != NULL) && (*a == NULL))
  347. *a = ret;
  348. *pp = p;
  349. return ret;
  350. err:
  351. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  352. if ((a == NULL) || (*a != ret))
  353. SSL_SESSION_free(ret);
  354. return NULL;
  355. }