summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/cms/cms_env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/cms/cms_env.c')
-rw-r--r--src/lib/libcrypto/cms/cms_env.c203
1 files changed, 114 insertions, 89 deletions
diff --git a/src/lib/libcrypto/cms/cms_env.c b/src/lib/libcrypto/cms/cms_env.c
index e137cadf34..fbfbdd98ca 100644
--- a/src/lib/libcrypto/cms/cms_env.c
+++ b/src/lib/libcrypto/cms/cms_env.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_env.c,v 1.15 2019/08/10 16:42:20 jsing Exp $ */ 1/* $OpenBSD: cms_env.c,v 1.16 2019/08/10 18:15:52 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -65,17 +65,19 @@
65 65
66/* CMS EnvelopedData Utilities */ 66/* CMS EnvelopedData Utilities */
67 67
68CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms) 68CMS_EnvelopedData *
69cms_get0_enveloped(CMS_ContentInfo *cms)
69{ 70{
70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) { 71 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
71 CMSerr(CMS_F_CMS_GET0_ENVELOPED, 72 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
72 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); 73 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
73 return NULL; 74 return NULL;
74 } 75 }
75 return cms->d.envelopedData; 76 return cms->d.envelopedData;
76} 77}
77 78
78static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms) 79static CMS_EnvelopedData *
80cms_enveloped_data_init(CMS_ContentInfo *cms)
79{ 81{
80 if (cms->d.other == NULL) { 82 if (cms->d.other == NULL) {
81 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData); 83 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
@@ -93,10 +95,12 @@ static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
93 return cms_get0_enveloped(cms); 95 return cms_get0_enveloped(cms);
94} 96}
95 97
96int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd) 98int
99cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
97{ 100{
98 EVP_PKEY *pkey; 101 EVP_PKEY *pkey;
99 int i; 102 int i;
103
100 if (ri->type == CMS_RECIPINFO_TRANS) 104 if (ri->type == CMS_RECIPINFO_TRANS)
101 pkey = ri->d.ktri->pkey; 105 pkey = ri->d.ktri->pkey;
102 else if (ri->type == CMS_RECIPINFO_AGREE) { 106 else if (ri->type == CMS_RECIPINFO_AGREE) {
@@ -113,53 +117,64 @@ int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
113 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri); 117 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
114 if (i == -2) { 118 if (i == -2) {
115 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, 119 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
116 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 120 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
117 return 0; 121 return 0;
118 } 122 }
119 if (i <= 0) { 123 if (i <= 0) {
120 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE); 124 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
121 return 0; 125 return 0;
122 } 126 }
127
123 return 1; 128 return 1;
124} 129}
125 130
126STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms) 131STACK_OF(CMS_RecipientInfo) *
132CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
127{ 133{
128 CMS_EnvelopedData *env; 134 CMS_EnvelopedData *env;
135
129 env = cms_get0_enveloped(cms); 136 env = cms_get0_enveloped(cms);
130 if (!env) 137 if (!env)
131 return NULL; 138 return NULL;
139
132 return env->recipientInfos; 140 return env->recipientInfos;
133} 141}
134 142
135int CMS_RecipientInfo_type(CMS_RecipientInfo *ri) 143int
144CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
136{ 145{
137 return ri->type; 146 return ri->type;
138} 147}
139 148
140EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri) 149EVP_PKEY_CTX *
150CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
141{ 151{
142 if (ri->type == CMS_RECIPINFO_TRANS) 152 if (ri->type == CMS_RECIPINFO_TRANS)
143 return ri->d.ktri->pctx; 153 return ri->d.ktri->pctx;
144 else if (ri->type == CMS_RECIPINFO_AGREE) 154 else if (ri->type == CMS_RECIPINFO_AGREE)
145 return ri->d.kari->pctx; 155 return ri->d.kari->pctx;
156
146 return NULL; 157 return NULL;
147} 158}
148 159
149CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher) 160CMS_ContentInfo *
161CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
150{ 162{
151 CMS_ContentInfo *cms; 163 CMS_ContentInfo *cms;
152 CMS_EnvelopedData *env; 164 CMS_EnvelopedData *env;
165
153 cms = CMS_ContentInfo_new(); 166 cms = CMS_ContentInfo_new();
154 if (cms == NULL) 167 if (cms == NULL)
155 goto merr; 168 goto merr;
156 env = cms_enveloped_data_init(cms); 169 env = cms_enveloped_data_init(cms);
157 if (env == NULL) 170 if (env == NULL)
158 goto merr; 171 goto merr;
159 if (!cms_EncryptedContent_init(env->encryptedContentInfo, 172 if (!cms_EncryptedContent_init(env->encryptedContentInfo, cipher,
160 cipher, NULL, 0)) 173 NULL, 0))
161 goto merr; 174 goto merr;
175
162 return cms; 176 return cms;
177
163 merr: 178 merr:
164 CMS_ContentInfo_free(cms); 179 CMS_ContentInfo_free(cms);
165 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE); 180 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
@@ -170,8 +185,9 @@ CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
170 185
171/* Initialise a ktri based on passed certificate and key */ 186/* Initialise a ktri based on passed certificate and key */
172 187
173static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, 188static int
174 EVP_PKEY *pk, unsigned int flags) 189cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, EVP_PKEY *pk,
190 unsigned int flags)
175{ 191{
176 CMS_KeyTransRecipientInfo *ktri; 192 CMS_KeyTransRecipientInfo *ktri;
177 int idtype; 193 int idtype;
@@ -213,6 +229,7 @@ static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
213 return 0; 229 return 0;
214 } else if (!cms_env_asn1_ctrl(ri, 0)) 230 } else if (!cms_env_asn1_ctrl(ri, 0))
215 return 0; 231 return 0;
232
216 return 1; 233 return 1;
217} 234}
218 235
@@ -220,12 +237,13 @@ static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
220 * Add a recipient certificate using appropriate type of RecipientInfo 237 * Add a recipient certificate using appropriate type of RecipientInfo
221 */ 238 */
222 239
223CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, 240CMS_RecipientInfo *
224 X509 *recip, unsigned int flags) 241CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip, unsigned int flags)
225{ 242{
226 CMS_RecipientInfo *ri = NULL; 243 CMS_RecipientInfo *ri = NULL;
227 CMS_EnvelopedData *env; 244 CMS_EnvelopedData *env;
228 EVP_PKEY *pk = NULL; 245 EVP_PKEY *pk = NULL;
246
229 env = cms_get0_enveloped(cms); 247 env = cms_get0_enveloped(cms);
230 if (!env) 248 if (!env)
231 goto err; 249 goto err;
@@ -237,7 +255,8 @@ CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
237 255
238 pk = X509_get0_pubkey(recip); 256 pk = X509_get0_pubkey(recip);
239 if (!pk) { 257 if (!pk) {
240 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY); 258 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
259 CMS_R_ERROR_GETTING_PUBLIC_KEY);
241 goto err; 260 goto err;
242 } 261 }
243 262
@@ -255,7 +274,7 @@ CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
255 274
256 default: 275 default:
257 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, 276 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
258 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 277 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
259 goto err; 278 goto err;
260 279
261 } 280 }
@@ -270,17 +289,16 @@ CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
270 err: 289 err:
271 M_ASN1_free_of(ri, CMS_RecipientInfo); 290 M_ASN1_free_of(ri, CMS_RecipientInfo);
272 return NULL; 291 return NULL;
273
274} 292}
275 293
276int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, 294int
277 EVP_PKEY **pk, X509 **recip, 295CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, EVP_PKEY **pk,
278 X509_ALGOR **palg) 296 X509 **recip, X509_ALGOR **palg)
279{ 297{
280 CMS_KeyTransRecipientInfo *ktri; 298 CMS_KeyTransRecipientInfo *ktri;
299
281 if (ri->type != CMS_RECIPINFO_TRANS) { 300 if (ri->type != CMS_RECIPINFO_TRANS) {
282 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, 301 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, CMS_R_NOT_KEY_TRANSPORT);
283 CMS_R_NOT_KEY_TRANSPORT);
284 return 0; 302 return 0;
285 } 303 }
286 304
@@ -292,18 +310,19 @@ int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
292 *recip = ktri->recip; 310 *recip = ktri->recip;
293 if (palg) 311 if (palg)
294 *palg = ktri->keyEncryptionAlgorithm; 312 *palg = ktri->keyEncryptionAlgorithm;
313
295 return 1; 314 return 1;
296} 315}
297 316
298int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, 317int
299 ASN1_OCTET_STRING **keyid, 318CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
300 X509_NAME **issuer, 319 ASN1_OCTET_STRING **keyid, X509_NAME **issuer, ASN1_INTEGER **sno)
301 ASN1_INTEGER **sno)
302{ 320{
303 CMS_KeyTransRecipientInfo *ktri; 321 CMS_KeyTransRecipientInfo *ktri;
322
304 if (ri->type != CMS_RECIPINFO_TRANS) { 323 if (ri->type != CMS_RECIPINFO_TRANS) {
305 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID, 324 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
306 CMS_R_NOT_KEY_TRANSPORT); 325 CMS_R_NOT_KEY_TRANSPORT);
307 return 0; 326 return 0;
308 } 327 }
309 ktri = ri->d.ktri; 328 ktri = ri->d.ktri;
@@ -311,17 +330,20 @@ int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
311 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno); 330 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
312} 331}
313 332
314int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) 333int
334CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
315{ 335{
316 if (ri->type != CMS_RECIPINFO_TRANS) { 336 if (ri->type != CMS_RECIPINFO_TRANS) {
317 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP, 337 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
318 CMS_R_NOT_KEY_TRANSPORT); 338 CMS_R_NOT_KEY_TRANSPORT);
319 return -2; 339 return -2;
320 } 340 }
341
321 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); 342 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
322} 343}
323 344
324int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) 345int
346CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
325{ 347{
326 if (ri->type != CMS_RECIPINFO_TRANS) { 348 if (ri->type != CMS_RECIPINFO_TRANS) {
327 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT); 349 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
@@ -329,13 +351,14 @@ int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
329 } 351 }
330 EVP_PKEY_free(ri->d.ktri->pkey); 352 EVP_PKEY_free(ri->d.ktri->pkey);
331 ri->d.ktri->pkey = pkey; 353 ri->d.ktri->pkey = pkey;
354
332 return 1; 355 return 1;
333} 356}
334 357
335/* Encrypt content key in key transport recipient info */ 358/* Encrypt content key in key transport recipient info */
336 359
337static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms, 360static int
338 CMS_RecipientInfo *ri) 361cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
339{ 362{
340 CMS_KeyTransRecipientInfo *ktri; 363 CMS_KeyTransRecipientInfo *ktri;
341 CMS_EncryptedContentInfo *ec; 364 CMS_EncryptedContentInfo *ec;
@@ -394,14 +417,14 @@ static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
394 EVP_PKEY_CTX_free(pctx); 417 EVP_PKEY_CTX_free(pctx);
395 ktri->pctx = NULL; 418 ktri->pctx = NULL;
396 OPENSSL_free(ek); 419 OPENSSL_free(ek);
397 return ret;
398 420
421 return ret;
399} 422}
400 423
401/* Decrypt content key from KTRI */ 424/* Decrypt content key from KTRI */
402 425
403static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, 426static int
404 CMS_RecipientInfo *ri) 427cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
405{ 428{
406 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; 429 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
407 EVP_PKEY *pkey = ktri->pkey; 430 EVP_PKEY *pkey = ktri->pkey;
@@ -409,6 +432,7 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
409 size_t eklen; 432 size_t eklen;
410 int ret = 0; 433 int ret = 0;
411 CMS_EncryptedContentInfo *ec; 434 CMS_EncryptedContentInfo *ec;
435
412 ec = cms->d.envelopedData->encryptedContentInfo; 436 ec = cms->d.envelopedData->encryptedContentInfo;
413 437
414 if (ktri->pkey == NULL) { 438 if (ktri->pkey == NULL) {
@@ -427,14 +451,13 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
427 goto err; 451 goto err;
428 452
429 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT, 453 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
430 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) { 454 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
431 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR); 455 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
432 goto err; 456 goto err;
433 } 457 }
434 458
435 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen, 459 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen, ktri->encryptedKey->data,
436 ktri->encryptedKey->data, 460 ktri->encryptedKey->length) <= 0)
437 ktri->encryptedKey->length) <= 0)
438 goto err; 461 goto err;
439 462
440 ek = OPENSSL_malloc(eklen); 463 ek = OPENSSL_malloc(eklen);
@@ -444,9 +467,8 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
444 goto err; 467 goto err;
445 } 468 }
446 469
447 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen, 470 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen, ktri->encryptedKey->data,
448 ktri->encryptedKey->data, 471 ktri->encryptedKey->length) <= 0) {
449 ktri->encryptedKey->length) <= 0) {
450 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB); 472 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
451 goto err; 473 goto err;
452 } 474 }
@@ -468,11 +490,13 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
468 490
469/* Key Encrypted Key (KEK) RecipientInfo routines */ 491/* Key Encrypted Key (KEK) RecipientInfo routines */
470 492
471int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, 493int
472 const unsigned char *id, size_t idlen) 494CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, const unsigned char *id,
495 size_t idlen)
473{ 496{
474 ASN1_OCTET_STRING tmp_os; 497 ASN1_OCTET_STRING tmp_os;
475 CMS_KEKRecipientInfo *kekri; 498 CMS_KEKRecipientInfo *kekri;
499
476 if (ri->type != CMS_RECIPINFO_KEK) { 500 if (ri->type != CMS_RECIPINFO_KEK) {
477 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK); 501 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
478 return -2; 502 return -2;
@@ -482,12 +506,14 @@ int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
482 tmp_os.flags = 0; 506 tmp_os.flags = 0;
483 tmp_os.data = (unsigned char *)id; 507 tmp_os.data = (unsigned char *)id;
484 tmp_os.length = (int)idlen; 508 tmp_os.length = (int)idlen;
509
485 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); 510 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
486} 511}
487 512
488/* For now hard code AES key wrap info */ 513/* For now hard code AES key wrap info */
489 514
490static size_t aes_wrap_keylen(int nid) 515static size_t
516aes_wrap_keylen(int nid)
491{ 517{
492 switch (nid) { 518 switch (nid) {
493 case NID_id_aes128_wrap: 519 case NID_id_aes128_wrap:
@@ -504,16 +530,15 @@ static size_t aes_wrap_keylen(int nid)
504 } 530 }
505} 531}
506 532
507CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, 533CMS_RecipientInfo *
508 unsigned char *key, size_t keylen, 534CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, unsigned char *key,
509 unsigned char *id, size_t idlen, 535 size_t keylen, unsigned char *id, size_t idlen, ASN1_GENERALIZEDTIME *date,
510 ASN1_GENERALIZEDTIME *date, 536 ASN1_OBJECT *otherTypeId, ASN1_TYPE *otherType)
511 ASN1_OBJECT *otherTypeId,
512 ASN1_TYPE *otherType)
513{ 537{
514 CMS_RecipientInfo *ri = NULL; 538 CMS_RecipientInfo *ri = NULL;
515 CMS_EnvelopedData *env; 539 CMS_EnvelopedData *env;
516 CMS_KEKRecipientInfo *kekri; 540 CMS_KEKRecipientInfo *kekri;
541
517 env = cms_get0_enveloped(cms); 542 env = cms_get0_enveloped(cms);
518 if (!env) 543 if (!env)
519 goto err; 544 goto err;
@@ -601,17 +626,15 @@ CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
601 err: 626 err:
602 M_ASN1_free_of(ri, CMS_RecipientInfo); 627 M_ASN1_free_of(ri, CMS_RecipientInfo);
603 return NULL; 628 return NULL;
604
605} 629}
606 630
607int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, 631int
608 X509_ALGOR **palg, 632CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, X509_ALGOR **palg,
609 ASN1_OCTET_STRING **pid, 633 ASN1_OCTET_STRING **pid, ASN1_GENERALIZEDTIME **pdate,
610 ASN1_GENERALIZEDTIME **pdate, 634 ASN1_OBJECT **potherid, ASN1_TYPE **pothertype)
611 ASN1_OBJECT **potherid,
612 ASN1_TYPE **pothertype)
613{ 635{
614 CMS_KEKIdentifier *rkid; 636 CMS_KEKIdentifier *rkid;
637
615 if (ri->type != CMS_RECIPINFO_KEK) { 638 if (ri->type != CMS_RECIPINFO_KEK) {
616 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK); 639 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
617 return 0; 640 return 0;
@@ -635,13 +658,16 @@ int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
635 else 658 else
636 *pothertype = NULL; 659 *pothertype = NULL;
637 } 660 }
661
638 return 1; 662 return 1;
639} 663}
640 664
641int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, 665int
642 unsigned char *key, size_t keylen) 666CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, unsigned char *key,
667 size_t keylen)
643{ 668{
644 CMS_KEKRecipientInfo *kekri; 669 CMS_KEKRecipientInfo *kekri;
670
645 if (ri->type != CMS_RECIPINFO_KEK) { 671 if (ri->type != CMS_RECIPINFO_KEK) {
646 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK); 672 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
647 return 0; 673 return 0;
@@ -655,8 +681,8 @@ int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
655 681
656/* Encrypt content key in KEK recipient info */ 682/* Encrypt content key in KEK recipient info */
657 683
658static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms, 684static int
659 CMS_RecipientInfo *ri) 685cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
660{ 686{
661 CMS_EncryptedContentInfo *ec; 687 CMS_EncryptedContentInfo *ec;
662 CMS_KEKRecipientInfo *kekri; 688 CMS_KEKRecipientInfo *kekri;
@@ -666,7 +692,6 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
666 int r = 0; 692 int r = 0;
667 693
668 ec = cms->d.envelopedData->encryptedContentInfo; 694 ec = cms->d.envelopedData->encryptedContentInfo;
669
670 kekri = ri->d.kekri; 695 kekri = ri->d.kekri;
671 696
672 if (!kekri->key) { 697 if (!kekri->key) {
@@ -681,14 +706,12 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
681 } 706 }
682 707
683 wkey = OPENSSL_malloc(ec->keylen + 8); 708 wkey = OPENSSL_malloc(ec->keylen + 8);
684
685 if (wkey == NULL) { 709 if (wkey == NULL) {
686 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE); 710 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
687 goto err; 711 goto err;
688 } 712 }
689 713
690 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen); 714 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
691
692 if (wkeylen <= 0) { 715 if (wkeylen <= 0) {
693 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR); 716 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
694 goto err; 717 goto err;
@@ -699,19 +722,17 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
699 r = 1; 722 r = 1;
700 723
701 err: 724 err:
702
703 if (!r) 725 if (!r)
704 OPENSSL_free(wkey); 726 OPENSSL_free(wkey);
705 OPENSSL_cleanse(&actx, sizeof(actx)); 727 OPENSSL_cleanse(&actx, sizeof(actx));
706 728
707 return r; 729 return r;
708
709} 730}
710 731
711/* Decrypt content key in KEK recipient info */ 732/* Decrypt content key in KEK recipient info */
712 733
713static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, 734static int
714 CMS_RecipientInfo *ri) 735cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
715{ 736{
716 CMS_EncryptedContentInfo *ec; 737 CMS_EncryptedContentInfo *ec;
717 CMS_KEKRecipientInfo *kekri; 738 CMS_KEKRecipientInfo *kekri;
@@ -721,7 +742,6 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
721 int r = 0, wrap_nid; 742 int r = 0, wrap_nid;
722 743
723 ec = cms->d.envelopedData->encryptedContentInfo; 744 ec = cms->d.envelopedData->encryptedContentInfo;
724
725 kekri = ri->d.kekri; 745 kekri = ri->d.kekri;
726 746
727 if (!kekri->key) { 747 if (!kekri->key) {
@@ -732,7 +752,7 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
732 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); 752 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
733 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { 753 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
734 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 754 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
735 CMS_R_INVALID_KEY_LENGTH); 755 CMS_R_INVALID_KEY_LENGTH);
736 return 0; 756 return 0;
737 } 757 }
738 758
@@ -740,26 +760,24 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
740 760
741 if (kekri->encryptedKey->length < 16) { 761 if (kekri->encryptedKey->length < 16) {
742 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 762 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
743 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); 763 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
744 goto err; 764 goto err;
745 } 765 }
746 766
747 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) { 767 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
748 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 768 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
749 CMS_R_ERROR_SETTING_KEY); 769 CMS_R_ERROR_SETTING_KEY);
750 goto err; 770 goto err;
751 } 771 }
752 772
753 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); 773 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
754
755 if (ukey == NULL) { 774 if (ukey == NULL) {
756 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE); 775 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
757 goto err; 776 goto err;
758 } 777 }
759 778
760 ukeylen = AES_unwrap_key(&actx, NULL, ukey, 779 ukeylen = AES_unwrap_key(&actx, NULL, ukey, kekri->encryptedKey->data,
761 kekri->encryptedKey->data, 780 kekri->encryptedKey->length);
762 kekri->encryptedKey->length);
763 781
764 if (ukeylen <= 0) { 782 if (ukeylen <= 0) {
765 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR); 783 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
@@ -778,10 +796,10 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
778 OPENSSL_cleanse(&actx, sizeof(actx)); 796 OPENSSL_cleanse(&actx, sizeof(actx));
779 797
780 return r; 798 return r;
781
782} 799}
783 800
784int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) 801int
802CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
785{ 803{
786 switch (ri->type) { 804 switch (ri->type) {
787 case CMS_RECIPINFO_TRANS: 805 case CMS_RECIPINFO_TRANS:
@@ -795,12 +813,13 @@ int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
795 813
796 default: 814 default:
797 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, 815 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
798 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); 816 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
799 return 0; 817 return 0;
800 } 818 }
801} 819}
802 820
803int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) 821int
822CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
804{ 823{
805 switch (ri->type) { 824 switch (ri->type) {
806 case CMS_RECIPINFO_TRANS: 825 case CMS_RECIPINFO_TRANS:
@@ -817,21 +836,24 @@ int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
817 836
818 default: 837 default:
819 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT, 838 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
820 CMS_R_UNSUPPORTED_RECIPIENT_TYPE); 839 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
821 return 0; 840 return 0;
822 } 841 }
823} 842}
824 843
825/* Check structures and fixup version numbers (if necessary) */ 844/* Check structures and fixup version numbers (if necessary) */
826 845
827static void cms_env_set_originfo_version(CMS_EnvelopedData *env) 846static void
847cms_env_set_originfo_version(CMS_EnvelopedData *env)
828{ 848{
829 CMS_OriginatorInfo *org = env->originatorInfo; 849 CMS_OriginatorInfo *org = env->originatorInfo;
830 int i; 850 int i;
851
831 if (org == NULL) 852 if (org == NULL)
832 return; 853 return;
833 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { 854 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
834 CMS_CertificateChoices *cch; 855 CMS_CertificateChoices *cch;
856
835 cch = sk_CMS_CertificateChoices_value(org->certificates, i); 857 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
836 if (cch->type == CMS_CERTCHOICE_OTHER) { 858 if (cch->type == CMS_CERTCHOICE_OTHER) {
837 env->version = 4; 859 env->version = 4;
@@ -844,6 +866,7 @@ static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
844 866
845 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { 867 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
846 CMS_RevocationInfoChoice *rch; 868 CMS_RevocationInfoChoice *rch;
869
847 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); 870 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
848 if (rch->type == CMS_REVCHOICE_OTHER) { 871 if (rch->type == CMS_REVCHOICE_OTHER) {
849 env->version = 4; 872 env->version = 4;
@@ -852,7 +875,8 @@ static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
852 } 875 }
853} 876}
854 877
855static void cms_env_set_version(CMS_EnvelopedData *env) 878static void
879cms_env_set_version(CMS_EnvelopedData *env)
856{ 880{
857 int i; 881 int i;
858 CMS_RecipientInfo *ri; 882 CMS_RecipientInfo *ri;
@@ -885,7 +909,8 @@ static void cms_env_set_version(CMS_EnvelopedData *env)
885 env->version = 0; 909 env->version = 0;
886} 910}
887 911
888BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) 912BIO *
913cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
889{ 914{
890 CMS_EncryptedContentInfo *ec; 915 CMS_EncryptedContentInfo *ec;
891 STACK_OF(CMS_RecipientInfo) *rinfos; 916 STACK_OF(CMS_RecipientInfo) *rinfos;
@@ -928,7 +953,6 @@ BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
928 return ret; 953 return ret;
929 BIO_free(ret); 954 BIO_free(ret);
930 return NULL; 955 return NULL;
931
932} 956}
933 957
934/* 958/*
@@ -936,7 +960,8 @@ BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
936 * retain compatibility with previous behaviour if the ctrl value isn't 960 * retain compatibility with previous behaviour if the ctrl value isn't
937 * supported we assume key transport. 961 * supported we assume key transport.
938 */ 962 */
939int cms_pkey_get_ri_type(EVP_PKEY *pk) 963int
964cms_pkey_get_ri_type(EVP_PKEY *pk)
940{ 965{
941 if (pk->ameth && pk->ameth->pkey_ctrl) { 966 if (pk->ameth && pk->ameth->pkey_ctrl) {
942 int i, r; 967 int i, r;