anqputil.c (4152B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 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 <ell/ell.h> 28 29 #include "ell/useful.h" 30 #include "src/anqputil.h" 31 #include "src/ie.h" 32 #include "src/util.h" 33 34 void anqp_iter_init(struct anqp_iter *iter, const unsigned char *anqp, 35 unsigned int len) 36 { 37 iter->anqp = anqp; 38 iter->max = len; 39 iter->pos = 0; 40 } 41 42 bool anqp_iter_next(struct anqp_iter *iter, uint16_t *id, uint16_t *len, 43 const void **data) 44 { 45 const unsigned char *anqp = iter->anqp + iter->pos; 46 const unsigned char *end = iter->anqp + iter->max; 47 48 if (iter->pos + 4 >= iter->max) 49 return false; 50 51 if (l_get_le16(anqp + 2) > end - anqp) 52 return false; 53 54 *id = l_get_le16(anqp); 55 anqp += 2; 56 57 *len = l_get_le16(anqp); 58 anqp += 2; 59 60 *data = anqp; 61 62 iter->id = *id; 63 iter->len = *len; 64 iter->data = *data; 65 66 iter->pos = anqp + *len - iter->anqp; 67 68 return true; 69 } 70 71 bool anqp_hs20_parse_osu_provider_nai(const unsigned char *anqp, 72 unsigned int len, const char **nai_out) 73 { 74 uint8_t nai_len; 75 static char nai[256] = { 0 }; 76 77 if (len < 1) 78 return false; 79 80 nai_len = *anqp++; 81 len--; 82 83 if (len < nai_len) 84 return false; 85 86 memcpy(nai, anqp, nai_len); 87 88 *nai_out = nai; 89 90 return true; 91 } 92 93 bool anqp_iter_is_hs20(const struct anqp_iter *iter, uint8_t *stype, 94 unsigned int *len, const unsigned char **data) 95 { 96 const unsigned char *anqp = iter->data; 97 unsigned int anqp_len = iter->len; 98 uint8_t type; 99 100 if (iter->len < 6) 101 return false; 102 103 if (memcmp(anqp, wifi_alliance_oui, 3)) 104 return false; 105 106 anqp += 3; 107 anqp_len -= 3; 108 109 type = *anqp++; 110 anqp_len--; 111 112 if (type != 0x11) 113 return false; 114 115 *stype = *anqp++; 116 anqp_len--; 117 118 /* reserved byte */ 119 anqp++; 120 anqp_len--; 121 122 *data = anqp; 123 *len = anqp_len; 124 125 return true; 126 } 127 128 char **anqp_parse_nai_realms(const unsigned char *anqp, unsigned int len) 129 { 130 char **realms = NULL; 131 uint16_t count; 132 133 if (len < 2) 134 return NULL; 135 136 count = l_get_le16(anqp); 137 138 anqp += 2; 139 len -= 2; 140 141 l_debug(""); 142 143 while (count--) { 144 uint16_t realm_len; 145 uint8_t encoding; 146 uint8_t nai_len; 147 char nai_realm[256] = { 0 }; 148 149 /* 150 * The method list is a variable field, so the only way to 151 * reliably increment anqp is by realm_len at the very end since 152 * we don't know how many bytes parse_eap advanced (it does 153 * internal length checking so it should not overflow). We 154 * can't incrementally advance anqp/len, hence the hardcoded 155 * length and pointer adjustments. 156 */ 157 158 if (len < 4) 159 goto failed; 160 161 realm_len = l_get_le16(anqp); 162 anqp += 2; 163 len -= 2; 164 165 encoding = anqp[0]; 166 167 nai_len = anqp[1]; 168 169 if (len - 2 < nai_len) 170 goto failed; 171 172 memcpy(nai_realm, anqp + 2, nai_len); 173 174 /* 175 * TODO: Verify NAI encoding in accordance with RFC 4282 ? 176 * 177 * The encoding in RFC 4282 seems to only limit which characters 178 * can be used in an NAI. Since these come in from public 179 * action frames it could have been spoofed, but ultimately if 180 * its bogus the AP won't allow us to connect. 181 */ 182 if (!test_bit(&encoding, 0)) 183 l_warn("Not verifying NAI encoding"); 184 else if (!l_utf8_validate(nai_realm, nai_len, NULL)) { 185 l_warn("NAI is not UTF-8"); 186 goto failed; 187 } 188 189 realms = l_strv_append(realms, nai_realm); 190 191 if (len < realm_len) 192 goto failed; 193 194 anqp += realm_len; 195 len -= realm_len; 196 } 197 198 return realms; 199 200 failed: 201 l_strv_free(realms); 202 return NULL; 203 }