1/* 2 * {- join("\n * ", @autowarntext) -} 3 * 4 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 5 * 6 * Licensed under the Apache License 2.0 (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{- 13use OpenSSL::stackhash qw(generate_stack_macros); 14-} 15 16#ifndef OPENSSL_PKCS7_H 17# define OPENSSL_PKCS7_H 18# pragma once 19 20# include <openssl/macros.h> 21# ifndef OPENSSL_NO_DEPRECATED_3_0 22# define HEADER_PKCS7_H 23# endif 24 25# include <openssl/asn1.h> 26# include <openssl/bio.h> 27# include <openssl/e_os2.h> 28 29# include <openssl/symhacks.h> 30# include <openssl/types.h> 31# include <openssl/pkcs7err.h> 32# ifndef OPENSSL_NO_STDIO 33# include <stdio.h> 34# endif 35 36#ifdef __cplusplus 37extern "C" { 38#endif 39 40 41/*- 42Encryption_ID DES-CBC 43Digest_ID MD5 44Digest_Encryption_ID rsaEncryption 45Key_Encryption_ID rsaEncryption 46*/ 47 48typedef struct PKCS7_CTX_st { 49 OSSL_LIB_CTX *libctx; 50 char *propq; 51} PKCS7_CTX; 52 53typedef struct pkcs7_issuer_and_serial_st { 54 X509_NAME *issuer; 55 ASN1_INTEGER *serial; 56} PKCS7_ISSUER_AND_SERIAL; 57 58typedef struct pkcs7_signer_info_st { 59 ASN1_INTEGER *version; /* version 1 */ 60 PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; 61 X509_ALGOR *digest_alg; 62 STACK_OF(X509_ATTRIBUTE) *auth_attr; /* [ 0 ] */ 63 X509_ALGOR *digest_enc_alg; /* confusing name, actually used for signing */ 64 ASN1_OCTET_STRING *enc_digest; /* confusing name, actually signature */ 65 STACK_OF(X509_ATTRIBUTE) *unauth_attr; /* [ 1 ] */ 66 /* The private key to sign with */ 67 EVP_PKEY *pkey; 68 const PKCS7_CTX *ctx; 69} PKCS7_SIGNER_INFO; 70{- 71 generate_stack_macros("PKCS7_SIGNER_INFO"); 72-} 73 74typedef struct pkcs7_recip_info_st { 75 ASN1_INTEGER *version; /* version 0 */ 76 PKCS7_ISSUER_AND_SERIAL *issuer_and_serial; 77 X509_ALGOR *key_enc_algor; 78 ASN1_OCTET_STRING *enc_key; 79 X509 *cert; /* get the pub-key from this */ 80 const PKCS7_CTX *ctx; 81} PKCS7_RECIP_INFO; 82{- 83 generate_stack_macros("PKCS7_RECIP_INFO"); 84-} 85 86 87typedef struct pkcs7_signed_st { 88 ASN1_INTEGER *version; /* version 1 */ 89 STACK_OF(X509_ALGOR) *md_algs; /* md used */ 90 STACK_OF(X509) *cert; /* [ 0 ] */ /* name should be 'certificates' */ 91 STACK_OF(X509_CRL) *crl; /* [ 1 ] */ /* name should be 'crls' */ 92 STACK_OF(PKCS7_SIGNER_INFO) *signer_info; 93 struct pkcs7_st *contents; 94} PKCS7_SIGNED; 95/* 96 * The above structure is very very similar to PKCS7_SIGN_ENVELOPE. How about 97 * merging the two 98 */ 99 100typedef struct pkcs7_enc_content_st { 101 ASN1_OBJECT *content_type; 102 X509_ALGOR *algorithm; 103 ASN1_OCTET_STRING *enc_data; /* [ 0 ] */ 104 const EVP_CIPHER *cipher; 105 const PKCS7_CTX *ctx; 106} PKCS7_ENC_CONTENT; 107 108typedef struct pkcs7_enveloped_st { 109 ASN1_INTEGER *version; /* version 0 */ 110 STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; 111 PKCS7_ENC_CONTENT *enc_data; 112} PKCS7_ENVELOPE; 113 114typedef struct pkcs7_signedandenveloped_st { 115 ASN1_INTEGER *version; /* version 1 */ 116 STACK_OF(X509_ALGOR) *md_algs; /* md used */ 117 STACK_OF(X509) *cert; /* [ 0 ] */ /* name should be 'certificates' */ 118 STACK_OF(X509_CRL) *crl; /* [ 1 ] */ /* name should be 'crls' */ 119 STACK_OF(PKCS7_SIGNER_INFO) *signer_info; 120 PKCS7_ENC_CONTENT *enc_data; 121 STACK_OF(PKCS7_RECIP_INFO) *recipientinfo; 122} PKCS7_SIGN_ENVELOPE; 123 124typedef struct pkcs7_digest_st { 125 ASN1_INTEGER *version; /* version 0 */ 126 X509_ALGOR *md; /* md used */ 127 struct pkcs7_st *contents; 128 ASN1_OCTET_STRING *digest; 129} PKCS7_DIGEST; 130 131typedef struct pkcs7_encrypted_st { 132 ASN1_INTEGER *version; /* version 0 */ 133 PKCS7_ENC_CONTENT *enc_data; 134} PKCS7_ENCRYPT; 135 136typedef struct pkcs7_st { 137 /* 138 * The following is non NULL if it contains ASN1 encoding of this 139 * structure 140 */ 141 unsigned char *asn1; 142 long length; 143# define PKCS7_S_HEADER 0 144# define PKCS7_S_BODY 1 145# define PKCS7_S_TAIL 2 146 int state; /* used during processing */ 147 int detached; 148 ASN1_OBJECT *type; 149 /* content as defined by the type */ 150 /* 151 * all encryption/message digests are applied to the 'contents', leaving 152 * out the 'type' field. 153 */ 154 union { 155 char *ptr; 156 /* NID_pkcs7_data */ 157 ASN1_OCTET_STRING *data; 158 /* NID_pkcs7_signed */ 159 PKCS7_SIGNED *sign; /* field name 'signed' would clash with C keyword */ 160 /* NID_pkcs7_enveloped */ 161 PKCS7_ENVELOPE *enveloped; 162 /* NID_pkcs7_signedAndEnveloped */ 163 PKCS7_SIGN_ENVELOPE *signed_and_enveloped; 164 /* NID_pkcs7_digest */ 165 PKCS7_DIGEST *digest; 166 /* NID_pkcs7_encrypted */ 167 PKCS7_ENCRYPT *encrypted; 168 /* Anything else */ 169 ASN1_TYPE *other; 170 } d; 171 PKCS7_CTX ctx; 172} PKCS7; 173{- 174 generate_stack_macros("PKCS7"); 175-} 176 177 178# define PKCS7_OP_SET_DETACHED_SIGNATURE 1 179# define PKCS7_OP_GET_DETACHED_SIGNATURE 2 180 181# define PKCS7_get_signed_attributes(si) ((si)->auth_attr) 182# define PKCS7_get_attributes(si) ((si)->unauth_attr) 183 184# define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed) 185# define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted) 186# define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped) 187# define PKCS7_type_is_signedAndEnveloped(a) \ 188 (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped) 189# define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data) 190# define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest) 191 192# define PKCS7_set_detached(p,v) \ 193 PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL) 194# define PKCS7_get_detached(p) \ 195 PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL) 196 197# define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7)) 198 199/* S/MIME related flags */ 200 201# define PKCS7_TEXT 0x1 202# define PKCS7_NOCERTS 0x2 203# define PKCS7_NOSIGS 0x4 204# define PKCS7_NOCHAIN 0x8 205# define PKCS7_NOINTERN 0x10 206# define PKCS7_NOVERIFY 0x20 207# define PKCS7_DETACHED 0x40 208# define PKCS7_BINARY 0x80 209# define PKCS7_NOATTR 0x100 210# define PKCS7_NOSMIMECAP 0x200 211# define PKCS7_NOOLDMIMETYPE 0x400 212# define PKCS7_CRLFEOL 0x800 213# define PKCS7_STREAM 0x1000 214# define PKCS7_NOCRL 0x2000 215# define PKCS7_PARTIAL 0x4000 216# define PKCS7_REUSE_DIGEST 0x8000 217# define PKCS7_NO_DUAL_CONTENT 0x10000 218 219/* Flags: for compatibility with older code */ 220 221# define SMIME_TEXT PKCS7_TEXT 222# define SMIME_NOCERTS PKCS7_NOCERTS 223# define SMIME_NOSIGS PKCS7_NOSIGS 224# define SMIME_NOCHAIN PKCS7_NOCHAIN 225# define SMIME_NOINTERN PKCS7_NOINTERN 226# define SMIME_NOVERIFY PKCS7_NOVERIFY 227# define SMIME_DETACHED PKCS7_DETACHED 228# define SMIME_BINARY PKCS7_BINARY 229# define SMIME_NOATTR PKCS7_NOATTR 230 231/* CRLF ASCII canonicalisation */ 232# define SMIME_ASCIICRLF 0x80000 233 234DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL) 235 236int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data, 237 const EVP_MD *type, unsigned char *md, 238 unsigned int *len); 239# ifndef OPENSSL_NO_STDIO 240PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7); 241int i2d_PKCS7_fp(FILE *fp, const PKCS7 *p7); 242# endif 243DECLARE_ASN1_DUP_FUNCTION(PKCS7) 244PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7); 245int i2d_PKCS7_bio(BIO *bp, const PKCS7 *p7); 246int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); 247int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags); 248 249DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO) 250DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO) 251DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED) 252DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT) 253DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE) 254DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE) 255DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST) 256DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT) 257DECLARE_ASN1_FUNCTIONS(PKCS7) 258PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq); 259 260DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN) 261DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY) 262 263DECLARE_ASN1_NDEF_FUNCTION(PKCS7) 264DECLARE_ASN1_PRINT_FUNCTION(PKCS7) 265 266long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg); 267 268int PKCS7_type_is_other(PKCS7 *p7); 269int PKCS7_set_type(PKCS7 *p7, int type); 270int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other); 271int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); 272int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, 273 const EVP_MD *dgst); 274int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si); 275int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); 276int PKCS7_add_certificate(PKCS7 *p7, X509 *cert); 277int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl); 278int PKCS7_content_new(PKCS7 *p7, int nid); 279int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, 280 BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); 281int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, 282 X509 *signer); 283 284BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio); 285int PKCS7_dataFinal(PKCS7 *p7, BIO *bio); 286BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert); 287 288PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, 289 EVP_PKEY *pkey, const EVP_MD *dgst); 290X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si); 291int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md); 292STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); 293 294PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509); 295void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, 296 X509_ALGOR **pdig, X509_ALGOR **psig); 297void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc); 298int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); 299int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); 300int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); 301int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7); 302 303PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx); 304ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7); 305ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk); 306int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int type, 307 void *data); 308int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype, 309 void *value); 310ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid); 311ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid); 312int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si, 313 STACK_OF(X509_ATTRIBUTE) *sk); 314int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si, 315 STACK_OF(X509_ATTRIBUTE) *sk); 316 317PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 318 BIO *data, int flags); 319PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, 320 BIO *data, int flags, OSSL_LIB_CTX *libctx, 321 const char *propq); 322 323PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, 324 X509 *signcert, EVP_PKEY *pkey, 325 const EVP_MD *md, int flags); 326 327int PKCS7_final(PKCS7 *p7, BIO *data, int flags); 328int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store, 329 BIO *indata, BIO *out, int flags); 330STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, 331 int flags); 332PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher, 333 int flags); 334PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in, 335 const EVP_CIPHER *cipher, int flags, 336 OSSL_LIB_CTX *libctx, const char *propq); 337int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, 338 int flags); 339 340int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si, 341 STACK_OF(X509_ALGOR) *cap); 342STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si); 343int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg); 344 345int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid); 346int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t); 347int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si, 348 const unsigned char *md, int mdlen); 349 350int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags); 351PKCS7 *SMIME_read_PKCS7_ex(BIO *bio, BIO **bcont, PKCS7 **p7); 352PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont); 353 354BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7); 355 356# ifdef __cplusplus 357} 358# endif 359#endif 360