check_chain_extensions(): Add check that Basic Constraints of CA cert are marked...
[openssl.git] / include / openssl / cms.h
1 /*
2  * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #ifndef OPENSSL_CMS_H
11 # define OPENSSL_CMS_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 #  define HEADER_CMS_H
17 # endif
18
19 # include <openssl/opensslconf.h>
20
21 # ifndef OPENSSL_NO_CMS
22 # include <openssl/x509.h>
23 # include <openssl/x509v3.h>
24 # include <openssl/cmserr.h>
25 # ifdef __cplusplus
26 extern "C" {
27 # endif
28
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;
38
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)
43
44 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
45 DECLARE_ASN1_FUNCTIONS(CMS_ReceiptRequest)
46 DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
47
48 CMS_ContentInfo *CMS_ContentInfo_new_with_libctx(OPENSSL_CTX *libctx,
49                                                  const char *propq);
50
51 # define CMS_SIGNERINFO_ISSUER_SERIAL    0
52 # define CMS_SIGNERINFO_KEYIDENTIFIER    1
53
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
60
61 /* S/MIME related flags */
62
63 # define CMS_TEXT                        0x1
64 # define CMS_NOCERTS                     0x2
65 # define CMS_NO_CONTENT_VERIFY           0x4
66 # define CMS_NO_ATTR_VERIFY              0x8
67 # define CMS_NOSIGS                      \
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
88
89 const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms);
90
91 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont);
92 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *bio);
93
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);
97
98 # ifdef OPENSSL_PEM_H
99 DECLARE_PEM_rw(CMS, CMS_ContentInfo)
100 # endif
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);
104
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,
108                              int flags);
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);
112
113 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont,
114               unsigned int flags);
115
116 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
117                           STACK_OF(X509) *certs, BIO *data,
118                           unsigned int flags);
119 CMS_ContentInfo *CMS_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
120                                       STACK_OF(X509) *certs, BIO *data,
121                                       unsigned int flags,
122                                       OPENSSL_CTX *ctx, const char *propq);
123
124 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
125                                   X509 *signcert, EVP_PKEY *pkey,
126                                   STACK_OF(X509) *certs, unsigned int flags);
127
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,
131                                              OPENSSL_CTX *ctx,
132                                              const char *propq);
133
134 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
135                       unsigned int flags);
136 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
137                                    unsigned int flags);
138 CMS_ContentInfo *CMS_digest_create_with_libctx(BIO *in, const EVP_MD *md,
139                                                unsigned int flags,
140                                                OPENSSL_CTX *ctx,
141                                                const char *propq);
142
143 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
144                               const unsigned char *key, size_t keylen,
145                               BIO *dcont, BIO *out, unsigned int flags);
146
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,
153                                                        size_t keylen,
154                                                        unsigned int flags,
155                                                        OPENSSL_CTX *ctx,
156                                                        const char *propq);
157
158 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
159                                const unsigned char *key, size_t keylen);
160
161 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
162                X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags);
163
164 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
165                        STACK_OF(X509) *certs,
166                        X509_STORE *store, unsigned int flags);
167
168 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms);
169
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,
174                                          unsigned int flags,
175                                          OPENSSL_CTX *ctx, const char *propq);
176
177 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert,
178                 BIO *dcont, BIO *out, unsigned int flags);
179
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);
188
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);
193 CMS_ContentInfo *
194 CMS_AuthEnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
195                                          OPENSSL_CTX *ctx,
196                                          const char *propq);
197 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher);
198 CMS_ContentInfo *CMS_EnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
199                                                       OPENSSL_CTX *ctx,
200                                                       const char *propq);
201
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,
210                                      X509_ALGOR **palg);
211 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
212                                           ASN1_OCTET_STRING **keyid,
213                                           X509_NAME **issuer,
214                                           ASN1_INTEGER **sno);
215
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);
222
223 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
224                                     X509_ALGOR **palg,
225                                     ASN1_OCTET_STRING **pid,
226                                     ASN1_GENERALIZEDTIME **pdate,
227                                     ASN1_OBJECT **potherid,
228                                     ASN1_TYPE **pothertype);
229
230 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
231                                unsigned char *key, size_t keylen);
232
233 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
234                                    const unsigned char *id, size_t idlen);
235
236 int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
237                                     unsigned char *pass,
238                                     ossl_ssize_t passlen);
239
240 CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
241                                                int iter, int wrap_nid,
242                                                int pbe_nid,
243                                                unsigned char *pass,
244                                                ossl_ssize_t passlen,
245                                                const EVP_CIPHER *kekciph);
246
247 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri);
248 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri);
249
250 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
251                    unsigned int flags);
252 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags);
253
254 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid);
255 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms);
256
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);
261
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);
266
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,
270                                 unsigned int flags);
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);
274
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,
281                            unsigned int flags);
282 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
283                               X509 **signer, X509_ALGOR **pdig,
284                               X509_ALGOR **psig);
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);
289
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);
294
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,
297                                int lastpos);
298 int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
299                                int lastpos);
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,
307                                 int nid, int type,
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);
314
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,
317                                  int lastpos);
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,
327                                   int nid, int type,
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);
334
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);
345
346 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr);
347 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
348                                     ASN1_STRING **pcid,
349                                     int *pallorfirst,
350                                     STACK_OF(GENERAL_NAMES) **plist,
351                                     STACK_OF(GENERAL_NAMES) **prto);
352 int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
353                                     X509_ALGOR **palg,
354                                     ASN1_OCTET_STRING **pukm);
355 STACK_OF(CMS_RecipientEncryptedKey)
356 *CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri);
357
358 int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
359                                         X509_ALGOR **pubalg,
360                                         ASN1_BIT_STRING **pubkey,
361                                         ASN1_OCTET_STRING **keyid,
362                                         X509_NAME **issuer,
363                                         ASN1_INTEGER **sno);
364
365 int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert);
366
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,
373                                        X509 *cert);
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);
380
381 int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg,
382                           ASN1_OCTET_STRING *ukm, int keylen);
383
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
388
389 #  ifdef  __cplusplus
390 }
391 #  endif
392 # endif
393 #endif