Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cti-tools/titan-test-system-framework
1 result
Show changes
Showing
with 986 additions and 190 deletions
#include "LibSecurity_Hash.hh"
#include "LibSecurity_Signature.hh"
#include "LibSecurity_Certificates.hh"
#include "loggers.hh"
#include "sha1.hh"
#include "sha256.hh"
#include "sha384.hh"
#include "certs_db.hh"
#include "security_services.hh"
static std::unique_ptr<security_services> _security_services;
static int transform_signature_workaround(std::string& str) {
loggers::get_instance().log(">>> transform_signature_workaround: '%s'", str.c_str());
int first = str.find("&lt;");
while ((first != -1) && (first < str.length())) {
str = str.substr(0, first) + "<" + str.substr(first + 4, str.length() - 4);
//loggers::get_instance().log("transform_signature_workaround: New str (1): '%s'", str.c_str());
first = str.find("&lt;");
} // End of 'while'statement
first = str.find("&gt;");
while ((first != -1) && (first < str.length())) {
str = str.substr(0, first) + ">" + str.substr(first + 4, str.length() - 4);
//loggers::get_instance().log("transform_signature_workaround: New str (2): '%s'", str.c_str());
first = str.find("&gt;");
} // End of 'while'statement
first = str.find("&quot;");
while ((first != -1) && (first < str.length())) {
str = str.substr(0, first) + "'" + str.substr(first + 6, str.length() - 6);
//loggers::get_instance().log("transform_signature_workaround: New str (3): '%s'", str.c_str());
first = str.find("&quot;");
} // End of 'while'statement
first = str.find("&#13;");
while ((first != -1) && (first < str.length())) {
str = str.substr(0, first) + "\r" + str.substr(first + 5, str.length() - 5);
//loggers::get_instance().log("transform_signature_workaround: New str (4): '%s'", str.c_str());
first = str.find("&#13;");
} // End of 'while'statement
std::replace(str.begin(), str.end(), '\'', '\"');
loggers::get_instance().log("<<< transform_signature_workaround: '%s'", str.c_str());
return 0;
}
static int transform_xslt_workaround(std::string& str) {
loggers::get_instance().log(">>> transform_xslt_workaround: '%s'", str.c_str());
int start = str.find("<xsl:stylesheet");
//loggers::get_instance().log("transform_xslt_workaround: start='%d' ", start);
int stop = str.find("</Transform>");
//loggers::get_instance().log("transform_xslt_workaround: stop='%d' ", stop);
int first = str.find("<", start);
while ((first != -1) && (first < stop)) {
//loggers::get_instance().log("transform_xslt_workaround: first='%d' ", first);
str = str.substr(0, first) + "&lt;" + str.substr(first + 1, str.length() - 1);
//loggers::get_instance().log("transform_xslt_workaround: New str (1): '%s'", str.c_str());
first = str.find("<", first);
stop = str.find("</Transform>");
} // End of 'while'statement
first = str.find(">", start);
while ((first != -1) && (first < stop)) {
//loggers::get_instance().log("transform_xslt_workaround: first='%d' ", first);
str = str.substr(0, first) + "&gt;" + str.substr(first + 1, str.length() - 1);
//loggers::get_instance().log("transform_xslt_workaround: New str (1): '%s'", str.c_str());
first = str.find(">", first);
stop = str.find("</Transform>");
} // End of 'while'statement
first = str.find("\"", start);
while ((first != -1) && (first < stop)) {
//loggers::get_instance().log("transform_xslt_workaround: first='%d' ", first);
str = str.substr(0, first) + "&quot;" + str.substr(first + 1, str.length() - 1);
//loggers::get_instance().log("transform_xslt_workaround: New str (1): '%s'", str.c_str());
first = str.find("\"", first);
stop = str.find("</Transform>");
} // End of 'while'statement
loggers::get_instance().log("<<< transform_xslt_workaround: '%s'", str.c_str());
return 0;
}
INTEGER LibSecurity__Certificates::fx__init__certs__db(const CHARSTRING& p_certs_db_path) {
loggers::get_instance().log_msg(">>> fx__init__certs__db: ", p_certs_db_path);
int ret = _security_services->initialize(std::string(static_cast<const char*>(p_certs_db_path)));
loggers::get_instance().log("<<< fx__init__certs__db.");
return ret;
}
INTEGER LibSecurity__Certificates::fx__load__certificate(const CHARSTRING& p_certificate_name, const CHARSTRING& p_private_key_name, const CHARSTRING& p_private_key_passwd) {
loggers::get_instance().log_msg(">>> fx__load__certificate: ", p_certificate_name);
loggers::get_instance().log_msg(">>> fx__load__certificate: ", p_private_key_name);
const X509* certificate;
int ret = _security_services->load_certificate(std::string(static_cast<const char*>(p_certificate_name)), std::string(static_cast<const char*>(p_private_key_name)), std::string(static_cast<const char*>(p_private_key_passwd)), &certificate);
loggers::get_instance().log("fx__load__certificate: certificate: '%p'", certificate);
if (ret == 0) {
ASN1_INTEGER* asn1_serial = ::X509_get_serialNumber((X509*)certificate);
if (asn1_serial == nullptr) {
loggers::get_instance().log("fx__load__certificate: Failed to retrieve X509 serial number");
return -1;
}
uint64_t v;
ASN1_INTEGER_get_uint64(&v, asn1_serial);
loggers::get_instance().log("fx__load__certificate: Loaded certificate: serial number: %ld", v);
}
loggers::get_instance().log("<<< fx__load__certificate");
return ret;
}
OCTETSTRING LibSecurity__Hash::fx__hash(const OCTETSTRING& p_to_be_hashed, const LibSecurity__Hash::HashAlgorithm& p_hash_algorithm) {
loggers::get_instance().log_msg(">>> fx__hash: ", p_to_be_hashed);
OCTETSTRING hash;
switch (p_hash_algorithm) {
case LibSecurity__Hash::HashAlgorithm::e__sha1: {
sha1 s;
s.generate(p_to_be_hashed, hash);
}
break;
case LibSecurity__Hash::HashAlgorithm::e__sha256: {
sha256 s;
s.generate(p_to_be_hashed, hash);
}
break;
case LibSecurity__Hash::HashAlgorithm::e__sha384: {
sha384 s;
s.generate(p_to_be_hashed, hash);
}
break;
} // End of 'switch' statement
loggers::get_instance().log_msg("<<< fx__hash: ", hash);
return hash;
}
BITSTRING LibSecurity__Signature::fx__enc__xmldsig__signed__info(const http__www__w3__org__2000__09__xmldsig::Signature_signedInfo& s) { // FIXME Use enc/dec TITAN function external function f_enc_value(in Value x) return bitstring with { extension "prototype(convert) encode(abc)" }
loggers::get_instance().log(">>> fx__enc__xmldsig__signed__info");
TTCN_EncDec::clear_error();
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
TTCN_Buffer encoding_buffer;
s.encode(http__www__w3__org__2000__09__xmldsig::Signature_signedInfo_descr_, encoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
// FIXME Update <transform>
std::string str(static_cast<const char*>((const char*)encoding_buffer.get_data()), encoding_buffer.get_len() + static_cast<const char*>((const char*)encoding_buffer.get_data()));
loggers::get_instance().log("fx__enc__xmldsig__signed__info: Before str: '%s'", str.c_str());
transform_signature_workaround(str);
loggers::get_instance().log("fx__enc__xmldsig__signed__info: Afer str: '%s'", str.c_str());
OCTETSTRING os = char2oct(CHARSTRING(str.c_str()));
loggers::get_instance().log_msg("fx__enc__xmldsig__signed__info: os: ", os);
return oct2bit(os);
}
BITSTRING LibSecurity__Signature::fx__enc__xmldsig(const http__www__w3__org__2000__09__xmldsig::Signature& s) { // FIXME Use enc/dec TITAN function external function f_enc_value(in Value x) return bitstring with { extension "prototype(convert) encode(abc)" }
loggers::get_instance().log(">>> fx__enc__xmldsig");
TTCN_EncDec::clear_error();
TTCN_EncDec::set_error_behavior(TTCN_EncDec::ET_ALL, TTCN_EncDec::EB_DEFAULT);
TTCN_Buffer encoding_buffer;
s.encode(http__www__w3__org__2000__09__xmldsig::Signature_descr_, encoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
// FIXME Update <transform>
std::string str(static_cast<const char*>((const char*)encoding_buffer.get_data()), encoding_buffer.get_len() + static_cast<const char*>((const char*)encoding_buffer.get_data()));
loggers::get_instance().log("fx__enc__xmldsig: Before str: '%s'", str.c_str());
transform_signature_workaround(str);
loggers::get_instance().log("fx__enc__xmldsig: Afer str: '%s'", str.c_str());
OCTETSTRING os = char2oct(CHARSTRING(str.c_str()));
loggers::get_instance().log_msg("fx__enc__xmldsig: os: ", os);
return oct2bit(os);
}
INTEGER LibSecurity__Signature::fx__dec__xmldsig(BITSTRING& bs, http__www__w3__org__2000__09__xmldsig::Signature& s) { // FIXME Use enc/dec TITAN function external function f_enc_value(in Value x) return bitstring with { extension "prototype(convert) encode(abc)" }
loggers::get_instance().log(">>> fx__dec__xmldsig");
std::string str(static_cast<const char*>(oct2char(bit2oct(bs))));
loggers::get_instance().log("fx__dec__xmldsig: Before str: '%s'", str.c_str());
transform_signature_workaround(str);
transform_xslt_workaround(str);
loggers::get_instance().log("fx__dec__xmldsig: Afer str: '%s'", str.c_str());
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()));
s.decode(http__www__w3__org__2000__09__xmldsig::Signature_descr_, decoding_buffer, TTCN_EncDec::CT_XER, XER_EXTENDED);
loggers::get_instance().log_msg("<<< fx__dec__xmldsig: ", s);
return 0;
}
INTEGER LibSecurity__Signature::fx__sign(const OCTETSTRING& p_encoded_message, const OCTETSTRING& p_empty_signature, const CHARSTRING& p_certificate_name, const CHARSTRING& p_private_key_name, const CHARSTRING& p_private_key_passwd, OCTETSTRING& p_signature, OCTETSTRING& p_digest, CHARSTRING& p_x509_certificate_subject, CHARSTRING& p_x509_certificate_pem, CHARSTRING& p_pull_request_canonicalized) {
loggers::get_instance().log_msg(">>> fx__sign: ", p_encoded_message);
if (_security_services->do_sign(p_encoded_message, p_empty_signature, p_certificate_name, p_private_key_name, p_private_key_passwd, p_signature, p_digest, p_x509_certificate_subject, p_x509_certificate_pem, p_pull_request_canonicalized) == -1) {
loggers::get_instance().log("fx__sign: Failed to signed message");
return -1;
}
return 0;
}
BOOLEAN LibSecurity__Signature::fx__do__sign__verify(const CHARSTRING& p_message, const OCTETSTRING& p_empty_signature, const UNIVERSAL_CHARSTRING& p_canonicalization_method, const UNIVERSAL_CHARSTRING& p_signature_method, const UNIVERSAL_CHARSTRING& p_digest_method, const UNIVERSAL_CHARSTRING& p_digest_value, const UNIVERSAL_CHARSTRING& p_signature_value, const UNIVERSAL_CHARSTRING& p_subject_name, const UNIVERSAL_CHARSTRING& p_certificate, CHARSTRING& p_debug_message) {
loggers::get_instance().log(">>> fx__do__sign__verify");
if (!_security_services->do_sign_verify(p_message, p_empty_signature, p_canonicalization_method, p_signature_method, p_digest_method, p_digest_value, p_signature_value, p_subject_name, p_certificate, p_debug_message)) {
loggers::get_instance().log("fx__do__sign__verify: Failed to verify message signature");
return false;
}
return true;
}
This diff is collapsed.
/*!
* \file sha1.cc
* \brief Source file for SHA-1 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha1.hh"
int sha1::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha1_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha1::generate(const unsigned char* 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();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA_DIGEST_LENGTH);
// 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);
return 0;
};
/*!
* \file sha256.cc
* \brief Source file for SHA-256 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha256.hh"
int sha256::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha256_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha256::generate(const unsigned char* 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();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA256_DIGEST_LENGTH);
// 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);
return 0;
};
/*!
* \file sha384.cc
* \brief Sorce file for SHA-384 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha384.hh"
int sha384::generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha384_empty_string();
return 0;
}
return generate(static_cast<const unsigned char *>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha384::generate(const unsigned char *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();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA384_DIGEST_LENGTH);
// 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);
return 0;
}
const OCTETSTRING sha384::get_sha384_empty_string() const {
static unsigned char
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
return OCTETSTRING(48, sha384_empty_string);
}
sources := \
ttcn/LibHelpers_Functions.ttcn
......@@ -9,14 +9,14 @@
* All rights reserved.
*
*/
module LibHelpers_Functions {
// LibCommon
import from LibCommon_BasicTypesAndValues all;
group math {
/**
* @desc function to generate integer random values
* @see ttcn-3 - rnd()
......@@ -25,16 +25,17 @@ module LibHelpers_Functions {
* @return random integer
*
*/
function f_random( in integer p_lowerbound,
in integer p_upperbound )
return integer {
function f_random(
in integer p_lowerbound,
in integer p_upperbound
) return integer {
//Variables
var integer v_random := 0;
v_random := float2int(int2float(p_upperbound - p_lowerbound +1)*rnd()) + p_lowerbound;
v_random := float2int(int2float(p_upperbound - p_lowerbound + 1) * rnd()) + p_lowerbound;
log("*** f_random: INFO: OK - random value = " & int2str(v_random) & " ***");
return v_random;
} // End of function f_random
/**
......@@ -44,7 +45,7 @@ module LibHelpers_Functions {
*/
function f_abs(in integer p_number)
return integer {
if(p_number < 0) {
return 0 - p_number;
}
......@@ -58,7 +59,7 @@ module LibHelpers_Functions {
*/
function ff_abs(in float p_number)
return float {
if(p_number < 0.0) {
return 0.0 - p_number;
}
......@@ -73,7 +74,7 @@ module LibHelpers_Functions {
*/
function f_min(in integer p_a, in integer p_b)
return integer {
if(p_a < p_b) {
return p_a;
}
......@@ -90,7 +91,7 @@ module LibHelpers_Functions {
function f_removeUnsignificantBits(in bitstring p_bitstring)
return bitstring {
var integer i, len;
len := lengthof(p_bitstring);
for(i:=len-1; i >=0 and p_bitstring[i] == '0'B; i:=i-1) {}
return substr(p_bitstring, 0, i + 1);
......@@ -105,61 +106,86 @@ module LibHelpers_Functions {
*/
function f_getCurrentTimeUtc() return UInt64 {
var UInt64 v_time := 0;
// log("*** f_getCurrentTimeUtc: INFO: calling fx_getCurrentTimeUtc() ***");
v_time := fx_getCurrentTimeUtc();
return v_time;
}
/**
* @desc Gets the Minute of current UTC year
* @return MinuteOfTheYear - tenths of a second in the current or next hour in units of 1/10th second from UTC time
*/
function f_getMinuteOfTheYear() return UInt16 {
var UInt16 v_minuteOfTheYear := 0;
// log("*** f_getMinuteOfTheYear: INFO: calling fx_getMinuteOfTheYear() ***");
v_minuteOfTheYear := fx_getMinuteOfTheYear();
return v_minuteOfTheYear;
}
/**
* @desc Gets the milliseconds point in the current UTC minute
* @return DSecond - The milliseconds point in the current UTC minute (0..60000)
*/
function f_getDSecond() return UInt16 {
var UInt16 v_dSecond := 0;
// log("*** f_getDSecond: INFO: calling fx_getDSecond() ***");
v_dSecond := fx_getDSecond();
return v_dSecond;
}
} // End of group datetime
group externals {
/**
* @desc Gets the current time since 01/01/1970 in UTC format
* @return The current time since 01/01/1970 in UTC format
*/
external function fx_getCurrentTimeUtc() return UInt64;
/**
* @desc Gets the minutes of current UTC year
* @return MinuteOfTheYear - minutes of current UTC year
*/
external function fx_getMinuteOfTheYear() return UInt16;
/**
* @desc Gets the milliseconds point in the current UTC minute
* @return DSecond - the milliseconds point in the current UTC minute
*/
external function fx_getDSecond() return UInt16;
/**
* @desc Encode into Base64
* @return p_to_encode - The buffer to be encoded
*/
external function fx_enc_base64(in octetstring p_to_encode) return octetstring;
/**
* @desc Decode from Base64
* @return p_to_decode - The buffer to be decoded
*/
external function fx_dec_base64(in octetstring p_to_decode) return octetstring;
/**
* @desc Generate a new UUID
* @return The UUID in string format on success, a null string otherwise
*/
external function fx_generate_uuid() return charstring;
/**
* @brief Retrieve the current local date/time formatted as yyyy-mm-ddThh:mm:ss.lll+nn:00
* @param p_shift_time The algebric number of seconds to add to the current time
* @return The the current date/time on success, a null string otherwise
*/
external function fx_get_current_date_time(in integer p_shift_time) return charstring;
} // End of externals
} // End of module LibHelpers_Functions
sources := \
ttcn/LibHttp_BinaryMessageBodyTypes.ttcn \
ttcn/LibHttp_EncdecDeclarations.ttcn \
ttcn/LibHttp_Functions.ttcn \
ttcn/LibHttp_JSONTypes.ttcn \
ttcn/LibHttp_MessageBodyTypes.ttcn \
ttcn/LibHttp_Pics.ttcn \
ttcn/LibHttp_Pixits.ttcn \
ttcn/LibHttp_Templates.ttcn \
ttcn/LibHttp_TestSystem.ttcn \
ttcn/LibHttp_TypesAndValues.ttcn \
ttcn/LibHttp_XMLTypes.ttcn \
# Please, move and comment the module you need to overwrite tofit your project
# ttcn/LibHttp_XmlMessageBodyTypes.ttcn \
# ttcn/LibHttp_XmlTemplates.ttcn
# ttcn/LibHttp_JsonMessageBodyTypes.ttcn \
# ttcn/LibHttp_JsonTemplates.ttcn \
......@@ -22,7 +22,7 @@ module LibHttp_BinaryMessageBodyTypes {
} with {
variant ""
}
} with {
variant ""
} // End of LibHttp_BinaryMessageBodyTypes
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.