1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-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 "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/x509.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1.h>
14e1051a39Sopenharmony_ci#include "pk7_local.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci/* PKCS#7 wrappers round generalised stream and MIME routines */
17e1051a39Sopenharmony_ci
18e1051a39Sopenharmony_ciint i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags)
19e1051a39Sopenharmony_ci{
20e1051a39Sopenharmony_ci    return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)p7, in, flags,
21e1051a39Sopenharmony_ci                               ASN1_ITEM_rptr(PKCS7));
22e1051a39Sopenharmony_ci}
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ciint PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags)
25e1051a39Sopenharmony_ci{
26e1051a39Sopenharmony_ci    return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)p7, in, flags,
27e1051a39Sopenharmony_ci                                     "PKCS7", ASN1_ITEM_rptr(PKCS7));
28e1051a39Sopenharmony_ci}
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciint SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags)
31e1051a39Sopenharmony_ci{
32e1051a39Sopenharmony_ci    STACK_OF(X509_ALGOR) *mdalgs;
33e1051a39Sopenharmony_ci    int ctype_nid = OBJ_obj2nid(p7->type);
34e1051a39Sopenharmony_ci    const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    if (ctype_nid == NID_pkcs7_signed) {
37e1051a39Sopenharmony_ci        if (p7->d.sign == NULL)
38e1051a39Sopenharmony_ci            return 0;
39e1051a39Sopenharmony_ci        mdalgs = p7->d.sign->md_algs;
40e1051a39Sopenharmony_ci    } else {
41e1051a39Sopenharmony_ci        mdalgs = NULL;
42e1051a39Sopenharmony_ci    }
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    flags ^= SMIME_OLDMIME;
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ci    return SMIME_write_ASN1_ex(bio, (ASN1_VALUE *)p7, data, flags, ctype_nid,
47e1051a39Sopenharmony_ci                               NID_undef, mdalgs, ASN1_ITEM_rptr(PKCS7),
48e1051a39Sopenharmony_ci                               ossl_pkcs7_ctx_get0_libctx(ctx),
49e1051a39Sopenharmony_ci                               ossl_pkcs7_ctx_get0_propq(ctx));
50e1051a39Sopenharmony_ci}
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ciPKCS7 *SMIME_read_PKCS7_ex(BIO *bio, BIO **bcont, PKCS7 **p7)
53e1051a39Sopenharmony_ci{
54e1051a39Sopenharmony_ci    PKCS7 *ret;
55e1051a39Sopenharmony_ci    OSSL_LIB_CTX *libctx = NULL;
56e1051a39Sopenharmony_ci    const char *propq = NULL;
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    if (p7 != NULL && *p7 != NULL) {
59e1051a39Sopenharmony_ci        libctx = (*p7)->ctx.libctx;
60e1051a39Sopenharmony_ci        propq = (*p7)->ctx.propq;
61e1051a39Sopenharmony_ci    }
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    ret = (PKCS7 *)SMIME_read_ASN1_ex(bio, 0, bcont, ASN1_ITEM_rptr(PKCS7),
64e1051a39Sopenharmony_ci                                      (ASN1_VALUE **)p7, libctx, propq);
65e1051a39Sopenharmony_ci    if (ret != NULL)
66e1051a39Sopenharmony_ci        ossl_pkcs7_resolve_libctx(ret);
67e1051a39Sopenharmony_ci    return ret;
68e1051a39Sopenharmony_ci}
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ciPKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont)
71e1051a39Sopenharmony_ci{
72e1051a39Sopenharmony_ci    return SMIME_read_PKCS7_ex(bio, bcont, NULL);
73e1051a39Sopenharmony_ci}
74