From b478dd38d29b0dfdf9d9c4d6f22ec4a24e4c8fcd Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Wed, 29 Mar 2017 15:50:05 +0200 Subject: [PATCH] gsm0808: Add create functions for CIPHER MODE COMMAND gsm0808.h/c lacks functionality to generate CIPHER MODE COMMAND messages. These messages are required if the code is used in an MSC implementation. This commit adds a gsm0808_create_cipher() function, that generates an A/AoiP CIPHER MODE COMMAND message. Change-Id: I8eb1c357860c3e740b0f5d17e1c256bc87920958 --- include/osmocom/gsm/gsm0808.h | 2 ++ src/gsm/gsm0808.c | 33 ++++++++++++++++++++++++++++ src/gsm/libosmogsm.map | 1 + tests/gsm0808/gsm0808_test.c | 41 +++++++++++++++++++++++++++++++++++ tests/gsm0808/gsm0808_test.ok | 1 + 5 files changed, 78 insertions(+) diff --git a/include/osmocom/gsm/gsm0808.h b/include/osmocom/gsm/gsm0808.h index fd73376a4..9738a5542 100644 --- a/include/osmocom/gsm/gsm0808.h +++ b/include/osmocom/gsm/gsm0808.h @@ -35,6 +35,8 @@ struct msgb *gsm0808_create_reset(void); struct msgb *gsm0808_create_reset_ack(void); struct msgb *gsm0808_create_clear_command(uint8_t reason); struct msgb *gsm0808_create_clear_complete(void); +struct msgb *gsm0808_create_cipher(const struct gsm0808_encrypt_info *ei, + const uint8_t *cipher_response_mode); struct msgb *gsm0808_create_cipher_complete(struct msgb *layer3, uint8_t alg_id); struct msgb *gsm0808_create_cipher_reject(uint8_t cause); struct msgb *gsm0808_create_classmark_update(const uint8_t *cm2, uint8_t cm2_len, diff --git a/src/gsm/gsm0808.c b/src/gsm/gsm0808.c index b8ab79b12..c952a9f8c 100644 --- a/src/gsm/gsm0808.c +++ b/src/gsm/gsm0808.c @@ -131,6 +131,39 @@ struct msgb *gsm0808_create_clear_command(uint8_t reason) return msg; } +struct msgb *gsm0808_create_cipher(const struct gsm0808_encrypt_info *ei, + const uint8_t *cipher_response_mode) +{ + /* See also: 3GPP TS 48.008 3.2.1.30 CIPHER MODE COMMAND */ + struct msgb *msg; + + /* Mandatory emelent! */ + OSMO_ASSERT(ei); + + msg = + msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, + "cipher-mode-command"); + if (!msg) + return NULL; + + /* Message Type 3.2.2.1 */ + msgb_v_put(msg, BSS_MAP_MSG_CIPHER_MODE_CMD); + + /* Encryption Information 3.2.2.10 */ + gsm0808_enc_encrypt_info(msg, ei); + + /* Cipher Response Mode 3.2.2.34 */ + if (cipher_response_mode) + msgb_tv_put(msg, GSM0808_IE_CIPHER_RESPONSE_MODE, + *cipher_response_mode); + + /* pre-pend the header */ + msg->l3h = + msgb_tv_push(msg, BSSAP_MSG_BSS_MANAGEMENT, msgb_length(msg)); + + return msg; +} + struct msgb *gsm0808_create_cipher_complete(struct msgb *layer3, uint8_t alg_id) { struct msgb *msg = msgb_alloc_headroom(BSSMAP_MSG_SIZE, BSSMAP_MSG_HEADROOM, diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map index cf0a7fee2..786bf08bb 100644 --- a/src/gsm/libosmogsm.map +++ b/src/gsm/libosmogsm.map @@ -128,6 +128,7 @@ gsm0808_create_assignment_completed; gsm0808_create_ass_compl; gsm0808_create_assignment_failure; gsm0808_create_ass_fail; +gsm0808_create_cipher; gsm0808_create_cipher_complete; gsm0808_create_cipher_reject; gsm0808_create_classmark_update; diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c index af457b4d0..f33e0bd15 100644 --- a/tests/gsm0808/gsm0808_test.c +++ b/tests/gsm0808/gsm0808_test.c @@ -144,6 +144,46 @@ static void test_create_clear_complete() msgb_free(msg); } +static void test_create_cipher() +{ + static const uint8_t res[] = + { 0x00, 0x0c, 0x53, 0x0a, 0x09, 0x03, 0xaa, + 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 }; + static const uint8_t res2[] = + { 0x00, 0x0e, 0x53, 0x0a, 0x09, 0x03, 0xaa, + 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42, + GSM0808_IE_CIPHER_RESPONSE_MODE, 0x01 }; + struct msgb *msg; + struct gsm0808_encrypt_info ei; + uint8_t include_imeisv; + + memset(&ei, 0, sizeof(ei)); + ei.perm_algo[0] = GSM0808_ALG_ID_A5_0; + ei.perm_algo[1] = GSM0808_ALG_ID_A5_1; + ei.perm_algo_len = 2; + ei.key[0] = 0xaa; + ei.key[1] = 0xbb; + ei.key[2] = 0xcc; + ei.key[3] = 0xdd; + ei.key[4] = 0xee; + ei.key[5] = 0xff; + ei.key[6] = 0x23; + ei.key[7] = 0x42; + ei.key_len = 8; + include_imeisv = 1; + + printf("Testing creating Chipher Mode Command\n"); + msg = gsm0808_create_cipher(&ei, NULL); + OSMO_ASSERT(msg); + VERIFY(msg, res, ARRAY_SIZE(res)); + msgb_free(msg); + + msg = gsm0808_create_cipher(&ei, &include_imeisv); + OSMO_ASSERT(msg); + VERIFY(msg, res2, ARRAY_SIZE(res2)); + msgb_free(msg); +} + static void test_create_cipher_complete() { static const uint8_t res1[] = { @@ -700,6 +740,7 @@ int main(int argc, char **argv) test_create_reset(); test_create_clear_command(); test_create_clear_complete(); + test_create_cipher(); test_create_cipher_complete(); test_create_cipher_reject(); test_create_cm_u(); diff --git a/tests/gsm0808/gsm0808_test.ok b/tests/gsm0808/gsm0808_test.ok index f406551c1..8e2087d83 100644 --- a/tests/gsm0808/gsm0808_test.ok +++ b/tests/gsm0808/gsm0808_test.ok @@ -4,6 +4,7 @@ Testing creating Layer3 (AoIP) Testing creating Reset Testing creating Clear Command Testing creating Clear Complete +Testing creating Chipher Mode Command Testing creating Cipher Complete Testing creating Cipher Reject Testing creating CM U