1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <stdlib.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include <time.h>
14e1051a39Sopenharmony_ci#include "apps.h"
15e1051a39Sopenharmony_ci#include "progs.h"
16e1051a39Sopenharmony_ci#include <openssl/err.h>
17e1051a39Sopenharmony_ci#include <openssl/objects.h>
18e1051a39Sopenharmony_ci#include <openssl/evp.h>
19e1051a39Sopenharmony_ci#include <openssl/x509.h>
20e1051a39Sopenharmony_ci#include <openssl/pkcs7.h>
21e1051a39Sopenharmony_ci#include <openssl/pem.h>
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_citypedef enum OPTION_choice {
24e1051a39Sopenharmony_ci    OPT_COMMON,
25e1051a39Sopenharmony_ci    OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
26e1051a39Sopenharmony_ci    OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE,
27e1051a39Sopenharmony_ci    OPT_PROV_ENUM
28e1051a39Sopenharmony_ci} OPTION_CHOICE;
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciconst OPTIONS pkcs7_options[] = {
31e1051a39Sopenharmony_ci    OPT_SECTION("General"),
32e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
33e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
34e1051a39Sopenharmony_ci    {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
35e1051a39Sopenharmony_ci#endif
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_ci    OPT_SECTION("Input"),
38e1051a39Sopenharmony_ci    {"in", OPT_IN, '<', "Input file"},
39e1051a39Sopenharmony_ci    {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ci    OPT_SECTION("Output"),
42e1051a39Sopenharmony_ci    {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
43e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "Output file"},
44e1051a39Sopenharmony_ci    {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
45e1051a39Sopenharmony_ci    {"text", OPT_TEXT, '-', "Print full details of certificates"},
46e1051a39Sopenharmony_ci    {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
47e1051a39Sopenharmony_ci    {"print_certs", OPT_PRINT_CERTS, '-',
48e1051a39Sopenharmony_ci     "Print_certs  print any certs or crl in the input"},
49e1051a39Sopenharmony_ci
50e1051a39Sopenharmony_ci    OPT_PROV_OPTIONS,
51e1051a39Sopenharmony_ci    {NULL}
52e1051a39Sopenharmony_ci};
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ciint pkcs7_main(int argc, char **argv)
55e1051a39Sopenharmony_ci{
56e1051a39Sopenharmony_ci    ENGINE *e = NULL;
57e1051a39Sopenharmony_ci    PKCS7 *p7 = NULL, *p7i;
58e1051a39Sopenharmony_ci    BIO *in = NULL, *out = NULL;
59e1051a39Sopenharmony_ci    int informat = FORMAT_PEM, outformat = FORMAT_PEM;
60e1051a39Sopenharmony_ci    char *infile = NULL, *outfile = NULL, *prog;
61e1051a39Sopenharmony_ci    int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
62e1051a39Sopenharmony_ci    OPTION_CHOICE o;
63e1051a39Sopenharmony_ci    OSSL_LIB_CTX *libctx = app_get0_libctx();
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, pkcs7_options);
66e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
67e1051a39Sopenharmony_ci        switch (o) {
68e1051a39Sopenharmony_ci        case OPT_EOF:
69e1051a39Sopenharmony_ci        case OPT_ERR:
70e1051a39Sopenharmony_ci opthelp:
71e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
72e1051a39Sopenharmony_ci            goto end;
73e1051a39Sopenharmony_ci        case OPT_HELP:
74e1051a39Sopenharmony_ci            opt_help(pkcs7_options);
75e1051a39Sopenharmony_ci            ret = 0;
76e1051a39Sopenharmony_ci            goto end;
77e1051a39Sopenharmony_ci        case OPT_INFORM:
78e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
79e1051a39Sopenharmony_ci                goto opthelp;
80e1051a39Sopenharmony_ci            break;
81e1051a39Sopenharmony_ci        case OPT_OUTFORM:
82e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
83e1051a39Sopenharmony_ci                goto opthelp;
84e1051a39Sopenharmony_ci            break;
85e1051a39Sopenharmony_ci        case OPT_IN:
86e1051a39Sopenharmony_ci            infile = opt_arg();
87e1051a39Sopenharmony_ci            break;
88e1051a39Sopenharmony_ci        case OPT_OUT:
89e1051a39Sopenharmony_ci            outfile = opt_arg();
90e1051a39Sopenharmony_ci            break;
91e1051a39Sopenharmony_ci        case OPT_NOOUT:
92e1051a39Sopenharmony_ci            noout = 1;
93e1051a39Sopenharmony_ci            break;
94e1051a39Sopenharmony_ci        case OPT_TEXT:
95e1051a39Sopenharmony_ci            text = 1;
96e1051a39Sopenharmony_ci            break;
97e1051a39Sopenharmony_ci        case OPT_PRINT:
98e1051a39Sopenharmony_ci            p7_print = 1;
99e1051a39Sopenharmony_ci            break;
100e1051a39Sopenharmony_ci        case OPT_PRINT_CERTS:
101e1051a39Sopenharmony_ci            print_certs = 1;
102e1051a39Sopenharmony_ci            break;
103e1051a39Sopenharmony_ci        case OPT_ENGINE:
104e1051a39Sopenharmony_ci            e = setup_engine(opt_arg(), 0);
105e1051a39Sopenharmony_ci            break;
106e1051a39Sopenharmony_ci        case OPT_PROV_CASES:
107e1051a39Sopenharmony_ci            if (!opt_provider(o))
108e1051a39Sopenharmony_ci                goto end;
109e1051a39Sopenharmony_ci            break;
110e1051a39Sopenharmony_ci        }
111e1051a39Sopenharmony_ci    }
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci    /* No extra arguments. */
114e1051a39Sopenharmony_ci    argc = opt_num_rest();
115e1051a39Sopenharmony_ci    if (argc != 0)
116e1051a39Sopenharmony_ci        goto opthelp;
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ci    in = bio_open_default(infile, 'r', informat);
119e1051a39Sopenharmony_ci    if (in == NULL)
120e1051a39Sopenharmony_ci        goto end;
121e1051a39Sopenharmony_ci
122e1051a39Sopenharmony_ci    p7 = PKCS7_new_ex(libctx, app_get0_propq());
123e1051a39Sopenharmony_ci    if (p7 == NULL) {
124e1051a39Sopenharmony_ci        BIO_printf(bio_err, "unable to allocate PKCS7 object\n");
125e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
126e1051a39Sopenharmony_ci        goto end;
127e1051a39Sopenharmony_ci    }
128e1051a39Sopenharmony_ci
129e1051a39Sopenharmony_ci    if (informat == FORMAT_ASN1)
130e1051a39Sopenharmony_ci        p7i = d2i_PKCS7_bio(in, &p7);
131e1051a39Sopenharmony_ci    else
132e1051a39Sopenharmony_ci        p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
133e1051a39Sopenharmony_ci    if (p7i == NULL) {
134e1051a39Sopenharmony_ci        BIO_printf(bio_err, "unable to load PKCS7 object\n");
135e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
136e1051a39Sopenharmony_ci        goto end;
137e1051a39Sopenharmony_ci    }
138e1051a39Sopenharmony_ci
139e1051a39Sopenharmony_ci    out = bio_open_default(outfile, 'w', outformat);
140e1051a39Sopenharmony_ci    if (out == NULL)
141e1051a39Sopenharmony_ci        goto end;
142e1051a39Sopenharmony_ci
143e1051a39Sopenharmony_ci    if (p7_print)
144e1051a39Sopenharmony_ci        PKCS7_print_ctx(out, p7, 0, NULL);
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    if (print_certs) {
147e1051a39Sopenharmony_ci        STACK_OF(X509) *certs = NULL;
148e1051a39Sopenharmony_ci        STACK_OF(X509_CRL) *crls = NULL;
149e1051a39Sopenharmony_ci
150e1051a39Sopenharmony_ci        i = OBJ_obj2nid(p7->type);
151e1051a39Sopenharmony_ci        switch (i) {
152e1051a39Sopenharmony_ci        case NID_pkcs7_signed:
153e1051a39Sopenharmony_ci            if (p7->d.sign != NULL) {
154e1051a39Sopenharmony_ci                certs = p7->d.sign->cert;
155e1051a39Sopenharmony_ci                crls = p7->d.sign->crl;
156e1051a39Sopenharmony_ci            }
157e1051a39Sopenharmony_ci            break;
158e1051a39Sopenharmony_ci        case NID_pkcs7_signedAndEnveloped:
159e1051a39Sopenharmony_ci            if (p7->d.signed_and_enveloped != NULL) {
160e1051a39Sopenharmony_ci                certs = p7->d.signed_and_enveloped->cert;
161e1051a39Sopenharmony_ci                crls = p7->d.signed_and_enveloped->crl;
162e1051a39Sopenharmony_ci            }
163e1051a39Sopenharmony_ci            break;
164e1051a39Sopenharmony_ci        default:
165e1051a39Sopenharmony_ci            break;
166e1051a39Sopenharmony_ci        }
167e1051a39Sopenharmony_ci
168e1051a39Sopenharmony_ci        if (certs != NULL) {
169e1051a39Sopenharmony_ci            X509 *x;
170e1051a39Sopenharmony_ci
171e1051a39Sopenharmony_ci            for (i = 0; i < sk_X509_num(certs); i++) {
172e1051a39Sopenharmony_ci                x = sk_X509_value(certs, i);
173e1051a39Sopenharmony_ci                if (text)
174e1051a39Sopenharmony_ci                    X509_print(out, x);
175e1051a39Sopenharmony_ci                else
176e1051a39Sopenharmony_ci                    dump_cert_text(out, x);
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci                if (!noout)
179e1051a39Sopenharmony_ci                    PEM_write_bio_X509(out, x);
180e1051a39Sopenharmony_ci                BIO_puts(out, "\n");
181e1051a39Sopenharmony_ci            }
182e1051a39Sopenharmony_ci        }
183e1051a39Sopenharmony_ci        if (crls != NULL) {
184e1051a39Sopenharmony_ci            X509_CRL *crl;
185e1051a39Sopenharmony_ci
186e1051a39Sopenharmony_ci            for (i = 0; i < sk_X509_CRL_num(crls); i++) {
187e1051a39Sopenharmony_ci                crl = sk_X509_CRL_value(crls, i);
188e1051a39Sopenharmony_ci
189e1051a39Sopenharmony_ci                X509_CRL_print_ex(out, crl, get_nameopt());
190e1051a39Sopenharmony_ci
191e1051a39Sopenharmony_ci                if (!noout)
192e1051a39Sopenharmony_ci                    PEM_write_bio_X509_CRL(out, crl);
193e1051a39Sopenharmony_ci                BIO_puts(out, "\n");
194e1051a39Sopenharmony_ci            }
195e1051a39Sopenharmony_ci        }
196e1051a39Sopenharmony_ci
197e1051a39Sopenharmony_ci        ret = 0;
198e1051a39Sopenharmony_ci        goto end;
199e1051a39Sopenharmony_ci    }
200e1051a39Sopenharmony_ci
201e1051a39Sopenharmony_ci    if (!noout) {
202e1051a39Sopenharmony_ci        if (outformat == FORMAT_ASN1)
203e1051a39Sopenharmony_ci            i = i2d_PKCS7_bio(out, p7);
204e1051a39Sopenharmony_ci        else
205e1051a39Sopenharmony_ci            i = PEM_write_bio_PKCS7(out, p7);
206e1051a39Sopenharmony_ci
207e1051a39Sopenharmony_ci        if (!i) {
208e1051a39Sopenharmony_ci            BIO_printf(bio_err, "unable to write pkcs7 object\n");
209e1051a39Sopenharmony_ci            ERR_print_errors(bio_err);
210e1051a39Sopenharmony_ci            goto end;
211e1051a39Sopenharmony_ci        }
212e1051a39Sopenharmony_ci    }
213e1051a39Sopenharmony_ci    ret = 0;
214e1051a39Sopenharmony_ci end:
215e1051a39Sopenharmony_ci    PKCS7_free(p7);
216e1051a39Sopenharmony_ci    release_engine(e);
217e1051a39Sopenharmony_ci    BIO_free(in);
218e1051a39Sopenharmony_ci    BIO_free_all(out);
219e1051a39Sopenharmony_ci    return ret;
220e1051a39Sopenharmony_ci}
221