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