Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/evp/p5_crpt.c

raw
1 /*
2 * Copyright 1999-2025 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 <stdio.h>
11 #include <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include <openssl/core_names.h>
16 #include <openssl/kdf.h>
17
18 /*
19 * Doesn't do anything now: Builtin PBE algorithms in static table.
20 */
21
22 void PKCS5_PBE_add(void)
23 {
24 }
25
26 int PKCS5_PBE_keyivgen_ex(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
27 ASN1_TYPE *param, const EVP_CIPHER *cipher,
28 const EVP_MD *md, int en_de, OSSL_LIB_CTX *libctx,
29 const char *propq)
30 {
31 unsigned char md_tmp[EVP_MAX_MD_SIZE];
32 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
33 int ivl, kl;
34 PBEPARAM *pbe = NULL;
35 int saltlen, iter;
36 unsigned char *salt;
37 int mdsize;
38 int rv = 0;
39 EVP_KDF *kdf;
40 EVP_KDF_CTX *kctx = NULL;
41 OSSL_PARAM params[5], *p = params;
42 const char *mdname = EVP_MD_name(md);
43
44 /* Extract useful info from parameter */
45 if (param == NULL || param->type != V_ASN1_SEQUENCE || param->value.sequence == NULL) {
46 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
47 return 0;
48 }
49
50 pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
51 if (pbe == NULL) {
52 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
53 return 0;
54 }
55
56 ivl = EVP_CIPHER_get_iv_length(cipher);
57 if (ivl < 0 || ivl > 16) {
58 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
59 goto err;
60 }
61 kl = EVP_CIPHER_get_key_length(cipher);
62 if (kl < 0 || kl > (int)sizeof(md_tmp)) {
63 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
64 goto err;
65 }
66
67 if (pbe->iter == NULL)
68 iter = 1;
69 else
70 iter = ASN1_INTEGER_get(pbe->iter);
71 salt = pbe->salt->data;
72 saltlen = pbe->salt->length;
73
74 if (pass == NULL)
75 passlen = 0;
76 else if (passlen == -1)
77 passlen = (int)strlen(pass);
78
79 mdsize = EVP_MD_get_size(md);
80 if (mdsize <= 0)
81 goto err;
82
83 kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF1, propq);
84 kctx = EVP_KDF_CTX_new(kdf);
85 EVP_KDF_free(kdf);
86 if (kctx == NULL)
87 goto err;
88 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
89 (char *)pass, (size_t)passlen);
90 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
91 salt, saltlen);
92 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
93 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
94 (char *)mdname, 0);
95 *p = OSSL_PARAM_construct_end();
96 if (EVP_KDF_derive(kctx, md_tmp, mdsize, params) != 1)
97 goto err;
98 memcpy(key, md_tmp, kl);
99 memcpy(iv, md_tmp + (16 - ivl), ivl);
100 if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
101 goto err;
102 OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
103 OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
104 OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
105 rv = 1;
106 err:
107 EVP_KDF_CTX_free(kctx);
108 PBEPARAM_free(pbe);
109 return rv;
110 }
111
112 int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
113 ASN1_TYPE *param, const EVP_CIPHER *cipher,
114 const EVP_MD *md, int en_de)
115 {
116 return PKCS5_PBE_keyivgen_ex(cctx, pass, passlen, param, cipher, md, en_de,
117 NULL, NULL);
118 }