Fix safestack issues in x509.h
[openssl.git] / crypto / cms / cms_env.c
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 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include <openssl/evp.h>
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "crypto/x509.h"
20 #include "cms_local.h"
21
22 DEFINE_STACK_OF(CMS_RecipientInfo)
23 DEFINE_STACK_OF(CMS_RevocationInfoChoice)
24
25 /* CMS EnvelopedData Utilities */
26 static void cms_env_set_version(CMS_EnvelopedData *env);
27
28 #define CMS_ENVELOPED_STANDARD 1
29 #define CMS_ENVELOPED_AUTH     2
30
31 static int cms_get_enveloped_type(const CMS_ContentInfo *cms)
32 {
33     int nid = OBJ_obj2nid(cms->contentType);
34
35     switch (nid) {
36     case NID_pkcs7_enveloped:
37         return CMS_ENVELOPED_STANDARD;
38
39     case NID_id_smime_ct_authEnvelopedData:
40         return CMS_ENVELOPED_AUTH;
41
42     default:
43         CMSerr(0, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
44         return 0;
45     }
46 }
47
48 CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
49 {
50     if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
51         CMSerr(CMS_F_CMS_GET0_ENVELOPED,
52                CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
53         return NULL;
54     }
55     return cms->d.envelopedData;
56 }
57
58 CMS_AuthEnvelopedData *cms_get0_auth_enveloped(CMS_ContentInfo *cms)
59 {
60     if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
61         CMSerr(0, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
62         return NULL;
63     }
64     return cms->d.authEnvelopedData;
65 }
66
67 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
68 {
69     if (cms->d.other == NULL) {
70         cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
71         if (cms->d.envelopedData == NULL) {
72             CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
73             return NULL;
74         }
75         cms->d.envelopedData->version = 0;
76         cms->d.envelopedData->encryptedContentInfo->contentType =
77             OBJ_nid2obj(NID_pkcs7_data);
78         ASN1_OBJECT_free(cms->contentType);
79         cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
80         return cms->d.envelopedData;
81     }
82     return cms_get0_enveloped(cms);
83 }
84
85 static CMS_AuthEnvelopedData *
86 cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
87 {
88     if (cms->d.other == NULL) {
89         cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
90         if (cms->d.authEnvelopedData == NULL) {
91             CMSerr(0, ERR_R_MALLOC_FAILURE);
92             return NULL;
93         }
94         /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
95         cms->d.authEnvelopedData->version = 0;
96         cms->d.authEnvelopedData->authEncryptedContentInfo->contentType =
97             OBJ_nid2obj(NID_pkcs7_data);
98         ASN1_OBJECT_free(cms->contentType);
99         cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
100         return cms->d.authEnvelopedData;
101     }
102     return cms_get0_auth_enveloped(cms);
103 }
104
105 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
106 {
107     EVP_PKEY *pkey;
108     int i;
109     if (ri->type == CMS_RECIPINFO_TRANS)
110         pkey = ri->d.ktri->pkey;
111     else if (ri->type == CMS_RECIPINFO_AGREE) {
112         EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
113
114         if (pctx == NULL)
115             return 0;
116         pkey = EVP_PKEY_CTX_get0_pkey(pctx);
117         if (pkey == NULL)
118             return 0;
119     } else
120         return 0;
121     if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
122         return 1;
123     i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
124     if (i == -2) {
125         CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
126                CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
127         return 0;
128     }
129     if (i <= 0) {
130         CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
131         return 0;
132     }
133     return 1;
134 }
135
136 CMS_EncryptedContentInfo* cms_get0_env_enc_content(const CMS_ContentInfo *cms)
137 {
138     switch (cms_get_enveloped_type(cms)) {
139     case CMS_ENVELOPED_STANDARD:
140         return cms->d.envelopedData->encryptedContentInfo;
141
142     case CMS_ENVELOPED_AUTH:
143         return cms->d.authEnvelopedData->authEncryptedContentInfo;
144
145     default:
146         return NULL;
147     }
148 }
149
150 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
151 {
152     switch (cms_get_enveloped_type(cms)) {
153     case CMS_ENVELOPED_STANDARD:
154         return cms->d.envelopedData->recipientInfos;
155
156     case CMS_ENVELOPED_AUTH:
157         return cms->d.authEnvelopedData->recipientInfos;
158
159     default:
160         return NULL;
161     }
162 }
163
164 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
165 {
166     int i;
167     CMS_RecipientInfo *ri;
168     const CMS_CTX *ctx = cms_get0_cmsctx(cms);
169     STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
170
171     for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
172         ri = sk_CMS_RecipientInfo_value(rinfos, i);
173         if (ri != NULL) {
174             switch (ri->type) {
175             case CMS_RECIPINFO_AGREE:
176                 ri->d.kari->cms_ctx = ctx;
177                 break;
178             case CMS_RECIPINFO_TRANS:
179                 ri->d.ktri->cms_ctx = ctx;
180                 x509_set0_libctx(ri->d.ktri->recip, ctx->libctx, ctx->propq);
181                 break;
182             case CMS_RECIPINFO_KEK:
183                 ri->d.kekri->cms_ctx = ctx;
184                 break;
185             case CMS_RECIPINFO_PASS:
186                 ri->d.pwri->cms_ctx = ctx;
187                 break;
188             default:
189                 break;
190             }
191         }
192     }
193 }
194
195 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
196 {
197     return ri->type;
198 }
199
200 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
201 {
202     if (ri->type == CMS_RECIPINFO_TRANS)
203         return ri->d.ktri->pctx;
204     else if (ri->type == CMS_RECIPINFO_AGREE)
205         return ri->d.kari->pctx;
206     return NULL;
207 }
208
209 CMS_ContentInfo *CMS_EnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
210                                                       OPENSSL_CTX *libctx,
211                                                       const char *propq)
212 {
213     CMS_ContentInfo *cms;
214     CMS_EnvelopedData *env;
215
216     cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
217     if (cms == NULL)
218         goto merr;
219     env = cms_enveloped_data_init(cms);
220     if (env == NULL)
221         goto merr;
222
223     if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, 0,
224                                    cms_get0_cmsctx(cms)))
225         goto merr;
226     return cms;
227  merr:
228     CMS_ContentInfo_free(cms);
229     CMSerr(0, ERR_R_MALLOC_FAILURE);
230     return NULL;
231 }
232
233 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
234 {
235     return CMS_EnvelopedData_create_with_libctx(cipher, NULL, NULL);
236 }
237
238 CMS_ContentInfo *
239 CMS_AuthEnvelopedData_create_with_libctx(const EVP_CIPHER *cipher,
240                                          OPENSSL_CTX *libctx,
241                                          const char *propq)
242 {
243     CMS_ContentInfo *cms;
244     CMS_AuthEnvelopedData *aenv;
245
246     cms = CMS_ContentInfo_new_with_libctx(libctx, propq);
247     if (cms == NULL)
248         goto merr;
249     aenv = cms_auth_enveloped_data_init(cms);
250     if (aenv == NULL)
251         goto merr;
252     if (!cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
253                                    cipher, NULL, 0, cms_get0_cmsctx(cms)))
254         goto merr;
255     return cms;
256  merr:
257     CMS_ContentInfo_free(cms);
258     CMSerr(0, ERR_R_MALLOC_FAILURE);
259     return NULL;
260 }
261
262
263 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
264 {
265     return CMS_AuthEnvelopedData_create_with_libctx(cipher, NULL, NULL);
266 }
267
268 /* Key Transport Recipient Info (KTRI) routines */
269
270 /* Initialise a ktri based on passed certificate and key */
271
272 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
273                                        EVP_PKEY *pk, unsigned int flags,
274                                        const CMS_CTX *ctx)
275 {
276     CMS_KeyTransRecipientInfo *ktri;
277     int idtype;
278
279     ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
280     if (!ri->d.ktri)
281         return 0;
282     ri->type = CMS_RECIPINFO_TRANS;
283
284     ktri = ri->d.ktri;
285     ktri->cms_ctx = ctx;
286
287     if (flags & CMS_USE_KEYID) {
288         ktri->version = 2;
289         idtype = CMS_RECIPINFO_KEYIDENTIFIER;
290     } else {
291         ktri->version = 0;
292         idtype = CMS_RECIPINFO_ISSUER_SERIAL;
293     }
294
295     /*
296      * Not a typo: RecipientIdentifier and SignerIdentifier are the same
297      * structure.
298      */
299
300     if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
301         return 0;
302
303     X509_up_ref(recip);
304     EVP_PKEY_up_ref(pk);
305
306     ktri->pkey = pk;
307     ktri->recip = recip;
308
309     if (flags & CMS_KEY_PARAM) {
310         ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey,
311                                                 ctx->propq);
312         if (ktri->pctx == NULL)
313             return 0;
314         if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
315             return 0;
316     } else if (!cms_env_asn1_ctrl(ri, 0))
317         return 0;
318     return 1;
319 }
320
321 /*
322  * Add a recipient certificate using appropriate type of RecipientInfo
323  */
324
325 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
326                                       EVP_PKEY *originatorPrivKey,
327                                       X509 *originator, unsigned int flags)
328 {
329     CMS_RecipientInfo *ri = NULL;
330     STACK_OF(CMS_RecipientInfo) *ris;
331     EVP_PKEY *pk = NULL;
332     const CMS_CTX *ctx = cms_get0_cmsctx(cms);
333
334     ris = CMS_get0_RecipientInfos(cms);
335     if (ris == NULL)
336         goto err;
337
338     /* Initialize recipient info */
339     ri = M_ASN1_new_of(CMS_RecipientInfo);
340     if (ri == NULL)
341         goto merr;
342
343     pk = X509_get0_pubkey(recip);
344     if (pk == NULL) {
345         CMSerr(CMS_F_CMS_ADD1_RECIPIENT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
346         goto err;
347     }
348
349     switch (cms_pkey_get_ri_type(pk)) {
350
351     case CMS_RECIPINFO_TRANS:
352         if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
353             goto err;
354         break;
355
356     case CMS_RECIPINFO_AGREE:
357         if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator,
358                                          originatorPrivKey, flags, ctx))
359             goto err;
360         break;
361
362     default:
363         CMSerr(CMS_F_CMS_ADD1_RECIPIENT,
364                CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
365         goto err;
366
367     }
368
369     if (!sk_CMS_RecipientInfo_push(ris, ri))
370         goto merr;
371
372     return ri;
373
374  merr:
375     CMSerr(CMS_F_CMS_ADD1_RECIPIENT, ERR_R_MALLOC_FAILURE);
376  err:
377     M_ASN1_free_of(ri, CMS_RecipientInfo);
378     return NULL;
379
380 }
381
382 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
383                                            unsigned int flags)
384 {
385      return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
386 }
387
388 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
389                                      EVP_PKEY **pk, X509 **recip,
390                                      X509_ALGOR **palg)
391 {
392     CMS_KeyTransRecipientInfo *ktri;
393     if (ri->type != CMS_RECIPINFO_TRANS) {
394         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
395                CMS_R_NOT_KEY_TRANSPORT);
396         return 0;
397     }
398
399     ktri = ri->d.ktri;
400
401     if (pk)
402         *pk = ktri->pkey;
403     if (recip)
404         *recip = ktri->recip;
405     if (palg)
406         *palg = ktri->keyEncryptionAlgorithm;
407     return 1;
408 }
409
410 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
411                                           ASN1_OCTET_STRING **keyid,
412                                           X509_NAME **issuer,
413                                           ASN1_INTEGER **sno)
414 {
415     CMS_KeyTransRecipientInfo *ktri;
416     if (ri->type != CMS_RECIPINFO_TRANS) {
417         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
418                CMS_R_NOT_KEY_TRANSPORT);
419         return 0;
420     }
421     ktri = ri->d.ktri;
422
423     return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
424 }
425
426 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
427 {
428     if (ri->type != CMS_RECIPINFO_TRANS) {
429         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
430                CMS_R_NOT_KEY_TRANSPORT);
431         return -2;
432     }
433     return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
434 }
435
436 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
437 {
438     if (ri->type != CMS_RECIPINFO_TRANS) {
439         CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
440         return 0;
441     }
442     EVP_PKEY_free(ri->d.ktri->pkey);
443     ri->d.ktri->pkey = pkey;
444     return 1;
445 }
446
447 /* Encrypt content key in key transport recipient info */
448
449 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
450                                           CMS_RecipientInfo *ri)
451 {
452     CMS_KeyTransRecipientInfo *ktri;
453     CMS_EncryptedContentInfo *ec;
454     EVP_PKEY_CTX *pctx;
455     unsigned char *ek = NULL;
456     size_t eklen;
457     const CMS_CTX *ctx = cms_get0_cmsctx(cms);
458
459     int ret = 0;
460
461     if (ri->type != CMS_RECIPINFO_TRANS) {
462         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
463         return 0;
464     }
465     ktri = ri->d.ktri;
466     ec = cms_get0_env_enc_content(cms);
467
468     pctx = ktri->pctx;
469
470     if (pctx) {
471         if (!cms_env_asn1_ctrl(ri, 0))
472             goto err;
473     } else {
474         pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey, ctx->propq);
475         if (pctx == NULL)
476             return 0;
477
478         if (EVP_PKEY_encrypt_init(pctx) <= 0)
479             goto err;
480     }
481
482     if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
483                           EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
484         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
485         goto err;
486     }
487
488     if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
489         goto err;
490
491     ek = OPENSSL_malloc(eklen);
492
493     if (ek == NULL) {
494         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
495         goto err;
496     }
497
498     if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
499         goto err;
500
501     ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
502     ek = NULL;
503
504     ret = 1;
505
506  err:
507     EVP_PKEY_CTX_free(pctx);
508     ktri->pctx = NULL;
509     OPENSSL_free(ek);
510     return ret;
511 }
512
513 /* Decrypt content key from KTRI */
514
515 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
516                                           CMS_RecipientInfo *ri)
517 {
518     CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
519     EVP_PKEY *pkey = ktri->pkey;
520     unsigned char *ek = NULL;
521     size_t eklen;
522     int ret = 0;
523     size_t fixlen = 0;
524     const EVP_CIPHER *cipher = NULL;
525     EVP_CIPHER *fetched_cipher = NULL;
526     CMS_EncryptedContentInfo *ec;
527     const CMS_CTX *ctx = cms_get0_cmsctx(cms);
528
529     ec = cms_get0_env_enc_content(cms);
530
531     if (ktri->pkey == NULL) {
532         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
533         return 0;
534     }
535
536     if (cms->d.envelopedData->encryptedContentInfo->havenocert
537             && !cms->d.envelopedData->encryptedContentInfo->debug) {
538         X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
539         const char *name = OBJ_nid2sn(OBJ_obj2nid(calg->algorithm));
540
541         (void)ERR_set_mark();
542         fetched_cipher = EVP_CIPHER_fetch(ctx->libctx, name, ctx->propq);
543
544         if (fetched_cipher != NULL)
545             cipher = fetched_cipher;
546         else
547             cipher = EVP_get_cipherbyobj(calg->algorithm);
548         if (cipher == NULL) {
549             (void)ERR_clear_last_mark();
550             CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_UNKNOWN_CIPHER);
551             return 0;
552         }
553         (void)ERR_pop_to_mark();
554
555         fixlen = EVP_CIPHER_key_length(cipher);
556         EVP_CIPHER_free(fetched_cipher);
557     }
558
559     ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
560     if (ktri->pctx == NULL)
561         goto err;
562
563     if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
564         goto err;
565
566     if (!cms_env_asn1_ctrl(ri, 1))
567         goto err;
568
569     if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
570                           EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
571         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
572         goto err;
573     }
574
575     if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
576                          ktri->encryptedKey->data,
577                          ktri->encryptedKey->length) <= 0)
578         goto err;
579
580     ek = OPENSSL_malloc(eklen);
581     if (ek == NULL) {
582         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
583         goto err;
584     }
585
586     if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
587                          ktri->encryptedKey->data,
588                          ktri->encryptedKey->length) <= 0
589             || eklen == 0
590             || (fixlen != 0 && eklen != fixlen)) {
591         CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
592         goto err;
593     }
594
595     ret = 1;
596
597     OPENSSL_clear_free(ec->key, ec->keylen);
598     ec->key = ek;
599     ec->keylen = eklen;
600
601  err:
602     EVP_PKEY_CTX_free(ktri->pctx);
603     ktri->pctx = NULL;
604     if (!ret)
605         OPENSSL_free(ek);
606
607     return ret;
608 }
609
610 /* Key Encrypted Key (KEK) RecipientInfo routines */
611
612 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
613                                    const unsigned char *id, size_t idlen)
614 {
615     ASN1_OCTET_STRING tmp_os;
616     CMS_KEKRecipientInfo *kekri;
617     if (ri->type != CMS_RECIPINFO_KEK) {
618         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
619         return -2;
620     }
621     kekri = ri->d.kekri;
622     tmp_os.type = V_ASN1_OCTET_STRING;
623     tmp_os.flags = 0;
624     tmp_os.data = (unsigned char *)id;
625     tmp_os.length = (int)idlen;
626     return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
627 }
628
629 /* For now hard code AES key wrap info */
630
631 static size_t aes_wrap_keylen(int nid)
632 {
633     switch (nid) {
634     case NID_id_aes128_wrap:
635         return 16;
636
637     case NID_id_aes192_wrap:
638         return 24;
639
640     case NID_id_aes256_wrap:
641         return 32;
642
643     default:
644         return 0;
645     }
646 }
647
648 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
649                                           unsigned char *key, size_t keylen,
650                                           unsigned char *id, size_t idlen,
651                                           ASN1_GENERALIZEDTIME *date,
652                                           ASN1_OBJECT *otherTypeId,
653                                           ASN1_TYPE *otherType)
654 {
655     CMS_RecipientInfo *ri = NULL;
656     CMS_KEKRecipientInfo *kekri;
657     STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
658
659     if (ris == NULL)
660         goto err;
661
662     if (nid == NID_undef) {
663         switch (keylen) {
664         case 16:
665             nid = NID_id_aes128_wrap;
666             break;
667
668         case 24:
669             nid = NID_id_aes192_wrap;
670             break;
671
672         case 32:
673             nid = NID_id_aes256_wrap;
674             break;
675
676         default:
677             CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
678             goto err;
679         }
680
681     } else {
682
683         size_t exp_keylen = aes_wrap_keylen(nid);
684
685         if (!exp_keylen) {
686             CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
687                    CMS_R_UNSUPPORTED_KEK_ALGORITHM);
688             goto err;
689         }
690
691         if (keylen != exp_keylen) {
692             CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
693             goto err;
694         }
695
696     }
697
698     /* Initialize recipient info */
699     ri = M_ASN1_new_of(CMS_RecipientInfo);
700     if (!ri)
701         goto merr;
702
703     ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
704     if (!ri->d.kekri)
705         goto merr;
706     ri->type = CMS_RECIPINFO_KEK;
707
708     kekri = ri->d.kekri;
709
710     if (otherTypeId) {
711         kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
712         if (kekri->kekid->other == NULL)
713             goto merr;
714     }
715
716     if (!sk_CMS_RecipientInfo_push(ris, ri))
717         goto merr;
718
719     /* After this point no calls can fail */
720
721     kekri->version = 4;
722
723     kekri->key = key;
724     kekri->keylen = keylen;
725
726     ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
727
728     kekri->kekid->date = date;
729
730     if (kekri->kekid->other) {
731         kekri->kekid->other->keyAttrId = otherTypeId;
732         kekri->kekid->other->keyAttr = otherType;
733     }
734
735     X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
736                     OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
737
738     return ri;
739
740  merr:
741     CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
742  err:
743     M_ASN1_free_of(ri, CMS_RecipientInfo);
744     return NULL;
745 }
746
747 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
748                                     X509_ALGOR **palg,
749                                     ASN1_OCTET_STRING **pid,
750                                     ASN1_GENERALIZEDTIME **pdate,
751                                     ASN1_OBJECT **potherid,
752                                     ASN1_TYPE **pothertype)
753 {
754     CMS_KEKIdentifier *rkid;
755     if (ri->type != CMS_RECIPINFO_KEK) {
756         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
757         return 0;
758     }
759     rkid = ri->d.kekri->kekid;
760     if (palg)
761         *palg = ri->d.kekri->keyEncryptionAlgorithm;
762     if (pid)
763         *pid = rkid->keyIdentifier;
764     if (pdate)
765         *pdate = rkid->date;
766     if (potherid) {
767         if (rkid->other)
768             *potherid = rkid->other->keyAttrId;
769         else
770             *potherid = NULL;
771     }
772     if (pothertype) {
773         if (rkid->other)
774             *pothertype = rkid->other->keyAttr;
775         else
776             *pothertype = NULL;
777     }
778     return 1;
779 }
780
781 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
782                                unsigned char *key, size_t keylen)
783 {
784     CMS_KEKRecipientInfo *kekri;
785     if (ri->type != CMS_RECIPINFO_KEK) {
786         CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
787         return 0;
788     }
789
790     kekri = ri->d.kekri;
791     kekri->key = key;
792     kekri->keylen = keylen;
793     return 1;
794 }
795
796 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
797 {
798     const char *alg = NULL;
799
800     switch(keylen) {
801     case 16:
802         alg = "AES-128-WRAP";
803         break;
804     case 24:
805         alg = "AES-192-WRAP";
806         break;
807     case 32:
808         alg = "AES-256-WRAP";
809         break;
810     default:
811         return NULL;
812     }
813     return EVP_CIPHER_fetch(ctx->libctx, alg, ctx->propq);
814 }
815
816
817 /* Encrypt content key in KEK recipient info */
818
819 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
820                                            CMS_RecipientInfo *ri)
821 {
822     CMS_EncryptedContentInfo *ec;
823     CMS_KEKRecipientInfo *kekri;
824     unsigned char *wkey = NULL;
825     int wkeylen;
826     int r = 0;
827     EVP_CIPHER *cipher = NULL;
828     int outlen = 0;
829     EVP_CIPHER_CTX *ctx = NULL;
830     const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
831
832     ec = cms_get0_env_enc_content(cms);
833     if (ec == NULL)
834         return 0;
835
836     kekri = ri->d.kekri;
837
838     if (kekri->key == NULL) {
839         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
840         return 0;
841     }
842
843     cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
844     if (cipher == NULL) {
845         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_INVALID_KEY_LENGTH);
846         goto err;
847     }
848
849     /* 8 byte prefix for AES wrap ciphers */
850     wkey = OPENSSL_malloc(ec->keylen + 8);
851     if (wkey == NULL) {
852         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
853         goto err;
854     }
855
856     ctx = EVP_CIPHER_CTX_new();
857     if (ctx == NULL) {
858         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
859         goto err;
860     }
861
862     EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
863     if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
864             || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
865             || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
866         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
867         goto err;
868     }
869     wkeylen += outlen;
870     if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
871         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
872         goto err;
873     }
874
875     ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
876
877     r = 1;
878
879  err:
880     EVP_CIPHER_free(cipher);
881     if (!r)
882         OPENSSL_free(wkey);
883     EVP_CIPHER_CTX_free(ctx);
884
885     return r;
886 }
887
888 /* Decrypt content key in KEK recipient info */
889
890 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
891                                            CMS_RecipientInfo *ri)
892 {
893     CMS_EncryptedContentInfo *ec;
894     CMS_KEKRecipientInfo *kekri;
895     unsigned char *ukey = NULL;
896     int ukeylen;
897     int r = 0, wrap_nid;
898     EVP_CIPHER *cipher = NULL;
899     int outlen = 0;
900     EVP_CIPHER_CTX *ctx = NULL;
901     const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
902
903     ec = cms_get0_env_enc_content(cms);
904     if (ec == NULL)
905         return 0;
906
907     kekri = ri->d.kekri;
908
909     if (!kekri->key) {
910         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
911         return 0;
912     }
913
914     wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
915     if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
916         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
917                CMS_R_INVALID_KEY_LENGTH);
918         return 0;
919     }
920
921     /* If encrypted key length is invalid don't bother */
922
923     if (kekri->encryptedKey->length < 16) {
924         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
925                CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
926         goto err;
927     }
928
929     cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
930     if (cipher == NULL) {
931         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_INVALID_KEY_LENGTH);
932         goto err;
933     }
934
935     ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
936     if (ukey == NULL) {
937         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
938         goto err;
939     }
940
941     ctx = EVP_CIPHER_CTX_new();
942     if (ctx == NULL) {
943         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
944         goto err;
945     }
946
947     if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
948             || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
949                                   kekri->encryptedKey->data,
950                                   kekri->encryptedKey->length)
951             || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
952         CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
953         goto err;
954     }
955     ukeylen += outlen;
956
957     ec->key = ukey;
958     ec->keylen = ukeylen;
959
960     r = 1;
961
962  err:
963     EVP_CIPHER_free(cipher);
964     if (!r)
965         OPENSSL_free(ukey);
966     EVP_CIPHER_CTX_free(ctx);
967
968     return r;
969 }
970
971 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
972 {
973     switch (ri->type) {
974     case CMS_RECIPINFO_TRANS:
975         return cms_RecipientInfo_ktri_decrypt(cms, ri);
976
977     case CMS_RECIPINFO_KEK:
978         return cms_RecipientInfo_kekri_decrypt(cms, ri);
979
980     case CMS_RECIPINFO_PASS:
981         return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
982
983     default:
984         CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
985                CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
986         return 0;
987     }
988 }
989
990 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
991 {
992     switch (ri->type) {
993     case CMS_RECIPINFO_TRANS:
994         return cms_RecipientInfo_ktri_encrypt(cms, ri);
995
996     case CMS_RECIPINFO_AGREE:
997         return cms_RecipientInfo_kari_encrypt(cms, ri);
998
999     case CMS_RECIPINFO_KEK:
1000         return cms_RecipientInfo_kekri_encrypt(cms, ri);
1001
1002     case CMS_RECIPINFO_PASS:
1003         return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1004
1005     default:
1006         CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
1007                CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1008         return 0;
1009     }
1010 }
1011
1012 /* Check structures and fixup version numbers (if necessary) */
1013
1014 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1015 {
1016     CMS_OriginatorInfo *org = env->originatorInfo;
1017     int i;
1018     if (org == NULL)
1019         return;
1020     for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1021         CMS_CertificateChoices *cch;
1022         cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1023         if (cch->type == CMS_CERTCHOICE_OTHER) {
1024             env->version = 4;
1025             return;
1026         } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1027             if (env->version < 3)
1028                 env->version = 3;
1029         }
1030     }
1031
1032     for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1033         CMS_RevocationInfoChoice *rch;
1034         rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1035         if (rch->type == CMS_REVCHOICE_OTHER) {
1036             env->version = 4;
1037             return;
1038         }
1039     }
1040 }
1041
1042 static void cms_env_set_version(CMS_EnvelopedData *env)
1043 {
1044     int i;
1045     CMS_RecipientInfo *ri;
1046
1047     /*
1048      * Can't set version higher than 4 so if 4 or more already nothing to do.
1049      */
1050     if (env->version >= 4)
1051         return;
1052
1053     cms_env_set_originfo_version(env);
1054
1055     if (env->version >= 3)
1056         return;
1057
1058     for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1059         ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1060         if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1061             env->version = 3;
1062             return;
1063         } else if (ri->type != CMS_RECIPINFO_TRANS
1064                    || ri->d.ktri->version != 0) {
1065             env->version = 2;
1066         }
1067     }
1068     if (env->originatorInfo || env->unprotectedAttrs)
1069         env->version = 2;
1070     if (env->version == 2)
1071         return;
1072     env->version = 0;
1073 }
1074
1075 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1076                                        STACK_OF(CMS_RecipientInfo) *ris)
1077 {
1078     int i;
1079     CMS_RecipientInfo *ri;
1080
1081     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1082         ri = sk_CMS_RecipientInfo_value(ris, i);
1083         if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1084             return -1;
1085     }
1086     return 1;
1087 }
1088
1089 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1090 {
1091     ec->cipher = NULL;
1092     OPENSSL_clear_free(ec->key, ec->keylen);
1093     ec->key = NULL;
1094     ec->keylen = 0;
1095 }
1096
1097 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1098 {
1099     CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1100     BIO *contentBio = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1101     EVP_CIPHER_CTX *ctx = NULL;
1102
1103     if (contentBio == NULL)
1104         return NULL;
1105
1106     BIO_get_cipher_ctx(contentBio, &ctx);
1107     if (ctx == NULL) {
1108         BIO_free(contentBio);
1109         return NULL;
1110     }
1111     /*
1112      * If the selected cipher supports unprotected attributes,
1113      * deal with it using special ctrl function
1114      */
1115     if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1116          && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1117                                 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1118         BIO_free(contentBio);
1119         return NULL;
1120     }
1121     return contentBio;
1122 }
1123
1124 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1125 {
1126     CMS_EncryptedContentInfo *ec;
1127     STACK_OF(CMS_RecipientInfo) *rinfos;
1128     int ok = 0;
1129     BIO *ret;
1130     CMS_EnvelopedData *env = cms->d.envelopedData;
1131
1132     /* Get BIO first to set up key */
1133
1134     ec = env->encryptedContentInfo;
1135     ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1136
1137     /* If error end of processing */
1138     if (!ret)
1139         return ret;
1140
1141     /* Now encrypt content key according to each RecipientInfo type */
1142     rinfos = env->recipientInfos;
1143     if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1144         CMSerr(CMS_F_CMS_ENVELOPEDDATA_ENCRYPTION_INIT_BIO,
1145                CMS_R_ERROR_SETTING_RECIPIENTINFO);
1146         goto err;
1147     }
1148
1149     /* And finally set the version */
1150     cms_env_set_version(env);
1151
1152     ok = 1;
1153
1154  err:
1155     cms_env_clear_ec(ec);
1156     if (ok)
1157         return ret;
1158     BIO_free(ret);
1159     return NULL;
1160 }
1161
1162 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1163 {
1164     if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1165          /* If cipher is set it's encryption */
1166          return cms_EnvelopedData_Encryption_init_bio(cms);
1167     }
1168
1169     /* If cipher is not set it's decryption */
1170     return cms_EnvelopedData_Decryption_init_bio(cms);
1171 }
1172
1173 BIO *cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1174 {
1175     CMS_EncryptedContentInfo *ec;
1176     STACK_OF(CMS_RecipientInfo) *rinfos;
1177     int ok = 0;
1178     BIO *ret;
1179     CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1180
1181     /* Get BIO first to set up key */
1182     ec = aenv->authEncryptedContentInfo;
1183     /* Set tag for decryption */
1184     if (ec->cipher == NULL) {
1185         ec->tag = aenv->mac->data;
1186         ec->taglen = aenv->mac->length;
1187     }
1188     ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1189
1190     /* If error or no cipher end of processing */
1191     if (ret == NULL || ec->cipher == NULL)
1192         return ret;
1193
1194     /* Now encrypt content key according to each RecipientInfo type */
1195     rinfos = aenv->recipientInfos;
1196     if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1197         CMSerr(0, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1198         goto err;
1199     }
1200
1201     /* And finally set the version */
1202     aenv->version = 0;
1203
1204     ok = 1;
1205
1206  err:
1207     cms_env_clear_ec(ec);
1208     if (ok)
1209         return ret;
1210     BIO_free(ret);
1211     return NULL;
1212 }
1213
1214 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1215 {
1216     CMS_EnvelopedData *env = NULL;
1217     EVP_CIPHER_CTX *ctx = NULL;
1218     BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1219
1220     env = cms_get0_enveloped(cms);
1221     if (env == NULL)
1222         return 0;
1223
1224     if (mbio == NULL) {
1225         CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CONTENT_NOT_FOUND);
1226         return 0;
1227     }
1228
1229     BIO_get_cipher_ctx(mbio, &ctx);
1230
1231     /*
1232      * If the selected cipher supports unprotected attributes,
1233      * deal with it using special ctrl function
1234      */
1235     if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
1236         if (env->unprotectedAttrs == NULL)
1237             env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1238
1239         if (env->unprotectedAttrs == NULL) {
1240             CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, ERR_R_MALLOC_FAILURE);
1241             return 0;
1242         }
1243
1244         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1245                                 1, env->unprotectedAttrs) <= 0) {
1246             CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CTRL_FAILURE);
1247             return 0;
1248         }
1249     }
1250
1251     cms_env_set_version(cms->d.envelopedData);
1252     return 1;
1253 }
1254
1255 int cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1256 {
1257     EVP_CIPHER_CTX *ctx;
1258     unsigned char *tag = NULL;
1259     int taglen, ok = 0;
1260
1261     BIO_get_cipher_ctx(cmsbio, &ctx);
1262
1263     /* 
1264      * The tag is set only for encryption. There is nothing to do for
1265      * decryption.
1266      */
1267     if (!EVP_CIPHER_CTX_encrypting(ctx))
1268         return 1;
1269
1270     taglen = EVP_CIPHER_CTX_tag_length(ctx);
1271     if (taglen <= 0
1272             || (tag = OPENSSL_malloc(taglen)) == NULL
1273             || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1274                                    tag) <= 0) {
1275         CMSerr(0, CMS_R_CIPHER_GET_TAG);
1276         goto err;
1277     }
1278
1279     if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1280         goto err;
1281
1282     ok = 1;
1283 err:
1284     OPENSSL_free(tag);
1285     return ok;
1286 }
1287
1288 /*
1289  * Get RecipientInfo type (if any) supported by a key (public or private). To
1290  * retain compatibility with previous behaviour if the ctrl value isn't
1291  * supported we assume key transport.
1292  */
1293 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1294 {
1295     if (pk->ameth && pk->ameth->pkey_ctrl) {
1296         int i, r;
1297         i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1298         if (i > 0)
1299             return r;
1300     }
1301     return CMS_RECIPINFO_TRANS;
1302 }
1303
1304 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1305 {
1306     int supportedRiType;
1307
1308     if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1309         int i, r;
1310
1311         i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1312                                  ri_type, &r);
1313         if (i > 0)
1314             return r;
1315     }
1316
1317     supportedRiType = cms_pkey_get_ri_type(pk);
1318     if (supportedRiType < 0)
1319         return 0;
1320
1321     return (supportedRiType == ri_type);
1322 }