ошибка: агрегат HMAC_CTX ctx имеет неполный тип и не может быть определен - PullRequest
1 голос
/ 05 августа 2020

Я пытаюсь понять, что происходит не так, пока я компилирую свое приложение, и я получил следующую ошибку:

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 файлов:

безопасность. 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);
}

security.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 HMA C с использованием OpenSSL 1.1 не компилируется , но когда я это делаю, я получаю ошибку DWARF, хотя я делаю

make clean

, а затем

make

Кстати, мой 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

Я был бы признателен за любое предложенное решение, так как я застрял в что на два дня

1 Ответ

0 голосов
/ 05 августа 2020

В последних версиях OpenSSL (я думаю, начиная с 1.1.0) ряд структур, которые ранее были объявлены публично, теперь стали частными и отображаются в файлах заголовков publi c только как неполные типы. Следовательно, вы не можете использовать их непосредственно в своем коде (вы можете использовать только указатели).

Как следствие, способ создания HMA C немного изменился. Также было бы разумнее объявить 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);
}

Документация здесь .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...