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