]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
First working empty protocol test
authorTomas Mraz <tomas@openssl.org>
Fri, 13 May 2022 14:45:07 +0000 (16:45 +0200)
committerPauli <pauli@openssl.org>
Fri, 3 Jun 2022 02:07:18 +0000 (12:07 +1000)
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/18307)

ssl/quic/quic_impl.c
ssl/quic/quic_local.h
test/build.info
test/quicapitest.c [new file with mode: 0644]
test/recipes/75-test_quicapi.t [new file with mode: 0644]

index bc10a4b61588ab2a3fc2b813edba2b514ad80e4b..1c673d23b66bbc4b22e863e08aff510b30cc9206 100644 (file)
@@ -11,7 +11,7 @@
 #include <openssl/objects.h>
 #include "quic_local.h"
 
-__owur int ossl_quic_new(SSL *s)
+int ossl_quic_new(SSL *s)
 {
     return s->method->ssl_clear(s);
 }
@@ -26,57 +26,89 @@ int ossl_quic_clear(SSL *s)
     return 1;
 }
 
-__owur int ossl_quic_accept(SSL *s)
+int ossl_quic_accept(SSL *s)
 {
     return 1;
 }
 
-__owur int ossl_quic_connect(SSL *s)
+int ossl_quic_connect(SSL *s)
 {
     return 1;
 }
 
-__owur int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *readbytes)
+int ossl_quic_read(SSL *s, void *buf, size_t len, size_t *readbytes)
 {
-    return 1;
+    BIO *rbio = SSL_get_rbio(s);
+
+    if (rbio == NULL)
+        return 0;
+
+    return BIO_read_ex(rbio, buf, len, readbytes);
 }
 
-__owur int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *readbytes)
+int ossl_quic_peek(SSL *s, void *buf, size_t len, size_t *readbytes)
 {
     return 1;
 }
 
-__owur int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
+int ossl_quic_write(SSL *s, const void *buf, size_t len, size_t *written)
 {
-    return 1;
+    BIO *wbio = SSL_get_wbio(s);
+
+    if (wbio == NULL)
+        return 0;
+
+    return BIO_write_ex(wbio, buf, len, written);
 }
 
-__owur int ossl_quic_shutdown(SSL *s)
+int ossl_quic_shutdown(SSL *s)
 {
     return 1;
 }
 
-__owur long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
+long ossl_quic_ctrl(SSL *s, int cmd, long larg, void *parg)
 {
     return 0;
 }
 
-__owur long ossl_quic_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg)
+long ossl_quic_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg)
 {
     return 0;
 }
 
-__owur long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
+long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
 {
     return 0;
 }
 
-__owur long ossl_quic_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp) (void))
+long ossl_quic_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp) (void))
 {
     return 0;
 }
 
-__owur size_t ossl_quic_pending(const SSL *s)
+size_t ossl_quic_pending(const SSL *s)
 {
     return 0;
 }
+
+long ossl_quic_default_timeout(void)
+{
+    return 0;
+}
+
+int ossl_quic_num_ciphers(void)
+{
+    return 1;
+}
+
+const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u)
+{
+    static const SSL_CIPHER ciph = { 0 };
+
+    return &ciph;
+}
+
+int ossl_quic_renegotiate_check(SSL *ssl, int initok)
+{
+    return 1;
+}
index ffb617184f673bcab2daaadc7ce50a92f26d45f9..3b738e541bc30a33407f637ea8f7b18dc342287b 100644 (file)
@@ -33,7 +33,7 @@ const SSL_METHOD *func_name(void)  \
                 ossl_quic_write, \
                 ossl_quic_shutdown, \
                 NULL /* renegotiate */, \
-                NULL /* renegotiate_check */, \
+                ossl_quic_renegotiate_check, \
                 NULL /* read_bytes */, \
                 NULL /* write_bytes */, \
                 NULL /* dispatch_alert */, \
@@ -42,9 +42,9 @@ const SSL_METHOD *func_name(void)  \
                 NULL /* get_cipher_by_char */, \
                 NULL /* put_cipher_by_char */, \
                 ossl_quic_pending, \
-                NULL /* num_ciphers */, \
-                NULL /* get_cipher */, \
-                NULL /* default_timeout */, \
+                ossl_quic_num_ciphers, \
+                ossl_quic_get_cipher, \
+                ossl_quic_default_timeout, \
                 &enc_data, \
                 ssl_undefined_void_function, \
                 ossl_quic_callback_ctrl, \
@@ -67,5 +67,9 @@ __owur long ossl_quic_ctx_ctrl(SSL_CTX *s, int cmd, long larg, void *parg);
 __owur long ossl_quic_callback_ctrl(SSL *s, int cmd, void (*fp) (void));
 __owur long ossl_quic_ctx_callback_ctrl(SSL_CTX *s, int cmd, void (*fp) (void));
 __owur size_t ossl_quic_pending(const SSL *s);
+__owur long ossl_quic_default_timeout(void);
+__owur int ossl_quic_num_ciphers(void);
+__owur const SSL_CIPHER *ossl_quic_get_cipher(unsigned int u);
+int ossl_quic_renegotiate_check(SSL *ssl, int initok);
 
 #endif
index fd7289053991d650a50663a47a27ed23bc8b2426..0f7420825e4b767dfee4cb2396806d5641f8a51b 100644 (file)
@@ -934,6 +934,14 @@ ENDIF
   INCLUDE[build_wincrypt_test]=../include
   DEPEND[build_wincrypt_test]=../libssl ../libcrypto
 
+  IF[{- !$disabled{'quic'} -}]
+    PROGRAMS{noinst}=quicapitest
+  ENDIF
+
+  SOURCE[quicapitest]=quicapitest.c helpers/ssltestlib.c
+  INCLUDE[quicapitest]=../include ../apps/include
+  DEPEND[quicapitest]=../libcrypto ../libssl libtestutil.a
+
 {-
    use File::Spec::Functions;
    use File::Basename;
diff --git a/test/quicapitest.c b/test/quicapitest.c
new file mode 100644 (file)
index 0000000..1b64776
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include <openssl/opensslconf.h>
+#include <openssl/quic.h>
+
+#include "helpers/ssltestlib.h"
+#include "testutil.h"
+#include "testutil/output.h"
+
+static OSSL_LIB_CTX *libctx = NULL;
+static OSSL_PROVIDER *defctxnull = NULL;
+
+static int is_fips = 0;
+
+/*
+ * Test that we read what we've written.
+ */
+static int test_quic_write_read(void)
+{
+    SSL_CTX *cctx = NULL, *sctx = NULL;
+    SSL *clientquic = NULL, *serverquic = NULL;
+    int j, ret = 0;
+    char buf[20];
+    static char *msg = "A test message";
+    size_t msglen = strlen(msg);
+    size_t numbytes = 0;
+
+    if (!TEST_true(create_ssl_ctx_pair(libctx, OSSL_QUIC_server_method(),
+                                       OSSL_QUIC_client_method(),
+                                       0,
+                                       0,
+                                       &sctx, &cctx, NULL, NULL))
+            || !TEST_true(create_ssl_objects(sctx, cctx, &serverquic, &clientquic,
+                                             NULL, NULL))
+            || !TEST_true(create_bare_ssl_connection(serverquic, clientquic,
+                                                     SSL_ERROR_NONE, 0)))
+        goto end;
+
+    for (j = 0; j < 2; j++) {
+        /* Check that sending and receiving app data is ok */
+        if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
+                || !TEST_true(SSL_read_ex(serverquic, buf, sizeof(buf),
+                                          &numbytes))
+                || !TEST_mem_eq(buf, numbytes, msg, msglen))
+            goto end;
+
+        if (!TEST_true(SSL_write_ex(serverquic, msg, msglen, &numbytes))
+                || !TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf),
+                                          &numbytes))
+                || !TEST_mem_eq(buf, numbytes, msg, msglen))
+            goto end;
+    }
+
+    ret = 1;
+
+ end:
+    SSL_free(serverquic);
+    SSL_free(clientquic);
+    SSL_CTX_free(sctx);
+    SSL_CTX_free(cctx);
+
+    return ret;
+}
+
+OPT_TEST_DECLARE_USAGE("provider config\n")
+
+int setup_tests(void)
+{
+    char *modulename;
+    char *configfile;
+
+    libctx = OSSL_LIB_CTX_new();
+    if (!TEST_ptr(libctx))
+        return 0;
+
+    defctxnull = OSSL_PROVIDER_load(NULL, "null");
+
+    /*
+     * Verify that the default and fips providers in the default libctx are not
+     * available
+     */
+    if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
+            || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
+        return 0;
+
+    if (!test_skip_common_options()) {
+        TEST_error("Error parsing test options\n");
+        return 0;
+    }
+
+    if (!TEST_ptr(modulename = test_get_argument(0))
+            || !TEST_ptr(configfile = test_get_argument(1)))
+        return 0;
+
+    if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
+        return 0;
+
+    /* Check we have the expected provider available */
+    if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
+        return 0;
+
+    /* Check the default provider is not available */
+    if (strcmp(modulename, "default") != 0
+            && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
+        return 0;
+
+    if (strcmp(modulename, "fips") == 0)
+        is_fips = 1;
+
+    ADD_TEST(test_quic_write_read);
+    return 1;
+}
+
+void cleanup_tests(void)
+{
+    OSSL_PROVIDER_unload(defctxnull);
+    OSSL_LIB_CTX_free(libctx);
+}
diff --git a/test/recipes/75-test_quicapi.t b/test/recipes/75-test_quicapi.t
new file mode 100644 (file)
index 0000000..6e96d45
--- /dev/null
@@ -0,0 +1,37 @@
+#! /usr/bin/env perl
+# Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+
+use OpenSSL::Test::Utils;
+use OpenSSL::Test qw/:DEFAULT srctop_file srctop_dir bldtop_dir/;
+
+BEGIN {
+setup("test_quicapi");
+}
+
+use lib srctop_dir('Configurations');
+use lib bldtop_dir('.');
+
+my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
+
+plan skip_all => "QUIC protocol is not supported by this OpenSSL build"
+    if disabled('quic');
+
+plan tests =>
+    ($no_fips ? 0 : 1)          # quicapitest with fips
+    + 1;                        # quicapitest with default provider
+
+ok(run(test(["quicapitest", "default",
+             srctop_file("test", "default.cnf")])),
+             "running quicapitest");
+
+unless ($no_fips) {
+    ok(run(test(["quicapitest", "fips",
+                 srctop_file("test", "fips-and-base.cnf")])),
+                 "running quicapitest");
+}