| // |
| 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 <openssl/err.h> |
| 18 |
| 19 | #include "../../evp/internal.h" |
| 20 | #include "../delocate.h" |
| 21 | #include "../digest/internal.h" |
| 22 | #include "../service_indicator/internal.h" |
| 23 |
| 24 |
| 25 | enum evp_sign_verify_t { |
| 26 | evp_sign, |
| 27 | evp_verify, |
| 28 | }; |
| 29 |
| 30 | DEFINE_LOCAL_DATA(struct evp_md_pctx_ops, md_pctx_ops) { |
| 31 | out->free = EVP_PKEY_CTX_free; |
| 32 | out->dup = EVP_PKEY_CTX_dup; |
| 33 | } |
| 34 |
| 35 | static int uses_prehash(EVP_MD_CTX *ctx, enum evp_sign_verify_t op) { |
| 36 | return (op == evp_sign) ? (ctx->pctx->pmeth->sign != NULL) |
| 37 | : (ctx->pctx->pmeth->verify != NULL); |
| 38 | } |
| 39 |
| 40 | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
| 41 | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey, |
| 42 | enum evp_sign_verify_t op) { |
| 43 | if (ctx->pctx == NULL) { |
| 44 | ctx->pctx = EVP_PKEY_CTX_new(pkey, e); |
| 45 | } |
| 46 | if (ctx->pctx == NULL) { |
| 47 | return 0; |
| 48 | } |
| 49 | ctx->pctx_ops = md_pctx_ops(); |
| 50 |
| 51 | if (op == evp_verify) { |
| 52 | if (!EVP_PKEY_verify_init(ctx->pctx)) { |
| 53 | return 0; |
| 54 | } |
| 55 | } else { |
| 56 | if (!EVP_PKEY_sign_init(ctx->pctx)) { |
| 57 | return 0; |
| 58 | } |
| 59 | } |
| 60 |
| 61 | if (type != NULL && |
| 62 | !EVP_PKEY_CTX_set_signature_md(ctx->pctx, type)) { |
| 63 | return 0; |
| 64 | } |
| 65 |
| 66 | if (uses_prehash(ctx, op)) { |
| 67 | if (type == NULL) { |
| 68 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_DEFAULT_DIGEST); |
| 69 | return 0; |
| 70 | } |
| 71 | if (!EVP_DigestInit_ex(ctx, type, e)) { |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 |
| 76 | if (pctx) { |
| 77 | *pctx = ctx->pctx; |
| 78 | } |
| 79 | return 1; |
| 80 | } |
| 81 |
| 82 | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, const EVP_MD *type, |
| 83 | ENGINE *e, EVP_PKEY *pkey) { |
| 84 | return do_sigver_init(ctx, pctx, type, e, pkey, evp_sign); |
| 85 | } |
| 86 |
| 87 | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
| 88 | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) { |
| 89 | return do_sigver_init(ctx, pctx, type, e, pkey, evp_verify); |
| 90 | } |
| 91 |
| 92 | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 93 | if (!uses_prehash(ctx, evp_sign)) { |
| 94 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 95 | return 0; |
| 96 | } |
| 97 |
| 98 | return EVP_DigestUpdate(ctx, data, len); |
| 99 | } |
| 100 |
| 101 | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t len) { |
| 102 | if (!uses_prehash(ctx, evp_verify)) { |
| 103 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 104 | return 0; |
| 105 | } |
| 106 |
| 107 | return EVP_DigestUpdate(ctx, data, len); |
| 108 | } |
| 109 |
| 110 | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, uint8_t *out_sig, |
| 111 | size_t *out_sig_len) { |
| 112 | if (!uses_prehash(ctx, evp_sign)) { |
| 113 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 114 | return 0; |
| 115 | } |
| 116 |
| 117 | if (out_sig) { |
| 118 | EVP_MD_CTX tmp_ctx; |
| 119 | int ret; |
| 120 | uint8_t md[EVP_MAX_MD_SIZE]; |
| 121 | unsigned int mdlen; |
| 122 |
| 123 | FIPS_service_indicator_lock_state(); |
| 124 | EVP_MD_CTX_init(&tmp_ctx); |
| 125 | ret = EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) && |
| 126 | EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
| 127 | EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, md, mdlen); |
| 128 | EVP_MD_CTX_cleanup(&tmp_ctx); |
| 129 | FIPS_service_indicator_unlock_state(); |
| 130 |
| 131 | if (ret) { |
| 132 | EVP_DigestSign_verify_service_indicator(ctx); |
| 133 | } |
| 134 |
| 135 | return ret; |
| 136 | } else { |
| 137 | size_t s = EVP_MD_size(ctx->digest); |
| 138 | return EVP_PKEY_sign(ctx->pctx, out_sig, out_sig_len, NULL, s); |
| 139 | } |
| 140 | } |
| 141 |
| 142 | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const uint8_t *sig, |
| 143 | size_t sig_len) { |
| 144 | if (!uses_prehash(ctx, evp_verify)) { |
| 145 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 146 | return 0; |
| 147 | } |
| 148 |
| 149 | EVP_MD_CTX tmp_ctx; |
| 150 | int ret; |
| 151 | uint8_t md[EVP_MAX_MD_SIZE]; |
| 152 | unsigned int mdlen; |
| 153 |
| 154 | FIPS_service_indicator_lock_state(); |
| 155 | EVP_MD_CTX_init(&tmp_ctx); |
| 156 | ret = EVP_MD_CTX_copy_ex(&tmp_ctx, ctx) && |
| 157 | EVP_DigestFinal_ex(&tmp_ctx, md, &mdlen) && |
| 158 | EVP_PKEY_verify(ctx->pctx, sig, sig_len, md, mdlen); |
| 159 | FIPS_service_indicator_unlock_state(); |
| 160 | EVP_MD_CTX_cleanup(&tmp_ctx); |
| 161 |
| 162 | if (ret) { |
| 163 | EVP_DigestVerify_verify_service_indicator(ctx); |
| 164 | } |
| 165 |
| 166 | return ret; |
| 167 | } |
| 168 |
| 169 | int EVP_DigestSign(EVP_MD_CTX *ctx, uint8_t *out_sig, size_t *out_sig_len, |
| 170 | const uint8_t *data, size_t data_len) { |
| 171 | FIPS_service_indicator_lock_state(); |
| 172 | int ret = 0; |
| 173 |
| 174 | if (uses_prehash(ctx, evp_sign)) { |
| 175 | // If |out_sig| is NULL, the caller is only querying the maximum output |
| 176 | // length. |data| should only be incorporated in the final call. |
| 177 | if (out_sig != NULL && |
| 178 | !EVP_DigestSignUpdate(ctx, data, data_len)) { |
| 179 | goto end; |
| 180 | } |
| 181 |
| 182 | ret = EVP_DigestSignFinal(ctx, out_sig, out_sig_len); |
| 183 | goto end; |
| 184 | } |
| 185 |
| 186 | if (ctx->pctx->pmeth->sign_message == NULL) { |
| 187 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 188 | goto end; |
| 189 | } |
| 190 |
| 191 | ret = ctx->pctx->pmeth->sign_message(ctx->pctx, out_sig, out_sig_len, data, |
| 192 | data_len); |
| 193 |
| 194 | end: |
| 195 | FIPS_service_indicator_unlock_state(); |
| 196 | if (ret) { |
| 197 | EVP_DigestSign_verify_service_indicator(ctx); |
| 198 | } |
| 199 | return ret; |
| 200 | } |
| 201 |
| 202 | int EVP_DigestVerify(EVP_MD_CTX *ctx, const uint8_t *sig, size_t sig_len, |
| 203 | const uint8_t *data, size_t len) { |
| 204 | FIPS_service_indicator_lock_state(); |
| 205 | int ret = 0; |
| 206 |
| 207 | if (uses_prehash(ctx, evp_verify)) { |
| 208 | ret = EVP_DigestVerifyUpdate(ctx, data, len) && |
| 209 | EVP_DigestVerifyFinal(ctx, sig, sig_len); |
| 210 | goto end; |
| 211 | } |
| 212 |
| 213 | if (ctx->pctx->pmeth->verify_message == NULL) { |
| 214 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 215 | goto end; |
| 216 | } |
| 217 |
| 218 | ret = ctx->pctx->pmeth->verify_message(ctx->pctx, sig, sig_len, data, len); |
| 219 |
| 220 | end: |
| 221 | FIPS_service_indicator_unlock_state(); |
| 222 | if (ret) { |
| 223 | EVP_DigestVerify_verify_service_indicator(ctx); |
| 224 | } |
| 225 | return ret; |
| 226 | } |
| 227 |