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