Commit 70af3d8e authored by Matt Caswell's avatar Matt Caswell
Browse files

Avoid repeatedly scanning the list of extensions

Because extensions were keyed by type which is sparse, we were continually
scanning the list to find the one we wanted. The way we stored them also
had the side effect that we were running initialisers/finalisers in a
different oder to the parsers. In this commit we change things so that we
instead key on an index value for each extension.

Perl changes reviewed by Richard Levitte. Non-perl changes reviewed by Rich
Salz

Reviewed-by: default avatarRich Salz <rsalz@openssl.org>
Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent 24b8e4b2
Loading
Loading
Loading
Loading
+27 −3
Changes for ssl/ssl_locl.h: 27 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -1636,14 +1636,39 @@ typedef struct ssl3_comp_st {
# endif

typedef struct raw_extension_st {
    /* The type of the extension */
    unsigned int type;
    /* Raw packet data for the extension */
    PACKET data;
    /* Set to 1 if the extension is present or 0 otherwise */
    int present;
    /* Set to 1 if we have already parsed the extension or 0 otherwise */
    int parsed;
    /* The type of this extension */
    unsigned int type;
} RAW_EXTENSION;

/*
 * Extension index values NOTE: Any updates to these defines should be mirrored
 * with equivalent updates to ext_defs in extensions.c
 */
#define TLSEXT_IDX_renegotiate                                   0
#define TLSEXT_IDX_server_name                                   1
#define TLSEXT_IDX_srp                                           2
#define TLSEXT_IDX_ec_point_formats                              3
#define TLSEXT_IDX_supported_groups                              4
#define TLSEXT_IDX_session_ticket                                5
#define TLSEXT_IDX_signature_algorithms                          6
#define TLSEXT_IDX_status_request                                7
#define TLSEXT_IDX_next_proto_neg                                8
#define TLSEXT_IDX_application_layer_protocol_negotiation        9
#define TLSEXT_IDX_use_srtp                                     10
#define TLSEXT_IDX_encrypt_then_mac                             11
#define TLSEXT_IDX_signed_certificate_timestamp                 12
#define TLSEXT_IDX_extended_master_secret                       13
#define TLSEXT_IDX_supported_versions                           14
#define TLSEXT_IDX_key_share                                    15
#define TLSEXT_IDX_cryptopro_bug                                16
#define TLSEXT_IDX_padding                                      17

#define MAX_COMPRESSIONS_SIZE   255

typedef struct {
@@ -1658,7 +1683,6 @@ typedef struct {
    size_t compressions_len;
    unsigned char compressions[MAX_COMPRESSIONS_SIZE];
    PACKET extensions;
    size_t num_extensions;
    RAW_EXTENSION *pre_proc_exts;
} CLIENTHELLO_MSG;

+176 −187

File changed.

Preview size limit exceeded, changes collapsed.

+3 −5
Changes for ssl/statem/extensions_clnt.c: 3 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -1036,7 +1036,6 @@ int tls_parse_server_key_share(SSL *s, PACKET *pkt, int *al)

static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
{
    size_t num_extensions = 0;
    RAW_EXTENSION *extensions = NULL;
    PACKET extpkt;

@@ -1071,7 +1070,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
                                            | EXT_TLS1_3_SERVER_HELLO
                                            | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
                                            | EXT_TLS1_3_CERTIFICATE,
                                &extensions, &num_extensions, al))
                                &extensions, al))
        return 0;

    /*
@@ -1083,8 +1082,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
     */
    if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
            && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
            && tls_get_extension_by_type(extensions, num_extensions,
                                         TLSEXT_TYPE_renegotiate) == NULL) {
            && !extensions[TLSEXT_IDX_renegotiate].present) {
        *al = SSL_AD_HANDSHAKE_FAILURE;
        SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
               SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
@@ -1095,7 +1093,7 @@ static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
                                     | EXT_TLS1_3_SERVER_HELLO
                                     | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
                                     | EXT_TLS1_3_CERTIFICATE,
                                  extensions, num_extensions, al))
                                  extensions,al))
        return 0;

    if (s->hit) {
+1 −3
Changes for ssl/statem/extensions_srvr.c: 1 added line, 3 removed lines.
Original line number Diff line number Diff line
@@ -522,9 +522,7 @@ int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al)
    }

    /*
     * Get the clients list of supported curves. Note: We rely on the fact
     * extension parsing happens in order of type. supported_groups has a lower
     * type than key_share so will have been processed first.
     * Get the clients list of supported curves.
     * TODO(TLS1.3): We should validate that we actually received
     * supported_groups!
     */
+2 −4
Changes for ssl/statem/statem_lib.c: 2 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -948,11 +948,9 @@ int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello)
        break;
    }

    suppversions = tls_get_extension_by_type(hello->pre_proc_exts,
                                             hello->num_extensions,
                                             TLSEXT_TYPE_supported_versions);
    suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];

    if (suppversions != NULL && !SSL_IS_DTLS(s)) {
    if (suppversions->present && !SSL_IS_DTLS(s)) {
        unsigned int candidate_vers = 0;
        unsigned int best_vers = 0;
        const SSL_METHOD *best_method = NULL;
Loading