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