Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 1 | /* Test Osmocom Authentication Protocol */ |
| 2 | /* |
| 3 | * (C) 2015 by sysmocom s.f.m.c. GmbH |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | c53afd9 | 2019-05-27 23:13:11 +0200 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | c53afd9 | 2019-05-27 23:13:11 +0200 | [diff] [blame] | 14 | * GNU General Public License for more details. |
Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 15 | * |
Harald Welte | c53afd9 | 2019-05-27 23:13:11 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
Harald Welte | c53afd9 | 2019-05-27 23:13:11 +0200 | [diff] [blame] | 19 | * SPDX-License-Identifier: GPLv2+ |
Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <osmocom/core/application.h> |
| 23 | #include <osmocom/core/logging.h> |
| 24 | #include <osmocom/gsm/oap.h> |
| 25 | |
| 26 | #include <osmocom/gsm/oap_client.h> |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | static void test_oap_api(void) |
| 32 | { |
| 33 | printf("Testing OAP API\n"); |
| 34 | |
| 35 | struct osmo_oap_client_config _config; |
| 36 | struct osmo_oap_client_config *config = &_config; |
| 37 | |
| 38 | struct osmo_oap_client_state _state; |
| 39 | struct osmo_oap_client_state *state = &_state; |
| 40 | |
| 41 | struct osmo_oap_message oap_rx; |
| 42 | struct msgb *msg_rx; |
| 43 | |
| 44 | struct osmo_oap_message oap_tx; |
| 45 | struct msgb *msg_tx; |
| 46 | |
| 47 | memset(config, 0, sizeof(*config)); |
| 48 | memset(state, 0, sizeof(*state)); |
| 49 | |
| 50 | OSMO_ASSERT(osmo_hexparse("0102030405060708090a0b0c0d0e0f10", config->secret_k, 16) == 16); |
| 51 | OSMO_ASSERT(osmo_hexparse("1112131415161718191a1b1c1d1e1f20", config->secret_opc, 16) == 16); |
| 52 | |
| 53 | fprintf(stderr, "- make sure filling with zeros means uninitialized\n"); |
| 54 | OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED); |
| 55 | |
| 56 | fprintf(stderr, "- reject messages in uninitialized state\n"); |
| 57 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 58 | state->client_id = 1; |
| 59 | oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; |
| 60 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 61 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0); |
| 62 | OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED); |
| 63 | msgb_free(msg_rx); |
| 64 | OSMO_ASSERT(!msg_tx); |
| 65 | |
| 66 | fprintf(stderr, "- NULL config should disable\n"); |
| 67 | OSMO_ASSERT( osmo_oap_client_init(NULL, state) == 0 ); |
| 68 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 69 | |
| 70 | fprintf(stderr, "- reject messages in disabled state\n"); |
| 71 | memset(state, 0, sizeof(*state)); |
| 72 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 73 | state->state = OSMO_OAP_DISABLED; |
| 74 | state->client_id = 1; |
| 75 | oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; |
| 76 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 77 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0); |
| 78 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 79 | msgb_free(msg_rx); |
| 80 | OSMO_ASSERT(!msg_tx); |
| 81 | |
| 82 | fprintf(stderr, "- invalid client_id and shared secret\n"); |
| 83 | memset(state, 0, sizeof(*state)); |
| 84 | config->client_id = 0; |
| 85 | config->secret_k_present = 0; |
| 86 | config->secret_opc_present = 0; |
| 87 | OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 ); |
| 88 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 89 | |
| 90 | fprintf(stderr, "- reset state\n"); |
| 91 | memset(state, 0, sizeof(*state)); |
| 92 | |
| 93 | fprintf(stderr, "- only client_id is invalid\n"); |
| 94 | config->client_id = 0; |
| 95 | config->secret_k_present = 1; |
| 96 | config->secret_opc_present = 1; |
| 97 | OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 ); |
| 98 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 99 | |
| 100 | memset(state, 0, sizeof(*state)); |
| 101 | |
| 102 | fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n"); |
| 103 | config->client_id = 12345; |
| 104 | config->secret_k_present = 0; |
| 105 | config->secret_opc_present = 1; |
| 106 | OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 ); |
| 107 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 108 | |
| 109 | memset(state, 0, sizeof(*state)); |
| 110 | |
| 111 | fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n"); |
| 112 | config->client_id = 12345; |
| 113 | config->secret_k_present = 1; |
| 114 | config->secret_opc_present = 0; |
| 115 | OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 ); |
| 116 | OSMO_ASSERT(state->state == OSMO_OAP_DISABLED); |
| 117 | |
| 118 | memset(state, 0, sizeof(*state)); |
| 119 | |
| 120 | |
| 121 | fprintf(stderr, "- mint configuration\n"); |
| 122 | config->client_id = 12345; |
| 123 | config->secret_k_present = 1; |
| 124 | config->secret_opc_present = 1; |
| 125 | /*config->secret_* buffers are still set from the top */ |
| 126 | OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 ); |
| 127 | OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED); |
| 128 | |
| 129 | |
| 130 | fprintf(stderr, "- Missing challenge data\n"); |
| 131 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 132 | oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 133 | oap_rx.rand_present = 0; |
| 134 | oap_rx.autn_present = 0; |
| 135 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 136 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2); |
| 137 | msgb_free(msg_rx); |
| 138 | OSMO_ASSERT(!msg_tx); |
| 139 | |
| 140 | fprintf(stderr, "- AUTN missing\n"); |
| 141 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 142 | oap_rx.rand, 16); |
| 143 | oap_rx.rand_present = 1; |
| 144 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 145 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2); |
| 146 | msgb_free(msg_rx); |
| 147 | OSMO_ASSERT(!msg_tx); |
| 148 | |
| 149 | fprintf(stderr, "- RAND missing\n"); |
| 150 | oap_rx.rand_present = 0; |
| 151 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 152 | oap_rx.autn, 16); |
| 153 | oap_rx.autn_present = 1; |
| 154 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 155 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2); |
| 156 | msgb_free(msg_rx); |
| 157 | OSMO_ASSERT(!msg_tx); |
| 158 | |
| 159 | fprintf(stderr, "- wrong autn (by one bit)\n"); |
| 160 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 161 | oap_rx.rand, 16); |
| 162 | osmo_hexparse("dec4e3848a33000086781158ca40f136", |
| 163 | oap_rx.autn, 16); |
| 164 | oap_rx.rand_present = 1; |
| 165 | oap_rx.autn_present = 1; |
| 166 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 167 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2); |
| 168 | msgb_free(msg_rx); |
| 169 | OSMO_ASSERT(!msg_tx); |
| 170 | |
| 171 | fprintf(stderr, "- all data correct\n"); |
| 172 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 173 | oap_rx.autn, 16); |
| 174 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 175 | |
| 176 | fprintf(stderr, "- but refuse to evaluate in uninitialized state\n"); |
| 177 | OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED); |
| 178 | |
| 179 | state->state = OSMO_OAP_UNINITIALIZED; |
| 180 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0); |
| 181 | OSMO_ASSERT(!msg_tx); |
| 182 | |
| 183 | state->state = OSMO_OAP_DISABLED; |
| 184 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0); |
| 185 | OSMO_ASSERT(!msg_tx); |
| 186 | |
| 187 | state->state = OSMO_OAP_INITIALIZED; |
| 188 | |
| 189 | fprintf(stderr, "- now everything is correct\n"); |
| 190 | /* a successful return value here indicates correct autn */ |
| 191 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0); |
| 192 | msgb_free(msg_rx); |
| 193 | |
| 194 | fprintf(stderr, "- Expect the challenge response in msg_tx\n"); |
| 195 | OSMO_ASSERT(msg_tx); |
| 196 | OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); |
| 197 | OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT); |
| 198 | OSMO_ASSERT(strcmp("e2d05b598c61d9ba", |
| 199 | osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres))) |
| 200 | == 0); |
| 201 | OSMO_ASSERT(state->state == OSMO_OAP_SENT_CHALLENGE_RESULT); |
| 202 | msgb_free(msg_tx); |
| 203 | msg_tx = 0; |
| 204 | |
| 205 | struct osmo_oap_client_state saved_state = _state; |
| 206 | |
| 207 | fprintf(stderr, "- Receive registration error for the first time.\n"); |
| 208 | |
| 209 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 210 | oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; |
| 211 | oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 212 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 213 | |
| 214 | OSMO_ASSERT(state->registration_failures == 0); |
| 215 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0); |
| 216 | OSMO_ASSERT(state->registration_failures == 1); |
| 217 | OSMO_ASSERT(msg_tx); |
| 218 | OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); |
| 219 | OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST); |
| 220 | OSMO_ASSERT(state->state == OSMO_OAP_REQUESTED_CHALLENGE); |
| 221 | msgb_free(msg_tx); |
| 222 | msg_tx = 0; |
| 223 | |
| 224 | fprintf(stderr, "- Receive registration error for the Nth time.\n"); |
| 225 | state->registration_failures = 999; |
| 226 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -11); |
| 227 | OSMO_ASSERT(!msg_tx); |
| 228 | OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED); |
| 229 | msgb_free(msg_tx); |
| 230 | msg_tx = 0; |
| 231 | |
| 232 | msgb_free(msg_rx); |
| 233 | |
| 234 | fprintf(stderr, "- Registration success\n"); |
| 235 | |
| 236 | _state = saved_state; |
| 237 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 238 | oap_rx.message_type = OAP_MSGT_REGISTER_RESULT; |
| 239 | msg_rx = osmo_oap_client_encoded(&oap_rx); |
| 240 | OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0); |
| 241 | OSMO_ASSERT(!msg_tx); |
| 242 | OSMO_ASSERT(state->state == OSMO_OAP_REGISTERED); |
| 243 | msgb_free(msg_rx); |
| 244 | } |
| 245 | |
| 246 | static struct log_info_cat oap_client_test_categories[] = { |
| 247 | }; |
| 248 | |
| 249 | static struct log_info info = { |
| 250 | .cat = oap_client_test_categories, |
| 251 | .num_cat = ARRAY_SIZE(oap_client_test_categories), |
| 252 | }; |
| 253 | |
| 254 | int main(int argc, char **argv) |
| 255 | { |
| 256 | void *ctx = talloc_named_const(NULL, 0, "oap_client_test"); |
| 257 | msgb_talloc_ctx_init(ctx, 0); |
| 258 | osmo_init_logging2(ctx, &info); |
| 259 | |
| 260 | OSMO_ASSERT(osmo_stderr_target); |
| 261 | log_set_use_color(osmo_stderr_target, 0); |
| 262 | log_set_print_timestamp(osmo_stderr_target, 0); |
Pau Espin Pedrol | 01e0d3e | 2021-02-18 19:25:44 +0100 | [diff] [blame] | 263 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Pau Espin Pedrol | 690b661 | 2021-02-18 19:10:28 +0100 | [diff] [blame] | 264 | log_set_print_category_hex(osmo_stderr_target, 0); |
Harald Welte | 6db529a | 2018-07-30 18:14:45 +0200 | [diff] [blame] | 265 | log_set_print_category(osmo_stderr_target, 1); |
| 266 | log_parse_category_mask(osmo_stderr_target, "DLOAP,1"); |
| 267 | |
| 268 | test_oap_api(); |
| 269 | printf("Done\n"); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |