Convert more tests to framework
[openssl.git] / test / x509aux.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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.
9  */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 #include <openssl/conf.h>
18 #include <openssl/err.h>
19 #include "e_os.h"
20 #include "test_main.h"
21 #include "test_main_custom.h"
22 #include "testutil.h"
23
24
25 /* List of files, from argv */
26 static char **files;
27
28 static int test_certs(int num)
29 {
30     int count;
31     char *name = 0;
32     char *header = 0;
33     unsigned char *data = 0;
34     long len;
35     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
36     typedef int (*i2d_X509_t)(X509 *, unsigned char **);
37     int err = 0;
38     BIO *fp = BIO_new_file(files[num], "r");
39
40     if (!TEST_ptr(fp))
41         return 0;
42
43     for (count = 0;
44          !err && PEM_read_bio(fp, &name, &header, &data, &len);
45          ++count) {
46         int trusted = strcmp(name, PEM_STRING_X509_TRUSTED) == 0;
47         d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509;
48         i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509;
49         X509 *cert = NULL;
50         const unsigned char *p = data;
51         unsigned char *buf = NULL;
52         unsigned char *bufp;
53         long enclen;
54
55         if (!trusted
56             && strcmp(name, PEM_STRING_X509) != 0
57             && strcmp(name, PEM_STRING_X509_OLD) != 0) {
58             fprintf(stderr, "unexpected PEM object: %s\n", name);
59             err = 1;
60             goto next;
61         }
62         cert = d2i(NULL, &p, len);
63
64         if (cert == NULL || (p - data) != len) {
65             fprintf(stderr, "error parsing input %s\n", name);
66             err = 1;
67             goto next;
68         }
69
70         /* Test traditional 2-pass encoding into caller allocated buffer */
71         enclen = i2d(cert, NULL);
72         if (len != enclen) {
73             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
74                     enclen, name, len);
75             err = 1;
76             goto next;
77         }
78         if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
79             perror("malloc");
80             err = 1;
81             goto next;
82         }
83         enclen = i2d(cert, &bufp);
84         if (len != enclen) {
85             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
86                     enclen, name, len);
87             err = 1;
88             goto next;
89         }
90         enclen = (long) (bufp - buf);
91         if (enclen != len) {
92             fprintf(stderr, "unexpected buffer position after encoding %s\n",
93                     name);
94             err = 1;
95             goto next;
96         }
97         if (memcmp(buf, data, len) != 0) {
98             fprintf(stderr, "encoded content of %s does not match input\n",
99                     name);
100             err = 1;
101             goto next;
102         }
103         OPENSSL_free(buf);
104         buf = NULL;
105
106         /* Test 1-pass encoding into library allocated buffer */
107         enclen = i2d(cert, &buf);
108         if (len != enclen) {
109             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
110                     enclen, name, len);
111             err = 1;
112             goto next;
113         }
114         if (memcmp(buf, data, len) != 0) {
115             fprintf(stderr, "encoded content of %s does not match input\n",
116                     name);
117             err = 1;
118             goto next;
119         }
120
121         if (trusted) {
122             /* Encode just the cert and compare with initial encoding */
123             OPENSSL_free(buf);
124             buf = NULL;
125
126             /* Test 1-pass encoding into library allocated buffer */
127             enclen = i2d(cert, &buf);
128             if (enclen > len) {
129                 fprintf(stderr, "encoded length %ld of %s > input length %ld\n",
130                         enclen, name, len);
131                 err = 1;
132                 goto next;
133             }
134             if (memcmp(buf, data, enclen) != 0) {
135                 fprintf(stderr, "encoded cert content does not match input\n");
136                 err = 1;
137                 goto next;
138             }
139         }
140
141         /*
142          * If any of these were null, PEM_read() would have failed.
143          */
144     next:
145         X509_free(cert);
146         OPENSSL_free(buf);
147         OPENSSL_free(name);
148         OPENSSL_free(header);
149         OPENSSL_free(data);
150     }
151     BIO_free(fp);
152
153     if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
154         /* Reached end of PEM file */
155         if (count > 0) {
156             ERR_clear_error();
157             return 1;
158         }
159     }
160
161     /* Some other PEM read error */
162     return 0;
163 }
164
165 int test_main(int argc, char *argv[])
166 {
167     if (argc < 2) {
168         TEST_error("usage: %s certfile...", argv[0]);
169         return 0;
170     }
171
172     files = &argv[1];
173     ADD_ALL_TESTS(test_certs, argc - 1);
174     return run_tests(argv[0]);
175 }