Convert dhtest, dsatest, cipherbytes_test
[openssl.git] / test / cipherbytes_test.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL licenses, (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * https://www.openssl.org/source/license.html
8  * or in the file LICENSE in the source distribution.
9  */
10
11 #include <string.h>
12 #include <stdio.h>
13
14 #include <openssl/opensslconf.h>
15 #include <openssl/err.h>
16 #include <openssl/e_os2.h>
17 #include <openssl/ssl.h>
18 #include <openssl/ssl3.h>
19 #include <openssl/tls1.h>
20
21 #include "e_os.h"
22 #include "testutil.h"
23 #include "test_main_custom.h"
24
25 static SSL_CTX *ctx;
26 static SSL *s;
27
28 static int test_empty(void)
29 {
30     STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
31     const unsigned char bytes[] = {0x00};
32     int ret = 0;
33
34     if (!TEST_int_eq(SSL_bytes_to_cipher_list(s, bytes, 0, 0, &sk, &scsv), 0)
35             || !TEST_ptr_null(sk)
36             || !TEST_ptr_null(scsv))
37         goto err;
38     ret = 1;
39
40 err:
41     sk_SSL_CIPHER_free(sk);
42     sk_SSL_CIPHER_free(scsv);
43     return ret;
44 }
45
46 static int test_unsupported(void)
47 {
48     STACK_OF(SSL_CIPHER) *sk, *scsv;
49     /* ECDH-RSA-AES256 (unsupported), ECDHE-ECDSA-AES128, <unassigned> */
50     const unsigned char bytes[] = {0xc0, 0x0f, 0x00, 0x2f, 0x01, 0x00};
51     int ret = 0;
52
53     if (!TEST_true(SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes),
54                                             0, &sk, &scsv))
55             || !TEST_ptr(sk)
56             || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 1)
57             || !TEST_ptr(scsv)
58             || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 0)
59             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
60                             "AES128-SHA"))
61         goto err;
62
63     ret = 1;
64 err:
65     sk_SSL_CIPHER_free(sk);
66     sk_SSL_CIPHER_free(scsv);
67     return ret;
68 }
69
70 static int test_v2(void)
71 {
72     STACK_OF(SSL_CIPHER) *sk, *scsv;
73     /* ECDHE-ECDSA-AES256GCM, SSL2_RC4_1238_WITH_MD5,
74      * ECDHE-ECDSA-CHACHA20-POLY1305 */
75     const unsigned char bytes[] = {0x00, 0x00, 0x35, 0x01, 0x00, 0x80,
76                                    0x00, 0x00, 0x33};
77     int ret = 0;
78
79     if (!TEST_true(SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 1,
80                                             &sk, &scsv))
81             || !TEST_ptr(sk)
82             || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 2)
83             || !TEST_ptr(scsv)
84             || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 0))
85         goto err;
86     if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
87                "AES256-SHA") != 0 ||
88         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
89                "DHE-RSA-AES128-SHA") != 0)
90         goto err;
91
92     ret = 1;
93
94 err:
95     sk_SSL_CIPHER_free(sk);
96     sk_SSL_CIPHER_free(scsv);
97     return ret;
98 }
99
100 static int test_v3(void)
101 {
102     STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
103     /* ECDHE-ECDSA-AES256GCM, ECDHE-ECDSA-CHACHAPOLY, DHE-RSA-AES256GCM,
104      * EMPTY-RENEGOTIATION-INFO-SCSV, FALLBACK-SCSV */
105     const unsigned char bytes[] = {0x00, 0x2f, 0x00, 0x33, 0x00, 0x9f, 0x00, 0xff,
106                                    0x56, 0x00};
107     int ret = 0;
108
109     if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv)
110             || !TEST_ptr(sk)
111             || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 3)
112             || !TEST_ptr(scsv)
113             || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 2)
114             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
115                             "AES128-SHA")
116             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
117                             "DHE-RSA-AES128-SHA")
118             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 2)),
119                             "DHE-RSA-AES256-GCM-SHA384")
120             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 0)),
121                             "TLS_EMPTY_RENEGOTIATION_INFO_SCSV")
122             || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 1)),
123                             "TLS_FALLBACK_SCSV"))
124         goto err;
125
126     ret = 1;
127 err:
128     sk_SSL_CIPHER_free(sk);
129     sk_SSL_CIPHER_free(scsv);
130     return ret;
131 }
132
133 int test_main(int argc, char **argv)
134 {
135     int ret;
136
137     if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method()))
138             || !TEST_ptr(s = SSL_new(ctx)))
139         return EXIT_FAILURE;
140
141     ADD_TEST(test_empty);
142     ADD_TEST(test_unsupported);
143     ADD_TEST(test_v2);
144     ADD_TEST(test_v3);
145     ret = run_tests(argv[0]);
146
147     SSL_free(s);
148     SSL_CTX_free(ctx);
149
150     return ret;
151 }