No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

258 líneas
8.6 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. size_t i;
  29. for (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 *ssl, CBB *extensions) {
  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. size_t i;
  61. for (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. !(ssl->s3->tmp.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((ssl->s3->tmp.custom_extensions.sent & (1u << i)) == 0);
  91. ssl->s3->tmp.custom_extensions.sent |= (1u << i);
  92. }
  93. break;
  94. case 0:
  95. break;
  96. default:
  97. ssl3_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 *ssl, CBB *extensions) {
  106. return custom_ext_add_hello(ssl, extensions);
  107. }
  108. int custom_ext_parse_serverhello(SSL *ssl, int *out_alert, uint16_t value,
  109. const CBS *extension) {
  110. unsigned index;
  111. const SSL_CUSTOM_EXTENSION *ext =
  112. custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
  113. if (/* Unknown extensions are not allowed in a ServerHello. */
  114. ext == NULL ||
  115. /* Also, if we didn't send the extension, that's also unacceptable. */
  116. !(ssl->s3->tmp.custom_extensions.sent & (1u << index))) {
  117. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  118. ERR_add_error_dataf("extension: %u", (unsigned)value);
  119. *out_alert = SSL_AD_DECODE_ERROR;
  120. return 0;
  121. }
  122. if (ext->parse_callback != NULL &&
  123. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  124. out_alert, ext->parse_arg)) {
  125. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  126. ERR_add_error_dataf("extension: %u", (unsigned)ext->value);
  127. return 0;
  128. }
  129. return 1;
  130. }
  131. int custom_ext_parse_clienthello(SSL *ssl, int *out_alert, uint16_t value,
  132. const CBS *extension) {
  133. unsigned index;
  134. const SSL_CUSTOM_EXTENSION *ext =
  135. custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
  136. if (ext == NULL) {
  137. return 1;
  138. }
  139. assert((ssl->s3->tmp.custom_extensions.received & (1u << index)) == 0);
  140. ssl->s3->tmp.custom_extensions.received |= (1u << index);
  141. if (ext->parse_callback &&
  142. !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
  143. out_alert, ext->parse_arg)) {
  144. OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
  145. ERR_add_error_dataf("extension: %u", (unsigned)ext->value);
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. int custom_ext_add_serverhello(SSL *ssl, CBB *extensions) {
  151. return custom_ext_add_hello(ssl, extensions);
  152. }
  153. /* MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
  154. * can be set on an |SSL_CTX|. It's determined by the size of the bitset used
  155. * to track when an extension has been sent. */
  156. #define MAX_NUM_CUSTOM_EXTENSIONS \
  157. (sizeof(((struct ssl3_state_st *)NULL)->tmp.custom_extensions.sent) * 8)
  158. static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
  159. unsigned extension_value,
  160. SSL_custom_ext_add_cb add_cb,
  161. SSL_custom_ext_free_cb free_cb, void *add_arg,
  162. SSL_custom_ext_parse_cb parse_cb,
  163. void *parse_arg) {
  164. if (add_cb == NULL ||
  165. 0xffff < extension_value ||
  166. SSL_extension_supported(extension_value) ||
  167. /* Specifying a free callback without an add callback is nonsensical
  168. * and an error. */
  169. (*stack != NULL &&
  170. (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
  171. custom_ext_find(*stack, NULL, extension_value) != NULL))) {
  172. return 0;
  173. }
  174. SSL_CUSTOM_EXTENSION *ext = OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
  175. if (ext == NULL) {
  176. return 0;
  177. }
  178. ext->add_callback = add_cb;
  179. ext->add_arg = add_arg;
  180. ext->free_callback = free_cb;
  181. ext->parse_callback = parse_cb;
  182. ext->parse_arg = parse_arg;
  183. ext->value = extension_value;
  184. if (*stack == NULL) {
  185. *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
  186. if (*stack == NULL) {
  187. SSL_CUSTOM_EXTENSION_free(ext);
  188. return 0;
  189. }
  190. }
  191. if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
  192. SSL_CUSTOM_EXTENSION_free(ext);
  193. if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
  194. sk_SSL_CUSTOM_EXTENSION_free(*stack);
  195. *stack = NULL;
  196. }
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
  202. SSL_custom_ext_add_cb add_cb,
  203. SSL_custom_ext_free_cb free_cb, void *add_arg,
  204. SSL_custom_ext_parse_cb parse_cb,
  205. void *parse_arg) {
  206. return custom_ext_append(&ctx->client_custom_extensions, extension_value,
  207. add_cb ? add_cb : default_add_callback, free_cb,
  208. add_arg, parse_cb, parse_arg);
  209. }
  210. int SSL_CTX_add_server_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->server_custom_extensions, extension_value,
  216. add_cb ? add_cb : default_add_callback, free_cb,
  217. add_arg, parse_cb, parse_arg);
  218. }