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.
 
 
 
 
 
 

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