@@ -3306,6 +3306,243 @@ static int test_brainpool_server_offer(int* tls13Cnt, int* tls12Cnt)
33063306 return EXPECT_RESULT ();
33073307}
33083308#endif
3309+
3310+ #if defined(HAVE_ECH ) && defined(HAVE_SNI ) && !defined(NO_CERTS ) && \
3311+ !defined(NO_FILESYSTEM ) && !defined(NO_RSA ) && !defined(NO_WOLFSSL_SERVER )
3312+ /* Counts the brainpool codepoints in the ECH inner ClientHello as the server
3313+ * parsed it, or in the list the client uses after a TLS 1.2 ServerHello. */
3314+ static int test_brainpool_ech_offer (method_provider serverMethod ,
3315+ int * tls13Cnt , int * tls12Cnt )
3316+ {
3317+ EXPECT_DECLS ;
3318+ struct test_memio_ctx test_ctx ;
3319+ WOLFSSL_CTX * ctx_c = NULL ;
3320+ WOLFSSL_CTX * ctx_s = NULL ;
3321+ WOLFSSL * ssl_c = NULL ;
3322+ WOLFSSL * ssl_s = NULL ;
3323+ TLSX * ext = NULL ;
3324+ static const char pubName [] = "ech-public-name.com" ;
3325+ static const char privName [] = "ech-private-name.com" ;
3326+ byte configs [512 ];
3327+ word32 configsLen = (word32 )sizeof (configs );
3328+ int tls12Server = (serverMethod == wolfTLSv1_2_server_method );
3329+
3330+ * tls13Cnt = 0 ;
3331+ * tls12Cnt = 0 ;
3332+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
3333+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
3334+ wolfSSLv23_client_method , serverMethod ), 0 );
3335+ wolfSSL_set_verify (ssl_c , WOLFSSL_VERIFY_NONE , NULL );
3336+ ExpectIntEQ (wolfSSL_CTX_GenerateEchConfig (ctx_s , pubName , 0 , 0 , 0 ),
3337+ WOLFSSL_SUCCESS );
3338+ ExpectIntEQ (wolfSSL_CTX_GetEchConfigs (ctx_s , configs , & configsLen ),
3339+ WOLFSSL_SUCCESS );
3340+ ExpectIntEQ (wolfSSL_SetEchConfigs (ssl_c , configs , configsLen ),
3341+ WOLFSSL_SUCCESS );
3342+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME , privName ,
3343+ (word16 )XSTRLEN (privName )), WOLFSSL_SUCCESS );
3344+
3345+ ExpectIntEQ (wolfSSL_connect (ssl_c ), -1 );
3346+ ExpectIntEQ (wolfSSL_get_error (ssl_c , -1 ), WOLFSSL_ERROR_WANT_READ );
3347+ ExpectIntEQ (wolfSSL_accept (ssl_s ), -1 );
3348+ ExpectIntEQ (wolfSSL_get_error (ssl_s , -1 ), WOLFSSL_ERROR_WANT_READ );
3349+ if (tls12Server ) {
3350+ ExpectIntEQ (wolfSSL_connect (ssl_c ), -1 );
3351+ ExpectIntEQ (wolfSSL_get_error (ssl_c , -1 ), WOLFSSL_ERROR_WANT_READ );
3352+ ExpectIntEQ (wolfSSL_version (ssl_c ), TLS1_2_VERSION );
3353+ if (ssl_c != NULL )
3354+ ext = TLSX_Find (ssl_c -> extensions , TLSX_SUPPORTED_GROUPS );
3355+ }
3356+ else {
3357+ ExpectIntEQ (wolfSSL_GetEchStatus (ssl_s ),
3358+ WOLFSSL_ECH_STATUS_ACCEPTED );
3359+ if (ssl_s != NULL )
3360+ ext = TLSX_Find (ssl_s -> extensions , TLSX_SUPPORTED_GROUPS );
3361+ if (ext != NULL )
3362+ ExpectIntEQ (ext -> resp , 0 );
3363+ }
3364+ ExpectNotNull (ext );
3365+ test_brainpool_count (ext , tls13Cnt , tls12Cnt );
3366+
3367+ wolfSSL_free (ssl_c );
3368+ wolfSSL_free (ssl_s );
3369+ wolfSSL_CTX_free (ctx_c );
3370+ wolfSSL_CTX_free (ctx_s );
3371+ return EXPECT_RESULT ();
3372+ }
3373+
3374+ #define TEST_BP_MAX_EXTS 64
3375+
3376+ /* Lists a ClientHello's extension types, less those allowed to change after a
3377+ * HelloRetryRequest and supported_versions, and finds supported_groups. */
3378+ static int test_bp_parse_ch (const byte * buf , int len , word16 * types ,
3379+ int * typeCnt , const byte * * groups , word16 * groupsLen )
3380+ {
3381+ int idx = 0 ;
3382+ int end ;
3383+ word16 recLen ;
3384+ word16 extLen ;
3385+ word16 type ;
3386+
3387+ * typeCnt = 0 ;
3388+ * groups = NULL ;
3389+ * groupsLen = 0 ;
3390+ while (idx + RECORD_HEADER_SZ <= len && buf [idx ] != handshake ) {
3391+ ato16 (buf + idx + 3 , & recLen );
3392+ idx += RECORD_HEADER_SZ + recLen ;
3393+ }
3394+ if (idx + RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ > len ||
3395+ buf [idx + RECORD_HEADER_SZ ] != client_hello ) {
3396+ return -1 ;
3397+ }
3398+ ato16 (buf + idx + 3 , & recLen );
3399+ end = idx + RECORD_HEADER_SZ + recLen ;
3400+ if (end > len )
3401+ return -1 ;
3402+ idx += RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + VERSION_SZ + RAN_LEN ;
3403+ if (idx >= end )
3404+ return -1 ;
3405+ idx += 1 + buf [idx ];
3406+ if (idx + OPAQUE16_LEN > end )
3407+ return -1 ;
3408+ ato16 (buf + idx , & extLen );
3409+ idx += OPAQUE16_LEN + extLen ;
3410+ if (idx >= end )
3411+ return -1 ;
3412+ idx += 1 + buf [idx ];
3413+ idx += OPAQUE16_LEN ;
3414+ while (idx + 2 * OPAQUE16_LEN <= end && * typeCnt < TEST_BP_MAX_EXTS ) {
3415+ ato16 (buf + idx , & type );
3416+ ato16 (buf + idx + OPAQUE16_LEN , & extLen );
3417+ if (idx + 2 * OPAQUE16_LEN + extLen > end )
3418+ return -1 ;
3419+ if (type == TLSXT_SUPPORTED_GROUPS ) {
3420+ * groups = buf + idx + 2 * OPAQUE16_LEN ;
3421+ * groupsLen = extLen ;
3422+ }
3423+ if (type != TLSXT_KEY_SHARE && type != TLSXT_EARLY_DATA &&
3424+ type != TLSXT_COOKIE && type != TLSXT_PRE_SHARED_KEY &&
3425+ type != TLSXT_ECH && type != TLSXT_SUPPORTED_VERSIONS ) {
3426+ types [(* typeCnt )++ ] = type ;
3427+ }
3428+ idx += 2 * OPAQUE16_LEN + extLen ;
3429+ }
3430+ return (* groups != NULL && idx == end ) ? 0 : -1 ;
3431+ }
3432+
3433+ /* Compares the ClientHellos before and after a HelloRetryRequest with ECH and
3434+ * counts the brainpool codepoints in the second one. */
3435+ static int test_brainpool_ech_hrr (int accept , int * tls13Cnt , int * tls12Cnt )
3436+ {
3437+ EXPECT_DECLS ;
3438+ struct test_memio_ctx test_ctx ;
3439+ WOLFSSL_CTX * ctx_c = NULL ;
3440+ WOLFSSL_CTX * ctx_s = NULL ;
3441+ WOLFSSL_CTX * ctx_o = NULL ;
3442+ WOLFSSL * ssl_c = NULL ;
3443+ WOLFSSL * ssl_s = NULL ;
3444+ static const char pubName [] = "ech-public-name.com" ;
3445+ static const char privName [] = "ech-private-name.com" ;
3446+ byte configs [512 ];
3447+ word32 configsLen = (word32 )sizeof (configs );
3448+ byte ch1 [TEST_MEMIO_BUF_SZ ];
3449+ int ch1Len = 0 ;
3450+ word16 types1 [TEST_BP_MAX_EXTS ];
3451+ word16 types2 [TEST_BP_MAX_EXTS ];
3452+ int cnt1 = 0 ;
3453+ int cnt2 = 0 ;
3454+ const byte * groups1 = NULL ;
3455+ const byte * groups2 = NULL ;
3456+ word16 groupsLen1 = 0 ;
3457+ word16 groupsLen2 = 0 ;
3458+ word16 name ;
3459+ int innerTls13Cnt = 0 ;
3460+ int innerTls12Cnt = 0 ;
3461+ int i ;
3462+ int j ;
3463+
3464+ * tls13Cnt = 0 ;
3465+ * tls12Cnt = 0 ;
3466+ XMEMSET (& test_ctx , 0 , sizeof (test_ctx ));
3467+ ExpectIntEQ (test_memio_setup (& test_ctx , & ctx_c , & ctx_s , & ssl_c , & ssl_s ,
3468+ wolfSSLv23_client_method , wolfSSLv23_server_method ), 0 );
3469+ wolfSSL_set_verify (ssl_c , WOLFSSL_VERIFY_NONE , NULL );
3470+ ExpectIntEQ (wolfSSL_CTX_GenerateEchConfig (ctx_s , pubName , 0 , 0 , 0 ),
3471+ WOLFSSL_SUCCESS );
3472+ if (accept ) {
3473+ ExpectIntEQ (wolfSSL_CTX_GetEchConfigs (ctx_s , configs , & configsLen ),
3474+ WOLFSSL_SUCCESS );
3475+ }
3476+ else {
3477+ /* Keys the server does not hold, so it rejects ECH. */
3478+ ExpectNotNull (ctx_o = wolfSSL_CTX_new (wolfSSLv23_server_method ()));
3479+ ExpectIntEQ (wolfSSL_CTX_GenerateEchConfig (ctx_o , pubName , 0 , 0 , 0 ),
3480+ WOLFSSL_SUCCESS );
3481+ ExpectIntEQ (wolfSSL_CTX_GetEchConfigs (ctx_o , configs , & configsLen ),
3482+ WOLFSSL_SUCCESS );
3483+ }
3484+ ExpectIntEQ (wolfSSL_SetEchConfigs (ssl_c , configs , configsLen ),
3485+ WOLFSSL_SUCCESS );
3486+ ExpectIntEQ (wolfSSL_UseSNI (ssl_c , WOLFSSL_SNI_HOST_NAME , privName ,
3487+ (word16 )XSTRLEN (privName )), WOLFSSL_SUCCESS );
3488+ ExpectIntEQ (wolfSSL_NoKeyShares (ssl_c ), WOLFSSL_SUCCESS );
3489+
3490+ ExpectIntEQ (wolfSSL_connect (ssl_c ), -1 );
3491+ ExpectIntEQ (wolfSSL_get_error (ssl_c , -1 ), WOLFSSL_ERROR_WANT_READ );
3492+ if (EXPECT_SUCCESS ()) {
3493+ ch1Len = test_ctx .s_len ;
3494+ XMEMCPY (ch1 , test_ctx .s_buff , (size_t )ch1Len );
3495+ }
3496+ ExpectIntEQ (wolfSSL_accept (ssl_s ), -1 );
3497+ ExpectIntEQ (wolfSSL_get_error (ssl_s , -1 ), WOLFSSL_ERROR_WANT_READ );
3498+ ExpectIntEQ (wolfSSL_connect (ssl_c ), -1 );
3499+ ExpectIntEQ (wolfSSL_get_error (ssl_c , -1 ), WOLFSSL_ERROR_WANT_READ );
3500+ if (ssl_c != NULL ) {
3501+ ExpectIntEQ (ssl_c -> options .serverState ,
3502+ SERVER_HELLO_RETRY_REQUEST_COMPLETE );
3503+ }
3504+
3505+ ExpectIntEQ (test_bp_parse_ch (ch1 , ch1Len , types1 , & cnt1 , & groups1 ,
3506+ & groupsLen1 ), 0 );
3507+ ExpectIntEQ (test_bp_parse_ch (test_ctx .s_buff , test_ctx .s_len , types2 ,
3508+ & cnt2 , & groups2 , & groupsLen2 ), 0 );
3509+ ExpectIntEQ (cnt1 , cnt2 );
3510+ if (EXPECT_SUCCESS ())
3511+ ExpectIntEQ (XMEMCMP (types1 , types2 , (size_t )cnt1 * sizeof (word16 )), 0 );
3512+ ExpectIntEQ (groupsLen1 , groupsLen2 );
3513+ if (EXPECT_SUCCESS ()) {
3514+ ExpectIntEQ (XMEMCMP (groups1 , groups2 , groupsLen1 ), 0 );
3515+ for (i = OPAQUE16_LEN ; i + OPAQUE16_LEN <= groupsLen2 ;
3516+ i += OPAQUE16_LEN ) {
3517+ ato16 (groups2 + i , & name );
3518+ for (j = 0 ; j < TEST_BP_CNT ; j ++ ) {
3519+ if (name == test_bp_tls13 [j ])
3520+ (* tls13Cnt )++ ;
3521+ else if (name == test_bp_tls12 [j ])
3522+ (* tls12Cnt )++ ;
3523+ }
3524+ }
3525+ }
3526+
3527+ if (accept ) {
3528+ if (ssl_c != NULL ) {
3529+ test_brainpool_count (TLSX_Find (ssl_c -> extensions ,
3530+ TLSX_SUPPORTED_GROUPS ), & innerTls13Cnt , & innerTls12Cnt );
3531+ }
3532+ ExpectIntEQ (innerTls13Cnt , TEST_BP_CNT );
3533+ ExpectIntEQ (innerTls12Cnt , 0 );
3534+ ExpectIntEQ (test_memio_do_handshake (ssl_c , ssl_s , 10 , NULL ), 0 );
3535+ ExpectIntEQ (wolfSSL_GetEchStatus (ssl_c ), WOLFSSL_ECH_STATUS_ACCEPTED );
3536+ }
3537+
3538+ wolfSSL_free (ssl_c );
3539+ wolfSSL_free (ssl_s );
3540+ wolfSSL_CTX_free (ctx_c );
3541+ wolfSSL_CTX_free (ctx_s );
3542+ wolfSSL_CTX_free (ctx_o );
3543+ return EXPECT_RESULT ();
3544+ }
3545+ #endif
33093546#endif
33103547
33113548/* The RFC 7027 brainpool codepoints are offered only when TLS 1.2 or older can
@@ -3412,6 +3649,29 @@ int test_TLSX_SupportedCurve_brainpool_downgrade(void)
34123649 ExpectIntEQ (tls13Cnt , TEST_BP_CNT );
34133650 ExpectIntEQ (tls12Cnt , 0 );
34143651#endif
3652+
3653+ #if defined(HAVE_ECH ) && defined(HAVE_SNI ) && !defined(NO_CERTS ) && \
3654+ !defined(NO_FILESYSTEM ) && !defined(NO_RSA ) && !defined(NO_WOLFSSL_SERVER )
3655+ ExpectIntEQ (test_brainpool_ech_offer (wolfSSLv23_server_method , & tls13Cnt ,
3656+ & tls12Cnt ), TEST_SUCCESS );
3657+ ExpectIntEQ (tls13Cnt , TEST_BP_CNT );
3658+ ExpectIntEQ (tls12Cnt , 0 );
3659+
3660+ ExpectIntEQ (test_brainpool_ech_offer (wolfTLSv1_2_server_method , & tls13Cnt ,
3661+ & tls12Cnt ), TEST_SUCCESS );
3662+ ExpectIntEQ (tls13Cnt , TEST_BP_CNT );
3663+ ExpectIntEQ (tls12Cnt , TEST_BP_CNT );
3664+
3665+ ExpectIntEQ (test_brainpool_ech_hrr (1 , & tls13Cnt , & tls12Cnt ),
3666+ TEST_SUCCESS );
3667+ ExpectIntEQ (tls13Cnt , TEST_BP_CNT );
3668+ ExpectIntEQ (tls12Cnt , TEST_BP_CNT );
3669+
3670+ ExpectIntEQ (test_brainpool_ech_hrr (0 , & tls13Cnt , & tls12Cnt ),
3671+ TEST_SUCCESS );
3672+ ExpectIntEQ (tls13Cnt , TEST_BP_CNT );
3673+ ExpectIntEQ (tls12Cnt , TEST_BP_CNT );
3674+ #endif
34153675#endif
34163676 return EXPECT_RESULT ();
34173677}
0 commit comments