Ignore:
Timestamp:
Jun 22, 2021, 9:00:19 PM (3 years ago)
Author:
coas-nagasima
Message:

WolfSSLとAzure IoT SDKを更新

Location:
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/src/tls13.c

    r457 r464  
    3030 * NO_PSK
    3131 *    Do not enable Pre-Shared Keys.
    32  * TLS13_SUPPORTS_EXPORTERS
    33  *    Guard to compile out any code for exporter keys.
    34  *    Feature not supported yet.
     32 * HAVE_KEYING_MATERIAL
     33 *    Enables exporting keying material based on section 7.5 of RFC 8446.
    3534 * WOLFSSL_ASYNC_CRYPT
    3635 *    Enables the use of asynchronous cryptographic operations.
     
    5655 * WOLFSSL_TLS13
    5756 *    Enable TLS 1.3 protocol implementation.
    58  * WOLFSSL_TLS13_DRAFT_18
    59  *    Conform with Draft 18 of the TLS v1.3 specification.
    60  * WOLFSSL_TLS13_DRAFT_22
    61  *    Conform with Draft 22 of the TLS v1.3 specification.
    62  * WOLFSSL_TLS13_DRAFT_23
    63  *    Conform with Draft 23 of the TLS v1.3 specification.
    6457 * WOLFSSL_TLS13_MIDDLEBOX_COMPAT
    6558 *    Enable middlebox compatibility in the TLS 1.3 handshake.
     
    119112
    120113#ifndef HAVE_HKDF
    121     #error The build option HAVE_HKDF is required for TLS 1.3
     114    #ifndef _MSC_VER
     115        #error "The build option HAVE_HKDF is required for TLS 1.3"
     116    #else
     117        #pragma message("error: The build option HAVE_HKDF is required for TLS 1.3")
     118    #endif
    122119#endif
    123120
     
    264261}
    265262
    266 #ifdef WOLFSSL_TLS13_DRAFT_18
    267 /* Size of the TLS v1.3 label use when deriving keys. */
    268 #define TLS13_PROTOCOL_LABEL_SZ    9
    269 /* The protocol label for TLS v1.3. */
    270 static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "TLS 1.3, ";
    271 #else
    272263/* Size of the TLS v1.3 label use when deriving keys. */
    273264#define TLS13_PROTOCOL_LABEL_SZ    6
    274265/* The protocol label for TLS v1.3. */
    275266static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 ";
    276 #endif
    277 
    278 #if !defined(WOLFSSL_TLS13_DRAFT_18) || defined(HAVE_SESSION_TICKET) || \
    279                                         !defined(NO_PSK)
     267
    280268/* Derive a key from a message.
    281269 *
     
    370358                             hash, hashSz, digestAlg);
    371359}
    372 #endif
    373360
    374361/* Derive a key.
     
    442429
    443430#ifndef NO_PSK
    444 #ifdef WOLFSSL_TLS13_DRAFT_18
    445 /* The length of the binder key label. */
    446 #define BINDER_KEY_LABEL_SZ         23
    447 /* The binder key label. */
    448 static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] =
    449     "external psk binder key";
    450 #else
    451431/* The length of the binder key label. */
    452432#define BINDER_KEY_LABEL_SZ         10
     
    454434static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] =
    455435    "ext binder";
    456 #endif
     436
    457437/* Derive the binder key.
    458438 *
     
    464444{
    465445    WOLFSSL_MSG("Derive Binder Key");
     446    if (ssl == NULL || ssl->arrays == NULL) {
     447        return BAD_FUNC_ARG;
     448    }
    466449    return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
    467450                        binderKeyLabel, BINDER_KEY_LABEL_SZ,
     
    471454
    472455#ifdef HAVE_SESSION_TICKET
    473 #ifdef WOLFSSL_TLS13_DRAFT_18
    474 /* The length of the binder key resume label. */
    475 #define BINDER_KEY_RESUME_LABEL_SZ  25
    476 /* The binder key resume label. */
    477 static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] =
    478     "resumption psk binder key";
    479 #else
     456
    480457/* The length of the binder key resume label. */
    481458#define BINDER_KEY_RESUME_LABEL_SZ  10
     
    483460static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] =
    484461    "res binder";
    485 #endif
     462
    486463/* Derive the binder resumption key.
    487464 *
     
    493470{
    494471    WOLFSSL_MSG("Derive Binder Key - Resumption");
     472    if (ssl == NULL || ssl->arrays == NULL) {
     473        return BAD_FUNC_ARG;
     474    }
    495475    return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
    496476                        binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ,
     
    500480
    501481#ifdef WOLFSSL_EARLY_DATA
    502 #ifdef WOLFSSL_TLS13_DRAFT_18
    503 /* The length of the early traffic label. */
    504 #define EARLY_TRAFFIC_LABEL_SZ      27
    505 /* The early traffic label. */
    506 static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] =
    507     "client early traffic secret";
    508 #else
     482
    509483/* The length of the early traffic label. */
    510484#define EARLY_TRAFFIC_LABEL_SZ      11
     
    512486static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] =
    513487    "c e traffic";
    514 #endif
     488
    515489/* Derive the early traffic key.
    516490 *
     
    523497    int ret;
    524498    WOLFSSL_MSG("Derive Early Traffic Secret");
     499    if (ssl == NULL || ssl->arrays == NULL) {
     500        return BAD_FUNC_ARG;
     501    }
    525502    ret = DeriveKey(ssl, key, -1, ssl->arrays->secret,
    526503                    earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ,
     
    538515}
    539516
    540 #ifdef TLS13_SUPPORTS_EXPORTERS
    541 #ifdef WOLFSSL_TLS13_DRAFT_18
    542 /* The length of the early exporter label. */
    543 #define EARLY_EXPORTER_LABEL_SZ     28
    544 /* The early exporter label. */
    545 static const byte earlyExporterLabel[EARLY_EXPORTER_LABEL_SZ + 1] =
    546     "early exporter master secret";
    547 #else
     517#ifdef HAVE_KEYING_MATERIAL
    548518/* The length of the early exporter label. */
    549519#define EARLY_EXPORTER_LABEL_SZ     12
     
    551521static const byte earlyExporterLabel[EARLY_EXPORTER_LABEL_SZ + 1] =
    552522    "e exp master";
    553 #endif
     523
    554524/* Derive the early exporter key.
    555525 *
     
    562532    int ret;
    563533    WOLFSSL_MSG("Derive Early Exporter Secret");
     534    if (ssl == NULL || ssl->arrays == NULL) {
     535        return BAD_FUNC_ARG;
     536    }
    564537    ret = DeriveKey(ssl, key, -1, ssl->arrays->secret,
    565538                    earlyExporterLabel, EARLY_EXPORTER_LABEL_SZ,
     
    579552#endif
    580553
    581 #ifdef WOLFSSL_TLS13_DRAFT_18
    582 /* The length of the client handshake label. */
    583 #define CLIENT_HANDSHAKE_LABEL_SZ   31
    584 /* The client handshake label. */
    585 static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] =
    586     "client handshake traffic secret";
    587 #else
    588554/* The length of the client handshake label. */
    589555#define CLIENT_HANDSHAKE_LABEL_SZ   12
     
    591557static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] =
    592558    "c hs traffic";
    593 #endif
     559
    594560/* Derive the client handshake key.
    595561 *
     
    602568    int ret;
    603569    WOLFSSL_MSG("Derive Client Handshake Secret");
     570    if (ssl == NULL || ssl->arrays == NULL) {
     571        return BAD_FUNC_ARG;
     572    }
    604573    ret = DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret,
    605574                    clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ,
     
    617586}
    618587
    619 #ifdef WOLFSSL_TLS13_DRAFT_18
    620 /* The length of the server handshake label. */
    621 #define SERVER_HANDSHAKE_LABEL_SZ   31
    622 /* The server handshake label. */
    623 static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] =
    624     "server handshake traffic secret";
    625 #else
    626588/* The length of the server handshake label. */
    627589#define SERVER_HANDSHAKE_LABEL_SZ   12
     
    629591static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] =
    630592    "s hs traffic";
    631 #endif
     593
    632594/* Derive the server handshake key.
    633595 *
     
    640602    int ret;
    641603    WOLFSSL_MSG("Derive Server Handshake Secret");
     604    if (ssl == NULL || ssl->arrays == NULL) {
     605        return BAD_FUNC_ARG;
     606    }
    642607    ret = DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret,
    643608                    serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ,
     
    655620}
    656621
    657 #ifdef WOLFSSL_TLS13_DRAFT_18
    658 /* The length of the client application traffic label. */
    659 #define CLIENT_APP_LABEL_SZ         33
    660 /* The client application traffic label. */
    661 static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] =
    662     "client application traffic secret";
    663 #else
    664622/* The length of the client application traffic label. */
    665623#define CLIENT_APP_LABEL_SZ         12
     
    667625static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] =
    668626    "c ap traffic";
    669 #endif
     627
    670628/* Derive the client application traffic key.
    671629 *
     
    678636    int ret;
    679637    WOLFSSL_MSG("Derive Client Traffic Secret");
     638    if (ssl == NULL || ssl->arrays == NULL) {
     639        return BAD_FUNC_ARG;
     640    }
    680641    ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
    681642                    clientAppLabel, CLIENT_APP_LABEL_SZ,
     
    693654}
    694655
    695 #ifdef WOLFSSL_TLS13_DRAFT_18
    696 /* The length of the server application traffic label. */
    697 #define SERVER_APP_LABEL_SZ         33
    698 /* The  server application traffic label. */
    699 static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] =
    700     "server application traffic secret";
    701 #else
    702656/* The length of the server application traffic label. */
    703657#define SERVER_APP_LABEL_SZ         12
     
    705659static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] =
    706660    "s ap traffic";
    707 #endif
     661
    708662/* Derive the server application traffic key.
    709663 *
     
    716670    int ret;
    717671    WOLFSSL_MSG("Derive Server Traffic Secret");
     672    if (ssl == NULL || ssl->arrays == NULL) {
     673        return BAD_FUNC_ARG;
     674    }
    718675    ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
    719676                    serverAppLabel, SERVER_APP_LABEL_SZ,
     
    731688}
    732689
    733 #ifdef TLS13_SUPPORTS_EXPORTERS
    734 #ifdef WOLFSSL_TLS13_DRAFT_18
    735 /* The length of the exporter master secret label. */
    736 #define EXPORTER_MASTER_LABEL_SZ    22
    737 /* The exporter master secret label. */
    738 static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] =
    739     "exporter master secret";
    740 #else
     690#ifdef HAVE_KEYING_MATERIAL
    741691/* The length of the exporter master secret label. */
    742692#define EXPORTER_MASTER_LABEL_SZ    10
     
    744694static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] =
    745695    "exp master";
    746 #endif
     696
    747697/* Derive the exporter secret.
    748698 *
     
    754704{
    755705    int ret;
    756     WOLFSSL_MSG("Derive Exporter Secret");
     706    WOLFSSL_ENTER("Derive Exporter Secret");
     707    if (ssl == NULL || ssl->arrays == NULL) {
     708        return BAD_FUNC_ARG;
     709    }
    757710    ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
    758711                    exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ,
     
    769722    return ret;
    770723}
     724
     725/* The length of the exporter label. */
     726#define EXPORTER_LABEL_SZ    8
     727/* The exporter label. */
     728static const byte exporterLabel[EXPORTER_LABEL_SZ + 1] =
     729    "exporter";
     730/* Hash("") */
     731#ifndef NO_SHA256
     732static const byte emptySHA256Hash[] = {
     733    0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 0x9A, 0xFB, 0xF4, 0xC8,
     734    0x99, 0x6F, 0xB9, 0x24, 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
     735    0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
     736};
     737#endif
     738#ifdef WOLFSSL_SHA384
     739static const byte emptySHA384Hash[] = {
     740    0x38, 0xB0, 0x60, 0xA7, 0x51, 0xAC, 0x96, 0x38, 0x4C, 0xD9, 0x32, 0x7E,
     741    0xB1, 0xB1, 0xE3, 0x6A, 0x21, 0xFD, 0xB7, 0x11, 0x14, 0xBE, 0x07, 0x43,
     742    0x4C, 0x0C, 0xC7, 0xBF, 0x63, 0xF6, 0xE1, 0xDA, 0x27, 0x4E, 0xDE, 0xBF,
     743    0xE7, 0x6F, 0x65, 0xFB, 0xD5, 0x1A, 0xD2, 0xF1, 0x48, 0x98, 0xB9, 0x5B
     744};
     745#endif
     746#ifdef WOLFSSL_TLS13_SHA512
     747static const byte emptySHA512Hash[] = {
     748    0xCF, 0x83, 0xE1, 0x35, 0x7E, 0xEF, 0xB8, 0xBD, 0xF1, 0x54, 0x28, 0x50,
     749    0xD6, 0x6D, 0x80, 0x07, 0xD6, 0x20, 0xE4, 0x05, 0x0B, 0x57, 0x15, 0xDC,
     750    0x83, 0xF4, 0xA9, 0x21, 0xD3, 0x6C, 0xE9, 0xCE, 0x47, 0xD0, 0xD1, 0x3C,
     751    0x5D, 0x85, 0xF2, 0xB0, 0xFF, 0x83, 0x18, 0xD2, 0x87, 0x7E, 0xEC, 0x2F,
     752    0x63, 0xB9, 0x31, 0xBD, 0x47, 0x41, 0x7A, 0x81, 0xA5, 0x38, 0x32, 0x7A,
     753    0xF9, 0x27, 0xDA, 0x3E
     754};
     755#endif
     756/**
     757 * Implement section 7.5 of RFC 8446
     758 * @return  0 on success
     759 *         <0 on failure
     760 */
     761int Tls13_Exporter(WOLFSSL* ssl, unsigned char *out, size_t outLen,
     762        const char *label, size_t labelLen,
     763        const unsigned char *context, size_t contextLen)
     764{
     765    int                 ret;
     766    enum wc_HashType    hashType = WC_HASH_TYPE_NONE;
     767    int                 hashLen = 0;
     768    byte                hashOut[WC_MAX_DIGEST_SIZE];
     769    const byte*         emptyHash = NULL;
     770    byte                firstExpand[WC_MAX_DIGEST_SIZE];
     771    const byte*         protocol = tls13ProtocolLabel;
     772    word32              protocolLen = TLS13_PROTOCOL_LABEL_SZ;
     773
     774    if (ssl->version.minor != TLSv1_3_MINOR)
     775        return VERSION_ERROR;
     776
     777    switch (ssl->specs.mac_algorithm) {
     778        #ifndef NO_SHA256
     779        case sha256_mac:
     780            hashType  = WC_HASH_TYPE_SHA256;
     781            hashLen   = WC_SHA256_DIGEST_SIZE;
     782            emptyHash = emptySHA256Hash;
     783            break;
     784        #endif
     785
     786        #ifdef WOLFSSL_SHA384
     787        case sha384_mac:
     788            hashType  = WC_HASH_TYPE_SHA384;
     789            hashLen   = WC_SHA384_DIGEST_SIZE;
     790            emptyHash = emptySHA384Hash;
     791            break;
     792        #endif
     793
     794        #ifdef WOLFSSL_TLS13_SHA512
     795        case sha512_mac:
     796            hashType  = WC_HASH_TYPE_SHA512;
     797            hashLen   = WC_SHA512_DIGEST_SIZE;
     798            emptyHash = emptySHA512Hash;
     799            break;
     800        #endif
     801    }
     802
     803    /* Derive-Secret(Secret, label, "") */
     804    ret = HKDF_Expand_Label(firstExpand, hashLen,
     805            ssl->arrays->exporterSecret, hashLen,
     806            protocol, protocolLen, (byte*)label, (word32)labelLen,
     807            emptyHash, hashLen, hashType);
     808    if (ret != 0)
     809        return ret;
     810
     811    /* Hash(context_value) */
     812    ret = wc_Hash(hashType, context, (word32)contextLen, hashOut, WC_MAX_DIGEST_SIZE);
     813    if (ret != 0)
     814        return ret;
     815
     816    ret = HKDF_Expand_Label(out, (word32)outLen, firstExpand, hashLen,
     817            protocol, protocolLen, exporterLabel, EXPORTER_LABEL_SZ,
     818            hashOut, hashLen, hashType);
     819
     820    return ret;
     821}
    771822#endif
    772823
    773824#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
    774 #ifdef WOLFSSL_TLS13_DRAFT_18
    775 /* The length of the resumption master secret label. */
    776 #define RESUME_MASTER_LABEL_SZ      24
    777 /* The resumption master secret label. */
    778 static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] =
    779     "resumption master secret";
    780 #else
    781825/* The length of the resumption master secret label. */
    782826#define RESUME_MASTER_LABEL_SZ      10
     
    784828static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] =
    785829    "res master";
    786 #endif
     830
    787831/* Derive the resumption secret.
    788832 *
     
    791835 * returns 0 on success, otherwise failure.
    792836 */
    793 static int DeriveResumptionSecret(WOLFSSL* ssl, byte* key)
    794 {
     837int DeriveResumptionSecret(WOLFSSL* ssl, byte* key)
     838{
     839    byte* masterSecret;
     840
    795841    WOLFSSL_MSG("Derive Resumption Secret");
    796     return DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
    797                      resumeMasterLabel, RESUME_MASTER_LABEL_SZ,
    798                      ssl->specs.mac_algorithm, 1);
     842    if (ssl == NULL) {
     843        return BAD_FUNC_ARG;
     844    }
     845    if (ssl->arrays != NULL) {
     846        masterSecret = ssl->arrays->masterSecret;
     847    }
     848    else {
     849        masterSecret = ssl->session.masterSecret;
     850    }
     851    return DeriveKey(ssl, key, -1, masterSecret, resumeMasterLabel,
     852                     RESUME_MASTER_LABEL_SZ, ssl->specs.mac_algorithm, 1);
    799853}
    800854#endif
     
    818872}
    819873
    820 #ifdef WOLFSSL_TLS13_DRAFT_18
    821 /* The length of the application traffic label. */
    822 #define APP_TRAFFIC_LABEL_SZ        26
    823 /* The application traffic label. */
    824 static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] =
    825     "application traffic secret";
    826 #else
    827874/* The length of the application traffic label. */
    828875#define APP_TRAFFIC_LABEL_SZ        11
     
    830877static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] =
    831878    "traffic upd";
    832 #endif
     879
    833880/* Update the traffic secret.
    834881 *
     
    849896 * ssl  The SSL/TLS object.
    850897 */
    851 static int DeriveEarlySecret(WOLFSSL* ssl)
     898int DeriveEarlySecret(WOLFSSL* ssl)
    852899{
    853900    WOLFSSL_MSG("Derive Early Secret");
     901    if (ssl == NULL || ssl->arrays == NULL) {
     902        return BAD_FUNC_ARG;
     903    }
    854904#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
    855905    return Tls13_HKDF_Extract(ssl->arrays->secret, NULL, 0,
     
    862912}
    863913
    864 #ifndef WOLFSSL_TLS13_DRAFT_18
    865914/* The length of the derived label. */
    866915#define DERIVED_LABEL_SZ        7
     
    868917static const byte derivedLabel[DERIVED_LABEL_SZ + 1] =
    869918    "derived";
    870 #endif
     919
    871920/* Derive the handshake secret using HKDF Extract.
    872921 *
    873922 * ssl  The SSL/TLS object.
    874923 */
    875 static int DeriveHandshakeSecret(WOLFSSL* ssl)
    876 {
    877 #ifdef WOLFSSL_TLS13_DRAFT_18
    878     WOLFSSL_MSG("Derive Handshake Secret");
    879     return Tls13_HKDF_Extract(ssl->arrays->preMasterSecret,
    880             ssl->arrays->secret, ssl->specs.hash_size,
    881             ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
    882             ssl->specs.mac_algorithm);
    883 #else
     924int DeriveHandshakeSecret(WOLFSSL* ssl)
     925{
    884926    byte key[WC_MAX_DIGEST_SIZE];
    885927    int ret;
    886 
    887928    WOLFSSL_MSG("Derive Handshake Secret");
    888 
     929    if (ssl == NULL || ssl->arrays == NULL) {
     930        return BAD_FUNC_ARG;
     931    }
    889932    ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
    890933                        derivedLabel, DERIVED_LABEL_SZ,
     
    897940            ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
    898941            ssl->specs.mac_algorithm);
    899 #endif
    900942}
    901943
     
    904946 * ssl  The SSL/TLS object.
    905947 */
    906 static int DeriveMasterSecret(WOLFSSL* ssl)
    907 {
    908 #ifdef WOLFSSL_TLS13_DRAFT_18
    909     WOLFSSL_MSG("Derive Master Secret");
    910     return Tls13_HKDF_Extract(ssl->arrays->masterSecret,
    911             ssl->arrays->preMasterSecret, ssl->specs.hash_size,
    912             ssl->arrays->masterSecret, 0, ssl->specs.mac_algorithm);
    913 #else
     948int DeriveMasterSecret(WOLFSSL* ssl)
     949{
    914950    byte key[WC_MAX_DIGEST_SIZE];
    915951    int ret;
    916 
    917952    WOLFSSL_MSG("Derive Master Secret");
    918 
     953    if (ssl == NULL || ssl->arrays == NULL) {
     954        return BAD_FUNC_ARG;
     955    }
    919956    ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret,
    920957                        derivedLabel, DERIVED_LABEL_SZ,
     
    923960        return ret;
    924961
    925     return Tls13_HKDF_Extract(ssl->arrays->masterSecret,
     962    ret = Tls13_HKDF_Extract(ssl->arrays->masterSecret,
    926963            key, ssl->specs.hash_size,
    927964            ssl->arrays->masterSecret, 0, ssl->specs.mac_algorithm);
    928 #endif
    929 }
    930 
    931 #ifndef WOLFSSL_TLS13_DRAFT_18
     965
     966#ifdef HAVE_KEYING_MATERIAL
     967    if (ret != 0)
     968        return ret;
     969    /* Calculate exporter secret only when saving arrays */
     970    if (ssl->options.saveArrays)
     971        ret = DeriveExporterSecret(ssl, ssl->arrays->exporterSecret);
     972#endif
     973
     974    return ret;
     975}
     976
    932977#if defined(HAVE_SESSION_TICKET)
    933978/* Length of the resumption label. */
     
    943988 * returns 0 on success, otherwise failure.
    944989 */
    945 static int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen,
    946                                byte* secret)
     990int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen, byte* secret)
    947991{
    948992    int         digestAlg;
     
    9821026}
    9831027#endif /* HAVE_SESSION_TICKET */
    984 #endif /* WOLFSSL_TLS13_DRAFT_18 */
    9851028
    9861029
     
    9991042    int  hashSz = WC_SHA256_DIGEST_SIZE;
    10001043    int  ret = BAD_FUNC_ARG;
     1044
     1045    if (ssl == NULL || key == NULL || hash == NULL) {
     1046        return BAD_FUNC_ARG;
     1047    }
    10011048
    10021049    /* Get the hash of the previous handshake messages. */
     
    10711118 * returns 0 on success, otherwise failure.
    10721119 */
    1073 static int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
     1120int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
    10741121{
    10751122    int   ret = BAD_FUNC_ARG; /* Assume failure */
     
    12821329    word32 TimeNowInMilliseconds(void)
    12831330    {
    1284         const uint32_t systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
    1285         uint32_t *systemTickPtr = systemTickPointer();
     1331        const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
     1332        word32 *systemTickPtr = systemTickPointer();
    12861333
    12871334        return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000;
     
    14071454        return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000));
    14081455    }
     1456#elif defined(WOLFSSL_LINUXKM)
     1457    /* The time in milliseconds.
     1458     * Used for tickets to represent difference between when first seen and when
     1459     * sending.
     1460     *
     1461     * returns the time in milliseconds as a 32-bit value.
     1462     */
     1463    word32 TimeNowInMilliseconds(void)
     1464    {
     1465    #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
     1466        return (word32)(ktime_get_real_ns() / (s64)1000000);
     1467    #else
     1468        return (word32)(ktime_get_real_ns() / (ktime_t)1000000);
     1469    #endif
     1470    }
     1471#elif defined(FUSION_RTOS)
     1472    /* The time in milliseconds.
     1473     * Used for tickets to represent difference between when first seen and when
     1474     * sending.
     1475     *
     1476     * returns the time in milliseconds as a 32-bit value.
     1477     */
     1478    word32 TimeNowInMilliseconds(void)
     1479    {
     1480        struct timeval now;
     1481        if (FCL_GETTIMEOFDAY(&now, 0) < 0)
     1482            return (word32)GETTIME_ERROR; /* TODO: return 0 for failure */
     1483
     1484        /* Convert to milliseconds number. */
     1485        return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000);
     1486    }
     1487
    14091488#else
    14101489    /* The time in milliseconds.
     
    14191498
    14201499        if (gettimeofday(&now, 0) < 0)
    1421             return GETTIME_ERROR;
     1500            return (word32)GETTIME_ERROR; /* TODO: return 0 for failure */
     1501
    14221502        /* Convert to milliseconds number. */
    14231503        return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000);
     
    14261506#endif /* HAVE_SESSION_TICKET || !NO_PSK */
    14271507
    1428 
    1429 #if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_SESSION_TICKET) || \
    1430                                     !defined(NO_PSK))
    1431 /* Add input to all handshake hashes.
    1432  *
    1433  * ssl    The SSL/TLS object.
    1434  * input  The data to hash.
    1435  * sz     The size of the data to hash.
    1436  * returns 0 on success, otherwise failure.
    1437  */
    1438 static int HashInputRaw(WOLFSSL* ssl, const byte* input, int sz)
    1439 {
    1440     int ret = BAD_FUNC_ARG;
    1441 
    1442 #ifndef NO_SHA256
    1443     ret = wc_Sha256Update(&ssl->hsHashes->hashSha256, input, sz);
    1444     if (ret != 0)
    1445         return ret;
    1446 #endif
    1447 #ifdef WOLFSSL_SHA384
    1448     ret = wc_Sha384Update(&ssl->hsHashes->hashSha384, input, sz);
    1449     if (ret != 0)
    1450         return ret;
    1451 #endif
    1452 #ifdef WOLFSSL_TLS13_SHA512
    1453     ret = wc_Sha512Update(&ssl->hsHashes->hashSha512, input, sz);
    1454     if (ret != 0)
    1455         return ret;
    1456 #endif
    1457 
    1458     return ret;
    1459 }
    1460 #endif
    14611508
    14621509/* Extract the handshake header information.
     
    15021549    rl->type    = type;
    15031550    rl->pvMajor = ssl->version.major;
    1504 #ifdef WOLFSSL_TLS13_DRAFT_18
    1505     rl->pvMinor = TLSv1_MINOR;
    1506 #else
    15071551    /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */
    15081552    rl->pvMinor = TLSv1_2_MINOR;
    1509 #endif
    15101553    c16toa((word16)length, rl->length);
    15111554}
     
    15841627 * out          The buffer to write into.
    15851628 */
    1586 static WC_INLINE void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out)
     1629static WC_INLINE void WriteSEQTls13(WOLFSSL* ssl, int verifyOrder, byte* out)
    15871630{
    15881631    word32 seq[2] = {0, 0};
     
    16201663
    16211664    /* The nonce is the IV with the sequence XORed into the last bytes. */
    1622     WriteSEQ(ssl, order, nonce + AEAD_NONCE_SZ - SEQ_SZ);
     1665    WriteSEQTls13(ssl, order, nonce + AEAD_NONCE_SZ - SEQ_SZ);
    16231666    for (i = 0; i < AEAD_NONCE_SZ - SEQ_SZ; i++)
    16241667        nonce[i] = iv[i];
     
    16281671
    16291672#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
    1630 /* Encrypt with ChaCha20 and create authenication tag with Poly1305.
     1673/* Encrypt with ChaCha20 and create authentication tag with Poly1305.
    16311674 *
    16321675 * ssl     The SSL/TLS object.
     
    16831726
    16841727#ifdef HAVE_NULL_CIPHER
    1685 /* Create authenication tag and copy data over input.
     1728/* Create authentication tag and copy data over input.
    16861729 *
    16871730 * ssl     The SSL/TLS object.
     
    17651808            WOLFSSL_MSG("Data to encrypt");
    17661809            WOLFSSL_BUFFER(input, dataSz);
    1767 #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22) && \
    1768                                         !defined(WOLFSSL_TLS13_DRAFT_23)
    17691810            WOLFSSL_MSG("Additional Authentication Data");
    17701811            WOLFSSL_BUFFER(aad, aadSz);
    1771 #endif
    17721812        #endif
    17731813
     
    19121952
    19131953#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
    1914 /* Decrypt with ChaCha20 and check authenication tag with Poly1305.
     1954/* Decrypt with ChaCha20 and check authentication tag with Poly1305.
    19151955 *
    19161956 * ssl     The SSL/TLS object.
     
    20682108            WOLFSSL_MSG("Data to decrypt");
    20692109            WOLFSSL_BUFFER(input, dataSz);
    2070 #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22) && \
    2071                                         !defined(WOLFSSL_TLS13_DRAFT_23)
    20722110            WOLFSSL_MSG("Additional Authentication Data");
    20732111            WOLFSSL_BUFFER(aad, aadSz);
    2074 #endif
    20752112            WOLFSSL_MSG("Authentication tag");
    20762113            WOLFSSL_BUFFER(input + dataSz, macSz);
     
    23512388        #endif
    23522389            {
    2353 #if defined(WOLFSSL_TLS13_DRAFT_18) || defined(WOLFSSL_TLS13_DRAFT_22) || \
    2354                                        defined(WOLFSSL_TLS13_DRAFT_23)
    2355                 output += args->headerSz;
    2356                 ret = EncryptTls13(ssl, output, output, args->size, NULL, 0,
    2357                                                                      asyncOkay);
    2358 #else
    23592390                const byte* aad = output;
    23602391                output += args->headerSz;
    23612392                ret = EncryptTls13(ssl, output, output, args->size, aad,
    23622393                                   RECORD_HEADER_SZ, asyncOkay);
    2363 #endif
    23642394            }
    23652395            break;
     
    24152445#endif
    24162446
    2417 #ifndef WOLFSSL_TLS13_DRAFT_18
    24182447#if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
    24192448/* Create Cookie extension using the hash of the first ClientHello.
     
    24622491 * returns 0 on success, otherwise failure.
    24632492 */
    2464 static int RestartHandshakeHash(WOLFSSL* ssl)
     2493int RestartHandshakeHash(WOLFSSL* ssl)
    24652494{
    24662495    int    ret;
     
    25262555    if (ret != 0)
    25272556        return ret;
    2528     ret = HashOutputRaw(ssl, header, sizeof(header));
     2557    ret = HashRaw(ssl, header, sizeof(header));
    25292558    if (ret != 0)
    25302559        return ret;
    2531     return HashOutputRaw(ssl, hash, hashSz);
     2560    return HashRaw(ssl, hash, hashSz);
    25322561}
    25332562
     
    25412570    0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
    25422571};
    2543 #endif /* WOLFSSL_TLS13_DRAFT_18 */
     2572
    25442573
    25452574#ifndef NO_WOLFSSL_CLIENT
     
    25782607        /* Resumption PSK is master secret. */
    25792608        ssl->arrays->psk_keySz = ssl->specs.hash_size;
    2580 #ifdef WOLFSSL_TLS13_DRAFT_18
    2581         XMEMCPY(ssl->arrays->psk_key, ssl->session.masterSecret,
    2582                 ssl->arrays->psk_keySz);
    2583 #else
    25842609        if ((ret = DeriveResumptionPSK(ssl, ssl->session.ticketNonce.data,
    25852610                    ssl->session.ticketNonce.len, ssl->arrays->psk_key)) != 0) {
    25862611            return ret;
    25872612        }
    2588 #endif
    25892613    }
    25902614#endif
     
    25942618        const char* cipherName = NULL;
    25952619        byte cipherSuite0 = TLS13_BYTE, cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
     2620        int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
    25962621
    25972622        /* Get the pre-shared key. */
     
    26022627                    &cipherName);
    26032628            if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
    2604                                                            &cipherSuite) != 0) {
     2629                                       &cipherSuite, &cipherSuiteFlags) != 0) {
    26052630                return PSK_KEY_ERROR;
    26062631            }
     
    26202645            return PSK_KEY_ERROR;
    26212646        }
     2647        (void)cipherSuiteFlags;
    26222648    #else
    26232649        /* PSK information loaded during setting of default TLS extensions. */
     
    26262652#endif
    26272653
    2628     if (ssl->options.noPskDheKe)
     2654    if (ssl->options.noPskDheKe) {
    26292655        ssl->arrays->preMasterSz = 0;
     2656    }
    26302657
    26312658    /* Derive the early secret using the PSK. */
     
    27072734
    27082735    /* Hash binders to complete the hash of the ClientHello. */
    2709     ret = HashOutputRaw(ssl, output + idx, len);
     2736    ret = HashRaw(ssl, output + idx, len);
    27102737    if (ret < 0)
    27112738        return ret;
     
    27782805    length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->suites->suiteSz +
    27792806             SUITE_LEN + COMP_LEN + ENUM_LEN;
    2780 #ifndef WOLFSSL_TLS13_DRAFT_18
    27812807    #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
    27822808    length += ID_LEN;
     
    27852811        length += ssl->session.sessionIDSz;
    27862812    #endif
    2787 #endif
    27882813
    27892814    /* Auto populate extensions supported unless user defined. */
     
    28462871    idx += RAN_LEN;
    28472872
    2848 #ifdef WOLFSSL_TLS13_DRAFT_18
    2849     /* TLS v1.3 does not use session id - 0 length. */
    2850     output[idx++] = 0;
    2851 #else
    28522873    if (ssl->session.sessionIDSz > 0) {
    28532874        /* Session resumption for old versions of protocol. */
     
    28662887    #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
    28672888    }
    2868 #endif /* WOLFSSL_TLS13_DRAFT_18 */
    28692889
    28702890    /* Cipher suites */
     
    29212941}
    29222942
    2923 #ifdef WOLFSSL_TLS13_DRAFT_18
    2924 /* handle rocessing of TLS 1.3 hello_retry_request (6) */
    2925 /* Parse and handle a HelloRetryRequest message.
    2926  * Only a client will receive this message.
    2927  *
    2928  * ssl       The SSL/TLS object.
    2929  * input     The message buffer.
    2930  * inOutIdx  On entry, the index into the message buffer of
    2931  *           HelloRetryRequest.
    2932  *           On exit, the index of byte after the HelloRetryRequest message.
    2933  * totalSz   The length of the current handshake message.
    2934  * returns 0 on success and otherwise failure.
    2935  */
    2936 static int DoTls13HelloRetryRequest(WOLFSSL* ssl, const byte* input,
    2937                                     word32* inOutIdx, word32 totalSz)
    2938 {
    2939     int             ret;
    2940     word32          begin = *inOutIdx;
    2941     word32          i = begin;
    2942     word16          totalExtSz;
    2943     ProtocolVersion pv;
    2944 
    2945     WOLFSSL_ENTER("DoTls13HelloRetryRequest");
    2946 
    2947 #ifdef WOLFSSL_CALLBACKS
    2948     if (ssl->hsInfoOn) AddPacketName(ssl, "HelloRetryRequest");
    2949     if (ssl->toInfoOn) AddLateName("HelloRetryRequest", &ssl->timeoutInfo);
    2950 #endif
    2951 
    2952     /* Version info and length field of extension data. */
    2953     if (totalSz < i - begin + OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
    2954         return BUFFER_ERROR;
    2955 
    2956     /* Protocol version. */
    2957     XMEMCPY(&pv, input + i, OPAQUE16_LEN);
    2958     i += OPAQUE16_LEN;
    2959     ret = CheckVersion(ssl, pv);
    2960     if (ret != 0)
    2961         return ret;
    2962 
    2963     /* Length of extension data. */
    2964     ato16(&input[i], &totalExtSz);
    2965     i += OPAQUE16_LEN;
    2966     if (totalExtSz == 0) {
    2967         WOLFSSL_MSG("HelloRetryRequest must contain extensions");
    2968         return MISSING_HANDSHAKE_DATA;
    2969     }
    2970 
    2971     /* Extension data. */
    2972     if (i - begin + totalExtSz > totalSz)
    2973         return BUFFER_ERROR;
    2974     if ((ret = TLSX_Parse(ssl, (byte *)(input + i), totalExtSz,
    2975                           hello_retry_request, NULL)) != 0)
    2976         return ret;
    2977     /* The KeyShare extension parsing fails when not valid. */
    2978 
    2979     /* Move index to byte after message. */
    2980     *inOutIdx = i + totalExtSz;
    2981 
    2982     ssl->options.tls1_3 = 1;
    2983     ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
    2984 
    2985     WOLFSSL_LEAVE("DoTls13HelloRetryRequest", ret);
    2986 
    2987     return ret;
    2988 }
    2989 #endif
    2990 
    2991 
    29922943/* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */
    29932944/* Handle the ServerHello message from the server.
     
    30082959    word32          begin = i;
    30092960    int             ret;
    3010 #ifndef WOLFSSL_TLS13_DRAFT_18
    30112961    byte            sessIdSz;
    30122962    const byte*     sessId;
    30132963    byte            b;
    30142964    int             foundVersion;
    3015 #endif
    30162965    word16          totalExtSz;
    30172966#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
     
    30232972    WOLFSSL_ENTER("DoTls13ServerHello");
    30242973
     2974    if (ssl->arrays == NULL)
     2975        return BAD_FUNC_ARG;
     2976
    30252977#ifdef WOLFSSL_CALLBACKS
    30262978    if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello");
     
    30282980#endif
    30292981
     2982    if (ssl == NULL || ssl->arrays == NULL)
     2983        return BAD_FUNC_ARG;
     2984
    30302985    /* Protocol version length check. */
    30312986    if (OPAQUE16_LEN > helloSz)
     
    30352990    XMEMCPY(&pv, input + i, OPAQUE16_LEN);
    30362991    i += OPAQUE16_LEN;
    3037 #ifdef WOLFSSL_TLS13_DRAFT_18
    3038     ret = CheckVersion(ssl, pv);
    3039     if (ret != 0)
    3040         return ret;
    3041     if (!IsAtLeastTLSv1_3(pv) && pv.major != TLS_DRAFT_MAJOR) {
    3042 #ifndef WOLFSSL_NO_TLS12
    3043         if (ssl->options.downgrade) {
    3044             ssl->version = pv;
    3045             return DoServerHello(ssl, input, inOutIdx, helloSz);
    3046         }
    3047 #endif
    3048 
    3049         WOLFSSL_MSG("Client using higher version, fatal error");
    3050         return VERSION_ERROR;
    3051     }
    3052 #else
     2992
    30532993#ifndef WOLFSSL_NO_TLS12
    30542994    if (pv.major == ssl->version.major  && pv.minor < TLSv1_2_MINOR &&
     
    30623002    if (pv.major != ssl->version.major || pv.minor != TLSv1_2_MINOR)
    30633003        return VERSION_ERROR;
    3064 #endif
    3065 
    3066 #ifdef WOLFSSL_TLS13_DRAFT_18
    3067     /* Random length check */
    3068     if ((i - begin) + RAN_LEN > helloSz)
    3069         return BUFFER_ERROR;
    3070 #else
     3004
    30713005    /* Random and session id length check */
    30723006    if ((i - begin) + RAN_LEN + ENUM_LEN > helloSz)
    30733007        return BUFFER_ERROR;
    30743008
    3075     if (XMEMCMP(input + i, helloRetryRequestRandom, RAN_LEN) == 0)
     3009    if (XMEMCMP(input + i, helloRetryRequestRandom, RAN_LEN) == 0) {
    30763010        *extMsgType = hello_retry_request;
    3077 #endif
     3011        /* A HelloRetryRequest comes in as an ServerHello for MiddleBox compat.
     3012         * Found message to be a HelloRetryRequest.
     3013         * Don't allow more than one HelloRetryRequest or ServerHello.
     3014         */
     3015        if (ssl->msgsReceived.got_hello_retry_request == 1) {
     3016            return DUPLICATE_MSG_E;
     3017        }
     3018        /* Update counts to reflect change of message type. */
     3019        ssl->msgsReceived.got_hello_retry_request++;
     3020        ssl->msgsReceived.got_server_hello--;
     3021    }
    30783022
    30793023    /* Server random - keep for debugging. */
     
    30813025    i += RAN_LEN;
    30823026
    3083 #ifndef WOLFSSL_TLS13_DRAFT_18
    30843027    /* Session id */
    30853028    sessIdSz = input[i++];
     
    30883031    sessId = input + i;
    30893032    i += sessIdSz;
    3090 #endif /* WOLFSSL_TLS13_DRAFT_18 */
     3033
    30913034    ssl->options.haveSessionId = 1;
    30923035
    3093 #ifdef WOLFSSL_TLS13_DRAFT_18
    3094     /* Ciphersuite check */
    3095     if ((i - begin) + OPAQUE16_LEN + OPAQUE16_LEN > helloSz)
    3096         return BUFFER_ERROR;
    3097 #else
    30983036    /* Ciphersuite and compression check */
    30993037    if ((i - begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz)
    31003038        return BUFFER_ERROR;
    3101 #endif
    31023039
    31033040    /* Set the cipher suite from the message. */
     
    31053042    ssl->options.cipherSuite  = input[i++];
    31063043
    3107 #ifndef WOLFSSL_TLS13_DRAFT_18
    31083044    /* Compression */
    31093045    b = input[i++];
     
    31123048        return INVALID_PARAMETER;
    31133049    }
    3114 #endif
    3115 
    3116 #ifndef WOLFSSL_TLS13_DRAFT_18
     3050
    31173051    if ((i - begin) + OPAQUE16_LEN > helloSz) {
    31183052        if (!ssl->options.downgrade)
     
    31233057        ssl->options.haveEMS = 0;
    31243058    }
    3125     if ((i - begin) < helloSz)
    3126 #endif
    3127     {
     3059    if ((i - begin) < helloSz) {
    31283060        /* Get extension length and length check. */
    31293061        if ((i - begin) + OPAQUE16_LEN > helloSz)
     
    31343066            return BUFFER_ERROR;
    31353067
    3136 #ifndef WOLFSSL_TLS13_DRAFT_18
    31373068        /* Need to negotiate version first. */
    31383069        if ((ret = TLSX_ParseVersion(ssl, (byte*)input + i, totalExtSz,
     
    31513082            ssl->version.minor = pv.minor;
    31523083        }
    3153 #endif
    31543084
    31553085        /* Parse and handle extensions. */
     
    31763106#endif /* HAVE_SECRET_CALLBACK */
    31773107
    3178 #ifndef WOLFSSL_TLS13_DRAFT_18
    31793108    /* Version only negotiated in extensions for TLS v1.3.
    31803109     * Only now do we know how to deal with session id.
     
    32083137    }
    32093138
    3210     #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
     3139#ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
    32113140    if (sessIdSz == 0)
    32123141        return INVALID_PARAMETER;
     
    32193148    else if (XMEMCMP(ssl->arrays->clientRandom, sessId, sessIdSz) != 0)
    32203149        return INVALID_PARAMETER;
    3221     #else
     3150#else
    32223151    if (sessIdSz != ssl->session.sessionIDSz || (sessIdSz > 0 &&
    32233152                  XMEMCMP(ssl->session.sessionID, sessId, sessIdSz) != 0)) {
     
    32253154        return INVALID_PARAMETER;
    32263155    }
    3227     #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
    3228 #endif
     3156#endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
    32293157
    32303158    ret = SetCipherSpecs(ssl);
     
    32463174
    32473175#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
    3248 #ifndef WOLFSSL_TLS13_DRAFT_18
    3249     if (*extMsgType == server_hello)
    3250 #endif
    3251     {
     3176    if (*extMsgType == server_hello) {
    32523177        ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
    32533178        if (ext != NULL)
     
    32653190#endif
    32663191
    3267 #ifdef WOLFSSL_TLS13_DRAFT_18
    3268     ssl->keys.encryptionOn = 1;
    3269 #else
    32703192    if (*extMsgType == server_hello) {
    32713193        ssl->keys.encryptionOn = 1;
     
    32783200        ret = RestartHandshakeHash(ssl);
    32793201    }
    3280 #endif
    32813202
    32823203    WOLFSSL_LEAVE("DoTls13ServerHello", ret);
     
    33583279}
    33593280
     3281#ifndef NO_CERTS
    33603282/* handle processing TLS v1.3 certificate_request (13) */
    33613283/* Handle a TLS v1.3 CertificateRequest message.
     
    33763298    word32      begin = *inOutIdx;
    33773299    int         ret = 0;
    3378 #ifndef WOLFSSL_TLS13_DRAFT_18
    33793300    Suites      peerSuites;
    3380 #endif
    33813301#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
    33823302    CertReqCtx* certReqCtx;
     
    33863306    WOLFSSL_ENTER("DoTls13CertificateRequest");
    33873307
    3388 #ifndef WOLFSSL_TLS13_DRAFT_18
    33893308    XMEMSET(&peerSuites, 0, sizeof(Suites));
    3390 #endif
     3309
    33913310#ifdef WOLFSSL_CALLBACKS
    33923311    if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest");
     
    34203339    *inOutIdx += len;
    34213340
    3422 #ifdef WOLFSSL_TLS13_DRAFT_18
    3423     /* Signature and hash algorithms. */
    3424     if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
    3425         return BUFFER_ERROR;
    3426     ato16(input + *inOutIdx, &len);
    3427     *inOutIdx += OPAQUE16_LEN;
    3428     if ((*inOutIdx - begin) + len > size)
    3429         return BUFFER_ERROR;
    3430     if (PickHashSigAlgo(ssl, input + *inOutIdx, len) != 0 &&
    3431                  ssl->buffers.certificate && ssl->buffers.certificate->buffer &&
    3432                  ssl->buffers.key && ssl->buffers.key->buffer) {
    3433         return INVALID_PARAMETER;
    3434     }
    3435     *inOutIdx += len;
    3436 
    3437     /* Length of certificate authority data. */
    3438     if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
    3439         return BUFFER_ERROR;
    3440     ato16(input + *inOutIdx, &len);
    3441     *inOutIdx += OPAQUE16_LEN;
    3442     if ((*inOutIdx - begin) + len > size)
    3443         return BUFFER_ERROR;
    3444 
    3445     /* Certificate authorities. */
    3446     while (len) {
    3447         word16 dnSz;
    3448 
    3449         if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
    3450             return BUFFER_ERROR;
    3451 
    3452         ato16(input + *inOutIdx, &dnSz);
    3453         *inOutIdx += OPAQUE16_LEN;
    3454 
    3455         if ((*inOutIdx - begin) + dnSz > size)
    3456             return BUFFER_ERROR;
    3457 
    3458         *inOutIdx += dnSz;
    3459         len -= OPAQUE16_LEN + dnSz;
    3460     }
    3461 
    3462     /* Certificate extensions */
    3463     if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
    3464         return BUFFER_ERROR;
    3465     ato16(input + *inOutIdx, &len);
    3466     *inOutIdx += OPAQUE16_LEN;
    3467     if ((*inOutIdx - begin) + len > size)
    3468         return BUFFER_ERROR;
    3469     *inOutIdx += len;
    3470 #else
    34713341    /* TODO: Add support for more extensions:
    34723342     *   signed_certificate_timestamp, certificate_authorities, oid_filters.
     
    34863356    }
    34873357    *inOutIdx += len;
    3488 #endif
    34893358
    34903359    if (ssl->buffers.certificate && ssl->buffers.certificate->buffer &&
     
    34943363        #endif
    34953364    )) {
    3496 #ifndef WOLFSSL_TLS13_DRAFT_18
    34973365        if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo,
    34983366                                               peerSuites.hashSigAlgoSz) != 0) {
    34993367            return INVALID_PARAMETER;
    35003368        }
    3501 #endif
    35023369        ssl->options.sendVerify = SEND_CERT;
    35033370    }
    35043371    else {
     3372#ifndef WOLFSSL_NO_CLIENT_CERT_ERROR
    35053373        ssl->options.sendVerify = SEND_BLANK_CERT;
     3374#else
     3375        WOLFSSL_MSG("Certificate required but none set on client");
     3376        SendAlert(ssl, alert_fatal, illegal_parameter);
     3377        return NO_CERT_ERROR;
     3378#endif
    35063379    }
    35073380
     
    35143387    return ret;
    35153388}
    3516 
     3389#endif /* !NO_CERTS */
    35173390#endif /* !NO_WOLFSSL_CLIENT */
    35183391
     
    35763449    byte          cipherSuite0 = TLS13_BYTE;
    35773450    byte          cipherSuite  = WOLFSSL_DEF_PSK_CIPHER;
     3451    int           cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
    35783452#endif
    35793453
     
    36253499    #ifdef HAVE_SESSION_TICKET
    36263500        /* Decode the identity. */
    3627         if ((ret = DoClientTicket(ssl, current->identity, current->identityLen))
     3501        if (DoClientTicket(ssl, current->identity, current->identityLen)
    36283502                                                     == WOLFSSL_TICKET_RET_OK) {
    36293503            word32 now;
     
    36423516                /* Invalid difference, fallback to full handshake. */
    36433517                ssl->options.resuming = 0;
    3644                 break;
     3518                /* Hash the rest of the ClientHello. */
     3519                return HashRaw(ssl, input + helloSz - bindersLen, bindersLen);
    36453520            }
    36463521
     
    36673542            /* Resumption PSK is resumption master secret. */
    36683543            ssl->arrays->psk_keySz = ssl->specs.hash_size;
    3669         #ifdef WOLFSSL_TLS13_DRAFT_18
    3670             XMEMCPY(ssl->arrays->psk_key, ssl->session.masterSecret,
    3671                     ssl->arrays->psk_keySz);
    3672         #else
    36733544            if ((ret = DeriveResumptionPSK(ssl, ssl->session.ticketNonce.data,
    36743545                    ssl->session.ticketNonce.len, ssl->arrays->psk_key)) != 0) {
    36753546                return ret;
    36763547            }
    3677         #endif
    36783548
    36793549            /* Derive the early secret using the PSK. */
     
    36943564                             MAX_PSK_KEY_LEN, &cipherName)) != 0 &&
    36953565             GetCipherSuiteFromName(cipherName, &cipherSuite0,
    3696                                                           &cipherSuite) == 0) ||
     3566                                    &cipherSuite, &cipherSuiteFlags) == 0) ||
    36973567            (ssl->options.server_psk_cb != NULL &&
    36983568             (ssl->arrays->psk_keySz = ssl->options.server_psk_cb(ssl,
     
    37053575            suite[0] = cipherSuite0;
    37063576            suite[1] = cipherSuite;
     3577            (void)cipherSuiteFlags;
    37073578            if (!FindSuiteSSL(ssl, suite)) {
    37083579                current = current->next;
     
    37123583            /* Default to ciphersuite if cb doesn't specify. */
    37133584            ssl->options.resuming = 0;
     3585            /* Don't send certificate request when using PSK. */
     3586            ssl->options.verifyPeer = 0;
    37143587
    37153588            /* PSK age is always zero. */
     
    37653638
    37663639    /* Hash the rest of the ClientHello. */
    3767     ret = HashInputRaw(ssl, input + helloSz - bindersLen, bindersLen);
     3640    ret = HashRaw(ssl, input + helloSz - bindersLen, bindersLen);
    37683641    if (ret != 0)
    37693642        return ret;
     
    38073680    modes = ext->val;
    38083681
     3682#ifdef HAVE_SUPPORTED_CURVES
    38093683    ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
    38103684    /* Use (EC)DHE for forward-security if possible. */
     
    38263700        ext->resp = 1;
    38273701    }
    3828     else {
     3702    else
     3703#endif
     3704    {
    38293705        if ((modes & (1 << PSK_KE)) == 0)
    38303706            return PSK_KEY_ERROR;
     
    38413717#endif
    38423718
    3843 #if !defined(WOLFSSL_TLS13_DRAFT_18) && defined(WOLFSSL_SEND_HRR_COOKIE)
     3719#if defined(WOLFSSL_SEND_HRR_COOKIE)
    38443720/* Check that the Cookie data's integrity.
    38453721 *
     
    38923768/* Length of the Cookie Extension excluding cookie data */
    38933769#define HRR_COOKIE_HDR_SZ  (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
    3894 #ifdef WOLFSSL_TLS13_DRAFT_18
    3895 /* PV | CipherSuite | Ext Len */
    3896 #define HRR_BODY_SZ        (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
    3897 /* HH | PV | CipherSuite | Ext Len | Key Share | Cookie */
    3898 #define MAX_HRR_SZ   (HANDSHAKE_HEADER_SZ   + \
    3899                         HRR_BODY_SZ         + \
    3900                           HRR_KEY_SHARE_SZ  + \
    3901                           HRR_COOKIE_HDR_SZ)
    3902 #else
    39033770/* PV | Random | Session Id | CipherSuite | Compression | Ext Len */
    39043771#define HRR_BODY_SZ        (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \
     
    39103777                          HRR_VERSIONS_SZ   + \
    39113778                          HRR_COOKIE_HDR_SZ)
    3912 #endif
     3779
    39133780
    39143781/* Restart the handshake hash from the cookie value.
     
    39423809    if ((ret = InitHandshakeHashes(ssl)) != 0)
    39433810        return ret;
    3944     if ((ret = HashOutputRaw(ssl, header, sizeof(header))) != 0)
    3945         return ret;
    3946     if ((ret = HashOutputRaw(ssl, cookieData + idx, hashSz)) != 0)
     3811    if ((ret = HashRaw(ssl, header, sizeof(header))) != 0)
     3812        return ret;
     3813    if ((ret = HashRaw(ssl, cookieData + idx, hashSz)) != 0)
    39473814        return ret;
    39483815
    39493816    /* Reconstruct the HelloRetryMessage for handshake hash. */
    3950 #ifdef WOLFSSL_TLS13_DRAFT_18
    3951     length = HRR_BODY_SZ + HRR_COOKIE_HDR_SZ + cookie->len;
    3952 #else
    39533817    length = HRR_BODY_SZ - ID_LEN + ssl->session.sessionIDSz +
    39543818             HRR_COOKIE_HDR_SZ + cookie->len;
    39553819    length += HRR_VERSIONS_SZ;
    3956 #endif
    39573820    if (cookieDataSz > hashSz + OPAQUE16_LEN) {
    39583821        keyShareExt = 1;
    39593822        length += HRR_KEY_SHARE_SZ;
    39603823    }
    3961 #ifdef WOLFSSL_TLS13_DRAFT_18
    3962     AddTls13HandShakeHeader(hrr, length, 0, 0, hello_retry_request, ssl);
    3963 
    3964     idx += hashSz;
    3965     hrrIdx = HANDSHAKE_HEADER_SZ;
    3966     /* The negotiated protocol version. */
    3967     hrr[hrrIdx++] = TLS_DRAFT_MAJOR;
    3968     hrr[hrrIdx++] = TLS_DRAFT_MINOR;
    3969     /* Cipher Suite */
    3970     hrr[hrrIdx++] = cookieData[idx++];
    3971     hrr[hrrIdx++] = cookieData[idx++];
    3972 
    3973     /* Extensions' length */
    3974     length -= HRR_BODY_SZ;
    3975     c16toa(length, hrr + hrrIdx);
    3976     hrrIdx += 2;
    3977 #else
     3824
    39783825    AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl);
    39793826
     
    40073854    hrrIdx += 2;
    40083855
    4009 #endif
    40103856    /* Optional KeyShare Extension */
    40113857    if (keyShareExt) {
     
    40173863        hrr[hrrIdx++] = cookieData[idx++];
    40183864    }
    4019 #ifndef WOLFSSL_TLS13_DRAFT_18
    40203865    c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx);
    40213866    hrrIdx += 2;
     
    40293874        hrr[hrrIdx++] = ssl->version.minor;
    40303875    #endif
    4031 #endif
     3876
    40323877    /* Mandatory Cookie Extension */
    40333878    c16toa(TLSX_COOKIE, hrr + hrrIdx);
     
    40453890#endif
    40463891
    4047     if ((ret = HashOutputRaw(ssl, hrr, hrrIdx)) != 0)
    4048         return ret;
    4049     return HashOutputRaw(ssl, cookieData, cookie->len);
     3892    if ((ret = HashRaw(ssl, hrr, hrrIdx)) != 0)
     3893        return ret;
     3894    return HashRaw(ssl, cookieData, cookie->len);
    40503895}
    40513896#endif
     
    42054050    }
    42064051
     4052    /* From here on we are a TLS 1.3 ClientHello. */
     4053
    42074054    /* Client random */
    42084055    XMEMCPY(ssl->arrays->clientRandom, input + i, RAN_LEN);
     
    42144061#endif
    42154062
    4216 #ifdef WOLFSSL_TLS13_DRAFT_18
    4217     /* Session id - empty in TLS v1.3 */
    4218     sessIdSz = input[i++];
    4219     if (sessIdSz > 0 && !ssl->options.downgrade) {
    4220         WOLFSSL_MSG("Client sent session id - not supported");
    4221         return BUFFER_ERROR;
    4222     }
    4223 #else
    42244063    sessIdSz = input[i++];
    42254064    if (sessIdSz != ID_LEN && sessIdSz != 0)
    42264065        return INVALID_PARAMETER;
    4227 #endif
    42284066
    42294067    if (sessIdSz + i > helloSz) {
     
    42514089    clSuites.hashSigAlgoSz = 0;
    42524090
    4253 #ifdef HAVE_SERVER_RENEGOTIATION_INFO
    4254     ret = FindSuite(&clSuites, 0, TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
    4255     if (ret == SUITES_ERROR)
    4256         return BUFFER_ERROR;
    4257     if (ret >= 0) {
    4258         TLSX* extension;
    4259 
    4260         /* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */
    4261         ret = TLSX_AddEmptyRenegotiationInfo(&ssl->extensions, ssl->heap);
    4262         if (ret != WOLFSSL_SUCCESS)
    4263             return ret;
    4264 
    4265         extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
    4266         if (extension) {
    4267             ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
    4268             ssl->secure_renegotiation->enabled = 1;
    4269         }
    4270     }
    4271 #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
    4272 
    42734091    /* Compression */
    42744092    b = input[i++];
     
    43064124    }
    43074125
    4308 #if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
    4309                                                         defined(WOLFSSL_HAPROXY)
     4126#ifdef HAVE_SNI
    43104127        if ((ret = SNI_Callback(ssl)) != 0)
    43114128            return ret;
    43124129        ssl->options.side = WOLFSSL_SERVER_END;
    4313 #endif /* OPENSSL_ALL || HAVE_STUNNEL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
     4130#endif
    43144131
    43154132    i += totalExtSz;
     
    43214138    ssl->options.haveSessionId = 1;
    43224139
    4323 #if !defined(WOLFSSL_TLS13_DRAFT_18) && defined(WOLFSSL_SEND_HRR_COOKIE)
     4140#if defined(WOLFSSL_SEND_HRR_COOKIE)
    43244141    if (ssl->options.sendCookie &&
    43254142              ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
     
    43404157
    43414158#if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \
    4342      defined(HAVE_TLS_EXTENSIONS)
     4159                                                    defined(HAVE_TLS_EXTENSIONS)
    43434160    if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY) != NULL) {
    43444161        /* Refine list for PSK processing. */
     
    43624179
    43634180    if (!usingPSK) {
     4181#ifndef NO_CERTS
    43644182        if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) {
    43654183            WOLFSSL_MSG("Client did not send a KeyShare extension");
     
    43794197        }
    43804198
    4381 #ifdef HAVE_NULL_CIPHER
     4199    #ifdef HAVE_NULL_CIPHER
    43824200        if (ssl->options.cipherSuite0 == ECC_BYTE &&
    43834201                              (ssl->options.cipherSuite == TLS_SHA256_SHA256 ||
     
    43864204        }
    43874205        else
    4388 #endif
     4206    #endif
    43894207        /* Check that the negotiated ciphersuite matches protocol version. */
    43904208        if (ssl->options.cipherSuite0 != TLS13_BYTE) {
     
    43954213        }
    43964214
    4397 #ifdef HAVE_SESSION_TICKET
     4215    #ifdef HAVE_SESSION_TICKET
    43984216        if (ssl->options.resuming) {
    43994217            ssl->options.resuming = 0;
    44004218            XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size);
    44014219        }
    4402 #endif
     4220    #endif
    44034221
    44044222        /* Derive early secret for handshake secret. */
    44054223        if ((ret = DeriveEarlySecret(ssl)) != 0)
    44064224            return ret;
     4225#else
     4226        ret = INVALID_PARAMETER;
     4227#endif
    44074228    }
    44084229
     
    44134234}
    44144235
    4415 #ifdef WOLFSSL_TLS13_DRAFT_18
    4416 /* handle generation of TLS 1.3 hello_retry_request (6) */
    4417 /* Send the HelloRetryRequest message to indicate the negotiated protocol
    4418  * version and security parameters the server is willing to use.
     4236/* Send TLS v1.3 ServerHello message to client.
    44194237 * Only a server will send this message.
    44204238 *
     
    44224240 * returns 0 on success, otherwise failure.
    44234241 */
    4424 int SendTls13HelloRetryRequest(WOLFSSL* ssl)
    4425 {
    4426     int    ret;
    4427     byte*  output;
    4428     word32 length;
    4429     word16 len;
    4430     word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
    4431     int    sendSz;
    4432 
    4433     WOLFSSL_ENTER("SendTls13HelloRetryRequest");
    4434 
    4435     /* Get the length of the extensions that will be written. */
    4436     len = 0;
    4437     ret = TLSX_GetResponseSize(ssl, hello_retry_request, &len);
    4438     /* There must be extensions sent to indicate what client needs to do. */
    4439     if (ret != 0)
    4440         return MISSING_HANDSHAKE_DATA;
    4441 
    4442     /* Protocol version + Extensions */
    4443     length = OPAQUE16_LEN + len;
    4444     sendSz = idx + length;
    4445 
    4446     /* Check buffers are big enough and grow if needed. */
    4447     if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
    4448         return ret;
    4449 
    4450     /* Get position in output buffer to write new message to. */
    4451     output = ssl->buffers.outputBuffer.buffer +
    4452              ssl->buffers.outputBuffer.length;
    4453     /* Add record and handshake headers. */
    4454     AddTls13Headers(output, length, hello_retry_request, ssl);
    4455 
    4456     /* The negotiated protocol version. */
    4457     output[idx++] = TLS_DRAFT_MAJOR;
    4458     output[idx++] = TLS_DRAFT_MINOR;
    4459 
    4460     /* Add TLS extensions. */
    4461     ret = TLSX_WriteResponse(ssl, output + idx, hello_retry_request, NULL);
    4462     if (ret != 0)
    4463         return ret;
    4464     idx += len;
    4465 
    4466 #ifdef WOLFSSL_CALLBACKS
    4467     if (ssl->hsInfoOn)
    4468         AddPacketName(ssl, "HelloRetryRequest");
    4469     if (ssl->toInfoOn) {
    4470         AddPacketInfo(ssl, "HelloRetryRequest", handshake, output, sendSz,
    4471                       WRITE_PROTO, ssl->heap);
    4472     }
    4473 #endif
    4474     if ((ret = HashOutput(ssl, output, idx, 0)) != 0)
    4475         return ret;
    4476 
    4477     ssl->buffers.outputBuffer.length += sendSz;
    4478 
    4479     if (!ssl->options.groupMessages)
    4480         ret = SendBuffered(ssl);
    4481 
    4482     WOLFSSL_LEAVE("SendTls13HelloRetryRequest", ret);
    4483 
    4484     return ret;
    4485 }
    4486 #endif /* WOLFSSL_TLS13_DRAFT_18 */
    4487 
    4488 /* Send TLS v1.3 ServerHello message to client.
    4489  * Only a server will send this message.
    4490  *
    4491  * ssl  The SSL/TLS object.
    4492  * returns 0 on success, otherwise failure.
    4493  */
    4494 #ifdef WOLFSSL_TLS13_DRAFT_18
    4495 static
    4496 #endif
    44974242/* handle generation of TLS 1.3 server_hello (2) */
    44984243int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
     
    45074252    WOLFSSL_ENTER("SendTls13ServerHello");
    45084253
    4509 #ifndef WOLFSSL_TLS13_DRAFT_18
    45104254    if (extMsgType == hello_retry_request) {
    45114255        WOLFSSL_MSG("wolfSSL Doing HelloRetryRequest");
     
    45134257            return ret;
    45144258    }
    4515 #endif
    4516 
    4517 #ifdef WOLFSSL_TLS13_DRAFT_18
    4518     /* Protocol version, server random, cipher suite and extensions. */
    4519     length = VERSION_SZ + RAN_LEN + SUITE_LEN;
    4520     ret = TLSX_GetResponseSize(ssl, server_hello, &length);
    4521     if (ret != 0)
    4522         return ret;
    4523 #else
     4259
    45244260    /* Protocol version, server random, session id, cipher suite, compression
    45254261     * and extensions.
     
    45304266    if (ret != 0)
    45314267        return ret;
    4532 #endif
    45334268    sendSz = idx + length;
    45344269
     
    45444279    AddTls13Headers(output, length, server_hello, ssl);
    45454280
    4546 #ifdef WOLFSSL_TLS13_DRAFT_18
    4547     /* The negotiated protocol version. */
    4548     output[idx++] = TLS_DRAFT_MAJOR;
    4549     output[idx++] = TLS_DRAFT_MINOR;
    4550 #else
    45514281    /* The protocol version must be TLS v1.2 for middleboxes. */
    45524282    output[idx++] = ssl->version.major;
    45534283    output[idx++] = TLSv1_2_MINOR;
    4554 #endif
    45554284
    45564285    if (extMsgType == server_hello) {
     
    45594288            return ret;
    45604289    }
    4561 #ifndef WOLFSSL_TLS13_DRAFT_18
    45624290    else {
    45634291        /* HelloRetryRequest message has fixed value for random. */
    45644292        XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN);
    45654293    }
    4566 #endif
     4294
    45674295    /* Store in SSL for debugging. */
    45684296    XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN);
     
    45744302#endif
    45754303
    4576 #ifndef WOLFSSL_TLS13_DRAFT_18
    45774304    output[idx++] = ssl->session.sessionIDSz;
    45784305    if (ssl->session.sessionIDSz > 0) {
     
    45804307        idx += ssl->session.sessionIDSz;
    45814308    }
    4582 #endif
    45834309
    45844310    /* Chosen cipher suite */
     
    45864312    output[idx++] = ssl->options.cipherSuite;
    45874313
    4588 #ifndef WOLFSSL_TLS13_DRAFT_18
    45894314    /* Compression not supported in TLS v1.3. */
    45904315    output[idx++] = 0;
    4591 #endif
    45924316
    45934317    /* Extensions */
     
    46104334    #endif
    46114335
    4612 #ifdef WOLFSSL_TLS13_DRAFT_18
    4613     ssl->options.serverState = SERVER_HELLO_COMPLETE;
    4614 #else
    46154336    if (extMsgType == server_hello)
    46164337        ssl->options.serverState = SERVER_HELLO_COMPLETE;
    4617 #endif
    4618 
    4619 #ifdef WOLFSSL_TLS13_DRAFT_18
    4620     if (!ssl->options.groupMessages)
    4621 #else
     4338
    46224339    if (!ssl->options.groupMessages || extMsgType != server_hello)
    4623 #endif
     4340
    46244341        ret = SendBuffered(ssl);
    46254342
     
    46514368    ssl->keys.encryptionOn = 1;
    46524369
    4653 #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
     4370#if defined(HAVE_SUPPORTED_CURVES) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
    46544371    if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0)
    46554372        return ret;
     
    47504467    word32 i;
    47514468    word16 reqSz;
    4752 #ifndef WOLFSSL_TLS13_DRAFT_18
    47534469    TLSX*  ext;
    4754 #endif
    47554470
    47564471    WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND);
     
    47604475        InitSuitesHashSigAlgo(ssl->suites, 1, 1, 0, 1, ssl->buffers.keySz);
    47614476
    4762 #ifdef WOLFSSL_TLS13_DRAFT_18
    4763     i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
    4764     reqSz = OPAQUE8_LEN + reqCtxLen + REQ_HEADER_SZ + REQ_HEADER_SZ;
    4765     reqSz += LENGTH_SZ + ssl->suites->hashSigAlgoSz;
    4766 
    4767     sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + reqSz;
    4768     /* Always encrypted and make room for padding. */
    4769     sendSz += MAX_MSG_EXTRA;
    4770 
    4771     /* Check buffers are big enough and grow if needed. */
    4772     if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
    4773         return ret;
    4774 
    4775     /* Get position in output buffer to write new message to. */
    4776     output = ssl->buffers.outputBuffer.buffer +
    4777              ssl->buffers.outputBuffer.length;
    4778 
    4779     /* Put the record and handshake headers on. */
    4780     AddTls13Headers(output, reqSz, certificate_request, ssl);
    4781 
    4782     /* Certificate request context. */
    4783     output[i++] = reqCtxLen;
    4784     if (reqCtxLen != 0) {
    4785         XMEMCPY(output + i, reqCtx, reqCtxLen);
    4786         i += reqCtxLen;
    4787     }
    4788 
    4789     /* supported hash/sig */
    4790     c16toa(ssl->suites->hashSigAlgoSz, &output[i]);
    4791     i += LENGTH_SZ;
    4792 
    4793     XMEMCPY(&output[i], ssl->suites->hashSigAlgo, ssl->suites->hashSigAlgoSz);
    4794     i += ssl->suites->hashSigAlgoSz;
    4795 
    4796     /* Certificate authorities not supported yet - empty buffer. */
    4797     c16toa(0, &output[i]);
    4798     i += REQ_HEADER_SZ;
    4799 
    4800     /* Certificate extensions. */
    4801     c16toa(0, &output[i]);  /* auth's */
    4802     i += REQ_HEADER_SZ;
    4803 #else
    48044477    ext = TLSX_Find(ssl->extensions, TLSX_SIGNATURE_ALGORITHMS);
    48054478    if (ext == NULL)
     
    48414514        return ret;
    48424515    i += reqSz;
    4843 #endif
    48444516
    48454517    /* Always encrypted. */
     
    51814853#endif /* HAVE_ECC */
    51824854
    5183 #ifndef NO_RSA
     4855#if !defined(NO_RSA) && defined(WC_RSA_PSS)
    51844856/* Check that the decrypted signature matches the encoded signature
    51854857 * based on the digest of the signature data.
     
    52244896    return ret;
    52254897}
    5226 #endif /* !NO_RSA */
     4898#endif /* !NO_RSA && WC_RSA_PSS */
    52274899#endif /* !NO_RSA || HAVE_ECC */
    52284900
     
    55345206}
    55355207
     5208#if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
     5209     defined(HAVE_ED448)) && \
     5210    (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH))
    55365211typedef struct Scv13Args {
    55375212    byte*  output; /* not allocated */
     
    56505325            }
    56515326
    5652             if (args->length <= 0) {
     5327            if (args->length == 0) {
    56535328                ERROR_OUT(NO_PRIVATE_KEY, exit_scv);
    56545329            }
     
    56575332            if (ssl->hsType == DYNAMIC_TYPE_RSA)
    56585333                args->sigAlgo = rsa_pss_sa_algo;
     5334        #ifdef HAVE_ECC
    56595335            else if (ssl->hsType == DYNAMIC_TYPE_ECC)
    56605336                args->sigAlgo = ecc_dsa_sa_algo;
     5337        #endif
    56615338        #ifdef HAVE_ED25519
    56625339            else if (ssl->hsType == DYNAMIC_TYPE_ED25519)
     
    56675344                args->sigAlgo = ed448_sa_algo;
    56685345        #endif
     5346            else {
     5347                ERROR_OUT(ALGO_ID_E, exit_scv);
     5348            }
    56695349            EncodeSigAlg(ssl->suites->hashAlgo, args->sigAlgo, args->verify);
    56705350
     
    59135593    return ret;
    59145594}
    5915 
     5595#endif
     5596
     5597#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
    59165598/* handle processing TLS v1.3 certificate (11) */
    59175599/* Parse and handle a TLS v1.3 Certificate message.
     
    59545636    return ret;
    59555637}
     5638#endif
    59565639
    59575640#if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
     
    60785761            if (args->sigAlgo == ed25519_sa_algo &&
    60795762                                                  !ssl->peerEd25519KeyPresent) {
    6080                 WOLFSSL_MSG("Oops, peer sent ED25519 key but not in verify");
     5763                WOLFSSL_MSG("Peer sent ED22519 sig but not ED22519 cert");
     5764                ret = SIG_VERIFY_E;
     5765                goto exit_dcv;
    60815766            }
    60825767        #endif
    60835768        #ifdef HAVE_ED448
    60845769            if (args->sigAlgo == ed448_sa_algo && !ssl->peerEd448KeyPresent) {
    6085                 WOLFSSL_MSG("Oops, peer sent ED448 key but not in verify");
     5770                WOLFSSL_MSG("Peer sent ED448 sig but not ED448 cert");
     5771                ret = SIG_VERIFY_E;
     5772                goto exit_dcv;
    60865773            }
    60875774        #endif
     
    60895776            if (args->sigAlgo == ecc_dsa_sa_algo &&
    60905777                                                   !ssl->peerEccDsaKeyPresent) {
    6091                 WOLFSSL_MSG("Oops, peer sent ECC key but not in verify");
     5778                WOLFSSL_MSG("Peer sent ECC sig but not ECC cert");
     5779                ret = SIG_VERIFY_E;
     5780                goto exit_dcv;
    60925781            }
    60935782        #endif
    60945783        #ifndef NO_RSA
    60955784            if (args->sigAlgo == rsa_sa_algo) {
    6096                 WOLFSSL_MSG("Oops, peer sent PKCS#1.5 signature");
     5785                WOLFSSL_MSG("Peer sent PKCS#1.5 algo but not in certificate");
    60975786                ERROR_OUT(INVALID_PARAMETER, exit_dcv);
    60985787            }
    60995788            if (args->sigAlgo == rsa_pss_sa_algo &&
    61005789                         (ssl->peerRsaKey == NULL || !ssl->peerRsaKeyPresent)) {
    6101                 WOLFSSL_MSG("Oops, peer sent RSA key but not in verify");
     5790                WOLFSSL_MSG("Peer sent RSA sig but not RSA cert");
     5791                ret = SIG_VERIFY_E;
     5792                goto exit_dcv;
    61025793            }
    61035794        #endif
     
    62615952        case TLS_ASYNC_VERIFY:
    62625953        {
    6263         #ifndef NO_RSA
     5954        #if !defined(NO_RSA) && defined(WC_RSA_PSS)
    62645955            if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) {
    62655956                ret = CheckRSASignature(ssl, args->sigAlgo, args->hashAlgo,
     
    62715962                ssl->peerRsaKeyPresent = 0;
    62725963            }
    6273         #endif /* !NO_RSA */
     5964        #endif /* !NO_RSA && WC_RSA_PSS */
    62745965
    62755966            /* Advance state and proceed */
     
    62915982            /* Advance state and proceed */
    62925983            ssl->options.asyncState = TLS_ASYNC_END;
     5984
     5985        #if !defined(NO_WOLFSSL_CLIENT)
     5986            if (ssl->options.side == WOLFSSL_CLIENT_END)
     5987                ssl->options.serverState = SERVER_CERT_VERIFY_COMPLETE;
     5988        #endif
    62935989        } /* case TLS_ASYNC_FINALIZE */
    62945990
     
    63266022}
    63276023#endif /* !NO_RSA || HAVE_ECC */
     6024#endif /* !NO_CERTS */
    63286025
    63296026/* Parse and handle a TLS v1.3 Finished message.
     
    63386035 * returns 0 on success and otherwise failure.
    63396036 */
    6340 static int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
     6037int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
    63416038                           word32 size, word32 totalSz, int sniff)
    63426039{
     
    63776074        secret = ssl->keys.server_write_MAC_secret;
    63786075    }
    6379     else
     6076    else {
    63806077        secret = ssl->keys.client_write_MAC_secret;
    6381 
    6382     ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz);
    6383     if (ret != 0)
    6384         return ret;
    6385     if (size != finishedSz)
    6386         return BUFFER_ERROR;
    6387 
    6388     #ifdef WOLFSSL_CALLBACKS
    6389         if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
    6390         if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo);
    6391     #endif
     6078    }
     6079
     6080    if (sniff == NO_SNIFF) {
     6081        ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz);
     6082        if (ret != 0)
     6083            return ret;
     6084        if (size != finishedSz)
     6085            return BUFFER_ERROR;
     6086    }
     6087
     6088#ifdef WOLFSSL_CALLBACKS
     6089    if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
     6090    if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo);
     6091#endif
    63926092
    63936093    if (sniff == NO_SNIFF) {
     
    64336133    return 0;
    64346134}
    6435 #endif /* NO_CERTS */
    64366135
    64376136/* Send the TLS v1.3 Finished message.
     
    65026201    if (sendSz < 0)
    65036202        return BUILD_MSG_ERROR;
    6504 
    6505 #ifndef NO_SESSION_CACHE
    6506     if (!ssl->options.resuming && (ssl->options.side == WOLFSSL_SERVER_END ||
    6507             (ssl->options.side == WOLFSSL_SERVER_END && ssl->arrays != NULL))) {
    6508         AddSession(ssl);    /* just try */
    6509     }
    6510 #endif
    65116203
    65126204    #ifdef WOLFSSL_CALLBACKS
     
    68396531    word16 length;
    68406532    word32 now;
    6841 #ifndef WOLFSSL_TLS13_DRAFT_18
    6842     const byte*  nonce;
    6843     byte         nonceLength;
    6844 #endif
     6533    const byte* nonce;
     6534    byte        nonceLength;
    68456535
    68466536    WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO);
     
    68616551    *inOutIdx += SESSION_ADD_SZ;
    68626552
    6863 #ifndef WOLFSSL_TLS13_DRAFT_18
    68646553    /* Ticket nonce. */
    68656554    if ((*inOutIdx - begin) + 1 > size)
     
    68756564    nonce = input + *inOutIdx;
    68766565    *inOutIdx += nonceLength;
    6877 #endif
    68786566
    68796567    /* Ticket length. */
     
    69026590    ssl->session.maxEarlyDataSz = ssl->options.maxEarlyDataSz;
    69036591    #endif
    6904 #ifndef WOLFSSL_TLS13_DRAFT_18
    69056592    ssl->session.ticketNonce.len = nonceLength;
    69066593    if (nonceLength > 0)
    69076594        XMEMCPY(&ssl->session.ticketNonce.data, nonce, nonceLength);
    6908 #endif
    69096595    ssl->session.namedGroup     = ssl->namedGroup;
    69106596
     
    70026688    if (ssl->earlyData != no_early_data) {
    70036689        static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 };
    7004         ret = HashInputRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData));
     6690        ret = HashRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData));
    70056691        if (ret != 0)
    70066692            return ret;
    70076693    }
    70086694#endif
    7009     if ((ret = HashInputRaw(ssl, header, sizeof(header))) != 0)
    7010         return ret;
    7011     if ((ret = HashInputRaw(ssl, mac, finishedSz)) != 0)
     6695    if ((ret = HashRaw(ssl, header, sizeof(header))) != 0)
     6696        return ret;
     6697    if ((ret = HashRaw(ssl, mac, finishedSz)) != 0)
    70126698        return ret;
    70136699
     
    70696755#endif
    70706756
    7071 #ifndef WOLFSSL_TLS13_DRAFT_18
    70726757    /* Start ticket nonce at 0 and go up to 255. */
    70736758    if (ssl->session.ticketNonce.len == 0) {
     
    70776762    else
    70786763        ssl->session.ticketNonce.data[0]++;
    7079 #endif
    70806764
    70816765    if (!ssl->options.noTicketTls13) {
     
    70996783    length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ +
    71006784             ssl->session.ticketLen + extSz;
    7101 #ifndef WOLFSSL_TLS13_DRAFT_18
    71026785    /* Nonce */
    71036786    length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ;
    7104 #endif
    71056787    sendSz = idx + length + MAX_MSG_EXTRA;
    71066788
     
    71236805    idx += SESSION_ADD_SZ;
    71246806
    7125 #ifndef WOLFSSL_TLS13_DRAFT_18
    71266807    output[idx++] = ssl->session.ticketNonce.len;
    71276808    output[idx++] = ssl->session.ticketNonce.data[0];
    7128 #endif
    71296809
    71306810    /* length */
     
    71616841    ssl->buffers.outputBuffer.length += sendSz;
    71626842
    7163     if (!ssl->options.groupMessages)
    7164         ret = SendBuffered(ssl);
     6843    /* Always send as this is either directly after server's Finished or only
     6844     * message after client's Finished.
     6845     */
     6846    ret = SendBuffered(ssl);
    71656847
    71666848    WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0);
     
    72126894            }
    72136895        #endif
    7214         #ifdef WOLFSSL_TLS13_DRAFT_18
    7215             if (ssl->msgsReceived.got_server_hello) {
     6896            if (ssl->msgsReceived.got_server_hello == 1) {
    72166897                WOLFSSL_MSG("Duplicate ServerHello received");
    72176898                return DUPLICATE_MSG_E;
    72186899            }
    7219             ssl->msgsReceived.got_server_hello = 1;
    7220         #else
    7221             if (ssl->msgsReceived.got_server_hello == 2) {
    7222                 WOLFSSL_MSG("Duplicate ServerHello received");
    7223                 return DUPLICATE_MSG_E;
    7224             }
    72256900            ssl->msgsReceived.got_server_hello++;
    7226         #endif
    72276901
    72286902            break;
     
    72736947#endif
    72746948
    7275 #ifdef WOLFSSL_TLS13_DRAFT_18
    7276     #ifndef NO_WOLFSSL_CLIENT
    7277         case hello_retry_request:
    7278         #ifndef NO_WOLFSSL_SERVER
    7279             if (ssl->options.side == WOLFSSL_SERVER_END) {
    7280                 WOLFSSL_MSG("HelloRetryRequest received by server");
    7281                 return OUT_OF_ORDER_E;
    7282             }
    7283         #endif
    7284             if (ssl->options.clientState > CLIENT_FINISHED_COMPLETE) {
    7285                 WOLFSSL_MSG("HelloRetryRequest received out of order");
    7286                 return OUT_OF_ORDER_E;
    7287             }
    7288             if (ssl->msgsReceived.got_hello_retry_request) {
    7289                 WOLFSSL_MSG("Duplicate HelloRetryRequest received");
    7290                 return DUPLICATE_MSG_E;
    7291             }
    7292             ssl->msgsReceived.got_hello_retry_request = 1;
    7293 
    7294             break;
    7295     #endif
    7296 #endif
    7297 
    72986949#ifndef NO_WOLFSSL_CLIENT
    72996950        case encrypted_extensions:
     
    73777028            if (ssl->options.serverState ==
    73787029                                         SERVER_ENCRYPTED_EXTENSIONS_COMPLETE &&
     7030                                                  ssl->arrays != NULL &&
    73797031                                                  ssl->arrays->psk_keySz != 0) {
    73807032                WOLFSSL_MSG("CertificateRequset received while using PSK");
     
    74037055                /* Server's authenticating with PSK must not send this. */
    74047056                if (ssl->options.serverState == SERVER_CERT_COMPLETE &&
     7057                                                  ssl->arrays != NULL &&
    74057058                                                  ssl->arrays->psk_keySz != 0) {
    74067059                    WOLFSSL_MSG("CertificateVerify received while using PSK");
     
    74417094                    return OUT_OF_ORDER_E;
    74427095                }
    7443                 if (ssl->options.serverState <
     7096                /* Must have seen certificate and verify from server except when
     7097                 * using PSK. */
     7098            #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
     7099                if (ssl->arrays != NULL && ssl->arrays->psk_keySz != 0) {
     7100                    if (ssl->options.serverState !=
    74447101                                         SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) {
     7102                        WOLFSSL_MSG("Finished received out of order");
     7103                        return OUT_OF_ORDER_E;
     7104                    }
     7105                }
     7106                else
     7107            #endif
     7108                if (ssl->options.serverState != SERVER_CERT_VERIFY_COMPLETE) {
    74457109                    WOLFSSL_MSG("Finished received out of order");
    74467110                    return OUT_OF_ORDER_E;
     
    74507114        #ifndef NO_WOLFSSL_SERVER
    74517115            if (ssl->options.side == WOLFSSL_SERVER_END) {
    7452                 if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
     7116                if (ssl->options.serverState != SERVER_FINISHED_COMPLETE) {
    74537117                    WOLFSSL_MSG("Finished received out of order");
    74547118                    return OUT_OF_ORDER_E;
     
    75317195    if (ssl->options.handShakeState == HANDSHAKE_DONE &&
    75327196            type != session_ticket && type != certificate_request &&
    7533             type != certificate && type != key_update) {
     7197            type != certificate && type != key_update && type != finished) {
    75347198        WOLFSSL_MSG("HandShake message after handshake complete");
    75357199        SendAlert(ssl, alert_fatal, unexpected_message);
     
    75567220#ifndef NO_WOLFSSL_CLIENT
    75577221    /* Messages only received by client. */
    7558     #ifdef WOLFSSL_TLS13_DRAFT_18
    7559     case hello_retry_request:
    7560         WOLFSSL_MSG("processing hello retry request");
    7561         ret = DoTls13HelloRetryRequest(ssl, input, inOutIdx, size);
    7562         break;
    7563     #endif
    7564 
    75657222    case server_hello:
    75667223        WOLFSSL_MSG("processing server hello");
     
    75727229                                               IsAtLeastTLSv1_3(ssl->version)) {
    75737230            ssl->options.cacheMessages = 0;
    7574             if (ssl->hsHashes->messages != NULL) {
     7231            if ((ssl->hsHashes != NULL) && (ssl->hsHashes->messages != NULL)) {
    75757232                XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES);
    75767233                ssl->hsHashes->messages = NULL;
     
    76147271
    76157272    /* Messages received by both client and server. */
    7616 #ifndef NO_CERTS
     7273#if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
     7274                           !defined(WOLFSSL_NO_CLIENT_AUTH))
    76177275    case certificate:
    76187276        WOLFSSL_MSG("processing certificate");
     
    76767334
    76777335                if ((ret = DeriveTls13Keys(ssl, handshake_key,
    7678                                            ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
     7336                                        ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
    76797337                    return ret;
    76807338                }
    76817339        #ifdef WOLFSSL_EARLY_DATA
    7682                 if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
    7683                     return ret;
    7684         #else
     7340                if (ssl->earlyData != no_early_data) {
     7341                    if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
     7342                        return ret;
     7343                }
     7344                else
     7345        #endif
    76857346                if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0)
    76867347                    return ret;
    7687         #endif
    76887348            }
    76897349
     
    76937353        #ifdef WOLFSSL_EARLY_DATA
    76947354                if ((ret = DeriveTls13Keys(ssl, traffic_key,
    7695                                        ENCRYPT_AND_DECRYPT_SIDE,
    7696                                        ssl->earlyData == no_early_data)) != 0) {
     7355                                    ENCRYPT_AND_DECRYPT_SIDE,
     7356                                    ssl->earlyData == no_early_data)) != 0) {
    76977357                    return ret;
    76987358                }
    76997359        #else
    77007360                if ((ret = DeriveTls13Keys(ssl, traffic_key,
    7701                                            ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
     7361                                        ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
    77027362                    return ret;
    77037363                }
     
    77117371                ssl->options.connectState  = FIRST_REPLY_DONE;
    77127372                ssl->options.handShakeState = CLIENT_HELLO_COMPLETE;
    7713 
    7714                 if (wolfSSL_connect_TLSv13(ssl) != SSL_SUCCESS)
    7715                     ret = POST_HAND_AUTH_ERROR;
     7373                ssl->options.processReply = 0; /* doProcessInit */
     7374
     7375                if (wolfSSL_connect_TLSv13(ssl) != WOLFSSL_SUCCESS) {
     7376                    ret = ssl->error;
     7377                    if (ret != WC_PENDING_E)
     7378                        ret = POST_HAND_AUTH_ERROR;
     7379                }
    77167380            }
    77177381        #endif
     
    77567420    int    ret = 0;
    77577421    word32 inputLength;
     7422    byte   type;
     7423    word32 size = 0;
    77587424
    77597425    WOLFSSL_ENTER("DoTls13HandShakeMsg()");
    77607426
    77617427    if (ssl->arrays == NULL) {
    7762         byte   type;
    7763         word32 size;
     7428
    77647429
    77657430        if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size,
     
    77787443     * pending message size will be non-zero. */
    77797444    if (ssl->arrays->pendingMsgSz == 0) {
    7780         byte   type;
    7781         word32 size;
    77827445
    77837446        if (GetHandshakeHeader(ssl,input, inOutIdx, &type, &size, totalSz) != 0)
     
    78797542    if (ssl->buffers.outputBuffer.length > 0
    78807543    #ifdef WOLFSSL_ASYNC_CRYPT
    7881         /* do not send buffered or advance state if last error was an 
     7544        /* do not send buffered or advance state if last error was an
    78827545            async pending operation */
    78837546        && ssl->error != WC_PENDING_E
     
    79177580    #ifdef WOLFSSL_EARLY_DATA
    79187581            if (ssl->earlyData != no_early_data) {
    7919         #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
    7920                                          defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
     7582        #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
    79217583                if ((ssl->error = SendChangeCipher(ssl)) != 0) {
    79227584                    WOLFSSL_ERROR(ssl->error);
     
    79417603            }
    79427604
    7943             ssl->options.connectState = HELLO_AGAIN;
    7944             WOLFSSL_MSG("connect state: HELLO_AGAIN");
    7945             FALL_THROUGH;
    7946 
    7947         case HELLO_AGAIN:
    7948             if (ssl->options.certOnly)
    7949                 return WOLFSSL_SUCCESS;
    7950 
    79517605            if (!ssl->options.tls1_3) {
    79527606    #ifndef WOLFSSL_NO_TLS12
     
    79547608                    return wolfSSL_connect(ssl);
    79557609    #endif
    7956 
    79577610                WOLFSSL_MSG("Client using higher version, fatal error");
    79587611                return VERSION_ERROR;
    79597612            }
    79607613
     7614            ssl->options.connectState = HELLO_AGAIN;
     7615            WOLFSSL_MSG("connect state: HELLO_AGAIN");
     7616            FALL_THROUGH;
     7617
     7618        case HELLO_AGAIN:
     7619            if (ssl->options.certOnly)
     7620                return WOLFSSL_SUCCESS;
     7621
    79617622            if (ssl->options.serverState ==
    79627623                                          SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
    7963         #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
    7964                                          defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
     7624        #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
    79657625                if (!ssl->options.sentChangeCipher) {
    79667626                    if ((ssl->error = SendChangeCipher(ssl)) != 0) {
     
    80117671
    80127672        case FIRST_REPLY_FIRST:
    8013         #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
    8014                                          defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
     7673        #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
    80157674            if (!ssl->options.sentChangeCipher) {
    80167675                if ((ssl->error = SendChangeCipher(ssl)) != 0) {
     
    80437702
    80447703        case FIRST_REPLY_THIRD:
    8045         #ifndef NO_CERTS
     7704        #if (!defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \
     7705             defined(HAVE_ED25519) || defined(HAVE_ED448))) && \
     7706             (!defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH))
    80467707            if (!ssl->options.resuming && ssl->options.sendVerify) {
    80477708                ssl->error = SendTls13CertificateVerify(ssl);
     
    81727833#endif
    81737834
     7835#ifdef HAVE_SUPPORTED_CURVES
    81747836/* Create a key share entry from group.
    81757837 * Generates a key pair.
     
    82137875    return WOLFSSL_SUCCESS;
    82147876}
     7877#endif
    82157878
    82167879/* Do not send a ticket after TLS v1.3 handshake for resumption.
     
    84148077        return NOT_READY_ERROR;
    84158078
     8079#ifdef HAVE_SUPPORTED_CURVES
    84168080    /* Return supported groups only. */
    84178081    return TLSX_SupportedCurve_Preferred(ssl, 1);
    84188082#else
     8083    return 0;
     8084#endif
     8085#else
    84198086    return SIDE_ERROR;
    84208087#endif
     
    84228089#endif
    84238090
     8091#if defined(HAVE_SUPPORTED_CURVES)
    84248092/* Sets the key exchange groups in rank order on a context.
    84258093 *
     
    84328100int wolfSSL_CTX_set_groups(WOLFSSL_CTX* ctx, int* groups, int count)
    84338101{
    8434     int i;
    8435 
     8102    int ret, i;
     8103
     8104    WOLFSSL_ENTER("wolfSSL_CTX_set_groups");
    84368105    if (ctx == NULL || groups == NULL || count > WOLFSSL_MAX_GROUP_COUNT)
    84378106        return BAD_FUNC_ARG;
     
    84398108        return BAD_FUNC_ARG;
    84408109
    8441     for (i = 0; i < count; i++)
    8442         ctx->group[i] = (word16)groups[i];
     8110    ctx->numGroups = 0;
     8111    TLSX_Remove(&ctx->extensions, TLSX_SUPPORTED_GROUPS, ctx->heap);
     8112    for (i = 0; i < count; i++) {
     8113        /* Call to wolfSSL_CTX_UseSupportedCurve also checks if input groups
     8114         * are valid */
     8115        if ((ret = wolfSSL_CTX_UseSupportedCurve(ctx, groups[i]))
     8116                != WOLFSSL_SUCCESS) {
     8117            TLSX_Remove(&ctx->extensions, TLSX_SUPPORTED_GROUPS, ctx->heap);
     8118            return ret;
     8119        }
     8120        ctx->group[i] = groups[i];
     8121    }
    84438122    ctx->numGroups = (byte)count;
    84448123
     
    84568135int wolfSSL_set_groups(WOLFSSL* ssl, int* groups, int count)
    84578136{
    8458     int i;
    8459 
     8137    int ret, i;
     8138
     8139    WOLFSSL_ENTER("wolfSSL_set_groups");
    84608140    if (ssl == NULL || groups == NULL || count > WOLFSSL_MAX_GROUP_COUNT)
    84618141        return BAD_FUNC_ARG;
     
    84638143        return BAD_FUNC_ARG;
    84648144
    8465     for (i = 0; i < count; i++)
    8466         ssl->group[i] = (word16)groups[i];
     8145    ssl->numGroups = 0;
     8146    TLSX_Remove(&ssl->extensions, TLSX_SUPPORTED_GROUPS, ssl->heap);
     8147    for (i = 0; i < count; i++) {
     8148        /* Call to wolfSSL_UseSupportedCurve also checks if input groups
     8149                 * are valid */
     8150        if ((ret = wolfSSL_UseSupportedCurve(ssl, groups[i]))
     8151                != WOLFSSL_SUCCESS) {
     8152            TLSX_Remove(&ssl->extensions, TLSX_SUPPORTED_GROUPS, ssl->heap);
     8153            return ret;
     8154        }
     8155        ssl->group[i] = groups[i];
     8156    }
    84678157    ssl->numGroups = (byte)count;
    84688158
    84698159    return WOLFSSL_SUCCESS;
    84708160}
     8161#endif /* HAVE_SUPPORTED_CURVES */
    84718162
    84728163#ifndef NO_PSK
     
    86078298    if (ssl->buffers.outputBuffer.length > 0
    86088299    #ifdef WOLFSSL_ASYNC_CRYPT
    8609         /* do not send buffered or advance state if last error was an 
     8300        /* do not send buffered or advance state if last error was an
    86108301            async pending operation */
    86118302        && ssl->error != WC_PENDING_E
     
    86538344
    86548345        case TLS13_ACCEPT_CLIENT_HELLO_DONE :
    8655 #ifdef WOLFSSL_TLS13_DRAFT_18
    8656             if (ssl->options.serverState ==
    8657                                           SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
    8658                 if ((ssl->error = SendTls13HelloRetryRequest(ssl)) != 0) {
    8659                     WOLFSSL_ERROR(ssl->error);
    8660                     return WOLFSSL_FATAL_ERROR;
    8661                 }
    8662             }
    8663 
    8664             ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE;
    8665             WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
    8666             FALL_THROUGH;
    8667 
    8668         case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE :
    8669 #else
    86708346            if (ssl->options.serverState ==
    86718347                                          SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
     
    86968372            WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
    86978373            FALL_THROUGH;
    8698 #endif
    86998374
    87008375        case TLS13_ACCEPT_FIRST_REPLY_DONE :
     
    87248399
    87258400        case TLS13_SERVER_HELLO_SENT :
    8726     #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
    8727                                          defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
     8401    #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
    87288402            if (!ssl->options.sentChangeCipher) {
    87298403                if ((ssl->error = SendChangeCipher(ssl)) != 0) {
     
    87408414
    87418415        case TLS13_ACCEPT_THIRD_REPLY_DONE :
     8416#ifdef HAVE_SUPPORTED_CURVES
    87428417            if (!ssl->options.noPskDheKe) {
    87438418                ssl->error = TLSX_KeyShare_DeriveSecret(ssl);
     
    87458420                    return WOLFSSL_FATAL_ERROR;
    87468421            }
     8422#endif
    87478423
    87488424            if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) {
     
    87848460
    87858461        case TLS13_CERT_SENT :
    8786 #ifndef NO_CERTS
     8462#if !defined(NO_CERTS) && (!defined(NO_RSA) || defined(HAVE_ECC) || \
     8463     defined(HAVE_ED25519) || defined(HAVE_ED448))
    87878464            if (!ssl->options.resuming && ssl->options.sendVerify) {
    87888465                if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) {
Note: See TracChangeset for help on using the changeset viewer.