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