2 * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2019
4 * Copyright Siemens AG 2015-2019
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
12 #include "cmp_testlib.h"
13 #include <openssl/rsa.h> /* needed in case config no-deprecated */
15 EVP_PKEY *load_pem_key(const char *file)
20 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
22 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
23 (void)TEST_ptr(key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
29 X509 *load_pem_cert(const char *file)
34 if (!TEST_ptr(bio = BIO_new(BIO_s_file())))
36 if (TEST_int_gt(BIO_read_filename(bio, file), 0))
37 (void)TEST_ptr(cert = PEM_read_bio_X509(bio, NULL, NULL, NULL));
43 OSSL_CMP_MSG *load_pkimsg(const char *file)
47 (void)TEST_ptr((msg = ossl_cmp_msg_load(file)));
51 X509_REQ *load_csr(const char *file)
56 if (!TEST_ptr(file) || !TEST_ptr(bio = BIO_new_file(file, "rb")))
58 (void)TEST_ptr(csr = d2i_X509_REQ_bio(bio, NULL));
63 EVP_PKEY *gen_rsa(void)
65 EVP_PKEY_CTX *ctx = NULL;
66 EVP_PKEY *pkey = NULL;
68 (void)(TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL))
69 && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
70 && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048), 0)
71 && TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0));
72 EVP_PKEY_CTX_free(ctx);
77 * Checks whether the syntax of msg conforms to ASN.1
79 int valid_asn1_encoding(const OSSL_CMP_MSG *msg)
81 return msg != NULL ? i2d_OSSL_CMP_MSG(msg, NULL) > 0 : 0;
85 * Compares two stacks of certificates in the order of their elements.
86 * Returns 0 if sk1 and sk2 are equal and another value otherwise
88 int STACK_OF_X509_cmp(const STACK_OF(X509) *sk1, const STACK_OF(X509) *sk2)
99 if ((res = sk_X509_num(sk1) - sk_X509_num(sk2)))
101 for (i = 0; i < sk_X509_num(sk1); i++) {
102 a = sk_X509_value(sk1, i);
103 b = sk_X509_value(sk2, i);
105 if ((res = X509_cmp(a, b)) != 0)
112 * Up refs and push a cert onto sk.
113 * Returns the number of certificates on the stack on success
114 * Returns -1 or 0 on error
116 int STACK_OF_X509_push1(STACK_OF(X509) *sk, X509 *cert)
120 if (sk == NULL || cert == NULL)
122 if (!X509_up_ref(cert))
124 res = sk_X509_push(sk, cert);
126 X509_free(cert); /* down-ref */