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