blob: 92140c17d522943250155074247de548dc0657a2 [file] [log] [blame]
Neels Hofmeyrd739f092015-10-12 11:57:34 +02001/* Osmocom Authentication Protocol API */
2
3/* (C) 2015 by Sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Harald Welte31760a12016-04-27 15:17:14 +020023#include <string.h>
24
Harald Welte50f1c0a2016-04-25 19:01:26 +020025#include <osmocom/core/utils.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020026#include <osmocom/crypt/auth.h>
Harald Welte736474c2016-05-06 23:28:52 +020027#include <osmocom/gsm/oap.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020028
Neels Hofmeyr11ecc932016-12-08 21:29:23 +010029#include <openbsc/oap_client.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020030#include <openbsc/debug.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020031
Neels Hofmeyr49012f12016-12-08 21:30:34 +010032int oap_client_init(struct oap_client_config *config,
33 struct oap_client_state *state)
Neels Hofmeyrd739f092015-10-12 11:57:34 +020034{
35 OSMO_ASSERT(state->state == OAP_UNINITIALIZED);
36
37 if (config->client_id == 0)
38 goto disable;
39
40 if (config->secret_k_present == 0) {
41 LOGP(DGPRS, LOGL_NOTICE, "OAP: client ID set, but secret K missing.\n");
42 goto disable;
43 }
44
45 if (config->secret_opc_present == 0) {
46 LOGP(DGPRS, LOGL_NOTICE, "OAP: client ID set, but secret OPC missing.\n");
47 goto disable;
48 }
49
50 state->client_id = config->client_id;
51 memcpy(state->secret_k, config->secret_k, sizeof(state->secret_k));
52 memcpy(state->secret_opc, config->secret_opc, sizeof(state->secret_opc));
53 state->state = OAP_INITIALIZED;
54 return 0;
55
56disable:
57 state->state = OAP_DISABLED;
58 return 0;
59}
60
61/* From the given state and received RAND and AUTN octets, validate the
62 * server's authenticity and formulate the matching milenage reply octets in
63 * *tx_xres. The state is not modified.
64 * On success, and if tx_res is not NULL, exactly 8 octets will be written to
65 * *tx_res. If not NULL, tx_res must point at allocated memory of at least 8
66 * octets. The caller will want to send XRES back to the server in a challenge
67 * response message and update the state.
68 * Return 0 on success; -1 if OAP is disabled; -2 if rx_random and rx_autn fail
69 * the authentication check; -3 for any other errors. */
Neels Hofmeyr49012f12016-12-08 21:30:34 +010070static int oap_evaluate_challenge(const struct oap_client_state *state,
Neels Hofmeyrd739f092015-10-12 11:57:34 +020071 const uint8_t *rx_random,
72 const uint8_t *rx_autn,
73 uint8_t *tx_xres)
74{
Neels Hofmeyrd739f092015-10-12 11:57:34 +020075 struct osmo_auth_vector vec;
76
77 struct osmo_sub_auth_data auth = {
78 .type = OSMO_AUTH_TYPE_UMTS,
79 .algo = OSMO_AUTH_ALG_MILENAGE,
80 };
81
Harald Welted8aa4122016-04-27 18:17:26 +020082 osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.k)
83 == sizeof(state->secret_k), _secret_k_size_match);
84 osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.opc)
85 == sizeof(state->secret_opc), _secret_opc_size_match);
86
87 switch (state->state) {
88 case OAP_UNINITIALIZED:
89 case OAP_DISABLED:
90 return -1;
91 default:
92 break;
93 }
94
Neels Hofmeyrd739f092015-10-12 11:57:34 +020095 memcpy(auth.u.umts.k, state->secret_k, sizeof(auth.u.umts.k));
96 memcpy(auth.u.umts.opc, state->secret_opc, sizeof(auth.u.umts.opc));
97 memset(auth.u.umts.amf, '\0', sizeof(auth.u.umts.amf));
98 auth.u.umts.sqn = 42; /* TODO use incrementing sequence nr */
99
100 memset(&vec, 0, sizeof(vec));
101 osmo_auth_gen_vec(&vec, &auth, rx_random);
102
103 if (vec.res_len != 8) {
104 LOGP(DGPRS, LOGL_ERROR, "OAP: Expected XRES to be 8 octets, got %d\n",
105 vec.res_len);
106 return -3;
107 }
108
Harald Welte50f1c0a2016-04-25 19:01:26 +0200109 if (osmo_constant_time_cmp(vec.autn, rx_autn, sizeof(vec.autn)) != 0) {
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200110 LOGP(DGPRS, LOGL_ERROR, "OAP: AUTN mismatch!\n");
111 LOGP(DGPRS, LOGL_INFO, "OAP: AUTN from server: %s\n",
112 osmo_hexdump_nospc(rx_autn, sizeof(vec.autn)));
113 LOGP(DGPRS, LOGL_INFO, "OAP: AUTN expected: %s\n",
114 osmo_hexdump_nospc(vec.autn, sizeof(vec.autn)));
115 return -2;
116 }
117
118 if (tx_xres != NULL)
119 memcpy(tx_xres, vec.res, 8);
120 return 0;
121}
122
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100123struct msgb *oap_client_encoded(const struct osmo_oap_message *oap_msg)
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200124{
125 struct msgb *msg = msgb_alloc_headroom(1000, 64, __func__);
126 OSMO_ASSERT(msg);
Harald Welte564c0652016-04-27 18:14:14 +0200127 osmo_oap_encode(msg, oap_msg);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200128 return msg;
129}
130
131/* Create a new msgb containing an OAP registration message.
132 * On error, return NULL. */
133static struct msgb* oap_msg_register(uint16_t client_id)
134{
Harald Welted8aa4122016-04-27 18:17:26 +0200135 struct osmo_oap_message oap_msg = {0};
136
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200137 if (client_id < 1) {
138 LOGP(DGPRS, LOGL_ERROR, "OAP: Invalid client ID: %d\n", client_id);
139 return NULL;
140 }
141
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200142 oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST;
143 oap_msg.client_id = client_id;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100144 return oap_client_encoded(&oap_msg);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200145}
146
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100147int oap_client_register(struct oap_client_state *state, struct msgb **msg_tx)
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200148{
149 *msg_tx = oap_msg_register(state->client_id);
150 if (!(*msg_tx))
151 return -1;
152
153 state->state = OAP_REQUESTED_CHALLENGE;
154 return 0;
155}
156
157/* Create a new msgb containing an OAP challenge response message.
158 * xres must point at 8 octets to return as challenge response.
159 * On error, return NULL. */
160static struct msgb* oap_msg_challenge_response(uint8_t *xres)
161{
Harald Welte564c0652016-04-27 18:14:14 +0200162 struct osmo_oap_message oap_reply = {0};
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200163
164 oap_reply.message_type = OAP_MSGT_CHALLENGE_RESULT;
165 memcpy(oap_reply.xres, xres, sizeof(oap_reply.xres));
166 oap_reply.xres_present = 1;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100167 return oap_client_encoded(&oap_reply);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200168}
169
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100170static int handle_challenge(struct oap_client_state *state,
Harald Welte564c0652016-04-27 18:14:14 +0200171 struct osmo_oap_message *oap_rx,
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200172 struct msgb **msg_tx)
173{
174 int rc;
Harald Welted8aa4122016-04-27 18:17:26 +0200175 uint8_t xres[8];
176
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200177 if (!(oap_rx->rand_present && oap_rx->autn_present)) {
178 LOGP(DGPRS, LOGL_ERROR,
179 "OAP challenge incomplete (rand_present: %d, autn_present: %d)\n",
180 oap_rx->rand_present, oap_rx->autn_present);
181 rc = -2;
182 goto failure;
183 }
184
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200185 rc = oap_evaluate_challenge(state,
186 oap_rx->rand,
187 oap_rx->autn,
188 xres);
189 if (rc < 0)
190 goto failure;
191
192 *msg_tx = oap_msg_challenge_response(xres);
193 if ((*msg_tx) == NULL) {
194 rc = -1;
195 goto failure;
196 }
197
198 state->state = OAP_SENT_CHALLENGE_RESULT;
199 return 0;
200
201failure:
202 OSMO_ASSERT(rc < 0);
203 state->state = OAP_INITIALIZED;
204 return rc;
205}
206
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100207int oap_client_handle(struct oap_client_state *state,
208 const struct msgb *msg_rx, struct msgb **msg_tx)
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200209{
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200210 uint8_t *data = msgb_l2(msg_rx);
211 size_t data_len = msgb_l2len(msg_rx);
Harald Welted8aa4122016-04-27 18:17:26 +0200212 struct osmo_oap_message oap_msg = {0};
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200213 int rc = 0;
214
Harald Welted8aa4122016-04-27 18:17:26 +0200215 *msg_tx = NULL;
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200216
217 OSMO_ASSERT(data);
218
Harald Welte5d547a42016-04-27 18:21:16 +0200219 rc = osmo_oap_decode(&oap_msg, data, data_len);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200220 if (rc < 0) {
221 LOGP(DGPRS, LOGL_ERROR,
222 "Decoding OAP message failed with error '%s' (%d)\n",
223 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
224 return -10;
225 }
226
227 switch (oap_msg.message_type) {
228 case OAP_MSGT_CHALLENGE_REQUEST:
229 return handle_challenge(state, &oap_msg, msg_tx);
230
231 case OAP_MSGT_REGISTER_RESULT:
232 /* successfully registered */
233 state->state = OAP_REGISTERED;
234 break;
235
236 case OAP_MSGT_REGISTER_ERROR:
237 LOGP(DGPRS, LOGL_ERROR,
238 "OAP registration failed\n");
239 state->state = OAP_INITIALIZED;
240 if (state->registration_failures < 3) {
241 state->registration_failures ++;
Neels Hofmeyr49012f12016-12-08 21:30:34 +0100242 return oap_client_register(state, msg_tx);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200243 }
244 return -11;
245
246 case OAP_MSGT_REGISTER_REQUEST:
247 case OAP_MSGT_CHALLENGE_RESULT:
248 LOGP(DGPRS, LOGL_ERROR,
249 "Received invalid OAP message type for OAP client side: %d\n",
250 (int)oap_msg.message_type);
251 return -12;
252
253 default:
254 LOGP(DGPRS, LOGL_ERROR,
255 "Unknown OAP message type: %d\n",
256 (int)oap_msg.message_type);
257 return -13;
258 }
259
260 return 0;
261}