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> |
| 25 | |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/linuxlist.h> |
| 28 | #include <osmocom/core/plugin.h> |
| 29 | |
| 30 | #include <osmocom/crypt/auth.h> |
| 31 | |
| 32 | static LLIST_HEAD(osmo_auths); |
| 33 | |
| 34 | static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM]; |
| 35 | |
| 36 | /* register a cipher with the core */ |
| 37 | int osmo_auth_register(struct osmo_auth_impl *impl) |
| 38 | { |
| 39 | if (impl->algo >= ARRAY_SIZE(selected_auths)) |
| 40 | return -ERANGE; |
| 41 | |
| 42 | llist_add_tail(&impl->list, &osmo_auths); |
| 43 | |
| 44 | /* check if we want to select this implementation over others */ |
| 45 | if (!selected_auths[impl->algo] || |
| 46 | (selected_auths[impl->algo]->priority > impl->priority)) |
| 47 | selected_auths[impl->algo] = impl; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* load all available GPRS cipher plugins */ |
| 53 | int osmo_auth_load(const char *path) |
| 54 | { |
| 55 | /* load all plugins available from path */ |
| 56 | return osmo_plugin_load_all(path); |
| 57 | } |
| 58 | |
| 59 | int osmo_auth_supported(enum osmo_auth_algo algo) |
| 60 | { |
| 61 | if (algo >= ARRAY_SIZE(selected_auths)) |
| 62 | return -ERANGE; |
| 63 | |
| 64 | if (selected_auths[algo]) |
| 65 | return 1; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | int osmo_auth_gen_vec(struct osmo_auth_vector *vec, |
| 71 | struct osmo_sub_auth_data *aud, |
| 72 | const uint8_t *_rand) |
| 73 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame^] | 74 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 75 | |
| 76 | if (!impl) |
| 77 | return -ENOENT; |
| 78 | |
| 79 | return impl->gen_vec(vec, aud, _rand); |
| 80 | } |
| 81 | |
| 82 | int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec, |
| 83 | struct osmo_sub_auth_data *aud, |
| 84 | const uint8_t *rand_auts, const uint8_t *auts, |
| 85 | const uint8_t *_rand) |
| 86 | { |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame^] | 87 | struct osmo_auth_impl *impl = selected_auths[aud->algo]; |
Harald Welte | d82e0eb | 2011-12-06 21:53:42 +0100 | [diff] [blame] | 88 | |
| 89 | if (!impl || !impl->gen_vec_auts) |
| 90 | return -ENOENT; |
| 91 | |
| 92 | return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand); |
| 93 | } |