owe.c (5687B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2018-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 "src/crypto.h" 30 #include "src/ie.h" 31 #include "src/handshake.h" 32 #include "src/owe.h" 33 #include "src/mpdu.h" 34 #include "src/auth-proto.h" 35 36 struct owe_sm { 37 struct handshake_state *hs; 38 const struct l_ecc_curve *curve; 39 struct l_ecc_scalar *private; 40 struct l_ecc_point *public_key; 41 uint8_t retry; 42 uint16_t group; 43 const unsigned int *ecc_groups; 44 }; 45 46 static bool owe_reset(struct owe_sm *owe) 47 { 48 if (owe->hs->force_default_ecc_group) { 49 if (owe->retry != 0) { 50 l_warn("Forced default OWE group but was rejected!"); 51 return false; 52 } 53 54 l_debug("Forcing default OWE group 19"); 55 56 owe->retry++; 57 owe->group = 19; 58 59 goto get_curve; 60 } 61 62 /* 63 * Reset OWE with a different curve group and generate a new key pair 64 */ 65 if (owe->ecc_groups[owe->retry] == 0) 66 return false; 67 68 owe->group = owe->ecc_groups[owe->retry]; 69 70 get_curve: 71 owe->curve = l_ecc_curve_from_ike_group(owe->group); 72 73 if (owe->private) 74 l_ecc_scalar_free(owe->private); 75 76 if (owe->public_key) 77 l_ecc_point_free(owe->public_key); 78 79 if (!l_ecdh_generate_key_pair(owe->curve, &owe->private, 80 &owe->public_key)) 81 return false; 82 83 return true; 84 } 85 86 void owe_sm_free(struct owe_sm *owe) 87 { 88 l_ecc_scalar_free(owe->private); 89 l_ecc_point_free(owe->public_key); 90 91 l_free(owe); 92 } 93 94 void owe_build_dh_ie(struct owe_sm *owe, uint8_t *buf, size_t *len_out) 95 { 96 /* 97 * A client wishing to do OWE ... MUST include a Diffie-Hellman 98 * Parameter element to its 802.11 association request. 99 */ 100 buf[0] = IE_TYPE_EXTENSION; 101 buf[2] = IE_TYPE_OWE_DH_PARAM - 256; 102 l_put_le16(owe->group, buf + 3); /* group */ 103 *len_out = l_ecc_point_get_x(owe->public_key, buf + 5, 104 L_ECC_SCALAR_MAX_BYTES); 105 buf[1] = 3 + *len_out; /* length */ 106 107 *len_out += 5; 108 } 109 110 /* 111 * RFC 8110 Section 4.4 Post Association 112 */ 113 static bool owe_compute_keys(struct owe_sm *owe, const void *public_key, 114 size_t pub_len) 115 { 116 struct l_ecc_scalar *shared_secret; 117 uint8_t ss_buf[L_ECC_SCALAR_MAX_BYTES]; 118 uint8_t prk[L_ECC_SCALAR_MAX_BYTES]; 119 uint8_t pmk[L_ECC_SCALAR_MAX_BYTES]; 120 uint8_t pmkid[16]; 121 uint8_t key[L_ECC_SCALAR_MAX_BYTES + L_ECC_SCALAR_MAX_BYTES + 2]; 122 uint8_t *ptr = key; 123 struct iovec iov[2]; 124 struct l_checksum *sha; 125 struct l_ecc_point *other_public; 126 ssize_t nbytes; 127 enum l_checksum_type type; 128 129 other_public = l_ecc_point_from_data(owe->curve, 130 L_ECC_POINT_TYPE_COMPLIANT, 131 public_key, pub_len); 132 if (!other_public) { 133 l_error("AP public key was not valid"); 134 return false; 135 } 136 137 if (!l_ecdh_generate_shared_secret(owe->private, other_public, 138 &shared_secret)) { 139 l_ecc_point_free(other_public); 140 return false; 141 } 142 143 l_ecc_point_free(other_public); 144 145 nbytes = l_ecc_scalar_get_data(shared_secret, ss_buf, sizeof(ss_buf)); 146 l_ecc_scalar_free(shared_secret); 147 148 if (nbytes < 0) 149 return false; 150 151 ptr += l_ecc_point_get_x(owe->public_key, ptr, sizeof(key)); 152 memcpy(ptr, public_key, nbytes); 153 ptr += nbytes; 154 l_put_le16(owe->group, ptr); 155 ptr += 2; 156 157 switch (owe->group) { 158 case 19: 159 type = L_CHECKSUM_SHA256; 160 break; 161 case 20: 162 type = L_CHECKSUM_SHA384; 163 break; 164 default: 165 goto failed; 166 } 167 168 /* prk = HKDF-extract(C | A | group, z) */ 169 if (!hkdf_extract(type, key, ptr - key, 1, prk, ss_buf, nbytes)) 170 goto failed; 171 172 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */ 173 if (!hkdf_expand(type, prk, nbytes, "OWE Key Generation", pmk, nbytes)) 174 goto failed; 175 176 sha = l_checksum_new(type); 177 178 /* PMKID = Truncate-128(Hash(C | A)) */ 179 iov[0].iov_base = key; /* first nbytes of key are owe->public_key */ 180 iov[0].iov_len = nbytes; 181 iov[1].iov_base = (void *) public_key; 182 iov[1].iov_len = nbytes; 183 184 l_checksum_updatev(sha, iov, 2); 185 186 l_checksum_get_digest(sha, pmkid, 16); 187 188 l_checksum_free(sha); 189 190 handshake_state_set_pmk(owe->hs, pmk, nbytes); 191 handshake_state_set_pmkid(owe->hs, pmkid); 192 193 return true; 194 195 failed: 196 memset(ss_buf, 0, sizeof(ss_buf)); 197 return false; 198 } 199 200 bool owe_next_group(struct owe_sm *owe) 201 { 202 /* retry with another group, if possible */ 203 owe->retry++; 204 205 if (!owe_reset(owe)) 206 return false; 207 208 return true; 209 } 210 211 int owe_process_dh_ie(struct owe_sm *owe, const uint8_t *dh, size_t len) 212 { 213 if (!dh || len < 34) { 214 l_error("associate response did not include proper OWE IE's"); 215 goto invalid_ies; 216 } 217 218 if (l_get_le16(dh) != owe->group) { 219 l_error("associate response contained unsupported group %u", 220 l_get_le16(dh)); 221 return -EBADMSG; 222 } 223 224 if (!owe_compute_keys(owe, dh + 2, len - 2)) { 225 l_error("could not compute OWE keys"); 226 return -EBADMSG; 227 } 228 229 return 0; 230 231 invalid_ies: 232 return MMPDU_STATUS_CODE_INVALID_ELEMENT; 233 } 234 235 struct owe_sm *owe_sm_new(struct handshake_state *hs) 236 { 237 struct owe_sm *owe = l_new(struct owe_sm, 1); 238 239 owe->hs = hs; 240 owe->ecc_groups = l_ecc_supported_ike_groups(); 241 242 if (!owe_reset(owe)) { 243 l_free(owe); 244 return NULL; 245 } 246 247 return owe; 248 }