static int test_custom_exts(int tst)
{
    SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
    SSL *clientssl = NULL, *serverssl = NULL;
    int testresult = 0;
    static int server = 1;
    static int client = 0;
    SSL_SESSION *sess = NULL;
    unsigned int context;
#if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)
    if (tst < 3)
        return 1;
#endif
    clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
    clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
    snicb = 0;
    if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
                                       TLS1_VERSION, 0,
                                       &sctx, &cctx, cert, privkey)))
        goto end;
    if (tst == 2
            && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL,
                                              TLS1_VERSION, 0,
                                              &sctx2, NULL, cert, privkey)))
        goto end;
    if (tst < 3) {
        SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
        SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
        if (sctx2 != NULL)
            SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
    }
    if (tst == 4) {
        context = SSL_EXT_CLIENT_HELLO
                  | SSL_EXT_TLS1_2_SERVER_HELLO
                  | SSL_EXT_TLS1_3_SERVER_HELLO
                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
                  | SSL_EXT_TLS1_3_CERTIFICATE
                  | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
    } else {
        context = SSL_EXT_CLIENT_HELLO
                  | SSL_EXT_TLS1_2_SERVER_HELLO
                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
    }
    if (tst == 0) {
        if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
                                                     old_add_cb, old_free_cb,
                                                     &client, old_parse_cb,
                                                     &client)))
            goto end;
    } else {
        if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
                                              new_add_cb, new_free_cb,
                                              &client, new_parse_cb, &client)))
            goto end;
    }
    if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
                                                  old_add_cb, old_free_cb,
                                                  &client, old_parse_cb,
                                                  &client))
            || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
                                                  context, new_add_cb,
                                                  new_free_cb, &client,
                                                  new_parse_cb, &client)))
        goto end;
    if (tst == 0) {
        if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
                                                     old_add_cb, old_free_cb,
                                                     &server, old_parse_cb,
                                                     &server)))
            goto end;
    } else {
        if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
                                              new_add_cb, new_free_cb,
                                              &server, new_parse_cb, &server)))
            goto end;
        if (sctx2 != NULL
                && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
                                                     context, new_add_cb,
                                                     new_free_cb, &server,
                                                     new_parse_cb, &server)))
            goto end;
    }
    if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
                                                  old_add_cb, old_free_cb,
                                                  &server, old_parse_cb,
                                                  &server))
            || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
                                                  context, new_add_cb,
                                                  new_free_cb, &server,
                                                  new_parse_cb, &server)))
        goto end;
    if (tst == 2) {
        if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
                || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
            goto end;
    }
    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
                                      &clientssl, NULL, NULL))
            || !TEST_true(create_ssl_connection(serverssl, clientssl,
                                                SSL_ERROR_NONE)))
        goto end;
    if (tst == 0) {
        if (clntaddoldcb != 1
                || clntparseoldcb != 1
                || srvaddoldcb != 1
                || srvparseoldcb != 1)
            goto end;
    } else if (tst == 1 || tst == 2 || tst == 3) {
        if (clntaddnewcb != 1
                || clntparsenewcb != 1
                || srvaddnewcb != 1
                || srvparsenewcb != 1
                || (tst != 2 && snicb != 0)
                || (tst == 2 && snicb != 1))
            goto end;
    } else {
        if (clntaddnewcb != 1
                || clntparsenewcb != 5
                || srvaddnewcb != 5
                || srvparsenewcb != 1)
            goto end;
    }
    sess = SSL_get1_session(clientssl);
    SSL_shutdown(clientssl);
    SSL_shutdown(serverssl);
    SSL_free(serverssl);
    SSL_free(clientssl);
    serverssl = clientssl = NULL;
    if (tst == 3) {
        testresult = 1;
        goto end;
    }
    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
                                      NULL, NULL))
            || !TEST_true(SSL_set_session(clientssl, sess))
            || !TEST_true(create_ssl_connection(serverssl, clientssl,
                                               SSL_ERROR_NONE)))
        goto end;
    if (tst == 0) {
        if (clntaddoldcb != 2
                || clntparseoldcb != 1
                || srvaddoldcb != 1
                || srvparseoldcb != 1)
            goto end;
    } else if (tst == 1 || tst == 2 || tst == 3) {
        if (clntaddnewcb != 2
                || clntparsenewcb != 2
                || srvaddnewcb != 2
                || srvparsenewcb != 2)
            goto end;
    } else {
        if (clntaddnewcb != 2
                || clntparsenewcb != 8
                || srvaddnewcb != 8
                || srvparsenewcb != 2)
            goto end;
    }
    testresult = 1;
end:
    SSL_SESSION_free(sess);
    SSL_free(serverssl);
    SSL_free(clientssl);
    SSL_CTX_free(sctx2);
    SSL_CTX_free(sctx);
    SSL_CTX_free(cctx);
    return testresult;
}
['static int test_custom_exts(int tst)\n{\n    SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;\n    SSL *clientssl = NULL, *serverssl = NULL;\n    int testresult = 0;\n    static int server = 1;\n    static int client = 0;\n    SSL_SESSION *sess = NULL;\n    unsigned int context;\n#if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)\n    if (tst < 3)\n        return 1;\n#endif\n    clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;\n    clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;\n    snicb = 0;\n    if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),\n                                       TLS1_VERSION, 0,\n                                       &sctx, &cctx, cert, privkey)))\n        goto end;\n    if (tst == 2\n            && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL,\n                                              TLS1_VERSION, 0,\n                                              &sctx2, NULL, cert, privkey)))\n        goto end;\n    if (tst < 3) {\n        SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);\n        SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);\n        if (sctx2 != NULL)\n            SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);\n    }\n    if (tst == 4) {\n        context = SSL_EXT_CLIENT_HELLO\n                  | SSL_EXT_TLS1_2_SERVER_HELLO\n                  | SSL_EXT_TLS1_3_SERVER_HELLO\n                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS\n                  | SSL_EXT_TLS1_3_CERTIFICATE\n                  | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;\n    } else {\n        context = SSL_EXT_CLIENT_HELLO\n                  | SSL_EXT_TLS1_2_SERVER_HELLO\n                  | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;\n    }\n    if (tst == 0) {\n        if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,\n                                                     old_add_cb, old_free_cb,\n                                                     &client, old_parse_cb,\n                                                     &client)))\n            goto end;\n    } else {\n        if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,\n                                              new_add_cb, new_free_cb,\n                                              &client, new_parse_cb, &client)))\n            goto end;\n    }\n    if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,\n                                                  old_add_cb, old_free_cb,\n                                                  &client, old_parse_cb,\n                                                  &client))\n            || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,\n                                                  context, new_add_cb,\n                                                  new_free_cb, &client,\n                                                  new_parse_cb, &client)))\n        goto end;\n    if (tst == 0) {\n        if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,\n                                                     old_add_cb, old_free_cb,\n                                                     &server, old_parse_cb,\n                                                     &server)))\n            goto end;\n    } else {\n        if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,\n                                              new_add_cb, new_free_cb,\n                                              &server, new_parse_cb, &server)))\n            goto end;\n        if (sctx2 != NULL\n                && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,\n                                                     context, new_add_cb,\n                                                     new_free_cb, &server,\n                                                     new_parse_cb, &server)))\n            goto end;\n    }\n    if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,\n                                                  old_add_cb, old_free_cb,\n                                                  &server, old_parse_cb,\n                                                  &server))\n            || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,\n                                                  context, new_add_cb,\n                                                  new_free_cb, &server,\n                                                  new_parse_cb, &server)))\n        goto end;\n    if (tst == 2) {\n        if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))\n                || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))\n            goto end;\n    }\n    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n                                      &clientssl, NULL, NULL))\n            || !TEST_true(create_ssl_connection(serverssl, clientssl,\n                                                SSL_ERROR_NONE)))\n        goto end;\n    if (tst == 0) {\n        if (clntaddoldcb != 1\n                || clntparseoldcb != 1\n                || srvaddoldcb != 1\n                || srvparseoldcb != 1)\n            goto end;\n    } else if (tst == 1 || tst == 2 || tst == 3) {\n        if (clntaddnewcb != 1\n                || clntparsenewcb != 1\n                || srvaddnewcb != 1\n                || srvparsenewcb != 1\n                || (tst != 2 && snicb != 0)\n                || (tst == 2 && snicb != 1))\n            goto end;\n    } else {\n        if (clntaddnewcb != 1\n                || clntparsenewcb != 5\n                || srvaddnewcb != 5\n                || srvparsenewcb != 1)\n            goto end;\n    }\n    sess = SSL_get1_session(clientssl);\n    SSL_shutdown(clientssl);\n    SSL_shutdown(serverssl);\n    SSL_free(serverssl);\n    SSL_free(clientssl);\n    serverssl = clientssl = NULL;\n    if (tst == 3) {\n        testresult = 1;\n        goto end;\n    }\n    if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n                                      NULL, NULL))\n            || !TEST_true(SSL_set_session(clientssl, sess))\n            || !TEST_true(create_ssl_connection(serverssl, clientssl,\n                                               SSL_ERROR_NONE)))\n        goto end;\n    if (tst == 0) {\n        if (clntaddoldcb != 2\n                || clntparseoldcb != 1\n                || srvaddoldcb != 1\n                || srvparseoldcb != 1)\n            goto end;\n    } else if (tst == 1 || tst == 2 || tst == 3) {\n        if (clntaddnewcb != 2\n                || clntparsenewcb != 2\n                || srvaddnewcb != 2\n                || srvparsenewcb != 2)\n            goto end;\n    } else {\n        if (clntaddnewcb != 2\n                || clntparsenewcb != 8\n                || srvaddnewcb != 8\n                || srvparsenewcb != 2)\n            goto end;\n    }\n    testresult = 1;\nend:\n    SSL_SESSION_free(sess);\n    SSL_free(serverssl);\n    SSL_free(clientssl);\n    SSL_CTX_free(sctx2);\n    SSL_CTX_free(sctx);\n    SSL_CTX_free(cctx);\n    return testresult;\n}', 'IMPLEMENT_tls_meth_func(TLS_ANY_VERSION, 0, 0,\n                        TLS_server_method,\n                        ossl_statem_accept,\n                        ssl_undefined_function, TLSv1_2_enc_data)', 'IMPLEMENT_tls_meth_func(TLS_ANY_VERSION, 0, 0,\n                        TLS_client_method,\n                        ssl_undefined_function,\n                        ossl_statem_connect, TLSv1_2_enc_data)', 'int test_true(const char *file, int line, const char *s, int b)\n{\n    if (b)\n        return 1;\n    test_fail_message(NULL, file, line, "bool", s, "true", "==", "false");\n    return 0;\n}', 'void SSL_SESSION_free(SSL_SESSION *ss)\n{\n    int i;\n    if (ss == NULL)\n        return;\n    CRYPTO_DOWN_REF(&ss->references, &i, ss->lock);\n    REF_PRINT_COUNT("SSL_SESSION", ss);\n    if (i > 0)\n        return;\n    REF_ASSERT_ISNT(i < 0);\n    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);\n    OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));\n    OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));\n    X509_free(ss->peer);\n    sk_X509_pop_free(ss->peer_chain, X509_free);\n    OPENSSL_free(ss->ext.hostname);\n    OPENSSL_free(ss->ext.tick);\n#ifndef OPENSSL_NO_PSK\n    OPENSSL_free(ss->psk_identity_hint);\n    OPENSSL_free(ss->psk_identity);\n#endif\n#ifndef OPENSSL_NO_SRP\n    OPENSSL_free(ss->srp_username);\n#endif\n    OPENSSL_free(ss->ext.alpn_selected);\n    OPENSSL_free(ss->ticket_appdata);\n    CRYPTO_THREAD_lock_free(ss->lock);\n    OPENSSL_clear_free(ss, sizeof(*ss));\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n    int i;\n    if (a == NULL)\n        return;\n    CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n    REF_PRINT_COUNT("SSL_CTX", a);\n    if (i > 0)\n        return;\n    REF_ASSERT_ISNT(i < 0);\n    X509_VERIFY_PARAM_free(a->param);\n    dane_ctx_final(&a->dane);\n    if (a->sessions != NULL)\n        SSL_CTX_flush_sessions(a, 0);\n    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n    lh_SSL_SESSION_free(a->sessions);\n    X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n    CTLOG_STORE_free(a->ctlog_store);\n#endif\n    sk_SSL_CIPHER_free(a->cipher_list);\n    sk_SSL_CIPHER_free(a->cipher_list_by_id);\n    sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n    ssl_cert_free(a->cert);\n    sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n    sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n    sk_X509_pop_free(a->extra_certs, X509_free);\n    a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n    sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n    SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n    ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n    OPENSSL_free(a->ext.ecpointformats);\n    OPENSSL_free(a->ext.supportedgroups);\n#endif\n    OPENSSL_free(a->ext.alpn);\n    OPENSSL_secure_free(a->ext.secure);\n    CRYPTO_THREAD_lock_free(a->lock);\n    OPENSSL_free(a);\n}', 'static inline int CRYPTO_DOWN_REF(_Atomic int *val, int *ret, void *lock)\n{\n    *ret = atomic_fetch_sub_explicit(val, 1, memory_order_relaxed) - 1;\n    if (*ret == 0)\n        atomic_thread_fence(memory_order_acquire);\n    return 1;\n}']