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