X-Git-Url: http://www.wagner.pp.ru/gitweb/?p=openssl-gost%2Fengine.git;a=blobdiff_plain;f=test_params.c;h=114193346e63da2362a1b265ae74ac134e424b3b;hp=3e12d5a9669bbd23e2a8da997270128f6e38e4e7;hb=HEAD;hpb=f6b18779d239d771e1e67d04b07c8beb277bd586 diff --git a/test_params.c b/test_params.c index 3e12d5a..1141933 100644 --- a/test_params.c +++ b/test_params.c @@ -7,6 +7,11 @@ * See https://www.openssl.org/source/license.html for details */ +#ifdef _MSC_VER +# pragma warning(push, 3) +# include +# pragma warning(pop) +#endif #include "e_gost_err.h" #include "gost_lcl.h" #include @@ -19,17 +24,17 @@ #include #include -#define T(e) ({ if (!(e)) { \ - ERR_print_errors_fp(stderr); \ - OpenSSLDie(__FILE__, __LINE__, #e); \ - } \ - }) -#define TE(e) ({ if (!(e)) { \ - ERR_print_errors_fp(stderr); \ - fprintf(stderr, "Error at %s:%d %s\n", __FILE__, __LINE__, #e); \ - return -1; \ - } \ - }) +#define T(e) \ + if (!(e)) { \ + ERR_print_errors_fp(stderr); \ + OpenSSLDie(__FILE__, __LINE__, #e); \ + } +#define TE(e) \ + if (!(e)) { \ + ERR_print_errors_fp(stderr); \ + fprintf(stderr, "Error at %s:%d %s\n", __FILE__, __LINE__, #e); \ + return -1; \ + } #define cRED "\033[1;31m" #define cDRED "\033[0;31m" @@ -39,9 +44,9 @@ #define cDBLUE "\033[0;34m" #define cNORM "\033[m" #define TEST_ASSERT(e) {if ((test = (e))) \ - printf(cRED " Test FAILED\n" cNORM); \ + printf(cRED " Test FAILED" cNORM "\n"); \ else \ - printf(cGREEN " Test passed\n" cNORM);} + printf(cGREEN " Test passed" cNORM "\n");} struct test_param { unsigned int param; /* NID of EC parameters */ @@ -873,20 +878,39 @@ static void hexdump(const void *ptr, size_t len) static void print_test_result(int err) { if (err == 1) - printf(cGREEN "correct\n" cNORM); + printf(cGREEN "correct" cNORM "\n"); else if (err == 0) - printf(cRED "incorrect\n" cNORM); + printf(cRED "incorrect" cNORM "\n"); else ERR_print_errors_fp(stderr); } +/* copy-paste from crypto/crmf/crmf_lib.c */ +static int X509_PUBKEY_cmp(X509_PUBKEY *a, X509_PUBKEY *b) +{ + X509_ALGOR *algA = NULL, *algB = NULL; + int res = 0; + + if (a == b) + return 0; + if (a == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) + || algA == NULL) + return -1; + if (b == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) + || algB == NULL) + return 1; + if ((res = X509_ALGOR_cmp(algA, algB)) != 0) + return res; + return !EVP_PKEY_cmp(X509_PUBKEY_get0(a), X509_PUBKEY_get0(b)); +} + static int test_cert(struct test_cert *tc) { int ret = 0, err; X509 *x; const unsigned char *p; - printf(cBLUE "Test %s (it):\n" cNORM, tc->name); + printf(cBLUE "Test %s (it): " cNORM, tc->name); p = tc->cert; T(x = d2i_X509(NULL, &p, tc->len)); @@ -914,6 +938,54 @@ static int test_cert(struct test_cert *tc) printf(" (curve %s)\n", OBJ_nid2sn(param_nid)); sk_ASN1_TYPE_pop_free(seq, ASN1_TYPE_free); + /* + * Conversion tests. + */ + /* Convert cert to DER and back. */ + BIO *bp; + T(bp = BIO_new(BIO_s_mem())); + T(i2d_X509_bio(bp, x)); + X509 *y = NULL; + T(d2i_X509_bio(bp, &y)); + err = X509_cmp(x, y); + printf(" d2i_X509_bio\t\t\t"); + print_test_result(!err); + ret |= err; + X509_free(y); + + /* Convert cert to PEM and back. */ + y = NULL; + T(PEM_write_bio_X509(bp, x)); + T(PEM_read_bio_X509(bp, &y, 0, NULL)); + err = X509_cmp(x, y); + printf(" PEM_read_bio_X509\t\t"); + print_test_result(!err); + ret |= err; + X509_free(y); + + /* Convert public key to PEM and back. */ + T(BIO_reset(bp)); + T(PEM_write_bio_X509_PUBKEY(bp, xk)); + X509_PUBKEY *tk = NULL; + T(PEM_read_bio_X509_PUBKEY(bp, &tk, NULL, NULL)); + err = X509_PUBKEY_cmp(xk, tk); + X509_PUBKEY_free(tk); + printf(" PEM_read_bio_X509_PUBKEY\t"); + print_test_result(!err); + ret |= err; + + /* Convert public key to DER and back. */ + T(BIO_reset(bp)); + T(i2d_X509_PUBKEY_bio(bp, xk)); + tk = NULL; + T(d2i_X509_PUBKEY_bio(bp, &tk)); + err = X509_PUBKEY_cmp(xk, tk); + X509_PUBKEY_free(tk); + printf(" d2i_X509_PUBKEY_bio\t\t"); + print_test_result(!err); + ret |= err; + BIO_free(bp); + /* * Verify */ @@ -983,7 +1055,7 @@ static int test_param(struct test_param *t) int hash_nid = 0; const char *sn = OBJ_nid2sn(t->param); - printf(cBLUE "Test %s (cp):\n" cNORM, sn); + printf(cBLUE "Test %s (cp):" cNORM "\n", sn); switch (t->len) { case 256 / 8: @@ -1060,7 +1132,7 @@ static int test_param(struct test_param *t) T(mdtype = EVP_get_digestbynid(hash_nid)); T(EVP_VerifyInit(md_ctx, mdtype)); /* Feed byte-by-byte. */ - int i; + size_t i; for (i = 0; i < t->data_len; i++) T(EVP_VerifyUpdate(md_ctx, &t->data[i], 1)); err = EVP_VerifyFinal(md_ctx, sig, siglen, pkey); @@ -1093,9 +1165,7 @@ int main(int argc, char **argv) { int ret = 0; - setenv("OPENSSL_CONF", "../example.conf", 0); OPENSSL_add_all_algorithms_conf(); - ERR_load_crypto_strings(); struct test_param **tpp; for (tpp = test_params; *tpp; tpp++) @@ -1105,5 +1175,9 @@ int main(int argc, char **argv) for (tc = test_certs; tc->cert; tc++) ret |= test_cert(tc); + if (ret) + printf(cDRED "= Some tests FAILED!" cNORM "\n"); + else + printf(cDGREEN "= All tests passed!" cNORM "\n"); return ret; }