![](/img/trans.png)
[英]EVP_MD_CTX_destroy segfaults when linking to a dynamic lib using openssl 1.1
[英]Error while using EVP_MD_CTX_cleanup(ctx ) in C++ using openssl 1.1.0
我正在嘗試運行/編譯 2018 年使用舊 OpenSSL 版本制作的舊代碼。
我遇到了很多錯誤,但我都解決了。 現在我只是堅持這一行的最后一個錯誤:
EVP_MD_CTX_cleanup(ctx)
錯誤在這里:
error: ‘EVP_MD_CTX_cleanup’ was not declared in this scope; did you mean ‘EVP_MD_CTX_create’?
22 | if ( EVP_MD_CTX_cleanup(ctx ) != 1 ) { }
該部分的代碼如下:
#ifndef SHA256_HPP
#define SHA256_HPP
#include <openssl/evp.h>
#include <array>
class sha256
{
public:
inline sha256()
: ctx()
{
if ( EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr ) != 1 ) {
throw "EVP_DigestInit_ex() failed";
}
}
inline ~sha256()
{
if ( EVP_MD_CTX_cleanup(ctx ) != 1 ) { }
}
sha256( sha256 const& ) = delete;
sha256& operator=( sha256 const& ) = delete;
sha256( sha256&& ) = delete;
sha256& operator=( sha256&& ) = delete;
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(ctx, data, len ) != 1 ) {
throw "EVP_DigestUpdate() failed";
}
std::array<unsigned char, 32> hash;
unsigned int hash_size = sizeof( hash );
if ( EVP_DigestFinal_ex(ctx, hash.data(), &hash_size ) != 1 ) {
throw "EVP_DigestFinal_ex() failed";
}
if ( hash_size != sizeof( hash ) ) {
throw "unexpected hash size";
}
return hash;
}
private:
EVP_MD_CTX *ctx;
};
#endif // SHA256_HPP
我試過了:
if ( EVP_MD_CTX_free(ctx ) != 1 )
if ( EVP_MD_CTX_destroy (ctx ) != 1 )
但是,我仍然有同樣的錯誤。
有什么想法我必須改變才能讓它在 cygwin64 上的 Openssl 1.1.0 上運行嗎?
OpenSSL 1.1.0 沒那么老,只有幾年。 您確定這是您應該使用的正確版本嗎? 更有可能的是,您應該改用 1.0.2,因為EVP_MD_CTX_cleanup()
在 1.1.0 中不存在。
1.1.0 中的主要變化之一是將大部分 OpenSSL 結構改為不透明指針。 您顯示的代碼不應在任何版本中工作,因為您將 null EVP_MD_CTX*
指針傳遞給所有EVP_Digest...()
函數,這將不起作用。
代碼最初看起來更像這樣,在你亂搞它之前:
class sha256
{
public:
inline sha256()
{
EVP_MD_CTX_init(&ctx); // <-- init with this
if ( EVP_DigestInit_ex(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
}
inline ~sha256()
{
if ( EVP_MD_CTX_cleanup(&ctx) != 1 ) { } // <-- use '&' here
}
...
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
...
if ( EVP_DigestFinal_ex(&ctx, ...) != 1 ) { // <-- use '&' here
...
}
...
}
private:
EVP_MD_CTX ctx; // <-- no '*' here
};
在 OpenSSL 1.1.0 中,它看起來像這樣:
class sha256
{
public:
inline sha256()
{
ctx = EVP_MD_CTX_new(); // <-- init with this
if ( !ctx ) {
...
}
if ( EVP_DigestInit_ex(ctx, ...) != 1 ) { // <-- no '&' here
...
}
}
inline ~sha256()
{
EVP_MD_CTX_free(ctx); // <-- no '&' here
}
...
inline std::array<unsigned char, 32> hash( unsigned char const* const data, int const len )
{
if ( EVP_DigestUpdate(ctx, ...) != 1 ) { // <-- no '&' here
...
}
...
if ( EVP_DigestFinal_ex(ctx, ...) != 1 ) { // <-- no '&' here
...
}
...
}
private:
EVP_MD_CTX *ctx; // <-- use '*' here
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.