| 1 |
/* |
| 2 |
* Copyright 2009-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 |
#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/rand.h> |
| 17 |
#include <openssl/aes.h> |
| 18 |
#include "internal/sizes.h" |
| 19 |
#include "crypto/asn1.h" |
| 20 |
#include "cms_local.h" |
| 21 |
|
| 22 |
int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri, |
| 23 |
unsigned char *pass, ossl_ssize_t passlen) |
| 24 |
{ |
| 25 |
CMS_PasswordRecipientInfo *pwri; |
| 26 |
if (ri->type != CMS_RECIPINFO_PASS) { |
| 27 |
ERR_raise(ERR_LIB_CMS, CMS_R_NOT_PWRI); |
| 28 |
return 0; |
| 29 |
} |
| 30 |
|
| 31 |
pwri = ri->d.pwri; |
| 32 |
pwri->pass = pass; |
| 33 |
if (pass && passlen < 0) |
| 34 |
passlen = strlen((char *)pass); |
| 35 |
pwri->passlen = passlen; |
| 36 |
return 1; |
| 37 |
} |
| 38 |
|
| 39 |
CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms, |
| 40 |
int iter, int wrap_nid, |
| 41 |
int pbe_nid, |
| 42 |
unsigned char *pass, |
| 43 |
ossl_ssize_t passlen, |
| 44 |
const EVP_CIPHER *kekciph) |
| 45 |
{ |
| 46 |
STACK_OF(CMS_RecipientInfo) *ris; |
| 47 |
CMS_RecipientInfo *ri = NULL; |
| 48 |
CMS_EncryptedContentInfo *ec; |
| 49 |
CMS_PasswordRecipientInfo *pwri; |
| 50 |
EVP_CIPHER_CTX *ctx = NULL; |
| 51 |
X509_ALGOR *encalg = NULL; |
| 52 |
unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 53 |
int ivlen; |
| 54 |
const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
| 55 |
|
| 56 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 57 |
if (ec == NULL) |
| 58 |
return NULL; |
| 59 |
ris = CMS_get0_RecipientInfos(cms); |
| 60 |
if (ris == NULL) |
| 61 |
return NULL; |
| 62 |
|
| 63 |
if (wrap_nid <= 0) |
| 64 |
wrap_nid = NID_id_alg_PWRI_KEK; |
| 65 |
|
| 66 |
/* Get from enveloped data */ |
| 67 |
if (kekciph == NULL) |
| 68 |
kekciph = ec->cipher; |
| 69 |
|
| 70 |
if (kekciph == NULL) { |
| 71 |
ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
if ((EVP_CIPHER_get_flags(kekciph) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) { |
| 75 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEK_ALGORITHM); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
if (wrap_nid != NID_id_alg_PWRI_KEK) { |
| 79 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
/* Setup algorithm identifier for cipher */ |
| 84 |
encalg = X509_ALGOR_new(); |
| 85 |
if (encalg == NULL) { |
| 86 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 87 |
goto err; |
| 88 |
} |
| 89 |
ctx = EVP_CIPHER_CTX_new(); |
| 90 |
if (ctx == NULL) { |
| 91 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 92 |
goto err; |
| 93 |
} |
| 94 |
|
| 95 |
if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) { |
| 96 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 97 |
goto err; |
| 98 |
} |
| 99 |
|
| 100 |
ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); |
| 101 |
if (ivlen < 0) { |
| 102 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 103 |
goto err; |
| 104 |
} |
| 105 |
|
| 106 |
if (ivlen > 0) { |
| 107 |
if (RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), iv, ivlen, 0) <= 0) |
| 108 |
goto err; |
| 109 |
if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) { |
| 110 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 111 |
goto err; |
| 112 |
} |
| 113 |
encalg->parameter = ASN1_TYPE_new(); |
| 114 |
if (!encalg->parameter) { |
| 115 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 116 |
goto err; |
| 117 |
} |
| 118 |
if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) { |
| 119 |
ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); |
| 120 |
goto err; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_get_type(ctx)); |
| 125 |
|
| 126 |
EVP_CIPHER_CTX_free(ctx); |
| 127 |
ctx = NULL; |
| 128 |
|
| 129 |
/* Initialize recipient info */ |
| 130 |
ri = M_ASN1_new_of(CMS_RecipientInfo); |
| 131 |
if (ri == NULL) { |
| 132 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 133 |
goto err; |
| 134 |
} |
| 135 |
|
| 136 |
ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo); |
| 137 |
if (ri->d.pwri == NULL) { |
| 138 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 139 |
goto err; |
| 140 |
} |
| 141 |
ri->encoded_type = ri->type = CMS_RECIPINFO_PASS; |
| 142 |
|
| 143 |
pwri = ri->d.pwri; |
| 144 |
pwri->cms_ctx = cms_ctx; |
| 145 |
/* Since this is overwritten, free up empty structure already there */ |
| 146 |
X509_ALGOR_free(pwri->keyEncryptionAlgorithm); |
| 147 |
pwri->keyEncryptionAlgorithm = X509_ALGOR_new(); |
| 148 |
if (pwri->keyEncryptionAlgorithm == NULL) { |
| 149 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 150 |
goto err; |
| 151 |
} |
| 152 |
pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid); |
| 153 |
pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new(); |
| 154 |
if (pwri->keyEncryptionAlgorithm->parameter == NULL) { |
| 155 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 156 |
goto err; |
| 157 |
} |
| 158 |
|
| 159 |
if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR), |
| 160 |
&pwri->keyEncryptionAlgorithm->parameter->value.sequence)) { |
| 161 |
ERR_raise(ERR_LIB_CMS, ERR_R_ASN1_LIB); |
| 162 |
goto err; |
| 163 |
} |
| 164 |
pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE; |
| 165 |
|
| 166 |
X509_ALGOR_free(encalg); |
| 167 |
encalg = NULL; |
| 168 |
|
| 169 |
/* Setup PBE algorithm */ |
| 170 |
|
| 171 |
pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set_ex(iter, NULL, 0, -1, -1, |
| 172 |
cms_ctx->libctx); |
| 173 |
|
| 174 |
if (pwri->keyDerivationAlgorithm == NULL) |
| 175 |
goto err; |
| 176 |
|
| 177 |
CMS_RecipientInfo_set0_password(ri, pass, passlen); |
| 178 |
pwri->version = 0; |
| 179 |
|
| 180 |
if (!sk_CMS_RecipientInfo_push(ris, ri)) { |
| 181 |
ERR_raise(ERR_LIB_CMS, ERR_R_CRYPTO_LIB); |
| 182 |
goto err; |
| 183 |
} |
| 184 |
|
| 185 |
return ri; |
| 186 |
|
| 187 |
err: |
| 188 |
EVP_CIPHER_CTX_free(ctx); |
| 189 |
if (ri) |
| 190 |
M_ASN1_free_of(ri, CMS_RecipientInfo); |
| 191 |
X509_ALGOR_free(encalg); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* This is an implementation of the key wrapping mechanism in RFC3211, at |
| 197 |
* some point this should go into EVP. |
| 198 |
*/ |
| 199 |
|
| 200 |
static int kek_unwrap_key(unsigned char *out, size_t *outlen, |
| 201 |
const unsigned char *in, size_t inlen, |
| 202 |
EVP_CIPHER_CTX *ctx) |
| 203 |
{ |
| 204 |
int blocklen = EVP_CIPHER_CTX_get_block_size(ctx); |
| 205 |
unsigned char *tmp; |
| 206 |
int outl, rv = 0; |
| 207 |
|
| 208 |
if (blocklen < 4) |
| 209 |
return 0; |
| 210 |
|
| 211 |
if (inlen < 2 * (size_t)blocklen) { |
| 212 |
/* too small */ |
| 213 |
return 0; |
| 214 |
} |
| 215 |
if (inlen > INT_MAX || inlen % blocklen) { |
| 216 |
/* Invalid size */ |
| 217 |
return 0; |
| 218 |
} |
| 219 |
if ((tmp = OPENSSL_malloc(inlen)) == NULL) |
| 220 |
return 0; |
| 221 |
/* setup IV by decrypting last two blocks */ |
| 222 |
if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl, |
| 223 |
in + inlen - 2 * blocklen, blocklen * 2) |
| 224 |
/* |
| 225 |
* Do a decrypt of last decrypted block to set IV to correct value |
| 226 |
* output it to start of buffer so we don't corrupt decrypted block |
| 227 |
* this works because buffer is at least two block lengths long. |
| 228 |
*/ |
| 229 |
|| !EVP_DecryptUpdate(ctx, tmp, &outl, |
| 230 |
tmp + inlen - blocklen, blocklen) |
| 231 |
/* Can now decrypt first n - 1 blocks */ |
| 232 |
|| !EVP_DecryptUpdate(ctx, tmp, &outl, in, (int)(inlen - blocklen)) |
| 233 |
|
| 234 |
/* Reset IV to original value */ |
| 235 |
|| !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL) |
| 236 |
/* Decrypt again */ |
| 237 |
|| !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, (int)inlen)) |
| 238 |
goto err; |
| 239 |
/* Check check bytes */ |
| 240 |
if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) { |
| 241 |
/* Check byte failure */ |
| 242 |
goto err; |
| 243 |
} |
| 244 |
if (inlen < 4 + (size_t)tmp[0]) { |
| 245 |
/* Invalid length value */ |
| 246 |
goto err; |
| 247 |
} |
| 248 |
*outlen = (size_t)tmp[0]; |
| 249 |
memcpy(out, tmp + 4, *outlen); |
| 250 |
rv = 1; |
| 251 |
err: |
| 252 |
OPENSSL_clear_free(tmp, inlen); |
| 253 |
return rv; |
| 254 |
} |
| 255 |
|
| 256 |
static int kek_wrap_key(unsigned char *out, size_t *outlen, |
| 257 |
const unsigned char *in, size_t inlen, |
| 258 |
EVP_CIPHER_CTX *ctx, const CMS_CTX *cms_ctx) |
| 259 |
{ |
| 260 |
size_t blocklen = EVP_CIPHER_CTX_get_block_size(ctx); |
| 261 |
size_t olen; |
| 262 |
int dummy; |
| 263 |
|
| 264 |
if (blocklen == 0) |
| 265 |
return 0; |
| 266 |
|
| 267 |
/* |
| 268 |
* First decide length of output buffer: need header and round up to |
| 269 |
* multiple of block length. |
| 270 |
*/ |
| 271 |
olen = (inlen + 4 + blocklen - 1) / blocklen; |
| 272 |
olen *= blocklen; |
| 273 |
if (olen < 2 * blocklen) { |
| 274 |
/* Key too small */ |
| 275 |
return 0; |
| 276 |
} |
| 277 |
if (inlen > 0xFF) { |
| 278 |
/* Key too large */ |
| 279 |
return 0; |
| 280 |
} |
| 281 |
if (out) { |
| 282 |
/* Set header */ |
| 283 |
out[0] = (unsigned char)inlen; |
| 284 |
out[1] = in[0] ^ 0xFF; |
| 285 |
out[2] = in[1] ^ 0xFF; |
| 286 |
out[3] = in[2] ^ 0xFF; |
| 287 |
memcpy(out + 4, in, inlen); |
| 288 |
/* Add random padding to end */ |
| 289 |
if (olen > inlen + 4 |
| 290 |
&& RAND_bytes_ex(ossl_cms_ctx_get0_libctx(cms_ctx), out + 4 + inlen, |
| 291 |
olen - 4 - inlen, 0) |
| 292 |
<= 0) |
| 293 |
return 0; |
| 294 |
/* Encrypt twice */ |
| 295 |
if (!EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen) |
| 296 |
|| !EVP_EncryptUpdate(ctx, out, &dummy, out, (int)olen)) |
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
*outlen = olen; |
| 301 |
|
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
/* Encrypt/Decrypt content key in PWRI recipient info */ |
| 306 |
|
| 307 |
int ossl_cms_RecipientInfo_pwri_crypt(const CMS_ContentInfo *cms, |
| 308 |
CMS_RecipientInfo *ri, int en_de) |
| 309 |
{ |
| 310 |
CMS_EncryptedContentInfo *ec; |
| 311 |
CMS_PasswordRecipientInfo *pwri; |
| 312 |
int r = 0; |
| 313 |
X509_ALGOR *algtmp, *kekalg = NULL; |
| 314 |
EVP_CIPHER_CTX *kekctx = NULL; |
| 315 |
char name[OSSL_MAX_NAME_SIZE]; |
| 316 |
EVP_CIPHER *kekcipher; |
| 317 |
unsigned char *key = NULL; |
| 318 |
size_t keylen; |
| 319 |
const CMS_CTX *cms_ctx = ossl_cms_get0_cmsctx(cms); |
| 320 |
|
| 321 |
ec = ossl_cms_get0_env_enc_content(cms); |
| 322 |
|
| 323 |
pwri = ri->d.pwri; |
| 324 |
|
| 325 |
if (pwri->pass == NULL) { |
| 326 |
ERR_raise(ERR_LIB_CMS, CMS_R_NO_PASSWORD); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
algtmp = pwri->keyEncryptionAlgorithm; |
| 330 |
|
| 331 |
if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) { |
| 332 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); |
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR), |
| 337 |
algtmp->parameter); |
| 338 |
|
| 339 |
if (kekalg == NULL) { |
| 340 |
ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
OBJ_obj2txt(name, sizeof(name), kekalg->algorithm, 0); |
| 345 |
kekcipher = EVP_CIPHER_fetch(ossl_cms_ctx_get0_libctx(cms_ctx), name, |
| 346 |
ossl_cms_ctx_get0_propq(cms_ctx)); |
| 347 |
|
| 348 |
if (kekcipher == NULL) { |
| 349 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNKNOWN_CIPHER); |
| 350 |
goto err; |
| 351 |
} |
| 352 |
|
| 353 |
kekctx = EVP_CIPHER_CTX_new(); |
| 354 |
if (kekctx == NULL) { |
| 355 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 356 |
goto err; |
| 357 |
} |
| 358 |
/* Fixup cipher based on AlgorithmIdentifier to set IV etc */ |
| 359 |
if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de)) |
| 360 |
goto err; |
| 361 |
EVP_CIPHER_CTX_set_padding(kekctx, 0); |
| 362 |
if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) { |
| 363 |
ERR_raise(ERR_LIB_CMS, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); |
| 364 |
goto err; |
| 365 |
} |
| 366 |
|
| 367 |
algtmp = pwri->keyDerivationAlgorithm; |
| 368 |
|
| 369 |
/* Finish password based key derivation to setup key in "ctx" */ |
| 370 |
|
| 371 |
if (algtmp == NULL) { |
| 372 |
ERR_raise_data(ERR_LIB_CMS, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER, |
| 373 |
"Missing KeyDerivationAlgorithm"); |
| 374 |
goto err; |
| 375 |
} |
| 376 |
if (!EVP_PBE_CipherInit_ex(algtmp->algorithm, |
| 377 |
(char *)pwri->pass, (int)pwri->passlen, |
| 378 |
algtmp->parameter, kekctx, en_de, |
| 379 |
cms_ctx->libctx, cms_ctx->propq)) { |
| 380 |
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
| 381 |
goto err; |
| 382 |
} |
| 383 |
|
| 384 |
/* Finally wrap/unwrap the key */ |
| 385 |
|
| 386 |
if (en_de) { |
| 387 |
|
| 388 |
if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx, cms_ctx)) |
| 389 |
goto err; |
| 390 |
|
| 391 |
key = OPENSSL_malloc(keylen); |
| 392 |
|
| 393 |
if (key == NULL) |
| 394 |
goto err; |
| 395 |
|
| 396 |
if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx, cms_ctx)) |
| 397 |
goto err; |
| 398 |
pwri->encryptedKey->data = key; |
| 399 |
pwri->encryptedKey->length = (int)keylen; |
| 400 |
} else { |
| 401 |
key = OPENSSL_malloc(pwri->encryptedKey->length); |
| 402 |
if (key == NULL) |
| 403 |
goto err; |
| 404 |
if (!kek_unwrap_key(key, &keylen, |
| 405 |
pwri->encryptedKey->data, |
| 406 |
pwri->encryptedKey->length, kekctx)) { |
| 407 |
ERR_raise(ERR_LIB_CMS, CMS_R_UNWRAP_FAILURE); |
| 408 |
goto err; |
| 409 |
} |
| 410 |
|
| 411 |
OPENSSL_clear_free(ec->key, ec->keylen); |
| 412 |
ec->key = key; |
| 413 |
ec->keylen = keylen; |
| 414 |
} |
| 415 |
|
| 416 |
r = 1; |
| 417 |
|
| 418 |
err: |
| 419 |
EVP_CIPHER_free(kekcipher); |
| 420 |
EVP_CIPHER_CTX_free(kekctx); |
| 421 |
|
| 422 |
if (!r) |
| 423 |
OPENSSL_free(key); |
| 424 |
X509_ALGOR_free(kekalg); |
| 425 |
|
| 426 |
return r; |
| 427 |
} |