Convert x509aux, cipherlist, casttest
[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 static int test_certs(BIO *fp)
25 {
26     int count;
27     char *name = 0;
28     char *header = 0;
29     unsigned char *data = 0;
30     long len;
31     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long);
32     typedef int (*i2d_X509_t)(X509 *, unsigned char **);
33     int err = 0;
34
35     for (count = 0;
36          !err && PEM_read_bio(fp, &name, &header, &data, &len);
37          ++count) {
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;
41         X509 *cert = NULL;
42         const unsigned char *p = data;
43         unsigned char *buf = NULL;
44         unsigned char *bufp;
45         long enclen;
46
47         if (!trusted
48             && strcmp(name, PEM_STRING_X509) != 0
49             && strcmp(name, PEM_STRING_X509_OLD) != 0) {
50             fprintf(stderr, "unexpected PEM object: %s\n", name);
51             err = 1;
52             goto next;
53         }
54         cert = d2i(NULL, &p, len);
55
56         if (cert == NULL || (p - data) != len) {
57             fprintf(stderr, "error parsing input %s\n", name);
58             err = 1;
59             goto next;
60         }
61
62         /* Test traditional 2-pass encoding into caller allocated buffer */
63         enclen = i2d(cert, NULL);
64         if (len != enclen) {
65             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
66                     enclen, name, len);
67             err = 1;
68             goto next;
69         }
70         if ((buf = bufp = OPENSSL_malloc(len)) == NULL) {
71             perror("malloc");
72             err = 1;
73             goto next;
74         }
75         enclen = i2d(cert, &bufp);
76         if (len != enclen) {
77             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
78                     enclen, name, len);
79             err = 1;
80             goto next;
81         }
82         enclen = (long) (bufp - buf);
83         if (enclen != len) {
84             fprintf(stderr, "unexpected buffer position after encoding %s\n",
85                     name);
86             err = 1;
87             goto next;
88         }
89         if (memcmp(buf, data, len) != 0) {
90             fprintf(stderr, "encoded content of %s does not match input\n",
91                     name);
92             err = 1;
93             goto next;
94         }
95         OPENSSL_free(buf);
96         buf = NULL;
97
98         /* Test 1-pass encoding into library allocated buffer */
99         enclen = i2d(cert, &buf);
100         if (len != enclen) {
101             fprintf(stderr, "encoded length %ld of %s != input length %ld\n",
102                     enclen, name, len);
103             err = 1;
104             goto next;
105         }
106         if (memcmp(buf, data, len) != 0) {
107             fprintf(stderr, "encoded content of %s does not match input\n",
108                     name);
109             err = 1;
110             goto next;
111         }
112
113         if (trusted) {
114             /* Encode just the cert and compare with initial encoding */
115             OPENSSL_free(buf);
116             buf = NULL;
117
118             /* Test 1-pass encoding into library allocated buffer */
119             enclen = i2d(cert, &buf);
120             if (enclen > len) {
121                 fprintf(stderr, "encoded length %ld of %s > input length %ld\n",
122                         enclen, name, len);
123                 err = 1;
124                 goto next;
125             }
126             if (memcmp(buf, data, enclen) != 0) {
127                 fprintf(stderr, "encoded cert content does not match input\n");
128                 err = 1;
129                 goto next;
130             }
131         }
132
133         /*
134          * If any of these were null, PEM_read() would have failed.
135          */
136     next:
137         X509_free(cert);
138         OPENSSL_free(buf);
139         OPENSSL_free(name);
140         OPENSSL_free(header);
141         OPENSSL_free(data);
142     }
143
144     if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) {
145         /* Reached end of PEM file */
146         if (count > 0) {
147             ERR_clear_error();
148             return 1;
149         }
150     }
151
152     /* Some other PEM read error */
153     return 0;
154 }
155
156 int test_main(int argc, char *argv[])
157 {
158     BIO *bio_err;
159     int ret = 1;
160
161     if (argc < 2)
162         TEST_error("usage: %s certfile...", argv[0]);
163
164     bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
165
166     for (argv++; *argv; argv++) {
167         BIO *f = BIO_new_file(*argv, "r");
168         int ok;
169
170         TEST_check(f != NULL);
171         ok = test_certs(f);
172         BIO_free(f);
173
174         if (!TEST_int_eq(ok, 1))
175             break;
176         ret = 0;
177     }
178     BIO_free(bio_err);
179     return ret;
180 }