]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cms/cms_env.c
Move CMS enveloping code out of the algorithms and into CMS
[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 CMSerr(0, 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 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
49 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
50 return NULL;
51 }
52 return cms->d.envelopedData;
53 }
54
55 CMS_AuthEnvelopedData *cms_get0_auth_enveloped(CMS_ContentInfo *cms)
56 {
57 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) {
58 CMSerr(0, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
59 return NULL;
60 }
61 return cms->d.authEnvelopedData;
62 }
63
64 static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
65 {
66 if (cms->d.other == NULL) {
67 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
68 if (cms->d.envelopedData == NULL) {
69 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
70 return NULL;
71 }
72 cms->d.envelopedData->version = 0;
73 cms->d.envelopedData->encryptedContentInfo->contentType =
74 OBJ_nid2obj(NID_pkcs7_data);
75 ASN1_OBJECT_free(cms->contentType);
76 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
77 return cms->d.envelopedData;
78 }
79 return cms_get0_enveloped(cms);
80 }
81
82 static CMS_AuthEnvelopedData *
83 cms_auth_enveloped_data_init(CMS_ContentInfo *cms)
84 {
85 if (cms->d.other == NULL) {
86 cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData);
87 if (cms->d.authEnvelopedData == NULL) {
88 CMSerr(0, ERR_R_MALLOC_FAILURE);
89 return NULL;
90 }
91 /* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */
92 cms->d.authEnvelopedData->version = 0;
93 cms->d.authEnvelopedData->authEncryptedContentInfo->contentType =
94 OBJ_nid2obj(NID_pkcs7_data);
95 ASN1_OBJECT_free(cms->contentType);
96 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData);
97 return cms->d.authEnvelopedData;
98 }
99 return cms_get0_auth_enveloped(cms);
100 }
101
102 int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
103 {
104 EVP_PKEY *pkey;
105 int i;
106 if (ri->type == CMS_RECIPINFO_TRANS)
107 pkey = ri->d.ktri->pkey;
108 else if (ri->type == CMS_RECIPINFO_AGREE) {
109 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
110
111 if (pctx == NULL)
112 return 0;
113 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
114 if (pkey == NULL)
115 return 0;
116 } else
117 return 0;
118
119 if (EVP_PKEY_is_a(pkey, "DHX"))
120 return cms_dh_envelope(ri, cmd);
121 else if (EVP_PKEY_is_a(pkey, "EC"))
122 return cms_ecdh_envelope(ri, cmd);
123 else if (EVP_PKEY_is_a(pkey, "RSA"))
124 return cms_rsa_envelope(ri, cmd);
125
126 /* Something else? We'll give engines etc a chance to handle this */
127 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
128 return 1;
129 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
130 if (i == -2) {
131 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
132 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
133 return 0;
134 }
135 if (i <= 0) {
136 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
137 return 0;
138 }
139 return 1;
140 }
141
142 CMS_EncryptedContentInfo* cms_get0_env_enc_content(const CMS_ContentInfo *cms)
143 {
144 switch (cms_get_enveloped_type(cms)) {
145 case CMS_ENVELOPED_STANDARD:
146 return cms->d.envelopedData->encryptedContentInfo;
147
148 case CMS_ENVELOPED_AUTH:
149 return cms->d.authEnvelopedData->authEncryptedContentInfo;
150
151 default:
152 return NULL;
153 }
154 }
155
156 STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
157 {
158 switch (cms_get_enveloped_type(cms)) {
159 case CMS_ENVELOPED_STANDARD:
160 return cms->d.envelopedData->recipientInfos;
161
162 case CMS_ENVELOPED_AUTH:
163 return cms->d.authEnvelopedData->recipientInfos;
164
165 default:
166 return NULL;
167 }
168 }
169
170 void cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms)
171 {
172 int i;
173 CMS_RecipientInfo *ri;
174 const CMS_CTX *ctx = cms_get0_cmsctx(cms);
175 STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms);
176
177 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
178 ri = sk_CMS_RecipientInfo_value(rinfos, i);
179 if (ri != NULL) {
180 switch (ri->type) {
181 case CMS_RECIPINFO_AGREE:
182 ri->d.kari->cms_ctx = ctx;
183 break;
184 case CMS_RECIPINFO_TRANS:
185 ri->d.ktri->cms_ctx = ctx;
186 x509_set0_libctx(ri->d.ktri->recip, ctx->libctx, ctx->propq);
187 break;
188 case CMS_RECIPINFO_KEK:
189 ri->d.kekri->cms_ctx = ctx;
190 break;
191 case CMS_RECIPINFO_PASS:
192 ri->d.pwri->cms_ctx = ctx;
193 break;
194 default:
195 break;
196 }
197 }
198 }
199 }
200
201 int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
202 {
203 return ri->type;
204 }
205
206 EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
207 {
208 if (ri->type == CMS_RECIPINFO_TRANS)
209 return ri->d.ktri->pctx;
210 else if (ri->type == CMS_RECIPINFO_AGREE)
211 return ri->d.kari->pctx;
212 return NULL;
213 }
214
215 CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher,
216 OPENSSL_CTX *libctx,
217 const char *propq)
218 {
219 CMS_ContentInfo *cms;
220 CMS_EnvelopedData *env;
221
222 cms = CMS_ContentInfo_new_ex(libctx, propq);
223 if (cms == NULL)
224 goto merr;
225 env = cms_enveloped_data_init(cms);
226 if (env == NULL)
227 goto merr;
228
229 if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, 0,
230 cms_get0_cmsctx(cms)))
231 goto merr;
232 return cms;
233 merr:
234 CMS_ContentInfo_free(cms);
235 CMSerr(0, ERR_R_MALLOC_FAILURE);
236 return NULL;
237 }
238
239 CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
240 {
241 return CMS_EnvelopedData_create_ex(cipher, NULL, NULL);
242 }
243
244 CMS_ContentInfo *
245 CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OPENSSL_CTX *libctx,
246 const char *propq)
247 {
248 CMS_ContentInfo *cms;
249 CMS_AuthEnvelopedData *aenv;
250
251 cms = CMS_ContentInfo_new_ex(libctx, propq);
252 if (cms == NULL)
253 goto merr;
254 aenv = cms_auth_enveloped_data_init(cms);
255 if (aenv == NULL)
256 goto merr;
257 if (!cms_EncryptedContent_init(aenv->authEncryptedContentInfo,
258 cipher, NULL, 0, cms_get0_cmsctx(cms)))
259 goto merr;
260 return cms;
261 merr:
262 CMS_ContentInfo_free(cms);
263 CMSerr(0, ERR_R_MALLOC_FAILURE);
264 return NULL;
265 }
266
267
268 CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher)
269 {
270 return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL);
271 }
272
273 /* Key Transport Recipient Info (KTRI) routines */
274
275 /* Initialise a ktri based on passed certificate and key */
276
277 static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
278 EVP_PKEY *pk, unsigned int flags,
279 const CMS_CTX *ctx)
280 {
281 CMS_KeyTransRecipientInfo *ktri;
282 int idtype;
283
284 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
285 if (!ri->d.ktri)
286 return 0;
287 ri->type = CMS_RECIPINFO_TRANS;
288
289 ktri = ri->d.ktri;
290 ktri->cms_ctx = ctx;
291
292 if (flags & CMS_USE_KEYID) {
293 ktri->version = 2;
294 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
295 } else {
296 ktri->version = 0;
297 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
298 }
299
300 /*
301 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
302 * structure.
303 */
304
305 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx))
306 return 0;
307
308 X509_up_ref(recip);
309 EVP_PKEY_up_ref(pk);
310
311 ktri->pkey = pk;
312 ktri->recip = recip;
313
314 if (flags & CMS_KEY_PARAM) {
315 ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, ktri->pkey,
316 ctx->propq);
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 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, 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 CMSerr(CMS_F_CMS_ADD1_RECIPIENT,
369 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
370 goto err;
371
372 }
373
374 if (!sk_CMS_RecipientInfo_push(ris, ri))
375 goto merr;
376
377 return ri;
378
379 merr:
380 CMSerr(CMS_F_CMS_ADD1_RECIPIENT, ERR_R_MALLOC_FAILURE);
381 err:
382 M_ASN1_free_of(ri, CMS_RecipientInfo);
383 return NULL;
384
385 }
386
387 CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip,
388 unsigned int flags)
389 {
390 return CMS_add1_recipient(cms, recip, NULL, NULL, flags);
391 }
392
393 int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
394 EVP_PKEY **pk, X509 **recip,
395 X509_ALGOR **palg)
396 {
397 CMS_KeyTransRecipientInfo *ktri;
398 if (ri->type != CMS_RECIPINFO_TRANS) {
399 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
400 CMS_R_NOT_KEY_TRANSPORT);
401 return 0;
402 }
403
404 ktri = ri->d.ktri;
405
406 if (pk)
407 *pk = ktri->pkey;
408 if (recip)
409 *recip = ktri->recip;
410 if (palg)
411 *palg = ktri->keyEncryptionAlgorithm;
412 return 1;
413 }
414
415 int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
416 ASN1_OCTET_STRING **keyid,
417 X509_NAME **issuer,
418 ASN1_INTEGER **sno)
419 {
420 CMS_KeyTransRecipientInfo *ktri;
421 if (ri->type != CMS_RECIPINFO_TRANS) {
422 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
423 CMS_R_NOT_KEY_TRANSPORT);
424 return 0;
425 }
426 ktri = ri->d.ktri;
427
428 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
429 }
430
431 int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
432 {
433 if (ri->type != CMS_RECIPINFO_TRANS) {
434 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
435 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 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 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, 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 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, 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 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
692 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
693 goto err;
694 }
695
696 if (keylen != exp_keylen) {
697 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
698 goto err;
699 }
700
701 }
702
703 /* Initialize recipient info */
704 ri = M_ASN1_new_of(CMS_RecipientInfo);
705 if (!ri)
706 goto merr;
707
708 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
709 if (!ri->d.kekri)
710 goto merr;
711 ri->type = CMS_RECIPINFO_KEK;
712
713 kekri = ri->d.kekri;
714
715 if (otherTypeId) {
716 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
717 if (kekri->kekid->other == NULL)
718 goto merr;
719 }
720
721 if (!sk_CMS_RecipientInfo_push(ris, ri))
722 goto merr;
723
724 /* After this point no calls can fail */
725
726 kekri->version = 4;
727
728 kekri->key = key;
729 kekri->keylen = keylen;
730
731 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
732
733 kekri->kekid->date = date;
734
735 if (kekri->kekid->other) {
736 kekri->kekid->other->keyAttrId = otherTypeId;
737 kekri->kekid->other->keyAttr = otherType;
738 }
739
740 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
741 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
742
743 return ri;
744
745 merr:
746 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
747 err:
748 M_ASN1_free_of(ri, CMS_RecipientInfo);
749 return NULL;
750 }
751
752 int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
753 X509_ALGOR **palg,
754 ASN1_OCTET_STRING **pid,
755 ASN1_GENERALIZEDTIME **pdate,
756 ASN1_OBJECT **potherid,
757 ASN1_TYPE **pothertype)
758 {
759 CMS_KEKIdentifier *rkid;
760 if (ri->type != CMS_RECIPINFO_KEK) {
761 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
762 return 0;
763 }
764 rkid = ri->d.kekri->kekid;
765 if (palg)
766 *palg = ri->d.kekri->keyEncryptionAlgorithm;
767 if (pid)
768 *pid = rkid->keyIdentifier;
769 if (pdate)
770 *pdate = rkid->date;
771 if (potherid) {
772 if (rkid->other)
773 *potherid = rkid->other->keyAttrId;
774 else
775 *potherid = NULL;
776 }
777 if (pothertype) {
778 if (rkid->other)
779 *pothertype = rkid->other->keyAttr;
780 else
781 *pothertype = NULL;
782 }
783 return 1;
784 }
785
786 int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
787 unsigned char *key, size_t keylen)
788 {
789 CMS_KEKRecipientInfo *kekri;
790 if (ri->type != CMS_RECIPINFO_KEK) {
791 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
792 return 0;
793 }
794
795 kekri = ri->d.kekri;
796 kekri->key = key;
797 kekri->keylen = keylen;
798 return 1;
799 }
800
801 static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx)
802 {
803 const char *alg = NULL;
804
805 switch(keylen) {
806 case 16:
807 alg = "AES-128-WRAP";
808 break;
809 case 24:
810 alg = "AES-192-WRAP";
811 break;
812 case 32:
813 alg = "AES-256-WRAP";
814 break;
815 default:
816 return NULL;
817 }
818 return EVP_CIPHER_fetch(ctx->libctx, alg, ctx->propq);
819 }
820
821
822 /* Encrypt content key in KEK recipient info */
823
824 static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms,
825 CMS_RecipientInfo *ri)
826 {
827 CMS_EncryptedContentInfo *ec;
828 CMS_KEKRecipientInfo *kekri;
829 unsigned char *wkey = NULL;
830 int wkeylen;
831 int r = 0;
832 EVP_CIPHER *cipher = NULL;
833 int outlen = 0;
834 EVP_CIPHER_CTX *ctx = NULL;
835 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
836
837 ec = cms_get0_env_enc_content(cms);
838 if (ec == NULL)
839 return 0;
840
841 kekri = ri->d.kekri;
842
843 if (kekri->key == NULL) {
844 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
845 return 0;
846 }
847
848 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
849 if (cipher == NULL) {
850 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_INVALID_KEY_LENGTH);
851 goto err;
852 }
853
854 /* 8 byte prefix for AES wrap ciphers */
855 wkey = OPENSSL_malloc(ec->keylen + 8);
856 if (wkey == NULL) {
857 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
858 goto err;
859 }
860
861 ctx = EVP_CIPHER_CTX_new();
862 if (ctx == NULL) {
863 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
864 goto err;
865 }
866
867 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
868 if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
869 || !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, ec->keylen)
870 || !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) {
871 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
872 goto err;
873 }
874 wkeylen += outlen;
875 if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) {
876 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
877 goto err;
878 }
879
880 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
881
882 r = 1;
883
884 err:
885 EVP_CIPHER_free(cipher);
886 if (!r)
887 OPENSSL_free(wkey);
888 EVP_CIPHER_CTX_free(ctx);
889
890 return r;
891 }
892
893 /* Decrypt content key in KEK recipient info */
894
895 static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
896 CMS_RecipientInfo *ri)
897 {
898 CMS_EncryptedContentInfo *ec;
899 CMS_KEKRecipientInfo *kekri;
900 unsigned char *ukey = NULL;
901 int ukeylen;
902 int r = 0, wrap_nid;
903 EVP_CIPHER *cipher = NULL;
904 int outlen = 0;
905 EVP_CIPHER_CTX *ctx = NULL;
906 const CMS_CTX *cms_ctx = cms_get0_cmsctx(cms);
907
908 ec = cms_get0_env_enc_content(cms);
909 if (ec == NULL)
910 return 0;
911
912 kekri = ri->d.kekri;
913
914 if (!kekri->key) {
915 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
916 return 0;
917 }
918
919 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
920 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
921 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
922 CMS_R_INVALID_KEY_LENGTH);
923 return 0;
924 }
925
926 /* If encrypted key length is invalid don't bother */
927
928 if (kekri->encryptedKey->length < 16) {
929 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
930 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
931 goto err;
932 }
933
934 cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx);
935 if (cipher == NULL) {
936 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_INVALID_KEY_LENGTH);
937 goto err;
938 }
939
940 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
941 if (ukey == NULL) {
942 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
943 goto err;
944 }
945
946 ctx = EVP_CIPHER_CTX_new();
947 if (ctx == NULL) {
948 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
949 goto err;
950 }
951
952 if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL)
953 || !EVP_DecryptUpdate(ctx, ukey, &ukeylen,
954 kekri->encryptedKey->data,
955 kekri->encryptedKey->length)
956 || !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) {
957 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
958 goto err;
959 }
960 ukeylen += outlen;
961
962 ec->key = ukey;
963 ec->keylen = ukeylen;
964
965 r = 1;
966
967 err:
968 EVP_CIPHER_free(cipher);
969 if (!r)
970 OPENSSL_free(ukey);
971 EVP_CIPHER_CTX_free(ctx);
972
973 return r;
974 }
975
976 int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
977 {
978 switch (ri->type) {
979 case CMS_RECIPINFO_TRANS:
980 return cms_RecipientInfo_ktri_decrypt(cms, ri);
981
982 case CMS_RECIPINFO_KEK:
983 return cms_RecipientInfo_kekri_decrypt(cms, ri);
984
985 case CMS_RECIPINFO_PASS:
986 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
987
988 default:
989 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
990 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
991 return 0;
992 }
993 }
994
995 int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
996 {
997 switch (ri->type) {
998 case CMS_RECIPINFO_TRANS:
999 return cms_RecipientInfo_ktri_encrypt(cms, ri);
1000
1001 case CMS_RECIPINFO_AGREE:
1002 return cms_RecipientInfo_kari_encrypt(cms, ri);
1003
1004 case CMS_RECIPINFO_KEK:
1005 return cms_RecipientInfo_kekri_encrypt(cms, ri);
1006
1007 case CMS_RECIPINFO_PASS:
1008 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
1009
1010 default:
1011 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
1012 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
1013 return 0;
1014 }
1015 }
1016
1017 /* Check structures and fixup version numbers (if necessary) */
1018
1019 static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
1020 {
1021 CMS_OriginatorInfo *org = env->originatorInfo;
1022 int i;
1023 if (org == NULL)
1024 return;
1025 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
1026 CMS_CertificateChoices *cch;
1027 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
1028 if (cch->type == CMS_CERTCHOICE_OTHER) {
1029 env->version = 4;
1030 return;
1031 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
1032 if (env->version < 3)
1033 env->version = 3;
1034 }
1035 }
1036
1037 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
1038 CMS_RevocationInfoChoice *rch;
1039 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
1040 if (rch->type == CMS_REVCHOICE_OTHER) {
1041 env->version = 4;
1042 return;
1043 }
1044 }
1045 }
1046
1047 static void cms_env_set_version(CMS_EnvelopedData *env)
1048 {
1049 int i;
1050 CMS_RecipientInfo *ri;
1051
1052 /*
1053 * Can't set version higher than 4 so if 4 or more already nothing to do.
1054 */
1055 if (env->version >= 4)
1056 return;
1057
1058 cms_env_set_originfo_version(env);
1059
1060 if (env->version >= 3)
1061 return;
1062
1063 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
1064 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
1065 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
1066 env->version = 3;
1067 return;
1068 } else if (ri->type != CMS_RECIPINFO_TRANS
1069 || ri->d.ktri->version != 0) {
1070 env->version = 2;
1071 }
1072 }
1073 if (env->originatorInfo || env->unprotectedAttrs)
1074 env->version = 2;
1075 if (env->version == 2)
1076 return;
1077 env->version = 0;
1078 }
1079
1080 static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms,
1081 STACK_OF(CMS_RecipientInfo) *ris)
1082 {
1083 int i;
1084 CMS_RecipientInfo *ri;
1085
1086 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
1087 ri = sk_CMS_RecipientInfo_value(ris, i);
1088 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0)
1089 return -1;
1090 }
1091 return 1;
1092 }
1093
1094 static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec)
1095 {
1096 ec->cipher = NULL;
1097 OPENSSL_clear_free(ec->key, ec->keylen);
1098 ec->key = NULL;
1099 ec->keylen = 0;
1100 }
1101
1102 static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms)
1103 {
1104 CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo;
1105 BIO *contentBio = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1106 EVP_CIPHER_CTX *ctx = NULL;
1107
1108 if (contentBio == NULL)
1109 return NULL;
1110
1111 BIO_get_cipher_ctx(contentBio, &ctx);
1112 if (ctx == NULL) {
1113 BIO_free(contentBio);
1114 return NULL;
1115 }
1116 /*
1117 * If the selected cipher supports unprotected attributes,
1118 * deal with it using special ctrl function
1119 */
1120 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
1121 && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0,
1122 cms->d.envelopedData->unprotectedAttrs) <= 0) {
1123 BIO_free(contentBio);
1124 return NULL;
1125 }
1126 return contentBio;
1127 }
1128
1129 static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms)
1130 {
1131 CMS_EncryptedContentInfo *ec;
1132 STACK_OF(CMS_RecipientInfo) *rinfos;
1133 int ok = 0;
1134 BIO *ret;
1135 CMS_EnvelopedData *env = cms->d.envelopedData;
1136
1137 /* Get BIO first to set up key */
1138
1139 ec = env->encryptedContentInfo;
1140 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1141
1142 /* If error end of processing */
1143 if (!ret)
1144 return ret;
1145
1146 /* Now encrypt content key according to each RecipientInfo type */
1147 rinfos = env->recipientInfos;
1148 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1149 CMSerr(CMS_F_CMS_ENVELOPEDDATA_ENCRYPTION_INIT_BIO,
1150 CMS_R_ERROR_SETTING_RECIPIENTINFO);
1151 goto err;
1152 }
1153
1154 /* And finally set the version */
1155 cms_env_set_version(env);
1156
1157 ok = 1;
1158
1159 err:
1160 cms_env_clear_ec(ec);
1161 if (ok)
1162 return ret;
1163 BIO_free(ret);
1164 return NULL;
1165 }
1166
1167 BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
1168 {
1169 if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) {
1170 /* If cipher is set it's encryption */
1171 return cms_EnvelopedData_Encryption_init_bio(cms);
1172 }
1173
1174 /* If cipher is not set it's decryption */
1175 return cms_EnvelopedData_Decryption_init_bio(cms);
1176 }
1177
1178 BIO *cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms)
1179 {
1180 CMS_EncryptedContentInfo *ec;
1181 STACK_OF(CMS_RecipientInfo) *rinfos;
1182 int ok = 0;
1183 BIO *ret;
1184 CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData;
1185
1186 /* Get BIO first to set up key */
1187 ec = aenv->authEncryptedContentInfo;
1188 /* Set tag for decryption */
1189 if (ec->cipher == NULL) {
1190 ec->tag = aenv->mac->data;
1191 ec->taglen = aenv->mac->length;
1192 }
1193 ret = cms_EncryptedContent_init_bio(ec, cms_get0_cmsctx(cms));
1194
1195 /* If error or no cipher end of processing */
1196 if (ret == NULL || ec->cipher == NULL)
1197 return ret;
1198
1199 /* Now encrypt content key according to each RecipientInfo type */
1200 rinfos = aenv->recipientInfos;
1201 if (cms_env_encrypt_content_key(cms, rinfos) < 0) {
1202 CMSerr(0, CMS_R_ERROR_SETTING_RECIPIENTINFO);
1203 goto err;
1204 }
1205
1206 /* And finally set the version */
1207 aenv->version = 0;
1208
1209 ok = 1;
1210
1211 err:
1212 cms_env_clear_ec(ec);
1213 if (ok)
1214 return ret;
1215 BIO_free(ret);
1216 return NULL;
1217 }
1218
1219 int cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain)
1220 {
1221 CMS_EnvelopedData *env = NULL;
1222 EVP_CIPHER_CTX *ctx = NULL;
1223 BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER);
1224
1225 env = cms_get0_enveloped(cms);
1226 if (env == NULL)
1227 return 0;
1228
1229 if (mbio == NULL) {
1230 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CONTENT_NOT_FOUND);
1231 return 0;
1232 }
1233
1234 BIO_get_cipher_ctx(mbio, &ctx);
1235
1236 /*
1237 * If the selected cipher supports unprotected attributes,
1238 * deal with it using special ctrl function
1239 */
1240 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
1241 if (env->unprotectedAttrs == NULL)
1242 env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null();
1243
1244 if (env->unprotectedAttrs == NULL) {
1245 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, ERR_R_MALLOC_FAILURE);
1246 return 0;
1247 }
1248
1249 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED,
1250 1, env->unprotectedAttrs) <= 0) {
1251 CMSerr(CMS_F_CMS_ENVELOPEDDATA_FINAL, CMS_R_CTRL_FAILURE);
1252 return 0;
1253 }
1254 }
1255
1256 cms_env_set_version(cms->d.envelopedData);
1257 return 1;
1258 }
1259
1260 int cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio)
1261 {
1262 EVP_CIPHER_CTX *ctx;
1263 unsigned char *tag = NULL;
1264 int taglen, ok = 0;
1265
1266 BIO_get_cipher_ctx(cmsbio, &ctx);
1267
1268 /*
1269 * The tag is set only for encryption. There is nothing to do for
1270 * decryption.
1271 */
1272 if (!EVP_CIPHER_CTX_encrypting(ctx))
1273 return 1;
1274
1275 taglen = EVP_CIPHER_CTX_tag_length(ctx);
1276 if (taglen <= 0
1277 || (tag = OPENSSL_malloc(taglen)) == NULL
1278 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
1279 tag) <= 0) {
1280 CMSerr(0, CMS_R_CIPHER_GET_TAG);
1281 goto err;
1282 }
1283
1284 if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen))
1285 goto err;
1286
1287 ok = 1;
1288 err:
1289 OPENSSL_free(tag);
1290 return ok;
1291 }
1292
1293 /*
1294 * Get RecipientInfo type (if any) supported by a key (public or private). To
1295 * retain compatibility with previous behaviour if the ctrl value isn't
1296 * supported we assume key transport.
1297 */
1298 int cms_pkey_get_ri_type(EVP_PKEY *pk)
1299 {
1300 if (pk->ameth && pk->ameth->pkey_ctrl) {
1301 int i, r;
1302 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
1303 if (i > 0)
1304 return r;
1305 }
1306 return CMS_RECIPINFO_TRANS;
1307 }
1308
1309 int cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type)
1310 {
1311 int supportedRiType;
1312
1313 if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) {
1314 int i, r;
1315
1316 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED,
1317 ri_type, &r);
1318 if (i > 0)
1319 return r;
1320 }
1321
1322 supportedRiType = cms_pkey_get_ri_type(pk);
1323 if (supportedRiType < 0)
1324 return 0;
1325
1326 return (supportedRiType == ri_type);
1327 }