ssl_asn1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (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. uint32_t kex_group;
  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_EMBED(SSL_SESSION_ASN1, tlsext_max_fragment_len_mode, ZUINT32, 17),
  74. ASN1_EXP_OPT(SSL_SESSION_ASN1, ticket_appdata, ASN1_OCTET_STRING, 18),
  75. ASN1_EXP_OPT_EMBED(SSL_SESSION_ASN1, kex_group, UINT32, 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. const unsigned char *data, size_t len)
  82. {
  83. os->data = (unsigned char *)data; /* justified cast: data is not modified */
  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. const char *data)
  91. {
  92. if (data != NULL)
  93. ssl_session_oinit(dest, os, (const unsigned char *)data, strlen(data));
  94. else
  95. *dest = NULL;
  96. }
  97. int i2d_SSL_SESSION(const 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 ticket_appdata;
  116. long l;
  117. if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))
  118. return 0;
  119. memset(&as, 0, sizeof(as));
  120. as.version = SSL_SESSION_ASN1_VERSION;
  121. as.ssl_version = in->ssl_version;
  122. as.kex_group = in->kex_group;
  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 = (int64_t)in->time;
  143. as.timeout = (int64_t)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. as.tlsext_max_fragment_len_mode = in->ext.max_fragment_len_mode;
  171. if (in->ticket_appdata == NULL)
  172. as.ticket_appdata = NULL;
  173. else
  174. ssl_session_oinit(&as.ticket_appdata, &ticket_appdata,
  175. in->ticket_appdata, in->ticket_appdata_len);
  176. return i2d_SSL_SESSION_ASN1(&as, pp);
  177. }
  178. /* Utility functions for d2i_SSL_SESSION */
  179. /* OPENSSL_strndup an OCTET STRING */
  180. static int ssl_session_strndup(char **pdst, ASN1_OCTET_STRING *src)
  181. {
  182. OPENSSL_free(*pdst);
  183. *pdst = NULL;
  184. if (src == NULL)
  185. return 1;
  186. *pdst = OPENSSL_strndup((char *)src->data, src->length);
  187. if (*pdst == NULL)
  188. return 0;
  189. return 1;
  190. }
  191. /* Copy an OCTET STRING, return error if it exceeds maximum length */
  192. static int ssl_session_memcpy(unsigned char *dst, size_t *pdstlen,
  193. ASN1_OCTET_STRING *src, size_t maxlen)
  194. {
  195. if (src == NULL || src->length == 0) {
  196. *pdstlen = 0;
  197. return 1;
  198. }
  199. if (src->length < 0 || src->length > (int)maxlen)
  200. return 0;
  201. memcpy(dst, src->data, src->length);
  202. *pdstlen = src->length;
  203. return 1;
  204. }
  205. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const unsigned char **pp,
  206. long length)
  207. {
  208. long id;
  209. size_t tmpl;
  210. const unsigned char *p = *pp;
  211. SSL_SESSION_ASN1 *as = NULL;
  212. SSL_SESSION *ret = NULL;
  213. as = d2i_SSL_SESSION_ASN1(NULL, &p, length);
  214. /* ASN.1 code returns suitable error */
  215. if (as == NULL)
  216. goto err;
  217. if (a == NULL || *a == NULL) {
  218. ret = SSL_SESSION_new();
  219. if (ret == NULL)
  220. goto err;
  221. } else {
  222. ret = *a;
  223. }
  224. if (as->version != SSL_SESSION_ASN1_VERSION) {
  225. ERR_raise(ERR_LIB_SSL, SSL_R_UNKNOWN_SSL_VERSION);
  226. goto err;
  227. }
  228. if ((as->ssl_version >> 8) != SSL3_VERSION_MAJOR
  229. && (as->ssl_version >> 8) != DTLS1_VERSION_MAJOR
  230. && as->ssl_version != DTLS1_BAD_VER) {
  231. ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION);
  232. goto err;
  233. }
  234. ret->ssl_version = (int)as->ssl_version;
  235. ret->kex_group = as->kex_group;
  236. if (as->cipher->length != 2) {
  237. ERR_raise(ERR_LIB_SSL, SSL_R_CIPHER_CODE_WRONG_LENGTH);
  238. goto err;
  239. }
  240. id = 0x03000000L | ((unsigned long)as->cipher->data[0] << 8L)
  241. | (unsigned long)as->cipher->data[1];
  242. ret->cipher_id = id;
  243. ret->cipher = ssl3_get_cipher_by_id(id);
  244. if (ret->cipher == NULL)
  245. goto err;
  246. if (!ssl_session_memcpy(ret->session_id, &ret->session_id_length,
  247. as->session_id, SSL3_MAX_SSL_SESSION_ID_LENGTH))
  248. goto err;
  249. if (!ssl_session_memcpy(ret->master_key, &tmpl,
  250. as->master_key, TLS13_MAX_RESUMPTION_PSK_LENGTH))
  251. goto err;
  252. ret->master_key_length = tmpl;
  253. if (as->time != 0)
  254. ret->time = (time_t)as->time;
  255. else
  256. ret->time = time(NULL);
  257. if (as->timeout != 0)
  258. ret->timeout = (time_t)as->timeout;
  259. else
  260. ret->timeout = 3;
  261. ssl_session_calculate_timeout(ret);
  262. X509_free(ret->peer);
  263. ret->peer = as->peer;
  264. as->peer = NULL;
  265. if (!ssl_session_memcpy(ret->sid_ctx, &ret->sid_ctx_length,
  266. as->session_id_context, SSL_MAX_SID_CTX_LENGTH))
  267. goto err;
  268. /* NB: this defaults to zero which is X509_V_OK */
  269. ret->verify_result = as->verify_result;
  270. if (!ssl_session_strndup(&ret->ext.hostname, as->tlsext_hostname))
  271. goto err;
  272. #ifndef OPENSSL_NO_PSK
  273. if (!ssl_session_strndup(&ret->psk_identity_hint, as->psk_identity_hint))
  274. goto err;
  275. if (!ssl_session_strndup(&ret->psk_identity, as->psk_identity))
  276. goto err;
  277. #endif
  278. ret->ext.tick_lifetime_hint = (unsigned long)as->tlsext_tick_lifetime_hint;
  279. ret->ext.tick_age_add = as->tlsext_tick_age_add;
  280. OPENSSL_free(ret->ext.tick);
  281. if (as->tlsext_tick != NULL) {
  282. ret->ext.tick = as->tlsext_tick->data;
  283. ret->ext.ticklen = as->tlsext_tick->length;
  284. as->tlsext_tick->data = NULL;
  285. } else {
  286. ret->ext.tick = NULL;
  287. }
  288. #ifndef OPENSSL_NO_COMP
  289. if (as->comp_id) {
  290. if (as->comp_id->length != 1) {
  291. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
  292. goto err;
  293. }
  294. ret->compress_meth = as->comp_id->data[0];
  295. } else {
  296. ret->compress_meth = 0;
  297. }
  298. #endif
  299. #ifndef OPENSSL_NO_SRP
  300. if (!ssl_session_strndup(&ret->srp_username, as->srp_username))
  301. goto err;
  302. #endif /* OPENSSL_NO_SRP */
  303. /* Flags defaults to zero which is fine */
  304. ret->flags = (int32_t)as->flags;
  305. ret->ext.max_early_data = as->max_early_data;
  306. OPENSSL_free(ret->ext.alpn_selected);
  307. if (as->alpn_selected != NULL) {
  308. ret->ext.alpn_selected = as->alpn_selected->data;
  309. ret->ext.alpn_selected_len = as->alpn_selected->length;
  310. as->alpn_selected->data = NULL;
  311. } else {
  312. ret->ext.alpn_selected = NULL;
  313. ret->ext.alpn_selected_len = 0;
  314. }
  315. ret->ext.max_fragment_len_mode = as->tlsext_max_fragment_len_mode;
  316. OPENSSL_free(ret->ticket_appdata);
  317. if (as->ticket_appdata != NULL) {
  318. ret->ticket_appdata = as->ticket_appdata->data;
  319. ret->ticket_appdata_len = as->ticket_appdata->length;
  320. as->ticket_appdata->data = NULL;
  321. } else {
  322. ret->ticket_appdata = NULL;
  323. ret->ticket_appdata_len = 0;
  324. }
  325. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  326. if ((a != NULL) && (*a == NULL))
  327. *a = ret;
  328. *pp = p;
  329. return ret;
  330. err:
  331. M_ASN1_free_of(as, SSL_SESSION_ASN1);
  332. if ((a == NULL) || (*a != ret))
  333. SSL_SESSION_free(ret);
  334. return NULL;
  335. }