1 | /*
|
---|
2 | * Copyright 2018-2021 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/core_names.h>
|
---|
16 | #include <openssl/proverr.h>
|
---|
17 | #include "internal/cryptlib.h"
|
---|
18 | #include "internal/numbers.h"
|
---|
19 | #include "crypto/evp.h"
|
---|
20 | #include "prov/provider_ctx.h"
|
---|
21 | #include "prov/providercommon.h"
|
---|
22 | #include "prov/implementations.h"
|
---|
23 | #include "prov/provider_util.h"
|
---|
24 |
|
---|
25 | /* See RFC 4253, Section 7.2 */
|
---|
26 | static OSSL_FUNC_kdf_newctx_fn kdf_sshkdf_new;
|
---|
27 | static OSSL_FUNC_kdf_freectx_fn kdf_sshkdf_free;
|
---|
28 | static OSSL_FUNC_kdf_reset_fn kdf_sshkdf_reset;
|
---|
29 | static OSSL_FUNC_kdf_derive_fn kdf_sshkdf_derive;
|
---|
30 | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params;
|
---|
31 | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params;
|
---|
32 | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params;
|
---|
33 | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params;
|
---|
34 |
|
---|
35 | static int SSHKDF(const EVP_MD *evp_md,
|
---|
36 | const unsigned char *key, size_t key_len,
|
---|
37 | const unsigned char *xcghash, size_t xcghash_len,
|
---|
38 | const unsigned char *session_id, size_t session_id_len,
|
---|
39 | char type, unsigned char *okey, size_t okey_len);
|
---|
40 |
|
---|
41 | typedef struct {
|
---|
42 | void *provctx;
|
---|
43 | PROV_DIGEST digest;
|
---|
44 | unsigned char *key; /* K */
|
---|
45 | size_t key_len;
|
---|
46 | unsigned char *xcghash; /* H */
|
---|
47 | size_t xcghash_len;
|
---|
48 | char type; /* X */
|
---|
49 | unsigned char *session_id;
|
---|
50 | size_t session_id_len;
|
---|
51 | } KDF_SSHKDF;
|
---|
52 |
|
---|
53 | static void *kdf_sshkdf_new(void *provctx)
|
---|
54 | {
|
---|
55 | KDF_SSHKDF *ctx;
|
---|
56 |
|
---|
57 | if (!ossl_prov_is_running())
|
---|
58 | return NULL;
|
---|
59 |
|
---|
60 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
|
---|
61 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
---|
62 | ctx->provctx = provctx;
|
---|
63 | return ctx;
|
---|
64 | }
|
---|
65 |
|
---|
66 | static void kdf_sshkdf_free(void *vctx)
|
---|
67 | {
|
---|
68 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
---|
69 |
|
---|
70 | if (ctx != NULL) {
|
---|
71 | kdf_sshkdf_reset(ctx);
|
---|
72 | OPENSSL_free(ctx);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | static void kdf_sshkdf_reset(void *vctx)
|
---|
77 | {
|
---|
78 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
---|
79 | void *provctx = ctx->provctx;
|
---|
80 |
|
---|
81 | ossl_prov_digest_reset(&ctx->digest);
|
---|
82 | OPENSSL_clear_free(ctx->key, ctx->key_len);
|
---|
83 | OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len);
|
---|
84 | OPENSSL_clear_free(ctx->session_id, ctx->session_id_len);
|
---|
85 | memset(ctx, 0, sizeof(*ctx));
|
---|
86 | ctx->provctx = provctx;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
|
---|
90 | const OSSL_PARAM *p)
|
---|
91 | {
|
---|
92 | OPENSSL_clear_free(*dst, *dst_len);
|
---|
93 | *dst = NULL;
|
---|
94 | *dst_len = 0;
|
---|
95 | return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
|
---|
96 | }
|
---|
97 |
|
---|
98 | static int kdf_sshkdf_derive(void *vctx, unsigned char *key, size_t keylen,
|
---|
99 | const OSSL_PARAM params[])
|
---|
100 | {
|
---|
101 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
|
---|
102 | const EVP_MD *md;
|
---|
103 |
|
---|
104 | if (!ossl_prov_is_running() || !kdf_sshkdf_set_ctx_params(ctx, params))
|
---|
105 | return 0;
|
---|
106 |
|
---|
107 | md = ossl_prov_digest_md(&ctx->digest);
|
---|
108 | if (md == NULL) {
|
---|
109 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 | if (ctx->key == NULL) {
|
---|
113 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 | if (ctx->xcghash == NULL) {
|
---|
117 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH);
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 | if (ctx->session_id == NULL) {
|
---|
121 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID);
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 | if (ctx->type == 0) {
|
---|
125 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE);
|
---|
126 | return 0;
|
---|
127 | }
|
---|
128 | return SSHKDF(md, ctx->key, ctx->key_len,
|
---|
129 | ctx->xcghash, ctx->xcghash_len,
|
---|
130 | ctx->session_id, ctx->session_id_len,
|
---|
131 | ctx->type, key, keylen);
|
---|
132 | }
|
---|
133 |
|
---|
134 | static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
---|
135 | {
|
---|
136 | const OSSL_PARAM *p;
|
---|
137 | KDF_SSHKDF *ctx = vctx;
|
---|
138 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
|
---|
139 |
|
---|
140 | if (params == NULL)
|
---|
141 | return 1;
|
---|
142 |
|
---|
143 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
|
---|
144 | return 0;
|
---|
145 |
|
---|
146 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
|
---|
147 | if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p))
|
---|
148 | return 0;
|
---|
149 |
|
---|
150 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH))
|
---|
151 | != NULL)
|
---|
152 | if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p))
|
---|
153 | return 0;
|
---|
154 |
|
---|
155 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID))
|
---|
156 | != NULL)
|
---|
157 | if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p))
|
---|
158 | return 0;
|
---|
159 |
|
---|
160 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE))
|
---|
161 | != NULL) {
|
---|
162 | const char *kdftype;
|
---|
163 |
|
---|
164 | if (!OSSL_PARAM_get_utf8_string_ptr(p, &kdftype))
|
---|
165 | return 0;
|
---|
166 | /* Expect one character (byte in this case) */
|
---|
167 | if (kdftype == NULL || p->data_size != 1)
|
---|
168 | return 0;
|
---|
169 | if (kdftype[0] < 65 || kdftype[0] > 70) {
|
---|
170 | ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR);
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 | ctx->type = kdftype[0];
|
---|
174 | }
|
---|
175 | return 1;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(ossl_unused void *ctx,
|
---|
179 | ossl_unused void *p_ctx)
|
---|
180 | {
|
---|
181 | static const OSSL_PARAM known_settable_ctx_params[] = {
|
---|
182 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
|
---|
183 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
|
---|
184 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
|
---|
185 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0),
|
---|
186 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0),
|
---|
187 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0),
|
---|
188 | OSSL_PARAM_END
|
---|
189 | };
|
---|
190 | return known_settable_ctx_params;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
---|
194 | {
|
---|
195 | OSSL_PARAM *p;
|
---|
196 |
|
---|
197 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
|
---|
198 | return OSSL_PARAM_set_size_t(p, SIZE_MAX);
|
---|
199 | return -2;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(ossl_unused void *ctx,
|
---|
203 | ossl_unused void *p_ctx)
|
---|
204 | {
|
---|
205 | static const OSSL_PARAM known_gettable_ctx_params[] = {
|
---|
206 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
|
---|
207 | OSSL_PARAM_END
|
---|
208 | };
|
---|
209 | return known_gettable_ctx_params;
|
---|
210 | }
|
---|
211 |
|
---|
212 | const OSSL_DISPATCH ossl_kdf_sshkdf_functions[] = {
|
---|
213 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new },
|
---|
214 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free },
|
---|
215 | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset },
|
---|
216 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive },
|
---|
217 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
|
---|
218 | (void(*)(void))kdf_sshkdf_settable_ctx_params },
|
---|
219 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params },
|
---|
220 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
|
---|
221 | (void(*)(void))kdf_sshkdf_gettable_ctx_params },
|
---|
222 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params },
|
---|
223 | { 0, NULL }
|
---|
224 | };
|
---|
225 |
|
---|
226 | static int SSHKDF(const EVP_MD *evp_md,
|
---|
227 | const unsigned char *key, size_t key_len,
|
---|
228 | const unsigned char *xcghash, size_t xcghash_len,
|
---|
229 | const unsigned char *session_id, size_t session_id_len,
|
---|
230 | char type, unsigned char *okey, size_t okey_len)
|
---|
231 | {
|
---|
232 | EVP_MD_CTX *md = NULL;
|
---|
233 | unsigned char digest[EVP_MAX_MD_SIZE];
|
---|
234 | unsigned int dsize = 0;
|
---|
235 | size_t cursize = 0;
|
---|
236 | int ret = 0;
|
---|
237 |
|
---|
238 | md = EVP_MD_CTX_new();
|
---|
239 | if (md == NULL)
|
---|
240 | return 0;
|
---|
241 |
|
---|
242 | if (!EVP_DigestInit_ex(md, evp_md, NULL))
|
---|
243 | goto out;
|
---|
244 |
|
---|
245 | if (!EVP_DigestUpdate(md, key, key_len))
|
---|
246 | goto out;
|
---|
247 |
|
---|
248 | if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
|
---|
249 | goto out;
|
---|
250 |
|
---|
251 | if (!EVP_DigestUpdate(md, &type, 1))
|
---|
252 | goto out;
|
---|
253 |
|
---|
254 | if (!EVP_DigestUpdate(md, session_id, session_id_len))
|
---|
255 | goto out;
|
---|
256 |
|
---|
257 | if (!EVP_DigestFinal_ex(md, digest, &dsize))
|
---|
258 | goto out;
|
---|
259 |
|
---|
260 | if (okey_len < dsize) {
|
---|
261 | memcpy(okey, digest, okey_len);
|
---|
262 | ret = 1;
|
---|
263 | goto out;
|
---|
264 | }
|
---|
265 |
|
---|
266 | memcpy(okey, digest, dsize);
|
---|
267 |
|
---|
268 | for (cursize = dsize; cursize < okey_len; cursize += dsize) {
|
---|
269 |
|
---|
270 | if (!EVP_DigestInit_ex(md, evp_md, NULL))
|
---|
271 | goto out;
|
---|
272 |
|
---|
273 | if (!EVP_DigestUpdate(md, key, key_len))
|
---|
274 | goto out;
|
---|
275 |
|
---|
276 | if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
|
---|
277 | goto out;
|
---|
278 |
|
---|
279 | if (!EVP_DigestUpdate(md, okey, cursize))
|
---|
280 | goto out;
|
---|
281 |
|
---|
282 | if (!EVP_DigestFinal_ex(md, digest, &dsize))
|
---|
283 | goto out;
|
---|
284 |
|
---|
285 | if (okey_len < cursize + dsize) {
|
---|
286 | memcpy(okey + cursize, digest, okey_len - cursize);
|
---|
287 | ret = 1;
|
---|
288 | goto out;
|
---|
289 | }
|
---|
290 |
|
---|
291 | memcpy(okey + cursize, digest, dsize);
|
---|
292 | }
|
---|
293 |
|
---|
294 | ret = 1;
|
---|
295 |
|
---|
296 | out:
|
---|
297 | EVP_MD_CTX_free(md);
|
---|
298 | OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);
|
---|
299 | return ret;
|
---|
300 | }
|
---|
301 |
|
---|