Skip to content
sslapitest.c 67.5 KiB
Newer Older
Matt Caswell's avatar
Matt Caswell committed

    if (!SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
            || written != strlen(MSG2)) {
        printf("Failed writing message 2\n");
        goto end;
    }

    /*
     * Should block due to the NewSessionTicket arrival unless we're using
     * read_ahead
     */
    if (idx == 0) {
        if (SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)) {
            printf("Unexpected success reading message 2\n");
            goto end;
        }
Matt Caswell's avatar
Matt Caswell committed
    }

    if (!SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
            || readbytes != strlen(MSG2)
            || memcmp(MSG2, buf, strlen(MSG2))) {
        printf("Failed reading message 2\n");
        goto end;
    }

    testresult = 1;

 end:
    if(!testresult)
        ERR_print_errors_fp(stdout);
    SSL_SESSION_free(sess);
    SSL_free(serverssl);
    SSL_free(clientssl);
    SSL_CTX_free(sctx);
    SSL_CTX_free(cctx);

    return testresult;
}

static int test_early_data_not_expected(int idx)
Matt Caswell's avatar
Matt Caswell committed
{
    SSL_CTX *cctx = NULL, *sctx = NULL;
    SSL *clientssl = NULL, *serverssl = NULL;
    int testresult = 0;
    SSL_SESSION *sess;
    unsigned char buf[20];
    size_t readbytes, written;

    /*
     * Test that a server that doesn't try to read early data can handle a
     * client sending some.
     */

    if (!setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl, &sess, idx))
Matt Caswell's avatar
Matt Caswell committed
        goto end;

    /* Write some early data */
    if (!SSL_write_early_data(clientssl, MSG1, strlen(MSG1), &written)) {
Matt Caswell's avatar
Matt Caswell committed
        printf("Unexpected failure writing message 1\n");
        goto end;
    }

    /*
     * Server should skip over early data and then block waiting for client to
     * continue handshake
     */
    if (SSL_accept(serverssl) > 0) {
        printf("Unexpected success setting up server connection\n");
        goto end;
    }

    if (SSL_connect(clientssl) <= 0) {
        printf("Failed setting up client connection\n");
        goto end;
    }

    if (SSL_get_early_data_status(serverssl) != SSL_EARLY_DATA_REJECTED) {
        printf("Unexpected early data status\n");
        goto end;
    }

    if (SSL_accept(serverssl) <= 0) {
        printf("Failed setting up server connection\n");
        goto end;
    }

    if (SSL_get_early_data_status(clientssl) != SSL_EARLY_DATA_REJECTED) {
        printf("Unexpected early data status (2)\n");
        goto end;
    }

    /* Send some normal data from client to server */
    if (!SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written)
            || written != strlen(MSG2)) {
        printf("Failed writing message 2\n");
        goto end;
    }

    if (!SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)
            || readbytes != strlen(MSG2)
            || memcmp(MSG2, buf, strlen(MSG2))) {
        printf("Failed reading message 2\n");
        goto end;
    }

    testresult = 1;

 end:
    if(!testresult)
        ERR_print_errors_fp(stdout);
    SSL_SESSION_free(sess);
    SSL_free(serverssl);
    SSL_free(clientssl);
    SSL_CTX_free(sctx);
    SSL_CTX_free(cctx);

    return testresult;
}


# ifndef OPENSSL_NO_TLS1_2
static int test_early_data_tls1_2(int idx)
Matt Caswell's avatar
Matt Caswell committed
{
    SSL_CTX *cctx = NULL, *sctx = NULL;
    SSL *clientssl = NULL, *serverssl = NULL;
    int testresult = 0;
    unsigned char buf[20];
    size_t readbytes, written;

    /*
     * Test that a server attempting to read early data can handle a connection
     * from a TLSv1.2 client.
     */

    if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
                             &cctx, cert, privkey)) {
        printf("Unable to create SSL_CTX pair\n");
        goto end;
    }

    /* When idx == 1 we repeat the tests with read_ahead set */
    if (idx > 0) {
        SSL_CTX_set_read_ahead(cctx, 1);
        SSL_CTX_set_read_ahead(sctx, 1);
    }

Matt Caswell's avatar
Matt Caswell committed
    if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
        printf("Unable to create SSL objects\n");
        goto end;
    }

    /* Write some data - should block due to handshake with server */
    SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
    SSL_set_connect_state(clientssl);
    if (SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) {
        printf("Unexpected success writing message 1\n");
        goto end;
    }

    /*
     * Server should do TLSv1.2 handshake. First it will block waiting for more
     * messages from client after ServerDone. Then SSL_read_early_data should
     * finish and detect that early data has not been sent
Matt Caswell's avatar
Matt Caswell committed
     */
    if (SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes)
                != SSL_READ_EARLY_DATA_ERROR) {
Matt Caswell's avatar
Matt Caswell committed
        printf("Unexpected success reading early data\n");
        goto end;
    }

    /*
     * Continue writing the message we started earlier. Will still block waiting
     * for the CCS/Finished from server
     */
    if (SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)) {
        printf("Unexpected success writing message 1\n");
        goto end;
    }

    if (SSL_read_early_data(serverssl, buf, sizeof(buf), &readbytes)
                != SSL_READ_EARLY_DATA_FINISH
Matt Caswell's avatar
Matt Caswell committed
            || readbytes != 0) {
        printf("Failed reading early data\n");
        goto end;
    }

    if (SSL_get_early_data_status(serverssl) != SSL_EARLY_DATA_NOT_SENT) {
        printf("Unexpected early data status\n");
        goto end;
    }

    /* Continue writing the message we started earlier */
    if (!SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)
            || written != strlen(MSG1)) {
        printf("Failed writing message 1\n");
        goto end;
    }

    if (SSL_get_early_data_status(clientssl) != SSL_EARLY_DATA_NOT_SENT) {
        printf("Unexpected early data status (2)\n");
        goto end;
    }

    if (!SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)
            || readbytes != strlen(MSG1)
            || memcmp(MSG1, buf, strlen(MSG1))) {
        printf("Failed reading message 1\n");
        goto end;
    }

    if (!SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
            || written != strlen(MSG2)) {
        printf("Failed writing message 2\n");
        goto end;
    }

    if (!SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
            || readbytes != strlen(MSG2)
            || memcmp(MSG2, buf, strlen(MSG2))) {
        printf("Failed reading message 2\n");
        goto end;
    }

    testresult = 1;

 end:
    if(!testresult)
        ERR_print_errors_fp(stdout);
    SSL_free(serverssl);
    SSL_free(clientssl);
    SSL_CTX_free(sctx);
    SSL_CTX_free(cctx);

    return testresult;
}
# endif
#endif

int test_main(int argc, char *argv[])
    int testresult = 1;
Matt Caswell's avatar
Matt Caswell committed

    if (argc != 3) {
        printf("Invalid argument count\n");
Matt Caswell's avatar
Matt Caswell committed
    }

    cert = argv[1];
    privkey = argv[2];

    ADD_TEST(test_large_message_tls);
Matt Caswell's avatar
Matt Caswell committed
    ADD_TEST(test_large_message_tls_read_ahead);
Matt Caswell's avatar
Matt Caswell committed
#ifndef OPENSSL_NO_DTLS
    ADD_TEST(test_large_message_dtls);
Matt Caswell's avatar
Matt Caswell committed
#endif
#ifndef OPENSSL_NO_OCSP
    ADD_TEST(test_tlsext_status_type);
Matt Caswell's avatar
Matt Caswell committed
    ADD_TEST(test_session_with_only_int_cache);
    ADD_TEST(test_session_with_only_ext_cache);
    ADD_TEST(test_session_with_both_cache);
    ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
Matt Caswell's avatar
Matt Caswell committed
    ADD_TEST(test_ssl_bio_pop_next_bio);
    ADD_TEST(test_ssl_bio_pop_ssl_bio);
    ADD_TEST(test_ssl_bio_change_rbio);
    ADD_TEST(test_ssl_bio_change_wbio);
    ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
    ADD_TEST(test_keylog);
#ifndef OPENSSL_NO_TLS1_3
    ADD_TEST(test_keylog_no_master_key);
#endif
#ifndef OPENSSL_NO_TLS1_2
    ADD_TEST(test_early_cb);
Matt Caswell's avatar
Matt Caswell committed
#endif
#ifndef OPENSSL_NO_TLS1_3
    ADD_ALL_TESTS(test_early_data_read_write, 2);
    ADD_ALL_TESTS(test_early_data_skip, 2);
    ADD_ALL_TESTS(test_early_data_not_sent, 2);
    ADD_ALL_TESTS(test_early_data_not_expected, 2);
Matt Caswell's avatar
Matt Caswell committed
# ifndef OPENSSL_NO_TLS1_2
    ADD_ALL_TESTS(test_early_data_tls1_2, 2);
Matt Caswell's avatar
Matt Caswell committed
# endif
    testresult = run_tests(argv[0]);
    bio_s_mempacket_test_free();

    return testresult;