ssl_asn1.c 12 KB

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