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