blob: 45decf8da1f9b4065a34805c1010e7851527875e [file] [log] [blame]
Neels Hofmeyrf06046b2015-10-12 11:57:35 +02001/* 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 Welte736474c2016-05-06 23:28:52 +020022#include <osmocom/gsm/oap.h>
Neels Hofmeyrf06046b2015-10-12 11:57:35 +020023
24#include <openbsc/debug.h>
Neels Hofmeyr11ecc932016-12-08 21:29:23 +010025#include <openbsc/oap_client.h>
Neels Hofmeyrf06046b2015-10-12 11:57:35 +020026
27#include <stdio.h>
Harald Welte31760a12016-04-27 15:17:14 +020028#include <string.h>
Neels Hofmeyrf06046b2015-10-12 11:57:35 +020029
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020030static void test_oap_api(void)
Neels Hofmeyrf06046b2015-10-12 11:57:35 +020031{
Neels Hofmeyr2e109f02016-12-08 23:35:20 +010032 printf("Testing OAP API\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020033
Neels Hofmeyr49012f12016-12-08 21:30:34 +010034 struct oap_client_config _config;
35 struct oap_client_config *config = &_config;
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020036
Neels Hofmeyr49012f12016-12-08 21:30:34 +010037 struct oap_client_state _state;
38 struct oap_client_state *state = &_state;
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020039
Neels Hofmeyr3cbc0522016-12-08 23:13:29 +010040 struct osmo_oap_message oap_rx;
41 struct msgb *msg_rx;
42
43 struct osmo_oap_message oap_tx;
44 struct msgb *msg_tx;
45
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020046 memset(config, 0, sizeof(*config));
47 memset(state, 0, sizeof(*state));
48
49 OSMO_ASSERT(osmo_hexparse("0102030405060708090a0b0c0d0e0f10", config->secret_k, 16) == 16);
50 OSMO_ASSERT(osmo_hexparse("1112131415161718191a1b1c1d1e1f20", config->secret_opc, 16) == 16);
51
Neels Hofmeyr2e109f02016-12-08 23:35:20 +010052 fprintf(stderr, "- make sure filling with zeros means uninitialized\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020053 OSMO_ASSERT(state->state == OAP_UNINITIALIZED);
54
Neels Hofmeyr3cbc0522016-12-08 23:13:29 +010055 fprintf(stderr, "- reject messages in uninitialized state EXPECTING BUGS\n");
56 memset(&oap_rx, 0, sizeof(oap_rx));
57 state->client_id = 1;
58 oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
59 msg_rx = oap_client_encoded(&oap_rx);
60 /* ATTENTION: This shows a bug in OAP: the rc should be < 0, but OAP
61 * happily accepts this message and breaks the uninitialized state. The
62 * expected rc, state and msg_tx will be fixed along with the fix. */
63 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0 /* BUG, expecting < 0 */);
64 OSMO_ASSERT(state->state == OAP_REQUESTED_CHALLENGE /* BUG, expecting OAP_UNINITIALIZED */);
65 msgb_free(msg_rx);
66 OSMO_ASSERT(msg_tx /* BUG, expecting NULL */);
67 msgb_free(msg_tx);
68
69 fprintf(stderr, "- reject messages in disabled state\n");
70 memset(state, 0, sizeof(*state));
71 memset(&oap_rx, 0, sizeof(oap_rx));
72 state->state = OAP_DISABLED;
73 state->client_id = 1;
74 oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
75 msg_rx = oap_client_encoded(&oap_rx);
76 /* ATTENTION: This shows a bug in OAP: the rc should be < 0, but OAP
77 * happily accepts this message and breaks the uninitialized state. The
78 * expected rc, state and msg_tx will be fixed along with the fix. */
79 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0 /* BUG, expecting < 0 */);
80 OSMO_ASSERT(state->state == OAP_REQUESTED_CHALLENGE /* BUG, expecting OAP_DISABLED */);
81 msgb_free(msg_rx);
82 OSMO_ASSERT(msg_tx /* BUG, expecting NULL */);
83 msgb_free(msg_tx);
84
Neels Hofmeyr2e109f02016-12-08 23:35:20 +010085 fprintf(stderr, "- invalid client_id and shared secret\n");
Neels Hofmeyr3cbc0522016-12-08 23:13:29 +010086 memset(state, 0, sizeof(*state));
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020087 config->client_id = 0;
88 config->secret_k_present = 0;
89 config->secret_opc_present = 0;
Neels Hofmeyr49012f12016-12-08 21:30:34 +010090 OSMO_ASSERT( oap_client_init(config, state) == 0 );
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020091 OSMO_ASSERT(state->state == OAP_DISABLED);
92
Neels Hofmeyr2e109f02016-12-08 23:35:20 +010093 fprintf(stderr, "- reset state\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020094 memset(state, 0, sizeof(*state));
95
Neels Hofmeyr2e109f02016-12-08 23:35:20 +010096 fprintf(stderr, "- only client_id is invalid\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +020097 config->client_id = 0;
98 config->secret_k_present = 1;
99 config->secret_opc_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100100 OSMO_ASSERT( oap_client_init(config, state) == 0 );
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200101 OSMO_ASSERT(state->state == OAP_DISABLED);
102
103 memset(state, 0, sizeof(*state));
104
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100105 fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200106 config->client_id = 12345;
107 config->secret_k_present = 0;
108 config->secret_opc_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100109 OSMO_ASSERT( oap_client_init(config, state) == 0 );
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200110 OSMO_ASSERT(state->state == OAP_DISABLED);
111
112 memset(state, 0, sizeof(*state));
113
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100114 fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200115 config->client_id = 12345;
116 config->secret_k_present = 1;
117 config->secret_opc_present = 0;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100118 OSMO_ASSERT( oap_client_init(config, state) == 0 );
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200119 OSMO_ASSERT(state->state == OAP_DISABLED);
120
121 memset(state, 0, sizeof(*state));
122
123
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100124 fprintf(stderr, "- mint configuration\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200125 config->client_id = 12345;
126 config->secret_k_present = 1;
127 config->secret_opc_present = 1;
128 /*config->secret_* buffers are still set from the top */
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100129 OSMO_ASSERT( oap_client_init(config, state) == 0 );
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200130 OSMO_ASSERT(state->state == OAP_INITIALIZED);
131
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200132
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100133 fprintf(stderr, "- Missing challenge data\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200134 memset(&oap_rx, 0, sizeof(oap_rx));
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200135 oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST;
136 oap_rx.rand_present = 0;
137 oap_rx.autn_present = 0;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100138 msg_rx = oap_client_encoded(&oap_rx);
139 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200140 msgb_free(msg_rx);
141 OSMO_ASSERT(!msg_tx);
142
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100143 fprintf(stderr, "- AUTN missing\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200144 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
145 oap_rx.rand, 16);
146 oap_rx.rand_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100147 msg_rx = oap_client_encoded(&oap_rx);
148 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200149 msgb_free(msg_rx);
150 OSMO_ASSERT(!msg_tx);
151
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100152 fprintf(stderr, "- RAND missing\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200153 oap_rx.rand_present = 0;
154 osmo_hexparse("cec4e3848a33000086781158ca40f136",
155 oap_rx.autn, 16);
156 oap_rx.autn_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100157 msg_rx = oap_client_encoded(&oap_rx);
158 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200159 msgb_free(msg_rx);
160 OSMO_ASSERT(!msg_tx);
161
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100162 fprintf(stderr, "- wrong autn (by one bit)\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200163 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
164 oap_rx.rand, 16);
165 osmo_hexparse("dec4e3848a33000086781158ca40f136",
166 oap_rx.autn, 16);
167 oap_rx.rand_present = 1;
168 oap_rx.autn_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100169 msg_rx = oap_client_encoded(&oap_rx);
170 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -2);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200171 msgb_free(msg_rx);
172 OSMO_ASSERT(!msg_tx);
173
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100174 fprintf(stderr, "- all data correct\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200175 osmo_hexparse("cec4e3848a33000086781158ca40f136",
176 oap_rx.autn, 16);
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100177 msg_rx = oap_client_encoded(&oap_rx);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200178
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100179 fprintf(stderr, "- but refuse to evaluate in uninitialized state\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200180 OSMO_ASSERT(state->state == OAP_INITIALIZED);
181
182 state->state = OAP_UNINITIALIZED;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100183 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -1);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200184 OSMO_ASSERT(!msg_tx);
185
186 state->state = OAP_DISABLED;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100187 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -1);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200188 OSMO_ASSERT(!msg_tx);
189
190 state->state = OAP_INITIALIZED;
191
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100192 fprintf(stderr, "- now everything is correct\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200193 /* a successful return value here indicates correct autn */
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100194 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200195 msgb_free(msg_rx);
196
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100197 fprintf(stderr, "- Expect the challenge response in msg_tx\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200198 OSMO_ASSERT(msg_tx);
Harald Welte5d547a42016-04-27 18:21:16 +0200199 OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200200 OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT);
201 OSMO_ASSERT(strcmp("e2d05b598c61d9ba",
202 osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres)))
203 == 0);
204 OSMO_ASSERT(state->state == OAP_SENT_CHALLENGE_RESULT);
205 msgb_free(msg_tx);
206 msg_tx = 0;
207
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100208 struct oap_client_state saved_state = _state;
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200209
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100210 fprintf(stderr, "- Receive registration error for the first time.\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200211
212 memset(&oap_rx, 0, sizeof(oap_rx));
213 oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
214 oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100215 msg_rx = oap_client_encoded(&oap_rx);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200216
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200217 OSMO_ASSERT(state->registration_failures == 0);
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100218 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200219 OSMO_ASSERT(state->registration_failures == 1);
220 OSMO_ASSERT(msg_tx);
Harald Welte5d547a42016-04-27 18:21:16 +0200221 OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200222 OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST);
223 OSMO_ASSERT(state->state == OAP_REQUESTED_CHALLENGE);
224 msgb_free(msg_tx);
225 msg_tx = 0;
226
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100227 fprintf(stderr, "- Receive registration error for the Nth time.\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200228 state->registration_failures = 999;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100229 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == -11);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200230 OSMO_ASSERT(!msg_tx);
231 OSMO_ASSERT(state->state == OAP_INITIALIZED);
232 msgb_free(msg_tx);
233 msg_tx = 0;
234
235 msgb_free(msg_rx);
236
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100237 fprintf(stderr, "- Registration success\n");
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200238
239 _state = saved_state;
240 memset(&oap_rx, 0, sizeof(oap_rx));
241 oap_rx.message_type = OAP_MSGT_REGISTER_RESULT;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100242 msg_rx = oap_client_encoded(&oap_rx);
243 OSMO_ASSERT(oap_client_handle(state, msg_rx, &msg_tx) == 0);
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200244 OSMO_ASSERT(!msg_tx);
245 OSMO_ASSERT(state->state == OAP_REGISTERED);
246 msgb_free(msg_rx);
Neels Hofmeyrf06046b2015-10-12 11:57:35 +0200247}
248
Neels Hofmeyr73ed4552016-12-09 00:01:56 +0100249static struct log_info_cat oap_client_test_categories[] = {
Neels Hofmeyrf06046b2015-10-12 11:57:35 +0200250};
251
252static struct log_info info = {
Neels Hofmeyr73ed4552016-12-09 00:01:56 +0100253 .cat = oap_client_test_categories,
254 .num_cat = ARRAY_SIZE(oap_client_test_categories),
Neels Hofmeyrf06046b2015-10-12 11:57:35 +0200255};
256
257int main(int argc, char **argv)
258{
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +0200259 msgb_talloc_ctx_init(NULL, 0);
Neels Hofmeyrf06046b2015-10-12 11:57:35 +0200260 osmo_init_logging(&info);
261
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100262 OSMO_ASSERT(osmo_stderr_target);
Neels Hofmeyr73ed4552016-12-09 00:01:56 +0100263 log_set_use_color(osmo_stderr_target, 0);
264 log_set_print_timestamp(osmo_stderr_target, 0);
265 log_set_print_filename(osmo_stderr_target, 0);
266 log_set_print_category(osmo_stderr_target, 1);
267 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
Neels Hofmeyr2e109f02016-12-08 23:35:20 +0100268
Neels Hofmeyr89ef3242015-10-12 11:57:36 +0200269 test_oap_api();
Neels Hofmeyrf06046b2015-10-12 11:57:35 +0200270 printf("Done\n");
271
272 return 0;
273}
274