Line | Source | Count |
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 | #include <stdio.h> | - |
65 | #include <string.h> | - |
66 | | - |
67 | #include <openssl/opensslconf.h> | - |
68 | | - |
69 | #include <openssl/crypto.h> | - |
70 | #include <openssl/dso.h> | - |
71 | #include <openssl/engine.h> | - |
72 | #include <openssl/err.h> | - |
73 | #include <openssl/evp.h> | - |
74 | #include <openssl/pem.h> | - |
75 | #include <openssl/rand.h> | - |
76 | | - |
77 | #ifndef OPENSSL_NO_DH | - |
78 | #include <openssl/dh.h> | - |
79 | #endif | - |
80 | #ifndef OPENSSL_NO_DSA | - |
81 | #include <openssl/dsa.h> | - |
82 | #endif | - |
83 | #ifndef OPENSSL_NO_RSA | - |
84 | #include <openssl/rsa.h> | - |
85 | #endif | - |
86 | | - |
87 | | - |
88 | | - |
89 | | - |
90 | #define TEST_ENG_OPENSSL_RC4 | - |
91 | #define TEST_ENG_OPENSSL_PKEY | - |
92 | | - |
93 | #define TEST_ENG_OPENSSL_RC4_P_INIT | - |
94 | | - |
95 | #define TEST_ENG_OPENSSL_SHA | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | #ifdef OPENSSL_NO_RC4 | - |
103 | #undef TEST_ENG_OPENSSL_RC4 | - |
104 | #undef TEST_ENG_OPENSSL_RC4_OTHERS | - |
105 | #undef TEST_ENG_OPENSSL_RC4_P_INIT | - |
106 | #undef TEST_ENG_OPENSSL_RC4_P_CIPHER | - |
107 | #endif | - |
108 | #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA1) | - |
109 | #undef TEST_ENG_OPENSSL_SHA | - |
110 | #undef TEST_ENG_OPENSSL_SHA_OTHERS | - |
111 | #undef TEST_ENG_OPENSSL_SHA_P_INIT | - |
112 | #undef TEST_ENG_OPENSSL_SHA_P_UPDATE | - |
113 | #undef TEST_ENG_OPENSSL_SHA_P_FINAL | - |
114 | #endif | - |
115 | | - |
116 | #ifdef TEST_ENG_OPENSSL_RC4 | - |
117 | static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | - |
118 | const int **nids, int nid); | - |
119 | #endif | - |
120 | #ifdef TEST_ENG_OPENSSL_SHA | - |
121 | static int openssl_digests(ENGINE *e, const EVP_MD **digest, | - |
122 | const int **nids, int nid); | - |
123 | #endif | - |
124 | | - |
125 | #ifdef TEST_ENG_OPENSSL_PKEY | - |
126 | static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, | - |
127 | UI_METHOD *ui_method, void *callback_data); | - |
128 | #endif | - |
129 | | - |
130 | | - |
131 | static const char *engine_openssl_id = "openssl"; | - |
132 | static const char *engine_openssl_name = "Software engine support"; | - |
133 | | - |
134 | | - |
135 | | - |
136 | static int | - |
137 | bind_helper(ENGINE *e) | - |
138 | { | - |
139 | if (!ENGINE_set_id(e, engine_openssl_id) ||TRUE | never evaluated | FALSE | never evaluated |
| 0 |
140 | !ENGINE_set_name(e, engine_openssl_name)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
141 | #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS | - |
142 | #ifndef OPENSSL_NO_RSA | - |
143 | || !ENGINE_set_RSA(e, RSA_get_default_method())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
144 | #endif | - |
145 | #ifndef OPENSSL_NO_DSA | - |
146 | || !ENGINE_set_DSA(e, DSA_get_default_method())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
147 | #endif | - |
148 | #ifndef OPENSSL_NO_ECDH | - |
149 | || !ENGINE_set_ECDH(e, ECDH_OpenSSL())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
150 | #endif | - |
151 | #ifndef OPENSSL_NO_ECDSA | - |
152 | || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
153 | #endif | - |
154 | #ifndef OPENSSL_NO_DH | - |
155 | || !ENGINE_set_DH(e, DH_get_default_method())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
156 | #endif | - |
157 | || !ENGINE_set_RAND(e, RAND_SSLeay())TRUE | never evaluated | FALSE | never evaluated |
| 0 |
158 | #ifdef TEST_ENG_OPENSSL_RC4 | - |
159 | || !ENGINE_set_ciphers(e, openssl_ciphers)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
160 | #endif | - |
161 | #ifdef TEST_ENG_OPENSSL_SHA | - |
162 | || !ENGINE_set_digests(e, openssl_digests)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
163 | #endif | - |
164 | #endif | - |
165 | #ifdef TEST_ENG_OPENSSL_PKEY | - |
166 | || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
167 | #endif | - |
168 | ) | - |
169 | return 0; never executed: return 0; | 0 |
170 | | - |
171 | | - |
172 | return 1; never executed: return 1; | 0 |
173 | } | - |
174 | | - |
175 | static ENGINE * | - |
176 | engine_openssl(void) | - |
177 | { | - |
178 | ENGINE *ret = ENGINE_new(); | - |
179 | | - |
180 | if (ret == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
181 | return NULL; never executed: return ((void *)0) ; | 0 |
182 | if (!bind_helper(ret)) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
183 | ENGINE_free(ret); | - |
184 | return NULL; never executed: return ((void *)0) ; | 0 |
185 | } | - |
186 | return ret; never executed: return ret; | 0 |
187 | } | - |
188 | | - |
189 | void | - |
190 | ENGINE_load_openssl(void) | - |
191 | { | - |
192 | ENGINE *toadd = engine_openssl(); | - |
193 | | - |
194 | if (toadd == NULL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
195 | return; never executed: return; | 0 |
196 | (void) ENGINE_add(toadd); | - |
197 | | - |
198 | | - |
199 | ENGINE_free(toadd); | - |
200 | ERR_clear_error(); | - |
201 | } never executed: end of block | 0 |
202 | | - |
203 | | - |
204 | | - |
205 | #ifdef ENGINE_DYNAMIC_SUPPORT | - |
206 | static int | - |
207 | bind_fn(ENGINE *e, const char *id) | - |
208 | { | - |
209 | if (id && (strcmp(id, engine_openssl_id) != 0)) | - |
210 | return 0; | - |
211 | if (!bind_helper(e)) | - |
212 | return 0; | - |
213 | return 1; | - |
214 | } | - |
215 | IMPLEMENT_DYNAMIC_CHECK_FN() | - |
216 | IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) | - |
217 | #endif /* ENGINE_DYNAMIC_SUPPORT */ | - |
218 | | - |
219 | #ifdef TEST_ENG_OPENSSL_RC4 | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | | - |
225 | | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
230 | #include <openssl/rc4.h> | - |
231 | #define TEST_RC4_KEY_SIZE 16 | - |
232 | static int test_cipher_nids[] = {NID_rc4, NID_rc4_40}; | - |
233 | static int test_cipher_nids_number = 2; | - |
234 | | - |
235 | typedef struct { | - |
236 | unsigned char key[TEST_RC4_KEY_SIZE]; | - |
237 | RC4_KEY ks; | - |
238 | } TEST_RC4_KEY; | - |
239 | | - |
240 | #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data) | - |
241 | static int | - |
242 | test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | - |
243 | const unsigned char *iv, int enc) | - |
244 | { | - |
245 | #ifdef TEST_ENG_OPENSSL_RC4_P_INIT | - |
246 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); | - |
247 | #endif | - |
248 | memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx)); | - |
249 | RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), | - |
250 | test(ctx)->key); | - |
251 | return 1; never executed: return 1; | 0 |
252 | } | - |
253 | | - |
254 | static int | - |
255 | test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | - |
256 | const unsigned char *in, size_t inl) | - |
257 | { | - |
258 | #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER | - |
259 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); | - |
260 | #endif | - |
261 | RC4(&test(ctx)->ks, inl, in, out); | - |
262 | return 1; never executed: return 1; | 0 |
263 | } | - |
264 | | - |
265 | static const EVP_CIPHER test_r4_cipher = { | - |
266 | NID_rc4, | - |
267 | 1, TEST_RC4_KEY_SIZE, 0, | - |
268 | EVP_CIPH_VARIABLE_LENGTH, | - |
269 | test_rc4_init_key, | - |
270 | test_rc4_cipher, | - |
271 | NULL, | - |
272 | sizeof(TEST_RC4_KEY), | - |
273 | NULL, | - |
274 | NULL, | - |
275 | NULL, | - |
276 | NULL | - |
277 | }; | - |
278 | | - |
279 | static const EVP_CIPHER test_r4_40_cipher = { | - |
280 | NID_rc4_40, | - |
281 | 1,5 ,0, | - |
282 | EVP_CIPH_VARIABLE_LENGTH, | - |
283 | test_rc4_init_key, | - |
284 | test_rc4_cipher, | - |
285 | NULL, | - |
286 | sizeof(TEST_RC4_KEY), | - |
287 | NULL, | - |
288 | NULL, | - |
289 | NULL, | - |
290 | NULL | - |
291 | }; | - |
292 | | - |
293 | static int | - |
294 | openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid) | - |
295 | { | - |
296 | if (!cipher) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
297 | | - |
298 | *nids = test_cipher_nids; | - |
299 | return test_cipher_nids_number; never executed: return test_cipher_nids_number; | 0 |
300 | } | - |
301 | | - |
302 | if (nid == NID_rc4)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
303 | *cipher = &test_r4_cipher; never executed: *cipher = &test_r4_cipher; | 0 |
304 | else if (nid == NID_rc4_40)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
305 | *cipher = &test_r4_40_cipher; never executed: *cipher = &test_r4_40_cipher; | 0 |
306 | else { | - |
307 | #ifdef TEST_ENG_OPENSSL_RC4_OTHERS | - |
308 | fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " | - |
309 | "nid %d\n", nid); | - |
310 | #endif | - |
311 | *cipher = NULL; | - |
312 | return 0; never executed: return 0; | 0 |
313 | } | - |
314 | return 1; never executed: return 1; | 0 |
315 | } | - |
316 | #endif | - |
317 | | - |
318 | #ifdef TEST_ENG_OPENSSL_SHA | - |
319 | | - |
320 | #include <openssl/sha.h> | - |
321 | static int test_digest_nids[] = {NID_sha1}; | - |
322 | static int test_digest_nids_number = 1; | - |
323 | | - |
324 | static int | - |
325 | test_sha1_init(EVP_MD_CTX *ctx) | - |
326 | { | - |
327 | #ifdef TEST_ENG_OPENSSL_SHA_P_INIT | - |
328 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); | - |
329 | #endif | - |
330 | return SHA1_Init(ctx->md_data); never executed: return SHA1_Init(ctx->md_data); | 0 |
331 | } | - |
332 | | - |
333 | static int | - |
334 | test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) | - |
335 | { | - |
336 | #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE | - |
337 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); | - |
338 | #endif | - |
339 | return SHA1_Update(ctx->md_data, data, count); never executed: return SHA1_Update(ctx->md_data, data, count); | 0 |
340 | } | - |
341 | | - |
342 | static int | - |
343 | test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md) | - |
344 | { | - |
345 | #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL | - |
346 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); | - |
347 | #endif | - |
348 | return SHA1_Final(md, ctx->md_data); never executed: return SHA1_Final(md, ctx->md_data); | 0 |
349 | } | - |
350 | | - |
351 | static const EVP_MD test_sha_md = { | - |
352 | NID_sha1, | - |
353 | NID_sha1WithRSAEncryption, | - |
354 | SHA_DIGEST_LENGTH, | - |
355 | 0, | - |
356 | test_sha1_init, | - |
357 | test_sha1_update, | - |
358 | test_sha1_final, | - |
359 | NULL, | - |
360 | NULL, | - |
361 | EVP_PKEY_RSA_method, | - |
362 | SHA_CBLOCK, | - |
363 | sizeof(EVP_MD *) + sizeof(SHA_CTX), | - |
364 | }; | - |
365 | | - |
366 | static int | - |
367 | openssl_digests(ENGINE *e, const EVP_MD **digest, const int **nids, int nid) | - |
368 | { | - |
369 | if (!digest) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
370 | | - |
371 | *nids = test_digest_nids; | - |
372 | return test_digest_nids_number; never executed: return test_digest_nids_number; | 0 |
373 | } | - |
374 | | - |
375 | if (nid == NID_sha1)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
376 | *digest = &test_sha_md; never executed: *digest = &test_sha_md; | 0 |
377 | else { | - |
378 | #ifdef TEST_ENG_OPENSSL_SHA_OTHERS | - |
379 | fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " | - |
380 | "nid %d\n", nid); | - |
381 | #endif | - |
382 | *digest = NULL; | - |
383 | return 0; never executed: return 0; | 0 |
384 | } | - |
385 | return 1; never executed: return 1; | 0 |
386 | } | - |
387 | #endif | - |
388 | | - |
389 | #ifdef TEST_ENG_OPENSSL_PKEY | - |
390 | static EVP_PKEY * | - |
391 | openssl_load_privkey(ENGINE *eng, const char *key_id, UI_METHOD *ui_method, | - |
392 | void *callback_data) | - |
393 | { | - |
394 | BIO *in; | - |
395 | EVP_PKEY *key; | - |
396 | | - |
397 | fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", | - |
398 | key_id); | - |
399 | in = BIO_new_file(key_id, "r"); | - |
400 | if (!in)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
401 | return NULL; never executed: return ((void *)0) ; | 0 |
402 | key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); | - |
403 | BIO_free(in); | - |
404 | return key; never executed: return key; | 0 |
405 | } | - |
406 | #endif | - |
| | |