| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/bytestring.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/stack.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
|
| 17 |
BSSL_NAMESPACE_BEGIN |
| 18 |
|
| 19 |
void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) { |
| 20 |
OPENSSL_free(custom_extension); |
| 21 |
} |
| 22 |
|
| 23 |
static const SSL_CUSTOM_EXTENSION *custom_ext_find( |
| 24 |
STACK_OF(SSL_CUSTOM_EXTENSION) *stack, |
| 25 |
unsigned *out_index, uint16_t value) { |
| 26 |
for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) { |
| 27 |
const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i); |
| 28 |
if (ext->value == value) { |
| 29 |
if (out_index != NULL) { |
| 30 |
*out_index = i; |
| 31 |
} |
| 32 |
return ext; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return NULL; |
| 37 |
} |
| 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 |
|
| 52 |
static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 53 |
SSL *const ssl = hs->ssl; |
| 54 |
STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions; |
| 55 |
if (ssl->server) { |
| 56 |
stack = ssl->ctx->server_custom_extensions; |
| 57 |
} |
| 58 |
|
| 59 |
if (stack == NULL) { |
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) { |
| 64 |
const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i); |
| 65 |
|
| 66 |
if (ssl->server && |
| 67 |
!(hs->custom_extensions.received & (1u << i))) { |
| 68 |
// Servers cannot echo extensions that the client didn't send. |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
const uint8_t *contents; |
| 73 |
size_t contents_len; |
| 74 |
int alert = SSL_AD_DECODE_ERROR; |
| 75 |
CBB contents_cbb; |
| 76 |
|
| 77 |
switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert, |
| 78 |
ext->add_arg)) { |
| 79 |
case 1: |
| 80 |
if (!CBB_add_u16(extensions, ext->value) || |
| 81 |
!CBB_add_u16_length_prefixed(extensions, &contents_cbb) || |
| 82 |
!CBB_add_bytes(&contents_cbb, contents, contents_len) || |
| 83 |
!CBB_flush(extensions)) { |
| 84 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 85 |
ERR_add_error_dataf("extension %u", (unsigned) ext->value); |
| 86 |
if (ext->free_callback && 0 < contents_len) { |
| 87 |
ext->free_callback(ssl, ext->value, contents, ext->add_arg); |
| 88 |
} |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
if (ext->free_callback && 0 < contents_len) { |
| 93 |
ext->free_callback(ssl, ext->value, contents, ext->add_arg); |
| 94 |
} |
| 95 |
|
| 96 |
if (!ssl->server) { |
| 97 |
assert((hs->custom_extensions.sent & (1u << i)) == 0); |
| 98 |
hs->custom_extensions.sent |= (1u << i); |
| 99 |
} |
| 100 |
break; |
| 101 |
|
| 102 |
case 0: |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 107 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
| 108 |
ERR_add_error_dataf("extension %u", (unsigned) ext->value); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 117 |
return custom_ext_add_hello(hs, extensions); |
| 118 |
} |
| 119 |
|
| 120 |
int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert, |
| 121 |
uint16_t value, const CBS *extension) { |
| 122 |
SSL *const ssl = hs->ssl; |
| 123 |
unsigned index; |
| 124 |
const SSL_CUSTOM_EXTENSION *ext = |
| 125 |
custom_ext_find(ssl->ctx->client_custom_extensions, &index, value); |
| 126 |
|
| 127 |
if (// Unknown extensions are not allowed in a ServerHello. |
| 128 |
ext == NULL || |
| 129 |
// Also, if we didn't send the extension, that's also unacceptable. |
| 130 |
!(hs->custom_extensions.sent & (1u << index))) { |
| 131 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 132 |
ERR_add_error_dataf("extension %u", (unsigned)value); |
| 133 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
if (ext->parse_callback != NULL && |
| 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 |
|
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert, |
| 149 |
uint16_t value, const CBS *extension) { |
| 150 |
SSL *const ssl = hs->ssl; |
| 151 |
unsigned index; |
| 152 |
const SSL_CUSTOM_EXTENSION *ext = |
| 153 |
custom_ext_find(ssl->ctx->server_custom_extensions, &index, value); |
| 154 |
|
| 155 |
if (ext == NULL) { |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
assert((hs->custom_extensions.received & (1u << index)) == 0); |
| 160 |
hs->custom_extensions.received |= (1u << index); |
| 161 |
|
| 162 |
if (ext->parse_callback && |
| 163 |
!ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension), |
| 164 |
out_alert, ext->parse_arg)) { |
| 165 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
| 166 |
ERR_add_error_dataf("extension %u", (unsigned)ext->value); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
return 1; |
| 171 |
} |
| 172 |
|
| 173 |
int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 174 |
return custom_ext_add_hello(hs, extensions); |
| 175 |
} |
| 176 |
|
| 177 |
// MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that |
| 178 |
// can be set on an |SSL_CTX|. It's determined by the size of the bitset used |
| 179 |
// to track when an extension has been sent. |
| 180 |
#define MAX_NUM_CUSTOM_EXTENSIONS \ |
| 181 |
(sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8) |
| 182 |
|
| 183 |
static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack, |
| 184 |
unsigned extension_value, |
| 185 |
SSL_custom_ext_add_cb add_cb, |
| 186 |
SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 187 |
SSL_custom_ext_parse_cb parse_cb, |
| 188 |
void *parse_arg) { |
| 189 |
if (add_cb == NULL || |
| 190 |
0xffff < extension_value || |
| 191 |
SSL_extension_supported(extension_value) || |
| 192 |
// Specifying a free callback without an add callback is nonsensical |
| 193 |
// and an error. |
| 194 |
(*stack != NULL && |
| 195 |
(MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) || |
| 196 |
custom_ext_find(*stack, NULL, extension_value) != NULL))) { |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
SSL_CUSTOM_EXTENSION *ext = |
| 201 |
(SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION)); |
| 202 |
if (ext == NULL) { |
| 203 |
return 0; |
| 204 |
} |
| 205 |
ext->add_callback = add_cb; |
| 206 |
ext->add_arg = add_arg; |
| 207 |
ext->free_callback = free_cb; |
| 208 |
ext->parse_callback = parse_cb; |
| 209 |
ext->parse_arg = parse_arg; |
| 210 |
ext->value = extension_value; |
| 211 |
|
| 212 |
if (*stack == NULL) { |
| 213 |
*stack = sk_SSL_CUSTOM_EXTENSION_new_null(); |
| 214 |
if (*stack == NULL) { |
| 215 |
SSL_CUSTOM_EXTENSION_free(ext); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) { |
| 221 |
SSL_CUSTOM_EXTENSION_free(ext); |
| 222 |
if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) { |
| 223 |
sk_SSL_CUSTOM_EXTENSION_free(*stack); |
| 224 |
*stack = NULL; |
| 225 |
} |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
return 1; |
| 230 |
} |
| 231 |
|
| 232 |
BSSL_NAMESPACE_END |
| 233 |
|
| 234 |
using namespace bssl; |
| 235 |
|
| 236 |
int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value, |
| 237 |
SSL_custom_ext_add_cb add_cb, |
| 238 |
SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 239 |
SSL_custom_ext_parse_cb parse_cb, |
| 240 |
void *parse_arg) { |
| 241 |
return custom_ext_append(&ctx->client_custom_extensions, extension_value, |
| 242 |
add_cb ? add_cb : default_add_callback, free_cb, |
| 243 |
add_arg, parse_cb, parse_arg); |
| 244 |
} |
| 245 |
|
| 246 |
int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value, |
| 247 |
SSL_custom_ext_add_cb add_cb, |
| 248 |
SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 249 |
SSL_custom_ext_parse_cb parse_cb, |
| 250 |
void *parse_arg) { |
| 251 |
return custom_ext_append(&ctx->server_custom_extensions, extension_value, |
| 252 |
add_cb ? add_cb : default_add_callback, free_cb, |
| 253 |
add_arg, parse_cb, parse_arg); |
| 254 |
} |