| 1 |
/* |
| 2 |
* Copyright 2008-2026 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 |
/* |
| 11 |
* Low level key APIs (DH etc) are deprecated for public use, but still ok for |
| 12 |
* internal use. |
| 13 |
*/ |
| 14 |
#include "internal/deprecated.h" |
| 15 |
|
| 16 |
#include "internal/cryptlib.h" |
| 17 |
#include <openssl/asn1t.h> |
| 18 |
#include <openssl/pem.h> |
| 19 |
#include <openssl/x509v3.h> |
| 20 |
#include <openssl/err.h> |
| 21 |
#include <openssl/cms.h> |
| 22 |
#include <openssl/evp.h> |
| 23 |
#include <openssl/core_names.h> |
| 24 |
#include "internal/sizes.h" |
| 25 |
#include "crypto/asn1.h" |
| 26 |
#include "crypto/evp.h" |
| 27 |
#include "crypto/x509.h" |
| 28 |
#include "cms_local.h" |
| 29 |
|
| 30 |
/* CMS EnvelopedData Utilities */ |
| 31 |
static void cms_env_set_version(CMS_EnvelopedData *env); |
| 32 |
|
| 33 |
#define CMS_ENVELOPED_STANDARD 1 |
| 34 |
#define CMS_ENVELOPED_AUTH 2 |
| 35 |
|
| 36 |
static int cms_get_enveloped_type_simple(const CMS_ContentInfo *cms) |
| 37 |
{ |
| 38 |
int nid = OBJ_obj2nid(cms->contentType); |
| 39 |
|
| 40 |
switch (nid) { |
| 41 |
case NID_pkcs7_enveloped: |
| 42 |
return CMS_ENVELOPED_STANDARD; |
| 43 |
|
| 44 |
case NID_id_smime_ct_authEnvelopedData: |
| 45 |
return CMS_ENVELOPED_AUTH; |
| 46 |
|
| 47 |
default: |
| 48 |
return 0; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static int cms_get_enveloped_type(const CMS_ContentInfo *cms) |
| 53 |
{ |
| 54 |
int ret = cms_get_enveloped_type_simple(cms); |
| 55 |
|
| 56 |
if (ret == 0) |
| 57 |
ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
| 58 |
return ret; |
| 59 |
} |
| 60 |
|
| 61 |
CMS_EnvelopedData *ossl_cms_get0_enveloped(CMS_ContentInfo *cms) |
| 62 |
{ |
| 63 |
if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) { |
| 64 |
ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
return cms->d.envelopedData; |
| 68 |
} |
| 69 |
|
| 70 |
CMS_AuthEnvelopedData *ossl_cms_get0_auth_enveloped(CMS_ContentInfo *cms) |
| 71 |
{ |
| 72 |
if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_authEnvelopedData) { |
| 73 |
ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
return cms->d.authEnvelopedData; |
| 77 |
} |
| 78 |
|
| 79 |
static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms) |
| 80 |
{ |
| 81 |
if (cms->d.other == NULL) { |
| 82 |
cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData); |
| 83 |
if (cms->d.envelopedData == NULL) { |
| 84 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
cms->d.envelopedData->version = 0; |
| 88 |
cms->d.envelopedData->encryptedContentInfo->contentType = OBJ_nid2obj(NID_pkcs7_data); |
| 89 |
ASN1_OBJECT_free(cms->contentType); |
| 90 |
cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); |
| 91 |
return cms->d.envelopedData; |
| 92 |
} |
| 93 |
return ossl_cms_get0_enveloped(cms); |
| 94 |
} |
| 95 |
|
| 96 |
static CMS_AuthEnvelopedData * |
| 97 |
cms_auth_enveloped_data_init(CMS_ContentInfo *cms) |
| 98 |
{ |
| 99 |
if (cms->d.other == NULL) { |
| 100 |
cms->d.authEnvelopedData = M_ASN1_new_of(CMS_AuthEnvelopedData); |
| 101 |
if (cms->d.authEnvelopedData == NULL) { |
| 102 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
/* Defined in RFC 5083 - Section 2.1. "AuthEnvelopedData Type" */ |
| 106 |
cms->d.authEnvelopedData->version = 0; |
| 107 |
cms->d.authEnvelopedData->authEncryptedContentInfo->contentType = OBJ_nid2obj(NID_pkcs7_data); |
| 108 |
ASN1_OBJECT_free(cms->contentType); |
| 109 |
cms->contentType = OBJ_nid2obj(NID_id_smime_ct_authEnvelopedData); |
| 110 |
return cms->d.authEnvelopedData; |
| 111 |
} |
| 112 |
return ossl_cms_get0_auth_enveloped(cms); |
| 113 |
} |
| 114 |
|
| 115 |
int ossl_cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd) |
| 116 |
{ |
| 117 |
EVP_PKEY *pkey; |
| 118 |
int i; |
| 119 |
|
| 120 |
switch (ri->type) { |
| 121 |
case CMS_RECIPINFO_TRANS: |
| 122 |
pkey = ri->d.ktri->pkey; |
| 123 |
break; |
| 124 |
case CMS_RECIPINFO_AGREE: { |
| 125 |
EVP_PKEY_CTX *pctx = ri->d.kari->pctx; |
| 126 |
|
| 127 |
if (pctx == NULL) |
| 128 |
return 0; |
| 129 |
pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
| 130 |
if (pkey == NULL) |
| 131 |
return 0; |
| 132 |
break; |
| 133 |
} |
| 134 |
case CMS_RECIPINFO_KEM: |
| 135 |
return ossl_cms_kem_envelope(ri, cmd); |
| 136 |
default: |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
if (EVP_PKEY_is_a(pkey, "DHX") || EVP_PKEY_is_a(pkey, "DH")) |
| 141 |
return ossl_cms_dh_envelope(ri, cmd); |
| 142 |
else if (EVP_PKEY_is_a(pkey, "EC")) |
| 143 |
return ossl_cms_ecdh_envelope(ri, cmd); |
| 144 |
else if (EVP_PKEY_is_a(pkey, "RSA")) |
| 145 |
return ossl_cms_rsa_envelope(ri, cmd); |
| 146 |
|
| 147 |
/* Something else? We'll give engines etc a chance to handle this */ |
| 148 |
if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
| 149 |
return 1; |
| 150 |
i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri); |
| 151 |
if (i == -2) { |
| 152 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
if (i <= 0) { |
| 156 |
ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
return 1; |
| 160 |
} |
| 161 |
|
| 162 |
CMS_EncryptedContentInfo *ossl_cms_get0_env_enc_content(const CMS_ContentInfo *cms) |
| 163 |
{ |
| 164 |
switch (cms_get_enveloped_type(cms)) { |
| 165 |
case CMS_ENVELOPED_STANDARD: |
| 166 |
return cms->d.envelopedData == NULL ? NULL |
| 167 |
: cms->d.envelopedData->encryptedContentInfo; |
| 168 |
|
| 169 |
case CMS_ENVELOPED_AUTH: |
| 170 |
return cms->d.authEnvelopedData == NULL ? NULL |
| 171 |
: cms->d.authEnvelopedData->authEncryptedContentInfo; |
| 172 |
|
| 173 |
default: |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms) |
| 179 |
{ |
| 180 |
switch (cms_get_enveloped_type(cms)) { |
| 181 |
case CMS_ENVELOPED_STANDARD: |
| 182 |
return cms->d.envelopedData->recipientInfos; |
| 183 |
|
| 184 |
case CMS_ENVELOPED_AUTH: |
| 185 |
return cms->d.authEnvelopedData->recipientInfos; |
| 186 |
|
| 187 |
default: |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void ossl_cms_RecipientInfos_set_cmsctx(CMS_ContentInfo *cms) |
| 193 |
{ |
| 194 |
int i; |
| 195 |
CMS_RecipientInfo *ri; |
| 196 |
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
| 197 |
STACK_OF(CMS_RecipientInfo) *rinfos = CMS_get0_RecipientInfos(cms); |
| 198 |
|
| 199 |
for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) { |
| 200 |
ri = sk_CMS_RecipientInfo_value(rinfos, i); |
| 201 |
if (ri != NULL) { |
| 202 |
switch (ri->type) { |
| 203 |
case CMS_RECIPINFO_AGREE: |
| 204 |
ri->d.kari->cms_ctx = ctx; |
| 205 |
break; |
| 206 |
case CMS_RECIPINFO_TRANS: |
| 207 |
ri->d.ktri->cms_ctx = ctx; |
| 208 |
ossl_x509_set0_libctx(ri->d.ktri->recip, |
| 209 |
ossl_cms_ctx_get0_libctx(ctx), |
| 210 |
ossl_cms_ctx_get0_propq(ctx)); |
| 211 |
break; |
| 212 |
case CMS_RECIPINFO_KEK: |
| 213 |
ri->d.kekri->cms_ctx = ctx; |
| 214 |
break; |
| 215 |
case CMS_RECIPINFO_PASS: |
| 216 |
ri->d.pwri->cms_ctx = ctx; |
| 217 |
break; |
| 218 |
case CMS_RECIPINFO_KEM: |
| 219 |
ri->d.ori->d.kemri->cms_ctx = ctx; |
| 220 |
break; |
| 221 |
default: |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
int CMS_RecipientInfo_type(CMS_RecipientInfo *ri) |
| 229 |
{ |
| 230 |
return ri->type; |
| 231 |
} |
| 232 |
|
| 233 |
EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri) |
| 234 |
{ |
| 235 |
if (ri->type == CMS_RECIPINFO_TRANS) |
| 236 |
return ri->d.ktri->pctx; |
| 237 |
else if (ri->type == CMS_RECIPINFO_AGREE) |
| 238 |
return ri->d.kari->pctx; |
| 239 |
else if (ri->type == CMS_RECIPINFO_KEM) |
| 240 |
return ri->d.ori->d.kemri->pctx; |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
|
| 244 |
CMS_ContentInfo *CMS_EnvelopedData_create_ex(const EVP_CIPHER *cipher, |
| 245 |
OSSL_LIB_CTX *libctx, |
| 246 |
const char *propq) |
| 247 |
{ |
| 248 |
CMS_ContentInfo *cms; |
| 249 |
CMS_EnvelopedData *env; |
| 250 |
|
| 251 |
cms = CMS_ContentInfo_new_ex(libctx, propq); |
| 252 |
if (cms == NULL) |
| 253 |
goto err; |
| 254 |
env = cms_enveloped_data_init(cms); |
| 255 |
if (env == NULL) |
| 256 |
goto err; |
| 257 |
|
| 258 |
if (!ossl_cms_EncryptedContent_init(env->encryptedContentInfo, cipher, NULL, |
| 259 |
0, ossl_cms_get0_cmsctx(cms))) |
| 260 |
goto err; |
| 261 |
return cms; |
| 262 |
err: |
| 263 |
CMS_ContentInfo_free(cms); |
| 264 |
ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
| 265 |
return NULL; |
| 266 |
} |
| 267 |
|
| 268 |
CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher) |
| 269 |
{ |
| 270 |
return CMS_EnvelopedData_create_ex(cipher, NULL, NULL); |
| 271 |
} |
| 272 |
|
| 273 |
BIO *CMS_EnvelopedData_decrypt(CMS_EnvelopedData *env, BIO *detached_data, |
| 274 |
EVP_PKEY *pkey, X509 *cert, |
| 275 |
ASN1_OCTET_STRING *secret, unsigned int flags, |
| 276 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 277 |
{ |
| 278 |
CMS_ContentInfo *ci; |
| 279 |
BIO *bio = NULL; |
| 280 |
int res = 0; |
| 281 |
|
| 282 |
if (env == NULL) { |
| 283 |
ERR_raise(ERR_LIB_CMS, ERR_R_PASSED_NULL_PARAMETER); |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
|
| 287 |
if ((ci = CMS_ContentInfo_new_ex(libctx, propq)) == NULL |
| 288 |
|| (bio = BIO_new(BIO_s_mem())) == NULL) |
| 289 |
goto end; |
| 290 |
ci->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); |
| 291 |
ci->d.envelopedData = env; |
| 292 |
if (secret != NULL |
| 293 |
&& CMS_decrypt_set1_password(ci, (unsigned char *)ASN1_STRING_get0_data(secret), |
| 294 |
ASN1_STRING_length(secret)) |
| 295 |
!= 1) |
| 296 |
goto end; |
| 297 |
res = CMS_decrypt(ci, secret == NULL ? pkey : NULL, |
| 298 |
secret == NULL ? cert : NULL, detached_data, bio, flags); |
| 299 |
|
| 300 |
end: |
| 301 |
if (ci != NULL) { |
| 302 |
ci->d.envelopedData = NULL; /* do not indirectly free |env| */ |
| 303 |
ci->contentType = NULL; |
| 304 |
} |
| 305 |
CMS_ContentInfo_free(ci); |
| 306 |
if (!res) { |
| 307 |
BIO_free(bio); |
| 308 |
bio = NULL; |
| 309 |
} |
| 310 |
return bio; |
| 311 |
} |
| 312 |
|
| 313 |
CMS_ContentInfo * |
| 314 |
CMS_AuthEnvelopedData_create_ex(const EVP_CIPHER *cipher, OSSL_LIB_CTX *libctx, |
| 315 |
const char *propq) |
| 316 |
{ |
| 317 |
CMS_ContentInfo *cms; |
| 318 |
CMS_AuthEnvelopedData *aenv; |
| 319 |
|
| 320 |
cms = CMS_ContentInfo_new_ex(libctx, propq); |
| 321 |
if (cms == NULL) |
| 322 |
goto merr; |
| 323 |
aenv = cms_auth_enveloped_data_init(cms); |
| 324 |
if (aenv == NULL) |
| 325 |
goto merr; |
| 326 |
if (!ossl_cms_EncryptedContent_init(aenv->authEncryptedContentInfo, |
| 327 |
cipher, NULL, 0, |
| 328 |
ossl_cms_get0_cmsctx(cms))) |
| 329 |
goto merr; |
| 330 |
return cms; |
| 331 |
merr: |
| 332 |
CMS_ContentInfo_free(cms); |
| 333 |
ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB); |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
|
| 337 |
CMS_ContentInfo *CMS_AuthEnvelopedData_create(const EVP_CIPHER *cipher) |
| 338 |
{ |
| 339 |
return CMS_AuthEnvelopedData_create_ex(cipher, NULL, NULL); |
| 340 |
} |
| 341 |
|
| 342 |
/* Key Transport Recipient Info (KTRI) routines */ |
| 343 |
|
| 344 |
/* Initialise a ktri based on passed certificate and key */ |
| 345 |
|
| 346 |
static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, |
| 347 |
EVP_PKEY *pk, unsigned int flags, |
| 348 |
const CMS_CTX *ctx) |
| 349 |
{ |
| 350 |
CMS_KeyTransRecipientInfo *ktri; |
| 351 |
int idtype; |
| 352 |
|
| 353 |
ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo); |
| 354 |
if (!ri->d.ktri) |
| 355 |
return 0; |
| 356 |
ri->encoded_type = ri->type = CMS_RECIPINFO_TRANS; |
| 357 |
|
| 358 |
ktri = ri->d.ktri; |
| 359 |
ktri->cms_ctx = ctx; |
| 360 |
|
| 361 |
if (flags & CMS_USE_KEYID) { |
| 362 |
ktri->version = 2; |
| 363 |
idtype = CMS_RECIPINFO_KEYIDENTIFIER; |
| 364 |
} else { |
| 365 |
ktri->version = 0; |
| 366 |
idtype = CMS_RECIPINFO_ISSUER_SERIAL; |
| 367 |
} |
| 368 |
|
| 369 |
/* |
| 370 |
* Not a typo: RecipientIdentifier and SignerIdentifier are the same |
| 371 |
* structure. |
| 372 |
*/ |
| 373 |
|
| 374 |
if (!ossl_cms_set1_SignerIdentifier(ktri->rid, recip, idtype, ctx)) |
| 375 |
return 0; |
| 376 |
|
| 377 |
if (!X509_up_ref(recip)) |
| 378 |
return 0; |
| 379 |
if (!EVP_PKEY_up_ref(pk)) { |
| 380 |
X509_free(recip); |
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
ktri->pkey = pk; |
| 385 |
ktri->recip = recip; |
| 386 |
|
| 387 |
if (flags & CMS_KEY_PARAM) { |
| 388 |
ktri->pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
| 389 |
ktri->pkey, |
| 390 |
ossl_cms_ctx_get0_propq(ctx)); |
| 391 |
if (ktri->pctx == NULL) |
| 392 |
return 0; |
| 393 |
if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0) |
| 394 |
return 0; |
| 395 |
} else if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
| 396 |
return 0; |
| 397 |
return 1; |
| 398 |
} |
| 399 |
|
| 400 |
/* |
| 401 |
* Add a recipient certificate using appropriate type of RecipientInfo |
| 402 |
*/ |
| 403 |
|
| 404 |
CMS_RecipientInfo *CMS_add1_recipient(CMS_ContentInfo *cms, X509 *recip, |
| 405 |
EVP_PKEY *originatorPrivKey, |
| 406 |
X509 *originator, unsigned int flags) |
| 407 |
{ |
| 408 |
CMS_RecipientInfo *ri = NULL; |
| 409 |
STACK_OF(CMS_RecipientInfo) *ris; |
| 410 |
EVP_PKEY *pk = NULL; |
| 411 |
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
| 412 |
|
| 413 |
ris = CMS_get0_RecipientInfos(cms); |
| 414 |
if (ris == NULL) |
| 415 |
goto err; |
| 416 |
|
| 417 |
/* Initialize recipient info */ |
| 418 |
ri = M_ASN1_new_of(CMS_RecipientInfo); |
| 419 |
if (ri == NULL) { |
| 420 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 421 |
goto err; |
| 422 |
} |
| 423 |
|
| 424 |
pk = X509_get0_pubkey(recip); |
| 425 |
if (pk == NULL) { |
| 426 |
ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_GETTING_PUBLIC_KEY); |
| 427 |
goto err; |
| 428 |
} |
| 429 |
|
| 430 |
switch (ossl_cms_pkey_get_ri_type(pk)) { |
| 431 |
|
| 432 |
case CMS_RECIPINFO_TRANS: |
| 433 |
if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags, ctx)) |
| 434 |
goto err; |
| 435 |
break; |
| 436 |
|
| 437 |
case CMS_RECIPINFO_AGREE: |
| 438 |
if (!ossl_cms_RecipientInfo_kari_init(ri, recip, pk, originator, |
| 439 |
originatorPrivKey, flags, ctx)) |
| 440 |
goto err; |
| 441 |
break; |
| 442 |
|
| 443 |
case CMS_RECIPINFO_KEM: |
| 444 |
if (!ossl_cms_RecipientInfo_kemri_init(ri, recip, pk, flags, ctx)) |
| 445 |
goto err; |
| 446 |
break; |
| 447 |
|
| 448 |
default: |
| 449 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
| 450 |
goto err; |
| 451 |
} |
| 452 |
|
| 453 |
if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
| 454 |
ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
| 455 |
goto err; |
| 456 |
} |
| 457 |
|
| 458 |
return ri; |
| 459 |
|
| 460 |
err: |
| 461 |
M_ASN1_free_of(ri, CMS_RecipientInfo); |
| 462 |
return NULL; |
| 463 |
} |
| 464 |
|
| 465 |
CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, X509 *recip, |
| 466 |
unsigned int flags) |
| 467 |
{ |
| 468 |
return CMS_add1_recipient(cms, recip, NULL, NULL, flags); |
| 469 |
} |
| 470 |
|
| 471 |
int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, |
| 472 |
EVP_PKEY **pk, X509 **recip, |
| 473 |
X509_ALGOR **palg) |
| 474 |
{ |
| 475 |
CMS_KeyTransRecipientInfo *ktri; |
| 476 |
if (ri->type != CMS_RECIPINFO_TRANS) { |
| 477 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
ktri = ri->d.ktri; |
| 482 |
|
| 483 |
if (pk) |
| 484 |
*pk = ktri->pkey; |
| 485 |
if (recip) |
| 486 |
*recip = ktri->recip; |
| 487 |
if (palg) |
| 488 |
*palg = ktri->keyEncryptionAlgorithm; |
| 489 |
return 1; |
| 490 |
} |
| 491 |
|
| 492 |
int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, |
| 493 |
ASN1_OCTET_STRING **keyid, |
| 494 |
X509_NAME **issuer, |
| 495 |
ASN1_INTEGER **sno) |
| 496 |
{ |
| 497 |
CMS_KeyTransRecipientInfo *ktri; |
| 498 |
if (ri->type != CMS_RECIPINFO_TRANS) { |
| 499 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
ktri = ri->d.ktri; |
| 503 |
|
| 504 |
return ossl_cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, |
| 505 |
sno); |
| 506 |
} |
| 507 |
|
| 508 |
int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) |
| 509 |
{ |
| 510 |
if (ri->type != CMS_RECIPINFO_TRANS) { |
| 511 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
| 512 |
return -2; |
| 513 |
} |
| 514 |
return ossl_cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); |
| 515 |
} |
| 516 |
|
| 517 |
int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) |
| 518 |
{ |
| 519 |
if (ri->type != CMS_RECIPINFO_TRANS) { |
| 520 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
| 521 |
return 0; |
| 522 |
} |
| 523 |
EVP_PKEY_free(ri->d.ktri->pkey); |
| 524 |
ri->d.ktri->pkey = pkey; |
| 525 |
return 1; |
| 526 |
} |
| 527 |
|
| 528 |
/* Encrypt content key in key transport recipient info */ |
| 529 |
|
| 530 |
static int cms_RecipientInfo_ktri_encrypt(const CMS_ContentInfo *cms, |
| 531 |
CMS_RecipientInfo *ri) |
| 532 |
{ |
| 533 |
CMS_KeyTransRecipientInfo *ktri; |
| 534 |
CMS_EncryptedContentInfo *ec; |
| 535 |
EVP_PKEY_CTX *pctx; |
| 536 |
unsigned char *ek = NULL; |
| 537 |
size_t eklen; |
| 538 |
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
| 539 |
|
| 540 |
int ret = 0; |
| 541 |
|
| 542 |
if (ri->type != CMS_RECIPINFO_TRANS) { |
| 543 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEY_TRANSPORT); |
| 544 |
return 0; |
| 545 |
} |
| 546 |
ktri = ri->d.ktri; |
| 547 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 548 |
|
| 549 |
pctx = ktri->pctx; |
| 550 |
|
| 551 |
if (pctx) { |
| 552 |
if (!ossl_cms_env_asn1_ctrl(ri, 0)) |
| 553 |
goto err; |
| 554 |
} else { |
| 555 |
pctx = EVP_PKEY_CTX_new_from_pkey(ossl_cms_ctx_get0_libctx(ctx), |
| 556 |
ktri->pkey, |
| 557 |
ossl_cms_ctx_get0_propq(ctx)); |
| 558 |
if (pctx == NULL) |
| 559 |
return 0; |
| 560 |
|
| 561 |
if (EVP_PKEY_encrypt_init(pctx) <= 0) |
| 562 |
goto err; |
| 563 |
} |
| 564 |
|
| 565 |
if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0) |
| 566 |
goto err; |
| 567 |
|
| 568 |
ek = OPENSSL_malloc(eklen); |
| 569 |
if (ek == NULL) |
| 570 |
goto err; |
| 571 |
|
| 572 |
if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0) |
| 573 |
goto err; |
| 574 |
|
| 575 |
ASN1_STRING_set0(ktri->encryptedKey, ek, (int)eklen); |
| 576 |
ek = NULL; |
| 577 |
|
| 578 |
ret = 1; |
| 579 |
|
| 580 |
err: |
| 581 |
EVP_PKEY_CTX_free(pctx); |
| 582 |
ktri->pctx = NULL; |
| 583 |
OPENSSL_free(ek); |
| 584 |
return ret; |
| 585 |
} |
| 586 |
|
| 587 |
/* Decrypt content key from KTRI */ |
| 588 |
|
| 589 |
static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, |
| 590 |
CMS_RecipientInfo *ri) |
| 591 |
{ |
| 592 |
CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; |
| 593 |
EVP_PKEY *pkey = ktri->pkey; |
| 594 |
unsigned char *ek = NULL; |
| 595 |
size_t eklen; |
| 596 |
int ret = 0; |
| 597 |
size_t fixlen = 0; |
| 598 |
const EVP_CIPHER *cipher = NULL; |
| 599 |
EVP_CIPHER *fetched_cipher = NULL; |
| 600 |
CMS_EncryptedContentInfo *ec; |
| 601 |
const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms); |
| 602 |
OSSL_LIB_CTX *libctx = ossl_cms_ctx_get0_libctx(ctx); |
| 603 |
const char *propq = ossl_cms_ctx_get0_propq(ctx); |
| 604 |
|
| 605 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 606 |
|
| 607 |
if (ktri->pkey == NULL) { |
| 608 |
ERR_raise(ERR_LIB_CMS, CMS_R_NO_PRIVATE_KEY); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
|
| 612 |
if (cms->d.envelopedData->encryptedContentInfo->havenocert |
| 613 |
&& !cms->d.envelopedData->encryptedContentInfo->debug) { |
| 614 |
X509_ALGOR *calg = ec->contentEncryptionAlgorithm; |
| 615 |
char name[OSSL_MAX_NAME_SIZE]; |
| 616 |
|
| 617 |
OBJ_obj2txt(name, sizeof(name), calg->algorithm, 0); |
| 618 |
|
| 619 |
(void)ERR_set_mark(); |
| 620 |
fetched_cipher = EVP_CIPHER_fetch(libctx, name, propq); |
| 621 |
|
| 622 |
if (fetched_cipher != NULL) |
| 623 |
cipher = fetched_cipher; |
| 624 |
else |
| 625 |
cipher = EVP_get_cipherbyobj(calg->algorithm); |
| 626 |
if (cipher == NULL) { |
| 627 |
(void)ERR_clear_last_mark(); |
| 628 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER); |
| 629 |
return 0; |
| 630 |
} |
| 631 |
(void)ERR_pop_to_mark(); |
| 632 |
|
| 633 |
fixlen = EVP_CIPHER_get_key_length(cipher); |
| 634 |
EVP_CIPHER_free(fetched_cipher); |
| 635 |
} |
| 636 |
|
| 637 |
ktri->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
| 638 |
if (ktri->pctx == NULL) |
| 639 |
goto err; |
| 640 |
|
| 641 |
if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0) |
| 642 |
goto err; |
| 643 |
|
| 644 |
if (!ossl_cms_env_asn1_ctrl(ri, 1)) |
| 645 |
goto err; |
| 646 |
|
| 647 |
if (evp_pkey_decrypt_alloc(ktri->pctx, &ek, &eklen, fixlen, |
| 648 |
ktri->encryptedKey->data, |
| 649 |
ktri->encryptedKey->length) |
| 650 |
<= 0) |
| 651 |
goto err; |
| 652 |
|
| 653 |
ret = 1; |
| 654 |
|
| 655 |
OPENSSL_clear_free(ec->key, ec->keylen); |
| 656 |
ec->key = ek; |
| 657 |
ec->keylen = eklen; |
| 658 |
|
| 659 |
err: |
| 660 |
EVP_PKEY_CTX_free(ktri->pctx); |
| 661 |
ktri->pctx = NULL; |
| 662 |
if (!ret) |
| 663 |
OPENSSL_free(ek); |
| 664 |
|
| 665 |
return ret; |
| 666 |
} |
| 667 |
|
| 668 |
/* Key Encrypted Key (KEK) RecipientInfo routines */ |
| 669 |
|
| 670 |
int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, |
| 671 |
const unsigned char *id, size_t idlen) |
| 672 |
{ |
| 673 |
ASN1_OCTET_STRING tmp_os; |
| 674 |
CMS_KEKRecipientInfo *kekri; |
| 675 |
if (ri->type != CMS_RECIPINFO_KEK) { |
| 676 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
| 677 |
return -2; |
| 678 |
} |
| 679 |
kekri = ri->d.kekri; |
| 680 |
tmp_os.type = V_ASN1_OCTET_STRING; |
| 681 |
tmp_os.flags = 0; |
| 682 |
tmp_os.data = (unsigned char *)id; |
| 683 |
tmp_os.length = (int)idlen; |
| 684 |
return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); |
| 685 |
} |
| 686 |
|
| 687 |
/* For now hard code AES key wrap info */ |
| 688 |
|
| 689 |
static size_t aes_wrap_keylen(int nid) |
| 690 |
{ |
| 691 |
switch (nid) { |
| 692 |
case NID_id_aes128_wrap: |
| 693 |
return 16; |
| 694 |
|
| 695 |
case NID_id_aes192_wrap: |
| 696 |
return 24; |
| 697 |
|
| 698 |
case NID_id_aes256_wrap: |
| 699 |
return 32; |
| 700 |
|
| 701 |
default: |
| 702 |
return 0; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, |
| 707 |
unsigned char *key, size_t keylen, |
| 708 |
unsigned char *id, size_t idlen, |
| 709 |
ASN1_GENERALIZEDTIME *date, |
| 710 |
ASN1_OBJECT *otherTypeId, |
| 711 |
ASN1_TYPE *otherType) |
| 712 |
{ |
| 713 |
CMS_RecipientInfo *ri = NULL; |
| 714 |
CMS_KEKRecipientInfo *kekri; |
| 715 |
STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms); |
| 716 |
|
| 717 |
if (ris == NULL || idlen > INT_MAX) |
| 718 |
goto err; |
| 719 |
|
| 720 |
if (nid == NID_undef) { |
| 721 |
switch (keylen) { |
| 722 |
case 16: |
| 723 |
nid = NID_id_aes128_wrap; |
| 724 |
break; |
| 725 |
|
| 726 |
case 24: |
| 727 |
nid = NID_id_aes192_wrap; |
| 728 |
break; |
| 729 |
|
| 730 |
case 32: |
| 731 |
nid = NID_id_aes256_wrap; |
| 732 |
break; |
| 733 |
|
| 734 |
default: |
| 735 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 736 |
goto err; |
| 737 |
} |
| 738 |
|
| 739 |
} else { |
| 740 |
|
| 741 |
size_t exp_keylen = aes_wrap_keylen(nid); |
| 742 |
|
| 743 |
if (!exp_keylen) { |
| 744 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM); |
| 745 |
goto err; |
| 746 |
} |
| 747 |
|
| 748 |
if (keylen != exp_keylen) { |
| 749 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 750 |
goto err; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
/* Initialize recipient info */ |
| 755 |
ri = M_ASN1_new_of(CMS_RecipientInfo); |
| 756 |
if (!ri) { |
| 757 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 758 |
goto err; |
| 759 |
} |
| 760 |
|
| 761 |
ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo); |
| 762 |
if (!ri->d.kekri) { |
| 763 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 764 |
goto err; |
| 765 |
} |
| 766 |
ri->encoded_type = ri->type = CMS_RECIPINFO_KEK; |
| 767 |
|
| 768 |
kekri = ri->d.kekri; |
| 769 |
|
| 770 |
if (otherTypeId) { |
| 771 |
kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute); |
| 772 |
if (kekri->kekid->other == NULL) { |
| 773 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 774 |
goto err; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
| 779 |
ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
| 780 |
goto err; |
| 781 |
} |
| 782 |
|
| 783 |
/* After this point no calls can fail */ |
| 784 |
|
| 785 |
kekri->version = 4; |
| 786 |
|
| 787 |
kekri->key = key; |
| 788 |
kekri->keylen = keylen; |
| 789 |
|
| 790 |
ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, (int)idlen); |
| 791 |
|
| 792 |
kekri->kekid->date = date; |
| 793 |
|
| 794 |
if (kekri->kekid->other) { |
| 795 |
kekri->kekid->other->keyAttrId = otherTypeId; |
| 796 |
kekri->kekid->other->keyAttr = otherType; |
| 797 |
} |
| 798 |
|
| 799 |
(void)X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, OBJ_nid2obj(nid), |
| 800 |
V_ASN1_UNDEF, NULL); /* cannot fail */ |
| 801 |
|
| 802 |
return ri; |
| 803 |
|
| 804 |
err: |
| 805 |
M_ASN1_free_of(ri, CMS_RecipientInfo); |
| 806 |
return NULL; |
| 807 |
} |
| 808 |
|
| 809 |
int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, |
| 810 |
X509_ALGOR **palg, |
| 811 |
ASN1_OCTET_STRING **pid, |
| 812 |
ASN1_GENERALIZEDTIME **pdate, |
| 813 |
ASN1_OBJECT **potherid, |
| 814 |
ASN1_TYPE **pothertype) |
| 815 |
{ |
| 816 |
CMS_KEKIdentifier *rkid; |
| 817 |
if (ri->type != CMS_RECIPINFO_KEK) { |
| 818 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
| 819 |
return 0; |
| 820 |
} |
| 821 |
rkid = ri->d.kekri->kekid; |
| 822 |
if (palg) |
| 823 |
*palg = ri->d.kekri->keyEncryptionAlgorithm; |
| 824 |
if (pid) |
| 825 |
*pid = rkid->keyIdentifier; |
| 826 |
if (pdate) |
| 827 |
*pdate = rkid->date; |
| 828 |
if (potherid) { |
| 829 |
if (rkid->other) |
| 830 |
*potherid = rkid->other->keyAttrId; |
| 831 |
else |
| 832 |
*potherid = NULL; |
| 833 |
} |
| 834 |
if (pothertype) { |
| 835 |
if (rkid->other) |
| 836 |
*pothertype = rkid->other->keyAttr; |
| 837 |
else |
| 838 |
*pothertype = NULL; |
| 839 |
} |
| 840 |
return 1; |
| 841 |
} |
| 842 |
|
| 843 |
int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, |
| 844 |
unsigned char *key, size_t keylen) |
| 845 |
{ |
| 846 |
CMS_KEKRecipientInfo *kekri; |
| 847 |
if (ri->type != CMS_RECIPINFO_KEK) { |
| 848 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_KEK); |
| 849 |
return 0; |
| 850 |
} |
| 851 |
|
| 852 |
kekri = ri->d.kekri; |
| 853 |
kekri->key = key; |
| 854 |
kekri->keylen = keylen; |
| 855 |
return 1; |
| 856 |
} |
| 857 |
|
| 858 |
static EVP_CIPHER *cms_get_key_wrap_cipher(size_t keylen, const CMS_CTX *ctx) |
| 859 |
{ |
| 860 |
const char *alg = NULL; |
| 861 |
|
| 862 |
switch (keylen) { |
| 863 |
case 16: |
| 864 |
alg = "AES-128-WRAP"; |
| 865 |
break; |
| 866 |
case 24: |
| 867 |
alg = "AES-192-WRAP"; |
| 868 |
break; |
| 869 |
case 32: |
| 870 |
alg = "AES-256-WRAP"; |
| 871 |
break; |
| 872 |
default: |
| 873 |
return NULL; |
| 874 |
} |
| 875 |
return EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(ctx), alg, |
| 876 |
ossl_cms_ctx_get0_propq(ctx)); |
| 877 |
} |
| 878 |
|
| 879 |
/* Encrypt content key in KEK recipient info */ |
| 880 |
|
| 881 |
static int cms_RecipientInfo_kekri_encrypt(const CMS_ContentInfo *cms, |
| 882 |
CMS_RecipientInfo *ri) |
| 883 |
{ |
| 884 |
CMS_EncryptedContentInfo *ec; |
| 885 |
CMS_KEKRecipientInfo *kekri; |
| 886 |
unsigned char *wkey = NULL; |
| 887 |
int wkeylen; |
| 888 |
int r = 0; |
| 889 |
EVP_CIPHER *cipher = NULL; |
| 890 |
int outlen = 0; |
| 891 |
EVP_CIPHER_CTX *ctx = NULL; |
| 892 |
const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
| 893 |
|
| 894 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 895 |
if (ec == NULL) |
| 896 |
return 0; |
| 897 |
|
| 898 |
kekri = ri->d.kekri; |
| 899 |
|
| 900 |
if (kekri->key == NULL) { |
| 901 |
ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
| 902 |
return 0; |
| 903 |
} |
| 904 |
|
| 905 |
cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
| 906 |
if (cipher == NULL) { |
| 907 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 908 |
goto err; |
| 909 |
} |
| 910 |
|
| 911 |
/* 8 byte prefix for AES wrap ciphers */ |
| 912 |
wkey = OPENSSL_malloc(ec->keylen + 8); |
| 913 |
if (wkey == NULL) |
| 914 |
goto err; |
| 915 |
|
| 916 |
ctx = EVP_CIPHER_CTX_new(); |
| 917 |
if (ctx == NULL) { |
| 918 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 919 |
goto err; |
| 920 |
} |
| 921 |
|
| 922 |
EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); |
| 923 |
if (!EVP_EncryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
| 924 |
|| !EVP_EncryptUpdate(ctx, wkey, &wkeylen, ec->key, (int)ec->keylen) |
| 925 |
|| !EVP_EncryptFinal_ex(ctx, wkey + wkeylen, &outlen)) { |
| 926 |
ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
| 927 |
goto err; |
| 928 |
} |
| 929 |
wkeylen += outlen; |
| 930 |
if (!ossl_assert((size_t)wkeylen == ec->keylen + 8)) { |
| 931 |
ERR_raise(ERR_LIB_CMS, CMS_R_WRAP_ERROR); |
| 932 |
goto err; |
| 933 |
} |
| 934 |
|
| 935 |
ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen); |
| 936 |
|
| 937 |
r = 1; |
| 938 |
|
| 939 |
err: |
| 940 |
EVP_CIPHER_free(cipher); |
| 941 |
if (!r) |
| 942 |
OPENSSL_free(wkey); |
| 943 |
EVP_CIPHER_CTX_free(ctx); |
| 944 |
|
| 945 |
return r; |
| 946 |
} |
| 947 |
|
| 948 |
/* Decrypt content key in KEK recipient info */ |
| 949 |
|
| 950 |
static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, |
| 951 |
CMS_RecipientInfo *ri) |
| 952 |
{ |
| 953 |
CMS_EncryptedContentInfo *ec; |
| 954 |
CMS_KEKRecipientInfo *kekri; |
| 955 |
unsigned char *ukey = NULL; |
| 956 |
int ukeylen; |
| 957 |
int r = 0, wrap_nid; |
| 958 |
EVP_CIPHER *cipher = NULL; |
| 959 |
int outlen = 0; |
| 960 |
EVP_CIPHER_CTX *ctx = NULL; |
| 961 |
const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
| 962 |
|
| 963 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 964 |
if (ec == NULL) |
| 965 |
return 0; |
| 966 |
|
| 967 |
kekri = ri->d.kekri; |
| 968 |
|
| 969 |
if (!kekri->key) { |
| 970 |
ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY); |
| 971 |
return 0; |
| 972 |
} |
| 973 |
|
| 974 |
wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); |
| 975 |
if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { |
| 976 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 977 |
return 0; |
| 978 |
} |
| 979 |
|
| 980 |
/* If encrypted key length is invalid don't bother */ |
| 981 |
|
| 982 |
if (kekri->encryptedKey->length < 16) { |
| 983 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); |
| 984 |
goto err; |
| 985 |
} |
| 986 |
|
| 987 |
cipher = cms_get_key_wrap_cipher(kekri->keylen, cms_ctx); |
| 988 |
if (cipher == NULL) { |
| 989 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 990 |
goto err; |
| 991 |
} |
| 992 |
|
| 993 |
ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); |
| 994 |
if (ukey == NULL) |
| 995 |
goto err; |
| 996 |
|
| 997 |
ctx = EVP_CIPHER_CTX_new(); |
| 998 |
if (ctx == NULL) { |
| 999 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 1000 |
goto err; |
| 1001 |
} |
| 1002 |
|
| 1003 |
if (!EVP_DecryptInit_ex(ctx, cipher, NULL, kekri->key, NULL) |
| 1004 |
|| !EVP_DecryptUpdate(ctx, ukey, &ukeylen, |
| 1005 |
kekri->encryptedKey->data, |
| 1006 |
kekri->encryptedKey->length) |
| 1007 |
|| !EVP_DecryptFinal_ex(ctx, ukey + ukeylen, &outlen)) { |
| 1008 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_ERROR); |
| 1009 |
goto err; |
| 1010 |
} |
| 1011 |
ukeylen += outlen; |
| 1012 |
|
| 1013 |
OPENSSL_clear_free(ec->key, ec->keylen); |
| 1014 |
ec->key = ukey; |
| 1015 |
ec->keylen = ukeylen; |
| 1016 |
|
| 1017 |
r = 1; |
| 1018 |
|
| 1019 |
err: |
| 1020 |
EVP_CIPHER_free(cipher); |
| 1021 |
if (!r) |
| 1022 |
OPENSSL_free(ukey); |
| 1023 |
EVP_CIPHER_CTX_free(ctx); |
| 1024 |
|
| 1025 |
return r; |
| 1026 |
} |
| 1027 |
|
| 1028 |
int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
| 1029 |
{ |
| 1030 |
switch (ri->type) { |
| 1031 |
case CMS_RECIPINFO_TRANS: |
| 1032 |
return cms_RecipientInfo_ktri_decrypt(cms, ri); |
| 1033 |
|
| 1034 |
case CMS_RECIPINFO_KEK: |
| 1035 |
return cms_RecipientInfo_kekri_decrypt(cms, ri); |
| 1036 |
|
| 1037 |
case CMS_RECIPINFO_PASS: |
| 1038 |
return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 0); |
| 1039 |
|
| 1040 |
case CMS_RECIPINFO_KEM: |
| 1041 |
return ossl_cms_RecipientInfo_kemri_decrypt(cms, ri); |
| 1042 |
|
| 1043 |
default: |
| 1044 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); |
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
int CMS_RecipientInfo_encrypt(const CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
| 1050 |
{ |
| 1051 |
switch (ri->type) { |
| 1052 |
case CMS_RECIPINFO_TRANS: |
| 1053 |
return cms_RecipientInfo_ktri_encrypt(cms, ri); |
| 1054 |
|
| 1055 |
case CMS_RECIPINFO_AGREE: |
| 1056 |
return ossl_cms_RecipientInfo_kari_encrypt(cms, ri); |
| 1057 |
|
| 1058 |
case CMS_RECIPINFO_KEK: |
| 1059 |
return cms_RecipientInfo_kekri_encrypt(cms, ri); |
| 1060 |
|
| 1061 |
case CMS_RECIPINFO_PASS: |
| 1062 |
return ossl_cms_RecipientInfo_pwri_crypt(cms, ri, 1); |
| 1063 |
|
| 1064 |
case CMS_RECIPINFO_KEM: |
| 1065 |
return ossl_cms_RecipientInfo_kemri_encrypt(cms, ri); |
| 1066 |
|
| 1067 |
default: |
| 1068 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENT_TYPE); |
| 1069 |
return 0; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* Check structures and fixup version numbers (if necessary) */ |
| 1074 |
|
| 1075 |
static void cms_env_set_originfo_version(CMS_EnvelopedData *env) |
| 1076 |
{ |
| 1077 |
CMS_OriginatorInfo *org = env->originatorInfo; |
| 1078 |
int i; |
| 1079 |
if (org == NULL) |
| 1080 |
return; |
| 1081 |
for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { |
| 1082 |
CMS_CertificateChoices *cch; |
| 1083 |
cch = sk_CMS_CertificateChoices_value(org->certificates, i); |
| 1084 |
if (cch->type == CMS_CERTCHOICE_OTHER) { |
| 1085 |
env->version = 4; |
| 1086 |
return; |
| 1087 |
} else if (cch->type == CMS_CERTCHOICE_V2ACERT) { |
| 1088 |
if (env->version < 3) |
| 1089 |
env->version = 3; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { |
| 1094 |
CMS_RevocationInfoChoice *rch; |
| 1095 |
rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); |
| 1096 |
if (rch->type == CMS_REVCHOICE_OTHER) { |
| 1097 |
env->version = 4; |
| 1098 |
return; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
static void cms_env_set_version(CMS_EnvelopedData *env) |
| 1104 |
{ |
| 1105 |
int i; |
| 1106 |
CMS_RecipientInfo *ri; |
| 1107 |
|
| 1108 |
/* |
| 1109 |
* Can't set version higher than 4 so if 4 or more already nothing to do. |
| 1110 |
*/ |
| 1111 |
if (env->version >= 4) |
| 1112 |
return; |
| 1113 |
|
| 1114 |
cms_env_set_originfo_version(env); |
| 1115 |
|
| 1116 |
if (env->version >= 3) |
| 1117 |
return; |
| 1118 |
|
| 1119 |
for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) { |
| 1120 |
ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i); |
| 1121 |
if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER |
| 1122 |
|| ri->type == CMS_RECIPINFO_KEM) { |
| 1123 |
env->version = 3; |
| 1124 |
return; |
| 1125 |
} else if (ri->type != CMS_RECIPINFO_TRANS |
| 1126 |
|| ri->d.ktri->version != 0) { |
| 1127 |
env->version = 2; |
| 1128 |
} |
| 1129 |
} |
| 1130 |
if (env->originatorInfo || env->unprotectedAttrs) |
| 1131 |
env->version = 2; |
| 1132 |
if (env->version == 2) |
| 1133 |
return; |
| 1134 |
env->version = 0; |
| 1135 |
} |
| 1136 |
|
| 1137 |
static int cms_env_encrypt_content_key(const CMS_ContentInfo *cms, |
| 1138 |
STACK_OF(CMS_RecipientInfo) *ris) |
| 1139 |
{ |
| 1140 |
int i; |
| 1141 |
CMS_RecipientInfo *ri; |
| 1142 |
|
| 1143 |
for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { |
| 1144 |
ri = sk_CMS_RecipientInfo_value(ris, i); |
| 1145 |
if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) |
| 1146 |
return -1; |
| 1147 |
} |
| 1148 |
return 1; |
| 1149 |
} |
| 1150 |
|
| 1151 |
static void cms_env_clear_ec(CMS_EncryptedContentInfo *ec) |
| 1152 |
{ |
| 1153 |
ec->cipher = NULL; |
| 1154 |
OPENSSL_clear_free(ec->key, ec->keylen); |
| 1155 |
ec->key = NULL; |
| 1156 |
ec->keylen = 0; |
| 1157 |
} |
| 1158 |
|
| 1159 |
static BIO *cms_EnvelopedData_Decryption_init_bio(CMS_ContentInfo *cms) |
| 1160 |
{ |
| 1161 |
CMS_EncryptedContentInfo *ec = cms->d.envelopedData->encryptedContentInfo; |
| 1162 |
BIO *contentBio = ossl_cms_EncryptedContent_init_bio(ec, |
| 1163 |
ossl_cms_get0_cmsctx(cms), |
| 1164 |
0); |
| 1165 |
EVP_CIPHER_CTX *ctx = NULL; |
| 1166 |
|
| 1167 |
if (contentBio == NULL) |
| 1168 |
return NULL; |
| 1169 |
|
| 1170 |
BIO_get_cipher_ctx(contentBio, &ctx); |
| 1171 |
if (ctx == NULL) { |
| 1172 |
BIO_free(contentBio); |
| 1173 |
return NULL; |
| 1174 |
} |
| 1175 |
/* |
| 1176 |
* If the selected cipher supports unprotected attributes, |
| 1177 |
* deal with it using special ctrl function |
| 1178 |
*/ |
| 1179 |
if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
| 1180 |
& EVP_CIPH_FLAG_CIPHER_WITH_MAC) |
| 1181 |
!= 0 |
| 1182 |
&& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, 0, |
| 1183 |
cms->d.envelopedData->unprotectedAttrs) |
| 1184 |
<= 0) { |
| 1185 |
BIO_free(contentBio); |
| 1186 |
return NULL; |
| 1187 |
} |
| 1188 |
return contentBio; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static BIO *cms_EnvelopedData_Encryption_init_bio(CMS_ContentInfo *cms) |
| 1192 |
{ |
| 1193 |
CMS_EncryptedContentInfo *ec; |
| 1194 |
STACK_OF(CMS_RecipientInfo) *rinfos; |
| 1195 |
int ok = 0; |
| 1196 |
BIO *ret; |
| 1197 |
CMS_EnvelopedData *env = cms->d.envelopedData; |
| 1198 |
|
| 1199 |
/* Get BIO first to set up key */ |
| 1200 |
|
| 1201 |
ec = env->encryptedContentInfo; |
| 1202 |
ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 0); |
| 1203 |
|
| 1204 |
/* If error end of processing */ |
| 1205 |
if (!ret) |
| 1206 |
return ret; |
| 1207 |
|
| 1208 |
/* Now encrypt content key according to each RecipientInfo type */ |
| 1209 |
rinfos = env->recipientInfos; |
| 1210 |
if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
| 1211 |
ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
| 1212 |
goto err; |
| 1213 |
} |
| 1214 |
|
| 1215 |
/* And finally set the version */ |
| 1216 |
cms_env_set_version(env); |
| 1217 |
|
| 1218 |
ok = 1; |
| 1219 |
|
| 1220 |
err: |
| 1221 |
cms_env_clear_ec(ec); |
| 1222 |
if (ok) |
| 1223 |
return ret; |
| 1224 |
BIO_free(ret); |
| 1225 |
return NULL; |
| 1226 |
} |
| 1227 |
|
| 1228 |
BIO *ossl_cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) |
| 1229 |
{ |
| 1230 |
if (cms->d.envelopedData->encryptedContentInfo->cipher != NULL) { |
| 1231 |
/* If cipher is set it's encryption */ |
| 1232 |
return cms_EnvelopedData_Encryption_init_bio(cms); |
| 1233 |
} |
| 1234 |
|
| 1235 |
/* If cipher is not set it's decryption */ |
| 1236 |
return cms_EnvelopedData_Decryption_init_bio(cms); |
| 1237 |
} |
| 1238 |
|
| 1239 |
BIO *ossl_cms_AuthEnvelopedData_init_bio(CMS_ContentInfo *cms) |
| 1240 |
{ |
| 1241 |
CMS_EncryptedContentInfo *ec; |
| 1242 |
STACK_OF(CMS_RecipientInfo) *rinfos; |
| 1243 |
int ok = 0; |
| 1244 |
BIO *ret; |
| 1245 |
CMS_AuthEnvelopedData *aenv = cms->d.authEnvelopedData; |
| 1246 |
|
| 1247 |
/* Get BIO first to set up key */ |
| 1248 |
ec = aenv->authEncryptedContentInfo; |
| 1249 |
/* Set tag for decryption */ |
| 1250 |
if (ec->cipher == NULL) { |
| 1251 |
ec->tag = aenv->mac->data; |
| 1252 |
ec->taglen = aenv->mac->length; |
| 1253 |
} |
| 1254 |
ret = ossl_cms_EncryptedContent_init_bio(ec, ossl_cms_get0_cmsctx(cms), 1); |
| 1255 |
|
| 1256 |
/* If error or no cipher end of processing */ |
| 1257 |
if (ret == NULL || ec->cipher == NULL) |
| 1258 |
return ret; |
| 1259 |
|
| 1260 |
/* Now encrypt content key according to each RecipientInfo type */ |
| 1261 |
rinfos = aenv->recipientInfos; |
| 1262 |
if (cms_env_encrypt_content_key(cms, rinfos) < 0) { |
| 1263 |
ERR_raise(ERR_LIB_CMS, CMS_R_ERROR_SETTING_RECIPIENTINFO); |
| 1264 |
goto err; |
| 1265 |
} |
| 1266 |
|
| 1267 |
/* And finally set the version */ |
| 1268 |
aenv->version = 0; |
| 1269 |
|
| 1270 |
ok = 1; |
| 1271 |
|
| 1272 |
err: |
| 1273 |
cms_env_clear_ec(ec); |
| 1274 |
if (ok) |
| 1275 |
return ret; |
| 1276 |
BIO_free(ret); |
| 1277 |
return NULL; |
| 1278 |
} |
| 1279 |
|
| 1280 |
int ossl_cms_EnvelopedData_final(CMS_ContentInfo *cms, BIO *chain) |
| 1281 |
{ |
| 1282 |
CMS_EnvelopedData *env = NULL; |
| 1283 |
EVP_CIPHER_CTX *ctx = NULL; |
| 1284 |
BIO *mbio = BIO_find_type(chain, BIO_TYPE_CIPHER); |
| 1285 |
|
| 1286 |
env = ossl_cms_get0_enveloped(cms); |
| 1287 |
if (env == NULL) |
| 1288 |
return 0; |
| 1289 |
|
| 1290 |
if (mbio == NULL) { |
| 1291 |
ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_NOT_FOUND); |
| 1292 |
return 0; |
| 1293 |
} |
| 1294 |
|
| 1295 |
BIO_get_cipher_ctx(mbio, &ctx); |
| 1296 |
|
| 1297 |
/* |
| 1298 |
* If the selected cipher supports unprotected attributes, |
| 1299 |
* deal with it using special ctrl function |
| 1300 |
*/ |
| 1301 |
if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) |
| 1302 |
& EVP_CIPH_FLAG_CIPHER_WITH_MAC) |
| 1303 |
!= 0) { |
| 1304 |
if (env->unprotectedAttrs == NULL) |
| 1305 |
env->unprotectedAttrs = sk_X509_ATTRIBUTE_new_null(); |
| 1306 |
|
| 1307 |
if (env->unprotectedAttrs == NULL) { |
| 1308 |
ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PROCESS_UNPROTECTED, |
| 1313 |
1, env->unprotectedAttrs) |
| 1314 |
<= 0) { |
| 1315 |
ERR_raise(ERR_LIB_CMS, CMS_R_CTRL_FAILURE); |
| 1316 |
return 0; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
cms_env_set_version(cms->d.envelopedData); |
| 1321 |
return 1; |
| 1322 |
} |
| 1323 |
|
| 1324 |
int ossl_cms_AuthEnvelopedData_final(CMS_ContentInfo *cms, BIO *cmsbio) |
| 1325 |
{ |
| 1326 |
EVP_CIPHER_CTX *ctx; |
| 1327 |
unsigned char *tag = NULL; |
| 1328 |
int taglen, ok = 0; |
| 1329 |
|
| 1330 |
BIO_get_cipher_ctx(cmsbio, &ctx); |
| 1331 |
|
| 1332 |
/* |
| 1333 |
* The tag is set only for encryption. There is nothing to do for |
| 1334 |
* decryption. |
| 1335 |
*/ |
| 1336 |
if (!EVP_CIPHER_CTX_is_encrypting(ctx)) |
| 1337 |
return 1; |
| 1338 |
|
| 1339 |
taglen = EVP_CIPHER_CTX_get_tag_length(ctx); |
| 1340 |
if (taglen <= 0 |
| 1341 |
|| (tag = OPENSSL_malloc(taglen)) == NULL |
| 1342 |
|| EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, |
| 1343 |
tag) |
| 1344 |
<= 0) { |
| 1345 |
ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_GET_TAG); |
| 1346 |
goto err; |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (!ASN1_OCTET_STRING_set(cms->d.authEnvelopedData->mac, tag, taglen)) |
| 1350 |
goto err; |
| 1351 |
|
| 1352 |
ok = 1; |
| 1353 |
err: |
| 1354 |
OPENSSL_free(tag); |
| 1355 |
return ok; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* |
| 1359 |
* Get RecipientInfo type (if any) supported by a key (public or private). To |
| 1360 |
* retain compatibility with previous behaviour if the ctrl value isn't |
| 1361 |
* supported we assume key transport. |
| 1362 |
*/ |
| 1363 |
int ossl_cms_pkey_get_ri_type(EVP_PKEY *pk) |
| 1364 |
{ |
| 1365 |
int ri_type; |
| 1366 |
EVP_PKEY_CTX *ctx = NULL; |
| 1367 |
|
| 1368 |
/* |
| 1369 |
* First check the provider for RecipientInfo support since a key may support |
| 1370 |
* multiple types, e.g. an RSA key and provider may support RSA key transport |
| 1371 |
* and/or RSA-KEM. |
| 1372 |
*/ |
| 1373 |
if (evp_pkey_is_provided(pk) |
| 1374 |
&& EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_CMS_RI_TYPE, &ri_type)) |
| 1375 |
return ri_type; |
| 1376 |
|
| 1377 |
/* Check types that we know about */ |
| 1378 |
if (EVP_PKEY_is_a(pk, "DH")) |
| 1379 |
return CMS_RECIPINFO_AGREE; |
| 1380 |
else if (EVP_PKEY_is_a(pk, "DHX")) |
| 1381 |
return CMS_RECIPINFO_AGREE; |
| 1382 |
else if (EVP_PKEY_is_a(pk, "DSA")) |
| 1383 |
return CMS_RECIPINFO_NONE; |
| 1384 |
else if (EVP_PKEY_is_a(pk, "EC")) |
| 1385 |
return CMS_RECIPINFO_AGREE; |
| 1386 |
else if (EVP_PKEY_is_a(pk, "RSA")) |
| 1387 |
return CMS_RECIPINFO_TRANS; |
| 1388 |
|
| 1389 |
/* |
| 1390 |
* Otherwise this might be an engine implementation, so see if we can get |
| 1391 |
* the type from the ameth. |
| 1392 |
*/ |
| 1393 |
if (pk->ameth && pk->ameth->pkey_ctrl) { |
| 1394 |
int i, r; |
| 1395 |
i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r); |
| 1396 |
if (i > 0) |
| 1397 |
return r; |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* |
| 1401 |
* Otherwise try very hard to figure out what RecipientInfo the key supports. |
| 1402 |
*/ |
| 1403 |
ri_type = CMS_RECIPINFO_TRANS; |
| 1404 |
ctx = EVP_PKEY_CTX_new(pk, NULL); |
| 1405 |
if (ctx != NULL) { |
| 1406 |
ERR_set_mark(); |
| 1407 |
if (EVP_PKEY_encrypt_init(ctx) > 0) |
| 1408 |
ri_type = CMS_RECIPINFO_TRANS; |
| 1409 |
else if (EVP_PKEY_derive_init(ctx) > 0) |
| 1410 |
ri_type = CMS_RECIPINFO_AGREE; |
| 1411 |
else if (EVP_PKEY_encapsulate_init(ctx, NULL) > 0) |
| 1412 |
ri_type = CMS_RECIPINFO_KEM; |
| 1413 |
ERR_pop_to_mark(); |
| 1414 |
} |
| 1415 |
EVP_PKEY_CTX_free(ctx); |
| 1416 |
|
| 1417 |
return ri_type; |
| 1418 |
} |
| 1419 |
|
| 1420 |
int ossl_cms_pkey_is_ri_type_supported(EVP_PKEY *pk, int ri_type) |
| 1421 |
{ |
| 1422 |
int supportedRiType; |
| 1423 |
|
| 1424 |
if (pk->ameth != NULL && pk->ameth->pkey_ctrl != NULL) { |
| 1425 |
int i, r; |
| 1426 |
|
| 1427 |
i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_IS_RI_TYPE_SUPPORTED, |
| 1428 |
ri_type, &r); |
| 1429 |
if (i > 0) |
| 1430 |
return r; |
| 1431 |
} |
| 1432 |
|
| 1433 |
supportedRiType = ossl_cms_pkey_get_ri_type(pk); |
| 1434 |
if (supportedRiType < 0) |
| 1435 |
return 0; |
| 1436 |
|
| 1437 |
return (supportedRiType == ri_type); |
| 1438 |
} |
| 1439 |
|
| 1440 |
int ossl_cms_RecipientInfo_wrap_init(CMS_RecipientInfo *ri, |
| 1441 |
const EVP_CIPHER *cipher) |
| 1442 |
{ |
| 1443 |
const CMS_CTX *cms_ctx; |
| 1444 |
EVP_CIPHER_CTX *ctx; |
| 1445 |
const EVP_CIPHER *kekcipher; |
| 1446 |
EVP_CIPHER *fetched_kekcipher; |
| 1447 |
const char *kekcipher_name; |
| 1448 |
int keylen; |
| 1449 |
int ret; |
| 1450 |
|
| 1451 |
if (ri->type == CMS_RECIPINFO_AGREE) { |
| 1452 |
cms_ctx = ri->d.kari->cms_ctx; |
| 1453 |
ctx = ri->d.kari->ctx; |
| 1454 |
} else if (ri->type == CMS_RECIPINFO_KEM) { |
| 1455 |
cms_ctx = ri->d.ori->d.kemri->cms_ctx; |
| 1456 |
ctx = ri->d.ori->d.kemri->ctx; |
| 1457 |
} else { |
| 1458 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); |
| 1459 |
return 0; |
| 1460 |
} |
| 1461 |
|
| 1462 |
/* If a suitable wrap algorithm is already set nothing to do */ |
| 1463 |
kekcipher = EVP_CIPHER_CTX_get0_cipher(ctx); |
| 1464 |
if (kekcipher != NULL) { |
| 1465 |
if (EVP_CIPHER_CTX_get_mode(ctx) != EVP_CIPH_WRAP_MODE) |
| 1466 |
return 0; |
| 1467 |
return 1; |
| 1468 |
} |
| 1469 |
if (cipher == NULL) |
| 1470 |
return 0; |
| 1471 |
keylen = EVP_CIPHER_get_key_length(cipher); |
| 1472 |
if (keylen <= 0) { |
| 1473 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_LENGTH); |
| 1474 |
return 0; |
| 1475 |
} |
| 1476 |
if ((EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_GET_WRAP_CIPHER) != 0) { |
| 1477 |
ret = EVP_CIPHER_meth_get_ctrl(cipher)(NULL, EVP_CTRL_GET_WRAP_CIPHER, |
| 1478 |
0, &kekcipher); |
| 1479 |
if (ret <= 0) |
| 1480 |
return 0; |
| 1481 |
|
| 1482 |
if (kekcipher != NULL) { |
| 1483 |
if (EVP_CIPHER_get_mode(kekcipher) != EVP_CIPH_WRAP_MODE) |
| 1484 |
return 0; |
| 1485 |
kekcipher_name = EVP_CIPHER_get0_name(kekcipher); |
| 1486 |
goto enc; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* |
| 1491 |
* Pick a cipher based on content encryption cipher. If it is DES3 use |
| 1492 |
* DES3 wrap otherwise use AES wrap similar to key size. |
| 1493 |
*/ |
| 1494 |
#ifndef OPENSSL_NO_DES |
| 1495 |
if (EVP_CIPHER_get_type(cipher) == NID_des_ede3_cbc) |
| 1496 |
kekcipher_name = SN_id_smime_alg_CMS3DESwrap; |
| 1497 |
else |
| 1498 |
#endif |
| 1499 |
if (keylen <= 16) |
| 1500 |
kekcipher_name = SN_id_aes128_wrap; |
| 1501 |
else if (keylen <= 24) |
| 1502 |
kekcipher_name = SN_id_aes192_wrap; |
| 1503 |
else |
| 1504 |
kekcipher_name = SN_id_aes256_wrap; |
| 1505 |
enc: |
| 1506 |
fetched_kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), |
| 1507 |
kekcipher_name, |
| 1508 |
ossl_cms_ctx_get0_propq(cms_ctx)); |
| 1509 |
if (fetched_kekcipher == NULL) |
| 1510 |
return 0; |
| 1511 |
ret = EVP_EncryptInit_ex(ctx, fetched_kekcipher, NULL, NULL, NULL); |
| 1512 |
EVP_CIPHER_free(fetched_kekcipher); |
| 1513 |
return ret; |
| 1514 |
} |