source: rtems-libbsd/freebsd/crypto/openssl/apps/crl2p7.c @ 0a699e7

5-freebsd-126-freebsd-12
Last change on this file since 0a699e7 was 0a699e7, checked in by Christian Mauderer <christian.mauderer@…>, on 03/28/19 at 06:13:59

bin/openssl: Import from FreeBSD.

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