extensions_cust.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Copyright 2014-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Custom extension utility functions */
  10. #include <openssl/ct.h>
  11. #include "../ssl_local.h"
  12. #include "internal/cryptlib.h"
  13. #include "internal/ssl_unwrap.h"
  14. #include "statem_local.h"
  15. typedef struct {
  16. void *add_arg;
  17. custom_ext_add_cb add_cb;
  18. custom_ext_free_cb free_cb;
  19. } custom_ext_add_cb_wrap;
  20. typedef struct {
  21. void *parse_arg;
  22. custom_ext_parse_cb parse_cb;
  23. } custom_ext_parse_cb_wrap;
  24. /*
  25. * Provide thin wrapper callbacks which convert new style arguments to old style
  26. */
  27. static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,
  28. unsigned int context,
  29. const unsigned char **out,
  30. size_t *outlen, X509 *x, size_t chainidx,
  31. int *al, void *add_arg)
  32. {
  33. custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
  34. if (add_cb_wrap->add_cb == NULL)
  35. return 1;
  36. return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,
  37. add_cb_wrap->add_arg);
  38. }
  39. static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,
  40. unsigned int context,
  41. const unsigned char *out, void *add_arg)
  42. {
  43. custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
  44. if (add_cb_wrap->free_cb == NULL)
  45. return;
  46. add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);
  47. }
  48. static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,
  49. unsigned int context,
  50. const unsigned char *in,
  51. size_t inlen, X509 *x, size_t chainidx,
  52. int *al, void *parse_arg)
  53. {
  54. custom_ext_parse_cb_wrap *parse_cb_wrap =
  55. (custom_ext_parse_cb_wrap *)parse_arg;
  56. if (parse_cb_wrap->parse_cb == NULL)
  57. return 1;
  58. return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,
  59. parse_cb_wrap->parse_arg);
  60. }
  61. /*
  62. * Find a custom extension from the list. The |role| param is there to
  63. * support the legacy API where custom extensions for client and server could
  64. * be set independently on the same SSL_CTX. It is set to ENDPOINT_SERVER if we
  65. * are trying to find a method relevant to the server, ENDPOINT_CLIENT for the
  66. * client, or ENDPOINT_BOTH for either
  67. */
  68. custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
  69. ENDPOINT role, unsigned int ext_type,
  70. size_t *idx)
  71. {
  72. size_t i;
  73. custom_ext_method *meth = exts->meths;
  74. for (i = 0; i < exts->meths_count; i++, meth++) {
  75. if (ext_type == meth->ext_type
  76. && (role == ENDPOINT_BOTH || role == meth->role
  77. || meth->role == ENDPOINT_BOTH)) {
  78. if (idx != NULL)
  79. *idx = i;
  80. return meth;
  81. }
  82. }
  83. return NULL;
  84. }
  85. /*
  86. * Initialise custom extensions flags to indicate neither sent nor received.
  87. */
  88. void custom_ext_init(custom_ext_methods *exts)
  89. {
  90. size_t i;
  91. custom_ext_method *meth = exts->meths;
  92. for (i = 0; i < exts->meths_count; i++, meth++)
  93. meth->ext_flags = 0;
  94. }
  95. /* Pass received custom extension data to the application for parsing. */
  96. int custom_ext_parse(SSL_CONNECTION *s, unsigned int context,
  97. unsigned int ext_type,
  98. const unsigned char *ext_data, size_t ext_size, X509 *x,
  99. size_t chainidx)
  100. {
  101. int al = 0;
  102. custom_ext_methods *exts = &s->cert->custext;
  103. custom_ext_method *meth;
  104. ENDPOINT role = ENDPOINT_BOTH;
  105. if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
  106. role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
  107. meth = custom_ext_find(exts, role, ext_type, NULL);
  108. /* If not found return success */
  109. if (!meth)
  110. return 1;
  111. /* Check if extension is defined for our protocol. If not, skip */
  112. if (!extension_is_relevant(s, meth->context, context))
  113. return 1;
  114. if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
  115. | SSL_EXT_TLS1_3_SERVER_HELLO
  116. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
  117. /*
  118. * If it's ServerHello or EncryptedExtensions we can't have any
  119. * extensions not sent in ClientHello.
  120. */
  121. if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
  122. SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
  123. return 0;
  124. }
  125. }
  126. /*
  127. * Extensions received in the ClientHello or CertificateRequest are marked
  128. * with the SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
  129. * extensions in the response messages
  130. */
  131. if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST))
  132. != 0)
  133. meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
  134. /* If no parse function set return success */
  135. if (meth->parse_cb == NULL)
  136. return 1;
  137. if (meth->parse_cb(SSL_CONNECTION_GET_SSL(s), ext_type, context, ext_data,
  138. ext_size, x, chainidx, &al, meth->parse_arg) <= 0) {
  139. SSLfatal(s, al, SSL_R_BAD_EXTENSION);
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. /*
  145. * Request custom extension data from the application and add to the return
  146. * buffer.
  147. */
  148. int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,
  149. size_t chainidx, int maxversion)
  150. {
  151. custom_ext_methods *exts = &s->cert->custext;
  152. custom_ext_method *meth;
  153. size_t i;
  154. int al;
  155. int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
  156. for (i = 0; i < exts->meths_count; i++) {
  157. const unsigned char *out = NULL;
  158. size_t outlen = 0;
  159. meth = exts->meths + i;
  160. if (!should_add_extension(s, meth->context, context, maxversion))
  161. continue;
  162. if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
  163. | SSL_EXT_TLS1_3_SERVER_HELLO
  164. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  165. | SSL_EXT_TLS1_3_CERTIFICATE
  166. | SSL_EXT_TLS1_3_RAW_PUBLIC_KEY
  167. | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) {
  168. /* Only send extensions present in ClientHello/CertificateRequest */
  169. if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
  170. continue;
  171. }
  172. /*
  173. * We skip it if the callback is absent - except for a ClientHello where
  174. * we add an empty extension.
  175. */
  176. if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
  177. continue;
  178. if (meth->add_cb != NULL) {
  179. int cb_retval = meth->add_cb(SSL_CONNECTION_GET_SSL(s),
  180. meth->ext_type, context, &out,
  181. &outlen, x, chainidx, &al,
  182. meth->add_arg);
  183. if (cb_retval < 0) {
  184. if (!for_comp)
  185. SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
  186. return 0; /* error */
  187. }
  188. if (cb_retval == 0)
  189. continue; /* skip this extension */
  190. }
  191. if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
  192. || !WPACKET_start_sub_packet_u16(pkt)
  193. || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
  194. || !WPACKET_close(pkt)) {
  195. if (meth->free_cb != NULL)
  196. meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type, context,
  197. out, meth->add_arg);
  198. if (!for_comp)
  199. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  200. return 0;
  201. }
  202. if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
  203. /*
  204. * We can't send duplicates: code logic should prevent this.
  205. */
  206. if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {
  207. if (meth->free_cb != NULL)
  208. meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type,
  209. context, out, meth->add_arg);
  210. if (!for_comp)
  211. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  212. return 0;
  213. }
  214. /*
  215. * Indicate extension has been sent: this is both a sanity check to
  216. * ensure we don't send duplicate extensions and indicates that it
  217. * is not an error if the extension is present in ServerHello.
  218. */
  219. meth->ext_flags |= SSL_EXT_FLAG_SENT;
  220. }
  221. if (meth->free_cb != NULL)
  222. meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type, context,
  223. out, meth->add_arg);
  224. }
  225. return 1;
  226. }
  227. /* Copy the flags from src to dst for any extensions that exist in both */
  228. int custom_exts_copy_flags(custom_ext_methods *dst,
  229. const custom_ext_methods *src)
  230. {
  231. size_t i;
  232. custom_ext_method *methsrc = src->meths;
  233. for (i = 0; i < src->meths_count; i++, methsrc++) {
  234. custom_ext_method *methdst = custom_ext_find(dst, methsrc->role,
  235. methsrc->ext_type, NULL);
  236. if (methdst == NULL)
  237. continue;
  238. methdst->ext_flags = methsrc->ext_flags;
  239. }
  240. return 1;
  241. }
  242. /* Copy table of custom extensions */
  243. int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
  244. {
  245. size_t i;
  246. int err = 0;
  247. if (src->meths_count > 0) {
  248. dst->meths =
  249. OPENSSL_memdup(src->meths,
  250. sizeof(*src->meths) * src->meths_count);
  251. if (dst->meths == NULL)
  252. return 0;
  253. dst->meths_count = src->meths_count;
  254. for (i = 0; i < src->meths_count; i++) {
  255. custom_ext_method *methsrc = src->meths + i;
  256. custom_ext_method *methdst = dst->meths + i;
  257. if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
  258. continue;
  259. /*
  260. * We have found an old style API wrapper. We need to copy the
  261. * arguments too.
  262. */
  263. if (err) {
  264. methdst->add_arg = NULL;
  265. methdst->parse_arg = NULL;
  266. continue;
  267. }
  268. methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
  269. sizeof(custom_ext_add_cb_wrap));
  270. methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
  271. sizeof(custom_ext_parse_cb_wrap));
  272. if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
  273. err = 1;
  274. }
  275. }
  276. if (err) {
  277. custom_exts_free(dst);
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. void custom_exts_free(custom_ext_methods *exts)
  283. {
  284. size_t i;
  285. custom_ext_method *meth;
  286. for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
  287. if (meth->add_cb != custom_ext_add_old_cb_wrap)
  288. continue;
  289. /* Old style API wrapper. Need to free the arguments too */
  290. OPENSSL_free(meth->add_arg);
  291. OPENSSL_free(meth->parse_arg);
  292. }
  293. OPENSSL_free(exts->meths);
  294. exts->meths = NULL;
  295. exts->meths_count = 0;
  296. }
  297. /* Return true if a client custom extension exists, false otherwise */
  298. int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
  299. {
  300. return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
  301. NULL) != NULL;
  302. }
  303. int ossl_tls_add_custom_ext_intern(SSL_CTX *ctx, custom_ext_methods *exts,
  304. ENDPOINT role, unsigned int ext_type,
  305. unsigned int context,
  306. SSL_custom_ext_add_cb_ex add_cb,
  307. SSL_custom_ext_free_cb_ex free_cb,
  308. void *add_arg,
  309. SSL_custom_ext_parse_cb_ex parse_cb,
  310. void *parse_arg)
  311. {
  312. custom_ext_method *meth, *tmp;
  313. /*
  314. * Check application error: if add_cb is not set free_cb will never be
  315. * called.
  316. */
  317. if (add_cb == NULL && free_cb != NULL)
  318. return 0;
  319. if (exts == NULL)
  320. exts = &ctx->cert->custext;
  321. #ifndef OPENSSL_NO_CT
  322. /*
  323. * We don't want applications registering callbacks for SCT extensions
  324. * whilst simultaneously using the built-in SCT validation features, as
  325. * these two things may not play well together.
  326. */
  327. if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
  328. && (context & SSL_EXT_CLIENT_HELLO) != 0
  329. && ctx != NULL
  330. && SSL_CTX_ct_is_enabled(ctx))
  331. return 0;
  332. #endif
  333. /*
  334. * Don't add if extension supported internally, but make exception
  335. * for extension types that previously were not supported, but now are.
  336. */
  337. if (SSL_extension_supported(ext_type)
  338. && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
  339. return 0;
  340. /* Extension type must fit in 16 bits */
  341. if (ext_type > 0xffff)
  342. return 0;
  343. /* Search for duplicate */
  344. if (custom_ext_find(exts, role, ext_type, NULL))
  345. return 0;
  346. tmp = OPENSSL_realloc(exts->meths,
  347. (exts->meths_count + 1) * sizeof(custom_ext_method));
  348. if (tmp == NULL)
  349. return 0;
  350. exts->meths = tmp;
  351. meth = exts->meths + exts->meths_count;
  352. memset(meth, 0, sizeof(*meth));
  353. meth->role = role;
  354. meth->context = context;
  355. meth->parse_cb = parse_cb;
  356. meth->add_cb = add_cb;
  357. meth->free_cb = free_cb;
  358. meth->ext_type = ext_type;
  359. meth->add_arg = add_arg;
  360. meth->parse_arg = parse_arg;
  361. exts->meths_count++;
  362. return 1;
  363. }
  364. static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,
  365. unsigned int ext_type,
  366. unsigned int context,
  367. custom_ext_add_cb add_cb,
  368. custom_ext_free_cb free_cb,
  369. void *add_arg,
  370. custom_ext_parse_cb parse_cb, void *parse_arg)
  371. {
  372. custom_ext_add_cb_wrap *add_cb_wrap
  373. = OPENSSL_malloc(sizeof(*add_cb_wrap));
  374. custom_ext_parse_cb_wrap *parse_cb_wrap
  375. = OPENSSL_malloc(sizeof(*parse_cb_wrap));
  376. int ret;
  377. if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
  378. OPENSSL_free(add_cb_wrap);
  379. OPENSSL_free(parse_cb_wrap);
  380. return 0;
  381. }
  382. add_cb_wrap->add_arg = add_arg;
  383. add_cb_wrap->add_cb = add_cb;
  384. add_cb_wrap->free_cb = free_cb;
  385. parse_cb_wrap->parse_arg = parse_arg;
  386. parse_cb_wrap->parse_cb = parse_cb;
  387. ret = ossl_tls_add_custom_ext_intern(ctx, NULL, role, ext_type,
  388. context,
  389. custom_ext_add_old_cb_wrap,
  390. custom_ext_free_old_cb_wrap,
  391. add_cb_wrap,
  392. custom_ext_parse_old_cb_wrap,
  393. parse_cb_wrap);
  394. if (!ret) {
  395. OPENSSL_free(add_cb_wrap);
  396. OPENSSL_free(parse_cb_wrap);
  397. }
  398. return ret;
  399. }
  400. /* Application level functions to add the old custom extension callbacks */
  401. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  402. custom_ext_add_cb add_cb,
  403. custom_ext_free_cb free_cb,
  404. void *add_arg,
  405. custom_ext_parse_cb parse_cb, void *parse_arg)
  406. {
  407. return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,
  408. SSL_EXT_TLS1_2_AND_BELOW_ONLY
  409. | SSL_EXT_CLIENT_HELLO
  410. | SSL_EXT_TLS1_2_SERVER_HELLO
  411. | SSL_EXT_IGNORE_ON_RESUMPTION,
  412. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  413. }
  414. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  415. custom_ext_add_cb add_cb,
  416. custom_ext_free_cb free_cb,
  417. void *add_arg,
  418. custom_ext_parse_cb parse_cb, void *parse_arg)
  419. {
  420. return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,
  421. SSL_EXT_TLS1_2_AND_BELOW_ONLY
  422. | SSL_EXT_CLIENT_HELLO
  423. | SSL_EXT_TLS1_2_SERVER_HELLO
  424. | SSL_EXT_IGNORE_ON_RESUMPTION,
  425. add_cb, free_cb, add_arg, parse_cb, parse_arg);
  426. }
  427. int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
  428. unsigned int context,
  429. SSL_custom_ext_add_cb_ex add_cb,
  430. SSL_custom_ext_free_cb_ex free_cb,
  431. void *add_arg,
  432. SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
  433. {
  434. return ossl_tls_add_custom_ext_intern(ctx, NULL, ENDPOINT_BOTH, ext_type,
  435. context, add_cb, free_cb, add_arg,
  436. parse_cb, parse_arg);
  437. }
  438. int SSL_extension_supported(unsigned int ext_type)
  439. {
  440. switch (ext_type) {
  441. /* Internally supported extensions. */
  442. case TLSEXT_TYPE_application_layer_protocol_negotiation:
  443. case TLSEXT_TYPE_ec_point_formats:
  444. case TLSEXT_TYPE_supported_groups:
  445. case TLSEXT_TYPE_key_share:
  446. #ifndef OPENSSL_NO_NEXTPROTONEG
  447. case TLSEXT_TYPE_next_proto_neg:
  448. #endif
  449. case TLSEXT_TYPE_padding:
  450. case TLSEXT_TYPE_renegotiate:
  451. case TLSEXT_TYPE_max_fragment_length:
  452. case TLSEXT_TYPE_server_name:
  453. case TLSEXT_TYPE_session_ticket:
  454. case TLSEXT_TYPE_signature_algorithms:
  455. #ifndef OPENSSL_NO_SRP
  456. case TLSEXT_TYPE_srp:
  457. #endif
  458. #ifndef OPENSSL_NO_OCSP
  459. case TLSEXT_TYPE_status_request:
  460. #endif
  461. #ifndef OPENSSL_NO_CT
  462. case TLSEXT_TYPE_signed_certificate_timestamp:
  463. #endif
  464. #ifndef OPENSSL_NO_SRTP
  465. case TLSEXT_TYPE_use_srtp:
  466. #endif
  467. case TLSEXT_TYPE_encrypt_then_mac:
  468. case TLSEXT_TYPE_supported_versions:
  469. case TLSEXT_TYPE_extended_master_secret:
  470. case TLSEXT_TYPE_psk_kex_modes:
  471. case TLSEXT_TYPE_cookie:
  472. case TLSEXT_TYPE_early_data:
  473. case TLSEXT_TYPE_certificate_authorities:
  474. case TLSEXT_TYPE_psk:
  475. case TLSEXT_TYPE_post_handshake_auth:
  476. case TLSEXT_TYPE_compress_certificate:
  477. case TLSEXT_TYPE_client_cert_type:
  478. case TLSEXT_TYPE_server_cert_type:
  479. return 1;
  480. default:
  481. return 0;
  482. }
  483. }