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