From: Matt Caswell Date: Wed, 7 Dec 2016 17:04:46 +0000 (+0000) Subject: Various style updates following extensions refactor X-Git-Tag: OpenSSL_1_1_1-pre1~2884 X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=commitdiff_plain;h=1266eefdb66db6c01e859ae672ccc19261e75bbf Various style updates following extensions refactor Perl changes reviewed by Richard Levitte. Non-perl changes reviewed by Rich Salz Reviewed-by: Rich Salz Reviewed-by: Richard Levitte --- diff --git a/ssl/statem/extensions.c b/ssl/statem/extensions.c index 2d0a323cf9..51f8965606 100644 --- a/ssl/statem/extensions.c +++ b/ssl/statem/extensions.c @@ -10,59 +10,63 @@ #include "../ssl_locl.h" #include "statem_locl.h" -static int tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent, +static int final_renegotiate(SSL *s, unsigned int context, int sent, int *al); -static int tls_ext_init_server_name(SSL *s, unsigned int context); -static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent, +static int init_server_name(SSL *s, unsigned int context); +static int final_server_name(SSL *s, unsigned int context, int sent, int *al); #ifndef OPENSSL_NO_EC -static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent, +static int final_ec_pt_formats(SSL *s, unsigned int context, int sent, int *al); #endif -static int tls_ext_init_session_ticket(SSL *s, unsigned int context); -static int tls_ext_init_status_request(SSL *s, unsigned int context); -static int tls_ext_final_status_request(SSL *s, unsigned int context, int sent, +static int init_session_ticket(SSL *s, unsigned int context); +static int init_status_request(SSL *s, unsigned int context); +static int final_status_request(SSL *s, unsigned int context, int sent, int *al); #ifndef OPENSSL_NO_NEXTPROTONEG -static int tls_ext_init_npn(SSL *s, unsigned int context); +static int init_npn(SSL *s, unsigned int context); #endif -static int tls_ext_init_alpn(SSL *s, unsigned int context); -static int tls_ext_final_alpn(SSL *s, unsigned int context, int sent, int *al); -static int tls_ext_init_sig_algs(SSL *s, unsigned int context); +static int init_alpn(SSL *s, unsigned int context); +static int final_alpn(SSL *s, unsigned int context, int sent, int *al); +static int init_sig_algs(SSL *s, unsigned int context); #ifndef OPENSSL_NO_SRP -static int tls_ext_init_srp(SSL *s, unsigned int context); +static int init_srp(SSL *s, unsigned int context); #endif -static int tls_ext_init_etm(SSL *s, unsigned int context); -static int tls_ext_init_ems(SSL *s, unsigned int context); -static int tls_ext_final_ems(SSL *s, unsigned int context, int sent, int *al); +static int init_etm(SSL *s, unsigned int context); +static int init_ems(SSL *s, unsigned int context); +static int final_ems(SSL *s, unsigned int context, int sent, int *al); #ifndef OPENSSL_NO_SRTP -static int tls_ext_init_srtp(SSL *s, unsigned int context); +static int init_srtp(SSL *s, unsigned int context); #endif /* Structure to define a built-in extension */ -typedef struct { - /* The ID for the extension */ +typedef struct extensions_definition_st { + /* The defined type for the extension */ unsigned int type; + /* + * The context that this extension applies to, e.g. what messages and + * protocol versions + */ + unsigned int context; /* * Initialise extension before parsing. Always called for relevant contexts * even if extension not present */ - int (*init_ext)(SSL *s, unsigned int context); - /* Parse extension received by server from client */ - int (*parse_client_ext)(SSL *s, PACKET *pkt, int *al); - /* Parse extension received by client from server */ - int (*parse_server_ext)(SSL *s, PACKET *pkt, int *al); - /* Construct extension sent by server */ - int (*construct_server_ext)(SSL *s, WPACKET *pkt, int *al); - /* Construct extension sent by client */ - int (*construct_client_ext)(SSL *s, WPACKET *pkt, int *al); + int (*init)(SSL *s, unsigned int context); + /* Parse extension sent from client to server */ + int (*parse_ctos)(SSL *s, PACKET *pkt, int *al); + /* Parse extension send from server to client */ + int (*parse_stoc)(SSL *s, PACKET *pkt, int *al); + /* Construct extension sent from server to client */ + int (*construct_stoc)(SSL *s, WPACKET *pkt, int *al); + /* Construct extension sent from client to server */ + int (*construct_ctos)(SSL *s, WPACKET *pkt, int *al); /* * Finalise extension after parsing. Always called where an extensions was * initialised even if the extension was not present. |sent| is set to 1 if * the extension was seen, or 0 otherwise. */ - int (*finalise_ext)(SSL *s, unsigned int context, int sent, int *al); - unsigned int context; + int (*final)(SSL *s, unsigned int context, int sent, int *al); } EXTENSION_DEFINITION; /* @@ -91,103 +95,73 @@ typedef struct { static const EXTENSION_DEFINITION ext_defs[] = { { TLSEXT_TYPE_renegotiate, - NULL, - tls_parse_client_renegotiate, - tls_parse_server_renegotiate, - tls_construct_server_renegotiate, - tls_construct_client_renegotiate, - tls_ext_final_renegotiate, EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED - | EXT_TLS1_2_AND_BELOW_ONLY + | EXT_TLS1_2_AND_BELOW_ONLY, + NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate, + tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate, + final_renegotiate }, { TLSEXT_TYPE_server_name, - tls_ext_init_server_name, - tls_parse_client_server_name, - tls_parse_server_server_name, - tls_construct_server_server_name, - tls_construct_client_server_name, - tls_ext_final_server_name, EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO - | EXT_TLS1_3_ENCRYPTED_EXTENSIONS + | EXT_TLS1_3_ENCRYPTED_EXTENSIONS, + init_server_name, + tls_parse_ctos_server_name, tls_parse_stoc_server_name, + tls_construct_stoc_server_name, tls_construct_ctos_server_name, + final_server_name }, #ifndef OPENSSL_NO_SRP { TLSEXT_TYPE_srp, - tls_ext_init_srp, - tls_parse_client_srp, - NULL, - NULL, - tls_construct_client_srp, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL }, #endif #ifndef OPENSSL_NO_EC { TLSEXT_TYPE_ec_point_formats, - NULL, - tls_parse_client_ec_pt_formats, - tls_parse_server_ec_pt_formats, - tls_construct_server_ec_pt_formats, - tls_construct_client_ec_pt_formats, - tls_ext_final_ec_pt_formats, - EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats, + tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats, + final_ec_pt_formats }, { TLSEXT_TYPE_supported_groups, - NULL, - tls_parse_client_supported_groups, - NULL, + EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS, + NULL, tls_parse_ctos_supported_groups, NULL, NULL /* TODO(TLS1.3): Need to add this */, - tls_construct_client_supported_groups, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS + tls_construct_ctos_supported_groups, NULL }, #endif { TLSEXT_TYPE_session_ticket, - tls_ext_init_session_ticket, - tls_parse_client_session_ticket, - tls_parse_server_session_ticket, - tls_construct_server_session_ticket, - tls_construct_client_session_ticket, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + init_session_ticket, tls_parse_ctos_session_ticket, + tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket, + tls_construct_ctos_session_ticket, NULL }, { TLSEXT_TYPE_signature_algorithms, - tls_ext_init_sig_algs, - tls_parse_client_sig_algs, - NULL, - NULL, - tls_construct_client_sig_algs, - NULL, - EXT_CLIENT_HELLO + EXT_CLIENT_HELLO, + init_sig_algs, tls_parse_ctos_sig_algs, NULL, NULL, + tls_construct_ctos_sig_algs, NULL }, #ifndef OPENSSL_NO_OCSP { TLSEXT_TYPE_status_request, - tls_ext_init_status_request, - tls_parse_client_status_request, - tls_parse_server_status_request, - tls_construct_server_status_request, - tls_construct_client_status_request, - tls_ext_final_status_request, EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO - | EXT_TLS1_3_CERTIFICATE + | EXT_TLS1_3_CERTIFICATE, + init_status_request, tls_parse_ctos_status_request, + tls_parse_stoc_status_request, tls_construct_stoc_status_request, + tls_construct_ctos_status_request, final_status_request }, #endif #ifndef OPENSSL_NO_NEXTPROTONEG { TLSEXT_TYPE_next_proto_neg, - tls_ext_init_npn, - tls_parse_client_npn, - tls_parse_server_npn, - tls_construct_server_next_proto_neg, - tls_construct_client_npn, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn, + tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL }, #endif { @@ -196,76 +170,52 @@ static const EXTENSION_DEFINITION ext_defs[] = { * happens after server_name callbacks */ TLSEXT_TYPE_application_layer_protocol_negotiation, - tls_ext_init_alpn, - tls_parse_client_alpn, - tls_parse_server_alpn, - tls_construct_server_alpn, - tls_construct_client_alpn, - tls_ext_final_alpn, EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO - | EXT_TLS1_3_ENCRYPTED_EXTENSIONS + | EXT_TLS1_3_ENCRYPTED_EXTENSIONS, + init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn, + tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn }, #ifndef OPENSSL_NO_SRTP { TLSEXT_TYPE_use_srtp, - tls_ext_init_srtp, - tls_parse_client_use_srtp, - tls_parse_server_use_srtp, - tls_construct_server_use_srtp, - tls_construct_client_use_srtp, - NULL, EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO - | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY + | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY, + init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp, + tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL }, #endif { TLSEXT_TYPE_encrypt_then_mac, - tls_ext_init_etm, - tls_parse_client_etm, - tls_parse_server_etm, - tls_construct_server_etm, - tls_construct_client_etm, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm, + tls_construct_stoc_etm, tls_construct_ctos_etm, NULL }, #ifndef OPENSSL_NO_CT { TLSEXT_TYPE_signed_certificate_timestamp, + EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO + | EXT_TLS1_3_CERTIFICATE, NULL, /* * No server side support for this, but can be provided by a custom * extension. This is an exception to the rule that custom extensions * cannot override built in ones. */ - NULL, - tls_parse_server_sct, - NULL, - tls_construct_client_sct, - NULL, - EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO - | EXT_TLS1_3_CERTIFICATE + NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL }, #endif { TLSEXT_TYPE_extended_master_secret, - tls_ext_init_ems, - tls_parse_client_ems, - tls_parse_server_ems, - tls_construct_server_ems, - tls_construct_client_ems, - tls_ext_final_ems, - EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems, + tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems }, { TLSEXT_TYPE_supported_versions, + EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY, NULL, /* Processed inline as part of version selection */ - NULL, - NULL, - NULL, - tls_construct_client_supported_versions, - NULL, - EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY + NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL }, { /* @@ -273,15 +223,11 @@ static const EXTENSION_DEFINITION ext_defs[] = { * been parsed before we do this one. */ TLSEXT_TYPE_key_share, - NULL, - tls_parse_client_key_share, - tls_parse_server_key_share, - tls_construct_server_key_share, - tls_construct_client_key_share, - NULL, EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY - | EXT_TLS1_3_ONLY + | EXT_TLS1_3_ONLY, + NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share, + tls_construct_stoc_key_share, tls_construct_ctos_key_share, NULL }, { /* @@ -289,25 +235,16 @@ static const EXTENSION_DEFINITION ext_defs[] = { * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set */ TLSEXT_TYPE_cryptopro_bug, - NULL, - NULL, - NULL, - tls_construct_server_cryptopro_bug, - NULL, - NULL, - EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY + EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY, + NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL }, { /* Last in the list because it must be added as the last extension */ TLSEXT_TYPE_padding, + EXT_CLIENT_HELLO, NULL, /* We send this, but don't read it */ - NULL, - NULL, - NULL, - tls_construct_client_padding, - NULL, - EXT_CLIENT_HELLO + NULL, NULL, NULL, tls_construct_ctos_padding, NULL } }; @@ -318,26 +255,27 @@ static const EXTENSION_DEFINITION ext_defs[] = { * 1 if we found a definition for the extension, and |*idx| is set to its index */ static int verify_extension(SSL *s, unsigned int context, unsigned int type, - custom_ext_methods *meths, int *found, size_t *idx) + custom_ext_methods *meths, RAW_EXTENSION *rawexlist, + RAW_EXTENSION **found) { size_t i; size_t builtin_num = OSSL_NELEM(ext_defs); + EXTENSION_DEFINITION *thisext; - for (i = 0; i < builtin_num; i++) { - if (type == ext_defs[i].type) { + for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) { + if (type == thisext->type) { /* Check we're allowed to use this extension in this context */ - if ((context & ext_defs[i].context) == 0) + if ((context & thisext->context) == 0) return 0; if (SSL_IS_DTLS(s)) { - if ((ext_defs[i].context & EXT_TLS_ONLY) != 0) + if ((thisext->context & EXT_TLS_ONLY) != 0) return 0; - } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) { + } else if ((thisext->context & EXT_DTLS_ONLY) != 0) { return 0; } - *found = 1; - *idx = i; + *found = &rawexlist[i]; return 1; } } @@ -347,7 +285,7 @@ static int verify_extension(SSL *s, unsigned int context, unsigned int type, * Custom extensions only apply to <=TLS1.2. This extension is unknown * in this context - we allow it */ - *found = 0; + *found = NULL; return 1; } @@ -355,15 +293,14 @@ static int verify_extension(SSL *s, unsigned int context, unsigned int type, if (meths != NULL) { for (i = builtin_num; i < builtin_num + meths->meths_count; i++) { if (meths->meths[i - builtin_num].ext_type == type) { - *found = 1; - *idx = i; + *found = &rawexlist[i]; return 1; } } } /* Unknown extension. We allow it */ - *found = 0; + *found = NULL; return 1; } @@ -390,16 +327,17 @@ static int extension_is_relevant(SSL *s, unsigned int extctx, /* * Gather a list of all the extensions from the data in |packet]. |context| * tells us which message this extension is for. The raw extension data is - * stored in |*res|. In the event of an error the alert type to use is stored in - * |*al|. We don't actually process the content of the extensions yet, except to - * check their types. This function also runs the initialiser functions for all - * known extensions (whether we have collected them or not). + * stored in |*res| on success. In the event of an error the alert type to use + * is stored in |*al|. We don't actually process the content of the extensions + * yet, except to check their types. This function also runs the initialiser + * functions for all known extensions (whether we have collected them or not). + * If successful the caller is responsible for freeing the contents of |*res|. * * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be * more than one extension of the same type in a ClientHello or ServerHello. * This function returns 1 if all extensions are unique and we have parsed their * types, and 0 if the extensions contain duplicates, could not be successfully - * collected, or an internal error occurred. We only check duplicates for + * found, or an internal error occurred. We only check duplicates for * extensions that we know about. We ignore others. */ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, @@ -410,6 +348,7 @@ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, int found = 0; custom_ext_methods *exts = NULL; RAW_EXTENSION *raw_extensions = NULL; + EXTENSION_DEFINITION *thisexd; /* * Initialise server side custom extensions. Client side is done during @@ -424,7 +363,7 @@ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, raw_extensions = OPENSSL_zalloc((OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0)) - * sizeof(RAW_EXTENSION)); + * sizeof(*raw_extensions)); if (raw_extensions == NULL) { *al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE); @@ -434,6 +373,7 @@ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, while (PACKET_remaining(&extensions) > 0) { unsigned int type; PACKET extension; + RAW_EXTENSION *thisex; if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extension)) { @@ -445,17 +385,16 @@ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, * Verify this extension is allowed. We only check duplicates for * extensions that we recognise. */ - if (!verify_extension(s, context, type, exts, &found, &idx) - || (found == 1 - && raw_extensions[idx].present == 1)) { + if (!verify_extension(s, context, type, exts, raw_extensions, &thisex) + || (thisex != NULL && thisex->present == 1)) { SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION); *al = SSL_AD_ILLEGAL_PARAMETER; goto err; } - if (found) { - raw_extensions[idx].data = extension; - raw_extensions[idx].present = 1; - raw_extensions[idx].type = type; + if (thisex != NULL) { + thisex->data = extension; + thisex->present = 1; + thisex->type = type; } } @@ -463,10 +402,10 @@ int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context, * Initialise all known extensions relevant to this context, whether we have * found them or not */ - for (i = 0; i < OSSL_NELEM(ext_defs); i++) { - if(ext_defs[i].init_ext != NULL && (ext_defs[i].context & context) != 0 - && extension_is_relevant(s, ext_defs[i].context, context) - && !ext_defs[i].init_ext(s, context)) { + for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs); i++, thisexd++) { + if(thisexd->init != NULL && (thisexd->context & context) != 0 + && extension_is_relevant(s, thisexd->context, context) + && !thisexd->init(s, context)) { *al = SSL_AD_INTERNAL_ERROR; goto err; } @@ -518,14 +457,10 @@ int tls_parse_extension(SSL *s, unsigned int idx, int context, if (!extension_is_relevant(s, extdef->context, context)) return 1; - parser = s->server ? extdef->parse_client_ext : extdef->parse_server_ext; + parser = s->server ? extdef->parse_ctos : extdef->parse_stoc; - if (parser != NULL) { - if (!parser(s, &currext->data, al)) - return 0; - - return 1; - } + if (parser != NULL) + return parser(s, &currext->data, al); /* * If the parser is NULL we fall through to the custom extension @@ -561,7 +496,8 @@ int tls_parse_extension(SSL *s, unsigned int idx, int context, */ int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al) { - size_t loop, numexts = OSSL_NELEM(ext_defs); + size_t i, numexts = OSSL_NELEM(ext_defs); + EXTENSION_DEFINITION *thisexd; /* Calculate the number of extensions in the extensions list */ if ((context & EXT_CLIENT_HELLO) != 0) { @@ -571,7 +507,7 @@ int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al) } /* Parse each extension in turn */ - for (loop = 0; loop < numexts; loop++) { + for (i = 0; i < numexts; i++) { if (!tls_parse_extension(s, loop, context, exts, al)) return 0; } @@ -580,11 +516,10 @@ int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al) * Finalise all known extensions relevant to this context, whether we have * found them or not */ - for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) { - if(ext_defs[loop].finalise_ext != NULL - && (ext_defs[loop].context & context) != 0 - && !ext_defs[loop].finalise_ext(s, context, exts[loop].present, - al)) + for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) { + if(thisexd->final != NULL + && (thisexd->context & context) != 0 + && !thisexd->final(s, context, exts[i].present, al)) return 0; } @@ -594,14 +529,15 @@ int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al) /* * Construct all the extensions relevant to the current |context| and write * them to |pkt|. Returns 1 on success or 0 on failure. If a failure occurs then - * |al| is populated with a suitable alert code. + * |al| is populated with a suitable alert code. On a failure construction stops + * at the first extension to fail to construct. */ int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context, int *al) { - size_t loop; - int addcustom = 0; - int min_version, max_version = 0, reason, tmpal; + size_t i; + int addcustom = 0, min_version, max_version = 0, reason, tmpal; + EXTENSION_DEFINITION *thisexd; /* * Normally if something goes wrong during construction it's an internal @@ -651,29 +587,29 @@ int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context, goto err; } - for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) { + for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) { int (*construct)(SSL *s, WPACKET *pkt, int *al); /* Skip if not relevant for our context */ if ((ext_defs[loop].context & context) == 0) continue; - construct = s->server ? ext_defs[loop].construct_server_ext - : ext_defs[loop].construct_client_ext; + construct = s->server ? thisexd->construct_stoc + : thisexd->construct_ctos; /* Check if this extension is defined for our protocol. If not, skip */ if ((SSL_IS_DTLS(s) - && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY) + && (thisexd->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0) || (s->version == SSL3_VERSION - && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0) + && (thisexd->context & EXT_SSL3_ALLOWED) == 0) || (SSL_IS_TLS13(s) - && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY) + && (thisexd->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0) || (!SSL_IS_TLS13(s) - && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0 + && (thisexd->context & EXT_TLS1_3_ONLY) != 0 && (context & EXT_CLIENT_HELLO) == 0) - || ((ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0 + || ((thisexd->context & EXT_TLS1_3_ONLY) != 0 && (context & EXT_CLIENT_HELLO) != 0 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)) || construct == NULL) @@ -703,7 +639,7 @@ int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context, * of a failure then |*al| is populated with a suitable error code. */ -static int tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent, +static int final_renegotiate(SSL *s, unsigned int context, int sent, int *al) { if (!s->server) { @@ -737,7 +673,7 @@ static int tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent, return 1; } -static int tls_ext_init_server_name(SSL *s, unsigned int context) +static int init_server_name(SSL *s, unsigned int context) { if (s->server) s->servername_done = 0; @@ -745,7 +681,7 @@ static int tls_ext_init_server_name(SSL *s, unsigned int context) return 1; } -static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent, +static int final_server_name(SSL *s, unsigned int context, int sent, int *al) { int ret = SSL_TLSEXT_ERR_NOACK; @@ -778,7 +714,7 @@ static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent, } #ifndef OPENSSL_NO_EC -static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent, +static int final_ec_pt_formats(SSL *s, unsigned int context, int sent, int *al) { unsigned long alg_k, alg_a; @@ -794,23 +730,20 @@ static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent, * suite, then if server returns an EC point formats lists extension it * must contain uncompressed. */ - if ((s->tlsext_ecpointformatlist != NULL) - && (s->tlsext_ecpointformatlist_length > 0) - && (s->session->tlsext_ecpointformatlist != NULL) - && (s->session->tlsext_ecpointformatlist_length > 0) - && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { + if (s->tlsext_ecpointformatlist != NULL + && s->tlsext_ecpointformatlist_length > 0 + && s->session->tlsext_ecpointformatlist != NULL + && s->session->tlsext_ecpointformatlist_length > 0 + && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { /* we are using an ECC cipher */ size_t i; - unsigned char *list; - int found_uncompressed = 0; - list = s->session->tlsext_ecpointformatlist; + unsigned char *list = s->session->tlsext_ecpointformatlist; + for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { - if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { - found_uncompressed = 1; + if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed) break; - } } - if (!found_uncompressed) { + if (i == s->session->tlsext_ecpointformatlist_length) { SSLerr(SSL_F_TLS_EXT_FINAL_EC_PT_FORMATS, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); return 0; @@ -821,7 +754,7 @@ static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent, } #endif -static int tls_ext_init_session_ticket(SSL *s, unsigned int context) +static int init_session_ticket(SSL *s, unsigned int context) { if (!s->server) s->tlsext_ticket_expected = 0; @@ -829,7 +762,7 @@ static int tls_ext_init_session_ticket(SSL *s, unsigned int context) return 1; } -static int tls_ext_init_status_request(SSL *s, unsigned int context) +static int init_status_request(SSL *s, unsigned int context) { if (s->server) s->tlsext_status_type = -1; @@ -837,7 +770,7 @@ static int tls_ext_init_status_request(SSL *s, unsigned int context) return 1; } -static int tls_ext_final_status_request(SSL *s, unsigned int context, int sent, +static int final_status_request(SSL *s, unsigned int context, int sent, int *al) { if (s->server) @@ -855,7 +788,7 @@ static int tls_ext_final_status_request(SSL *s, unsigned int context, int sent, } #ifndef OPENSSL_NO_NEXTPROTONEG -static int tls_ext_init_npn(SSL *s, unsigned int context) +static int init_npn(SSL *s, unsigned int context) { s->s3->next_proto_neg_seen = 0; @@ -863,7 +796,7 @@ static int tls_ext_init_npn(SSL *s, unsigned int context) } #endif -static int tls_ext_init_alpn(SSL *s, unsigned int context) +static int init_alpn(SSL *s, unsigned int context) { OPENSSL_free(s->s3->alpn_selected); s->s3->alpn_selected = NULL; @@ -876,7 +809,7 @@ static int tls_ext_init_alpn(SSL *s, unsigned int context) return 1; } -static int tls_ext_final_alpn(SSL *s, unsigned int context, int sent, int *al) +static int final_alpn(SSL *s, unsigned int context, int sent, int *al) { const unsigned char *selected = NULL; unsigned char selected_len = 0; @@ -911,7 +844,7 @@ static int tls_ext_final_alpn(SSL *s, unsigned int context, int sent, int *al) return 1; } -static int tls_ext_init_sig_algs(SSL *s, unsigned int context) +static int init_sig_algs(SSL *s, unsigned int context) { /* Clear any signature algorithms extension received */ OPENSSL_free(s->s3->tmp.peer_sigalgs); @@ -921,7 +854,7 @@ static int tls_ext_init_sig_algs(SSL *s, unsigned int context) } #ifndef OPENSSL_NO_SRP -static int tls_ext_init_srp(SSL *s, unsigned int context) +static int init_srp(SSL *s, unsigned int context) { OPENSSL_free(s->srp_ctx.login); s->srp_ctx.login = NULL; @@ -930,14 +863,14 @@ static int tls_ext_init_srp(SSL *s, unsigned int context) } #endif -static int tls_ext_init_etm(SSL *s, unsigned int context) +static int init_etm(SSL *s, unsigned int context) { s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; return 1; } -static int tls_ext_init_ems(SSL *s, unsigned int context) +static int init_ems(SSL *s, unsigned int context) { if (!s->server) s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; @@ -945,7 +878,7 @@ static int tls_ext_init_ems(SSL *s, unsigned int context) return 1; } -static int tls_ext_final_ems(SSL *s, unsigned int context, int sent, int *al) +static int final_ems(SSL *s, unsigned int context, int sent, int *al) { if (!s->server && s->hit) { /* @@ -964,7 +897,7 @@ static int tls_ext_final_ems(SSL *s, unsigned int context, int sent, int *al) } #ifndef OPENSSL_NO_SRTP -static int tls_ext_init_srtp(SSL *s, unsigned int context) +static int init_srtp(SSL *s, unsigned int context) { if (s->server) s->srtp_profile = NULL; diff --git a/ssl/statem/extensions_clnt.c b/ssl/statem/extensions_clnt.c index df12969c43..950f1990cc 100644 --- a/ssl/statem/extensions_clnt.c +++ b/ssl/statem/extensions_clnt.c @@ -12,7 +12,7 @@ #include "../ssl_locl.h" #include "statem_locl.h" -int tls_construct_client_renegotiate(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, int *al) { /* Add RI if renegotiating */ if (!s->renegotiate) @@ -30,7 +30,7 @@ int tls_construct_client_renegotiate(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_client_server_name(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, int *al) { if (s->tlsext_hostname == NULL) return 1; @@ -54,7 +54,7 @@ int tls_construct_client_server_name(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRP -int tls_construct_client_srp(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, int *al) { /* Add SRP username if there is one */ if (s->srp_ctx.login == NULL) @@ -81,8 +81,7 @@ int tls_construct_client_srp(SSL *s, WPACKET *pkt, int *al) #ifndef OPENSSL_NO_EC static int use_ecc(SSL *s) { - int using_ecc = 0; - int i; + int i, end; unsigned long alg_k, alg_a; STACK_OF(SSL_CIPHER) *cipher_stack = NULL; @@ -90,26 +89,23 @@ static int use_ecc(SSL *s) if (s->version == SSL3_VERSION) return 0; - cipher_stack = SSL_get_ciphers(s); - - for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { + end = sk_SSL_CIPHER_num(cipher_stack); + for (i = 0; i < end; i++) { const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); alg_k = c->algorithm_mkey; alg_a = c->algorithm_auth; if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) - || (alg_a & SSL_aECDSA) - || c->min_tls >= TLS1_3_VERSION) { - using_ecc = 1; + || (alg_a & SSL_aECDSA) + || c->min_tls >= TLS1_3_VERSION) break; - } } - return using_ecc; + return i < end; } -int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) { const unsigned char *pformats; size_t num_formats; @@ -118,7 +114,6 @@ int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) return 1; /* Add TLS extension ECPointFormats to the ClientHello message */ - tls1_get_formatlist(s, &pformats, &num_formats); if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) @@ -134,7 +129,7 @@ int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) } -int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt, int *al) { const unsigned char *pcurves = NULL, *pcurvestmp; size_t num_curves = 0, i; @@ -183,7 +178,7 @@ int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_client_session_ticket(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt, int *al) { size_t ticklen; @@ -222,7 +217,7 @@ int tls_construct_client_session_ticket(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, int *al) { size_t salglen; const unsigned char *salg; @@ -231,7 +226,6 @@ int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al) return 1; salglen = tls12_get_psigalgs(s, &salg); - if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) /* Sub-packet for sig-algs extension */ || !WPACKET_start_sub_packet_u16(pkt) @@ -248,7 +242,7 @@ int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_OCSP -int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt, int *al) { int i; @@ -266,11 +260,9 @@ int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al) } for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { unsigned char *idbytes; - int idlen; - OCSP_RESPID *id; + OCSP_RESPID *id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); + int idlen = i2d_OCSP_RESPID(id, NULL); - id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); - idlen = i2d_OCSP_RESPID(id, NULL); if (idlen <= 0 /* Sub-packet for an individual id */ || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) @@ -312,7 +304,7 @@ int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al) #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_construct_client_npn(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, int *al) { if (s->ctx->next_proto_select_cb == NULL || s->s3->tmp.finish_md_len != 0) return 1; @@ -331,7 +323,7 @@ int tls_construct_client_npn(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_client_alpn(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, int *al) { s->s3->alpn_sent = 0; @@ -359,11 +351,10 @@ int tls_construct_client_alpn(SSL *s, WPACKET *pkt, int *al) #ifndef OPENSSL_NO_SRTP -int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, int *al) { STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s); - SRTP_PROTECTION_PROFILE *prof; - int i, ct; + int i, end; if (clnt == NULL) return 1; @@ -376,9 +367,12 @@ int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al) SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_USE_SRTP, ERR_R_INTERNAL_ERROR); return 0; } - ct = sk_SRTP_PROTECTION_PROFILE_num(clnt); - for (i = 0; i < ct; i++) { - prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); + + end = sk_SRTP_PROTECTION_PROFILE_num(clnt); + for (i = 0; i < end; i++) { + const SRTP_PROTECTION_PROFILE *prof = + sk_SRTP_PROTECTION_PROFILE_value(clnt, i); + if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_USE_SRTP, ERR_R_INTERNAL_ERROR); return 0; @@ -396,7 +390,7 @@ int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_client_etm(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, int *al) { if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) return 1; @@ -411,7 +405,7 @@ int tls_construct_client_etm(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_CT -int tls_construct_client_sct(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, int *al) { if (s->ct_validation_callback == NULL) return 1; @@ -426,7 +420,7 @@ int tls_construct_client_sct(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_client_ems(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, int *al) { if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) || !WPACKET_put_bytes_u16(pkt, 0)) { @@ -437,7 +431,7 @@ int tls_construct_client_ems(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_client_supported_versions(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt, int *al) { int currv, min_version, max_version, reason; @@ -484,7 +478,7 @@ int tls_construct_client_supported_versions(SSL *s, WPACKET *pkt, int *al) } -int tls_construct_client_key_share(SSL *s, WPACKET *pkt, int *al) +int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, int *al) { size_t i, sharessent = 0, num_curves = 0; const unsigned char *pcurves = NULL; @@ -569,7 +563,10 @@ int tls_construct_client_key_share(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al) +#define F5_WORKAROUND_MIN_MSG_LEN 0xff +#define F5_WORKAROUND_MAX_MSG_LEN 0x200 + +int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, int *al) { unsigned char *padbytes; size_t hlen; @@ -580,7 +577,7 @@ int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al) /* * Add padding to workaround bugs in F5 terminators. See * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this - * code works out the length of all existing extensions it MUST always + * code calculates the length of all existing extensions it MUST always * appear last. */ if (!WPACKET_get_total_written(pkt, &hlen)) { @@ -588,8 +585,14 @@ int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al) return 0; } - if (hlen > 0xff && hlen < 0x200) { - hlen = 0x200 - hlen; + if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) { + /* Calculate the amond of padding we need to add */ + hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen; + + /* + * Take off the size of extension header itself (2 bytes for type and + * 2 bytes for length bytes) + */ if (hlen >= 4) hlen -= 4; else @@ -609,7 +612,7 @@ int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al) /* * Parse the server's renegotiation binding and abort if it's not right */ -int tls_parse_server_renegotiate(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, int *al) { size_t expected_len = s->s3->previous_client_finished_len + s->s3->previous_server_finished_len; @@ -666,7 +669,7 @@ int tls_parse_server_renegotiate(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_server_server_name(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, int *al) { if (s->tlsext_hostname == NULL || PACKET_remaining(pkt) > 0) { *al = SSL_AD_UNRECOGNIZED_NAME; @@ -689,7 +692,7 @@ int tls_parse_server_server_name(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_EC -int tls_parse_server_ec_pt_formats(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, int *al) { unsigned int ecpointformatlist_length; PACKET ecptformatlist; @@ -724,26 +727,28 @@ int tls_parse_server_ec_pt_formats(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_server_session_ticket(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, int *al) { - if (s->tls_session_ticket_ext_cb && + if (s->tls_session_ticket_ext_cb != NULL && !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt), PACKET_remaining(pkt), s->tls_session_ticket_ext_cb_arg)) { *al = SSL_AD_INTERNAL_ERROR; return 0; } + if (!tls_use_ticket(s) || PACKET_remaining(pkt) > 0) { *al = SSL_AD_UNSUPPORTED_EXTENSION; return 0; } + s->tlsext_ticket_expected = 1; return 1; } #ifndef OPENSSL_NO_OCSP -int tls_parse_server_status_request(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, int *al) { /* * MUST be empty and only sent if we've requested a status @@ -762,7 +767,7 @@ int tls_parse_server_status_request(SSL *s, PACKET *pkt, int *al) #ifndef OPENSSL_NO_CT -int tls_parse_server_sct(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_sct(SSL *s, PACKET *pkt, int *al) { /* * Only take it if we asked for it - i.e if there is no CT validation @@ -773,10 +778,9 @@ int tls_parse_server_sct(SSL *s, PACKET *pkt, int *al) size_t size = PACKET_remaining(pkt); /* Simply copy it off for later processing */ - if (s->tlsext_scts != NULL) { - OPENSSL_free(s->tlsext_scts); - s->tlsext_scts = NULL; - } + OPENSSL_free(s->tlsext_scts); + s->tlsext_scts = NULL; + s->tlsext_scts_len = size; if (size > 0) { s->tlsext_scts = OPENSSL_malloc(size); @@ -816,12 +820,13 @@ static int ssl_next_proto_validate(PACKET *pkt) return 1; } -int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_npn(SSL *s, PACKET *pkt, int *al) { unsigned char *selected; unsigned char selected_len; PACKET tmppkt; + /* Check if we are in a renegotiation. If so ignore this extension */ if (s->s3->tmp.finish_md_len != 0) return 1; @@ -830,6 +835,7 @@ int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al) *al = SSL_AD_UNSUPPORTED_EXTENSION; return 0; } + /* The data must be valid */ tmppkt = *pkt; if (!ssl_next_proto_validate(&tmppkt)) { @@ -844,6 +850,7 @@ int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al) *al = SSL_AD_INTERNAL_ERROR; return 0; } + /* * Could be non-NULL if server has sent multiple NPN extensions in * a single Serverhello @@ -863,7 +870,7 @@ int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_server_alpn(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, int *al) { size_t len; @@ -900,17 +907,17 @@ int tls_parse_server_alpn(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRTP -int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, int *al) { unsigned int id, ct, mki; int i; STACK_OF(SRTP_PROTECTION_PROFILE) *clnt; SRTP_PROTECTION_PROFILE *prof; - if (!PACKET_get_net_2(pkt, &ct) - || ct != 2 || !PACKET_get_net_2(pkt, &id) - || !PACKET_get_1(pkt, &mki) - || PACKET_remaining(pkt) != 0) { + if (!PACKET_get_net_2(pkt, &ct) || ct != 2 + || !PACKET_get_net_2(pkt, &id) + || !PACKET_get_1(pkt, &mki) + || PACKET_remaining(pkt) != 0) { SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); *al = SSL_AD_DECODE_ERROR; @@ -924,9 +931,8 @@ int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al) return 0; } - clnt = SSL_get_srtp_profiles(s); - /* Throw an error if the server gave us an unsolicited extension */ + clnt = SSL_get_srtp_profiles(s); if (clnt == NULL) { SSLerr(SSL_F_TLS_PARSE_SERVER_USE_SRTP, SSL_R_NO_SRTP_PROFILES); *al = SSL_AD_DECODE_ERROR; @@ -954,7 +960,7 @@ int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_server_etm(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_etm(SSL *s, PACKET *pkt, int *al) { /* Ignore if inappropriate ciphersuite */ if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) @@ -965,7 +971,7 @@ int tls_parse_server_etm(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_server_ems(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_ems(SSL *s, PACKET *pkt, int *al) { s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; if (!s->hit) @@ -974,7 +980,7 @@ int tls_parse_server_ems(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_server_key_share(SSL *s, PACKET *pkt, int *al) +int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, int *al) { unsigned int group_id; PACKET encoded_pt; diff --git a/ssl/statem/extensions_srvr.c b/ssl/statem/extensions_srvr.c index 370d0b9b02..9763c47ed3 100644 --- a/ssl/statem/extensions_srvr.c +++ b/ssl/statem/extensions_srvr.c @@ -14,7 +14,7 @@ /* * Parse the client's renegotiation binding and abort if it's not right */ -int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, int *al) { unsigned int ilen; const unsigned char *data; @@ -49,35 +49,34 @@ int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) +/*- + * The servername extension is treated as follows: + * + * - Only the hostname type is supported with a maximum length of 255. + * - The servername is rejected if too long or if it contains zeros, + * in which case an fatal alert is generated. + * - The servername field is maintained together with the session cache. + * - When a session is resumed, the servername call back invoked in order + * to allow the application to position itself to the right context. + * - The servername is acknowledged if it is new for a session or when + * it is identical to a previously used for the same session. + * Applications can control the behaviour. They can at any time + * set a 'desirable' servername for a new SSL object. This can be the + * case for example with HTTPS when a Host: header field is received and + * a renegotiation is requested. In this case, a possible servername + * presented in the new client hello is only acknowledged if it matches + * the value of the Host: field. + * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION + * if they provide for changing an explicit servername context for the + * session, i.e. when the session has been established with a servername + * extension. + * - On session reconnect, the servername extension may be absent. + */ +int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, int *al) { unsigned int servname_type; PACKET sni, hostname; - /*- - * The servername extension is treated as follows: - * - * - Only the hostname type is supported with a maximum length of 255. - * - The servername is rejected if too long or if it contains zeros, - * in which case an fatal alert is generated. - * - The servername field is maintained together with the session cache. - * - When a session is resumed, the servername call back invoked in order - * to allow the application to position itself to the right context. - * - The servername is acknowledged if it is new for a session or when - * it is identical to a previously used for the same session. - * Applications can control the behaviour. They can at any time - * set a 'desirable' servername for a new SSL object. This can be the - * case for example with HTTPS when a Host: header field is received and - * a renegotiation is requested. In this case, a possible servername - * presented in the new client hello is only acknowledged if it matches - * the value of the Host: field. - * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION - * if they provide for changing an explicit servername context for the - * session, i.e. when the session has been established with a servername - * extension. - * - On session reconnect, the servername extension may be absent. - * - */ if (!PACKET_as_length_prefixed_2(pkt, &sni) /* ServerNameList must be at least 1 byte long. */ || PACKET_remaining(&sni) == 0) { @@ -88,7 +87,7 @@ int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) /* * Although the server_name extension was intended to be * extensible to new name types, RFC 4366 defined the - * syntax inextensibility and OpenSSL 1.0.x parses it as + * syntax inextensibly and OpenSSL 1.0.x parses it as * such. * RFC 6066 corrected the mistake but adding new name types * is nevertheless no longer feasible, so act as if no other @@ -135,7 +134,7 @@ int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRP -int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_srp(SSL *s, PACKET *pkt, int *al) { PACKET srp_I; @@ -159,7 +158,7 @@ int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al) #endif #ifndef OPENSSL_NO_EC -int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, int *al) { PACKET ec_point_format_list; @@ -182,7 +181,7 @@ int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al) } #endif /* OPENSSL_NO_EC */ -int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, int *al) { if (s->tls_session_ticket_ext_cb && !s->tls_session_ticket_ext_cb(s, PACKET_data(pkt), @@ -195,7 +194,7 @@ int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al) return 1; } -int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, int *al) { PACKET supported_sig_algs; @@ -216,93 +215,93 @@ int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_OCSP -int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, int *al) { + PACKET responder_id_list, exts; + if (!PACKET_get_1(pkt, (unsigned int *)&s->tlsext_status_type)) { *al = SSL_AD_DECODE_ERROR; return 0; } - if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { - const unsigned char *ext_data; - PACKET responder_id_list, exts; - if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } - + if (s->tlsext_status_type != TLSEXT_STATUSTYPE_ocsp) { /* - * We remove any OCSP_RESPIDs from a previous handshake - * to prevent unbounded memory growth - CVE-2016-6304 + * We don't know what to do with any other type so ignore it. */ - sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); - if (PACKET_remaining(&responder_id_list) > 0) { - s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null(); - if (s->tlsext_ocsp_ids == NULL) { - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } - } else { - s->tlsext_ocsp_ids = NULL; - } + s->tlsext_status_type = -1; + return 1; + } - while (PACKET_remaining(&responder_id_list) > 0) { - OCSP_RESPID *id; - PACKET responder_id; - const unsigned char *id_data; + if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } - if (!PACKET_get_length_prefixed_2(&responder_id_list, - &responder_id) - || PACKET_remaining(&responder_id) == 0) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + /* + * We remove any OCSP_RESPIDs from a previous handshake + * to prevent unbounded memory growth - CVE-2016-6304 + */ + sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); + if (PACKET_remaining(&responder_id_list) > 0) { + s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null(); + if (s->tlsext_ocsp_ids == NULL) { + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + } else { + s->tlsext_ocsp_ids = NULL; + } - id_data = PACKET_data(&responder_id); - /* TODO(size_t): Convert d2i_* to size_t */ - id = d2i_OCSP_RESPID(NULL, &id_data, - (int)PACKET_remaining(&responder_id)); - if (id == NULL) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + while (PACKET_remaining(&responder_id_list) > 0) { + OCSP_RESPID *id; + PACKET responder_id; + const unsigned char *id_data; - if (id_data != PACKET_end(&responder_id)) { - OCSP_RESPID_free(id); - *al = SSL_AD_DECODE_ERROR; - return 0; - } + if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id) + || PACKET_remaining(&responder_id) == 0) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } - if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) { - OCSP_RESPID_free(id); - *al = SSL_AD_INTERNAL_ERROR; - return 0; - } + id_data = PACKET_data(&responder_id); + /* TODO(size_t): Convert d2i_* to size_t */ + id = d2i_OCSP_RESPID(NULL, &id_data, + (int)PACKET_remaining(&responder_id)); + if (id == NULL) { + *al = SSL_AD_DECODE_ERROR; + return 0; } - /* Read in request_extensions */ - if (!PACKET_as_length_prefixed_2(pkt, &exts)) { + if (id_data != PACKET_end(&responder_id)) { + OCSP_RESPID_free(id); *al = SSL_AD_DECODE_ERROR; return 0; } - if (PACKET_remaining(&exts) > 0) { - ext_data = PACKET_data(&exts); - sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, - X509_EXTENSION_free); - s->tlsext_ocsp_exts = - d2i_X509_EXTENSIONS(NULL, &ext_data, - (int)PACKET_remaining(&exts)); - if (s->tlsext_ocsp_exts == NULL || ext_data != PACKET_end(&exts)) { - *al = SSL_AD_DECODE_ERROR; - return 0; - } + if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) { + OCSP_RESPID_free(id); + *al = SSL_AD_INTERNAL_ERROR; + return 0; + } + } + + /* Read in request_extensions */ + if (!PACKET_as_length_prefixed_2(pkt, &exts)) { + *al = SSL_AD_DECODE_ERROR; + return 0; + } + + if (PACKET_remaining(&exts) > 0) { + const unsigned char *ext_data = PACKET_data(&exts); + + sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, + X509_EXTENSION_free); + s->tlsext_ocsp_exts = + d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts)); + if (s->tlsext_ocsp_exts == NULL || ext_data != PACKET_end(&exts)) { + *al = SSL_AD_DECODE_ERROR; + return 0; } - } else { - /* - * We don't know what to do with any other type so ignore it. - */ - s->tlsext_status_type = -1; } return 1; @@ -310,40 +309,38 @@ int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al) #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_parse_client_npn(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_npn(SSL *s, PACKET *pkt, int *al) { - if (s->s3->tmp.finish_md_len == 0) { - /*- - * We shouldn't accept this extension on a - * renegotiation. - * - * s->new_session will be set on renegotiation, but we - * probably shouldn't rely that it couldn't be set on - * the initial renegotiation too in certain cases (when - * there's some other reason to disallow resuming an - * earlier session -- the current code won't be doing - * anything like that, but this might change). - * - * A valid sign that there's been a previous handshake - * in this connection is if s->s3->tmp.finish_md_len > - * 0. (We are talking about a check that will happen - * in the Hello protocol round, well before a new - * Finished message could have been computed.) - */ + /* + * We shouldn't accept this extension on a + * renegotiation. + * + * s->new_session will be set on renegotiation, but we + * probably shouldn't rely that it couldn't be set on + * the initial renegotiation too in certain cases (when + * there's some other reason to disallow resuming an + * earlier session -- the current code won't be doing + * anything like that, but this might change). + * + * A valid sign that there's been a previous handshake + * in this connection is if s->s3->tmp.finish_md_len > + * 0. (We are talking about a check that will happen + * in the Hello protocol round, well before a new + * Finished message could have been computed.) + */ + if (s->s3->tmp.finish_md_len == 0) s->s3->next_proto_neg_seen = 1; - } return 1; } #endif /* - * Save the ALPN extension in a ClientHello. - * pkt: the contents of the ALPN extension, not including type and length. - * al: a pointer to the alert value to send in the event of a failure. - * returns: 1 on success, 0 on error. + * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN + * extension, not including type and length. |al| is a pointer to the alert + * value to send in the event of a failure. Returns: 1 on success, 0 on error. */ -int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, int *al) { PACKET protocol_list, save_protocol_list, protocol; @@ -376,9 +373,8 @@ int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRTP -int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, int *al) { - SRTP_PROTECTION_PROFILE *sprof; STACK_OF(SRTP_PROTECTION_PROFILE) *srvr; unsigned int ct, mki_len, id; int i, srtp_pref; @@ -389,8 +385,8 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) return 1; /* Pull off the length of the cipher suite list and check it is even */ - if (!PACKET_get_net_2(pkt, &ct) - || (ct & 1) != 0 || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { + if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0 + || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); *al = SSL_AD_DECODE_ERROR; @@ -417,7 +413,9 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) * does nothing. */ for (i = 0; i < srtp_pref; i++) { - sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i); + const SRTP_PROTECTION_PROFILE *sprof = + sk_SRTP_PROTECTION_PROFILE_value(srvr, i); + if (sprof->id == id) { s->srtp_profile = sprof; srtp_pref = i; @@ -426,9 +424,7 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) } } - /* - * Now extract the MKI value as a sanity check, but discard it for now - */ + /* Now extract the MKI value as a sanity check, but discard it for now */ if (!PACKET_get_1(pkt, &mki_len)) { SSLerr(SSL_F_TLS_PARSE_CLIENT_USE_SRTP, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); @@ -447,7 +443,7 @@ int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_client_etm(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_etm(SSL *s, PACKET *pkt, int *al) { if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; @@ -474,8 +470,8 @@ static int check_in_list(SSL *s, unsigned int group_id, unsigned int share_id = (groups[0] << 8) | (groups[1]); if (group_id == share_id - && (!checkallow || tls_curve_allowed(s, groups, - SSL_SECOP_CURVE_CHECK))) { + && (!checkallow + || tls_curve_allowed(s, groups, SSL_SECOP_CURVE_CHECK))) { break; } } @@ -489,7 +485,7 @@ static int check_in_list(SSL *s, unsigned int group_id, * the raw PACKET data for the extension. Returns 1 on success or 0 on failure. * If a failure occurs then |*al| is set to an appropriate alert value. */ -int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, int *al) { unsigned int group_id; PACKET key_share_list, encoded_pt; @@ -585,6 +581,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) } else { /* Set up EVP_PKEY with named curve as parameters */ EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); + if (pctx == NULL || EVP_PKEY_paramgen_init(pctx) <= 0 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, @@ -615,7 +612,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al) } #ifndef OPENSSL_NO_EC -int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, int *al) { PACKET supported_groups_list; @@ -639,7 +636,7 @@ int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al) } #endif -int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al) +int tls_parse_ctos_ems(SSL *s, PACKET *pkt, int *al) { /* The extension must always be empty */ if (PACKET_remaining(pkt) != 0) { @@ -652,8 +649,10 @@ int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al) return 1; } -/* Add the server's renegotiation binding */ -int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al) +/* + * Add the server's renegotiation binding + */ +int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, int *al) { if (!s->s3->send_connection_binding) return 1; @@ -674,7 +673,7 @@ int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, int *al) { if (s->hit || s->servername_done != 1 || s->session->tlsext_hostname == NULL) @@ -690,7 +689,7 @@ int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_EC -int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) { unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; @@ -703,7 +702,6 @@ int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) return 1; tls1_get_formatlist(s, &plist, &plistlen); - if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) || !WPACKET_start_sub_packet_u16(pkt) || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen) @@ -716,7 +714,7 @@ int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt, int *al) { if (!s->tlsext_ticket_expected || !tls_use_ticket(s)) { s->tlsext_ticket_expected = 0; @@ -733,7 +731,7 @@ int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_OCSP -int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt, int *al) { if (!s->tlsext_status_expected) return 1; @@ -750,7 +748,7 @@ int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al) #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_construct_server_next_proto_neg(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt, int *al) { const unsigned char *npa; unsigned int npalen; @@ -777,7 +775,7 @@ int tls_construct_server_next_proto_neg(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, int *al) { if (s->s3->alpn_selected == NULL) return 1; @@ -798,7 +796,7 @@ int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al) } #ifndef OPENSSL_NO_SRTP -int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, int *al) { if (s->srtp_profile == NULL) return 1; @@ -817,7 +815,7 @@ int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al) } #endif -int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, int *al) { if ((s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) == 0) return 1; @@ -843,7 +841,7 @@ int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_server_ems(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, int *al) { if ((s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0) return 1; @@ -857,7 +855,7 @@ int tls_construct_server_ems(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, int *al) { unsigned char *encodedPoint; size_t encoded_pt_len = 0; @@ -911,7 +909,7 @@ int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al) return 1; } -int tls_construct_server_cryptopro_bug(SSL *s, WPACKET *pkt, int *al) +int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, int *al) { const unsigned char cryptopro_ext[36] = { 0xfd, 0xe8, /* 65000 */ diff --git a/ssl/statem/statem_clnt.c b/ssl/statem/statem_clnt.c index 38b8ecb290..7017615af3 100644 --- a/ssl/statem/statem_clnt.c +++ b/ssl/statem/statem_clnt.c @@ -1132,7 +1132,7 @@ MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt) * server wants to resume. */ if (s->version >= TLS1_VERSION && !SSL_IS_TLS13(s) - && s->tls_session_secret_cb && s->session->tlsext_tick) { + && s->tls_session_secret_cb != NULL && s->session->tlsext_tick) { const SSL_CIPHER *pref_cipher = NULL; /* * s->session->master_key_length is a size_t, but this is an int for @@ -3112,10 +3112,11 @@ static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL *s, PACKET *pkt) */ if (!tls_collect_extensions(s, &extensions, EXT_TLS1_3_ENCRYPTED_EXTENSIONS - | EXT_TLS1_3_CERTIFICATE, &rawexts, &al) + | EXT_TLS1_3_CERTIFICATE, + &rawexts, &al) || !tls_parse_all_extensions(s, EXT_TLS1_3_ENCRYPTED_EXTENSIONS - | EXT_TLS1_3_CERTIFICATE, + | EXT_TLS1_3_CERTIFICATE, rawexts, &al)) goto err; diff --git a/ssl/statem/statem_locl.h b/ssl/statem/statem_locl.h index b0c77ce640..9d9c1a92b7 100644 --- a/ssl/statem/statem_locl.h +++ b/ssl/statem/statem_locl.h @@ -163,106 +163,106 @@ __owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context, int *al); /* Server Extension processing */ -int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al); -int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_SRP -int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_srp(SSL *s, PACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_EC -int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al); -int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al); -int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_OCSP -int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_parse_client_npn(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_npn(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_SRTP -int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_client_etm(SSL *s, PACKET *pkt, int *al); -int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al); -int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_etm(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, int *al); +int tls_parse_ctos_ems(SSL *s, PACKET *pkt, int *al); -int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al); -int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_EC -int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_OCSP -int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_construct_server_next_proto_neg(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_SRTP -int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al); -int tls_construct_server_ems(SSL *s, WPACKET *pkt, int *al); -int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, int *al); /* * Not in public headers as this is not an official extension. Only used when * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. */ #define TLSEXT_TYPE_cryptopro_bug 0xfde8 -int tls_construct_server_cryptopro_bug(SSL *s, WPACKET *pkt, int *al); +int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, int *al); /* Client Extension processing */ -int tls_construct_client_renegotiate(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_server_name(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_SRP -int tls_construct_client_srp(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_EC -int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_client_session_ticket(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_OCSP -int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_construct_client_npn(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_client_alpn(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_SRTP -int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_client_etm(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, int *al); #ifndef OPENSSL_NO_CT -int tls_construct_client_sct(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, int *al); #endif -int tls_construct_client_ems(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_supported_versions(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_key_share(SSL *s, WPACKET *pkt, int *al); -int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al); -int tls_parse_server_renegotiate(SSL *s, PACKET *pkt, int *al); -int tls_parse_server_server_name(SSL *s, PACKET *pkt, int *al); +int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, int *al); +int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, int *al); +int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_EC -int tls_parse_server_ec_pt_formats(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_server_session_ticket(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_OCSP -int tls_parse_server_status_request(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_CT -int tls_parse_server_sct(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_sct(SSL *s, PACKET *pkt, int *al); #endif #ifndef OPENSSL_NO_NEXTPROTONEG -int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_npn(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_server_alpn(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, int *al); #ifndef OPENSSL_NO_SRTP -int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, int *al); #endif -int tls_parse_server_etm(SSL *s, PACKET *pkt, int *al); -int tls_parse_server_ems(SSL *s, PACKET *pkt, int *al); -int tls_parse_server_key_share(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_etm(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_ems(SSL *s, PACKET *pkt, int *al); +int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, int *al); diff --git a/ssl/statem/statem_srvr.c b/ssl/statem/statem_srvr.c index d40141d516..12d1670278 100644 --- a/ssl/statem/statem_srvr.c +++ b/ssl/statem/statem_srvr.c @@ -1078,10 +1078,6 @@ int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt) */ static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello) { - unsigned int type; - PACKET sni, tmppkt; - size_t ext_len; - static const unsigned char kSafariExtensionsBlock[] = { 0x00, 0x0a, /* elliptic_curves extension */ 0x00, 0x08, /* 8 bytes */ @@ -1104,9 +1100,11 @@ static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello) 0x04, 0x03, /* SHA-256/ECDSA */ 0x02, 0x03, /* SHA-1/ECDSA */ }; - /* Length of the common prefix (first two extensions). */ static const size_t kSafariCommonExtensionsLength = 18; + unsigned int type; + PACKET sni, tmppkt; + size_t ext_len; tmppkt = hello->extensions; @@ -1694,7 +1692,7 @@ MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt) /* * Call the status request callback if needed. Upon success, returns 1. - * Upon failure, returns 0 and sets |al| to the appropriate fatal alert. + * Upon failure, returns 0 and sets |*al| to the appropriate fatal alert. */ static int tls_handle_status_request(SSL *s, int *al) { @@ -1706,10 +1704,11 @@ static int tls_handle_status_request(SSL *s, int *al) * and must be called after the cipher has been chosen because this may * influence which certificate is sent */ - if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { + if (s->tlsext_status_type != -1 && s->ctx != NULL + && s->ctx->tlsext_status_cb != NULL) { int ret; - CERT_PKEY *certpkey; - certpkey = ssl_get_server_send_pkey(s); + CERT_PKEY *certpkey = ssl_get_server_send_pkey(s); + /* If no certificate can't return certificate status */ if (certpkey != NULL) { /* @@ -1912,8 +1911,8 @@ int tls_construct_server_hello(SSL *s, WPACKET *pkt) && !WPACKET_put_bytes_u8(pkt, compm)) || !tls_construct_extensions(s, pkt, SSL_IS_TLS13(s) - ? EXT_TLS1_3_SERVER_HELLO - : EXT_TLS1_2_SERVER_HELLO, &al)) { + ? EXT_TLS1_3_SERVER_HELLO + : EXT_TLS1_2_SERVER_HELLO, &al)) { SSLerr(SSL_F_TLS_CONSTRUCT_SERVER_HELLO, ERR_R_INTERNAL_ERROR); goto err; } diff --git a/ssl/t1_trce.c b/ssl/t1_trce.c index 948931d2bf..796759e55c 100644 --- a/ssl/t1_trce.c +++ b/ssl/t1_trce.c @@ -593,6 +593,7 @@ static int ssl_print_version(BIO *bio, int indent, const char *name, unsigned int *version) { int vers; + if (*pmsglen < 2) return 0; vers = ((*pmsg)[0] << 8) | (*pmsg)[1]; @@ -867,6 +868,7 @@ static int ssl_print_server_hello(BIO *bio, int indent, { unsigned int cs; unsigned int vers; + if (!ssl_print_version(bio, indent, "server_version", &msg, &msglen, &vers)) return 0; if (!ssl_print_random(bio, indent, &msg, &msglen))