1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert * You may obtain a copy of the License at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert #include <stdio.h>
12*e0c4386eSCy Schubert #include <string.h>
13*e0c4386eSCy Schubert #include <errno.h>
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include <openssl/x509.h>
16*e0c4386eSCy Schubert #include <openssl/pem.h>
17*e0c4386eSCy Schubert #include <openssl/conf.h>
18*e0c4386eSCy Schubert #include <openssl/err.h>
19*e0c4386eSCy Schubert #include "internal/nelem.h"
20*e0c4386eSCy Schubert #include "testutil.h"
21*e0c4386eSCy Schubert
test_certs(int num)22*e0c4386eSCy Schubert static int test_certs(int num)
23*e0c4386eSCy Schubert {
24*e0c4386eSCy Schubert int c;
25*e0c4386eSCy Schubert char *name = 0;
26*e0c4386eSCy Schubert char *header = 0;
27*e0c4386eSCy Schubert unsigned char *data = 0;
28*e0c4386eSCy Schubert long len;
29*e0c4386eSCy Schubert typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
30*e0c4386eSCy Schubert typedef int (*i2d_X509_t)(const X509 *, unsigned char **);
31*e0c4386eSCy Schubert int err = 0;
32*e0c4386eSCy Schubert BIO *fp = BIO_new_file(test_get_argument(num), "r");
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert if (!TEST_ptr(fp))
35*e0c4386eSCy Schubert return 0;
36*e0c4386eSCy Schubert
37*e0c4386eSCy Schubert for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) {
38*e0c4386eSCy Schubert const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0);
39*e0c4386eSCy Schubert d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
40*e0c4386eSCy Schubert i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
41*e0c4386eSCy Schubert X509 *cert = NULL;
42*e0c4386eSCy Schubert X509 *reuse = NULL;
43*e0c4386eSCy Schubert const unsigned char *p = data;
44*e0c4386eSCy Schubert unsigned char *buf = NULL;
45*e0c4386eSCy Schubert unsigned char *bufp;
46*e0c4386eSCy Schubert long enclen;
47*e0c4386eSCy Schubert
48*e0c4386eSCy Schubert if (!trusted
49*e0c4386eSCy Schubert && strcmp(name, PEM_STRING_X509) != 0
50*e0c4386eSCy Schubert && strcmp(name, PEM_STRING_X509_OLD) != 0) {
51*e0c4386eSCy Schubert TEST_error("unexpected PEM object: %s", name);
52*e0c4386eSCy Schubert err = 1;
53*e0c4386eSCy Schubert goto next;
54*e0c4386eSCy Schubert }
55*e0c4386eSCy Schubert cert = d2i(NULL, &p, len);
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert if (cert == NULL || (p - data) != len) {
58*e0c4386eSCy Schubert TEST_error("error parsing input %s", name);
59*e0c4386eSCy Schubert err = 1;
60*e0c4386eSCy Schubert goto next;
61*e0c4386eSCy Schubert }
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubert /* Test traditional 2-pass encoding into caller allocated buffer */
64*e0c4386eSCy Schubert enclen = i2d(cert, NULL);
65*e0c4386eSCy Schubert if (len != enclen) {
66*e0c4386eSCy Schubert TEST_error("encoded length %ld of %s != input length %ld",
67*e0c4386eSCy Schubert enclen, name, len);
68*e0c4386eSCy Schubert err = 1;
69*e0c4386eSCy Schubert goto next;
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
72*e0c4386eSCy Schubert TEST_perror("malloc");
73*e0c4386eSCy Schubert err = 1;
74*e0c4386eSCy Schubert goto next;
75*e0c4386eSCy Schubert }
76*e0c4386eSCy Schubert enclen = i2d(cert, &bufp);
77*e0c4386eSCy Schubert if (len != enclen) {
78*e0c4386eSCy Schubert TEST_error("encoded length %ld of %s != input length %ld",
79*e0c4386eSCy Schubert enclen, name, len);
80*e0c4386eSCy Schubert err = 1;
81*e0c4386eSCy Schubert goto next;
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert enclen = (long) (bufp - buf);
84*e0c4386eSCy Schubert if (enclen != len) {
85*e0c4386eSCy Schubert TEST_error("unexpected buffer position after encoding %s", name);
86*e0c4386eSCy Schubert err = 1;
87*e0c4386eSCy Schubert goto next;
88*e0c4386eSCy Schubert }
89*e0c4386eSCy Schubert if (memcmp(buf, data, len) != 0) {
90*e0c4386eSCy Schubert TEST_error("encoded content of %s does not match input", name);
91*e0c4386eSCy Schubert err = 1;
92*e0c4386eSCy Schubert goto next;
93*e0c4386eSCy Schubert }
94*e0c4386eSCy Schubert p = buf;
95*e0c4386eSCy Schubert reuse = d2i(NULL, &p, enclen);
96*e0c4386eSCy Schubert if (reuse == NULL) {
97*e0c4386eSCy Schubert TEST_error("second d2i call failed for %s", name);
98*e0c4386eSCy Schubert err = 1;
99*e0c4386eSCy Schubert goto next;
100*e0c4386eSCy Schubert }
101*e0c4386eSCy Schubert err = X509_cmp(reuse, cert);
102*e0c4386eSCy Schubert if (err != 0) {
103*e0c4386eSCy Schubert TEST_error("X509_cmp for %s resulted in %d", name, err);
104*e0c4386eSCy Schubert err = 1;
105*e0c4386eSCy Schubert goto next;
106*e0c4386eSCy Schubert }
107*e0c4386eSCy Schubert OPENSSL_free(buf);
108*e0c4386eSCy Schubert buf = NULL;
109*e0c4386eSCy Schubert
110*e0c4386eSCy Schubert /* Test 1-pass encoding into library allocated buffer */
111*e0c4386eSCy Schubert enclen = i2d(cert, &buf);
112*e0c4386eSCy Schubert if (len != enclen) {
113*e0c4386eSCy Schubert TEST_error("encoded length %ld of %s != input length %ld",
114*e0c4386eSCy Schubert enclen, name, len);
115*e0c4386eSCy Schubert err = 1;
116*e0c4386eSCy Schubert goto next;
117*e0c4386eSCy Schubert }
118*e0c4386eSCy Schubert if (memcmp(buf, data, len) != 0) {
119*e0c4386eSCy Schubert TEST_error("encoded content of %s does not match input", name);
120*e0c4386eSCy Schubert err = 1;
121*e0c4386eSCy Schubert goto next;
122*e0c4386eSCy Schubert }
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert if (trusted) {
125*e0c4386eSCy Schubert /* Encode just the cert and compare with initial encoding */
126*e0c4386eSCy Schubert OPENSSL_free(buf);
127*e0c4386eSCy Schubert buf = NULL;
128*e0c4386eSCy Schubert
129*e0c4386eSCy Schubert /* Test 1-pass encoding into library allocated buffer */
130*e0c4386eSCy Schubert enclen = i2d(cert, &buf);
131*e0c4386eSCy Schubert if (enclen > len) {
132*e0c4386eSCy Schubert TEST_error("encoded length %ld of %s > input length %ld",
133*e0c4386eSCy Schubert enclen, name, len);
134*e0c4386eSCy Schubert err = 1;
135*e0c4386eSCy Schubert goto next;
136*e0c4386eSCy Schubert }
137*e0c4386eSCy Schubert if (memcmp(buf, data, enclen) != 0) {
138*e0c4386eSCy Schubert TEST_error("encoded cert content does not match input");
139*e0c4386eSCy Schubert err = 1;
140*e0c4386eSCy Schubert goto next;
141*e0c4386eSCy Schubert }
142*e0c4386eSCy Schubert }
143*e0c4386eSCy Schubert
144*e0c4386eSCy Schubert /*
145*e0c4386eSCy Schubert * If any of these were null, PEM_read() would have failed.
146*e0c4386eSCy Schubert */
147*e0c4386eSCy Schubert next:
148*e0c4386eSCy Schubert X509_free(cert);
149*e0c4386eSCy Schubert X509_free(reuse);
150*e0c4386eSCy Schubert OPENSSL_free(buf);
151*e0c4386eSCy Schubert OPENSSL_free(name);
152*e0c4386eSCy Schubert OPENSSL_free(header);
153*e0c4386eSCy Schubert OPENSSL_free(data);
154*e0c4386eSCy Schubert }
155*e0c4386eSCy Schubert BIO_free(fp);
156*e0c4386eSCy Schubert
157*e0c4386eSCy Schubert if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
158*e0c4386eSCy Schubert /* Reached end of PEM file */
159*e0c4386eSCy Schubert if (c > 0) {
160*e0c4386eSCy Schubert ERR_clear_error();
161*e0c4386eSCy Schubert return 1;
162*e0c4386eSCy Schubert }
163*e0c4386eSCy Schubert }
164*e0c4386eSCy Schubert
165*e0c4386eSCy Schubert /* Some other PEM read error */
166*e0c4386eSCy Schubert return 0;
167*e0c4386eSCy Schubert }
168*e0c4386eSCy Schubert
169*e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("certfile...\n")
170*e0c4386eSCy Schubert
setup_tests(void)171*e0c4386eSCy Schubert int setup_tests(void)
172*e0c4386eSCy Schubert {
173*e0c4386eSCy Schubert size_t n;
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubert if (!test_skip_common_options()) {
176*e0c4386eSCy Schubert TEST_error("Error parsing test options\n");
177*e0c4386eSCy Schubert return 0;
178*e0c4386eSCy Schubert }
179*e0c4386eSCy Schubert
180*e0c4386eSCy Schubert n = test_get_argument_count();
181*e0c4386eSCy Schubert if (n == 0)
182*e0c4386eSCy Schubert return 0;
183*e0c4386eSCy Schubert
184*e0c4386eSCy Schubert ADD_ALL_TESTS(test_certs, (int)n);
185*e0c4386eSCy Schubert return 1;
186*e0c4386eSCy Schubert }
187