]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/cipherbytes_test.c
Update copyright year
[thirdparty/openssl.git] / test / cipherbytes_test.c
1 /*
2 * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "internal/nelem.h"
22 #include "testutil.h"
23
24 DEFINE_STACK_OF(SSL_CIPHER)
25
26 static SSL_CTX *ctx;
27 static SSL *s;
28
29 static int test_empty(void)
30 {
31 STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
32 const unsigned char bytes[] = {0x00};
33 int ret = 0;
34
35 if (!TEST_int_eq(SSL_bytes_to_cipher_list(s, bytes, 0, 0, &sk, &scsv), 0)
36 || !TEST_ptr_null(sk)
37 || !TEST_ptr_null(scsv))
38 goto err;
39 ret = 1;
40
41 err:
42 sk_SSL_CIPHER_free(sk);
43 sk_SSL_CIPHER_free(scsv);
44 return ret;
45 }
46
47 static int test_unsupported(void)
48 {
49 STACK_OF(SSL_CIPHER) *sk, *scsv;
50 /* ECDH-RSA-AES256 (unsupported), ECDHE-ECDSA-AES128, <unassigned> */
51 const unsigned char bytes[] = {0xc0, 0x0f, 0x00, 0x2f, 0x01, 0x00};
52 int ret = 0;
53
54 if (!TEST_true(SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes),
55 0, &sk, &scsv))
56 || !TEST_ptr(sk)
57 || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 1)
58 || !TEST_ptr(scsv)
59 || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 0)
60 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
61 "AES128-SHA"))
62 goto err;
63
64 ret = 1;
65 err:
66 sk_SSL_CIPHER_free(sk);
67 sk_SSL_CIPHER_free(scsv);
68 return ret;
69 }
70
71 static int test_v2(void)
72 {
73 STACK_OF(SSL_CIPHER) *sk, *scsv;
74 /* ECDHE-ECDSA-AES256GCM, SSL2_RC4_1238_WITH_MD5,
75 * ECDHE-ECDSA-CHACHA20-POLY1305 */
76 const unsigned char bytes[] = {0x00, 0x00, 0x35, 0x01, 0x00, 0x80,
77 0x00, 0x00, 0x33};
78 int ret = 0;
79
80 if (!TEST_true(SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 1,
81 &sk, &scsv))
82 || !TEST_ptr(sk)
83 || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 2)
84 || !TEST_ptr(scsv)
85 || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 0))
86 goto err;
87 if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
88 "AES256-SHA") != 0 ||
89 strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
90 "DHE-RSA-AES128-SHA") != 0)
91 goto err;
92
93 ret = 1;
94
95 err:
96 sk_SSL_CIPHER_free(sk);
97 sk_SSL_CIPHER_free(scsv);
98 return ret;
99 }
100
101 static int test_v3(void)
102 {
103 STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
104 /* ECDHE-ECDSA-AES256GCM, ECDHE-ECDSA-CHACHAPOLY, DHE-RSA-AES256GCM,
105 * EMPTY-RENEGOTIATION-INFO-SCSV, FALLBACK-SCSV */
106 const unsigned char bytes[] = {0x00, 0x2f, 0x00, 0x33, 0x00, 0x9f, 0x00, 0xff,
107 0x56, 0x00};
108 int ret = 0;
109
110 if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv)
111 || !TEST_ptr(sk)
112 || !TEST_int_eq(sk_SSL_CIPHER_num(sk), 3)
113 || !TEST_ptr(scsv)
114 || !TEST_int_eq(sk_SSL_CIPHER_num(scsv), 2)
115 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
116 "AES128-SHA")
117 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
118 "DHE-RSA-AES128-SHA")
119 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 2)),
120 "DHE-RSA-AES256-GCM-SHA384")
121 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 0)),
122 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV")
123 || !TEST_str_eq(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 1)),
124 "TLS_FALLBACK_SCSV"))
125 goto err;
126
127 ret = 1;
128 err:
129 sk_SSL_CIPHER_free(sk);
130 sk_SSL_CIPHER_free(scsv);
131 return ret;
132 }
133
134 int setup_tests(void)
135 {
136 if (!TEST_ptr(ctx = SSL_CTX_new(TLS_server_method()))
137 || !TEST_ptr(s = SSL_new(ctx)))
138 return 0;
139
140 ADD_TEST(test_empty);
141 ADD_TEST(test_unsupported);
142 ADD_TEST(test_v2);
143 ADD_TEST(test_v3);
144 return 1;
145 }
146
147 void cleanup_tests(void)
148 {
149 SSL_free(s);
150 SSL_CTX_free(ctx);
151 }