Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

268 řádky
9.0 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/ssl.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <openssl/bytestring.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/stack.h>
  21. #include "internal.h"
  22. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) {
  23. OPENSSL_free(custom_extension);
  24. }
  25. static const SSL_CUSTOM_EXTENSION *custom_ext_find(
  26. STACK_OF(SSL_CUSTOM_EXTENSION) *stack,
  27. unsigned *out_index, uint16_t value) {
  28. for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
  29. const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
  30. if (ext->value == value) {
  31. if (out_index != NULL) {
  32. *out_index = i;
  33. }
  34. return ext;
  35. }
  36. }
  37. return NULL;
  38. }
  39. /* default_add_callback is used as the |add_callback| when the user doesn't
  40. * provide one. For servers, it does nothing while, for clients, it causes an
  41. * empty extension to be included. */
  42. static int default_add_callback(SSL *ssl, unsigned extension_value,
  43. const uint8_t **out, size_t *out_len,
  44. int *out_alert_value, void *add_arg) {
  45. if (ssl->server) {
  46. return 0;
  47. }
  48. *out_len = 0;
  49. return 1;
  50. }
  51. static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) {
  52. SSL *const ssl = hs->ssl;
  53. STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions;
  54. if (ssl->server) {
  55. stack = ssl->ctx->server_custom_extensions;
  56. }
  57. if (stack == NULL) {
  58. return 1;
  59. }
  60. if (ssl->cert->enable_early_data) {
  61. /* TODO(svaldez): Support Custom Extensions with 0-RTT. For now the caller
  62. * is expected not to configure both together.
  63. * https://crbug.com/boringssl/173. */
  64. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  65. return 0;
  66. }
  67. for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
  68. const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
  69. if (ssl->server &&
  70. !(hs->custom_extensions.received & (1u << i))) {
  71. /* Servers cannot echo extensions that the client didn't send. */
  72. continue;
  73. }
  74. const uint8_t *contents;
  75. size_t contents_len;
  76. int alert = SSL_AD_DECODE_ERROR;
  77. CBB contents_cbb;
  78. switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert,
  79. ext->add_arg)) {
  80. case 1:
  81. if (!CBB_add_u16(extensions, ext->value) ||
  82. !CBB_add_u16_length_prefixed(extensions, &contents_cbb) ||
  83. !CBB_add_bytes(&contents_cbb, contents, contents_len) ||
  84. !CBB_flush(extensions)) {
  85. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  86. ERR_add_error_dataf("extension %u", (unsigned) ext->value);
  87. if (ext->free_callback && 0 < contents_len) {
  88. ext->free_callback(ssl, ext->value, contents, ext->add_arg);
  89. }
  90. return 0;
  91. }
  92. if (ext->free_callback && 0 < contents_len) {
  93. ext->free_callback(ssl, ext->value, contents, ext->add_arg);
  94. }
  95. if (!ssl->server) {
  96. assert((hs->custom_extensions.sent & (1u << i)) == 0);
  97. hs->custom_extensions.sent |= (1u << i);
  98. }
  99. break;
  100. case 0:
  101. break;
  102. default:
  103. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  104. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  105. ERR_add_error_dataf("extension %u", (unsigned) ext->value);
  106. return 0;
  107. }
  108. }
  109. return 1;
  110. }
  111. int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) {
  112. return custom_ext_add_hello(hs, extensions);
  113. }
  114. int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
  115. uint16_t value, const CBS *extension) {
  116. SSL *const ssl = hs->ssl;
  117. unsigned index;
  118. const SSL_CUSTOM_EXTENSION *ext =
  119. custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
  120. if (/* Unknown extensions are not allowed in a ServerHello. */
  121. ext == NULL ||
  122. /* Also, if we didn't send the extension, that's also unacceptable. */
  123. !(hs->custom_extensions.sent & (1u << index))) {
  124. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  125. ERR_add_error_dataf("extension %u", (unsigned)value);
  126. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  127. return 0;
  128. }
  129. if (ext->parse_callback != NULL &&
  130. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  131. out_alert, ext->parse_arg)) {
  132. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  133. ERR_add_error_dataf("extension %u", (unsigned)ext->value);
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
  139. uint16_t value, const CBS *extension) {
  140. SSL *const ssl = hs->ssl;
  141. unsigned index;
  142. const SSL_CUSTOM_EXTENSION *ext =
  143. custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
  144. if (ext == NULL) {
  145. return 1;
  146. }
  147. assert((hs->custom_extensions.received & (1u << index)) == 0);
  148. hs->custom_extensions.received |= (1u << index);
  149. if (ext->parse_callback &&
  150. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  151. out_alert, ext->parse_arg)) {
  152. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  153. ERR_add_error_dataf("extension %u", (unsigned)ext->value);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) {
  159. return custom_ext_add_hello(hs, extensions);
  160. }
  161. /* MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
  162. * can be set on an |SSL_CTX|. It's determined by the size of the bitset used
  163. * to track when an extension has been sent. */
  164. #define MAX_NUM_CUSTOM_EXTENSIONS \
  165. (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8)
  166. static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
  167. unsigned extension_value,
  168. SSL_custom_ext_add_cb add_cb,
  169. SSL_custom_ext_free_cb free_cb, void *add_arg,
  170. SSL_custom_ext_parse_cb parse_cb,
  171. void *parse_arg) {
  172. if (add_cb == NULL ||
  173. 0xffff < extension_value ||
  174. SSL_extension_supported(extension_value) ||
  175. /* Specifying a free callback without an add callback is nonsensical
  176. * and an error. */
  177. (*stack != NULL &&
  178. (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
  179. custom_ext_find(*stack, NULL, extension_value) != NULL))) {
  180. return 0;
  181. }
  182. SSL_CUSTOM_EXTENSION *ext =
  183. (SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
  184. if (ext == NULL) {
  185. return 0;
  186. }
  187. ext->add_callback = add_cb;
  188. ext->add_arg = add_arg;
  189. ext->free_callback = free_cb;
  190. ext->parse_callback = parse_cb;
  191. ext->parse_arg = parse_arg;
  192. ext->value = extension_value;
  193. if (*stack == NULL) {
  194. *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
  195. if (*stack == NULL) {
  196. SSL_CUSTOM_EXTENSION_free(ext);
  197. return 0;
  198. }
  199. }
  200. if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
  201. SSL_CUSTOM_EXTENSION_free(ext);
  202. if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
  203. sk_SSL_CUSTOM_EXTENSION_free(*stack);
  204. *stack = NULL;
  205. }
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
  211. SSL_custom_ext_add_cb add_cb,
  212. SSL_custom_ext_free_cb free_cb, void *add_arg,
  213. SSL_custom_ext_parse_cb parse_cb,
  214. void *parse_arg) {
  215. return custom_ext_append(&ctx->client_custom_extensions, extension_value,
  216. add_cb ? add_cb : default_add_callback, free_cb,
  217. add_arg, parse_cb, parse_arg);
  218. }
  219. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value,
  220. SSL_custom_ext_add_cb add_cb,
  221. SSL_custom_ext_free_cb free_cb, void *add_arg,
  222. SSL_custom_ext_parse_cb parse_cb,
  223. void *parse_arg) {
  224. return custom_ext_append(&ctx->server_custom_extensions, extension_value,
  225. add_cb ? add_cb : default_add_callback, free_cb,
  226. add_arg, parse_cb, parse_arg);
  227. }