ssl_asn1.c 12 KB

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