| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/digest.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 |
| 23 | #include "../internal.h" |
| 24 | #include "internal.h" |
| 25 |
| 26 |
| 27 | static const EVP_PKEY_METHOD *const evp_methods[] = { |
| 28 | &rsa_pkey_meth, &ec_pkey_meth, &ed25519_pkey_meth, |
| 29 | &x25519_pkey_meth, &hkdf_pkey_meth, |
| 30 | }; |
| 31 |
| 32 | static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) { |
| 33 | for (size_t i = 0; i < sizeof(evp_methods) / sizeof(EVP_PKEY_METHOD *); i++) { |
| 34 | if (evp_methods[i]->pkey_id == type) { |
| 35 | return evp_methods[i]; |
| 36 | } |
| 37 | } |
| 38 |
| 39 | return NULL; |
| 40 | } |
| 41 |
| 42 | static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, |
| 43 | const EVP_PKEY_METHOD *pmeth) { |
| 44 | EVP_PKEY_CTX *ret = |
| 45 | reinterpret_cast<EVP_PKEY_CTX *>(OPENSSL_zalloc(sizeof(EVP_PKEY_CTX))); |
| 46 | if (!ret) { |
| 47 | return NULL; |
| 48 | } |
| 49 |
| 50 | ret->engine = e; |
| 51 | ret->pmeth = pmeth; |
| 52 | ret->operation = EVP_PKEY_OP_UNDEFINED; |
| 53 |
| 54 | if (pkey) { |
| 55 | EVP_PKEY_up_ref(pkey); |
| 56 | ret->pkey = pkey; |
| 57 | } |
| 58 |
| 59 | if (pmeth->init) { |
| 60 | if (pmeth->init(ret) <= 0) { |
| 61 | EVP_PKEY_free(ret->pkey); |
| 62 | OPENSSL_free(ret); |
| 63 | return NULL; |
| 64 | } |
| 65 | } |
| 66 |
| 67 | return ret; |
| 68 | } |
| 69 |
| 70 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) { |
| 71 | if (pkey == NULL || pkey->ameth == NULL) { |
| 72 | OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 73 | return NULL; |
| 74 | } |
| 75 |
| 76 | const EVP_PKEY_METHOD *pkey_method = pkey->ameth->pkey_method; |
| 77 | if (pkey_method == NULL) { |
| 78 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 79 | ERR_add_error_dataf("algorithm %d", pkey->ameth->pkey_id); |
| 80 | return NULL; |
| 81 | } |
| 82 |
| 83 | return evp_pkey_ctx_new(pkey, e, pkey_method); |
| 84 | } |
| 85 |
| 86 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) { |
| 87 | const EVP_PKEY_METHOD *pkey_method = evp_pkey_meth_find(id); |
| 88 | if (pkey_method == NULL) { |
| 89 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
| 90 | ERR_add_error_dataf("algorithm %d", id); |
| 91 | return NULL; |
| 92 | } |
| 93 |
| 94 | return evp_pkey_ctx_new(NULL, e, pkey_method); |
| 95 | } |
| 96 |
| 97 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) { |
| 98 | if (ctx == NULL) { |
| 99 | return; |
| 100 | } |
| 101 | if (ctx->pmeth && ctx->pmeth->cleanup) { |
| 102 | ctx->pmeth->cleanup(ctx); |
| 103 | } |
| 104 | EVP_PKEY_free(ctx->pkey); |
| 105 | EVP_PKEY_free(ctx->peerkey); |
| 106 | OPENSSL_free(ctx); |
| 107 | } |
| 108 |
| 109 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *ctx) { |
| 110 | if (!ctx->pmeth || !ctx->pmeth->copy) { |
| 111 | return NULL; |
| 112 | } |
| 113 |
| 114 | EVP_PKEY_CTX *ret = |
| 115 | reinterpret_cast<EVP_PKEY_CTX *>(OPENSSL_zalloc(sizeof(EVP_PKEY_CTX))); |
| 116 | if (!ret) { |
| 117 | return NULL; |
| 118 | } |
| 119 |
| 120 | ret->pmeth = ctx->pmeth; |
| 121 | ret->engine = ctx->engine; |
| 122 | ret->operation = ctx->operation; |
| 123 |
| 124 | if (ctx->pkey != NULL) { |
| 125 | EVP_PKEY_up_ref(ctx->pkey); |
| 126 | ret->pkey = ctx->pkey; |
| 127 | } |
| 128 |
| 129 | if (ctx->peerkey != NULL) { |
| 130 | EVP_PKEY_up_ref(ctx->peerkey); |
| 131 | ret->peerkey = ctx->peerkey; |
| 132 | } |
| 133 |
| 134 | if (ctx->pmeth->copy(ret, ctx) <= 0) { |
| 135 | ret->pmeth = NULL; |
| 136 | EVP_PKEY_CTX_free(ret); |
| 137 | OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP); |
| 138 | return NULL; |
| 139 | } |
| 140 |
| 141 | return ret; |
| 142 | } |
| 143 |
| 144 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; } |
| 145 |
| 146 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd, |
| 147 | int p1, void *p2) { |
| 148 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) { |
| 149 | OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 150 | return 0; |
| 151 | } |
| 152 | if (keytype != -1 && ctx->pmeth->pkey_id != keytype) { |
| 153 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 154 | return 0; |
| 155 | } |
| 156 |
| 157 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
| 158 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_OPERATION_SET); |
| 159 | return 0; |
| 160 | } |
| 161 |
| 162 | if (optype != -1 && !(ctx->operation & optype)) { |
| 163 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_OPERATION); |
| 164 | return 0; |
| 165 | } |
| 166 |
| 167 | return ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
| 168 | } |
| 169 |
| 170 | int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) { |
| 171 | if (ctx == NULL || ctx->pmeth == NULL || |
| 172 | (ctx->pmeth->sign == NULL && ctx->pmeth->sign_message == NULL)) { |
| 173 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 174 | return 0; |
| 175 | } |
| 176 |
| 177 | ctx->operation = EVP_PKEY_OP_SIGN; |
| 178 | return 1; |
| 179 | } |
| 180 |
| 181 | int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len, |
| 182 | const uint8_t *digest, size_t digest_len) { |
| 183 | if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) { |
| 184 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 185 | return 0; |
| 186 | } |
| 187 | if (ctx->operation != EVP_PKEY_OP_SIGN) { |
| 188 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 189 | return 0; |
| 190 | } |
| 191 | return ctx->pmeth->sign(ctx, sig, sig_len, digest, digest_len); |
| 192 | } |
| 193 |
| 194 | int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) { |
| 195 | if (ctx == NULL || ctx->pmeth == NULL || |
| 196 | (ctx->pmeth->verify == NULL && ctx->pmeth->verify_message == NULL)) { |
| 197 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 198 | return 0; |
| 199 | } |
| 200 | ctx->operation = EVP_PKEY_OP_VERIFY; |
| 201 | return 1; |
| 202 | } |
| 203 |
| 204 | int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len, |
| 205 | const uint8_t *digest, size_t digest_len) { |
| 206 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) { |
| 207 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 208 | return 0; |
| 209 | } |
| 210 | if (ctx->operation != EVP_PKEY_OP_VERIFY) { |
| 211 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 212 | return 0; |
| 213 | } |
| 214 | return ctx->pmeth->verify(ctx, sig, sig_len, digest, digest_len); |
| 215 | } |
| 216 |
| 217 | int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) { |
| 218 | if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) { |
| 219 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 220 | return 0; |
| 221 | } |
| 222 | ctx->operation = EVP_PKEY_OP_ENCRYPT; |
| 223 | return 1; |
| 224 | } |
| 225 |
| 226 | int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen, |
| 227 | const uint8_t *in, size_t inlen) { |
| 228 | if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) { |
| 229 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 230 | return 0; |
| 231 | } |
| 232 | if (ctx->operation != EVP_PKEY_OP_ENCRYPT) { |
| 233 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 234 | return 0; |
| 235 | } |
| 236 | return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen); |
| 237 | } |
| 238 |
| 239 | int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) { |
| 240 | if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) { |
| 241 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 242 | return 0; |
| 243 | } |
| 244 | ctx->operation = EVP_PKEY_OP_DECRYPT; |
| 245 | return 1; |
| 246 | } |
| 247 |
| 248 | int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen, |
| 249 | const uint8_t *in, size_t inlen) { |
| 250 | if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) { |
| 251 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 252 | return 0; |
| 253 | } |
| 254 | if (ctx->operation != EVP_PKEY_OP_DECRYPT) { |
| 255 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 256 | return 0; |
| 257 | } |
| 258 | return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen); |
| 259 | } |
| 260 |
| 261 | int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx) { |
| 262 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) { |
| 263 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 264 | return 0; |
| 265 | } |
| 266 | ctx->operation = EVP_PKEY_OP_VERIFYRECOVER; |
| 267 | return 1; |
| 268 | } |
| 269 |
| 270 | int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len, |
| 271 | const uint8_t *sig, size_t sig_len) { |
| 272 | if (!ctx || !ctx->pmeth || !ctx->pmeth->verify_recover) { |
| 273 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 274 | return 0; |
| 275 | } |
| 276 | if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) { |
| 277 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 278 | return 0; |
| 279 | } |
| 280 | return ctx->pmeth->verify_recover(ctx, out, out_len, sig, sig_len); |
| 281 | } |
| 282 |
| 283 | int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) { |
| 284 | if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) { |
| 285 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 286 | return 0; |
| 287 | } |
| 288 | ctx->operation = EVP_PKEY_OP_DERIVE; |
| 289 | return 1; |
| 290 | } |
| 291 |
| 292 | int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) { |
| 293 | int ret; |
| 294 | if (!ctx || !ctx->pmeth || |
| 295 | !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) || |
| 296 | !ctx->pmeth->ctrl) { |
| 297 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 298 | return 0; |
| 299 | } |
| 300 | if (ctx->operation != EVP_PKEY_OP_DERIVE && |
| 301 | ctx->operation != EVP_PKEY_OP_ENCRYPT && |
| 302 | ctx->operation != EVP_PKEY_OP_DECRYPT) { |
| 303 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 304 | return 0; |
| 305 | } |
| 306 |
| 307 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer); |
| 308 |
| 309 | if (ret <= 0) { |
| 310 | return 0; |
| 311 | } |
| 312 |
| 313 | if (ret == 2) { |
| 314 | return 1; |
| 315 | } |
| 316 |
| 317 | if (!ctx->pkey) { |
| 318 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); |
| 319 | return 0; |
| 320 | } |
| 321 |
| 322 | if (ctx->pkey->type != peer->type) { |
| 323 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES); |
| 324 | return 0; |
| 325 | } |
| 326 |
| 327 | // ran@cryptocom.ru: For clarity. The error is if parameters in peer are |
| 328 | // present (!missing) but don't match. EVP_PKEY_cmp_parameters may return |
| 329 | // 1 (match), 0 (don't match) and -2 (comparison is not defined). -1 |
| 330 | // (different key types) is impossible here because it is checked earlier. |
| 331 | // -2 is OK for us here, as well as 1, so we can check for 0 only. |
| 332 | if (!EVP_PKEY_missing_parameters(peer) && |
| 333 | !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) { |
| 334 | OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS); |
| 335 | return 0; |
| 336 | } |
| 337 |
| 338 | EVP_PKEY_free(ctx->peerkey); |
| 339 | ctx->peerkey = peer; |
| 340 |
| 341 | ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer); |
| 342 |
| 343 | if (ret <= 0) { |
| 344 | ctx->peerkey = NULL; |
| 345 | return 0; |
| 346 | } |
| 347 |
| 348 | EVP_PKEY_up_ref(peer); |
| 349 | return 1; |
| 350 | } |
| 351 |
| 352 | int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) { |
| 353 | if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) { |
| 354 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 355 | return 0; |
| 356 | } |
| 357 | if (ctx->operation != EVP_PKEY_OP_DERIVE) { |
| 358 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 359 | return 0; |
| 360 | } |
| 361 | return ctx->pmeth->derive(ctx, key, out_key_len); |
| 362 | } |
| 363 |
| 364 | int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) { |
| 365 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
| 366 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 367 | return 0; |
| 368 | } |
| 369 | ctx->operation = EVP_PKEY_OP_KEYGEN; |
| 370 | return 1; |
| 371 | } |
| 372 |
| 373 | int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey) { |
| 374 | if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) { |
| 375 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 376 | return 0; |
| 377 | } |
| 378 | if (ctx->operation != EVP_PKEY_OP_KEYGEN) { |
| 379 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 380 | return 0; |
| 381 | } |
| 382 |
| 383 | if (!out_pkey) { |
| 384 | return 0; |
| 385 | } |
| 386 |
| 387 | if (!*out_pkey) { |
| 388 | *out_pkey = EVP_PKEY_new(); |
| 389 | if (!*out_pkey) { |
| 390 | OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP); |
| 391 | return 0; |
| 392 | } |
| 393 | } |
| 394 |
| 395 | if (!ctx->pmeth->keygen(ctx, *out_pkey)) { |
| 396 | EVP_PKEY_free(*out_pkey); |
| 397 | *out_pkey = NULL; |
| 398 | return 0; |
| 399 | } |
| 400 | return 1; |
| 401 | } |
| 402 |
| 403 | int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) { |
| 404 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
| 405 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 406 | return 0; |
| 407 | } |
| 408 | ctx->operation = EVP_PKEY_OP_PARAMGEN; |
| 409 | return 1; |
| 410 | } |
| 411 |
| 412 | int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **out_pkey) { |
| 413 | if (!ctx || !ctx->pmeth || !ctx->pmeth->paramgen) { |
| 414 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 415 | return 0; |
| 416 | } |
| 417 | if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { |
| 418 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATON_NOT_INITIALIZED); |
| 419 | return 0; |
| 420 | } |
| 421 |
| 422 | if (!out_pkey) { |
| 423 | return 0; |
| 424 | } |
| 425 |
| 426 | if (!*out_pkey) { |
| 427 | *out_pkey = EVP_PKEY_new(); |
| 428 | if (!*out_pkey) { |
| 429 | OPENSSL_PUT_ERROR(EVP, ERR_LIB_EVP); |
| 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 |
| 434 | if (!ctx->pmeth->paramgen(ctx, *out_pkey)) { |
| 435 | EVP_PKEY_free(*out_pkey); |
| 436 | *out_pkey = NULL; |
| 437 | return 0; |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 |