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