Home | History | Annotate | Line # | Download | only in statem
      1      1.1  christos /*
      2      1.1  christos  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
      4      1.1  christos  * Copyright 2005 Nokia. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      7      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      8      1.1  christos  * in the file LICENSE in the source distribution or at
      9      1.1  christos  * https://www.openssl.org/source/license.html
     10      1.1  christos  */
     11      1.1  christos 
     12      1.1  christos #include <stdio.h>
     13      1.1  christos #include <time.h>
     14      1.1  christos #include <assert.h>
     15      1.1  christos #include "../ssl_local.h"
     16      1.1  christos #include "statem_local.h"
     17      1.1  christos #include <openssl/buffer.h>
     18      1.1  christos #include <openssl/rand.h>
     19      1.1  christos #include <openssl/objects.h>
     20      1.1  christos #include <openssl/evp.h>
     21      1.1  christos #include <openssl/md5.h>
     22      1.1  christos #include <openssl/dh.h>
     23      1.1  christos #include <openssl/rsa.h>
     24      1.1  christos #include <openssl/bn.h>
     25      1.1  christos #include <openssl/engine.h>
     26      1.1  christos #include <openssl/trace.h>
     27      1.1  christos #include <openssl/core_names.h>
     28      1.1  christos #include <openssl/param_build.h>
     29      1.1  christos #include "internal/cryptlib.h"
     30      1.1  christos #include "internal/comp.h"
     31      1.1  christos #include "internal/ssl_unwrap.h"
     32      1.1  christos 
     33      1.1  christos static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
     34  1.1.1.2  christos     PACKET *pkt);
     35      1.1  christos static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
     36  1.1.1.2  christos     PACKET *pkt);
     37      1.1  christos 
     38      1.1  christos static ossl_inline int cert_req_allowed(SSL_CONNECTION *s);
     39      1.1  christos static int key_exchange_expected(SSL_CONNECTION *s);
     40      1.1  christos static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
     41  1.1.1.2  christos     WPACKET *pkt);
     42      1.1  christos 
     43      1.1  christos static ossl_inline int received_server_cert(SSL_CONNECTION *sc)
     44      1.1  christos {
     45      1.1  christos     return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
     46      1.1  christos }
     47      1.1  christos 
     48      1.1  christos /*
     49      1.1  christos  * Is a CertificateRequest message allowed at the moment or not?
     50      1.1  christos  *
     51      1.1  christos  *  Return values are:
     52      1.1  christos  *  1: Yes
     53      1.1  christos  *  0: No
     54      1.1  christos  */
     55      1.1  christos static ossl_inline int cert_req_allowed(SSL_CONNECTION *s)
     56      1.1  christos {
     57      1.1  christos     /* TLS does not like anon-DH with client cert */
     58      1.1  christos     if ((s->version > SSL3_VERSION
     59  1.1.1.2  christos             && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
     60      1.1  christos         || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
     61      1.1  christos         return 0;
     62      1.1  christos 
     63      1.1  christos     return 1;
     64      1.1  christos }
     65      1.1  christos 
     66      1.1  christos /*
     67      1.1  christos  * Should we expect the ServerKeyExchange message or not?
     68      1.1  christos  *
     69      1.1  christos  *  Return values are:
     70      1.1  christos  *  1: Yes
     71      1.1  christos  *  0: No
     72      1.1  christos  */
     73      1.1  christos static int key_exchange_expected(SSL_CONNECTION *s)
     74      1.1  christos {
     75      1.1  christos     long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
     76      1.1  christos 
     77      1.1  christos     /*
     78      1.1  christos      * Can't skip server key exchange if this is an ephemeral
     79      1.1  christos      * ciphersuite or for SRP
     80      1.1  christos      */
     81  1.1.1.2  christos     if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK | SSL_kSRP)) {
     82      1.1  christos         return 1;
     83      1.1  christos     }
     84      1.1  christos 
     85      1.1  christos     return 0;
     86      1.1  christos }
     87      1.1  christos 
     88      1.1  christos /*
     89      1.1  christos  * ossl_statem_client_read_transition() encapsulates the logic for the allowed
     90      1.1  christos  * handshake state transitions when a TLS1.3 client is reading messages from the
     91      1.1  christos  * server. The message type that the server has sent is provided in |mt|. The
     92      1.1  christos  * current state is in |s->statem.hand_state|.
     93      1.1  christos  *
     94      1.1  christos  * Return values are 1 for success (transition allowed) and  0 on error
     95      1.1  christos  * (transition not allowed)
     96      1.1  christos  */
     97      1.1  christos static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt)
     98      1.1  christos {
     99      1.1  christos     OSSL_STATEM *st = &s->statem;
    100      1.1  christos 
    101      1.1  christos     /*
    102      1.1  christos      * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
    103      1.1  christos      * yet negotiated TLSv1.3 at that point so that is handled by
    104      1.1  christos      * ossl_statem_client_read_transition()
    105      1.1  christos      */
    106      1.1  christos 
    107      1.1  christos     switch (st->hand_state) {
    108      1.1  christos     default:
    109      1.1  christos         break;
    110      1.1  christos 
    111      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    112      1.1  christos         /*
    113      1.1  christos          * This must a ClientHello following a HelloRetryRequest, so the only
    114      1.1  christos          * thing we can get now is a ServerHello.
    115      1.1  christos          */
    116      1.1  christos         if (mt == SSL3_MT_SERVER_HELLO) {
    117      1.1  christos             st->hand_state = TLS_ST_CR_SRVR_HELLO;
    118      1.1  christos             return 1;
    119      1.1  christos         }
    120      1.1  christos         break;
    121      1.1  christos 
    122      1.1  christos     case TLS_ST_CR_SRVR_HELLO:
    123      1.1  christos         if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
    124      1.1  christos             st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
    125      1.1  christos             return 1;
    126      1.1  christos         }
    127      1.1  christos         break;
    128      1.1  christos 
    129      1.1  christos     case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
    130      1.1  christos         if (s->hit) {
    131      1.1  christos             if (mt == SSL3_MT_FINISHED) {
    132      1.1  christos                 st->hand_state = TLS_ST_CR_FINISHED;
    133      1.1  christos                 return 1;
    134      1.1  christos             }
    135      1.1  christos         } else {
    136      1.1  christos             if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
    137      1.1  christos                 st->hand_state = TLS_ST_CR_CERT_REQ;
    138      1.1  christos                 return 1;
    139      1.1  christos             }
    140      1.1  christos             if (mt == SSL3_MT_CERTIFICATE) {
    141      1.1  christos                 st->hand_state = TLS_ST_CR_CERT;
    142      1.1  christos                 return 1;
    143      1.1  christos             }
    144      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    145      1.1  christos             if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
    146  1.1.1.2  christos                 && s->ext.compress_certificate_sent) {
    147      1.1  christos                 st->hand_state = TLS_ST_CR_COMP_CERT;
    148      1.1  christos                 return 1;
    149      1.1  christos             }
    150      1.1  christos #endif
    151      1.1  christos         }
    152      1.1  christos         break;
    153      1.1  christos 
    154      1.1  christos     case TLS_ST_CR_CERT_REQ:
    155      1.1  christos         if (mt == SSL3_MT_CERTIFICATE) {
    156      1.1  christos             st->hand_state = TLS_ST_CR_CERT;
    157      1.1  christos             return 1;
    158      1.1  christos         }
    159      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    160      1.1  christos         if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
    161  1.1.1.2  christos             && s->ext.compress_certificate_sent) {
    162      1.1  christos             st->hand_state = TLS_ST_CR_COMP_CERT;
    163      1.1  christos             return 1;
    164      1.1  christos         }
    165      1.1  christos #endif
    166      1.1  christos         break;
    167      1.1  christos 
    168      1.1  christos     case TLS_ST_CR_CERT:
    169      1.1  christos     case TLS_ST_CR_COMP_CERT:
    170      1.1  christos         if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
    171      1.1  christos             st->hand_state = TLS_ST_CR_CERT_VRFY;
    172      1.1  christos             return 1;
    173      1.1  christos         }
    174      1.1  christos         break;
    175      1.1  christos 
    176      1.1  christos     case TLS_ST_CR_CERT_VRFY:
    177      1.1  christos         if (mt == SSL3_MT_FINISHED) {
    178      1.1  christos             st->hand_state = TLS_ST_CR_FINISHED;
    179      1.1  christos             return 1;
    180      1.1  christos         }
    181      1.1  christos         break;
    182      1.1  christos 
    183      1.1  christos     case TLS_ST_OK:
    184      1.1  christos         if (mt == SSL3_MT_NEWSESSION_TICKET) {
    185      1.1  christos             st->hand_state = TLS_ST_CR_SESSION_TICKET;
    186      1.1  christos             return 1;
    187      1.1  christos         }
    188      1.1  christos         if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
    189      1.1  christos             st->hand_state = TLS_ST_CR_KEY_UPDATE;
    190      1.1  christos             return 1;
    191      1.1  christos         }
    192      1.1  christos         if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
    193      1.1  christos #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
    194      1.1  christos             /* Restore digest for PHA before adding message.*/
    195  1.1.1.2  christos #error Internal DTLS version error
    196      1.1  christos #endif
    197      1.1  christos             if (!SSL_CONNECTION_IS_DTLS(s)
    198      1.1  christos                 && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
    199      1.1  christos                 s->post_handshake_auth = SSL_PHA_REQUESTED;
    200      1.1  christos                 /*
    201      1.1  christos                  * In TLS, this is called before the message is added to the
    202      1.1  christos                  * digest. In DTLS, this is expected to be called after adding
    203      1.1  christos                  * to the digest. Either move the digest restore, or add the
    204      1.1  christos                  * message here after the swap, or do it after the clientFinished?
    205      1.1  christos                  */
    206      1.1  christos                 if (!tls13_restore_handshake_digest_for_pha(s)) {
    207      1.1  christos                     /* SSLfatal() already called */
    208      1.1  christos                     return 0;
    209      1.1  christos                 }
    210      1.1  christos                 st->hand_state = TLS_ST_CR_CERT_REQ;
    211      1.1  christos                 return 1;
    212      1.1  christos             }
    213      1.1  christos         }
    214      1.1  christos         break;
    215      1.1  christos     }
    216      1.1  christos 
    217      1.1  christos     /* No valid transition found */
    218      1.1  christos     return 0;
    219      1.1  christos }
    220      1.1  christos 
    221      1.1  christos /*
    222      1.1  christos  * ossl_statem_client_read_transition() encapsulates the logic for the allowed
    223      1.1  christos  * handshake state transitions when the client is reading messages from the
    224      1.1  christos  * server. The message type that the server has sent is provided in |mt|. The
    225      1.1  christos  * current state is in |s->statem.hand_state|.
    226      1.1  christos  *
    227      1.1  christos  * Return values are 1 for success (transition allowed) and  0 on error
    228      1.1  christos  * (transition not allowed)
    229      1.1  christos  */
    230      1.1  christos int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt)
    231      1.1  christos {
    232      1.1  christos     OSSL_STATEM *st = &s->statem;
    233      1.1  christos     int ske_expected;
    234      1.1  christos 
    235      1.1  christos     /*
    236      1.1  christos      * Note that after writing the first ClientHello we don't know what version
    237      1.1  christos      * we are going to negotiate yet, so we don't take this branch until later.
    238      1.1  christos      */
    239      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
    240      1.1  christos         if (!ossl_statem_client13_read_transition(s, mt))
    241      1.1  christos             goto err;
    242      1.1  christos         return 1;
    243      1.1  christos     }
    244      1.1  christos 
    245      1.1  christos     switch (st->hand_state) {
    246      1.1  christos     default:
    247      1.1  christos         break;
    248      1.1  christos 
    249      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    250      1.1  christos         if (mt == SSL3_MT_SERVER_HELLO) {
    251      1.1  christos             st->hand_state = TLS_ST_CR_SRVR_HELLO;
    252      1.1  christos             return 1;
    253      1.1  christos         }
    254      1.1  christos 
    255      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s)) {
    256      1.1  christos             if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
    257      1.1  christos                 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
    258      1.1  christos                 return 1;
    259      1.1  christos             }
    260      1.1  christos         }
    261      1.1  christos         break;
    262      1.1  christos 
    263      1.1  christos     case TLS_ST_EARLY_DATA:
    264      1.1  christos         /*
    265      1.1  christos          * We've not actually selected TLSv1.3 yet, but we have sent early
    266      1.1  christos          * data. The only thing allowed now is a ServerHello or a
    267      1.1  christos          * HelloRetryRequest.
    268      1.1  christos          */
    269      1.1  christos         if (mt == SSL3_MT_SERVER_HELLO) {
    270      1.1  christos             st->hand_state = TLS_ST_CR_SRVR_HELLO;
    271      1.1  christos             return 1;
    272      1.1  christos         }
    273      1.1  christos         break;
    274      1.1  christos 
    275      1.1  christos     case TLS_ST_CR_SRVR_HELLO:
    276      1.1  christos         if (s->hit) {
    277      1.1  christos             if (s->ext.ticket_expected) {
    278      1.1  christos                 if (mt == SSL3_MT_NEWSESSION_TICKET) {
    279      1.1  christos                     st->hand_state = TLS_ST_CR_SESSION_TICKET;
    280      1.1  christos                     return 1;
    281      1.1  christos                 }
    282      1.1  christos             } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
    283      1.1  christos                 st->hand_state = TLS_ST_CR_CHANGE;
    284      1.1  christos                 return 1;
    285      1.1  christos             }
    286      1.1  christos         } else {
    287      1.1  christos             if (SSL_CONNECTION_IS_DTLS(s)
    288      1.1  christos                 && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
    289      1.1  christos                 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
    290      1.1  christos                 return 1;
    291      1.1  christos             } else if (s->version >= TLS1_VERSION
    292  1.1.1.2  christos                 && s->ext.session_secret_cb != NULL
    293  1.1.1.2  christos                 && s->session->ext.tick != NULL
    294  1.1.1.2  christos                 && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
    295      1.1  christos                 /*
    296      1.1  christos                  * Normally, we can tell if the server is resuming the session
    297      1.1  christos                  * from the session ID. EAP-FAST (RFC 4851), however, relies on
    298      1.1  christos                  * the next server message after the ServerHello to determine if
    299      1.1  christos                  * the server is resuming.
    300      1.1  christos                  */
    301      1.1  christos                 s->hit = 1;
    302      1.1  christos                 st->hand_state = TLS_ST_CR_CHANGE;
    303      1.1  christos                 return 1;
    304      1.1  christos             } else if (!(s->s3.tmp.new_cipher->algorithm_auth
    305  1.1.1.2  christos                            & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
    306      1.1  christos                 if (mt == SSL3_MT_CERTIFICATE) {
    307      1.1  christos                     st->hand_state = TLS_ST_CR_CERT;
    308      1.1  christos                     return 1;
    309      1.1  christos                 }
    310      1.1  christos             } else {
    311      1.1  christos                 ske_expected = key_exchange_expected(s);
    312      1.1  christos                 /* SKE is optional for some PSK ciphersuites */
    313      1.1  christos                 if (ske_expected
    314      1.1  christos                     || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
    315      1.1  christos                         && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
    316      1.1  christos                     if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
    317      1.1  christos                         st->hand_state = TLS_ST_CR_KEY_EXCH;
    318      1.1  christos                         return 1;
    319      1.1  christos                     }
    320      1.1  christos                 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
    321  1.1.1.2  christos                     && cert_req_allowed(s)) {
    322      1.1  christos                     st->hand_state = TLS_ST_CR_CERT_REQ;
    323      1.1  christos                     return 1;
    324      1.1  christos                 } else if (mt == SSL3_MT_SERVER_DONE) {
    325      1.1  christos                     st->hand_state = TLS_ST_CR_SRVR_DONE;
    326      1.1  christos                     return 1;
    327      1.1  christos                 }
    328      1.1  christos             }
    329      1.1  christos         }
    330      1.1  christos         break;
    331      1.1  christos 
    332      1.1  christos     case TLS_ST_CR_CERT:
    333      1.1  christos     case TLS_ST_CR_COMP_CERT:
    334      1.1  christos         /*
    335      1.1  christos          * The CertificateStatus message is optional even if
    336      1.1  christos          * |ext.status_expected| is set
    337      1.1  christos          */
    338      1.1  christos         if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
    339      1.1  christos             st->hand_state = TLS_ST_CR_CERT_STATUS;
    340      1.1  christos             return 1;
    341      1.1  christos         }
    342      1.1  christos         /* Fall through */
    343      1.1  christos 
    344      1.1  christos     case TLS_ST_CR_CERT_STATUS:
    345      1.1  christos         ske_expected = key_exchange_expected(s);
    346      1.1  christos         /* SKE is optional for some PSK ciphersuites */
    347  1.1.1.2  christos         if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
    348      1.1  christos             if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
    349      1.1  christos                 st->hand_state = TLS_ST_CR_KEY_EXCH;
    350      1.1  christos                 return 1;
    351      1.1  christos             }
    352      1.1  christos             goto err;
    353      1.1  christos         }
    354      1.1  christos         /* Fall through */
    355      1.1  christos 
    356      1.1  christos     case TLS_ST_CR_KEY_EXCH:
    357      1.1  christos         if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
    358      1.1  christos             if (cert_req_allowed(s)) {
    359      1.1  christos                 st->hand_state = TLS_ST_CR_CERT_REQ;
    360      1.1  christos                 return 1;
    361      1.1  christos             }
    362      1.1  christos             goto err;
    363      1.1  christos         }
    364      1.1  christos         /* Fall through */
    365      1.1  christos 
    366      1.1  christos     case TLS_ST_CR_CERT_REQ:
    367      1.1  christos         if (mt == SSL3_MT_SERVER_DONE) {
    368      1.1  christos             st->hand_state = TLS_ST_CR_SRVR_DONE;
    369      1.1  christos             return 1;
    370      1.1  christos         }
    371      1.1  christos         break;
    372      1.1  christos 
    373      1.1  christos     case TLS_ST_CW_FINISHED:
    374      1.1  christos         if (s->ext.ticket_expected) {
    375      1.1  christos             if (mt == SSL3_MT_NEWSESSION_TICKET) {
    376      1.1  christos                 st->hand_state = TLS_ST_CR_SESSION_TICKET;
    377      1.1  christos                 return 1;
    378      1.1  christos             }
    379      1.1  christos         } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
    380      1.1  christos             st->hand_state = TLS_ST_CR_CHANGE;
    381      1.1  christos             return 1;
    382      1.1  christos         }
    383      1.1  christos         break;
    384      1.1  christos 
    385      1.1  christos     case TLS_ST_CR_SESSION_TICKET:
    386      1.1  christos         if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
    387      1.1  christos             st->hand_state = TLS_ST_CR_CHANGE;
    388      1.1  christos             return 1;
    389      1.1  christos         }
    390      1.1  christos         break;
    391      1.1  christos 
    392      1.1  christos     case TLS_ST_CR_CHANGE:
    393      1.1  christos         if (mt == SSL3_MT_FINISHED) {
    394      1.1  christos             st->hand_state = TLS_ST_CR_FINISHED;
    395      1.1  christos             return 1;
    396      1.1  christos         }
    397      1.1  christos         break;
    398      1.1  christos 
    399      1.1  christos     case TLS_ST_OK:
    400      1.1  christos         if (mt == SSL3_MT_HELLO_REQUEST) {
    401      1.1  christos             st->hand_state = TLS_ST_CR_HELLO_REQ;
    402      1.1  christos             return 1;
    403      1.1  christos         }
    404      1.1  christos         break;
    405      1.1  christos     }
    406      1.1  christos 
    407  1.1.1.2  christos err:
    408      1.1  christos     /* No valid transition found */
    409      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
    410      1.1  christos         BIO *rbio;
    411      1.1  christos 
    412      1.1  christos         /*
    413      1.1  christos          * CCS messages don't have a message sequence number so this is probably
    414      1.1  christos          * because of an out-of-order CCS. We'll just drop it.
    415      1.1  christos          */
    416      1.1  christos         s->init_num = 0;
    417      1.1  christos         s->rwstate = SSL_READING;
    418      1.1  christos         rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
    419      1.1  christos         BIO_clear_retry_flags(rbio);
    420      1.1  christos         BIO_set_retry_read(rbio);
    421      1.1  christos         return 0;
    422      1.1  christos     }
    423      1.1  christos     SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
    424      1.1  christos     return 0;
    425      1.1  christos }
    426      1.1  christos 
    427      1.1  christos static int do_compressed_cert(SSL_CONNECTION *sc)
    428      1.1  christos {
    429      1.1  christos     /* If we negotiated RPK, we won't try to compress it */
    430      1.1  christos     return sc->ext.client_cert_type == TLSEXT_cert_type_x509
    431      1.1  christos         && sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none;
    432      1.1  christos }
    433      1.1  christos 
    434      1.1  christos /*
    435      1.1  christos  * ossl_statem_client13_write_transition() works out what handshake state to
    436      1.1  christos  * move to next when the TLSv1.3 client is writing messages to be sent to the
    437      1.1  christos  * server.
    438      1.1  christos  */
    439      1.1  christos static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s)
    440      1.1  christos {
    441      1.1  christos     OSSL_STATEM *st = &s->statem;
    442      1.1  christos 
    443      1.1  christos     /*
    444      1.1  christos      * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
    445      1.1  christos      * TLSv1.3 yet at that point. They are handled by
    446      1.1  christos      * ossl_statem_client_write_transition().
    447      1.1  christos      */
    448      1.1  christos     switch (st->hand_state) {
    449      1.1  christos     default:
    450      1.1  christos         /* Shouldn't happen */
    451      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    452      1.1  christos         return WRITE_TRAN_ERROR;
    453      1.1  christos 
    454      1.1  christos     case TLS_ST_CR_CERT_REQ:
    455      1.1  christos         if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
    456      1.1  christos             if (do_compressed_cert(s))
    457      1.1  christos                 st->hand_state = TLS_ST_CW_COMP_CERT;
    458      1.1  christos             else
    459      1.1  christos                 st->hand_state = TLS_ST_CW_CERT;
    460      1.1  christos             return WRITE_TRAN_CONTINUE;
    461      1.1  christos         }
    462      1.1  christos         /*
    463      1.1  christos          * We should only get here if we received a CertificateRequest after
    464      1.1  christos          * we already sent close_notify
    465      1.1  christos          */
    466      1.1  christos         if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
    467      1.1  christos             /* Shouldn't happen - same as default case */
    468      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    469      1.1  christos             return WRITE_TRAN_ERROR;
    470      1.1  christos         }
    471      1.1  christos         st->hand_state = TLS_ST_OK;
    472      1.1  christos         return WRITE_TRAN_CONTINUE;
    473      1.1  christos 
    474      1.1  christos     case TLS_ST_CR_FINISHED:
    475      1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
    476  1.1.1.2  christos             || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
    477      1.1  christos             st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
    478      1.1  christos         else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
    479  1.1.1.2  christos             && s->hello_retry_request == SSL_HRR_NONE)
    480      1.1  christos             st->hand_state = TLS_ST_CW_CHANGE;
    481      1.1  christos         else if (s->s3.tmp.cert_req == 0)
    482      1.1  christos             st->hand_state = TLS_ST_CW_FINISHED;
    483      1.1  christos         else if (do_compressed_cert(s))
    484      1.1  christos             st->hand_state = TLS_ST_CW_COMP_CERT;
    485      1.1  christos         else
    486      1.1  christos             st->hand_state = TLS_ST_CW_CERT;
    487      1.1  christos 
    488      1.1  christos         s->ts_msg_read = ossl_time_now();
    489      1.1  christos         return WRITE_TRAN_CONTINUE;
    490      1.1  christos 
    491      1.1  christos     case TLS_ST_PENDING_EARLY_DATA_END:
    492      1.1  christos         if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED && !SSL_NO_EOED(s)) {
    493      1.1  christos             st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
    494      1.1  christos             return WRITE_TRAN_CONTINUE;
    495      1.1  christos         }
    496      1.1  christos         /* Fall through */
    497      1.1  christos 
    498      1.1  christos     case TLS_ST_CW_END_OF_EARLY_DATA:
    499      1.1  christos     case TLS_ST_CW_CHANGE:
    500      1.1  christos         if (s->s3.tmp.cert_req == 0)
    501      1.1  christos             st->hand_state = TLS_ST_CW_FINISHED;
    502      1.1  christos         else if (do_compressed_cert(s))
    503      1.1  christos             st->hand_state = TLS_ST_CW_COMP_CERT;
    504      1.1  christos         else
    505      1.1  christos             st->hand_state = TLS_ST_CW_CERT;
    506      1.1  christos         return WRITE_TRAN_CONTINUE;
    507      1.1  christos 
    508      1.1  christos     case TLS_ST_CW_COMP_CERT:
    509      1.1  christos     case TLS_ST_CW_CERT:
    510      1.1  christos         /* If a non-empty Certificate we also send CertificateVerify */
    511      1.1  christos         st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
    512  1.1.1.2  christos                                                    : TLS_ST_CW_FINISHED;
    513      1.1  christos         return WRITE_TRAN_CONTINUE;
    514      1.1  christos 
    515      1.1  christos     case TLS_ST_CW_CERT_VRFY:
    516      1.1  christos         st->hand_state = TLS_ST_CW_FINISHED;
    517      1.1  christos         return WRITE_TRAN_CONTINUE;
    518      1.1  christos 
    519      1.1  christos     case TLS_ST_CR_KEY_UPDATE:
    520      1.1  christos     case TLS_ST_CW_KEY_UPDATE:
    521      1.1  christos     case TLS_ST_CR_SESSION_TICKET:
    522      1.1  christos     case TLS_ST_CW_FINISHED:
    523      1.1  christos         st->hand_state = TLS_ST_OK;
    524      1.1  christos         return WRITE_TRAN_CONTINUE;
    525      1.1  christos 
    526      1.1  christos     case TLS_ST_OK:
    527      1.1  christos         if (s->key_update != SSL_KEY_UPDATE_NONE) {
    528      1.1  christos             st->hand_state = TLS_ST_CW_KEY_UPDATE;
    529      1.1  christos             return WRITE_TRAN_CONTINUE;
    530      1.1  christos         }
    531      1.1  christos 
    532      1.1  christos         /* Try to read from the server instead */
    533      1.1  christos         return WRITE_TRAN_FINISHED;
    534      1.1  christos     }
    535      1.1  christos }
    536      1.1  christos 
    537      1.1  christos /*
    538      1.1  christos  * ossl_statem_client_write_transition() works out what handshake state to
    539      1.1  christos  * move to next when the client is writing messages to be sent to the server.
    540      1.1  christos  */
    541      1.1  christos WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s)
    542      1.1  christos {
    543      1.1  christos     OSSL_STATEM *st = &s->statem;
    544      1.1  christos 
    545      1.1  christos     /*
    546      1.1  christos      * Note that immediately before/after a ClientHello we don't know what
    547      1.1  christos      * version we are going to negotiate yet, so we don't take this branch until
    548      1.1  christos      * later
    549      1.1  christos      */
    550      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s))
    551      1.1  christos         return ossl_statem_client13_write_transition(s);
    552      1.1  christos 
    553      1.1  christos     switch (st->hand_state) {
    554      1.1  christos     default:
    555      1.1  christos         /* Shouldn't happen */
    556      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    557      1.1  christos         return WRITE_TRAN_ERROR;
    558      1.1  christos 
    559      1.1  christos     case TLS_ST_OK:
    560      1.1  christos         if (!s->renegotiate) {
    561      1.1  christos             /*
    562      1.1  christos              * We haven't requested a renegotiation ourselves so we must have
    563      1.1  christos              * received a message from the server. Better read it.
    564      1.1  christos              */
    565      1.1  christos             return WRITE_TRAN_FINISHED;
    566      1.1  christos         }
    567      1.1  christos         /* Renegotiation */
    568      1.1  christos         /* fall thru */
    569      1.1  christos     case TLS_ST_BEFORE:
    570      1.1  christos         st->hand_state = TLS_ST_CW_CLNT_HELLO;
    571      1.1  christos         return WRITE_TRAN_CONTINUE;
    572      1.1  christos 
    573      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    574      1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
    575  1.1.1.2  christos             && !SSL_IS_QUIC_HANDSHAKE(s)) {
    576      1.1  christos             /*
    577      1.1  christos              * We are assuming this is a TLSv1.3 connection, although we haven't
    578      1.1  christos              * actually selected a version yet.
    579      1.1  christos              */
    580      1.1  christos             if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
    581      1.1  christos                 st->hand_state = TLS_ST_CW_CHANGE;
    582      1.1  christos             else
    583      1.1  christos                 st->hand_state = TLS_ST_EARLY_DATA;
    584      1.1  christos             return WRITE_TRAN_CONTINUE;
    585      1.1  christos         }
    586      1.1  christos         /*
    587      1.1  christos          * No transition at the end of writing because we don't know what
    588      1.1  christos          * we will be sent
    589      1.1  christos          */
    590      1.1  christos         s->ts_msg_write = ossl_time_now();
    591      1.1  christos         return WRITE_TRAN_FINISHED;
    592      1.1  christos 
    593      1.1  christos     case TLS_ST_CR_SRVR_HELLO:
    594      1.1  christos         /*
    595      1.1  christos          * We only get here in TLSv1.3. We just received an HRR, so issue a
    596      1.1  christos          * CCS unless middlebox compat mode is off, or we already issued one
    597      1.1  christos          * because we did early data.
    598      1.1  christos          */
    599      1.1  christos         if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
    600  1.1.1.2  christos             && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
    601      1.1  christos             st->hand_state = TLS_ST_CW_CHANGE;
    602      1.1  christos         else
    603      1.1  christos             st->hand_state = TLS_ST_CW_CLNT_HELLO;
    604      1.1  christos         return WRITE_TRAN_CONTINUE;
    605      1.1  christos 
    606      1.1  christos     case TLS_ST_EARLY_DATA:
    607      1.1  christos         s->ts_msg_write = ossl_time_now();
    608      1.1  christos         return WRITE_TRAN_FINISHED;
    609      1.1  christos 
    610      1.1  christos     case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
    611      1.1  christos         st->hand_state = TLS_ST_CW_CLNT_HELLO;
    612      1.1  christos         return WRITE_TRAN_CONTINUE;
    613      1.1  christos 
    614      1.1  christos     case TLS_ST_CR_SRVR_DONE:
    615      1.1  christos         s->ts_msg_read = ossl_time_now();
    616      1.1  christos         if (s->s3.tmp.cert_req)
    617      1.1  christos             st->hand_state = TLS_ST_CW_CERT;
    618      1.1  christos         else
    619      1.1  christos             st->hand_state = TLS_ST_CW_KEY_EXCH;
    620      1.1  christos         return WRITE_TRAN_CONTINUE;
    621      1.1  christos 
    622      1.1  christos     case TLS_ST_CW_CERT:
    623      1.1  christos         st->hand_state = TLS_ST_CW_KEY_EXCH;
    624      1.1  christos         return WRITE_TRAN_CONTINUE;
    625      1.1  christos 
    626      1.1  christos     case TLS_ST_CW_KEY_EXCH:
    627      1.1  christos         /*
    628      1.1  christos          * For TLS, cert_req is set to 2, so a cert chain of nothing is
    629      1.1  christos          * sent, but no verify packet is sent
    630      1.1  christos          */
    631      1.1  christos         /*
    632      1.1  christos          * XXX: For now, we do not support client authentication in ECDH
    633      1.1  christos          * cipher suites with ECDH (rather than ECDSA) certificates. We
    634      1.1  christos          * need to skip the certificate verify message when client's
    635      1.1  christos          * ECDH public key is sent inside the client certificate.
    636      1.1  christos          */
    637      1.1  christos         if (s->s3.tmp.cert_req == 1) {
    638      1.1  christos             st->hand_state = TLS_ST_CW_CERT_VRFY;
    639      1.1  christos         } else {
    640      1.1  christos             st->hand_state = TLS_ST_CW_CHANGE;
    641      1.1  christos         }
    642      1.1  christos         if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
    643      1.1  christos             st->hand_state = TLS_ST_CW_CHANGE;
    644      1.1  christos         }
    645      1.1  christos         return WRITE_TRAN_CONTINUE;
    646      1.1  christos 
    647      1.1  christos     case TLS_ST_CW_CERT_VRFY:
    648      1.1  christos         st->hand_state = TLS_ST_CW_CHANGE;
    649      1.1  christos         return WRITE_TRAN_CONTINUE;
    650      1.1  christos 
    651      1.1  christos     case TLS_ST_CW_CHANGE:
    652      1.1  christos         if (s->hello_retry_request == SSL_HRR_PENDING) {
    653      1.1  christos             st->hand_state = TLS_ST_CW_CLNT_HELLO;
    654      1.1  christos         } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
    655      1.1  christos             st->hand_state = TLS_ST_EARLY_DATA;
    656      1.1  christos         } else {
    657      1.1  christos #if defined(OPENSSL_NO_NEXTPROTONEG)
    658      1.1  christos             st->hand_state = TLS_ST_CW_FINISHED;
    659      1.1  christos #else
    660      1.1  christos             if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen)
    661      1.1  christos                 st->hand_state = TLS_ST_CW_NEXT_PROTO;
    662      1.1  christos             else
    663      1.1  christos                 st->hand_state = TLS_ST_CW_FINISHED;
    664      1.1  christos #endif
    665      1.1  christos         }
    666      1.1  christos         return WRITE_TRAN_CONTINUE;
    667      1.1  christos 
    668      1.1  christos #if !defined(OPENSSL_NO_NEXTPROTONEG)
    669      1.1  christos     case TLS_ST_CW_NEXT_PROTO:
    670      1.1  christos         st->hand_state = TLS_ST_CW_FINISHED;
    671      1.1  christos         return WRITE_TRAN_CONTINUE;
    672      1.1  christos #endif
    673      1.1  christos 
    674      1.1  christos     case TLS_ST_CW_FINISHED:
    675      1.1  christos         if (s->hit) {
    676      1.1  christos             st->hand_state = TLS_ST_OK;
    677      1.1  christos             return WRITE_TRAN_CONTINUE;
    678      1.1  christos         } else {
    679      1.1  christos             return WRITE_TRAN_FINISHED;
    680      1.1  christos         }
    681      1.1  christos 
    682      1.1  christos     case TLS_ST_CR_FINISHED:
    683      1.1  christos         if (s->hit) {
    684      1.1  christos             st->hand_state = TLS_ST_CW_CHANGE;
    685      1.1  christos             return WRITE_TRAN_CONTINUE;
    686      1.1  christos         } else {
    687      1.1  christos             st->hand_state = TLS_ST_OK;
    688      1.1  christos             return WRITE_TRAN_CONTINUE;
    689      1.1  christos         }
    690      1.1  christos 
    691      1.1  christos     case TLS_ST_CR_HELLO_REQ:
    692      1.1  christos         /*
    693      1.1  christos          * If we can renegotiate now then do so, otherwise wait for a more
    694      1.1  christos          * convenient time.
    695      1.1  christos          */
    696      1.1  christos         if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) {
    697      1.1  christos             if (!tls_setup_handshake(s)) {
    698      1.1  christos                 /* SSLfatal() already called */
    699      1.1  christos                 return WRITE_TRAN_ERROR;
    700      1.1  christos             }
    701      1.1  christos             st->hand_state = TLS_ST_CW_CLNT_HELLO;
    702      1.1  christos             return WRITE_TRAN_CONTINUE;
    703      1.1  christos         }
    704      1.1  christos         st->hand_state = TLS_ST_OK;
    705      1.1  christos         return WRITE_TRAN_CONTINUE;
    706      1.1  christos     }
    707      1.1  christos }
    708      1.1  christos 
    709      1.1  christos /*
    710      1.1  christos  * Perform any pre work that needs to be done prior to sending a message from
    711      1.1  christos  * the client to the server.
    712      1.1  christos  */
    713      1.1  christos WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
    714      1.1  christos {
    715      1.1  christos     OSSL_STATEM *st = &s->statem;
    716      1.1  christos 
    717      1.1  christos     switch (st->hand_state) {
    718      1.1  christos     default:
    719      1.1  christos         /* No pre work to be done */
    720      1.1  christos         break;
    721      1.1  christos 
    722      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    723      1.1  christos         s->shutdown = 0;
    724      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s)) {
    725      1.1  christos             /* every DTLS ClientHello resets Finished MAC */
    726      1.1  christos             if (!ssl3_init_finished_mac(s)) {
    727      1.1  christos                 /* SSLfatal() already called */
    728      1.1  christos                 return WORK_ERROR;
    729      1.1  christos             }
    730      1.1  christos         } else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) {
    731      1.1  christos             /*
    732      1.1  christos              * This must be a second ClientHello after an HRR following an
    733      1.1  christos              * earlier rejected attempt to send early data. Since we were
    734      1.1  christos              * previously encrypting the early data we now need to reset the
    735      1.1  christos              * write record layer in order to write in plaintext again.
    736      1.1  christos              */
    737      1.1  christos             if (!ssl_set_new_record_layer(s,
    738  1.1.1.2  christos                     TLS_ANY_VERSION,
    739  1.1.1.2  christos                     OSSL_RECORD_DIRECTION_WRITE,
    740  1.1.1.2  christos                     OSSL_RECORD_PROTECTION_LEVEL_NONE,
    741  1.1.1.2  christos                     NULL, 0, NULL, 0, NULL, 0, NULL, 0,
    742  1.1.1.2  christos                     NULL, 0, NID_undef, NULL, NULL,
    743  1.1.1.2  christos                     NULL)) {
    744      1.1  christos                 /* SSLfatal already called */
    745      1.1  christos                 return WORK_ERROR;
    746      1.1  christos             }
    747      1.1  christos         }
    748      1.1  christos         break;
    749      1.1  christos 
    750      1.1  christos     case TLS_ST_CW_CHANGE:
    751      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s)) {
    752      1.1  christos             if (s->hit) {
    753      1.1  christos                 /*
    754      1.1  christos                  * We're into the last flight so we don't retransmit these
    755      1.1  christos                  * messages unless we need to.
    756      1.1  christos                  */
    757      1.1  christos                 st->use_timer = 0;
    758      1.1  christos             }
    759      1.1  christos #ifndef OPENSSL_NO_SCTP
    760      1.1  christos             if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) {
    761      1.1  christos                 /* Calls SSLfatal() as required */
    762      1.1  christos                 return dtls_wait_for_dry(s);
    763      1.1  christos             }
    764      1.1  christos #endif
    765      1.1  christos         }
    766      1.1  christos         break;
    767      1.1  christos 
    768      1.1  christos     case TLS_ST_PENDING_EARLY_DATA_END:
    769      1.1  christos         /*
    770      1.1  christos          * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
    771      1.1  christos          * attempt to write early data before calling SSL_read() then we press
    772      1.1  christos          * on with the handshake. Otherwise we pause here.
    773      1.1  christos          */
    774      1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
    775  1.1.1.2  christos             || s->early_data_state == SSL_EARLY_DATA_NONE)
    776      1.1  christos             return WORK_FINISHED_CONTINUE;
    777      1.1  christos         /* Fall through */
    778      1.1  christos 
    779      1.1  christos     case TLS_ST_EARLY_DATA:
    780      1.1  christos         return tls_finish_handshake(s, wst, 0, 1);
    781      1.1  christos 
    782      1.1  christos     case TLS_ST_OK:
    783      1.1  christos         /* Calls SSLfatal() as required */
    784      1.1  christos         return tls_finish_handshake(s, wst, 1, 1);
    785      1.1  christos     }
    786      1.1  christos 
    787      1.1  christos     return WORK_FINISHED_CONTINUE;
    788      1.1  christos }
    789      1.1  christos 
    790      1.1  christos /*
    791      1.1  christos  * Perform any work that needs to be done after sending a message from the
    792      1.1  christos  * client to the server.
    793      1.1  christos  */
    794      1.1  christos WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst)
    795      1.1  christos {
    796      1.1  christos     OSSL_STATEM *st = &s->statem;
    797      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
    798      1.1  christos 
    799      1.1  christos     s->init_num = 0;
    800      1.1  christos 
    801      1.1  christos     switch (st->hand_state) {
    802      1.1  christos     default:
    803      1.1  christos         /* No post work to be done */
    804      1.1  christos         break;
    805      1.1  christos 
    806      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    807      1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
    808  1.1.1.2  christos             && s->max_early_data > 0) {
    809      1.1  christos             /*
    810      1.1  christos              * We haven't selected TLSv1.3 yet so we don't call the change
    811      1.1  christos              * cipher state function associated with the SSL_METHOD. Instead
    812      1.1  christos              * we call tls13_change_cipher_state() directly.
    813      1.1  christos              */
    814      1.1  christos             if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
    815      1.1  christos                 if (!tls13_change_cipher_state(s,
    816  1.1.1.2  christos                         SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
    817      1.1  christos                     /* SSLfatal() already called */
    818      1.1  christos                     return WORK_ERROR;
    819      1.1  christos                 }
    820      1.1  christos             }
    821      1.1  christos             /* else we're in compat mode so we delay flushing until after CCS */
    822      1.1  christos         } else if (!statem_flush(s)) {
    823      1.1  christos             return WORK_MORE_A;
    824      1.1  christos         }
    825      1.1  christos 
    826      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s)) {
    827      1.1  christos             /* Treat the next message as the first packet */
    828      1.1  christos             s->first_packet = 1;
    829      1.1  christos         }
    830      1.1  christos         break;
    831      1.1  christos 
    832      1.1  christos     case TLS_ST_CW_KEY_EXCH:
    833      1.1  christos         if (tls_client_key_exchange_post_work(s) == 0) {
    834      1.1  christos             /* SSLfatal() already called */
    835      1.1  christos             return WORK_ERROR;
    836      1.1  christos         }
    837      1.1  christos         break;
    838      1.1  christos 
    839      1.1  christos     case TLS_ST_CW_CHANGE:
    840      1.1  christos         if (SSL_CONNECTION_IS_TLS13(s)
    841      1.1  christos             || s->hello_retry_request == SSL_HRR_PENDING)
    842      1.1  christos             break;
    843      1.1  christos         if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
    844  1.1.1.2  christos             && s->max_early_data > 0) {
    845      1.1  christos             /*
    846      1.1  christos              * We haven't selected TLSv1.3 yet so we don't call the change
    847      1.1  christos              * cipher state function associated with the SSL_METHOD. Instead
    848      1.1  christos              * we call tls13_change_cipher_state() directly.
    849      1.1  christos              */
    850      1.1  christos             if (!tls13_change_cipher_state(s,
    851  1.1.1.2  christos                     SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
    852      1.1  christos                 return WORK_ERROR;
    853      1.1  christos             break;
    854      1.1  christos         }
    855      1.1  christos         s->session->cipher = s->s3.tmp.new_cipher;
    856      1.1  christos #ifdef OPENSSL_NO_COMP
    857      1.1  christos         s->session->compress_meth = 0;
    858      1.1  christos #else
    859      1.1  christos         if (s->s3.tmp.new_compression == NULL)
    860      1.1  christos             s->session->compress_meth = 0;
    861      1.1  christos         else
    862      1.1  christos             s->session->compress_meth = s->s3.tmp.new_compression->id;
    863      1.1  christos #endif
    864      1.1  christos         if (!ssl->method->ssl3_enc->setup_key_block(s)) {
    865      1.1  christos             /* SSLfatal() already called */
    866      1.1  christos             return WORK_ERROR;
    867      1.1  christos         }
    868      1.1  christos 
    869      1.1  christos         if (!ssl->method->ssl3_enc->change_cipher_state(s,
    870  1.1.1.2  christos                 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
    871      1.1  christos             /* SSLfatal() already called */
    872      1.1  christos             return WORK_ERROR;
    873      1.1  christos         }
    874      1.1  christos 
    875      1.1  christos #ifndef OPENSSL_NO_SCTP
    876      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
    877      1.1  christos             /*
    878  1.1.1.2  christos              * Change to new shared key of SCTP-Auth, will be ignored if
    879  1.1.1.2  christos              * no SCTP used.
    880  1.1.1.2  christos              */
    881      1.1  christos             BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
    882  1.1.1.2  christos                 0, NULL);
    883      1.1  christos         }
    884      1.1  christos #endif
    885      1.1  christos         break;
    886      1.1  christos 
    887      1.1  christos     case TLS_ST_CW_FINISHED:
    888      1.1  christos #ifndef OPENSSL_NO_SCTP
    889      1.1  christos         if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) {
    890      1.1  christos             /*
    891      1.1  christos              * Change to new shared key of SCTP-Auth, will be ignored if
    892      1.1  christos              * no SCTP used.
    893      1.1  christos              */
    894      1.1  christos             BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
    895  1.1.1.2  christos                 0, NULL);
    896      1.1  christos         }
    897      1.1  christos #endif
    898      1.1  christos         if (statem_flush(s) != 1)
    899      1.1  christos             return WORK_MORE_B;
    900      1.1  christos 
    901      1.1  christos         if (SSL_CONNECTION_IS_TLS13(s)) {
    902      1.1  christos             if (!tls13_save_handshake_digest_for_pha(s)) {
    903      1.1  christos                 /* SSLfatal() already called */
    904      1.1  christos                 return WORK_ERROR;
    905      1.1  christos             }
    906      1.1  christos             if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
    907      1.1  christos                 if (!ssl->method->ssl3_enc->change_cipher_state(s,
    908      1.1  christos                         SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
    909      1.1  christos                     /* SSLfatal() already called */
    910      1.1  christos                     return WORK_ERROR;
    911      1.1  christos                 }
    912      1.1  christos                 /*
    913      1.1  christos                  * For QUIC we deferred setting up these keys until now so
    914      1.1  christos                  * that we can ensure write keys are always set up before read
    915      1.1  christos                  * keys.
    916      1.1  christos                  */
    917      1.1  christos                 if (SSL_IS_QUIC_HANDSHAKE(s)
    918  1.1.1.2  christos                     && !ssl->method->ssl3_enc->change_cipher_state(s,
    919  1.1.1.2  christos                         SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
    920      1.1  christos                     /* SSLfatal() already called */
    921      1.1  christos                     return WORK_ERROR;
    922      1.1  christos                 }
    923      1.1  christos             }
    924      1.1  christos         }
    925      1.1  christos         break;
    926      1.1  christos 
    927      1.1  christos     case TLS_ST_CW_KEY_UPDATE:
    928      1.1  christos         if (statem_flush(s) != 1)
    929      1.1  christos             return WORK_MORE_A;
    930      1.1  christos         if (!tls13_update_key(s, 1)) {
    931      1.1  christos             /* SSLfatal() already called */
    932      1.1  christos             return WORK_ERROR;
    933      1.1  christos         }
    934      1.1  christos         break;
    935      1.1  christos     }
    936      1.1  christos 
    937      1.1  christos     return WORK_FINISHED_CONTINUE;
    938      1.1  christos }
    939      1.1  christos 
    940      1.1  christos /*
    941      1.1  christos  * Get the message construction function and message type for sending from the
    942      1.1  christos  * client
    943      1.1  christos  *
    944      1.1  christos  * Valid return values are:
    945      1.1  christos  *   1: Success
    946      1.1  christos  *   0: Error
    947      1.1  christos  */
    948      1.1  christos int ossl_statem_client_construct_message(SSL_CONNECTION *s,
    949  1.1.1.2  christos     confunc_f *confunc, int *mt)
    950      1.1  christos {
    951      1.1  christos     OSSL_STATEM *st = &s->statem;
    952      1.1  christos 
    953      1.1  christos     switch (st->hand_state) {
    954      1.1  christos     default:
    955      1.1  christos         /* Shouldn't happen */
    956      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
    957      1.1  christos         return 0;
    958      1.1  christos 
    959      1.1  christos     case TLS_ST_CW_CHANGE:
    960      1.1  christos         if (SSL_CONNECTION_IS_DTLS(s))
    961      1.1  christos             *confunc = dtls_construct_change_cipher_spec;
    962      1.1  christos         else
    963      1.1  christos             *confunc = tls_construct_change_cipher_spec;
    964      1.1  christos         *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
    965      1.1  christos         break;
    966      1.1  christos 
    967      1.1  christos     case TLS_ST_CW_CLNT_HELLO:
    968      1.1  christos         *confunc = tls_construct_client_hello;
    969      1.1  christos         *mt = SSL3_MT_CLIENT_HELLO;
    970      1.1  christos         break;
    971      1.1  christos 
    972      1.1  christos     case TLS_ST_CW_END_OF_EARLY_DATA:
    973      1.1  christos         *confunc = tls_construct_end_of_early_data;
    974      1.1  christos         *mt = SSL3_MT_END_OF_EARLY_DATA;
    975      1.1  christos         break;
    976      1.1  christos 
    977      1.1  christos     case TLS_ST_PENDING_EARLY_DATA_END:
    978      1.1  christos         *confunc = NULL;
    979      1.1  christos         *mt = SSL3_MT_DUMMY;
    980      1.1  christos         break;
    981      1.1  christos 
    982      1.1  christos     case TLS_ST_CW_CERT:
    983      1.1  christos         *confunc = tls_construct_client_certificate;
    984      1.1  christos         *mt = SSL3_MT_CERTIFICATE;
    985      1.1  christos         break;
    986      1.1  christos 
    987      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    988      1.1  christos     case TLS_ST_CW_COMP_CERT:
    989      1.1  christos         *confunc = tls_construct_client_compressed_certificate;
    990      1.1  christos         *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
    991      1.1  christos         break;
    992      1.1  christos #endif
    993      1.1  christos 
    994      1.1  christos     case TLS_ST_CW_KEY_EXCH:
    995      1.1  christos         *confunc = tls_construct_client_key_exchange;
    996      1.1  christos         *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
    997      1.1  christos         break;
    998      1.1  christos 
    999      1.1  christos     case TLS_ST_CW_CERT_VRFY:
   1000      1.1  christos         *confunc = tls_construct_cert_verify;
   1001      1.1  christos         *mt = SSL3_MT_CERTIFICATE_VERIFY;
   1002      1.1  christos         break;
   1003      1.1  christos 
   1004      1.1  christos #if !defined(OPENSSL_NO_NEXTPROTONEG)
   1005      1.1  christos     case TLS_ST_CW_NEXT_PROTO:
   1006      1.1  christos         *confunc = tls_construct_next_proto;
   1007      1.1  christos         *mt = SSL3_MT_NEXT_PROTO;
   1008      1.1  christos         break;
   1009      1.1  christos #endif
   1010      1.1  christos     case TLS_ST_CW_FINISHED:
   1011      1.1  christos         *confunc = tls_construct_finished;
   1012      1.1  christos         *mt = SSL3_MT_FINISHED;
   1013      1.1  christos         break;
   1014      1.1  christos 
   1015      1.1  christos     case TLS_ST_CW_KEY_UPDATE:
   1016      1.1  christos         *confunc = tls_construct_key_update;
   1017      1.1  christos         *mt = SSL3_MT_KEY_UPDATE;
   1018      1.1  christos         break;
   1019      1.1  christos     }
   1020      1.1  christos 
   1021      1.1  christos     return 1;
   1022      1.1  christos }
   1023      1.1  christos 
   1024      1.1  christos /*
   1025      1.1  christos  * Returns the maximum allowed length for the current message that we are
   1026      1.1  christos  * reading. Excludes the message header.
   1027      1.1  christos  */
   1028      1.1  christos size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s)
   1029      1.1  christos {
   1030      1.1  christos     OSSL_STATEM *st = &s->statem;
   1031      1.1  christos 
   1032      1.1  christos     switch (st->hand_state) {
   1033      1.1  christos     default:
   1034      1.1  christos         /* Shouldn't happen */
   1035      1.1  christos         return 0;
   1036      1.1  christos 
   1037      1.1  christos     case TLS_ST_CR_SRVR_HELLO:
   1038      1.1  christos         return SERVER_HELLO_MAX_LENGTH;
   1039      1.1  christos 
   1040      1.1  christos     case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
   1041      1.1  christos         return HELLO_VERIFY_REQUEST_MAX_LENGTH;
   1042      1.1  christos 
   1043      1.1  christos     case TLS_ST_CR_COMP_CERT:
   1044      1.1  christos     case TLS_ST_CR_CERT:
   1045      1.1  christos         return s->max_cert_list;
   1046      1.1  christos 
   1047      1.1  christos     case TLS_ST_CR_CERT_VRFY:
   1048      1.1  christos         return CERTIFICATE_VERIFY_MAX_LENGTH;
   1049      1.1  christos 
   1050      1.1  christos     case TLS_ST_CR_CERT_STATUS:
   1051      1.1  christos         return SSL3_RT_MAX_PLAIN_LENGTH;
   1052      1.1  christos 
   1053      1.1  christos     case TLS_ST_CR_KEY_EXCH:
   1054      1.1  christos         return SERVER_KEY_EXCH_MAX_LENGTH;
   1055      1.1  christos 
   1056      1.1  christos     case TLS_ST_CR_CERT_REQ:
   1057      1.1  christos         /*
   1058      1.1  christos          * Set to s->max_cert_list for compatibility with previous releases. In
   1059      1.1  christos          * practice these messages can get quite long if servers are configured
   1060      1.1  christos          * to provide a long list of acceptable CAs
   1061      1.1  christos          */
   1062      1.1  christos         return s->max_cert_list;
   1063      1.1  christos 
   1064      1.1  christos     case TLS_ST_CR_SRVR_DONE:
   1065      1.1  christos         return SERVER_HELLO_DONE_MAX_LENGTH;
   1066      1.1  christos 
   1067      1.1  christos     case TLS_ST_CR_CHANGE:
   1068      1.1  christos         if (s->version == DTLS1_BAD_VER)
   1069      1.1  christos             return 3;
   1070      1.1  christos         return CCS_MAX_LENGTH;
   1071      1.1  christos 
   1072      1.1  christos     case TLS_ST_CR_SESSION_TICKET:
   1073      1.1  christos         return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
   1074      1.1  christos                                             : SESSION_TICKET_MAX_LENGTH_TLS12;
   1075      1.1  christos 
   1076      1.1  christos     case TLS_ST_CR_FINISHED:
   1077      1.1  christos         return FINISHED_MAX_LENGTH;
   1078      1.1  christos 
   1079      1.1  christos     case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
   1080      1.1  christos         return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
   1081      1.1  christos 
   1082      1.1  christos     case TLS_ST_CR_KEY_UPDATE:
   1083      1.1  christos         return KEY_UPDATE_MAX_LENGTH;
   1084      1.1  christos     }
   1085      1.1  christos }
   1086      1.1  christos 
   1087      1.1  christos /*
   1088      1.1  christos  * Process a message that the client has received from the server.
   1089      1.1  christos  */
   1090      1.1  christos MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s,
   1091  1.1.1.2  christos     PACKET *pkt)
   1092      1.1  christos {
   1093      1.1  christos     OSSL_STATEM *st = &s->statem;
   1094      1.1  christos 
   1095      1.1  christos     switch (st->hand_state) {
   1096      1.1  christos     default:
   1097      1.1  christos         /* Shouldn't happen */
   1098      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1099      1.1  christos         return MSG_PROCESS_ERROR;
   1100      1.1  christos 
   1101      1.1  christos     case TLS_ST_CR_SRVR_HELLO:
   1102      1.1  christos         return tls_process_server_hello(s, pkt);
   1103      1.1  christos 
   1104      1.1  christos     case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
   1105      1.1  christos         return dtls_process_hello_verify(s, pkt);
   1106      1.1  christos 
   1107      1.1  christos     case TLS_ST_CR_CERT:
   1108      1.1  christos         return tls_process_server_certificate(s, pkt);
   1109      1.1  christos 
   1110      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
   1111      1.1  christos     case TLS_ST_CR_COMP_CERT:
   1112      1.1  christos         return tls_process_server_compressed_certificate(s, pkt);
   1113      1.1  christos #endif
   1114      1.1  christos 
   1115      1.1  christos     case TLS_ST_CR_CERT_VRFY:
   1116      1.1  christos         return tls_process_cert_verify(s, pkt);
   1117      1.1  christos 
   1118      1.1  christos     case TLS_ST_CR_CERT_STATUS:
   1119      1.1  christos         return tls_process_cert_status(s, pkt);
   1120      1.1  christos 
   1121      1.1  christos     case TLS_ST_CR_KEY_EXCH:
   1122      1.1  christos         return tls_process_key_exchange(s, pkt);
   1123      1.1  christos 
   1124      1.1  christos     case TLS_ST_CR_CERT_REQ:
   1125      1.1  christos         return tls_process_certificate_request(s, pkt);
   1126      1.1  christos 
   1127      1.1  christos     case TLS_ST_CR_SRVR_DONE:
   1128      1.1  christos         return tls_process_server_done(s, pkt);
   1129      1.1  christos 
   1130      1.1  christos     case TLS_ST_CR_CHANGE:
   1131      1.1  christos         return tls_process_change_cipher_spec(s, pkt);
   1132      1.1  christos 
   1133      1.1  christos     case TLS_ST_CR_SESSION_TICKET:
   1134      1.1  christos         return tls_process_new_session_ticket(s, pkt);
   1135      1.1  christos 
   1136      1.1  christos     case TLS_ST_CR_FINISHED:
   1137      1.1  christos         return tls_process_finished(s, pkt);
   1138      1.1  christos 
   1139      1.1  christos     case TLS_ST_CR_HELLO_REQ:
   1140      1.1  christos         return tls_process_hello_req(s, pkt);
   1141      1.1  christos 
   1142      1.1  christos     case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
   1143      1.1  christos         return tls_process_encrypted_extensions(s, pkt);
   1144      1.1  christos 
   1145      1.1  christos     case TLS_ST_CR_KEY_UPDATE:
   1146      1.1  christos         return tls_process_key_update(s, pkt);
   1147      1.1  christos     }
   1148      1.1  christos }
   1149      1.1  christos 
   1150      1.1  christos /*
   1151      1.1  christos  * Perform any further processing required following the receipt of a message
   1152      1.1  christos  * from the server
   1153      1.1  christos  */
   1154      1.1  christos WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s,
   1155  1.1.1.2  christos     WORK_STATE wst)
   1156      1.1  christos {
   1157      1.1  christos     OSSL_STATEM *st = &s->statem;
   1158      1.1  christos 
   1159      1.1  christos     switch (st->hand_state) {
   1160      1.1  christos     default:
   1161      1.1  christos         /* Shouldn't happen */
   1162      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1163      1.1  christos         return WORK_ERROR;
   1164      1.1  christos 
   1165      1.1  christos     case TLS_ST_CR_CERT:
   1166      1.1  christos     case TLS_ST_CR_COMP_CERT:
   1167      1.1  christos         return tls_post_process_server_certificate(s, wst);
   1168      1.1  christos 
   1169      1.1  christos     case TLS_ST_CR_CERT_VRFY:
   1170      1.1  christos     case TLS_ST_CR_CERT_REQ:
   1171      1.1  christos         return tls_prepare_client_certificate(s, wst);
   1172      1.1  christos     }
   1173      1.1  christos }
   1174      1.1  christos 
   1175      1.1  christos CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt)
   1176      1.1  christos {
   1177      1.1  christos     unsigned char *p;
   1178      1.1  christos     size_t sess_id_len;
   1179      1.1  christos     int i, protverr;
   1180      1.1  christos #ifndef OPENSSL_NO_COMP
   1181      1.1  christos     SSL_COMP *comp;
   1182      1.1  christos #endif
   1183      1.1  christos     SSL_SESSION *sess = s->session;
   1184      1.1  christos     unsigned char *session_id;
   1185      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   1186      1.1  christos 
   1187      1.1  christos     /* Work out what SSL/TLS/DTLS version to use */
   1188      1.1  christos     protverr = ssl_set_client_hello_version(s);
   1189      1.1  christos     if (protverr != 0) {
   1190      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
   1191      1.1  christos         return CON_FUNC_ERROR;
   1192      1.1  christos     }
   1193      1.1  christos 
   1194      1.1  christos     if (sess == NULL
   1195  1.1.1.2  christos         || !ssl_version_supported(s, sess->ssl_version, NULL)
   1196  1.1.1.2  christos         || !SSL_SESSION_is_resumable(sess)) {
   1197      1.1  christos         if (s->hello_retry_request == SSL_HRR_NONE
   1198  1.1.1.2  christos             && !ssl_get_new_session(s, 0)) {
   1199      1.1  christos             /* SSLfatal() already called */
   1200      1.1  christos             return CON_FUNC_ERROR;
   1201      1.1  christos         }
   1202      1.1  christos     }
   1203      1.1  christos     /* else use the pre-loaded session */
   1204      1.1  christos 
   1205      1.1  christos     p = s->s3.client_random;
   1206      1.1  christos 
   1207      1.1  christos     /*
   1208      1.1  christos      * for DTLS if client_random is initialized, reuse it, we are
   1209      1.1  christos      * required to use same upon reply to HelloVerify
   1210      1.1  christos      */
   1211      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s)) {
   1212      1.1  christos         size_t idx;
   1213      1.1  christos         i = 1;
   1214      1.1  christos         for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
   1215      1.1  christos             if (p[idx]) {
   1216      1.1  christos                 i = 0;
   1217      1.1  christos                 break;
   1218      1.1  christos             }
   1219      1.1  christos         }
   1220      1.1  christos     } else {
   1221      1.1  christos         i = (s->hello_retry_request == SSL_HRR_NONE);
   1222      1.1  christos     }
   1223      1.1  christos 
   1224  1.1.1.2  christos     if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random), DOWNGRADE_NONE) <= 0) {
   1225      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1226      1.1  christos         return CON_FUNC_ERROR;
   1227      1.1  christos     }
   1228      1.1  christos 
   1229      1.1  christos     /*-
   1230      1.1  christos      * version indicates the negotiated version: for example from
   1231      1.1  christos      * an SSLv2/v3 compatible client hello). The client_version
   1232      1.1  christos      * field is the maximum version we permit and it is also
   1233      1.1  christos      * used in RSA encrypted premaster secrets. Some servers can
   1234      1.1  christos      * choke if we initially report a higher version then
   1235      1.1  christos      * renegotiate to a lower one in the premaster secret. This
   1236      1.1  christos      * didn't happen with TLS 1.0 as most servers supported it
   1237      1.1  christos      * but it can with TLS 1.1 or later if the server only supports
   1238      1.1  christos      * 1.0.
   1239      1.1  christos      *
   1240      1.1  christos      * Possible scenario with previous logic:
   1241      1.1  christos      *      1. Client hello indicates TLS 1.2
   1242      1.1  christos      *      2. Server hello says TLS 1.0
   1243      1.1  christos      *      3. RSA encrypted premaster secret uses 1.2.
   1244      1.1  christos      *      4. Handshake proceeds using TLS 1.0.
   1245      1.1  christos      *      5. Server sends hello request to renegotiate.
   1246      1.1  christos      *      6. Client hello indicates TLS v1.0 as we now
   1247      1.1  christos      *         know that is maximum server supports.
   1248      1.1  christos      *      7. Server chokes on RSA encrypted premaster secret
   1249      1.1  christos      *         containing version 1.0.
   1250      1.1  christos      *
   1251      1.1  christos      * For interoperability it should be OK to always use the
   1252      1.1  christos      * maximum version we support in client hello and then rely
   1253      1.1  christos      * on the checking of version to ensure the servers isn't
   1254      1.1  christos      * being inconsistent: for example initially negotiating with
   1255      1.1  christos      * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
   1256      1.1  christos      * client_version in client hello and not resetting it to
   1257      1.1  christos      * the negotiated version.
   1258      1.1  christos      *
   1259      1.1  christos      * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
   1260      1.1  christos      * supported_versions extension for the real supported versions.
   1261      1.1  christos      */
   1262      1.1  christos     if (!WPACKET_put_bytes_u16(pkt, s->client_version)
   1263  1.1.1.2  christos         || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
   1264      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1265      1.1  christos         return CON_FUNC_ERROR;
   1266      1.1  christos     }
   1267      1.1  christos 
   1268      1.1  christos     /* Session ID */
   1269      1.1  christos     session_id = s->session->session_id;
   1270      1.1  christos     if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
   1271      1.1  christos         if (s->version == TLS1_3_VERSION
   1272  1.1.1.2  christos             && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
   1273      1.1  christos             sess_id_len = sizeof(s->tmp_session_id);
   1274      1.1  christos             s->tmp_session_id_len = sess_id_len;
   1275      1.1  christos             session_id = s->tmp_session_id;
   1276      1.1  christos             if (s->hello_retry_request == SSL_HRR_NONE
   1277  1.1.1.2  christos                 && RAND_bytes_ex(sctx->libctx, s->tmp_session_id,
   1278  1.1.1.2  christos                        sess_id_len, 0)
   1279  1.1.1.2  christos                     <= 0) {
   1280      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1281      1.1  christos                 return CON_FUNC_ERROR;
   1282      1.1  christos             }
   1283      1.1  christos         } else {
   1284      1.1  christos             sess_id_len = 0;
   1285      1.1  christos         }
   1286      1.1  christos     } else {
   1287      1.1  christos         assert(s->session->session_id_length <= sizeof(s->session->session_id));
   1288      1.1  christos         sess_id_len = s->session->session_id_length;
   1289      1.1  christos         if (s->version == TLS1_3_VERSION) {
   1290      1.1  christos             s->tmp_session_id_len = sess_id_len;
   1291      1.1  christos             memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
   1292      1.1  christos         }
   1293      1.1  christos     }
   1294      1.1  christos     if (!WPACKET_start_sub_packet_u8(pkt)
   1295  1.1.1.2  christos         || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id, sess_id_len))
   1296  1.1.1.2  christos         || !WPACKET_close(pkt)) {
   1297      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1298      1.1  christos         return CON_FUNC_ERROR;
   1299      1.1  christos     }
   1300      1.1  christos 
   1301      1.1  christos     /* cookie stuff for DTLS */
   1302      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s)) {
   1303      1.1  christos         if (s->d1->cookie_len > sizeof(s->d1->cookie)
   1304  1.1.1.2  christos             || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
   1305  1.1.1.2  christos                 s->d1->cookie_len)) {
   1306      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1307      1.1  christos             return CON_FUNC_ERROR;
   1308      1.1  christos         }
   1309      1.1  christos     }
   1310      1.1  christos 
   1311      1.1  christos     /* Ciphers supported */
   1312      1.1  christos     if (!WPACKET_start_sub_packet_u16(pkt)) {
   1313      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1314      1.1  christos         return CON_FUNC_ERROR;
   1315      1.1  christos     }
   1316      1.1  christos 
   1317      1.1  christos     if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)),
   1318  1.1.1.2  christos             pkt)) {
   1319      1.1  christos         /* SSLfatal() already called */
   1320      1.1  christos         return CON_FUNC_ERROR;
   1321      1.1  christos     }
   1322      1.1  christos     if (!WPACKET_close(pkt)) {
   1323      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1324      1.1  christos         return CON_FUNC_ERROR;
   1325      1.1  christos     }
   1326      1.1  christos 
   1327      1.1  christos     /* COMPRESSION */
   1328      1.1  christos     if (!WPACKET_start_sub_packet_u8(pkt)) {
   1329      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1330      1.1  christos         return CON_FUNC_ERROR;
   1331      1.1  christos     }
   1332      1.1  christos #ifndef OPENSSL_NO_COMP
   1333      1.1  christos     if (ssl_allow_compression(s)
   1334  1.1.1.2  christos         && sctx->comp_methods
   1335  1.1.1.2  christos         && (SSL_CONNECTION_IS_DTLS(s)
   1336  1.1.1.2  christos             || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
   1337      1.1  christos         int compnum = sk_SSL_COMP_num(sctx->comp_methods);
   1338      1.1  christos         for (i = 0; i < compnum; i++) {
   1339      1.1  christos             comp = sk_SSL_COMP_value(sctx->comp_methods, i);
   1340      1.1  christos             if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
   1341      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1342      1.1  christos                 return CON_FUNC_ERROR;
   1343      1.1  christos             }
   1344      1.1  christos         }
   1345      1.1  christos     }
   1346      1.1  christos #endif
   1347      1.1  christos     /* Add the NULL method */
   1348      1.1  christos     if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
   1349      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1350      1.1  christos         return CON_FUNC_ERROR;
   1351      1.1  christos     }
   1352      1.1  christos 
   1353      1.1  christos     /* TLS extensions */
   1354      1.1  christos     if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
   1355      1.1  christos         /* SSLfatal() already called */
   1356      1.1  christos         return CON_FUNC_ERROR;
   1357      1.1  christos     }
   1358      1.1  christos 
   1359      1.1  christos     return CON_FUNC_SUCCESS;
   1360      1.1  christos }
   1361      1.1  christos 
   1362      1.1  christos MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt)
   1363      1.1  christos {
   1364      1.1  christos     size_t cookie_len;
   1365      1.1  christos     PACKET cookiepkt;
   1366      1.1  christos 
   1367      1.1  christos     if (!PACKET_forward(pkt, 2)
   1368      1.1  christos         || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
   1369      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1370      1.1  christos         return MSG_PROCESS_ERROR;
   1371      1.1  christos     }
   1372      1.1  christos 
   1373      1.1  christos     cookie_len = PACKET_remaining(&cookiepkt);
   1374      1.1  christos     if (cookie_len > sizeof(s->d1->cookie)) {
   1375      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
   1376      1.1  christos         return MSG_PROCESS_ERROR;
   1377      1.1  christos     }
   1378      1.1  christos 
   1379      1.1  christos     if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
   1380      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1381      1.1  christos         return MSG_PROCESS_ERROR;
   1382      1.1  christos     }
   1383      1.1  christos     s->d1->cookie_len = cookie_len;
   1384      1.1  christos 
   1385      1.1  christos     return MSG_PROCESS_FINISHED_READING;
   1386      1.1  christos }
   1387      1.1  christos 
   1388      1.1  christos static int set_client_ciphersuite(SSL_CONNECTION *s,
   1389  1.1.1.2  christos     const unsigned char *cipherchars)
   1390      1.1  christos {
   1391      1.1  christos     STACK_OF(SSL_CIPHER) *sk;
   1392      1.1  christos     const SSL_CIPHER *c;
   1393      1.1  christos     int i;
   1394      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   1395      1.1  christos 
   1396      1.1  christos     c = ssl_get_cipher_by_char(s, cipherchars, 0);
   1397      1.1  christos     if (c == NULL) {
   1398      1.1  christos         /* unknown cipher */
   1399      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
   1400      1.1  christos         return 0;
   1401      1.1  christos     }
   1402      1.1  christos     /*
   1403      1.1  christos      * If it is a disabled cipher we either didn't send it in client hello,
   1404      1.1  christos      * or it's not allowed for the selected protocol. So we return an error.
   1405      1.1  christos      */
   1406      1.1  christos     if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
   1407      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
   1408      1.1  christos         return 0;
   1409      1.1  christos     }
   1410      1.1  christos 
   1411      1.1  christos     sk = ssl_get_ciphers_by_id(s);
   1412      1.1  christos     i = sk_SSL_CIPHER_find(sk, c);
   1413      1.1  christos     if (i < 0) {
   1414      1.1  christos         /* we did not say we would use this cipher */
   1415      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
   1416      1.1  christos         return 0;
   1417      1.1  christos     }
   1418      1.1  christos 
   1419      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
   1420  1.1.1.2  christos         && s->s3.tmp.new_cipher->id != c->id) {
   1421      1.1  christos         /* ServerHello selected a different ciphersuite to that in the HRR */
   1422      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
   1423      1.1  christos         return 0;
   1424      1.1  christos     }
   1425      1.1  christos 
   1426      1.1  christos     /*
   1427      1.1  christos      * Depending on the session caching (internal/external), the cipher
   1428      1.1  christos      * and/or cipher_id values may not be set. Make sure that cipher_id is
   1429      1.1  christos      * set and use it for comparison.
   1430      1.1  christos      */
   1431      1.1  christos     if (s->session->cipher != NULL)
   1432      1.1  christos         s->session->cipher_id = s->session->cipher->id;
   1433      1.1  christos     if (s->hit && (s->session->cipher_id != c->id)) {
   1434      1.1  christos         if (SSL_CONNECTION_IS_TLS13(s)) {
   1435      1.1  christos             const EVP_MD *md = ssl_md(sctx, c->algorithm2);
   1436      1.1  christos 
   1437      1.1  christos             if (!ossl_assert(s->session->cipher != NULL)) {
   1438      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1439      1.1  christos                 return 0;
   1440      1.1  christos             }
   1441      1.1  christos             /*
   1442      1.1  christos              * In TLSv1.3 it is valid for the server to select a different
   1443      1.1  christos              * ciphersuite as long as the hash is the same.
   1444      1.1  christos              */
   1445      1.1  christos             if (md == NULL
   1446  1.1.1.2  christos                 || md != ssl_md(sctx, s->session->cipher->algorithm2)) {
   1447      1.1  christos                 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1448  1.1.1.2  christos                     SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
   1449      1.1  christos                 return 0;
   1450      1.1  christos             }
   1451      1.1  christos         } else {
   1452      1.1  christos             /*
   1453      1.1  christos              * Prior to TLSv1.3 resuming a session always meant using the same
   1454      1.1  christos              * ciphersuite.
   1455      1.1  christos              */
   1456      1.1  christos             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1457  1.1.1.2  christos                 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
   1458      1.1  christos             return 0;
   1459      1.1  christos         }
   1460      1.1  christos     }
   1461      1.1  christos     s->s3.tmp.new_cipher = c;
   1462      1.1  christos 
   1463      1.1  christos     return 1;
   1464      1.1  christos }
   1465      1.1  christos 
   1466      1.1  christos MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt)
   1467      1.1  christos {
   1468      1.1  christos     PACKET session_id, extpkt;
   1469      1.1  christos     size_t session_id_len;
   1470      1.1  christos     const unsigned char *cipherchars;
   1471      1.1  christos     int hrr = 0;
   1472      1.1  christos     unsigned int compression;
   1473      1.1  christos     unsigned int sversion;
   1474      1.1  christos     unsigned int context;
   1475      1.1  christos     RAW_EXTENSION *extensions = NULL;
   1476      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   1477      1.1  christos     SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
   1478      1.1  christos #ifndef OPENSSL_NO_COMP
   1479      1.1  christos     SSL_COMP *comp;
   1480      1.1  christos #endif
   1481      1.1  christos 
   1482      1.1  christos     if (!PACKET_get_net_2(pkt, &sversion)) {
   1483      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1484      1.1  christos         goto err;
   1485      1.1  christos     }
   1486      1.1  christos 
   1487      1.1  christos     /* load the server random */
   1488      1.1  christos     if (s->version == TLS1_3_VERSION
   1489  1.1.1.2  christos         && sversion == TLS1_2_VERSION
   1490  1.1.1.2  christos         && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
   1491  1.1.1.2  christos         && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
   1492      1.1  christos         if (s->hello_retry_request != SSL_HRR_NONE) {
   1493      1.1  christos             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
   1494      1.1  christos             goto err;
   1495      1.1  christos         }
   1496      1.1  christos         s->hello_retry_request = SSL_HRR_PENDING;
   1497      1.1  christos         /* Tell the record layer that we know we're going to get TLSv1.3 */
   1498      1.1  christos         if (!ssl_set_record_protocol_version(s, s->version)) {
   1499      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1500      1.1  christos             goto err;
   1501      1.1  christos         }
   1502      1.1  christos         hrr = 1;
   1503      1.1  christos         if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
   1504      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1505      1.1  christos             goto err;
   1506      1.1  christos         }
   1507      1.1  christos     } else {
   1508      1.1  christos         if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
   1509      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1510      1.1  christos             goto err;
   1511      1.1  christos         }
   1512      1.1  christos     }
   1513      1.1  christos 
   1514      1.1  christos     /* Get the session-id. */
   1515      1.1  christos     if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
   1516      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1517      1.1  christos         goto err;
   1518      1.1  christos     }
   1519      1.1  christos     session_id_len = PACKET_remaining(&session_id);
   1520      1.1  christos     if (session_id_len > sizeof(s->session->session_id)
   1521      1.1  christos         || session_id_len > SSL3_SESSION_ID_SIZE) {
   1522      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
   1523      1.1  christos         goto err;
   1524      1.1  christos     }
   1525      1.1  christos 
   1526      1.1  christos     if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
   1527      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1528      1.1  christos         goto err;
   1529      1.1  christos     }
   1530      1.1  christos 
   1531      1.1  christos     if (!PACKET_get_1(pkt, &compression)) {
   1532      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   1533      1.1  christos         goto err;
   1534      1.1  christos     }
   1535      1.1  christos 
   1536      1.1  christos     /* TLS extensions */
   1537      1.1  christos     if (PACKET_remaining(pkt) == 0 && !hrr) {
   1538      1.1  christos         PACKET_null_init(&extpkt);
   1539      1.1  christos     } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
   1540  1.1.1.2  christos         || PACKET_remaining(pkt) != 0) {
   1541      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
   1542      1.1  christos         goto err;
   1543      1.1  christos     }
   1544      1.1  christos 
   1545      1.1  christos     if (!hrr) {
   1546      1.1  christos         if (!tls_collect_extensions(s, &extpkt,
   1547  1.1.1.2  christos                 SSL_EXT_TLS1_2_SERVER_HELLO
   1548  1.1.1.2  christos                     | SSL_EXT_TLS1_3_SERVER_HELLO,
   1549  1.1.1.2  christos                 &extensions, NULL, 1)) {
   1550      1.1  christos             /* SSLfatal() already called */
   1551      1.1  christos             goto err;
   1552      1.1  christos         }
   1553      1.1  christos 
   1554      1.1  christos         if (!ssl_choose_client_version(s, sversion, extensions)) {
   1555      1.1  christos             /* SSLfatal() already called */
   1556      1.1  christos             goto err;
   1557      1.1  christos         }
   1558      1.1  christos     }
   1559      1.1  christos 
   1560      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s) || hrr) {
   1561      1.1  christos         if (compression != 0) {
   1562      1.1  christos             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1563  1.1.1.2  christos                 SSL_R_INVALID_COMPRESSION_ALGORITHM);
   1564      1.1  christos             goto err;
   1565      1.1  christos         }
   1566      1.1  christos 
   1567      1.1  christos         if (session_id_len != s->tmp_session_id_len
   1568  1.1.1.2  christos             || memcmp(PACKET_data(&session_id), s->tmp_session_id,
   1569  1.1.1.2  christos                    session_id_len)
   1570  1.1.1.2  christos                 != 0) {
   1571      1.1  christos             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
   1572      1.1  christos             goto err;
   1573      1.1  christos         }
   1574      1.1  christos     }
   1575      1.1  christos 
   1576      1.1  christos     if (hrr) {
   1577      1.1  christos         if (!set_client_ciphersuite(s, cipherchars)) {
   1578      1.1  christos             /* SSLfatal() already called */
   1579      1.1  christos             goto err;
   1580      1.1  christos         }
   1581      1.1  christos 
   1582      1.1  christos         return tls_process_as_hello_retry_request(s, &extpkt);
   1583      1.1  christos     }
   1584      1.1  christos 
   1585      1.1  christos     /*
   1586      1.1  christos      * Now we have chosen the version we need to check again that the extensions
   1587      1.1  christos      * are appropriate for this version.
   1588      1.1  christos      */
   1589      1.1  christos     context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
   1590      1.1  christos                                          : SSL_EXT_TLS1_2_SERVER_HELLO;
   1591      1.1  christos     if (!tls_validate_all_contexts(s, context, extensions)) {
   1592      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
   1593      1.1  christos         goto err;
   1594      1.1  christos     }
   1595      1.1  christos 
   1596      1.1  christos     s->hit = 0;
   1597      1.1  christos 
   1598      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   1599      1.1  christos         /*
   1600      1.1  christos          * In TLSv1.3 a ServerHello message signals a key change so the end of
   1601      1.1  christos          * the message must be on a record boundary.
   1602      1.1  christos          */
   1603      1.1  christos         if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
   1604      1.1  christos             SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
   1605  1.1.1.2  christos                 SSL_R_NOT_ON_RECORD_BOUNDARY);
   1606      1.1  christos             goto err;
   1607      1.1  christos         }
   1608      1.1  christos 
   1609      1.1  christos         /* This will set s->hit if we are resuming */
   1610      1.1  christos         if (!tls_parse_extension(s, TLSEXT_IDX_psk,
   1611  1.1.1.2  christos                 SSL_EXT_TLS1_3_SERVER_HELLO,
   1612  1.1.1.2  christos                 extensions, NULL, 0)) {
   1613      1.1  christos             /* SSLfatal() already called */
   1614      1.1  christos             goto err;
   1615      1.1  christos         }
   1616      1.1  christos     } else {
   1617      1.1  christos         /*
   1618      1.1  christos          * Check if we can resume the session based on external pre-shared
   1619      1.1  christos          * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
   1620      1.1  christos          * Resumption based on server-side state works with session IDs.
   1621      1.1  christos          * Resumption based on pre-shared Protected Access Credentials (PACs)
   1622      1.1  christos          * works by overriding the SessionTicket extension at the application
   1623      1.1  christos          * layer, and does not send a session ID. (We do not know whether
   1624      1.1  christos          * EAP-FAST servers would honour the session ID.) Therefore, the session
   1625      1.1  christos          * ID alone is not a reliable indicator of session resumption, so we
   1626      1.1  christos          * first check if we can resume, and later peek at the next handshake
   1627      1.1  christos          * message to see if the server wants to resume.
   1628      1.1  christos          */
   1629      1.1  christos         if (s->version >= TLS1_VERSION
   1630  1.1.1.2  christos             && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
   1631      1.1  christos             const SSL_CIPHER *pref_cipher = NULL;
   1632      1.1  christos             /*
   1633      1.1  christos              * s->session->master_key_length is a size_t, but this is an int for
   1634      1.1  christos              * backwards compat reasons
   1635      1.1  christos              */
   1636      1.1  christos             int master_key_length;
   1637      1.1  christos 
   1638      1.1  christos             master_key_length = sizeof(s->session->master_key);
   1639      1.1  christos             if (s->ext.session_secret_cb(ussl, s->session->master_key,
   1640  1.1.1.2  christos                     &master_key_length,
   1641  1.1.1.2  christos                     NULL, &pref_cipher,
   1642  1.1.1.2  christos                     s->ext.session_secret_cb_arg)
   1643  1.1.1.2  christos                 && master_key_length > 0) {
   1644      1.1  christos                 s->session->master_key_length = master_key_length;
   1645  1.1.1.2  christos                 s->session->cipher = pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
   1646      1.1  christos             } else {
   1647      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1648      1.1  christos                 goto err;
   1649      1.1  christos             }
   1650      1.1  christos         }
   1651      1.1  christos 
   1652      1.1  christos         if (session_id_len != 0
   1653  1.1.1.2  christos             && session_id_len == s->session->session_id_length
   1654  1.1.1.2  christos             && memcmp(PACKET_data(&session_id), s->session->session_id,
   1655  1.1.1.2  christos                    session_id_len)
   1656  1.1.1.2  christos                 == 0)
   1657      1.1  christos             s->hit = 1;
   1658      1.1  christos     }
   1659      1.1  christos 
   1660      1.1  christos     if (s->hit) {
   1661      1.1  christos         if (s->sid_ctx_length != s->session->sid_ctx_length
   1662  1.1.1.2  christos             || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
   1663      1.1  christos             /* actually a client application bug */
   1664      1.1  christos             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1665  1.1.1.2  christos                 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
   1666      1.1  christos             goto err;
   1667      1.1  christos         }
   1668      1.1  christos     } else {
   1669      1.1  christos         /*
   1670      1.1  christos          * If we were trying for session-id reuse but the server
   1671      1.1  christos          * didn't resume, make a new SSL_SESSION.
   1672      1.1  christos          * In the case of EAP-FAST and PAC, we do not send a session ID,
   1673      1.1  christos          * so the PAC-based session secret is always preserved. It'll be
   1674      1.1  christos          * overwritten if the server refuses resumption.
   1675      1.1  christos          */
   1676      1.1  christos         if (s->session->session_id_length > 0) {
   1677      1.1  christos             ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
   1678      1.1  christos             if (!ssl_get_new_session(s, 0)) {
   1679      1.1  christos                 /* SSLfatal() already called */
   1680      1.1  christos                 goto err;
   1681      1.1  christos             }
   1682      1.1  christos         }
   1683      1.1  christos 
   1684      1.1  christos         s->session->ssl_version = s->version;
   1685      1.1  christos         /*
   1686      1.1  christos          * In TLSv1.2 and below we save the session id we were sent so we can
   1687      1.1  christos          * resume it later. In TLSv1.3 the session id we were sent is just an
   1688      1.1  christos          * echo of what we originally sent in the ClientHello and should not be
   1689      1.1  christos          * used for resumption.
   1690      1.1  christos          */
   1691      1.1  christos         if (!SSL_CONNECTION_IS_TLS13(s)) {
   1692      1.1  christos             s->session->session_id_length = session_id_len;
   1693      1.1  christos             /* session_id_len could be 0 */
   1694      1.1  christos             if (session_id_len > 0)
   1695      1.1  christos                 memcpy(s->session->session_id, PACKET_data(&session_id),
   1696  1.1.1.2  christos                     session_id_len);
   1697      1.1  christos         }
   1698      1.1  christos     }
   1699      1.1  christos 
   1700      1.1  christos     /* Session version and negotiated protocol version should match */
   1701      1.1  christos     if (s->version != s->session->ssl_version) {
   1702      1.1  christos         SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
   1703  1.1.1.2  christos             SSL_R_SSL_SESSION_VERSION_MISMATCH);
   1704      1.1  christos         goto err;
   1705      1.1  christos     }
   1706      1.1  christos     /*
   1707      1.1  christos      * Now that we know the version, update the check to see if it's an allowed
   1708      1.1  christos      * version.
   1709      1.1  christos      */
   1710      1.1  christos     s->s3.tmp.min_ver = s->version;
   1711      1.1  christos     s->s3.tmp.max_ver = s->version;
   1712      1.1  christos 
   1713      1.1  christos     if (!set_client_ciphersuite(s, cipherchars)) {
   1714      1.1  christos         /* SSLfatal() already called */
   1715      1.1  christos         goto err;
   1716      1.1  christos     }
   1717      1.1  christos 
   1718      1.1  christos #ifdef OPENSSL_NO_COMP
   1719      1.1  christos     if (compression != 0) {
   1720      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1721  1.1.1.2  christos             SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
   1722      1.1  christos         goto err;
   1723      1.1  christos     }
   1724      1.1  christos     /*
   1725      1.1  christos      * If compression is disabled we'd better not try to resume a session
   1726      1.1  christos      * using compression.
   1727      1.1  christos      */
   1728      1.1  christos     if (s->session->compress_meth != 0) {
   1729      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
   1730      1.1  christos         goto err;
   1731      1.1  christos     }
   1732      1.1  christos #else
   1733      1.1  christos     if (s->hit && compression != s->session->compress_meth) {
   1734      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1735  1.1.1.2  christos             SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
   1736      1.1  christos         goto err;
   1737      1.1  christos     }
   1738      1.1  christos     if (compression == 0)
   1739      1.1  christos         comp = NULL;
   1740      1.1  christos     else if (!ssl_allow_compression(s)) {
   1741      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
   1742      1.1  christos         goto err;
   1743      1.1  christos     } else {
   1744      1.1  christos         comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods,
   1745  1.1.1.2  christos             compression);
   1746      1.1  christos     }
   1747      1.1  christos 
   1748      1.1  christos     if (compression != 0 && comp == NULL) {
   1749      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
   1750  1.1.1.2  christos             SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
   1751      1.1  christos         goto err;
   1752      1.1  christos     } else {
   1753      1.1  christos         s->s3.tmp.new_compression = comp;
   1754      1.1  christos     }
   1755      1.1  christos #endif
   1756      1.1  christos 
   1757      1.1  christos     if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
   1758      1.1  christos         /* SSLfatal() already called */
   1759      1.1  christos         goto err;
   1760      1.1  christos     }
   1761      1.1  christos 
   1762      1.1  christos #ifndef OPENSSL_NO_SCTP
   1763      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
   1764      1.1  christos         unsigned char sctpauthkey[64];
   1765      1.1  christos         char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
   1766      1.1  christos         size_t labellen;
   1767      1.1  christos 
   1768      1.1  christos         /*
   1769      1.1  christos          * Add new shared key for SCTP-Auth, will be ignored if
   1770      1.1  christos          * no SCTP used.
   1771      1.1  christos          */
   1772      1.1  christos         memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
   1773  1.1.1.2  christos             sizeof(DTLS1_SCTP_AUTH_LABEL));
   1774      1.1  christos 
   1775      1.1  christos         /* Don't include the terminating zero. */
   1776      1.1  christos         labellen = sizeof(labelbuffer) - 1;
   1777      1.1  christos         if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
   1778      1.1  christos             labellen += 1;
   1779      1.1  christos 
   1780      1.1  christos         if (SSL_export_keying_material(ssl, sctpauthkey,
   1781  1.1.1.2  christos                 sizeof(sctpauthkey),
   1782  1.1.1.2  christos                 labelbuffer,
   1783  1.1.1.2  christos                 labellen, NULL, 0, 0)
   1784  1.1.1.2  christos             <= 0) {
   1785      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   1786      1.1  christos             goto err;
   1787      1.1  christos         }
   1788      1.1  christos 
   1789      1.1  christos         BIO_ctrl(SSL_get_wbio(ssl),
   1790  1.1.1.2  christos             BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
   1791  1.1.1.2  christos             sizeof(sctpauthkey), sctpauthkey);
   1792      1.1  christos     }
   1793      1.1  christos #endif
   1794      1.1  christos 
   1795      1.1  christos     /*
   1796      1.1  christos      * In TLSv1.3 we have some post-processing to change cipher state, otherwise
   1797      1.1  christos      * we're done with this message
   1798      1.1  christos      */
   1799      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   1800      1.1  christos         if (!ssl->method->ssl3_enc->setup_key_block(s)
   1801  1.1.1.2  christos             || !tls13_store_handshake_traffic_hash(s)) {
   1802      1.1  christos             /* SSLfatal() already called */
   1803      1.1  christos             goto err;
   1804      1.1  christos         }
   1805      1.1  christos         /*
   1806      1.1  christos          * If we're not doing early-data and we're not going to send a dummy CCS
   1807      1.1  christos          * (i.e. no middlebox compat mode) then we can change the write keys
   1808      1.1  christos          * immediately. Otherwise we have to defer this until after all possible
   1809      1.1  christos          * early data is written. We could just always defer until the last
   1810      1.1  christos          * moment except QUIC needs it done at the same time as the read keys
   1811      1.1  christos          * are changed. Since QUIC doesn't do TLS early data or need middlebox
   1812      1.1  christos          * compat this doesn't cause a problem.
   1813      1.1  christos          */
   1814      1.1  christos         if (SSL_IS_QUIC_HANDSHAKE(s)
   1815  1.1.1.2  christos             || (s->early_data_state == SSL_EARLY_DATA_NONE
   1816  1.1.1.2  christos                 && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0)) {
   1817      1.1  christos             if (!ssl->method->ssl3_enc->change_cipher_state(s,
   1818      1.1  christos                     SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
   1819      1.1  christos                 /* SSLfatal() already called */
   1820      1.1  christos                 goto err;
   1821  1.1.1.2  christos             }
   1822      1.1  christos         }
   1823      1.1  christos         if (!ssl->method->ssl3_enc->change_cipher_state(s,
   1824  1.1.1.2  christos                 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
   1825      1.1  christos             /* SSLfatal() already called */
   1826      1.1  christos             goto err;
   1827      1.1  christos         }
   1828      1.1  christos     }
   1829      1.1  christos 
   1830      1.1  christos     OPENSSL_free(extensions);
   1831      1.1  christos     return MSG_PROCESS_CONTINUE_READING;
   1832  1.1.1.2  christos err:
   1833      1.1  christos     OPENSSL_free(extensions);
   1834      1.1  christos     return MSG_PROCESS_ERROR;
   1835      1.1  christos }
   1836      1.1  christos 
   1837      1.1  christos static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
   1838  1.1.1.2  christos     PACKET *extpkt)
   1839      1.1  christos {
   1840      1.1  christos     RAW_EXTENSION *extensions = NULL;
   1841      1.1  christos 
   1842      1.1  christos     /*
   1843      1.1  christos      * If we were sending early_data then any alerts should not be sent using
   1844      1.1  christos      * the old wrlmethod.
   1845      1.1  christos      */
   1846      1.1  christos     if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
   1847  1.1.1.2  christos         && !ssl_set_new_record_layer(s,
   1848  1.1.1.2  christos             TLS_ANY_VERSION,
   1849  1.1.1.2  christos             OSSL_RECORD_DIRECTION_WRITE,
   1850  1.1.1.2  christos             OSSL_RECORD_PROTECTION_LEVEL_NONE,
   1851  1.1.1.2  christos             NULL, 0, NULL, 0, NULL, 0, NULL, 0,
   1852  1.1.1.2  christos             NULL, 0, NID_undef, NULL, NULL, NULL)) {
   1853      1.1  christos         /* SSLfatal already called */
   1854      1.1  christos         goto err;
   1855      1.1  christos     }
   1856      1.1  christos     /* We are definitely going to be using TLSv1.3 */
   1857      1.1  christos     s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION);
   1858      1.1  christos 
   1859      1.1  christos     if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
   1860  1.1.1.2  christos             &extensions, NULL, 1)
   1861  1.1.1.2  christos         || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
   1862  1.1.1.2  christos             extensions, NULL, 0, 1)) {
   1863      1.1  christos         /* SSLfatal() already called */
   1864      1.1  christos         goto err;
   1865      1.1  christos     }
   1866      1.1  christos 
   1867      1.1  christos     OPENSSL_free(extensions);
   1868      1.1  christos     extensions = NULL;
   1869      1.1  christos 
   1870      1.1  christos     if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
   1871      1.1  christos         /*
   1872      1.1  christos          * We didn't receive a cookie or a new key_share so the next
   1873      1.1  christos          * ClientHello will not change
   1874      1.1  christos          */
   1875      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
   1876      1.1  christos         goto err;
   1877      1.1  christos     }
   1878      1.1  christos 
   1879      1.1  christos     /*
   1880      1.1  christos      * Re-initialise the Transcript Hash. We're going to prepopulate it with
   1881      1.1  christos      * a synthetic message_hash in place of ClientHello1.
   1882      1.1  christos      */
   1883      1.1  christos     if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
   1884      1.1  christos         /* SSLfatal() already called */
   1885      1.1  christos         goto err;
   1886      1.1  christos     }
   1887      1.1  christos 
   1888      1.1  christos     /*
   1889      1.1  christos      * Add this message to the Transcript Hash. Normally this is done
   1890      1.1  christos      * automatically prior to the message processing stage. However due to the
   1891      1.1  christos      * need to create the synthetic message hash, we defer that step until now
   1892      1.1  christos      * for HRR messages.
   1893      1.1  christos      */
   1894      1.1  christos     if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
   1895  1.1.1.2  christos             s->init_num + SSL3_HM_HEADER_LENGTH)) {
   1896      1.1  christos         /* SSLfatal() already called */
   1897      1.1  christos         goto err;
   1898      1.1  christos     }
   1899      1.1  christos 
   1900      1.1  christos     return MSG_PROCESS_FINISHED_READING;
   1901  1.1.1.2  christos err:
   1902      1.1  christos     OPENSSL_free(extensions);
   1903      1.1  christos     return MSG_PROCESS_ERROR;
   1904      1.1  christos }
   1905      1.1  christos 
   1906      1.1  christos MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt)
   1907      1.1  christos {
   1908      1.1  christos     EVP_PKEY *peer_rpk = NULL;
   1909      1.1  christos 
   1910      1.1  christos     if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
   1911      1.1  christos         /* SSLfatal() already called */
   1912      1.1  christos         return MSG_PROCESS_ERROR;
   1913      1.1  christos     }
   1914      1.1  christos 
   1915      1.1  christos     if (peer_rpk == NULL) {
   1916      1.1  christos         SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE);
   1917      1.1  christos         return MSG_PROCESS_ERROR;
   1918      1.1  christos     }
   1919      1.1  christos 
   1920      1.1  christos     EVP_PKEY_free(sc->session->peer_rpk);
   1921      1.1  christos     sc->session->peer_rpk = peer_rpk;
   1922      1.1  christos 
   1923      1.1  christos     return MSG_PROCESS_CONTINUE_PROCESSING;
   1924      1.1  christos }
   1925      1.1  christos 
   1926      1.1  christos static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc,
   1927  1.1.1.2  christos     WORK_STATE wst)
   1928      1.1  christos {
   1929      1.1  christos     size_t certidx;
   1930      1.1  christos     const SSL_CERT_LOOKUP *clu;
   1931      1.1  christos     int v_ok;
   1932      1.1  christos 
   1933      1.1  christos     if (sc->session->peer_rpk == NULL) {
   1934      1.1  christos         SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER,
   1935  1.1.1.2  christos             SSL_R_INVALID_RAW_PUBLIC_KEY);
   1936      1.1  christos         return WORK_ERROR;
   1937      1.1  christos     }
   1938      1.1  christos 
   1939      1.1  christos     if (sc->rwstate == SSL_RETRY_VERIFY)
   1940      1.1  christos         sc->rwstate = SSL_NOTHING;
   1941      1.1  christos 
   1942      1.1  christos     ERR_set_mark();
   1943      1.1  christos     v_ok = ssl_verify_rpk(sc, sc->session->peer_rpk);
   1944      1.1  christos     if (v_ok <= 0 && sc->verify_mode != SSL_VERIFY_NONE) {
   1945      1.1  christos         ERR_clear_last_mark();
   1946      1.1  christos         SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
   1947  1.1.1.2  christos             SSL_R_CERTIFICATE_VERIFY_FAILED);
   1948      1.1  christos         return WORK_ERROR;
   1949      1.1  christos     }
   1950  1.1.1.2  christos     ERR_pop_to_mark(); /* but we keep s->verify_result */
   1951      1.1  christos     if (v_ok > 0 && sc->rwstate == SSL_RETRY_VERIFY) {
   1952      1.1  christos         return WORK_MORE_A;
   1953      1.1  christos     }
   1954      1.1  christos 
   1955      1.1  christos     if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx,
   1956  1.1.1.2  christos              SSL_CONNECTION_GET_CTX(sc)))
   1957  1.1.1.2  christos         == NULL) {
   1958      1.1  christos         SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
   1959      1.1  christos         return WORK_ERROR;
   1960      1.1  christos     }
   1961      1.1  christos 
   1962      1.1  christos     /*
   1963      1.1  christos      * Check certificate type is consistent with ciphersuite. For TLS 1.3
   1964      1.1  christos      * skip check since TLS 1.3 ciphersuites can be used with any certificate
   1965      1.1  christos      * type.
   1966      1.1  christos      */
   1967      1.1  christos     if (!SSL_CONNECTION_IS_TLS13(sc)) {
   1968      1.1  christos         if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) {
   1969      1.1  christos             SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE);
   1970      1.1  christos             return WORK_ERROR;
   1971      1.1  christos         }
   1972      1.1  christos     }
   1973      1.1  christos 
   1974      1.1  christos     /* Ensure there is no peer/peer_chain */
   1975      1.1  christos     X509_free(sc->session->peer);
   1976      1.1  christos     sc->session->peer = NULL;
   1977      1.1  christos     sk_X509_pop_free(sc->session->peer_chain, X509_free);
   1978      1.1  christos     sc->session->peer_chain = NULL;
   1979      1.1  christos     sc->session->verify_result = sc->verify_result;
   1980      1.1  christos 
   1981      1.1  christos     /* Save the current hash state for when we receive the CertificateVerify */
   1982      1.1  christos     if (SSL_CONNECTION_IS_TLS13(sc)
   1983  1.1.1.2  christos         && !ssl_handshake_hash(sc, sc->cert_verify_hash,
   1984  1.1.1.2  christos             sizeof(sc->cert_verify_hash),
   1985  1.1.1.2  christos             &sc->cert_verify_hash_len)) {
   1986      1.1  christos         /* SSLfatal() already called */
   1987      1.1  christos         return WORK_ERROR;
   1988      1.1  christos     }
   1989      1.1  christos 
   1990      1.1  christos     return WORK_FINISHED_CONTINUE;
   1991      1.1  christos }
   1992      1.1  christos 
   1993      1.1  christos /* prepare server cert verification by setting s->session->peer_chain from pkt */
   1994      1.1  christos MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s,
   1995  1.1.1.2  christos     PACKET *pkt)
   1996      1.1  christos {
   1997      1.1  christos     unsigned long cert_list_len, cert_len;
   1998      1.1  christos     X509 *x = NULL;
   1999      1.1  christos     const unsigned char *certstart, *certbytes;
   2000      1.1  christos     size_t chainidx;
   2001      1.1  christos     unsigned int context = 0;
   2002      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   2003      1.1  christos 
   2004      1.1  christos     if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
   2005      1.1  christos         return tls_process_server_rpk(s, pkt);
   2006      1.1  christos     if (s->ext.server_cert_type != TLSEXT_cert_type_x509) {
   2007      1.1  christos         SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
   2008  1.1.1.2  christos             SSL_R_UNKNOWN_CERTIFICATE_TYPE);
   2009      1.1  christos         goto err;
   2010      1.1  christos     }
   2011      1.1  christos 
   2012      1.1  christos     if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
   2013      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   2014      1.1  christos         goto err;
   2015      1.1  christos     }
   2016      1.1  christos 
   2017      1.1  christos     if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
   2018  1.1.1.2  christos         || context != 0
   2019  1.1.1.2  christos         || !PACKET_get_net_3(pkt, &cert_list_len)
   2020  1.1.1.2  christos         || PACKET_remaining(pkt) != cert_list_len
   2021  1.1.1.2  christos         || PACKET_remaining(pkt) == 0) {
   2022      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2023      1.1  christos         goto err;
   2024      1.1  christos     }
   2025      1.1  christos     for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
   2026      1.1  christos         if (!PACKET_get_net_3(pkt, &cert_len)
   2027      1.1  christos             || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
   2028      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
   2029      1.1  christos             goto err;
   2030      1.1  christos         }
   2031      1.1  christos 
   2032      1.1  christos         certstart = certbytes;
   2033      1.1  christos         x = X509_new_ex(sctx->libctx, sctx->propq);
   2034      1.1  christos         if (x == NULL) {
   2035      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
   2036      1.1  christos             goto err;
   2037      1.1  christos         }
   2038      1.1  christos         if (d2i_X509(&x, (const unsigned char **)&certbytes,
   2039  1.1.1.2  christos                 cert_len)
   2040  1.1.1.2  christos             == NULL) {
   2041      1.1  christos             SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
   2042      1.1  christos             goto err;
   2043      1.1  christos         }
   2044      1.1  christos 
   2045      1.1  christos         if (certbytes != (certstart + cert_len)) {
   2046      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
   2047      1.1  christos             goto err;
   2048      1.1  christos         }
   2049      1.1  christos 
   2050      1.1  christos         if (SSL_CONNECTION_IS_TLS13(s)) {
   2051      1.1  christos             RAW_EXTENSION *rawexts = NULL;
   2052      1.1  christos             PACKET extensions;
   2053      1.1  christos 
   2054      1.1  christos             if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
   2055      1.1  christos                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
   2056      1.1  christos                 goto err;
   2057      1.1  christos             }
   2058      1.1  christos             if (!tls_collect_extensions(s, &extensions,
   2059  1.1.1.2  christos                     SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
   2060  1.1.1.2  christos                     NULL, chainidx == 0)
   2061      1.1  christos                 || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
   2062  1.1.1.2  christos                     rawexts, x, chainidx,
   2063  1.1.1.2  christos                     PACKET_remaining(pkt) == 0)) {
   2064      1.1  christos                 OPENSSL_free(rawexts);
   2065      1.1  christos                 /* SSLfatal already called */
   2066      1.1  christos                 goto err;
   2067      1.1  christos             }
   2068      1.1  christos             OPENSSL_free(rawexts);
   2069      1.1  christos         }
   2070      1.1  christos 
   2071      1.1  christos         if (!sk_X509_push(s->session->peer_chain, x)) {
   2072      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   2073      1.1  christos             goto err;
   2074      1.1  christos         }
   2075      1.1  christos         x = NULL;
   2076      1.1  christos     }
   2077      1.1  christos     return MSG_PROCESS_CONTINUE_PROCESSING;
   2078      1.1  christos 
   2079  1.1.1.2  christos err:
   2080      1.1  christos     X509_free(x);
   2081      1.1  christos     OSSL_STACK_OF_X509_free(s->session->peer_chain);
   2082      1.1  christos     s->session->peer_chain = NULL;
   2083      1.1  christos     return MSG_PROCESS_ERROR;
   2084      1.1  christos }
   2085      1.1  christos 
   2086      1.1  christos /*
   2087      1.1  christos  * Verify the s->session->peer_chain and check server cert type.
   2088      1.1  christos  * On success set s->session->peer and s->session->verify_result.
   2089      1.1  christos  * Else the peer certificate verification callback may request retry.
   2090      1.1  christos  */
   2091      1.1  christos WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s,
   2092  1.1.1.2  christos     WORK_STATE wst)
   2093      1.1  christos {
   2094      1.1  christos     X509 *x;
   2095      1.1  christos     EVP_PKEY *pkey = NULL;
   2096      1.1  christos     const SSL_CERT_LOOKUP *clu;
   2097      1.1  christos     size_t certidx;
   2098      1.1  christos     int i;
   2099      1.1  christos 
   2100      1.1  christos     if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
   2101      1.1  christos         return tls_post_process_server_rpk(s, wst);
   2102      1.1  christos 
   2103      1.1  christos     if (s->rwstate == SSL_RETRY_VERIFY)
   2104      1.1  christos         s->rwstate = SSL_NOTHING;
   2105      1.1  christos 
   2106      1.1  christos     /*
   2107      1.1  christos      * The documented interface is that SSL_VERIFY_PEER should be set in order
   2108      1.1  christos      * for client side verification of the server certificate to take place.
   2109      1.1  christos      * However, historically the code has only checked that *any* flag is set
   2110      1.1  christos      * to cause server verification to take place. Use of the other flags makes
   2111      1.1  christos      * no sense in client mode. An attempt to clean up the semantics was
   2112      1.1  christos      * reverted because at least one application *only* set
   2113      1.1  christos      * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
   2114      1.1  christos      * server verification to take place, after the clean up it silently did
   2115      1.1  christos      * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
   2116      1.1  christos      * sent to them because they are void functions. Therefore, we now use the
   2117      1.1  christos      * (less clean) historic behaviour of performing validation if any flag is
   2118      1.1  christos      * set. The *documented* interface remains the same.
   2119      1.1  christos      */
   2120      1.1  christos     ERR_set_mark();
   2121      1.1  christos     i = ssl_verify_cert_chain(s, s->session->peer_chain);
   2122      1.1  christos     if (i <= 0 && s->verify_mode != SSL_VERIFY_NONE) {
   2123      1.1  christos         ERR_clear_last_mark();
   2124      1.1  christos         SSLfatal(s, ssl_x509err2alert(s->verify_result),
   2125  1.1.1.2  christos             SSL_R_CERTIFICATE_VERIFY_FAILED);
   2126      1.1  christos         return WORK_ERROR;
   2127      1.1  christos     }
   2128  1.1.1.2  christos     ERR_pop_to_mark(); /* but we keep s->verify_result */
   2129      1.1  christos     if (i > 0 && s->rwstate == SSL_RETRY_VERIFY)
   2130      1.1  christos         return WORK_MORE_A;
   2131      1.1  christos 
   2132      1.1  christos     /*
   2133      1.1  christos      * Inconsistency alert: cert_chain does include the peer's certificate,
   2134      1.1  christos      * which we don't include in statem_srvr.c
   2135      1.1  christos      */
   2136      1.1  christos     x = sk_X509_value(s->session->peer_chain, 0);
   2137      1.1  christos 
   2138      1.1  christos     pkey = X509_get0_pubkey(x);
   2139      1.1  christos 
   2140      1.1  christos     if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
   2141      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2142  1.1.1.2  christos             SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
   2143      1.1  christos         return WORK_ERROR;
   2144      1.1  christos     }
   2145      1.1  christos 
   2146      1.1  christos     if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx,
   2147  1.1.1.2  christos              SSL_CONNECTION_GET_CTX(s)))
   2148  1.1.1.2  christos         == NULL) {
   2149      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
   2150      1.1  christos         return WORK_ERROR;
   2151      1.1  christos     }
   2152      1.1  christos     /*
   2153      1.1  christos      * Check certificate type is consistent with ciphersuite. For TLS 1.3
   2154      1.1  christos      * skip check since TLS 1.3 ciphersuites can be used with any certificate
   2155      1.1  christos      * type.
   2156      1.1  christos      */
   2157      1.1  christos     if (!SSL_CONNECTION_IS_TLS13(s)) {
   2158      1.1  christos         if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
   2159      1.1  christos             SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
   2160      1.1  christos             return WORK_ERROR;
   2161      1.1  christos         }
   2162      1.1  christos     }
   2163      1.1  christos 
   2164      1.1  christos     if (!X509_up_ref(x)) {
   2165      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2166      1.1  christos         return WORK_ERROR;
   2167      1.1  christos     }
   2168      1.1  christos 
   2169      1.1  christos     X509_free(s->session->peer);
   2170      1.1  christos     s->session->peer = x;
   2171      1.1  christos     s->session->verify_result = s->verify_result;
   2172      1.1  christos     /* Ensure there is no RPK */
   2173      1.1  christos     EVP_PKEY_free(s->session->peer_rpk);
   2174      1.1  christos     s->session->peer_rpk = NULL;
   2175      1.1  christos 
   2176      1.1  christos     /* Save the current hash state for when we receive the CertificateVerify */
   2177      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)
   2178  1.1.1.2  christos         && !ssl_handshake_hash(s, s->cert_verify_hash,
   2179  1.1.1.2  christos             sizeof(s->cert_verify_hash),
   2180  1.1.1.2  christos             &s->cert_verify_hash_len)) {
   2181      1.1  christos         /* SSLfatal() already called */;
   2182      1.1  christos         return WORK_ERROR;
   2183      1.1  christos     }
   2184      1.1  christos     return WORK_FINISHED_CONTINUE;
   2185      1.1  christos }
   2186      1.1  christos 
   2187      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
   2188      1.1  christos MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
   2189      1.1  christos {
   2190      1.1  christos     MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
   2191      1.1  christos     PACKET tmppkt;
   2192      1.1  christos     BUF_MEM *buf = BUF_MEM_new();
   2193      1.1  christos 
   2194      1.1  christos     if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
   2195      1.1  christos         ret = tls_process_server_certificate(sc, &tmppkt);
   2196      1.1  christos 
   2197      1.1  christos     BUF_MEM_free(buf);
   2198      1.1  christos     return ret;
   2199      1.1  christos }
   2200      1.1  christos #endif
   2201      1.1  christos 
   2202      1.1  christos static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
   2203      1.1  christos {
   2204      1.1  christos #ifndef OPENSSL_NO_PSK
   2205      1.1  christos     PACKET psk_identity_hint;
   2206      1.1  christos 
   2207      1.1  christos     /* PSK ciphersuites are preceded by an identity hint */
   2208      1.1  christos 
   2209      1.1  christos     if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
   2210      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2211      1.1  christos         return 0;
   2212      1.1  christos     }
   2213      1.1  christos 
   2214      1.1  christos     /*
   2215      1.1  christos      * Store PSK identity hint for later use, hint is used in
   2216      1.1  christos      * tls_construct_client_key_exchange.  Assume that the maximum length of
   2217      1.1  christos      * a PSK identity hint can be as long as the maximum length of a PSK
   2218      1.1  christos      * identity.
   2219      1.1  christos      */
   2220      1.1  christos     if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
   2221      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
   2222      1.1  christos         return 0;
   2223      1.1  christos     }
   2224      1.1  christos 
   2225      1.1  christos     if (PACKET_remaining(&psk_identity_hint) == 0) {
   2226      1.1  christos         OPENSSL_free(s->session->psk_identity_hint);
   2227      1.1  christos         s->session->psk_identity_hint = NULL;
   2228      1.1  christos     } else if (!PACKET_strndup(&psk_identity_hint,
   2229  1.1.1.2  christos                    &s->session->psk_identity_hint)) {
   2230      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2231      1.1  christos         return 0;
   2232      1.1  christos     }
   2233      1.1  christos 
   2234      1.1  christos     return 1;
   2235      1.1  christos #else
   2236      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2237      1.1  christos     return 0;
   2238      1.1  christos #endif
   2239      1.1  christos }
   2240      1.1  christos 
   2241      1.1  christos static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
   2242      1.1  christos {
   2243      1.1  christos #ifndef OPENSSL_NO_SRP
   2244      1.1  christos     PACKET prime, generator, salt, server_pub;
   2245      1.1  christos 
   2246      1.1  christos     if (!PACKET_get_length_prefixed_2(pkt, &prime)
   2247      1.1  christos         || !PACKET_get_length_prefixed_2(pkt, &generator)
   2248      1.1  christos         || !PACKET_get_length_prefixed_1(pkt, &salt)
   2249      1.1  christos         || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
   2250      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2251      1.1  christos         return 0;
   2252      1.1  christos     }
   2253      1.1  christos 
   2254  1.1.1.2  christos     if ((s->srp_ctx.N = BN_bin2bn(PACKET_data(&prime),
   2255  1.1.1.2  christos              (int)PACKET_remaining(&prime), NULL))
   2256  1.1.1.2  christos             == NULL
   2257  1.1.1.2  christos         || (s->srp_ctx.g = BN_bin2bn(PACKET_data(&generator),
   2258  1.1.1.2  christos                 (int)PACKET_remaining(&generator), NULL))
   2259  1.1.1.2  christos             == NULL
   2260  1.1.1.2  christos         || (s->srp_ctx.s = BN_bin2bn(PACKET_data(&salt),
   2261  1.1.1.2  christos                 (int)PACKET_remaining(&salt), NULL))
   2262  1.1.1.2  christos             == NULL
   2263  1.1.1.2  christos         || (s->srp_ctx.B = BN_bin2bn(PACKET_data(&server_pub),
   2264  1.1.1.2  christos                 (int)PACKET_remaining(&server_pub), NULL))
   2265  1.1.1.2  christos             == NULL) {
   2266      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
   2267      1.1  christos         return 0;
   2268      1.1  christos     }
   2269      1.1  christos 
   2270      1.1  christos     if (!srp_verify_server_param(s)) {
   2271      1.1  christos         /* SSLfatal() already called */
   2272      1.1  christos         return 0;
   2273      1.1  christos     }
   2274      1.1  christos 
   2275      1.1  christos     /* We must check if there is a certificate */
   2276      1.1  christos     if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
   2277      1.1  christos         *pkey = tls_get_peer_pkey(s);
   2278      1.1  christos 
   2279      1.1  christos     return 1;
   2280      1.1  christos #else
   2281      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2282      1.1  christos     return 0;
   2283      1.1  christos #endif
   2284      1.1  christos }
   2285      1.1  christos 
   2286      1.1  christos static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
   2287      1.1  christos {
   2288      1.1  christos     PACKET prime, generator, pub_key;
   2289      1.1  christos     EVP_PKEY *peer_tmp = NULL;
   2290      1.1  christos     BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
   2291      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   2292      1.1  christos     OSSL_PARAM *params = NULL;
   2293      1.1  christos     OSSL_PARAM_BLD *tmpl = NULL;
   2294      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   2295      1.1  christos     int ret = 0;
   2296      1.1  christos 
   2297      1.1  christos     if (!PACKET_get_length_prefixed_2(pkt, &prime)
   2298      1.1  christos         || !PACKET_get_length_prefixed_2(pkt, &generator)
   2299      1.1  christos         || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
   2300      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2301      1.1  christos         return 0;
   2302      1.1  christos     }
   2303      1.1  christos 
   2304      1.1  christos     p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
   2305      1.1  christos     g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
   2306  1.1.1.2  christos         NULL);
   2307      1.1  christos     bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
   2308  1.1.1.2  christos         (int)PACKET_remaining(&pub_key), NULL);
   2309      1.1  christos     if (p == NULL || g == NULL || bnpub_key == NULL) {
   2310      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
   2311      1.1  christos         goto err;
   2312      1.1  christos     }
   2313      1.1  christos 
   2314      1.1  christos     tmpl = OSSL_PARAM_BLD_new();
   2315      1.1  christos     if (tmpl == NULL
   2316  1.1.1.2  christos         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
   2317  1.1.1.2  christos         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
   2318  1.1.1.2  christos         || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
   2319  1.1.1.2  christos             bnpub_key)
   2320  1.1.1.2  christos         || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
   2321      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2322      1.1  christos         goto err;
   2323      1.1  christos     }
   2324      1.1  christos 
   2325      1.1  christos     pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);
   2326      1.1  christos     if (pctx == NULL) {
   2327      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2328      1.1  christos         goto err;
   2329      1.1  christos     }
   2330      1.1  christos     if (EVP_PKEY_fromdata_init(pctx) <= 0
   2331  1.1.1.2  christos         || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
   2332      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
   2333      1.1  christos         goto err;
   2334      1.1  christos     }
   2335      1.1  christos 
   2336      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2337      1.1  christos     pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq);
   2338      1.1  christos     if (pctx == NULL
   2339  1.1.1.2  christos         /*
   2340  1.1.1.2  christos          * EVP_PKEY_param_check() will verify that the DH params are using
   2341  1.1.1.2  christos          * a safe prime. In this context, because we're using ephemeral DH,
   2342  1.1.1.2  christos          * we're ok with it not being a safe prime.
   2343  1.1.1.2  christos          * EVP_PKEY_param_check_quick() skips the safe prime check.
   2344  1.1.1.2  christos          */
   2345  1.1.1.2  christos         || EVP_PKEY_param_check_quick(pctx) != 1
   2346  1.1.1.2  christos         || EVP_PKEY_public_check(pctx) != 1) {
   2347      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
   2348      1.1  christos         goto err;
   2349      1.1  christos     }
   2350      1.1  christos 
   2351      1.1  christos     if (!ssl_security(s, SSL_SECOP_TMP_DH,
   2352  1.1.1.2  christos             EVP_PKEY_get_security_bits(peer_tmp),
   2353  1.1.1.2  christos             0, peer_tmp)) {
   2354      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
   2355      1.1  christos         goto err;
   2356      1.1  christos     }
   2357      1.1  christos 
   2358      1.1  christos     s->s3.peer_tmp = peer_tmp;
   2359      1.1  christos     peer_tmp = NULL;
   2360      1.1  christos 
   2361      1.1  christos     /*
   2362      1.1  christos      * FIXME: This makes assumptions about which ciphersuites come with
   2363      1.1  christos      * public keys. We should have a less ad-hoc way of doing this
   2364      1.1  christos      */
   2365      1.1  christos     if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
   2366      1.1  christos         *pkey = tls_get_peer_pkey(s);
   2367      1.1  christos     /* else anonymous DH, so no certificate or pkey. */
   2368      1.1  christos 
   2369      1.1  christos     ret = 1;
   2370      1.1  christos 
   2371  1.1.1.2  christos err:
   2372      1.1  christos     OSSL_PARAM_BLD_free(tmpl);
   2373      1.1  christos     OSSL_PARAM_free(params);
   2374      1.1  christos     EVP_PKEY_free(peer_tmp);
   2375      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2376      1.1  christos     BN_free(p);
   2377      1.1  christos     BN_free(g);
   2378      1.1  christos     BN_free(bnpub_key);
   2379      1.1  christos 
   2380      1.1  christos     return ret;
   2381      1.1  christos }
   2382      1.1  christos 
   2383      1.1  christos static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
   2384      1.1  christos {
   2385      1.1  christos     PACKET encoded_pt;
   2386      1.1  christos     unsigned int curve_type, curve_id;
   2387      1.1  christos 
   2388      1.1  christos     /*
   2389      1.1  christos      * Extract elliptic curve parameters and the server's ephemeral ECDH
   2390      1.1  christos      * public key. We only support named (not generic) curves and
   2391      1.1  christos      * ECParameters in this case is just three bytes.
   2392      1.1  christos      */
   2393      1.1  christos     if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
   2394      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
   2395      1.1  christos         return 0;
   2396      1.1  christos     }
   2397      1.1  christos     /*
   2398      1.1  christos      * Check curve is named curve type and one of our preferences, if not
   2399      1.1  christos      * server has sent an invalid curve.
   2400      1.1  christos      */
   2401      1.1  christos     if (curve_type != NAMED_CURVE_TYPE
   2402  1.1.1.2  christos         || !tls1_check_group_id(s, curve_id, 1)) {
   2403      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
   2404      1.1  christos         return 0;
   2405      1.1  christos     }
   2406      1.1  christos 
   2407      1.1  christos     if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
   2408      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2409  1.1.1.2  christos             SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
   2410      1.1  christos         return 0;
   2411      1.1  christos     }
   2412      1.1  christos 
   2413      1.1  christos     if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
   2414      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2415      1.1  christos         return 0;
   2416      1.1  christos     }
   2417      1.1  christos 
   2418      1.1  christos     if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
   2419  1.1.1.2  christos             PACKET_data(&encoded_pt),
   2420  1.1.1.2  christos             PACKET_remaining(&encoded_pt))
   2421  1.1.1.2  christos         <= 0) {
   2422      1.1  christos         SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
   2423      1.1  christos         return 0;
   2424      1.1  christos     }
   2425      1.1  christos 
   2426      1.1  christos     /*
   2427      1.1  christos      * The ECC/TLS specification does not mention the use of DSA to sign
   2428      1.1  christos      * ECParameters in the server key exchange message. We do support RSA
   2429      1.1  christos      * and ECDSA.
   2430      1.1  christos      */
   2431      1.1  christos     if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
   2432      1.1  christos         *pkey = tls_get_peer_pkey(s);
   2433      1.1  christos     else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
   2434      1.1  christos         *pkey = tls_get_peer_pkey(s);
   2435      1.1  christos     /* else anonymous ECDH, so no certificate or pkey. */
   2436      1.1  christos 
   2437      1.1  christos     /* Cache the agreed upon group in the SSL_SESSION */
   2438      1.1  christos     s->session->kex_group = curve_id;
   2439      1.1  christos     return 1;
   2440      1.1  christos }
   2441      1.1  christos 
   2442      1.1  christos MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt)
   2443      1.1  christos {
   2444      1.1  christos     long alg_k;
   2445      1.1  christos     EVP_PKEY *pkey = NULL;
   2446      1.1  christos     EVP_MD_CTX *md_ctx = NULL;
   2447      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   2448      1.1  christos     PACKET save_param_start, signature;
   2449      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   2450      1.1  christos 
   2451      1.1  christos     alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
   2452      1.1  christos 
   2453      1.1  christos     save_param_start = *pkt;
   2454      1.1  christos 
   2455      1.1  christos     EVP_PKEY_free(s->s3.peer_tmp);
   2456      1.1  christos     s->s3.peer_tmp = NULL;
   2457      1.1  christos 
   2458      1.1  christos     if (alg_k & SSL_PSK) {
   2459      1.1  christos         if (!tls_process_ske_psk_preamble(s, pkt)) {
   2460      1.1  christos             /* SSLfatal() already called */
   2461      1.1  christos             goto err;
   2462      1.1  christos         }
   2463      1.1  christos     }
   2464      1.1  christos 
   2465      1.1  christos     /* Nothing else to do for plain PSK or RSAPSK */
   2466      1.1  christos     if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
   2467      1.1  christos     } else if (alg_k & SSL_kSRP) {
   2468      1.1  christos         if (!tls_process_ske_srp(s, pkt, &pkey)) {
   2469      1.1  christos             /* SSLfatal() already called */
   2470      1.1  christos             goto err;
   2471      1.1  christos         }
   2472      1.1  christos     } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
   2473      1.1  christos         if (!tls_process_ske_dhe(s, pkt, &pkey)) {
   2474      1.1  christos             /* SSLfatal() already called */
   2475      1.1  christos             goto err;
   2476      1.1  christos         }
   2477      1.1  christos     } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
   2478      1.1  christos         if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
   2479      1.1  christos             /* SSLfatal() already called */
   2480      1.1  christos             goto err;
   2481      1.1  christos         }
   2482      1.1  christos     } else if (alg_k) {
   2483      1.1  christos         SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
   2484      1.1  christos         goto err;
   2485      1.1  christos     }
   2486      1.1  christos 
   2487      1.1  christos     /* if it was signed, check the signature */
   2488      1.1  christos     if (pkey != NULL) {
   2489      1.1  christos         PACKET params;
   2490      1.1  christos         const EVP_MD *md = NULL;
   2491      1.1  christos         unsigned char *tbs;
   2492      1.1  christos         size_t tbslen;
   2493      1.1  christos         int rv;
   2494      1.1  christos 
   2495      1.1  christos         /*
   2496      1.1  christos          * |pkt| now points to the beginning of the signature, so the difference
   2497      1.1  christos          * equals the length of the parameters.
   2498      1.1  christos          */
   2499      1.1  christos         if (!PACKET_get_sub_packet(&save_param_start, &params,
   2500  1.1.1.2  christos                 PACKET_remaining(&save_param_start) - PACKET_remaining(pkt))) {
   2501      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
   2502      1.1  christos             goto err;
   2503      1.1  christos         }
   2504      1.1  christos 
   2505      1.1  christos         if (SSL_USE_SIGALGS(s)) {
   2506      1.1  christos             unsigned int sigalg;
   2507      1.1  christos 
   2508      1.1  christos             if (!PACKET_get_net_2(pkt, &sigalg)) {
   2509      1.1  christos                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
   2510      1.1  christos                 goto err;
   2511      1.1  christos             }
   2512  1.1.1.2  christos             if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
   2513      1.1  christos                 /* SSLfatal() already called */
   2514      1.1  christos                 goto err;
   2515      1.1  christos             }
   2516      1.1  christos         } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
   2517      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2518  1.1.1.2  christos                 SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
   2519      1.1  christos             goto err;
   2520      1.1  christos         }
   2521      1.1  christos 
   2522      1.1  christos         if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
   2523      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2524  1.1.1.2  christos                 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
   2525      1.1  christos             goto err;
   2526      1.1  christos         }
   2527      1.1  christos         if (SSL_USE_SIGALGS(s))
   2528      1.1  christos             OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
   2529  1.1.1.2  christos                 md == NULL ? "n/a" : EVP_MD_get0_name(md));
   2530      1.1  christos 
   2531      1.1  christos         if (!PACKET_get_length_prefixed_2(pkt, &signature)
   2532      1.1  christos             || PACKET_remaining(pkt) != 0) {
   2533      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2534      1.1  christos             goto err;
   2535      1.1  christos         }
   2536      1.1  christos 
   2537      1.1  christos         md_ctx = EVP_MD_CTX_new();
   2538      1.1  christos         if (md_ctx == NULL) {
   2539      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   2540      1.1  christos             goto err;
   2541      1.1  christos         }
   2542      1.1  christos 
   2543      1.1  christos         if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
   2544  1.1.1.2  christos                 md == NULL ? NULL : EVP_MD_get0_name(md),
   2545  1.1.1.2  christos                 sctx->libctx, sctx->propq, pkey,
   2546  1.1.1.2  christos                 NULL)
   2547  1.1.1.2  christos             <= 0) {
   2548      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   2549      1.1  christos             goto err;
   2550      1.1  christos         }
   2551      1.1  christos         if (SSL_USE_PSS(s)) {
   2552      1.1  christos             if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
   2553      1.1  christos                 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
   2554  1.1.1.2  christos                        RSA_PSS_SALTLEN_DIGEST)
   2555  1.1.1.2  christos                     <= 0) {
   2556      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   2557      1.1  christos                 goto err;
   2558      1.1  christos             }
   2559      1.1  christos         }
   2560      1.1  christos         tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
   2561  1.1.1.2  christos             PACKET_remaining(&params));
   2562      1.1  christos         if (tbslen == 0) {
   2563      1.1  christos             /* SSLfatal() already called */
   2564      1.1  christos             goto err;
   2565      1.1  christos         }
   2566      1.1  christos 
   2567      1.1  christos         rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
   2568  1.1.1.2  christos             PACKET_remaining(&signature), tbs, tbslen);
   2569      1.1  christos         OPENSSL_free(tbs);
   2570      1.1  christos         if (rv <= 0) {
   2571      1.1  christos             SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
   2572      1.1  christos             goto err;
   2573      1.1  christos         }
   2574      1.1  christos         EVP_MD_CTX_free(md_ctx);
   2575      1.1  christos         md_ctx = NULL;
   2576      1.1  christos     } else {
   2577      1.1  christos         /* aNULL, aSRP or PSK do not need public keys */
   2578      1.1  christos         if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
   2579      1.1  christos             && !(alg_k & SSL_PSK)) {
   2580      1.1  christos             /* Might be wrong key type, check it */
   2581      1.1  christos             if (ssl3_check_cert_and_algorithm(s)) {
   2582      1.1  christos                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
   2583      1.1  christos             }
   2584      1.1  christos             /* else this shouldn't happen, SSLfatal() already called */
   2585      1.1  christos             goto err;
   2586      1.1  christos         }
   2587      1.1  christos         /* still data left over */
   2588      1.1  christos         if (PACKET_remaining(pkt) != 0) {
   2589      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
   2590      1.1  christos             goto err;
   2591      1.1  christos         }
   2592      1.1  christos     }
   2593      1.1  christos 
   2594      1.1  christos     return MSG_PROCESS_CONTINUE_READING;
   2595  1.1.1.2  christos err:
   2596      1.1  christos     EVP_MD_CTX_free(md_ctx);
   2597      1.1  christos     return MSG_PROCESS_ERROR;
   2598      1.1  christos }
   2599      1.1  christos 
   2600      1.1  christos MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s,
   2601  1.1.1.2  christos     PACKET *pkt)
   2602      1.1  christos {
   2603      1.1  christos     /* Clear certificate validity flags */
   2604      1.1  christos     if (s->s3.tmp.valid_flags != NULL)
   2605      1.1  christos         memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t));
   2606      1.1  christos     else
   2607      1.1  christos         s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t));
   2608      1.1  christos 
   2609      1.1  christos     /* Give up for good if allocation didn't work */
   2610      1.1  christos     if (s->s3.tmp.valid_flags == NULL)
   2611      1.1  christos         return 0;
   2612      1.1  christos 
   2613      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   2614      1.1  christos         PACKET reqctx, extensions;
   2615      1.1  christos         RAW_EXTENSION *rawexts = NULL;
   2616      1.1  christos 
   2617      1.1  christos         if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
   2618      1.1  christos             /*
   2619      1.1  christos              * We already sent close_notify. This can only happen in TLSv1.3
   2620      1.1  christos              * post-handshake messages. We can't reasonably respond to this, so
   2621      1.1  christos              * we just ignore it
   2622      1.1  christos              */
   2623      1.1  christos             return MSG_PROCESS_FINISHED_READING;
   2624      1.1  christos         }
   2625      1.1  christos 
   2626      1.1  christos         /* Free and zero certificate types: it is not present in TLS 1.3 */
   2627      1.1  christos         OPENSSL_free(s->s3.tmp.ctype);
   2628      1.1  christos         s->s3.tmp.ctype = NULL;
   2629      1.1  christos         s->s3.tmp.ctype_len = 0;
   2630      1.1  christos         OPENSSL_free(s->pha_context);
   2631      1.1  christos         s->pha_context = NULL;
   2632      1.1  christos         s->pha_context_len = 0;
   2633      1.1  christos 
   2634  1.1.1.2  christos         if (!PACKET_get_length_prefixed_1(pkt, &reqctx) || !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
   2635      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2636      1.1  christos             return MSG_PROCESS_ERROR;
   2637      1.1  christos         }
   2638      1.1  christos 
   2639      1.1  christos         if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
   2640      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
   2641      1.1  christos             return MSG_PROCESS_ERROR;
   2642      1.1  christos         }
   2643      1.1  christos         if (!tls_collect_extensions(s, &extensions,
   2644  1.1.1.2  christos                 SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
   2645  1.1.1.2  christos                 &rawexts, NULL, 1)
   2646      1.1  christos             || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
   2647  1.1.1.2  christos                 rawexts, NULL, 0, 1)) {
   2648      1.1  christos             /* SSLfatal() already called */
   2649      1.1  christos             OPENSSL_free(rawexts);
   2650      1.1  christos             return MSG_PROCESS_ERROR;
   2651      1.1  christos         }
   2652      1.1  christos         OPENSSL_free(rawexts);
   2653      1.1  christos         if (!tls1_process_sigalgs(s)) {
   2654      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
   2655      1.1  christos             return MSG_PROCESS_ERROR;
   2656      1.1  christos         }
   2657      1.1  christos     } else {
   2658      1.1  christos         PACKET ctypes;
   2659      1.1  christos 
   2660      1.1  christos         /* get the certificate types */
   2661      1.1  christos         if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
   2662      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2663      1.1  christos             return MSG_PROCESS_ERROR;
   2664      1.1  christos         }
   2665      1.1  christos 
   2666      1.1  christos         if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
   2667      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2668      1.1  christos             return MSG_PROCESS_ERROR;
   2669      1.1  christos         }
   2670      1.1  christos 
   2671      1.1  christos         if (SSL_USE_SIGALGS(s)) {
   2672      1.1  christos             PACKET sigalgs;
   2673      1.1  christos 
   2674      1.1  christos             if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
   2675      1.1  christos                 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2676      1.1  christos                 return MSG_PROCESS_ERROR;
   2677      1.1  christos             }
   2678      1.1  christos 
   2679      1.1  christos             /*
   2680      1.1  christos              * Despite this being for certificates, preserve compatibility
   2681      1.1  christos              * with pre-TLS 1.3 and use the regular sigalgs field.
   2682      1.1  christos              */
   2683      1.1  christos             if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
   2684      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2685  1.1.1.2  christos                     SSL_R_SIGNATURE_ALGORITHMS_ERROR);
   2686      1.1  christos                 return MSG_PROCESS_ERROR;
   2687      1.1  christos             }
   2688      1.1  christos             if (!tls1_process_sigalgs(s)) {
   2689      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
   2690      1.1  christos                 return MSG_PROCESS_ERROR;
   2691      1.1  christos             }
   2692      1.1  christos         }
   2693      1.1  christos 
   2694      1.1  christos         /* get the CA RDNs */
   2695      1.1  christos         if (!parse_ca_names(s, pkt)) {
   2696      1.1  christos             /* SSLfatal() already called */
   2697      1.1  christos             return MSG_PROCESS_ERROR;
   2698      1.1  christos         }
   2699      1.1  christos     }
   2700      1.1  christos 
   2701      1.1  christos     if (PACKET_remaining(pkt) != 0) {
   2702      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2703      1.1  christos         return MSG_PROCESS_ERROR;
   2704      1.1  christos     }
   2705      1.1  christos 
   2706      1.1  christos     /* we should setup a certificate to return.... */
   2707      1.1  christos     s->s3.tmp.cert_req = 1;
   2708      1.1  christos 
   2709      1.1  christos     /*
   2710      1.1  christos      * In TLSv1.3 we don't prepare the client certificate yet. We wait until
   2711      1.1  christos      * after the CertificateVerify message has been received. This is because
   2712      1.1  christos      * in TLSv1.3 the CertificateRequest arrives before the Certificate message
   2713      1.1  christos      * but in TLSv1.2 it is the other way around. We want to make sure that
   2714      1.1  christos      * SSL_get1_peer_certificate() returns something sensible in
   2715      1.1  christos      * client_cert_cb.
   2716      1.1  christos      */
   2717      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)
   2718      1.1  christos         && s->post_handshake_auth != SSL_PHA_REQUESTED)
   2719      1.1  christos         return MSG_PROCESS_CONTINUE_READING;
   2720      1.1  christos 
   2721      1.1  christos     return MSG_PROCESS_CONTINUE_PROCESSING;
   2722      1.1  christos }
   2723      1.1  christos 
   2724      1.1  christos MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s,
   2725  1.1.1.2  christos     PACKET *pkt)
   2726      1.1  christos {
   2727      1.1  christos     unsigned int ticklen;
   2728      1.1  christos     unsigned long ticket_lifetime_hint, age_add = 0;
   2729      1.1  christos     unsigned int sess_len;
   2730      1.1  christos     RAW_EXTENSION *exts = NULL;
   2731      1.1  christos     PACKET nonce;
   2732      1.1  christos     EVP_MD *sha256 = NULL;
   2733      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   2734      1.1  christos 
   2735      1.1  christos     PACKET_null_init(&nonce);
   2736      1.1  christos 
   2737      1.1  christos     if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
   2738      1.1  christos         || (SSL_CONNECTION_IS_TLS13(s)
   2739      1.1  christos             && (!PACKET_get_net_4(pkt, &age_add)
   2740      1.1  christos                 || !PACKET_get_length_prefixed_1(pkt, &nonce)))
   2741      1.1  christos         || !PACKET_get_net_2(pkt, &ticklen)
   2742      1.1  christos         || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0
   2743  1.1.1.2  christos                                              || PACKET_remaining(pkt) < ticklen)
   2744      1.1  christos                                        : PACKET_remaining(pkt) != ticklen)) {
   2745      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2746      1.1  christos         goto err;
   2747      1.1  christos     }
   2748      1.1  christos 
   2749      1.1  christos     /*
   2750      1.1  christos      * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
   2751      1.1  christos      * ticket. We already checked this TLSv1.3 case above, so it should never
   2752      1.1  christos      * be 0 here in that instance
   2753      1.1  christos      */
   2754      1.1  christos     if (ticklen == 0)
   2755      1.1  christos         return MSG_PROCESS_CONTINUE_READING;
   2756      1.1  christos 
   2757      1.1  christos     /*
   2758      1.1  christos      * Sessions must be immutable once they go into the session cache. Otherwise
   2759      1.1  christos      * we can get multi-thread problems. Therefore we don't "update" sessions,
   2760      1.1  christos      * we replace them with a duplicate. In TLSv1.3 we need to do this every
   2761      1.1  christos      * time a NewSessionTicket arrives because those messages arrive
   2762      1.1  christos      * post-handshake and the session may have already gone into the session
   2763      1.1  christos      * cache.
   2764      1.1  christos      */
   2765      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) {
   2766      1.1  christos         SSL_SESSION *new_sess;
   2767      1.1  christos 
   2768      1.1  christos         /*
   2769      1.1  christos          * We reused an existing session, so we need to replace it with a new
   2770      1.1  christos          * one
   2771      1.1  christos          */
   2772      1.1  christos         if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
   2773      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
   2774      1.1  christos             goto err;
   2775      1.1  christos         }
   2776      1.1  christos 
   2777      1.1  christos         if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
   2778  1.1.1.2  christos             && !SSL_CONNECTION_IS_TLS13(s)) {
   2779      1.1  christos             /*
   2780      1.1  christos              * In TLSv1.2 and below the arrival of a new tickets signals that
   2781      1.1  christos              * any old ticket we were using is now out of date, so we remove the
   2782      1.1  christos              * old session from the cache. We carry on if this fails
   2783      1.1  christos              */
   2784      1.1  christos             SSL_CTX_remove_session(s->session_ctx, s->session);
   2785      1.1  christos         }
   2786      1.1  christos 
   2787      1.1  christos         SSL_SESSION_free(s->session);
   2788      1.1  christos         s->session = new_sess;
   2789      1.1  christos     }
   2790      1.1  christos 
   2791      1.1  christos     s->session->time = ossl_time_now();
   2792      1.1  christos     ssl_session_calculate_timeout(s->session);
   2793      1.1  christos 
   2794      1.1  christos     OPENSSL_free(s->session->ext.tick);
   2795      1.1  christos     s->session->ext.tick = NULL;
   2796      1.1  christos     s->session->ext.ticklen = 0;
   2797      1.1  christos 
   2798      1.1  christos     s->session->ext.tick = OPENSSL_malloc(ticklen);
   2799      1.1  christos     if (s->session->ext.tick == NULL) {
   2800      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   2801      1.1  christos         goto err;
   2802      1.1  christos     }
   2803      1.1  christos     if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
   2804      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2805      1.1  christos         goto err;
   2806      1.1  christos     }
   2807      1.1  christos 
   2808      1.1  christos     s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
   2809      1.1  christos     s->session->ext.tick_age_add = age_add;
   2810      1.1  christos     s->session->ext.ticklen = ticklen;
   2811      1.1  christos 
   2812      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   2813      1.1  christos         PACKET extpkt;
   2814      1.1  christos 
   2815      1.1  christos         if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
   2816  1.1.1.2  christos             || PACKET_remaining(pkt) != 0) {
   2817      1.1  christos             SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2818      1.1  christos             goto err;
   2819      1.1  christos         }
   2820      1.1  christos 
   2821      1.1  christos         if (!tls_collect_extensions(s, &extpkt,
   2822  1.1.1.2  christos                 SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
   2823  1.1.1.2  christos                 NULL, 1)
   2824  1.1.1.2  christos             || !tls_parse_all_extensions(s,
   2825  1.1.1.2  christos                 SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
   2826  1.1.1.2  christos                 exts, NULL, 0, 1)) {
   2827      1.1  christos             /* SSLfatal() already called */
   2828      1.1  christos             goto err;
   2829      1.1  christos         }
   2830      1.1  christos     }
   2831      1.1  christos 
   2832      1.1  christos     /*
   2833      1.1  christos      * There are two ways to detect a resumed ticket session. One is to set
   2834      1.1  christos      * an appropriate session ID and then the server must return a match in
   2835      1.1  christos      * ServerHello. This allows the normal client session ID matching to work
   2836      1.1  christos      * and we know much earlier that the ticket has been accepted. The
   2837      1.1  christos      * other way is to set zero length session ID when the ticket is
   2838      1.1  christos      * presented and rely on the handshake to determine session resumption.
   2839      1.1  christos      * We choose the former approach because this fits in with assumptions
   2840      1.1  christos      * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
   2841      1.1  christos      * ticket.
   2842      1.1  christos      */
   2843      1.1  christos     sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
   2844      1.1  christos     if (sha256 == NULL) {
   2845      1.1  christos         /* Error is already recorded */
   2846      1.1  christos         SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
   2847      1.1  christos         goto err;
   2848      1.1  christos     }
   2849      1.1  christos     /*
   2850      1.1  christos      * We use sess_len here because EVP_Digest expects an int
   2851      1.1  christos      * but s->session->session_id_length is a size_t
   2852      1.1  christos      */
   2853      1.1  christos     if (!EVP_Digest(s->session->ext.tick, ticklen,
   2854  1.1.1.2  christos             s->session->session_id, &sess_len,
   2855  1.1.1.2  christos             sha256, NULL)) {
   2856      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   2857      1.1  christos         goto err;
   2858      1.1  christos     }
   2859      1.1  christos     EVP_MD_free(sha256);
   2860      1.1  christos     sha256 = NULL;
   2861      1.1  christos     s->session->session_id_length = sess_len;
   2862      1.1  christos     s->session->not_resumable = 0;
   2863      1.1  christos 
   2864      1.1  christos     /* This is a standalone message in TLSv1.3, so there is no more to read */
   2865      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   2866      1.1  christos         const EVP_MD *md = ssl_handshake_md(s);
   2867      1.1  christos         int hashleni = EVP_MD_get_size(md);
   2868      1.1  christos         size_t hashlen;
   2869  1.1.1.2  christos         /* ASCII: "resumption", in hex for EBCDIC compatibility */
   2870  1.1.1.2  christos         static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D,
   2871  1.1.1.2  christos             0x70, 0x74, 0x69, 0x6F, 0x6E };
   2872      1.1  christos 
   2873      1.1  christos         /* Ensure cast to size_t is safe */
   2874      1.1  christos         if (!ossl_assert(hashleni > 0)) {
   2875      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   2876      1.1  christos             goto err;
   2877      1.1  christos         }
   2878      1.1  christos         hashlen = (size_t)hashleni;
   2879      1.1  christos 
   2880      1.1  christos         if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
   2881  1.1.1.2  christos                 nonce_label,
   2882  1.1.1.2  christos                 sizeof(nonce_label),
   2883  1.1.1.2  christos                 PACKET_data(&nonce),
   2884  1.1.1.2  christos                 PACKET_remaining(&nonce),
   2885  1.1.1.2  christos                 s->session->master_key,
   2886  1.1.1.2  christos                 hashlen, 1)) {
   2887      1.1  christos             /* SSLfatal() already called */
   2888      1.1  christos             goto err;
   2889      1.1  christos         }
   2890      1.1  christos         s->session->master_key_length = hashlen;
   2891      1.1  christos 
   2892      1.1  christos         OPENSSL_free(exts);
   2893      1.1  christos         ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
   2894      1.1  christos         return MSG_PROCESS_FINISHED_READING;
   2895      1.1  christos     }
   2896      1.1  christos 
   2897      1.1  christos     return MSG_PROCESS_CONTINUE_READING;
   2898  1.1.1.2  christos err:
   2899      1.1  christos     EVP_MD_free(sha256);
   2900      1.1  christos     OPENSSL_free(exts);
   2901      1.1  christos     return MSG_PROCESS_ERROR;
   2902      1.1  christos }
   2903      1.1  christos 
   2904      1.1  christos /*
   2905      1.1  christos  * In TLSv1.3 this is called from the extensions code, otherwise it is used to
   2906      1.1  christos  * parse a separate message. Returns 1 on success or 0 on failure
   2907      1.1  christos  */
   2908      1.1  christos int tls_process_cert_status_body(SSL_CONNECTION *s, PACKET *pkt)
   2909      1.1  christos {
   2910      1.1  christos     size_t resplen;
   2911      1.1  christos     unsigned int type;
   2912      1.1  christos 
   2913      1.1  christos     if (!PACKET_get_1(pkt, &type)
   2914      1.1  christos         || type != TLSEXT_STATUSTYPE_ocsp) {
   2915      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
   2916      1.1  christos         return 0;
   2917      1.1  christos     }
   2918      1.1  christos     if (!PACKET_get_net_3_len(pkt, &resplen)
   2919      1.1  christos         || PACKET_remaining(pkt) != resplen) {
   2920      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2921      1.1  christos         return 0;
   2922      1.1  christos     }
   2923      1.1  christos     s->ext.ocsp.resp = OPENSSL_malloc(resplen);
   2924      1.1  christos     if (s->ext.ocsp.resp == NULL) {
   2925      1.1  christos         s->ext.ocsp.resp_len = 0;
   2926      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   2927      1.1  christos         return 0;
   2928      1.1  christos     }
   2929      1.1  christos     s->ext.ocsp.resp_len = resplen;
   2930      1.1  christos     if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
   2931      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   2932      1.1  christos         return 0;
   2933      1.1  christos     }
   2934      1.1  christos 
   2935      1.1  christos     return 1;
   2936      1.1  christos }
   2937      1.1  christos 
   2938      1.1  christos MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt)
   2939      1.1  christos {
   2940      1.1  christos     if (!tls_process_cert_status_body(s, pkt)) {
   2941      1.1  christos         /* SSLfatal() already called */
   2942      1.1  christos         return MSG_PROCESS_ERROR;
   2943      1.1  christos     }
   2944      1.1  christos 
   2945      1.1  christos     return MSG_PROCESS_CONTINUE_READING;
   2946      1.1  christos }
   2947      1.1  christos 
   2948      1.1  christos /*
   2949      1.1  christos  * Perform miscellaneous checks and processing after we have received the
   2950      1.1  christos  * server's initial flight. In TLS1.3 this is after the Server Finished message.
   2951      1.1  christos  * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
   2952      1.1  christos  * on failure.
   2953      1.1  christos  */
   2954      1.1  christos int tls_process_initial_server_flight(SSL_CONNECTION *s)
   2955      1.1  christos {
   2956      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   2957      1.1  christos 
   2958      1.1  christos     /*
   2959      1.1  christos      * at this point we check that we have the required stuff from
   2960      1.1  christos      * the server
   2961      1.1  christos      */
   2962      1.1  christos     if (!ssl3_check_cert_and_algorithm(s)) {
   2963      1.1  christos         /* SSLfatal() already called */
   2964      1.1  christos         return 0;
   2965      1.1  christos     }
   2966      1.1  christos 
   2967      1.1  christos     /*
   2968      1.1  christos      * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
   2969      1.1  christos      * |ext.ocsp.resp_len| values will be set if we actually received a status
   2970      1.1  christos      * message, or NULL and -1 otherwise
   2971      1.1  christos      */
   2972      1.1  christos     if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
   2973  1.1.1.2  christos         && sctx->ext.status_cb != NULL) {
   2974      1.1  christos         int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s),
   2975  1.1.1.2  christos             sctx->ext.status_arg);
   2976      1.1  christos 
   2977      1.1  christos         if (ret == 0) {
   2978      1.1  christos             SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
   2979  1.1.1.2  christos                 SSL_R_INVALID_STATUS_RESPONSE);
   2980      1.1  christos             return 0;
   2981      1.1  christos         }
   2982      1.1  christos         if (ret < 0) {
   2983      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR,
   2984  1.1.1.2  christos                 SSL_R_OCSP_CALLBACK_FAILURE);
   2985      1.1  christos             return 0;
   2986      1.1  christos         }
   2987      1.1  christos     }
   2988      1.1  christos #ifndef OPENSSL_NO_CT
   2989      1.1  christos     if (s->ct_validation_callback != NULL) {
   2990      1.1  christos         /* Note we validate the SCTs whether or not we abort on error */
   2991      1.1  christos         if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
   2992      1.1  christos             /* SSLfatal() already called */
   2993      1.1  christos             return 0;
   2994      1.1  christos         }
   2995      1.1  christos     }
   2996      1.1  christos #endif
   2997      1.1  christos 
   2998      1.1  christos     return 1;
   2999      1.1  christos }
   3000      1.1  christos 
   3001      1.1  christos MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt)
   3002      1.1  christos {
   3003      1.1  christos     if (PACKET_remaining(pkt) > 0) {
   3004      1.1  christos         /* should contain no data */
   3005      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   3006      1.1  christos         return MSG_PROCESS_ERROR;
   3007      1.1  christos     }
   3008      1.1  christos #ifndef OPENSSL_NO_SRP
   3009      1.1  christos     if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
   3010      1.1  christos         if (ssl_srp_calc_a_param_intern(s) <= 0) {
   3011      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
   3012      1.1  christos             return MSG_PROCESS_ERROR;
   3013      1.1  christos         }
   3014      1.1  christos     }
   3015      1.1  christos #endif
   3016      1.1  christos 
   3017      1.1  christos     if (!tls_process_initial_server_flight(s)) {
   3018      1.1  christos         /* SSLfatal() already called */
   3019      1.1  christos         return MSG_PROCESS_ERROR;
   3020      1.1  christos     }
   3021      1.1  christos 
   3022      1.1  christos     return MSG_PROCESS_FINISHED_READING;
   3023      1.1  christos }
   3024      1.1  christos 
   3025      1.1  christos static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt)
   3026      1.1  christos {
   3027      1.1  christos #ifndef OPENSSL_NO_PSK
   3028      1.1  christos     int ret = 0;
   3029      1.1  christos     /*
   3030      1.1  christos      * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
   3031      1.1  christos      * \0-terminated identity. The last byte is for us for simulating
   3032      1.1  christos      * strnlen.
   3033      1.1  christos      */
   3034      1.1  christos     char identity[PSK_MAX_IDENTITY_LEN + 1];
   3035      1.1  christos     size_t identitylen = 0;
   3036      1.1  christos     unsigned char psk[PSK_MAX_PSK_LEN];
   3037      1.1  christos     unsigned char *tmppsk = NULL;
   3038      1.1  christos     char *tmpidentity = NULL;
   3039      1.1  christos     size_t psklen = 0;
   3040      1.1  christos 
   3041      1.1  christos     if (s->psk_client_callback == NULL) {
   3042      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
   3043      1.1  christos         goto err;
   3044      1.1  christos     }
   3045      1.1  christos 
   3046      1.1  christos     memset(identity, 0, sizeof(identity));
   3047      1.1  christos 
   3048      1.1  christos     psklen = s->psk_client_callback(SSL_CONNECTION_GET_USER_SSL(s),
   3049  1.1.1.2  christos         s->session->psk_identity_hint,
   3050  1.1.1.2  christos         identity, sizeof(identity) - 1,
   3051  1.1.1.2  christos         psk, sizeof(psk));
   3052      1.1  christos 
   3053      1.1  christos     if (psklen > PSK_MAX_PSK_LEN) {
   3054      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
   3055  1.1.1.2  christos         psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
   3056      1.1  christos         goto err;
   3057      1.1  christos     } else if (psklen == 0) {
   3058      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
   3059      1.1  christos         goto err;
   3060      1.1  christos     }
   3061      1.1  christos 
   3062      1.1  christos     identitylen = strlen(identity);
   3063      1.1  christos     if (identitylen > PSK_MAX_IDENTITY_LEN) {
   3064      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3065      1.1  christos         goto err;
   3066      1.1  christos     }
   3067      1.1  christos 
   3068      1.1  christos     tmppsk = OPENSSL_memdup(psk, psklen);
   3069      1.1  christos     tmpidentity = OPENSSL_strdup(identity);
   3070      1.1  christos     if (tmppsk == NULL || tmpidentity == NULL) {
   3071      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   3072      1.1  christos         goto err;
   3073      1.1  christos     }
   3074      1.1  christos 
   3075      1.1  christos     OPENSSL_free(s->s3.tmp.psk);
   3076      1.1  christos     s->s3.tmp.psk = tmppsk;
   3077      1.1  christos     s->s3.tmp.psklen = psklen;
   3078      1.1  christos     tmppsk = NULL;
   3079      1.1  christos     OPENSSL_free(s->session->psk_identity);
   3080      1.1  christos     s->session->psk_identity = tmpidentity;
   3081      1.1  christos     tmpidentity = NULL;
   3082      1.1  christos 
   3083  1.1.1.2  christos     if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
   3084      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3085      1.1  christos         goto err;
   3086      1.1  christos     }
   3087      1.1  christos 
   3088      1.1  christos     ret = 1;
   3089      1.1  christos 
   3090  1.1.1.2  christos err:
   3091      1.1  christos     OPENSSL_cleanse(psk, psklen);
   3092      1.1  christos     OPENSSL_cleanse(identity, sizeof(identity));
   3093      1.1  christos     OPENSSL_clear_free(tmppsk, psklen);
   3094      1.1  christos     OPENSSL_clear_free(tmpidentity, identitylen);
   3095      1.1  christos 
   3096      1.1  christos     return ret;
   3097      1.1  christos #else
   3098      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3099      1.1  christos     return 0;
   3100      1.1  christos #endif
   3101      1.1  christos }
   3102      1.1  christos 
   3103      1.1  christos static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt)
   3104      1.1  christos {
   3105      1.1  christos     unsigned char *encdata = NULL;
   3106      1.1  christos     EVP_PKEY *pkey = NULL;
   3107      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   3108      1.1  christos     size_t enclen;
   3109      1.1  christos     unsigned char *pms = NULL;
   3110      1.1  christos     size_t pmslen = 0;
   3111      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   3112      1.1  christos 
   3113      1.1  christos     if (!received_server_cert(s)) {
   3114      1.1  christos         /*
   3115      1.1  christos          * We should always have a server certificate with SSL_kRSA.
   3116      1.1  christos          */
   3117      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3118      1.1  christos         return 0;
   3119      1.1  christos     }
   3120      1.1  christos 
   3121      1.1  christos     if ((pkey = tls_get_peer_pkey(s)) == NULL) {
   3122      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3123      1.1  christos         return 0;
   3124      1.1  christos     }
   3125      1.1  christos 
   3126      1.1  christos     if (!EVP_PKEY_is_a(pkey, "RSA")) {
   3127      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3128      1.1  christos         return 0;
   3129      1.1  christos     }
   3130      1.1  christos 
   3131      1.1  christos     pmslen = SSL_MAX_MASTER_KEY_LENGTH;
   3132      1.1  christos     pms = OPENSSL_malloc(pmslen);
   3133      1.1  christos     if (pms == NULL) {
   3134      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   3135      1.1  christos         return 0;
   3136      1.1  christos     }
   3137      1.1  christos 
   3138      1.1  christos     pms[0] = s->client_version >> 8;
   3139      1.1  christos     pms[1] = s->client_version & 0xff;
   3140      1.1  christos     if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
   3141      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB);
   3142      1.1  christos         goto err;
   3143      1.1  christos     }
   3144      1.1  christos 
   3145      1.1  christos     /* Fix buf for TLS and beyond */
   3146      1.1  christos     if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
   3147      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3148      1.1  christos         goto err;
   3149      1.1  christos     }
   3150      1.1  christos 
   3151      1.1  christos     pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq);
   3152      1.1  christos     if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
   3153      1.1  christos         || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
   3154      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   3155      1.1  christos         goto err;
   3156      1.1  christos     }
   3157      1.1  christos     if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
   3158  1.1.1.2  christos         || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
   3159      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
   3160      1.1  christos         goto err;
   3161      1.1  christos     }
   3162      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3163      1.1  christos     pctx = NULL;
   3164      1.1  christos 
   3165      1.1  christos     /* Fix buf for TLS and beyond */
   3166      1.1  christos     if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
   3167      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3168      1.1  christos         goto err;
   3169      1.1  christos     }
   3170      1.1  christos 
   3171      1.1  christos     /* Log the premaster secret, if logging is enabled. */
   3172      1.1  christos     if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
   3173      1.1  christos         /* SSLfatal() already called */
   3174      1.1  christos         goto err;
   3175      1.1  christos     }
   3176      1.1  christos 
   3177      1.1  christos     s->s3.tmp.pms = pms;
   3178      1.1  christos     s->s3.tmp.pmslen = pmslen;
   3179      1.1  christos 
   3180      1.1  christos     return 1;
   3181  1.1.1.2  christos err:
   3182      1.1  christos     OPENSSL_clear_free(pms, pmslen);
   3183      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3184      1.1  christos 
   3185      1.1  christos     return 0;
   3186      1.1  christos }
   3187      1.1  christos 
   3188      1.1  christos static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt)
   3189      1.1  christos {
   3190      1.1  christos     EVP_PKEY *ckey = NULL, *skey = NULL;
   3191      1.1  christos     unsigned char *keybytes = NULL;
   3192      1.1  christos     int prime_len;
   3193      1.1  christos     unsigned char *encoded_pub = NULL;
   3194      1.1  christos     size_t encoded_pub_len, pad_len;
   3195      1.1  christos     int ret = 0;
   3196      1.1  christos 
   3197      1.1  christos     skey = s->s3.peer_tmp;
   3198      1.1  christos     if (skey == NULL) {
   3199      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3200      1.1  christos         goto err;
   3201      1.1  christos     }
   3202      1.1  christos 
   3203      1.1  christos     ckey = ssl_generate_pkey(s, skey);
   3204      1.1  christos     if (ckey == NULL) {
   3205      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3206      1.1  christos         goto err;
   3207      1.1  christos     }
   3208      1.1  christos 
   3209      1.1  christos     if (ssl_derive(s, ckey, skey, 0) == 0) {
   3210      1.1  christos         /* SSLfatal() already called */
   3211      1.1  christos         goto err;
   3212      1.1  christos     }
   3213      1.1  christos 
   3214      1.1  christos     /* send off the data */
   3215      1.1  christos 
   3216      1.1  christos     /* Generate encoding of server key */
   3217      1.1  christos     encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
   3218      1.1  christos     if (encoded_pub_len == 0) {
   3219      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3220      1.1  christos         EVP_PKEY_free(ckey);
   3221      1.1  christos         return EXT_RETURN_FAIL;
   3222      1.1  christos     }
   3223      1.1  christos 
   3224      1.1  christos     /*
   3225      1.1  christos      * For interoperability with some versions of the Microsoft TLS
   3226      1.1  christos      * stack, we need to zero pad the DHE pub key to the same length
   3227      1.1  christos      * as the prime.
   3228      1.1  christos      */
   3229      1.1  christos     prime_len = EVP_PKEY_get_size(ckey);
   3230      1.1  christos     pad_len = prime_len - encoded_pub_len;
   3231      1.1  christos     if (pad_len > 0) {
   3232      1.1  christos         if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
   3233      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3234      1.1  christos             goto err;
   3235      1.1  christos         }
   3236      1.1  christos         memset(keybytes, 0, pad_len);
   3237      1.1  christos     }
   3238      1.1  christos 
   3239      1.1  christos     if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
   3240      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3241      1.1  christos         goto err;
   3242      1.1  christos     }
   3243      1.1  christos 
   3244      1.1  christos     ret = 1;
   3245  1.1.1.2  christos err:
   3246      1.1  christos     OPENSSL_free(encoded_pub);
   3247      1.1  christos     EVP_PKEY_free(ckey);
   3248      1.1  christos     return ret;
   3249      1.1  christos }
   3250      1.1  christos 
   3251      1.1  christos static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt)
   3252      1.1  christos {
   3253      1.1  christos     unsigned char *encodedPoint = NULL;
   3254      1.1  christos     size_t encoded_pt_len = 0;
   3255      1.1  christos     EVP_PKEY *ckey = NULL, *skey = NULL;
   3256      1.1  christos     int ret = 0;
   3257      1.1  christos 
   3258      1.1  christos     skey = s->s3.peer_tmp;
   3259      1.1  christos     if (skey == NULL) {
   3260      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3261      1.1  christos         return 0;
   3262      1.1  christos     }
   3263      1.1  christos 
   3264      1.1  christos     ckey = ssl_generate_pkey(s, skey);
   3265      1.1  christos     if (ckey == NULL) {
   3266      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
   3267      1.1  christos         goto err;
   3268      1.1  christos     }
   3269      1.1  christos 
   3270      1.1  christos     if (ssl_derive(s, ckey, skey, 0) == 0) {
   3271      1.1  christos         /* SSLfatal() already called */
   3272      1.1  christos         goto err;
   3273      1.1  christos     }
   3274      1.1  christos 
   3275      1.1  christos     /* Generate encoding of client key */
   3276      1.1  christos     encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
   3277      1.1  christos 
   3278      1.1  christos     if (encoded_pt_len == 0) {
   3279      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
   3280      1.1  christos         goto err;
   3281      1.1  christos     }
   3282      1.1  christos 
   3283      1.1  christos     if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
   3284      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3285      1.1  christos         goto err;
   3286      1.1  christos     }
   3287      1.1  christos 
   3288      1.1  christos     ret = 1;
   3289  1.1.1.2  christos err:
   3290      1.1  christos     OPENSSL_free(encodedPoint);
   3291      1.1  christos     EVP_PKEY_free(ckey);
   3292      1.1  christos     return ret;
   3293      1.1  christos }
   3294      1.1  christos 
   3295      1.1  christos static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt)
   3296      1.1  christos {
   3297      1.1  christos #ifndef OPENSSL_NO_GOST
   3298      1.1  christos     /* GOST key exchange message creation */
   3299      1.1  christos     EVP_PKEY_CTX *pkey_ctx = NULL;
   3300      1.1  christos     EVP_PKEY *pkey = NULL;
   3301      1.1  christos     size_t msglen;
   3302      1.1  christos     unsigned int md_len;
   3303      1.1  christos     unsigned char shared_ukm[32], tmp[256];
   3304      1.1  christos     EVP_MD_CTX *ukm_hash = NULL;
   3305      1.1  christos     int dgst_nid = NID_id_GostR3411_94;
   3306      1.1  christos     unsigned char *pms = NULL;
   3307      1.1  christos     size_t pmslen = 0;
   3308      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   3309      1.1  christos 
   3310      1.1  christos     if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
   3311      1.1  christos         dgst_nid = NID_id_GostR3411_2012_256;
   3312      1.1  christos 
   3313      1.1  christos     /*
   3314      1.1  christos      * Get server certificate PKEY and create ctx from it
   3315      1.1  christos      */
   3316      1.1  christos     if ((pkey = tls_get_peer_pkey(s)) == NULL) {
   3317      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
   3318  1.1.1.2  christos             SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
   3319      1.1  christos         return 0;
   3320      1.1  christos     }
   3321      1.1  christos 
   3322      1.1  christos     pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
   3323  1.1.1.2  christos         pkey,
   3324  1.1.1.2  christos         sctx->propq);
   3325      1.1  christos     if (pkey_ctx == NULL) {
   3326      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   3327      1.1  christos         return 0;
   3328      1.1  christos     }
   3329      1.1  christos     /*
   3330      1.1  christos      * If we have send a certificate, and certificate key
   3331      1.1  christos      * parameters match those of server certificate, use
   3332      1.1  christos      * certificate key for key exchange
   3333      1.1  christos      */
   3334      1.1  christos 
   3335      1.1  christos     /* Otherwise, generate ephemeral key pair */
   3336      1.1  christos     pmslen = 32;
   3337      1.1  christos     pms = OPENSSL_malloc(pmslen);
   3338      1.1  christos     if (pms == NULL) {
   3339      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   3340      1.1  christos         goto err;
   3341      1.1  christos     }
   3342      1.1  christos 
   3343      1.1  christos     if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
   3344      1.1  christos         /* Generate session key
   3345      1.1  christos          */
   3346      1.1  christos         || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
   3347      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3348      1.1  christos         goto err;
   3349      1.1  christos     };
   3350      1.1  christos     /*
   3351      1.1  christos      * Compute shared IV and store it in algorithm-specific context
   3352      1.1  christos      * data
   3353      1.1  christos      */
   3354      1.1  christos     ukm_hash = EVP_MD_CTX_new();
   3355      1.1  christos     if (ukm_hash == NULL
   3356      1.1  christos         || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
   3357      1.1  christos         || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
   3358  1.1.1.2  christos                SSL3_RANDOM_SIZE)
   3359  1.1.1.2  christos             <= 0
   3360      1.1  christos         || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
   3361  1.1.1.2  christos                SSL3_RANDOM_SIZE)
   3362  1.1.1.2  christos             <= 0
   3363      1.1  christos         || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
   3364      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3365      1.1  christos         goto err;
   3366      1.1  christos     }
   3367      1.1  christos     EVP_MD_CTX_free(ukm_hash);
   3368      1.1  christos     ukm_hash = NULL;
   3369      1.1  christos     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
   3370  1.1.1.2  christos             EVP_PKEY_CTRL_SET_IV, 8, shared_ukm)
   3371  1.1.1.2  christos         <= 0) {
   3372      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
   3373      1.1  christos         goto err;
   3374      1.1  christos     }
   3375      1.1  christos     /* Make GOST keytransport blob message */
   3376      1.1  christos     /*
   3377      1.1  christos      * Encapsulate it into sequence
   3378      1.1  christos      */
   3379      1.1  christos     msglen = 255;
   3380      1.1  christos     if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
   3381      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
   3382      1.1  christos         goto err;
   3383      1.1  christos     }
   3384      1.1  christos 
   3385      1.1  christos     if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
   3386  1.1.1.2  christos         || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
   3387  1.1.1.2  christos         || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
   3388      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3389      1.1  christos         goto err;
   3390      1.1  christos     }
   3391      1.1  christos 
   3392      1.1  christos     EVP_PKEY_CTX_free(pkey_ctx);
   3393      1.1  christos     s->s3.tmp.pms = pms;
   3394      1.1  christos     s->s3.tmp.pmslen = pmslen;
   3395      1.1  christos 
   3396      1.1  christos     return 1;
   3397  1.1.1.2  christos err:
   3398      1.1  christos     EVP_PKEY_CTX_free(pkey_ctx);
   3399      1.1  christos     OPENSSL_clear_free(pms, pmslen);
   3400      1.1  christos     EVP_MD_CTX_free(ukm_hash);
   3401      1.1  christos     return 0;
   3402      1.1  christos #else
   3403      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3404      1.1  christos     return 0;
   3405      1.1  christos #endif
   3406      1.1  christos }
   3407      1.1  christos 
   3408      1.1  christos #ifndef OPENSSL_NO_GOST
   3409      1.1  christos int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s)
   3410      1.1  christos {
   3411      1.1  christos     if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
   3412      1.1  christos         return NID_magma_ctr;
   3413      1.1  christos     else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
   3414      1.1  christos         return NID_kuznyechik_ctr;
   3415      1.1  christos 
   3416      1.1  christos     return NID_undef;
   3417      1.1  christos }
   3418      1.1  christos 
   3419      1.1  christos int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf)
   3420      1.1  christos {
   3421      1.1  christos     EVP_MD_CTX *hash = NULL;
   3422      1.1  christos     unsigned int md_len;
   3423      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   3424      1.1  christos     const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256,
   3425  1.1.1.2  christos         sctx->propq);
   3426      1.1  christos 
   3427      1.1  christos     if (md == NULL)
   3428      1.1  christos         return 0;
   3429      1.1  christos 
   3430      1.1  christos     if ((hash = EVP_MD_CTX_new()) == NULL
   3431      1.1  christos         || EVP_DigestInit(hash, md) <= 0
   3432      1.1  christos         || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
   3433      1.1  christos         || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
   3434      1.1  christos         || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
   3435      1.1  christos         EVP_MD_CTX_free(hash);
   3436      1.1  christos         ssl_evp_md_free(md);
   3437      1.1  christos         return 0;
   3438      1.1  christos     }
   3439      1.1  christos 
   3440      1.1  christos     EVP_MD_CTX_free(hash);
   3441      1.1  christos     ssl_evp_md_free(md);
   3442      1.1  christos     return 1;
   3443      1.1  christos }
   3444      1.1  christos #endif
   3445      1.1  christos 
   3446      1.1  christos static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt)
   3447      1.1  christos {
   3448      1.1  christos #ifndef OPENSSL_NO_GOST
   3449      1.1  christos     /* GOST 2018 key exchange message creation */
   3450      1.1  christos     unsigned char rnd_dgst[32];
   3451      1.1  christos     unsigned char *encdata = NULL;
   3452      1.1  christos     EVP_PKEY_CTX *pkey_ctx = NULL;
   3453      1.1  christos     EVP_PKEY *pkey;
   3454      1.1  christos     unsigned char *pms = NULL;
   3455      1.1  christos     size_t pmslen = 0;
   3456      1.1  christos     size_t msglen;
   3457      1.1  christos     int cipher_nid = ossl_gost18_cke_cipher_nid(s);
   3458      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   3459      1.1  christos 
   3460      1.1  christos     if (cipher_nid == NID_undef) {
   3461      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3462      1.1  christos         return 0;
   3463      1.1  christos     }
   3464      1.1  christos 
   3465      1.1  christos     if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
   3466      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3467      1.1  christos         goto err;
   3468      1.1  christos     }
   3469      1.1  christos 
   3470      1.1  christos     /* Pre-master secret - random bytes */
   3471      1.1  christos     pmslen = 32;
   3472      1.1  christos     pms = OPENSSL_malloc(pmslen);
   3473      1.1  christos     if (pms == NULL) {
   3474      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   3475      1.1  christos         goto err;
   3476      1.1  christos     }
   3477      1.1  christos 
   3478      1.1  christos     if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
   3479      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3480      1.1  christos         goto err;
   3481      1.1  christos     }
   3482      1.1  christos 
   3483  1.1.1.2  christos     /* Get server certificate PKEY and create ctx from it */
   3484      1.1  christos     if ((pkey = tls_get_peer_pkey(s)) == NULL) {
   3485      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
   3486  1.1.1.2  christos             SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
   3487      1.1  christos         goto err;
   3488      1.1  christos     }
   3489      1.1  christos 
   3490      1.1  christos     pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
   3491  1.1.1.2  christos         pkey,
   3492  1.1.1.2  christos         sctx->propq);
   3493      1.1  christos     if (pkey_ctx == NULL) {
   3494      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   3495      1.1  christos         goto err;
   3496      1.1  christos     }
   3497      1.1  christos 
   3498      1.1  christos     if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) {
   3499      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3500      1.1  christos         goto err;
   3501      1.1  christos     };
   3502      1.1  christos 
   3503      1.1  christos     /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
   3504      1.1  christos     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
   3505  1.1.1.2  christos             EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst)
   3506  1.1.1.2  christos         <= 0) {
   3507      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
   3508      1.1  christos         goto err;
   3509      1.1  christos     }
   3510      1.1  christos 
   3511      1.1  christos     if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
   3512  1.1.1.2  christos             EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL)
   3513  1.1.1.2  christos         <= 0) {
   3514      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
   3515      1.1  christos         goto err;
   3516      1.1  christos     }
   3517      1.1  christos 
   3518      1.1  christos     if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) {
   3519      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   3520      1.1  christos         goto err;
   3521      1.1  christos     }
   3522      1.1  christos 
   3523      1.1  christos     if (!WPACKET_allocate_bytes(pkt, msglen, &encdata)
   3524  1.1.1.2  christos         || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) {
   3525      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
   3526      1.1  christos         goto err;
   3527      1.1  christos     }
   3528      1.1  christos 
   3529      1.1  christos     EVP_PKEY_CTX_free(pkey_ctx);
   3530      1.1  christos     pkey_ctx = NULL;
   3531      1.1  christos     s->s3.tmp.pms = pms;
   3532      1.1  christos     s->s3.tmp.pmslen = pmslen;
   3533      1.1  christos 
   3534      1.1  christos     return 1;
   3535  1.1.1.2  christos err:
   3536      1.1  christos     EVP_PKEY_CTX_free(pkey_ctx);
   3537      1.1  christos     OPENSSL_clear_free(pms, pmslen);
   3538      1.1  christos     return 0;
   3539      1.1  christos #else
   3540      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3541      1.1  christos     return 0;
   3542      1.1  christos #endif
   3543      1.1  christos }
   3544      1.1  christos 
   3545      1.1  christos static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt)
   3546      1.1  christos {
   3547      1.1  christos #ifndef OPENSSL_NO_SRP
   3548      1.1  christos     unsigned char *abytes = NULL;
   3549      1.1  christos 
   3550      1.1  christos     if (s->srp_ctx.A == NULL
   3551  1.1.1.2  christos         || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
   3552  1.1.1.2  christos             &abytes)) {
   3553      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3554      1.1  christos         return 0;
   3555      1.1  christos     }
   3556      1.1  christos     BN_bn2bin(s->srp_ctx.A, abytes);
   3557      1.1  christos 
   3558      1.1  christos     OPENSSL_free(s->session->srp_username);
   3559      1.1  christos     s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
   3560      1.1  christos     if (s->session->srp_username == NULL) {
   3561      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
   3562      1.1  christos         return 0;
   3563      1.1  christos     }
   3564      1.1  christos 
   3565      1.1  christos     return 1;
   3566      1.1  christos #else
   3567      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3568      1.1  christos     return 0;
   3569      1.1  christos #endif
   3570      1.1  christos }
   3571      1.1  christos 
   3572      1.1  christos CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s,
   3573  1.1.1.2  christos     WPACKET *pkt)
   3574      1.1  christos {
   3575      1.1  christos     unsigned long alg_k;
   3576      1.1  christos 
   3577      1.1  christos     alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
   3578      1.1  christos 
   3579      1.1  christos     /*
   3580      1.1  christos      * All of the construct functions below call SSLfatal() if necessary so
   3581      1.1  christos      * no need to do so here.
   3582      1.1  christos      */
   3583      1.1  christos     if ((alg_k & SSL_PSK)
   3584      1.1  christos         && !tls_construct_cke_psk_preamble(s, pkt))
   3585      1.1  christos         goto err;
   3586      1.1  christos 
   3587      1.1  christos     if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
   3588      1.1  christos         if (!tls_construct_cke_rsa(s, pkt))
   3589      1.1  christos             goto err;
   3590      1.1  christos     } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
   3591      1.1  christos         if (!tls_construct_cke_dhe(s, pkt))
   3592      1.1  christos             goto err;
   3593      1.1  christos     } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
   3594      1.1  christos         if (!tls_construct_cke_ecdhe(s, pkt))
   3595      1.1  christos             goto err;
   3596      1.1  christos     } else if (alg_k & SSL_kGOST) {
   3597      1.1  christos         if (!tls_construct_cke_gost(s, pkt))
   3598      1.1  christos             goto err;
   3599      1.1  christos     } else if (alg_k & SSL_kGOST18) {
   3600      1.1  christos         if (!tls_construct_cke_gost18(s, pkt))
   3601      1.1  christos             goto err;
   3602      1.1  christos     } else if (alg_k & SSL_kSRP) {
   3603      1.1  christos         if (!tls_construct_cke_srp(s, pkt))
   3604      1.1  christos             goto err;
   3605      1.1  christos     } else if (!(alg_k & SSL_kPSK)) {
   3606      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3607      1.1  christos         goto err;
   3608      1.1  christos     }
   3609      1.1  christos 
   3610      1.1  christos     return CON_FUNC_SUCCESS;
   3611  1.1.1.2  christos err:
   3612      1.1  christos     OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
   3613      1.1  christos     s->s3.tmp.pms = NULL;
   3614      1.1  christos     s->s3.tmp.pmslen = 0;
   3615      1.1  christos #ifndef OPENSSL_NO_PSK
   3616      1.1  christos     OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
   3617      1.1  christos     s->s3.tmp.psk = NULL;
   3618      1.1  christos     s->s3.tmp.psklen = 0;
   3619      1.1  christos #endif
   3620      1.1  christos     return CON_FUNC_ERROR;
   3621      1.1  christos }
   3622      1.1  christos 
   3623      1.1  christos int tls_client_key_exchange_post_work(SSL_CONNECTION *s)
   3624      1.1  christos {
   3625      1.1  christos     unsigned char *pms = NULL;
   3626      1.1  christos     size_t pmslen = 0;
   3627      1.1  christos 
   3628      1.1  christos     pms = s->s3.tmp.pms;
   3629      1.1  christos     pmslen = s->s3.tmp.pmslen;
   3630      1.1  christos 
   3631      1.1  christos #ifndef OPENSSL_NO_SRP
   3632      1.1  christos     /* Check for SRP */
   3633      1.1  christos     if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
   3634      1.1  christos         if (!srp_generate_client_master_secret(s)) {
   3635      1.1  christos             /* SSLfatal() already called */
   3636      1.1  christos             goto err;
   3637      1.1  christos         }
   3638      1.1  christos         return 1;
   3639      1.1  christos     }
   3640      1.1  christos #endif
   3641      1.1  christos 
   3642      1.1  christos     if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
   3643      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT);
   3644      1.1  christos         goto err;
   3645      1.1  christos     }
   3646      1.1  christos     if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
   3647      1.1  christos         /* SSLfatal() already called */
   3648      1.1  christos         /* ssl_generate_master_secret frees the pms even on error */
   3649      1.1  christos         pms = NULL;
   3650      1.1  christos         pmslen = 0;
   3651      1.1  christos         goto err;
   3652      1.1  christos     }
   3653      1.1  christos     pms = NULL;
   3654      1.1  christos     pmslen = 0;
   3655      1.1  christos 
   3656      1.1  christos #ifndef OPENSSL_NO_SCTP
   3657      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s)) {
   3658      1.1  christos         unsigned char sctpauthkey[64];
   3659      1.1  christos         char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
   3660      1.1  christos         size_t labellen;
   3661      1.1  christos         SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   3662      1.1  christos 
   3663      1.1  christos         /*
   3664      1.1  christos          * Add new shared key for SCTP-Auth, will be ignored if no SCTP
   3665      1.1  christos          * used.
   3666      1.1  christos          */
   3667      1.1  christos         memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
   3668  1.1.1.2  christos             sizeof(DTLS1_SCTP_AUTH_LABEL));
   3669      1.1  christos 
   3670      1.1  christos         /* Don't include the terminating zero. */
   3671      1.1  christos         labellen = sizeof(labelbuffer) - 1;
   3672      1.1  christos         if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
   3673      1.1  christos             labellen += 1;
   3674      1.1  christos 
   3675      1.1  christos         if (SSL_export_keying_material(ssl, sctpauthkey,
   3676  1.1.1.2  christos                 sizeof(sctpauthkey), labelbuffer,
   3677  1.1.1.2  christos                 labellen, NULL, 0, 0)
   3678  1.1.1.2  christos             <= 0) {
   3679      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3680      1.1  christos             goto err;
   3681      1.1  christos         }
   3682      1.1  christos 
   3683      1.1  christos         BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
   3684  1.1.1.2  christos             sizeof(sctpauthkey), sctpauthkey);
   3685      1.1  christos     }
   3686      1.1  christos #endif
   3687      1.1  christos 
   3688      1.1  christos     return 1;
   3689  1.1.1.2  christos err:
   3690      1.1  christos     OPENSSL_clear_free(pms, pmslen);
   3691      1.1  christos     s->s3.tmp.pms = NULL;
   3692      1.1  christos     s->s3.tmp.pmslen = 0;
   3693      1.1  christos     return 0;
   3694      1.1  christos }
   3695      1.1  christos 
   3696      1.1  christos /*
   3697      1.1  christos  * Check a certificate can be used for client authentication. Currently check
   3698      1.1  christos  * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
   3699      1.1  christos  * certificates can be used and optionally checks suitability for Suite B.
   3700      1.1  christos  */
   3701      1.1  christos static int ssl3_check_client_certificate(SSL_CONNECTION *s)
   3702      1.1  christos {
   3703      1.1  christos     /* If no suitable signature algorithm can't use certificate */
   3704      1.1  christos     if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
   3705      1.1  christos         return 0;
   3706      1.1  christos     /*
   3707      1.1  christos      * If strict mode check suitability of chain before using it. This also
   3708      1.1  christos      * adjusts suite B digest if necessary.
   3709      1.1  christos      */
   3710  1.1.1.2  christos     if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && !tls1_check_chain(s, NULL, NULL, NULL, -2))
   3711      1.1  christos         return 0;
   3712      1.1  christos     return 1;
   3713      1.1  christos }
   3714      1.1  christos 
   3715      1.1  christos WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst)
   3716      1.1  christos {
   3717      1.1  christos     X509 *x509 = NULL;
   3718      1.1  christos     EVP_PKEY *pkey = NULL;
   3719      1.1  christos     int i;
   3720      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   3721      1.1  christos 
   3722      1.1  christos     if (wst == WORK_MORE_A) {
   3723      1.1  christos         /* Let cert callback update client certificates if required */
   3724      1.1  christos         if (s->cert->cert_cb) {
   3725      1.1  christos             i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
   3726      1.1  christos             if (i < 0) {
   3727      1.1  christos                 s->rwstate = SSL_X509_LOOKUP;
   3728      1.1  christos                 return WORK_MORE_A;
   3729      1.1  christos             }
   3730      1.1  christos             if (i == 0) {
   3731      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
   3732      1.1  christos                 return WORK_ERROR;
   3733      1.1  christos             }
   3734      1.1  christos             s->rwstate = SSL_NOTHING;
   3735      1.1  christos         }
   3736      1.1  christos         if (ssl3_check_client_certificate(s)) {
   3737      1.1  christos             if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
   3738      1.1  christos                 return WORK_FINISHED_STOP;
   3739      1.1  christos             }
   3740      1.1  christos             return WORK_FINISHED_CONTINUE;
   3741      1.1  christos         }
   3742      1.1  christos 
   3743      1.1  christos         /* Fall through to WORK_MORE_B */
   3744      1.1  christos         wst = WORK_MORE_B;
   3745      1.1  christos     }
   3746      1.1  christos 
   3747      1.1  christos     /* We need to get a client cert */
   3748      1.1  christos     if (wst == WORK_MORE_B) {
   3749      1.1  christos         /*
   3750      1.1  christos          * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
   3751      1.1  christos          * return(-1); We then get retied later
   3752      1.1  christos          */
   3753      1.1  christos         i = ssl_do_client_cert_cb(s, &x509, &pkey);
   3754      1.1  christos         if (i < 0) {
   3755      1.1  christos             s->rwstate = SSL_X509_LOOKUP;
   3756      1.1  christos             return WORK_MORE_B;
   3757      1.1  christos         }
   3758      1.1  christos         s->rwstate = SSL_NOTHING;
   3759      1.1  christos         if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
   3760      1.1  christos             if (!SSL_use_certificate(ssl, x509)
   3761      1.1  christos                 || !SSL_use_PrivateKey(ssl, pkey))
   3762      1.1  christos                 i = 0;
   3763      1.1  christos         } else if (i == 1) {
   3764      1.1  christos             i = 0;
   3765      1.1  christos             ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
   3766      1.1  christos         }
   3767      1.1  christos 
   3768      1.1  christos         X509_free(x509);
   3769      1.1  christos         EVP_PKEY_free(pkey);
   3770      1.1  christos         if (i && !ssl3_check_client_certificate(s))
   3771      1.1  christos             i = 0;
   3772      1.1  christos         if (i == 0) {
   3773      1.1  christos             if (s->version == SSL3_VERSION) {
   3774      1.1  christos                 s->s3.tmp.cert_req = 0;
   3775      1.1  christos                 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
   3776      1.1  christos                 return WORK_FINISHED_CONTINUE;
   3777      1.1  christos             } else {
   3778      1.1  christos                 s->s3.tmp.cert_req = 2;
   3779      1.1  christos                 s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
   3780      1.1  christos                 if (!ssl3_digest_cached_records(s, 0)) {
   3781      1.1  christos                     /* SSLfatal() already called */
   3782      1.1  christos                     return WORK_ERROR;
   3783      1.1  christos                 }
   3784      1.1  christos             }
   3785      1.1  christos         }
   3786      1.1  christos 
   3787      1.1  christos         if (!SSL_CONNECTION_IS_TLS13(s)
   3788  1.1.1.2  christos             || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
   3789      1.1  christos             s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
   3790      1.1  christos 
   3791      1.1  christos         if (s->post_handshake_auth == SSL_PHA_REQUESTED)
   3792      1.1  christos             return WORK_FINISHED_STOP;
   3793      1.1  christos         return WORK_FINISHED_CONTINUE;
   3794      1.1  christos     }
   3795      1.1  christos 
   3796      1.1  christos     /* Shouldn't ever get here */
   3797      1.1  christos     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3798      1.1  christos     return WORK_ERROR;
   3799      1.1  christos }
   3800      1.1  christos 
   3801      1.1  christos CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s,
   3802  1.1.1.2  christos     WPACKET *pkt)
   3803      1.1  christos {
   3804      1.1  christos     CERT_PKEY *cpk = NULL;
   3805      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   3806      1.1  christos 
   3807      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)) {
   3808      1.1  christos         if (s->pha_context == NULL) {
   3809      1.1  christos             /* no context available, add 0-length context */
   3810      1.1  christos             if (!WPACKET_put_bytes_u8(pkt, 0)) {
   3811      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3812      1.1  christos                 return CON_FUNC_ERROR;
   3813      1.1  christos             }
   3814      1.1  christos         } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
   3815      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3816      1.1  christos             return CON_FUNC_ERROR;
   3817      1.1  christos         }
   3818      1.1  christos     }
   3819      1.1  christos     if (s->s3.tmp.cert_req != 2)
   3820      1.1  christos         cpk = s->cert->key;
   3821      1.1  christos     switch (s->ext.client_cert_type) {
   3822      1.1  christos     case TLSEXT_cert_type_rpk:
   3823      1.1  christos         if (!tls_output_rpk(s, pkt, cpk)) {
   3824      1.1  christos             /* SSLfatal() already called */
   3825      1.1  christos             return CON_FUNC_ERROR;
   3826      1.1  christos         }
   3827      1.1  christos         break;
   3828      1.1  christos     case TLSEXT_cert_type_x509:
   3829      1.1  christos         if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
   3830      1.1  christos             /* SSLfatal() already called */
   3831      1.1  christos             return CON_FUNC_ERROR;
   3832      1.1  christos         }
   3833      1.1  christos         break;
   3834      1.1  christos     default:
   3835      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3836      1.1  christos         return CON_FUNC_ERROR;
   3837      1.1  christos     }
   3838      1.1  christos 
   3839      1.1  christos     /*
   3840      1.1  christos      * If we attempted to write early data or we're in middlebox compat mode
   3841      1.1  christos      * then we deferred changing the handshake write keys to the last possible
   3842      1.1  christos      * moment. We need to do it now.
   3843      1.1  christos      */
   3844      1.1  christos     if (SSL_CONNECTION_IS_TLS13(s)
   3845  1.1.1.2  christos         && !SSL_IS_QUIC_HANDSHAKE(s)
   3846  1.1.1.2  christos         && SSL_IS_FIRST_HANDSHAKE(s)
   3847  1.1.1.2  christos         && (s->early_data_state != SSL_EARLY_DATA_NONE
   3848  1.1.1.2  christos             || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
   3849  1.1.1.2  christos         && (!ssl->method->ssl3_enc->change_cipher_state(s,
   3850  1.1.1.2  christos             SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
   3851      1.1  christos         /*
   3852      1.1  christos          * This is a fatal error, which leaves enc_write_ctx in an inconsistent
   3853      1.1  christos          * state and thus ssl3_send_alert may crash.
   3854      1.1  christos          */
   3855      1.1  christos         SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
   3856      1.1  christos         return CON_FUNC_ERROR;
   3857      1.1  christos     }
   3858      1.1  christos 
   3859      1.1  christos     return CON_FUNC_SUCCESS;
   3860      1.1  christos }
   3861      1.1  christos 
   3862      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
   3863      1.1  christos CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc,
   3864  1.1.1.2  christos     WPACKET *pkt)
   3865      1.1  christos {
   3866      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(sc);
   3867      1.1  christos     WPACKET tmppkt;
   3868      1.1  christos     BUF_MEM *buf = NULL;
   3869      1.1  christos     size_t length;
   3870      1.1  christos     size_t max_length;
   3871      1.1  christos     COMP_METHOD *method;
   3872      1.1  christos     COMP_CTX *comp = NULL;
   3873      1.1  christos     int comp_len;
   3874      1.1  christos     int ret = 0;
   3875      1.1  christos     int alg = sc->ext.compress_certificate_from_peer[0];
   3876      1.1  christos 
   3877      1.1  christos     /* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */
   3878      1.1  christos 
   3879      1.1  christos     if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf))
   3880      1.1  christos         goto err;
   3881      1.1  christos 
   3882      1.1  christos     /* Use the |tmppkt| for the to-be-compressed data */
   3883      1.1  christos     if (sc->pha_context == NULL) {
   3884      1.1  christos         /* no context available, add 0-length context */
   3885      1.1  christos         if (!WPACKET_put_bytes_u8(&tmppkt, 0))
   3886      1.1  christos             goto err;
   3887      1.1  christos     } else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len))
   3888      1.1  christos         goto err;
   3889      1.1  christos 
   3890      1.1  christos     if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) {
   3891      1.1  christos         /* SSLfatal() already called */
   3892      1.1  christos         goto out;
   3893      1.1  christos     }
   3894      1.1  christos 
   3895      1.1  christos     /* continue with the real |pkt| */
   3896      1.1  christos     if (!WPACKET_put_bytes_u16(pkt, alg)
   3897  1.1.1.2  christos         || !WPACKET_get_total_written(&tmppkt, &length)
   3898  1.1.1.2  christos         || !WPACKET_put_bytes_u24(pkt, length))
   3899      1.1  christos         goto err;
   3900      1.1  christos 
   3901      1.1  christos     switch (alg) {
   3902      1.1  christos     case TLSEXT_comp_cert_zlib:
   3903      1.1  christos         method = COMP_zlib_oneshot();
   3904      1.1  christos         break;
   3905      1.1  christos     case TLSEXT_comp_cert_brotli:
   3906      1.1  christos         method = COMP_brotli_oneshot();
   3907      1.1  christos         break;
   3908      1.1  christos     case TLSEXT_comp_cert_zstd:
   3909      1.1  christos         method = COMP_zstd_oneshot();
   3910      1.1  christos         break;
   3911      1.1  christos     default:
   3912      1.1  christos         goto err;
   3913      1.1  christos     }
   3914      1.1  christos     max_length = ossl_calculate_comp_expansion(alg, length);
   3915      1.1  christos 
   3916      1.1  christos     if ((comp = COMP_CTX_new(method)) == NULL
   3917  1.1.1.2  christos         || !WPACKET_start_sub_packet_u24(pkt)
   3918  1.1.1.2  christos         || !WPACKET_reserve_bytes(pkt, max_length, NULL))
   3919      1.1  christos         goto err;
   3920      1.1  christos 
   3921      1.1  christos     comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), max_length,
   3922  1.1.1.2  christos         (unsigned char *)buf->data, length);
   3923      1.1  christos     if (comp_len <= 0)
   3924      1.1  christos         goto err;
   3925      1.1  christos 
   3926      1.1  christos     if (!WPACKET_allocate_bytes(pkt, comp_len, NULL)
   3927  1.1.1.2  christos         || !WPACKET_close(pkt))
   3928      1.1  christos         goto err;
   3929      1.1  christos 
   3930      1.1  christos     /*
   3931      1.1  christos      * If we attempted to write early data or we're in middlebox compat mode
   3932      1.1  christos      * then we deferred changing the handshake write keys to the last possible
   3933      1.1  christos      * moment. We need to do it now.
   3934      1.1  christos      */
   3935      1.1  christos     if (SSL_IS_FIRST_HANDSHAKE(sc)
   3936  1.1.1.2  christos         && !SSL_IS_QUIC_HANDSHAKE(sc)
   3937  1.1.1.2  christos         && (sc->early_data_state != SSL_EARLY_DATA_NONE
   3938  1.1.1.2  christos             || (sc->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
   3939  1.1.1.2  christos         && (!ssl->method->ssl3_enc->change_cipher_state(sc,
   3940  1.1.1.2  christos             SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
   3941      1.1  christos         /*
   3942      1.1  christos          * This is a fatal error, which leaves sc->enc_write_ctx in an
   3943      1.1  christos          * inconsistent state and thus ssl3_send_alert may crash.
   3944      1.1  christos          */
   3945      1.1  christos         SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
   3946      1.1  christos         goto out;
   3947      1.1  christos     }
   3948      1.1  christos     ret = 1;
   3949      1.1  christos     goto out;
   3950      1.1  christos 
   3951  1.1.1.2  christos err:
   3952      1.1  christos     SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3953  1.1.1.2  christos out:
   3954      1.1  christos     if (buf != NULL) {
   3955      1.1  christos         /* If |buf| is NULL, then |tmppkt| could not have been initialized */
   3956      1.1  christos         WPACKET_cleanup(&tmppkt);
   3957      1.1  christos     }
   3958      1.1  christos     BUF_MEM_free(buf);
   3959      1.1  christos     COMP_CTX_free(comp);
   3960      1.1  christos     return ret;
   3961      1.1  christos }
   3962      1.1  christos #endif
   3963      1.1  christos 
   3964      1.1  christos int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s)
   3965      1.1  christos {
   3966      1.1  christos     const SSL_CERT_LOOKUP *clu;
   3967      1.1  christos     size_t idx;
   3968      1.1  christos     long alg_k, alg_a;
   3969      1.1  christos     EVP_PKEY *pkey;
   3970      1.1  christos 
   3971      1.1  christos     alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
   3972      1.1  christos     alg_a = s->s3.tmp.new_cipher->algorithm_auth;
   3973      1.1  christos 
   3974      1.1  christos     /* we don't have a certificate */
   3975      1.1  christos     if (!(alg_a & SSL_aCERT))
   3976      1.1  christos         return 1;
   3977      1.1  christos 
   3978      1.1  christos     /* This is the passed certificate */
   3979      1.1  christos     pkey = tls_get_peer_pkey(s);
   3980      1.1  christos     clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s));
   3981      1.1  christos 
   3982      1.1  christos     /* Check certificate is recognised and suitable for cipher */
   3983      1.1  christos     if (clu == NULL || (alg_a & clu->amask) == 0) {
   3984      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
   3985      1.1  christos         return 0;
   3986      1.1  christos     }
   3987      1.1  christos 
   3988      1.1  christos     if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
   3989      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
   3990  1.1.1.2  christos             SSL_R_MISSING_RSA_ENCRYPTING_CERT);
   3991      1.1  christos         return 0;
   3992      1.1  christos     }
   3993      1.1  christos 
   3994      1.1  christos     if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
   3995      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   3996      1.1  christos         return 0;
   3997      1.1  christos     }
   3998      1.1  christos 
   3999      1.1  christos     /* Early out to skip the checks below */
   4000      1.1  christos     if (s->session->peer_rpk != NULL)
   4001      1.1  christos         return 1;
   4002      1.1  christos 
   4003      1.1  christos     if (clu->amask & SSL_aECDSA) {
   4004      1.1  christos         if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
   4005      1.1  christos             return 1;
   4006      1.1  christos         SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
   4007      1.1  christos         return 0;
   4008      1.1  christos     }
   4009      1.1  christos 
   4010      1.1  christos     return 1;
   4011      1.1  christos }
   4012      1.1  christos 
   4013      1.1  christos #ifndef OPENSSL_NO_NEXTPROTONEG
   4014      1.1  christos CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt)
   4015      1.1  christos {
   4016      1.1  christos     size_t len, padding_len;
   4017      1.1  christos     unsigned char *padding = NULL;
   4018      1.1  christos 
   4019      1.1  christos     len = s->ext.npn_len;
   4020      1.1  christos     padding_len = 32 - ((len + 2) % 32);
   4021      1.1  christos 
   4022      1.1  christos     if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
   4023  1.1.1.2  christos         || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
   4024      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   4025      1.1  christos         return CON_FUNC_ERROR;
   4026      1.1  christos     }
   4027      1.1  christos 
   4028      1.1  christos     memset(padding, 0, padding_len);
   4029      1.1  christos 
   4030      1.1  christos     return CON_FUNC_SUCCESS;
   4031      1.1  christos }
   4032      1.1  christos #endif
   4033      1.1  christos 
   4034      1.1  christos MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt)
   4035      1.1  christos {
   4036      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   4037      1.1  christos 
   4038      1.1  christos     if (PACKET_remaining(pkt) > 0) {
   4039      1.1  christos         /* should contain no data */
   4040      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   4041      1.1  christos         return MSG_PROCESS_ERROR;
   4042      1.1  christos     }
   4043      1.1  christos 
   4044      1.1  christos     if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
   4045      1.1  christos         ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
   4046      1.1  christos         return MSG_PROCESS_FINISHED_READING;
   4047      1.1  christos     }
   4048      1.1  christos 
   4049      1.1  christos     /*
   4050      1.1  christos      * This is a historical discrepancy (not in the RFC) maintained for
   4051      1.1  christos      * compatibility reasons. If a TLS client receives a HelloRequest it will
   4052      1.1  christos      * attempt an abbreviated handshake. However if a DTLS client receives a
   4053      1.1  christos      * HelloRequest it will do a full handshake. Either behaviour is reasonable
   4054      1.1  christos      * but doing one for TLS and another for DTLS is odd.
   4055      1.1  christos      */
   4056      1.1  christos     if (SSL_CONNECTION_IS_DTLS(s))
   4057      1.1  christos         SSL_renegotiate(ssl);
   4058      1.1  christos     else
   4059      1.1  christos         SSL_renegotiate_abbreviated(ssl);
   4060      1.1  christos 
   4061      1.1  christos     return MSG_PROCESS_FINISHED_READING;
   4062      1.1  christos }
   4063      1.1  christos 
   4064      1.1  christos static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
   4065  1.1.1.2  christos     PACKET *pkt)
   4066      1.1  christos {
   4067      1.1  christos     PACKET extensions;
   4068      1.1  christos     RAW_EXTENSION *rawexts = NULL;
   4069      1.1  christos 
   4070      1.1  christos     if (!PACKET_as_length_prefixed_2(pkt, &extensions)
   4071  1.1.1.2  christos         || PACKET_remaining(pkt) != 0) {
   4072      1.1  christos         SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
   4073      1.1  christos         goto err;
   4074      1.1  christos     }
   4075      1.1  christos 
   4076      1.1  christos     if (!tls_collect_extensions(s, &extensions,
   4077  1.1.1.2  christos             SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
   4078  1.1.1.2  christos             NULL, 1)
   4079  1.1.1.2  christos         || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
   4080  1.1.1.2  christos             rawexts, NULL, 0, 1)) {
   4081      1.1  christos         /* SSLfatal() already called */
   4082      1.1  christos         goto err;
   4083      1.1  christos     }
   4084      1.1  christos 
   4085      1.1  christos     OPENSSL_free(rawexts);
   4086      1.1  christos     return MSG_PROCESS_CONTINUE_READING;
   4087      1.1  christos 
   4088  1.1.1.2  christos err:
   4089      1.1  christos     OPENSSL_free(rawexts);
   4090      1.1  christos     return MSG_PROCESS_ERROR;
   4091      1.1  christos }
   4092      1.1  christos 
   4093      1.1  christos int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey)
   4094      1.1  christos {
   4095      1.1  christos     int i = 0;
   4096      1.1  christos     SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
   4097      1.1  christos 
   4098      1.1  christos #ifndef OPENSSL_NO_ENGINE
   4099      1.1  christos     if (sctx->client_cert_engine) {
   4100      1.1  christos         i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
   4101      1.1  christos         if (i != 0)
   4102      1.1  christos             return i;
   4103      1.1  christos     }
   4104      1.1  christos #endif
   4105      1.1  christos     if (sctx->client_cert_cb)
   4106      1.1  christos         i = sctx->client_cert_cb(SSL_CONNECTION_GET_USER_SSL(s), px509, ppkey);
   4107      1.1  christos     return i;
   4108      1.1  christos }
   4109      1.1  christos 
   4110      1.1  christos int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
   4111  1.1.1.2  christos     WPACKET *pkt)
   4112      1.1  christos {
   4113      1.1  christos     int i;
   4114      1.1  christos     size_t totlen = 0, len, maxlen, maxverok = 0;
   4115      1.1  christos     int empty_reneg_info_scsv = !s->renegotiate
   4116  1.1.1.2  christos         && !SSL_CONNECTION_IS_DTLS(s)
   4117  1.1.1.2  christos         && ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
   4118  1.1.1.2  christos         && s->min_proto_version <= TLS1_VERSION;
   4119      1.1  christos     SSL *ssl = SSL_CONNECTION_GET_SSL(s);
   4120      1.1  christos 
   4121      1.1  christos     /* Set disabled masks for this session */
   4122      1.1  christos     if (!ssl_set_client_disabled(s)) {
   4123      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
   4124      1.1  christos         return 0;
   4125      1.1  christos     }
   4126      1.1  christos 
   4127      1.1  christos     if (sk == NULL) {
   4128      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   4129      1.1  christos         return 0;
   4130      1.1  christos     }
   4131      1.1  christos 
   4132      1.1  christos #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
   4133  1.1.1.2  christos #if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
   4134  1.1.1.2  christos #error Max cipher length too short
   4135  1.1.1.2  christos #endif
   4136      1.1  christos     /*
   4137      1.1  christos      * Some servers hang if client hello > 256 bytes as hack workaround
   4138      1.1  christos      * chop number of supported ciphers to keep it well below this if we
   4139      1.1  christos      * use TLS v1.2
   4140      1.1  christos      */
   4141      1.1  christos     if (TLS1_get_version(ssl) >= TLS1_2_VERSION)
   4142      1.1  christos         maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
   4143      1.1  christos     else
   4144      1.1  christos #endif
   4145      1.1  christos         /* Maximum length that can be stored in 2 bytes. Length must be even */
   4146      1.1  christos         maxlen = 0xfffe;
   4147      1.1  christos 
   4148      1.1  christos     if (empty_reneg_info_scsv)
   4149      1.1  christos         maxlen -= 2;
   4150      1.1  christos     if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
   4151      1.1  christos         maxlen -= 2;
   4152      1.1  christos 
   4153      1.1  christos     for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
   4154      1.1  christos         const SSL_CIPHER *c;
   4155      1.1  christos 
   4156      1.1  christos         c = sk_SSL_CIPHER_value(sk, i);
   4157      1.1  christos         /* Skip disabled ciphers */
   4158      1.1  christos         if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
   4159      1.1  christos             continue;
   4160      1.1  christos 
   4161      1.1  christos         if (!ssl->method->put_cipher_by_char(c, pkt, &len)) {
   4162      1.1  christos             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   4163      1.1  christos             return 0;
   4164      1.1  christos         }
   4165      1.1  christos 
   4166      1.1  christos         /* Sanity check that the maximum version we offer has ciphers enabled */
   4167      1.1  christos         if (!maxverok) {
   4168      1.1  christos             int minproto = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls;
   4169      1.1  christos             int maxproto = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls;
   4170      1.1  christos 
   4171      1.1  christos             if (ssl_version_cmp(s, maxproto, s->s3.tmp.max_ver) >= 0
   4172  1.1.1.2  christos                 && ssl_version_cmp(s, minproto, s->s3.tmp.max_ver) <= 0)
   4173      1.1  christos                 maxverok = 1;
   4174      1.1  christos         }
   4175      1.1  christos 
   4176      1.1  christos         totlen += len;
   4177      1.1  christos     }
   4178      1.1  christos 
   4179      1.1  christos     if (totlen == 0 || !maxverok) {
   4180  1.1.1.2  christos         const char *maxvertext = !maxverok
   4181      1.1  christos             ? "No ciphers enabled for max supported SSL/TLS version"
   4182      1.1  christos             : NULL;
   4183      1.1  christos 
   4184      1.1  christos         SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
   4185  1.1.1.2  christos             maxvertext);
   4186      1.1  christos         return 0;
   4187      1.1  christos     }
   4188      1.1  christos 
   4189      1.1  christos     if (totlen != 0) {
   4190      1.1  christos         if (empty_reneg_info_scsv) {
   4191      1.1  christos             static const SSL_CIPHER scsv = {
   4192      1.1  christos                 0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
   4193      1.1  christos             };
   4194      1.1  christos             if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
   4195      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   4196      1.1  christos                 return 0;
   4197      1.1  christos             }
   4198      1.1  christos         }
   4199      1.1  christos         if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
   4200      1.1  christos             static const SSL_CIPHER scsv = {
   4201      1.1  christos                 0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
   4202      1.1  christos             };
   4203      1.1  christos             if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
   4204      1.1  christos                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
   4205      1.1  christos                 return 0;
   4206      1.1  christos             }
   4207      1.1  christos         }
   4208      1.1  christos     }
   4209      1.1  christos 
   4210      1.1  christos     return 1;
   4211      1.1  christos }
   4212      1.1  christos 
   4213      1.1  christos CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt)
   4214      1.1  christos {
   4215      1.1  christos     if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
   4216  1.1.1.2  christos         && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
   4217      1.1  christos         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
   4218      1.1  christos         return CON_FUNC_ERROR;
   4219      1.1  christos     }
   4220      1.1  christos 
   4221      1.1  christos     s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
   4222      1.1  christos     return CON_FUNC_SUCCESS;
   4223      1.1  christos }
   4224