Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

266 linhas
8.7 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. namespace bssl {
  23. void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) {
  24. OPENSSL_free(custom_extension);
  25. }
  26. static const SSL_CUSTOM_EXTENSION *custom_ext_find(
  27. STACK_OF(SSL_CUSTOM_EXTENSION) *stack,
  28. unsigned *out_index, uint16_t value) {
  29. for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
  30. const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
  31. if (ext->value == value) {
  32. if (out_index != NULL) {
  33. *out_index = i;
  34. }
  35. return ext;
  36. }
  37. }
  38. return NULL;
  39. }
  40. // default_add_callback is used as the |add_callback| when the user doesn't
  41. // provide one. For servers, it does nothing while, for clients, it causes an
  42. // empty extension to be included.
  43. static int default_add_callback(SSL *ssl, unsigned extension_value,
  44. const uint8_t **out, size_t *out_len,
  45. int *out_alert_value, void *add_arg) {
  46. if (ssl->server) {
  47. return 0;
  48. }
  49. *out_len = 0;
  50. return 1;
  51. }
  52. static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) {
  53. SSL *const ssl = hs->ssl;
  54. STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions;
  55. if (ssl->server) {
  56. stack = ssl->ctx->server_custom_extensions;
  57. }
  58. if (stack == NULL) {
  59. return 1;
  60. }
  61. for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
  62. const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
  63. if (ssl->server &&
  64. !(hs->custom_extensions.received & (1u << i))) {
  65. // Servers cannot echo extensions that the client didn't send.
  66. continue;
  67. }
  68. const uint8_t *contents;
  69. size_t contents_len;
  70. int alert = SSL_AD_DECODE_ERROR;
  71. CBB contents_cbb;
  72. switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert,
  73. ext->add_arg)) {
  74. case 1:
  75. if (!CBB_add_u16(extensions, ext->value) ||
  76. !CBB_add_u16_length_prefixed(extensions, &contents_cbb) ||
  77. !CBB_add_bytes(&contents_cbb, contents, contents_len) ||
  78. !CBB_flush(extensions)) {
  79. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  80. ERR_add_error_dataf("extension %u", (unsigned) ext->value);
  81. if (ext->free_callback && 0 < contents_len) {
  82. ext->free_callback(ssl, ext->value, contents, ext->add_arg);
  83. }
  84. return 0;
  85. }
  86. if (ext->free_callback && 0 < contents_len) {
  87. ext->free_callback(ssl, ext->value, contents, ext->add_arg);
  88. }
  89. if (!ssl->server) {
  90. assert((hs->custom_extensions.sent & (1u << i)) == 0);
  91. hs->custom_extensions.sent |= (1u << i);
  92. }
  93. break;
  94. case 0:
  95. break;
  96. default:
  97. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  98. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  99. ERR_add_error_dataf("extension %u", (unsigned) ext->value);
  100. return 0;
  101. }
  102. }
  103. return 1;
  104. }
  105. int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) {
  106. return custom_ext_add_hello(hs, extensions);
  107. }
  108. int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
  109. uint16_t value, const CBS *extension) {
  110. SSL *const ssl = hs->ssl;
  111. unsigned index;
  112. const SSL_CUSTOM_EXTENSION *ext =
  113. custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
  114. if (// Unknown extensions are not allowed in a ServerHello.
  115. ext == NULL ||
  116. // Also, if we didn't send the extension, that's also unacceptable.
  117. !(hs->custom_extensions.sent & (1u << index))) {
  118. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  119. ERR_add_error_dataf("extension %u", (unsigned)value);
  120. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  121. return 0;
  122. }
  123. if (ext->parse_callback != NULL &&
  124. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  125. out_alert, ext->parse_arg)) {
  126. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  127. ERR_add_error_dataf("extension %u", (unsigned)ext->value);
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
  133. uint16_t value, const CBS *extension) {
  134. SSL *const ssl = hs->ssl;
  135. unsigned index;
  136. const SSL_CUSTOM_EXTENSION *ext =
  137. custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
  138. if (ext == NULL) {
  139. return 1;
  140. }
  141. assert((hs->custom_extensions.received & (1u << index)) == 0);
  142. hs->custom_extensions.received |= (1u << index);
  143. if (ext->parse_callback &&
  144. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  145. out_alert, ext->parse_arg)) {
  146. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  147. ERR_add_error_dataf("extension %u", (unsigned)ext->value);
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) {
  153. return custom_ext_add_hello(hs, extensions);
  154. }
  155. // MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
  156. // can be set on an |SSL_CTX|. It's determined by the size of the bitset used
  157. // to track when an extension has been sent.
  158. #define MAX_NUM_CUSTOM_EXTENSIONS \
  159. (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8)
  160. static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
  161. unsigned extension_value,
  162. SSL_custom_ext_add_cb add_cb,
  163. SSL_custom_ext_free_cb free_cb, void *add_arg,
  164. SSL_custom_ext_parse_cb parse_cb,
  165. void *parse_arg) {
  166. if (add_cb == NULL ||
  167. 0xffff < extension_value ||
  168. SSL_extension_supported(extension_value) ||
  169. // Specifying a free callback without an add callback is nonsensical
  170. // and an error.
  171. (*stack != NULL &&
  172. (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
  173. custom_ext_find(*stack, NULL, extension_value) != NULL))) {
  174. return 0;
  175. }
  176. SSL_CUSTOM_EXTENSION *ext =
  177. (SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
  178. if (ext == NULL) {
  179. return 0;
  180. }
  181. ext->add_callback = add_cb;
  182. ext->add_arg = add_arg;
  183. ext->free_callback = free_cb;
  184. ext->parse_callback = parse_cb;
  185. ext->parse_arg = parse_arg;
  186. ext->value = extension_value;
  187. if (*stack == NULL) {
  188. *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
  189. if (*stack == NULL) {
  190. SSL_CUSTOM_EXTENSION_free(ext);
  191. return 0;
  192. }
  193. }
  194. if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
  195. SSL_CUSTOM_EXTENSION_free(ext);
  196. if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
  197. sk_SSL_CUSTOM_EXTENSION_free(*stack);
  198. *stack = NULL;
  199. }
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. } // namespace bssl
  205. using namespace bssl;
  206. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
  207. SSL_custom_ext_add_cb add_cb,
  208. SSL_custom_ext_free_cb free_cb, void *add_arg,
  209. SSL_custom_ext_parse_cb parse_cb,
  210. void *parse_arg) {
  211. return custom_ext_append(&ctx->client_custom_extensions, extension_value,
  212. add_cb ? add_cb : default_add_callback, free_cb,
  213. add_arg, parse_cb, parse_arg);
  214. }
  215. int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value,
  216. SSL_custom_ext_add_cb add_cb,
  217. SSL_custom_ext_free_cb free_cb, void *add_arg,
  218. SSL_custom_ext_parse_cb parse_cb,
  219. void *parse_arg) {
  220. return custom_ext_append(&ctx->server_custom_extensions, extension_value,
  221. add_cb ? add_cb : default_add_callback, free_cb,
  222. add_arg, parse_cb, parse_arg);
  223. }