blob: 46b5efb49dcf2d7c267f1faa3651a0c3a37e1173 [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
28void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) {
29 OPENSSL_free(custom_extension);
30}
31
32static const SSL_CUSTOM_EXTENSION *custom_ext_find(
33 STACK_OF(SSL_CUSTOM_EXTENSION) *stack,
34 unsigned *out_index, uint16_t value) {
David Benjamin54091232016-09-05 12:47:25 -040035 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
Adam Langley09505632015-07-30 18:10:13 -070036 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
37 if (ext->value == value) {
38 if (out_index != NULL) {
39 *out_index = i;
40 }
41 return ext;
42 }
43 }
44
45 return NULL;
46}
47
48/* default_add_callback is used as the |add_callback| when the user doesn't
49 * provide one. For servers, it does nothing while, for clients, it causes an
50 * empty extension to be included. */
51static int default_add_callback(SSL *ssl, unsigned extension_value,
52 const uint8_t **out, size_t *out_len,
53 int *out_alert_value, void *add_arg) {
54 if (ssl->server) {
55 return 0;
56 }
57 *out_len = 0;
58 return 1;
59}
60
61static int custom_ext_add_hello(SSL *ssl, CBB *extensions) {
62 STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions;
63 if (ssl->server) {
64 stack = ssl->ctx->server_custom_extensions;
65 }
66
67 if (stack == NULL) {
68 return 1;
69 }
70
David Benjamin54091232016-09-05 12:47:25 -040071 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
Adam Langley09505632015-07-30 18:10:13 -070072 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
73
74 if (ssl->server &&
David Benjaminf5d2cd02016-10-06 19:39:20 -040075 !(ssl->s3->hs->custom_extensions.received & (1u << i))) {
Adam Langley09505632015-07-30 18:10:13 -070076 /* Servers cannot echo extensions that the client didn't send. */
77 continue;
78 }
79
80 const uint8_t *contents;
81 size_t contents_len;
82 int alert = SSL_AD_DECODE_ERROR;
83 CBB contents_cbb;
84
85 switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert,
86 ext->add_arg)) {
87 case 1:
88 if (!CBB_add_u16(extensions, ext->value) ||
89 !CBB_add_u16_length_prefixed(extensions, &contents_cbb) ||
90 !CBB_add_bytes(&contents_cbb, contents, contents_len) ||
91 !CBB_flush(extensions)) {
92 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -080093 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
Adam Langley09505632015-07-30 18:10:13 -070094 if (ext->free_callback && 0 < contents_len) {
95 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
96 }
97 return 0;
98 }
99
100 if (ext->free_callback && 0 < contents_len) {
101 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
102 }
103
104 if (!ssl->server) {
David Benjaminf5d2cd02016-10-06 19:39:20 -0400105 assert((ssl->s3->hs->custom_extensions.sent & (1u << i)) == 0);
106 ssl->s3->hs->custom_extensions.sent |= (1u << i);
Adam Langley09505632015-07-30 18:10:13 -0700107 }
108 break;
109
110 case 0:
111 break;
112
113 default:
114 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
115 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800116 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700117 return 0;
118 }
119 }
120
121 return 1;
122}
123
124int custom_ext_add_clienthello(SSL *ssl, CBB *extensions) {
125 return custom_ext_add_hello(ssl, extensions);
126}
127
128int custom_ext_parse_serverhello(SSL *ssl, int *out_alert, uint16_t value,
129 const CBS *extension) {
130 unsigned index;
131 const SSL_CUSTOM_EXTENSION *ext =
132 custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
133
134 if (/* Unknown extensions are not allowed in a ServerHello. */
135 ext == NULL ||
136 /* Also, if we didn't send the extension, that's also unacceptable. */
David Benjaminf5d2cd02016-10-06 19:39:20 -0400137 !(ssl->s3->hs->custom_extensions.sent & (1u << index))) {
Adam Langley09505632015-07-30 18:10:13 -0700138 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Adam Langleyfbbef122016-11-17 12:55:14 -0800139 ERR_add_error_dataf("extension %u", (unsigned)value);
David Benjamin0c40a962016-08-01 12:05:50 -0400140 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley09505632015-07-30 18:10:13 -0700141 return 0;
142 }
143
144 if (ext->parse_callback != NULL &&
145 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
146 out_alert, ext->parse_arg)) {
147 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800148 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700149 return 0;
150 }
151
152 return 1;
153}
154
155int custom_ext_parse_clienthello(SSL *ssl, int *out_alert, uint16_t value,
156 const CBS *extension) {
157 unsigned index;
158 const SSL_CUSTOM_EXTENSION *ext =
159 custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
160
161 if (ext == NULL) {
162 return 1;
163 }
164
David Benjaminf5d2cd02016-10-06 19:39:20 -0400165 assert((ssl->s3->hs->custom_extensions.received & (1u << index)) == 0);
166 ssl->s3->hs->custom_extensions.received |= (1u << index);
Adam Langley09505632015-07-30 18:10:13 -0700167
168 if (ext->parse_callback &&
169 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
170 out_alert, ext->parse_arg)) {
171 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
Adam Langleyfbbef122016-11-17 12:55:14 -0800172 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
Adam Langley09505632015-07-30 18:10:13 -0700173 return 0;
174 }
175
176 return 1;
177}
178
179int custom_ext_add_serverhello(SSL *ssl, CBB *extensions) {
180 return custom_ext_add_hello(ssl, extensions);
181}
182
183/* MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
184 * can be set on an |SSL_CTX|. It's determined by the size of the bitset used
185 * to track when an extension has been sent. */
186#define MAX_NUM_CUSTOM_EXTENSIONS \
David Benjaminf5d2cd02016-10-06 19:39:20 -0400187 (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8)
Adam Langley09505632015-07-30 18:10:13 -0700188
189static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
190 unsigned extension_value,
191 SSL_custom_ext_add_cb add_cb,
192 SSL_custom_ext_free_cb free_cb, void *add_arg,
193 SSL_custom_ext_parse_cb parse_cb,
194 void *parse_arg) {
195 if (add_cb == NULL ||
196 0xffff < extension_value ||
197 SSL_extension_supported(extension_value) ||
198 /* Specifying a free callback without an add callback is nonsensical
199 * and an error. */
200 (*stack != NULL &&
201 (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
202 custom_ext_find(*stack, NULL, extension_value) != NULL))) {
203 return 0;
204 }
205
206 SSL_CUSTOM_EXTENSION *ext = OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
207 if (ext == NULL) {
208 return 0;
209 }
210 ext->add_callback = add_cb;
211 ext->add_arg = add_arg;
212 ext->free_callback = free_cb;
213 ext->parse_callback = parse_cb;
214 ext->parse_arg = parse_arg;
215 ext->value = extension_value;
216
217 if (*stack == NULL) {
218 *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
219 if (*stack == NULL) {
220 SSL_CUSTOM_EXTENSION_free(ext);
221 return 0;
222 }
223 }
224
225 if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
226 SSL_CUSTOM_EXTENSION_free(ext);
227 if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
228 sk_SSL_CUSTOM_EXTENSION_free(*stack);
229 *stack = NULL;
230 }
231 return 0;
232 }
233
234 return 1;
235}
236
237int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
238 SSL_custom_ext_add_cb add_cb,
239 SSL_custom_ext_free_cb free_cb, void *add_arg,
240 SSL_custom_ext_parse_cb parse_cb,
241 void *parse_arg) {
242 return custom_ext_append(&ctx->client_custom_extensions, extension_value,
243 add_cb ? add_cb : default_add_callback, free_cb,
244 add_arg, parse_cb, parse_arg);
245}
246
247int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value,
248 SSL_custom_ext_add_cb add_cb,
249 SSL_custom_ext_free_cb free_cb, void *add_arg,
250 SSL_custom_ext_parse_cb parse_cb,
251 void *parse_arg) {
252 return custom_ext_append(&ctx->server_custom_extensions, extension_value,
253 add_cb ? add_cb : default_add_callback, free_cb,
254 add_arg, parse_cb, parse_arg);
255}