Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 1 | /* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Max | 38b1723 | 2017-12-01 19:06:21 +0100 | [diff] [blame] | 23 | #include "config.h" |
| 24 | |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <stdint.h> |
Harald Welte | d318e61 | 2011-12-07 12:16:27 +0100 | [diff] [blame] | 27 | #include <string.h> |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 28 | |
| 29 | #include <osmocom/core/utils.h> |
| 30 | #include <osmocom/core/linuxlist.h> |
| 31 | #include <osmocom/core/plugin.h> |
| 32 | |
| 33 | #include <osmocom/crypt/auth.h> |
| 34 | |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 35 | /*! \addtogroup auth |
| 36 | * @{ |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | * GSM/GPRS/3G authentication core infrastructure |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 38 | * |
| 39 | * \file auth_core.c */ |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 40 | |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 41 | static LLIST_HEAD(osmo_auths); |
| 42 | |
| 43 | static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM]; |
| 44 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 45 | /*! Register an authentication algorithm implementation with the core |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 46 | * \param[in] impl Structure describing implementation and it's callbacks |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 47 | * \returns 0 on success, or a negative error code on failure |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 48 | * |
| 49 | * This function is called by an authentication implementation plugin to |
| 50 | * register itself with the authentication core. |
| 51 | */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 52 | int osmo_auth_register(struct osmo_auth_impl *impl) |
| 53 | { |
| 54 | if (impl->algo >= ARRAY_SIZE(selected_auths)) |
| 55 | return -ERANGE; |
| 56 | |
| 57 | llist_add_tail(&impl->list, &osmo_auths); |
| 58 | |
| 59 | /* check if we want to select this implementation over others */ |
| 60 | if (!selected_auths[impl->algo] || |
| 61 | (selected_auths[impl->algo]->priority > impl->priority)) |
| 62 | selected_auths[impl->algo] = impl; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 67 | /*! Load all available authentication plugins from the given path |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 68 | * \param[in] path Path name of the directory containing the plugins |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 69 | * \returns number of plugins loaded in case of success, negative in case of error |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 70 | * |
| 71 | * This function will load all plugins contained in the specified path. |
| 72 | */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 73 | int osmo_auth_load(const char *path) |
| 74 | { |
| 75 | /* load all plugins available from path */ |
Max | 38b1723 | 2017-12-01 19:06:21 +0100 | [diff] [blame] | 76 | #if !defined(EMBEDDED) |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 77 | return osmo_plugin_load_all(path); |
Max | 38b1723 | 2017-12-01 19:06:21 +0100 | [diff] [blame] | 78 | #else |
| 79 | return -1; |
| 80 | #endif |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 83 | /*! Determine if a given authentication algorithm is supported |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 84 | * \param[in] algo Algorithm which should be checked |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 85 | * \returns 1 if algo is supported, 0 if not, negative error on failure |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 86 | * |
| 87 | * This function is used by an application to determine at runtime if a |
| 88 | * given authentication algorithm is supported or not. |
| 89 | */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 90 | int osmo_auth_supported(enum osmo_auth_algo algo) |
| 91 | { |
| 92 | if (algo >= ARRAY_SIZE(selected_auths)) |
| 93 | return -ERANGE; |
| 94 | |
| 95 | if (selected_auths[algo]) |
| 96 | return 1; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Max | af25c37 | 2018-12-19 20:12:19 +0100 | [diff] [blame] | 101 | /* 3GPP TS 33.102 §6.8.2.3 C5 function to derive UMTS IK from GSM Kc */ |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 102 | static inline void c5_function(uint8_t *ik, const uint8_t *kc) |
| 103 | { |
| 104 | unsigned int i; |
| 105 | |
| 106 | for (i = 0; i < 4; i++) |
| 107 | ik[i] = kc[i] ^ kc[i+4]; |
| 108 | memcpy(ik+4, kc, 8); |
| 109 | for (i = 12; i < 16; i++) |
| 110 | ik[i] = ik[i-12]; |
| 111 | } |
| 112 | |
Max | af25c37 | 2018-12-19 20:12:19 +0100 | [diff] [blame] | 113 | /* 3GPP TS 33.102 §6.8.2.3 C4 function to derive UMTS CK from GSM Kc */ |
Max | ceae123 | 2016-06-27 18:12:49 +0200 | [diff] [blame] | 114 | void osmo_c4(uint8_t *ck, const uint8_t *kc) |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 115 | { |
| 116 | memcpy(ck, kc, 8); |
| 117 | memcpy(ck+8, kc, 8); |
| 118 | } |
| 119 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 120 | /*! Generate 3G CK + IK from 2G authentication vector |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 121 | * \param vec Authentication Vector to be modified |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 122 | * \returns 1 if the vector was changed, 0 otherwise |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 123 | * |
| 124 | * This function performs the C5 and C4 functions to derive the UMTS key |
| 125 | * material from the GSM key material in the supplied vector, _if_ the input |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 126 | * vector doesn't yet have UMTS authentication capability. |
| 127 | */ |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 128 | int osmo_auth_3g_from_2g(struct osmo_auth_vector *vec) |
| 129 | { |
| 130 | if ((vec->auth_types & OSMO_AUTH_TYPE_GSM) && |
| 131 | !(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) { |
| 132 | c5_function(vec->ik, vec->kc); |
Max | ceae123 | 2016-06-27 18:12:49 +0200 | [diff] [blame] | 133 | osmo_c4(vec->ck, vec->kc); |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 134 | /* We cannot actually set OSMO_AUTH_TYPE_UMTS as we have no |
| 135 | * AUTN and no RES, and thus can only perform GSM |
| 136 | * authentication with this tuple. |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 137 | */ |
Harald Welte | cd9cb90 | 2016-04-20 10:39:00 +0200 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 144 | /*! Generate authentication vector |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 145 | * \param[out] vec Generated authentication vector |
| 146 | * \param[in] aud Subscriber-specific key material |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 147 | * \param[in] _rand Random challenge to be used |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 148 | * \returns 0 on success, negative error on failure |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 149 | * |
| 150 | * This function performs the core cryptographic function of the AUC, |
| 151 | * computing authentication triples/quintuples based on the permanent |
| 152 | * subscriber data and a random value. The result is what is forwarded |
| 153 | * by the AUC via HLR and VLR to the MSC which will then be able to |
| 154 | * invoke authentication with the MS |
| 155 | */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 156 | int osmo_auth_gen_vec(struct osmo_auth_vector *vec, |
| 157 | struct osmo_sub_auth_data *aud, |
| 158 | const uint8_t *_rand) |
| 159 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 160 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Harald Welte | 4afdd5d | 2011-12-07 02:38:18 +0100 | [diff] [blame] | 161 | int rc; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 162 | |
| 163 | if (!impl) |
| 164 | return -ENOENT; |
| 165 | |
Harald Welte | 4afdd5d | 2011-12-07 02:38:18 +0100 | [diff] [blame] | 166 | rc = impl->gen_vec(vec, aud, _rand); |
| 167 | if (rc < 0) |
| 168 | return rc; |
| 169 | |
| 170 | memcpy(vec->rand, _rand, sizeof(vec->rand)); |
| 171 | |
| 172 | return 0; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 175 | /*! Generate authentication vector and re-sync sequence |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 176 | * \param[out] vec Generated authentication vector |
| 177 | * \param[in] aud Subscriber-specific key material |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 178 | * \param[in] auts AUTS value sent by the SIM/MS |
Neels Hofmeyr | 03ab9a6 | 2017-02-03 05:00:24 +0100 | [diff] [blame] | 179 | * \param[in] rand_auts RAND value sent by the SIM/MS |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 180 | * \param[in] _rand Random challenge to be used to generate vector |
Neels Hofmeyr | c7bf8d0 | 2016-05-08 22:23:40 +0200 | [diff] [blame] | 181 | * \returns 0 on success, negative error on failure |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 182 | * |
| 183 | * This function performs a special variant of the core cryptographic |
| 184 | * function of the AUC: computing authentication triples/quintuples |
| 185 | * based on the permanent subscriber data, a random value as well as the |
| 186 | * AUTS and RAND values returned by the SIM/MS. This special variant is |
| 187 | * needed if the sequence numbers between MS and AUC have for some |
Thorsten Alteholz | a81055d | 2017-03-02 22:13:48 +0100 | [diff] [blame] | 188 | * reason become different. |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 189 | */ |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 190 | int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec, |
| 191 | struct osmo_sub_auth_data *aud, |
Neels Hofmeyr | 03ab9a6 | 2017-02-03 05:00:24 +0100 | [diff] [blame] | 192 | const uint8_t *auts, const uint8_t *rand_auts, |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 193 | const uint8_t *_rand) |
| 194 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 195 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Neels Hofmeyr | 3b8cb39 | 2017-02-21 19:54:36 +0100 | [diff] [blame] | 196 | int rc; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 197 | |
| 198 | if (!impl || !impl->gen_vec_auts) |
| 199 | return -ENOENT; |
| 200 | |
Neels Hofmeyr | 3b8cb39 | 2017-02-21 19:54:36 +0100 | [diff] [blame] | 201 | rc = impl->gen_vec_auts(vec, aud, auts, rand_auts, _rand); |
| 202 | if (rc < 0) |
| 203 | return rc; |
| 204 | |
| 205 | memcpy(vec->rand, _rand, sizeof(vec->rand)); |
| 206 | |
| 207 | return 0; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 208 | } |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 209 | |
Harald Welte | 1c72bfb | 2012-04-04 22:05:24 +0200 | [diff] [blame] | 210 | static const struct value_string auth_alg_vals[] = { |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 211 | { OSMO_AUTH_ALG_NONE, "None" }, |
| 212 | { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" }, |
| 213 | { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" }, |
| 214 | { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" }, |
| 215 | { OSMO_AUTH_ALG_XOR, "XOR" }, |
| 216 | { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" }, |
| 217 | { 0, NULL } |
| 218 | }; |
| 219 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 220 | /*! Get human-readable name of authentication algorithm */ |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 221 | const char *osmo_auth_alg_name(enum osmo_auth_algo alg) |
| 222 | { |
| 223 | return get_value_string(auth_alg_vals, alg); |
| 224 | } |
| 225 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 226 | /*! Parse human-readable name of authentication algorithm */ |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 227 | enum osmo_auth_algo osmo_auth_alg_parse(const char *name) |
| 228 | { |
| 229 | return get_string_value(auth_alg_vals, name); |
| 230 | } |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 231 | |
Neels Hofmeyr | 26e30b1 | 2017-10-07 04:41:22 +0200 | [diff] [blame] | 232 | const struct value_string osmo_sub_auth_type_names[] = { |
| 233 | { OSMO_AUTH_TYPE_NONE, "None" }, |
| 234 | { OSMO_AUTH_TYPE_GSM, "GSM" }, |
| 235 | { OSMO_AUTH_TYPE_UMTS, "UMTS" }, |
| 236 | { 0, NULL } |
| 237 | }; |
| 238 | |
Neels Hofmeyr | aa84b71 | 2017-12-18 03:12:01 +0100 | [diff] [blame] | 239 | /* Derive GSM AKA ciphering key Kc from UMTS AKA CK and IK (auth function c3 from 3GPP TS 33.103 § |
| 240 | * 4.6.1). |
| 241 | * \param[out] kc GSM AKA Kc, 8 byte target buffer. |
| 242 | * \param[in] ck UMTS AKA CK, 16 byte input buffer. |
| 243 | * \param[in] ik UMTS AKA IK, 16 byte input buffer. |
| 244 | */ |
| 245 | void osmo_auth_c3(uint8_t kc[], const uint8_t ck[], const uint8_t ik[]) |
| 246 | { |
| 247 | int i; |
| 248 | for (i = 0; i < 8; i++) |
| 249 | kc[i] = ck[i] ^ ck[i + 8] ^ ik[i] ^ ik[i + 8]; |
| 250 | } |
| 251 | |
Harald Welte | 007a71e | 2012-07-18 19:47:56 +0200 | [diff] [blame] | 252 | /*! @} */ |