Update more tests
[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
23 static int test_empty(SSL *s)
24 {
25     STACK_OF(SSL_CIPHER) *sk = NULL, *scsv = NULL;
26     const unsigned char bytes[] = {0x00};
27
28     if (SSL_bytes_to_cipher_list(s, bytes, 0, 0, &sk, &scsv) ||
29         sk != NULL || scsv != NULL)
30         return 0;
31     return 1;
32 }
33
34 static int test_unsupported(SSL *s)
35 {
36     STACK_OF(SSL_CIPHER) *sk, *scsv;
37     /* ECDH-RSA-AES256 (unsupported), ECDHE-ECDSA-AES128, <unassigned> */
38     const unsigned char bytes[] = {0xc0, 0x0f, 0x00, 0x2f, 0x01, 0x00};
39
40     if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv) ||
41         sk == NULL || sk_SSL_CIPHER_num(sk) != 1 || scsv == NULL ||
42         sk_SSL_CIPHER_num(scsv) != 0)
43         return 0;
44     if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
45                 "AES128-SHA") != 0)
46         return 0;
47
48     sk_SSL_CIPHER_free(sk);
49     sk_SSL_CIPHER_free(scsv);
50     return 1;
51 }
52
53 static int test_v2(SSL *s)
54 {
55     STACK_OF(SSL_CIPHER) *sk, *scsv;
56     /* ECDHE-ECDSA-AES256GCM, SSL2_RC4_1238_WITH_MD5,
57      * ECDHE-ECDSA-CHACHA20-POLY1305 */
58     const unsigned char bytes[] = {0x00, 0x00, 0x35, 0x01, 0x00, 0x80,
59                                    0x00, 0x00, 0x33};
60
61     if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 1, &sk, &scsv) ||
62         sk == NULL || sk_SSL_CIPHER_num(sk) != 2 || scsv == NULL ||
63         sk_SSL_CIPHER_num(scsv) != 0)
64         return 0;
65     if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
66                "AES256-SHA") != 0 ||
67         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
68                "DHE-RSA-AES128-SHA") != 0)
69         return 0;
70     sk_SSL_CIPHER_free(sk);
71     sk_SSL_CIPHER_free(scsv);
72     return 1;
73 }
74
75 static int test_v3(SSL *s)
76 {
77     STACK_OF(SSL_CIPHER) *sk, *scsv;
78     /* ECDHE-ECDSA-AES256GCM, ECDHE-ECDSA-CHACHAPOLY, DHE-RSA-AES256GCM,
79      * EMPTY-RENEGOTIATION-INFO-SCSV, FALLBACK-SCSV */
80     const unsigned char bytes[] = {0x00, 0x2f, 0x00, 0x33, 0x00, 0x9f, 0x00, 0xff,
81                                    0x56, 0x00};
82
83     if (!SSL_bytes_to_cipher_list(s, bytes, sizeof(bytes), 0, &sk, &scsv) ||
84         sk == NULL || sk_SSL_CIPHER_num(sk) != 3 || scsv == NULL ||
85         sk_SSL_CIPHER_num(scsv) != 2)
86         return 0;
87     if (strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 0)),
88                "AES128-SHA") != 0 ||
89         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 1)),
90                "DHE-RSA-AES128-SHA") != 0 ||
91         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(sk, 2)),
92                "DHE-RSA-AES256-GCM-SHA384") != 0 ||
93         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 0)),
94                "TLS_EMPTY_RENEGOTIATION_INFO_SCSV") != 0 ||
95         strcmp(SSL_CIPHER_get_name(sk_SSL_CIPHER_value(scsv, 1)),
96                "TLS_FALLBACK_SCSV") != 0)
97         return 0;
98     sk_SSL_CIPHER_free(sk);
99     sk_SSL_CIPHER_free(scsv);
100     return 1;
101 }
102
103 int main(int argc, char **argv)
104 {
105     SSL_CTX *ctx;
106     SSL *s;
107
108     ctx = SSL_CTX_new(TLS_server_method());
109     s = SSL_new(ctx);
110     OPENSSL_assert(s != NULL);
111
112     if (!test_empty(s))
113         return 1;
114
115     if (!test_unsupported(s))
116         return 1;
117
118     if (!test_v2(s))
119         return 1;
120
121     if (!test_v3(s))
122         return 1;
123
124     printf("PASS\n");
125     SSL_free(s);
126     SSL_CTX_free(ctx);
127     return 0;
128 }