/src/openssl30/crypto/evp/pmeth_check.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2006-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 <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 |  | #ifndef FIPS_MODULE | 
| 17 |  | # include "crypto/asn1.h" | 
| 18 |  | #endif | 
| 19 |  | #include "crypto/evp.h" | 
| 20 |  | #include "evp_local.h" | 
| 21 |  |  | 
| 22 |  | /* | 
| 23 |  |  * Returns: | 
| 24 |  |  *  1   True | 
| 25 |  |  *  0   False | 
| 26 |  |  * -1   Unsupported (use legacy path) | 
| 27 |  |  */ | 
| 28 |  | static int try_provided_check(EVP_PKEY_CTX *ctx, int selection, int checktype) | 
| 29 | 27.0k | { | 
| 30 | 27.0k |     EVP_KEYMGMT *keymgmt; | 
| 31 | 27.0k |     void *keydata; | 
| 32 |  |  | 
| 33 | 27.0k |     if (evp_pkey_ctx_is_legacy(ctx)) | 
| 34 | 0 |         return -1; | 
| 35 |  |  | 
| 36 | 27.0k |     keymgmt = ctx->keymgmt; | 
| 37 | 27.0k |     keydata = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, | 
| 38 | 27.0k |                                           &keymgmt, ctx->propquery); | 
| 39 | 27.0k |     if (keydata == NULL) { | 
| 40 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 41 | 0 |         return 0; | 
| 42 | 0 |     } | 
| 43 |  |  | 
| 44 | 27.0k |     return evp_keymgmt_validate(keymgmt, keydata, selection, checktype); | 
| 45 | 27.0k | } | 
| 46 |  |  | 
| 47 |  | static int evp_pkey_public_check_combined(EVP_PKEY_CTX *ctx, int checktype) | 
| 48 | 11.1k | { | 
| 49 | 11.1k |     EVP_PKEY *pkey = ctx->pkey; | 
| 50 | 11.1k |     int ok; | 
| 51 |  |  | 
| 52 | 11.1k |     if (pkey == NULL) { | 
| 53 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); | 
| 54 | 0 |         return 0; | 
| 55 | 0 |     } | 
| 56 |  |  | 
| 57 | 11.1k |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, | 
| 58 | 11.1k |                                  checktype)) != -1) | 
| 59 | 11.1k |         return ok; | 
| 60 |  |  | 
| 61 | 0 |     if (pkey->type == EVP_PKEY_NONE) | 
| 62 | 0 |         goto not_supported; | 
| 63 |  |  | 
| 64 | 0 | #ifndef FIPS_MODULE | 
| 65 |  |     /* legacy */ | 
| 66 |  |     /* call customized public key check function first */ | 
| 67 | 0 |     if (ctx->pmeth->public_check != NULL) | 
| 68 | 0 |         return ctx->pmeth->public_check(pkey); | 
| 69 |  |  | 
| 70 |  |     /* use default public key check function in ameth */ | 
| 71 | 0 |     if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) | 
| 72 | 0 |         goto not_supported; | 
| 73 |  |  | 
| 74 | 0 |     return pkey->ameth->pkey_public_check(pkey); | 
| 75 | 0 | #endif | 
| 76 | 0 |  not_supported: | 
| 77 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 78 | 0 |     return -2; | 
| 79 | 0 | } | 
| 80 |  |  | 
| 81 |  | int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx) | 
| 82 | 11.1k | { | 
| 83 | 11.1k |     return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK); | 
| 84 | 11.1k | } | 
| 85 |  |  | 
| 86 |  | int EVP_PKEY_public_check_quick(EVP_PKEY_CTX *ctx) | 
| 87 | 0 | { | 
| 88 | 0 |     return evp_pkey_public_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK); | 
| 89 | 0 | } | 
| 90 |  |  | 
| 91 |  | static int evp_pkey_param_check_combined(EVP_PKEY_CTX *ctx, int checktype) | 
| 92 | 7.18k | { | 
| 93 | 7.18k |     EVP_PKEY *pkey = ctx->pkey; | 
| 94 | 7.18k |     int ok; | 
| 95 |  |  | 
| 96 | 7.18k |     if (pkey == NULL) { | 
| 97 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); | 
| 98 | 0 |         return 0; | 
| 99 | 0 |     } | 
| 100 |  |  | 
| 101 | 7.18k |     if ((ok = try_provided_check(ctx, | 
| 102 | 7.18k |                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, | 
| 103 | 7.18k |                                  checktype)) != -1) | 
| 104 | 7.18k |         return ok; | 
| 105 |  |  | 
| 106 | 0 |     if (pkey->type == EVP_PKEY_NONE) | 
| 107 | 0 |         goto not_supported; | 
| 108 |  |  | 
| 109 | 0 | #ifndef FIPS_MODULE | 
| 110 |  |     /* legacy */ | 
| 111 |  |     /* call customized param check function first */ | 
| 112 | 0 |     if (ctx->pmeth->param_check != NULL) | 
| 113 | 0 |         return ctx->pmeth->param_check(pkey); | 
| 114 |  |  | 
| 115 |  |     /* use default param check function in ameth */ | 
| 116 | 0 |     if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) | 
| 117 | 0 |         goto not_supported; | 
| 118 |  |  | 
| 119 | 0 |     return pkey->ameth->pkey_param_check(pkey); | 
| 120 | 0 | #endif | 
| 121 | 0 |  not_supported: | 
| 122 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 123 | 0 |     return -2; | 
| 124 | 0 | } | 
| 125 |  |  | 
| 126 |  | int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx) | 
| 127 | 4.36k | { | 
| 128 | 4.36k |     return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_FULL_CHECK); | 
| 129 | 4.36k | } | 
| 130 |  |  | 
| 131 |  | int EVP_PKEY_param_check_quick(EVP_PKEY_CTX *ctx) | 
| 132 | 2.81k | { | 
| 133 | 2.81k |     return evp_pkey_param_check_combined(ctx, OSSL_KEYMGMT_VALIDATE_QUICK_CHECK); | 
| 134 | 2.81k | } | 
| 135 |  |  | 
| 136 |  | int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx) | 
| 137 | 4.36k | { | 
| 138 | 4.36k |     EVP_PKEY *pkey = ctx->pkey; | 
| 139 | 4.36k |     int ok; | 
| 140 |  |  | 
| 141 | 4.36k |     if (pkey == NULL) { | 
| 142 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); | 
| 143 | 0 |         return 0; | 
| 144 | 0 |     } | 
| 145 |  |  | 
| 146 | 4.36k |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, | 
| 147 | 4.36k |                                  OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1) | 
| 148 | 4.36k |         return ok; | 
| 149 |  |  | 
| 150 |  |     /* not supported for legacy keys */ | 
| 151 | 4.36k |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 152 | 0 |     return -2; | 
| 153 | 4.36k | } | 
| 154 |  |  | 
| 155 |  | int EVP_PKEY_check(EVP_PKEY_CTX *ctx) | 
| 156 | 0 | { | 
| 157 | 0 |     return EVP_PKEY_pairwise_check(ctx); | 
| 158 | 0 | } | 
| 159 |  |  | 
| 160 |  | int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx) | 
| 161 | 4.36k | { | 
| 162 | 4.36k |     EVP_PKEY *pkey = ctx->pkey; | 
| 163 | 4.36k |     int ok; | 
| 164 |  |  | 
| 165 | 4.36k |     if (pkey == NULL) { | 
| 166 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); | 
| 167 | 0 |         return 0; | 
| 168 | 0 |     } | 
| 169 |  |  | 
| 170 | 4.36k |     if ((ok = try_provided_check(ctx, OSSL_KEYMGMT_SELECT_KEYPAIR, | 
| 171 | 4.36k |                                  OSSL_KEYMGMT_VALIDATE_FULL_CHECK)) != -1) | 
| 172 | 4.36k |         return ok; | 
| 173 |  |  | 
| 174 | 0 |     if (pkey->type == EVP_PKEY_NONE) | 
| 175 | 0 |         goto not_supported; | 
| 176 |  |  | 
| 177 | 0 | #ifndef FIPS_MODULE | 
| 178 |  |     /* legacy */ | 
| 179 |  |     /* call customized check function first */ | 
| 180 | 0 |     if (ctx->pmeth->check != NULL) | 
| 181 | 0 |         return ctx->pmeth->check(pkey); | 
| 182 |  |  | 
| 183 |  |     /* use default check function in ameth */ | 
| 184 | 0 |     if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) | 
| 185 | 0 |         goto not_supported; | 
| 186 |  |  | 
| 187 | 0 |     return pkey->ameth->pkey_check(pkey); | 
| 188 | 0 | #endif | 
| 189 | 0 |  not_supported: | 
| 190 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 191 | 0 |     return -2; | 
| 192 | 0 | } | 
| 193 |  |  |