blob: 7efbe81cd42285fbb548e32bde27289860275a39 [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
29#include <openbsc/oap.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020030#include <openbsc/debug.h>
Neels Hofmeyrd739f092015-10-12 11:57:34 +020031
32int oap_init(struct oap_config *config, struct oap_state *state)
33{
34 OSMO_ASSERT(state->state == OAP_UNINITIALIZED);
35
36 if (config->client_id == 0)
37 goto disable;
38
39 if (config->secret_k_present == 0) {
40 LOGP(DGPRS, LOGL_NOTICE, "OAP: client ID set, but secret K missing.\n");
41 goto disable;
42 }
43
44 if (config->secret_opc_present == 0) {
45 LOGP(DGPRS, LOGL_NOTICE, "OAP: client ID set, but secret OPC missing.\n");
46 goto disable;
47 }
48
49 state->client_id = config->client_id;
50 memcpy(state->secret_k, config->secret_k, sizeof(state->secret_k));
51 memcpy(state->secret_opc, config->secret_opc, sizeof(state->secret_opc));
52 state->state = OAP_INITIALIZED;
53 return 0;
54
55disable:
56 state->state = OAP_DISABLED;
57 return 0;
58}
59
60/* From the given state and received RAND and AUTN octets, validate the
61 * server's authenticity and formulate the matching milenage reply octets in
62 * *tx_xres. The state is not modified.
63 * On success, and if tx_res is not NULL, exactly 8 octets will be written to
64 * *tx_res. If not NULL, tx_res must point at allocated memory of at least 8
65 * octets. The caller will want to send XRES back to the server in a challenge
66 * response message and update the state.
67 * Return 0 on success; -1 if OAP is disabled; -2 if rx_random and rx_autn fail
68 * the authentication check; -3 for any other errors. */
69static int oap_evaluate_challenge(const struct oap_state *state,
70 const uint8_t *rx_random,
71 const uint8_t *rx_autn,
72 uint8_t *tx_xres)
73{
Neels Hofmeyrd739f092015-10-12 11:57:34 +020074 struct osmo_auth_vector vec;
75
76 struct osmo_sub_auth_data auth = {
77 .type = OSMO_AUTH_TYPE_UMTS,
78 .algo = OSMO_AUTH_ALG_MILENAGE,
79 };
80
Harald Welted8aa4122016-04-27 18:17:26 +020081 osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.k)
82 == sizeof(state->secret_k), _secret_k_size_match);
83 osmo_static_assert(sizeof(((struct osmo_sub_auth_data*)0)->u.umts.opc)
84 == sizeof(state->secret_opc), _secret_opc_size_match);
85
86 switch (state->state) {
87 case OAP_UNINITIALIZED:
88 case OAP_DISABLED:
89 return -1;
90 default:
91 break;
92 }
93
Neels Hofmeyrd739f092015-10-12 11:57:34 +020094 memcpy(auth.u.umts.k, state->secret_k, sizeof(auth.u.umts.k));
95 memcpy(auth.u.umts.opc, state->secret_opc, sizeof(auth.u.umts.opc));
96 memset(auth.u.umts.amf, '\0', sizeof(auth.u.umts.amf));
97 auth.u.umts.sqn = 42; /* TODO use incrementing sequence nr */
98
99 memset(&vec, 0, sizeof(vec));
100 osmo_auth_gen_vec(&vec, &auth, rx_random);
101
102 if (vec.res_len != 8) {
103 LOGP(DGPRS, LOGL_ERROR, "OAP: Expected XRES to be 8 octets, got %d\n",
104 vec.res_len);
105 return -3;
106 }
107
Harald Welte50f1c0a2016-04-25 19:01:26 +0200108 if (osmo_constant_time_cmp(vec.autn, rx_autn, sizeof(vec.autn)) != 0) {
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200109 LOGP(DGPRS, LOGL_ERROR, "OAP: AUTN mismatch!\n");
110 LOGP(DGPRS, LOGL_INFO, "OAP: AUTN from server: %s\n",
111 osmo_hexdump_nospc(rx_autn, sizeof(vec.autn)));
112 LOGP(DGPRS, LOGL_INFO, "OAP: AUTN expected: %s\n",
113 osmo_hexdump_nospc(vec.autn, sizeof(vec.autn)));
114 return -2;
115 }
116
117 if (tx_xres != NULL)
118 memcpy(tx_xres, vec.res, 8);
119 return 0;
120}
121
Harald Welte564c0652016-04-27 18:14:14 +0200122struct msgb *oap_encoded(const struct osmo_oap_message *oap_msg)
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200123{
124 struct msgb *msg = msgb_alloc_headroom(1000, 64, __func__);
125 OSMO_ASSERT(msg);
Harald Welte564c0652016-04-27 18:14:14 +0200126 osmo_oap_encode(msg, oap_msg);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200127 return msg;
128}
129
130/* Create a new msgb containing an OAP registration message.
131 * On error, return NULL. */
132static struct msgb* oap_msg_register(uint16_t client_id)
133{
Harald Welted8aa4122016-04-27 18:17:26 +0200134 struct osmo_oap_message oap_msg = {0};
135
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200136 if (client_id < 1) {
137 LOGP(DGPRS, LOGL_ERROR, "OAP: Invalid client ID: %d\n", client_id);
138 return NULL;
139 }
140
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200141 oap_msg.message_type = OAP_MSGT_REGISTER_REQUEST;
142 oap_msg.client_id = client_id;
143 return oap_encoded(&oap_msg);
144}
145
146int oap_register(struct oap_state *state, struct msgb **msg_tx)
147{
148 *msg_tx = oap_msg_register(state->client_id);
149 if (!(*msg_tx))
150 return -1;
151
152 state->state = OAP_REQUESTED_CHALLENGE;
153 return 0;
154}
155
156/* Create a new msgb containing an OAP challenge response message.
157 * xres must point at 8 octets to return as challenge response.
158 * On error, return NULL. */
159static struct msgb* oap_msg_challenge_response(uint8_t *xres)
160{
Harald Welte564c0652016-04-27 18:14:14 +0200161 struct osmo_oap_message oap_reply = {0};
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200162
163 oap_reply.message_type = OAP_MSGT_CHALLENGE_RESULT;
164 memcpy(oap_reply.xres, xres, sizeof(oap_reply.xres));
165 oap_reply.xres_present = 1;
166 return oap_encoded(&oap_reply);
167}
168
169static int handle_challenge(struct oap_state *state,
Harald Welte564c0652016-04-27 18:14:14 +0200170 struct osmo_oap_message *oap_rx,
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200171 struct msgb **msg_tx)
172{
173 int rc;
Harald Welted8aa4122016-04-27 18:17:26 +0200174 uint8_t xres[8];
175
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200176 if (!(oap_rx->rand_present && oap_rx->autn_present)) {
177 LOGP(DGPRS, LOGL_ERROR,
178 "OAP challenge incomplete (rand_present: %d, autn_present: %d)\n",
179 oap_rx->rand_present, oap_rx->autn_present);
180 rc = -2;
181 goto failure;
182 }
183
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200184 rc = oap_evaluate_challenge(state,
185 oap_rx->rand,
186 oap_rx->autn,
187 xres);
188 if (rc < 0)
189 goto failure;
190
191 *msg_tx = oap_msg_challenge_response(xres);
192 if ((*msg_tx) == NULL) {
193 rc = -1;
194 goto failure;
195 }
196
197 state->state = OAP_SENT_CHALLENGE_RESULT;
198 return 0;
199
200failure:
201 OSMO_ASSERT(rc < 0);
202 state->state = OAP_INITIALIZED;
203 return rc;
204}
205
206int oap_handle(struct oap_state *state, const struct msgb *msg_rx, struct msgb **msg_tx)
207{
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200208 uint8_t *data = msgb_l2(msg_rx);
209 size_t data_len = msgb_l2len(msg_rx);
Harald Welted8aa4122016-04-27 18:17:26 +0200210 struct osmo_oap_message oap_msg = {0};
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200211 int rc = 0;
212
Harald Welted8aa4122016-04-27 18:17:26 +0200213 *msg_tx = NULL;
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200214
215 OSMO_ASSERT(data);
216
Harald Welte5d547a42016-04-27 18:21:16 +0200217 rc = osmo_oap_decode(&oap_msg, data, data_len);
Neels Hofmeyrd739f092015-10-12 11:57:34 +0200218 if (rc < 0) {
219 LOGP(DGPRS, LOGL_ERROR,
220 "Decoding OAP message failed with error '%s' (%d)\n",
221 get_value_string(gsm48_gmm_cause_names, -rc), -rc);
222 return -10;
223 }
224
225 switch (oap_msg.message_type) {
226 case OAP_MSGT_CHALLENGE_REQUEST:
227 return handle_challenge(state, &oap_msg, msg_tx);
228
229 case OAP_MSGT_REGISTER_RESULT:
230 /* successfully registered */
231 state->state = OAP_REGISTERED;
232 break;
233
234 case OAP_MSGT_REGISTER_ERROR:
235 LOGP(DGPRS, LOGL_ERROR,
236 "OAP registration failed\n");
237 state->state = OAP_INITIALIZED;
238 if (state->registration_failures < 3) {
239 state->registration_failures ++;
240 return oap_register(state, msg_tx);
241 }
242 return -11;
243
244 case OAP_MSGT_REGISTER_REQUEST:
245 case OAP_MSGT_CHALLENGE_RESULT:
246 LOGP(DGPRS, LOGL_ERROR,
247 "Received invalid OAP message type for OAP client side: %d\n",
248 (int)oap_msg.message_type);
249 return -12;
250
251 default:
252 LOGP(DGPRS, LOGL_ERROR,
253 "Unknown OAP message type: %d\n",
254 (int)oap_msg.message_type);
255 return -13;
256 }
257
258 return 0;
259}