Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 1 | /* ssl/t1_ext.c */ |
| 2 | /* ==================================================================== |
| 3 | * Copyright (c) 2014 The OpenSSL Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * openssl-core@openssl.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | * This product includes cryptographic software written by Eric Young |
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | /* Custom extension utility functions */ |
| 57 | |
| 58 | #include "ssl_locl.h" |
| 59 | |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 60 | |
Dr. Stephen Henson | 0cfefe4 | 2014-08-19 14:02:50 +0100 | [diff] [blame] | 61 | /* Find a custom extension from the list. */ |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 62 | static custom_ext_method *custom_ext_find(custom_ext_methods *exts, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 63 | unsigned int ext_type) |
| 64 | { |
| 65 | size_t i; |
| 66 | custom_ext_method *meth = exts->meths; |
| 67 | for (i = 0; i < exts->meths_count; i++, meth++) { |
| 68 | if (ext_type == meth->ext_type) |
| 69 | return meth; |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Initialise custom extensions flags to indicate neither sent nor received. |
Dr. Stephen Henson | 28ea0a0 | 2014-08-12 14:25:49 +0100 | [diff] [blame] | 76 | */ |
| 77 | void custom_ext_init(custom_ext_methods *exts) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 78 | { |
| 79 | size_t i; |
| 80 | custom_ext_method *meth = exts->meths; |
| 81 | for (i = 0; i < exts->meths_count; i++, meth++) |
| 82 | meth->ext_flags = 0; |
| 83 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 84 | |
Dr. Stephen Henson | 0cfefe4 | 2014-08-19 14:02:50 +0100 | [diff] [blame] | 85 | /* Pass received custom extension data to the application for parsing. */ |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 86 | int custom_ext_parse(SSL *s, int server, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 87 | unsigned int ext_type, |
| 88 | const unsigned char *ext_data, size_t ext_size, int *al) |
| 89 | { |
| 90 | custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext; |
| 91 | custom_ext_method *meth; |
| 92 | meth = custom_ext_find(exts, ext_type); |
| 93 | /* If not found return success */ |
| 94 | if (!meth) |
| 95 | return 1; |
| 96 | if (!server) { |
| 97 | /* |
| 98 | * If it's ServerHello we can't have any extensions not sent in |
| 99 | * ClientHello. |
| 100 | */ |
| 101 | if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) { |
| 102 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | /* If already present it's a duplicate */ |
| 107 | if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) { |
| 108 | *al = TLS1_AD_DECODE_ERROR; |
| 109 | return 0; |
| 110 | } |
| 111 | meth->ext_flags |= SSL_EXT_FLAG_RECEIVED; |
| 112 | /* If no parse function set return success */ |
| 113 | if (!meth->parse_cb) |
| 114 | return 1; |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 115 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 116 | return meth->parse_cb(s, ext_type, ext_data, ext_size, al, |
| 117 | meth->parse_arg); |
| 118 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 119 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 120 | /* |
| 121 | * Request custom extension data from the application and add to the return |
| 122 | * buffer. |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 123 | */ |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 124 | int custom_ext_add(SSL *s, int server, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 125 | unsigned char **pret, unsigned char *limit, int *al) |
| 126 | { |
| 127 | custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext; |
| 128 | custom_ext_method *meth; |
| 129 | unsigned char *ret = *pret; |
| 130 | size_t i; |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 131 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 132 | for (i = 0; i < exts->meths_count; i++) { |
| 133 | const unsigned char *out = NULL; |
| 134 | size_t outlen = 0; |
| 135 | meth = exts->meths + i; |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 136 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 137 | if (server) { |
| 138 | /* |
| 139 | * For ServerHello only send extensions present in ClientHello. |
| 140 | */ |
| 141 | if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED)) |
| 142 | continue; |
| 143 | /* If callback absent for server skip it */ |
| 144 | if (!meth->add_cb) |
| 145 | continue; |
| 146 | } |
| 147 | if (meth->add_cb) { |
| 148 | int cb_retval = 0; |
| 149 | cb_retval = meth->add_cb(s, meth->ext_type, |
| 150 | &out, &outlen, al, meth->add_arg); |
| 151 | if (cb_retval < 0) |
| 152 | return 0; /* error */ |
| 153 | if (cb_retval == 0) |
| 154 | continue; /* skip this extension */ |
| 155 | } |
| 156 | if (4 > limit - ret || outlen > (size_t)(limit - ret - 4)) |
| 157 | return 0; |
| 158 | s2n(meth->ext_type, ret); |
| 159 | s2n(outlen, ret); |
| 160 | if (outlen) { |
| 161 | memcpy(ret, out, outlen); |
| 162 | ret += outlen; |
| 163 | } |
| 164 | /* |
| 165 | * We can't send duplicates: code logic should prevent this. |
| 166 | */ |
| 167 | OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT)); |
| 168 | /* |
| 169 | * Indicate extension has been sent: this is both a sanity check to |
| 170 | * ensure we don't send duplicate extensions and indicates that it is |
| 171 | * not an error if the extension is present in ServerHello. |
| 172 | */ |
| 173 | meth->ext_flags |= SSL_EXT_FLAG_SENT; |
| 174 | if (meth->free_cb) |
| 175 | meth->free_cb(s, meth->ext_type, out, meth->add_arg); |
| 176 | } |
| 177 | *pret = ret; |
| 178 | return 1; |
| 179 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 180 | |
| 181 | /* Copy table of custom extensions */ |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 182 | int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 183 | { |
| 184 | if (src->meths_count) { |
| 185 | dst->meths = |
| 186 | BUF_memdup(src->meths, |
| 187 | sizeof(custom_ext_method) * src->meths_count); |
| 188 | if (dst->meths == NULL) |
| 189 | return 0; |
| 190 | dst->meths_count = src->meths_count; |
| 191 | } |
| 192 | return 1; |
| 193 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 194 | |
| 195 | void custom_exts_free(custom_ext_methods *exts) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 196 | { |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 197 | OPENSSL_free(exts->meths); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 198 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 199 | |
Dr. Stephen Henson | 0cfefe4 | 2014-08-19 14:02:50 +0100 | [diff] [blame] | 200 | /* Set callbacks for a custom extension. */ |
Dr. Stephen Henson | 8cafe9e | 2014-08-19 13:54:38 +0100 | [diff] [blame] | 201 | static int custom_ext_meth_add(custom_ext_methods *exts, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 202 | unsigned int ext_type, |
| 203 | custom_ext_add_cb add_cb, |
| 204 | custom_ext_free_cb free_cb, |
| 205 | void *add_arg, |
| 206 | custom_ext_parse_cb parse_cb, void *parse_arg) |
| 207 | { |
| 208 | custom_ext_method *meth; |
| 209 | /* |
| 210 | * Check application error: if add_cb is not set free_cb will never be |
| 211 | * called. |
| 212 | */ |
| 213 | if (!add_cb && free_cb) |
| 214 | return 0; |
| 215 | /* Don't add if extension supported internally. */ |
| 216 | if (SSL_extension_supported(ext_type)) |
| 217 | return 0; |
| 218 | /* Extension type must fit in 16 bits */ |
| 219 | if (ext_type > 0xffff) |
| 220 | return 0; |
| 221 | /* Search for duplicate */ |
| 222 | if (custom_ext_find(exts, ext_type)) |
| 223 | return 0; |
| 224 | exts->meths = OPENSSL_realloc(exts->meths, |
| 225 | (exts->meths_count + |
| 226 | 1) * sizeof(custom_ext_method)); |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 227 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 228 | if (!exts->meths) { |
| 229 | exts->meths_count = 0; |
| 230 | return 0; |
| 231 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 232 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 233 | meth = exts->meths + exts->meths_count; |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 234 | memset(meth, 0, sizeof(*meth)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 235 | meth->parse_cb = parse_cb; |
| 236 | meth->add_cb = add_cb; |
| 237 | meth->free_cb = free_cb; |
| 238 | meth->ext_type = ext_type; |
| 239 | meth->add_arg = add_arg; |
| 240 | meth->parse_arg = parse_arg; |
| 241 | exts->meths_count++; |
| 242 | return 1; |
| 243 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 244 | |
| 245 | /* Application level functions to add custom extension callbacks */ |
Dr. Stephen Henson | 8cafe9e | 2014-08-19 13:54:38 +0100 | [diff] [blame] | 246 | int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 247 | custom_ext_add_cb add_cb, |
| 248 | custom_ext_free_cb free_cb, |
Dr. Stephen Henson | 0cfefe4 | 2014-08-19 14:02:50 +0100 | [diff] [blame] | 249 | void *add_arg, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 250 | custom_ext_parse_cb parse_cb, |
| 251 | void *parse_arg) |
| 252 | { |
| 253 | return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, |
| 254 | add_cb, free_cb, add_arg, parse_cb, parse_arg); |
| 255 | } |
Dr. Stephen Henson | ecf4d66 | 2014-08-10 12:08:08 +0100 | [diff] [blame] | 256 | |
Dr. Stephen Henson | 8cafe9e | 2014-08-19 13:54:38 +0100 | [diff] [blame] | 257 | int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 258 | custom_ext_add_cb add_cb, |
| 259 | custom_ext_free_cb free_cb, |
Dr. Stephen Henson | 0cfefe4 | 2014-08-19 14:02:50 +0100 | [diff] [blame] | 260 | void *add_arg, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 261 | custom_ext_parse_cb parse_cb, |
| 262 | void *parse_arg) |
| 263 | { |
| 264 | return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type, |
| 265 | add_cb, free_cb, add_arg, parse_cb, parse_arg); |
| 266 | } |
Dr. Stephen Henson | c846a5f | 2014-08-19 13:33:51 +0100 | [diff] [blame] | 267 | |
| 268 | int SSL_extension_supported(unsigned int ext_type) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 269 | { |
| 270 | switch (ext_type) { |
| 271 | /* Internally supported extensions. */ |
| 272 | case TLSEXT_TYPE_application_layer_protocol_negotiation: |
| 273 | case TLSEXT_TYPE_ec_point_formats: |
| 274 | case TLSEXT_TYPE_elliptic_curves: |
| 275 | case TLSEXT_TYPE_heartbeat: |
| 276 | case TLSEXT_TYPE_next_proto_neg: |
| 277 | case TLSEXT_TYPE_padding: |
| 278 | case TLSEXT_TYPE_renegotiate: |
| 279 | case TLSEXT_TYPE_server_name: |
| 280 | case TLSEXT_TYPE_session_ticket: |
| 281 | case TLSEXT_TYPE_signature_algorithms: |
| 282 | case TLSEXT_TYPE_srp: |
| 283 | case TLSEXT_TYPE_status_request: |
| 284 | case TLSEXT_TYPE_use_srtp: |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame^] | 285 | #ifdef TLSEXT_TYPE_encrypt_then_mac |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 286 | case TLSEXT_TYPE_encrypt_then_mac: |
Matt Caswell | e481f9b | 2015-05-15 10:49:56 +0100 | [diff] [blame^] | 287 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 288 | return 1; |
| 289 | default: |
| 290 | return 0; |
| 291 | } |
| 292 | } |