]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/x509aux.c
2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 #include <openssl/conf.h>
18 #include <openssl/err.h>
19 #include "internal/nelem.h"
22 static int test_certs(int num
)
27 unsigned char *data
= 0;
29 typedef X509
*(*d2i_X509_t
)(X509
**, const unsigned char **, long);
30 typedef int (*i2d_X509_t
)(const X509
*, unsigned char **);
32 BIO
*fp
= BIO_new_file(test_get_argument(num
), "r");
37 for (c
= 0; !err
&& PEM_read_bio(fp
, &name
, &header
, &data
, &len
); ++c
) {
38 const int trusted
= (strcmp(name
, PEM_STRING_X509_TRUSTED
) == 0);
39 d2i_X509_t d2i
= trusted
? d2i_X509_AUX
: d2i_X509
;
40 i2d_X509_t i2d
= trusted
? i2d_X509_AUX
: i2d_X509
;
43 const unsigned char *p
= data
;
44 unsigned char *buf
= NULL
;
49 && strcmp(name
, PEM_STRING_X509
) != 0
50 && strcmp(name
, PEM_STRING_X509_OLD
) != 0) {
51 TEST_error("unexpected PEM object: %s", name
);
55 cert
= d2i(NULL
, &p
, len
);
57 if (cert
== NULL
|| (p
- data
) != len
) {
58 TEST_error("error parsing input %s", name
);
63 /* Test traditional 2-pass encoding into caller allocated buffer */
64 enclen
= i2d(cert
, NULL
);
66 TEST_error("encoded length %ld of %s != input length %ld",
71 if ((buf
= bufp
= OPENSSL_malloc(len
)) == NULL
) {
72 TEST_perror("malloc");
76 enclen
= i2d(cert
, &bufp
);
78 TEST_error("encoded length %ld of %s != input length %ld",
83 enclen
= (long) (bufp
- buf
);
85 TEST_error("unexpected buffer position after encoding %s", name
);
89 if (memcmp(buf
, data
, len
) != 0) {
90 TEST_error("encoded content of %s does not match input", name
);
95 reuse
= d2i(NULL
, &p
, enclen
);
97 TEST_error("second d2i call failed for %s", name
);
101 err
= X509_cmp(reuse
, cert
);
103 TEST_error("X509_cmp for %s resulted in %d", name
, err
);
110 /* Test 1-pass encoding into library allocated buffer */
111 enclen
= i2d(cert
, &buf
);
113 TEST_error("encoded length %ld of %s != input length %ld",
118 if (memcmp(buf
, data
, len
) != 0) {
119 TEST_error("encoded content of %s does not match input", name
);
125 /* Encode just the cert and compare with initial encoding */
129 /* Test 1-pass encoding into library allocated buffer */
130 enclen
= i2d(cert
, &buf
);
132 TEST_error("encoded length %ld of %s > input length %ld",
137 if (memcmp(buf
, data
, enclen
) != 0) {
138 TEST_error("encoded cert content does not match input");
145 * If any of these were null, PEM_read() would have failed.
152 OPENSSL_free(header
);
157 if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE
) {
158 /* Reached end of PEM file */
165 /* Some other PEM read error */
169 OPT_TEST_DECLARE_USAGE("certfile...\n")
171 int setup_tests(void)
175 if (!test_skip_common_options()) {
176 TEST_error("Error parsing test options\n");
180 n
= test_get_argument_count();
184 ADD_ALL_TESTS(test_certs
, (int)n
);