]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cms/cms_env.c
6f3ca020d8c195ca1b89d20e48d433458e317179
[thirdparty/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") || EVP_PKEY_is_a(pkey, "DH"))
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, cms_ctx_get0_libctx(ctx),
185 cms_ctx_get0_propq(ctx));
186 break;
187 case CMS_RECIPINFO_KEK:
188 ri->d.kekri->cms_ctx = ctx;
189 break;
190 case CMS_RECIPINFO_PASS:
191 ri->d.pwri->cms_ctx = ctx;
192 break;
193 default:
194 break;
195 }
196 }
197 }
198 }
199
200 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
201 {
202 return ri->type;
203 }
204
205 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
206 {
207 if (ri->type == CMS_RECIPINFO_TRANS)
208 return ri->d.ktri->pctx;
209 else if (ri->type == CMS_RECIPINFO_AGREE)
210 return ri->d.kari->pctx;
211 return NULL;
212 }
213
214 CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
215 OSSL_LIB_CTX *libctx,
216 const char *propq)
217 {
218 CMS_ContentInfo *cms;
219 CMS_EnvelopedData *env;
220
221 cms = CMS_ContentInfo_new_ex(libctx, propq);
222 if (cms == NULL)
223 goto merr;
224 env = cms_enveloped_data_init(cms);
225 if (env == NULL)
226 goto merr;
227
228 if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, 0,
229 cms_get0_cmsctx(cms)))
230 goto merr;
231 return cms;
232 merr:
233 CMS_ContentInfo_free(cms);
234 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
235 return NULL;
236 }
237
238 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
239 {
240 return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
241 }
242
243 CMS_ContentInfo *
244 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx,
245 const char *propq)
246 {
247 CMS_ContentInfo *cms;
248 CMS_AuthEnvelopedData *aenv;
249
250 cms = CMS_ContentInfo_new_ex(libctx, propq);
251 if (cms == NULL)
252 goto merr;
253 aenv = cms_auth_enveloped_data_init(cms);
254 if (aenv == NULL)
255 goto merr;
256 if (!cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
257 cipher, NULL, 0, cms_get0_cmsctx(cms)))
258 goto merr;
259 return cms;
260 merr:
261 CMS_ContentInfo_free(cms);
262 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
263 return NULL;
264 }
265
266
267 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
268 {
269 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
270 }
271
272 /* Key Transport Recipient Info (KTRI) routines */
273
274 /* Initialise a ktri based on passed certificate and key */
275
276 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
277 EVP_PKEY *pk, unsigned int flags,
278 const CMS_CTX *ctx)
279 {
280 CMS_KeyTransRecipientInfo *ktri;
281 int idtype;
282
283 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
284 if (!ri->d.ktri)
285 return 0;
286 ri->type = CMS_RECIPINFO_TRANS;
287
288 ktri = ri->d.ktri;
289 ktri->cms_ctx = ctx;
290
291 if (flags & CMS_USE_KEYID) {
292 ktri->version = 2;
293 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
294 } else {
295 ktri->version = 0;
296 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
297 }
298
299 /*
300 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
301 * structure.
302 */
303
304 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
305 return 0;
306
307 X509_up_ref(recip);
308 EVP_PKEY_up_ref(pk);
309
310 ktri->pkey = pk;
311 ktri->recip = recip;
312
313 if (flags & CMS_KEY_PARAM) {
314 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx),
315 ktri->pkey,
316 cms_ctx_get0_propq(ctx));
317 if (ktri->pctx == NULL)
318 return 0;
319 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
320 return 0;
321 } else if (!cms_env_asn1_ctrl(ri, 0))
322 return 0;
323 return 1;
324 }
325
326 /*
327 * Add a recipient certificate using appropriate type of RecipientInfo
328 */
329
330 CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip,
331 EVP_PKEY *originatorPrivKey,
332 X509 *originator, unsigned int flags)
333 {
334 CMS_RecipientInfo *ri = NULL;
335 STACK_OF(CMS_RecipientInfo) *ris;
336 EVP_PKEY *pk = NULL;
337 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
338
339 ris = CMS_get0_RecipientInfos(cms);
340 if (ris == NULL)
341 goto err;
342
343 /* Initialize recipient info */
344 ri = M_ASN1_new_of(CMS_RecipientInfo);
345 if (ri == NULL)
346 goto merr;
347
348 pk = X509_get0_pubkey(recip);
349 if (pk == NULL) {
350 ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY);
351 goto err;
352 }
353
354 switch (cms_pkey_get_ri_type(pk)) {
355
356 case CMS_RECIPINFO_TRANS:
357 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx))
358 goto err;
359 break;
360
361 case CMS_RECIPINFO_AGREE:
362 if (!cms_RecipientInfo_kari_init(ri, recip, pk, originator,
363 originatorPrivKey, flags, ctx))
364 goto err;
365 break;
366
367 default:
368 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
369 goto err;
370
371 }
372
373 if (!sk_CMS_RecipientInfo_push(ris, ri))
374 goto merr;
375
376 return ri;
377
378 merr:
379 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
380 err:
381 M_ASN1_free_of(ri, CMS_RecipientInfo);
382 return NULL;
383
384 }
385
386 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
387 unsigned int flags)
388 {
389 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
390 }
391
392 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
393 EVP_PKEY **pk, X509 **recip,
394 X509_ALGOR **palg)
395 {
396 CMS_KeyTransRecipientInfo *ktri;
397 if (ri->type != CMS_RECIPINFO_TRANS) {
398 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
399 return 0;
400 }
401
402 ktri = ri->d.ktri;
403
404 if (pk)
405 *pk = ktri->pkey;
406 if (recip)
407 *recip = ktri->recip;
408 if (palg)
409 *palg = ktri->keyEncryptionAlgorithm;
410 return 1;
411 }
412
413 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
414 ASN1_OCTET_STRING **keyid,
415 X509_NAME **issuer,
416 ASN1_INTEGER **sno)
417 {
418 CMS_KeyTransRecipientInfo *ktri;
419 if (ri->type != CMS_RECIPINFO_TRANS) {
420 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
421 return 0;
422 }
423 ktri = ri->d.ktri;
424
425 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
426 }
427
428 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
429 {
430 if (ri->type != CMS_RECIPINFO_TRANS) {
431 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
432 return -2;
433 }
434 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
435 }
436
437 int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
438 {
439 if (ri->type != CMS_RECIPINFO_TRANS) {
440 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
441 return 0;
442 }
443 EVP_PKEY_free(ri->d.ktri->pkey);
444 ri->d.ktri->pkey = pkey;
445 return 1;
446 }
447
448 /* Encrypt content key in key transport recipient info */
449
450 static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms,
451 CMS_RecipientInfo *ri)
452 {
453 CMS_KeyTransRecipientInfo *ktri;
454 CMS_EncryptedContentInfo *ec;
455 EVP_PKEY_CTX *pctx;
456 unsigned char *ek = NULL;
457 size_t eklen;
458 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
459
460 int ret = 0;
461
462 if (ri->type != CMS_RECIPINFO_TRANS) {
463 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT);
464 return 0;
465 }
466 ktri = ri->d.ktri;
467 ec = cms_get0_env_enc_content(cms);
468
469 pctx = ktri->pctx;
470
471 if (pctx) {
472 if (!cms_env_asn1_ctrl(ri, 0))
473 goto err;
474 } else {
475 pctx = EVP_PKEY_CTX_new_from_pkey(cms_ctx_get0_libctx(ctx), ktri->pkey,
476 cms_ctx_get0_propq(ctx));
477 if (pctx == NULL)
478 return 0;
479
480 if (EVP_PKEY_encrypt_init(pctx) <= 0)
481 goto err;
482 }
483
484 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
485 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
486 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
487 goto err;
488 }
489
490 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
491 goto err;
492
493 ek = OPENSSL_malloc(eklen);
494
495 if (ek == NULL) {
496 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
497 goto err;
498 }
499
500 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
501 goto err;
502
503 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
504 ek = NULL;
505
506 ret = 1;
507
508 err:
509 EVP_PKEY_CTX_free(pctx);
510 ktri->pctx = NULL;
511 OPENSSL_free(ek);
512 return ret;
513 }
514
515 /* Decrypt content key from KTRI */
516
517 static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
518 CMS_RecipientInfo *ri)
519 {
520 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
521 EVP_PKEY *pkey = ktri->pkey;
522 unsigned char *ek = NULL;
523 size_t eklen;
524 int ret = 0;
525 size_t fixlen = 0;
526 const EVP_CIPHER *cipher = NULL;
527 EVP_CIPHER *fetched_cipher = NULL;
528 CMS_EncryptedContentInfo *ec;
529 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
530 OSSL_LIB_CTX *libctx = cms_ctx_get0_libctx(ctx);
531 const char *propq = cms_ctx_get0_propq(ctx);
532
533 ec = cms_get0_env_enc_content(cms);
534
535 if (ktri->pkey == NULL) {
536 ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY);
537 return 0;
538 }
539
540 if (cms->d.envelopedData->encryptedContentInfo->havenocert
541 && !cms->d.envelopedData->encryptedContentInfo->debug) {
542 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
543 const char *name = OBJ_nid2sn(OBJ_obj2nid(calg->algorithm));
544
545 (void)ERR_set_mark();
546 fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq);
547
548 if (fetched_cipher != NULL)
549 cipher = fetched_cipher;
550 else
551 cipher = EVP_get_cipherbyobj(calg->algorithm);
552 if (cipher == NULL) {
553 (void)ERR_clear_last_mark();
554 ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER);
555 return 0;
556 }
557 (void)ERR_pop_to_mark();
558
559 fixlen = EVP_CIPHER_key_length(cipher);
560 EVP_CIPHER_free(fetched_cipher);
561 }
562
563 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
564 if (ktri->pctx == NULL)
565 goto err;
566
567 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
568 goto err;
569
570 if (!cms_env_asn1_ctrl(ri, 1))
571 goto err;
572
573 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
574 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
575 ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_ERROR);
576 goto err;
577 }
578
579 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
580 ktri->encryptedKey->data,
581 ktri->encryptedKey->length) <= 0)
582 goto err;
583
584 ek = OPENSSL_malloc(eklen);
585 if (ek == NULL) {
586 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
587 goto err;
588 }
589
590 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
591 ktri->encryptedKey->data,
592 ktri->encryptedKey->length) <= 0
593 || eklen == 0
594 || (fixlen != 0 && eklen != fixlen)) {
595 ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
596 goto err;
597 }
598
599 ret = 1;
600
601 OPENSSL_clear_free(ec->key, ec->keylen);
602 ec->key = ek;
603 ec->keylen = eklen;
604
605 err:
606 EVP_PKEY_CTX_free(ktri->pctx);
607 ktri->pctx = NULL;
608 if (!ret)
609 OPENSSL_free(ek);
610
611 return ret;
612 }
613
614 /* Key Encrypted Key (KEK) RecipientInfo routines */
615
616 int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
617 const unsigned char *id, size_t idlen)
618 {
619 ASN1_OCTET_STRING tmp_os;
620 CMS_KEKRecipientInfo *kekri;
621 if (ri->type != CMS_RECIPINFO_KEK) {
622 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
623 return -2;
624 }
625 kekri = ri->d.kekri;
626 tmp_os.type = V_ASN1_OCTET_STRING;
627 tmp_os.flags = 0;
628 tmp_os.data = (unsigned char *)id;
629 tmp_os.length = (int)idlen;
630 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
631 }
632
633 /* For now hard code AES key wrap info */
634
635 static size_t aes_wrap_keylen(int nid)
636 {
637 switch (nid) {
638 case NID_id_aes128_wrap:
639 return 16;
640
641 case NID_id_aes192_wrap:
642 return 24;
643
644 case NID_id_aes256_wrap:
645 return 32;
646
647 default:
648 return 0;
649 }
650 }
651
652 CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
653 unsigned char *key, size_t keylen,
654 unsigned char *id, size_t idlen,
655 ASN1_GENERALIZEDTIME *date,
656 ASN1_OBJECT *otherTypeId,
657 ASN1_TYPE *otherType)
658 {
659 CMS_RecipientInfo *ri = NULL;
660 CMS_KEKRecipientInfo *kekri;
661 STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
662
663 if (ris == NULL)
664 goto err;
665
666 if (nid == NID_undef) {
667 switch (keylen) {
668 case 16:
669 nid = NID_id_aes128_wrap;
670 break;
671
672 case 24:
673 nid = NID_id_aes192_wrap;
674 break;
675
676 case 32:
677 nid = NID_id_aes256_wrap;
678 break;
679
680 default:
681 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
682 goto err;
683 }
684
685 } else {
686
687 size_t exp_keylen = aes_wrap_keylen(nid);
688
689 if (!exp_keylen) {
690 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM);
691 goto err;
692 }
693
694 if (keylen != exp_keylen) {
695 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH);
696 goto err;
697 }
698
699 }
700
701 /* Initialize recipient info */
702 ri = M_ASN1_new_of(CMS_RecipientInfo);
703 if (!ri)
704 goto merr;
705
706 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
707 if (!ri->d.kekri)
708 goto merr;
709 ri->type = CMS_RECIPINFO_KEK;
710
711 kekri = ri->d.kekri;
712
713 if (otherTypeId) {
714 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
715 if (kekri->kekid->other == NULL)
716 goto merr;
717 }
718
719 if (!sk_CMS_RecipientInfo_push(ris, ri))
720 goto merr;
721
722 /* After this point no calls can fail */
723
724 kekri->version = 4;
725
726 kekri->key = key;
727 kekri->keylen = keylen;
728
729 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
730
731 kekri->kekid->date = date;
732
733 if (kekri->kekid->other) {
734 kekri->kekid->other->keyAttrId = otherTypeId;
735 kekri->kekid->other->keyAttr = otherType;
736 }
737
738 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
739 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
740
741 return ri;
742
743 merr:
744 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
745 err:
746 M_ASN1_free_of(ri, CMS_RecipientInfo);
747 return NULL;
748 }
749
750 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
751 X509_ALGOR **palg,
752 ASN1_OCTET_STRING **pid,
753 ASN1_GENERALIZEDTIME **pdate,
754 ASN1_OBJECT **potherid,
755 ASN1_TYPE **pothertype)
756 {
757 CMS_KEKIdentifier *rkid;
758 if (ri->type != CMS_RECIPINFO_KEK) {
759 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
760 return 0;
761 }
762 rkid = ri->d.kekri->kekid;
763 if (palg)
764 *palg = ri->d.kekri->keyEncryptionAlgorithm;
765 if (pid)
766 *pid = rkid->keyIdentifier;
767 if (pdate)
768 *pdate = rkid->date;
769 if (potherid) {
770 if (rkid->other)
771 *potherid = rkid->other->keyAttrId;
772 else
773 *potherid = NULL;
774 }
775 if (pothertype) {
776 if (rkid->other)
777 *pothertype = rkid->other->keyAttr;
778 else
779 *pothertype = NULL;
780 }
781 return 1;
782 }
783
784 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
785 unsigned char *key, size_t keylen)
786 {
787 CMS_KEKRecipientInfo *kekri;
788 if (ri->type != CMS_RECIPINFO_KEK) {
789 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK);
790 return 0;
791 }
792
793 kekri = ri->d.kekri;
794 kekri->key = key;
795 kekri->keylen = keylen;
796 return 1;
797 }
798
799 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
800 {
801 const char *alg = NULL;
802
803 switch(keylen) {
804 case 16:
805 alg = "AES-128-WRAP";
806 break;
807 case 24:
808 alg = "AES-192-WRAP";
809 break;
810 case 32:
811 alg = "AES-256-WRAP";
812 break;
813 default:
814 return NULL;
815 }
816 return EVP_CIPHER_fetch(cms_ctx_get0_libctx(ctx), alg,
817 cms_ctx_get0_propq(ctx));
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, "DHX"))
1298 return CMS_RECIPINFO_AGREE;
1299 else if (EVP_PKEY_is_a(pk, "DSA"))
1300 return CMS_RECIPINFO_NONE;
1301 else if (EVP_PKEY_is_a(pk, "EC"))
1302 return CMS_RECIPINFO_AGREE;
1303 else if (EVP_PKEY_is_a(pk, "RSA"))
1304 return CMS_RECIPINFO_TRANS;
1305
1306 /*
1307 * Otherwise this might ben an engine implementation, so see if we can get
1308 * the type from the ameth.
1309 */
1310 if (pk->ameth && pk->ameth->pkey_ctrl) {
1311 int i, r;
1312 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1313 if (i > 0)
1314 return r;
1315 }
1316 return CMS_RECIPINFO_TRANS;
1317 }
1318
1319 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1320 {
1321 int supportedRiType;
1322
1323 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1324 int i, r;
1325
1326 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1327 ri_type, &r);
1328 if (i > 0)
1329 return r;
1330 }
1331
1332 supportedRiType = cms_pkey_get_ri_type(pk);
1333 if (supportedRiType < 0)
1334 return 0;
1335
1336 return (supportedRiType == ri_type);
1337 }