Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 1 | /* GSM/GPRS/3G authentication core infrastructure */ |
| 2 | |
| 3 | /* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 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 | |
| 23 | #include <errno.h> |
| 24 | #include <stdint.h> |
Harald Welte | d318e61 | 2011-12-07 12:16:27 +0100 | [diff] [blame] | 25 | #include <string.h> |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 26 | |
| 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 | |
| 33 | static LLIST_HEAD(osmo_auths); |
| 34 | |
| 35 | static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM]; |
| 36 | |
| 37 | /* register a cipher with the core */ |
| 38 | int osmo_auth_register(struct osmo_auth_impl *impl) |
| 39 | { |
| 40 | if (impl->algo >= ARRAY_SIZE(selected_auths)) |
| 41 | return -ERANGE; |
| 42 | |
| 43 | llist_add_tail(&impl->list, &osmo_auths); |
| 44 | |
| 45 | /* check if we want to select this implementation over others */ |
| 46 | if (!selected_auths[impl->algo] || |
| 47 | (selected_auths[impl->algo]->priority > impl->priority)) |
| 48 | selected_auths[impl->algo] = impl; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* load all available GPRS cipher plugins */ |
| 54 | int osmo_auth_load(const char *path) |
| 55 | { |
| 56 | /* load all plugins available from path */ |
| 57 | return osmo_plugin_load_all(path); |
| 58 | } |
| 59 | |
| 60 | int osmo_auth_supported(enum osmo_auth_algo algo) |
| 61 | { |
| 62 | if (algo >= ARRAY_SIZE(selected_auths)) |
| 63 | return -ERANGE; |
| 64 | |
| 65 | if (selected_auths[algo]) |
| 66 | return 1; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int osmo_auth_gen_vec(struct osmo_auth_vector *vec, |
| 72 | struct osmo_sub_auth_data *aud, |
| 73 | const uint8_t *_rand) |
| 74 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 75 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Harald Welte | 4afdd5d | 2011-12-07 02:38:18 +0100 | [diff] [blame] | 76 | int rc; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 77 | |
| 78 | if (!impl) |
| 79 | return -ENOENT; |
| 80 | |
Harald Welte | 4afdd5d | 2011-12-07 02:38:18 +0100 | [diff] [blame] | 81 | rc = impl->gen_vec(vec, aud, _rand); |
| 82 | if (rc < 0) |
| 83 | return rc; |
| 84 | |
| 85 | memcpy(vec->rand, _rand, sizeof(vec->rand)); |
| 86 | |
| 87 | return 0; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec, |
| 91 | struct osmo_sub_auth_data *aud, |
| 92 | const uint8_t *rand_auts, const uint8_t *auts, |
| 93 | const uint8_t *_rand) |
| 94 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 95 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 96 | |
| 97 | if (!impl || !impl->gen_vec_auts) |
| 98 | return -ENOENT; |
| 99 | |
| 100 | return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand); |
| 101 | } |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 102 | |
Harald Welte | 1c72bfb | 2012-04-04 22:05:24 +0200 | [diff] [blame] | 103 | static const struct value_string auth_alg_vals[] = { |
Harald Welte | a5ab162 | 2011-12-07 02:33:11 +0100 | [diff] [blame] | 104 | { OSMO_AUTH_ALG_NONE, "None" }, |
| 105 | { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" }, |
| 106 | { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" }, |
| 107 | { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" }, |
| 108 | { OSMO_AUTH_ALG_XOR, "XOR" }, |
| 109 | { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" }, |
| 110 | { 0, NULL } |
| 111 | }; |
| 112 | |
| 113 | const char *osmo_auth_alg_name(enum osmo_auth_algo alg) |
| 114 | { |
| 115 | return get_value_string(auth_alg_vals, alg); |
| 116 | } |
| 117 | |
| 118 | enum osmo_auth_algo osmo_auth_alg_parse(const char *name) |
| 119 | { |
| 120 | return get_string_value(auth_alg_vals, name); |
| 121 | } |