simauth.c (4572B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2017-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library 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 GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #include <ctype.h> 28 #include <stdio.h> 29 #include <errno.h> 30 #include <ell/ell.h> 31 32 #include "src/iwd.h" 33 #include "src/module.h" 34 #include "src/watchlist.h" 35 #include "src/simauth.h" 36 37 static struct l_queue *auth_providers; 38 39 struct iwd_sim_auth { 40 const struct iwd_sim_auth_driver *driver; 41 void *driver_data; 42 bool aka_supported : 1; 43 bool sim_supported : 1; 44 char *nai; 45 int pending; 46 struct watchlist auth_watchers; 47 }; 48 49 struct iwd_sim_auth *iwd_sim_auth_create( 50 const struct iwd_sim_auth_driver *driver) 51 { 52 struct iwd_sim_auth *auth = l_new(struct iwd_sim_auth, 1); 53 54 auth->driver = driver; 55 watchlist_init(&auth->auth_watchers, NULL); 56 57 return auth; 58 } 59 60 void iwd_sim_auth_set_nai(struct iwd_sim_auth *auth, const char *nai) 61 { 62 auth->nai = l_strdup(nai); 63 } 64 65 void iwd_sim_auth_set_capabilities(struct iwd_sim_auth *auth, 66 bool sim_supported, bool aka_supported) 67 { 68 auth->sim_supported = sim_supported; 69 auth->aka_supported = aka_supported; 70 } 71 72 void iwd_sim_auth_set_data(struct iwd_sim_auth *auth, void *driver_data) 73 { 74 auth->driver_data = driver_data; 75 } 76 77 bool iwd_sim_auth_register(struct iwd_sim_auth *auth) 78 { 79 return l_queue_push_head(auth_providers, auth); 80 } 81 82 void *iwd_sim_auth_get_data(struct iwd_sim_auth *auth) 83 { 84 return auth->driver_data; 85 } 86 87 static void destroy_provider(void *data) 88 { 89 struct iwd_sim_auth *auth = data; 90 91 if (auth->driver->remove) 92 auth->driver->remove(auth); 93 94 watchlist_destroy(&auth->auth_watchers); 95 96 l_free(auth->nai); 97 l_free(auth); 98 } 99 100 void iwd_sim_auth_remove(struct iwd_sim_auth *auth) 101 { 102 if (auth->driver && auth->driver->cancel_request) 103 auth->driver->cancel_request(auth, auth->pending); 104 105 WATCHLIST_NOTIFY_NO_ARGS(&auth->auth_watchers, 106 sim_auth_unregistered_cb_t); 107 108 l_queue_remove(auth_providers, auth); 109 110 destroy_provider(auth); 111 } 112 113 const char *iwd_sim_auth_get_nai(struct iwd_sim_auth *auth) 114 { 115 return auth->nai; 116 } 117 118 struct iwd_sim_auth *iwd_sim_auth_find(bool sim, bool aka) 119 { 120 struct iwd_sim_auth *auth; 121 const struct l_queue_entry *entry; 122 123 for (entry = l_queue_get_entries(auth_providers); entry; 124 entry = entry->next) { 125 auth = entry->data; 126 127 if (sim && !auth->sim_supported) 128 continue; 129 130 if (aka && !auth->aka_supported) 131 continue; 132 133 return auth; 134 } 135 136 return NULL; 137 } 138 139 unsigned int sim_auth_unregistered_watch_add(struct iwd_sim_auth *auth, 140 sim_auth_unregistered_cb_t cb, void *data) 141 { 142 return watchlist_add(&auth->auth_watchers, cb, data, NULL); 143 } 144 145 void sim_auth_unregistered_watch_remove(struct iwd_sim_auth *auth, 146 unsigned int id) 147 { 148 watchlist_remove(&auth->auth_watchers, id); 149 } 150 151 int sim_auth_check_milenage(struct iwd_sim_auth *auth, 152 const uint8_t *rand, const uint8_t *autn, 153 sim_auth_check_milenage_cb_t cb, void *data) 154 { 155 if (!auth->aka_supported) 156 return -1; 157 158 /* save ID in case simauth is destroyed */ 159 auth->pending = auth->driver->check_milenage(auth, rand, autn, 160 cb, data); 161 162 return auth->pending; 163 } 164 165 int sim_auth_run_gsm(struct iwd_sim_auth *auth, const uint8_t *rands, 166 int num_rands, sim_auth_run_gsm_cb_t cb, void *data) 167 { 168 if (!auth->sim_supported) 169 return -1; 170 171 /* save ID in case simauth is destroyed */ 172 auth->pending = auth->driver->run_gsm(auth, rands, num_rands, cb, data); 173 174 return auth->pending; 175 } 176 177 void sim_auth_cancel_request(struct iwd_sim_auth *auth, int id) 178 { 179 if (auth->driver->cancel_request) 180 auth->driver->cancel_request(auth, id); 181 } 182 183 static int sim_auth_init(void) 184 { 185 auth_providers = l_queue_new(); 186 return 0; 187 } 188 189 static void sim_auth_exit(void) 190 { 191 if (l_queue_length(auth_providers) > 0) 192 l_warn("Auth provider queue was not empty on exit!"); 193 194 l_queue_destroy(auth_providers, destroy_provider); 195 } 196 197 IWD_MODULE(simauth, sim_auth_init, sim_auth_exit)