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