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