Add libctx support to CMS.
[openssl.git] / crypto / cms / cms_local.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 OSSL_CRYPTO_CMS_LOCAL_H
11 # define OSSL_CRYPTO_CMS_LOCAL_H
12
13 # include <openssl/x509.h>
14
15 /*
16  * Cryptographic message syntax (CMS) structures: taken from RFC3852
17  */
18
19 /* Forward references */
20
21 typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
22 typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
23 typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
24 typedef struct CMS_SignedData_st CMS_SignedData;
25 typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
26 typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
27 typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
28 typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
29 typedef struct CMS_DigestedData_st CMS_DigestedData;
30 typedef struct CMS_EncryptedData_st CMS_EncryptedData;
31 typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
32 typedef struct CMS_CompressedData_st CMS_CompressedData;
33 typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
34 typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
35 typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
36 typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
37 typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
38 typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
39 typedef struct CMS_KeyAgreeRecipientIdentifier_st
40     CMS_KeyAgreeRecipientIdentifier;
41 typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
42 typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
43 typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
44 typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
45 typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
46 typedef struct CMS_CTX_st CMS_CTX;
47
48 struct CMS_CTX_st {
49     OPENSSL_CTX *libctx;
50     char *propq;
51 };
52
53 struct CMS_ContentInfo_st {
54     ASN1_OBJECT *contentType;
55     union {
56         ASN1_OCTET_STRING *data;
57         CMS_SignedData *signedData;
58         CMS_EnvelopedData *envelopedData;
59         CMS_DigestedData *digestedData;
60         CMS_EncryptedData *encryptedData;
61         CMS_AuthenticatedData *authenticatedData;
62         CMS_CompressedData *compressedData;
63         ASN1_TYPE *other;
64         /* Other types ... */
65         void *otherData;
66     } d;
67     CMS_CTX ctx;
68 };
69
70 DEFINE_STACK_OF(CMS_CertificateChoices)
71
72 struct CMS_SignedData_st {
73     int32_t version;
74     STACK_OF(X509_ALGOR) *digestAlgorithms;
75     CMS_EncapsulatedContentInfo *encapContentInfo;
76     STACK_OF(CMS_CertificateChoices) *certificates;
77     STACK_OF(CMS_RevocationInfoChoice) *crls;
78     STACK_OF(CMS_SignerInfo) *signerInfos;
79 };
80
81 struct CMS_EncapsulatedContentInfo_st {
82     ASN1_OBJECT *eContentType;
83     ASN1_OCTET_STRING *eContent;
84     /* Set to 1 if incomplete structure only part set up */
85     int partial;
86 };
87
88 struct CMS_SignerInfo_st {
89     int32_t version;
90     CMS_SignerIdentifier *sid;
91     X509_ALGOR *digestAlgorithm;
92     STACK_OF(X509_ATTRIBUTE) *signedAttrs;
93     X509_ALGOR *signatureAlgorithm;
94     ASN1_OCTET_STRING *signature;
95     STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
96     /* Signing certificate and key */
97     X509 *signer;
98     EVP_PKEY *pkey;
99     /* Digest and public key context for alternative parameters */
100     EVP_MD_CTX *mctx;
101     EVP_PKEY_CTX *pctx;
102     const CMS_CTX *cms_ctx;
103 };
104
105 struct CMS_SignerIdentifier_st {
106     int type;
107     union {
108         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
109         ASN1_OCTET_STRING *subjectKeyIdentifier;
110     } d;
111 };
112
113 struct CMS_EnvelopedData_st {
114     int32_t version;
115     CMS_OriginatorInfo *originatorInfo;
116     STACK_OF(CMS_RecipientInfo) *recipientInfos;
117     CMS_EncryptedContentInfo *encryptedContentInfo;
118     STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
119 };
120
121 struct CMS_OriginatorInfo_st {
122     STACK_OF(CMS_CertificateChoices) *certificates;
123     STACK_OF(CMS_RevocationInfoChoice) *crls;
124 };
125
126 struct CMS_EncryptedContentInfo_st {
127     ASN1_OBJECT *contentType;
128     X509_ALGOR *contentEncryptionAlgorithm;
129     ASN1_OCTET_STRING *encryptedContent;
130     /* Content encryption algorithm and key */
131     const EVP_CIPHER *cipher;
132     unsigned char *key;
133     size_t keylen;
134     /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
135     int debug;
136     /* Set to 1 if we have no cert and need extra safety measures for MMA */
137     int havenocert;
138 };
139
140 struct CMS_RecipientInfo_st {
141     int type;
142     union {
143         CMS_KeyTransRecipientInfo *ktri;
144         CMS_KeyAgreeRecipientInfo *kari;
145         CMS_KEKRecipientInfo *kekri;
146         CMS_PasswordRecipientInfo *pwri;
147         CMS_OtherRecipientInfo *ori;
148     } d;
149 };
150
151 typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
152
153 struct CMS_KeyTransRecipientInfo_st {
154     int32_t version;
155     CMS_RecipientIdentifier *rid;
156     X509_ALGOR *keyEncryptionAlgorithm;
157     ASN1_OCTET_STRING *encryptedKey;
158     /* Recipient Key and cert */
159     X509 *recip;
160     EVP_PKEY *pkey;
161     /* Public key context for this operation */
162     EVP_PKEY_CTX *pctx;
163     const CMS_CTX *cms_ctx;
164 };
165
166 struct CMS_KeyAgreeRecipientInfo_st {
167     int32_t version;
168     CMS_OriginatorIdentifierOrKey *originator;
169     ASN1_OCTET_STRING *ukm;
170     X509_ALGOR *keyEncryptionAlgorithm;
171     STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
172     /* Public key context associated with current operation */
173     EVP_PKEY_CTX *pctx;
174     /* Cipher context for CEK wrapping */
175     EVP_CIPHER_CTX *ctx;
176     const CMS_CTX *cms_ctx;
177 };
178
179 struct CMS_OriginatorIdentifierOrKey_st {
180     int type;
181     union {
182         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
183         ASN1_OCTET_STRING *subjectKeyIdentifier;
184         CMS_OriginatorPublicKey *originatorKey;
185     } d;
186 };
187
188 struct CMS_OriginatorPublicKey_st {
189     X509_ALGOR *algorithm;
190     ASN1_BIT_STRING *publicKey;
191 };
192
193 struct CMS_RecipientEncryptedKey_st {
194     CMS_KeyAgreeRecipientIdentifier *rid;
195     ASN1_OCTET_STRING *encryptedKey;
196     /* Public key associated with this recipient */
197     EVP_PKEY *pkey;
198 };
199
200 struct CMS_KeyAgreeRecipientIdentifier_st {
201     int type;
202     union {
203         CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
204         CMS_RecipientKeyIdentifier *rKeyId;
205     } d;
206 };
207
208 struct CMS_RecipientKeyIdentifier_st {
209     ASN1_OCTET_STRING *subjectKeyIdentifier;
210     ASN1_GENERALIZEDTIME *date;
211     CMS_OtherKeyAttribute *other;
212 };
213
214 struct CMS_KEKRecipientInfo_st {
215     int32_t version;
216     CMS_KEKIdentifier *kekid;
217     X509_ALGOR *keyEncryptionAlgorithm;
218     ASN1_OCTET_STRING *encryptedKey;
219     /* Extra info: symmetric key to use */
220     unsigned char *key;
221     size_t keylen;
222     const CMS_CTX *cms_ctx;
223 };
224
225 struct CMS_KEKIdentifier_st {
226     ASN1_OCTET_STRING *keyIdentifier;
227     ASN1_GENERALIZEDTIME *date;
228     CMS_OtherKeyAttribute *other;
229 };
230
231 struct CMS_PasswordRecipientInfo_st {
232     int32_t version;
233     X509_ALGOR *keyDerivationAlgorithm;
234     X509_ALGOR *keyEncryptionAlgorithm;
235     ASN1_OCTET_STRING *encryptedKey;
236     /* Extra info: password to use */
237     unsigned char *pass;
238     size_t passlen;
239     const CMS_CTX *cms_ctx;
240 };
241
242 struct CMS_OtherRecipientInfo_st {
243     ASN1_OBJECT *oriType;
244     ASN1_TYPE *oriValue;
245 };
246
247 struct CMS_DigestedData_st {
248     int32_t version;
249     X509_ALGOR *digestAlgorithm;
250     CMS_EncapsulatedContentInfo *encapContentInfo;
251     ASN1_OCTET_STRING *digest;
252 };
253
254 struct CMS_EncryptedData_st {
255     int32_t version;
256     CMS_EncryptedContentInfo *encryptedContentInfo;
257     STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
258 };
259
260 struct CMS_AuthenticatedData_st {
261     int32_t version;
262     CMS_OriginatorInfo *originatorInfo;
263     STACK_OF(CMS_RecipientInfo) *recipientInfos;
264     X509_ALGOR *macAlgorithm;
265     X509_ALGOR *digestAlgorithm;
266     CMS_EncapsulatedContentInfo *encapContentInfo;
267     STACK_OF(X509_ATTRIBUTE) *authAttrs;
268     ASN1_OCTET_STRING *mac;
269     STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
270 };
271
272 struct CMS_CompressedData_st {
273     int32_t version;
274     X509_ALGOR *compressionAlgorithm;
275     STACK_OF(CMS_RecipientInfo) *recipientInfos;
276     CMS_EncapsulatedContentInfo *encapContentInfo;
277 };
278
279 struct CMS_RevocationInfoChoice_st {
280     int type;
281     union {
282         X509_CRL *crl;
283         CMS_OtherRevocationInfoFormat *other;
284     } d;
285 };
286
287 # define CMS_REVCHOICE_CRL               0
288 # define CMS_REVCHOICE_OTHER             1
289
290 struct CMS_OtherRevocationInfoFormat_st {
291     ASN1_OBJECT *otherRevInfoFormat;
292     ASN1_TYPE *otherRevInfo;
293 };
294
295 struct CMS_CertificateChoices {
296     int type;
297     union {
298         X509 *certificate;
299         ASN1_STRING *extendedCertificate; /* Obsolete */
300         ASN1_STRING *v1AttrCert; /* Left encoded for now */
301         ASN1_STRING *v2AttrCert; /* Left encoded for now */
302         CMS_OtherCertificateFormat *other;
303     } d;
304 };
305
306 # define CMS_CERTCHOICE_CERT             0
307 # define CMS_CERTCHOICE_EXCERT           1
308 # define CMS_CERTCHOICE_V1ACERT          2
309 # define CMS_CERTCHOICE_V2ACERT          3
310 # define CMS_CERTCHOICE_OTHER            4
311
312 struct CMS_OtherCertificateFormat_st {
313     ASN1_OBJECT *otherCertFormat;
314     ASN1_TYPE *otherCert;
315 };
316
317 /*
318  * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
319  * to be independent of PKCS#7
320  */
321
322 struct CMS_IssuerAndSerialNumber_st {
323     X509_NAME *issuer;
324     ASN1_INTEGER *serialNumber;
325 };
326
327 struct CMS_OtherKeyAttribute_st {
328     ASN1_OBJECT *keyAttrId;
329     ASN1_TYPE *keyAttr;
330 };
331
332 /* ESS structures */
333
334 struct CMS_ReceiptRequest_st {
335     ASN1_OCTET_STRING *signedContentIdentifier;
336     CMS_ReceiptsFrom *receiptsFrom;
337     STACK_OF(GENERAL_NAMES) *receiptsTo;
338 };
339
340 struct CMS_ReceiptsFrom_st {
341     int type;
342     union {
343         int32_t allOrFirstTier;
344         STACK_OF(GENERAL_NAMES) *receiptList;
345     } d;
346 };
347
348 struct CMS_Receipt_st {
349     int32_t version;
350     ASN1_OBJECT *contentType;
351     ASN1_OCTET_STRING *signedContentIdentifier;
352     ASN1_OCTET_STRING *originatorSignatureValue;
353 };
354
355 DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
356 DECLARE_ASN1_ITEM(CMS_SignerInfo)
357 DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
358 DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
359 DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
360 DECLARE_ASN1_ITEM(CMS_RecipientInfo)
361 DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
362 DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
363
364 # define CMS_SIGNERINFO_ISSUER_SERIAL    0
365 # define CMS_SIGNERINFO_KEYIDENTIFIER    1
366
367 # define CMS_RECIPINFO_ISSUER_SERIAL     0
368 # define CMS_RECIPINFO_KEYIDENTIFIER     1
369
370 # define CMS_REK_ISSUER_SERIAL           0
371 # define CMS_REK_KEYIDENTIFIER           1
372
373 # define CMS_OIK_ISSUER_SERIAL           0
374 # define CMS_OIK_KEYIDENTIFIER           1
375 # define CMS_OIK_PUBKEY                  2
376
377 BIO *cms_content_bio(CMS_ContentInfo *cms);
378 const CMS_CTX *cms_get0_cmsctx(const CMS_ContentInfo *cms);
379 OPENSSL_CTX *cms_ctx_get0_libctx(const CMS_CTX *ctx);
380 const char *cms_ctx_get0_propq(const CMS_CTX *ctx);
381 void cms_resolve_libctx(CMS_ContentInfo *ci);
382
383 CMS_ContentInfo *cms_Data_create(OPENSSL_CTX *ctx, const char *propq);
384
385 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md,
386                                          OPENSSL_CTX *libctx, const char *propq);
387 BIO *cms_DigestedData_init_bio(const CMS_ContentInfo *cms);
388 int cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, int verify);
389
390 BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
391 int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
392 int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
393                               int type, const CMS_CTX *ctx);
394 int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
395                                         ASN1_OCTET_STRING **keyid,
396                                         X509_NAME **issuer,
397                                         ASN1_INTEGER **sno);
398 int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
399
400 CMS_ContentInfo *cms_CompressedData_create(int comp_nid, OPENSSL_CTX *libctx,
401                                            const char *propq);
402 BIO *cms_CompressedData_init_bio(const CMS_ContentInfo *cms);
403
404 BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm,
405                                   const CMS_CTX *ctx);
406 int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
407                                  X509_ALGOR *mdalg);
408
409 int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
410 int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
411 int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
412 int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
413
414 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
415                                    const CMS_CTX *ctx);
416 BIO *cms_EncryptedData_init_bio(const CMS_ContentInfo *cms);
417 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
418                               const EVP_CIPHER *cipher,
419                               const unsigned char *key, size_t keylen,
420                               const CMS_CTX *ctx);
421
422 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
423 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
424 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
425
426 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
427 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain);
428 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
429
430 /* RecipientInfo routines */
431 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
432 int cms_pkey_get_ri_type(EVP_PKEY *pk);
433 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type);
434
435 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms);
436
437 /* KARI routines */
438 int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
439                                 EVP_PKEY *recipPubKey, X509 *originator,
440                                 EVP_PKEY *originatorPrivKey, unsigned int flags,
441                                 const CMS_CTX *ctx);
442 int cms_RecipientInfo_kari_encrypt(const CMS_ContentInfo *cms,
443                                    CMS_RecipientInfo *ri);
444
445 /* PWRI routines */
446 int cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms,
447                                  CMS_RecipientInfo *ri, int en_de);
448 /* SignerInfo routines */
449 int CMS_si_check_attributes(const CMS_SignerInfo *si);
450 void cms_SignerInfos_set_cmsctx(CMS_ContentInfo *cms);
451
452
453 /* ESS routines */
454 int ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain);
455
456 DECLARE_ASN1_ITEM(CMS_CertificateChoices)
457 DECLARE_ASN1_ITEM(CMS_DigestedData)
458 DECLARE_ASN1_ITEM(CMS_EncryptedData)
459 DECLARE_ASN1_ITEM(CMS_EnvelopedData)
460 DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
461 DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
462 DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
463 DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
464 DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
465 DECLARE_ASN1_ITEM(CMS_Receipt)
466 DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
467 DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
468 DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
469 DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
470 DECLARE_ASN1_ITEM(CMS_SignedData)
471 DECLARE_ASN1_ITEM(CMS_CompressedData)
472
473 #endif