Add libctx/provider support to cmp_client_test
[openssl.git] / test / cmp_testlib.c
1 /*
2  * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11
12 #include "cmp_testlib.h"
13 #include <openssl/rsa.h> /* needed in case config no-deprecated */
14
15 DEFINE_STACK_OF(X509)
16
17 EVP_PKEY *load_pem_key(const char *file)
18 {
19     EVP_PKEY *key = NULL;
20     BIO *bio = NULL;
21
22     if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
23         return NULL;
24     if (TEST_int_gt(BIO_read_filename(bio, file), 0))
25         (void)TEST_ptr(key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
26
27     BIO_free(bio);
28     return key;
29 }
30
31 X509 *load_pem_cert(const char *file, OPENSSL_CTX *libctx)
32 {
33     X509 *cert = NULL;
34     BIO *bio = NULL;
35
36     if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
37         return NULL;
38     if (TEST_int_gt(BIO_read_filename(bio, file), 0)
39             && TEST_ptr(cert = X509_new_with_libctx(libctx, NULL)))
40         (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL));
41
42     BIO_free(bio);
43     return cert;
44 }
45
46 OSSL_CMP_MSG *load_pkimsg(const char *file)
47 {
48     OSSL_CMP_MSG *msg;
49
50     (void)TEST_ptr((msg = OSSL_CMP_MSG_read(file)));
51     return msg;
52 }
53
54 X509_REQ *load_csr(const char *file)
55 {
56     X509_REQ *csr = NULL;
57     BIO *bio = NULL;
58
59     if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
60         return NULL;
61     (void)TEST_ptr(csr = d2i_X509_REQ_bio(bio, NULL));
62     BIO_free(bio);
63     return csr;
64 }
65
66 EVP_PKEY *gen_rsa(void)
67 {
68     EVP_PKEY_CTX *ctx = NULL;
69     EVP_PKEY *pkey = NULL;
70
71     (void)(TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))
72                && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
73                && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048), 0)
74                && TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0));
75     EVP_PKEY_CTX_free(ctx);
76     return pkey;
77 }
78
79 /*
80  * Checks whether the syntax of msg conforms to ASN.1
81  */
82 int valid_asn1_encoding(const OSSL_CMP_MSG *msg)
83 {
84     return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0;
85 }
86
87 /*
88  * Compares two stacks of certificates in the order of their elements.
89  * Returns 0 if sk1 and sk2 are equal and another value otherwise
90  */
91 int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2)
92 {
93     int i, res;
94     X509 *a, *b;
95
96     if (sk1 == sk2)
97         return 0;
98     if (sk1 == NULL)
99         return -1;
100     if (sk2 == NULL)
101         return 1;
102     if ((res = sk_X509_num(sk1) - sk_X509_num(sk2)))
103         return res;
104     for (i = 0; i < sk_X509_num(sk1); i++) {
105         a = sk_X509_value(sk1, i);
106         b = sk_X509_value(sk2, i);
107         if (a != b)
108             if ((res = X509_cmp(a, b)) != 0)
109                 return res;
110     }
111     return 0;
112 }
113
114 /*
115  * Up refs and push a cert onto sk.
116  * Returns the number of certificates on the stack on success
117  * Returns -1 or 0 on error
118  */
119 int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert)
120 {
121     int res;
122
123     if (sk == NULL || cert == NULL)
124         return -1;
125     if (!X509_up_ref(cert))
126         return -1;
127     res = sk_X509_push(sk, cert);
128     if (res <= 0)
129         X509_free(cert); /* down-ref */
130     return res;
131 }
132
133 int print_to_bio_out(const char *func, const char *file, int line,
134                      OSSL_CMP_severity level, const char *msg)
135 {
136     return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
137 }