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