Commit 8f498109 authored by Tien-Thinh Nguyen's avatar Tien-Thinh Nguyen

Polish the code/remove unnecessary files

parent e16571b4
......@@ -27,17 +27,18 @@
*/
#include "amf_app.hpp"
#include <stdexcept>
#include <iostream>
#include <cstdlib>
#include "itti.hpp"
#include "ngap_app.hpp"
#include "amf_config.hpp"
#include "amf_n2.hpp"
#include "amf_n1.hpp"
#include "amf_n11.hpp"
#include <stdexcept>
#include <iostream>
#include <cstdlib>
#include "amf_statistics.hpp"
#include "DLNASTransport.hpp"
using namespace ngap;
......
......@@ -27,13 +27,17 @@
*/
#include "amf_config.hpp"
#include "logger.hpp"
#include <iostream>
#include <libconfig.h++>
#include "string.hpp"
#include "thread_sched.hpp"
#include "logger.hpp"
#include "amf_app.hpp"
#include "if.hpp"
#include "3gpp_ts24501.hpp"
extern "C" {
#include <arpa/inet.h>
#include <stdbool.h>
......@@ -44,8 +48,6 @@ extern "C" {
#include "common_defs.h"
}
#include <iostream>
using namespace libconfig;
using namespace amf_application;
......
......@@ -29,22 +29,21 @@
#ifndef _AMF_CONFIG_H_
#define _AMF_CONFIG_H_
#include "amf_config.hpp"
#include <arpa/inet.h>
#include <libconfig.h++>
#include <netinet/in.h>
#include <sys/socket.h>
#include <mutex>
#include <vector>
#include <string>
#include "thread_sched.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include "amf_config.hpp"
#include "thread_sched.hpp"
#define AMF_CONFIG_STRING_AMF_CONFIG "AMF"
#define AMF_CONFIG_STRING_PID_DIRECTORY "PID_DIRECTORY"
#define AMF_CONFIG_STRING_INSTANCE_ID "INSTANCE_ID"
......
......@@ -27,10 +27,12 @@
*/
#include "amf_module_from_config.hpp"
#include "logger.hpp"
#include <iostream>
#include <string>
#include "logger.hpp"
namespace config {
//------------------------------------------------------------------------------
......
......@@ -27,6 +27,7 @@
*/
#include "amf_n1.hpp"
#include "amf_n11.hpp"
#include "amf_app.hpp"
#include "itti.hpp"
......
......@@ -31,14 +31,6 @@
#include <map>
#include <shared_mutex>
#include "nas_context.hpp"
#include "pdu_session_context.hpp"
#include "itti_msg_n1.hpp"
#include "bstrlib.h"
#include "3gpp_ts24501.hpp"
#include "amf_statistics.hpp"
#include "amf.hpp"
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
......@@ -50,6 +42,13 @@
#include <inttypes.h>
#include <mysql/mysql.h>
#include "nas_context.hpp"
#include "pdu_session_context.hpp"
#include "itti_msg_n1.hpp"
#include "bstrlib.h"
#include "3gpp_ts24501.hpp"
#include "amf_statistics.hpp"
#include "amf.hpp"
#include "mysql_db.hpp"
namespace amf_application {
......
......@@ -27,23 +27,23 @@
*/
#include "amf_n11.hpp"
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include "amf_n1.hpp"
#include "itti.hpp"
#include "itti_msg_amf_app.hpp"
#include "amf_config.hpp"
#include "nas_context.hpp"
#include "amf.hpp"
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include "3gpp_ts24501.hpp"
// For smf_client
#include "SmContextCreateData.h"
#include "SMContextsCollectionApi.h"
#include "ApiConfiguration.h"
#include "ApiClient.h"
#include "3gpp_ts24501.hpp"
using namespace oai::smf::model;
using namespace oai::smf::api;
using namespace web;
......
......@@ -28,12 +28,14 @@
#ifndef _AMF_N11_H_
#define _AMF_N11_H_
#include "itti_msg_n11.hpp"
#include <map>
#include <shared_mutex>
#include "pdu_session_context.hpp"
#include <string>
#include "itti_msg_n11.hpp"
#include "pdu_session_context.hpp"
namespace amf_application {
class amf_n11 {
......
......@@ -27,6 +27,7 @@
*/
#include "amf_n2.hpp"
#include "amf_n1.hpp"
#include "amf_app.hpp"
#include "logger.hpp"
......
......@@ -40,7 +40,6 @@ class amf_n2 : public ngap::ngap_app{
public:
amf_n2(const std::string &address, const uint16_t port_num);
~amf_n2();
//void handle_receive(bstring payload, sctp_assoc_id_t assoc_id, sctp_stream_id_t stream, sctp_stream_id_t instreams, sctp_stream_id_t outstreams);
void handle_itti_message(itti_new_sctp_association &new_assoc);
void handle_itti_message(itti_ng_setup_request &ngsetupreq);
void handle_itti_message(itti_initial_ue_message &init_ue_msg);
......
......@@ -27,9 +27,11 @@
*/
#include "amf_statistics.hpp"
#include "logger.hpp"
#include <iostream>
#include "logger.hpp"
//------------------------------------------------------------------------------
void statistics::display() {
Logger::amf_app().info("");
......
......@@ -34,6 +34,7 @@
#include <stdint.h>
#include <vector>
#include <string>
#include "amf.hpp"
#include "ngap_app.hpp"
......
......@@ -29,6 +29,7 @@
#include "amf_n1.hpp"
#include "logger.hpp"
#include "amf_config.hpp"
using namespace amf_application;
using namespace config;
......
......@@ -32,8 +32,10 @@
#include <stdint.h>
#include <string>
#include <vector>
#include "sctp_server.hpp"
#include "NgapIEsStruct.hpp"
extern "C" {
#include "Ngap_PagingDRX.h"
#include "bstrlib.h"
......
......@@ -30,12 +30,13 @@
#define _AMF_NAS_CONTEXT_H_
#include <stdint.h>
#include "security_def.hpp"
#include "authentication_algorithms_with_5gaka.hpp"
#include <string>
#include "nas_security_context.hpp"
#include "struct.hpp"
#include "nas_security_context.hpp"
#include "security_def.hpp"
#include "authentication_algorithms_with_5gaka.hpp"
class nas_context {
public:
nas_context();
......
......@@ -31,9 +31,10 @@
#include <stdint.h>
#include <map>
#include "NgapIEsStruct.hpp"
#include <shared_mutex>
#include "NgapIEsStruct.hpp"
extern "C" {
#include "Ngap_RRCEstablishmentCause.h"
}
......
......@@ -31,9 +31,10 @@
#include <stdint.h>
#include <map>
#include "gNB_context.hpp"
#include <shared_mutex>
#include "gNB_context.hpp"
using namespace sctp;
typedef enum {
NGAP_UE_INVALID_STATE,
......
......@@ -329,7 +329,6 @@ include_directories(${SRC_TOP_DIR}/sbi/amf_server/impl)
include_directories(${SRC_TOP_DIR}/sbi/amf_server/model)
include_directories(${SRC_TOP_DIR}/sbi/smf_client)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/api)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/inputs)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/model)
include_directories(${SRC_TOP_DIR}/sctp)
include_directories(${SRC_TOP_DIR}/secu_algorithms/5gaka)
......
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#include <iostream>
#include <thread>
#include <signal.h>
......@@ -19,9 +40,6 @@
#include "amf_module_from_config.hpp"
#include "amf_statistics.hpp"
#include "SMFClientApi.hpp"
#include "smf-client.hpp"
#include <string>
#include <cstring>
#include "normalizer.hh"
......@@ -58,7 +76,6 @@ if (!Options::parse(argc, argv)) {
itti_inst = new itti_mw();
itti_inst->start(amf_cfg.itti.itti_timer_sched_params);
//itti_inst->start();
amf_app_inst = new amf_app(amf_cfg);
amf_app_inst->allRegistredModulesInit(modules);
......
......@@ -28,7 +28,6 @@ include_directories(${SRC_TOP_DIR}/utils)
include_directories(${SRC_TOP_DIR}/utils/bstr)
include_directories(${SRC_TOP_DIR}/sbi/smf_client)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/api)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/inputs)
include_directories(${SRC_TOP_DIR}/sbi/smf_client/model)
add_library (AMF_SBI_CLIENT STATIC
......@@ -40,6 +39,5 @@ add_library (AMF_SBI_CLIENT STATIC
${CMAKE_CURRENT_SOURCE_DIR}/ModelBase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/MultipartFormData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Object.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SMFClientApi.cpp
)
#include "SMFClientApi.hpp"
#include "IndividualPDUSessionHSMFApi.h"
#include "IndividualSMContextApi.h"
#include "PDUSessionsCollectionApi.h"
#include "SMContextsCollectionApi.h"
#include "ApiConfiguration.h"
#include "ApiClient.h"
#include "nlohmann/json.hpp"
#include "logger.hpp"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#define DEFAULT_JSON_FILE "/home/oai/oai-5g-amf/src/sbi/smf_client/inputs/SmContextCreateData.json"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams // JSON library
using namespace oai::smf::api;
using namespace oai::smf::model;
//using json = nlohmann::json;
int smf_client_test()
{
// create ApiConfiguration
std::shared_ptr <ApiConfiguration> apiConfiguration (new ApiConfiguration);
apiConfiguration->setBaseUrl(utility::conversions::to_string_t("http://10.103.238.82:8889/nsmf-pdusession/v2"));
//apiConfiguration->setBaseUrl(utility::conversions::to_string_t("http://192.168.2.189:8889/nsmf-pdusession/v2"));
std::shared_ptr <ApiClient> apiClient(new ApiClient (apiConfiguration));
std::shared_ptr<SmContextMessage> smContextMessage (new SmContextMessage);
//fill the content of smContextMessage
//TODO:
std::shared_ptr<SmContextCreateData> smContextCreateData (new SmContextCreateData);
Logger::amf_app().debug("starting test smf_client");
json::value jv; // JSON read from input file
try {
Logger::amf_app().debug("try to get smcontextcreatedata json(%s)", DEFAULT_JSON_FILE);
string_t importFile = DEFAULT_JSON_FILE; // extract filename
ifstream_t f(importFile); // filestream of working file
stringstream_t s; // string stream for holding JSON read from file
if (f) {
s << f.rdbuf(); // stream results of reading from file stream into string stream
f.close(); // close the filestream
jv = json::value::parse(s); // parse the resultant string stream.
std::cout << "file" << DEFAULT_JSON_FILE<<std::endl;
}else{
Logger::amf_app().error("no json file");
}
}
catch (web::json::json_exception excep) {
//std::cout << "ERROR Parsing JSON: ";
//std::cout << excep.what();
Logger::amf_app().error("error!");
}
//auto supi = jv.at(U("supi"));
//std::cout << supi <<std::endl;
smContextCreateData->fromJson(jv);
smContextMessage->setJsonData(smContextCreateData);
std::shared_ptr<HttpContent> n1sm(new HttpContent);
//n1sm->setName("\\\"n1SmMsg\\\"");
//n1sm->setContentDisposition("attachment");
n1sm->setContentType("application/vnd.3gpp.5gnas");
//uint8_t buf[18] = {0x2e, 0x05, 0x1e, 0xc1, 0xff, 0xff, 0x91, 0xa1, 0x7b, 0x00, 0x07, 0x80, 0x00, 0xa0, 0x00, 0x00, 0x0d, 0x00};
uint8_t buf[8] = {0x2e, 0x05, 0x1e, 0xc1, 0xff, 0xff, 0x91, 0xa1};
char test[8];
for(int i=0; i<8; i++)
test[i] = (char)buf[i];
printf("%s\n", test);
std::string value = test;
n1sm->setData( std::shared_ptr<std::istream>( new std::stringstream( utility::conversions::to_utf8string(value) ) ) );
//n1sm->setData( std::shared_ptr<std::istream>( new std::stringstream( "2e051ec1ffff91a17b00078000a00000d00" ) ) );
//n1sm->setData( std::shared_ptr<std::istream>(test));
smContextMessage->setBinaryDataN1SmMessage(n1sm);
std::shared_ptr<SMContextsCollectionApi> smContextsCollectionApi (new SMContextsCollectionApi (apiClient));
smContextsCollectionApi->postSmContexts (smContextMessage);
return 0;
}
#include "IndividualPDUSessionHSMFApi.h"
#include "IndividualSMContextApi.h"
#include "PDUSessionsCollectionApi.h"
#include "SMContextsCollectionApi.h"
#include "ApiConfiguration.h"
#include "ApiClient.h"
#include "nlohmann/json.hpp"
#include "logger.hpp"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#define DEFAULT_JSON_FILE "/home/oai/oai-5g-amf/src/sbi/smf_client/inputs/SmContextCreateData.json"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams // JSON library
using namespace oai::smf::api;
using namespace oai::smf::model;
//using json = nlohmann::json;
int smf_client_test();
{
"supi": "supi",
"pei": "pei",
"gpsi": "gpsi",
"pduSessionId": 1,
"dnn": "dnn",
"sNssai": {"sst":1, "sd":"sd"},
"hplmnSnssai": {"sst":1, "sd":"sd"},
"servingNfId": "",
"guami": {"plmnId": {"mcc": "122", "mnc":"13"}, "amfId":"2"},
"serviceName": "",
"servingNetwork": {"mcc": "122", "mnc":"13"},
"requestType": "",
"anType": "",
"secondAnType": "",
"ratType": "",
"presenceInLadn": "",
"ueTimeZone": "",
"smContextStatusUri": "",
"hSmfUri": "",
"additionalHsmfUri": [],
"oldPduSessionId": 1,
"pduSessionsActivateList": [1,2],
"ueEpsPdnConnection": "",
"hoState": "",
"pcfId": "",
"nrfUi": "",
"supportedFeatures": "",
"selMode": "",
"udmGroupId": "",
"routingIndicator": "",
"epsBearerCtxStatus": "",
"cpCiotEnabled": true,
"invokeNef": true,
"maPduIndication": false,
"smContextRef": "",
"n2SmInfo": {"contentId":"n2SmInfo"},
"n1SmMsg": {"contentId":"n1SmMsg"}
}
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include <iostream>
#include <string>
#include <unistd.h>
#include "amf.hpp"
/*
* To read content of the response from UDM
*/
#if 0
static std::size_t callback(
const char* in,
std::size_t size,
std::size_t num,
std::string* out)
{
const std::size_t totalBytes(size * num);
out->append(in, totalBytes);
return totalBytes;
}
#endif
#define ENCODE_U8(buffer, value, size) \
*(uint8_t*)(buffer) = value; \
size += sizeof(uint8_t)
extern void msg_str_2_msg_hex(std::string msg, bstring &b);
extern void convert_string_2_hex(std::string&input, std::string&output);
extern void print_buffer(const std::string app, const std::string commit, uint8_t *buf, int len);
extern unsigned char * format_string_as_hex(std::string str);
extern std::size_t callback(
const char* in,
std::size_t size,
std::size_t num,
std::string* out);
extern bool multipart_parser(std::string input, std::string &jsonData, std::string &n1sm, std::string &n2sm);
//---------------------------------------------------------------------------------------------
void send_pdu_session_establishment_request()
{
std::cout << "[AMF N11] PDU Session Establishment Request"<<std::endl;
nlohmann::json pdu_session_establishment_request;
std::string n1_msg = "2e0561c1ffff91a17b000780000a00000d00";
std::string n2_msg;
//format string as hex
unsigned char *n1_msg_hex = format_string_as_hex(n1_msg);
//Fill Json part
//get supi and put into URL
std::string supi_str;
std::string url = std::string("http://192.168.2.189:8889/nsmf-pdusession/v2/sm-contexts");
//Fill the json part
pdu_session_establishment_request["supi"] = "imsi-200000000000001";
pdu_session_establishment_request["pei"] = "imei-200000000000001";
pdu_session_establishment_request["gpsi"] = "msisdn-200000000001";
pdu_session_establishment_request["dnn"] = "carrier.com";
pdu_session_establishment_request["sNssai"]["sst"] = 222;
pdu_session_establishment_request["sNssai"]["sd"] = "0000D4";
pdu_session_establishment_request["pduSessionId"] = 5;
pdu_session_establishment_request["requestType"] = "INITIAL_REQUEST";
pdu_session_establishment_request["servingNfId"] = "servingNfId";
pdu_session_establishment_request["servingNetwork"]["mcc"] = "234";
pdu_session_establishment_request["servingNetwork"]["mnc"] = "067";
pdu_session_establishment_request["anType"] = "3GPP_ACCESS";
pdu_session_establishment_request["smContextStatusUri"] = "smContextStatusUri";
pdu_session_establishment_request["n1MessageContainer"]["n1MessageClass"] = "SM";
pdu_session_establishment_request["n1MessageContainer"]["n1MessageContent"]["contentId"] = "n1SmMsg"; //part 2
//N1SM
//pdu_session_establishment_request["n1SmMsg"] = "SM";
//pdu_session_establishment_request["n1SmMsg"]["contentId"] = "n1SmMsg"; //part 2
CURL *curl = curl_easy_init();
//N1N2MessageTransfer Notification URI??
std::string json_part = pdu_session_establishment_request.dump();
std::cout<< " Sending message to SMF....\n";
if(curl) {
std::cout << "send curl command"<<std::endl;
CURLcode res;
struct curl_slist *headers = nullptr;
struct curl_slist *slist = nullptr;
curl_mime *mime;
curl_mime *alt;
curl_mimepart *part;
//headers = curl_slist_append(headers, "charsets: utf-8");
headers = curl_slist_append(headers, "content-type: multipart/related");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 100L);
mime = curl_mime_init(curl);
alt = curl_mime_init(curl);
//part with N1N2MessageTransferReqData (JsonData)
part = curl_mime_addpart(mime);
curl_mime_data(part, json_part.c_str(), CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/json");
part = curl_mime_addpart(mime);
curl_mime_data(part, reinterpret_cast<const char*>(n1_msg_hex), n1_msg.length()/2);
//curl_mime_data(part, reinterpret_cast<const char*>(n1_msg_hex), CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/vnd.3gpp.5gnas");
//curl_mime_name (part, "n1SmMsg");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
//res = curl_easy_perform(curl);
// Response information.
long httpCode(0);
std::unique_ptr<std::string> httpData(new std::string());
// Hook up data handling function.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
//get cause from the response
std::string response = *httpData.get();
std::string jsonData = "";
std::string n1sm = "";
std::string n2sm = "";
if(httpCode != 200 && httpCode != 201){
if(!(multipart_parser(response, jsonData, n1sm, n2sm))){
std::cout << "Could not get the cause from the response" <<std::endl;
}
}
nlohmann::json response_data;
try{
response_data = nlohmann::json::parse(jsonData);
Logger::amf_server().debug("Get response with jsonData: %s", jsonData.c_str());
bstring n1sm_hex;
msg_str_2_msg_hex(n1sm, n1sm_hex);
print_buffer("amf_server", "Get response with n1sm:", (uint8_t*)bdata(n1sm_hex), blength(n1sm_hex));
} catch (nlohmann::json::exception& e){
//Set the default Cause
response_data["cause"] = "504 Gateway Timeout";
}
std::cout << "[AMF N11] PDU session establishment request, response from SMF, Http Code " << httpCode << " cause "<< response_data["cause"].dump().c_str()<<std::endl;
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
}
}
void send_pdu_session_update_sm_context_establishment()
{
std::cout << "[AMF N11] send_pdu_session_update_sm_context_establishment"<<std::endl;
nlohmann::json pdu_session_modification_request;
std::string n2_msg = "0003e0c0a8029d56ba263c003c";
//format string as hex
unsigned char *n2_msg_hex = format_string_as_hex(n2_msg);
//encode
size_t buffer_size = 128;
char *buffer = (char *)calloc(1,buffer_size);
int size = 0;
ENCODE_U8 (buffer, 0 , size);
ENCODE_U8 (buffer+size, 3 , size);
ENCODE_U8 (buffer+size, 0xe0 , size);
ENCODE_U8 (buffer+size, 0xac , size);
ENCODE_U8 (buffer+size, 0x0a , size);
ENCODE_U8 (buffer+size, 0x05 , size);
ENCODE_U8 (buffer+size, 0x01 , size);
ENCODE_U8 (buffer+size, 0x00 , size);
ENCODE_U8 (buffer+size, 0x00 , size);
ENCODE_U8 (buffer+size, 0x00 , size);
ENCODE_U8 (buffer+size, 0x01 , size);
ENCODE_U8 (buffer+size, 0x00 , size);
ENCODE_U8 (buffer+size, 0x00 , size);
/*
0000 00 00 04 00 82 00 04 00 01 00 02 00 8b 00 0a 01
0010 f0 04 03 02 01 7f 00 00 01 00 86 00 01 10 00 88
0020 00 07 00 3c 00 00 3c 00 00 00 00 00 00 00 00 00
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*/
std::cout << "Buffer: "<<std::endl;
for(int i=0;i<2;i++)
{
printf("%02x ", buffer[i]);
}
std::cout << "Buffer: "<<std::endl;
//Fill Json part
//get supi and put into URL
std::string supi_str;
//std::string url = std::string("http://172.16.1.101/nsmf-pdusession/v2/sm-contexts");
//std::string url = std::string("http://172.16.1.101/nsmf-pdusession/v2/sm-contexts/imsi-200000000000001/modify");
std::string url = std::string("http://192.168.2.189:8889/nsmf-pdusession/v2/sm-contexts/1/modify");
//Fill the json part
pdu_session_modification_request["n2SmInfoType"] = "PDU_RES_SETUP_RSP";
pdu_session_modification_request["n2SmInfo"]["contentId"] = "n2SmMsg"; //part 2
//N1SM
//pdu_session_establishment_request["n1SmMsg"] = "SM";
//pdu_session_establishment_request["n1SmMsg"]["contentId"] = "n1SmMsg"; //part 2
CURL *curl = curl_easy_init();
//N1N2MessageTransfer Notification URI??
std::string json_part = pdu_session_modification_request.dump();
std::cout<< " Sending message to SMF....\n";
if(curl) {
std::cout << "send curl command"<<std::endl;
CURLcode res;
struct curl_slist *headers = nullptr;
struct curl_slist *slist = nullptr;
curl_mime *mime;
curl_mime *alt;
curl_mimepart *part;
headers = curl_slist_append(headers, "charsets: utf-8");
headers = curl_slist_append(headers, "content-type: multipart/related");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 100L);
mime = curl_mime_init(curl);
alt = curl_mime_init(curl);
//part with N1N2MessageTransferReqData (JsonData)
part = curl_mime_addpart(mime);
curl_mime_data(part, json_part.c_str(), CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/json");
part = curl_mime_addpart(mime);
curl_mime_data(part, reinterpret_cast<const char*>(buffer), size);
//curl_mime_data(part, "\x00\x03\xe0\xac\x0a\x05\x01\x01\x01\x01\x01\x00\x00", CURL_ZERO_TERMINATED);
//curl_mime_data(part, "\x2e\x01\x01\xc1\xff\xff\x95", CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/vnd.3gpp.ngap");
curl_mime_name (part, "n2SmMsg");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
//res = curl_easy_perform(curl);
// Response information.
long httpCode(0);
std::unique_ptr<std::string> httpData(new std::string());
// Hook up data handling function.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
//get cause from the response
nlohmann::json response_data;
try{
response_data = nlohmann::json::parse(*httpData.get());
} catch (nlohmann::json::exception& e){
std::cout << "Could not get the cause from the response" <<std::endl;
//Set the default Cause
response_data["cause"] = "504 Gateway Timeout";
}
std::cout << "[AMF N11] PDU session modification request, response from SMF, Http Code " << httpCode << " cause "<< response_data["cause"].dump().c_str()<<std::endl;
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
}
}
void send_pdu_session_update_sm_context_modification()
{
std::cout << "[AMF N11] send_pdu_session_update_sm_context_modification"<<std::endl;
nlohmann::json pdu_session_modification_request;
std::string n2_msg = "0003e0ac0a0501000000010000";
//format string as hex
unsigned char *n2_msg_hex = format_string_as_hex(n2_msg);
//encode
size_t buffer_size = 128;
char *buffer = (char *)calloc(1,buffer_size);
int size = 0;
ENCODE_U8 (buffer, 0x2e , size);
ENCODE_U8 (buffer+size, 0x01 , size);
ENCODE_U8 (buffer+size, 0x01 , size);
ENCODE_U8 (buffer+size, 0xc1 , size);
ENCODE_U8 (buffer+size, 0xff , size);
ENCODE_U8 (buffer+size, 0xff , size);
ENCODE_U8 (buffer+size, 0x95 , size);
////step 1.a,UE-initiated: SM Context ID + N1 (PDU Session Modification Request)
std::cout << "Buffer: "<<std::endl;
for(int i=0;i<2;i++)
{
printf("%02x ", buffer[i]);
}
std::cout << "Buffer: "<<std::endl;
//Fill Json part
//get supi and put into URL
std::string supi_str;
//std::string url = std::string("http://172.16.1.101/nsmf-pdusession/v2/sm-contexts");
//std::string url = std::string("http://172.16.1.101/nsmf-pdusession/v2/sm-contexts/imsi-200000000000001/modify");
std::string url = std::string("http://172.16.1.101/nsmf-pdusession/v2/sm-contexts/1/modify");
//Fill the json part
pdu_session_modification_request["n1SmMsg"]["contentId"] = "n1SmMsg"; //part 2
//N1SM
//pdu_session_establishment_request["n1SmMsg"] = "SM";
//pdu_session_establishment_request["n1SmMsg"]["contentId"] = "n1SmMsg"; //part 2
CURL *curl = curl_easy_init();
//N1N2MessageTransfer Notification URI??
std::string json_part = pdu_session_modification_request.dump();
std::cout<< " Sending message to SMF....\n";
if(curl) {
std::cout << "send curl command"<<std::endl;
CURLcode res;
struct curl_slist *headers = nullptr;
struct curl_slist *slist = nullptr;
curl_mime *mime;
curl_mime *alt;
curl_mimepart *part;
headers = curl_slist_append(headers, "charsets: utf-8");
headers = curl_slist_append(headers, "content-type: multipart/related");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 100L);
mime = curl_mime_init(curl);
alt = curl_mime_init(curl);
//part with N1N2MessageTransferReqData (JsonData)
part = curl_mime_addpart(mime);
curl_mime_data(part, json_part.c_str(), CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/json");
part = curl_mime_addpart(mime);
curl_mime_data(part, reinterpret_cast<const char*>(buffer), size);
//curl_mime_data(part, "\x00\x03\xe0\xac\x0a\x05\x01\x01\x01\x01\x01\x00\x00", CURL_ZERO_TERMINATED);
//curl_mime_data(part, "\x2e\x01\x01\xc1\xff\xff\x95", CURL_ZERO_TERMINATED);
curl_mime_type(part, "application/vnd.3gpp.5gnas");
curl_mime_name (part, "n1SmMsg");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
//res = curl_easy_perform(curl);
// Response information.
long httpCode(0);
std::unique_ptr<std::string> httpData(new std::string());
// Hook up data handling function.
//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
//get cause from the response
nlohmann::json response_data;
try{
response_data = nlohmann::json::parse(*httpData.get());
} catch (nlohmann::json::exception& e){
std::cout << "Could not get the cause from the response" <<std::endl;
//Set the default Cause
response_data["cause"] = "504 Gateway Timeout";
}
std::cout << "[AMF N11] PDU session modification request, response from SMF, Http Code " << httpCode << " cause "<< response_data["cause"].dump().c_str()<<std::endl;
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_mime_free(mime);
}
}
int main_test()
{
send_pdu_session_establishment_request();
//usleep(100000);
//send_pdu_session_update_sm_context_establishment();
//send_pdu_session_update_sm_context_modification();
return 0;
}
extern void octet_stream_2_hex_stream(uint8_t *buf, int len, std::string &out);
void hexTest(){
std::string hexString;
uint8_t test[4] = {0x12, 0x43, 0x00, 0x45};
octet_stream_2_hex_stream(test, 4, hexString);
printf("hexStream: %s\n", hexString.c_str());
}
#include "authentication_algorithms_with_5gaka.hpp"
#include <stdlib.h>
#include "f12345.h"
//uint8_t key[16] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
//uint8_t key[16] = {0x03, 0x96, 0xeb, 0x31, 0x7b, 0x6d, 0x1c, 0x36, 0xf1, 0x9c, 0x1c, 0x84, 0xcd, 0x6f, 0xfd, 0x16};
//uint8_t opc[16] = {0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e, 0x48, 0xa5, 0x99, 0x4e, 0x37, 0xa0, 0x2b, 0xaf};
//uint8_t rand_[16] = {0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d, 0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35 };
//uint8_t sqn[6] = {0xff, 0x9b, 0xb4, 0xd0, 0xb6, 0x07};
//uint8_t amf[2] = {0xb9, 0xb9};
extern void print_buffer(const std::string app, const std::string commit, uint8_t *buf, int len);
int ff1_test(){
#if 0
uint8_t mac_a[8];
Authentication_5gaka::f1(opc, key, rand_, sqn, amf, mac_a);
//f1star(key, rand_, sqn, amf, mac_a);
printf("mac_a: 0x");
for(int i=0; i<8; i++)
printf("%x", mac_a[i]);
printf("\n");
#endif
#if 0
Authentication_5gaka::RijndaelKeySchedule(key);
uint8_t plain_text[16] = {0xee, 0x36, 0xf7, 0xcf, 0x03, 0x7d, 0x37, 0xd3, 0x69, 0x2f, 0x7f, 0x03, 0x99, 0xe7, 0x94, 0x9a};
//uint8_t plain_text[16] = {0x93, 0xcc, 0x36, 0x40, 0xc5, 0xd6, 0xa5, 0x21, 0xd8, 0x12, 0x35, 0xbd, 0x08, 0x82, 0xbf, 0x0a};
uint8_t output[16];
Authentication_5gaka::RijndaelEncrypt(plain_text, output);
printf("output: 0x");
for(int i=0; i<16; i++)
printf("%x", output[i]);
printf("\n");
#endif
#if 0
uint8_t op[16] = {0xcd, 0xc2, 0x02, 0xd5, 0x12, 0x3e, 0x20, 0xf6, 0x2b, 0x6d, 0x67, 0x6a, 0xc7, 0x2c, 0xb3, 0x18};
uint8_t opc[16];
Authentication_5gaka::ComputeOPc(key, op, opc);
uint8_t res[8], ck[16], ik[16], ak[6];
Authentication_5gaka::f2345(opc, key, rand_, res, ck, ik, ak);
printf("res: 0x");
for(int i=0; i<8; i++)
printf("%x ", res[i]);
printf("\n");
printf("ak: 0x");
for(int i=0; i<6; i++)
printf("%x ", ak[i]);
printf("\n");
printf("ck: 0x");
for(int i=0; i<16; i++)
printf("%x ", ck[i]);
printf("\n");
printf("ik: 0x");
for(int i=0; i<16; i++)
printf("%x ", ik[i]);
printf("\n");
uint8_t akstar[6];
Authentication_5gaka::f5star(opc, key, rand_, akstar);
printf("akstar: 0x");
for(int i=0; i<6; i++)
printf("%x ", akstar[i]);
printf("\n");
#endif
}
void string_to_utf8(const string & word, vector<string> & characters){
int num = word.size();
int i = 0;
while(i < num)
{
int size = 1;
if(word[i] & 0x80)
{
char temp = word[i];
temp <<= 1;
do{
temp <<= 1;
++size;
}while(temp & 0x80);
}
printf("size = %d", size);
string subWord;
subWord = word.substr(i, size);
characters.push_back(subWord);
i += size;
}
}
#if 1
void kdf_test(){
//string key = "00112233445566778899aabbccddeeff";
//OCTET_STRING_t K;
//OCTET_STRING_fromBuf(&K, key.c_str(), key.length());
uint8_t Key[32] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
//uint8_t Key[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
//uint8_t key[32] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc, 0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
uint8_t key[16] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
string msg = "k5G:mnc011.mcc460.3gppnetwork.org";
//string msg = "k";
OCTET_STRING_t M;
OCTET_STRING_fromBuf(&M, msg.c_str(), msg.length());
uint8_t O[16];
Authentication_5gaka::kdf(Key, 32, M.buf, M.size, O, 16);
//Authentication_5gaka::kdf(K.buf, K.size, M.buf, M.size, O, 16);
printf("KDF test ...\n");
for(int i=0; i<16; i++)
printf("%x ", O[i]);
printf("\n");
}
#endif
void rijndael_test(){
printf("RijndaelKeySchedule testing ...\n");
uint8_t key[16] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
uint8_t key1[16] = {0x03, 0x96, 0xeb, 0x31, 0x7b, 0x6d, 0x1c, 0x36, 0xf1, 0x9c, 0x1c, 0x84, 0xcd, 0x6f, 0xfd, 0x16};
Authentication_5gaka::RijndaelKeySchedule(key1);
uint8_t txt[16] = {0x93, 0xcc, 0x36, 0x40, 0xc5, 0xd6, 0xa5, 0x21, 0xd8, 0x12, 0x35, 0xbd, 0x08, 0x82, 0xbf, 0x0a};
uint8_t txt_enc[16];
Authentication_5gaka::RijndaelEncrypt(txt, txt_enc);
printf("encrypted text ...\n");
for(int i=0; i<16; i++)
printf("%x ",txt_enc[i]);
printf("\n");
}
void f1_test(){
uint8_t opc[16] = {0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e, 0x48, 0xa5, 0x99, 0x4e, 0x37, 0xa0, 0x2b, 0xaf};
uint8_t rand[16] = {0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d, 0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35};
uint8_t key[16] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
uint8_t sqn[6] = {0xff, 0x9b, 0xb4, 0xd0, 0xb6, 0x07};
uint8_t amf[2] = {0xb9, 0xb9};
uint8_t mac_a[8], mac_s[8];
Authentication_5gaka::f1(opc, key, rand, sqn, amf, mac_a);
Authentication_5gaka::f1star(opc, key, rand, sqn, amf, mac_s);
printf("testing f1...\n");
for(int i=0; i<8; i++)
printf("%x ",mac_a[i]);
printf("\n");
for(int i=0; i<8; i++)
printf("%x ",mac_s[i]);
printf("\n");
}
void f2345_test(){
//uint8_t opc[16] = {0xcd, 0x63, 0xcb, 0x71, 0x95, 0x4a, 0x9f, 0x4e, 0x48, 0xa5, 0x99, 0x4e, 0x37, 0xa0, 0x2b, 0xaf};
//uint8_t rand[16] = {0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d, 0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35};
//uint8_t key[16] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
uint8_t opc[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t key[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
//uint8_t rand[16] = {0x5b, 0x2e, 0x1c, 0x24, 0x28, 0xc7, 0x56, 0x28, 0xe4, 0x43, 0xb0, 0xac, 0x89, 0x5d, 0x5b, 0x73};
uint8_t rand[16] = {0x72, 0x13, 0x34, 0xee, 0x3e, 0xf1, 0xcd, 0x1b, 0x10, 0xfc, 0xe3, 0x63, 0xb4, 0x52, 0xde, 0xaa};
uint8_t res[8], ck[16], ik[16], ak[6];
Authentication_5gaka::f2345(opc, key, rand, res, ck, ik, ak);
printf("res: 0x\n");
for(int i=0; i<8; i++)
printf("%x", res[i]);
printf("\n");
printf("ak: 0x");
for(int i=0; i<6; i++)
printf("%x", ak[i]);
printf("\n");
printf("ck: 0x\n");
for(int i=0; i<16; i++)
printf("%x", ck[i]);
printf("\n");
printf("ik: 0x\n");
for(int i=0; i<16; i++)
printf("%x", ik[i]);
printf("\n");
uint8_t ak2[6];
Authentication_5gaka::f5star(opc, key, rand, ak2);
printf("ak2: 0x");
for(int i=0; i<6; i++)
printf("%x ", ak2[i]);
printf("\n");
}
void res_test(){
uint8_t rand[16] = {0x23, 0x55, 0x3c, 0xbe, 0x96, 0x37, 0xa8, 0x9d, 0x21, 0x8a, 0xe6, 0x4d, 0xae, 0x47, 0xbf, 0x35};
//uint8_t key[32] = {0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc, 0x46, 0x5b, 0x5c, 0xe8, 0xb1, 0x99, 0xb4, 0x9f, 0xaa, 0x5f, 0x0a, 0x2e, 0xe2, 0x38, 0xa6, 0xbc};
uint8_t key[32] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
string snn = "5G:mnc011.mcc460.3gppnetwork.org";
OCTET_STRING_t netName;
OCTET_STRING_fromBuf(&netName, snn.c_str(), snn.length());
uint8_t S[100];
S[0] = 0x6B;
memcpy (&S[1], netName.buf, netName.size);
printf("size = %x", netName.size);
S[1+netName.size] = (netName.size & 0xff00)>>8;
S[2+netName.size] = (netName.size & 0x00ff);
//for(int i=0; i<16; i++)
// S[3+netName.size+i] = rand[i];
//S[19+netName.size] = 0x00;
//S[20+netName.size] = 0x10;
uint8_t output[16];
//Authentication_5gaka::kdf(key, 32, K.buf, K.size, output, 16);
Authentication_5gaka::kdf(key, 32, S, 3+netName.size, output, 16);
printf("inputstring ...\n");
//for(int i=0; i< 1; i++)
for(int i=0; i< 3+netName.size; i++)
printf("%c", S[i]);
printf("\n");
printf("key ...\n");
for(int i=0; i< 32; i++)
printf("%x", key[i]);
printf("\n");
printf("out ...\n");
for(int i=0; i< 16; i++)
printf("%x ", output[i]);
printf("\n");
}
#include "nas_algorithms.hpp"
void nia1_test(){
uint32_t count = 0x38a6f056;
uint8_t key[16] = {0x2b, 0xd6, 0x45, 0x9f, 0x82, 0xc5, 0xb3, 0x00, 0x95, 0x2c, 0x49, 0x10, 0x48, 0x81, 0xff, 0x48};
uint8_t message[16] = {0x33, 0x32, 0x34, 0x62, 0x63, 0x39, 0x38, 0x61, 0x37, 0x34, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00};
nas_stream_cipher_t stream_cipher = {0};
uint8_t mac[4];
stream_cipher.key = key;
stream_cipher.key_length = 16;
stream_cipher.count = count;
stream_cipher.bearer = 0x1f;
stream_cipher.direction = 0;
stream_cipher.message = message;
stream_cipher.blength = 88;
nas_algorithms::nas_stream_encrypt_nia1 (&stream_cipher, mac);
uint32_t mac32 = ntohl(*((uint32_t*) mac));
printf("mac: 0x%x\n", mac32);
}
void nia2_test(){
uint32_t count = 0x38a6f056;
uint8_t key[16] = {0x2b, 0xd6, 0x45, 0x9f, 0x82, 0xc5, 0xb3, 0x00, 0x95, 0x2c, 0x49, 0x10, 0x48, 0x81, 0xff, 0x48};
uint8_t message[8] = {0x33, 0x32, 0x34, 0x62, 0x63, 0x39, 0x38, 0x40};
nas_stream_cipher_t stream_cipher = {0};
uint8_t mac[4];
stream_cipher.key = key;
stream_cipher.key_length = 16;
stream_cipher.count = count;
stream_cipher.bearer = 0x18;
stream_cipher.direction = 0;
stream_cipher.message = message;
stream_cipher.blength = 58;
nas_algorithms::nas_stream_encrypt_nia2 (&stream_cipher, mac);
uint32_t mac32 = ntohl(*((uint32_t*) mac));
printf("mac: 0x%x\n", mac32);
}
void buffer_test(){
uint8_t mac[4] = {0x11, 0x22, 0x33, 0x44};
uint8_t buffer[6] = {0};
uint32_t mac32;
mac32 = ntohl(*((uint32_t*)mac));
memcpy(buffer, &mac32, 4);
print_buffer("amf_n1", "uint8_t *mac", mac, 4);
printf("mac32: 0x%x\n", mac32);
print_buffer("amf_n1", "buffer", buffer, 4);
*((uint32_t*)buffer) = htonl(mac32);
print_buffer("amf_n1", "buffer2", buffer, 4);
}
void xresStar_test(){
uint8_t opc[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t key[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t rand[16] = {0x5b, 0x2e, 0x1c, 0x24, 0x28, 0xc7, 0x56, 0x28, 0xe4, 0x43, 0xb0, 0xac, 0x89, 0x5d, 0x5b, 0x73};
uint8_t res[8], ck[16], ik[16], ak[6];
Authentication_5gaka::f2345(opc, key, rand, res, ck, ik, ak);
std::string snn = "5G:mnc011.mcc460.3gppnetwork.org";
OCTET_STRING_t netName;
OCTET_STRING_fromBuf(&netName, snn.c_str(), snn.length());
uint8_t S[100];
S[0] = 0x6B;
memcpy (&S[1], netName.buf, netName.size);
printf("snn length(0x%x)\n", netName.size);
S[1+netName.size] = (netName.size & 0xff00)>>8;
S[2+netName.size] = (netName.size & 0x00ff);
for(int i=0; i<16; i++)
S[3+netName.size+i] = rand[i];
S[19+netName.size] = 0x00;
S[20+netName.size] = 0x10;
for(int i=0; i<8; i++)
S[21+netName.size+i] = res[i];
S[29+netName.size] = 0x00;
S[30+netName.size] = 0x08;
uint8_t ckik[32];
memcpy (&key[0], ck, 16);
memcpy (&key[16], ik, 16);//KEY
uint8_t out[32], output[16];
Authentication_5gaka::kdf(ckik, 32, S, 31+netName.size, out, 32);
for(int i=0; i<16; i++)
output[i] = out[16+i];
print_buffer("amf_n1", "XRES*", output, 16);
}
void mac_test(){
uint8_t opc[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t key[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t rand[16] = {0x6a, 0x89, 0x59, 0xfb, 0x18, 0x8c, 0x73, 0x30, 0x8d, 0x67, 0x9f, 0x7b, 0xc8, 0x31, 0x3d, 0x65};
//uint8_t rand[16] = {0x02, 0x27, 0x40, 0x55, 0xea, 0x5a, 0x2f, 0x7e, 0x3e, 0x87, 0x16, 0x4b, 0xdf, 0x80, 0xff, 0x32};
//uint8_t rand[16] = {0x97, 0xce, 0xf6, 0x4e, 0x75, 0xbf, 0x90, 0x7c, 0x01, 0xf0, 0x65, 0x6a, 0xbb, 0xf8, 0x84, 0x02};
uint8_t res[8], ck[16], ik[16], ak[6];
Authentication_5gaka::f2345(opc, key, rand, res, ck, ik, ak);
print_buffer("amf_n1", "XRES = F2(RAND)", res, 8);
print_buffer("amf_n1", "CK = F3(RAND)", ck, 16);
print_buffer("amf_n1", "IK = F4(RAND)", ik, 16);
print_buffer("amf_n1", "IK = F4(RAND)", ak, 16);
/*
uint8_t CK[16] = {0xb1, 0xaa, 0xdd, 0xdb, 0x49, 0x84, 0x0f, 0x49, 0x65, 0x6f, 0xf2, 0x70, 0x31, 0x44, 0x97, 0x86};
uint8_t IK[16] = {0x39, 0x8a, 0x1b, 0xc9, 0x2e, 0x35, 0xbd, 0x89, 0xb4, 0xe9, 0x5d, 0x03, 0xf4, 0x83, 0x5b, 0x37};
memcpy(ck, CK, 16);
memcpy(ik, IK, 16);
*/
/******************** XRES to XRES* **************************/
//std::string snnStr = "5G:mnc002.mcc460.3gppnetwork.org";
std::string snnStr = "5G:mnc011.mcc460.3gppnetwork.org";
uint8_t * snn = (uint8_t*)snnStr.c_str();
int len = snnStr.length();
uint8_t KEY[32];
memcpy(&KEY[0], ck, 16);
memcpy(&KEY[16], ik, 16);
print_buffer("amf_n1", "XRES* Derivation: KEY", KEY, 32);
uint8_t S[200];
S[0] = 0x6B;
memcpy(&S[1], snn, len);
S[1+len] = (len & 0xff00)>>8;
S[2+len] = len & 0x00ff;
memcpy(&S[3+len], rand, 16);
S[19+len] = 0x00;
S[20+len] = 0x10;
memcpy(&S[21+len], res, 8);
S[29+len] = 0x00;
S[30+len] = 0x08;
print_buffer("amf_n1", "XRES* Derivation: Input", S, 31+len);
uint8_t out[32], output[16];
Authentication_5gaka::kdf(KEY, 32, S, 31+len, out, 32);
memcpy(output, out+16, 16);
print_buffer("amf_n1" ,"Result of KDF: XRES*", output, 16);
/******************* MAC *******************************************/
//uint8_t sqnak[6] = {0xe4, 0x0f, 0x91, 0xf6, 0x89, 0xf3};
//uint8_t sqnak[6] = {0x0f, 0xa2, 0x08, 0x8b, 0x0d, 0xfa};
//uint8_t sqnak[6] = {0xcb, 0x9f, 0x58, 0xa7, 0x8d, 0x98};
uint8_t sqnak[6] = {0x97, 0x77, 0x9b, 0x30, 0x56, 0x86};
uint8_t sqn[6];
for(int i=0; i<6; i++)
sqn[i] = sqnak[i] ^ ak[i];
print_buffer("amf_n1", "sqn", sqn, 6);
uint8_t amf[2] = {0x90, 0x01};
uint8_t mac_a[8];
Authentication_5gaka::f1(opc, key, rand, sqn, amf, mac_a);
print_buffer("amf_n1", "mac_a", mac_a, 8);
/******************** KAUSF Derivation *****************************/
memset(S, 0, 200);
//print_buffer("amf_n1", "reset input S", S, 200);
S[0] = 0x6A;
memcpy(&S[1], snn, len);
S[1+len] = (len & 0xff00)>>8;
S[2+len] = len & 0x00ff;
memcpy(&S[3+len], sqnak, 6);
S[9+len] = 0x00;
S[10+len] = 0x06;
print_buffer("amf_n1", "Kausf Derivation: Input", S, 11+len);
uint8_t kausf[32];
Authentication_5gaka::kdf(KEY, 32, S, 11+len, kausf, 32);
print_buffer("amf_n1" ,"Result of KDF: Kausf", kausf, 32);
/******************* Kseaf Derivation *****************************/
memset(S, 0, 200);
//std::string newsnnStr = "5G:mnc002.mcc460.3gppnetwork.org";
std::string newsnnStr = "5G:mnc011.mcc460.3gppnetwork.org";
uint8_t * newsnn = (uint8_t*)newsnnStr.c_str();
len = newsnnStr.length();
S[0] = 0x6C;
memcpy(&S[1], newsnn, len);
S[1+len] = (len & 0xff00)>>8;
S[2+len] = len & 0x00ff;
print_buffer("amf_n1", "Kseaf Derivation: Input", S, 3+len);
uint8_t kseaf[32];
Authentication_5gaka::kdf(kausf, 32, S, 3+len, kseaf, 32);
print_buffer("amf_n1" ,"Result of KDF: Kseaf", kseaf, 32);
/******************* Kamf Derivation ******************************/
uint8_t abba[2] = {0x00, 0x00};
//std::string supiStr = "";
//std::string supiStr = "imsi-460110123456789";
//std::string supiStr = "460020100001103";
std::string supiStr = "460110123456789";
uint8_t *supi = (uint8_t*)supiStr.c_str();
len = supiStr.length();
//uint8_t supi[8] = {0x46, 0x0F, 0x11, 0x01, 0x23, 0x45, 0x67, 0x89};
//len = 8;
memset(S, 0, 200);
S[0] = 0x6D;
memcpy(&S[1], supi, len);
S[1+len] = (len & 0xff00)>>8;
S[2+len] = len & 0x00ff;
memcpy(&S[3+len], abba, 2);
S[5+len] = 0x00;
S[6+len] = 0x02;
print_buffer("amf_n1", "Kamf Derivation: Input", S, 7+len);
uint8_t kamf[32];
Authentication_5gaka::kdf(kseaf, 32, S, 7+len, kamf, 32);
print_buffer("amf_n1" ,"Result of KDF: Kamf", kamf, 32);
/******************** Knas_int with NIA1 Derivation ****************/
memset(S, 0, 200);
S[0] = 0x69;
S[1] = 0x02;
S[2] = 0x00;
S[3] = 0x01;
S[4] = 0x01;
S[5] = 0x00;
S[6] = 0x01;
print_buffer("amf_n1", "Kamf_int with NIA1 Derivation: Input", S, 7);
uint8_t kamf_int[16];
memset(out, 0, 32);
Authentication_5gaka::kdf(kamf, 32, S, 7, out, 32);
memcpy(kamf_int, out+16, 16);
print_buffer("amf_n1" ,"Result of KDF: Kamf_int", kamf_int, 16);
/********************** NIA1 Algrithms ******************************/
uint32_t count = 0;
uint8_t message[13] = {0x00 ,0x7e, 0x00, 0x5d, 0x01, 0x01, 0x02, 0xf0, 0xf0, 0xe1, 0x36, 0x01, 0x02};
nas_stream_cipher_t stream_cipher = {0};
uint8_t mac[4];
stream_cipher.key = kamf_int;
stream_cipher.key_length = 16;
stream_cipher.count = count;
stream_cipher.bearer = 0x01;
stream_cipher.direction = 1;
stream_cipher.message = message;
stream_cipher.blength = 13*8;
nas_algorithms::nas_stream_encrypt_nia1 (&stream_cipher, mac);
//uint32_t mac32 = ntohl(*((uint32_t*) mac));
print_buffer("amf_n1", "calculated MAC", mac, 4);
}
void new_mac_test(){
uint8_t Kausf_Input[43] = {0x6a, 0x35, 0x47, 0x3a, 0x6d, 0x6e, 0x63, 0x30, 0x30, 0x32, 0x2e, 0x6d, 0x63, 0x63, 0x34, 0x36, 0x30, 0x2e, 0x33, 0x67, 0x70, 0x70, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2e, 0x6f, 0x72, 0x67, 0x00, 0x20, 0x2b, 0x9f, 0x0a, 0xf9, 0x8d, 0x89, 0x00, 0x06};
uint8_t key[32] = {0xc4, 0xda, 0x59, 0x6a, 0xd1, 0x36, 0xe7, 0xe7, 0x45, 0x32, 0x40, 0x82, 0xdd, 0xa3, 0x30, 0x98, 0x58, 0x2b, 0x9a, 0x60, 0x5d, 0x73, 0x25, 0xf4, 0x3c, 0x2b, 0x8d, 0x5a, 0x98, 0x57, 0xc7, 0xa5};
uint8_t Kausf[32];
Authentication_5gaka::kdf(key, 32, Kausf_Input, 43, Kausf, 32);
print_buffer("amf_n1", "Kausf out", Kausf, 32);
uint8_t Kseaf[32];
uint8_t Kseaf_Input[35] = {0x6c, 0x35, 0x47, 0x3a, 0x6d, 0x6e, 0x63, 0x30, 0x30, 0x32, 0x2e, 0x6d, 0x63, 0x63, 0x34, 0x36, 0x30, 0x2e, 0x33, 0x67, 0x70, 0x70, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2e, 0x6f, 0x72, 0x67, 0x00, 0x20};
Authentication_5gaka::kdf(Kausf, 32, Kseaf_Input, 35, Kseaf, 32);
print_buffer("amf_n1", "Kseaf out", Kseaf, 32);
uint8_t Kamf[32];
uint8_t Kamf_Input[22] = {0x6d, 0x34, 0x36, 0x30, 0x30, 0x32, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x30, 0x33, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x02};
Authentication_5gaka::kdf(Kseaf, 32, Kamf_Input, 22, Kamf, 32);
print_buffer("amf_n1", "Kseaf out", Kamf, 32);
uint8_t Knas_int[16], out[32];
uint8_t Knas_int_Input[7] = {0x69, 0x02, 0x00, 0x01, 0x02, 0x00, 0x01};
Authentication_5gaka::kdf(Kamf, 32, Knas_int_Input, 7, out, 32);
print_buffer("amf_n1", "Knas out", out, 32);
memcpy(Knas_int, out+16, 16);
print_buffer("amf_n1", "Knas_int out", Knas_int, 16);
}
void istream_test(){
char c = '1';
}
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