eap-md5.c (4642B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2016-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 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <errno.h> 30 #include <string.h> 31 #include <ell/ell.h> 32 33 #include "src/missing.h" 34 #include "src/eap.h" 35 #include "src/eap-private.h" 36 37 struct eap_md5_state { 38 char *secret; 39 }; 40 41 static void eap_md5_free(struct eap_state *eap) 42 { 43 struct eap_md5_state *md5 = eap_get_data(eap); 44 45 eap_set_data(eap, NULL); 46 47 if (md5->secret) 48 explicit_bzero(md5->secret, strlen(md5->secret)); 49 50 l_free(md5->secret); 51 l_free(md5); 52 } 53 54 static void eap_md5_handle_request(struct eap_state *eap, 55 const uint8_t *pkt, size_t len) 56 { 57 struct eap_md5_state *md5 = eap_get_data(eap); 58 const uint8_t *value; 59 struct l_checksum *hash; 60 uint8_t identifier, response[5 + 1 + 16]; 61 62 if (len < 1 || len < (size_t) pkt[0] + 1 || pkt[0] < 1) { 63 l_error("EAP-MD5 request too short"); 64 goto err; 65 } 66 67 value = pkt + 1; 68 69 hash = l_checksum_new(L_CHECKSUM_MD5); 70 if (!hash) { 71 l_error("Can't create the MD5 checksum"); 72 goto err; 73 } 74 75 eap_save_last_id(eap, &identifier); 76 l_checksum_update(hash, &identifier, 1); 77 l_checksum_update(hash, md5->secret, strlen(md5->secret)); 78 l_checksum_update(hash, value, pkt[0]); 79 80 response[5] = 16; 81 l_checksum_get_digest(hash, response + 6, 16); 82 l_checksum_free(hash); 83 84 eap_method_respond(eap, response, sizeof(response)); 85 86 /* We have no choice but to call it a success */ 87 eap_method_success(eap); 88 89 return; 90 91 err: 92 eap_method_error(eap); 93 } 94 95 static int eap_md5_check_settings(struct l_settings *settings, 96 struct l_queue *secrets, 97 const char *prefix, 98 struct l_queue **out_missing) 99 { 100 const struct eap_secret_info *secret; 101 char identity_key[72]; 102 char password_key[72]; 103 104 L_AUTO_FREE_VAR(char *, identity); 105 L_AUTO_FREE_VAR(char *, password) = NULL; 106 107 snprintf(identity_key, sizeof(identity_key), "%sIdentity", prefix); 108 snprintf(password_key, sizeof(password_key), "%sPassword", prefix); 109 110 identity = l_settings_get_string(settings, "Security", identity_key); 111 112 if (!identity) { 113 secret = l_queue_find(secrets, eap_secret_info_match, 114 identity_key); 115 if (secret) 116 return 0; 117 118 eap_append_secret(out_missing, EAP_SECRET_REMOTE_USER_PASSWORD, 119 identity_key, password_key, NULL, 120 EAP_CACHE_TEMPORARY); 121 122 return 0; 123 } 124 125 password = l_settings_get_string(settings, "Security", password_key); 126 127 if (!password) { 128 secret = l_queue_find(secrets, eap_secret_info_match, 129 password_key); 130 if (secret) 131 return 0; 132 133 eap_append_secret(out_missing, EAP_SECRET_REMOTE_PASSWORD, 134 password_key, NULL, identity, 135 EAP_CACHE_TEMPORARY); 136 } else 137 explicit_bzero(password, strlen(password)); 138 139 return 0; 140 } 141 142 static bool eap_md5_load_settings(struct eap_state *eap, 143 struct l_settings *settings, 144 const char *prefix) 145 { 146 struct eap_md5_state *md5; 147 char password_key[72]; 148 char *secret; 149 150 snprintf(password_key, sizeof(password_key), "%sPassword", prefix); 151 secret = l_settings_get_string(settings, "Security", password_key); 152 153 if (!secret) { 154 snprintf(password_key, sizeof(password_key), "%sMD5-Secret", 155 prefix); 156 secret = l_settings_get_string(settings, "Security", 157 password_key); 158 if (!secret) 159 return false; 160 } 161 162 md5 = l_new(struct eap_md5_state, 1); 163 md5->secret = secret; 164 165 eap_set_data(eap, md5); 166 167 return true; 168 } 169 170 static struct eap_method eap_md5 = { 171 .request_type = EAP_TYPE_MD5_CHALLENGE, 172 .exports_msk = false, 173 .name = "MD5", 174 175 .free = eap_md5_free, 176 .handle_request = eap_md5_handle_request, 177 .check_settings = eap_md5_check_settings, 178 .load_settings = eap_md5_load_settings, 179 }; 180 181 static int eap_md5_init(void) 182 { 183 l_debug(""); 184 return eap_register_method(&eap_md5); 185 } 186 187 static void eap_md5_exit(void) 188 { 189 l_debug(""); 190 eap_unregister_method(&eap_md5); 191 } 192 193 EAP_METHOD_BUILTIN(eap_md5, eap_md5_init, eap_md5_exit)