2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 # define OPENSSL_CMS_H
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
19 # include <openssl/opensslconf.h>
21 # ifndef OPENSSL_NO_CMS
22 # include <openssl/x509.h>
23 # include <openssl/x509v3.h>
24 # include <openssl/cmserr.h>
29 typedef struct CMS_ContentInfo_st CMS_ContentInfo;
30 typedef struct CMS_SignerInfo_st CMS_SignerInfo;
31 typedef struct CMS_CertificateChoices CMS_CertificateChoices;
32 typedef struct CMS_RevocationInfoChoice_st CMS_RevocationInfoChoice;
33 typedef struct CMS_RecipientInfo_st CMS_RecipientInfo;
34 typedef struct CMS_ReceiptRequest_st CMS_ReceiptRequest;
35 typedef struct CMS_Receipt_st CMS_Receipt;
36 typedef struct CMS_RecipientEncryptedKey_st CMS_RecipientEncryptedKey;
37 typedef struct CMS_OtherKeyAttribute_st CMS_OtherKeyAttribute;
39 DEFINE_OR_DECLARE_STACK_OF(CMS_SignerInfo)
40 DEFINE_OR_DECLARE_STACK_OF(CMS_RecipientEncryptedKey)
41 DEFINE_OR_DECLARE_STACK_OF(CMS_RecipientInfo)
42 DEFINE_OR_DECLARE_STACK_OF(CMS_RevocationInfoChoice)
44 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
45 DECLARE_ASN1_FUNCTIONS(CMS_ReceiptRequest)
46 DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
48 CMS_ContentInfo *CMS_ContentInfo_new_with_libctx(OPENSSL_CTX *libctx,
51 # define CMS_SIGNERINFO_ISSUER_SERIAL 0
52 # define CMS_SIGNERINFO_KEYIDENTIFIER 1
54 # define CMS_RECIPINFO_NONE -1
55 # define CMS_RECIPINFO_TRANS 0
56 # define CMS_RECIPINFO_AGREE 1
57 # define CMS_RECIPINFO_KEK 2
58 # define CMS_RECIPINFO_PASS 3
59 # define CMS_RECIPINFO_OTHER 4
61 /* S/MIME related flags */
64 # define CMS_NOCERTS 0x2
65 # define CMS_NO_CONTENT_VERIFY 0x4
66 # define CMS_NO_ATTR_VERIFY 0x8
68 (CMS_NO_CONTENT_VERIFY|CMS_NO_ATTR_VERIFY)
69 # define CMS_NOINTERN 0x10
70 # define CMS_NO_SIGNER_CERT_VERIFY 0x20
71 # define CMS_NOVERIFY 0x20
72 # define CMS_DETACHED 0x40
73 # define CMS_BINARY 0x80
74 # define CMS_NOATTR 0x100
75 # define CMS_NOSMIMECAP 0x200
76 # define CMS_NOOLDMIMETYPE 0x400
77 # define CMS_CRLFEOL 0x800
78 # define CMS_STREAM 0x1000
79 # define CMS_NOCRL 0x2000
80 # define CMS_PARTIAL 0x4000
81 # define CMS_REUSE_DIGEST 0x8000
82 # define CMS_USE_KEYID 0x10000
83 # define CMS_DEBUG_DECRYPT 0x20000
84 # define CMS_KEY_PARAM 0x40000
85 # define CMS_ASCIICRLF 0x80000
86 # define CMS_CADES 0x100000
87 # define CMS_USE_ORIGINATOR_KEYID 0x200000
89 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms);
91 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont);
92 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *bio);
94 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms);
95 int CMS_is_detached(CMS_ContentInfo *cms);
96 int CMS_set_detached(CMS_ContentInfo *cms, int detached);
99 DECLARE_PEM_rw(CMS, CMS_ContentInfo)
101 int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms);
102 CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms);
103 int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms);
105 BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms);
106 int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags);
107 int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
109 CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont);
110 CMS_ContentInfo *SMIME_read_CMS_ex(BIO *bio, BIO **bcont, CMS_ContentInfo **ci);
111 int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags);
113 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont,
116 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
117 STACK_OF(X509) *certs, BIO *data,
119 CMS_ContentInfo *CMS_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
120 STACK_OF(X509) *certs, BIO *data,
122 OPENSSL_CTX *ctx, const char *propq);
124 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
125 X509 *signcert, EVP_PKEY *pkey,
126 STACK_OF(X509) *certs, unsigned int flags);
128 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags);
129 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags);
130 CMS_ContentInfo *CMS_data_create_with_libctx(BIO *in, unsigned int flags,
134 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
136 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
138 CMS_ContentInfo *CMS_digest_create_with_libctx(BIO *in, const EVP_MD *md,
143 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
144 const unsigned char *key, size_t keylen,
145 BIO *dcont, BIO *out, unsigned int flags);
147 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
148 const unsigned char *key,
149 size_t keylen, unsigned int flags);
150 CMS_ContentInfo *CMS_EncryptedData_encrypt_with_libctx(BIO *in,
151 const EVP_CIPHER *cipher,
152 const unsigned char *key,
158 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
159 const unsigned char *key, size_t keylen);
161 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
162 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags);
164 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
165 STACK_OF(X509) *certs,
166 X509_STORE *store, unsigned int flags);
168 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms);
170 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in,
171 const EVP_CIPHER *cipher, unsigned int flags);
172 CMS_ContentInfo *CMS_encrypt_with_libctx(STACK_OF(X509) *certs,
173 BIO *in, const EVP_CIPHER *cipher,
175 OPENSSL_CTX *ctx, const char *propq);
177 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert,
178 BIO *dcont, BIO *out, unsigned int flags);
180 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert);
181 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
182 X509 *cert, X509 *peer);
183 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
184 unsigned char *key, size_t keylen,
185 const unsigned char *id, size_t idlen);
186 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
187 unsigned char *pass, ossl_ssize_t passlen);
189 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms);
190 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri);
191 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri);
192 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher);
194 CMS_AuthEnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
197 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher);
198 CMS_ContentInfo *CMS_EnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
202 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
203 X509 *recip, unsigned int flags);
204 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
205 EVP_PKEY *originatorPrivKey, X509 * originator, unsigned int flags);
206 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey);
207 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert);
208 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
209 EVP_PKEY **pk, X509 **recip,
211 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
212 ASN1_OCTET_STRING **keyid,
216 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
217 unsigned char *key, size_t keylen,
218 unsigned char *id, size_t idlen,
219 ASN1_GENERALIZEDTIME *date,
220 ASN1_OBJECT *otherTypeId,
221 ASN1_TYPE *otherType);
223 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
225 ASN1_OCTET_STRING **pid,
226 ASN1_GENERALIZEDTIME **pdate,
227 ASN1_OBJECT **potherid,
228 ASN1_TYPE **pothertype);
230 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
231 unsigned char *key, size_t keylen);
233 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
234 const unsigned char *id, size_t idlen);
236 int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
238 ossl_ssize_t passlen);
240 CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
241 int iter, int wrap_nid,
244 ossl_ssize_t passlen,
245 const EVP_CIPHER *kekciph);
247 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri);
248 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri);
250 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
252 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags);
254 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid);
255 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms);
257 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms);
258 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert);
259 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert);
260 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms);
262 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms);
263 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl);
264 int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl);
265 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms);
267 int CMS_SignedData_init(CMS_ContentInfo *cms);
268 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
269 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
271 EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si);
272 EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si);
273 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms);
275 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer);
276 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
277 ASN1_OCTET_STRING **keyid,
278 X509_NAME **issuer, ASN1_INTEGER **sno);
279 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert);
280 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
282 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
283 X509 **signer, X509_ALGOR **pdig,
285 ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si);
286 int CMS_SignerInfo_sign(CMS_SignerInfo *si);
287 int CMS_SignerInfo_verify(CMS_SignerInfo *si);
288 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain);
290 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs);
291 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
292 int algnid, int keysize);
293 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap);
295 int CMS_signed_get_attr_count(const CMS_SignerInfo *si);
296 int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
298 int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
300 X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc);
301 X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc);
302 int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
303 int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
304 const ASN1_OBJECT *obj, int type,
305 const void *bytes, int len);
306 int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
308 const void *bytes, int len);
309 int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
310 const char *attrname, int type,
311 const void *bytes, int len);
312 void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid,
313 int lastpos, int type);
315 int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si);
316 int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
318 int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
319 const ASN1_OBJECT *obj, int lastpos);
320 X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc);
321 X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc);
322 int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
323 int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
324 const ASN1_OBJECT *obj, int type,
325 const void *bytes, int len);
326 int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
328 const void *bytes, int len);
329 int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
330 const char *attrname, int type,
331 const void *bytes, int len);
332 void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
333 int lastpos, int type);
335 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr);
336 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
337 unsigned char *id, int idlen, int allorfirst,
338 STACK_OF(GENERAL_NAMES) *receiptList,
339 STACK_OF(GENERAL_NAMES) *receiptsTo);
340 CMS_ReceiptRequest *CMS_ReceiptRequest_create0_with_libctx(
341 unsigned char *id, int idlen, int allorfirst,
342 STACK_OF(GENERAL_NAMES) *receiptList,
343 STACK_OF(GENERAL_NAMES) *receiptsTo,
344 OPENSSL_CTX *ctx, const char *propq);
346 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr);
347 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
350 STACK_OF(GENERAL_NAMES) **plist,
351 STACK_OF(GENERAL_NAMES) **prto);
352 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
354 ASN1_OCTET_STRING **pukm);
355 STACK_OF(CMS_RecipientEncryptedKey)
356 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri);
358 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
360 ASN1_BIT_STRING **pubkey,
361 ASN1_OCTET_STRING **keyid,
365 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert);
367 int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
368 ASN1_OCTET_STRING **keyid,
369 ASN1_GENERALIZEDTIME **tm,
370 CMS_OtherKeyAttribute **other,
371 X509_NAME **issuer, ASN1_INTEGER **sno);
372 int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
374 int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk);
375 int CMS_RecipientInfo_kari_set0_pkey_and_peer(CMS_RecipientInfo *ri, EVP_PKEY *pk, X509 *peer);
376 EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri);
377 int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
378 CMS_RecipientInfo *ri,
379 CMS_RecipientEncryptedKey *rek);
381 int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg,
382 ASN1_OCTET_STRING *ukm, int keylen);
384 /* Backward compatibility for spelling errors. */
385 # define CMS_R_UNKNOWN_DIGEST_ALGORITM CMS_R_UNKNOWN_DIGEST_ALGORITHM
386 # define CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE \
387 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE