]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/helpers/cmp_testlib.c
remove obsolete test/drbg_cavs_data.h
[thirdparty/openssl.git] / test / helpers / 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 EVP_PKEY *load_pem_key(const char *file, OSSL_LIB_CTX *libctx)
16 {
17 EVP_PKEY *key = NULL;
18 BIO *bio = NULL;
19
20 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
21 return NULL;
22 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
23 (void)TEST_ptr(key = PEM_read_bio_PrivateKey_ex(bio, NULL, NULL, NULL,
24 libctx, NULL));
25
26 BIO_free(bio);
27 return key;
28 }
29
30 X509 *load_pem_cert(const char *file, OSSL_LIB_CTX *libctx)
31 {
32 X509 *cert = NULL;
33 BIO *bio = NULL;
34
35 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
36 return NULL;
37 if (TEST_int_gt(BIO_read_filename(bio, file), 0)
38 && TEST_ptr(cert = X509_new_ex(libctx, NULL)))
39 (void)TEST_ptr(cert = PEM_read_bio_X509(bio, &cert, NULL, NULL));
40
41 BIO_free(bio);
42 return cert;
43 }
44
45 OSSL_CMP_MSG *load_pkimsg(const char *file)
46 {
47 OSSL_CMP_MSG *msg;
48
49 (void)TEST_ptr((msg = OSSL_CMP_MSG_read(file)));
50 return msg;
51 }
52
53 X509_REQ *load_csr(const char *file)
54 {
55 X509_REQ *csr = NULL;
56 BIO *bio = NULL;
57
58 if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
59 return NULL;
60 (void)TEST_ptr(csr = d2i_X509_REQ_bio(bio, NULL));
61 BIO_free(bio);
62 return csr;
63 }
64
65 /*
66 * Checks whether the syntax of msg conforms to ASN.1
67 */
68 int valid_asn1_encoding(const OSSL_CMP_MSG *msg)
69 {
70 return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0;
71 }
72
73 /*
74 * Compares two stacks of certificates in the order of their elements.
75 * Returns 0 if sk1 and sk2 are equal and another value otherwise
76 */
77 int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2)
78 {
79 int i, res;
80 X509 *a, *b;
81
82 if (sk1 == sk2)
83 return 0;
84 if (sk1 == NULL)
85 return -1;
86 if (sk2 == NULL)
87 return 1;
88 if ((res = sk_X509_num(sk1) - sk_X509_num(sk2)))
89 return res;
90 for (i = 0; i < sk_X509_num(sk1); i++) {
91 a = sk_X509_value(sk1, i);
92 b = sk_X509_value(sk2, i);
93 if (a != b)
94 if ((res = X509_cmp(a, b)) != 0)
95 return res;
96 }
97 return 0;
98 }
99
100 /*
101 * Up refs and push a cert onto sk.
102 * Returns the number of certificates on the stack on success
103 * Returns -1 or 0 on error
104 */
105 int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert)
106 {
107 int res;
108
109 if (sk == NULL || cert == NULL)
110 return -1;
111 if (!X509_up_ref(cert))
112 return -1;
113 res = sk_X509_push(sk, cert);
114 if (res <= 0)
115 X509_free(cert); /* down-ref */
116 return res;
117 }
118
119 int print_to_bio_out(const char *func, const char *file, int line,
120 OSSL_CMP_severity level, const char *msg)
121 {
122 return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
123 }