Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +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 |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 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 |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/application.h> |
Harald Welte | 736474c | 2016-05-06 23:28:52 +0200 | [diff] [blame] | 22 | #include <osmocom/gsm/oap.h> |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 23 | |
| 24 | #include <openbsc/debug.h> |
Neels Hofmeyr | 11ecc93 | 2016-12-08 21:29:23 +0100 | [diff] [blame] | 25 | #include <openbsc/oap_client.h> |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
Harald Welte | 31760a1 | 2016-04-27 15:17:14 +0200 | [diff] [blame] | 28 | #include <string.h> |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 29 | |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 30 | static void test_oap_api(void) |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 31 | { |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 32 | printf("Testing OAP API\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 33 | |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 34 | struct oap_client_config _config; |
| 35 | struct oap_client_config *config = &_config; |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 36 | |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 37 | struct oap_client_state _state; |
| 38 | struct oap_client_state *state = &_state; |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 39 | |
| 40 | memset(config, 0, sizeof(*config)); |
| 41 | memset(state, 0, sizeof(*state)); |
| 42 | |
| 43 | OSMO_ASSERT(osmo_hexparse("0102030405060708090a0b0c0d0e0f10", config->secret_k, 16) == 16); |
| 44 | OSMO_ASSERT(osmo_hexparse("1112131415161718191a1b1c1d1e1f20", config->secret_opc, 16) == 16); |
| 45 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 46 | fprintf(stderr, "- make sure filling with zeros means uninitialized\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 47 | OSMO_ASSERT(state->state == OAP_UNINITIALIZED); |
| 48 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 49 | fprintf(stderr, "- invalid client_id and shared secret\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 50 | config->client_id = 0; |
| 51 | config->secret_k_present = 0; |
| 52 | config->secret_opc_present = 0; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 53 | OSMO_ASSERT( oap_client_init(config, state) == 0 ); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 54 | OSMO_ASSERT(state->state == OAP_DISABLED); |
| 55 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 56 | fprintf(stderr, "- reset state\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 57 | memset(state, 0, sizeof(*state)); |
| 58 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 59 | fprintf(stderr, "- only client_id is invalid\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 60 | config->client_id = 0; |
| 61 | config->secret_k_present = 1; |
| 62 | config->secret_opc_present = 1; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 63 | OSMO_ASSERT( oap_client_init(config, state) == 0 ); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 64 | OSMO_ASSERT(state->state == OAP_DISABLED); |
| 65 | |
| 66 | memset(state, 0, sizeof(*state)); |
| 67 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 68 | fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 69 | config->client_id = 12345; |
| 70 | config->secret_k_present = 0; |
| 71 | config->secret_opc_present = 1; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 72 | OSMO_ASSERT( oap_client_init(config, state) == 0 ); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 73 | OSMO_ASSERT(state->state == OAP_DISABLED); |
| 74 | |
| 75 | memset(state, 0, sizeof(*state)); |
| 76 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 77 | fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 78 | config->client_id = 12345; |
| 79 | config->secret_k_present = 1; |
| 80 | config->secret_opc_present = 0; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 81 | OSMO_ASSERT( oap_client_init(config, state) == 0 ); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 82 | OSMO_ASSERT(state->state == OAP_DISABLED); |
| 83 | |
| 84 | memset(state, 0, sizeof(*state)); |
| 85 | |
| 86 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 87 | fprintf(stderr, "- mint configuration\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 88 | config->client_id = 12345; |
| 89 | config->secret_k_present = 1; |
| 90 | config->secret_opc_present = 1; |
| 91 | /*config->secret_* buffers are still set from the top */ |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 92 | OSMO_ASSERT( oap_client_init(config, state) == 0 ); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 93 | OSMO_ASSERT(state->state == OAP_INITIALIZED); |
| 94 | |
Harald Welte | 564c065 | 2016-04-27 18:14:14 +0200 | [diff] [blame] | 95 | struct osmo_oap_message oap_rx; |
| 96 | struct osmo_oap_message oap_tx; |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 97 | struct msgb *msg_rx; |
| 98 | struct msgb *msg_tx; |
| 99 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 100 | fprintf(stderr, "- Missing challenge data\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 101 | memset(&oap_rx, 0, sizeof(oap_rx)); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 102 | oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST; |
| 103 | oap_rx.rand_present = 0; |
| 104 | oap_rx.autn_present = 0; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 105 | msg_rx = oap_client_encoded(&oap_rx); |
| 106 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 107 | msgb_free(msg_rx); |
| 108 | OSMO_ASSERT(!msg_tx); |
| 109 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 110 | fprintf(stderr, "- AUTN missing\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 111 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 112 | oap_rx.rand, 16); |
| 113 | oap_rx.rand_present = 1; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 114 | msg_rx = oap_client_encoded(&oap_rx); |
| 115 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 116 | msgb_free(msg_rx); |
| 117 | OSMO_ASSERT(!msg_tx); |
| 118 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 119 | fprintf(stderr, "- RAND missing\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 120 | oap_rx.rand_present = 0; |
| 121 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 122 | oap_rx.autn, 16); |
| 123 | oap_rx.autn_present = 1; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 124 | msg_rx = oap_client_encoded(&oap_rx); |
| 125 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 126 | msgb_free(msg_rx); |
| 127 | OSMO_ASSERT(!msg_tx); |
| 128 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 129 | fprintf(stderr, "- wrong autn (by one bit)\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 130 | osmo_hexparse("0102030405060708090a0b0c0d0e0f10", |
| 131 | oap_rx.rand, 16); |
| 132 | osmo_hexparse("dec4e3848a33000086781158ca40f136", |
| 133 | oap_rx.autn, 16); |
| 134 | oap_rx.rand_present = 1; |
| 135 | oap_rx.autn_present = 1; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 136 | msg_rx = oap_client_encoded(&oap_rx); |
| 137 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 138 | msgb_free(msg_rx); |
| 139 | OSMO_ASSERT(!msg_tx); |
| 140 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 141 | fprintf(stderr, "- all data correct\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 142 | osmo_hexparse("cec4e3848a33000086781158ca40f136", |
| 143 | oap_rx.autn, 16); |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 144 | msg_rx = oap_client_encoded(&oap_rx); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 145 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 146 | fprintf(stderr, "- but refuse to evaluate in uninitialized state\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 147 | OSMO_ASSERT(state->state == OAP_INITIALIZED); |
| 148 | |
| 149 | state->state = OAP_UNINITIALIZED; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 150 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -1); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 151 | OSMO_ASSERT(!msg_tx); |
| 152 | |
| 153 | state->state = OAP_DISABLED; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 154 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -1); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 155 | OSMO_ASSERT(!msg_tx); |
| 156 | |
| 157 | state->state = OAP_INITIALIZED; |
| 158 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 159 | fprintf(stderr, "- now everything is correct\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 160 | /* a successful return value here indicates correct autn */ |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 161 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 162 | msgb_free(msg_rx); |
| 163 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 164 | fprintf(stderr, "- Expect the challenge response in msg_tx\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 165 | OSMO_ASSERT(msg_tx); |
Harald Welte | 5d547a4 | 2016-04-27 18:21:16 +0200 | [diff] [blame] | 166 | OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 167 | OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT); |
| 168 | OSMO_ASSERT(strcmp("e2d05b598c61d9ba", |
| 169 | osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres))) |
| 170 | == 0); |
| 171 | OSMO_ASSERT(state->state == OAP_SENT_CHALLENGE_RESULT); |
| 172 | msgb_free(msg_tx); |
| 173 | msg_tx = 0; |
| 174 | |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 175 | struct oap_client_state saved_state = _state; |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 176 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 177 | fprintf(stderr, "- Receive registration error for the first time.\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 178 | |
| 179 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 180 | oap_rx.message_type = OAP_MSGT_REGISTER_ERROR; |
| 181 | oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 182 | msg_rx = oap_client_encoded(&oap_rx); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 183 | |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 184 | OSMO_ASSERT(state->registration_failures == 0); |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 185 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 186 | OSMO_ASSERT(state->registration_failures == 1); |
| 187 | OSMO_ASSERT(msg_tx); |
Harald Welte | 5d547a4 | 2016-04-27 18:21:16 +0200 | [diff] [blame] | 188 | OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 189 | OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST); |
| 190 | OSMO_ASSERT(state->state == OAP_REQUESTED_CHALLENGE); |
| 191 | msgb_free(msg_tx); |
| 192 | msg_tx = 0; |
| 193 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 194 | fprintf(stderr, "- Receive registration error for the Nth time.\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 195 | state->registration_failures = 999; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 196 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -11); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 197 | OSMO_ASSERT(!msg_tx); |
| 198 | OSMO_ASSERT(state->state == OAP_INITIALIZED); |
| 199 | msgb_free(msg_tx); |
| 200 | msg_tx = 0; |
| 201 | |
| 202 | msgb_free(msg_rx); |
| 203 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 204 | fprintf(stderr, "- Registration success\n"); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 205 | |
| 206 | _state = saved_state; |
| 207 | memset(&oap_rx, 0, sizeof(oap_rx)); |
| 208 | oap_rx.message_type = OAP_MSGT_REGISTER_RESULT; |
Neels Hofmeyr | 49012f1 | 2016-12-08 21:30:34 +0100 | [diff] [blame] | 209 | msg_rx = oap_client_encoded(&oap_rx); |
| 210 | OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0); |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 211 | OSMO_ASSERT(!msg_tx); |
| 212 | OSMO_ASSERT(state->state == OAP_REGISTERED); |
| 213 | msgb_free(msg_rx); |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static struct log_info_cat gprs_categories[] = { |
| 217 | [DGPRS] = { |
| 218 | .name = "DGPRS", |
| 219 | .description = "GPRS Packet Service", |
| 220 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 221 | }, |
| 222 | }; |
| 223 | |
| 224 | static struct log_info info = { |
| 225 | .cat = gprs_categories, |
| 226 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 227 | }; |
| 228 | |
| 229 | int main(int argc, char **argv) |
| 230 | { |
Neels Hofmeyr | 4c2d4ab | 2016-09-16 02:31:17 +0200 | [diff] [blame] | 231 | msgb_talloc_ctx_init(NULL, 0); |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 232 | osmo_init_logging(&info); |
| 233 | |
Neels Hofmeyr | 2e109f0 | 2016-12-08 23:35:20 +0100 | [diff] [blame^] | 234 | OSMO_ASSERT(osmo_stderr_target); |
| 235 | osmo_stderr_target->use_color = 0; |
| 236 | osmo_stderr_target->print_timestamp = 0; |
| 237 | osmo_stderr_target->print_filename = 0; |
| 238 | osmo_stderr_target->print_category = 1; |
| 239 | |
Neels Hofmeyr | 89ef324 | 2015-10-12 11:57:36 +0200 | [diff] [blame] | 240 | test_oap_api(); |
Neels Hofmeyr | f06046b | 2015-10-12 11:57:35 +0200 | [diff] [blame] | 241 | printf("Done\n"); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |