extensions_cust.c 17 KB

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