1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/* $OpenBSD: ssl_methods.c,v 1.3 2021/04/04 20:16:29 tb Exp $ */
/*
* Copyright (c) 2020 Theo Buehler <tb@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <openssl/ssl.h>
struct ssl_method_test_data {
const SSL_METHOD *(*method)(void);
const char *name;
int server;
int dtls;
};
struct ssl_method_test_data ssl_method_tests[] = {
{
.method = SSLv23_method,
.name = "SSLv23_method",
.server = 1,
.dtls = 0,
},
{
.method = SSLv23_server_method,
.name = "SSLv23_server_method",
.server = 1,
.dtls = 0,
},
{
.method = SSLv23_client_method,
.name = "SSLv23_client_method",
.server = 0,
.dtls = 0,
},
{
.method = TLSv1_method,
.name = "TLSv1_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_server_method,
.name = "TLSv1_server_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_client_method,
.name = "TLSv1_client_method",
.server = 0,
.dtls = 0,
},
{
.method = TLSv1_1_method,
.name = "TLSv1_1_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_1_server_method,
.name = "TLSv1_1_server_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_1_client_method,
.name = "TLSv1_1_client_method",
.server = 0,
.dtls = 0,
},
{
.method = TLSv1_2_method,
.name = "TLSv1_2_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_2_server_method,
.name = "TLSv1_2_server_method",
.server = 1,
.dtls = 0,
},
{
.method = TLSv1_2_client_method,
.name = "TLSv1_2_client_method",
.server = 0,
.dtls = 0,
},
{
.method = TLS_method,
.name = "TLS_method",
.server = 1,
.dtls = 0,
},
{
.method = TLS_server_method,
.name = "TLS_server_method",
.server = 1,
.dtls = 0,
},
{
.method = TLS_client_method,
.name = "TLS_client_method",
.server = 0,
.dtls = 0,
},
{
.method = DTLSv1_method,
.name = "DTLSv1_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLSv1_server_method,
.name = "DTLSv1_server_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLSv1_client_method,
.name = "DTLSv1_client_method",
.server = 0,
.dtls = 1,
},
{
.method = DTLSv1_2_method,
.name = "DTLSv1_2_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLSv1_2_server_method,
.name = "DTLSv1_2_server_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLSv1_2_client_method,
.name = "DTLSv1_2_client_method",
.server = 0,
.dtls = 1,
},
{
.method = DTLS_method,
.name = "DTLS_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLS_server_method,
.name = "DTLS_server_method",
.server = 1,
.dtls = 1,
},
{
.method = DTLS_client_method,
.name = "DTLS_client_method",
.server = 0,
.dtls = 1,
},
};
#define N_METHOD_TESTS (sizeof(ssl_method_tests) / sizeof(ssl_method_tests[0]))
int test_client_or_server_method(struct ssl_method_test_data *);
int test_dtls_method(struct ssl_method_test_data *);
int
test_client_or_server_method(struct ssl_method_test_data *testcase)
{
SSL_CTX *ssl_ctx;
SSL *ssl = NULL;
int failed = 1;
if ((ssl_ctx = SSL_CTX_new(testcase->method())) == NULL) {
fprintf(stderr, "SSL_CTX_new returned NULL\n");
goto err;
}
if ((ssl = SSL_new(ssl_ctx)) == NULL) {
fprintf(stderr, "SSL_CTX_new returned NULL\n");
goto err;
}
if (SSL_is_server(ssl) != testcase->server) {
fprintf(stderr, "%s: SSL_is_server: want %d, got %d\n",
testcase->name, testcase->server, SSL_is_server(ssl));
goto err;
}
failed = 0;
err:
SSL_free(ssl);
SSL_CTX_free(ssl_ctx);
return failed;
}
int
test_dtls_method(struct ssl_method_test_data *testcase)
{
SSL_CTX *ssl_ctx;
SSL *ssl = NULL;
int failed = 1;
if ((ssl_ctx = SSL_CTX_new(testcase->method())) == NULL) {
fprintf(stderr, "SSL_CTX_new returned NULL\n");
goto err;
}
if ((ssl = SSL_new(ssl_ctx)) == NULL) {
fprintf(stderr, "SSL_CTX_new returned NULL\n");
goto err;
}
if (SSL_is_dtls(ssl) != testcase->dtls) {
fprintf(stderr, "%s: SSL_is_dtls: want %d, got %d\n",
testcase->name, testcase->dtls, SSL_is_dtls(ssl));
goto err;
}
failed = 0;
err:
SSL_free(ssl);
SSL_CTX_free(ssl_ctx);
return failed;
}
int
main(int argc, char **argv)
{
size_t i;
int failed = 0;
for (i = 0; i < N_METHOD_TESTS; i++) {
failed |= test_client_or_server_method(&ssl_method_tests[i]);
failed |= test_dtls_method(&ssl_method_tests[i]);
}
if (failed == 0)
printf("PASS %s\n", __FILE__);
return failed;
}
|