2
0

ssl_asn1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "ssl_locl.h"
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509.h>
  15. typedef struct {
  16. uint32_t version;
  17. int32_t ssl_version;
  18. ASN1_OCTET_STRING *cipher;
  19. ASN1_OCTET_STRING *comp_id;
  20. ASN1_OCTET_STRING *master_key;
  21. ASN1_OCTET_STRING *session_id;
  22. ASN1_OCTET_STRING *key_arg;
  23. int64_t time;
  24. int64_t timeout;
  25. X509 *peer;
  26. ASN1_OCTET_STRING *session_id_context;
  27. int32_t verify_result;
  28. ASN1_OCTET_STRING *tlsext_hostname;
  29. uint64_t tlsext_tick_lifetime_hint;
  30. uint32_t tlsext_tick_age_add;
  31. ASN1_OCTET_STRING *tlsext_tick;
  32. #ifndef OPENSSL_NO_PSK
  33. ASN1_OCTET_STRING *psk_identity_hint;
  34. ASN1_OCTET_STRING *psk_identity;
  35. #endif
  36. #ifndef OPENSSL_NO_SRP
  37. ASN1_OCTET_STRING *srp_username;
  38. #endif
  39. uint64_t flags;
  40. uint32_t max_early_data;
  41. ASN1_OCTET_STRING *alpn_selected;
  42. ASN1_OCTET_STRING *tick_nonce;
  43. uint32_t tlsext_max_fragment_len_mode;
  44. ASN1_OCTET_STRING *ticket_appdata;
  45. } SSL_SESSION_ASN1;
  46. ASN1_SEQUENCE(SSL_SESSION_ASN1) = {
  47. ASN1_EMBED(SSL_SESSION_ASN1, version, UINT32),
  48. ASN1_EMBED(SSL_SESSION_ASN1, ssl_version, INT32),
  49. ASN1_SIMPLE(SSL_SESSION_ASN1, cipher, ASN1_OCTET_STRING),
  50. ASN1_SIMPLE(SSL_SESSION_ASN1, session_id, ASN1_OCTET_STRING),
  51. ASN1_SIMPLE(SSL_SESSION_ASN1, master_key, ASN1_OCTET_STRING),
  52. ASN1_IMP_OPT(SSL_SESSION_ASN1, key_arg, ASN1_OCTET_STRING, 0),
  53. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, time, ZINT64, 1),
  54. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, timeout, ZINT64, 2),
  55. ASN1_EXP_OPT(SSL_SESSION_ASN1, peer, X509, 3),
  56. ASN1_EXP_OPT(SSL_SESSION_ASN1, session_id_context, ASN1_OCTET_STRING, 4),
  57. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, verify_result, ZINT32, 5),
  58. ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_hostname, ASN1_OCTET_STRING, 6),
  59. #ifndef OPENSSL_NO_PSK
  60. ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity_hint, ASN1_OCTET_STRING, 7),
  61. ASN1_EXP_OPT(SSL_SESSION_ASN1, psk_identity, ASN1_OCTET_STRING, 8),
  62. #endif
  63. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_tick_lifetime_hint, ZUINT64, 9),
  64. ASN1_EXP_OPT(SSL_SESSION_ASN1, tlsext_tick, ASN1_OCTET_STRING, 10),
  65. ASN1_EXP_OPT(SSL_SESSION_ASN1, comp_id, ASN1_OCTET_STRING, 11),
  66. #ifndef OPENSSL_NO_SRP
  67. ASN1_EXP_OPT(SSL_SESSION_ASN1, srp_username, ASN1_OCTET_STRING, 12),
  68. #endif
  69. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, flags, ZUINT64, 13),
  70. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_tick_age_add, ZUINT32, 14),
  71. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, max_early_data, ZUINT32, 15),
  72. ASN1_EXP_OPT(SSL_SESSION_ASN1, alpn_selected, ASN1_OCTET_STRING, 16),
  73. ASN1_EXP_OPT(SSL_SESSION_ASN1, tick_nonce, ASN1_OCTET_STRING, 17),
  74. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, tlsext_max_fragment_len_mode, ZUINT32, 18),
  75. ASN1_EXP_OPT(SSL_SESSION_ASN1, ticket_appdata, ASN1_OCTET_STRING, 19)
  76. } static_ASN1_SEQUENCE_END(SSL_SESSION_ASN1)
  77. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(SSL_SESSION_ASN1)
  78. /* Utility functions for i2d_SSL_SESSION */
  79. /* Initialise OCTET STRING from buffer and length */
  80. static void ssl_session_oinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
  81. unsigned char *data, size_t len)
  82. {
  83. os->data = data;
  84. os->length = (int)len;
  85. os->flags = 0;
  86. *dest = os;
  87. }
  88. /* Initialise OCTET STRING from string */
  89. static void ssl_session_sinit(ASN1_OCTET_STRING **dest, ASN1_OCTET_STRING *os,
  90. char *data)
  91. {
  92. if (data != NULL)
  93. ssl_session_oinit(dest, os, (unsigned char *)data, strlen(data));
  94. else
  95. *dest = NULL;
  96. }
  97. int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)
  98. {
  99. SSL_SESSION_ASN1 as;
  100. ASN1_OCTET_STRING cipher;
  101. unsigned char cipher_data[2];
  102. ASN1_OCTET_STRING master_key, session_id, sid_ctx;
  103. #ifndef OPENSSL_NO_COMP
  104. ASN1_OCTET_STRING comp_id;
  105. unsigned char comp_id_data;
  106. #endif
  107. ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;
  108. #ifndef OPENSSL_NO_SRP
  109. ASN1_OCTET_STRING srp_username;
  110. #endif
  111. #ifndef OPENSSL_NO_PSK
  112. ASN1_OCTET_STRING psk_identity, psk_identity_hint;
  113. #endif
  114. ASN1_OCTET_STRING alpn_selected;
  115. ASN1_OCTET_STRING tick_nonce;
  116. ASN1_OCTET_STRING ticket_appdata;
  117. long l;
  118. if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
  119. return 0;
  120. memset(&as, 0, sizeof(as));
  121. as.version = SSL_SESSION_ASN1_VERSION;
  122. as.ssl_version = in->ssl_version;
  123. if (in->cipher == NULL)
  124. l = in->cipher_id;
  125. else
  126. l = in->cipher->id;
  127. cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;
  128. cipher_data[1] = ((unsigned char)(l)) & 0xff;
  129. ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);
  130. #ifndef OPENSSL_NO_COMP
  131. if (in->compress_meth) {
  132. comp_id_data = (unsigned char)in->compress_meth;
  133. ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);
  134. }
  135. #endif
  136. ssl_session_oinit(&as.master_key, &master_key,
  137. in->master_key, in->master_key_length);
  138. ssl_session_oinit(&as.session_id, &session_id,
  139. in->session_id, in->session_id_length);
  140. ssl_session_oinit(&as.session_id_context, &sid_ctx,
  141. in->sid_ctx, in->sid_ctx_length);
  142. as.time = in->time;
  143. as.timeout = in->timeout;
  144. as.verify_result = in->verify_result;
  145. as.peer = in->peer;
  146. ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,
  147. in->ext.hostname);
  148. if (in->ext.tick) {
  149. ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,
  150. in->ext.tick, in->ext.ticklen);
  151. }
  152. if (in->ext.tick_lifetime_hint > 0)
  153. as.tlsext_tick_lifetime_hint = in->ext.tick_lifetime_hint;
  154. as.tlsext_tick_age_add = in->ext.tick_age_add;
  155. #ifndef OPENSSL_NO_PSK
  156. ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,
  157. in->psk_identity_hint);
  158. ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);
  159. #endif /* OPENSSL_NO_PSK */
  160. #ifndef OPENSSL_NO_SRP
  161. ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);
  162. #endif /* OPENSSL_NO_SRP */
  163. as.flags = in->flags;
  164. as.max_early_data = in->ext.max_early_data;
  165. if (in->ext.alpn_selected == NULL)
  166. as.alpn_selected = NULL;
  167. else
  168. ssl_session_oinit(&as.alpn_selected, &alpn_selected,
  169. in->ext.alpn_selected, in->ext.alpn_selected_len);
  170. if (in->ext.tick_nonce == NULL)
  171. as.tick_nonce = NULL;
  172. else
  173. ssl_session_oinit(&as.tick_nonce, &tick_nonce,
  174. in->ext.tick_nonce, in->ext.tick_nonce_len);
  175. as.tlsext_max_fragment_len_mode = in->ext.max_fragment_len_mode;
  176. if (in->ticket_appdata == NULL)
  177. as.ticket_appdata = NULL;
  178. else
  179. ssl_session_oinit(&as.ticket_appdata, &ticket_appdata,
  180. in->ticket_appdata, in->ticket_appdata_len);
  181. return i2d_SSL_SESSION_ASN1(&as, pp);
  182. }
  183. /* Utility functions for d2i_SSL_SESSION */
  184. /* OPENSSL_strndup an OCTET STRING */
  185. static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
  186. {
  187. OPENSSL_free(*pdst);
  188. *pdst = NULL;
  189. if (src == NULL)
  190. return 1;
  191. *pdst = OPENSSL_strndup((char *)src->data, src->length);
  192. if (*pdst == NULL)
  193. return 0;
  194. return 1;
  195. }
  196. /* Copy an OCTET STRING, return error if it exceeds maximum length */
  197. static int ssl_session_memcpy(unsigned char *dst, size_t *pdstlen,
  198. ASN1_OCTET_STRING *src, size_t maxlen)
  199. {
  200. if (src == NULL) {
  201. *pdstlen = 0;
  202. return 1;
  203. }
  204. if (src->length < 0 || src->length > (int)maxlen)
  205. return 0;
  206. memcpy(dst, src->data, src->length);
  207. *pdstlen = src->length;
  208. return 1;
  209. }
  210. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
  211. long length)
  212. {
  213. long id;
  214. size_t tmpl;
  215. const unsigned char *p = *pp;
  216. SSL_SESSION_ASN1 *as = NULL;
  217. SSL_SESSION *ret = NULL;
  218. as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
  219. /* ASN.1 code returns suitable error */
  220. if (as == NULL)
  221. goto err;
  222. if (!a || !*a) {
  223. ret = SSL_SESSION_new();
  224. if (ret == NULL)
  225. goto err;
  226. } else {
  227. ret = *a;
  228. }
  229. if (as->version != SSL_SESSION_ASN1_VERSION) {
  230. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
  231. goto err;
  232. }
  233. if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
  234. && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
  235. && as->ssl_version != DTLS1_BAD_VER) {
  236. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
  237. goto err;
  238. }
  239. ret->ssl_version = (int)as->ssl_version;
  240. if (as->cipher->length != 2) {
  241. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  242. goto err;
  243. }
  244. id = 0x03000000L | ((unsigned long)as->cipher->data[0] << 8L)
  245. | (unsigned long)as->cipher->data[1];
  246. ret->cipher_id = id;
  247. ret->cipher = ssl3_get_cipher_by_id(id);
  248. if (ret->cipher == NULL)
  249. goto err;
  250. if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
  251. as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
  252. goto err;
  253. if (!ssl_session_memcpy(ret->master_key, &tmpl,
  254. as->master_key, TLS13_MAX_RESUMPTION_MASTER_LENGTH))
  255. goto err;
  256. ret->master_key_length = tmpl;
  257. if (as->time != 0)
  258. ret->time = (long)as->time;
  259. else
  260. ret->time = (long)time(NULL);
  261. if (as->timeout != 0)
  262. ret->timeout = (long)as->timeout;
  263. else
  264. ret->timeout = 3;
  265. X509_free(ret->peer);
  266. ret->peer = as->peer;
  267. as->peer = NULL;
  268. if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
  269. as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
  270. goto err;
  271. /* NB: this defaults to zero which is X509_V_OK */
  272. ret->verify_result = as->verify_result;
  273. if (!ssl_session_strndup(&ret->ext.hostname, as->tlsext_hostname))
  274. goto err;
  275. #ifndef OPENSSL_NO_PSK
  276. if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
  277. goto err;
  278. if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
  279. goto err;
  280. #endif
  281. ret->ext.tick_lifetime_hint = (unsigned long)as->tlsext_tick_lifetime_hint;
  282. ret->ext.tick_age_add = as->tlsext_tick_age_add;
  283. if (as->tlsext_tick) {
  284. ret->ext.tick = as->tlsext_tick->data;
  285. ret->ext.ticklen = as->tlsext_tick->length;
  286. as->tlsext_tick->data = NULL;
  287. } else {
  288. ret->ext.tick = NULL;
  289. }
  290. #ifndef OPENSSL_NO_COMP
  291. if (as->comp_id) {
  292. if (as->comp_id->length != 1) {
  293. SSLerr(SSL_F_D2I_SSL_SESSION, SSL_R_BAD_LENGTH);
  294. goto err;
  295. }
  296. ret->compress_meth = as->comp_id->data[0];
  297. } else {
  298. ret->compress_meth = 0;
  299. }
  300. #endif
  301. #ifndef OPENSSL_NO_SRP
  302. if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
  303. goto err;
  304. #endif /* OPENSSL_NO_SRP */
  305. /* Flags defaults to zero which is fine */
  306. ret->flags = (int32_t)as->flags;
  307. ret->ext.max_early_data = as->max_early_data;
  308. if (as->alpn_selected != NULL) {
  309. if (!ssl_session_strndup((char **)&ret->ext.alpn_selected,
  310. as->alpn_selected))
  311. goto err;
  312. ret->ext.alpn_selected_len = as->alpn_selected->length;
  313. } else {
  314. ret->ext.alpn_selected = NULL;
  315. ret->ext.alpn_selected_len = 0;
  316. }
  317. if (as->tick_nonce != NULL) {
  318. ret->ext.tick_nonce = as->tick_nonce->data;
  319. ret->ext.tick_nonce_len = as->tick_nonce->length;
  320. as->tick_nonce->data = NULL;
  321. } else {
  322. ret->ext.tick_nonce = NULL;
  323. ret->ext.tick_nonce_len = 0;
  324. }
  325. ret->ext.max_fragment_len_mode = as->tlsext_max_fragment_len_mode;
  326. if (as->ticket_appdata != NULL) {
  327. ret->ticket_appdata = as->ticket_appdata->data;
  328. ret->ticket_appdata_len = as->ticket_appdata->length;
  329. as->ticket_appdata->data = NULL;
  330. } else {
  331. ret->ticket_appdata = NULL;
  332. ret->ticket_appdata_len = 0;
  333. }
  334. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  335. if ((a != NULL) && (*a == NULL))
  336. *a = ret;
  337. *pp = p;
  338. return ret;
  339. err:
  340. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  341. if ((a == NULL) || (*a != ret))
  342. SSL_SESSION_free(ret);
  343. return NULL;
  344. }