Commit 047d11f9 authored by yangjian's avatar yangjian

Fix Authentication failed(Synch Failure)

parent 791e52be
...@@ -86,6 +86,10 @@ extern int ncc; ...@@ -86,6 +86,10 @@ extern int ncc;
Sha256 ctx; Sha256 ctx;
random_state_t random_state; random_state_t random_state;
static uint8_t no_random_delta = 0; static uint8_t no_random_delta = 0;
static std::map<std::string, std::string> rand_record;
void amf_n1_task(void *); void amf_n1_task(void *);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -1141,7 +1145,7 @@ bool amf_n1::auth_vectors_generator(std::shared_ptr<nas_context>& nc) { ...@@ -1141,7 +1145,7 @@ bool amf_n1::auth_vectors_generator(std::shared_ptr<nas_context>& nc) {
return true; return true;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#define CURL_TIMEOUT_MS 100L #define CURL_TIMEOUT_MS 300L
std::size_t callback_ausf( std::size_t callback_ausf(
const char* in, std::size_t size, std::size_t num, std::string* out) { const char* in, std::size_t size, std::size_t num, std::string* out) {
...@@ -1293,10 +1297,10 @@ bool amf_n1::authentication_vectors_from_ausf( ...@@ -1293,10 +1297,10 @@ bool amf_n1::authentication_vectors_from_ausf(
Logger::amf_n1().debug("********* has auts *********"); Logger::amf_n1().debug("********* has auts *********");
char* auts_s = (char*)malloc(auts_len*2+1); char* auts_s = (char*)malloc(auts_len*2+1);
char* rand_s = (char*)malloc(RAND_LENGTH*2+1); // char* rand_s = (char*)malloc(RAND_LENGTH*2+1);
memset(auts_s,0,sizeof(auts_s)); memset(auts_s,0,sizeof(auts_s));
memset(rand_s,0,sizeof(rand_s)); // memset(rand_s,0,sizeof(rand_s));
uint8_t rand_value[RAND_LENGTH]; // uint8_t *rand_value;//[RAND_LENGTH];
Logger::amf_n1().debug("********* auts_len (%d) *********",auts_len); Logger::amf_n1().debug("********* auts_len (%d) *********",auts_len);
...@@ -1309,21 +1313,32 @@ bool amf_n1::authentication_vectors_from_ausf( ...@@ -1309,21 +1313,32 @@ bool amf_n1::authentication_vectors_from_ausf(
print_buffer("amf_n1", "********** ausf ***********", auts_value, auts_len); print_buffer("amf_n1", "********** ausf ***********", auts_value, auts_len);
Logger::amf_n1().info("********** ausf_s (%s) ***********",auts_s); Logger::amf_n1().info("********** ausf_s (%s) ***********",auts_s);
generate_random(rand_value, RAND_LENGTH); //generate_random(rand_value, RAND_LENGTH);
for(int i=0;i<RAND_LENGTH;i++) std::map<std::string,std::string>::iterator iter;
iter=rand_record.find(nc.get()->imsi);
if (iter != rand_record.end())
{
authenticationinfo_rand = iter->second;
Logger::amf_n1().info("********** rand_s (%s) ***********",authenticationinfo_rand.c_str());
}
else
{ {
sprintf(&rand_s[i*2],"%02X",rand_value[i]); Logger::amf_n1().error("********** There's no last rand ***********");
} }
authenticationinfo_rand = rand_s; // rand_value = nc.get()->_5g_av[0].rand;
print_buffer("amf_n1", "********** rand ***********", rand_value, RAND_LENGTH); // for(int i=0;i<RAND_LENGTH;i++)
Logger::amf_n1().info("********** rand_s (%s) ***********",rand_s); // {
// sprintf(&rand_s[i*2],"%02X",rand_value[i]);
// }
// authenticationinfo_rand = rand_s;
// print_buffer("amf_n1", "********** rand ***********", rand_value, RAND_LENGTH);
resynchronizationInfo.setAuts(authenticationinfo_auts); resynchronizationInfo.setAuts(authenticationinfo_auts);
resynchronizationInfo.setRand(authenticationinfo_rand); resynchronizationInfo.setRand(authenticationinfo_rand);
authenticationinfo.setResynchronizationInfo(resynchronizationInfo); authenticationinfo.setResynchronizationInfo(resynchronizationInfo);
free(auts_s); free_wrapper((void**) &auts_s);
free(rand_s); // free_wrapper((void**) &rand_s);
} }
to_json(authenticationinfo_j, authenticationinfo); to_json(authenticationinfo_j, authenticationinfo);
...@@ -1339,6 +1354,8 @@ bool amf_n1::authentication_vectors_from_ausf( ...@@ -1339,6 +1354,8 @@ bool amf_n1::authentication_vectors_from_ausf(
unsigned char* r5gauthdata_rand = unsigned char* r5gauthdata_rand =
format_string_as_hex(ueauthenticationctx.getR5gAuthData().getRand()); format_string_as_hex(ueauthenticationctx.getR5gAuthData().getRand());
memcpy(nc.get()->_5g_av[0].rand, r5gauthdata_rand, 16); memcpy(nc.get()->_5g_av[0].rand, r5gauthdata_rand, 16);
rand_record[nc.get()->imsi] = ueauthenticationctx.getR5gAuthData().getRand();
print_buffer("amf_n1", "5G AV: rand", nc.get()->_5g_av[0].rand, 16); print_buffer("amf_n1", "5G AV: rand", nc.get()->_5g_av[0].rand, 16);
free_wrapper((void**) &r5gauthdata_rand); free_wrapper((void**) &r5gauthdata_rand);
...@@ -1371,7 +1388,7 @@ bool amf_n1::authentication_vectors_from_ausf( ...@@ -1371,7 +1388,7 @@ bool amf_n1::authentication_vectors_from_ausf(
} }
bool amf_n1::_5g_aka_confirmation_from_ausf( bool amf_n1::_5g_aka_confirmation_from_ausf(
std::shared_ptr<nas_context>& nc, std::string& resStar) { std::shared_ptr<nas_context>& nc, bstring resStar) {
Logger::amf_n1().debug("_5g_aka_confirmation_from_ausf"); Logger::amf_n1().debug("_5g_aka_confirmation_from_ausf");
std::string remoteUri = nc.get()->Href; std::string remoteUri = nc.get()->Href;
...@@ -1379,7 +1396,22 @@ bool amf_n1::_5g_aka_confirmation_from_ausf( ...@@ -1379,7 +1396,22 @@ bool amf_n1::_5g_aka_confirmation_from_ausf(
std::string Response; std::string Response;
std::string resStar_string; std::string resStar_string;
convert_string_2_hex(resStar, resStar_string); std::map<std::string,std::string>::iterator iter;
iter=rand_record.find(nc.get()->imsi);
rand_record.erase(iter);
//convert_string_2_hex(resStar, resStar_string);
uint8_t resStar_len = blength(resStar);
uint8_t* resStar_value = (uint8_t *)bdata(resStar);
char* resStar_s = (char*)malloc(resStar_len*2+1);
for(int i=0;i<resStar_len;i++)
{
sprintf(&resStar_s[i*2],"%02X",resStar_value[i]);
}
resStar_string = resStar_s;
print_buffer("amf_n1", "********** resStar ***********", resStar_value, resStar_len);
Logger::amf_n1().info("********** resStar_s (%s) ***********",resStar_s);
nlohmann::json confirmationdata_j; nlohmann::json confirmationdata_j;
ConfirmationData confirmationdata; ConfirmationData confirmationdata;
...@@ -1388,9 +1420,11 @@ bool amf_n1::_5g_aka_confirmation_from_ausf( ...@@ -1388,9 +1420,11 @@ bool amf_n1::_5g_aka_confirmation_from_ausf(
to_json(confirmationdata_j, confirmationdata); to_json(confirmationdata_j, confirmationdata);
msgBody = confirmationdata_j.dump(); msgBody = confirmationdata_j.dump();
curl_http_client(remoteUri, "PUT", msgBody, Response); curl_http_client(remoteUri, "PUT", msgBody, Response);
free_wrapper((void**) &resStar_s);
Logger::amf_n1().info("Get Json content from AUSF response: %s",Response.c_str()); Logger::amf_n1().info("Get Json content from AUSF response: %s",Response.c_str());
try { try {
ConfirmationDataResponse confirmationdataresponse; ConfirmationDataResponse confirmationdataresponse;
nlohmann::json::parse(Response.c_str()).get_to(confirmationdataresponse); nlohmann::json::parse(Response.c_str()).get_to(confirmationdataresponse);
...@@ -1555,13 +1589,14 @@ void amf_n1::generate_random(uint8_t* random_p, ssize_t length) { ...@@ -1555,13 +1589,14 @@ void amf_n1::generate_random(uint8_t* random_p, ssize_t length) {
mpz_urandomb(random_nb, random_state.state, 8 * length); mpz_urandomb(random_nb, random_state.state, 8 * length);
pthread_mutex_unlock(&random_state.lock); pthread_mutex_unlock(&random_state.lock);
mpz_export(random_p, NULL, 1, length, 0, 0, random_nb); mpz_export(random_p, NULL, 1, length, 0, 0, random_nb);
// int r = 0, mask = 0, shift; int r = 0, mask = 0, shift;
// for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
// if ((i % sizeof(i)) == 0) r = rand(); if ((i % sizeof(i)) == 0) r = rand();
// shift = 8 * (i % sizeof(i)); shift = 8 * (i % sizeof(i));
// mask = 0xFF << shift; mask = 0xFF << shift;
// random_p[i] = (r & mask) >> shift; random_p[i] = (r & mask) >> shift;
// } }
Logger::amf_n1().debug("*************** generate rand function %x ***************",r);
} else { } else {
Logger::amf_n1().error("AMF config random -> false"); Logger::amf_n1().error("AMF config random -> false");
pthread_mutex_lock(&random_state.lock); pthread_mutex_lock(&random_state.lock);
...@@ -1778,8 +1813,8 @@ void amf_n1::authentication_response_handle(uint32_t ran_ue_ngap_id, ...@@ -1778,8 +1813,8 @@ void amf_n1::authentication_response_handle(uint32_t ran_ue_ngap_id,
} else { } else {
if(amf_cfg.is_Nausf) if(amf_cfg.is_Nausf)
{ {
std::string data = bdata(resStar); //std::string data = bdata(resStar);
if(!_5g_aka_confirmation_from_ausf(nc,data)) if(!_5g_aka_confirmation_from_ausf(nc,resStar))
isAuthOk = false; isAuthOk = false;
} }
else else
......
...@@ -112,7 +112,7 @@ class amf_n1 { ...@@ -112,7 +112,7 @@ class amf_n1 {
std::string& Response); std::string& Response);
bool authentication_vectors_from_ausf(std::shared_ptr<nas_context>& nc); bool authentication_vectors_from_ausf(std::shared_ptr<nas_context>& nc);
bool _5g_aka_confirmation_from_ausf( bool _5g_aka_confirmation_from_ausf(
std::shared_ptr<nas_context>& nc, std::string& resStar); std::shared_ptr<nas_context>& nc, bstring resStar);
bool authentication_vectors_generator_in_ausf( bool authentication_vectors_generator_in_ausf(
std::shared_ptr<nas_context>& nc); std::shared_ptr<nas_context>& nc);
......
...@@ -87,6 +87,13 @@ int UESecurityCapability::encode2buffer(uint8_t* buf, int len) { ...@@ -87,6 +87,13 @@ int UESecurityCapability::encode2buffer(uint8_t* buf, int len) {
encoded_size++; encoded_size++;
*(buf + encoded_size) = _5g_IASel; *(buf + encoded_size) = _5g_IASel;
encoded_size++; encoded_size++;
// if(length == 4)
// {
// *(buf + encoded_size) = 0xf0;
// encoded_size++;
// *(buf + encoded_size) = 0xf0;
// encoded_size++;
// }
} else { } else {
*(buf + encoded_size) = length - 2; *(buf + encoded_size) = length - 2;
encoded_size++; encoded_size++;
...@@ -94,6 +101,13 @@ int UESecurityCapability::encode2buffer(uint8_t* buf, int len) { ...@@ -94,6 +101,13 @@ int UESecurityCapability::encode2buffer(uint8_t* buf, int len) {
encoded_size++; encoded_size++;
*(buf + encoded_size) = _5g_IASel; *(buf + encoded_size) = _5g_IASel;
encoded_size++; encoded_size++;
// if(length == 4)
// {
// *(buf + encoded_size) = 0xf0;
// encoded_size++;
// *(buf + encoded_size) = 0xf0;
// encoded_size++;
// }
} }
Logger::nas_mm().debug("encoded UESecurityCapability (len %d)", encoded_size); Logger::nas_mm().debug("encoded UESecurityCapability (len %d)", encoded_size);
return encoded_size; return encoded_size;
...@@ -113,6 +127,13 @@ int UESecurityCapability::decodefrombuffer( ...@@ -113,6 +127,13 @@ int UESecurityCapability::decodefrombuffer(
decoded_size++; decoded_size++;
_5g_IASel = *(buf + decoded_size); _5g_IASel = *(buf + decoded_size);
decoded_size++; decoded_size++;
if(length == 4)
{
decoded_size++;
decoded_size++;
}
Logger::nas_mm().debug( Logger::nas_mm().debug(
"UESecurityCapability EA 0x%d,IA 0x%d", _5g_EASel, _5g_IASel); "UESecurityCapability EA 0x%d,IA 0x%d", _5g_EASel, _5g_IASel);
return decoded_size; return decoded_size;
......
...@@ -470,7 +470,7 @@ void InitialContextSetupRequestMsg::setNasPdu(uint8_t* nas, size_t sizeofnas) { ...@@ -470,7 +470,7 @@ void InitialContextSetupRequestMsg::setNasPdu(uint8_t* nas, size_t sizeofnas) {
(Ngap_InitialContextSetupRequestIEs_t*) calloc( (Ngap_InitialContextSetupRequestIEs_t*) calloc(
1, sizeof(Ngap_InitialContextSetupRequestIEs_t)); 1, sizeof(Ngap_InitialContextSetupRequestIEs_t));
ie->id = Ngap_ProtocolIE_ID_id_NAS_PDU; ie->id = Ngap_ProtocolIE_ID_id_NAS_PDU;
ie->criticality = Ngap_Criticality_reject; ie->criticality = Ngap_Criticality_ignore;
ie->value.present = Ngap_InitialContextSetupRequestIEs__value_PR_NAS_PDU; ie->value.present = Ngap_InitialContextSetupRequestIEs__value_PR_NAS_PDU;
int ret = nasPdu->encode2octetstring(ie->value.choice.NAS_PDU); int ret = nasPdu->encode2octetstring(ie->value.choice.NAS_PDU);
...@@ -738,7 +738,7 @@ bool InitialContextSetupRequestMsg::decodefrompdu( ...@@ -738,7 +738,7 @@ bool InitialContextSetupRequestMsg::decodefrompdu(
} break; } break;
case Ngap_ProtocolIE_ID_id_NAS_PDU: { case Ngap_ProtocolIE_ID_id_NAS_PDU: {
if (initialContextSetupRequestIEs->protocolIEs.list.array[i] if (initialContextSetupRequestIEs->protocolIEs.list.array[i]
->criticality == Ngap_Criticality_reject && ->criticality == Ngap_Criticality_ignore &&
initialContextSetupRequestIEs->protocolIEs.list.array[i] initialContextSetupRequestIEs->protocolIEs.list.array[i]
->value.present == ->value.present ==
Ngap_InitialContextSetupRequestIEs__value_PR_NAS_PDU) { Ngap_InitialContextSetupRequestIEs__value_PR_NAS_PDU) {
......
...@@ -267,8 +267,8 @@ bool InitialContextSetupResponseMsg::decodefrompdu( ...@@ -267,8 +267,8 @@ bool InitialContextSetupResponseMsg::decodefrompdu(
i++) { i++) {
switch (initialContextSetupResponseIEs->protocolIEs.list.array[i]->id) { switch (initialContextSetupResponseIEs->protocolIEs.list.array[i]->id) {
case Ngap_ProtocolIE_ID_id_AMF_UE_NGAP_ID: { case Ngap_ProtocolIE_ID_id_AMF_UE_NGAP_ID: {
if (initialContextSetupResponseIEs->protocolIEs.list.array[i] if (/*nitialContextSetupResponseIEs->protocolIEs.list.array[i]
->criticality == Ngap_Criticality_ignore && ->criticality == Ngap_Criticality_ignore &&*/
initialContextSetupResponseIEs->protocolIEs.list.array[i] initialContextSetupResponseIEs->protocolIEs.list.array[i]
->value.present == ->value.present ==
Ngap_InitialContextSetupResponseIEs__value_PR_AMF_UE_NGAP_ID) { Ngap_InitialContextSetupResponseIEs__value_PR_AMF_UE_NGAP_ID) {
...@@ -285,8 +285,8 @@ bool InitialContextSetupResponseMsg::decodefrompdu( ...@@ -285,8 +285,8 @@ bool InitialContextSetupResponseMsg::decodefrompdu(
} }
} break; } break;
case Ngap_ProtocolIE_ID_id_RAN_UE_NGAP_ID: { case Ngap_ProtocolIE_ID_id_RAN_UE_NGAP_ID: {
if (initialContextSetupResponseIEs->protocolIEs.list.array[i] if (/*initialContextSetupResponseIEs->protocolIEs.list.array[i]
->criticality == Ngap_Criticality_ignore && ->criticality == Ngap_Criticality_ignore &&*/
initialContextSetupResponseIEs->protocolIEs.list.array[i] initialContextSetupResponseIEs->protocolIEs.list.array[i]
->value.present == ->value.present ==
Ngap_InitialContextSetupResponseIEs__value_PR_RAN_UE_NGAP_ID) { Ngap_InitialContextSetupResponseIEs__value_PR_RAN_UE_NGAP_ID) {
......
...@@ -294,8 +294,8 @@ bool InitialUEMessageMsg::decodefrompdu(Ngap_NGAP_PDU_t* ngap_msg_pdu) { ...@@ -294,8 +294,8 @@ bool InitialUEMessageMsg::decodefrompdu(Ngap_NGAP_PDU_t* ngap_msg_pdu) {
} }
} break; } break;
case Ngap_ProtocolIE_ID_id_UserLocationInformation: { case Ngap_ProtocolIE_ID_id_UserLocationInformation: {
if (initialUEMessageIEs->protocolIEs.list.array[i]->criticality == if (/*initialUEMessageIEs->protocolIEs.list.array[i]->criticality ==
Ngap_Criticality_reject && Ngap_Criticality_reject &&*/
initialUEMessageIEs->protocolIEs.list.array[i]->value.present == initialUEMessageIEs->protocolIEs.list.array[i]->value.present ==
Ngap_InitialUEMessage_IEs__value_PR_UserLocationInformation) { Ngap_InitialUEMessage_IEs__value_PR_UserLocationInformation) {
userLocationInformation = new UserLocationInformation(); userLocationInformation = new UserLocationInformation();
......
...@@ -273,8 +273,8 @@ bool PduSessionResourceSetupResponseMsg::decodefrompdu( ...@@ -273,8 +273,8 @@ bool PduSessionResourceSetupResponseMsg::decodefrompdu(
i < pduSessionResourceSetupResponseIEs->protocolIEs.list.count; i++) { i < pduSessionResourceSetupResponseIEs->protocolIEs.list.count; i++) {
switch (pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]->id) { switch (pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]->id) {
case Ngap_ProtocolIE_ID_id_AMF_UE_NGAP_ID: { case Ngap_ProtocolIE_ID_id_AMF_UE_NGAP_ID: {
if (pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i] if (/*pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]
->criticality == Ngap_Criticality_ignore && ->criticality == Ngap_Criticality_ignore &&*/
pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i] pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]
->value.present == ->value.present ==
Ngap_PDUSessionResourceSetupResponseIEs__value_PR_AMF_UE_NGAP_ID) { Ngap_PDUSessionResourceSetupResponseIEs__value_PR_AMF_UE_NGAP_ID) {
...@@ -291,8 +291,8 @@ bool PduSessionResourceSetupResponseMsg::decodefrompdu( ...@@ -291,8 +291,8 @@ bool PduSessionResourceSetupResponseMsg::decodefrompdu(
} }
} break; } break;
case Ngap_ProtocolIE_ID_id_RAN_UE_NGAP_ID: { case Ngap_ProtocolIE_ID_id_RAN_UE_NGAP_ID: {
if (pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i] if (/*pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]
->criticality == Ngap_Criticality_ignore && ->criticality == Ngap_Criticality_ignore &&*/
pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i] pduSessionResourceSetupResponseIEs->protocolIEs.list.array[i]
->value.present == ->value.present ==
Ngap_PDUSessionResourceSetupResponseIEs__value_PR_RAN_UE_NGAP_ID) { Ngap_PDUSessionResourceSetupResponseIEs__value_PR_RAN_UE_NGAP_ID) {
......
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