]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/crl2p7.c
Update client fuzz corpus
[thirdparty/openssl.git] / apps / crl2p7.c
CommitLineData
0f113f3e 1/*
846e33c7
RS
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
0f113f3e 8 */
d02b48c6
RE
9
10#include <stdio.h>
11#include <string.h>
12#include <sys/types.h>
d02b48c6 13#include "apps.h"
ec577822
BM
14#include <openssl/err.h>
15#include <openssl/evp.h>
16#include <openssl/x509.h>
17#include <openssl/pkcs7.h>
18#include <openssl/pem.h>
19#include <openssl/objects.h>
d02b48c6 20
f73e07cf 21static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
d02b48c6 22
7e1b7485
RS
23typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE
26} OPTION_CHOICE;
d02b48c6 27
44c83ebd 28const OPTIONS crl2pkcs7_options[] = {
7e1b7485
RS
29 {"help", OPT_HELP, '-', "Display this summary"},
30 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
31 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
32 {"in", OPT_IN, '<', "Input file"},
33 {"out", OPT_OUT, '>', "Output file"},
34 {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"},
35 {"certfile", OPT_CERTFILE, '<',
36 "File of chain of certs to a trusted CA; can be repeated"},
37 {NULL}
38};
667ac4ec 39
7e1b7485 40int crl2pkcs7_main(int argc, char **argv)
0f113f3e 41{
0f113f3e 42 BIO *in = NULL, *out = NULL;
0f113f3e
MC
43 PKCS7 *p7 = NULL;
44 PKCS7_SIGNED *p7s = NULL;
0f113f3e 45 STACK_OF(OPENSSL_STRING) *certflst = NULL;
0f113f3e 46 STACK_OF(X509) *cert_stack = NULL;
7e1b7485
RS
47 STACK_OF(X509_CRL) *crl_stack = NULL;
48 X509_CRL *crl = NULL;
49 char *infile = NULL, *outfile = NULL, *prog, *certfile;
50 int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl =
51 0;
52 OPTION_CHOICE o;
d02b48c6 53
7e1b7485
RS
54 prog = opt_init(argc, argv, crl2pkcs7_options);
55 while ((o = opt_next()) != OPT_EOF) {
56 switch (o) {
57 case OPT_EOF:
58 case OPT_ERR:
59 opthelp:
60 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
61 goto end;
62 case OPT_HELP:
63 opt_help(crl2pkcs7_options);
64 ret = 0;
65 goto end;
66 case OPT_INFORM:
67 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
68 goto opthelp;
69 break;
70 case OPT_OUTFORM:
71 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
72 goto opthelp;
73 break;
74 case OPT_IN:
75 infile = opt_arg();
76 break;
77 case OPT_OUT:
78 outfile = opt_arg();
79 break;
80 case OPT_NOCRL:
0f113f3e 81 nocrl = 1;
7e1b7485
RS
82 break;
83 case OPT_CERTFILE:
75ebbd9a
RS
84 if ((certflst == NULL)
85 && (certflst = sk_OPENSSL_STRING_new_null()) == NULL)
0f113f3e 86 goto end;
d285b541 87 if (!sk_OPENSSL_STRING_push(certflst, opt_arg()))
0f113f3e 88 goto end;
0f113f3e
MC
89 break;
90 }
0f113f3e 91 }
7e1b7485 92 argc = opt_num_rest();
03358517
KR
93 if (argc != 0)
94 goto opthelp;
d02b48c6 95
0f113f3e 96 if (!nocrl) {
bdd58d98 97 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
98 if (in == NULL)
99 goto end;
d02b48c6 100
0f113f3e
MC
101 if (informat == FORMAT_ASN1)
102 crl = d2i_X509_CRL_bio(in, NULL);
103 else if (informat == FORMAT_PEM)
104 crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
0f113f3e
MC
105 if (crl == NULL) {
106 BIO_printf(bio_err, "unable to load CRL\n");
107 ERR_print_errors(bio_err);
108 goto end;
109 }
110 }
d02b48c6 111
0f113f3e
MC
112 if ((p7 = PKCS7_new()) == NULL)
113 goto end;
114 if ((p7s = PKCS7_SIGNED_new()) == NULL)
115 goto end;
116 p7->type = OBJ_nid2obj(NID_pkcs7_signed);
117 p7->d.sign = p7s;
118 p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data);
d02b48c6 119
0f113f3e
MC
120 if (!ASN1_INTEGER_set(p7s->version, 1))
121 goto end;
122 if ((crl_stack = sk_X509_CRL_new_null()) == NULL)
123 goto end;
124 p7s->crl = crl_stack;
125 if (crl != NULL) {
126 sk_X509_CRL_push(crl_stack, crl);
127 crl = NULL; /* now part of p7 for OPENSSL_freeing */
128 }
d02b48c6 129
0f113f3e
MC
130 if ((cert_stack = sk_X509_new_null()) == NULL)
131 goto end;
132 p7s->cert = cert_stack;
9fe84296 133
0f113f3e
MC
134 if (certflst)
135 for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
136 certfile = sk_OPENSSL_STRING_value(certflst, i);
137 if (add_certs_from_file(cert_stack, certfile) < 0) {
138 BIO_printf(bio_err, "error loading certificates\n");
139 ERR_print_errors(bio_err);
140 goto end;
141 }
142 }
d02b48c6 143
bdd58d98 144 out = bio_open_default(outfile, 'w', outformat);
7e1b7485
RS
145 if (out == NULL)
146 goto end;
d02b48c6 147
0f113f3e
MC
148 if (outformat == FORMAT_ASN1)
149 i = i2d_PKCS7_bio(out, p7);
150 else if (outformat == FORMAT_PEM)
151 i = PEM_write_bio_PKCS7(out, p7);
0f113f3e
MC
152 if (!i) {
153 BIO_printf(bio_err, "unable to write pkcs7 object\n");
154 ERR_print_errors(bio_err);
155 goto end;
156 }
157 ret = 0;
158 end:
1c422164 159 sk_OPENSSL_STRING_free(certflst);
ca3a82c3
RS
160 BIO_free(in);
161 BIO_free_all(out);
e0e920b1 162 PKCS7_free(p7);
222561fe 163 X509_CRL_free(crl);
d02b48c6 164
7e1b7485 165 return (ret);
0f113f3e 166}
d02b48c6 167
c80fd6b2 168/*-
d02b48c6
RE
169 *----------------------------------------------------------------------
170 * int add_certs_from_file
171 *
0f113f3e 172 * Read a list of certificates to be checked from a file.
d02b48c6
RE
173 *
174 * Results:
0f113f3e 175 * number of certs added if successful, -1 if not.
d02b48c6
RE
176 *----------------------------------------------------------------------
177 */
6b691a5c 178static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
0f113f3e
MC
179{
180 BIO *in = NULL;
181 int count = 0;
182 int ret = -1;
183 STACK_OF(X509_INFO) *sk = NULL;
184 X509_INFO *xi;
d02b48c6 185
7e1b7485
RS
186 in = BIO_new_file(certfile, "r");
187 if (in == NULL) {
0f113f3e
MC
188 BIO_printf(bio_err, "error opening the file, %s\n", certfile);
189 goto end;
190 }
d02b48c6 191
0f113f3e
MC
192 /* This loads from a file, a stack of x509/crl/pkey sets */
193 sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
194 if (sk == NULL) {
195 BIO_printf(bio_err, "error reading the file, %s\n", certfile);
196 goto end;
197 }
d02b48c6 198
0f113f3e
MC
199 /* scan over it and pull out the CRL's */
200 while (sk_X509_INFO_num(sk)) {
201 xi = sk_X509_INFO_shift(sk);
202 if (xi->x509 != NULL) {
203 sk_X509_push(stack, xi->x509);
204 xi->x509 = NULL;
205 count++;
206 }
207 X509_INFO_free(xi);
208 }
d02b48c6 209
0f113f3e
MC
210 ret = count;
211 end:
212 /* never need to OPENSSL_free x */
ca3a82c3 213 BIO_free(in);
222561fe 214 sk_X509_INFO_free(sk);
0f113f3e
MC
215 return (ret);
216}