update it

This commit is contained in:
Sukchan Lee 2017-03-02 22:47:43 +09:00
parent 604817869f
commit 32d1d630bc
8 changed files with 125 additions and 37 deletions

View File

@ -78,10 +78,17 @@ extern "C" {
#define c_max(x , y) (((x) > (y)) ? (x) : (y))
#define c_min(x , y) (((x) < (y)) ? (x) : (y))
#define c_uint64_to_array(array, uint64) \
{ \
int i = 0; \
for (i = 0; i < 8; i++) array[i] = ((uint64 >> (8-1-i) * 8) & 0xff); \
}
CORE_DECLARE(status_t) core_generate_random_bytes(
unsigned char *buf, int length);
CORE_DECLARE(void *) core_ascii_to_hex(char *in, int len, char *out);
CORE_DECLARE(void *) core_uint64_to_array(c_uint8_t *array, c_uint64_t num);
/** @} */

View File

@ -13,3 +13,12 @@ void *core_ascii_to_hex(char *in, int len, char *out)
return out;
}
void *core_uint64_to_array(c_uint8_t *array, c_uint64_t num)
{
int i;
for (i = 0; i < 8; i++)
array[i] = (num >> (8-1-i) * 8) & 0xff;
return array;
}

View File

@ -33,12 +33,22 @@ static void misc_test2(abts_case *tc, void *data)
core_ascii_to_hex(AMF, strlen(AMF), buffer), 2) == 0);
}
static void misc_test3(abts_case *tc, void *data)
{
c_uint64_t num = 0x0123456789abcdef;
c_uint8_t array[8];
c_uint8_t tmp[8] = "\x01\x23\x45\x67\x89\xab\xcd\xef";
ABTS_TRUE(tc, memcmp(tmp, core_uint64_to_array(array, num), 8) == 0);
}
abts_suite *testmisc(abts_suite *suite)
{
suite = ADD_SUITE(suite)
abts_run_test(suite, misc_test1, NULL);
abts_run_test(suite, misc_test2, NULL);
abts_run_test(suite, misc_test3, NULL);
return suite;
}

View File

@ -2,16 +2,35 @@
#include "core_debug.h"
#include "core_pool.h"
#include "core_lib.h"
#include "hss_ctx.h"
#define OP "5F1D289C5D354D0A140C2548F5F3E3BA"
#define OPc "E8ED289DEBA952E4283B54E88E6183CA"
#define AMF "8000"
static hss_ctx_t self;
pool_declare(ue_pool, ue_ctx_t, SIZE_OF_UE_POOL);
static list_t g_ue_list;
hss_ctx_t* hss_self()
{
return &self;
}
status_t hss_ctx_init(void)
{
char buf[MAX_KEY_LEN];
pool_init(&ue_pool, SIZE_OF_UE_POOL);
memset(&self, 0, sizeof(hss_ctx_t));
memcpy(self.op, core_ascii_to_hex(OP, strlen(OP), buf), MAX_KEY_LEN);
memcpy(self.amf, core_ascii_to_hex(AMF, strlen(AMF), buf), MAX_KEY_LEN);
return CORE_OK;
}
@ -67,14 +86,14 @@ status_t hss_ue_ctx_remove_all()
return CORE_OK;
}
ue_ctx_t* hss_ue_ctx_find_by_imsi(c_uint8_t *imsi)
ue_ctx_t* hss_ue_ctx_find_by_imsi(c_uint8_t *imsi, c_uint8_t imsi_len)
{
ue_ctx_t *ue = NULL;
ue = list_first(&g_ue_list);
while (ue)
{
if (memcmp(ue->imsi, imsi, ue->imsi_len) == 0)
if (memcmp(ue->imsi, imsi, imsi_len) == 0)
break;
ue = list_next(ue);

View File

@ -17,24 +17,33 @@ extern "C" {
typedef struct _ue_ctx_t {
lnode_t node; /**< A node of list_t */
c_uint8_t imsi[MAX_IMSI_LEN+1];
c_uint8_t imsi_len;
c_uint8_t imsi[MAX_IMSI_LEN+1];
c_uint8_t imsi_len;
c_uint8_t k[MAX_KEY_LEN];
c_uint8_t op[MAX_KEY_LEN];
c_uint8_t opc[MAX_KEY_LEN];
c_uint8_t amf[MAX_AMF_LEN];
c_uint8_t k[MAX_KEY_LEN];
c_uint64_t seq;
c_uint8_t rand[MAX_KEY_LEN];
c_uint8_t opc[MAX_KEY_LEN];
} ue_ctx_t;
CORE_DECLARE(status_t) hss_ctx_init(void);
CORE_DECLARE(void) hss_ctx_final(void);
typedef struct _hss_ctx_t {
c_uint8_t op[MAX_KEY_LEN];
c_uint8_t amf[MAX_KEY_LEN];
} hss_ctx_t;
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_add(void);
CORE_DECLARE(status_t) hss_ue_ctx_remove(ue_ctx_t *ue);
CORE_DECLARE(status_t) hss_ue_ctx_remove_all(void);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_find_by_imsi(c_uint8_t *imsi);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_first(void);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_next(ue_ctx_t *ue);
CORE_DECLARE(hss_ctx_t*) hss_self(void);
#define self() hss_self()
CORE_DECLARE(status_t) hss_ctx_init(void);
CORE_DECLARE(void) hss_ctx_final(void);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_add(void);
CORE_DECLARE(status_t) hss_ue_ctx_remove(ue_ctx_t *ue);
CORE_DECLARE(status_t) hss_ue_ctx_remove_all(void);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_find_by_imsi(
c_uint8_t *imsi, c_uint8_t imsi_len);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_first(void);
CORE_DECLARE(ue_ctx_t*) hss_ue_ctx_next(ue_ctx_t *ue);
#ifdef __cplusplus
}

View File

@ -4,6 +4,8 @@
#include "core_pool.h"
#include "core_lib.h"
#include "milenage.h"
#include "hss_ctx.h"
#include "s6a_app.h"
@ -26,7 +28,16 @@ static int hss_air_cb( struct msg **msg, struct avp *avp,
{
struct msg *ans, *qry;
struct avp *avpch1, *avpch2;
struct avp_hdr *hdr;
union avp_value val;
ue_ctx_t *ue = NULL;
c_uint8_t mac_a[8];
c_uint8_t seq[6];
c_uint8_t xres[8];
c_uint8_t ak[6];
c_uint8_t autn[16];
int i;
if (msg == NULL)
return EINVAL;
@ -35,7 +46,27 @@ static int hss_air_cb( struct msg **msg, struct avp *avp,
qry = *msg;
d_assert(fd_msg_new_answer_from_req(fd_g_config->cnf_dict, msg, 0) == 0,
return -1,);
ans = *msg;
ans = *msg;
d_assert(fd_msg_search_avp(qry, s6a_user_name, &avp) && avp,goto out,);
d_assert(fd_msg_avp_hdr(avp, &hdr) && hdr,,);
ue = hss_ue_ctx_find_by_imsi(hdr->avp_value->os.data,
hdr->avp_value->os.len);
d_assert(ue, goto out,);
core_generate_random_bytes(ue->rand, MAX_KEY_LEN);
milenage_opc(ue->k, hss_self()->op, ue->opc);
milenage_f1(ue->opc, ue->k, ue->rand, core_uint64_to_array(seq, ue->seq),
hss_self()->amf, mac_a, NULL);
milenage_f2345(ue->opc, ue->k, ue->rand, xres, NULL, NULL, ak, NULL);
for ( i = 0; i < 6; i++)
autn[i] = seq[i] ^ ak[i];
memcpy(&autn[6], hss_self()->amf, 2);
memcpy(&autn[7], mac_a, 8);
ue->seq = (ue->seq + 32) & 0x7ffffffffff;
/* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
d_assert(fd_msg_rescode_set(ans, "DIAMETER_SUCCESS", NULL, NULL, 1) == 0,
@ -98,6 +129,7 @@ static int hss_air_cb( struct msg **msg, struct avp *avp,
return 0;
out:
d_assert(fd_msg_free(qry) == 0,,);
d_assert(fd_msg_free(ans) == 0,,);
return -1;
@ -112,40 +144,25 @@ int hss_init(void)
/* FIXME : this is a sample UE for testing */
{
ue_ctx_t *ue;
char buffer[MAX_KEY_LEN];
char buf[MAX_KEY_LEN];
#define K "465B5CE8B199B49FAA5F0A2EE238A6BC"
#define OP "5F1D289C5D354D0A140C2548F5F3E3BA"
#define OPc "E8ED289DEBA952E4283B54E88E6183CA"
#define AMF "8000"
#define UE1_IMSI "001010123456800"
#define UE2_IMSI "001010123456796"
ue = hss_ue_ctx_add();
d_assert(ue, return -1, "UE context add failed");
#define UE1_IMSI "001010123456800"
strcpy((char*)ue->imsi, UE1_IMSI);
ue->imsi_len = strlen(UE1_IMSI);
memcpy(ue->k, core_ascii_to_hex(K, strlen(K), buffer), MAX_KEY_LEN);
memcpy(ue->op, core_ascii_to_hex(OP, strlen(OP), buffer), MAX_KEY_LEN);
memcpy(ue->opc,
core_ascii_to_hex(OPc, strlen(OPc), buffer), MAX_KEY_LEN);
memcpy(ue->amf,
core_ascii_to_hex(AMF, strlen(AMF), buffer), MAX_KEY_LEN);
memcpy(ue->k, core_ascii_to_hex(K, strlen(K), buf), MAX_KEY_LEN);
ue = hss_ue_ctx_add();
d_assert(ue, return -1, "UE context add failed");
#define UE2_IMSI "001010123456796"
strcpy((char*)ue->imsi, UE2_IMSI);
ue->imsi_len = strlen(UE2_IMSI);
memcpy(ue->k, core_ascii_to_hex(K, strlen(K), buffer), MAX_KEY_LEN);
memcpy(ue->op, core_ascii_to_hex(OP, strlen(OP), buffer), MAX_KEY_LEN);
memcpy(ue->opc,
core_ascii_to_hex(OPc, strlen(OPc), buffer), MAX_KEY_LEN);
memcpy(ue->amf,
core_ascii_to_hex(AMF, strlen(AMF), buffer), MAX_KEY_LEN);
memcpy(ue->k, core_ascii_to_hex(K, strlen(K), buf), MAX_KEY_LEN);
}
memset(&data, 0, sizeof(data));

View File

@ -341,3 +341,18 @@ int milenage_check(const c_uint8_t *opc, const c_uint8_t *k,
return 0;
}
int milenage_opc(const c_uint8_t *k, const c_uint8_t *op, c_uint8_t *opc)
{
int i;
if (aes_128_encrypt_block(k, op, opc))
return -1;
for (i = 0; i < 16; i++)
{
opc[i] ^= op[i];
}
return 0;
}

View File

@ -30,4 +30,6 @@ int milenage_f2345(const c_uint8_t *opc, const c_uint8_t *k,
const c_uint8_t *_rand, c_uint8_t *res, c_uint8_t *ck, c_uint8_t *ik,
c_uint8_t *ak, c_uint8_t *akstar);
int milenage_opc(const c_uint8_t *k, const c_uint8_t *op, c_uint8_t *opc);
#endif /* MILENAGE_H */