c++ 错误:聚合“HMAC_CTX ctx”的类型不完整,无法定义

nkhmeac6  于 2023-02-06  发布在  Mac
关注(0)|答案(1)|浏览(628)

我试图了解什么是错的,而我编译我的应用程序,得到了以下错误:

security.cpp: In member function ‘void Crypt::load()’:
security.cpp:16:21: warning: ‘void OPENSSL_config(const char*)’ is deprecated [-Wdeprecated-declarations]
  OPENSSL_config(NULL);
                     ^
In file included from /usr/local/include/openssl/e_os2.h:13:0,
                 from /usr/local/include/openssl/bio.h:13,
                 from /usr/local/include/openssl/conf.h:13,
                 from security.h:5,
                 from security.cpp:1:
/usr/local/include/openssl/conf.h:91:25: note: declared here
 DEPRECATEDIN_1_1_0(void OPENSSL_config(const char *config_name))
                         ^
security.cpp: In member function ‘void Integrity::get_hmac(uint8_t*, int, uint8_t*, uint64_t)’:
security.cpp:112:11: error: aggregate ‘HMAC_CTX ctx’ has incomplete type and cannot be defined
  HMAC_CTX ctx;
           ^~~
security.cpp:115:2: error: ‘HMAC_CTX_init’ was not declared in this scope
  HMAC_CTX_init(&ctx);
  ^~~~~~~~~~~~~
security.cpp:115:2: note: suggested alternative: ‘HMAC_CTX_new’
  HMAC_CTX_init(&ctx);
  ^~~~~~~~~~~~~
  HMAC_CTX_new
security.cpp:119:2: error: ‘HMAC_CTX_cleanup’ was not declared in this scope
  HMAC_CTX_cleanup(&ctx);
  ^~~~~~~~~~~~~~~~
security.cpp:119:2: note: suggested alternative: ‘HMAC_CTX_get_md’
  HMAC_CTX_cleanup(&ctx);
  ^~~~~~~~~~~~~~~~
  HMAC_CTX_get_md
Makefile:25: recipe for target 'security.o' failed
make: *** [security.o] Error 1

我的cpp文件的一些代码部分:
security.cpp:

Crypt g_crypt;
Integrity g_integrity;

Crypt::Crypt() {
        key = (uint8_t *)"01234567890123456789012345678901";
        iv = (uint8_t *)"01234567890123456";
        load();
}

void Crypt::load() {    
        ERR_load_crypto_strings();
        OpenSSL_add_all_algorithms();
        OPENSSL_config(NULL);
}

......
......
......
......
void Integrity::get_hmac(uint8_t *data, int data_len, uint8_t *hmac, uint64_t k_nas_int) {
        HMAC_CTX ctx;
        int res_len;

        HMAC_CTX_init(&ctx);
        HMAC_Init_ex(&ctx, key, strlen((const char*)key), EVP_sha1(), NULL);
        HMAC_Update(&ctx, data, data_len);
        HMAC_Final(&ctx, hmac, (unsigned int*)&res_len);
        HMAC_CTX_cleanup(&ctx);
}

安全性.h:

#ifndef SECURITY_H
#define SECURITY_H

/* (C) OPENSSL_config */
#include </usr/local/include/openssl/conf.h>

/* (C) EVP_* */
#include </usr/local/include/openssl/evp.h>

/* (C) ERR_* */
#include </usr/local/include/openssl/err.h>

/* (C) HMAC_* */
#include </usr/local/include/openssl/hmac.h>

#include "packet.h"
#include "utils.h"

#define HMAC_ON 1
#define ENC_ON 1

const int HMAC_LEN = 20;

class Crypt {
private:
        uint8_t *key;
        uint8_t *iv;

        void load();
        int enc_data(uint8_t*, int, uint8_t*, uint64_t);
        int dec_data(uint8_t*, int, uint8_t*, uint64_t);
        void handle_crypt_error();

public:
        Crypt();
        void enc(Packet&, uint64_t);
        void dec(Packet&, uint64_t);
        ~Crypt();
};

class Integrity {
private:
        uint8_t *key;

public:
        Integrity();
        void add_hmac(Packet&, uint64_t);
        void get_hmac(uint8_t*, int, uint8_t*, uint64_t);
        void rem_hmac(Packet&, uint8_t*);
        bool hmac_check(Packet&, uint64_t);
        bool cmp_hmacs(uint8_t*, uint8_t*);
        void print_hmac(uint8_t*);
        ~Integrity();
};

extern Crypt g_crypt;
extern Integrity g_integrity;
void encrypt_add_hmac(Packet &pkt);
void decrypt_remove_hmac(Packet &pkt);

现在我已经看到,对于OpenSSL版本〉1.1.0,有一些修改建议SHA256 HMAC using OpenSSL 1.1 not compiling,但当我这样做时,我得到一个DWARF错误,即使我做了
清理
然后
制造
顺便说一下,我的Makefile(它停止的地方)看起来像这样

G++ = g++ -std=c++0x -std=c++11 -std=gnu++0x -ggdb -O3 -fpermissive -Wno-narrowing
security.o: packet.h security.cpp security.h utils.h
        $(G++) -c -o security.o security.cpp -lcrypto

我会感激任何建议的解决方案,因为我被困在那里两天了

qacovj5a

qacovj5a1#

在OpenSSL的最新版本中(我认为是1.1.0),许多以前公开声明的结构现在变成了私有的,并且只作为不完整的类型出现在公共头文件中。因此,您不能在代码中直接使用它们(您只能使用指针)。
因此,生成HMAC的方式略有改变,将res_len声明为unsigned int也会更有意义,这样就可以摆脱丑陋的强制转换:

void Integrity::get_hmac(uint8_t *data, int data_len, uint8_t *hmac, uint64_t k_nas_int) {
        HMAC_CTX *ctx;
        unsigned int res_len;

        ctx = HMAC_CTX_new();
        HMAC_Init_ex(ctx, key, strlen((const char*)key), EVP_sha1(), NULL);
        HMAC_Update(ctx, data, data_len);
        HMAC_Final(ctx, hmac, &res_len);
        HMAC_CTX_free(ctx);
}

文件here

相关问题