2 * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved.
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
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"
25 typedef struct evp_test_method_st EVP_TEST_METHOD
;
28 * Structure holding test information
30 typedef struct evp_test_st
{
31 STANZA s
; /* Common test stanza */
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 */
43 * Test method structure
45 struct evp_test_method_st
{
46 /* Name of test as it appears in file */
48 /* Initialise test for "alg" */
49 int (*init
) (EVP_TEST
* t
, const char *alg
);
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
);
60 * Linked list of named keys.
62 typedef struct key_list_st
{
65 struct key_list_st
*next
;
69 * List of public and private keys
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
);
75 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
);
78 * Compare two memory regions for equality, returning zero if they differ.
79 * However, if there is expected to be an error and the actual error
80 * matches then the memory is expected to be different so handle this
81 * case without producing unnecessary test framework output.
83 static int memory_err_compare(EVP_TEST
*t
, const char *err
,
84 const void *expected
, size_t expected_len
,
85 const void *got
, size_t got_len
)
89 if (t
->expected_err
!= NULL
&& strcmp(t
->expected_err
, err
) == 0)
90 r
= !TEST_mem_ne(expected
, expected_len
, got
, got_len
);
92 r
= TEST_mem_eq(expected
, expected_len
, got
, got_len
);
99 * Structure used to hold a list of blocks of memory to test
100 * calls to "update" like functions.
102 struct evp_test_buffer_st
{
109 static void evp_test_buffer_free(EVP_TEST_BUFFER
*db
)
112 OPENSSL_free(db
->buf
);
118 * append buffer to a list
120 static int evp_test_buffer_append(const char *value
,
121 STACK_OF(EVP_TEST_BUFFER
) **sk
)
123 EVP_TEST_BUFFER
*db
= NULL
;
125 if (!TEST_ptr(db
= OPENSSL_malloc(sizeof(*db
))))
128 if (!parse_bin(value
, &db
->buf
, &db
->buflen
))
133 if (*sk
== NULL
&& !TEST_ptr(*sk
= sk_EVP_TEST_BUFFER_new_null()))
135 if (!sk_EVP_TEST_BUFFER_push(*sk
, db
))
141 evp_test_buffer_free(db
);
146 * replace last buffer in list with copies of itself
148 static int evp_test_buffer_ncopy(const char *value
,
149 STACK_OF(EVP_TEST_BUFFER
) *sk
)
152 unsigned char *tbuf
, *p
;
154 int ncopy
= atoi(value
);
159 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
161 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
163 tbuflen
= db
->buflen
* ncopy
;
164 if (!TEST_ptr(tbuf
= OPENSSL_malloc(tbuflen
)))
166 for (i
= 0, p
= tbuf
; i
< ncopy
; i
++, p
+= db
->buflen
)
167 memcpy(p
, db
->buf
, db
->buflen
);
169 OPENSSL_free(db
->buf
);
171 db
->buflen
= tbuflen
;
176 * set repeat count for last buffer in list
178 static int evp_test_buffer_set_count(const char *value
,
179 STACK_OF(EVP_TEST_BUFFER
) *sk
)
182 int count
= atoi(value
);
187 if (sk
== NULL
|| sk_EVP_TEST_BUFFER_num(sk
) == 0)
190 db
= sk_EVP_TEST_BUFFER_value(sk
, sk_EVP_TEST_BUFFER_num(sk
) - 1);
191 if (db
->count_set
!= 0)
194 db
->count
= (size_t)count
;
200 * call "fn" with each element of the list in turn
202 static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER
) *sk
,
204 const unsigned char *buf
,
210 for (i
= 0; i
< sk_EVP_TEST_BUFFER_num(sk
); i
++) {
211 EVP_TEST_BUFFER
*tb
= sk_EVP_TEST_BUFFER_value(sk
, i
);
214 for (j
= 0; j
< tb
->count
; j
++) {
215 if (fn(ctx
, tb
->buf
, tb
->buflen
) <= 0)
223 * Unescape some sequences in string literals (only \n for now).
224 * Return an allocated buffer, set |out_len|. If |input_len|
225 * is zero, get an empty buffer but set length to zero.
227 static unsigned char* unescape(const char *input
, size_t input_len
,
230 unsigned char *ret
, *p
;
233 if (input_len
== 0) {
235 return OPENSSL_zalloc(1);
238 /* Escaping is non-expanding; over-allocate original size for simplicity. */
239 if (!TEST_ptr(ret
= p
= OPENSSL_malloc(input_len
)))
242 for (i
= 0; i
< input_len
; i
++) {
243 if (*input
== '\\') {
244 if (i
== input_len
- 1 || *++input
!= 'n') {
245 TEST_error("Bad escape sequence in file");
265 * For a hex string "value" convert to a binary allocated buffer.
266 * Return 1 on success or 0 on failure.
268 static int parse_bin(const char *value
, unsigned char **buf
, size_t *buflen
)
272 /* Check for NULL literal */
273 if (strcmp(value
, "NULL") == 0) {
279 /* Check for empty value */
280 if (*value
== '\0') {
282 * Don't return NULL for zero length buffer. This is needed for
283 * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key
284 * buffer even if the key length is 0, in order to detect key reset.
286 *buf
= OPENSSL_malloc(1);
294 /* Check for string literal */
295 if (value
[0] == '"') {
296 size_t vlen
= strlen(++value
);
298 if (vlen
== 0 || value
[vlen
- 1] != '"')
301 *buf
= unescape(value
, vlen
, buflen
);
302 return *buf
== NULL
? 0 : 1;
305 /* Otherwise assume as hex literal and convert it to binary buffer */
306 if (!TEST_ptr(*buf
= OPENSSL_hexstr2buf(value
, &len
))) {
307 TEST_info("Can't convert %s", value
);
308 TEST_openssl_errors();
311 /* Size of input buffer means we'll never overflow */
318 *** MESSAGE DIGEST TESTS
321 typedef struct digest_data_st
{
322 /* Digest this test is for */
323 const EVP_MD
*digest
;
324 /* Input to digest */
325 STACK_OF(EVP_TEST_BUFFER
) *input
;
326 /* Expected output */
327 unsigned char *output
;
331 static int digest_test_init(EVP_TEST
*t
, const char *alg
)
334 const EVP_MD
*digest
;
336 if ((digest
= EVP_get_digestbyname(alg
)) == NULL
) {
337 /* If alg has an OID assume disabled algorithm */
338 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
344 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
347 mdat
->digest
= digest
;
351 static void digest_test_cleanup(EVP_TEST
*t
)
353 DIGEST_DATA
*mdat
= t
->data
;
355 sk_EVP_TEST_BUFFER_pop_free(mdat
->input
, evp_test_buffer_free
);
356 OPENSSL_free(mdat
->output
);
359 static int digest_test_parse(EVP_TEST
*t
,
360 const char *keyword
, const char *value
)
362 DIGEST_DATA
*mdata
= t
->data
;
364 if (strcmp(keyword
, "Input") == 0)
365 return evp_test_buffer_append(value
, &mdata
->input
);
366 if (strcmp(keyword
, "Output") == 0)
367 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
368 if (strcmp(keyword
, "Count") == 0)
369 return evp_test_buffer_set_count(value
, mdata
->input
);
370 if (strcmp(keyword
, "Ncopy") == 0)
371 return evp_test_buffer_ncopy(value
, mdata
->input
);
375 static int digest_update_fn(void *ctx
, const unsigned char *buf
, size_t buflen
)
377 return EVP_DigestUpdate(ctx
, buf
, buflen
);
380 static int digest_test_run(EVP_TEST
*t
)
382 DIGEST_DATA
*expected
= t
->data
;
384 unsigned char *got
= NULL
;
385 unsigned int got_len
;
387 t
->err
= "TEST_FAILURE";
388 if (!TEST_ptr(mctx
= EVP_MD_CTX_new()))
391 got
= OPENSSL_malloc(expected
->output_len
> EVP_MAX_MD_SIZE
?
392 expected
->output_len
: EVP_MAX_MD_SIZE
);
396 if (!EVP_DigestInit_ex(mctx
, expected
->digest
, NULL
)) {
397 t
->err
= "DIGESTINIT_ERROR";
400 if (!evp_test_buffer_do(expected
->input
, digest_update_fn
, mctx
)) {
401 t
->err
= "DIGESTUPDATE_ERROR";
405 if (EVP_MD_flags(expected
->digest
) & EVP_MD_FLAG_XOF
) {
406 got_len
= expected
->output_len
;
407 if (!EVP_DigestFinalXOF(mctx
, got
, got_len
)) {
408 t
->err
= "DIGESTFINALXOF_ERROR";
412 if (!EVP_DigestFinal(mctx
, got
, &got_len
)) {
413 t
->err
= "DIGESTFINAL_ERROR";
417 if (!TEST_int_eq(expected
->output_len
, got_len
)) {
418 t
->err
= "DIGEST_LENGTH_MISMATCH";
421 if (!memory_err_compare(t
, "DIGEST_MISMATCH",
422 expected
->output
, expected
->output_len
,
430 EVP_MD_CTX_free(mctx
);
434 static const EVP_TEST_METHOD digest_test_method
= {
447 typedef struct cipher_data_st
{
448 const EVP_CIPHER
*cipher
;
450 /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
456 unsigned char *plaintext
;
457 size_t plaintext_len
;
458 unsigned char *ciphertext
;
459 size_t ciphertext_len
;
460 /* GCM, CCM and OCB only */
468 static int cipher_test_init(EVP_TEST
*t
, const char *alg
)
470 const EVP_CIPHER
*cipher
;
474 if ((cipher
= EVP_get_cipherbyname(alg
)) == NULL
) {
475 /* If alg has an OID assume disabled algorithm */
476 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
482 cdat
= OPENSSL_zalloc(sizeof(*cdat
));
483 cdat
->cipher
= cipher
;
485 m
= EVP_CIPHER_mode(cipher
);
486 if (m
== EVP_CIPH_GCM_MODE
487 || m
== EVP_CIPH_OCB_MODE
488 || m
== EVP_CIPH_CCM_MODE
)
490 else if (EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
)
499 static void cipher_test_cleanup(EVP_TEST
*t
)
501 CIPHER_DATA
*cdat
= t
->data
;
503 OPENSSL_free(cdat
->key
);
504 OPENSSL_free(cdat
->iv
);
505 OPENSSL_free(cdat
->ciphertext
);
506 OPENSSL_free(cdat
->plaintext
);
507 OPENSSL_free(cdat
->aad
);
508 OPENSSL_free(cdat
->tag
);
511 static int cipher_test_parse(EVP_TEST
*t
, const char *keyword
,
514 CIPHER_DATA
*cdat
= t
->data
;
516 if (strcmp(keyword
, "Key") == 0)
517 return parse_bin(value
, &cdat
->key
, &cdat
->key_len
);
518 if (strcmp(keyword
, "IV") == 0)
519 return parse_bin(value
, &cdat
->iv
, &cdat
->iv_len
);
520 if (strcmp(keyword
, "Plaintext") == 0)
521 return parse_bin(value
, &cdat
->plaintext
, &cdat
->plaintext_len
);
522 if (strcmp(keyword
, "Ciphertext") == 0)
523 return parse_bin(value
, &cdat
->ciphertext
, &cdat
->ciphertext_len
);
525 if (strcmp(keyword
, "AAD") == 0)
526 return parse_bin(value
, &cdat
->aad
, &cdat
->aad_len
);
527 if (strcmp(keyword
, "Tag") == 0)
528 return parse_bin(value
, &cdat
->tag
, &cdat
->tag_len
);
529 if (strcmp(keyword
, "SetTagLate") == 0) {
530 if (strcmp(value
, "TRUE") == 0)
532 else if (strcmp(value
, "FALSE") == 0)
540 if (strcmp(keyword
, "Operation") == 0) {
541 if (strcmp(value
, "ENCRYPT") == 0)
543 else if (strcmp(value
, "DECRYPT") == 0)
552 static int cipher_test_enc(EVP_TEST
*t
, int enc
,
553 size_t out_misalign
, size_t inp_misalign
, int frag
)
555 CIPHER_DATA
*expected
= t
->data
;
556 unsigned char *in
, *expected_out
, *tmp
= NULL
;
557 size_t in_len
, out_len
, donelen
= 0;
558 int ok
= 0, tmplen
, chunklen
, tmpflen
;
559 EVP_CIPHER_CTX
*ctx
= NULL
;
561 t
->err
= "TEST_FAILURE";
562 if (!TEST_ptr(ctx
= EVP_CIPHER_CTX_new()))
564 EVP_CIPHER_CTX_set_flags(ctx
, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
);
566 in
= expected
->plaintext
;
567 in_len
= expected
->plaintext_len
;
568 expected_out
= expected
->ciphertext
;
569 out_len
= expected
->ciphertext_len
;
571 in
= expected
->ciphertext
;
572 in_len
= expected
->ciphertext_len
;
573 expected_out
= expected
->plaintext
;
574 out_len
= expected
->plaintext_len
;
576 if (inp_misalign
== (size_t)-1) {
578 * Exercise in-place encryption
580 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
);
583 in
= memcpy(tmp
+ out_misalign
, in
, in_len
);
585 inp_misalign
+= 16 - ((out_misalign
+ in_len
) & 15);
587 * 'tmp' will store both output and copy of input. We make the copy
588 * of input to specifically aligned part of 'tmp'. So we just
589 * figured out how much padding would ensure the required alignment,
590 * now we allocate extended buffer and finally copy the input just
591 * past inp_misalign in expression below. Output will be written
592 * past out_misalign...
594 tmp
= OPENSSL_malloc(out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
595 inp_misalign
+ in_len
);
598 in
= memcpy(tmp
+ out_misalign
+ in_len
+ 2 * EVP_MAX_BLOCK_LENGTH
+
599 inp_misalign
, in
, in_len
);
601 if (!EVP_CipherInit_ex(ctx
, expected
->cipher
, NULL
, NULL
, NULL
, enc
)) {
602 t
->err
= "CIPHERINIT_ERROR";
606 if (expected
->aead
) {
607 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_IVLEN
,
608 expected
->iv_len
, 0)) {
609 t
->err
= "INVALID_IV_LENGTH";
612 } else if (expected
->iv_len
!= (size_t)EVP_CIPHER_CTX_iv_length(ctx
)) {
613 t
->err
= "INVALID_IV_LENGTH";
617 if (expected
->aead
) {
620 * If encrypting or OCB just set tag length initially, otherwise
621 * set tag length and value.
623 if (enc
|| expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
) {
624 t
->err
= "TAG_LENGTH_SET_ERROR";
627 t
->err
= "TAG_SET_ERROR";
630 if (tag
|| expected
->aead
!= EVP_CIPH_GCM_MODE
) {
631 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
632 expected
->tag_len
, tag
))
637 if (!EVP_CIPHER_CTX_set_key_length(ctx
, expected
->key_len
)) {
638 t
->err
= "INVALID_KEY_LENGTH";
641 if (!EVP_CipherInit_ex(ctx
, NULL
, NULL
, expected
->key
, expected
->iv
, -1)) {
642 t
->err
= "KEY_SET_ERROR";
646 if (expected
->aead
== EVP_CIPH_CCM_MODE
) {
647 if (!EVP_CipherUpdate(ctx
, NULL
, &tmplen
, NULL
, out_len
)) {
648 t
->err
= "CCM_PLAINTEXT_LENGTH_SET_ERROR";
653 t
->err
= "AAD_SET_ERROR";
655 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
,
660 * Supply the AAD in chunks less than the block size where possible
662 if (expected
->aad_len
> 0) {
663 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
, expected
->aad
, 1))
667 if (expected
->aad_len
> 2) {
668 if (!EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
669 expected
->aad
+ donelen
,
670 expected
->aad_len
- 2))
672 donelen
+= expected
->aad_len
- 2;
674 if (expected
->aad_len
> 1
675 && !EVP_CipherUpdate(ctx
, NULL
, &chunklen
,
676 expected
->aad
+ donelen
, 1))
681 if (!enc
&& (expected
->aead
== EVP_CIPH_OCB_MODE
|| expected
->tag_late
)) {
682 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_SET_TAG
,
683 expected
->tag_len
, expected
->tag
)) {
684 t
->err
= "TAG_SET_ERROR";
689 EVP_CIPHER_CTX_set_padding(ctx
, 0);
690 t
->err
= "CIPHERUPDATE_ERROR";
693 /* We supply the data all in one go */
694 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &tmplen
, in
, in_len
))
697 /* Supply the data in chunks less than the block size where possible */
699 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
, &chunklen
, in
, 1))
706 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
714 if (!EVP_CipherUpdate(ctx
, tmp
+ out_misalign
+ tmplen
, &chunklen
,
720 if (!EVP_CipherFinal_ex(ctx
, tmp
+ out_misalign
+ tmplen
, &tmpflen
)) {
721 t
->err
= "CIPHERFINAL_ERROR";
724 if (!memory_err_compare(t
, "VALUE_MISMATCH", expected_out
, out_len
,
725 tmp
+ out_misalign
, tmplen
+ tmpflen
))
727 if (enc
&& expected
->aead
) {
728 unsigned char rtag
[16];
730 if (!TEST_size_t_le(expected
->tag_len
, sizeof(rtag
))) {
731 t
->err
= "TAG_LENGTH_INTERNAL_ERROR";
734 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_AEAD_GET_TAG
,
735 expected
->tag_len
, rtag
)) {
736 t
->err
= "TAG_RETRIEVE_ERROR";
739 if (!memory_err_compare(t
, "TAG_VALUE_MISMATCH",
740 expected
->tag
, expected
->tag_len
,
741 rtag
, expected
->tag_len
))
748 EVP_CIPHER_CTX_free(ctx
);
752 static int cipher_test_run(EVP_TEST
*t
)
754 CIPHER_DATA
*cdat
= t
->data
;
756 size_t out_misalign
, inp_misalign
;
762 if (!cdat
->iv
&& EVP_CIPHER_iv_length(cdat
->cipher
)) {
763 /* IV is optional and usually omitted in wrap mode */
764 if (EVP_CIPHER_mode(cdat
->cipher
) != EVP_CIPH_WRAP_MODE
) {
769 if (cdat
->aead
&& !cdat
->tag
) {
773 for (out_misalign
= 0; out_misalign
<= 1;) {
774 static char aux_err
[64];
775 t
->aux_err
= aux_err
;
776 for (inp_misalign
= (size_t)-1; inp_misalign
!= 2; inp_misalign
++) {
777 if (inp_misalign
== (size_t)-1) {
778 /* kludge: inp_misalign == -1 means "exercise in-place" */
779 BIO_snprintf(aux_err
, sizeof(aux_err
),
780 "%s in-place, %sfragmented",
781 out_misalign
? "misaligned" : "aligned",
784 BIO_snprintf(aux_err
, sizeof(aux_err
),
785 "%s output and %s input, %sfragmented",
786 out_misalign
? "misaligned" : "aligned",
787 inp_misalign
? "misaligned" : "aligned",
791 rv
= cipher_test_enc(t
, 1, out_misalign
, inp_misalign
, frag
);
792 /* Not fatal errors: return */
799 if (cdat
->enc
!= 1) {
800 rv
= cipher_test_enc(t
, 0, out_misalign
, inp_misalign
, frag
);
801 /* Not fatal errors: return */
810 if (out_misalign
== 1 && frag
== 0) {
812 * XTS, CCM and Wrap modes have special requirements about input
813 * lengths so we don't fragment for those
815 if (cdat
->aead
== EVP_CIPH_CCM_MODE
816 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_XTS_MODE
817 || EVP_CIPHER_mode(cdat
->cipher
) == EVP_CIPH_WRAP_MODE
)
830 static const EVP_TEST_METHOD cipher_test_method
= {
843 typedef struct mac_data_st
{
846 /* Algorithm string for this MAC */
852 unsigned char *input
;
854 /* Expected output */
855 unsigned char *output
;
857 /* Collection of controls */
858 STACK_OF(OPENSSL_STRING
) *controls
;
861 static int mac_test_init(EVP_TEST
*t
, const char *alg
)
866 if (strcmp(alg
, "HMAC") == 0) {
867 type
= EVP_PKEY_HMAC
;
868 } else if (strcmp(alg
, "CMAC") == 0) {
869 #ifndef OPENSSL_NO_CMAC
870 type
= EVP_PKEY_CMAC
;
875 } else if (strcmp(alg
, "Poly1305") == 0) {
876 #ifndef OPENSSL_NO_POLY1305
877 type
= EVP_PKEY_POLY1305
;
882 } else if (strcmp(alg
, "SipHash") == 0) {
883 #ifndef OPENSSL_NO_SIPHASH
884 type
= EVP_PKEY_SIPHASH
;
892 mdat
= OPENSSL_zalloc(sizeof(*mdat
));
894 mdat
->controls
= sk_OPENSSL_STRING_new_null();
899 /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */
900 static void openssl_free(char *m
)
905 static void mac_test_cleanup(EVP_TEST
*t
)
907 MAC_DATA
*mdat
= t
->data
;
909 sk_OPENSSL_STRING_pop_free(mdat
->controls
, openssl_free
);
910 OPENSSL_free(mdat
->alg
);
911 OPENSSL_free(mdat
->key
);
912 OPENSSL_free(mdat
->input
);
913 OPENSSL_free(mdat
->output
);
916 static int mac_test_parse(EVP_TEST
*t
,
917 const char *keyword
, const char *value
)
919 MAC_DATA
*mdata
= t
->data
;
921 if (strcmp(keyword
, "Key") == 0)
922 return parse_bin(value
, &mdata
->key
, &mdata
->key_len
);
923 if (strcmp(keyword
, "Algorithm") == 0) {
924 mdata
->alg
= OPENSSL_strdup(value
);
929 if (strcmp(keyword
, "Input") == 0)
930 return parse_bin(value
, &mdata
->input
, &mdata
->input_len
);
931 if (strcmp(keyword
, "Output") == 0)
932 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
933 if (strcmp(keyword
, "Ctrl") == 0)
934 return sk_OPENSSL_STRING_push(mdata
->controls
,
935 OPENSSL_strdup(value
)) != 0;
939 static int mac_test_ctrl_pkey(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
945 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
947 p
= strchr(tmpval
, ':');
950 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
952 t
->err
= "PKEY_CTRL_INVALID";
954 t
->err
= "PKEY_CTRL_ERROR";
957 OPENSSL_free(tmpval
);
961 static int mac_test_run(EVP_TEST
*t
)
963 MAC_DATA
*expected
= t
->data
;
964 EVP_MD_CTX
*mctx
= NULL
;
965 EVP_PKEY_CTX
*pctx
= NULL
, *genctx
= NULL
;
966 EVP_PKEY
*key
= NULL
;
967 const EVP_MD
*md
= NULL
;
968 unsigned char *got
= NULL
;
972 #ifdef OPENSSL_NO_DES
973 if (expected
->alg
!= NULL
&& strstr(expected
->alg
, "DES") != NULL
) {
980 if (expected
->type
== EVP_PKEY_CMAC
)
981 key
= EVP_PKEY_new_CMAC_key(NULL
, expected
->key
, expected
->key_len
,
982 EVP_get_cipherbyname(expected
->alg
));
984 key
= EVP_PKEY_new_raw_private_key(expected
->type
, NULL
, expected
->key
,
987 t
->err
= "MAC_KEY_CREATE_ERROR";
991 if (expected
->type
== EVP_PKEY_HMAC
) {
992 if (!TEST_ptr(md
= EVP_get_digestbyname(expected
->alg
))) {
993 t
->err
= "MAC_ALGORITHM_SET_ERROR";
997 if (!TEST_ptr(mctx
= EVP_MD_CTX_new())) {
998 t
->err
= "INTERNAL_ERROR";
1001 if (!EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, key
)) {
1002 t
->err
= "DIGESTSIGNINIT_ERROR";
1005 for (i
= 0; i
< sk_OPENSSL_STRING_num(expected
->controls
); i
++)
1006 if (!mac_test_ctrl_pkey(t
, pctx
,
1007 sk_OPENSSL_STRING_value(expected
->controls
,
1009 t
->err
= "EVPPKEYCTXCTRL_ERROR";
1012 if (!EVP_DigestSignUpdate(mctx
, expected
->input
, expected
->input_len
)) {
1013 t
->err
= "DIGESTSIGNUPDATE_ERROR";
1016 if (!EVP_DigestSignFinal(mctx
, NULL
, &got_len
)) {
1017 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
1020 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1021 t
->err
= "TEST_FAILURE";
1024 if (!EVP_DigestSignFinal(mctx
, got
, &got_len
)
1025 || !memory_err_compare(t
, "TEST_MAC_ERR",
1026 expected
->output
, expected
->output_len
,
1028 t
->err
= "TEST_MAC_ERR";
1033 EVP_MD_CTX_free(mctx
);
1035 EVP_PKEY_CTX_free(genctx
);
1040 static const EVP_TEST_METHOD mac_test_method
= {
1050 *** PUBLIC KEY TESTS
1051 *** These are all very similar and share much common code.
1054 typedef struct pkey_data_st
{
1055 /* Context for this operation */
1057 /* Key operation to perform */
1058 int (*keyop
) (EVP_PKEY_CTX
*ctx
,
1059 unsigned char *sig
, size_t *siglen
,
1060 const unsigned char *tbs
, size_t tbslen
);
1062 unsigned char *input
;
1064 /* Expected output */
1065 unsigned char *output
;
1070 * Perform public key operation setup: lookup key, allocated ctx and call
1071 * the appropriate initialisation function
1073 static int pkey_test_init(EVP_TEST
*t
, const char *name
,
1075 int (*keyopinit
) (EVP_PKEY_CTX
*ctx
),
1076 int (*keyop
)(EVP_PKEY_CTX
*ctx
,
1077 unsigned char *sig
, size_t *siglen
,
1078 const unsigned char *tbs
,
1082 EVP_PKEY
*pkey
= NULL
;
1086 rv
= find_key(&pkey
, name
, public_keys
);
1088 rv
= find_key(&pkey
, name
, private_keys
);
1089 if (rv
== 0 || pkey
== NULL
) {
1094 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
)))) {
1095 EVP_PKEY_free(pkey
);
1098 kdata
->keyop
= keyop
;
1099 if (!TEST_ptr(kdata
->ctx
= EVP_PKEY_CTX_new(pkey
, NULL
))) {
1100 EVP_PKEY_free(pkey
);
1101 OPENSSL_free(kdata
);
1104 if (keyopinit(kdata
->ctx
) <= 0)
1105 t
->err
= "KEYOP_INIT_ERROR";
1110 static void pkey_test_cleanup(EVP_TEST
*t
)
1112 PKEY_DATA
*kdata
= t
->data
;
1114 OPENSSL_free(kdata
->input
);
1115 OPENSSL_free(kdata
->output
);
1116 EVP_PKEY_CTX_free(kdata
->ctx
);
1119 static int pkey_test_ctrl(EVP_TEST
*t
, EVP_PKEY_CTX
*pctx
,
1125 if (!TEST_ptr(tmpval
= OPENSSL_strdup(value
)))
1127 p
= strchr(tmpval
, ':');
1130 rv
= EVP_PKEY_CTX_ctrl_str(pctx
, tmpval
, p
);
1132 t
->err
= "PKEY_CTRL_INVALID";
1134 } else if (p
!= NULL
&& rv
<= 0) {
1135 /* If p has an OID and lookup fails assume disabled algorithm */
1136 int nid
= OBJ_sn2nid(p
);
1138 if (nid
== NID_undef
)
1139 nid
= OBJ_ln2nid(p
);
1140 if (nid
!= NID_undef
1141 && EVP_get_digestbynid(nid
) == NULL
1142 && EVP_get_cipherbynid(nid
) == NULL
) {
1146 t
->err
= "PKEY_CTRL_ERROR";
1150 OPENSSL_free(tmpval
);
1154 static int pkey_test_parse(EVP_TEST
*t
,
1155 const char *keyword
, const char *value
)
1157 PKEY_DATA
*kdata
= t
->data
;
1158 if (strcmp(keyword
, "Input") == 0)
1159 return parse_bin(value
, &kdata
->input
, &kdata
->input_len
);
1160 if (strcmp(keyword
, "Output") == 0)
1161 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1162 if (strcmp(keyword
, "Ctrl") == 0)
1163 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1167 static int pkey_test_run(EVP_TEST
*t
)
1169 PKEY_DATA
*expected
= t
->data
;
1170 unsigned char *got
= NULL
;
1173 if (expected
->keyop(expected
->ctx
, NULL
, &got_len
,
1174 expected
->input
, expected
->input_len
) <= 0
1175 || !TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1176 t
->err
= "KEYOP_LENGTH_ERROR";
1179 if (expected
->keyop(expected
->ctx
, got
, &got_len
,
1180 expected
->input
, expected
->input_len
) <= 0) {
1181 t
->err
= "KEYOP_ERROR";
1184 if (!memory_err_compare(t
, "KEYOP_MISMATCH",
1185 expected
->output
, expected
->output_len
,
1195 static int sign_test_init(EVP_TEST
*t
, const char *name
)
1197 return pkey_test_init(t
, name
, 0, EVP_PKEY_sign_init
, EVP_PKEY_sign
);
1200 static const EVP_TEST_METHOD psign_test_method
= {
1208 static int verify_recover_test_init(EVP_TEST
*t
, const char *name
)
1210 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_recover_init
,
1211 EVP_PKEY_verify_recover
);
1214 static const EVP_TEST_METHOD pverify_recover_test_method
= {
1216 verify_recover_test_init
,
1222 static int decrypt_test_init(EVP_TEST
*t
, const char *name
)
1224 return pkey_test_init(t
, name
, 0, EVP_PKEY_decrypt_init
,
1228 static const EVP_TEST_METHOD pdecrypt_test_method
= {
1236 static int verify_test_init(EVP_TEST
*t
, const char *name
)
1238 return pkey_test_init(t
, name
, 1, EVP_PKEY_verify_init
, 0);
1241 static int verify_test_run(EVP_TEST
*t
)
1243 PKEY_DATA
*kdata
= t
->data
;
1245 if (EVP_PKEY_verify(kdata
->ctx
, kdata
->output
, kdata
->output_len
,
1246 kdata
->input
, kdata
->input_len
) <= 0)
1247 t
->err
= "VERIFY_ERROR";
1251 static const EVP_TEST_METHOD pverify_test_method
= {
1260 static int pderive_test_init(EVP_TEST
*t
, const char *name
)
1262 return pkey_test_init(t
, name
, 0, EVP_PKEY_derive_init
, 0);
1265 static int pderive_test_parse(EVP_TEST
*t
,
1266 const char *keyword
, const char *value
)
1268 PKEY_DATA
*kdata
= t
->data
;
1270 if (strcmp(keyword
, "PeerKey") == 0) {
1272 if (find_key(&peer
, value
, public_keys
) == 0)
1274 if (EVP_PKEY_derive_set_peer(kdata
->ctx
, peer
) <= 0)
1278 if (strcmp(keyword
, "SharedSecret") == 0)
1279 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1280 if (strcmp(keyword
, "Ctrl") == 0)
1281 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1285 static int pderive_test_run(EVP_TEST
*t
)
1287 PKEY_DATA
*expected
= t
->data
;
1288 unsigned char *got
= NULL
;
1291 if (EVP_PKEY_derive(expected
->ctx
, NULL
, &got_len
) <= 0) {
1292 t
->err
= "DERIVE_ERROR";
1295 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1296 t
->err
= "DERIVE_ERROR";
1299 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1300 t
->err
= "DERIVE_ERROR";
1303 if (!memory_err_compare(t
, "SHARED_SECRET_MISMATCH",
1304 expected
->output
, expected
->output_len
,
1314 static const EVP_TEST_METHOD pderive_test_method
= {
1327 typedef enum pbe_type_enum
{
1328 PBE_TYPE_INVALID
= 0,
1329 PBE_TYPE_SCRYPT
, PBE_TYPE_PBKDF2
, PBE_TYPE_PKCS12
1332 typedef struct pbe_data_st
{
1334 /* scrypt parameters */
1335 uint64_t N
, r
, p
, maxmem
;
1336 /* PKCS#12 parameters */
1340 unsigned char *pass
;
1343 unsigned char *salt
;
1345 /* Expected output */
1350 #ifndef OPENSSL_NO_SCRYPT
1352 * Parse unsigned decimal 64 bit integer value
1354 static int parse_uint64(const char *value
, uint64_t *pr
)
1356 const char *p
= value
;
1358 if (!TEST_true(*p
)) {
1359 TEST_info("Invalid empty integer value");
1362 for (*pr
= 0; *p
; ) {
1363 if (*pr
> UINT64_MAX
/ 10) {
1364 TEST_error("Integer overflow in string %s", value
);
1368 if (!TEST_true(isdigit((unsigned char)*p
))) {
1369 TEST_error("Invalid character in string %s", value
);
1378 static int scrypt_test_parse(EVP_TEST
*t
,
1379 const char *keyword
, const char *value
)
1381 PBE_DATA
*pdata
= t
->data
;
1383 if (strcmp(keyword
, "N") == 0)
1384 return parse_uint64(value
, &pdata
->N
);
1385 if (strcmp(keyword
, "p") == 0)
1386 return parse_uint64(value
, &pdata
->p
);
1387 if (strcmp(keyword
, "r") == 0)
1388 return parse_uint64(value
, &pdata
->r
);
1389 if (strcmp(keyword
, "maxmem") == 0)
1390 return parse_uint64(value
, &pdata
->maxmem
);
1395 static int pbkdf2_test_parse(EVP_TEST
*t
,
1396 const char *keyword
, const char *value
)
1398 PBE_DATA
*pdata
= t
->data
;
1400 if (strcmp(keyword
, "iter") == 0) {
1401 pdata
->iter
= atoi(value
);
1402 if (pdata
->iter
<= 0)
1406 if (strcmp(keyword
, "MD") == 0) {
1407 pdata
->md
= EVP_get_digestbyname(value
);
1408 if (pdata
->md
== NULL
)
1415 static int pkcs12_test_parse(EVP_TEST
*t
,
1416 const char *keyword
, const char *value
)
1418 PBE_DATA
*pdata
= t
->data
;
1420 if (strcmp(keyword
, "id") == 0) {
1421 pdata
->id
= atoi(value
);
1426 return pbkdf2_test_parse(t
, keyword
, value
);
1429 static int pbe_test_init(EVP_TEST
*t
, const char *alg
)
1432 PBE_TYPE pbe_type
= PBE_TYPE_INVALID
;
1434 if (strcmp(alg
, "scrypt") == 0) {
1435 #ifndef OPENSSL_NO_SCRYPT
1436 pbe_type
= PBE_TYPE_SCRYPT
;
1441 } else if (strcmp(alg
, "pbkdf2") == 0) {
1442 pbe_type
= PBE_TYPE_PBKDF2
;
1443 } else if (strcmp(alg
, "pkcs12") == 0) {
1444 pbe_type
= PBE_TYPE_PKCS12
;
1446 TEST_error("Unknown pbe algorithm %s", alg
);
1448 pdat
= OPENSSL_zalloc(sizeof(*pdat
));
1449 pdat
->pbe_type
= pbe_type
;
1454 static void pbe_test_cleanup(EVP_TEST
*t
)
1456 PBE_DATA
*pdat
= t
->data
;
1458 OPENSSL_free(pdat
->pass
);
1459 OPENSSL_free(pdat
->salt
);
1460 OPENSSL_free(pdat
->key
);
1463 static int pbe_test_parse(EVP_TEST
*t
,
1464 const char *keyword
, const char *value
)
1466 PBE_DATA
*pdata
= t
->data
;
1468 if (strcmp(keyword
, "Password") == 0)
1469 return parse_bin(value
, &pdata
->pass
, &pdata
->pass_len
);
1470 if (strcmp(keyword
, "Salt") == 0)
1471 return parse_bin(value
, &pdata
->salt
, &pdata
->salt_len
);
1472 if (strcmp(keyword
, "Key") == 0)
1473 return parse_bin(value
, &pdata
->key
, &pdata
->key_len
);
1474 if (pdata
->pbe_type
== PBE_TYPE_PBKDF2
)
1475 return pbkdf2_test_parse(t
, keyword
, value
);
1476 else if (pdata
->pbe_type
== PBE_TYPE_PKCS12
)
1477 return pkcs12_test_parse(t
, keyword
, value
);
1478 #ifndef OPENSSL_NO_SCRYPT
1479 else if (pdata
->pbe_type
== PBE_TYPE_SCRYPT
)
1480 return scrypt_test_parse(t
, keyword
, value
);
1485 static int pbe_test_run(EVP_TEST
*t
)
1487 PBE_DATA
*expected
= t
->data
;
1490 if (!TEST_ptr(key
= OPENSSL_malloc(expected
->key_len
))) {
1491 t
->err
= "INTERNAL_ERROR";
1494 if (expected
->pbe_type
== PBE_TYPE_PBKDF2
) {
1495 if (PKCS5_PBKDF2_HMAC((char *)expected
->pass
, expected
->pass_len
,
1496 expected
->salt
, expected
->salt_len
,
1497 expected
->iter
, expected
->md
,
1498 expected
->key_len
, key
) == 0) {
1499 t
->err
= "PBKDF2_ERROR";
1502 #ifndef OPENSSL_NO_SCRYPT
1503 } else if (expected
->pbe_type
== PBE_TYPE_SCRYPT
) {
1504 if (EVP_PBE_scrypt((const char *)expected
->pass
, expected
->pass_len
,
1505 expected
->salt
, expected
->salt_len
, expected
->N
,
1506 expected
->r
, expected
->p
, expected
->maxmem
,
1507 key
, expected
->key_len
) == 0) {
1508 t
->err
= "SCRYPT_ERROR";
1512 } else if (expected
->pbe_type
== PBE_TYPE_PKCS12
) {
1513 if (PKCS12_key_gen_uni(expected
->pass
, expected
->pass_len
,
1514 expected
->salt
, expected
->salt_len
,
1515 expected
->id
, expected
->iter
, expected
->key_len
,
1516 key
, expected
->md
) == 0) {
1517 t
->err
= "PKCS12_ERROR";
1521 if (!memory_err_compare(t
, "KEY_MISMATCH", expected
->key
, expected
->key_len
,
1522 key
, expected
->key_len
))
1531 static const EVP_TEST_METHOD pbe_test_method
= {
1545 BASE64_CANONICAL_ENCODING
= 0,
1546 BASE64_VALID_ENCODING
= 1,
1547 BASE64_INVALID_ENCODING
= 2
1548 } base64_encoding_type
;
1550 typedef struct encode_data_st
{
1551 /* Input to encoding */
1552 unsigned char *input
;
1554 /* Expected output */
1555 unsigned char *output
;
1557 base64_encoding_type encoding
;
1560 static int encode_test_init(EVP_TEST
*t
, const char *encoding
)
1564 if (!TEST_ptr(edata
= OPENSSL_zalloc(sizeof(*edata
))))
1566 if (strcmp(encoding
, "canonical") == 0) {
1567 edata
->encoding
= BASE64_CANONICAL_ENCODING
;
1568 } else if (strcmp(encoding
, "valid") == 0) {
1569 edata
->encoding
= BASE64_VALID_ENCODING
;
1570 } else if (strcmp(encoding
, "invalid") == 0) {
1571 edata
->encoding
= BASE64_INVALID_ENCODING
;
1572 if (!TEST_ptr(t
->expected_err
= OPENSSL_strdup("DECODE_ERROR")))
1575 TEST_error("Bad encoding: %s."
1576 " Should be one of {canonical, valid, invalid}",
1583 OPENSSL_free(edata
);
1587 static void encode_test_cleanup(EVP_TEST
*t
)
1589 ENCODE_DATA
*edata
= t
->data
;
1591 OPENSSL_free(edata
->input
);
1592 OPENSSL_free(edata
->output
);
1593 memset(edata
, 0, sizeof(*edata
));
1596 static int encode_test_parse(EVP_TEST
*t
,
1597 const char *keyword
, const char *value
)
1599 ENCODE_DATA
*edata
= t
->data
;
1601 if (strcmp(keyword
, "Input") == 0)
1602 return parse_bin(value
, &edata
->input
, &edata
->input_len
);
1603 if (strcmp(keyword
, "Output") == 0)
1604 return parse_bin(value
, &edata
->output
, &edata
->output_len
);
1608 static int encode_test_run(EVP_TEST
*t
)
1610 ENCODE_DATA
*expected
= t
->data
;
1611 unsigned char *encode_out
= NULL
, *decode_out
= NULL
;
1612 int output_len
, chunk_len
;
1613 EVP_ENCODE_CTX
*decode_ctx
= NULL
, *encode_ctx
= NULL
;
1615 if (!TEST_ptr(decode_ctx
= EVP_ENCODE_CTX_new())) {
1616 t
->err
= "INTERNAL_ERROR";
1620 if (expected
->encoding
== BASE64_CANONICAL_ENCODING
) {
1622 if (!TEST_ptr(encode_ctx
= EVP_ENCODE_CTX_new())
1623 || !TEST_ptr(encode_out
=
1624 OPENSSL_malloc(EVP_ENCODE_LENGTH(expected
->input_len
))))
1627 EVP_EncodeInit(encode_ctx
);
1628 if (!TEST_true(EVP_EncodeUpdate(encode_ctx
, encode_out
, &chunk_len
,
1629 expected
->input
, expected
->input_len
)))
1632 output_len
= chunk_len
;
1634 EVP_EncodeFinal(encode_ctx
, encode_out
+ chunk_len
, &chunk_len
);
1635 output_len
+= chunk_len
;
1637 if (!memory_err_compare(t
, "BAD_ENCODING",
1638 expected
->output
, expected
->output_len
,
1639 encode_out
, output_len
))
1643 if (!TEST_ptr(decode_out
=
1644 OPENSSL_malloc(EVP_DECODE_LENGTH(expected
->output_len
))))
1647 EVP_DecodeInit(decode_ctx
);
1648 if (EVP_DecodeUpdate(decode_ctx
, decode_out
, &chunk_len
, expected
->output
,
1649 expected
->output_len
) < 0) {
1650 t
->err
= "DECODE_ERROR";
1653 output_len
= chunk_len
;
1655 if (EVP_DecodeFinal(decode_ctx
, decode_out
+ chunk_len
, &chunk_len
) != 1) {
1656 t
->err
= "DECODE_ERROR";
1659 output_len
+= chunk_len
;
1661 if (expected
->encoding
!= BASE64_INVALID_ENCODING
1662 && !memory_err_compare(t
, "BAD_DECODING",
1663 expected
->input
, expected
->input_len
,
1664 decode_out
, output_len
)) {
1665 t
->err
= "BAD_DECODING";
1671 OPENSSL_free(encode_out
);
1672 OPENSSL_free(decode_out
);
1673 EVP_ENCODE_CTX_free(decode_ctx
);
1674 EVP_ENCODE_CTX_free(encode_ctx
);
1678 static const EVP_TEST_METHOD encode_test_method
= {
1681 encode_test_cleanup
,
1690 typedef struct kdf_data_st
{
1691 /* Context for this operation */
1693 /* Expected output */
1694 unsigned char *output
;
1699 * Perform public key operation setup: lookup key, allocated ctx and call
1700 * the appropriate initialisation function
1702 static int kdf_test_init(EVP_TEST
*t
, const char *name
)
1705 int kdf_nid
= OBJ_sn2nid(name
);
1707 #ifdef OPENSSL_NO_SCRYPT
1708 if (strcmp(name
, "scrypt") == 0) {
1714 if (kdf_nid
== NID_undef
)
1715 kdf_nid
= OBJ_ln2nid(name
);
1717 if (!TEST_ptr(kdata
= OPENSSL_zalloc(sizeof(*kdata
))))
1719 kdata
->ctx
= EVP_PKEY_CTX_new_id(kdf_nid
, NULL
);
1720 if (kdata
->ctx
== NULL
) {
1721 OPENSSL_free(kdata
);
1724 if (EVP_PKEY_derive_init(kdata
->ctx
) <= 0) {
1725 EVP_PKEY_CTX_free(kdata
->ctx
);
1726 OPENSSL_free(kdata
);
1733 static void kdf_test_cleanup(EVP_TEST
*t
)
1735 KDF_DATA
*kdata
= t
->data
;
1736 OPENSSL_free(kdata
->output
);
1737 EVP_PKEY_CTX_free(kdata
->ctx
);
1740 static int kdf_test_parse(EVP_TEST
*t
,
1741 const char *keyword
, const char *value
)
1743 KDF_DATA
*kdata
= t
->data
;
1745 if (strcmp(keyword
, "Output") == 0)
1746 return parse_bin(value
, &kdata
->output
, &kdata
->output_len
);
1747 if (strncmp(keyword
, "Ctrl", 4) == 0)
1748 return pkey_test_ctrl(t
, kdata
->ctx
, value
);
1752 static int kdf_test_run(EVP_TEST
*t
)
1754 KDF_DATA
*expected
= t
->data
;
1755 unsigned char *got
= NULL
;
1756 size_t got_len
= expected
->output_len
;
1758 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
1759 t
->err
= "INTERNAL_ERROR";
1762 if (EVP_PKEY_derive(expected
->ctx
, got
, &got_len
) <= 0) {
1763 t
->err
= "KDF_DERIVE_ERROR";
1766 if (!memory_err_compare(t
, "KDF_MISMATCH",
1767 expected
->output
, expected
->output_len
,
1778 static const EVP_TEST_METHOD kdf_test_method
= {
1791 typedef struct keypair_test_data_st
{
1794 } KEYPAIR_TEST_DATA
;
1796 static int keypair_test_init(EVP_TEST
*t
, const char *pair
)
1798 KEYPAIR_TEST_DATA
*data
;
1800 EVP_PKEY
*pk
= NULL
, *pubk
= NULL
;
1801 char *pub
, *priv
= NULL
;
1803 /* Split private and public names. */
1804 if (!TEST_ptr(priv
= OPENSSL_strdup(pair
))
1805 || !TEST_ptr(pub
= strchr(priv
, ':'))) {
1806 t
->err
= "PARSING_ERROR";
1811 if (!TEST_true(find_key(&pk
, priv
, private_keys
))) {
1812 TEST_info("Can't find private key: %s", priv
);
1813 t
->err
= "MISSING_PRIVATE_KEY";
1816 if (!TEST_true(find_key(&pubk
, pub
, public_keys
))) {
1817 TEST_info("Can't find public key: %s", pub
);
1818 t
->err
= "MISSING_PUBLIC_KEY";
1822 if (pk
== NULL
&& pubk
== NULL
) {
1823 /* Both keys are listed but unsupported: skip this test */
1829 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
1842 static void keypair_test_cleanup(EVP_TEST
*t
)
1844 OPENSSL_free(t
->data
);
1849 * For tests that do not accept any custom keywords.
1851 static int void_test_parse(EVP_TEST
*t
, const char *keyword
, const char *value
)
1856 static int keypair_test_run(EVP_TEST
*t
)
1859 const KEYPAIR_TEST_DATA
*pair
= t
->data
;
1861 if (pair
->privk
== NULL
|| pair
->pubk
== NULL
) {
1863 * this can only happen if only one of the keys is not set
1864 * which means that one of them was unsupported while the
1865 * other isn't: hence a key type mismatch.
1867 t
->err
= "KEYPAIR_TYPE_MISMATCH";
1872 if ((rv
= EVP_PKEY_cmp(pair
->privk
, pair
->pubk
)) != 1 ) {
1874 t
->err
= "KEYPAIR_MISMATCH";
1875 } else if ( -1 == rv
) {
1876 t
->err
= "KEYPAIR_TYPE_MISMATCH";
1877 } else if ( -2 == rv
) {
1878 t
->err
= "UNSUPPORTED_KEY_COMPARISON";
1880 TEST_error("Unexpected error in key comparison");
1895 static const EVP_TEST_METHOD keypair_test_method
= {
1898 keypair_test_cleanup
,
1907 typedef struct keygen_test_data_st
{
1908 EVP_PKEY_CTX
*genctx
; /* Keygen context to use */
1909 char *keyname
; /* Key name to store key or NULL */
1912 static int keygen_test_init(EVP_TEST
*t
, const char *alg
)
1914 KEYGEN_TEST_DATA
*data
;
1915 EVP_PKEY_CTX
*genctx
;
1916 int nid
= OBJ_sn2nid(alg
);
1918 if (nid
== NID_undef
) {
1919 nid
= OBJ_ln2nid(alg
);
1920 if (nid
== NID_undef
)
1924 if (!TEST_ptr(genctx
= EVP_PKEY_CTX_new_id(nid
, NULL
))) {
1925 /* assume algorithm disabled */
1930 if (EVP_PKEY_keygen_init(genctx
) <= 0) {
1931 t
->err
= "KEYGEN_INIT_ERROR";
1935 if (!TEST_ptr(data
= OPENSSL_malloc(sizeof(*data
))))
1937 data
->genctx
= genctx
;
1938 data
->keyname
= NULL
;
1944 EVP_PKEY_CTX_free(genctx
);
1948 static void keygen_test_cleanup(EVP_TEST
*t
)
1950 KEYGEN_TEST_DATA
*keygen
= t
->data
;
1952 EVP_PKEY_CTX_free(keygen
->genctx
);
1953 OPENSSL_free(keygen
->keyname
);
1954 OPENSSL_free(t
->data
);
1958 static int keygen_test_parse(EVP_TEST
*t
,
1959 const char *keyword
, const char *value
)
1961 KEYGEN_TEST_DATA
*keygen
= t
->data
;
1963 if (strcmp(keyword
, "KeyName") == 0)
1964 return TEST_ptr(keygen
->keyname
= OPENSSL_strdup(value
));
1965 if (strcmp(keyword
, "Ctrl") == 0)
1966 return pkey_test_ctrl(t
, keygen
->genctx
, value
);
1970 static int keygen_test_run(EVP_TEST
*t
)
1972 KEYGEN_TEST_DATA
*keygen
= t
->data
;
1973 EVP_PKEY
*pkey
= NULL
;
1976 if (EVP_PKEY_keygen(keygen
->genctx
, &pkey
) <= 0) {
1977 t
->err
= "KEYGEN_GENERATE_ERROR";
1981 if (keygen
->keyname
!= NULL
) {
1984 if (find_key(NULL
, keygen
->keyname
, private_keys
)) {
1985 TEST_info("Duplicate key %s", keygen
->keyname
);
1989 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
1991 key
->name
= keygen
->keyname
;
1992 keygen
->keyname
= NULL
;
1994 key
->next
= private_keys
;
1997 EVP_PKEY_free(pkey
);
2003 EVP_PKEY_free(pkey
);
2007 static const EVP_TEST_METHOD keygen_test_method
= {
2010 keygen_test_cleanup
,
2016 *** DIGEST SIGN+VERIFY TESTS
2020 int is_verify
; /* Set to 1 if verifying */
2021 int is_oneshot
; /* Set to 1 for one shot operation */
2022 const EVP_MD
*md
; /* Digest to use */
2023 EVP_MD_CTX
*ctx
; /* Digest context */
2025 STACK_OF(EVP_TEST_BUFFER
) *input
; /* Input data: streaming */
2026 unsigned char *osin
; /* Input data if one shot */
2027 size_t osin_len
; /* Input length data if one shot */
2028 unsigned char *output
; /* Expected output */
2029 size_t output_len
; /* Expected output length */
2032 static int digestsigver_test_init(EVP_TEST
*t
, const char *alg
, int is_verify
,
2035 const EVP_MD
*md
= NULL
;
2036 DIGESTSIGN_DATA
*mdat
;
2038 if (strcmp(alg
, "NULL") != 0) {
2039 if ((md
= EVP_get_digestbyname(alg
)) == NULL
) {
2040 /* If alg has an OID assume disabled algorithm */
2041 if (OBJ_sn2nid(alg
) != NID_undef
|| OBJ_ln2nid(alg
) != NID_undef
) {
2048 if (!TEST_ptr(mdat
= OPENSSL_zalloc(sizeof(*mdat
))))
2051 if (!TEST_ptr(mdat
->ctx
= EVP_MD_CTX_new())) {
2055 mdat
->is_verify
= is_verify
;
2056 mdat
->is_oneshot
= is_oneshot
;
2061 static int digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2063 return digestsigver_test_init(t
, alg
, 0, 0);
2066 static void digestsigver_test_cleanup(EVP_TEST
*t
)
2068 DIGESTSIGN_DATA
*mdata
= t
->data
;
2070 EVP_MD_CTX_free(mdata
->ctx
);
2071 sk_EVP_TEST_BUFFER_pop_free(mdata
->input
, evp_test_buffer_free
);
2072 OPENSSL_free(mdata
->osin
);
2073 OPENSSL_free(mdata
->output
);
2074 OPENSSL_free(mdata
);
2078 static int digestsigver_test_parse(EVP_TEST
*t
,
2079 const char *keyword
, const char *value
)
2081 DIGESTSIGN_DATA
*mdata
= t
->data
;
2083 if (strcmp(keyword
, "Key") == 0) {
2084 EVP_PKEY
*pkey
= NULL
;
2087 if (mdata
->is_verify
)
2088 rv
= find_key(&pkey
, value
, public_keys
);
2090 rv
= find_key(&pkey
, value
, private_keys
);
2091 if (rv
== 0 || pkey
== NULL
) {
2095 if (mdata
->is_verify
) {
2096 if (!EVP_DigestVerifyInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
,
2098 t
->err
= "DIGESTVERIFYINIT_ERROR";
2101 if (!EVP_DigestSignInit(mdata
->ctx
, &mdata
->pctx
, mdata
->md
, NULL
,
2103 t
->err
= "DIGESTSIGNINIT_ERROR";
2107 if (strcmp(keyword
, "Input") == 0) {
2108 if (mdata
->is_oneshot
)
2109 return parse_bin(value
, &mdata
->osin
, &mdata
->osin_len
);
2110 return evp_test_buffer_append(value
, &mdata
->input
);
2112 if (strcmp(keyword
, "Output") == 0)
2113 return parse_bin(value
, &mdata
->output
, &mdata
->output_len
);
2115 if (!mdata
->is_oneshot
) {
2116 if (strcmp(keyword
, "Count") == 0)
2117 return evp_test_buffer_set_count(value
, mdata
->input
);
2118 if (strcmp(keyword
, "Ncopy") == 0)
2119 return evp_test_buffer_ncopy(value
, mdata
->input
);
2121 if (strcmp(keyword
, "Ctrl") == 0) {
2122 if (mdata
->pctx
== NULL
)
2124 return pkey_test_ctrl(t
, mdata
->pctx
, value
);
2129 static int digestsign_update_fn(void *ctx
, const unsigned char *buf
,
2132 return EVP_DigestSignUpdate(ctx
, buf
, buflen
);
2135 static int digestsign_test_run(EVP_TEST
*t
)
2137 DIGESTSIGN_DATA
*expected
= t
->data
;
2138 unsigned char *got
= NULL
;
2141 if (!evp_test_buffer_do(expected
->input
, digestsign_update_fn
,
2143 t
->err
= "DIGESTUPDATE_ERROR";
2147 if (!EVP_DigestSignFinal(expected
->ctx
, NULL
, &got_len
)) {
2148 t
->err
= "DIGESTSIGNFINAL_LENGTH_ERROR";
2151 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2152 t
->err
= "MALLOC_FAILURE";
2155 if (!EVP_DigestSignFinal(expected
->ctx
, got
, &got_len
)) {
2156 t
->err
= "DIGESTSIGNFINAL_ERROR";
2159 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2160 expected
->output
, expected
->output_len
,
2170 static const EVP_TEST_METHOD digestsign_test_method
= {
2172 digestsign_test_init
,
2173 digestsigver_test_cleanup
,
2174 digestsigver_test_parse
,
2178 static int digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2180 return digestsigver_test_init(t
, alg
, 1, 0);
2183 static int digestverify_update_fn(void *ctx
, const unsigned char *buf
,
2186 return EVP_DigestVerifyUpdate(ctx
, buf
, buflen
);
2189 static int digestverify_test_run(EVP_TEST
*t
)
2191 DIGESTSIGN_DATA
*mdata
= t
->data
;
2193 if (!evp_test_buffer_do(mdata
->input
, digestverify_update_fn
, mdata
->ctx
)) {
2194 t
->err
= "DIGESTUPDATE_ERROR";
2198 if (EVP_DigestVerifyFinal(mdata
->ctx
, mdata
->output
,
2199 mdata
->output_len
) <= 0)
2200 t
->err
= "VERIFY_ERROR";
2204 static const EVP_TEST_METHOD digestverify_test_method
= {
2206 digestverify_test_init
,
2207 digestsigver_test_cleanup
,
2208 digestsigver_test_parse
,
2209 digestverify_test_run
2212 static int oneshot_digestsign_test_init(EVP_TEST
*t
, const char *alg
)
2214 return digestsigver_test_init(t
, alg
, 0, 1);
2217 static int oneshot_digestsign_test_run(EVP_TEST
*t
)
2219 DIGESTSIGN_DATA
*expected
= t
->data
;
2220 unsigned char *got
= NULL
;
2223 if (!EVP_DigestSign(expected
->ctx
, NULL
, &got_len
,
2224 expected
->osin
, expected
->osin_len
)) {
2225 t
->err
= "DIGESTSIGN_LENGTH_ERROR";
2228 if (!TEST_ptr(got
= OPENSSL_malloc(got_len
))) {
2229 t
->err
= "MALLOC_FAILURE";
2232 if (!EVP_DigestSign(expected
->ctx
, got
, &got_len
,
2233 expected
->osin
, expected
->osin_len
)) {
2234 t
->err
= "DIGESTSIGN_ERROR";
2237 if (!memory_err_compare(t
, "SIGNATURE_MISMATCH",
2238 expected
->output
, expected
->output_len
,
2248 static const EVP_TEST_METHOD oneshot_digestsign_test_method
= {
2249 "OneShotDigestSign",
2250 oneshot_digestsign_test_init
,
2251 digestsigver_test_cleanup
,
2252 digestsigver_test_parse
,
2253 oneshot_digestsign_test_run
2256 static int oneshot_digestverify_test_init(EVP_TEST
*t
, const char *alg
)
2258 return digestsigver_test_init(t
, alg
, 1, 1);
2261 static int oneshot_digestverify_test_run(EVP_TEST
*t
)
2263 DIGESTSIGN_DATA
*mdata
= t
->data
;
2265 if (EVP_DigestVerify(mdata
->ctx
, mdata
->output
, mdata
->output_len
,
2266 mdata
->osin
, mdata
->osin_len
) <= 0)
2267 t
->err
= "VERIFY_ERROR";
2271 static const EVP_TEST_METHOD oneshot_digestverify_test_method
= {
2272 "OneShotDigestVerify",
2273 oneshot_digestverify_test_init
,
2274 digestsigver_test_cleanup
,
2275 digestsigver_test_parse
,
2276 oneshot_digestverify_test_run
2281 *** PARSING AND DISPATCH
2284 static const EVP_TEST_METHOD
*evp_test_list
[] = {
2285 &cipher_test_method
,
2286 &digest_test_method
,
2287 &digestsign_test_method
,
2288 &digestverify_test_method
,
2289 &encode_test_method
,
2291 &keypair_test_method
,
2292 &keygen_test_method
,
2294 &oneshot_digestsign_test_method
,
2295 &oneshot_digestverify_test_method
,
2297 &pdecrypt_test_method
,
2298 &pderive_test_method
,
2300 &pverify_recover_test_method
,
2301 &pverify_test_method
,
2305 static const EVP_TEST_METHOD
*find_test(const char *name
)
2307 const EVP_TEST_METHOD
**tt
;
2309 for (tt
= evp_test_list
; *tt
; tt
++) {
2310 if (strcmp(name
, (*tt
)->name
) == 0)
2316 static void clear_test(EVP_TEST
*t
)
2318 test_clearstanza(&t
->s
);
2320 if (t
->data
!= NULL
) {
2321 if (t
->meth
!= NULL
)
2322 t
->meth
->cleanup(t
);
2323 OPENSSL_free(t
->data
);
2326 OPENSSL_free(t
->expected_err
);
2327 t
->expected_err
= NULL
;
2328 OPENSSL_free(t
->func
);
2330 OPENSSL_free(t
->reason
);
2340 * Check for errors in the test structure; return 1 if okay, else 0.
2342 static int check_test_error(EVP_TEST
*t
)
2348 if (t
->err
== NULL
&& t
->expected_err
== NULL
)
2350 if (t
->err
!= NULL
&& t
->expected_err
== NULL
) {
2351 if (t
->aux_err
!= NULL
) {
2352 TEST_info("%s:%d: Source of above error (%s); unexpected error %s",
2353 t
->s
.test_file
, t
->s
.start
, t
->aux_err
, t
->err
);
2355 TEST_info("%s:%d: Source of above error; unexpected error %s",
2356 t
->s
.test_file
, t
->s
.start
, t
->err
);
2360 if (t
->err
== NULL
&& t
->expected_err
!= NULL
) {
2361 TEST_info("%s:%d: Succeeded but was expecting %s",
2362 t
->s
.test_file
, t
->s
.start
, t
->expected_err
);
2366 if (strcmp(t
->err
, t
->expected_err
) != 0) {
2367 TEST_info("%s:%d: Expected %s got %s",
2368 t
->s
.test_file
, t
->s
.start
, t
->expected_err
, t
->err
);
2372 if (t
->func
== NULL
&& t
->reason
== NULL
)
2375 if (t
->func
== NULL
|| t
->reason
== NULL
) {
2376 TEST_info("%s:%d: Test is missing function or reason code",
2377 t
->s
.test_file
, t
->s
.start
);
2381 err
= ERR_peek_error();
2383 TEST_info("%s:%d: Expected error \"%s:%s\" not set",
2384 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2388 func
= ERR_func_error_string(err
);
2389 reason
= ERR_reason_error_string(err
);
2390 if (func
== NULL
&& reason
== NULL
) {
2391 TEST_info("%s:%d: Expected error \"%s:%s\", no strings available."
2393 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
);
2397 if (strcmp(func
, t
->func
) == 0 && strcmp(reason
, t
->reason
) == 0)
2400 TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"",
2401 t
->s
.test_file
, t
->s
.start
, t
->func
, t
->reason
, func
, reason
);
2407 * Run a parsed test. Log a message and return 0 on error.
2409 static int run_test(EVP_TEST
*t
)
2411 if (t
->meth
== NULL
)
2418 if (t
->err
== NULL
&& t
->meth
->run_test(t
) != 1) {
2419 TEST_info("%s:%d %s error",
2420 t
->s
.test_file
, t
->s
.start
, t
->meth
->name
);
2423 if (!check_test_error(t
)) {
2424 TEST_openssl_errors();
2433 static int find_key(EVP_PKEY
**ppk
, const char *name
, KEY_LIST
*lst
)
2435 for (; lst
!= NULL
; lst
= lst
->next
) {
2436 if (strcmp(lst
->name
, name
) == 0) {
2445 static void free_key_list(KEY_LIST
*lst
)
2447 while (lst
!= NULL
) {
2448 KEY_LIST
*next
= lst
->next
;
2450 EVP_PKEY_free(lst
->key
);
2451 OPENSSL_free(lst
->name
);
2458 * Is the key type an unsupported algorithm?
2460 static int key_unsupported(void)
2462 long err
= ERR_peek_error();
2464 if (ERR_GET_LIB(err
) == ERR_LIB_EVP
2465 && ERR_GET_REASON(err
) == EVP_R_UNSUPPORTED_ALGORITHM
) {
2469 #ifndef OPENSSL_NO_EC
2471 * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
2472 * hint to an unsupported algorithm/curve (e.g. if binary EC support is
2475 if (ERR_GET_LIB(err
) == ERR_LIB_EC
2476 && ERR_GET_REASON(err
) == EC_R_UNKNOWN_GROUP
) {
2480 #endif /* OPENSSL_NO_EC */
2485 * NULL out the value from |pp| but return it. This "steals" a pointer.
2487 static char *take_value(PAIR
*pp
)
2489 char *p
= pp
->value
;
2496 * Read and parse one test. Return 0 if failure, 1 if okay.
2498 static int parse(EVP_TEST
*t
)
2500 KEY_LIST
*key
, **klist
;
2507 if (BIO_eof(t
->s
.fp
))
2510 if (!test_readstanza(&t
->s
))
2512 } while (t
->s
.numpairs
== 0);
2513 pp
= &t
->s
.pairs
[0];
2515 /* Are we adding a key? */
2518 if (strcmp(pp
->key
, "PrivateKey") == 0) {
2519 pkey
= PEM_read_bio_PrivateKey(t
->s
.key
, NULL
, 0, NULL
);
2520 if (pkey
== NULL
&& !key_unsupported()) {
2521 EVP_PKEY_free(pkey
);
2522 TEST_info("Can't read private key %s", pp
->value
);
2523 TEST_openssl_errors();
2526 klist
= &private_keys
;
2527 } else if (strcmp(pp
->key
, "PublicKey") == 0) {
2528 pkey
= PEM_read_bio_PUBKEY(t
->s
.key
, NULL
, 0, NULL
);
2529 if (pkey
== NULL
&& !key_unsupported()) {
2530 EVP_PKEY_free(pkey
);
2531 TEST_info("Can't read public key %s", pp
->value
);
2532 TEST_openssl_errors();
2535 klist
= &public_keys
;
2536 } else if (strcmp(pp
->key
, "PrivateKeyRaw") == 0
2537 || strcmp(pp
->key
, "PublicKeyRaw") == 0 ) {
2538 char *strnid
= NULL
, *keydata
= NULL
;
2539 unsigned char *keybin
;
2543 if (strcmp(pp
->key
, "PrivateKeyRaw") == 0)
2544 klist
= &private_keys
;
2546 klist
= &public_keys
;
2548 strnid
= strchr(pp
->value
, ':');
2549 if (strnid
!= NULL
) {
2551 keydata
= strchr(strnid
, ':');
2552 if (keydata
!= NULL
)
2555 if (keydata
== NULL
) {
2556 TEST_info("Failed to parse %s value", pp
->key
);
2560 nid
= OBJ_txt2nid(strnid
);
2561 if (nid
== NID_undef
) {
2562 TEST_info("Uncrecognised algorithm NID");
2565 if (!parse_bin(keydata
, &keybin
, &keylen
)) {
2566 TEST_info("Failed to create binary key");
2569 if (klist
== &private_keys
)
2570 pkey
= EVP_PKEY_new_raw_private_key(nid
, NULL
, keybin
, keylen
);
2572 pkey
= EVP_PKEY_new_raw_public_key(nid
, NULL
, keybin
, keylen
);
2573 if (pkey
== NULL
&& !key_unsupported()) {
2574 TEST_info("Can't read %s data", pp
->key
);
2575 OPENSSL_free(keybin
);
2576 TEST_openssl_errors();
2579 OPENSSL_free(keybin
);
2582 /* If we have a key add to list */
2583 if (klist
!= NULL
) {
2584 if (find_key(NULL
, pp
->value
, *klist
)) {
2585 TEST_info("Duplicate key %s", pp
->value
);
2588 if (!TEST_ptr(key
= OPENSSL_malloc(sizeof(*key
))))
2590 key
->name
= take_value(pp
);
2592 /* Hack to detect SM2 keys */
2593 if(pkey
!= NULL
&& strstr(key
->name
, "SM2") != NULL
) {
2594 #ifdef OPENSSL_NO_SM2
2595 EVP_PKEY_free(pkey
);
2598 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
2606 /* Go back and start a new stanza. */
2607 if (t
->s
.numpairs
!= 1)
2608 TEST_info("Line %d: missing blank line\n", t
->s
.curr
);
2612 /* Find the test, based on first keyword. */
2613 if (!TEST_ptr(t
->meth
= find_test(pp
->key
)))
2615 if (!t
->meth
->init(t
, pp
->value
)) {
2616 TEST_error("unknown %s: %s\n", pp
->key
, pp
->value
);
2620 /* TEST_info("skipping %s %s", pp->key, pp->value); */
2624 for (pp
++, i
= 1; i
< t
->s
.numpairs
; pp
++, i
++) {
2625 if (strcmp(pp
->key
, "Result") == 0) {
2626 if (t
->expected_err
!= NULL
) {
2627 TEST_info("Line %d: multiple result lines", t
->s
.curr
);
2630 t
->expected_err
= take_value(pp
);
2631 } else if (strcmp(pp
->key
, "Function") == 0) {
2632 if (t
->func
!= NULL
) {
2633 TEST_info("Line %d: multiple function lines\n", t
->s
.curr
);
2636 t
->func
= take_value(pp
);
2637 } else if (strcmp(pp
->key
, "Reason") == 0) {
2638 if (t
->reason
!= NULL
) {
2639 TEST_info("Line %d: multiple reason lines", t
->s
.curr
);
2642 t
->reason
= take_value(pp
);
2644 /* Must be test specific line: try to parse it */
2645 int rv
= t
->meth
->parse(t
, pp
->key
, pp
->value
);
2648 TEST_info("Line %d: unknown keyword %s", t
->s
.curr
, pp
->key
);
2652 TEST_info("Line %d: error processing keyword %s = %s\n",
2653 t
->s
.curr
, pp
->key
, pp
->value
);
2662 static int run_file_tests(int i
)
2665 const char *testfile
= test_get_argument(i
);
2668 if (!TEST_ptr(t
= OPENSSL_zalloc(sizeof(*t
))))
2670 if (!test_start_file(&t
->s
, testfile
)) {
2675 while (!BIO_eof(t
->s
.fp
)) {
2679 if (c
== 0 || !run_test(t
)) {
2684 test_end_file(&t
->s
);
2687 free_key_list(public_keys
);
2688 free_key_list(private_keys
);
2695 int setup_tests(void)
2697 size_t n
= test_get_argument_count();
2700 TEST_error("Usage: %s file...", test_get_program_name());
2704 ADD_ALL_TESTS(run_file_tests
, n
);