xref: /openbsd-src/regress/lib/libssl/ciphers/cipherstest.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*
2  * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <openssl/ssl.h>
18 
19 #include <err.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 static int
24 get_put_test(const char *name, const SSL_METHOD *method)
25 {
26 	STACK_OF(SSL_CIPHER) *ciphers;
27 	const SSL_CIPHER *cipher;
28 	unsigned char buf[2];
29 	SSL_CTX *ssl_ctx = NULL;
30 	SSL *ssl = NULL;
31 	int ret = 1;
32 	int i, len;
33 
34 	if ((len = method->put_cipher_by_char(NULL, NULL)) != 2) {
35 		fprintf(stderr,
36 		    "%s: put_cipher_by_char() returned len %i (want 2)\n",
37 		    name, len);
38 		return (1);
39 	}
40 
41 	if ((ssl_ctx = SSL_CTX_new(method)) == NULL) {
42 		fprintf(stderr, "%s: SSL_CTX_new() returned NULL\n", name);
43 		goto failure;
44 	}
45 	if ((ssl = SSL_new(ssl_ctx)) == NULL) {
46 		fprintf(stderr, "%s: SSL_new() returned NULL\n", name);
47 		goto failure;
48 	}
49 
50 	if ((ciphers = SSL_get_ciphers(ssl)) == NULL) {
51 		fprintf(stderr, "%s: no ciphers\n", name);
52 		goto failure;
53 	}
54 
55 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
56 		cipher = sk_SSL_CIPHER_value(ciphers, i);
57 		if ((len = method->put_cipher_by_char(cipher, buf)) != 2) {
58 			fprintf(stderr,
59 			    "%s: put_cipher_by_char() returned len %i for %s "
60 			    "(want 2)\n",
61 			    name, len, SSL_CIPHER_get_name(cipher));
62 			goto failure;
63 		}
64 		if ((cipher = method->get_cipher_by_char(buf)) == NULL) {
65 			fprintf(stderr,
66 			    "%s: get_cipher_by_char() returned NULL for %s\n",
67 			    name, SSL_CIPHER_get_name(cipher));
68 			goto failure;
69 		}
70 	}
71 
72 	ret = 0;
73 
74 failure:
75 	SSL_CTX_free(ssl_ctx);
76 	SSL_free(ssl);
77 
78 	return (ret);
79 }
80 
81 static int
82 cipher_get_put_tests(void)
83 {
84 	int failed = 0;
85 
86 	failed |= get_put_test("SSLv23", SSLv23_method());
87 	failed |= get_put_test("SSLv23_client", SSLv23_client_method());
88 	failed |= get_put_test("SSLv23_server", SSLv23_server_method());
89 
90 	failed |= get_put_test("TLSv1", TLSv1_method());
91 	failed |= get_put_test("TLSv1_client", TLSv1_client_method());
92 	failed |= get_put_test("TLSv1_server", TLSv1_server_method());
93 
94 	failed |= get_put_test("TLSv1_1", TLSv1_1_method());
95 	failed |= get_put_test("TLSv1_1_client", TLSv1_1_client_method());
96 	failed |= get_put_test("TLSv1_1_server", TLSv1_1_server_method());
97 
98 	failed |= get_put_test("TLSv1_2", TLSv1_2_method());
99 	failed |= get_put_test("TLSv1_2_client", TLSv1_2_client_method());
100 	failed |= get_put_test("TLSv1_2_server", TLSv1_2_server_method());
101 
102 	failed |= get_put_test("DTLSv1", DTLSv1_method());
103 	failed |= get_put_test("DTLSv1_client", DTLSv1_client_method());
104 	failed |= get_put_test("DTLSv1_server", DTLSv1_server_method());
105 
106 	return failed;
107 }
108 
109 static int
110 cipher_get_by_value_tests(void)
111 {
112 	STACK_OF(SSL_CIPHER) *ciphers;
113 	const SSL_CIPHER *cipher;
114 	SSL_CTX *ssl_ctx = NULL;
115 	SSL *ssl = NULL;
116 	unsigned long id;
117 	uint16_t value;
118 	int ret = 1;
119 	int i;
120 
121 	if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
122 		fprintf(stderr, "SSL_CTX_new() returned NULL\n");
123 		goto failure;
124 	}
125 	if ((ssl = SSL_new(ssl_ctx)) == NULL) {
126 		fprintf(stderr, "SSL_new() returned NULL\n");
127 		goto failure;
128 	}
129 
130 	if ((ciphers = SSL_get_ciphers(ssl)) == NULL) {
131 		fprintf(stderr, "no ciphers\n");
132 		goto failure;
133 	}
134 
135 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
136 		cipher = sk_SSL_CIPHER_value(ciphers, i);
137 
138 		id = SSL_CIPHER_get_id(cipher);
139 		if (SSL_CIPHER_get_by_id(id) == NULL) {
140 			fprintf(stderr, "SSL_CIPHER_get_by_id() failed "
141 			    "for %s (0x%lx)\n", SSL_CIPHER_get_name(cipher),
142 			    id);
143 			goto failure;
144 		}
145 
146 		value = SSL_CIPHER_get_value(cipher);
147 		if (SSL_CIPHER_get_by_value(value) == NULL) {
148 			fprintf(stderr, "SSL_CIPHER_get_by_value() failed "
149 			    "for %s (0x%04hx)\n", SSL_CIPHER_get_name(cipher),
150 			    value);
151 			goto failure;
152 		}
153 	}
154 
155 	ret = 0;
156 
157 failure:
158 	SSL_CTX_free(ssl_ctx);
159 	SSL_free(ssl);
160 
161 	return (ret);
162 }
163 
164 int
165 main(int argc, char **argv)
166 {
167 	int failed = 0;
168 
169 	SSL_library_init();
170 
171 	failed |= cipher_get_put_tests();
172 	failed |= cipher_get_by_value_tests();
173 
174 	return (failed);
175 }
176