From 0b588be87e7bc506c45f333e060c29b4a5634c6f Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 30 Jul 2018 18:25:31 +0200 Subject: migrate to oap_client in libosmogsm libosmogsm in libosmocore.git from Change-Id Ie36729996abd30b84d1c30a09f62ebc6a9794950 onwards contains oap_client.c, so we don't need our local copy here in this repo anymore. Change-Id: I7b194f98ef3f925b6178d8a8dbd9fcf2f0c6e132 Requires: libosmocore.git Change-Id Ie36729996abd30b84d1c30a09f62ebc6a9794950 --- configure.ac | 1 - include/osmocom/sgsn/Makefile.am | 1 - include/osmocom/sgsn/gsup_client.h | 6 +- include/osmocom/sgsn/oap_client.h | 82 ----------- include/osmocom/sgsn/sgsn.h | 4 +- src/gprs/Makefile.am | 1 - src/gprs/gsup_client.c | 10 +- src/gprs/oap_client.c | 280 ------------------------------------- src/libcommon/gsup_client.c | 10 +- src/libcommon/oap_client.c | 280 ------------------------------------- tests/Makefile.am | 1 - tests/oap/Makefile.am | 32 ----- tests/oap/oap_client_test.c | 271 ----------------------------------- tests/oap/oap_client_test.err | 35 ----- tests/oap/oap_client_test.ok | 2 - tests/sgsn/Makefile.am | 1 - tests/testsuite.at | 8 -- 17 files changed, 15 insertions(+), 1010 deletions(-) delete mode 100644 include/osmocom/sgsn/oap_client.h delete mode 100644 src/gprs/oap_client.c delete mode 100644 src/libcommon/oap_client.c delete mode 100644 tests/oap/Makefile.am delete mode 100644 tests/oap/oap_client_test.c delete mode 100644 tests/oap/oap_client_test.err delete mode 100644 tests/oap/oap_client_test.ok diff --git a/configure.ac b/configure.ac index 2b5bb1ad8..c233fa3e2 100644 --- a/configure.ac +++ b/configure.ac @@ -194,7 +194,6 @@ AC_OUTPUT( tests/gprs/Makefile tests/gbproxy/Makefile tests/sgsn/Makefile - tests/oap/Makefile tests/gtphub/Makefile tests/xid/Makefile tests/sndcp_xid/Makefile diff --git a/include/osmocom/sgsn/Makefile.am b/include/osmocom/sgsn/Makefile.am index 3f63b9fdc..269cebcd6 100644 --- a/include/osmocom/sgsn/Makefile.am +++ b/include/osmocom/sgsn/Makefile.am @@ -17,7 +17,6 @@ noinst_HEADERS = \ gprs_utils.h \ gsup_client.h \ gtphub.h \ - oap_client.h \ sgsn.h \ signal.h \ slhc.h \ diff --git a/include/osmocom/sgsn/gsup_client.h b/include/osmocom/sgsn/gsup_client.h index 29092ad5b..6ba0d1581 100644 --- a/include/osmocom/sgsn/gsup_client.h +++ b/include/osmocom/sgsn/gsup_client.h @@ -23,7 +23,7 @@ #include -#include +#include #define GSUP_CLIENT_RECONNECT_INTERVAL 10 #define GSUP_CLIENT_PING_INTERVAL 20 @@ -43,7 +43,7 @@ struct gsup_client { gsup_client_read_cb_t read_cb; void *data; - struct oap_client_state oap_state; + struct osmo_oap_client_state oap_state; struct osmo_timer_list ping_timer; struct osmo_timer_list connect_timer; @@ -55,7 +55,7 @@ struct gsup_client *gsup_client_create(const char *unit_name, const char *ip_addr, unsigned int tcp_port, gsup_client_read_cb_t read_cb, - struct oap_client_config *oapc_config); + struct osmo_oap_client_config *oapc_config); void gsup_client_destroy(struct gsup_client *gsupc); int gsup_client_send(struct gsup_client *gsupc, struct msgb *msg); diff --git a/include/osmocom/sgsn/oap_client.h b/include/osmocom/sgsn/oap_client.h deleted file mode 100644 index 80c86d5d6..000000000 --- a/include/osmocom/sgsn/oap_client.h +++ /dev/null @@ -1,82 +0,0 @@ -/* Osmocom Authentication Protocol API */ - -/* (C) 2015 by Sysmocom s.f.m.c. GmbH - * All Rights Reserved - * - * Author: Neels Hofmeyr - * - * 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 Affero 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 . - * - */ - -#pragma once - -#include - -struct msgb; -struct osmo_oap_message; - -/* This is the config part for vty. It is essentially copied in - * oap_client_state, where values are copied over once the config is - * considered valid. */ -struct oap_client_config { - uint16_t client_id; - int secret_k_present; - uint8_t secret_k[16]; - int secret_opc_present; - uint8_t secret_opc[16]; -}; - -/* The runtime state of the OAP client. client_id and the secrets are in fact - * duplicated from oap_client_config, so that a separate validation of the - * config data is possible, and so that only a struct oap_client_state* is - * passed around. */ -struct oap_client_state { - enum { - OAP_UNINITIALIZED = 0, /* just allocated. */ - OAP_DISABLED, /* disabled by config. */ - OAP_INITIALIZED, /* enabled, config is valid. */ - OAP_REQUESTED_CHALLENGE, - OAP_SENT_CHALLENGE_RESULT, - OAP_REGISTERED - } state; - uint16_t client_id; - uint8_t secret_k[16]; - uint8_t secret_opc[16]; - int registration_failures; -}; - -/* From config, initialize state. Return 0 on success. */ -int oap_client_init(struct oap_client_config *config, - struct oap_client_state *state); - -/* Construct an OAP registration message and return in *msg_tx. Use - * state->client_id and update state->state. - * Return 0 on success, or a negative value on error. - * If an error is returned, *msg_tx is guaranteed to be NULL. */ -int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx); - -/* Decode and act on a received OAP message msg_rx. Update state->state. If a - * non-NULL pointer is returned in *msg_tx, that msgb should be sent to the OAP - * server (and freed) by the caller. The received msg_rx is not freed. - * Return 0 on success, or a negative value on error. - * If an error is returned, *msg_tx is guaranteed to be NULL. */ -int oap_client_handle(struct oap_client_state *state, - const struct msgb *msg_rx, struct msgb **msg_tx); - -/* Allocate a msgb and in it, return the encoded oap_client_msg. Return - * NULL on error. (Like oap_client_encode(), but also allocates a msgb.) - * About the name: the idea is do_something(oap_client_encoded(my_struct)) - */ -struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_client_msg); diff --git a/include/osmocom/sgsn/sgsn.h b/include/osmocom/sgsn/sgsn.h index dae9d12aa..59d0bd72e 100644 --- a/include/osmocom/sgsn/sgsn.h +++ b/include/osmocom/sgsn/sgsn.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include "../../bscconfig.h" @@ -101,7 +101,7 @@ struct sgsn_config { int dynamic_lookup; - struct oap_client_config oap; + struct osmo_oap_client_config oap; /* RFC1144 TCP/IP header compression */ struct { diff --git a/src/gprs/Makefile.am b/src/gprs/Makefile.am index 1cf67850c..46d94d456 100644 --- a/src/gprs/Makefile.am +++ b/src/gprs/Makefile.am @@ -84,7 +84,6 @@ osmo_sgsn_SOURCES = \ gprs_llc_xid.c \ v42bis.c \ gsup_client.c \ - oap_client.c \ $(NULL) osmo_sgsn_LDADD = \ $(OSMO_LIBS) \ diff --git a/src/gprs/gsup_client.c b/src/gprs/gsup_client.c index 48357b475..95c498898 100644 --- a/src/gprs/gsup_client.c +++ b/src/gprs/gsup_client.c @@ -116,7 +116,7 @@ static void gsup_client_oap_register(struct gsup_client *gsupc) { struct msgb *msg_tx; int rc; - rc = oap_client_register(&gsupc->oap_state, &msg_tx); + rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx); if ((rc < 0) || (!msg_tx)) { LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); @@ -138,7 +138,7 @@ static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) if (up) { start_test_procedure(gsupc); - if (gsupc->oap_state.state == OAP_INITIALIZED) + if (gsupc->oap_state.state == OSMO_OAP_INITIALIZED) gsup_client_oap_register(gsupc); osmo_timer_del(&gsupc->connect_timer); @@ -156,7 +156,7 @@ static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx struct msgb *msg_tx; /* If the oap_state is disabled, this will reject the messages. */ - rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); + rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); msgb_free(msg_rx); if (rc < 0) return rc; @@ -269,7 +269,7 @@ struct gsup_client *gsup_client_create(const char *unit_name, const char *ip_addr, unsigned int tcp_port, gsup_client_read_cb_t read_cb, - struct oap_client_config *oapc_config) + struct osmo_oap_client_config *oapc_config) { struct gsup_client *gsupc; int rc; @@ -284,7 +284,7 @@ struct gsup_client *gsup_client_create(const char *unit_name, OSMO_ASSERT(gsupc->unit_name); /* a NULL oapc_config will mark oap_state disabled. */ - rc = oap_client_init(oapc_config, &gsupc->oap_state); + rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state); if (rc != 0) goto failed; diff --git a/src/gprs/oap_client.c b/src/gprs/oap_client.c deleted file mode 100644 index 9ff84a6ea..000000000 --- a/src/gprs/oap_client.c +++ /dev/null @@ -1,280 +0,0 @@ -/* Osmocom Authentication Protocol API */ - -/* (C) 2015 by Sysmocom s.f.m.c. GmbH - * All Rights Reserved - * - * Author: Neels Hofmeyr - * - * 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 Affero 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 . - * - */ - -#include -#include - -#include -#include -#include - -#include -#include - -int oap_client_init(struct oap_client_config *config, - struct oap_client_state *state) -{ - OSMO_ASSERT(state->state == OAP_UNINITIALIZED); - - if (!config) - goto disable; - - if (config->client_id == 0) - goto disable; - - if (config->secret_k_present == 0) { - LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret K missing.\n"); - goto disable; - } - - if (config->secret_opc_present == 0) { - LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret OPC missing.\n"); - goto disable; - } - - state->client_id = config->client_id; - memcpy(state->secret_k, config->secret_k, sizeof(state->secret_k)); - memcpy(state->secret_opc, config->secret_opc, sizeof(state->secret_opc)); - state->state = OAP_INITIALIZED; - return 0; - -disable: - state->state = OAP_DISABLED; - return 0; -} - -/* From the given state and received RAND and AUTN octets, validate the - * server's authenticity and formulate the matching milenage reply octets in - * *tx_xres. The state is not modified. - * On success, and if tx_res is not NULL, exactly 8 octets will be written to - * *tx_res. If not NULL, tx_res must point at allocated memory of at least 8 - * octets. The caller will want to send XRES back to the server in a challenge - * response message and update the state. - * Return 0 on success; -1 if OAP is disabled; -2 if rx_random and rx_autn fail - * the authentication check; -3 for any other errors. */ -static int oap_evaluate_challenge(const struct oap_client_state *state, - const uint8_t *rx_random, - const uint8_t *rx_autn, - uint8_t *tx_xres) -{ - struct osmo_auth_vector vec; - - struct osmo_sub_auth_data auth = { - .type = OSMO_AUTH_TYPE_UMTS, - .algo = OSMO_AUTH_ALG_MILENAGE, - }; - - osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.k) - == sizeof(state->secret_k), _secret_k_size_match); - osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.opc) - == sizeof(state->secret_opc), _secret_opc_size_match); - - switch (state->state) { - case OAP_UNINITIALIZED: - case OAP_DISABLED: - return -1; - default: - break; - } - - memcpy(auth.u.umts.k, state->secret_k, sizeof(auth.u.umts.k)); - memcpy(auth.u.umts.opc, state->secret_opc, sizeof(auth.u.umts.opc)); - memset(auth.u.umts.amf, '\0', sizeof(auth.u.umts.amf)); - auth.u.umts.sqn = 41; /* TODO use incrementing sequence nr */ - - memset(&vec, 0, sizeof(vec)); - osmo_auth_gen_vec(&vec, &auth, rx_random); - - if (vec.res_len != 8) { - LOGP(DLOAP, LOGL_ERROR, "OAP: Expected XRES to be 8 octets, got %d\n", - vec.res_len); - return -3; - } - - if (osmo_constant_time_cmp(vec.autn, rx_autn, sizeof(vec.autn)) != 0) { - LOGP(DLOAP, LOGL_ERROR, "OAP: AUTN mismatch!\n"); - LOGP(DLOAP, LOGL_INFO, "OAP: AUTN from server: %s\n", - osmo_hexdump_nospc(rx_autn, sizeof(vec.autn))); - LOGP(DLOAP, LOGL_INFO, "OAP: AUTN expected: %s\n", - osmo_hexdump_nospc(vec.autn, sizeof(vec.autn))); - return -2; - } - - if (tx_xres != NULL) - memcpy(tx_xres, vec.res, 8); - return 0; -} - -struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_msg) -{ - struct msgb *msg = msgb_alloc_headroom(1000, 64, __func__); - OSMO_ASSERT(msg); - osmo_oap_encode(msg, oap_msg); - return msg; -} - -/* Create a new msgb containing an OAP registration message. - * On error, return NULL. */ -static struct msgb* oap_msg_register(uint16_t client_id) -{ - struct osmo_oap_message oap_msg = {0}; - - if (client_id < 1) { - LOGP(DLOAP, LOGL_ERROR, "OAP: Invalid client ID: %d\n", client_id); - return NULL; - } - - oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST; - oap_msg.client_id = client_id; - return oap_client_encoded(&oap_msg); -} - -int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx) -{ - *msg_tx = oap_msg_register(state->client_id); - if (!(*msg_tx)) - return -1; - - state->state = OAP_REQUESTED_CHALLENGE; - return 0; -} - -/* Create a new msgb containing an OAP challenge response message. - * xres must point at 8 octets to return as challenge response. - * On error, return NULL. */ -static struct msgb* oap_msg_challenge_response(uint8_t *xres) -{ - struct osmo_oap_message oap_reply = {0}; - - oap_reply.message_type = OAP_MSGT_CHALLENGE_RESULT; - memcpy(oap_reply.xres, xres, sizeof(oap_reply.xres)); - oap_reply.xres_present = 1; - return oap_client_encoded(&oap_reply); -} - -static int handle_challenge(struct oap_client_state *state, - struct osmo_oap_message *oap_rx, - struct msgb **msg_tx) -{ - int rc; - uint8_t xres[8]; - - if (!(oap_rx->rand_present && oap_rx->autn_present)) { - LOGP(DLOAP, LOGL_ERROR, - "OAP challenge incomplete (rand_present: %d, autn_present: %d)\n", - oap_rx->rand_present, oap_rx->autn_present); - rc = -2; - goto failure; - } - - rc = oap_evaluate_challenge(state, - oap_rx->rand, - oap_rx->autn, - xres); - if (rc < 0) - goto failure; - - *msg_tx = oap_msg_challenge_response(xres); - if ((*msg_tx) == NULL) { - rc = -1; - goto failure; - } - - state->state = OAP_SENT_CHALLENGE_RESULT; - return 0; - -failure: - OSMO_ASSERT(rc < 0); - state->state = OAP_INITIALIZED; - return rc; -} - -int oap_client_handle(struct oap_client_state *state, - const struct msgb *msg_rx, struct msgb **msg_tx) -{ - uint8_t *data = msgb_l2(msg_rx); - size_t data_len = msgb_l2len(msg_rx); - struct osmo_oap_message oap_msg = {0}; - int rc = 0; - - *msg_tx = NULL; - - OSMO_ASSERT(data); - - rc = osmo_oap_decode(&oap_msg, data, data_len); - if (rc < 0) { - LOGP(DLOAP, LOGL_ERROR, - "Decoding OAP message failed with error '%s' (%d)\n", - get_value_string(gsm48_gmm_cause_names, -rc), -rc); - return -10; - } - - switch (state->state) { - case OAP_UNINITIALIZED: - LOGP(DLOAP, LOGL_ERROR, - "Received OAP message %d, but the OAP client is" - " not initialized\n", oap_msg.message_type); - return -ENOTCONN; - case OAP_DISABLED: - LOGP(DLOAP, LOGL_ERROR, - "Received OAP message %d, but the OAP client is" - " disabled\n", oap_msg.message_type); - return -ENOTCONN; - default: - break; - } - - switch (oap_msg.message_type) { - case OAP_MSGT_CHALLENGE_REQUEST: - return handle_challenge(state, &oap_msg, msg_tx); - - case OAP_MSGT_REGISTER_RESULT: - /* successfully registered */ - state->state = OAP_REGISTERED; - break; - - case OAP_MSGT_REGISTER_ERROR: - LOGP(DLOAP, LOGL_ERROR, - "OAP registration failed\n"); - state->state = OAP_INITIALIZED; - if (state->registration_failures < 3) { - state->registration_failures ++; - return oap_client_register(state, msg_tx); - } - return -11; - - case OAP_MSGT_REGISTER_REQUEST: - case OAP_MSGT_CHALLENGE_RESULT: - LOGP(DLOAP, LOGL_ERROR, - "Received invalid OAP message type for OAP client side: %d\n", - (int)oap_msg.message_type); - return -12; - - default: - LOGP(DLOAP, LOGL_ERROR, - "Unknown OAP message type: %d\n", - (int)oap_msg.message_type); - return -13; - } - - return 0; -} diff --git a/src/libcommon/gsup_client.c b/src/libcommon/gsup_client.c index 89a9f7060..052004548 100644 --- a/src/libcommon/gsup_client.c +++ b/src/libcommon/gsup_client.c @@ -116,7 +116,7 @@ static void gsup_client_oap_register(struct gsup_client *gsupc) { struct msgb *msg_tx; int rc; - rc = oap_client_register(&gsupc->oap_state, &msg_tx); + rc = osmo_oap_client_register(&gsupc->oap_state, &msg_tx); if ((rc < 0) || (!msg_tx)) { LOGP(DLGSUP, LOGL_ERROR, "GSUP OAP set up, but cannot register.\n"); @@ -138,7 +138,7 @@ static void gsup_client_updown_cb(struct ipa_client_conn *link, int up) if (up) { start_test_procedure(gsupc); - if (gsupc->oap_state.state == OAP_INITIALIZED) + if (gsupc->osmo_oap_state.state == OSMO_OAP_INITIALIZED) gsup_client_oap_register(gsupc); osmo_timer_del(&gsupc->connect_timer); @@ -156,7 +156,7 @@ static int gsup_client_oap_handle(struct gsup_client *gsupc, struct msgb *msg_rx struct msgb *msg_tx; /* If the oap_state is disabled, this will reject the messages. */ - rc = oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); + rc = osmo_oap_client_handle(&gsupc->oap_state, msg_rx, &msg_tx); msgb_free(msg_rx); if (rc < 0) return rc; @@ -269,7 +269,7 @@ struct gsup_client *gsup_client_create(const char *unit_name, const char *ip_addr, unsigned int tcp_port, gsup_client_read_cb_t read_cb, - struct oap_client_config *oapc_config) + struct osmo_oap_client_config *oapc_config) { struct gsup_client *gsupc; int rc; @@ -284,7 +284,7 @@ struct gsup_client *gsup_client_create(const char *unit_name, OSMO_ASSERT(gsupc->unit_name); /* a NULL oapc_config will mark oap_state disabled. */ - rc = oap_client_init(oapc_config, &gsupc->oap_state); + rc = osmo_oap_client_init(oapc_config, &gsupc->oap_state); if (rc != 0) goto failed; diff --git a/src/libcommon/oap_client.c b/src/libcommon/oap_client.c deleted file mode 100644 index 9ff84a6ea..000000000 --- a/src/libcommon/oap_client.c +++ /dev/null @@ -1,280 +0,0 @@ -/* Osmocom Authentication Protocol API */ - -/* (C) 2015 by Sysmocom s.f.m.c. GmbH - * All Rights Reserved - * - * Author: Neels Hofmeyr - * - * 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 Affero 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 . - * - */ - -#include -#include - -#include -#include -#include - -#include -#include - -int oap_client_init(struct oap_client_config *config, - struct oap_client_state *state) -{ - OSMO_ASSERT(state->state == OAP_UNINITIALIZED); - - if (!config) - goto disable; - - if (config->client_id == 0) - goto disable; - - if (config->secret_k_present == 0) { - LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret K missing.\n"); - goto disable; - } - - if (config->secret_opc_present == 0) { - LOGP(DLOAP, LOGL_NOTICE, "OAP: client ID set, but secret OPC missing.\n"); - goto disable; - } - - state->client_id = config->client_id; - memcpy(state->secret_k, config->secret_k, sizeof(state->secret_k)); - memcpy(state->secret_opc, config->secret_opc, sizeof(state->secret_opc)); - state->state = OAP_INITIALIZED; - return 0; - -disable: - state->state = OAP_DISABLED; - return 0; -} - -/* From the given state and received RAND and AUTN octets, validate the - * server's authenticity and formulate the matching milenage reply octets in - * *tx_xres. The state is not modified. - * On success, and if tx_res is not NULL, exactly 8 octets will be written to - * *tx_res. If not NULL, tx_res must point at allocated memory of at least 8 - * octets. The caller will want to send XRES back to the server in a challenge - * response message and update the state. - * Return 0 on success; -1 if OAP is disabled; -2 if rx_random and rx_autn fail - * the authentication check; -3 for any other errors. */ -static int oap_evaluate_challenge(const struct oap_client_state *state, - const uint8_t *rx_random, - const uint8_t *rx_autn, - uint8_t *tx_xres) -{ - struct osmo_auth_vector vec; - - struct osmo_sub_auth_data auth = { - .type = OSMO_AUTH_TYPE_UMTS, - .algo = OSMO_AUTH_ALG_MILENAGE, - }; - - osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.k) - == sizeof(state->secret_k), _secret_k_size_match); - osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.opc) - == sizeof(state->secret_opc), _secret_opc_size_match); - - switch (state->state) { - case OAP_UNINITIALIZED: - case OAP_DISABLED: - return -1; - default: - break; - } - - memcpy(auth.u.umts.k, state->secret_k, sizeof(auth.u.umts.k)); - memcpy(auth.u.umts.opc, state->secret_opc, sizeof(auth.u.umts.opc)); - memset(auth.u.umts.amf, '\0', sizeof(auth.u.umts.amf)); - auth.u.umts.sqn = 41; /* TODO use incrementing sequence nr */ - - memset(&vec, 0, sizeof(vec)); - osmo_auth_gen_vec(&vec, &auth, rx_random); - - if (vec.res_len != 8) { - LOGP(DLOAP, LOGL_ERROR, "OAP: Expected XRES to be 8 octets, got %d\n", - vec.res_len); - return -3; - } - - if (osmo_constant_time_cmp(vec.autn, rx_autn, sizeof(vec.autn)) != 0) { - LOGP(DLOAP, LOGL_ERROR, "OAP: AUTN mismatch!\n"); - LOGP(DLOAP, LOGL_INFO, "OAP: AUTN from server: %s\n", - osmo_hexdump_nospc(rx_autn, sizeof(vec.autn))); - LOGP(DLOAP, LOGL_INFO, "OAP: AUTN expected: %s\n", - osmo_hexdump_nospc(vec.autn, sizeof(vec.autn))); - return -2; - } - - if (tx_xres != NULL) - memcpy(tx_xres, vec.res, 8); - return 0; -} - -struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_msg) -{ - struct msgb *msg = msgb_alloc_headroom(1000, 64, __func__); - OSMO_ASSERT(msg); - osmo_oap_encode(msg, oap_msg); - return msg; -} - -/* Create a new msgb containing an OAP registration message. - * On error, return NULL. */ -static struct msgb* oap_msg_register(uint16_t client_id) -{ - struct osmo_oap_message oap_msg = {0}; - - if (client_id < 1) { - LOGP(DLOAP, LOGL_ERROR, "OAP: Invalid client ID: %d\n", client_id); - return NULL; - } - - oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST; - oap_msg.client_id = client_id; - return oap_client_encoded(&oap_msg); -} - -int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx) -{ - *msg_tx = oap_msg_register(state->client_id); - if (!(*msg_tx)) - return -1; - - state->state = OAP_REQUESTED_CHALLENGE; - return 0; -} - -/* Create a new msgb containing an OAP challenge response message. - * xres must point at 8 octets to return as challenge response. - * On error, return NULL. */ -static struct msgb* oap_msg_challenge_response(uint8_t *xres) -{ - struct osmo_oap_message oap_reply = {0}; - - oap_reply.message_type = OAP_MSGT_CHALLENGE_RESULT; - memcpy(oap_reply.xres, xres, sizeof(oap_reply.xres)); - oap_reply.xres_present = 1; - return oap_client_encoded(&oap_reply); -} - -static int handle_challenge(struct oap_client_state *state, - struct osmo_oap_message *oap_rx, - struct msgb **msg_tx) -{ - int rc; - uint8_t xres[8]; - - if (!(oap_rx->rand_present && oap_rx->autn_present)) { - LOGP(DLOAP, LOGL_ERROR, - "OAP challenge incomplete (rand_present: %d, autn_present: %d)\n", - oap_rx->rand_present, oap_rx->autn_present); - rc = -2; - goto failure; - } - - rc = oap_evaluate_challenge(state, - oap_rx->rand, - oap_rx->autn, - xres); - if (rc < 0) - goto failure; - - *msg_tx = oap_msg_challenge_response(xres); - if ((*msg_tx) == NULL) { - rc = -1; - goto failure; - } - - state->state = OAP_SENT_CHALLENGE_RESULT; - return 0; - -failure: - OSMO_ASSERT(rc < 0); - state->state = OAP_INITIALIZED; - return rc; -} - -int oap_client_handle(struct oap_client_state *state, - const struct msgb *msg_rx, struct msgb **msg_tx) -{ - uint8_t *data = msgb_l2(msg_rx); - size_t data_len = msgb_l2len(msg_rx); - struct osmo_oap_message oap_msg = {0}; - int rc = 0; - - *msg_tx = NULL; - - OSMO_ASSERT(data); - - rc = osmo_oap_decode(&oap_msg, data, data_len); - if (rc < 0) { - LOGP(DLOAP, LOGL_ERROR, - "Decoding OAP message failed with error '%s' (%d)\n", - get_value_string(gsm48_gmm_cause_names, -rc), -rc); - return -10; - } - - switch (state->state) { - case OAP_UNINITIALIZED: - LOGP(DLOAP, LOGL_ERROR, - "Received OAP message %d, but the OAP client is" - " not initialized\n", oap_msg.message_type); - return -ENOTCONN; - case OAP_DISABLED: - LOGP(DLOAP, LOGL_ERROR, - "Received OAP message %d, but the OAP client is" - " disabled\n", oap_msg.message_type); - return -ENOTCONN; - default: - break; - } - - switch (oap_msg.message_type) { - case OAP_MSGT_CHALLENGE_REQUEST: - return handle_challenge(state, &oap_msg, msg_tx); - - case OAP_MSGT_REGISTER_RESULT: - /* successfully registered */ - state->state = OAP_REGISTERED; - break; - - case OAP_MSGT_REGISTER_ERROR: - LOGP(DLOAP, LOGL_ERROR, - "OAP registration failed\n"); - state->state = OAP_INITIALIZED; - if (state->registration_failures < 3) { - state->registration_failures ++; - return oap_client_register(state, msg_tx); - } - return -11; - - case OAP_MSGT_REGISTER_REQUEST: - case OAP_MSGT_CHALLENGE_RESULT: - LOGP(DLOAP, LOGL_ERROR, - "Received invalid OAP message type for OAP client side: %d\n", - (int)oap_msg.message_type); - return -12; - - default: - LOGP(DLOAP, LOGL_ERROR, - "Unknown OAP message type: %d\n", - (int)oap_msg.message_type); - return -13; - } - - return 0; -} diff --git a/tests/Makefile.am b/tests/Makefile.am index f6a1efc9e..035cfb010 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,7 +3,6 @@ SUBDIRS = \ gbproxy \ gtphub \ sgsn \ - oap \ xid \ sndcp_xid \ slhc \ diff --git a/tests/oap/Makefile.am b/tests/oap/Makefile.am deleted file mode 100644 index ccc6d8cfd..000000000 --- a/tests/oap/Makefile.am +++ /dev/null @@ -1,32 +0,0 @@ -AM_CPPFLAGS = \ - $(all_includes) \ - -I$(top_srcdir)/include \ - $(NULL) - -AM_CFLAGS = \ - -Wall \ - -ggdb3 \ - $(LIBOSMOCORE_CFLAGS) \ - $(LIBOSMOGSM_CFLAGS) \ - $(NULL) - -EXTRA_DIST = \ - oap_client_test.ok \ - oap_client_test.err \ - $(NULL) - -noinst_PROGRAMS = \ - oap_client_test \ - $(NULL) - -oap_client_test_SOURCES = \ - oap_client_test.c \ - $(NULL) - -oap_client_test_LDADD = \ - $(top_builddir)/src/gprs/gprs_utils.o \ - $(top_builddir)/src/gprs/oap_client.o \ - $(LIBOSMOCORE_LIBS) \ - $(LIBOSMOGSM_LIBS) \ - -lrt - diff --git a/tests/oap/oap_client_test.c b/tests/oap/oap_client_test.c deleted file mode 100644 index 87d207057..000000000 --- a/tests/oap/oap_client_test.c +++ /dev/null @@ -1,271 +0,0 @@ -/* Test Osmocom Authentication Protocol */ -/* - * (C) 2015 by sysmocom s.f.m.c. GmbH - * 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 Affero 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 . - * - */ - -#include -#include - -#include -#include - -#include -#include - -static void test_oap_api(void) -{ - printf("Testing OAP API\n"); - - struct oap_client_config _config; - struct oap_client_config *config = &_config; - - struct oap_client_state _state; - struct oap_client_state *state = &_state; - - struct osmo_oap_message oap_rx; - struct msgb *msg_rx; - - struct osmo_oap_message oap_tx; - struct msgb *msg_tx; - - memset(config, 0, sizeof(*config)); - memset(state, 0, sizeof(*state)); - - OSMO_ASSERT(osmo_hexparse("0102030405060708090a0b0c0d0e0f10", config->secret_k, 16) == 16); - OSMO_ASSERT(osmo_hexparse("1112131415161718191a1b1c1d1e1f20", config->secret_opc, 16) == 16); - - fprintf(stderr, "- make sure filling with zeros means uninitialized\n"); - OSMO_ASSERT(state->state == OAP_UNINITIALIZED); - - fprintf(stderr, "- reject messages in uninitialized state\n"); - memset(&oap_rx, 0, sizeof(oap_rx)); - state->client_id = 1; - oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) < 0); - OSMO_ASSERT(state->state == OAP_UNINITIALIZED); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- NULL config should disable\n"); - OSMO_ASSERT( oap_client_init(NULL, state) == 0 ); - OSMO_ASSERT(state->state == OAP_DISABLED); - - fprintf(stderr, "- reject messages in disabled state\n"); - memset(state, 0, sizeof(*state)); - memset(&oap_rx, 0, sizeof(oap_rx)); - state->state = OAP_DISABLED; - state->client_id = 1; - oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) < 0); - OSMO_ASSERT(state->state == OAP_DISABLED); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- invalid client_id and shared secret\n"); - memset(state, 0, sizeof(*state)); - config->client_id = 0; - config->secret_k_present = 0; - config->secret_opc_present = 0; - OSMO_ASSERT( oap_client_init(config, state) == 0 ); - OSMO_ASSERT(state->state == OAP_DISABLED); - - fprintf(stderr, "- reset state\n"); - memset(state, 0, sizeof(*state)); - - fprintf(stderr, "- only client_id is invalid\n"); - config->client_id = 0; - config->secret_k_present = 1; - config->secret_opc_present = 1; - OSMO_ASSERT( oap_client_init(config, state) == 0 ); - OSMO_ASSERT(state->state == OAP_DISABLED); - - memset(state, 0, sizeof(*state)); - - fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n"); - config->client_id = 12345; - config->secret_k_present = 0; - config->secret_opc_present = 1; - OSMO_ASSERT( oap_client_init(config, state) == 0 ); - OSMO_ASSERT(state->state == OAP_DISABLED); - - memset(state, 0, sizeof(*state)); - - fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n"); - config->client_id = 12345; - config->secret_k_present = 1; - config->secret_opc_present = 0; - OSMO_ASSERT( oap_client_init(config, state) == 0 ); - OSMO_ASSERT(state->state == OAP_DISABLED); - - memset(state, 0, sizeof(*state)); - - - fprintf(stderr, "- mint configuration\n"); - config->client_id = 12345; - config->secret_k_present = 1; - config->secret_opc_present = 1; - /*config->secret_* buffers are still set from the top */ - OSMO_ASSERT( oap_client_init(config, state) == 0 ); - OSMO_ASSERT(state->state == OAP_INITIALIZED); - - - fprintf(stderr, "- Missing challenge data\n"); - memset(&oap_rx, 0, sizeof(oap_rx)); - oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST; - oap_rx.rand_present = 0; - oap_rx.autn_present = 0; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- AUTN missing\n"); - osmo_hexparse("0102030405060708090a0b0c0d0e0f10", - oap_rx.rand, 16); - oap_rx.rand_present = 1; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- RAND missing\n"); - oap_rx.rand_present = 0; - osmo_hexparse("cec4e3848a33000086781158ca40f136", - oap_rx.autn, 16); - oap_rx.autn_present = 1; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- wrong autn (by one bit)\n"); - osmo_hexparse("0102030405060708090a0b0c0d0e0f10", - oap_rx.rand, 16); - osmo_hexparse("dec4e3848a33000086781158ca40f136", - oap_rx.autn, 16); - oap_rx.rand_present = 1; - oap_rx.autn_present = 1; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); - msgb_free(msg_rx); - OSMO_ASSERT(!msg_tx); - - fprintf(stderr, "- all data correct\n"); - osmo_hexparse("cec4e3848a33000086781158ca40f136", - oap_rx.autn, 16); - msg_rx = oap_client_encoded(&oap_rx); - - fprintf(stderr, "- but refuse to evaluate in uninitialized state\n"); - OSMO_ASSERT(state->state == OAP_INITIALIZED); - - state->state = OAP_UNINITIALIZED; - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) < 0); - OSMO_ASSERT(!msg_tx); - - state->state = OAP_DISABLED; - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) < 0); - OSMO_ASSERT(!msg_tx); - - state->state = OAP_INITIALIZED; - - fprintf(stderr, "- now everything is correct\n"); - /* a successful return value here indicates correct autn */ - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); - msgb_free(msg_rx); - - fprintf(stderr, "- Expect the challenge response in msg_tx\n"); - OSMO_ASSERT(msg_tx); - OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); - OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT); - OSMO_ASSERT(strcmp("e2d05b598c61d9ba", - osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres))) - == 0); - OSMO_ASSERT(state->state == OAP_SENT_CHALLENGE_RESULT); - msgb_free(msg_tx); - msg_tx = 0; - - struct oap_client_state saved_state = _state; - - fprintf(stderr, "- Receive registration error for the first time.\n"); - - memset(&oap_rx, 0, sizeof(oap_rx)); - oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; - oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC; - msg_rx = oap_client_encoded(&oap_rx); - - OSMO_ASSERT(state->registration_failures == 0); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); - OSMO_ASSERT(state->registration_failures == 1); - OSMO_ASSERT(msg_tx); - OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); - OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST); - OSMO_ASSERT(state->state == OAP_REQUESTED_CHALLENGE); - msgb_free(msg_tx); - msg_tx = 0; - - fprintf(stderr, "- Receive registration error for the Nth time.\n"); - state->registration_failures = 999; - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -11); - OSMO_ASSERT(!msg_tx); - OSMO_ASSERT(state->state == OAP_INITIALIZED); - msgb_free(msg_tx); - msg_tx = 0; - - msgb_free(msg_rx); - - fprintf(stderr, "- Registration success\n"); - - _state = saved_state; - memset(&oap_rx, 0, sizeof(oap_rx)); - oap_rx.message_type = OAP_MSGT_REGISTER_RESULT; - msg_rx = oap_client_encoded(&oap_rx); - OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); - OSMO_ASSERT(!msg_tx); - OSMO_ASSERT(state->state == OAP_REGISTERED); - msgb_free(msg_rx); -} - -static struct log_info_cat oap_client_test_categories[] = { -}; - -static struct log_info info = { - .cat = oap_client_test_categories, - .num_cat = ARRAY_SIZE(oap_client_test_categories), -}; - -int main(int argc, char **argv) -{ - void *ctx = talloc_named_const(NULL, 0, "oap_client_test"); - msgb_talloc_ctx_init(ctx, 0); - osmo_init_logging2(ctx, &info); - - OSMO_ASSERT(osmo_stderr_target); - log_set_use_color(osmo_stderr_target, 0); - log_set_print_timestamp(osmo_stderr_target, 0); - log_set_print_filename(osmo_stderr_target, 0); - log_set_print_category(osmo_stderr_target, 1); - log_parse_category_mask(osmo_stderr_target, "DLOAP,1"); - - test_oap_api(); - printf("Done\n"); - - return 0; -} - diff --git a/tests/oap/oap_client_test.err b/tests/oap/oap_client_test.err deleted file mode 100644 index 62ddc9efa..000000000 --- a/tests/oap/oap_client_test.err +++ /dev/null @@ -1,35 +0,0 @@ -- make sure filling with zeros means uninitialized -- reject messages in uninitialized state -DLOAP Received OAP message 5, but the OAP client is not initialized -- NULL config should disable -- reject messages in disabled state -DLOAP Received OAP message 5, but the OAP client is disabled -- invalid client_id and shared secret -- reset state -- only client_id is invalid -- valid id, but omitted shared_secret (1/2) -DLOAP OAP: client ID set, but secret K missing. -- valid id, but omitted shared_secret (2/2) -DLOAP OAP: client ID set, but secret OPC missing. -- mint configuration -- Missing challenge data -DLOAP OAP challenge incomplete (rand_present: 0, autn_present: 0) -- AUTN missing -DLOAP OAP challenge incomplete (rand_present: 1, autn_present: 0) -- RAND missing -DLOAP OAP challenge incomplete (rand_present: 0, autn_present: 1) -- wrong autn (by one bit) -DLOAP OAP: AUTN mismatch! -DLOAP OAP: AUTN from server: dec4e3848a33000086781158ca40f136 -DLOAP OAP: AUTN expected: cec4e3848a33000086781158ca40f136 -- all data correct -- but refuse to evaluate in uninitialized state -DLOAP Received OAP message 8, but the OAP client is not initialized -DLOAP Received OAP message 8, but the OAP client is disabled -- now everything is correct -- Expect the challenge response in msg_tx -- Receive registration error for the first time. -DLOAP OAP registration failed -- Receive registration error for the Nth time. -DLOAP OAP registration failed -- Registration success diff --git a/tests/oap/oap_client_test.ok b/tests/oap/oap_client_test.ok deleted file mode 100644 index 59108a792..000000000 --- a/tests/oap/oap_client_test.ok +++ /dev/null @@ -1,2 +0,0 @@ -Testing OAP API -Done diff --git a/tests/sgsn/Makefile.am b/tests/sgsn/Makefile.am index 8692fc37c..abcc35708 100644 --- a/tests/sgsn/Makefile.am +++ b/tests/sgsn/Makefile.am @@ -62,7 +62,6 @@ sgsn_test_LDADD = \ $(top_builddir)/src/gprs/v42bis.o \ $(top_builddir)/src/gprs/gprs_sndcp_dcomp.o \ $(top_builddir)/src/gprs/gsup_client.o \ - $(top_builddir)/src/gprs/oap_client.o \ $(LIBOSMOABIS_LIBS) \ $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) \ diff --git a/tests/testsuite.at b/tests/testsuite.at index 81cd713f3..d30115e01 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -20,14 +20,6 @@ cat $abs_srcdir/sgsn/sgsn_test.ok > expout AT_CHECK([$abs_top_builddir/tests/sgsn/sgsn_test], [], [expout], [ignore]) AT_CLEANUP -AT_SETUP([oap]) -AT_KEYWORDS([oap]) -AT_CHECK([test "$enable_oap_test" != no || exit 77]) -cat $abs_srcdir/oap/oap_client_test.ok > expout -cat $abs_srcdir/oap/oap_client_test.err > experr -AT_CHECK([$abs_top_builddir/tests/oap/oap_client_test], [], [expout], [experr]) -AT_CLEANUP - AT_SETUP([gtphub]) AT_KEYWORDS([gtphub]) AT_CHECK([test "$enable_gtphub_test" != no || exit 77]) -- cgit v1.2.3