From 498e55a9a4a99aaea7e62b8a477a978e35a597f2 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 15 Jun 2021 01:09:37 +0200 Subject: implement A5/4 in Ciphering Mode procedure Receive and store the Kc128 key from MSC, and use as key sent to BTS if A5/4 is the chosen encryption algorithm. (A5/4 in handover will follow in a separate patch) Related: SYS#5324 Change-Id: I7c458c8a7350f34ff79531b3c891e1b367614469 --- include/osmocom/bsc/gsm_data.h | 2 ++ src/osmo-bsc/abis_rsl.c | 8 ++++++++ src/osmo-bsc/osmo_bsc_bssap.c | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/osmocom/bsc/gsm_data.h b/include/osmocom/bsc/gsm_data.h index 2a73f462a..1755b92d7 100644 --- a/include/osmocom/bsc/gsm_data.h +++ b/include/osmocom/bsc/gsm_data.h @@ -554,6 +554,8 @@ struct gsm_encr { uint8_t alg_id; uint8_t key_len; uint8_t key[MAX_A5_KEY_LEN]; + bool kc128_present; + uint8_t kc128[16]; }; #define LOGPLCHAN(lchan, ss, level, fmt, args...) \ diff --git a/src/osmo-bsc/abis_rsl.c b/src/osmo-bsc/abis_rsl.c index c844f181e..86b179067 100644 --- a/src/osmo-bsc/abis_rsl.c +++ b/src/osmo-bsc/abis_rsl.c @@ -178,6 +178,14 @@ static int build_encr_info(uint8_t *out, struct gsm_lchan *lchan) memcpy(out, lchan->encr.key, lchan->encr.key_len); return 1 + lchan->encr.key_len; + case GSM0808_ALG_ID_A5_4: + if (!lchan->encr.kc128_present) { + LOG_LCHAN(lchan, LOGL_ERROR, "A5/4 encryption chosen, but missing Kc128\n"); + return -EINVAL; + } + memcpy(out, lchan->encr.kc128, sizeof(lchan->encr.kc128)); + return 1 + sizeof(lchan->encr.kc128); + default: LOG_LCHAN(lchan, LOGL_ERROR, "A5/%d encryption not supported\n", lchan->encr.alg_id); return -EINVAL; diff --git a/src/osmo-bsc/osmo_bsc_bssap.c b/src/osmo-bsc/osmo_bsc_bssap.c index 9177e6b00..6f0caa76d 100644 --- a/src/osmo-bsc/osmo_bsc_bssap.c +++ b/src/osmo-bsc/osmo_bsc_bssap.c @@ -466,6 +466,7 @@ static int bssmap_handle_cipher_mode(struct gsm_subscriber_connection *conn, uint16_t enc_key_len; uint8_t enc_bits_msc; int chosen_cipher; + const struct tlv_p_entry *ie_kc128; if (!conn || !conn->lchan) { LOGP(DMSC, LOGL_ERROR, "No lchan/msc_data in cipher mode command.\n"); @@ -544,9 +545,26 @@ static int bssmap_handle_cipher_mode(struct gsm_subscriber_connection *conn, conn->lchan->encr.key_len = enc_key_len; memcpy(conn->lchan->encr.key, enc_key, enc_key_len); } + if ((ie_kc128 = TLVP_GET(&tp, GSM0808_IE_KC_128))) { + if (ie_kc128->len != sizeof(conn->lchan->encr.kc128)) { + LOGPFSML(conn->fi, LOGL_ERROR, "Kc128 IE has wrong length: %u (expect %zu)\n", + ie_kc128->len, sizeof(conn->lchan->encr.kc128)); + reject_cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; + goto reject; + } + memcpy(conn->lchan->encr.kc128, ie_kc128->val, sizeof(conn->lchan->encr.kc128)); + conn->lchan->encr.kc128_present = true; + } + + if (chosen_cipher == 4 && !conn->lchan->encr.kc128_present) { + LOGPFSML(conn->fi, LOGL_ERROR, "A5/4 encryption selected, but no Kc128\n"); + reject_cause = GSM0808_CAUSE_INFORMATION_ELEMENT_OR_FIELD_MISSING; + goto reject; + } - LOGP(DRSL, LOGL_DEBUG, "(subscr %s) Cipher Mode: cipher=%d key=%s include_imeisv=%d\n", + LOGP(DRSL, LOGL_DEBUG, "(subscr %s) Cipher Mode: cipher=%d key=%s kc128=%s include_imeisv=%d\n", bsc_subscr_name(conn->bsub), chosen_cipher, osmo_hexdump_nospc(enc_key, enc_key_len), + ie_kc128? osmo_hexdump_nospc_c(OTC_SELECT, ie_kc128->val, ie_kc128->len) : "-", include_imeisv); if (gsm48_send_rr_ciph_mode(conn->lchan, include_imeisv) < 0) { -- cgit v1.2.3