static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
                          OPENSSL_LH_DOALL_FUNC func,
                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
    int i;
    OPENSSL_LH_NODE *a, *n;
    if (lh == NULL)
        return;
    for (i = lh->num_nodes - 1; i >= 0; i--) {
        a = lh->b[i];
        while (a != NULL) {
            n = a->next;
            if (use_arg)
                func_arg(a->data, arg);
            else
                func(a->data);
            a = n;
        }
    }
}
['static int test_early_data_psk(int idx)\n{\n    SSL_CTX *cctx = NULL, *sctx = NULL;\n    SSL *clientssl = NULL, *serverssl = NULL;\n    int testresult = 0;\n    SSL_SESSION *sess = NULL;\n    unsigned char alpnlist[] = {\n        0x08, \'g\', \'o\', \'o\', \'d\', \'a\', \'l\', \'p\', \'n\', 0x07, \'b\', \'a\', \'d\', \'a\',\n        \'l\', \'p\', \'n\'\n    };\n#define GOODALPNLEN     9\n#define BADALPNLEN      8\n#define GOODALPN        (alpnlist)\n#define BADALPN         (alpnlist + GOODALPNLEN)\n    int err = 0;\n    unsigned char buf[20];\n    size_t readbytes, written;\n    int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;\n    int edstatus = SSL_EARLY_DATA_ACCEPTED;\n    if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,\n                                        &serverssl, &sess, 2)))\n        goto end;\n    servalpn = "goodalpn";\n    switch (idx) {\n    case 0:\n        err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;\n        if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))\n                || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))\n            goto end;\n        break;\n    case 1:\n        err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;\n        if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,\n                                                      GOODALPNLEN))\n                || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,\n                                                   BADALPNLEN)))\n            goto end;\n        break;\n    case 2:\n        err = SSL_R_BAD_PSK;\n        if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))\n            goto end;\n        break;\n    case 3:\n        SSL_SESSION_free(serverpsk);\n        serverpsk = SSL_SESSION_dup(clientpsk);\n        if (!TEST_ptr(serverpsk)\n                || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))\n            goto end;\n        edstatus = SSL_EARLY_DATA_REJECTED;\n        readearlyres = SSL_READ_EARLY_DATA_FINISH;\n    case 4:\n        if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))\n                || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))\n                || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,\n                                hostname_cb)))\n            goto end;\n        break;\n    case 5:\n        servalpn = "badalpn";\n        edstatus = SSL_EARLY_DATA_REJECTED;\n        readearlyres = SSL_READ_EARLY_DATA_FINISH;\n    case 6:\n        if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,\n                                                      GOODALPNLEN - 1))\n                || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,\n                                                   GOODALPNLEN)))\n            goto end;\n        SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);\n        break;\n    case 7:\n        SSL_SESSION_free(serverpsk);\n        serverpsk = SSL_SESSION_dup(clientpsk);\n        if (!TEST_ptr(serverpsk)\n                || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,\n                                                             BADALPN + 1,\n                                                             BADALPNLEN - 1))\n                || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,\n                                                             GOODALPN + 1,\n                                                             GOODALPNLEN - 1))\n                || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,\n                                                   sizeof(alpnlist))))\n            goto end;\n        SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);\n        edstatus = SSL_EARLY_DATA_ACCEPTED;\n        readearlyres = SSL_READ_EARLY_DATA_SUCCESS;\n        connectres = -1;\n        break;\n    default:\n        TEST_error("Bad test index");\n        goto end;\n    }\n    SSL_set_connect_state(clientssl);\n    if (err != 0) {\n        if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),\n                                            &written))\n                || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)\n                || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))\n            goto end;\n    } else {\n        if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),\n                                            &written)))\n            goto end;\n        if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n                                             &readbytes), readearlyres)\n                || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS\n                    && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))\n                || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)\n                || !TEST_int_eq(SSL_connect(clientssl), connectres))\n            goto end;\n    }\n    testresult = 1;\n end:\n    SSL_SESSION_free(clientpsk);\n    SSL_SESSION_free(serverpsk);\n    clientpsk = serverpsk = NULL;\n    SSL_free(serverssl);\n    SSL_free(clientssl);\n    SSL_CTX_free(sctx);\n    SSL_CTX_free(cctx);\n    return testresult;\n}', 'static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,\n                                SSL **serverssl, SSL_SESSION **sess, int idx)\n{\n    if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),\n                                       TLS_client_method(), sctx,\n                                       cctx, cert, privkey)))\n        return 0;\n    if (idx == 1) {\n        SSL_CTX_set_read_ahead(*cctx, 1);\n        SSL_CTX_set_read_ahead(*sctx, 1);\n    } else if (idx == 2) {\n        SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);\n        SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);\n        use_session_cb_cnt = 0;\n        find_session_cb_cnt = 0;\n        srvid = pskid;\n    }\n    if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,\n                                      NULL, NULL)))\n        return 0;\n    if (idx == 1\n            && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))\n        return 0;\n    if (idx == 2) {\n        const SSL_CIPHER *cipher = NULL;\n        const unsigned char key[] = {\n            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,\n            0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,\n            0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,\n            0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,\n            0x2c, 0x2d, 0x2e, 0x2f\n        };\n        cipher = SSL_CIPHER_find(*clientssl, TLS13_AES_256_GCM_SHA384_BYTES);\n        clientpsk = SSL_SESSION_new();\n        if (!TEST_ptr(clientpsk)\n                || !TEST_ptr(cipher)\n                || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,\n                                                          sizeof(key)))\n                || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))\n                || !TEST_true(\n                        SSL_SESSION_set_protocol_version(clientpsk,\n                                                         TLS1_3_VERSION))\n                || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,\n                                                             0x100))\n                || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {\n            SSL_SESSION_free(clientpsk);\n            clientpsk = NULL;\n            return 0;\n        }\n        serverpsk = clientpsk;\n        if (sess != NULL)\n            *sess = clientpsk;\n        return 1;\n    }\n    if (sess == NULL)\n        return 1;\n    if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,\n                                         SSL_ERROR_NONE)))\n        return 0;\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 (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,\n                                      clientssl, NULL, NULL))\n            || !TEST_true(SSL_set_session(*clientssl, *sess)))\n        return 0;\n    return 1;\n}', 'void SSL_free(SSL *s)\n{\n    int i;\n    if (s == NULL)\n        return;\n    CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n    REF_PRINT_COUNT("SSL", s);\n    if (i > 0)\n        return;\n    REF_ASSERT_ISNT(i < 0);\n    X509_VERIFY_PARAM_free(s->param);\n    dane_final(&s->dane);\n    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n    ssl_free_wbio_buffer(s);\n    BIO_free_all(s->wbio);\n    BIO_free_all(s->rbio);\n    BUF_MEM_free(s->init_buf);\n    sk_SSL_CIPHER_free(s->cipher_list);\n    sk_SSL_CIPHER_free(s->cipher_list_by_id);\n    if (s->session != NULL) {\n        ssl_clear_bad_session(s);\n        SSL_SESSION_free(s->session);\n    }\n    SSL_SESSION_free(s->psksession);\n    OPENSSL_free(s->psksession_id);\n    clear_ciphers(s);\n    ssl_cert_free(s->cert);\n    OPENSSL_free(s->ext.hostname);\n    SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n    OPENSSL_free(s->ext.ecpointformats);\n    OPENSSL_free(s->ext.supportedgroups);\n#endif\n    sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n    SCT_LIST_free(s->scts);\n    OPENSSL_free(s->ext.scts);\n#endif\n    OPENSSL_free(s->ext.ocsp.resp);\n    OPENSSL_free(s->ext.alpn);\n    OPENSSL_free(s->ext.tls13_cookie);\n    OPENSSL_free(s->clienthello);\n    OPENSSL_free(s->pha_context);\n    EVP_MD_CTX_free(s->pha_dgst);\n    sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n    sk_X509_pop_free(s->verified_chain, X509_free);\n    if (s->method != NULL)\n        s->method->ssl_free(s);\n    RECORD_LAYER_release(&s->rlayer);\n    SSL_CTX_free(s->ctx);\n    ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n    OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n    sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n    RAND_DRBG_free(s->drbg);\n    CRYPTO_THREAD_lock_free(s->lock);\n    OPENSSL_free(s);\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    ssl_cert_free(a->cert);\n    sk_X509_NAME_pop_free(a->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    CRYPTO_THREAD_lock_free(a->lock);\n    OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n    unsigned long i;\n    TIMEOUT_PARAM tp;\n    tp.ctx = s;\n    tp.cache = s->sessions;\n    if (tp.cache == NULL)\n        return;\n    tp.time = t;\n    CRYPTO_THREAD_write_lock(s->lock);\n    i = lh_SSL_SESSION_get_down_load(s->sessions);\n    lh_SSL_SESSION_set_down_load(s->sessions, 0);\n    lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n    lh_SSL_SESSION_set_down_load(s->sessions, i);\n    CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n                          OPENSSL_LH_DOALL_FUNC func,\n                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n    int i;\n    OPENSSL_LH_NODE *a, *n;\n    if (lh == NULL)\n        return;\n    for (i = lh->num_nodes - 1; i >= 0; i--) {\n        a = lh->b[i];\n        while (a != NULL) {\n            n = a->next;\n            if (use_arg)\n                func_arg(a->data, arg);\n            else\n                func(a->data);\n            a = n;\n        }\n    }\n}']