]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL license (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <string.h> | |
12 | #include <stdlib.h> | |
13 | #include <ctype.h> | |
14 | #include <openssl/evp.h> | |
15 | #include <openssl/pem.h> | |
16 | #include <openssl/err.h> | |
17 | #include <openssl/x509v3.h> | |
18 | #include <openssl/pkcs12.h> | |
19 | #include <openssl/kdf.h> | |
20 | #include "internal/numbers.h" | |
21 | #include "testutil.h" | |
22 | #include "evp_test.h" | |
23 | ||
24 | ||
25 | typedef struct evp_test_method_st EVP_TEST_METHOD; | |
26 | ||
27 | /* | |
28 | * Structure holding test information | |
29 | */ | |
30 | typedef struct evp_test_st { | |
31 | STANZA s; /* Common test stanza */ | |
32 | char *name; | |
33 | int skip; /* Current test should be skipped */ | |
34 | const EVP_TEST_METHOD *meth; /* method for this test */ | |
35 | const char *err, *aux_err; /* Error string for test */ | |
36 | char *expected_err; /* Expected error value of test */ | |
37 | char *func; /* Expected error function string */ | |
38 | char *reason; /* Expected error reason string */ | |
39 | void *data; /* test specific data */ | |
40 | } EVP_TEST; | |
41 | ||
42 | /* | |
43 | * Test method structure | |
44 | */ | |
45 | struct evp_test_method_st { | |
46 | /* Name of test as it appears in file */ | |
47 | const char *name; | |
48 | /* Initialise test for "alg" */ | |
49 | int (*init) (EVP_TEST * t, const char *alg); | |
50 | /* Clean up method */ | |
51 | void (*cleanup) (EVP_TEST * t); | |
52 | /* Test specific name value pair processing */ | |
53 | int (*parse) (EVP_TEST * t, const char *name, const char *value); | |
54 | /* Run the test itself */ | |
55 | int (*run_test) (EVP_TEST * t); | |
56 | }; | |
57 | ||
58 | ||
59 | /* | |
60 | * Linked list of named keys. | |
61 | */ | |
62 | typedef struct key_list_st { | |
63 | char *name; | |
64 | EVP_PKEY *key; | |
65 | struct key_list_st *next; | |
66 | } KEY_LIST; | |
67 | ||
68 | /* | |
69 | * List of public and private keys | |
70 | */ | |
71 | static KEY_LIST *private_keys; | |
72 | static KEY_LIST *public_keys; | |
73 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst); | |
74 | ||
75 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen); | |
76 | ||
77 | /* | |
78 | * Structure used to hold a list of blocks of memory to test | |
79 | * calls to "update" like functions. | |
80 | */ | |
81 | struct evp_test_buffer_st { | |
82 | unsigned char *buf; | |
83 | size_t buflen; | |
84 | size_t count; | |
85 | int count_set; | |
86 | }; | |
87 | ||
88 | static void evp_test_buffer_free(EVP_TEST_BUFFER *db) | |
89 | { | |
90 | if (db != NULL) { | |
91 | OPENSSL_free(db->buf); | |
92 | OPENSSL_free(db); | |
93 | } | |
94 | } | |
95 | ||
96 | /* | |
97 | * append buffer to a list | |
98 | */ | |
99 | static int evp_test_buffer_append(const char *value, | |
100 | STACK_OF(EVP_TEST_BUFFER) **sk) | |
101 | { | |
102 | EVP_TEST_BUFFER *db = NULL; | |
103 | ||
104 | if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db)))) | |
105 | goto err; | |
106 | ||
107 | if (!parse_bin(value, &db->buf, &db->buflen)) | |
108 | goto err; | |
109 | db->count = 1; | |
110 | db->count_set = 0; | |
111 | ||
112 | if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null())) | |
113 | goto err; | |
114 | if (!sk_EVP_TEST_BUFFER_push(*sk, db)) | |
115 | goto err; | |
116 | ||
117 | return 1; | |
118 | ||
119 | err: | |
120 | evp_test_buffer_free(db); | |
121 | return 0; | |
122 | } | |
123 | ||
124 | /* | |
125 | * replace last buffer in list with copies of itself | |
126 | */ | |
127 | static int evp_test_buffer_ncopy(const char *value, | |
128 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
129 | { | |
130 | EVP_TEST_BUFFER *db; | |
131 | unsigned char *tbuf, *p; | |
132 | size_t tbuflen; | |
133 | int ncopy = atoi(value); | |
134 | int i; | |
135 | ||
136 | if (ncopy <= 0) | |
137 | return 0; | |
138 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
139 | return 0; | |
140 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
141 | ||
142 | tbuflen = db->buflen * ncopy; | |
143 | if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen))) | |
144 | return 0; | |
145 | for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen) | |
146 | memcpy(p, db->buf, db->buflen); | |
147 | ||
148 | OPENSSL_free(db->buf); | |
149 | db->buf = tbuf; | |
150 | db->buflen = tbuflen; | |
151 | return 1; | |
152 | } | |
153 | ||
154 | /* | |
155 | * set repeat count for last buffer in list | |
156 | */ | |
157 | static int evp_test_buffer_set_count(const char *value, | |
158 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
159 | { | |
160 | EVP_TEST_BUFFER *db; | |
161 | int count = atoi(value); | |
162 | ||
163 | if (count <= 0) | |
164 | return 0; | |
165 | ||
166 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
167 | return 0; | |
168 | ||
169 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
170 | if (db->count_set != 0) | |
171 | return 0; | |
172 | ||
173 | db->count = (size_t)count; | |
174 | db->count_set = 1; | |
175 | return 1; | |
176 | } | |
177 | ||
178 | /* | |
179 | * call "fn" with each element of the list in turn | |
180 | */ | |
181 | static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk, | |
182 | int (*fn)(void *ctx, | |
183 | const unsigned char *buf, | |
184 | size_t buflen), | |
185 | void *ctx) | |
186 | { | |
187 | int i; | |
188 | ||
189 | for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) { | |
190 | EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i); | |
191 | size_t j; | |
192 | ||
193 | for (j = 0; j < tb->count; j++) { | |
194 | if (fn(ctx, tb->buf, tb->buflen) <= 0) | |
195 | return 0; | |
196 | } | |
197 | } | |
198 | return 1; | |
199 | } | |
200 | ||
201 | /* | |
202 | * Unescape some sequences in string literals (only \n for now). | |
203 | * Return an allocated buffer, set |out_len|. If |input_len| | |
204 | * is zero, get an empty buffer but set length to zero. | |
205 | */ | |
206 | static unsigned char* unescape(const char *input, size_t input_len, | |
207 | size_t *out_len) | |
208 | { | |
209 | unsigned char *ret, *p; | |
210 | size_t i; | |
211 | ||
212 | if (input_len == 0) { | |
213 | *out_len = 0; | |
214 | return OPENSSL_zalloc(1); | |
215 | } | |
216 | ||
217 | /* Escaping is non-expanding; over-allocate original size for simplicity. */ | |
218 | if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len))) | |
219 | return NULL; | |
220 | ||
221 | for (i = 0; i < input_len; i++) { | |
222 | if (*input == '\\') { | |
223 | if (i == input_len - 1 || *++input != 'n') { | |
224 | TEST_error("Bad escape sequence in file"); | |
225 | goto err; | |
226 | } | |
227 | *p++ = '\n'; | |
228 | i++; | |
229 | input++; | |
230 | } else { | |
231 | *p++ = *input++; | |
232 | } | |
233 | } | |
234 | ||
235 | *out_len = p - ret; | |
236 | return ret; | |
237 | ||
238 | err: | |
239 | OPENSSL_free(ret); | |
240 | return NULL; | |
241 | } | |
242 | ||
243 | /* | |
244 | * For a hex string "value" convert to a binary allocated buffer. | |
245 | * Return 1 on success or 0 on failure. | |
246 | */ | |
247 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen) | |
248 | { | |
249 | long len; | |
250 | ||
251 | /* Check for NULL literal */ | |
252 | if (strcmp(value, "NULL") == 0) { | |
253 | *buf = NULL; | |
254 | *buflen = 0; | |
255 | return 1; | |
256 | } | |
257 | ||
258 | /* Check for empty value */ | |
259 | if (*value == '\0') { | |
260 | /* | |
261 | * Don't return NULL for zero length buffer. This is needed for | |
262 | * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key | |
263 | * buffer even if the key length is 0, in order to detect key reset. | |
264 | */ | |
265 | *buf = OPENSSL_malloc(1); | |
266 | if (*buf == NULL) | |
267 | return 0; | |
268 | **buf = 0; | |
269 | *buflen = 0; | |
270 | return 1; | |
271 | } | |
272 | ||
273 | /* Check for string literal */ | |
274 | if (value[0] == '"') { | |
275 | size_t vlen = strlen(++value); | |
276 | ||
277 | if (vlen == 0 || value[vlen - 1] != '"') | |
278 | return 0; | |
279 | vlen--; | |
280 | *buf = unescape(value, vlen, buflen); | |
281 | return *buf == NULL ? 0 : 1; | |
282 | } | |
283 | ||
284 | /* Otherwise assume as hex literal and convert it to binary buffer */ | |
285 | if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) { | |
286 | TEST_info("Can't convert %s", value); | |
287 | TEST_openssl_errors(); | |
288 | return -1; | |
289 | } | |
290 | /* Size of input buffer means we'll never overflow */ | |
291 | *buflen = len; | |
292 | return 1; | |
293 | } | |
294 | ||
295 | ||
296 | /** | |
297 | *** MESSAGE DIGEST TESTS | |
298 | **/ | |
299 | ||
300 | typedef struct digest_data_st { | |
301 | /* Digest this test is for */ | |
302 | const EVP_MD *digest; | |
303 | /* Input to digest */ | |
304 | STACK_OF(EVP_TEST_BUFFER) *input; | |
305 | /* Expected output */ | |
306 | unsigned char *output; | |
307 | size_t output_len; | |
308 | } DIGEST_DATA; | |
309 | ||
310 | static int digest_test_init(EVP_TEST *t, const char *alg) | |
311 | { | |
312 | DIGEST_DATA *mdat; | |
313 | const EVP_MD *digest; | |
314 | ||
315 | if ((digest = EVP_get_digestbyname(alg)) == NULL) { | |
316 | /* If alg has an OID assume disabled algorithm */ | |
317 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
318 | t->skip = 1; | |
319 | return 1; | |
320 | } | |
321 | return 0; | |
322 | } | |
323 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
324 | return 0; | |
325 | t->data = mdat; | |
326 | mdat->digest = digest; | |
327 | return 1; | |
328 | } | |
329 | ||
330 | static void digest_test_cleanup(EVP_TEST *t) | |
331 | { | |
332 | DIGEST_DATA *mdat = t->data; | |
333 | ||
334 | sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free); | |
335 | OPENSSL_free(mdat->output); | |
336 | } | |
337 | ||
338 | static int digest_test_parse(EVP_TEST *t, | |
339 | const char *keyword, const char *value) | |
340 | { | |
341 | DIGEST_DATA *mdata = t->data; | |
342 | ||
343 | if (strcmp(keyword, "Input") == 0) | |
344 | return evp_test_buffer_append(value, &mdata->input); | |
345 | if (strcmp(keyword, "Output") == 0) | |
346 | return parse_bin(value, &mdata->output, &mdata->output_len); | |
347 | if (strcmp(keyword, "Count") == 0) | |
348 | return evp_test_buffer_set_count(value, mdata->input); | |
349 | if (strcmp(keyword, "Ncopy") == 0) | |
350 | return evp_test_buffer_ncopy(value, mdata->input); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen) | |
355 | { | |
356 | return EVP_DigestUpdate(ctx, buf, buflen); | |
357 | } | |
358 | ||
359 | static int digest_test_run(EVP_TEST *t) | |
360 | { | |
361 | DIGEST_DATA *expected = t->data; | |
362 | EVP_MD_CTX *mctx; | |
363 | unsigned char *got = NULL; | |
364 | unsigned int got_len; | |
365 | ||
366 | t->err = "TEST_FAILURE"; | |
367 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) | |
368 | goto err; | |
369 | ||
370 | got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ? | |
371 | expected->output_len : EVP_MAX_MD_SIZE); | |
372 | if (!TEST_ptr(got)) | |
373 | goto err; | |
374 | ||
375 | if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) { | |
376 | t->err = "DIGESTINIT_ERROR"; | |
377 | goto err; | |
378 | } | |
379 | if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) { | |
380 | t->err = "DIGESTUPDATE_ERROR"; | |
381 | goto err; | |
382 | } | |
383 | ||
384 | if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) { | |
385 | got_len = expected->output_len; | |
386 | if (!EVP_DigestFinalXOF(mctx, got, got_len)) { | |
387 | t->err = "DIGESTFINALXOF_ERROR"; | |
388 | goto err; | |
389 | } | |
390 | } else { | |
391 | if (!EVP_DigestFinal(mctx, got, &got_len)) { | |
392 | t->err = "DIGESTFINAL_ERROR"; | |
393 | goto err; | |
394 | } | |
395 | } | |
396 | if (!TEST_int_eq(expected->output_len, got_len)) { | |
397 | t->err = "DIGEST_LENGTH_MISMATCH"; | |
398 | goto err; | |
399 | } | |
400 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
401 | t->err = "DIGEST_MISMATCH"; | |
402 | goto err; | |
403 | } | |
404 | t->err = NULL; | |
405 | ||
406 | err: | |
407 | OPENSSL_free(got); | |
408 | EVP_MD_CTX_free(mctx); | |
409 | return 1; | |
410 | } | |
411 | ||
412 | static const EVP_TEST_METHOD digest_test_method = { | |
413 | "Digest", | |
414 | digest_test_init, | |
415 | digest_test_cleanup, | |
416 | digest_test_parse, | |
417 | digest_test_run | |
418 | }; | |
419 | ||
420 | ||
421 | /** | |
422 | *** CIPHER TESTS | |
423 | **/ | |
424 | ||
425 | typedef struct cipher_data_st { | |
426 | const EVP_CIPHER *cipher; | |
427 | int enc; | |
428 | /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */ | |
429 | int aead; | |
430 | unsigned char *key; | |
431 | size_t key_len; | |
432 | unsigned char *iv; | |
433 | size_t iv_len; | |
434 | unsigned char *plaintext; | |
435 | size_t plaintext_len; | |
436 | unsigned char *ciphertext; | |
437 | size_t ciphertext_len; | |
438 | /* GCM, CCM only */ | |
439 | unsigned char *aad; | |
440 | size_t aad_len; | |
441 | unsigned char *tag; | |
442 | size_t tag_len; | |
443 | } CIPHER_DATA; | |
444 | ||
445 | static int cipher_test_init(EVP_TEST *t, const char *alg) | |
446 | { | |
447 | const EVP_CIPHER *cipher; | |
448 | CIPHER_DATA *cdat; | |
449 | int m; | |
450 | ||
451 | if ((cipher = EVP_get_cipherbyname(alg)) == NULL) { | |
452 | /* If alg has an OID assume disabled algorithm */ | |
453 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
454 | t->skip = 1; | |
455 | return 1; | |
456 | } | |
457 | return 0; | |
458 | } | |
459 | cdat = OPENSSL_zalloc(sizeof(*cdat)); | |
460 | cdat->cipher = cipher; | |
461 | cdat->enc = -1; | |
462 | m = EVP_CIPHER_mode(cipher); | |
463 | if (m == EVP_CIPH_GCM_MODE | |
464 | || m == EVP_CIPH_OCB_MODE | |
465 | || m == EVP_CIPH_CCM_MODE) | |
466 | cdat->aead = EVP_CIPHER_mode(cipher); | |
467 | else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) | |
468 | cdat->aead = -1; | |
469 | else | |
470 | cdat->aead = 0; | |
471 | ||
472 | t->data = cdat; | |
473 | return 1; | |
474 | } | |
475 | ||
476 | static void cipher_test_cleanup(EVP_TEST *t) | |
477 | { | |
478 | CIPHER_DATA *cdat = t->data; | |
479 | ||
480 | OPENSSL_free(cdat->key); | |
481 | OPENSSL_free(cdat->iv); | |
482 | OPENSSL_free(cdat->ciphertext); | |
483 | OPENSSL_free(cdat->plaintext); | |
484 | OPENSSL_free(cdat->aad); | |
485 | OPENSSL_free(cdat->tag); | |
486 | } | |
487 | ||
488 | static int cipher_test_parse(EVP_TEST *t, const char *keyword, | |
489 | const char *value) | |
490 | { | |
491 | CIPHER_DATA *cdat = t->data; | |
492 | ||
493 | if (strcmp(keyword, "Key") == 0) | |
494 | return parse_bin(value, &cdat->key, &cdat->key_len); | |
495 | if (strcmp(keyword, "IV") == 0) | |
496 | return parse_bin(value, &cdat->iv, &cdat->iv_len); | |
497 | if (strcmp(keyword, "Plaintext") == 0) | |
498 | return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len); | |
499 | if (strcmp(keyword, "Ciphertext") == 0) | |
500 | return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len); | |
501 | if (cdat->aead) { | |
502 | if (strcmp(keyword, "AAD") == 0) | |
503 | return parse_bin(value, &cdat->aad, &cdat->aad_len); | |
504 | if (strcmp(keyword, "Tag") == 0) | |
505 | return parse_bin(value, &cdat->tag, &cdat->tag_len); | |
506 | } | |
507 | ||
508 | if (strcmp(keyword, "Operation") == 0) { | |
509 | if (strcmp(value, "ENCRYPT") == 0) | |
510 | cdat->enc = 1; | |
511 | else if (strcmp(value, "DECRYPT") == 0) | |
512 | cdat->enc = 0; | |
513 | else | |
514 | return 0; | |
515 | return 1; | |
516 | } | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static int cipher_test_enc(EVP_TEST *t, int enc, | |
521 | size_t out_misalign, size_t inp_misalign, int frag) | |
522 | { | |
523 | CIPHER_DATA *expected = t->data; | |
524 | unsigned char *in, *expected_out, *tmp = NULL; | |
525 | size_t in_len, out_len, donelen = 0; | |
526 | int ok = 0, tmplen, chunklen, tmpflen; | |
527 | EVP_CIPHER_CTX *ctx = NULL; | |
528 | ||
529 | t->err = "TEST_FAILURE"; | |
530 | if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) | |
531 | goto err; | |
532 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); | |
533 | if (enc) { | |
534 | in = expected->plaintext; | |
535 | in_len = expected->plaintext_len; | |
536 | expected_out = expected->ciphertext; | |
537 | out_len = expected->ciphertext_len; | |
538 | } else { | |
539 | in = expected->ciphertext; | |
540 | in_len = expected->ciphertext_len; | |
541 | expected_out = expected->plaintext; | |
542 | out_len = expected->plaintext_len; | |
543 | } | |
544 | if (inp_misalign == (size_t)-1) { | |
545 | /* | |
546 | * Exercise in-place encryption | |
547 | */ | |
548 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH); | |
549 | if (!tmp) | |
550 | goto err; | |
551 | in = memcpy(tmp + out_misalign, in, in_len); | |
552 | } else { | |
553 | inp_misalign += 16 - ((out_misalign + in_len) & 15); | |
554 | /* | |
555 | * 'tmp' will store both output and copy of input. We make the copy | |
556 | * of input to specifically aligned part of 'tmp'. So we just | |
557 | * figured out how much padding would ensure the required alignment, | |
558 | * now we allocate extended buffer and finally copy the input just | |
559 | * past inp_misalign in expression below. Output will be written | |
560 | * past out_misalign... | |
561 | */ | |
562 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
563 | inp_misalign + in_len); | |
564 | if (!tmp) | |
565 | goto err; | |
566 | in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
567 | inp_misalign, in, in_len); | |
568 | } | |
569 | if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) { | |
570 | t->err = "CIPHERINIT_ERROR"; | |
571 | goto err; | |
572 | } | |
573 | if (expected->iv) { | |
574 | if (expected->aead) { | |
575 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, | |
576 | expected->iv_len, 0)) { | |
577 | t->err = "INVALID_IV_LENGTH"; | |
578 | goto err; | |
579 | } | |
580 | } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) { | |
581 | t->err = "INVALID_IV_LENGTH"; | |
582 | goto err; | |
583 | } | |
584 | } | |
585 | if (expected->aead) { | |
586 | unsigned char *tag; | |
587 | /* | |
588 | * If encrypting or OCB just set tag length initially, otherwise | |
589 | * set tag length and value. | |
590 | */ | |
591 | if (enc || expected->aead == EVP_CIPH_OCB_MODE) { | |
592 | t->err = "TAG_LENGTH_SET_ERROR"; | |
593 | tag = NULL; | |
594 | } else { | |
595 | t->err = "TAG_SET_ERROR"; | |
596 | tag = expected->tag; | |
597 | } | |
598 | if (tag || expected->aead != EVP_CIPH_GCM_MODE) { | |
599 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, | |
600 | expected->tag_len, tag)) | |
601 | goto err; | |
602 | } | |
603 | } | |
604 | ||
605 | if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) { | |
606 | t->err = "INVALID_KEY_LENGTH"; | |
607 | goto err; | |
608 | } | |
609 | if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) { | |
610 | t->err = "KEY_SET_ERROR"; | |
611 | goto err; | |
612 | } | |
613 | ||
614 | if (!enc && expected->aead == EVP_CIPH_OCB_MODE) { | |
615 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, | |
616 | expected->tag_len, expected->tag)) { | |
617 | t->err = "TAG_SET_ERROR"; | |
618 | goto err; | |
619 | } | |
620 | } | |
621 | ||
622 | if (expected->aead == EVP_CIPH_CCM_MODE) { | |
623 | if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) { | |
624 | t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR"; | |
625 | goto err; | |
626 | } | |
627 | } | |
628 | if (expected->aad) { | |
629 | t->err = "AAD_SET_ERROR"; | |
630 | if (!frag) { | |
631 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, | |
632 | expected->aad_len)) | |
633 | goto err; | |
634 | } else { | |
635 | /* | |
636 | * Supply the AAD in chunks less than the block size where possible | |
637 | */ | |
638 | if (expected->aad_len > 0) { | |
639 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad, 1)) | |
640 | goto err; | |
641 | donelen++; | |
642 | } | |
643 | if (expected->aad_len > 2) { | |
644 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, | |
645 | expected->aad + donelen, | |
646 | expected->aad_len - 2)) | |
647 | goto err; | |
648 | donelen += expected->aad_len - 2; | |
649 | } | |
650 | if (expected->aad_len > 1 | |
651 | && !EVP_CipherUpdate(ctx, NULL, &chunklen, | |
652 | expected->aad + donelen, 1)) | |
653 | goto err; | |
654 | } | |
655 | } | |
656 | EVP_CIPHER_CTX_set_padding(ctx, 0); | |
657 | t->err = "CIPHERUPDATE_ERROR"; | |
658 | tmplen = 0; | |
659 | if (!frag) { | |
660 | /* We supply the data all in one go */ | |
661 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len)) | |
662 | goto err; | |
663 | } else { | |
664 | /* Supply the data in chunks less than the block size where possible */ | |
665 | if (in_len > 0) { | |
666 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1)) | |
667 | goto err; | |
668 | tmplen += chunklen; | |
669 | in++; | |
670 | in_len--; | |
671 | } | |
672 | if (in_len > 1) { | |
673 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, | |
674 | in, in_len - 1)) | |
675 | goto err; | |
676 | tmplen += chunklen; | |
677 | in += in_len - 1; | |
678 | in_len = 1; | |
679 | } | |
680 | if (in_len > 0 ) { | |
681 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, | |
682 | in, 1)) | |
683 | goto err; | |
684 | tmplen += chunklen; | |
685 | } | |
686 | } | |
687 | if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) { | |
688 | t->err = "CIPHERFINAL_ERROR"; | |
689 | goto err; | |
690 | } | |
691 | if (!TEST_mem_eq(expected_out, out_len, | |
692 | tmp + out_misalign, tmplen + tmpflen)) { | |
693 | t->err = "VALUE_MISMATCH"; | |
694 | goto err; | |
695 | } | |
696 | if (enc && expected->aead) { | |
697 | unsigned char rtag[16]; | |
698 | ||
699 | if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) { | |
700 | t->err = "TAG_LENGTH_INTERNAL_ERROR"; | |
701 | goto err; | |
702 | } | |
703 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, | |
704 | expected->tag_len, rtag)) { | |
705 | t->err = "TAG_RETRIEVE_ERROR"; | |
706 | goto err; | |
707 | } | |
708 | if (!TEST_mem_eq(expected->tag, expected->tag_len, | |
709 | rtag, expected->tag_len)) { | |
710 | t->err = "TAG_VALUE_MISMATCH"; | |
711 | goto err; | |
712 | } | |
713 | } | |
714 | t->err = NULL; | |
715 | ok = 1; | |
716 | err: | |
717 | OPENSSL_free(tmp); | |
718 | EVP_CIPHER_CTX_free(ctx); | |
719 | return ok; | |
720 | } | |
721 | ||
722 | static int cipher_test_run(EVP_TEST *t) | |
723 | { | |
724 | CIPHER_DATA *cdat = t->data; | |
725 | int rv, frag = 0; | |
726 | size_t out_misalign, inp_misalign; | |
727 | ||
728 | if (!cdat->key) { | |
729 | t->err = "NO_KEY"; | |
730 | return 0; | |
731 | } | |
732 | if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) { | |
733 | /* IV is optional and usually omitted in wrap mode */ | |
734 | if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) { | |
735 | t->err = "NO_IV"; | |
736 | return 0; | |
737 | } | |
738 | } | |
739 | if (cdat->aead && !cdat->tag) { | |
740 | t->err = "NO_TAG"; | |
741 | return 0; | |
742 | } | |
743 | for (out_misalign = 0; out_misalign <= 1;) { | |
744 | static char aux_err[64]; | |
745 | t->aux_err = aux_err; | |
746 | for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) { | |
747 | if (inp_misalign == (size_t)-1) { | |
748 | /* kludge: inp_misalign == -1 means "exercise in-place" */ | |
749 | BIO_snprintf(aux_err, sizeof(aux_err), | |
750 | "%s in-place, %sfragmented", | |
751 | out_misalign ? "misaligned" : "aligned", | |
752 | frag ? "" : "not "); | |
753 | } else { | |
754 | BIO_snprintf(aux_err, sizeof(aux_err), | |
755 | "%s output and %s input, %sfragmented", | |
756 | out_misalign ? "misaligned" : "aligned", | |
757 | inp_misalign ? "misaligned" : "aligned", | |
758 | frag ? "" : "not "); | |
759 | } | |
760 | if (cdat->enc) { | |
761 | rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag); | |
762 | /* Not fatal errors: return */ | |
763 | if (rv != 1) { | |
764 | if (rv < 0) | |
765 | return 0; | |
766 | return 1; | |
767 | } | |
768 | } | |
769 | if (cdat->enc != 1) { | |
770 | rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag); | |
771 | /* Not fatal errors: return */ | |
772 | if (rv != 1) { | |
773 | if (rv < 0) | |
774 | return 0; | |
775 | return 1; | |
776 | } | |
777 | } | |
778 | } | |
779 | ||
780 | if (out_misalign == 1 && frag == 0) { | |
781 | /* | |
782 | * XTS, CCM and Wrap modes have special requirements about input | |
783 | * lengths so we don't fragment for those | |
784 | */ | |
785 | if (cdat->aead == EVP_CIPH_CCM_MODE | |
786 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE | |
787 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) | |
788 | break; | |
789 | out_misalign = 0; | |
790 | frag++; | |
791 | } else { | |
792 | out_misalign++; | |
793 | } | |
794 | } | |
795 | t->aux_err = NULL; | |
796 | ||
797 | return 1; | |
798 | } | |
799 | ||
800 | static const EVP_TEST_METHOD cipher_test_method = { | |
801 | "Cipher", | |
802 | cipher_test_init, | |
803 | cipher_test_cleanup, | |
804 | cipher_test_parse, | |
805 | cipher_test_run | |
806 | }; | |
807 | ||
808 | ||
809 | /** | |
810 | *** MAC TESTS | |
811 | **/ | |
812 | ||
813 | typedef struct mac_data_st { | |
814 | /* MAC type */ | |
815 | int type; | |
816 | /* Algorithm string for this MAC */ | |
817 | char *alg; | |
818 | /* MAC key */ | |
819 | unsigned char *key; | |
820 | size_t key_len; | |
821 | /* Input to MAC */ | |
822 | unsigned char *input; | |
823 | size_t input_len; | |
824 | /* Expected output */ | |
825 | unsigned char *output; | |
826 | size_t output_len; | |
827 | } MAC_DATA; | |
828 | ||
829 | static int mac_test_init(EVP_TEST *t, const char *alg) | |
830 | { | |
831 | int type; | |
832 | MAC_DATA *mdat; | |
833 | ||
834 | if (strcmp(alg, "HMAC") == 0) { | |
835 | type = EVP_PKEY_HMAC; | |
836 | } else if (strcmp(alg, "CMAC") == 0) { | |
837 | #ifndef OPENSSL_NO_CMAC | |
838 | type = EVP_PKEY_CMAC; | |
839 | #else | |
840 | t->skip = 1; | |
841 | return 1; | |
842 | #endif | |
843 | } else if (strcmp(alg, "Poly1305") == 0) { | |
844 | #ifndef OPENSSL_NO_POLY1305 | |
845 | type = EVP_PKEY_POLY1305; | |
846 | #else | |
847 | t->skip = 1; | |
848 | return 1; | |
849 | #endif | |
850 | } else if (strcmp(alg, "SipHash") == 0) { | |
851 | #ifndef OPENSSL_NO_SIPHASH | |
852 | type = EVP_PKEY_SIPHASH; | |
853 | #else | |
854 | t->skip = 1; | |
855 | return 1; | |
856 | #endif | |
857 | } else | |
858 | return 0; | |
859 | ||
860 | mdat = OPENSSL_zalloc(sizeof(*mdat)); | |
861 | mdat->type = type; | |
862 | t->data = mdat; | |
863 | return 1; | |
864 | } | |
865 | ||
866 | static void mac_test_cleanup(EVP_TEST *t) | |
867 | { | |
868 | MAC_DATA *mdat = t->data; | |
869 | ||
870 | OPENSSL_free(mdat->alg); | |
871 | OPENSSL_free(mdat->key); | |
872 | OPENSSL_free(mdat->input); | |
873 | OPENSSL_free(mdat->output); | |
874 | } | |
875 | ||
876 | static int mac_test_parse(EVP_TEST *t, | |
877 | const char *keyword, const char *value) | |
878 | { | |
879 | MAC_DATA *mdata = t->data; | |
880 | ||
881 | if (strcmp(keyword, "Key") == 0) | |
882 | return parse_bin(value, &mdata->key, &mdata->key_len); | |
883 | if (strcmp(keyword, "Algorithm") == 0) { | |
884 | mdata->alg = OPENSSL_strdup(value); | |
885 | if (!mdata->alg) | |
886 | return 0; | |
887 | return 1; | |
888 | } | |
889 | if (strcmp(keyword, "Input") == 0) | |
890 | return parse_bin(value, &mdata->input, &mdata->input_len); | |
891 | if (strcmp(keyword, "Output") == 0) | |
892 | return parse_bin(value, &mdata->output, &mdata->output_len); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | static int mac_test_run(EVP_TEST *t) | |
897 | { | |
898 | MAC_DATA *expected = t->data; | |
899 | EVP_MD_CTX *mctx = NULL; | |
900 | EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; | |
901 | EVP_PKEY *key = NULL; | |
902 | const EVP_MD *md = NULL; | |
903 | unsigned char *got = NULL; | |
904 | size_t got_len; | |
905 | ||
906 | #ifdef OPENSSL_NO_DES | |
907 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { | |
908 | /* Skip DES */ | |
909 | t->err = NULL; | |
910 | goto err; | |
911 | } | |
912 | #endif | |
913 | ||
914 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(expected->type, NULL))) { | |
915 | t->err = "MAC_PKEY_CTX_ERROR"; | |
916 | goto err; | |
917 | } | |
918 | ||
919 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
920 | t->err = "MAC_KEYGEN_INIT_ERROR"; | |
921 | goto err; | |
922 | } | |
923 | if (expected->type == EVP_PKEY_CMAC | |
924 | && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", expected->alg) <= 0) { | |
925 | t->err = "MAC_ALGORITHM_SET_ERROR"; | |
926 | goto err; | |
927 | } | |
928 | ||
929 | if (EVP_PKEY_CTX_set_mac_key(genctx, expected->key, | |
930 | expected->key_len) <= 0) { | |
931 | t->err = "MAC_KEY_SET_ERROR"; | |
932 | goto err; | |
933 | } | |
934 | ||
935 | if (EVP_PKEY_keygen(genctx, &key) <= 0) { | |
936 | t->err = "MAC_KEY_GENERATE_ERROR"; | |
937 | goto err; | |
938 | } | |
939 | if (expected->type == EVP_PKEY_HMAC) { | |
940 | if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) { | |
941 | t->err = "MAC_ALGORITHM_SET_ERROR"; | |
942 | goto err; | |
943 | } | |
944 | } | |
945 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { | |
946 | t->err = "INTERNAL_ERROR"; | |
947 | goto err; | |
948 | } | |
949 | if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) { | |
950 | t->err = "DIGESTSIGNINIT_ERROR"; | |
951 | goto err; | |
952 | } | |
953 | ||
954 | if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { | |
955 | t->err = "DIGESTSIGNUPDATE_ERROR"; | |
956 | goto err; | |
957 | } | |
958 | if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { | |
959 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; | |
960 | goto err; | |
961 | } | |
962 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
963 | t->err = "TEST_FAILURE"; | |
964 | goto err; | |
965 | } | |
966 | if (!EVP_DigestSignFinal(mctx, got, &got_len) | |
967 | || !TEST_mem_eq(expected->output, expected->output_len, | |
968 | got, got_len)) { | |
969 | t->err = "TEST_MAC_ERR"; | |
970 | goto err; | |
971 | } | |
972 | t->err = NULL; | |
973 | err: | |
974 | EVP_MD_CTX_free(mctx); | |
975 | OPENSSL_free(got); | |
976 | EVP_PKEY_CTX_free(genctx); | |
977 | EVP_PKEY_free(key); | |
978 | return 1; | |
979 | } | |
980 | ||
981 | static const EVP_TEST_METHOD mac_test_method = { | |
982 | "MAC", | |
983 | mac_test_init, | |
984 | mac_test_cleanup, | |
985 | mac_test_parse, | |
986 | mac_test_run | |
987 | }; | |
988 | ||
989 | ||
990 | /** | |
991 | *** PUBLIC KEY TESTS | |
992 | *** These are all very similar and share much common code. | |
993 | **/ | |
994 | ||
995 | typedef struct pkey_data_st { | |
996 | /* Context for this operation */ | |
997 | EVP_PKEY_CTX *ctx; | |
998 | /* Key operation to perform */ | |
999 | int (*keyop) (EVP_PKEY_CTX *ctx, | |
1000 | unsigned char *sig, size_t *siglen, | |
1001 | const unsigned char *tbs, size_t tbslen); | |
1002 | /* Input to MAC */ | |
1003 | unsigned char *input; | |
1004 | size_t input_len; | |
1005 | /* Expected output */ | |
1006 | unsigned char *output; | |
1007 | size_t output_len; | |
1008 | } PKEY_DATA; | |
1009 | ||
1010 | /* | |
1011 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1012 | * the appropriate initialisation function | |
1013 | */ | |
1014 | static int pkey_test_init(EVP_TEST *t, const char *name, | |
1015 | int use_public, | |
1016 | int (*keyopinit) (EVP_PKEY_CTX *ctx), | |
1017 | int (*keyop)(EVP_PKEY_CTX *ctx, | |
1018 | unsigned char *sig, size_t *siglen, | |
1019 | const unsigned char *tbs, | |
1020 | size_t tbslen)) | |
1021 | { | |
1022 | PKEY_DATA *kdata; | |
1023 | EVP_PKEY *pkey = NULL; | |
1024 | int rv = 0; | |
1025 | ||
1026 | if (use_public) | |
1027 | rv = find_key(&pkey, name, public_keys); | |
1028 | if (rv == 0) | |
1029 | rv = find_key(&pkey, name, private_keys); | |
1030 | if (rv == 0 || pkey == NULL) { | |
1031 | t->skip = 1; | |
1032 | return 1; | |
1033 | } | |
1034 | ||
1035 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { | |
1036 | EVP_PKEY_free(pkey); | |
1037 | return 0; | |
1038 | } | |
1039 | kdata->keyop = keyop; | |
1040 | if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) { | |
1041 | EVP_PKEY_free(pkey); | |
1042 | OPENSSL_free(kdata); | |
1043 | return 0; | |
1044 | } | |
1045 | if (keyopinit(kdata->ctx) <= 0) | |
1046 | t->err = "KEYOP_INIT_ERROR"; | |
1047 | t->data = kdata; | |
1048 | return 1; | |
1049 | } | |
1050 | ||
1051 | static void pkey_test_cleanup(EVP_TEST *t) | |
1052 | { | |
1053 | PKEY_DATA *kdata = t->data; | |
1054 | ||
1055 | OPENSSL_free(kdata->input); | |
1056 | OPENSSL_free(kdata->output); | |
1057 | EVP_PKEY_CTX_free(kdata->ctx); | |
1058 | } | |
1059 | ||
1060 | static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, | |
1061 | const char *value) | |
1062 | { | |
1063 | int rv; | |
1064 | char *p, *tmpval; | |
1065 | ||
1066 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
1067 | return 0; | |
1068 | p = strchr(tmpval, ':'); | |
1069 | if (p != NULL) | |
1070 | *p++ = '\0'; | |
1071 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); | |
1072 | if (rv == -2) { | |
1073 | t->err = "PKEY_CTRL_INVALID"; | |
1074 | rv = 1; | |
1075 | } else if (p != NULL && rv <= 0) { | |
1076 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
1077 | int nid = OBJ_sn2nid(p); | |
1078 | ||
1079 | if (nid == NID_undef) | |
1080 | nid = OBJ_ln2nid(p); | |
1081 | if (nid != NID_undef | |
1082 | && EVP_get_digestbynid(nid) == NULL | |
1083 | && EVP_get_cipherbynid(nid) == NULL) { | |
1084 | t->skip = 1; | |
1085 | rv = 1; | |
1086 | } else { | |
1087 | t->err = "PKEY_CTRL_ERROR"; | |
1088 | rv = 1; | |
1089 | } | |
1090 | } | |
1091 | OPENSSL_free(tmpval); | |
1092 | return rv > 0; | |
1093 | } | |
1094 | ||
1095 | static int pkey_test_parse(EVP_TEST *t, | |
1096 | const char *keyword, const char *value) | |
1097 | { | |
1098 | PKEY_DATA *kdata = t->data; | |
1099 | if (strcmp(keyword, "Input") == 0) | |
1100 | return parse_bin(value, &kdata->input, &kdata->input_len); | |
1101 | if (strcmp(keyword, "Output") == 0) | |
1102 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
1103 | if (strcmp(keyword, "Ctrl") == 0) | |
1104 | return pkey_test_ctrl(t, kdata->ctx, value); | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | static int pkey_test_run(EVP_TEST *t) | |
1109 | { | |
1110 | PKEY_DATA *expected = t->data; | |
1111 | unsigned char *got = NULL; | |
1112 | size_t got_len; | |
1113 | ||
1114 | if (expected->keyop(expected->ctx, NULL, &got_len, | |
1115 | expected->input, expected->input_len) <= 0 | |
1116 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1117 | t->err = "KEYOP_LENGTH_ERROR"; | |
1118 | goto err; | |
1119 | } | |
1120 | if (expected->keyop(expected->ctx, got, &got_len, | |
1121 | expected->input, expected->input_len) <= 0) { | |
1122 | t->err = "KEYOP_ERROR"; | |
1123 | goto err; | |
1124 | } | |
1125 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
1126 | t->err = "KEYOP_MISMATCH"; | |
1127 | goto err; | |
1128 | } | |
1129 | t->err = NULL; | |
1130 | err: | |
1131 | OPENSSL_free(got); | |
1132 | return 1; | |
1133 | } | |
1134 | ||
1135 | static int sign_test_init(EVP_TEST *t, const char *name) | |
1136 | { | |
1137 | return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); | |
1138 | } | |
1139 | ||
1140 | static const EVP_TEST_METHOD psign_test_method = { | |
1141 | "Sign", | |
1142 | sign_test_init, | |
1143 | pkey_test_cleanup, | |
1144 | pkey_test_parse, | |
1145 | pkey_test_run | |
1146 | }; | |
1147 | ||
1148 | static int verify_recover_test_init(EVP_TEST *t, const char *name) | |
1149 | { | |
1150 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, | |
1151 | EVP_PKEY_verify_recover); | |
1152 | } | |
1153 | ||
1154 | static const EVP_TEST_METHOD pverify_recover_test_method = { | |
1155 | "VerifyRecover", | |
1156 | verify_recover_test_init, | |
1157 | pkey_test_cleanup, | |
1158 | pkey_test_parse, | |
1159 | pkey_test_run | |
1160 | }; | |
1161 | ||
1162 | static int decrypt_test_init(EVP_TEST *t, const char *name) | |
1163 | { | |
1164 | return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, | |
1165 | EVP_PKEY_decrypt); | |
1166 | } | |
1167 | ||
1168 | static const EVP_TEST_METHOD pdecrypt_test_method = { | |
1169 | "Decrypt", | |
1170 | decrypt_test_init, | |
1171 | pkey_test_cleanup, | |
1172 | pkey_test_parse, | |
1173 | pkey_test_run | |
1174 | }; | |
1175 | ||
1176 | static int verify_test_init(EVP_TEST *t, const char *name) | |
1177 | { | |
1178 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); | |
1179 | } | |
1180 | ||
1181 | static int verify_test_run(EVP_TEST *t) | |
1182 | { | |
1183 | PKEY_DATA *kdata = t->data; | |
1184 | ||
1185 | if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, | |
1186 | kdata->input, kdata->input_len) <= 0) | |
1187 | t->err = "VERIFY_ERROR"; | |
1188 | return 1; | |
1189 | } | |
1190 | ||
1191 | static const EVP_TEST_METHOD pverify_test_method = { | |
1192 | "Verify", | |
1193 | verify_test_init, | |
1194 | pkey_test_cleanup, | |
1195 | pkey_test_parse, | |
1196 | verify_test_run | |
1197 | }; | |
1198 | ||
1199 | ||
1200 | static int pderive_test_init(EVP_TEST *t, const char *name) | |
1201 | { | |
1202 | return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); | |
1203 | } | |
1204 | ||
1205 | static int pderive_test_parse(EVP_TEST *t, | |
1206 | const char *keyword, const char *value) | |
1207 | { | |
1208 | PKEY_DATA *kdata = t->data; | |
1209 | ||
1210 | if (strcmp(keyword, "PeerKey") == 0) { | |
1211 | EVP_PKEY *peer; | |
1212 | if (find_key(&peer, value, public_keys) == 0) | |
1213 | return 0; | |
1214 | if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) | |
1215 | return 0; | |
1216 | return 1; | |
1217 | } | |
1218 | if (strcmp(keyword, "SharedSecret") == 0) | |
1219 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
1220 | if (strcmp(keyword, "Ctrl") == 0) | |
1221 | return pkey_test_ctrl(t, kdata->ctx, value); | |
1222 | return 0; | |
1223 | } | |
1224 | ||
1225 | static int pderive_test_run(EVP_TEST *t) | |
1226 | { | |
1227 | PKEY_DATA *expected = t->data; | |
1228 | unsigned char *got = NULL; | |
1229 | size_t got_len; | |
1230 | ||
1231 | got_len = expected->output_len; | |
1232 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1233 | t->err = "DERIVE_ERROR"; | |
1234 | goto err; | |
1235 | } | |
1236 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { | |
1237 | t->err = "DERIVE_ERROR"; | |
1238 | goto err; | |
1239 | } | |
1240 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
1241 | t->err = "SHARED_SECRET_MISMATCH"; | |
1242 | goto err; | |
1243 | } | |
1244 | ||
1245 | t->err = NULL; | |
1246 | err: | |
1247 | OPENSSL_free(got); | |
1248 | return 1; | |
1249 | } | |
1250 | ||
1251 | static const EVP_TEST_METHOD pderive_test_method = { | |
1252 | "Derive", | |
1253 | pderive_test_init, | |
1254 | pkey_test_cleanup, | |
1255 | pderive_test_parse, | |
1256 | pderive_test_run | |
1257 | }; | |
1258 | ||
1259 | ||
1260 | /** | |
1261 | *** PBE TESTS | |
1262 | **/ | |
1263 | ||
1264 | typedef enum pbe_type_enum { | |
1265 | PBE_TYPE_INVALID = 0, | |
1266 | PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 | |
1267 | } PBE_TYPE; | |
1268 | ||
1269 | typedef struct pbe_data_st { | |
1270 | PBE_TYPE pbe_type; | |
1271 | /* scrypt parameters */ | |
1272 | uint64_t N, r, p, maxmem; | |
1273 | /* PKCS#12 parameters */ | |
1274 | int id, iter; | |
1275 | const EVP_MD *md; | |
1276 | /* password */ | |
1277 | unsigned char *pass; | |
1278 | size_t pass_len; | |
1279 | /* salt */ | |
1280 | unsigned char *salt; | |
1281 | size_t salt_len; | |
1282 | /* Expected output */ | |
1283 | unsigned char *key; | |
1284 | size_t key_len; | |
1285 | } PBE_DATA; | |
1286 | ||
1287 | #ifndef OPENSSL_NO_SCRYPT | |
1288 | /* | |
1289 | * Parse unsigned decimal 64 bit integer value | |
1290 | */ | |
1291 | static int parse_uint64(const char *value, uint64_t *pr) | |
1292 | { | |
1293 | const char *p = value; | |
1294 | ||
1295 | if (!TEST_true(*p)) { | |
1296 | TEST_info("Invalid empty integer value"); | |
1297 | return -1; | |
1298 | } | |
1299 | for (*pr = 0; *p; ) { | |
1300 | if (*pr > UINT64_MAX / 10) { | |
1301 | TEST_error("Integer overflow in string %s", value); | |
1302 | return -1; | |
1303 | } | |
1304 | *pr *= 10; | |
1305 | if (!TEST_true(isdigit(*p))) { | |
1306 | TEST_error("Invalid character in string %s", value); | |
1307 | return -1; | |
1308 | } | |
1309 | *pr += *p - '0'; | |
1310 | p++; | |
1311 | } | |
1312 | return 1; | |
1313 | } | |
1314 | ||
1315 | static int scrypt_test_parse(EVP_TEST *t, | |
1316 | const char *keyword, const char *value) | |
1317 | { | |
1318 | PBE_DATA *pdata = t->data; | |
1319 | ||
1320 | if (strcmp(keyword, "N") == 0) | |
1321 | return parse_uint64(value, &pdata->N); | |
1322 | if (strcmp(keyword, "p") == 0) | |
1323 | return parse_uint64(value, &pdata->p); | |
1324 | if (strcmp(keyword, "r") == 0) | |
1325 | return parse_uint64(value, &pdata->r); | |
1326 | if (strcmp(keyword, "maxmem") == 0) | |
1327 | return parse_uint64(value, &pdata->maxmem); | |
1328 | return 0; | |
1329 | } | |
1330 | #endif | |
1331 | ||
1332 | static int pbkdf2_test_parse(EVP_TEST *t, | |
1333 | const char *keyword, const char *value) | |
1334 | { | |
1335 | PBE_DATA *pdata = t->data; | |
1336 | ||
1337 | if (strcmp(keyword, "iter") == 0) { | |
1338 | pdata->iter = atoi(value); | |
1339 | if (pdata->iter <= 0) | |
1340 | return -1; | |
1341 | return 1; | |
1342 | } | |
1343 | if (strcmp(keyword, "MD") == 0) { | |
1344 | pdata->md = EVP_get_digestbyname(value); | |
1345 | if (pdata->md == NULL) | |
1346 | return -1; | |
1347 | return 1; | |
1348 | } | |
1349 | return 0; | |
1350 | } | |
1351 | ||
1352 | static int pkcs12_test_parse(EVP_TEST *t, | |
1353 | const char *keyword, const char *value) | |
1354 | { | |
1355 | PBE_DATA *pdata = t->data; | |
1356 | ||
1357 | if (strcmp(keyword, "id") == 0) { | |
1358 | pdata->id = atoi(value); | |
1359 | if (pdata->id <= 0) | |
1360 | return -1; | |
1361 | return 1; | |
1362 | } | |
1363 | return pbkdf2_test_parse(t, keyword, value); | |
1364 | } | |
1365 | ||
1366 | static int pbe_test_init(EVP_TEST *t, const char *alg) | |
1367 | { | |
1368 | PBE_DATA *pdat; | |
1369 | PBE_TYPE pbe_type = PBE_TYPE_INVALID; | |
1370 | ||
1371 | if (strcmp(alg, "scrypt") == 0) { | |
1372 | #ifndef OPENSSL_NO_SCRYPT | |
1373 | pbe_type = PBE_TYPE_SCRYPT; | |
1374 | #else | |
1375 | t->skip = 1; | |
1376 | return 1; | |
1377 | #endif | |
1378 | } else if (strcmp(alg, "pbkdf2") == 0) { | |
1379 | pbe_type = PBE_TYPE_PBKDF2; | |
1380 | } else if (strcmp(alg, "pkcs12") == 0) { | |
1381 | pbe_type = PBE_TYPE_PKCS12; | |
1382 | } else { | |
1383 | TEST_error("Unknown pbe algorithm %s", alg); | |
1384 | } | |
1385 | pdat = OPENSSL_zalloc(sizeof(*pdat)); | |
1386 | pdat->pbe_type = pbe_type; | |
1387 | t->data = pdat; | |
1388 | return 1; | |
1389 | } | |
1390 | ||
1391 | static void pbe_test_cleanup(EVP_TEST *t) | |
1392 | { | |
1393 | PBE_DATA *pdat = t->data; | |
1394 | ||
1395 | OPENSSL_free(pdat->pass); | |
1396 | OPENSSL_free(pdat->salt); | |
1397 | OPENSSL_free(pdat->key); | |
1398 | } | |
1399 | ||
1400 | static int pbe_test_parse(EVP_TEST *t, | |
1401 | const char *keyword, const char *value) | |
1402 | { | |
1403 | PBE_DATA *pdata = t->data; | |
1404 | ||
1405 | if (strcmp(keyword, "Password") == 0) | |
1406 | return parse_bin(value, &pdata->pass, &pdata->pass_len); | |
1407 | if (strcmp(keyword, "Salt") == 0) | |
1408 | return parse_bin(value, &pdata->salt, &pdata->salt_len); | |
1409 | if (strcmp(keyword, "Key") == 0) | |
1410 | return parse_bin(value, &pdata->key, &pdata->key_len); | |
1411 | if (pdata->pbe_type == PBE_TYPE_PBKDF2) | |
1412 | return pbkdf2_test_parse(t, keyword, value); | |
1413 | else if (pdata->pbe_type == PBE_TYPE_PKCS12) | |
1414 | return pkcs12_test_parse(t, keyword, value); | |
1415 | #ifndef OPENSSL_NO_SCRYPT | |
1416 | else if (pdata->pbe_type == PBE_TYPE_SCRYPT) | |
1417 | return scrypt_test_parse(t, keyword, value); | |
1418 | #endif | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | static int pbe_test_run(EVP_TEST *t) | |
1423 | { | |
1424 | PBE_DATA *expected = t->data; | |
1425 | unsigned char *key; | |
1426 | ||
1427 | if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { | |
1428 | t->err = "INTERNAL_ERROR"; | |
1429 | goto err; | |
1430 | } | |
1431 | if (expected->pbe_type == PBE_TYPE_PBKDF2) { | |
1432 | if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, | |
1433 | expected->salt, expected->salt_len, | |
1434 | expected->iter, expected->md, | |
1435 | expected->key_len, key) == 0) { | |
1436 | t->err = "PBKDF2_ERROR"; | |
1437 | goto err; | |
1438 | } | |
1439 | #ifndef OPENSSL_NO_SCRYPT | |
1440 | } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { | |
1441 | if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, | |
1442 | expected->salt, expected->salt_len, expected->N, | |
1443 | expected->r, expected->p, expected->maxmem, | |
1444 | key, expected->key_len) == 0) { | |
1445 | t->err = "SCRYPT_ERROR"; | |
1446 | goto err; | |
1447 | } | |
1448 | #endif | |
1449 | } else if (expected->pbe_type == PBE_TYPE_PKCS12) { | |
1450 | if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, | |
1451 | expected->salt, expected->salt_len, | |
1452 | expected->id, expected->iter, expected->key_len, | |
1453 | key, expected->md) == 0) { | |
1454 | t->err = "PKCS12_ERROR"; | |
1455 | goto err; | |
1456 | } | |
1457 | } | |
1458 | if (!TEST_mem_eq(expected->key, expected->key_len, | |
1459 | key, expected->key_len)) { | |
1460 | t->err = "KEY_MISMATCH"; | |
1461 | goto err; | |
1462 | } | |
1463 | t->err = NULL; | |
1464 | err: | |
1465 | OPENSSL_free(key); | |
1466 | return 1; | |
1467 | } | |
1468 | ||
1469 | static const EVP_TEST_METHOD pbe_test_method = { | |
1470 | "PBE", | |
1471 | pbe_test_init, | |
1472 | pbe_test_cleanup, | |
1473 | pbe_test_parse, | |
1474 | pbe_test_run | |
1475 | }; | |
1476 | ||
1477 | ||
1478 | /** | |
1479 | *** BASE64 TESTS | |
1480 | **/ | |
1481 | ||
1482 | typedef enum { | |
1483 | BASE64_CANONICAL_ENCODING = 0, | |
1484 | BASE64_VALID_ENCODING = 1, | |
1485 | BASE64_INVALID_ENCODING = 2 | |
1486 | } base64_encoding_type; | |
1487 | ||
1488 | typedef struct encode_data_st { | |
1489 | /* Input to encoding */ | |
1490 | unsigned char *input; | |
1491 | size_t input_len; | |
1492 | /* Expected output */ | |
1493 | unsigned char *output; | |
1494 | size_t output_len; | |
1495 | base64_encoding_type encoding; | |
1496 | } ENCODE_DATA; | |
1497 | ||
1498 | static int encode_test_init(EVP_TEST *t, const char *encoding) | |
1499 | { | |
1500 | ENCODE_DATA *edata; | |
1501 | ||
1502 | if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) | |
1503 | return 0; | |
1504 | if (strcmp(encoding, "canonical") == 0) { | |
1505 | edata->encoding = BASE64_CANONICAL_ENCODING; | |
1506 | } else if (strcmp(encoding, "valid") == 0) { | |
1507 | edata->encoding = BASE64_VALID_ENCODING; | |
1508 | } else if (strcmp(encoding, "invalid") == 0) { | |
1509 | edata->encoding = BASE64_INVALID_ENCODING; | |
1510 | if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) | |
1511 | return 0; | |
1512 | } else { | |
1513 | TEST_error("Bad encoding: %s." | |
1514 | " Should be one of {canonical, valid, invalid}", | |
1515 | encoding); | |
1516 | return 0; | |
1517 | } | |
1518 | t->data = edata; | |
1519 | return 1; | |
1520 | } | |
1521 | ||
1522 | static void encode_test_cleanup(EVP_TEST *t) | |
1523 | { | |
1524 | ENCODE_DATA *edata = t->data; | |
1525 | ||
1526 | OPENSSL_free(edata->input); | |
1527 | OPENSSL_free(edata->output); | |
1528 | memset(edata, 0, sizeof(*edata)); | |
1529 | } | |
1530 | ||
1531 | static int encode_test_parse(EVP_TEST *t, | |
1532 | const char *keyword, const char *value) | |
1533 | { | |
1534 | ENCODE_DATA *edata = t->data; | |
1535 | ||
1536 | if (strcmp(keyword, "Input") == 0) | |
1537 | return parse_bin(value, &edata->input, &edata->input_len); | |
1538 | if (strcmp(keyword, "Output") == 0) | |
1539 | return parse_bin(value, &edata->output, &edata->output_len); | |
1540 | return 0; | |
1541 | } | |
1542 | ||
1543 | static int encode_test_run(EVP_TEST *t) | |
1544 | { | |
1545 | ENCODE_DATA *expected = t->data; | |
1546 | unsigned char *encode_out = NULL, *decode_out = NULL; | |
1547 | int output_len, chunk_len; | |
1548 | EVP_ENCODE_CTX *decode_ctx; | |
1549 | ||
1550 | if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { | |
1551 | t->err = "INTERNAL_ERROR"; | |
1552 | goto err; | |
1553 | } | |
1554 | ||
1555 | if (expected->encoding == BASE64_CANONICAL_ENCODING) { | |
1556 | EVP_ENCODE_CTX *encode_ctx; | |
1557 | ||
1558 | if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) | |
1559 | || !TEST_ptr(encode_out = | |
1560 | OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) | |
1561 | goto err; | |
1562 | ||
1563 | EVP_EncodeInit(encode_ctx); | |
1564 | EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, | |
1565 | expected->input, expected->input_len); | |
1566 | output_len = chunk_len; | |
1567 | ||
1568 | EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); | |
1569 | output_len += chunk_len; | |
1570 | ||
1571 | EVP_ENCODE_CTX_free(encode_ctx); | |
1572 | ||
1573 | if (!TEST_mem_eq(expected->output, expected->output_len, | |
1574 | encode_out, output_len)) { | |
1575 | t->err = "BAD_ENCODING"; | |
1576 | goto err; | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | if (!TEST_ptr(decode_out = | |
1581 | OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) | |
1582 | goto err; | |
1583 | ||
1584 | EVP_DecodeInit(decode_ctx); | |
1585 | if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, | |
1586 | expected->output_len) < 0) { | |
1587 | t->err = "DECODE_ERROR"; | |
1588 | goto err; | |
1589 | } | |
1590 | output_len = chunk_len; | |
1591 | ||
1592 | if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { | |
1593 | t->err = "DECODE_ERROR"; | |
1594 | goto err; | |
1595 | } | |
1596 | output_len += chunk_len; | |
1597 | ||
1598 | if (expected->encoding != BASE64_INVALID_ENCODING | |
1599 | && !TEST_mem_eq(expected->input, expected->input_len, | |
1600 | decode_out, output_len)) { | |
1601 | t->err = "BAD_DECODING"; | |
1602 | goto err; | |
1603 | } | |
1604 | ||
1605 | t->err = NULL; | |
1606 | err: | |
1607 | OPENSSL_free(encode_out); | |
1608 | OPENSSL_free(decode_out); | |
1609 | EVP_ENCODE_CTX_free(decode_ctx); | |
1610 | return 1; | |
1611 | } | |
1612 | ||
1613 | static const EVP_TEST_METHOD encode_test_method = { | |
1614 | "Encoding", | |
1615 | encode_test_init, | |
1616 | encode_test_cleanup, | |
1617 | encode_test_parse, | |
1618 | encode_test_run, | |
1619 | }; | |
1620 | ||
1621 | /** | |
1622 | *** KDF TESTS | |
1623 | **/ | |
1624 | ||
1625 | typedef struct kdf_data_st { | |
1626 | /* Context for this operation */ | |
1627 | EVP_PKEY_CTX *ctx; | |
1628 | /* Expected output */ | |
1629 | unsigned char *output; | |
1630 | size_t output_len; | |
1631 | } KDF_DATA; | |
1632 | ||
1633 | /* | |
1634 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1635 | * the appropriate initialisation function | |
1636 | */ | |
1637 | static int kdf_test_init(EVP_TEST *t, const char *name) | |
1638 | { | |
1639 | KDF_DATA *kdata; | |
1640 | int kdf_nid = OBJ_sn2nid(name); | |
1641 | ||
1642 | if (kdf_nid == NID_undef) | |
1643 | kdf_nid = OBJ_ln2nid(name); | |
1644 | ||
1645 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) | |
1646 | return 0; | |
1647 | kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL); | |
1648 | if (kdata->ctx == NULL) { | |
1649 | OPENSSL_free(kdata); | |
1650 | return 0; | |
1651 | } | |
1652 | if (EVP_PKEY_derive_init(kdata->ctx) <= 0) { | |
1653 | EVP_PKEY_CTX_free(kdata->ctx); | |
1654 | OPENSSL_free(kdata); | |
1655 | return 0; | |
1656 | } | |
1657 | t->data = kdata; | |
1658 | return 1; | |
1659 | } | |
1660 | ||
1661 | static void kdf_test_cleanup(EVP_TEST *t) | |
1662 | { | |
1663 | KDF_DATA *kdata = t->data; | |
1664 | OPENSSL_free(kdata->output); | |
1665 | EVP_PKEY_CTX_free(kdata->ctx); | |
1666 | } | |
1667 | ||
1668 | static int kdf_test_parse(EVP_TEST *t, | |
1669 | const char *keyword, const char *value) | |
1670 | { | |
1671 | KDF_DATA *kdata = t->data; | |
1672 | ||
1673 | if (strcmp(keyword, "Output") == 0) | |
1674 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
1675 | if (strncmp(keyword, "Ctrl", 4) == 0) | |
1676 | return pkey_test_ctrl(t, kdata->ctx, value); | |
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | static int kdf_test_run(EVP_TEST *t) | |
1681 | { | |
1682 | KDF_DATA *expected = t->data; | |
1683 | unsigned char *got = NULL; | |
1684 | size_t got_len = expected->output_len; | |
1685 | ||
1686 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1687 | t->err = "INTERNAL_ERROR"; | |
1688 | goto err; | |
1689 | } | |
1690 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { | |
1691 | t->err = "KDF_DERIVE_ERROR"; | |
1692 | goto err; | |
1693 | } | |
1694 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
1695 | t->err = "KDF_MISMATCH"; | |
1696 | goto err; | |
1697 | } | |
1698 | t->err = NULL; | |
1699 | ||
1700 | err: | |
1701 | OPENSSL_free(got); | |
1702 | return 1; | |
1703 | } | |
1704 | ||
1705 | static const EVP_TEST_METHOD kdf_test_method = { | |
1706 | "KDF", | |
1707 | kdf_test_init, | |
1708 | kdf_test_cleanup, | |
1709 | kdf_test_parse, | |
1710 | kdf_test_run | |
1711 | }; | |
1712 | ||
1713 | ||
1714 | /** | |
1715 | *** KEYPAIR TESTS | |
1716 | **/ | |
1717 | ||
1718 | typedef struct keypair_test_data_st { | |
1719 | EVP_PKEY *privk; | |
1720 | EVP_PKEY *pubk; | |
1721 | } KEYPAIR_TEST_DATA; | |
1722 | ||
1723 | static int keypair_test_init(EVP_TEST *t, const char *pair) | |
1724 | { | |
1725 | KEYPAIR_TEST_DATA *data; | |
1726 | int rv = 0; | |
1727 | EVP_PKEY *pk = NULL, *pubk = NULL; | |
1728 | char *pub, *priv = NULL; | |
1729 | ||
1730 | /* Split private and public names. */ | |
1731 | if (!TEST_ptr(priv = OPENSSL_strdup(pair)) | |
1732 | || !TEST_ptr(pub = strchr(priv, ':'))) { | |
1733 | t->err = "PARSING_ERROR"; | |
1734 | goto end; | |
1735 | } | |
1736 | *pub++ = '\0'; | |
1737 | ||
1738 | if (!TEST_true(find_key(&pk, priv, private_keys))) { | |
1739 | TEST_info("Can't find private key: %s", priv); | |
1740 | t->err = "MISSING_PRIVATE_KEY"; | |
1741 | goto end; | |
1742 | } | |
1743 | if (!TEST_true(find_key(&pubk, pub, public_keys))) { | |
1744 | TEST_info("Can't find public key: %s", pub); | |
1745 | t->err = "MISSING_PUBLIC_KEY"; | |
1746 | goto end; | |
1747 | } | |
1748 | ||
1749 | if (pk == NULL && pubk == NULL) { | |
1750 | /* Both keys are listed but unsupported: skip this test */ | |
1751 | t->skip = 1; | |
1752 | rv = 1; | |
1753 | goto end; | |
1754 | } | |
1755 | ||
1756 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
1757 | goto end; | |
1758 | data->privk = pk; | |
1759 | data->pubk = pubk; | |
1760 | t->data = data; | |
1761 | rv = 1; | |
1762 | t->err = NULL; | |
1763 | ||
1764 | end: | |
1765 | OPENSSL_free(priv); | |
1766 | return rv; | |
1767 | } | |
1768 | ||
1769 | static void keypair_test_cleanup(EVP_TEST *t) | |
1770 | { | |
1771 | OPENSSL_free(t->data); | |
1772 | t->data = NULL; | |
1773 | } | |
1774 | ||
1775 | /* | |
1776 | * For tests that do not accept any custom keywords. | |
1777 | */ | |
1778 | static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) | |
1779 | { | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | static int keypair_test_run(EVP_TEST *t) | |
1784 | { | |
1785 | int rv = 0; | |
1786 | const KEYPAIR_TEST_DATA *pair = t->data; | |
1787 | ||
1788 | if (pair->privk == NULL || pair->pubk == NULL) { | |
1789 | /* | |
1790 | * this can only happen if only one of the keys is not set | |
1791 | * which means that one of them was unsupported while the | |
1792 | * other isn't: hence a key type mismatch. | |
1793 | */ | |
1794 | t->err = "KEYPAIR_TYPE_MISMATCH"; | |
1795 | rv = 1; | |
1796 | goto end; | |
1797 | } | |
1798 | ||
1799 | if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) { | |
1800 | if ( 0 == rv ) { | |
1801 | t->err = "KEYPAIR_MISMATCH"; | |
1802 | } else if ( -1 == rv ) { | |
1803 | t->err = "KEYPAIR_TYPE_MISMATCH"; | |
1804 | } else if ( -2 == rv ) { | |
1805 | t->err = "UNSUPPORTED_KEY_COMPARISON"; | |
1806 | } else { | |
1807 | TEST_error("Unexpected error in key comparison"); | |
1808 | rv = 0; | |
1809 | goto end; | |
1810 | } | |
1811 | rv = 1; | |
1812 | goto end; | |
1813 | } | |
1814 | ||
1815 | rv = 1; | |
1816 | t->err = NULL; | |
1817 | ||
1818 | end: | |
1819 | return rv; | |
1820 | } | |
1821 | ||
1822 | static const EVP_TEST_METHOD keypair_test_method = { | |
1823 | "PrivPubKeyPair", | |
1824 | keypair_test_init, | |
1825 | keypair_test_cleanup, | |
1826 | void_test_parse, | |
1827 | keypair_test_run | |
1828 | }; | |
1829 | ||
1830 | /** | |
1831 | *** KEYGEN TEST | |
1832 | **/ | |
1833 | ||
1834 | typedef struct keygen_test_data_st { | |
1835 | EVP_PKEY_CTX *genctx; /* Keygen context to use */ | |
1836 | char *keyname; /* Key name to store key or NULL */ | |
1837 | } KEYGEN_TEST_DATA; | |
1838 | ||
1839 | static int keygen_test_init(EVP_TEST *t, const char *alg) | |
1840 | { | |
1841 | KEYGEN_TEST_DATA *data; | |
1842 | EVP_PKEY_CTX *genctx; | |
1843 | int nid = OBJ_sn2nid(alg); | |
1844 | ||
1845 | if (nid == NID_undef) { | |
1846 | nid = OBJ_ln2nid(alg); | |
1847 | if (nid == NID_undef) | |
1848 | return 0; | |
1849 | } | |
1850 | ||
1851 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) { | |
1852 | /* assume algorithm disabled */ | |
1853 | t->skip = 1; | |
1854 | return 1; | |
1855 | } | |
1856 | ||
1857 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
1858 | t->err = "KEYGEN_INIT_ERROR"; | |
1859 | goto err; | |
1860 | } | |
1861 | ||
1862 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
1863 | goto err; | |
1864 | data->genctx = genctx; | |
1865 | data->keyname = NULL; | |
1866 | t->data = data; | |
1867 | t->err = NULL; | |
1868 | return 1; | |
1869 | ||
1870 | err: | |
1871 | EVP_PKEY_CTX_free(genctx); | |
1872 | return 0; | |
1873 | } | |
1874 | ||
1875 | static void keygen_test_cleanup(EVP_TEST *t) | |
1876 | { | |
1877 | KEYGEN_TEST_DATA *keygen = t->data; | |
1878 | ||
1879 | EVP_PKEY_CTX_free(keygen->genctx); | |
1880 | OPENSSL_free(keygen->keyname); | |
1881 | OPENSSL_free(t->data); | |
1882 | t->data = NULL; | |
1883 | } | |
1884 | ||
1885 | static int keygen_test_parse(EVP_TEST *t, | |
1886 | const char *keyword, const char *value) | |
1887 | { | |
1888 | KEYGEN_TEST_DATA *keygen = t->data; | |
1889 | ||
1890 | if (strcmp(keyword, "KeyName") == 0) | |
1891 | return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); | |
1892 | if (strcmp(keyword, "Ctrl") == 0) | |
1893 | return pkey_test_ctrl(t, keygen->genctx, value); | |
1894 | return 0; | |
1895 | } | |
1896 | ||
1897 | static int keygen_test_run(EVP_TEST *t) | |
1898 | { | |
1899 | KEYGEN_TEST_DATA *keygen = t->data; | |
1900 | EVP_PKEY *pkey = NULL; | |
1901 | ||
1902 | t->err = NULL; | |
1903 | if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { | |
1904 | t->err = "KEYGEN_GENERATE_ERROR"; | |
1905 | goto err; | |
1906 | } | |
1907 | ||
1908 | if (keygen->keyname != NULL) { | |
1909 | KEY_LIST *key; | |
1910 | ||
1911 | if (find_key(NULL, keygen->keyname, private_keys)) { | |
1912 | TEST_info("Duplicate key %s", keygen->keyname); | |
1913 | goto err; | |
1914 | } | |
1915 | ||
1916 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
1917 | goto err; | |
1918 | key->name = keygen->keyname; | |
1919 | keygen->keyname = NULL; | |
1920 | key->key = pkey; | |
1921 | key->next = private_keys; | |
1922 | private_keys = key; | |
1923 | } else { | |
1924 | EVP_PKEY_free(pkey); | |
1925 | } | |
1926 | ||
1927 | return 1; | |
1928 | ||
1929 | err: | |
1930 | EVP_PKEY_free(pkey); | |
1931 | return 0; | |
1932 | } | |
1933 | ||
1934 | static const EVP_TEST_METHOD keygen_test_method = { | |
1935 | "KeyGen", | |
1936 | keygen_test_init, | |
1937 | keygen_test_cleanup, | |
1938 | keygen_test_parse, | |
1939 | keygen_test_run, | |
1940 | }; | |
1941 | ||
1942 | /** | |
1943 | *** DIGEST SIGN+VERIFY TESTS | |
1944 | **/ | |
1945 | ||
1946 | typedef struct { | |
1947 | int is_verify; /* Set to 1 if verifying */ | |
1948 | int is_oneshot; /* Set to 1 for one shot operation */ | |
1949 | const EVP_MD *md; /* Digest to use */ | |
1950 | EVP_MD_CTX *ctx; /* Digest context */ | |
1951 | EVP_PKEY_CTX *pctx; | |
1952 | STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ | |
1953 | unsigned char *osin; /* Input data if one shot */ | |
1954 | size_t osin_len; /* Input length data if one shot */ | |
1955 | unsigned char *output; /* Expected output */ | |
1956 | size_t output_len; /* Expected output length */ | |
1957 | } DIGESTSIGN_DATA; | |
1958 | ||
1959 | static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, | |
1960 | int is_oneshot) | |
1961 | { | |
1962 | const EVP_MD *md = NULL; | |
1963 | DIGESTSIGN_DATA *mdat; | |
1964 | ||
1965 | if (strcmp(alg, "NULL") != 0) { | |
1966 | if ((md = EVP_get_digestbyname(alg)) == NULL) { | |
1967 | /* If alg has an OID assume disabled algorithm */ | |
1968 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
1969 | t->skip = 1; | |
1970 | return 1; | |
1971 | } | |
1972 | return 0; | |
1973 | } | |
1974 | } | |
1975 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
1976 | return 0; | |
1977 | mdat->md = md; | |
1978 | if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { | |
1979 | OPENSSL_free(mdat); | |
1980 | return 0; | |
1981 | } | |
1982 | mdat->is_verify = is_verify; | |
1983 | mdat->is_oneshot = is_oneshot; | |
1984 | t->data = mdat; | |
1985 | return 1; | |
1986 | } | |
1987 | ||
1988 | static int digestsign_test_init(EVP_TEST *t, const char *alg) | |
1989 | { | |
1990 | return digestsigver_test_init(t, alg, 0, 0); | |
1991 | } | |
1992 | ||
1993 | static void digestsigver_test_cleanup(EVP_TEST *t) | |
1994 | { | |
1995 | DIGESTSIGN_DATA *mdata = t->data; | |
1996 | ||
1997 | EVP_MD_CTX_free(mdata->ctx); | |
1998 | sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); | |
1999 | OPENSSL_free(mdata->osin); | |
2000 | OPENSSL_free(mdata->output); | |
2001 | OPENSSL_free(mdata); | |
2002 | t->data = NULL; | |
2003 | } | |
2004 | ||
2005 | static int digestsigver_test_parse(EVP_TEST *t, | |
2006 | const char *keyword, const char *value) | |
2007 | { | |
2008 | DIGESTSIGN_DATA *mdata = t->data; | |
2009 | ||
2010 | if (strcmp(keyword, "Key") == 0) { | |
2011 | EVP_PKEY *pkey = NULL; | |
2012 | int rv = 0; | |
2013 | ||
2014 | if (mdata->is_verify) | |
2015 | rv = find_key(&pkey, value, public_keys); | |
2016 | if (rv == 0) | |
2017 | rv = find_key(&pkey, value, private_keys); | |
2018 | if (rv == 0 || pkey == NULL) { | |
2019 | t->skip = 1; | |
2020 | return 1; | |
2021 | } | |
2022 | if (mdata->is_verify) { | |
2023 | if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md, | |
2024 | NULL, pkey)) | |
2025 | t->err = "DIGESTVERIFYINIT_ERROR"; | |
2026 | return 1; | |
2027 | } | |
2028 | if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL, | |
2029 | pkey)) | |
2030 | t->err = "DIGESTSIGNINIT_ERROR"; | |
2031 | return 1; | |
2032 | } | |
2033 | ||
2034 | if (strcmp(keyword, "Input") == 0) { | |
2035 | if (mdata->is_oneshot) | |
2036 | return parse_bin(value, &mdata->osin, &mdata->osin_len); | |
2037 | return evp_test_buffer_append(value, &mdata->input); | |
2038 | } | |
2039 | if (strcmp(keyword, "Output") == 0) | |
2040 | return parse_bin(value, &mdata->output, &mdata->output_len); | |
2041 | ||
2042 | if (!mdata->is_oneshot) { | |
2043 | if (strcmp(keyword, "Count") == 0) | |
2044 | return evp_test_buffer_set_count(value, mdata->input); | |
2045 | if (strcmp(keyword, "Ncopy") == 0) | |
2046 | return evp_test_buffer_ncopy(value, mdata->input); | |
2047 | } | |
2048 | if (strcmp(keyword, "Ctrl") == 0) { | |
2049 | if (mdata->pctx == NULL) | |
2050 | return 0; | |
2051 | return pkey_test_ctrl(t, mdata->pctx, value); | |
2052 | } | |
2053 | return 0; | |
2054 | } | |
2055 | ||
2056 | static int digestsign_update_fn(void *ctx, const unsigned char *buf, | |
2057 | size_t buflen) | |
2058 | { | |
2059 | return EVP_DigestSignUpdate(ctx, buf, buflen); | |
2060 | } | |
2061 | ||
2062 | static int digestsign_test_run(EVP_TEST *t) | |
2063 | { | |
2064 | DIGESTSIGN_DATA *expected = t->data; | |
2065 | unsigned char *got = NULL; | |
2066 | size_t got_len; | |
2067 | ||
2068 | if (!evp_test_buffer_do(expected->input, digestsign_update_fn, | |
2069 | expected->ctx)) { | |
2070 | t->err = "DIGESTUPDATE_ERROR"; | |
2071 | goto err; | |
2072 | } | |
2073 | ||
2074 | if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { | |
2075 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; | |
2076 | goto err; | |
2077 | } | |
2078 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
2079 | t->err = "MALLOC_FAILURE"; | |
2080 | goto err; | |
2081 | } | |
2082 | if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { | |
2083 | t->err = "DIGESTSIGNFINAL_ERROR"; | |
2084 | goto err; | |
2085 | } | |
2086 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
2087 | t->err = "SIGNATURE_MISMATCH"; | |
2088 | goto err; | |
2089 | } | |
2090 | ||
2091 | err: | |
2092 | OPENSSL_free(got); | |
2093 | return 1; | |
2094 | } | |
2095 | ||
2096 | static const EVP_TEST_METHOD digestsign_test_method = { | |
2097 | "DigestSign", | |
2098 | digestsign_test_init, | |
2099 | digestsigver_test_cleanup, | |
2100 | digestsigver_test_parse, | |
2101 | digestsign_test_run | |
2102 | }; | |
2103 | ||
2104 | static int digestverify_test_init(EVP_TEST *t, const char *alg) | |
2105 | { | |
2106 | return digestsigver_test_init(t, alg, 1, 0); | |
2107 | } | |
2108 | ||
2109 | static int digestverify_update_fn(void *ctx, const unsigned char *buf, | |
2110 | size_t buflen) | |
2111 | { | |
2112 | return EVP_DigestVerifyUpdate(ctx, buf, buflen); | |
2113 | } | |
2114 | ||
2115 | static int digestverify_test_run(EVP_TEST *t) | |
2116 | { | |
2117 | DIGESTSIGN_DATA *mdata = t->data; | |
2118 | ||
2119 | if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { | |
2120 | t->err = "DIGESTUPDATE_ERROR"; | |
2121 | return 1; | |
2122 | } | |
2123 | ||
2124 | if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, | |
2125 | mdata->output_len) <= 0) | |
2126 | t->err = "VERIFY_ERROR"; | |
2127 | return 1; | |
2128 | } | |
2129 | ||
2130 | static const EVP_TEST_METHOD digestverify_test_method = { | |
2131 | "DigestVerify", | |
2132 | digestverify_test_init, | |
2133 | digestsigver_test_cleanup, | |
2134 | digestsigver_test_parse, | |
2135 | digestverify_test_run | |
2136 | }; | |
2137 | ||
2138 | static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) | |
2139 | { | |
2140 | return digestsigver_test_init(t, alg, 0, 1); | |
2141 | } | |
2142 | ||
2143 | static int oneshot_digestsign_test_run(EVP_TEST *t) | |
2144 | { | |
2145 | DIGESTSIGN_DATA *expected = t->data; | |
2146 | unsigned char *got = NULL; | |
2147 | size_t got_len; | |
2148 | ||
2149 | if (!EVP_DigestSign(expected->ctx, NULL, &got_len, | |
2150 | expected->osin, expected->osin_len)) { | |
2151 | t->err = "DIGESTSIGN_LENGTH_ERROR"; | |
2152 | goto err; | |
2153 | } | |
2154 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
2155 | t->err = "MALLOC_FAILURE"; | |
2156 | goto err; | |
2157 | } | |
2158 | if (!EVP_DigestSign(expected->ctx, got, &got_len, | |
2159 | expected->osin, expected->osin_len)) { | |
2160 | t->err = "DIGESTSIGN_ERROR"; | |
2161 | goto err; | |
2162 | } | |
2163 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
2164 | t->err = "SIGNATURE_MISMATCH"; | |
2165 | goto err; | |
2166 | } | |
2167 | ||
2168 | err: | |
2169 | OPENSSL_free(got); | |
2170 | return 1; | |
2171 | } | |
2172 | ||
2173 | static const EVP_TEST_METHOD oneshot_digestsign_test_method = { | |
2174 | "OneShotDigestSign", | |
2175 | oneshot_digestsign_test_init, | |
2176 | digestsigver_test_cleanup, | |
2177 | digestsigver_test_parse, | |
2178 | oneshot_digestsign_test_run | |
2179 | }; | |
2180 | ||
2181 | static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) | |
2182 | { | |
2183 | return digestsigver_test_init(t, alg, 1, 1); | |
2184 | } | |
2185 | ||
2186 | static int oneshot_digestverify_test_run(EVP_TEST *t) | |
2187 | { | |
2188 | DIGESTSIGN_DATA *mdata = t->data; | |
2189 | ||
2190 | if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, | |
2191 | mdata->osin, mdata->osin_len) <= 0) | |
2192 | t->err = "VERIFY_ERROR"; | |
2193 | return 1; | |
2194 | } | |
2195 | ||
2196 | static const EVP_TEST_METHOD oneshot_digestverify_test_method = { | |
2197 | "OneShotDigestVerify", | |
2198 | oneshot_digestverify_test_init, | |
2199 | digestsigver_test_cleanup, | |
2200 | digestsigver_test_parse, | |
2201 | oneshot_digestverify_test_run | |
2202 | }; | |
2203 | ||
2204 | ||
2205 | /** | |
2206 | *** PARSING AND DISPATCH | |
2207 | **/ | |
2208 | ||
2209 | static const EVP_TEST_METHOD *evp_test_list[] = { | |
2210 | &cipher_test_method, | |
2211 | &digest_test_method, | |
2212 | &digestsign_test_method, | |
2213 | &digestverify_test_method, | |
2214 | &encode_test_method, | |
2215 | &kdf_test_method, | |
2216 | &keypair_test_method, | |
2217 | &keygen_test_method, | |
2218 | &mac_test_method, | |
2219 | &oneshot_digestsign_test_method, | |
2220 | &oneshot_digestverify_test_method, | |
2221 | &pbe_test_method, | |
2222 | &pdecrypt_test_method, | |
2223 | &pderive_test_method, | |
2224 | &psign_test_method, | |
2225 | &pverify_recover_test_method, | |
2226 | &pverify_test_method, | |
2227 | NULL | |
2228 | }; | |
2229 | ||
2230 | static const EVP_TEST_METHOD *find_test(const char *name) | |
2231 | { | |
2232 | const EVP_TEST_METHOD **tt; | |
2233 | ||
2234 | for (tt = evp_test_list; *tt; tt++) { | |
2235 | if (strcmp(name, (*tt)->name) == 0) | |
2236 | return *tt; | |
2237 | } | |
2238 | return NULL; | |
2239 | } | |
2240 | ||
2241 | static void clear_test(EVP_TEST *t) | |
2242 | { | |
2243 | test_clearstanza(&t->s); | |
2244 | ERR_clear_error(); | |
2245 | if (t->data != NULL) { | |
2246 | if (t->meth != NULL) | |
2247 | t->meth->cleanup(t); | |
2248 | OPENSSL_free(t->data); | |
2249 | t->data = NULL; | |
2250 | } | |
2251 | OPENSSL_free(t->expected_err); | |
2252 | t->expected_err = NULL; | |
2253 | OPENSSL_free(t->func); | |
2254 | t->func = NULL; | |
2255 | OPENSSL_free(t->reason); | |
2256 | t->reason = NULL; | |
2257 | ||
2258 | /* Text literal. */ | |
2259 | t->err = NULL; | |
2260 | t->skip = 0; | |
2261 | t->meth = NULL; | |
2262 | } | |
2263 | ||
2264 | /* | |
2265 | * Check for errors in the test structure; return 1 if okay, else 0. | |
2266 | */ | |
2267 | static int check_test_error(EVP_TEST *t) | |
2268 | { | |
2269 | unsigned long err; | |
2270 | const char *func; | |
2271 | const char *reason; | |
2272 | ||
2273 | if (t->err == NULL && t->expected_err == NULL) | |
2274 | return 1; | |
2275 | if (t->err != NULL && t->expected_err == NULL) { | |
2276 | if (t->aux_err != NULL) { | |
2277 | TEST_info("%s:%d: Source of above error (%s); unexpected error %s", | |
2278 | t->s.test_file, t->s.start, t->aux_err, t->err); | |
2279 | } else { | |
2280 | TEST_info("%s:%d: Source of above error; unexpected error %s", | |
2281 | t->s.test_file, t->s.start, t->err); | |
2282 | } | |
2283 | return 0; | |
2284 | } | |
2285 | if (t->err == NULL && t->expected_err != NULL) { | |
2286 | TEST_info("%s:%d: Succeeded but was expecting %s", | |
2287 | t->s.test_file, t->s.start, t->expected_err); | |
2288 | return 0; | |
2289 | } | |
2290 | ||
2291 | if (strcmp(t->err, t->expected_err) != 0) { | |
2292 | TEST_info("%s:%d: Expected %s got %s", | |
2293 | t->s.test_file, t->s.start, t->expected_err, t->err); | |
2294 | return 0; | |
2295 | } | |
2296 | ||
2297 | if (t->func == NULL && t->reason == NULL) | |
2298 | return 1; | |
2299 | ||
2300 | if (t->func == NULL || t->reason == NULL) { | |
2301 | TEST_info("%s:%d: Test is missing function or reason code", | |
2302 | t->s.test_file, t->s.start); | |
2303 | return 0; | |
2304 | } | |
2305 | ||
2306 | err = ERR_peek_error(); | |
2307 | if (err == 0) { | |
2308 | TEST_info("%s:%d: Expected error \"%s:%s\" not set", | |
2309 | t->s.test_file, t->s.start, t->func, t->reason); | |
2310 | return 0; | |
2311 | } | |
2312 | ||
2313 | func = ERR_func_error_string(err); | |
2314 | reason = ERR_reason_error_string(err); | |
2315 | if (func == NULL && reason == NULL) { | |
2316 | TEST_info("%s:%d: Expected error \"%s:%s\", no strings available." | |
2317 | " Assuming ok.", | |
2318 | t->s.test_file, t->s.start, t->func, t->reason); | |
2319 | return 1; | |
2320 | } | |
2321 | ||
2322 | if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0) | |
2323 | return 1; | |
2324 | ||
2325 | TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"", | |
2326 | t->s.test_file, t->s.start, t->func, t->reason, func, reason); | |
2327 | ||
2328 | return 0; | |
2329 | } | |
2330 | ||
2331 | /* | |
2332 | * Run a parsed test. Log a message and return 0 on error. | |
2333 | */ | |
2334 | static int run_test(EVP_TEST *t) | |
2335 | { | |
2336 | if (t->meth == NULL) | |
2337 | return 1; | |
2338 | t->s.numtests++; | |
2339 | if (t->skip) { | |
2340 | t->s.numskip++; | |
2341 | } else { | |
2342 | /* run the test */ | |
2343 | if (t->err == NULL && t->meth->run_test(t) != 1) { | |
2344 | TEST_info("%s:%d %s error", | |
2345 | t->s.test_file, t->s.start, t->meth->name); | |
2346 | return 0; | |
2347 | } | |
2348 | if (!check_test_error(t)) { | |
2349 | TEST_openssl_errors(); | |
2350 | t->s.errors++; | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | /* clean it up */ | |
2355 | return 1; | |
2356 | } | |
2357 | ||
2358 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) | |
2359 | { | |
2360 | for (; lst != NULL; lst = lst->next) { | |
2361 | if (strcmp(lst->name, name) == 0) { | |
2362 | if (ppk != NULL) | |
2363 | *ppk = lst->key; | |
2364 | return 1; | |
2365 | } | |
2366 | } | |
2367 | return 0; | |
2368 | } | |
2369 | ||
2370 | static void free_key_list(KEY_LIST *lst) | |
2371 | { | |
2372 | while (lst != NULL) { | |
2373 | KEY_LIST *next = lst->next; | |
2374 | ||
2375 | EVP_PKEY_free(lst->key); | |
2376 | OPENSSL_free(lst->name); | |
2377 | OPENSSL_free(lst); | |
2378 | lst = next; | |
2379 | } | |
2380 | } | |
2381 | ||
2382 | /* | |
2383 | * Is the key type an unsupported algorithm? | |
2384 | */ | |
2385 | static int key_unsupported() | |
2386 | { | |
2387 | long err = ERR_peek_error(); | |
2388 | ||
2389 | if (ERR_GET_LIB(err) == ERR_LIB_EVP | |
2390 | && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) { | |
2391 | ERR_clear_error(); | |
2392 | return 1; | |
2393 | } | |
2394 | #ifndef OPENSSL_NO_EC | |
2395 | /* | |
2396 | * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an | |
2397 | * hint to an unsupported algorithm/curve (e.g. if binary EC support is | |
2398 | * disabled). | |
2399 | */ | |
2400 | if (ERR_GET_LIB(err) == ERR_LIB_EC | |
2401 | && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) { | |
2402 | ERR_clear_error(); | |
2403 | return 1; | |
2404 | } | |
2405 | #endif /* OPENSSL_NO_EC */ | |
2406 | return 0; | |
2407 | } | |
2408 | ||
2409 | /* | |
2410 | * NULL out the value from |pp| but return it. This "steals" a pointer. | |
2411 | */ | |
2412 | static char *take_value(PAIR *pp) | |
2413 | { | |
2414 | char *p = pp->value; | |
2415 | ||
2416 | pp->value = NULL; | |
2417 | return p; | |
2418 | } | |
2419 | ||
2420 | /* | |
2421 | * Read and parse one test. Return 0 if failure, 1 if okay. | |
2422 | */ | |
2423 | static int parse(EVP_TEST *t) | |
2424 | { | |
2425 | KEY_LIST *key, **klist; | |
2426 | EVP_PKEY *pkey; | |
2427 | PAIR *pp; | |
2428 | int i; | |
2429 | ||
2430 | top: | |
2431 | do { | |
2432 | if (BIO_eof(t->s.fp)) | |
2433 | return EOF; | |
2434 | clear_test(t); | |
2435 | if (!test_readstanza(&t->s)) | |
2436 | return 0; | |
2437 | } while (t->s.numpairs == 0); | |
2438 | pp = &t->s.pairs[0]; | |
2439 | ||
2440 | /* Are we adding a key? */ | |
2441 | klist = NULL; | |
2442 | pkey = NULL; | |
2443 | if (strcmp(pp->key, "PrivateKey") == 0) { | |
2444 | pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL); | |
2445 | if (pkey == NULL && !key_unsupported()) { | |
2446 | TEST_info("Can't read private key %s", pp->value); | |
2447 | TEST_openssl_errors(); | |
2448 | return 0; | |
2449 | } | |
2450 | klist = &private_keys; | |
2451 | } | |
2452 | else if (strcmp(pp->key, "PublicKey") == 0) { | |
2453 | pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL); | |
2454 | if (pkey == NULL && !key_unsupported()) { | |
2455 | TEST_info("Can't read public key %s", pp->value); | |
2456 | TEST_openssl_errors(); | |
2457 | return 0; | |
2458 | } | |
2459 | klist = &public_keys; | |
2460 | } | |
2461 | ||
2462 | /* If we have a key add to list */ | |
2463 | if (klist != NULL) { | |
2464 | if (find_key(NULL, pp->value, *klist)) { | |
2465 | TEST_info("Duplicate key %s", pp->value); | |
2466 | return 0; | |
2467 | } | |
2468 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
2469 | return 0; | |
2470 | key->name = take_value(pp); | |
2471 | key->key = pkey; | |
2472 | key->next = *klist; | |
2473 | *klist = key; | |
2474 | ||
2475 | /* Go back and start a new stanza. */ | |
2476 | if (t->s.numpairs != 1) | |
2477 | TEST_info("Line %d: missing blank line\n", t->s.curr); | |
2478 | goto top; | |
2479 | } | |
2480 | ||
2481 | /* Find the test, based on first keyword. */ | |
2482 | if (!TEST_ptr(t->meth = find_test(pp->key))) | |
2483 | return 0; | |
2484 | if (!t->meth->init(t, pp->value)) { | |
2485 | TEST_error("unknown %s: %s\n", pp->key, pp->value); | |
2486 | return 0; | |
2487 | } | |
2488 | if (t->skip == 1) { | |
2489 | /* TEST_info("skipping %s %s", pp->key, pp->value); */ | |
2490 | return 0; | |
2491 | } | |
2492 | ||
2493 | for (pp++, i = 1; i < t->s.numpairs; pp++, i++) { | |
2494 | if (strcmp(pp->key, "Result") == 0) { | |
2495 | if (t->expected_err != NULL) { | |
2496 | TEST_info("Line %d: multiple result lines", t->s.curr); | |
2497 | return 0; | |
2498 | } | |
2499 | t->expected_err = take_value(pp); | |
2500 | } else if (strcmp(pp->key, "Function") == 0) { | |
2501 | if (t->func != NULL) { | |
2502 | TEST_info("Line %d: multiple function lines\n", t->s.curr); | |
2503 | return 0; | |
2504 | } | |
2505 | t->func = take_value(pp); | |
2506 | } else if (strcmp(pp->key, "Reason") == 0) { | |
2507 | if (t->reason != NULL) { | |
2508 | TEST_info("Line %d: multiple reason lines", t->s.curr); | |
2509 | return 0; | |
2510 | } | |
2511 | t->reason = take_value(pp); | |
2512 | } else { | |
2513 | /* Must be test specific line: try to parse it */ | |
2514 | int rv = t->meth->parse(t, pp->key, pp->value); | |
2515 | ||
2516 | if (rv == 0) { | |
2517 | TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); | |
2518 | return 0; | |
2519 | } | |
2520 | if (rv < 0) { | |
2521 | TEST_info("Line %d: error processing keyword %s\n", | |
2522 | t->s.curr, pp->key); | |
2523 | return 0; | |
2524 | } | |
2525 | } | |
2526 | } | |
2527 | ||
2528 | return 1; | |
2529 | } | |
2530 | ||
2531 | static int run_file_tests(int i) | |
2532 | { | |
2533 | EVP_TEST *t; | |
2534 | const char *testfile = test_get_argument(i); | |
2535 | int c; | |
2536 | ||
2537 | if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) | |
2538 | return 0; | |
2539 | if (!test_start_file(&t->s, testfile)) { | |
2540 | OPENSSL_free(t); | |
2541 | return 0; | |
2542 | } | |
2543 | ||
2544 | while (!BIO_eof(t->s.fp)) { | |
2545 | c = parse(t); | |
2546 | if (t->skip) | |
2547 | continue; | |
2548 | if (c == 0 || !run_test(t)) { | |
2549 | t->s.errors++; | |
2550 | break; | |
2551 | } | |
2552 | } | |
2553 | test_end_file(&t->s); | |
2554 | clear_test(t); | |
2555 | ||
2556 | free_key_list(public_keys); | |
2557 | free_key_list(private_keys); | |
2558 | BIO_free(t->s.key); | |
2559 | c = t->s.errors; | |
2560 | OPENSSL_free(t); | |
2561 | return c == 0; | |
2562 | } | |
2563 | ||
2564 | int setup_tests(void) | |
2565 | { | |
2566 | size_t n = test_get_argument_count(); | |
2567 | ||
2568 | if (n == 0) { | |
2569 | TEST_error("Usage: %s file...", test_get_program_name()); | |
2570 | return 0; | |
2571 | } | |
2572 | ||
2573 | ADD_ALL_TESTS(run_file_tests, n); | |
2574 | return 1; | |
2575 | } |