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