| 1 | /* |
| 2 | * Copyright 2006-2016 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/objects.h> |
| 14 | #include <openssl/evp.h> |
| 15 | #include "crypto/bn.h" |
| 16 | #include "crypto/asn1.h" |
| 17 | #include "crypto/evp.h" |
| 18 | #include "evp_local.h" |
| 19 | |
| 20 | static int fromdata_init(EVP_PKEY_CTX *ctx, int operation) |
| 21 | { |
| 22 | if (ctx == NULL || ctx->algorithm == NULL) |
| 23 | goto not_supported; |
| 24 | |
| 25 | evp_pkey_ctx_free_old_ops(ctx); |
| 26 | ctx->operation = operation; |
| 27 | if (ctx->keymgmt == NULL) |
| 28 | ctx->keymgmt = EVP_KEYMGMT_fetch(NULL, ctx->algorithm, ctx->propquery); |
| 29 | if (ctx->keymgmt == NULL) |
| 30 | goto not_supported; |
| 31 | |
| 32 | return 1; |
| 33 | |
| 34 | not_supported: |
| 35 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 36 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 37 | return -2; |
| 38 | } |
| 39 | |
| 40 | int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx) |
| 41 | { |
| 42 | return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA); |
| 43 | } |
| 44 | |
| 45 | int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx) |
| 46 | { |
| 47 | return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA); |
| 48 | } |
| 49 | |
| 50 | int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[]) |
| 51 | { |
| 52 | void *provdata = NULL; |
| 53 | |
| 54 | if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) { |
| 55 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 56 | return -2; |
| 57 | } |
| 58 | |
| 59 | if (ppkey == NULL) |
| 60 | return -1; |
| 61 | |
| 62 | if (*ppkey == NULL) |
| 63 | *ppkey = EVP_PKEY_new(); |
| 64 | |
| 65 | if (*ppkey == NULL) { |
| 66 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | provdata = |
| 71 | evp_keymgmt_fromdata(*ppkey, ctx->keymgmt, params, |
| 72 | ctx->operation == EVP_PKEY_OP_PARAMFROMDATA); |
| 73 | |
| 74 | if (provdata == NULL) |
| 75 | return 0; |
| 76 | /* provdata is cached in *ppkey, so we need not bother with it further */ |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * TODO(3.0) Re-evaluate the names, it's possible that we find these to be |
| 82 | * better: |
| 83 | * |
| 84 | * EVP_PKEY_param_settable() |
| 85 | * EVP_PKEY_param_gettable() |
| 86 | */ |
| 87 | const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx) |
| 88 | { |
| 89 | /* We call fromdata_init to get ctx->keymgmt populated */ |
| 90 | if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED)) |
| 91 | return evp_keymgmt_importdomparam_types(ctx->keymgmt); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx) |
| 96 | { |
| 97 | /* We call fromdata_init to get ctx->keymgmt populated */ |
| 98 | if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED)) |
| 99 | return evp_keymgmt_importdomparam_types(ctx->keymgmt); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) |
| 104 | { |
| 105 | int ret; |
| 106 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
| 107 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN_INIT, |
| 108 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 109 | return -2; |
| 110 | } |
| 111 | ctx->operation = EVP_PKEY_OP_PARAMGEN; |
| 112 | if (!ctx->pmeth->paramgen_init) |
| 113 | return 1; |
| 114 | ret = ctx->pmeth->paramgen_init(ctx); |
| 115 | if (ret <= 0) |
| 116 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) |
| 121 | { |
| 122 | int ret; |
| 123 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
| 124 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, |
| 125 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 126 | return -2; |
| 127 | } |
| 128 | |
| 129 | if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { |
| 130 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, EVP_R_OPERATON_NOT_INITIALIZED); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | if (ppkey == NULL) |
| 135 | return -1; |
| 136 | |
| 137 | if (*ppkey == NULL) |
| 138 | *ppkey = EVP_PKEY_new(); |
| 139 | |
| 140 | if (*ppkey == NULL) { |
| 141 | EVPerr(EVP_F_EVP_PKEY_PARAMGEN, ERR_R_MALLOC_FAILURE); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | ret = ctx->pmeth->paramgen(ctx, *ppkey); |
| 146 | if (ret <= 0) { |
| 147 | EVP_PKEY_free(*ppkey); |
| 148 | *ppkey = NULL; |
| 149 | } |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) |
| 154 | { |
| 155 | int ret; |
| 156 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
| 157 | EVPerr(EVP_F_EVP_PKEY_KEYGEN_INIT, |
| 158 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 159 | return -2; |
| 160 | } |
| 161 | ctx->operation = EVP_PKEY_OP_KEYGEN; |
| 162 | if (!ctx->pmeth->keygen_init) |
| 163 | return 1; |
| 164 | ret = ctx->pmeth->keygen_init(ctx); |
| 165 | if (ret <= 0) |
| 166 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) |
| 171 | { |
| 172 | int ret; |
| 173 | |
| 174 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
| 175 | EVPerr(EVP_F_EVP_PKEY_KEYGEN, |
| 176 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 177 | return -2; |
| 178 | } |
| 179 | if (ctx->operation != EVP_PKEY_OP_KEYGEN) { |
| 180 | EVPerr(EVP_F_EVP_PKEY_KEYGEN, EVP_R_OPERATON_NOT_INITIALIZED); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | if (ppkey == NULL) |
| 185 | return -1; |
| 186 | |
| 187 | if (*ppkey == NULL) |
| 188 | *ppkey = EVP_PKEY_new(); |
| 189 | if (*ppkey == NULL) |
| 190 | return -1; |
| 191 | |
| 192 | ret = ctx->pmeth->keygen(ctx, *ppkey); |
| 193 | if (ret <= 0) { |
| 194 | EVP_PKEY_free(*ppkey); |
| 195 | *ppkey = NULL; |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) |
| 201 | { |
| 202 | ctx->pkey_gencb = cb; |
| 203 | } |
| 204 | |
| 205 | EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) |
| 206 | { |
| 207 | return ctx->pkey_gencb; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style |
| 212 | * callbacks. |
| 213 | */ |
| 214 | |
| 215 | static int trans_cb(int a, int b, BN_GENCB *gcb) |
| 216 | { |
| 217 | EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb); |
| 218 | ctx->keygen_info[0] = a; |
| 219 | ctx->keygen_info[1] = b; |
| 220 | return ctx->pkey_gencb(ctx); |
| 221 | } |
| 222 | |
| 223 | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) |
| 224 | { |
| 225 | BN_GENCB_set(cb, trans_cb, ctx); |
| 226 | } |
| 227 | |
| 228 | int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) |
| 229 | { |
| 230 | if (idx == -1) |
| 231 | return ctx->keygen_info_count; |
| 232 | if (idx < 0 || idx > ctx->keygen_info_count) |
| 233 | return 0; |
| 234 | return ctx->keygen_info[idx]; |
| 235 | } |
| 236 | |
| 237 | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, |
| 238 | const unsigned char *key, int keylen) |
| 239 | { |
| 240 | EVP_PKEY_CTX *mac_ctx = NULL; |
| 241 | EVP_PKEY *mac_key = NULL; |
| 242 | mac_ctx = EVP_PKEY_CTX_new_id(type, e); |
| 243 | if (!mac_ctx) |
| 244 | return NULL; |
| 245 | if (EVP_PKEY_keygen_init(mac_ctx) <= 0) |
| 246 | goto merr; |
| 247 | if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0) |
| 248 | goto merr; |
| 249 | if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) |
| 250 | goto merr; |
| 251 | merr: |
| 252 | EVP_PKEY_CTX_free(mac_ctx); |
| 253 | return mac_key; |
| 254 | } |
| 255 | |
| 256 | int EVP_PKEY_check(EVP_PKEY_CTX *ctx) |
| 257 | { |
| 258 | EVP_PKEY *pkey = ctx->pkey; |
| 259 | |
| 260 | if (pkey == NULL) { |
| 261 | EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* call customized check function first */ |
| 266 | if (ctx->pmeth->check != NULL) |
| 267 | return ctx->pmeth->check(pkey); |
| 268 | |
| 269 | /* use default check function in ameth */ |
| 270 | if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) { |
| 271 | EVPerr(EVP_F_EVP_PKEY_CHECK, |
| 272 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 273 | return -2; |
| 274 | } |
| 275 | |
| 276 | return pkey->ameth->pkey_check(pkey); |
| 277 | } |
| 278 | |
| 279 | int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx) |
| 280 | { |
| 281 | EVP_PKEY *pkey = ctx->pkey; |
| 282 | |
| 283 | if (pkey == NULL) { |
| 284 | EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* call customized public key check function first */ |
| 289 | if (ctx->pmeth->public_check != NULL) |
| 290 | return ctx->pmeth->public_check(pkey); |
| 291 | |
| 292 | /* use default public key check function in ameth */ |
| 293 | if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) { |
| 294 | EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, |
| 295 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 296 | return -2; |
| 297 | } |
| 298 | |
| 299 | return pkey->ameth->pkey_public_check(pkey); |
| 300 | } |
| 301 | |
| 302 | int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx) |
| 303 | { |
| 304 | EVP_PKEY *pkey = ctx->pkey; |
| 305 | |
| 306 | if (pkey == NULL) { |
| 307 | EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET); |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* call customized param check function first */ |
| 312 | if (ctx->pmeth->param_check != NULL) |
| 313 | return ctx->pmeth->param_check(pkey); |
| 314 | |
| 315 | /* use default param check function in ameth */ |
| 316 | if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) { |
| 317 | EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, |
| 318 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 319 | return -2; |
| 320 | } |
| 321 | |
| 322 | return pkey->ameth->pkey_param_check(pkey); |
| 323 | } |
| 324 | |