1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <stdio.h> 11e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 12e1051a39Sopenharmony_ci#include <openssl/evp.h> 13e1051a39Sopenharmony_ci#include <openssl/objects.h> 14e1051a39Sopenharmony_ci#include <openssl/x509.h> 15e1051a39Sopenharmony_ci#include "crypto/evp.h" 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_ciint EVP_VerifyFinal_ex(EVP_MD_CTX *ctx, const unsigned char *sigbuf, 18e1051a39Sopenharmony_ci unsigned int siglen, EVP_PKEY *pkey, OSSL_LIB_CTX *libctx, 19e1051a39Sopenharmony_ci const char *propq) 20e1051a39Sopenharmony_ci{ 21e1051a39Sopenharmony_ci unsigned char m[EVP_MAX_MD_SIZE]; 22e1051a39Sopenharmony_ci unsigned int m_len = 0; 23e1051a39Sopenharmony_ci int i = 0; 24e1051a39Sopenharmony_ci EVP_PKEY_CTX *pkctx = NULL; 25e1051a39Sopenharmony_ci 26e1051a39Sopenharmony_ci if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_FINALISE)) { 27e1051a39Sopenharmony_ci if (!EVP_DigestFinal_ex(ctx, m, &m_len)) 28e1051a39Sopenharmony_ci goto err; 29e1051a39Sopenharmony_ci } else { 30e1051a39Sopenharmony_ci int rv = 0; 31e1051a39Sopenharmony_ci EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); 32e1051a39Sopenharmony_ci 33e1051a39Sopenharmony_ci if (tmp_ctx == NULL) { 34e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); 35e1051a39Sopenharmony_ci return 0; 36e1051a39Sopenharmony_ci } 37e1051a39Sopenharmony_ci rv = EVP_MD_CTX_copy_ex(tmp_ctx, ctx); 38e1051a39Sopenharmony_ci if (rv) 39e1051a39Sopenharmony_ci rv = EVP_DigestFinal_ex(tmp_ctx, m, &m_len); 40e1051a39Sopenharmony_ci EVP_MD_CTX_free(tmp_ctx); 41e1051a39Sopenharmony_ci if (!rv) 42e1051a39Sopenharmony_ci return 0; 43e1051a39Sopenharmony_ci } 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_ci i = -1; 46e1051a39Sopenharmony_ci pkctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); 47e1051a39Sopenharmony_ci if (pkctx == NULL) 48e1051a39Sopenharmony_ci goto err; 49e1051a39Sopenharmony_ci if (EVP_PKEY_verify_init(pkctx) <= 0) 50e1051a39Sopenharmony_ci goto err; 51e1051a39Sopenharmony_ci if (EVP_PKEY_CTX_set_signature_md(pkctx, EVP_MD_CTX_get0_md(ctx)) <= 0) 52e1051a39Sopenharmony_ci goto err; 53e1051a39Sopenharmony_ci i = EVP_PKEY_verify(pkctx, sigbuf, siglen, m, m_len); 54e1051a39Sopenharmony_ci err: 55e1051a39Sopenharmony_ci EVP_PKEY_CTX_free(pkctx); 56e1051a39Sopenharmony_ci return i; 57e1051a39Sopenharmony_ci} 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ciint EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf, 60e1051a39Sopenharmony_ci unsigned int siglen, EVP_PKEY *pkey) 61e1051a39Sopenharmony_ci{ 62e1051a39Sopenharmony_ci return EVP_VerifyFinal_ex(ctx, sigbuf, siglen, pkey, NULL, NULL); 63e1051a39Sopenharmony_ci} 64