Commit 2d793f10 authored by yangjian's avatar yangjian

Fix Authentication failed(Synch Failure)

parent cc00d0c1
......@@ -521,7 +521,7 @@ uint8_t *Authentication_5gaka::sqn_ms_derive(const uint8_t opc[16],
uint8_t *mac_s = NULL;
uint8_t mac_s_computed[MAC_S_LENGTH] = {0};
uint8_t *sqn_ms = NULL;
// uint8_t amf[2] = {0, 0};
uint8_t amf_tmp[2] = {0, 0};
int i = 0;
conc_sqn_ms = auts;
......@@ -541,7 +541,7 @@ uint8_t *Authentication_5gaka::sqn_ms_derive(const uint8_t opc[16],
sqn_ms[i] = ak[i] ^ conc_sqn_ms[i];
}
f1star(opc, key, rand_p, sqn_ms, amf, mac_s_computed);
f1star(opc, key, rand_p, sqn_ms, amf_tmp, mac_s_computed);
if (memcmp(mac_s_computed, mac_s, 8) != 0) {
Logger::udm_ueau().error("Failed to verify computed SQN_MS");
......@@ -611,12 +611,12 @@ void Authentication_5gaka::generate_random(uint8_t *random_p, ssize_t length) {
mpz_urandomb(random_nb, random_state.state, 8 * length);
pthread_mutex_unlock(&random_state.lock);
mpz_export(random_p, NULL, 1, length, 0, 0, random_nb);
// int r = 0, mask = 0, shift;
// for (int i = 0; i < length; i++) {
// if ((i % sizeof(i)) == 0)
// r = rand();
// shift = 8 * (i % sizeof(i));
// mask = 0xFF << shift;
// random_p[i] = (r & mask) >> shift;
// }
int r = 0, mask = 0, shift;
for (int i = 0; i < length; i++) {
if ((i % sizeof(i)) == 0)
r = rand();
shift = 8 * (i % sizeof(i));
mask = 0xFF << shift;
random_p[i] = (r & mask) >> shift;
}
}
......@@ -207,41 +207,56 @@ void GenerateAuthDataApiImpl::generate_auth_data(
// UDR PATCH interface ------- replace SQNhe with
// SQNms------------------------------
remoteUri = udr_ip + ":" + udr_port + "/nudr-dr/v2/subscription-data/" +
supi + "/authentication-data/authentication-subscription";
Logger::udm_ueau().debug("PATCH Request:" + remoteUri);
Method = "PATCH";
// remoteUri = udr_ip + ":" + udr_port + "/nudr-dr/v2/subscription-data/" +
// supi + "/authentication-data/authentication-subscription";
// Logger::udm_ueau().debug("PATCH Request:" + remoteUri);
// Method = "PATCH";
//
// nlohmann::json j_SequenceNumber;
// SequenceNumber m_SequenceNumber;
// m_SequenceNumber.setSqnScheme("NON_TIME_BASED");
// r_sqnms_s = conv::uint8_to_hex_string(r_sqn, 6);
// m_SequenceNumber.setSqn(r_sqnms_s);
// std::map<std::string, int32_t> index;
// index["ausf"] = 0;
// m_SequenceNumber.setLastIndexes(index);
// to_json(j_SequenceNumber, m_SequenceNumber);
//
// nlohmann::json j_PatchItem;
// PatchItem m_PatchItem;
// m_PatchItem.setValue(j_SequenceNumber.dump());
// m_PatchItem.setOp("replace");
// m_PatchItem.setFrom("");
// m_PatchItem.setPath("");
// to_json(j_PatchItem, m_PatchItem);
//
// msgBody = "[" + j_PatchItem.dump() + "]";
// Logger::udm_ueau().debug("PATCH Request body = " + msgBody);
//
// Curl::curl_http_client(remoteUri, Method, msgBody, Response);
nlohmann::json j_SequenceNumber;
SequenceNumber m_SequenceNumber;
m_SequenceNumber.setSqnScheme("NON_TIME_BASED");
r_sqnms_s = conv::uint8_to_hex_string(r_sqn, 6);
m_SequenceNumber.setSqn(r_sqnms_s);
std::map<std::string, int32_t> index;
index["ausf"] = 0;
m_SequenceNumber.setLastIndexes(index);
to_json(j_SequenceNumber, m_SequenceNumber);
// replace SQNhe with SQNms
uint64_t sqn_value = 0;
for (int i=0; i < 6; i++)
{
sqn[i] = r_sqn[i]; // generate first, increase later
nlohmann::json j_PatchItem;
PatchItem m_PatchItem;
m_PatchItem.setValue(j_SequenceNumber.dump());
m_PatchItem.setOp("replace");
m_PatchItem.setFrom("");
m_PatchItem.setPath("");
to_json(j_PatchItem, m_PatchItem);
sqn_value = sqn_value << 8;
sqn_value |= (sqn[i] & 0xff);
}
msgBody = "[" + j_PatchItem.dump() + "]";
Logger::udm_ueau().debug("PATCH Request body = " + msgBody);
// increment sqn
sqn_value += 32;
for (int i=0; i < 6; i++)
{
sqn[i] = (sqn_value >> ((5-i)*8)) & 0xff;
}
sqn_s = conv::uint8_to_hex_string(sqn, 6);
Curl::curl_http_client(remoteUri, Method, msgBody, Response);
// replace SQNhe with SQNms
int i = 0;
for (i; i < 6; i++)
sqn[i] = r_sqn[i]; // generate first, increase later
sqn_s = conv::uint8_to_hex_string(sqn, 16);
// Logger::udm_ueau().debug("sqn string = "+sqn_s);
sqn_s[12] = '\0';
// sqn_s[12] = '\0';
print_buffer("udm_ueau", "SQNms", sqn, 6);
......@@ -286,17 +301,27 @@ void GenerateAuthDataApiImpl::generate_auth_data(
response.send(Pistache::Http::Code::Ok, AuthInfoResult.dump());
// calculate new sqn----------------------------------------------------------
unsigned long long sqn_value;
std::stringstream s1;
s1 << std::hex << sqn_s;
s1 >> sqn_value; // hex string to decimal value
// unsigned long long sqn_value;
// std::stringstream s1;
// s1 << std::hex << sqn_s;
// s1 >> sqn_value; // hex string to decimal value
uint64_t sqn_value = 0;
for (int i=0; i < 6; i++)
{
sqn_value = sqn_value << 8;
sqn_value |= (sqn[i] & 0xff);
}
sqn_value += 32;
std::stringstream s2;
s2 << std::hex << std::setw(12) << std::setfill('0')
<< sqn_value; // decimal value to hex string
std::string new_sqn(s2.str());
for (int i=0; i < 6; i++)
{
sqn[i] = (sqn_value >> ((5-i)*8)) & 0xff;
}
sqn_s = conv::uint8_to_hex_string(sqn, 6);
Logger::udm_ueau().debug("new_sqn = " + new_sqn);
Logger::udm_ueau().debug("new_sqn = " + sqn_s);
// UDR PATCH interface ------- increase
// sqn------------------------------------
......@@ -308,7 +333,7 @@ void GenerateAuthDataApiImpl::generate_auth_data(
nlohmann::json j_SequenceNumber;
SequenceNumber m_SequenceNumber;
m_SequenceNumber.setSqnScheme("NON_TIME_BASED");
m_SequenceNumber.setSqn(new_sqn);
m_SequenceNumber.setSqn(sqn_s);
std::map<std::string, int32_t> index;
index["ausf"] = 0;
m_SequenceNumber.setLastIndexes(index);
......
......@@ -105,6 +105,8 @@ int main(int argc, char **argv) {
setUpUnixSignals(sigs);
#endif
srand(time(NULL));
if (!Options::parse(argc, argv)) {
std::cout << "Options::parse() failed\n Please input args" << std::endl;
return 1;
......
......@@ -43,7 +43,7 @@
#include "logger.hpp"
#include "udm_config.hpp"
#define CURL_TIMEOUT_MS 100L
#define CURL_TIMEOUT_MS 300L
class Curl {
public:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment