]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/t1_ext.c
Make asn1 fuzzer more reproducible
[thirdparty/openssl.git] / ssl / t1_ext.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
ecf4d660 3 *
846e33c7
RS
4 * 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
ecf4d660
DSH
8 */
9
10/* Custom extension utility functions */
11
3c27208f 12#include <openssl/ct.h>
ecf4d660
DSH
13#include "ssl_locl.h"
14
0cfefe4b 15/* Find a custom extension from the list. */
ed29e82a 16static custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
0f113f3e
MC
17 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.
28ea0a0c
DSH
30 */
31void custom_ext_init(custom_ext_methods *exts)
0f113f3e
MC
32{
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}
ecf4d660 38
0cfefe4b 39/* Pass received custom extension data to the application for parsing. */
ecf4d660 40int custom_ext_parse(SSL *s, int server,
0f113f3e
MC
41 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;
ecf4d660 69
a230b26e 70 return meth->parse_cb(s, ext_type, ext_data, ext_size, al, meth->parse_arg);
0f113f3e 71}
ecf4d660 72
2c7b4dbc
MC
73/*
74 * Request custom extension data from the application and add to the return
75 * buffer.
76 */
ae2f7b37 77int custom_ext_add(SSL *s, int server, WPACKET *pkt, int *al)
2c7b4dbc
MC
78{
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;
2c7b4dbc
MC
86
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
08029dfa 109 if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
de451856 110 || !WPACKET_start_sub_packet_u16(pkt)
0217dd19
MC
111 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
112 || !WPACKET_close(pkt)) {
2c7b4dbc
MC
113 *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
ecf4d660 132/* Copy table of custom extensions */
ecf4d660 133int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
0f113f3e
MC
134{
135 if (src->meths_count) {
136 dst->meths =
7644a9ae 137 OPENSSL_memdup(src->meths,
a230b26e 138 sizeof(custom_ext_method) * src->meths_count);
0f113f3e
MC
139 if (dst->meths == NULL)
140 return 0;
141 dst->meths_count = src->meths_count;
142 }
143 return 1;
144}
ecf4d660
DSH
145
146void custom_exts_free(custom_ext_methods *exts)
0f113f3e 147{
b548a1f1 148 OPENSSL_free(exts->meths);
0f113f3e 149}
ecf4d660 150
0cfefe4b 151/* Set callbacks for a custom extension. */
8cafe9e8 152static int custom_ext_meth_add(custom_ext_methods *exts,
0f113f3e
MC
153 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{
7c0ef843 159 custom_ext_method *meth, *tmp;
0f113f3e
MC
160 /*
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;
ed29e82a
RP
166 /*
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)
0f113f3e
MC
172 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;
7c0ef843
DSH
179 tmp = OPENSSL_realloc(exts->meths,
180 (exts->meths_count + 1) * sizeof(custom_ext_method));
ecf4d660 181
7c0ef843
DSH
182 if (tmp == NULL) {
183 OPENSSL_free(exts->meths);
184 exts->meths = NULL;
0f113f3e
MC
185 exts->meths_count = 0;
186 return 0;
187 }
ecf4d660 188
7c0ef843 189 exts->meths = tmp;
0f113f3e 190 meth = exts->meths + exts->meths_count;
16f8d4eb 191 memset(meth, 0, sizeof(*meth));
0f113f3e
MC
192 meth->parse_cb = parse_cb;
193 meth->add_cb = add_cb;
194 meth->free_cb = free_cb;
195 meth->ext_type = ext_type;
196 meth->add_arg = add_arg;
197 meth->parse_arg = parse_arg;
198 exts->meths_count++;
199 return 1;
200}
ecf4d660 201
ed29e82a
RP
202/* Return true if a client custom extension exists, false otherwise */
203int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
204{
205 return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
206}
207
ecf4d660 208/* Application level functions to add custom extension callbacks */
8cafe9e8 209int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
0f113f3e
MC
210 custom_ext_add_cb add_cb,
211 custom_ext_free_cb free_cb,
0cfefe4b 212 void *add_arg,
a230b26e 213 custom_ext_parse_cb parse_cb, void *parse_arg)
0f113f3e 214{
ed29e82a
RP
215#ifndef OPENSSL_NO_CT
216 /*
217 * We don't want applications registering callbacks for SCT extensions
218 * whilst simultaneously using the built-in SCT validation features, as
219 * these two things may not play well together.
220 */
221 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
43341433
VD
222 SSL_CTX_ct_is_enabled(ctx))
223 return 0;
ed29e82a 224#endif
43341433
VD
225 return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
226 free_cb, add_arg, parse_cb, parse_arg);
0f113f3e 227}
ecf4d660 228
8cafe9e8 229int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
0f113f3e
MC
230 custom_ext_add_cb add_cb,
231 custom_ext_free_cb free_cb,
0cfefe4b 232 void *add_arg,
a230b26e 233 custom_ext_parse_cb parse_cb, void *parse_arg)
0f113f3e
MC
234{
235 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
236 add_cb, free_cb, add_arg, parse_cb, parse_arg);
237}
c846a5f5
DSH
238
239int SSL_extension_supported(unsigned int ext_type)
0f113f3e
MC
240{
241 switch (ext_type) {
242 /* Internally supported extensions. */
243 case TLSEXT_TYPE_application_layer_protocol_negotiation:
244 case TLSEXT_TYPE_ec_point_formats:
de4d764e 245 case TLSEXT_TYPE_supported_groups:
1595ca02 246#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e 247 case TLSEXT_TYPE_next_proto_neg:
1595ca02 248#endif
0f113f3e
MC
249 case TLSEXT_TYPE_padding:
250 case TLSEXT_TYPE_renegotiate:
251 case TLSEXT_TYPE_server_name:
252 case TLSEXT_TYPE_session_ticket:
253 case TLSEXT_TYPE_signature_algorithms:
254 case TLSEXT_TYPE_srp:
255 case TLSEXT_TYPE_status_request:
ed29e82a 256 case TLSEXT_TYPE_signed_certificate_timestamp:
0f113f3e 257 case TLSEXT_TYPE_use_srtp:
e481f9b9 258#ifdef TLSEXT_TYPE_encrypt_then_mac
0f113f3e 259 case TLSEXT_TYPE_encrypt_then_mac:
e481f9b9 260#endif
91b60e2a
MC
261 case TLSEXT_TYPE_key_share:
262 case TLSEXT_TYPE_supported_versions:
263 case TLSEXT_TYPE_extended_master_secret:
0f113f3e
MC
264 return 1;
265 default:
266 return 0;
267 }
268}