diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | configure.ac | 1 | ||||
-rw-r--r-- | include/osmo-bts/gsm_data.h | 8 | ||||
-rw-r--r-- | src/common/bts.c | 15 | ||||
-rw-r--r-- | src/common/rsl.c | 6 | ||||
-rw-r--r-- | src/osmo-bts-sysmo/main.c | 2 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/cipher/Makefile.am | 8 | ||||
-rw-r--r-- | tests/cipher/cipher_test.c | 130 | ||||
-rw-r--r-- | tests/cipher/cipher_test.ok | 1 | ||||
-rw-r--r-- | tests/testsuite.at | 6 |
11 files changed, 175 insertions, 5 deletions
@@ -26,6 +26,7 @@ src/osmo-bts-sysmo/sysmobts-mgr tests/atconfig tests/package.m4 tests/paging/paging_test +tests/cipher/cipher_test tests/testsuite tests/testsuite.log diff --git a/configure.ac b/configure.ac index 6c2d9092..62523564 100644 --- a/configure.ac +++ b/configure.ac @@ -59,4 +59,5 @@ dnl src/osmo-bts-bb/Makefile include/osmo-bts/Makefile tests/Makefile tests/paging/Makefile + tests/cipher/Makefile Makefile) diff --git a/include/osmo-bts/gsm_data.h b/include/osmo-bts/gsm_data.h index 33f074a8..55eec964 100644 --- a/include/osmo-bts/gsm_data.h +++ b/include/osmo-bts/gsm_data.h @@ -56,7 +56,7 @@ struct gsm_bts_role_bts { char *rtp_bind_host; unsigned int rtp_jitter_buf_ms; struct { - uint8_t ciphers; + uint8_t ciphers; /* flags A5/1==0x1, A5/2==0x2, A5/3==0x4 */ } support; struct { uint8_t tc4_ctr; @@ -85,4 +85,10 @@ static inline struct femtol1_hdl *trx_femtol1_hdl(struct gsm_bts_trx *trx) void lchan_set_state(struct gsm_lchan *lchan, enum gsm_lchan_state state); +/* cipher code */ +#define CIPHER_A5(x) (1 << (x-1)) + +int bts_supports_cipher(struct gsm_bts_role_bts *bts, int rsl_cipher); + + #endif /* _GSM_DATA_H */ diff --git a/src/common/bts.c b/src/common/bts.c index 1375f4a5..8f6dc694 100644 --- a/src/common/bts.c +++ b/src/common/bts.c @@ -213,3 +213,18 @@ struct msgb *bts_agch_dequeue(struct gsm_bts *bts) return msgb_dequeue(&btsb->agch_queue); } + +int bts_supports_cipher(struct gsm_bts_role_bts *bts, int rsl_cipher) +{ + int sup; + + if (rsl_cipher < 1 || rsl_cipher > 8) + return -ENOTSUP; + + /* No encryption is always supported */ + if (rsl_cipher == 1) + return 1; + + sup = (1 << (rsl_cipher - 2)) & bts->support.ciphers; + return sup > 0; +} diff --git a/src/common/rsl.c b/src/common/rsl.c index d53e07b9..481686ed 100644 --- a/src/common/rsl.c +++ b/src/common/rsl.c @@ -593,11 +593,13 @@ static void copy_sacch_si_to_lchan(struct gsm_lchan *lchan) static int encr_info2lchan(struct gsm_lchan *lchan, const uint8_t *val, uint8_t len) { + int rc; struct gsm_bts_role_bts *btsb = bts_role_bts(lchan->ts->trx->bts); /* check if the encryption algorithm sent by BSC is supported! */ - if (!((1 << *val) & btsb->support.ciphers)) - return -ENOTSUP; + rc = bts_supports_cipher(btsb, *val); + if (rc != 1) + return rc; /* length can be '1' in case of no ciphering */ if (len < 1) diff --git a/src/osmo-bts-sysmo/main.c b/src/osmo-bts-sysmo/main.c index 137a0b16..a00120e4 100644 --- a/src/osmo-bts-sysmo/main.c +++ b/src/osmo-bts-sysmo/main.c @@ -257,7 +257,7 @@ int main(int argc, char **argv) exit(1); } btsb = bts_role_bts(bts); - btsb->support.ciphers = (1 << 0) | (1 << 1) | (1 << 2); + btsb->support.ciphers = CIPHER_A5(1) | CIPHER_A5(2) | CIPHER_A5(3); rc = vty_read_config_file(config_file, NULL); if (rc < 0) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 4ea0c5ba..1d548f95 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = paging +SUBDIRS = paging cipher # The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac diff --git a/tests/cipher/Makefile.am b/tests/cipher/Makefile.am new file mode 100644 index 00000000..bd6f15b3 --- /dev/null +++ b/tests/cipher/Makefile.am @@ -0,0 +1,8 @@ +INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(OPENBSC_INCDIR) +AM_CFLAGS = -Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(LIBOSMOTRAU_CFLAGS) +LDADD = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) $(LIBOSMOTRAU_LIBS) -lortp +noinst_PROGRAMS = cipher_test +EXTRA_DIST = cipher_test.ok + +cipher_test_SOURCES = cipher_test.c +cipher_test_LDADD = $(top_builddir)/src/common/libbts.a $(LDADD) diff --git a/tests/cipher/cipher_test.c b/tests/cipher/cipher_test.c new file mode 100644 index 00000000..7d037c21 --- /dev/null +++ b/tests/cipher/cipher_test.c @@ -0,0 +1,130 @@ +/* (C) 2012 by Holger Hans Peter Freyther + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <osmo-bts/bts.h> +#include <osmo-bts/logging.h> +#include <osmo-bts/paging.h> +#include <osmo-bts/gsm_data.h> + +#include <osmocom/core/talloc.h> + +#include <errno.h> +#include <unistd.h> + +static struct gsm_bts *bts; +static struct gsm_bts_role_bts *btsb; +int pcu_direct = 0; + +#define ASSERT_TRUE(rc) \ + if (!(rc)) { \ + printf("Assert failed in %s:%d.\n", \ + __FILE__, __LINE__); \ + abort(); \ + } + +static void test_cipher_parsing(void) +{ + int i; + + btsb->support.ciphers = 0; + + /* always support A5/0 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x0) == -ENOTSUP); + ASSERT_TRUE(bts_supports_cipher(btsb, 0x1) == 1); /* A5/0 */ + for (i = 2; i <= 8; ++i) { + ASSERT_TRUE(bts_supports_cipher(btsb, i) == 0); + } + + /* checking default A5/1 to A5/3 support */ + btsb->support.ciphers = CIPHER_A5(1) | CIPHER_A5(2) | CIPHER_A5(3); + ASSERT_TRUE(bts_supports_cipher(btsb, 0x0) == -ENOTSUP); + ASSERT_TRUE(bts_supports_cipher(btsb, 0x1) == 1); /* A5/0 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x2) == 1); /* A5/1 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x3) == 1); /* A5/2 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x4) == 1); /* A5/3 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x5) == 0); /* A5/4 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x6) == 0); /* A5/5 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x7) == 0); /* A5/6 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x8) == 0); /* A5/7 */ + ASSERT_TRUE(bts_supports_cipher(btsb, 0x9) == -ENOTSUP); +} + +int main(int argc, char **argv) +{ + void *tall_msgb_ctx; + + tall_bts_ctx = talloc_named_const(NULL, 1, "OsmoBTS context"); + tall_msgb_ctx = talloc_named_const(tall_bts_ctx, 1, "msgb"); + msgb_set_talloc_ctx(tall_msgb_ctx); + + bts_log_init(NULL); + + bts = gsm_bts_alloc(tall_bts_ctx); + if (bts_init(bts) < 0) { + fprintf(stderr, "unable to to open bts\n"); + exit(1); + } + + btsb = bts_role_bts(bts); + test_cipher_parsing(); + printf("Success\n"); + + return 0; +} + +/* stub to link */ +const uint8_t abis_mac[6] = { 0,1,2,3,4,5 }; +const char *software_version = "0815"; + +int bts_model_chg_adm_state(struct gsm_bts *bts, struct gsm_abis_mo *mo, + void *obj, uint8_t adm_state) +{ return 0; } +int bts_model_init(struct gsm_bts *bts) +{ return 0; } +int bts_model_apply_oml(struct gsm_bts *bts, struct msgb *msg, + struct tlv_parsed *new_attr, void *obj) +{ return 0; } +int bts_model_rsl_chan_rel(struct gsm_lchan *lchan) +{ return 0;} + +int bts_model_rsl_deact_sacch(struct gsm_lchan *lchan) +{ return 0; } + +int bts_model_trx_deact_rf(struct gsm_bts_trx *trx) +{ return 0; } +int bts_model_check_oml(struct gsm_bts *bts, uint8_t msg_type, + struct tlv_parsed *old_attr, struct tlv_parsed *new_attr, + void *obj) +{ return 0; } +int bts_model_opstart(struct gsm_bts *bts, struct gsm_abis_mo *mo, + void *obj) +{ return 0; } +int bts_model_rsl_chan_act(struct gsm_lchan *lchan, struct tlv_parsed *tp) +{ return 0; } +int bts_model_rsl_mode_modify(struct gsm_lchan *lchan) +{ return 0; } +void bts_model_rtp_rx_cb(struct osmo_rtp_socket *rs, const uint8_t *rtp_pl, + unsigned int rtp_pl_len) {} + +int l1if_pdch_req(struct gsm_bts_trx_ts *ts, int is_ptcch, uint32_t fn, + uint16_t arfcn, uint8_t block_nr, uint8_t *data, uint8_t len) +{ return 0; } + +uint32_t trx_get_hlayer1(struct gsm_bts_trx *trx) +{ return 0; } diff --git a/tests/cipher/cipher_test.ok b/tests/cipher/cipher_test.ok new file mode 100644 index 00000000..35821117 --- /dev/null +++ b/tests/cipher/cipher_test.ok @@ -0,0 +1 @@ +Success diff --git a/tests/testsuite.at b/tests/testsuite.at index d97f27b3..357bfcbc 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -6,3 +6,9 @@ AT_KEYWORDS([paging]) cat $abs_srcdir/paging/paging_test.ok > expout AT_CHECK([$abs_top_builddir/tests/paging/paging_test], [], [expout], [ignore]) AT_CLEANUP + +AT_SETUP([cipher]) +AT_KEYWORDS([cipher]) +cat $abs_srcdir/cipher/cipher_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/cipher/cipher_test], [], [expout], [ignore]) +AT_CLEANUP |