You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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