/src/openssl/providers/implementations/kdfs/snmpkdf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 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 <stdlib.h> |
11 | | #include <stdarg.h> |
12 | | #include <string.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/kdf.h> |
15 | | #include <openssl/sha.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/proverr.h> |
18 | | #include "internal/cryptlib.h" |
19 | | #include "internal/numbers.h" |
20 | | #include "crypto/evp.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "prov/implementations.h" |
24 | | #include "prov/provider_util.h" |
25 | | #include "providers/implementations/kdfs/snmpkdf.inc" |
26 | | |
27 | 0 | #define KDF_SNMP_PASSWORD_HASH_AMOUNT (1024 * 1024) |
28 | 0 | #define KDF_SNMP_MIN_PASSWORD_LEN 8 |
29 | | |
30 | | /* See RFC 3414, Appendix A.2.2 */ |
31 | | /* See NIST SP800-135 Section 6.8 */ |
32 | | static OSSL_FUNC_kdf_newctx_fn kdf_snmpkdf_new; |
33 | | static OSSL_FUNC_kdf_dupctx_fn kdf_snmpkdf_dup; |
34 | | static OSSL_FUNC_kdf_freectx_fn kdf_snmpkdf_free; |
35 | | static OSSL_FUNC_kdf_reset_fn kdf_snmpkdf_reset; |
36 | | static OSSL_FUNC_kdf_derive_fn kdf_snmpkdf_derive; |
37 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_snmpkdf_settable_ctx_params; |
38 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_snmpkdf_set_ctx_params; |
39 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_snmpkdf_gettable_ctx_params; |
40 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_snmpkdf_get_ctx_params; |
41 | | |
42 | | static int SNMPKDF(const EVP_MD *evp_md, |
43 | | const unsigned char *eid, size_t eid_len, |
44 | | unsigned char *password, size_t password_len, |
45 | | unsigned char *key, size_t keylen); |
46 | | |
47 | | typedef struct { |
48 | | /* Warning: Any changes to this structure may require you to update kdf_snmpkdf_dup */ |
49 | | void *provctx; |
50 | | PROV_DIGEST digest; |
51 | | unsigned char *eid; |
52 | | size_t eid_len; |
53 | | unsigned char *password; |
54 | | size_t password_len; |
55 | | } KDF_SNMPKDF; |
56 | | |
57 | | static void *kdf_snmpkdf_new(void *provctx) |
58 | 0 | { |
59 | 0 | KDF_SNMPKDF *ctx; |
60 | |
|
61 | 0 | if (!ossl_prov_is_running()) |
62 | 0 | return NULL; |
63 | | |
64 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) |
65 | 0 | ctx->provctx = provctx; |
66 | 0 | return ctx; |
67 | 0 | } |
68 | | |
69 | | static void *kdf_snmpkdf_dup(void *vctx) |
70 | 0 | { |
71 | 0 | const KDF_SNMPKDF *src = (const KDF_SNMPKDF *)vctx; |
72 | 0 | KDF_SNMPKDF *dest; |
73 | |
|
74 | 0 | dest = kdf_snmpkdf_new(src->provctx); |
75 | 0 | if (dest != NULL) { |
76 | 0 | if (!ossl_prov_memdup(src->eid, src->eid_len, |
77 | 0 | &dest->eid, &dest->eid_len) |
78 | 0 | || !ossl_prov_memdup(src->password, src->password_len, |
79 | 0 | &dest->password, &dest->password_len) |
80 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
81 | 0 | goto err; |
82 | 0 | } |
83 | 0 | return dest; |
84 | | |
85 | 0 | err: |
86 | 0 | kdf_snmpkdf_free(dest); |
87 | 0 | return NULL; |
88 | 0 | } |
89 | | |
90 | | static void kdf_snmpkdf_free(void *vctx) |
91 | 0 | { |
92 | 0 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
93 | |
|
94 | 0 | if (ctx != NULL) { |
95 | 0 | kdf_snmpkdf_reset(ctx); |
96 | 0 | OPENSSL_free(ctx); |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | static void kdf_snmpkdf_reset(void *vctx) |
101 | 0 | { |
102 | 0 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
103 | 0 | void *provctx = ctx->provctx; |
104 | |
|
105 | 0 | ossl_prov_digest_reset(&ctx->digest); |
106 | 0 | OPENSSL_clear_free(ctx->eid, ctx->eid_len); |
107 | 0 | OPENSSL_clear_free(ctx->password, ctx->password_len); |
108 | 0 | memset(ctx, 0, sizeof(*ctx)); |
109 | 0 | ctx->provctx = provctx; |
110 | 0 | } |
111 | | |
112 | | static int snmpkdf_set_membuf(unsigned char **dst, size_t *dst_len, |
113 | | const OSSL_PARAM *p) |
114 | 0 | { |
115 | 0 | OPENSSL_clear_free(*dst, *dst_len); |
116 | 0 | *dst = NULL; |
117 | 0 | *dst_len = 0; |
118 | 0 | return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
119 | 0 | } |
120 | | |
121 | | static int kdf_snmpkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
122 | | const OSSL_PARAM params[]) |
123 | 0 | { |
124 | 0 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
125 | 0 | const EVP_MD *md; |
126 | |
|
127 | 0 | if (!ossl_prov_is_running() || !kdf_snmpkdf_set_ctx_params(ctx, params)) |
128 | 0 | return 0; |
129 | | |
130 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
131 | 0 | if (md == NULL) { |
132 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 | if (ctx->eid == NULL) { |
136 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_EID); |
137 | 0 | return 0; |
138 | 0 | } |
139 | 0 | if (ctx->password == NULL) { |
140 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
141 | 0 | return 0; |
142 | 0 | } |
143 | | |
144 | 0 | return SNMPKDF(md, ctx->eid, ctx->eid_len, |
145 | 0 | ctx->password, ctx->password_len, |
146 | 0 | key, keylen); |
147 | 0 | } |
148 | | |
149 | | static int kdf_snmpkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
150 | 0 | { |
151 | 0 | struct snmp_set_ctx_params_st p; |
152 | 0 | KDF_SNMPKDF *ctx = vctx; |
153 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
154 | | #ifdef FIPS_MODULE |
155 | | const EVP_MD *md = NULL; |
156 | | #endif |
157 | |
|
158 | 0 | if (params == NULL) |
159 | 0 | return 1; |
160 | | |
161 | 0 | if (ctx == NULL || !snmp_set_ctx_params_decoder(params, &p)) |
162 | 0 | return 0; |
163 | | |
164 | 0 | if (p.digest != NULL) { |
165 | 0 | if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, libctx)) |
166 | 0 | return 0; |
167 | | #ifdef FIPS_MODULE |
168 | | md = ossl_prov_digest_md(&ctx->digest); |
169 | | if (!EVP_MD_is_a(md, SN_sha1)) |
170 | | return 0; |
171 | | #endif |
172 | 0 | } |
173 | | |
174 | 0 | if (p.pw != NULL) { |
175 | 0 | if (!snmpkdf_set_membuf(&ctx->password, &ctx->password_len, p.pw)) |
176 | 0 | return 0; |
177 | 0 | if ((ctx->password_len > KDF_SNMP_PASSWORD_HASH_AMOUNT) || |
178 | 0 | (ctx->password_len < KDF_SNMP_MIN_PASSWORD_LEN)) |
179 | 0 | return 0; |
180 | 0 | } |
181 | | |
182 | 0 | if (p.eid != NULL && !snmpkdf_set_membuf(&ctx->eid, &ctx->eid_len, p.eid)) |
183 | 0 | return 0; |
184 | | |
185 | 0 | return 1; |
186 | 0 | } |
187 | | |
188 | | static const OSSL_PARAM *kdf_snmpkdf_settable_ctx_params(ossl_unused void *ctx, |
189 | | ossl_unused void *p_ctx) |
190 | 0 | { |
191 | 0 | return snmp_set_ctx_params_list; |
192 | 0 | } |
193 | | |
194 | | static size_t kdf_snmpkdf_size(KDF_SNMPKDF *ctx) |
195 | 0 | { |
196 | 0 | int len; |
197 | 0 | const EVP_MD *md = NULL; |
198 | |
|
199 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
200 | 0 | if (md == NULL) { |
201 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | len = EVP_MD_get_size(md); |
205 | 0 | return (len <= 0) ? 0 : (size_t)len; |
206 | 0 | } |
207 | | |
208 | | static int kdf_snmpkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
209 | 0 | { |
210 | 0 | struct snmp_get_ctx_params_st p; |
211 | 0 | KDF_SNMPKDF *ctx = vctx; |
212 | |
|
213 | 0 | if (ctx == NULL || !snmp_get_ctx_params_decoder(params, &p)) |
214 | 0 | return 0; |
215 | | |
216 | 0 | if (p.size != NULL) { |
217 | 0 | size_t sz = kdf_snmpkdf_size(ctx); |
218 | |
|
219 | 0 | if (sz == 0) |
220 | 0 | return 0; |
221 | 0 | if (!OSSL_PARAM_set_size_t(p.size, sz)) |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 | return 1; |
225 | 0 | } |
226 | | |
227 | | static const OSSL_PARAM *kdf_snmpkdf_gettable_ctx_params(ossl_unused void *ctx, |
228 | | ossl_unused void *p_ctx) |
229 | 0 | { |
230 | 0 | return snmp_get_ctx_params_list; |
231 | 0 | } |
232 | | |
233 | | const OSSL_DISPATCH ossl_kdf_snmpkdf_functions[] = { |
234 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_snmpkdf_new }, |
235 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_snmpkdf_dup }, |
236 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_snmpkdf_free }, |
237 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_snmpkdf_reset }, |
238 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_snmpkdf_derive }, |
239 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
240 | | (void(*)(void))kdf_snmpkdf_settable_ctx_params }, |
241 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_snmpkdf_set_ctx_params }, |
242 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
243 | | (void(*)(void))kdf_snmpkdf_gettable_ctx_params }, |
244 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_snmpkdf_get_ctx_params }, |
245 | | { 0, NULL } |
246 | | }; |
247 | | |
248 | | /* |
249 | | * SNMPKDF - In compliance with SP800-135 and RFC7860, calculate |
250 | | * a master key using the engine ID and password. |
251 | | * |
252 | | * Denote engineLength and passwordlen to be the lengths (in bytes) of an |
253 | | * snmpEngineID and a password, respectively. |
254 | | * |
255 | | * Let N = (1024*1024)/passwordlen |
256 | | * |
257 | | * Expanded_password = the leftmost 1048576 bytes of the string of N |
258 | | * repetitions of the password. |
259 | | * |
260 | | * Derived_password = SHA-1 (Expanded_password). The Derived_password |
261 | | * is the output of hashing the Expanded_password by SHA-1. |
262 | | * |
263 | | * Let Shared_key to be the key that the user shares with the authoritative |
264 | | * SNMP engine with ID snmpEngineID. The Shared_key is generated as follows: |
265 | | * |
266 | | * Shared_key = SHA-1(Derived_password || snmpEngineID || Derived_password). |
267 | | * |
268 | | * e_id - engine ID(eid) |
269 | | * e_len - engineID length |
270 | | * password - password |
271 | | * password_len - password length |
272 | | * okey - pointer to key output, FIPS testing limited to SHA-1. |
273 | | * okeylen - key output length |
274 | | * return - 1 pass 0 for error |
275 | | */ |
276 | | static int SNMPKDF(const EVP_MD *evp_md, |
277 | | const unsigned char *e_id, size_t e_len, |
278 | | unsigned char *password, size_t password_len, |
279 | | unsigned char *okey, size_t okeylen) |
280 | 0 | { |
281 | 0 | EVP_MD_CTX *md = NULL; |
282 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
283 | 0 | size_t mdsize = 0, len = 0; |
284 | 0 | unsigned int md_len = 0; |
285 | 0 | int ret = 0; |
286 | | |
287 | | /* Limited to SHA-1 and SHA-2 hashes presently */ |
288 | 0 | if (okey == NULL || okeylen == 0) |
289 | 0 | return 0; |
290 | | |
291 | 0 | md = EVP_MD_CTX_new(); |
292 | 0 | if (md == NULL) { |
293 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
294 | 0 | goto err; |
295 | 0 | } |
296 | | |
297 | 0 | mdsize = EVP_MD_get_size(evp_md); |
298 | 0 | if (mdsize <= 0 || mdsize < okeylen) |
299 | 0 | goto err; |
300 | | |
301 | 0 | if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
302 | 0 | goto err; |
303 | | |
304 | 0 | for (len = 0; len < KDF_SNMP_PASSWORD_HASH_AMOUNT - password_len; len += password_len) { |
305 | 0 | if (!EVP_DigestUpdate(md, password, password_len)) |
306 | 0 | goto err; |
307 | 0 | } |
308 | | |
309 | 0 | if (!EVP_DigestUpdate(md, password, KDF_SNMP_PASSWORD_HASH_AMOUNT - len) |
310 | 0 | || !EVP_DigestFinal_ex(md, digest, &md_len) |
311 | 0 | || !EVP_DigestInit_ex(md, evp_md, NULL) |
312 | 0 | || !EVP_DigestUpdate(md, digest, mdsize) |
313 | 0 | || !EVP_DigestUpdate(md, e_id, e_len) |
314 | 0 | || !EVP_DigestUpdate(md, digest, mdsize) |
315 | 0 | || !EVP_DigestFinal_ex(md, digest, &md_len)) |
316 | 0 | goto err; |
317 | | |
318 | 0 | memcpy(okey, digest, okeylen); |
319 | |
|
320 | 0 | ret = 1; |
321 | |
|
322 | 0 | err: |
323 | 0 | EVP_MD_CTX_free(md); |
324 | 0 | OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE); |
325 | 0 | return ret; |
326 | 0 | } |