Coverage Report

Created: 2026-11-16 07:12

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