blob: b19e75231faecc7f73a4acc1a787b01a21b19a31 [file] [log] [blame]
Rich Salz846e33c2016-05-17 14:18:30 -04001/*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +01003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +01008 */
9
10/* Custom extension utility functions */
11
Rich Salz3c272082016-03-18 14:30:20 -040012#include <openssl/ct.h>
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +010013#include "ssl_locl.h"
14
Dr. Stephen Henson0cfefe42014-08-19 14:02:50 +010015/* Find a custom extension from the list. */
Rob Percivaled29e822016-03-03 16:19:23 +000016static custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
Matt Caswell0f113f32015-01-22 03:40:55 +000017 unsigned int ext_type)
18{
19 size_t i;
20 custom_ext_method *meth = exts->meths;
21 for (i = 0; i < exts->meths_count; i++, meth++) {
22 if (ext_type == meth->ext_type)
23 return meth;
24 }
25 return NULL;
26}
27
28/*
29 * Initialise custom extensions flags to indicate neither sent nor received.
Dr. Stephen Henson28ea0a02014-08-12 14:25:49 +010030 */
31void custom_ext_init(custom_ext_methods *exts)
Matt Caswell0f113f32015-01-22 03:40:55 +000032{
33 size_t i;
34 custom_ext_method *meth = exts->meths;
35 for (i = 0; i < exts->meths_count; i++, meth++)
36 meth->ext_flags = 0;
37}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +010038
Dr. Stephen Henson0cfefe42014-08-19 14:02:50 +010039/* Pass received custom extension data to the application for parsing. */
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +010040int custom_ext_parse(SSL *s, int server,
Matt Caswell0f113f32015-01-22 03:40:55 +000041 unsigned int ext_type,
42 const unsigned char *ext_data, size_t ext_size, int *al)
43{
44 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
45 custom_ext_method *meth;
46 meth = custom_ext_find(exts, ext_type);
47 /* If not found return success */
48 if (!meth)
49 return 1;
50 if (!server) {
51 /*
52 * If it's ServerHello we can't have any extensions not sent in
53 * ClientHello.
54 */
55 if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
56 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
57 return 0;
58 }
59 }
60 /* If already present it's a duplicate */
61 if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
62 *al = TLS1_AD_DECODE_ERROR;
63 return 0;
64 }
65 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
66 /* If no parse function set return success */
67 if (!meth->parse_cb)
68 return 1;
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +010069
Emilia Kaspera230b262016-08-05 19:03:17 +020070 return meth->parse_cb(s, ext_type, ext_data, ext_size, al, meth->parse_arg);
Matt Caswell0f113f32015-01-22 03:40:55 +000071}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +010072
Matt Caswell0f113f32015-01-22 03:40:55 +000073/*
74 * Request custom extension data from the application and add to the return
Matt Caswell2c7b4db2016-08-03 20:57:52 +010075 * buffer.
76 */
Matt Caswellae2f7b32016-09-05 17:34:04 +010077int custom_ext_add(SSL *s, int server, WPACKET *pkt, int *al)
Matt Caswell2c7b4db2016-08-03 20:57:52 +010078{
79 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
80 custom_ext_method *meth;
81 size_t i;
82
83 for (i = 0; i < exts->meths_count; i++) {
84 const unsigned char *out = NULL;
85 size_t outlen = 0;
Matt Caswell2c7b4db2016-08-03 20:57:52 +010086
87 meth = exts->meths + i;
88
89 if (server) {
90 /*
91 * For ServerHello only send extensions present in ClientHello.
92 */
93 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
94 continue;
95 /* If callback absent for server skip it */
96 if (!meth->add_cb)
97 continue;
98 }
99 if (meth->add_cb) {
100 int cb_retval = 0;
101 cb_retval = meth->add_cb(s, meth->ext_type,
102 &out, &outlen, al, meth->add_arg);
103 if (cb_retval < 0)
104 return 0; /* error */
105 if (cb_retval == 0)
106 continue; /* skip this extension */
107 }
108
Matt Caswell08029df2016-09-20 14:47:44 +0100109 if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
Matt Caswellde451852016-09-09 00:13:41 +0100110 || !WPACKET_start_sub_packet_u16(pkt)
Matt Caswell0217dd12016-09-06 15:09:51 +0100111 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
112 || !WPACKET_close(pkt)) {
Matt Caswell2c7b4db2016-08-03 20:57:52 +0100113 *al = SSL_AD_INTERNAL_ERROR;
114 return 0;
115 }
116 /*
117 * We can't send duplicates: code logic should prevent this.
118 */
119 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
120 /*
121 * Indicate extension has been sent: this is both a sanity check to
122 * ensure we don't send duplicate extensions and indicates that it is
123 * not an error if the extension is present in ServerHello.
124 */
125 meth->ext_flags |= SSL_EXT_FLAG_SENT;
126 if (meth->free_cb)
127 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
128 }
129 return 1;
130}
131
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100132/* Copy table of custom extensions */
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100133int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
Matt Caswell0f113f32015-01-22 03:40:55 +0000134{
135 if (src->meths_count) {
136 dst->meths =
Rich Salz7644a9a2015-12-16 16:12:24 -0500137 OPENSSL_memdup(src->meths,
Emilia Kaspera230b262016-08-05 19:03:17 +0200138 sizeof(custom_ext_method) * src->meths_count);
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 if (dst->meths == NULL)
140 return 0;
141 dst->meths_count = src->meths_count;
142 }
143 return 1;
144}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100145
146void custom_exts_free(custom_ext_methods *exts)
Matt Caswell0f113f32015-01-22 03:40:55 +0000147{
Rich Salzb548a1f2015-05-01 10:02:07 -0400148 OPENSSL_free(exts->meths);
Matt Caswell0f113f32015-01-22 03:40:55 +0000149}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100150
Dr. Stephen Henson0cfefe42014-08-19 14:02:50 +0100151/* Set callbacks for a custom extension. */
Dr. Stephen Henson8cafe9e2014-08-19 13:54:38 +0100152static int custom_ext_meth_add(custom_ext_methods *exts,
Matt Caswell0f113f32015-01-22 03:40:55 +0000153 unsigned int ext_type,
154 custom_ext_add_cb add_cb,
155 custom_ext_free_cb free_cb,
156 void *add_arg,
157 custom_ext_parse_cb parse_cb, void *parse_arg)
158{
Dr. Stephen Henson7c0ef842016-05-11 21:14:57 +0100159 custom_ext_method *meth, *tmp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000160 /*
161 * Check application error: if add_cb is not set free_cb will never be
162 * called.
163 */
164 if (!add_cb && free_cb)
165 return 0;
Rob Percivaled29e822016-03-03 16:19:23 +0000166 /*
167 * Don't add if extension supported internally, but make exception
168 * for extension types that previously were not supported, but now are.
169 */
170 if (SSL_extension_supported(ext_type) &&
171 ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
Matt Caswell0f113f32015-01-22 03:40:55 +0000172 return 0;
173 /* Extension type must fit in 16 bits */
174 if (ext_type > 0xffff)
175 return 0;
176 /* Search for duplicate */
177 if (custom_ext_find(exts, ext_type))
178 return 0;
Dr. Stephen Henson7c0ef842016-05-11 21:14:57 +0100179 tmp = OPENSSL_realloc(exts->meths,
180 (exts->meths_count + 1) * sizeof(custom_ext_method));
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100181
Bernd Edlingered874fa2017-02-15 11:36:17 +0100182 if (tmp == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000183 return 0;
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100184
Dr. Stephen Henson7c0ef842016-05-11 21:14:57 +0100185 exts->meths = tmp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000186 meth = exts->meths + exts->meths_count;
Rich Salz16f8d4e2015-05-04 18:00:15 -0400187 memset(meth, 0, sizeof(*meth));
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 meth->parse_cb = parse_cb;
189 meth->add_cb = add_cb;
190 meth->free_cb = free_cb;
191 meth->ext_type = ext_type;
192 meth->add_arg = add_arg;
193 meth->parse_arg = parse_arg;
194 exts->meths_count++;
195 return 1;
196}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100197
Rob Percivaled29e822016-03-03 16:19:23 +0000198/* Return true if a client custom extension exists, false otherwise */
199int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
200{
201 return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
202}
203
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100204/* Application level functions to add custom extension callbacks */
Dr. Stephen Henson8cafe9e2014-08-19 13:54:38 +0100205int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
Matt Caswell0f113f32015-01-22 03:40:55 +0000206 custom_ext_add_cb add_cb,
207 custom_ext_free_cb free_cb,
Dr. Stephen Henson0cfefe42014-08-19 14:02:50 +0100208 void *add_arg,
Emilia Kaspera230b262016-08-05 19:03:17 +0200209 custom_ext_parse_cb parse_cb, void *parse_arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000210{
Rob Percivaled29e822016-03-03 16:19:23 +0000211#ifndef OPENSSL_NO_CT
212 /*
213 * We don't want applications registering callbacks for SCT extensions
214 * whilst simultaneously using the built-in SCT validation features, as
215 * these two things may not play well together.
216 */
217 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
Viktor Dukhovni43341432016-04-07 14:17:37 -0400218 SSL_CTX_ct_is_enabled(ctx))
219 return 0;
Rob Percivaled29e822016-03-03 16:19:23 +0000220#endif
Viktor Dukhovni43341432016-04-07 14:17:37 -0400221 return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
222 free_cb, add_arg, parse_cb, parse_arg);
Matt Caswell0f113f32015-01-22 03:40:55 +0000223}
Dr. Stephen Hensonecf4d662014-08-10 12:08:08 +0100224
Dr. Stephen Henson8cafe9e2014-08-19 13:54:38 +0100225int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
Matt Caswell0f113f32015-01-22 03:40:55 +0000226 custom_ext_add_cb add_cb,
227 custom_ext_free_cb free_cb,
Dr. Stephen Henson0cfefe42014-08-19 14:02:50 +0100228 void *add_arg,
Emilia Kaspera230b262016-08-05 19:03:17 +0200229 custom_ext_parse_cb parse_cb, void *parse_arg)
Matt Caswell0f113f32015-01-22 03:40:55 +0000230{
231 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
232 add_cb, free_cb, add_arg, parse_cb, parse_arg);
233}
Dr. Stephen Hensonc846a5f2014-08-19 13:33:51 +0100234
235int SSL_extension_supported(unsigned int ext_type)
Matt Caswell0f113f32015-01-22 03:40:55 +0000236{
237 switch (ext_type) {
238 /* Internally supported extensions. */
239 case TLSEXT_TYPE_application_layer_protocol_negotiation:
240 case TLSEXT_TYPE_ec_point_formats:
Matt Caswellde4d7642016-11-09 14:51:06 +0000241 case TLSEXT_TYPE_supported_groups:
Matt Caswell1595ca02016-04-11 11:41:19 +0100242#ifndef OPENSSL_NO_NEXTPROTONEG
Matt Caswell0f113f32015-01-22 03:40:55 +0000243 case TLSEXT_TYPE_next_proto_neg:
Matt Caswell1595ca02016-04-11 11:41:19 +0100244#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000245 case TLSEXT_TYPE_padding:
246 case TLSEXT_TYPE_renegotiate:
247 case TLSEXT_TYPE_server_name:
248 case TLSEXT_TYPE_session_ticket:
249 case TLSEXT_TYPE_signature_algorithms:
250 case TLSEXT_TYPE_srp:
251 case TLSEXT_TYPE_status_request:
Rob Percivaled29e822016-03-03 16:19:23 +0000252 case TLSEXT_TYPE_signed_certificate_timestamp:
Matt Caswell0f113f32015-01-22 03:40:55 +0000253 case TLSEXT_TYPE_use_srtp:
Matt Caswelle481f9b2015-05-15 10:49:56 +0100254#ifdef TLSEXT_TYPE_encrypt_then_mac
Matt Caswell0f113f32015-01-22 03:40:55 +0000255 case TLSEXT_TYPE_encrypt_then_mac:
Matt Caswelle481f9b2015-05-15 10:49:56 +0100256#endif
Matt Caswell91b60e22016-11-24 11:13:35 +0000257 case TLSEXT_TYPE_key_share:
258 case TLSEXT_TYPE_supported_versions:
259 case TLSEXT_TYPE_extended_master_secret:
Matt Caswell0f113f32015-01-22 03:40:55 +0000260 return 1;
261 default:
262 return 0;
263 }
264}