blob: d86bd4841f33a6f0d453d3804d2857df76a5ad5c [file] [log] [blame]
Adam Langley09505632015-07-30 18:10:13 -07001/* 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
David Benjamin9e4e01e2015-09-15 01:48:04 -040015#include <openssl/ssl.h>
16
Adam Langley09505632015-07-30 18:10:13 -070017#include <assert.h>
18#include <string.h>
19
David Benjamin9e4e01e2015-09-15 01:48:04 -040020#include <openssl/bytestring.h>
21#include <openssl/err.h>
22#include <openssl/mem.h>
23#include <openssl/stack.h>
Adam Langley09505632015-07-30 18:10:13 -070024
25#include "internal.h"
26
27
David Benjamin86e95b82017-07-18 16:34:25 -040028namespace bssl {
29
Adam Langley09505632015-07-30 18:10:13 -070030void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) {
31 OPENSSL_free(custom_extension);
32}
33
34static const SSL_CUSTOM_EXTENSION *custom_ext_find(
35 STACK_OF(SSL_CUSTOM_EXTENSION) *stack,
36 unsigned *out_index, uint16_t value) {
David Benjamin54091232016-09-05 12:47:25 -040037 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
Adam Langley09505632015-07-30 18:10:13 -070038 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
39 if (ext->value == value) {
40 if (out_index != NULL) {
41 *out_index = i;
42 }
43 return ext;
44 }
45 }
46
47 return NULL;
48}
49
50/* default_add_callback is used as the |add_callback| when the user doesn't
51 * provide one. For servers, it does nothing while, for clients, it causes an
52 * empty extension to be included. */
53static int default_add_callback(SSL *ssl, unsigned extension_value,
54 const uint8_t **out, size_t *out_len,
55 int *out_alert_value, void *add_arg) {
56 if (ssl->server) {
57 return 0;
58 }
59 *out_len = 0;
60 return 1;
61}
62
David Benjamin2bd19172016-11-17 16:47:15 +090063static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) {
64 SSL *const ssl = hs->ssl;
Adam Langley09505632015-07-30 18:10:13 -070065 STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions;
66 if (ssl->server) {
67 stack = ssl->ctx->server_custom_extensions;
68 }
69
70 if (stack == NULL) {
71 return 1;
72 }
73
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -050074 if (ssl->cert->enable_early_data) {
Steven Valdez2a070722017-03-25 20:54:16 -050075 /* TODO(svaldez): Support Custom Extensions with 0-RTT. For now the caller
76 * is expected not to configure both together.
77 * https://crbug.com/boringssl/173. */
78 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
79 return 0;
80 }
81
David Benjamin54091232016-09-05 12:47:25 -040082 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
Adam Langley09505632015-07-30 18:10:13 -070083 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
84
85 if (ssl->server &&
David Benjamin2bd19172016-11-17 16:47:15 +090086 !(hs->custom_extensions.received & (1u << i))) {
Adam Langley09505632015-07-30 18:10:13 -070087 /* Servers cannot echo extensions that the client didn't send. */
88 continue;
89 }
90
91 const uint8_t *contents;
92 size_t contents_len;
93 int alert = SSL_AD_DECODE_ERROR;
94 CBB contents_cbb;
95
96 switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert,
97 ext->add_arg)) {
98 case 1:
99 if (!CBB_add_u16(extensions, ext->value) ||
100 !CBB_add_u16_length_prefixed(extensions, &contents_cbb) ||
101 !CBB_add_bytes(&contents_cbb, contents, contents_len) ||
102 !CBB_flush(extensions)) {
103 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800104 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700105 if (ext->free_callback && 0 < contents_len) {
106 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
107 }
108 return 0;
109 }
110
111 if (ext->free_callback && 0 < contents_len) {
112 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
113 }
114
115 if (!ssl->server) {
David Benjamin2bd19172016-11-17 16:47:15 +0900116 assert((hs->custom_extensions.sent & (1u << i)) == 0);
117 hs->custom_extensions.sent |= (1u << i);
Adam Langley09505632015-07-30 18:10:13 -0700118 }
119 break;
120
121 case 0:
122 break;
123
124 default:
125 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
126 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800127 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700128 return 0;
129 }
130 }
131
132 return 1;
133}
134
David Benjamin2bd19172016-11-17 16:47:15 +0900135int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) {
136 return custom_ext_add_hello(hs, extensions);
Adam Langley09505632015-07-30 18:10:13 -0700137}
138
David Benjamin2bd19172016-11-17 16:47:15 +0900139int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
140 uint16_t value, const CBS *extension) {
141 SSL *const ssl = hs->ssl;
Adam Langley09505632015-07-30 18:10:13 -0700142 unsigned index;
143 const SSL_CUSTOM_EXTENSION *ext =
144 custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
145
146 if (/* Unknown extensions are not allowed in a ServerHello. */
147 ext == NULL ||
148 /* Also, if we didn't send the extension, that's also unacceptable. */
David Benjamin2bd19172016-11-17 16:47:15 +0900149 !(hs->custom_extensions.sent & (1u << index))) {
Adam Langley09505632015-07-30 18:10:13 -0700150 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -0800151 ERR_add_error_dataf("extension %u", (unsigned)value);
David Benjamin0c40a962016-08-01 12:05:50 -0400152 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley09505632015-07-30 18:10:13 -0700153 return 0;
154 }
155
156 if (ext->parse_callback != NULL &&
157 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
158 out_alert, ext->parse_arg)) {
159 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800160 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700161 return 0;
162 }
163
164 return 1;
165}
166
David Benjamin2bd19172016-11-17 16:47:15 +0900167int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
168 uint16_t value, const CBS *extension) {
169 SSL *const ssl = hs->ssl;
Adam Langley09505632015-07-30 18:10:13 -0700170 unsigned index;
171 const SSL_CUSTOM_EXTENSION *ext =
172 custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
173
174 if (ext == NULL) {
175 return 1;
176 }
177
David Benjamin2bd19172016-11-17 16:47:15 +0900178 assert((hs->custom_extensions.received & (1u << index)) == 0);
179 hs->custom_extensions.received |= (1u << index);
Adam Langley09505632015-07-30 18:10:13 -0700180
181 if (ext->parse_callback &&
182 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
183 out_alert, ext->parse_arg)) {
184 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800185 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700186 return 0;
187 }
188
189 return 1;
190}
191
David Benjamin2bd19172016-11-17 16:47:15 +0900192int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) {
193 return custom_ext_add_hello(hs, extensions);
Adam Langley09505632015-07-30 18:10:13 -0700194}
195
196/* MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
197 * can be set on an |SSL_CTX|. It's determined by the size of the bitset used
198 * to track when an extension has been sent. */
199#define MAX_NUM_CUSTOM_EXTENSIONS \
David Benjaminf5d2cd02016-10-06 19:39:20 -0400200 (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8)
Adam Langley09505632015-07-30 18:10:13 -0700201
202static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
203 unsigned extension_value,
204 SSL_custom_ext_add_cb add_cb,
205 SSL_custom_ext_free_cb free_cb, void *add_arg,
206 SSL_custom_ext_parse_cb parse_cb,
207 void *parse_arg) {
208 if (add_cb == NULL ||
209 0xffff < extension_value ||
210 SSL_extension_supported(extension_value) ||
211 /* Specifying a free callback without an add callback is nonsensical
212 * and an error. */
213 (*stack != NULL &&
214 (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
215 custom_ext_find(*stack, NULL, extension_value) != NULL))) {
216 return 0;
217 }
218
David Benjamine8703a32017-07-09 16:17:55 -0400219 SSL_CUSTOM_EXTENSION *ext =
220 (SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
Adam Langley09505632015-07-30 18:10:13 -0700221 if (ext == NULL) {
222 return 0;
223 }
224 ext->add_callback = add_cb;
225 ext->add_arg = add_arg;
226 ext->free_callback = free_cb;
227 ext->parse_callback = parse_cb;
228 ext->parse_arg = parse_arg;
229 ext->value = extension_value;
230
231 if (*stack == NULL) {
232 *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
233 if (*stack == NULL) {
234 SSL_CUSTOM_EXTENSION_free(ext);
235 return 0;
236 }
237 }
238
239 if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
240 SSL_CUSTOM_EXTENSION_free(ext);
241 if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
242 sk_SSL_CUSTOM_EXTENSION_free(*stack);
243 *stack = NULL;
244 }
245 return 0;
246 }
247
248 return 1;
249}
250
David Benjamin86e95b82017-07-18 16:34:25 -0400251} // namespace bssl
252
253using namespace bssl;
254
Adam Langley09505632015-07-30 18:10:13 -0700255int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
256 SSL_custom_ext_add_cb add_cb,
257 SSL_custom_ext_free_cb free_cb, void *add_arg,
258 SSL_custom_ext_parse_cb parse_cb,
259 void *parse_arg) {
260 return custom_ext_append(&ctx->client_custom_extensions, extension_value,
261 add_cb ? add_cb : default_add_callback, free_cb,
262 add_arg, parse_cb, parse_arg);
263}
264
265int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value,
266 SSL_custom_ext_add_cb add_cb,
267 SSL_custom_ext_free_cb free_cb, void *add_arg,
268 SSL_custom_ext_parse_cb parse_cb,
269 void *parse_arg) {
270 return custom_ext_append(&ctx->server_custom_extensions, extension_value,
271 add_cb ? add_cb : default_add_callback, free_cb,
272 add_arg, parse_cb, parse_arg);
273}