Initial support for enveloped data decrypt. Extent runex.pl to cover these
[openssl.git] / crypto / cms / cms.h
1 /* crypto/cms/cms.h */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54
55 #ifndef HEADER_CMS_H
56 #define HEADER_CMS_H
57
58 #include <openssl/x509.h>
59
60 #ifdef OPENSSL_NO_CMS
61 #error CMS is disabled.
62 #endif
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68
69 typedef struct CMS_ContentInfo_st CMS_ContentInfo;
70 typedef struct CMS_SignerInfo_st CMS_SignerInfo;
71 typedef struct CMS_CertificateChoices CMS_CertificateChoices;
72 typedef struct CMS_RevocationInfoChoice_st CMS_RevocationInfoChoice;
73 typedef struct CMS_RecipientInfo_st CMS_RecipientInfo;
74
75 DECLARE_STACK_OF(CMS_SignerInfo)
76 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
77 DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
78
79 #define CMS_SIGNERINFO_ISSUER_SERIAL    0
80 #define CMS_SIGNERINFO_KEYIDENTIFIER    1
81
82 #define CMS_RECIPINFO_TRANS             0
83 #define CMS_RECIPINFO_AGREE             1
84 #define CMS_RECIPINFO_KEK               2
85 #define CMS_RECIPINFO_PASS              3
86 #define CMS_RECIPINFO_OTHER             4
87
88 /* S/MIME related flags */
89
90 #define CMS_TEXT                        0x1
91 #define CMS_NOCERTS                     0x2
92 #define CMS_NO_CONTENT_VERIFY           0x4
93 #define CMS_NO_ATTR_VERIFY              0x8
94 #define CMS_NOSIGS                      \
95                         (CMS_NO_CONTENT_VERIFY|CMS_NO_ATTR_VERIFY)
96 #define CMS_NOINTERN                    0x10
97 #define CMS_NO_SIGNER_CERT_VERIFY       0x20
98 #define CMS_NOVERIFY                    0x20
99 #define CMS_DETACHED                    0x40
100 #define CMS_BINARY                      0x80
101 #define CMS_NOATTR                      0x100
102 #define CMS_NOSMIMECAP                  0x200
103 #define CMS_NOOLDMIMETYPE               0x400
104 #define CMS_CRLFEOL                     0x800
105 #define CMS_STREAM                      0x1000
106 #define CMS_NOCRL                       0x2000
107 #define CMS_PARTIAL                     0x4000
108 #define CMS_REUSE_DIGEST                0x8000
109 #define CMS_USE_KEYID                   0x10000
110
111 const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms);
112 CMS_ContentInfo *CMS_Data_Create(void);
113
114 BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont);
115 int CMS_dataFinal(CMS_ContentInfo *cms, BIO *bio);
116
117 ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms);
118 int CMS_is_detached(CMS_ContentInfo *cms);
119 int CMS_set_detached(CMS_ContentInfo *cms, int detached);
120
121 #ifdef HEADER_PEM_H
122 DECLARE_PEM_rw_const(CMS, CMS_ContentInfo)
123 #endif
124
125 int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms);
126 CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms);
127 int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms);
128
129 BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms);
130 int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags);
131 int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags);
132 CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont);
133 int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags);
134
135 int CMS_final(CMS_ContentInfo *cms, BIO *data, int flags);
136
137 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
138                                                 BIO *data, unsigned int flags);
139
140 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags);
141 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags);
142
143 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
144                                                         unsigned int flags);
145 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
146                                                         unsigned int flags);
147
148 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
149                                 const unsigned char *key, size_t keylen,
150                                 BIO *dcont, BIO *out, unsigned int flags);
151
152 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
153                                         const unsigned char *key, size_t keylen,
154                                         unsigned int flags);
155
156 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
157                                 const unsigned char *key, size_t keylen);
158
159 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
160                  X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags);
161
162 STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms);
163
164 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in,
165                                 const EVP_CIPHER *cipher, unsigned int flags);
166
167 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert,
168                                 BIO *data, BIO *dcont,
169                                 unsigned int flags);
170
171 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms);
172 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri);
173 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert);
174 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
175                                         EVP_PKEY **pk, X509 **recip,
176                                         X509_ALGOR **palg);
177 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
178                                         ASN1_OCTET_STRING **keyid,
179                                         X509_NAME **issuer, ASN1_INTEGER **sno);
180
181 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
182                                EVP_PKEY *pkey);
183         
184 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
185                                                         unsigned int flags);
186 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags);
187
188 int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid);
189 const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms);
190
191 CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms);
192 int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert);
193 int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert);
194 STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms);
195
196 CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms);
197 int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl);
198 STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms);
199
200 int CMS_SignedData_init(CMS_ContentInfo *cms);
201 CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
202                         X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
203                         unsigned int flags);
204 STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms);
205
206 void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer);
207 int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
208                                         ASN1_OCTET_STRING **keyid,
209                                         X509_NAME **issuer, ASN1_INTEGER **sno);
210 int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert);
211 int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
212                                         unsigned int flags);
213 void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, X509 **signer,
214                                         X509_ALGOR **pdig, X509_ALGOR **psig);
215 int CMS_SignerInfo_sign(CMS_SignerInfo *si);
216 int CMS_SignerInfo_verify(CMS_SignerInfo *si);
217 int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain);
218
219 int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs);
220 int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
221                                 int algnid, int keysize);
222 int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap);
223
224 int CMS_signed_get_attr_count(const CMS_SignerInfo *si);
225 int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
226                           int lastpos);
227 int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, ASN1_OBJECT *obj,
228                           int lastpos);
229 X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc);
230 X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc);
231 int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
232 int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
233                         const ASN1_OBJECT *obj, int type,
234                         const void *bytes, int len);
235 int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
236                         int nid, int type,
237                         const void *bytes, int len);
238 int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
239                         const char *attrname, int type,
240                         const void *bytes, int len);
241 void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
242                                         int lastpos, int type);
243
244 int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si);
245 int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
246                           int lastpos);
247 int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si, ASN1_OBJECT *obj,
248                           int lastpos);
249 X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc);
250 X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc);
251 int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr);
252 int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
253                         const ASN1_OBJECT *obj, int type,
254                         const void *bytes, int len);
255 int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
256                         int nid, int type,
257                         const void *bytes, int len);
258 int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
259                         const char *attrname, int type,
260                         const void *bytes, int len);
261 void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
262                                         int lastpos, int type);
263
264 /* BEGIN ERROR CODES */
265 /* The following lines are auto generated by the script mkerr.pl. Any changes
266  * made after this point may be overwritten when the script is next run.
267  */
268 void ERR_load_CMS_strings(void);
269
270 /* Error codes for the CMS functions. */
271
272 /* Function codes. */
273 #define CMS_F_CHECK_CONTENT                              151
274 #define CMS_F_CMS_ADD1_RECIPIENT_CERT                    99
275 #define CMS_F_CMS_ADD1_SIGNER                            100
276 #define CMS_F_CMS_ADD1_SIGNINGTIME                       101
277 #define CMS_F_CMS_BIO_TO_ENCRYPTEDCONTENT                137
278 #define CMS_F_CMS_BIO_TO_ENCRYPTEDCONTENT_BIO            142
279 #define CMS_F_CMS_COMPRESS                               102
280 #define CMS_F_CMS_COMPRESSEDDATA_CREATE                  103
281 #define CMS_F_CMS_COMPRESSEDDATA_INIT_BIO                104
282 #define CMS_F_CMS_COPY_CONTENT                           105
283 #define CMS_F_CMS_COPY_MESSAGEDIGEST                     106
284 #define CMS_F_CMS_DATA                                   107
285 #define CMS_F_CMS_DATAFINAL                              108
286 #define CMS_F_CMS_DATAINIT                               109
287 #define CMS_F_CMS_DECRYPT                                152
288 #define CMS_F_CMS_DECRYPTEDCONTENT_DECRYPT_BIO           145
289 #define CMS_F_CMS_DECRYPTEDCONTENT_ENCRYPT_BIO           143
290 #define CMS_F_CMS_DIGESTALGORITHM_FIND_CTX               110
291 #define CMS_F_CMS_DIGESTALGORITHM_INIT_BIO               111
292 #define CMS_F_CMS_DIGESTEDDATA_DO_FINAL                  112
293 #define CMS_F_CMS_DIGEST_VERIFY                          113
294 #define CMS_F_CMS_ENCRYPTEDCONTENT_DECRYPT_BIO           146
295 #define CMS_F_CMS_ENCRYPTEDCONTENT_ENCRYPT_BIO           144
296 #define CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO              148
297 #define CMS_F_CMS_ENCRYPTEDCONTENT_TO_BIO                138
298 #define CMS_F_CMS_ENCRYPTEDDATA_DECRYPT                  140
299 #define CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT                  149
300 #define CMS_F_CMS_ENCRYPTEDDATA_INIT_BIO                 147
301 #define CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY                 141
302 #define CMS_F_CMS_ENCRYPTED_DATA_DECRYPT                 139
303 #define CMS_F_CMS_ENVELOPED_DATA_INIT                    114
304 #define CMS_F_CMS_FINAL                                  115
305 #define CMS_F_CMS_GET0_CERTIFICATE_CHOICES               116
306 #define CMS_F_CMS_GET0_CONTENT                           117
307 #define CMS_F_CMS_GET0_ECONTENT_TYPE                     118
308 #define CMS_F_CMS_GET0_ENVELOPED                         119
309 #define CMS_F_CMS_GET0_REVOCATION_CHOICES                120
310 #define CMS_F_CMS_GET0_SIGNED                            121
311 #define CMS_F_CMS_RECIPIENTINFO_DECRYPT                  150
312 #define CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP            122
313 #define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS           123
314 #define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID      124
315 #define CMS_F_CMS_SET1_SIGNERIDENTIFIER                  125
316 #define CMS_F_CMS_SET_DETACHED                           126
317 #define CMS_F_CMS_SIGN                                   127
318 #define CMS_F_CMS_SIGNED_DATA_INIT                       128
319 #define CMS_F_CMS_SIGNERINFO_CONTENT_SIGN                129
320 #define CMS_F_CMS_SIGNERINFO_SIGN                        130
321 #define CMS_F_CMS_SIGNERINFO_VERIFY                      131
322 #define CMS_F_CMS_SIGNERINFO_VERIFY_CERT                 132
323 #define CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT              133
324 #define CMS_F_CMS_STREAM                                 134
325 #define CMS_F_CMS_UNCOMPRESS                             135
326 #define CMS_F_CMS_VERIFY                                 136
327
328 /* Reason codes. */
329 #define CMS_R_ADD_SIGNER_ERROR                           99
330 #define CMS_R_CERTIFICATE_VERIFY_ERROR                   100
331 #define CMS_R_CIPHER_INITIALISATION_ERROR                138
332 #define CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR      139
333 #define CMS_R_CMS_DATAFINAL_ERROR                        101
334 #define CMS_R_CMS_LIB                                    145
335 #define CMS_R_CONTENT_NOT_FOUND                          102
336 #define CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA           103
337 #define CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA            104
338 #define CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA               105
339 #define CMS_R_CONTENT_VERIFY_ERROR                       106
340 #define CMS_R_CTRL_ERROR                                 107
341 #define CMS_R_CTRL_FAILURE                               108
342 #define CMS_R_ERROR_GETTING_PUBLIC_KEY                   109
343 #define CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE      110
344 #define CMS_R_INVALID_KEY_LENGTH                         140
345 #define CMS_R_MD_BIO_INIT_ERROR                          111
346 #define CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH       112
347 #define CMS_R_MESSAGEDIGEST_WRONG_LENGTH                 113
348 #define CMS_R_NOT_ENCRYPTED_DATA                         143
349 #define CMS_R_NOT_KEY_TRANSPORT                          114
350 #define CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE            115
351 #define CMS_R_NO_CIPHER                                  144
352 #define CMS_R_NO_CONTENT                                 116
353 #define CMS_R_NO_DEFAULT_DIGEST                          117
354 #define CMS_R_NO_DIGEST_SET                              118
355 #define CMS_R_NO_MATCHING_DIGEST                         119
356 #define CMS_R_NO_MATCHING_RECIPIENT                      147
357 #define CMS_R_NO_PRIVATE_KEY                             120
358 #define CMS_R_NO_PUBLIC_KEY                              121
359 #define CMS_R_NO_SIGNERS                                 122
360 #define CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE     123
361 #define CMS_R_SIGNER_CERTIFICATE_NOT_FOUND               124
362 #define CMS_R_SIGNFINAL_ERROR                            125
363 #define CMS_R_SMIME_TEXT_ERROR                           126
364 #define CMS_R_STORE_INIT_ERROR                           127
365 #define CMS_R_TYPE_NOT_COMPRESSED_DATA                   128
366 #define CMS_R_TYPE_NOT_DATA                              129
367 #define CMS_R_TYPE_NOT_DIGESTED_DATA                     130
368 #define CMS_R_TYPE_NOT_ENCRYPTED_DATA                    142
369 #define CMS_R_TYPE_NOT_ENVELOPED_DATA                    146
370 #define CMS_R_UNABLE_TO_FINALIZE_CONTEXT                 131
371 #define CMS_R_UNKNOWN_CIPHER                             141
372 #define CMS_R_UNKNOWN_DIGEST_ALGORIHM                    132
373 #define CMS_R_UNKNOWN_ID                                 133
374 #define CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM          134
375 #define CMS_R_UNSUPPORTED_CONTENT_TYPE                   135
376 #define CMS_R_UNSUPPORTED_TYPE                           136
377 #define CMS_R_VERIFICATION_FAILURE                       137
378
379 #ifdef  __cplusplus
380 }
381 #endif
382 #endif