blob: a841b381ee3f59c152d7baa24ff23ad25ab1be76 [file] [log] [blame]
Harald Welte6db529a2018-07-30 18:14:45 +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>
22#include <osmocom/core/logging.h>
23#include <osmocom/gsm/oap.h>
24
25#include <osmocom/gsm/oap_client.h>
26
27#include <stdio.h>
28#include <string.h>
29
30static void test_oap_api(void)
31{
32 printf("Testing OAP API\n");
33
34 struct osmo_oap_client_config _config;
35 struct osmo_oap_client_config *config = &_config;
36
37 struct osmo_oap_client_state _state;
38 struct osmo_oap_client_state *state = &_state;
39
40 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
46 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
52 fprintf(stderr, "- make sure filling with zeros means uninitialized\n");
53 OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED);
54
55 fprintf(stderr, "- reject messages in uninitialized state\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 = osmo_oap_client_encoded(&oap_rx);
60 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
61 OSMO_ASSERT(state->state == OSMO_OAP_UNINITIALIZED);
62 msgb_free(msg_rx);
63 OSMO_ASSERT(!msg_tx);
64
65 fprintf(stderr, "- NULL config should disable\n");
66 OSMO_ASSERT( osmo_oap_client_init(NULL, state) == 0 );
67 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
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 = OSMO_OAP_DISABLED;
73 state->client_id = 1;
74 oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
75 msg_rx = osmo_oap_client_encoded(&oap_rx);
76 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
77 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
78 msgb_free(msg_rx);
79 OSMO_ASSERT(!msg_tx);
80
81 fprintf(stderr, "- invalid client_id and shared secret\n");
82 memset(state, 0, sizeof(*state));
83 config->client_id = 0;
84 config->secret_k_present = 0;
85 config->secret_opc_present = 0;
86 OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
87 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
88
89 fprintf(stderr, "- reset state\n");
90 memset(state, 0, sizeof(*state));
91
92 fprintf(stderr, "- only client_id is invalid\n");
93 config->client_id = 0;
94 config->secret_k_present = 1;
95 config->secret_opc_present = 1;
96 OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
97 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
98
99 memset(state, 0, sizeof(*state));
100
101 fprintf(stderr, "- valid id, but omitted shared_secret (1/2)\n");
102 config->client_id = 12345;
103 config->secret_k_present = 0;
104 config->secret_opc_present = 1;
105 OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
106 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
107
108 memset(state, 0, sizeof(*state));
109
110 fprintf(stderr, "- valid id, but omitted shared_secret (2/2)\n");
111 config->client_id = 12345;
112 config->secret_k_present = 1;
113 config->secret_opc_present = 0;
114 OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
115 OSMO_ASSERT(state->state == OSMO_OAP_DISABLED);
116
117 memset(state, 0, sizeof(*state));
118
119
120 fprintf(stderr, "- mint configuration\n");
121 config->client_id = 12345;
122 config->secret_k_present = 1;
123 config->secret_opc_present = 1;
124 /*config->secret_* buffers are still set from the top */
125 OSMO_ASSERT( osmo_oap_client_init(config, state) == 0 );
126 OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
127
128
129 fprintf(stderr, "- Missing challenge data\n");
130 memset(&oap_rx, 0, sizeof(oap_rx));
131 oap_rx.message_type = OAP_MSGT_CHALLENGE_REQUEST;
132 oap_rx.rand_present = 0;
133 oap_rx.autn_present = 0;
134 msg_rx = osmo_oap_client_encoded(&oap_rx);
135 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
136 msgb_free(msg_rx);
137 OSMO_ASSERT(!msg_tx);
138
139 fprintf(stderr, "- AUTN missing\n");
140 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
141 oap_rx.rand, 16);
142 oap_rx.rand_present = 1;
143 msg_rx = osmo_oap_client_encoded(&oap_rx);
144 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
145 msgb_free(msg_rx);
146 OSMO_ASSERT(!msg_tx);
147
148 fprintf(stderr, "- RAND missing\n");
149 oap_rx.rand_present = 0;
150 osmo_hexparse("cec4e3848a33000086781158ca40f136",
151 oap_rx.autn, 16);
152 oap_rx.autn_present = 1;
153 msg_rx = osmo_oap_client_encoded(&oap_rx);
154 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
155 msgb_free(msg_rx);
156 OSMO_ASSERT(!msg_tx);
157
158 fprintf(stderr, "- wrong autn (by one bit)\n");
159 osmo_hexparse("0102030405060708090a0b0c0d0e0f10",
160 oap_rx.rand, 16);
161 osmo_hexparse("dec4e3848a33000086781158ca40f136",
162 oap_rx.autn, 16);
163 oap_rx.rand_present = 1;
164 oap_rx.autn_present = 1;
165 msg_rx = osmo_oap_client_encoded(&oap_rx);
166 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -2);
167 msgb_free(msg_rx);
168 OSMO_ASSERT(!msg_tx);
169
170 fprintf(stderr, "- all data correct\n");
171 osmo_hexparse("cec4e3848a33000086781158ca40f136",
172 oap_rx.autn, 16);
173 msg_rx = osmo_oap_client_encoded(&oap_rx);
174
175 fprintf(stderr, "- but refuse to evaluate in uninitialized state\n");
176 OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
177
178 state->state = OSMO_OAP_UNINITIALIZED;
179 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
180 OSMO_ASSERT(!msg_tx);
181
182 state->state = OSMO_OAP_DISABLED;
183 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) < 0);
184 OSMO_ASSERT(!msg_tx);
185
186 state->state = OSMO_OAP_INITIALIZED;
187
188 fprintf(stderr, "- now everything is correct\n");
189 /* a successful return value here indicates correct autn */
190 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
191 msgb_free(msg_rx);
192
193 fprintf(stderr, "- Expect the challenge response in msg_tx\n");
194 OSMO_ASSERT(msg_tx);
195 OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
196 OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_CHALLENGE_RESULT);
197 OSMO_ASSERT(strcmp("e2d05b598c61d9ba",
198 osmo_hexdump_nospc(oap_tx.xres, sizeof(oap_tx.xres)))
199 == 0);
200 OSMO_ASSERT(state->state == OSMO_OAP_SENT_CHALLENGE_RESULT);
201 msgb_free(msg_tx);
202 msg_tx = 0;
203
204 struct osmo_oap_client_state saved_state = _state;
205
206 fprintf(stderr, "- Receive registration error for the first time.\n");
207
208 memset(&oap_rx, 0, sizeof(oap_rx));
209 oap_rx.message_type = OAP_MSGT_REGISTER_ERROR;
210 oap_rx.cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
211 msg_rx = osmo_oap_client_encoded(&oap_rx);
212
213 OSMO_ASSERT(state->registration_failures == 0);
214 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
215 OSMO_ASSERT(state->registration_failures == 1);
216 OSMO_ASSERT(msg_tx);
217 OSMO_ASSERT(osmo_oap_decode(&oap_tx, msg_tx->data, msg_tx->len) == 0);
218 OSMO_ASSERT(oap_tx.message_type == OAP_MSGT_REGISTER_REQUEST);
219 OSMO_ASSERT(state->state == OSMO_OAP_REQUESTED_CHALLENGE);
220 msgb_free(msg_tx);
221 msg_tx = 0;
222
223 fprintf(stderr, "- Receive registration error for the Nth time.\n");
224 state->registration_failures = 999;
225 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == -11);
226 OSMO_ASSERT(!msg_tx);
227 OSMO_ASSERT(state->state == OSMO_OAP_INITIALIZED);
228 msgb_free(msg_tx);
229 msg_tx = 0;
230
231 msgb_free(msg_rx);
232
233 fprintf(stderr, "- Registration success\n");
234
235 _state = saved_state;
236 memset(&oap_rx, 0, sizeof(oap_rx));
237 oap_rx.message_type = OAP_MSGT_REGISTER_RESULT;
238 msg_rx = osmo_oap_client_encoded(&oap_rx);
239 OSMO_ASSERT(osmo_oap_client_handle(state, msg_rx, &msg_tx) == 0);
240 OSMO_ASSERT(!msg_tx);
241 OSMO_ASSERT(state->state == OSMO_OAP_REGISTERED);
242 msgb_free(msg_rx);
243}
244
245static struct log_info_cat oap_client_test_categories[] = {
246};
247
248static struct log_info info = {
249 .cat = oap_client_test_categories,
250 .num_cat = ARRAY_SIZE(oap_client_test_categories),
251};
252
253int main(int argc, char **argv)
254{
255 void *ctx = talloc_named_const(NULL, 0, "oap_client_test");
256 msgb_talloc_ctx_init(ctx, 0);
257 osmo_init_logging2(ctx, &info);
258
259 OSMO_ASSERT(osmo_stderr_target);
260 log_set_use_color(osmo_stderr_target, 0);
261 log_set_print_timestamp(osmo_stderr_target, 0);
262 log_set_print_filename(osmo_stderr_target, 0);
263 log_set_print_category(osmo_stderr_target, 1);
264 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
265
266 test_oap_api();
267 printf("Done\n");
268
269 return 0;
270}
271