2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL licenses, (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>
20 #include "test_main.h"
21 #include "test_main_custom.h"
24 static int test_certs(BIO *fp)
29 unsigned char *data = 0;
31 typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
32 typedef int (*i2d_X509_t)(X509 *, unsigned char **);
36 !err && PEM_read_bio(fp, &name, &header, &data, &len);
38 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;
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 fprintf(stderr, "unexpected PEM object: %s\n", name);
54 cert = d2i(NULL, &p, len);
56 if (cert == NULL || (p - data) != len) {
57 fprintf(stderr, "error parsing input %s\n", name);
62 /* Test traditional 2-pass encoding into caller allocated buffer */
63 enclen = i2d(cert, NULL);
65 fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
70 if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
75 enclen = i2d(cert, &bufp);
77 fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
82 enclen = (long) (bufp - buf);
84 fprintf(stderr, "unexpected buffer position after encoding %s\n",
89 if (memcmp(buf, data, len) != 0) {
90 fprintf(stderr, "encoded content of %s does not match input\n",
98 /* Test 1-pass encoding into library allocated buffer */
99 enclen = i2d(cert, &buf);
101 fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
106 if (memcmp(buf, data, len) != 0) {
107 fprintf(stderr, "encoded content of %s does not match input\n",
114 /* Encode just the cert and compare with initial encoding */
118 /* Test 1-pass encoding into library allocated buffer */
119 enclen = i2d(cert, &buf);
121 fprintf(stderr, "encoded length %ld of %s > input length %ld\n",
126 if (memcmp(buf, data, enclen) != 0) {
127 fprintf(stderr, "encoded cert content does not match input\n");
134 * If any of these were null, PEM_read() would have failed.
140 OPENSSL_free(header);
144 if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
145 /* Reached end of PEM file */
152 /* Some other PEM read error */
156 int test_main(int argc, char *argv[])
162 TEST_error("usage: %s certfile...", argv[0]);
164 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
166 for (argv++; *argv; argv++) {
167 BIO *f = BIO_new_file(*argv, "r");
170 TEST_check(f != NULL);
174 if (!TEST_int_eq(ok, 1))