]> git.ipfire.org Git - thirdparty/openvpn.git/blob - tests/unit_tests/openvpn/test_crypto.c
unit-test: fix test_crypto when USE_COMP is not defined
[thirdparty/openvpn.git] / tests / unit_tests / openvpn / test_crypto.c
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2016-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29
30 #include "syshead.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <setjmp.h>
37 #include <cmocka.h>
38
39 #include "crypto.h"
40 #include "options.h"
41 #include "ssl_backend.h"
42
43 #include "mock_msg.h"
44
45 static const char testtext[] = "Dummy text to test PEM encoding";
46
47 static void
48 crypto_pem_encode_decode_loopback(void **state)
49 {
50 struct gc_arena gc = gc_new();
51 struct buffer src_buf;
52 buf_set_read(&src_buf, (void *)testtext, sizeof(testtext));
53
54 uint8_t dec[sizeof(testtext)];
55 struct buffer dec_buf;
56 buf_set_write(&dec_buf, dec, sizeof(dec));
57
58 struct buffer pem_buf;
59
60 assert_true(crypto_pem_encode("TESTKEYNAME", &pem_buf, &src_buf, &gc));
61 assert_true(BLEN(&src_buf) < BLEN(&pem_buf));
62
63 /* Wrong key name */
64 assert_false(crypto_pem_decode("WRONGNAME", &dec_buf, &pem_buf));
65
66 assert_true(crypto_pem_decode("TESTKEYNAME", &dec_buf, &pem_buf));
67 assert_int_equal(BLEN(&src_buf), BLEN(&dec_buf));
68 assert_memory_equal(BPTR(&src_buf), BPTR(&dec_buf), BLEN(&src_buf));
69
70 gc_free(&gc);
71 }
72
73 static void
74 test_translate_cipher(const char *ciphername, const char *openvpn_name)
75 {
76 bool cipher = cipher_valid(ciphername);
77
78 /* Empty cipher is fine */
79 if (!cipher)
80 {
81 return;
82 }
83
84 const char *kt_name = cipher_kt_name(ciphername);
85
86 assert_string_equal(kt_name, openvpn_name);
87 }
88
89 static void
90 test_cipher_names(const char *ciphername, const char *openvpn_name)
91 {
92 struct gc_arena gc = gc_new();
93 /* Go through some variants, if the cipher library accepts these, they
94 * should be normalised to the openvpn name */
95 char *upper = string_alloc(ciphername, &gc);
96 char *lower = string_alloc(ciphername, &gc);
97 char *random_case = string_alloc(ciphername, &gc);
98
99 for (int i = 0; i < strlen(ciphername); i++)
100 {
101 upper[i] = toupper(ciphername[i]);
102 lower[i] = tolower(ciphername[i]);
103 if (rand() & 0x1)
104 {
105 random_case[i] = upper[i];
106 }
107 else
108 {
109 random_case[i] = lower[i];
110 }
111 }
112
113 if (!openvpn_name)
114 {
115 openvpn_name = upper;
116 }
117
118 test_translate_cipher(upper, openvpn_name);
119 test_translate_cipher(lower, openvpn_name);
120 test_translate_cipher(random_case, openvpn_name);
121 test_translate_cipher(ciphername, openvpn_name);
122
123
124 gc_free(&gc);
125 }
126
127 static void
128 crypto_translate_cipher_names(void **state)
129 {
130 /* Test that a number of ciphers to see that they turn out correctly */
131 test_cipher_names("BF-CBC", NULL);
132 test_cipher_names("BLOWFISH-CBC", "BF-CBC");
133 test_cipher_names("Chacha20-Poly1305", NULL);
134 test_cipher_names("AES-128-GCM", NULL);
135 test_cipher_names("AES-128-CBC", NULL);
136 test_cipher_names("CAMELLIA-128-CFB128", "CAMELLIA-128-CFB");
137 test_cipher_names("id-aes256-GCM", "AES-256-GCM");
138 }
139
140
141 static uint8_t good_prf[32] = {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94, 0x69,
142 0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb,
143 0xb1, 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6,
144 0xa9, 0x04, 0xac, 0x2d, 0xda, 0xb7, 0x2d, 0x67};
145
146 static const char* ipsumlorem = "Lorem ipsum dolor sit amet, consectetur "
147 "adipisici elit, sed eiusmod tempor incidunt "
148 "ut labore et dolore magna aliqua.";
149
150 static void
151 crypto_test_tls_prf(void **state)
152 {
153 const char *seedstr = "Quis aute iure reprehenderit in voluptate "
154 "velit esse cillum dolore";
155 const unsigned char *seed = (const unsigned char *)seedstr;
156 const size_t seed_len = strlen(seedstr);
157
158
159 const unsigned char *secret = (const unsigned char *) ipsumlorem;
160 size_t secret_len = strlen((const char *)secret);
161
162
163 uint8_t out[32];
164 ssl_tls1_PRF(seed, seed_len, secret, secret_len, out, sizeof(out));
165
166 assert_memory_equal(good_prf, out, sizeof(out));
167 }
168
169 static uint8_t testkey[20] = {0x0b, 0x00};
170 static uint8_t goodhash[20] = {0x58, 0xea, 0x5a, 0xf0, 0x42, 0x94, 0xe9, 0x17,
171 0xed, 0x84, 0xb9, 0xf0, 0x83, 0x30, 0x23, 0xae,
172 0x8b, 0xa7, 0x7e, 0xb8};
173
174 static void
175 crypto_test_hmac(void **state)
176 {
177 hmac_ctx_t *hmac = hmac_ctx_new();
178
179 assert_int_equal(md_kt_size("SHA1"), 20);
180
181 uint8_t key[20];
182 memcpy(key, testkey, sizeof(key));
183
184 hmac_ctx_init(hmac, key, "SHA1");
185 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
186 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
187
188 uint8_t hash[20];
189 hmac_ctx_final(hmac, hash);
190
191 assert_memory_equal(hash, goodhash, sizeof(hash));
192 memset(hash, 0x00, sizeof(hash));
193
194 /* try again */
195 hmac_ctx_reset(hmac);
196 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
197 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
198 hmac_ctx_final(hmac, hash);
199
200 assert_memory_equal(hash, goodhash, sizeof(hash));
201
202 /* Fill our key with random data to ensure it is not used by hmac anymore */
203 memset(key, 0x55, sizeof(key));
204
205 hmac_ctx_reset(hmac);
206 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
207 hmac_ctx_update(hmac, (const uint8_t *)ipsumlorem, (int) strlen(ipsumlorem));
208 hmac_ctx_final(hmac, hash);
209
210 assert_memory_equal(hash, goodhash, sizeof(hash));
211 hmac_ctx_cleanup(hmac);
212 hmac_ctx_free(hmac);
213 }
214
215 void
216 test_des_encrypt(void **state)
217 {
218 /* We have a small des encrypt method that is only for NTLMv1. This unit
219 * test ensures that it is not accidentally broken */
220
221 const unsigned char des_key[DES_KEY_LENGTH] = {0x42, 0x23};
222
223 const char *src = "MoinWelt";
224
225 /* cipher_des_encrypt_ecb wants a non const */
226 unsigned char *src2 = (unsigned char *) strdup(src);
227
228 unsigned char dst[DES_KEY_LENGTH];
229 cipher_des_encrypt_ecb(des_key, src2, dst);
230
231 const unsigned char dst_good[DES_KEY_LENGTH] = {0xd3, 0x8f, 0x61, 0xf7, 0xbe, 0x27, 0xb6, 0xa2};
232
233 assert_memory_equal(dst, dst_good, DES_KEY_LENGTH);
234
235 free(src2);
236 }
237
238 /* This test is in test_crypto as it calls into the functions that calculate
239 * the crypto overhead */
240 static void
241 test_occ_mtu_calculation(void **state)
242 {
243 struct gc_arena gc = gc_new();
244
245 struct frame f = { 0 };
246 struct options o = { 0 };
247 size_t linkmtu;
248
249 /* common defaults */
250 o.ce.tun_mtu = 1400;
251 o.replay = true;
252 o.ce.proto = PROTO_UDP;
253
254 /* No crypto at all */
255 o.ciphername = "none";
256 o.authname = "none";
257 linkmtu = calc_options_string_link_mtu(&o, &f);
258 assert_int_equal(linkmtu, 1400);
259
260 /* Static key OCC examples */
261 o.shared_secret_file = "not null";
262
263 /* secret, auth none, cipher none */
264 o.ciphername = "none";
265 o.authname = "none";
266 linkmtu = calc_options_string_link_mtu(&o, &f);
267 assert_int_equal(linkmtu, 1408);
268
269 /* secret, cipher AES-128-CBC, auth none */
270 o.ciphername = "AES-128-CBC";
271 o.authname = "none";
272 linkmtu = calc_options_string_link_mtu(&o, &f);
273 assert_int_equal(linkmtu, 1440);
274
275 /* secret, cipher none, auth SHA256 */
276 o.ciphername = "none";
277 o.authname = "SHA256";
278 linkmtu = calc_options_string_link_mtu(&o, &f);
279 assert_int_equal(linkmtu, 1440);
280
281 /* secret, cipher BF-CBC, auth SHA1 */
282 o.ciphername = "BF-CBC";
283 o.authname = "SHA1";
284 linkmtu = calc_options_string_link_mtu(&o, &f);
285 assert_int_equal(linkmtu, 1444);
286
287 /* secret, cipher BF-CBC, auth SHA1, tcp-client */
288 o.ce.proto = PROTO_TCP_CLIENT;
289 linkmtu = calc_options_string_link_mtu(&o, &f);
290 assert_int_equal(linkmtu, 1446);
291
292 o.ce.proto = PROTO_UDP;
293
294 #if defined(USE_COMP)
295 o.comp.alg = COMP_ALG_LZO;
296
297 /* secret, comp-lzo yes, cipher BF-CBC, auth SHA1 */
298 linkmtu = calc_options_string_link_mtu(&o, &f);
299 assert_int_equal(linkmtu, 1445);
300
301 /* secret, comp-lzo yes, cipher BF-CBC, auth SHA1, fragment 1200 */
302 o.ce.fragment = 1200;
303 linkmtu = calc_options_string_link_mtu(&o, &f);
304 assert_int_equal(linkmtu, 1449);
305
306 o.comp.alg = COMP_ALG_UNDEF;
307 o.ce.fragment = 0;
308 #endif
309
310 /* TLS mode */
311 o.shared_secret_file = NULL;
312 o.tls_client = true;
313 o.pull = true;
314
315 /* tls client, cipher AES-128-CBC, auth SHA1, tls-auth */
316 o.authname = "SHA1";
317 o.ciphername = "AES-128-CBC";
318 o.tls_auth_file = "dummy";
319
320 linkmtu = calc_options_string_link_mtu(&o, &f);
321 assert_int_equal(linkmtu, 1457);
322
323 /* tls client, cipher AES-128-CBC, auth SHA1 */
324 o.tls_auth_file = NULL;
325
326 linkmtu = calc_options_string_link_mtu(&o, &f);
327 assert_int_equal(linkmtu, 1457);
328
329 /* tls client, cipher none, auth none */
330 o.authname = "none";
331 o.ciphername = "none";
332
333 linkmtu = calc_options_string_link_mtu(&o, &f);
334 assert_int_equal(linkmtu, 1405);
335
336 /* tls client, auth none, cipher none, no-replay */
337 o.replay = false;
338
339 linkmtu = calc_options_string_link_mtu(&o, &f);
340 assert_int_equal(linkmtu, 1401);
341
342
343 o.replay = true;
344
345 /* tls client, auth SHA1, cipher AES-256-GCM */
346 o.authname = "SHA1";
347 o.ciphername = "AES-256-GCM";
348 linkmtu = calc_options_string_link_mtu(&o, &f);
349 assert_int_equal(linkmtu, 1449);
350
351
352 #if defined(USE_COMP)
353 o.comp.alg = COMP_ALG_LZO;
354
355 /* tls client, auth SHA1, cipher AES-256-GCM, fragment, comp-lzo yes */
356 o.ce.fragment = 1200;
357 linkmtu = calc_options_string_link_mtu(&o, &f);
358 assert_int_equal(linkmtu, 1454);
359
360 /* tls client, auth SHA1, cipher AES-256-GCM, fragment, comp-lzo yes, socks */
361 o.ce.socks_proxy_server = "socks.example.com";
362 linkmtu = calc_options_string_link_mtu(&o, &f);
363 assert_int_equal(linkmtu, 1464);
364 #endif
365
366 gc_free(&gc);
367 }
368
369 int
370 main(void)
371 {
372 const struct CMUnitTest tests[] = {
373 cmocka_unit_test(crypto_pem_encode_decode_loopback),
374 cmocka_unit_test(crypto_translate_cipher_names),
375 cmocka_unit_test(crypto_test_tls_prf),
376 cmocka_unit_test(crypto_test_hmac),
377 cmocka_unit_test(test_des_encrypt),
378 cmocka_unit_test(test_occ_mtu_calculation)
379 };
380
381 #if defined(ENABLE_CRYPTO_OPENSSL)
382 OpenSSL_add_all_algorithms();
383 #endif
384
385 int ret = cmocka_run_group_tests_name("crypto tests", tests, NULL, NULL);
386
387 #if defined(ENABLE_CRYPTO_OPENSSL)
388 EVP_cleanup();
389 #endif
390
391 return ret;
392 }