From ef6344980b87f3971363ad9b971f16dd43d8d0ba Mon Sep 17 00:00:00 2001 From: garciay <yann.garcia@fscom.fr> Date: Mon, 2 Sep 2024 09:38:32 +0200 Subject: [PATCH] Editorial changes; CV2x#4 validation for AtsCAM; Enhance .cfg files --- ccsrc/Framework/include/converter.hh | 4 +- ccsrc/Framework/src/converter.cc | 22 ++++---- ccsrc/Helpers/helpers_externals.cc | 12 ++-- ccsrc/Protocols/ETH/ethernet_layer.cc | 8 +-- ccsrc/Protocols/Http/http_codec.cc | 28 +++++----- ccsrc/Protocols/Http/http_codec.hh | 4 +- ccsrc/Protocols/Pcap/pcap_cygwin_layer.cc | 8 +-- ccsrc/Protocols/Pcap/pcap_layer.cc | 4 +- ccsrc/Protocols/Pcap/pcap_linux_layer.cc | 64 ++++++++++++++++++---- ccsrc/Protocols/Pcap/pcap_linux_layer.hh | 1 - ccsrc/Protocols/Pcap/pcap_offline_layer.cc | 2 +- ccsrc/Protocols/Tcp/tcp_layer.cc | 8 +-- ccsrc/Protocols/Tcp/tcp_layer.hh | 8 +-- ccsrc/Protocols/UDP/udp_layer.cc | 10 ++-- ccsrc/loggers/loggers.hh | 8 +-- ccsrc/security/include/certs_cache.hh | 2 +- ccsrc/security/include/hmac.hh | 6 +- ccsrc/security/include/sha1.hh | 6 +- ccsrc/security/include/sha256.hh | 6 +- ccsrc/security/include/sha384.hh | 2 +- ccsrc/security/src/certs_cache.cc | 6 +- ccsrc/security/src/certs_loader.cc | 2 +- ccsrc/security/src/hmac.cc | 10 ++-- ccsrc/security/src/security_externals.cc | 2 +- ccsrc/security/src/securty_services.cc | 32 +++++------ ccsrc/security/src/sha1.cc | 6 +- ccsrc/security/src/sha256.cc | 6 +- ccsrc/security/src/sha384.cc | 8 +-- 28 files changed, 162 insertions(+), 123 deletions(-) diff --git a/ccsrc/Framework/include/converter.hh b/ccsrc/Framework/include/converter.hh index cdb29e9..31fc2a2 100644 --- a/ccsrc/Framework/include/converter.hh +++ b/ccsrc/Framework/include/converter.hh @@ -380,14 +380,14 @@ public: * \param[in] p_value The buffer value * \return The Base64 encoded buffert */ - std::vector<unsigned char> buffer_to_base64(const std::vector<unsigned char> &p_value, const bool p_is_url = false); + std::vector<uint8_t> buffer_to_base64(const std::vector<uint8_t> &p_value, const bool p_is_url = false); /*! * \brief Convert the provided Base64 buffer * \param[in] p_value The buffer value * \return The Base64 encoded buffert */ - std::vector<unsigned char> base64_to_buffer(const std::vector<unsigned char> &p_value, const bool p_remove_crlf = true); + std::vector<uint8_t> base64_to_buffer(const std::vector<uint8_t> &p_value, const bool p_remove_crlf = true); static const std::string lut_u; static const std::string lut_l; diff --git a/ccsrc/Framework/src/converter.cc b/ccsrc/Framework/src/converter.cc index 9df0ad9..79aa392 100644 --- a/ccsrc/Framework/src/converter.cc +++ b/ccsrc/Framework/src/converter.cc @@ -159,13 +159,13 @@ const std::string converter::base64_enc_map[2] = { * @param[in] chr * @return Return the position of chr within base64_encode() */ -static unsigned char pos_of_char(const unsigned char chr) { +static uint8_t pos_of_char(const uint8_t chr) { if ((chr >= 'A' && chr <= 'Z')) { - return static_cast<unsigned char>(chr - 'A'); + return static_cast<uint8_t>(chr - 'A'); } else if ((chr >= 'a' && chr <= 'z')) { - return static_cast<unsigned char>(chr - 'a' + ('Z' - 'A') + 1); + return static_cast<uint8_t>(chr - 'a' + ('Z' - 'A') + 1); } else if ((chr >= '0' && chr <= '9')) { - return static_cast<unsigned char>(chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2); + return static_cast<uint8_t>(chr - '0' + ('Z' - 'A') + ('z' - 'a') + 2); } else if ((chr == '+' || chr == '-')) { return 62; // Be liberal with input and accept both url ('-') and non-url ('+') base 64 characters ( } else if ((chr == '/' || chr == '_')) { @@ -197,11 +197,11 @@ std::string converter::replace(const std::string& p_value, const std::string& p_ return str; } -std::vector<unsigned char> converter::buffer_to_base64(const std::vector<unsigned char> &p_value, const bool p_is_url) { +std::vector<uint8_t> converter::buffer_to_base64(const std::vector<uint8_t> &p_value, const bool p_is_url) { const std::string& base64_enc_map_ = converter::base64_enc_map[(p_is_url) ? 1 : 0]; - const unsigned char trailing_char = (p_is_url) ? '.' : '='; + const uint8_t trailing_char = (p_is_url) ? '.' : '='; - std::vector<unsigned char> out; + std::vector<uint8_t> out; //out.resize((p_value.size() + 2) / 3 * 4); unsigned int pos = 0; @@ -229,19 +229,19 @@ std::vector<unsigned char> converter::buffer_to_base64(const std::vector<unsigne return out; } -std::vector<unsigned char> converter::base64_to_buffer(const std::vector<unsigned char> &p_value, const bool p_remove_crlf) { +std::vector<uint8_t> converter::base64_to_buffer(const std::vector<uint8_t> &p_value, const bool p_remove_crlf) { if (p_value.size() == 0) { - return std::vector<unsigned char>(); + return std::vector<uint8_t>(); } - std::vector<unsigned char> value(p_value); + std::vector<uint8_t> value(p_value); if (p_remove_crlf) { value.erase(std::remove(value.begin(), value.end(), '\r'), value.end()); value.erase(std::remove(value.begin(), value.end(), '\n'), value.end()); } - std::vector<unsigned char> out; + std::vector<uint8_t> out; //out.resize(value.size() / 4 * 3); size_t pos = 0; diff --git a/ccsrc/Helpers/helpers_externals.cc b/ccsrc/Helpers/helpers_externals.cc index 37d1f3e..bd77abc 100644 --- a/ccsrc/Helpers/helpers_externals.cc +++ b/ccsrc/Helpers/helpers_externals.cc @@ -57,8 +57,8 @@ namespace LibHelpers__Functions { OCTETSTRING fx__enc__base64(const OCTETSTRING& p_to_encode) { loggers::get_instance().log_msg(">>> fx__enc__base64: ", p_to_encode); - const std::vector<unsigned char> to_encode(static_cast<const unsigned char*>(p_to_encode), static_cast<const unsigned char*>(p_to_encode) + p_to_encode.lengthof()); - std::vector<unsigned char> b64 = converter::get_instance().buffer_to_base64(to_encode); + const std::vector<uint8_t> to_encode(static_cast<const uint8_t*>(p_to_encode), static_cast<const uint8_t*>(p_to_encode) + p_to_encode.lengthof()); + std::vector<uint8_t> b64 = converter::get_instance().buffer_to_base64(to_encode); OCTETSTRING os(b64.size(), b64.data()); loggers::get_instance().log_msg("<<< fx__enc__base64: ", os); return os; @@ -72,18 +72,18 @@ namespace LibHelpers__Functions { OCTETSTRING fx__dec__base64(const OCTETSTRING& p_to_decode) { loggers::get_instance().log_msg(">>> fx__dec__base64: ", p_to_decode); - const std::vector<unsigned char> to_decode(static_cast<const unsigned char*>(p_to_decode), static_cast<const unsigned char*>(p_to_decode) + p_to_decode.lengthof()); - std::vector<unsigned char> b64 = converter::get_instance().base64_to_buffer(to_decode); + const std::vector<uint8_t> to_decode(static_cast<const uint8_t*>(p_to_decode), static_cast<const uint8_t*>(p_to_decode) + p_to_decode.lengthof()); + std::vector<uint8_t> b64 = converter::get_instance().base64_to_buffer(to_decode); OCTETSTRING os(b64.size(), b64.data()); loggers::get_instance().log_msg("<<< fx__dec__base64: ", os); return os; } - static unsigned char random_char() { + static uint8_t random_char() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, 255); - return static_cast<unsigned char>(dis(gen)); + return static_cast<uint8_t>(dis(gen)); } static std::string generate_hex(const unsigned int len) { diff --git a/ccsrc/Protocols/ETH/ethernet_layer.cc b/ccsrc/Protocols/ETH/ethernet_layer.cc index 0b6bff7..1261675 100644 --- a/ccsrc/Protocols/ETH/ethernet_layer.cc +++ b/ccsrc/Protocols/ETH/ethernet_layer.cc @@ -60,15 +60,15 @@ void ethernet_layer::receive_data(OCTETSTRING& p_data, params& p_params) { loggers::get_instance().log_msg(">>> ethernet_layer::receive_data: ", p_data); // Extract dest MAC Address - OCTETSTRING dst = OCTETSTRING(6, static_cast<const unsigned char *>(p_data)); + OCTETSTRING dst = OCTETSTRING(6, static_cast<const uint8_t *>(p_data)); //loggers::get_instance().log_msg("ethernet_layer::receive_data: dst: ", dst); // Extract source MAC Address - OCTETSTRING src = OCTETSTRING(6, 6 + static_cast<const unsigned char *>(p_data)); + OCTETSTRING src = OCTETSTRING(6, 6 + static_cast<const uint8_t *>(p_data)); //loggers::get_instance().log_msg("ethernet_layer::receive_data: src: ", src); // Extract ethertype - OCTETSTRING proto = OCTETSTRING(2, 2 + static_cast<const unsigned char *>(p_data)); + OCTETSTRING proto = OCTETSTRING(2, 2 + static_cast<const uint8_t *>(p_data)); //loggers::get_instance().log_msg("ethernet_layer::receive_data: proto: ", proto); - OCTETSTRING data = OCTETSTRING(p_data.lengthof() - 14, 14 + static_cast<const unsigned char *>(p_data)); + OCTETSTRING data = OCTETSTRING(p_data.lengthof() - 14, 14 + static_cast<const uint8_t *>(p_data)); // Update params CHARSTRING s = oct2str(dst); p_params.insert(std::pair<std::string, std::string>(params::mac_dst, std::string(static_cast<const char *>(s)))); diff --git a/ccsrc/Protocols/Http/http_codec.cc b/ccsrc/Protocols/Http/http_codec.cc index bd13632..133ca64 100644 --- a/ccsrc/Protocols/Http/http_codec.cc +++ b/ccsrc/Protocols/Http/http_codec.cc @@ -512,7 +512,7 @@ int http_codec::encode_body(const LibHttp__MessageBodyTypes::HttpMessageBody &p_ if (p_message_body.ischosen(LibHttp__MessageBodyTypes::HttpMessageBody::ALT_binary__body)) { const LibHttp__BinaryMessageBodyTypes::BinaryBody &binary_body = p_message_body.binary__body(); if (binary_body.ischosen(LibHttp__BinaryMessageBodyTypes::BinaryBody::ALT_raw)) { - p_encoding_buffer = OCTETSTRING(binary_body.raw().lengthof(), (unsigned char *)static_cast<const unsigned char *>(binary_body.raw())); + p_encoding_buffer = OCTETSTRING(binary_body.raw().lengthof(), (uint8_t *)static_cast<const uint8_t *>(binary_body.raw())); } else { encode_body_binary(binary_body, p_encoding_buffer, p_content_type); } @@ -526,14 +526,14 @@ int http_codec::encode_body(const LibHttp__MessageBodyTypes::HttpMessageBody &p_ } else if (p_message_body.ischosen(LibHttp__MessageBodyTypes::HttpMessageBody::ALT_xml__body)) { const LibHttp__XmlMessageBodyTypes::XmlBody &xml_body = p_message_body.xml__body(); if (xml_body.msg().ischosen(LibHttp__XmlMessageBodyTypes::XmlBodyMsg::ALT_raw)) { - p_encoding_buffer = OCTETSTRING(xml_body.msg().raw().lengthof(), (unsigned char *)static_cast<const char *>(xml_body.msg().raw())); + p_encoding_buffer = OCTETSTRING(xml_body.msg().raw().lengthof(), (uint8_t *)static_cast<const char *>(xml_body.msg().raw())); } else { encode_body_xml(xml_body, p_encoding_buffer, p_content_type); } } else if (p_message_body.ischosen(LibHttp__MessageBodyTypes::HttpMessageBody::ALT_html__body)) { - p_encoding_buffer = OCTETSTRING(p_message_body.html__body().lengthof(), (unsigned char *)static_cast<const char *>(p_message_body.html__body())); + p_encoding_buffer = OCTETSTRING(p_message_body.html__body().lengthof(), (uint8_t *)static_cast<const char *>(p_message_body.html__body())); } else if (p_message_body.ischosen(LibHttp__MessageBodyTypes::HttpMessageBody::ALT_text__body)) { - p_encoding_buffer = OCTETSTRING(p_message_body.text__body().lengthof(), (unsigned char *)static_cast<const char *>(p_message_body.text__body())); + p_encoding_buffer = OCTETSTRING(p_message_body.text__body().lengthof(), (uint8_t *)static_cast<const char *>(p_message_body.text__body())); } else { loggers::get_instance().warning("http_codec::encode_body: Failed to encode HTTP message body"); return -1; @@ -567,7 +567,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy loggers::get_instance().log("http_codec::decode_body: _dc.length=%d - body length=%d", _dc.length, s.lengthof()); OCTETSTRING body; if (_dc.length != 0) { - const unsigned char *p = static_cast<const unsigned char *>(s); + const uint8_t *p = static_cast<const uint8_t *>(s); if ((unsigned int)s.lengthof() <= _dc.length) { body = OCTETSTRING(s.lengthof(), p); } else { @@ -589,7 +589,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy } } loggers::get_instance().log("http_codec::decode_body: counter=%d", counter); - body = OCTETSTRING(body.lengthof() - counter, static_cast<const unsigned char*>(body)); + body = OCTETSTRING(body.lengthof() - counter, static_cast<const uint8_t*>(body)); */ if (_dc.chunked) { int counter = 0; @@ -605,9 +605,9 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy loggers::get_instance().log("http_codec::decode_body: Chunked(0): prev = %d, counter=%d / %d", prev, counter, body.lengthof()); if (counter < body.lengthof()) { int idx = counter - prev; - OCTETSTRING trunk(idx, static_cast<const unsigned char *>(body)); + OCTETSTRING trunk(idx, static_cast<const uint8_t *>(body)); loggers::get_instance().log_msg("http_codec::decode_body: trunk: ", trunk); - std::string str((const char *)static_cast<const unsigned char *>(trunk), idx); + std::string str((const char *)static_cast<const uint8_t *>(trunk), idx); loggers::get_instance().log("http_codec::decode_body: str: '%s'", str.c_str()); int len = std::stoi(str, nullptr, 16); // converter::get_instance().string_to_int(str); loggers::get_instance().log("http_codec::decode_body: Chunk len: %d", len); @@ -616,7 +616,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy } // End of 'while' statement if (counter < body.lengthof()) { loggers::get_instance().log("http_codec::decode_body: Chunked (1): prev = %d, counter=%d / %d", prev, counter, body.lengthof()); - os += OCTETSTRING(len, counter + static_cast<const unsigned char *>(body)); + os += OCTETSTRING(len, counter + static_cast<const uint8_t *>(body)); loggers::get_instance().log_msg("http_codec::decode_body: os=", os); counter += len; loggers::get_instance().log("http_codec::decode_body: Chunked: %02x %02x %02x", body[counter].get_octet(), body[counter + 1].get_octet(), @@ -635,7 +635,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy } // Check if HTTP message body contains binary characters for (int i = 0; i < body.lengthof(); i++) { - unsigned char c = body[i].get_octet(); + uint8_t c = body[i].get_octet(); if (!std::isprint(c) && !std::isspace(c) && !std::ispunct(c)) { loggers::get_instance().log("http_codec::decode_body: Byte #%d is not printable: 0x%02x", i, body[i].get_octet()); _dc.is_binary = 0x01; @@ -651,7 +651,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy } else { // Convert into string params p; - p["decode_str"] = std::string(static_cast<const unsigned char *>(body), body.lengthof() + static_cast<const unsigned char *>(body)); + p["decode_str"] = std::string(static_cast<const uint8_t *>(body), body.lengthof() + static_cast<const uint8_t *>(body)); loggers::get_instance().log("http_codec::decode_body: decode_str: '%s'", p["decode_str"].c_str()); // Try to identify xml if (p["decode_str"].find("<?xml version=") != std::string::npos) { @@ -672,7 +672,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy } else { loggers::get_instance().log("http_codec::decode_body: Use textBody as default"); LibHttp__MessageBodyTypes::TextBody text_body; - message_body.text__body() = CHARSTRING(body.lengthof(), (char *)static_cast<const unsigned char *>(body)); + message_body.text__body() = CHARSTRING(body.lengthof(), (char *)static_cast<const uint8_t *>(body)); } } @@ -681,7 +681,7 @@ int http_codec::decode_body(TTCN_Buffer &decoding_buffer, LibHttp__MessageBodyTy int http_codec::get_line(TTCN_Buffer &buffer, CHARSTRING &to, const bool concatenate_header_lines) { unsigned int i = 0; - const unsigned char *cc_to = buffer.get_read_data(); + const uint8_t *cc_to = buffer.get_read_data(); // Sanity checks if (buffer.get_read_len() == 0) { @@ -755,6 +755,6 @@ void http_codec::set_payload_codecs(const std::string& p_codecs) { } bool http_codec::decode_body_html(const OCTETSTRING &p_data, CHARSTRING &p_html_body, const std::string& p_content_type, params* p_params) { - p_html_body = CHARSTRING(p_data.lengthof(), (char*)static_cast<const unsigned char*>(p_data)); + p_html_body = CHARSTRING(p_data.lengthof(), (char*)static_cast<const uint8_t*>(p_data)); return true; } diff --git a/ccsrc/Protocols/Http/http_codec.hh b/ccsrc/Protocols/Http/http_codec.hh index 37f313c..2e675c9 100644 --- a/ccsrc/Protocols/Http/http_codec.hh +++ b/ccsrc/Protocols/Http/http_codec.hh @@ -33,7 +33,7 @@ namespace LibHttp__JsonMessageBodyTypes { struct encoding_context { unsigned int length; - unsigned char is_content_length_present; + uint8_t is_content_length_present; encoding_context() { reset(); }; void reset() { @@ -44,7 +44,7 @@ struct encoding_context { struct decoding_context { unsigned int length; - unsigned char is_binary; + uint8_t is_binary; bool chunked; decoding_context() { reset(); }; diff --git a/ccsrc/Protocols/Pcap/pcap_cygwin_layer.cc b/ccsrc/Protocols/Pcap/pcap_cygwin_layer.cc index dca0cfa..1478b64 100644 --- a/ccsrc/Protocols/Pcap/pcap_cygwin_layer.cc +++ b/ccsrc/Protocols/Pcap/pcap_cygwin_layer.cc @@ -33,14 +33,14 @@ extern "C" int pcap_oid_get_request(pcap_t *p, bpf_u_int32 oid, void *data, size static const char *_hexDigits = "0123456789ABCDEF"; static char * _bin2hex(char *hex, size_t hlen, const char *bin, size_t blen) { - const unsigned char *b, *e; + const uint8_t *b, *e; char * s; // sanity check if (hlen >= 0 && hlen < blen * 2) return NULL; - b = (const unsigned char *)bin; + b = (const uint8_t *)bin; e = b + blen - 1; s = hex + blen * 2; if (s < hex + hlen) @@ -191,7 +191,7 @@ void *pcap_layer::run(void *p_this) { void *pcap_layer::thread() { pcap_o_pkthdr *pkt_header; const u_char * pkt_data; - unsigned char pkt_count = 0; + uint8_t pkt_count = 0; // loggers::get_instance().log(">>> pcap_layer::run"); @@ -237,7 +237,7 @@ void *pcap_layer::thread() { void pcap_layer::send_data(OCTETSTRING &data, params ¶ms) { loggers::get_instance().log_msg(">>> pcap_layer::send_data: ", data); - if (pcap_sendpacket(_device, static_cast<const unsigned char *>(data), data.lengthof()) == -1) { + if (pcap_sendpacket(_device, static_cast<const uint8_t *>(data), data.lengthof()) == -1) { loggers::get_instance().error("pcap_layer::send_data: Failed to send packet: '%s'", pcap_geterr(_device)); } } diff --git a/ccsrc/Protocols/Pcap/pcap_layer.cc b/ccsrc/Protocols/Pcap/pcap_layer.cc index 94f7e8b..6e258d0 100644 --- a/ccsrc/Protocols/Pcap/pcap_layer.cc +++ b/ccsrc/Protocols/Pcap/pcap_layer.cc @@ -211,7 +211,7 @@ void pcap_layer::send_data(OCTETSTRING& data, params& params) { loggers::get_instance().log_msg(">>> pcap_layer::send_data: ", data); if (_pcap_h != -1) { // Check if offline mode is used - if (pcap_sendpacket(_device, static_cast<const unsigned char *>(data), data.lengthof()) == -1) { + if (pcap_sendpacket(_device, static_cast<const uint8_t *>(data), data.lengthof()) == -1) { loggers::get_instance().error("pcap_layer::send_data: Failed to send packet: '%s'", pcap_geterr(_device)); } } else if (_sent_file != NULL) { @@ -222,7 +222,7 @@ void pcap_layer::send_data(OCTETSTRING& data, params& params) { hdr.ts.tv_usec = (ms.count() % 1000) * 1000; hdr.caplen = data.lengthof(); hdr.len = hdr.caplen; - pcap_dump((u_char *)_sent_file, &hdr, static_cast<const unsigned char *>(data)); + pcap_dump((u_char *)_sent_file, &hdr, static_cast<const uint8_t *>(data)); } else { loggers::get_instance().log("pcap_layer::send_data: Offline mode, operation was skipped"); } diff --git a/ccsrc/Protocols/Pcap/pcap_linux_layer.cc b/ccsrc/Protocols/Pcap/pcap_linux_layer.cc index 27ce633..ff44879 100644 --- a/ccsrc/Protocols/Pcap/pcap_linux_layer.cc +++ b/ccsrc/Protocols/Pcap/pcap_linux_layer.cc @@ -17,14 +17,14 @@ static const char *_hexDigits = "0123456789ABCDEF"; static char * _bin2hex(char *hex, size_t hlen, const char *bin, size_t blen) { - const unsigned char *b, *e; + const uint8_t *b, *e; char * s; // sanity check if (hlen >= 0 && hlen < blen * 2) return NULL; - b = (const unsigned char *)bin; + b = (const uint8_t *)bin; e = b + blen - 1; s = hex + blen * 2; if (s < hex + hlen) @@ -36,6 +36,46 @@ static char * _bin2hex(char *hex, size_t hlen, const char *bin, size_t blen return hex + blen * 2; } +char* _hex2bin(char* bin, size_t blen, const char* hex, size_t hlen, const char * charstoskip) +{ + // check + const char * h = hex; + const char * e = hex+hlen; + char * b = bin; + int n = 0; + while (h<e){ + char ch = *h++; + if (strchr(charstoskip, ch)) continue; + if (ch >= '0' && ch <= '9') continue; + if (ch >= 'A' && ch <= 'F') continue; + if (ch >= 'a' && ch <= 'f') continue; + return NULL; + } + h = hex; + while (h < e){ + char ch = *h++; + if (ch >= '0' && ch <= '9') ch -= '0'; + else if (ch >= 'A' && ch <= 'F') ch -= 'A' - 0x0A; + else if (ch >= 'a' && ch <= 'f') ch -= 'a' - 0x0A; + else continue; + if (!n){ + *b = ch; + } + else{ + char ch1 = *b; + *b++ = (ch1 << 4) | ch; + } + n = !n; + } + if (n){ + char ch1 = *b; + *b++ = (ch1 << 4); + n = 0; + } + return b; +} + + pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) : layer(p_type), PORT(p_type.c_str()), _params(), _device(NULL), _pcap_h(-1), _time_key("pcap_layer::Handle_Fd_Event_Readable") { char error_buffer[PCAP_ERRBUF_SIZE]; @@ -61,6 +101,7 @@ pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) loggers::get_instance().log("pcap_layer::pcap_layer: Device %s Network address: %d", nic.c_str(), net); } } + // Open the device in promiscuous mode _device = pcap_open_live(nic.c_str(), 65536 /*64*1024*/, 1, 100, error_buffer); // TODO Replace hard coded values by pcap_layer::<constants> if (_device == NULL) { @@ -127,17 +168,16 @@ pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) // Log final PCAP filter loggers::get_instance().user("pcap_layer::pcap_layer: Filter: '%s'", filter.c_str()); - { - struct bpf_program f = {0}; - if (pcap_compile(_device, &f, filter.c_str(), 1, PCAP_NETMASK_UNKNOWN) != 0) { - loggers::get_instance().error("pcap_layer::pcap_layer: Failed to compile PCAP filter"); - } else { - if (pcap_setfilter(_device, &f) != 0) { - loggers::get_instance().error("pcap_layer::pcap_layer: Failed to set PCAP filter"); - } + struct bpf_program f = {0}; + int res; + if ((res = pcap_compile(_device, &f, filter.c_str(), 1, PCAP_NETMASK_UNKNOWN)) != 0) { + loggers::get_instance().error("pcap_layer::pcap_layer: Failed to compile PCAP filter: %s", pcap_strerror(res)); + } else { + if (pcap_setfilter(_device, &f) != 0) { + loggers::get_instance().error("pcap_layer::pcap_layer: Failed to set PCAP filter"); } - pcap_freecode(&f); } + pcap_freecode(&f); // Pass the device file handler to the polling procedure Handler_Add_Fd_Read(_pcap_h); @@ -154,7 +194,7 @@ pcap_layer::~pcap_layer() { void pcap_layer::send_data(OCTETSTRING &data, params ¶ms) { loggers::get_instance().log_msg(">>> pcap_layer::send_data: ", data); - if (pcap_sendpacket(_device, static_cast<const unsigned char *>(data), data.lengthof()) == -1) { + if (pcap_sendpacket(_device, static_cast<const uint8_t *>(data), data.lengthof()) == -1) { loggers::get_instance().error("pcap_layer::send_data: Failed to send packet: '%s'", pcap_geterr(_device)); } } diff --git a/ccsrc/Protocols/Pcap/pcap_linux_layer.hh b/ccsrc/Protocols/Pcap/pcap_linux_layer.hh index 90c45dd..d3e96b0 100644 --- a/ccsrc/Protocols/Pcap/pcap_linux_layer.hh +++ b/ccsrc/Protocols/Pcap/pcap_linux_layer.hh @@ -27,7 +27,6 @@ class pcap_layer : public layer, public PORT { int _pcap_h; //! PCAP instance handle pcap_dumper_t *_sent_file; //! Write file handle to save sent packet, used in file mode std::string _time_key; //! \todo - public: //! \publicsection /*! * \brief Specialised constructor diff --git a/ccsrc/Protocols/Pcap/pcap_offline_layer.cc b/ccsrc/Protocols/Pcap/pcap_offline_layer.cc index 0f8808c..e400f53 100644 --- a/ccsrc/Protocols/Pcap/pcap_offline_layer.cc +++ b/ccsrc/Protocols/Pcap/pcap_offline_layer.cc @@ -135,7 +135,7 @@ void *pcap_offline_layer::thread() { pcap_o_pkthdr *pkt_header; pcap_o_pkthdr lh; const u_char * pkt_data; - unsigned char pkt_count = 0; + uint8_t pkt_count = 0; // loggers::get_instance().log(">>> pcap_offline_layer::run"); diff --git a/ccsrc/Protocols/Tcp/tcp_layer.cc b/ccsrc/Protocols/Tcp/tcp_layer.cc index 632ec3b..5a15ffd 100644 --- a/ccsrc/Protocols/Tcp/tcp_layer.cc +++ b/ccsrc/Protocols/Tcp/tcp_layer.cc @@ -189,7 +189,7 @@ void tcp_layer::send_data(OCTETSTRING& data, params& params) { loggers::get_instance().log("tcp_layer::send_data: Re-establish connection: '%s'/%s", _params[params::server].c_str(), _params[params::port].c_str()); open_client_connection(_params[params::server].c_str(), _params[params::port].c_str(), NULL, NULL); } - send_outgoing(static_cast<const unsigned char*>(data), data.lengthof(), _client_id); + send_outgoing(static_cast<const uint8_t*>(data), data.lengthof(), _client_id); } void tcp_layer::receive_data(OCTETSTRING& data, params& params) { @@ -198,7 +198,7 @@ void tcp_layer::receive_data(OCTETSTRING& data, params& params) { receive_to_all_layers(data, params); } -void tcp_layer::message_incoming(const unsigned char* message_buffer, int length, int client_id) { +void tcp_layer::message_incoming(const uint8_t* message_buffer, int length, int client_id) { loggers::get_instance().log(">>> tcp_layer::message_incoming"); loggers::get_instance().log_to_hexa("tcp_layer::message_incoming: ", message_buffer, length); @@ -234,7 +234,7 @@ bool tcp_layer::add_user_data(int p_client_id) return SSL_Socket::add_user_data(p_client_id); } -int tcp_layer::send_message_on_fd(int p_client_id, const unsigned char * message_buffer, int length_of_message) +int tcp_layer::send_message_on_fd(int p_client_id, const uint8_t * message_buffer, int length_of_message) { loggers::get_instance().log(">>> tcp_layer::send_message_on_fd: %d", p_client_id); @@ -247,7 +247,7 @@ int tcp_layer::send_message_on_fd(int p_client_id, const unsigned char * message return Abstract_Socket::send_message_on_fd(p_client_id, message_buffer, length_of_message); } -int tcp_layer::send_message_on_nonblocking_fd(int p_client_id, const unsigned char * message_buffer, int length_of_message) +int tcp_layer::send_message_on_nonblocking_fd(int p_client_id, const uint8_t * message_buffer, int length_of_message) { loggers::get_instance().log(">>> tcp_layer::send_message_on_nonblocking_fd: %d", p_client_id); diff --git a/ccsrc/Protocols/Tcp/tcp_layer.hh b/ccsrc/Protocols/Tcp/tcp_layer.hh index 8db788c..d13fa57 100644 --- a/ccsrc/Protocols/Tcp/tcp_layer.hh +++ b/ccsrc/Protocols/Tcp/tcp_layer.hh @@ -69,13 +69,13 @@ public: //! \publicsection /*! * \virtual - * \fn void message_incoming(const unsigned char* message_buffer, int length, int client_id = -1); + * \fn void message_incoming(const uint8_t* message_buffer, int length, int client_id = -1); * \brief Receive bytes formated data from the lower layers * \param[in] p_buffer The bytes formated data received * \param[in] p_length The number of bytes received * \param[in] p_client_id The connection identifier.Default: -1 */ - virtual void message_incoming(const unsigned char *p_buffer, int p_length, int p_client_id = -1); + virtual void message_incoming(const uint8_t *p_buffer, int p_length, int p_client_id = -1); protected: //! \protectedsection void init(); @@ -97,8 +97,8 @@ protected: //! \protectedsection void client_connection_opened(int p_client_id); bool add_user_data(int p_client_id); - int send_message_on_fd(int p_client_id, const unsigned char *message_buffer, int length_of_message); - int send_message_on_nonblocking_fd(int client_id, const unsigned char *message_buffer, int length_of_message); + int send_message_on_fd(int p_client_id, const uint8_t *message_buffer, int length_of_message); + int send_message_on_nonblocking_fd(int client_id, const uint8_t *message_buffer, int length_of_message); int receive_message_on_fd(int p_client_id); void peer_disconnected(int p_client_id); diff --git a/ccsrc/Protocols/UDP/udp_layer.cc b/ccsrc/Protocols/UDP/udp_layer.cc index e17c530..ff0c3d1 100644 --- a/ccsrc/Protocols/UDP/udp_layer.cc +++ b/ccsrc/Protocols/UDP/udp_layer.cc @@ -114,7 +114,7 @@ void udp_layer::send_data(OCTETSTRING &data, params ¶ms) { loggers::get_instance().log(">>> udp_layer::send_data: %d", _fd); loggers::get_instance().log_msg(">>> udp_layer::send_data: ", data); - int result = ::sendto(_fd, (const char *)static_cast<const unsigned char *>(data), data.lengthof(), 0, (struct sockaddr *)&_daddr, sizeof(_daddr)); + int result = ::sendto(_fd, (const char *)static_cast<const uint8_t *>(data), data.lengthof(), 0, (struct sockaddr *)&_daddr, sizeof(_daddr)); loggers::get_instance().log("udp_layer::send_data: #bytes sent: %d to %s:%d", result, ::inet_ntoa(_daddr.sin_addr), ntohs(_daddr.sin_port)); } @@ -127,16 +127,16 @@ void udp_layer::receive_data(OCTETSTRING& p_data, params& p_params) { void udp_layer::Handle_Fd_Event_Readable(int fd) { loggers::get_instance().log(">>> udp_layer::Handle_Fd_Event_Readable: %d", fd); - unsigned char buffer[3072] = {0}; + uint8_t buffer[3072] = {0}; struct sockaddr_in from = {0}; socklen_t len = sizeof(struct sockaddr_in); // Length of sender's address params params; - std::vector<unsigned char> acc; + std::vector<uint8_t> acc; int result = ::recvfrom(fd, buffer, 3072, 0, (struct sockaddr *)&from, &len); loggers::get_instance().log("udp_layer::Handle_Fd_Event_Readable: src_port = %s:%d, payload length = %d, errno = %d", ::inet_ntoa(from.sin_addr), ntohs(from.sin_port), result, errno); while ((result == 3072) && (errno == 0)) { - std::copy((unsigned char *)buffer, (unsigned char *)((unsigned char *)buffer + result), std::back_inserter(acc)); + std::copy((uint8_t *)buffer, (uint8_t *)((uint8_t *)buffer + result), std::back_inserter(acc)); result = ::recvfrom(fd, buffer, 3072, 0, (struct sockaddr *)&from, &len); loggers::get_instance().log("udp_layer::Handle_Fd_Event_Readable: src_port = %s:%d, payload length = %d, errno = %d", ::inet_ntoa(from.sin_addr), ntohs(from.sin_port), result, errno); @@ -145,7 +145,7 @@ void udp_layer::Handle_Fd_Event_Readable(int fd) { loggers::get_instance().warning("udp_layer::Handle_Fd_Event_Readable: Failed to read data, discard them: errno=%d", errno); return; } else { - std::copy((unsigned char *)buffer, (unsigned char *)((unsigned char *)buffer + result), std::back_inserter(acc)); + std::copy((uint8_t *)buffer, (uint8_t *)((uint8_t *)buffer + result), std::back_inserter(acc)); if (_reuse_incoming_source_adddress) { // Reuse the incoming address/port for sending memcpy((void *)&_daddr, (const void *)&from, sizeof(struct sockaddr_in)); loggers::get_instance().log("udp_layer::Handle_Fd_Event_Readable: New _daddr: '%s':%d", ::inet_ntoa(_daddr.sin_addr), ntohs(_daddr.sin_port)); diff --git a/ccsrc/loggers/loggers.hh b/ccsrc/loggers/loggers.hh index 035f2d1..a576548 100644 --- a/ccsrc/loggers/loggers.hh +++ b/ccsrc/loggers/loggers.hh @@ -84,13 +84,13 @@ public: //! \publicsection */ inline void log_to_hexa(const char *p_prompt, const OCTETSTRING &msg); /*! - * \fn void log_to_hexa(const char *p_prompt, const unsigned char* msg, const size_t msg_size); + * \fn void log_to_hexa(const char *p_prompt, const uint8_t* msg, const size_t msg_size); * \brief Hexa dump of the provided buffer * \param[in] p_prompt Label of the log to be produced * \param[in] msg The buffer to dump * \inline */ - inline void log_to_hexa(const char *p_prompt, const unsigned char *msg, const size_t msg_size); + inline void log_to_hexa(const char *p_prompt, const uint8_t *msg, const size_t msg_size); /*! * \fn void log_msg(const char *p_prompt, const Base_Type& p_type); * \brief Debug log of TITAN data structures @@ -181,13 +181,13 @@ void loggers::log_to_hexa(const char *p_prompt, const OCTETSTRING &msg) { TTCN_Logger::log_event("Size: %d,\nMsg: ", msg.lengthof()); for (int i = 0; i < msg.lengthof(); i++) { - TTCN_Logger::log_event(" %02x", ((const unsigned char *)msg)[i]); + TTCN_Logger::log_event(" %02x", ((const uint8_t *)msg)[i]); } TTCN_Logger::log_event("\n"); TTCN_Logger::end_event(); } -void loggers::log_to_hexa(const char *p_prompt, const unsigned char *msg, const size_t msg_size) { +void loggers::log_to_hexa(const char *p_prompt, const uint8_t *msg, const size_t msg_size) { TTCN_Logger::begin_event(TTCN_Logger::DEBUG_UNQUALIFIED); TTCN_Logger::log_event_str(p_prompt); for (size_t i = 0; i < msg_size; i++) { diff --git a/ccsrc/security/include/certs_cache.hh b/ccsrc/security/include/certs_cache.hh index 5e55c71..97889dc 100644 --- a/ccsrc/security/include/certs_cache.hh +++ b/ccsrc/security/include/certs_cache.hh @@ -87,7 +87,7 @@ public: /*! \publicsection */ const std::string cert_to_string(const std::string& p_certificate_id); // For debug purpose. To be removed - int publickey_to_string(const EVP_PKEY* p_public_kep, std::vector<unsigned char>& p_buffer); // For debug purpose. To be removed + int publickey_to_string(const EVP_PKEY* p_public_kep, std::vector<uint8_t>& p_buffer); // For debug purpose. To be removed private: /*! \privatesection */ diff --git a/ccsrc/security/include/hmac.hh b/ccsrc/security/include/hmac.hh index cbd1ccd..d69d6a5 100644 --- a/ccsrc/security/include/hmac.hh +++ b/ccsrc/security/include/hmac.hh @@ -19,7 +19,7 @@ class OCTETSTRING; //! TITAN forward declaration /*! * \enum Supported hash algorithms */ -enum class hash_algorithms : unsigned char { +enum class hash_algorithms : uint8_t { sha_256, /*!< HMAC with SHA-256 */ sha_384 /*!< HMAC with SHA-384 */ }; // End of class hash_algorithms @@ -58,12 +58,12 @@ public: int generate(const OCTETSTRING p_buffer, const OCTETSTRING p_secret_key, OCTETSTRING &p_hmac); // TODO Use reference & /*! - * \fn int generate(const unsigned char* p_buffer, const size_t p_buffer_length, const unsigned char* p_secret_key, const size_t p_secret_key_length, + * \fn int generate(const uint8_t* p_buffer, const size_t p_buffer_length, const uint8_t* p_secret_key, const size_t p_secret_key_length, * OCTETSTRING& p_hmac); \brief Generate the HMAC of data using a secret key \param[in] p_buffer The data to be hashed \param[in] p_buffer_length The size of * the data \param[in] p_secret_key The secret key to be used to generate the HMAC \param[in] p_secret_key_length The size of the secret key \param[out] * p_hmac The HMAC value based of the provided data \return 0 on success, -1 otherwise */ - int generate(const unsigned char *p_buffer, const size_t p_buffer_length, const unsigned char *p_secret_key, const size_t p_secret_key_length, + int generate(const uint8_t *p_buffer, const size_t p_buffer_length, const uint8_t *p_secret_key, const size_t p_secret_key_length, OCTETSTRING &p_hmac); }; // End of class hmac diff --git a/ccsrc/security/include/sha1.hh b/ccsrc/security/include/sha1.hh index e053544..1fa6ccf 100644 --- a/ccsrc/security/include/sha1.hh +++ b/ccsrc/security/include/sha1.hh @@ -42,14 +42,14 @@ public: int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash); /*! - * \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash); + * \fn int generate(const uint8_t* p_buffer, OCTETSTRING& p_hash); * \brief Receive bytes formated data from the lower layers * \param[in] p_buffer The data used to generate the SHA-1 hash * \param[in] The length of the data buffer * \param[out] p_hash The SHA-1 hash value based of the provided data * \return 0 on success, -1 otherwise */ - int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash); + int generate(const uint8_t *p_buffer, const size_t p_length, OCTETSTRING &p_hash); /*! * \fn const OCTETSTRING get_sha1_empty_string() const; @@ -57,7 +57,7 @@ public: * \return The SHA-1 of an empty string */ inline const OCTETSTRING get_sha1_empty_string() const { - static unsigned char sha1_empty_string[] = { + static uint8_t sha1_empty_string[] = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09}; //! SHA-1 of an empty string return OCTETSTRING(20, sha1_empty_string); diff --git a/ccsrc/security/include/sha256.hh b/ccsrc/security/include/sha256.hh index 4973a2e..d8e6251 100644 --- a/ccsrc/security/include/sha256.hh +++ b/ccsrc/security/include/sha256.hh @@ -42,14 +42,14 @@ public: int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash); /*! - * \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash); + * \fn int generate(const uint8_t* p_buffer, OCTETSTRING& p_hash); * \brief Receive bytes formated data from the lower layers * \param[in] p_buffer The data used to generate the SHA-256 hash * \param[in] The length of the data buffer * \param[out] p_hash The SHA-256 hash value based of the provided data * \return 0 on success, -1 otherwise */ - int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash); + int generate(const uint8_t *p_buffer, const size_t p_length, OCTETSTRING &p_hash); /*! * \fn const OCTETSTRING get_sha256_empty_string() const; @@ -57,7 +57,7 @@ public: * \return The SHA-256 of an empty string */ inline const OCTETSTRING get_sha256_empty_string() const { - static unsigned char sha256_empty_string[] = { + static uint8_t sha256_empty_string[] = { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}; //! SHA-256 of an empty string return OCTETSTRING(32, sha256_empty_string); diff --git a/ccsrc/security/include/sha384.hh b/ccsrc/security/include/sha384.hh index 67f2d19..dbb77d4 100644 --- a/ccsrc/security/include/sha384.hh +++ b/ccsrc/security/include/sha384.hh @@ -49,7 +49,7 @@ public: //! \publicsection * \param[out] p_hash The SHA-384 hash value based of the provided data * \return 0 on success, -1 otherwise */ - int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash); + int generate(const uint8_t *p_buffer, const size_t p_length, OCTETSTRING &p_hash); /*! * \fn const OCTETSTRING get_sha384_empty_string() const; diff --git a/ccsrc/security/src/certs_cache.cc b/ccsrc/security/src/certs_cache.cc index 1d7bcf7..f801962 100644 --- a/ccsrc/security/src/certs_cache.cc +++ b/ccsrc/security/src/certs_cache.cc @@ -243,10 +243,10 @@ const std::string certs_cache::cert_to_string(const std::string& p_certificate_i return s; } -int certs_cache::publickey_to_string(const EVP_PKEY* p_public_kep, std::vector<unsigned char>& p_buffer) { +int certs_cache::publickey_to_string(const EVP_PKEY* p_public_kep, std::vector<uint8_t>& p_buffer) { loggers::get_instance().log(">>> certs_cache::publickey_to_string: '%p'", p_public_kep); - unsigned char* buffer = nullptr; + uint8_t* buffer = nullptr; int ret = ::i2d_PublicKey((EVP_PKEY*)p_public_kep, &buffer); loggers::get_instance().log("certs_cache::publickey_to_string: ret: '%d'", ret); if (ret < 0) { @@ -255,7 +255,7 @@ int certs_cache::publickey_to_string(const EVP_PKEY* p_public_kep, std::vector<u return -1; } - p_buffer.assign((unsigned char*)buffer, (unsigned char*)(buffer + ret)); + p_buffer.assign((uint8_t*)buffer, (uint8_t*)(buffer + ret)); ::OPENSSL_free(buffer); loggers::get_instance().log("certs_cache::publickey_to_string: p_buffer len: '%d'", p_buffer.size()); loggers::get_instance().log("certs_cache::publickey_to_string: dump: '%s'", converter::get_instance().bytes_to_hexa(p_buffer).c_str()); diff --git a/ccsrc/security/src/certs_loader.cc b/ccsrc/security/src/certs_loader.cc index 306e6a5..f6fa0a1 100644 --- a/ccsrc/security/src/certs_loader.cc +++ b/ccsrc/security/src/certs_loader.cc @@ -62,7 +62,7 @@ int certs_loader::get_certificate_id(const std::string& p_certificate_name, std: // Build the certificate identifier sha1 s; - const OCTETSTRING buffer = OCTETSTRING(p_certificate_name.length(), (unsigned char*)p_certificate_name.c_str()); + const OCTETSTRING buffer = OCTETSTRING(p_certificate_name.length(), (uint8_t*)p_certificate_name.c_str()); OCTETSTRING hash; if (s.generate(buffer, hash) != 0) { loggers::get_instance().warning("certs_cache::load_certificate: Failed to build the certificate identifier"); diff --git a/ccsrc/security/src/hmac.cc b/ccsrc/security/src/hmac.cc index ab5c248..5431792 100644 --- a/ccsrc/security/src/hmac.cc +++ b/ccsrc/security/src/hmac.cc @@ -20,11 +20,11 @@ int hmac::generate(const OCTETSTRING p_buffer, const OCTETSTRING p_secret_key, O return -1; } - return generate(static_cast<const unsigned char *>(p_buffer), p_buffer.lengthof(), static_cast<const unsigned char *>(p_secret_key), p_secret_key.lengthof(), + return generate(static_cast<const uint8_t *>(p_buffer), p_buffer.lengthof(), static_cast<const uint8_t *>(p_secret_key), p_secret_key.lengthof(), p_hmac); } -int hmac::generate(const unsigned char *p_buffer, const size_t p_buffer_length, const unsigned char *p_secret_key, const size_t p_secret_key_length, +int hmac::generate(const uint8_t *p_buffer, const size_t p_buffer_length, const uint8_t *p_secret_key, const size_t p_secret_key_length, OCTETSTRING &p_hmac) { // Sanity check if ((p_buffer == nullptr) || (p_secret_key == nullptr)) { @@ -43,11 +43,11 @@ int hmac::generate(const unsigned char *p_buffer, const size_t p_buffer_length, // Compute the hash value ::HMAC_Update(_ctx, p_buffer, p_buffer_length); unsigned int length = p_hmac.lengthof(); - ::HMAC_Final(_ctx, (unsigned char *)static_cast<const unsigned char *>(p_hmac), &length); - loggers::get_instance().log_to_hexa("hmac::generate: ", (unsigned char *)static_cast<const unsigned char *>(p_hmac), length); + ::HMAC_Final(_ctx, (uint8_t *)static_cast<const uint8_t *>(p_hmac), &length); + loggers::get_instance().log_to_hexa("hmac::generate: ", (uint8_t *)static_cast<const uint8_t *>(p_hmac), length); // Resize the hmac if (_hash_algorithms == hash_algorithms::sha_256) { - p_hmac = OCTETSTRING(16, static_cast<const unsigned char *>(p_hmac)); + p_hmac = OCTETSTRING(16, static_cast<const uint8_t *>(p_hmac)); } // FIXME Check length for the other hash algorithm return 0; diff --git a/ccsrc/security/src/security_externals.cc b/ccsrc/security/src/security_externals.cc index 641c9f5..ce6312a 100644 --- a/ccsrc/security/src/security_externals.cc +++ b/ccsrc/security/src/security_externals.cc @@ -191,7 +191,7 @@ INTEGER LibSecurity__Signature::fx__dec__xmldsig(BITSTRING& bs, http__www__w3__o TTCN_EncDec::clear_error(); TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT); - TTCN_Buffer decoding_buffer(OCTETSTRING(str.length(), (const unsigned char*)str.c_str())); + TTCN_Buffer decoding_buffer(OCTETSTRING(str.length(), (const uint8_t*)str.c_str())); s.decode(http__www__w3__org__2000__09__xmldsig::Signature_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED); diff --git a/ccsrc/security/src/securty_services.cc b/ccsrc/security/src/securty_services.cc index eb459f6..2e256e7 100644 --- a/ccsrc/security/src/securty_services.cc +++ b/ccsrc/security/src/securty_services.cc @@ -49,7 +49,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET // Transform and canonicalize the message // Transform: Remove all CR, LS, TAB and SPACE outside of the tags std::string transformed; - xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const unsigned char*>(p_encoded_message)), p_encoded_message.lengthof()), transformed); + xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const uint8_t*>(p_encoded_message)), p_encoded_message.lengthof()), transformed); loggers::get_instance().log("security_services::do_sign: p_pull_request_transormed: '%s'", transformed.c_str()); // Canonicalization std::string canonicalized; @@ -62,7 +62,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET // Transform and canonicalize the empty signature // Transform: Remove all CR, LS, TAB and SPACE outside of the tags transformed.clear(); - xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const unsigned char*>(p_empty_signature)), p_empty_signature.lengthof()), transformed); + xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const uint8_t*>(p_empty_signature)), p_empty_signature.lengthof()), transformed); loggers::get_instance().log("security_services::do_sign: p_empty_signature transormed: '%s'", transformed.c_str()); // Canonicalization canonicalized.clear(); @@ -98,7 +98,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET } } loggers::get_instance().log_msg("security_services::do_sign: digest: ", p_digest); - std::vector<unsigned char> to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const unsigned char*>(p_digest), p_digest.lengthof() + static_cast<const unsigned char*>(p_digest))); + std::vector<uint8_t> to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const uint8_t*>(p_digest), p_digest.lengthof() + static_cast<const uint8_t*>(p_digest))); p_digest = OCTETSTRING(to64.size(), to64.data()); loggers::get_instance().log_msg("security_services::do_sign: digest (64): ", p_digest); @@ -133,7 +133,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET canonicalized = canonicalized.substr(0, i + 13) + std::string(to64.cbegin(), to64.cend()) + canonicalized.substr(i + 13, canonicalized.length() - i - 13); OCTETSTRING to_be_signed(char2oct(CHARSTRING(canonicalized.c_str()))); loggers::get_instance().log_msg("security_services::do_sign: to_be_signed: ", to_be_signed); - to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const unsigned char*>(to_be_signed), to_be_signed.lengthof() + static_cast<const unsigned char*>(to_be_signed))); + to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const uint8_t*>(to_be_signed), to_be_signed.lengthof() + static_cast<const uint8_t*>(to_be_signed))); loggers::get_instance().log("security_services::do_sign: Data to be signed/verified: '%s'", converter::get_instance().bytes_to_string(to64).c_str()); // Retrieve the private key @@ -156,7 +156,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET ::EVP_MD_CTX_free(ctx); return -1; } - if (::EVP_DigestSignUpdate(ctx, (const unsigned char*)canonicalized.c_str(), canonicalized.length()) != 1) { + if (::EVP_DigestSignUpdate(ctx, (const uint8_t*)canonicalized.c_str(), canonicalized.length()) != 1) { loggers::get_instance().warning("security_services::do_sign: EVP_DigestSignUpdate failed, error 0x%lx", ::ERR_get_error()); ::EVP_MD_CTX_free(ctx); return -1; @@ -174,7 +174,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET return -1; } loggers::get_instance().log("security_services::do_sign: signature_length: %d", signature_length); - std::vector<unsigned char> s(signature_length, 0x00); + std::vector<uint8_t> s(signature_length, 0x00); if (::EVP_DigestSignFinal(ctx, s.data(), &signature_length) != 1) { loggers::get_instance().warning("security_services::do_sign: EVP_DigestSignUpdate failed, error 0x%lx", ::ERR_get_error()); ::EVP_MD_CTX_free(ctx); @@ -191,11 +191,11 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET // return -1; // } // loggers::get_instance().log("security_services::do_sign: public_key: '%p'", public_key); - // std::vector<unsigned char> buffer; + // std::vector<uint8_t> buffer; // _certs_db->publickey_to_string(public_key, buffer); // ctx = ::EVP_MD_CTX_new(); // if (!::EVP_DigestVerifyInit(ctx, NULL, sign_digest, NULL, (EVP_PKEY*)public_key) || - // !::EVP_DigestVerifyUpdate(ctx, (const unsigned char*)canonicalized.c_str(), canonicalized.length()) || + // !::EVP_DigestVerifyUpdate(ctx, (const uint8_t*)canonicalized.c_str(), canonicalized.length()) || // !::EVP_DigestVerifyFinal(ctx, s.data(), s.size())) { // loggers::get_instance().warning("security_services::do_sign: Failed to verify signature, error 0x%lx", ::ERR_get_error()); // ::EVP_MD_CTX_free(ctx); @@ -207,7 +207,7 @@ int security_services::do_sign(const OCTETSTRING& p_encoded_message, const OCTET p_signature = OCTETSTRING(signature_length, s.data()); loggers::get_instance().log_msg("security_services::do_sign: signature: ", p_signature); - to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const unsigned char*>(p_signature), p_signature.lengthof() + static_cast<const unsigned char*>(p_signature))); + to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const uint8_t*>(p_signature), p_signature.lengthof() + static_cast<const uint8_t*>(p_signature))); p_signature = OCTETSTRING(to64.size(), to64.data()); loggers::get_instance().log_msg("security_services::do_sign: signature (64): ", p_signature); @@ -239,7 +239,7 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS // Transform and canonicalize the empty signature // Transform: Remove all CR, LS, TAB and SPACE outside of the tags transformed.clear(); - xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const unsigned char*>(p_empty_signature)), p_empty_signature.lengthof()), transformed); + xml_converters::get_instance().xml_transform(std::string((const char*)(static_cast<const uint8_t*>(p_empty_signature)), p_empty_signature.lengthof()), transformed); loggers::get_instance().log("security_services::do_sign_verify: p_empty_signature transormed: '%s'", transformed.c_str()); // Canonicalization canonicalized.clear(); @@ -276,7 +276,7 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS } } loggers::get_instance().log_msg("security_services::do_sign_verify: digest: ", dg); - std::vector<unsigned char> to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const unsigned char*>(dg), dg.lengthof() + static_cast<const unsigned char*>(dg))); + std::vector<uint8_t> to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const uint8_t*>(dg), dg.lengthof() + static_cast<const uint8_t*>(dg))); loggers::get_instance().log_msg("security_services::do_sign_verify: digest (64): ", OCTETSTRING(to64.size(), to64.data())); // Retrieve certificate @@ -318,7 +318,7 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS return false; } loggers::get_instance().log("security_services::do_sign_verify: public_key: '%p'", public_key); - std::vector<unsigned char> buffer; + std::vector<uint8_t> buffer; ret = _certs_db->publickey_to_string(public_key, buffer); if (ret == 1) { loggers::get_instance().warning("security_services::do_sign_verify: Failed to convert public key into string"); @@ -336,7 +336,7 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS canonicalized = canonicalized.substr(0, i + 13) + std::string(to64.cbegin(), to64.cend()) + canonicalized.substr(i + 13, canonicalized.length() - i - 13); OCTETSTRING to_be_signed(char2oct(CHARSTRING(canonicalized.c_str()))); loggers::get_instance().log_msg("security_services::do_sign_verify: to_be_signed: ", to_be_signed); - to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const unsigned char*>(to_be_signed), to_be_signed.lengthof() + static_cast<const unsigned char*>(to_be_signed))); + to64 = converter::get_instance().buffer_to_base64(std::vector(static_cast<const uint8_t*>(to_be_signed), to_be_signed.lengthof() + static_cast<const uint8_t*>(to_be_signed))); loggers::get_instance().log("security_services::do_sign_verify: Data to be signed/verified: '%s'", converter::get_instance().bytes_to_string(to64).c_str()); // Convert signature @@ -346,8 +346,8 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS //signature_value.erase(std::remove(signature_value.begin(), signature_value.end(), '\n'), signature_value.end()); loggers::get_instance().log("security_services::do_sign_verify: Before B64 decoded: '%s'", signature_value.c_str()); // Convert into bytes buffer - const std::vector<unsigned char> to_decode((const unsigned char*)signature_value.c_str(), static_cast<const unsigned char*>((const unsigned char*)signature_value.c_str() + signature_value.length())); - std::vector<unsigned char> s = converter::get_instance().base64_to_buffer(to_decode); + const std::vector<uint8_t> to_decode((const uint8_t*)signature_value.c_str(), static_cast<const uint8_t*>((const uint8_t*)signature_value.c_str() + signature_value.length())); + std::vector<uint8_t> s = converter::get_instance().base64_to_buffer(to_decode); loggers::get_instance().log("security_services::do_sign_verify: B64 decoded: '%s'", converter::get_instance().bytes_to_hexa(s, true).c_str()); loggers::get_instance().log("security_services::do_sign_verify: B64 decoded len: '%d'", s.size()); @@ -359,7 +359,7 @@ bool security_services::do_sign_verify(const CHARSTRING& p_message, const OCTETS } // Check signature if (!::EVP_DigestVerifyInit(ctx, NULL, sign_digest, NULL, (EVP_PKEY*)public_key) || - !::EVP_DigestVerifyUpdate(ctx, (const unsigned char*)canonicalized.c_str(), canonicalized.length()) || + !::EVP_DigestVerifyUpdate(ctx, (const uint8_t*)canonicalized.c_str(), canonicalized.length()) || !::EVP_DigestVerifyFinal(ctx, s.data(), s.size())) { loggers::get_instance().warning("security_services::do_sign_verify: Failed to verify signature, error 0x%lx", ::ERR_get_error()); ::EVP_MD_CTX_free(ctx); diff --git a/ccsrc/security/src/sha1.cc b/ccsrc/security/src/sha1.cc index 21b0514..1a7f834 100644 --- a/ccsrc/security/src/sha1.cc +++ b/ccsrc/security/src/sha1.cc @@ -19,10 +19,10 @@ int sha1::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) { return 0; } - return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash); + return generate(static_cast<const uint8_t*>(p_buffer), p_buffer.lengthof(), p_hash); } -int sha1::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) { +int sha1::generate(const uint8_t* p_buffer, const size_t p_length, OCTETSTRING& p_hash) { // Sanity check if ((p_buffer == nullptr) || (p_length == 0)) { p_hash = get_sha1_empty_string(); @@ -34,6 +34,6 @@ int sha1::generate(const unsigned char* p_buffer, const size_t p_length, OCTETST // Compute the hash value ::SHA1_Init(&_ctx); ::SHA1_Update(&_ctx, p_buffer, p_length); - ::SHA1_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx); + ::SHA1_Final((uint8_t*)static_cast<const uint8_t*>(p_hash), &_ctx); return 0; }; diff --git a/ccsrc/security/src/sha256.cc b/ccsrc/security/src/sha256.cc index 2238f09..f09d07e 100644 --- a/ccsrc/security/src/sha256.cc +++ b/ccsrc/security/src/sha256.cc @@ -19,10 +19,10 @@ int sha256::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) { return 0; } - return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash); + return generate(static_cast<const uint8_t*>(p_buffer), p_buffer.lengthof(), p_hash); } -int sha256::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) { +int sha256::generate(const uint8_t* p_buffer, const size_t p_length, OCTETSTRING& p_hash) { // Sanity check if ((p_buffer == nullptr) || (p_length == 0)) { p_hash = get_sha256_empty_string(); @@ -34,6 +34,6 @@ int sha256::generate(const unsigned char* p_buffer, const size_t p_length, OCTET // Compute the hash value ::SHA256_Init(&_ctx); ::SHA256_Update(&_ctx, p_buffer, p_length); - ::SHA256_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx); + ::SHA256_Final((uint8_t*)static_cast<const uint8_t*>(p_hash), &_ctx); return 0; }; diff --git a/ccsrc/security/src/sha384.cc b/ccsrc/security/src/sha384.cc index b32e4c2..fd40e18 100644 --- a/ccsrc/security/src/sha384.cc +++ b/ccsrc/security/src/sha384.cc @@ -19,10 +19,10 @@ int sha384::generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash) { return 0; } - return generate(static_cast<const unsigned char *>(p_buffer), p_buffer.lengthof(), p_hash); + return generate(static_cast<const uint8_t *>(p_buffer), p_buffer.lengthof(), p_hash); } -int sha384::generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash) { +int sha384::generate(const uint8_t *p_buffer, const size_t p_length, OCTETSTRING &p_hash) { // Sanity check if ((p_buffer == nullptr) || (p_length == 0)) { p_hash = get_sha384_empty_string(); @@ -34,12 +34,12 @@ int sha384::generate(const unsigned char *p_buffer, const size_t p_length, OCTET // Compute the hash value ::SHA384_Init(&_ctx); ::SHA384_Update(&_ctx, p_buffer, p_length); - ::SHA384_Final((unsigned char *)static_cast<const unsigned char *>(p_hash), &_ctx); + ::SHA384_Final((uint8_t *)static_cast<const uint8_t *>(p_hash), &_ctx); return 0; } const OCTETSTRING sha384::get_sha384_empty_string() const { - static unsigned char + static uint8_t sha384_empty_string[] = {0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b}; //! SHA-384 of an empty string -- GitLab