blob: 415669599abc78b0955b403940964183df08338a [file] [log] [blame]
Harald Welte007a71e2012-07-18 19:47:56 +02001/* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welted82e0eb2011-12-06 21:53:42 +01002 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <errno.h>
22#include <stdint.h>
Harald Welted318e612011-12-07 12:16:27 +010023#include <string.h>
Harald Welted82e0eb2011-12-06 21:53:42 +010024
25#include <osmocom/core/utils.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/plugin.h>
28
29#include <osmocom/crypt/auth.h>
30
Harald Welte007a71e2012-07-18 19:47:56 +020031/*! \addtogroup auth
32 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033 * GSM/GPRS/3G authentication core infrastructure
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020034 *
35 * \file auth_core.c */
Harald Welte007a71e2012-07-18 19:47:56 +020036
Harald Welted82e0eb2011-12-06 21:53:42 +010037static LLIST_HEAD(osmo_auths);
38
39static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM];
40
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041/*! Register an authentication algorithm implementation with the core
Harald Welte007a71e2012-07-18 19:47:56 +020042 * \param[in] impl Structure describing implementation and it's callbacks
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020043 * \returns 0 on success, or a negative error code on failure
Harald Welte007a71e2012-07-18 19:47:56 +020044 *
45 * This function is called by an authentication implementation plugin to
46 * register itself with the authentication core.
47 */
Harald Welted82e0eb2011-12-06 21:53:42 +010048int osmo_auth_register(struct osmo_auth_impl *impl)
49{
50 if (impl->algo >= ARRAY_SIZE(selected_auths))
51 return -ERANGE;
52
53 llist_add_tail(&impl->list, &osmo_auths);
54
55 /* check if we want to select this implementation over others */
56 if (!selected_auths[impl->algo] ||
57 (selected_auths[impl->algo]->priority > impl->priority))
58 selected_auths[impl->algo] = impl;
59
60 return 0;
61}
62
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! Load all available authentication plugins from the given path
Harald Welte007a71e2012-07-18 19:47:56 +020064 * \param[in] path Path name of the directory containing the plugins
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020065 * \returns number of plugins loaded in case of success, negative in case of error
Harald Welte007a71e2012-07-18 19:47:56 +020066 *
67 * This function will load all plugins contained in the specified path.
68 */
Harald Welted82e0eb2011-12-06 21:53:42 +010069int osmo_auth_load(const char *path)
70{
71 /* load all plugins available from path */
72 return osmo_plugin_load_all(path);
73}
74
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075/*! Determine if a given authentication algorithm is supported
Harald Welte007a71e2012-07-18 19:47:56 +020076 * \param[in] algo Algorithm which should be checked
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020077 * \returns 1 if algo is supported, 0 if not, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +020078 *
79 * This function is used by an application to determine at runtime if a
80 * given authentication algorithm is supported or not.
81 */
Harald Welted82e0eb2011-12-06 21:53:42 +010082int osmo_auth_supported(enum osmo_auth_algo algo)
83{
84 if (algo >= ARRAY_SIZE(selected_auths))
85 return -ERANGE;
86
87 if (selected_auths[algo])
88 return 1;
89
90 return 0;
91}
92
Harald Weltecd9cb902016-04-20 10:39:00 +020093/* C5 function to derive UMTS IK from GSM Kc */
94static inline void c5_function(uint8_t *ik, const uint8_t *kc)
95{
96 unsigned int i;
97
98 for (i = 0; i < 4; i++)
99 ik[i] = kc[i] ^ kc[i+4];
100 memcpy(ik+4, kc, 8);
101 for (i = 12; i < 16; i++)
102 ik[i] = ik[i-12];
103}
104
105/* C4 function to derive UMTS CK from GSM Kc */
Maxceae1232016-06-27 18:12:49 +0200106void osmo_c4(uint8_t *ck, const uint8_t *kc)
Harald Weltecd9cb902016-04-20 10:39:00 +0200107{
108 memcpy(ck, kc, 8);
109 memcpy(ck+8, kc, 8);
110}
111
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200112/*! Generate 3G CK + IK from 2G authentication vector
Harald Weltecd9cb902016-04-20 10:39:00 +0200113 * \param vec Authentication Vector to be modified
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200114 * \returns 1 if the vector was changed, 0 otherwise
Harald Weltecd9cb902016-04-20 10:39:00 +0200115 *
116 * This function performs the C5 and C4 functions to derive the UMTS key
117 * material from the GSM key material in the supplied vector, _if_ the input
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200118 * vector doesn't yet have UMTS authentication capability.
119 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200120int osmo_auth_3g_from_2g(struct osmo_auth_vector *vec)
121{
122 if ((vec->auth_types & OSMO_AUTH_TYPE_GSM) &&
123 !(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) {
124 c5_function(vec->ik, vec->kc);
Maxceae1232016-06-27 18:12:49 +0200125 osmo_c4(vec->ck, vec->kc);
Harald Weltecd9cb902016-04-20 10:39:00 +0200126 /* We cannot actually set OSMO_AUTH_TYPE_UMTS as we have no
127 * AUTN and no RES, and thus can only perform GSM
128 * authentication with this tuple.
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200129 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200130 return 1;
131 }
132
133 return 0;
134}
135
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136/*! Generate authentication vector
Harald Welte007a71e2012-07-18 19:47:56 +0200137 * \param[out] vec Generated authentication vector
138 * \param[in] aud Subscriber-specific key material
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100139 * \param[in] _rand Random challenge to be used
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200140 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200141 *
142 * This function performs the core cryptographic function of the AUC,
143 * computing authentication triples/quintuples based on the permanent
144 * subscriber data and a random value. The result is what is forwarded
145 * by the AUC via HLR and VLR to the MSC which will then be able to
146 * invoke authentication with the MS
147 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100148int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
149 struct osmo_sub_auth_data *aud,
150 const uint8_t *_rand)
151{
Harald Welte781bd5d2011-12-06 22:23:52 +0100152 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +0100153 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100154
155 if (!impl)
156 return -ENOENT;
157
Harald Welte4afdd5d2011-12-07 02:38:18 +0100158 rc = impl->gen_vec(vec, aud, _rand);
159 if (rc < 0)
160 return rc;
161
162 memcpy(vec->rand, _rand, sizeof(vec->rand));
163
164 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100165}
166
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167/*! Generate authentication vector and re-sync sequence
Harald Welte007a71e2012-07-18 19:47:56 +0200168 * \param[out] vec Generated authentication vector
169 * \param[in] aud Subscriber-specific key material
Harald Welte007a71e2012-07-18 19:47:56 +0200170 * \param[in] auts AUTS value sent by the SIM/MS
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100171 * \param[in] rand_auts RAND value sent by the SIM/MS
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100172 * \param[in] _rand Random challenge to be used to generate vector
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200173 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200174 *
175 * This function performs a special variant of the core cryptographic
176 * function of the AUC: computing authentication triples/quintuples
177 * based on the permanent subscriber data, a random value as well as the
178 * AUTS and RAND values returned by the SIM/MS. This special variant is
179 * needed if the sequence numbers between MS and AUC have for some
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100180 * reason become different.
Harald Welte007a71e2012-07-18 19:47:56 +0200181 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100182int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
183 struct osmo_sub_auth_data *aud,
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100184 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welted82e0eb2011-12-06 21:53:42 +0100185 const uint8_t *_rand)
186{
Harald Welte781bd5d2011-12-06 22:23:52 +0100187 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100188 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100189
190 if (!impl || !impl->gen_vec_auts)
191 return -ENOENT;
192
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100193 rc = impl->gen_vec_auts(vec, aud, auts, rand_auts, _rand);
194 if (rc < 0)
195 return rc;
196
197 memcpy(vec->rand, _rand, sizeof(vec->rand));
198
199 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100200}
Harald Weltea5ab1622011-12-07 02:33:11 +0100201
Harald Welte1c72bfb2012-04-04 22:05:24 +0200202static const struct value_string auth_alg_vals[] = {
Harald Weltea5ab1622011-12-07 02:33:11 +0100203 { OSMO_AUTH_ALG_NONE, "None" },
204 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
205 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
206 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
207 { OSMO_AUTH_ALG_XOR, "XOR" },
208 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
209 { 0, NULL }
210};
211
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200212/*! Get human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100213const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
214{
215 return get_value_string(auth_alg_vals, alg);
216}
217
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218/*! Parse human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100219enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
220{
221 return get_string_value(auth_alg_vals, name);
222}
Harald Welte007a71e2012-07-18 19:47:56 +0200223
224/*! @} */