-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-nm-l2tp-crypto.c
332 lines (283 loc) · 9.93 KB
/
test-nm-l2tp-crypto.c
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "nm-default.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <nm-utils.h>
#include <nm-errors.h>
#include "nm-l2tp-crypto-openssl.h"
#include "nm-l2tp-crypto-nss.h"
#define TEST_CERT_DIR "./certs"
#define TEST_NSS_DIR "./nss-db"
#define TEST_PEM_DIR "./pem-output"
char *test_files[] = {
"ca-no-ending-newline.pem",
"pkcs8-decrypted.der",
"pkcs8-enc-key.pem",
"pkcs8-noenc-key.pem",
"strongswan_ecCert.pem",
"strongswan_ecKey.pem",
"test2_ca_cert.pem",
"test2-cert.p12",
"test2_key_and_cert.pem",
"test-aes-key.pem",
"test_ca_cert.der",
"test_ca_cert.pem",
"test-ca-cert.pem",
"v3-certs-TDES.p12",
"test_key_and_cert.pem",
"test-key-and-cert.pem",
"test-key-only-decrypted.der",
"test-key-only-decrypted.pem",
"test-key-only.pem",
NULL
};
char *pkey_password[][2] = {
{"test_key_and_cert.pem", "test"},
{"test-key-only.pem", "test"},
{"test2_key_and_cert.pem", "12345testing"},
{"test-aes-128-key.pem", "test-aes-password"},
{"test-aes-256-key.pem", "test-aes-password"},
{NULL, NULL}
};
char *pkcs12_password[][2] = {
{"v3-certs-TDES.p12", "v3-certs"},
{"test2-cert.p12", "12345testing"},
{NULL, NULL}
};
char *pkey_cert_ca_password[][4] = {
{"sunKey.pem", "sunCert.pem", "strongswanCert.pem", NULL},
{NULL, NULL, NULL, NULL}
};
char *cryptoFileFormatToString(NML2tpCryptoFileFormat format) {
switch (format) {
case NM_L2TP_CRYPTO_FILE_FORMAT_UNKNOWN:
return "NM_L2TP_CRYPTO_FILE_FORMAT_UNKNOWN";
break;
case NM_L2TP_CRYPTO_FILE_FORMAT_X509_DER:
return "NM_L2TP_CRYPTO_FILE_FORMAT_X509_DER";
break;
case NM_L2TP_CRYPTO_FILE_FORMAT_X509_PEM:
return "NM_L2TP_CRYPTO_FILE_FORMAT_X509_PEM";
break;
case NM_L2TP_CRYPTO_FILE_FORMAT_PKCS8_DER:
return "NM_L2TP_CRYPTO_FILE_FORMAT_PKCS8_DER";
break;
case NM_L2TP_CRYPTO_FILE_FORMAT_PKCS8_PEM:
return "NM_L2TP_CRYPTO_FILE_FORMAT_PKCS8_PEM";
break;
case NM_L2TP_CRYPTO_FILE_FORMAT_PKCS12:
return "NM_L2TP_CRYPTO_FILE_FORMAT_PKCS12";
break;
}
return "NM_L2TP_CRYPTO_FILE_FORMAT_UNKNOWN";
}
void test_crypto_file_format (char *filename)
{
GError *err = NULL;
NML2tpCryptoFileFormat format = NM_L2TP_CRYPTO_FILE_FORMAT_UNKNOWN;
gboolean need_password;
gs_free char *filepath = NULL;
filepath = g_build_filename (TEST_CERT_DIR, (const char *) filename, NULL);
printf("testing crypto_file_format %s :\n", filename);
format = crypto_file_format (filepath, &need_password, &err);
if ( err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
err = NULL;
}
printf("\tformat = %s , need password = %d\n", cryptoFileFormatToString(format), need_password);
printf("testing NULL NULL crypto_file_format %s :\n", filename);
format = crypto_file_format (filepath, NULL, NULL);
}
void test_crypto_pkcs12_get_subject_name (const char *filename, const char *password)
{
GError *err = NULL;
GString *subject_name_str = NULL;
GByteArray *subject_name_asn1 = NULL;
gs_free char *filepath = NULL;
filepath = g_build_filename (TEST_CERT_DIR, (const char *) filename, NULL);
printf("testing crypto_pkcs12_get_subject_name %s :\n", filename);
crypto_pkcs12_get_subject_name (filepath, password, &subject_name_str, &subject_name_asn1, &err);
if ( err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
printf("\tsubject_name=%s\n", subject_name_str->str);
printf("\tasn1dn:#");
for (size_t i = 0; i < subject_name_asn1->len; i++)
printf("%02x", subject_name_asn1->data[i]);
printf("\n");
g_string_free (subject_name_str, TRUE);
g_byte_array_free (subject_name_asn1, TRUE);
}
void test_crypto_create_pkcs12_data (const char *pkey_filename,
const char *cert_filename,
const char *ca_filename,
const char *password)
{
GError *err = NULL;
gs_free char *pkey_filepath = NULL;
gs_free char *cert_filepath = NULL;
gs_free char *ca_filepath = NULL;
GByteArray *p12_array;
pkey_filepath = g_build_filename (TEST_CERT_DIR, (const char *) pkey_filename, NULL);
cert_filepath = g_build_filename (TEST_CERT_DIR, (const char *) cert_filename, NULL);
if (ca_filename)
ca_filepath = g_build_filename (TEST_CERT_DIR, (const char *) ca_filename, NULL);
printf("testing crypto_create_pkcs12_data,\n pkey='%s',\n cert='%s',\n CA='%s':\n", pkey_filepath, cert_filepath, (ca_filepath?ca_filepath:""));
p12_array = crypto_create_pkcs12_data (pkey_filepath, cert_filepath, ca_filepath, password, NULL, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
printf("testing crypto_import_nss_pkcs12\n");
crypto_import_nss_pkcs12 (p12_array, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
}
g_byte_array_free (p12_array, TRUE);
}
void test_crypto_decrypt_pkcs12_data (const char *filename, const char *password)
{
GError *err = NULL;
gs_free char *filepath = NULL;
GByteArray *p12_array;
filepath = g_build_filename (TEST_CERT_DIR, (const char *) filename, NULL);
printf("testing crypto_decrypt_pkcs12_data '%s'\n", filepath);
p12_array = crypto_decrypt_pkcs12_data (filepath, password, NULL, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
printf("testing crypto_import_nss_pkcs12\n");
crypto_import_nss_pkcs12 (p12_array, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
}
g_byte_array_free (p12_array, TRUE);
}
void test_crypto_pkcs12_to_pem_files (const char *p12_filename, const char *password)
{
GError *err = NULL;
gs_free char *p12_filepath = NULL;
gs_free char *pkey_filepath;
gs_free char *cert_filepath;
gs_free char *ca_filepath;
p12_filepath = g_build_filename (TEST_CERT_DIR, (const char *) p12_filename, NULL);
pkey_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "pkey.pem", NULL);
cert_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "cert.pem", NULL);
ca_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "ca.pem", NULL);
printf("testing crypto_pkcs12_to_pem_files '%s'\n", p12_filepath);
printf(" pkey='%s',\n cert='%s',\n CA='%s':\n", pkey_filepath, cert_filepath, ca_filepath);
crypto_pkcs12_to_pem_files (p12_filepath, password, pkey_filepath, cert_filepath, ca_filepath, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
}
void test_crypto_to_pem_file ()
{
GError *err = NULL;
gs_free char *ca_in_filepath = NULL;
gs_free char *pkey_in_filepath = NULL;
gs_free char *ca_filepath;
gs_free char *pkey_decrypted_filepath;
gs_free char *pkey_encrypted_filepath;
ca_in_filepath = g_build_filename (TEST_CERT_DIR, (const char *) "test_ca_cert.der", NULL);
pkey_in_filepath = g_build_filename (TEST_CERT_DIR, (const char *) "pkcs8-decrypted.der", NULL);
ca_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "der2pem-ca.pem", NULL);
pkey_decrypted_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "der2pem-pkey-decrypted.pem", NULL);
pkey_encrypted_filepath = g_build_filename (TEST_PEM_DIR, (const char *) "der2pem-pkey-encrypted.pem", NULL);
printf("testing crypto_x509_der_to_pem_file '%s'\n", ca_in_filepath);
printf(" CA='%s'\n", ca_filepath);
crypto_x509_der_to_pem_file (ca_in_filepath, ca_filepath, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
printf("testing crypto_pkey_der_to_pem_file '%s'\n", pkey_in_filepath);
printf(" pkey='%s'\n", pkey_decrypted_filepath);
crypto_pkey_der_to_pem_file (pkey_in_filepath, NULL, pkey_decrypted_filepath, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
printf("testing crypto_pkey_der_to_pem_file '%s'\n", pkey_in_filepath);
printf(" pkey='%s'\n", pkey_encrypted_filepath);
crypto_pkey_der_to_pem_file (pkey_in_filepath, "password", pkey_encrypted_filepath, &err);
if (err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
return;
}
}
int main (int argc, char **argv) {
GError *err = NULL;
int i;
printf("### Testing File Format ####\n");
i = 0;
while (test_files[i] != NULL) {
test_crypto_file_format(test_files[i]);
i++;
}
printf("\n### Testing PKCS#12 Subject Name ####\n");
i = 0;
while (pkcs12_password[i][0] != NULL) {
test_crypto_pkcs12_get_subject_name (pkcs12_password[i][0], pkcs12_password[i][1]);
i++;
}
printf("\n### Testing NSS database import ####\n");
/* Setup NSS database directory */
if (g_mkdir_with_parents (TEST_NSS_DIR, 0755) != 0) {
fprintf(stderr, "failed to create NSS dir '%s' : %s\n", TEST_NSS_DIR, g_strerror (errno));
exit(1);
}
crypto_init_nss (TEST_NSS_DIR, &err);
if ( err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
err = NULL;
}
i = 0;
while (pkey_cert_ca_password[i][0] != NULL) {
test_crypto_create_pkcs12_data (pkey_cert_ca_password[i][0],
pkey_cert_ca_password[i][1],
pkey_cert_ca_password[i][2],
pkey_cert_ca_password[i][3]);
i++;
}
i = 0;
while (pkcs12_password[i][0] != NULL) {
test_crypto_decrypt_pkcs12_data (pkcs12_password[i][0],
pkcs12_password[i][1]);
i++;
}
printf("\n### Testing PEM output functions ####\n");
/* Setup output pem directory */
if (g_mkdir_with_parents (TEST_PEM_DIR, 0755) != 0) {
fprintf(stderr, "failed to create PEM output dir '%s' : %s\n", TEST_PEM_DIR, g_strerror (errno));
exit(1);
}
i = 0;
while (pkcs12_password[i][0] != NULL) {
test_crypto_pkcs12_to_pem_files (pkcs12_password[i][0],
pkcs12_password[i][1]);
i++;
}
test_crypto_to_pem_file();
crypto_deinit_nss (&err);
if ( err != NULL ) {
fprintf(stderr, "\terr = %s\n", err->message);
g_error_free(err);
err = NULL;
}
}