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>
21 static int test_certs(int num)
26 unsigned char *data = 0;
28 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
29 typedef int (*i2d_X509_t)(const X509 *, unsigned char **);
31 BIO *fp = BIO_new_file(test_get_argument(num), "r");
36 for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) {
37 const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0);
38 d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
39 i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
42 const unsigned char *p = data;
43 unsigned char *buf = NULL;
48 && strcmp(name, PEM_STRING_X509) != 0
49 && strcmp(name, PEM_STRING_X509_OLD) != 0) {
50 TEST_error("unexpected PEM object: %s", name);
54 cert = d2i(NULL, &p, len);
56 if (cert == NULL || (p - data) != len) {
57 TEST_error("error parsing input %s", name);
62 /* Test traditional 2-pass encoding into caller allocated buffer */
63 enclen = i2d(cert, NULL);
65 TEST_error("encoded length %ld of %s != input length %ld",
70 if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
71 TEST_perror("malloc");
75 enclen = i2d(cert, &bufp);
77 TEST_error("encoded length %ld of %s != input length %ld",
82 enclen = (long) (bufp - buf);
84 TEST_error("unexpected buffer position after encoding %s", name);
88 if (memcmp(buf, data, len) != 0) {
89 TEST_error("encoded content of %s does not match input", name);
94 reuse = d2i(NULL, &p, enclen);
96 TEST_error("second d2i call failed for %s", name);
100 err = X509_cmp(reuse, cert);
102 TEST_error("X509_cmp for %s resulted in %d", name, err);
109 /* Test 1-pass encoding into library allocated buffer */
110 enclen = i2d(cert, &buf);
112 TEST_error("encoded length %ld of %s != input length %ld",
117 if (memcmp(buf, data, len) != 0) {
118 TEST_error("encoded content of %s does not match input", name);
124 /* Encode just the cert and compare with initial encoding */
128 /* Test 1-pass encoding into library allocated buffer */
129 enclen = i2d(cert, &buf);
131 TEST_error("encoded length %ld of %s > input length %ld",
136 if (memcmp(buf, data, enclen) != 0) {
137 TEST_error("encoded cert content does not match input");
144 * If any of these were null, PEM_read() would have failed.
151 OPENSSL_free(header);
156 if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
157 /* Reached end of PEM file */
164 /* Some other PEM read error */
168 OPT_TEST_DECLARE_USAGE("certfile...\n")
170 int setup_tests(void)
174 if (!test_skip_common_options()) {
175 TEST_error("Error parsing test options\n");
179 n = test_get_argument_count();
183 ADD_ALL_TESTS(test_certs, (int)n);