ssl_asn1.c 13 KB

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