blob: 9e3b2c0114dbd7ee965121e666d42aad790ef58d [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 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welted82e0eb2011-12-06 21:53:42 +01007 * 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
23#include <errno.h>
24#include <stdint.h>
Harald Welted318e612011-12-07 12:16:27 +010025#include <string.h>
Harald Welted82e0eb2011-12-06 21:53:42 +010026
27#include <osmocom/core/utils.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/plugin.h>
30
31#include <osmocom/crypt/auth.h>
32
Harald Welte007a71e2012-07-18 19:47:56 +020033/*! \addtogroup auth
34 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020035 * GSM/GPRS/3G authentication core infrastructure
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020036 *
37 * \file auth_core.c */
Harald Welte007a71e2012-07-18 19:47:56 +020038
Harald Welted82e0eb2011-12-06 21:53:42 +010039static LLIST_HEAD(osmo_auths);
40
41static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM];
42
Neels Hofmeyr87e45502017-06-20 00:17:59 +020043/*! Register an authentication algorithm implementation with the core
Harald Welte007a71e2012-07-18 19:47:56 +020044 * \param[in] impl Structure describing implementation and it's callbacks
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020045 * \returns 0 on success, or a negative error code on failure
Harald Welte007a71e2012-07-18 19:47:56 +020046 *
47 * This function is called by an authentication implementation plugin to
48 * register itself with the authentication core.
49 */
Harald Welted82e0eb2011-12-06 21:53:42 +010050int osmo_auth_register(struct osmo_auth_impl *impl)
51{
52 if (impl->algo >= ARRAY_SIZE(selected_auths))
53 return -ERANGE;
54
55 llist_add_tail(&impl->list, &osmo_auths);
56
57 /* check if we want to select this implementation over others */
58 if (!selected_auths[impl->algo] ||
59 (selected_auths[impl->algo]->priority > impl->priority))
60 selected_auths[impl->algo] = impl;
61
62 return 0;
63}
64
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065/*! Load all available authentication plugins from the given path
Harald Welte007a71e2012-07-18 19:47:56 +020066 * \param[in] path Path name of the directory containing the plugins
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020067 * \returns number of plugins loaded in case of success, negative in case of error
Harald Welte007a71e2012-07-18 19:47:56 +020068 *
69 * This function will load all plugins contained in the specified path.
70 */
Harald Welted82e0eb2011-12-06 21:53:42 +010071int osmo_auth_load(const char *path)
72{
73 /* load all plugins available from path */
74 return osmo_plugin_load_all(path);
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! Determine if a given authentication algorithm is supported
Harald Welte007a71e2012-07-18 19:47:56 +020078 * \param[in] algo Algorithm which should be checked
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020079 * \returns 1 if algo is supported, 0 if not, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +020080 *
81 * This function is used by an application to determine at runtime if a
82 * given authentication algorithm is supported or not.
83 */
Harald Welted82e0eb2011-12-06 21:53:42 +010084int osmo_auth_supported(enum osmo_auth_algo algo)
85{
86 if (algo >= ARRAY_SIZE(selected_auths))
87 return -ERANGE;
88
89 if (selected_auths[algo])
90 return 1;
91
92 return 0;
93}
94
Harald Weltecd9cb902016-04-20 10:39:00 +020095/* C5 function to derive UMTS IK from GSM Kc */
96static inline void c5_function(uint8_t *ik, const uint8_t *kc)
97{
98 unsigned int i;
99
100 for (i = 0; i < 4; i++)
101 ik[i] = kc[i] ^ kc[i+4];
102 memcpy(ik+4, kc, 8);
103 for (i = 12; i < 16; i++)
104 ik[i] = ik[i-12];
105}
106
107/* C4 function to derive UMTS CK from GSM Kc */
Maxceae1232016-06-27 18:12:49 +0200108void osmo_c4(uint8_t *ck, const uint8_t *kc)
Harald Weltecd9cb902016-04-20 10:39:00 +0200109{
110 memcpy(ck, kc, 8);
111 memcpy(ck+8, kc, 8);
112}
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114/*! Generate 3G CK + IK from 2G authentication vector
Harald Weltecd9cb902016-04-20 10:39:00 +0200115 * \param vec Authentication Vector to be modified
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200116 * \returns 1 if the vector was changed, 0 otherwise
Harald Weltecd9cb902016-04-20 10:39:00 +0200117 *
118 * This function performs the C5 and C4 functions to derive the UMTS key
119 * material from the GSM key material in the supplied vector, _if_ the input
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200120 * vector doesn't yet have UMTS authentication capability.
121 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200122int osmo_auth_3g_from_2g(struct osmo_auth_vector *vec)
123{
124 if ((vec->auth_types & OSMO_AUTH_TYPE_GSM) &&
125 !(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) {
126 c5_function(vec->ik, vec->kc);
Maxceae1232016-06-27 18:12:49 +0200127 osmo_c4(vec->ck, vec->kc);
Harald Weltecd9cb902016-04-20 10:39:00 +0200128 /* We cannot actually set OSMO_AUTH_TYPE_UMTS as we have no
129 * AUTN and no RES, and thus can only perform GSM
130 * authentication with this tuple.
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200131 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200132 return 1;
133 }
134
135 return 0;
136}
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138/*! Generate authentication vector
Harald Welte007a71e2012-07-18 19:47:56 +0200139 * \param[out] vec Generated authentication vector
140 * \param[in] aud Subscriber-specific key material
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100141 * \param[in] _rand Random challenge to be used
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200142 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200143 *
144 * This function performs the core cryptographic function of the AUC,
145 * computing authentication triples/quintuples based on the permanent
146 * subscriber data and a random value. The result is what is forwarded
147 * by the AUC via HLR and VLR to the MSC which will then be able to
148 * invoke authentication with the MS
149 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100150int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
151 struct osmo_sub_auth_data *aud,
152 const uint8_t *_rand)
153{
Harald Welte781bd5d2011-12-06 22:23:52 +0100154 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +0100155 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100156
157 if (!impl)
158 return -ENOENT;
159
Harald Welte4afdd5d2011-12-07 02:38:18 +0100160 rc = impl->gen_vec(vec, aud, _rand);
161 if (rc < 0)
162 return rc;
163
164 memcpy(vec->rand, _rand, sizeof(vec->rand));
165
166 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100167}
168
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200169/*! Generate authentication vector and re-sync sequence
Harald Welte007a71e2012-07-18 19:47:56 +0200170 * \param[out] vec Generated authentication vector
171 * \param[in] aud Subscriber-specific key material
Harald Welte007a71e2012-07-18 19:47:56 +0200172 * \param[in] auts AUTS value sent by the SIM/MS
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100173 * \param[in] rand_auts RAND value sent by the SIM/MS
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100174 * \param[in] _rand Random challenge to be used to generate vector
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200175 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200176 *
177 * This function performs a special variant of the core cryptographic
178 * function of the AUC: computing authentication triples/quintuples
179 * based on the permanent subscriber data, a random value as well as the
180 * AUTS and RAND values returned by the SIM/MS. This special variant is
181 * needed if the sequence numbers between MS and AUC have for some
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100182 * reason become different.
Harald Welte007a71e2012-07-18 19:47:56 +0200183 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100184int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
185 struct osmo_sub_auth_data *aud,
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100186 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welted82e0eb2011-12-06 21:53:42 +0100187 const uint8_t *_rand)
188{
Harald Welte781bd5d2011-12-06 22:23:52 +0100189 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100190 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100191
192 if (!impl || !impl->gen_vec_auts)
193 return -ENOENT;
194
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100195 rc = impl->gen_vec_auts(vec, aud, auts, rand_auts, _rand);
196 if (rc < 0)
197 return rc;
198
199 memcpy(vec->rand, _rand, sizeof(vec->rand));
200
201 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100202}
Harald Weltea5ab1622011-12-07 02:33:11 +0100203
Harald Welte1c72bfb2012-04-04 22:05:24 +0200204static const struct value_string auth_alg_vals[] = {
Harald Weltea5ab1622011-12-07 02:33:11 +0100205 { OSMO_AUTH_ALG_NONE, "None" },
206 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
207 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
208 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
209 { OSMO_AUTH_ALG_XOR, "XOR" },
210 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
211 { 0, NULL }
212};
213
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200214/*! Get human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100215const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
216{
217 return get_value_string(auth_alg_vals, alg);
218}
219
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200220/*! Parse human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100221enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
222{
223 return get_string_value(auth_alg_vals, name);
224}
Harald Welte007a71e2012-07-18 19:47:56 +0200225
Neels Hofmeyr26e30b12017-10-07 04:41:22 +0200226const struct value_string osmo_sub_auth_type_names[] = {
227 { OSMO_AUTH_TYPE_NONE, "None" },
228 { OSMO_AUTH_TYPE_GSM, "GSM" },
229 { OSMO_AUTH_TYPE_UMTS, "UMTS" },
230 { 0, NULL }
231};
232
Harald Welte007a71e2012-07-18 19:47:56 +0200233/*! @} */