]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/common/dpp.c
DPP2: Move dpp_build_conf_result() to be within ifdef block
[thirdparty/hostap.git] / src / common / dpp.c
1 /*
2 * DPP functionality shared between hostapd and wpa_supplicant
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2019, The Linux Foundation
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10 #include "utils/includes.h"
11 #include <fcntl.h>
12 #include <openssl/opensslv.h>
13 #include <openssl/err.h>
14 #include <openssl/asn1.h>
15 #include <openssl/asn1t.h>
16
17 #include "utils/common.h"
18 #include "utils/base64.h"
19 #include "utils/json.h"
20 #include "utils/ip_addr.h"
21 #include "utils/eloop.h"
22 #include "common/ieee802_11_common.h"
23 #include "common/ieee802_11_defs.h"
24 #include "common/wpa_ctrl.h"
25 #include "common/gas.h"
26 #include "crypto/crypto.h"
27 #include "crypto/random.h"
28 #include "crypto/aes.h"
29 #include "crypto/aes_siv.h"
30 #include "crypto/sha384.h"
31 #include "crypto/sha512.h"
32 #include "drivers/driver.h"
33 #include "dpp.h"
34
35
36 #ifdef CONFIG_TESTING_OPTIONS
37 enum dpp_test_behavior dpp_test = DPP_TEST_DISABLED;
38 u8 dpp_pkex_own_mac_override[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
39 u8 dpp_pkex_peer_mac_override[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
40 u8 dpp_pkex_ephemeral_key_override[600];
41 size_t dpp_pkex_ephemeral_key_override_len = 0;
42 u8 dpp_protocol_key_override[600];
43 size_t dpp_protocol_key_override_len = 0;
44 u8 dpp_nonce_override[DPP_MAX_NONCE_LEN];
45 size_t dpp_nonce_override_len = 0;
46
47 static int dpp_test_gen_invalid_key(struct wpabuf *msg,
48 const struct dpp_curve_params *curve);
49 #endif /* CONFIG_TESTING_OPTIONS */
50
51 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
52 (defined(LIBRESSL_VERSION_NUMBER) && \
53 LIBRESSL_VERSION_NUMBER < 0x20700000L)
54 /* Compatibility wrappers for older versions. */
55
56 static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
57 {
58 sig->r = r;
59 sig->s = s;
60 return 1;
61 }
62
63
64 static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
65 const BIGNUM **ps)
66 {
67 if (pr)
68 *pr = sig->r;
69 if (ps)
70 *ps = sig->s;
71 }
72
73 #endif
74
75
76 struct dpp_connection {
77 struct dl_list list;
78 struct dpp_controller *ctrl;
79 struct dpp_relay_controller *relay;
80 struct dpp_global *global;
81 struct dpp_authentication *auth;
82 int sock;
83 u8 mac_addr[ETH_ALEN];
84 unsigned int freq;
85 u8 msg_len[4];
86 size_t msg_len_octets;
87 struct wpabuf *msg;
88 struct wpabuf *msg_out;
89 size_t msg_out_pos;
90 unsigned int read_eloop:1;
91 unsigned int write_eloop:1;
92 unsigned int on_tcp_tx_complete_gas_done:1;
93 unsigned int on_tcp_tx_complete_remove:1;
94 unsigned int on_tcp_tx_complete_auth_ok:1;
95 };
96
97 /* Remote Controller */
98 struct dpp_relay_controller {
99 struct dl_list list;
100 struct dpp_global *global;
101 u8 pkhash[SHA256_MAC_LEN];
102 struct hostapd_ip_addr ipaddr;
103 void *cb_ctx;
104 void (*tx)(void *ctx, const u8 *addr, unsigned int freq, const u8 *msg,
105 size_t len);
106 void (*gas_resp_tx)(void *ctx, const u8 *addr, u8 dialog_token,
107 int prot, struct wpabuf *buf);
108 struct dl_list conn; /* struct dpp_connection */
109 };
110
111 /* Local Controller */
112 struct dpp_controller {
113 struct dpp_global *global;
114 u8 allowed_roles;
115 int qr_mutual;
116 int sock;
117 struct dl_list conn; /* struct dpp_connection */
118 char *configurator_params;
119 };
120
121 struct dpp_global {
122 void *msg_ctx;
123 struct dl_list bootstrap; /* struct dpp_bootstrap_info */
124 struct dl_list configurator; /* struct dpp_configurator */
125 #ifdef CONFIG_DPP2
126 struct dl_list controllers; /* struct dpp_relay_controller */
127 struct dpp_controller *controller;
128 struct dl_list tcp_init; /* struct dpp_connection */
129 void *cb_ctx;
130 int (*process_conf_obj)(void *ctx, struct dpp_authentication *auth);
131 #endif /* CONFIG_DPP2 */
132 };
133
134 static const struct dpp_curve_params dpp_curves[] = {
135 /* The mandatory to support and the default NIST P-256 curve needs to
136 * be the first entry on this list. */
137 { "prime256v1", 32, 32, 16, 32, "P-256", 19, "ES256" },
138 { "secp384r1", 48, 48, 24, 48, "P-384", 20, "ES384" },
139 { "secp521r1", 64, 64, 32, 66, "P-521", 21, "ES512" },
140 { "brainpoolP256r1", 32, 32, 16, 32, "BP-256", 28, "BS256" },
141 { "brainpoolP384r1", 48, 48, 24, 48, "BP-384", 29, "BS384" },
142 { "brainpoolP512r1", 64, 64, 32, 64, "BP-512", 30, "BS512" },
143 { NULL, 0, 0, 0, 0, NULL, 0, NULL }
144 };
145
146
147 /* Role-specific elements for PKEX */
148
149 /* NIST P-256 */
150 static const u8 pkex_init_x_p256[32] = {
151 0x56, 0x26, 0x12, 0xcf, 0x36, 0x48, 0xfe, 0x0b,
152 0x07, 0x04, 0xbb, 0x12, 0x22, 0x50, 0xb2, 0x54,
153 0xb1, 0x94, 0x64, 0x7e, 0x54, 0xce, 0x08, 0x07,
154 0x2e, 0xec, 0xca, 0x74, 0x5b, 0x61, 0x2d, 0x25
155 };
156 static const u8 pkex_init_y_p256[32] = {
157 0x3e, 0x44, 0xc7, 0xc9, 0x8c, 0x1c, 0xa1, 0x0b,
158 0x20, 0x09, 0x93, 0xb2, 0xfd, 0xe5, 0x69, 0xdc,
159 0x75, 0xbc, 0xad, 0x33, 0xc1, 0xe7, 0xc6, 0x45,
160 0x4d, 0x10, 0x1e, 0x6a, 0x3d, 0x84, 0x3c, 0xa4
161 };
162 static const u8 pkex_resp_x_p256[32] = {
163 0x1e, 0xa4, 0x8a, 0xb1, 0xa4, 0xe8, 0x42, 0x39,
164 0xad, 0x73, 0x07, 0xf2, 0x34, 0xdf, 0x57, 0x4f,
165 0xc0, 0x9d, 0x54, 0xbe, 0x36, 0x1b, 0x31, 0x0f,
166 0x59, 0x91, 0x52, 0x33, 0xac, 0x19, 0x9d, 0x76
167 };
168 static const u8 pkex_resp_y_p256[32] = {
169 0xd9, 0xfb, 0xf6, 0xb9, 0xf5, 0xfa, 0xdf, 0x19,
170 0x58, 0xd8, 0x3e, 0xc9, 0x89, 0x7a, 0x35, 0xc1,
171 0xbd, 0xe9, 0x0b, 0x77, 0x7a, 0xcb, 0x91, 0x2a,
172 0xe8, 0x21, 0x3f, 0x47, 0x52, 0x02, 0x4d, 0x67
173 };
174
175 /* NIST P-384 */
176 static const u8 pkex_init_x_p384[48] = {
177 0x95, 0x3f, 0x42, 0x9e, 0x50, 0x7f, 0xf9, 0xaa,
178 0xac, 0x1a, 0xf2, 0x85, 0x2e, 0x64, 0x91, 0x68,
179 0x64, 0xc4, 0x3c, 0xb7, 0x5c, 0xf8, 0xc9, 0x53,
180 0x6e, 0x58, 0x4c, 0x7f, 0xc4, 0x64, 0x61, 0xac,
181 0x51, 0x8a, 0x6f, 0xfe, 0xab, 0x74, 0xe6, 0x12,
182 0x81, 0xac, 0x38, 0x5d, 0x41, 0xe6, 0xb9, 0xa3
183 };
184 static const u8 pkex_init_y_p384[48] = {
185 0x76, 0x2f, 0x68, 0x84, 0xa6, 0xb0, 0x59, 0x29,
186 0x83, 0xa2, 0x6c, 0xa4, 0x6c, 0x3b, 0xf8, 0x56,
187 0x76, 0x11, 0x2a, 0x32, 0x90, 0xbd, 0x07, 0xc7,
188 0x37, 0x39, 0x9d, 0xdb, 0x96, 0xf3, 0x2b, 0xb6,
189 0x27, 0xbb, 0x29, 0x3c, 0x17, 0x33, 0x9d, 0x94,
190 0xc3, 0xda, 0xac, 0x46, 0xb0, 0x8e, 0x07, 0x18
191 };
192 static const u8 pkex_resp_x_p384[48] = {
193 0xad, 0xbe, 0xd7, 0x1d, 0x3a, 0x71, 0x64, 0x98,
194 0x5f, 0xb4, 0xd6, 0x4b, 0x50, 0xd0, 0x84, 0x97,
195 0x4b, 0x7e, 0x57, 0x70, 0xd2, 0xd9, 0xf4, 0x92,
196 0x2a, 0x3f, 0xce, 0x99, 0xc5, 0x77, 0x33, 0x44,
197 0x14, 0x56, 0x92, 0xcb, 0xae, 0x46, 0x64, 0xdf,
198 0xe0, 0xbb, 0xd7, 0xb1, 0x29, 0x20, 0x72, 0xdf
199 };
200 static const u8 pkex_resp_y_p384[48] = {
201 0xab, 0xa7, 0xdf, 0x52, 0xaa, 0xe2, 0x35, 0x0c,
202 0xe3, 0x75, 0x32, 0xe6, 0xbf, 0x06, 0xc8, 0x7c,
203 0x38, 0x29, 0x4c, 0xec, 0x82, 0xac, 0xd7, 0xa3,
204 0x09, 0xd2, 0x0e, 0x22, 0x5a, 0x74, 0x52, 0xa1,
205 0x7e, 0x54, 0x4e, 0xfe, 0xc6, 0x29, 0x33, 0x63,
206 0x15, 0xe1, 0x7b, 0xe3, 0x40, 0x1c, 0xca, 0x06
207 };
208
209 /* NIST P-521 */
210 static const u8 pkex_init_x_p521[66] = {
211 0x00, 0x16, 0x20, 0x45, 0x19, 0x50, 0x95, 0x23,
212 0x0d, 0x24, 0xbe, 0x00, 0x87, 0xdc, 0xfa, 0xf0,
213 0x58, 0x9a, 0x01, 0x60, 0x07, 0x7a, 0xca, 0x76,
214 0x01, 0xab, 0x2d, 0x5a, 0x46, 0xcd, 0x2c, 0xb5,
215 0x11, 0x9a, 0xff, 0xaa, 0x48, 0x04, 0x91, 0x38,
216 0xcf, 0x86, 0xfc, 0xa4, 0xa5, 0x0f, 0x47, 0x01,
217 0x80, 0x1b, 0x30, 0xa3, 0xae, 0xe8, 0x1c, 0x2e,
218 0xea, 0xcc, 0xf0, 0x03, 0x9f, 0x77, 0x4c, 0x8d,
219 0x97, 0x76
220 };
221 static const u8 pkex_init_y_p521[66] = {
222 0x00, 0xb3, 0x8e, 0x02, 0xe4, 0x2a, 0x63, 0x59,
223 0x12, 0xc6, 0x10, 0xba, 0x3a, 0xf9, 0x02, 0x99,
224 0x3f, 0x14, 0xf0, 0x40, 0xde, 0x5c, 0xc9, 0x8b,
225 0x02, 0x55, 0xfa, 0x91, 0xb1, 0xcc, 0x6a, 0xbd,
226 0xe5, 0x62, 0xc0, 0xc5, 0xe3, 0xa1, 0x57, 0x9f,
227 0x08, 0x1a, 0xa6, 0xe2, 0xf8, 0x55, 0x90, 0xbf,
228 0xf5, 0xa6, 0xc3, 0xd8, 0x52, 0x1f, 0xb7, 0x02,
229 0x2e, 0x7c, 0xc8, 0xb3, 0x20, 0x1e, 0x79, 0x8d,
230 0x03, 0xa8
231 };
232 static const u8 pkex_resp_x_p521[66] = {
233 0x00, 0x79, 0xe4, 0x4d, 0x6b, 0x5e, 0x12, 0x0a,
234 0x18, 0x2c, 0xb3, 0x05, 0x77, 0x0f, 0xc3, 0x44,
235 0x1a, 0xcd, 0x78, 0x46, 0x14, 0xee, 0x46, 0x3f,
236 0xab, 0xc9, 0x59, 0x7c, 0x85, 0xa0, 0xc2, 0xfb,
237 0x02, 0x32, 0x99, 0xde, 0x5d, 0xe1, 0x0d, 0x48,
238 0x2d, 0x71, 0x7d, 0x8d, 0x3f, 0x61, 0x67, 0x9e,
239 0x2b, 0x8b, 0x12, 0xde, 0x10, 0x21, 0x55, 0x0a,
240 0x5b, 0x2d, 0xe8, 0x05, 0x09, 0xf6, 0x20, 0x97,
241 0x84, 0xb4
242 };
243 static const u8 pkex_resp_y_p521[66] = {
244 0x00, 0x46, 0x63, 0x39, 0xbe, 0xcd, 0xa4, 0x2d,
245 0xca, 0x27, 0x74, 0xd4, 0x1b, 0x91, 0x33, 0x20,
246 0x83, 0xc7, 0x3b, 0xa4, 0x09, 0x8b, 0x8e, 0xa3,
247 0x88, 0xe9, 0x75, 0x7f, 0x56, 0x7b, 0x38, 0x84,
248 0x62, 0x02, 0x7c, 0x90, 0x51, 0x07, 0xdb, 0xe9,
249 0xd0, 0xde, 0xda, 0x9a, 0x5d, 0xe5, 0x94, 0xd2,
250 0xcf, 0x9d, 0x4c, 0x33, 0x91, 0xa6, 0xc3, 0x80,
251 0xa7, 0x6e, 0x7e, 0x8d, 0xf8, 0x73, 0x6e, 0x53,
252 0xce, 0xe1
253 };
254
255 /* Brainpool P-256r1 */
256 static const u8 pkex_init_x_bp_p256r1[32] = {
257 0x46, 0x98, 0x18, 0x6c, 0x27, 0xcd, 0x4b, 0x10,
258 0x7d, 0x55, 0xa3, 0xdd, 0x89, 0x1f, 0x9f, 0xca,
259 0xc7, 0x42, 0x5b, 0x8a, 0x23, 0xed, 0xf8, 0x75,
260 0xac, 0xc7, 0xe9, 0x8d, 0xc2, 0x6f, 0xec, 0xd8
261 };
262 static const u8 pkex_init_y_bp_p256r1[32] = {
263 0x93, 0xca, 0xef, 0xa9, 0x66, 0x3e, 0x87, 0xcd,
264 0x52, 0x6e, 0x54, 0x13, 0xef, 0x31, 0x67, 0x30,
265 0x15, 0x13, 0x9d, 0x6d, 0xc0, 0x95, 0x32, 0xbe,
266 0x4f, 0xab, 0x5d, 0xf7, 0xbf, 0x5e, 0xaa, 0x0b
267 };
268 static const u8 pkex_resp_x_bp_p256r1[32] = {
269 0x90, 0x18, 0x84, 0xc9, 0xdc, 0xcc, 0xb5, 0x2f,
270 0x4a, 0x3f, 0x4f, 0x18, 0x0a, 0x22, 0x56, 0x6a,
271 0xa9, 0xef, 0xd4, 0xe6, 0xc3, 0x53, 0xc2, 0x1a,
272 0x23, 0x54, 0xdd, 0x08, 0x7e, 0x10, 0xd8, 0xe3
273 };
274 static const u8 pkex_resp_y_bp_p256r1[32] = {
275 0x2a, 0xfa, 0x98, 0x9b, 0xe3, 0xda, 0x30, 0xfd,
276 0x32, 0x28, 0xcb, 0x66, 0xfb, 0x40, 0x7f, 0xf2,
277 0xb2, 0x25, 0x80, 0x82, 0x44, 0x85, 0x13, 0x7e,
278 0x4b, 0xb5, 0x06, 0xc0, 0x03, 0x69, 0x23, 0x64
279 };
280
281 /* Brainpool P-384r1 */
282 static const u8 pkex_init_x_bp_p384r1[48] = {
283 0x0a, 0x2c, 0xeb, 0x49, 0x5e, 0xb7, 0x23, 0xbd,
284 0x20, 0x5b, 0xe0, 0x49, 0xdf, 0xcf, 0xcf, 0x19,
285 0x37, 0x36, 0xe1, 0x2f, 0x59, 0xdb, 0x07, 0x06,
286 0xb5, 0xeb, 0x2d, 0xae, 0xc2, 0xb2, 0x38, 0x62,
287 0xa6, 0x73, 0x09, 0xa0, 0x6c, 0x0a, 0xa2, 0x30,
288 0x99, 0xeb, 0xf7, 0x1e, 0x47, 0xb9, 0x5e, 0xbe
289 };
290 static const u8 pkex_init_y_bp_p384r1[48] = {
291 0x54, 0x76, 0x61, 0x65, 0x75, 0x5a, 0x2f, 0x99,
292 0x39, 0x73, 0xca, 0x6c, 0xf9, 0xf7, 0x12, 0x86,
293 0x54, 0xd5, 0xd4, 0xad, 0x45, 0x7b, 0xbf, 0x32,
294 0xee, 0x62, 0x8b, 0x9f, 0x52, 0xe8, 0xa0, 0xc9,
295 0xb7, 0x9d, 0xd1, 0x09, 0xb4, 0x79, 0x1c, 0x3e,
296 0x1a, 0xbf, 0x21, 0x45, 0x66, 0x6b, 0x02, 0x52
297 };
298 static const u8 pkex_resp_x_bp_p384r1[48] = {
299 0x03, 0xa2, 0x57, 0xef, 0xe8, 0x51, 0x21, 0xa0,
300 0xc8, 0x9e, 0x21, 0x02, 0xb5, 0x9a, 0x36, 0x25,
301 0x74, 0x22, 0xd1, 0xf2, 0x1b, 0xa8, 0x9a, 0x9b,
302 0x97, 0xbc, 0x5a, 0xeb, 0x26, 0x15, 0x09, 0x71,
303 0x77, 0x59, 0xec, 0x8b, 0xb7, 0xe1, 0xe8, 0xce,
304 0x65, 0xb8, 0xaf, 0xf8, 0x80, 0xae, 0x74, 0x6c
305 };
306 static const u8 pkex_resp_y_bp_p384r1[48] = {
307 0x2f, 0xd9, 0x6a, 0xc7, 0x3e, 0xec, 0x76, 0x65,
308 0x2d, 0x38, 0x7f, 0xec, 0x63, 0x26, 0x3f, 0x04,
309 0xd8, 0x4e, 0xff, 0xe1, 0x0a, 0x51, 0x74, 0x70,
310 0xe5, 0x46, 0x63, 0x7f, 0x5c, 0xc0, 0xd1, 0x7c,
311 0xfb, 0x2f, 0xea, 0xe2, 0xd8, 0x0f, 0x84, 0xcb,
312 0xe9, 0x39, 0x5c, 0x64, 0xfe, 0xcb, 0x2f, 0xf1
313 };
314
315 /* Brainpool P-512r1 */
316 static const u8 pkex_init_x_bp_p512r1[64] = {
317 0x4c, 0xe9, 0xb6, 0x1c, 0xe2, 0x00, 0x3c, 0x9c,
318 0xa9, 0xc8, 0x56, 0x52, 0xaf, 0x87, 0x3e, 0x51,
319 0x9c, 0xbb, 0x15, 0x31, 0x1e, 0xc1, 0x05, 0xfc,
320 0x7c, 0x77, 0xd7, 0x37, 0x61, 0x27, 0xd0, 0x95,
321 0x98, 0xee, 0x5d, 0xa4, 0x3d, 0x09, 0xdb, 0x3d,
322 0xfa, 0x89, 0x9e, 0x7f, 0xa6, 0xa6, 0x9c, 0xff,
323 0x83, 0x5c, 0x21, 0x6c, 0x3e, 0xf2, 0xfe, 0xdc,
324 0x63, 0xe4, 0xd1, 0x0e, 0x75, 0x45, 0x69, 0x0f
325 };
326 static const u8 pkex_init_y_bp_p512r1[64] = {
327 0x50, 0xb5, 0x9b, 0xfa, 0x45, 0x67, 0x75, 0x94,
328 0x44, 0xe7, 0x68, 0xb0, 0xeb, 0x3e, 0xb3, 0xb8,
329 0xf9, 0x99, 0x05, 0xef, 0xae, 0x6c, 0xbc, 0xe3,
330 0xe1, 0xd2, 0x51, 0x54, 0xdf, 0x59, 0xd4, 0x45,
331 0x41, 0x3a, 0xa8, 0x0b, 0x76, 0x32, 0x44, 0x0e,
332 0x07, 0x60, 0x3a, 0x6e, 0xbe, 0xfe, 0xe0, 0x58,
333 0x52, 0xa0, 0xaa, 0x8b, 0xd8, 0x5b, 0xf2, 0x71,
334 0x11, 0x9a, 0x9e, 0x8f, 0x1a, 0xd1, 0xc9, 0x99
335 };
336 static const u8 pkex_resp_x_bp_p512r1[64] = {
337 0x2a, 0x60, 0x32, 0x27, 0xa1, 0xe6, 0x94, 0x72,
338 0x1c, 0x48, 0xbe, 0xc5, 0x77, 0x14, 0x30, 0x76,
339 0xe4, 0xbf, 0xf7, 0x7b, 0xc5, 0xfd, 0xdf, 0x19,
340 0x1e, 0x0f, 0xdf, 0x1c, 0x40, 0xfa, 0x34, 0x9e,
341 0x1f, 0x42, 0x24, 0xa3, 0x2c, 0xd5, 0xc7, 0xc9,
342 0x7b, 0x47, 0x78, 0x96, 0xf1, 0x37, 0x0e, 0x88,
343 0xcb, 0xa6, 0x52, 0x29, 0xd7, 0xa8, 0x38, 0x29,
344 0x8e, 0x6e, 0x23, 0x47, 0xd4, 0x4b, 0x70, 0x3e
345 };
346 static const u8 pkex_resp_y_bp_p512r1[64] = {
347 0x80, 0x1f, 0x43, 0xd2, 0x17, 0x35, 0xec, 0x81,
348 0xd9, 0x4b, 0xdc, 0x81, 0x19, 0xd9, 0x5f, 0x68,
349 0x16, 0x84, 0xfe, 0x63, 0x4b, 0x8d, 0x5d, 0xaa,
350 0x88, 0x4a, 0x47, 0x48, 0xd4, 0xea, 0xab, 0x7d,
351 0x6a, 0xbf, 0xe1, 0x28, 0x99, 0x6a, 0x87, 0x1c,
352 0x30, 0xb4, 0x44, 0x2d, 0x75, 0xac, 0x35, 0x09,
353 0x73, 0x24, 0x3d, 0xb4, 0x43, 0xb1, 0xc1, 0x56,
354 0x56, 0xad, 0x30, 0x87, 0xf4, 0xc3, 0x00, 0xc7
355 };
356
357
358 static void dpp_debug_print_point(const char *title, const EC_GROUP *group,
359 const EC_POINT *point)
360 {
361 BIGNUM *x, *y;
362 BN_CTX *ctx;
363 char *x_str = NULL, *y_str = NULL;
364
365 if (!wpa_debug_show_keys)
366 return;
367
368 ctx = BN_CTX_new();
369 x = BN_new();
370 y = BN_new();
371 if (!ctx || !x || !y ||
372 EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx) != 1)
373 goto fail;
374
375 x_str = BN_bn2hex(x);
376 y_str = BN_bn2hex(y);
377 if (!x_str || !y_str)
378 goto fail;
379
380 wpa_printf(MSG_DEBUG, "%s (%s,%s)", title, x_str, y_str);
381
382 fail:
383 OPENSSL_free(x_str);
384 OPENSSL_free(y_str);
385 BN_free(x);
386 BN_free(y);
387 BN_CTX_free(ctx);
388 }
389
390
391 static int dpp_hash_vector(const struct dpp_curve_params *curve,
392 size_t num_elem, const u8 *addr[], const size_t *len,
393 u8 *mac)
394 {
395 if (curve->hash_len == 32)
396 return sha256_vector(num_elem, addr, len, mac);
397 if (curve->hash_len == 48)
398 return sha384_vector(num_elem, addr, len, mac);
399 if (curve->hash_len == 64)
400 return sha512_vector(num_elem, addr, len, mac);
401 return -1;
402 }
403
404
405 static int dpp_hkdf_expand(size_t hash_len, const u8 *secret, size_t secret_len,
406 const char *label, u8 *out, size_t outlen)
407 {
408 if (hash_len == 32)
409 return hmac_sha256_kdf(secret, secret_len, NULL,
410 (const u8 *) label, os_strlen(label),
411 out, outlen);
412 if (hash_len == 48)
413 return hmac_sha384_kdf(secret, secret_len, NULL,
414 (const u8 *) label, os_strlen(label),
415 out, outlen);
416 if (hash_len == 64)
417 return hmac_sha512_kdf(secret, secret_len, NULL,
418 (const u8 *) label, os_strlen(label),
419 out, outlen);
420 return -1;
421 }
422
423
424 static int dpp_hmac_vector(size_t hash_len, const u8 *key, size_t key_len,
425 size_t num_elem, const u8 *addr[],
426 const size_t *len, u8 *mac)
427 {
428 if (hash_len == 32)
429 return hmac_sha256_vector(key, key_len, num_elem, addr, len,
430 mac);
431 if (hash_len == 48)
432 return hmac_sha384_vector(key, key_len, num_elem, addr, len,
433 mac);
434 if (hash_len == 64)
435 return hmac_sha512_vector(key, key_len, num_elem, addr, len,
436 mac);
437 return -1;
438 }
439
440
441 static int dpp_hmac(size_t hash_len, const u8 *key, size_t key_len,
442 const u8 *data, size_t data_len, u8 *mac)
443 {
444 if (hash_len == 32)
445 return hmac_sha256(key, key_len, data, data_len, mac);
446 if (hash_len == 48)
447 return hmac_sha384(key, key_len, data, data_len, mac);
448 if (hash_len == 64)
449 return hmac_sha512(key, key_len, data, data_len, mac);
450 return -1;
451 }
452
453
454 static int dpp_bn2bin_pad(const BIGNUM *bn, u8 *pos, size_t len)
455 {
456 int num_bytes, offset;
457
458 num_bytes = BN_num_bytes(bn);
459 if ((size_t) num_bytes > len)
460 return -1;
461 offset = len - num_bytes;
462 os_memset(pos, 0, offset);
463 BN_bn2bin(bn, pos + offset);
464 return 0;
465 }
466
467
468 static struct wpabuf * dpp_get_pubkey_point(EVP_PKEY *pkey, int prefix)
469 {
470 int len, res;
471 EC_KEY *eckey;
472 struct wpabuf *buf;
473 unsigned char *pos;
474
475 eckey = EVP_PKEY_get1_EC_KEY(pkey);
476 if (!eckey)
477 return NULL;
478 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
479 len = i2o_ECPublicKey(eckey, NULL);
480 if (len <= 0) {
481 wpa_printf(MSG_ERROR,
482 "DDP: Failed to determine public key encoding length");
483 EC_KEY_free(eckey);
484 return NULL;
485 }
486
487 buf = wpabuf_alloc(len);
488 if (!buf) {
489 EC_KEY_free(eckey);
490 return NULL;
491 }
492
493 pos = wpabuf_put(buf, len);
494 res = i2o_ECPublicKey(eckey, &pos);
495 EC_KEY_free(eckey);
496 if (res != len) {
497 wpa_printf(MSG_ERROR,
498 "DDP: Failed to encode public key (res=%d/%d)",
499 res, len);
500 wpabuf_free(buf);
501 return NULL;
502 }
503
504 if (!prefix) {
505 /* Remove 0x04 prefix to match DPP definition */
506 pos = wpabuf_mhead(buf);
507 os_memmove(pos, pos + 1, len - 1);
508 buf->used--;
509 }
510
511 return buf;
512 }
513
514
515 static EVP_PKEY * dpp_set_pubkey_point_group(const EC_GROUP *group,
516 const u8 *buf_x, const u8 *buf_y,
517 size_t len)
518 {
519 EC_KEY *eckey = NULL;
520 BN_CTX *ctx;
521 EC_POINT *point = NULL;
522 BIGNUM *x = NULL, *y = NULL;
523 EVP_PKEY *pkey = NULL;
524
525 ctx = BN_CTX_new();
526 if (!ctx) {
527 wpa_printf(MSG_ERROR, "DPP: Out of memory");
528 return NULL;
529 }
530
531 point = EC_POINT_new(group);
532 x = BN_bin2bn(buf_x, len, NULL);
533 y = BN_bin2bn(buf_y, len, NULL);
534 if (!point || !x || !y) {
535 wpa_printf(MSG_ERROR, "DPP: Out of memory");
536 goto fail;
537 }
538
539 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
540 wpa_printf(MSG_ERROR,
541 "DPP: OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s",
542 ERR_error_string(ERR_get_error(), NULL));
543 goto fail;
544 }
545
546 if (!EC_POINT_is_on_curve(group, point, ctx) ||
547 EC_POINT_is_at_infinity(group, point)) {
548 wpa_printf(MSG_ERROR, "DPP: Invalid point");
549 goto fail;
550 }
551 dpp_debug_print_point("DPP: dpp_set_pubkey_point_group", group, point);
552
553 eckey = EC_KEY_new();
554 if (!eckey ||
555 EC_KEY_set_group(eckey, group) != 1 ||
556 EC_KEY_set_public_key(eckey, point) != 1) {
557 wpa_printf(MSG_ERROR,
558 "DPP: Failed to set EC_KEY: %s",
559 ERR_error_string(ERR_get_error(), NULL));
560 goto fail;
561 }
562 EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
563
564 pkey = EVP_PKEY_new();
565 if (!pkey || EVP_PKEY_set1_EC_KEY(pkey, eckey) != 1) {
566 wpa_printf(MSG_ERROR, "DPP: Could not create EVP_PKEY");
567 goto fail;
568 }
569
570 out:
571 BN_free(x);
572 BN_free(y);
573 EC_KEY_free(eckey);
574 EC_POINT_free(point);
575 BN_CTX_free(ctx);
576 return pkey;
577 fail:
578 EVP_PKEY_free(pkey);
579 pkey = NULL;
580 goto out;
581 }
582
583
584 static EVP_PKEY * dpp_set_pubkey_point(EVP_PKEY *group_key,
585 const u8 *buf, size_t len)
586 {
587 EC_KEY *eckey;
588 const EC_GROUP *group;
589 EVP_PKEY *pkey = NULL;
590
591 if (len & 1)
592 return NULL;
593
594 eckey = EVP_PKEY_get1_EC_KEY(group_key);
595 if (!eckey) {
596 wpa_printf(MSG_ERROR,
597 "DPP: Could not get EC_KEY from group_key");
598 return NULL;
599 }
600
601 group = EC_KEY_get0_group(eckey);
602 if (group)
603 pkey = dpp_set_pubkey_point_group(group, buf, buf + len / 2,
604 len / 2);
605 else
606 wpa_printf(MSG_ERROR, "DPP: Could not get EC group");
607
608 EC_KEY_free(eckey);
609 return pkey;
610 }
611
612
613 static int dpp_ecdh(EVP_PKEY *own, EVP_PKEY *peer,
614 u8 *secret, size_t *secret_len)
615 {
616 EVP_PKEY_CTX *ctx;
617 int ret = -1;
618
619 ERR_clear_error();
620 *secret_len = 0;
621
622 ctx = EVP_PKEY_CTX_new(own, NULL);
623 if (!ctx) {
624 wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_CTX_new failed: %s",
625 ERR_error_string(ERR_get_error(), NULL));
626 return -1;
627 }
628
629 if (EVP_PKEY_derive_init(ctx) != 1) {
630 wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_derive_init failed: %s",
631 ERR_error_string(ERR_get_error(), NULL));
632 goto fail;
633 }
634
635 if (EVP_PKEY_derive_set_peer(ctx, peer) != 1) {
636 wpa_printf(MSG_ERROR,
637 "DPP: EVP_PKEY_derive_set_peet failed: %s",
638 ERR_error_string(ERR_get_error(), NULL));
639 goto fail;
640 }
641
642 if (EVP_PKEY_derive(ctx, NULL, secret_len) != 1) {
643 wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_derive(NULL) failed: %s",
644 ERR_error_string(ERR_get_error(), NULL));
645 goto fail;
646 }
647
648 if (*secret_len > DPP_MAX_SHARED_SECRET_LEN) {
649 u8 buf[200];
650 int level = *secret_len > 200 ? MSG_ERROR : MSG_DEBUG;
651
652 /* It looks like OpenSSL can return unexpectedly large buffer
653 * need for shared secret from EVP_PKEY_derive(NULL) in some
654 * cases. For example, group 19 has shown cases where secret_len
655 * is set to 72 even though the actual length ends up being
656 * updated to 32 when EVP_PKEY_derive() is called with a buffer
657 * for the value. Work around this by trying to fetch the value
658 * and continue if it is within supported range even when the
659 * initial buffer need is claimed to be larger. */
660 wpa_printf(level,
661 "DPP: Unexpected secret_len=%d from EVP_PKEY_derive()",
662 (int) *secret_len);
663 if (*secret_len > 200)
664 goto fail;
665 if (EVP_PKEY_derive(ctx, buf, secret_len) != 1) {
666 wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_derive failed: %s",
667 ERR_error_string(ERR_get_error(), NULL));
668 goto fail;
669 }
670 if (*secret_len > DPP_MAX_SHARED_SECRET_LEN) {
671 wpa_printf(MSG_ERROR,
672 "DPP: Unexpected secret_len=%d from EVP_PKEY_derive()",
673 (int) *secret_len);
674 goto fail;
675 }
676 wpa_hexdump_key(MSG_DEBUG, "DPP: Unexpected secret_len change",
677 buf, *secret_len);
678 os_memcpy(secret, buf, *secret_len);
679 forced_memzero(buf, sizeof(buf));
680 goto done;
681 }
682
683 if (EVP_PKEY_derive(ctx, secret, secret_len) != 1) {
684 wpa_printf(MSG_ERROR, "DPP: EVP_PKEY_derive failed: %s",
685 ERR_error_string(ERR_get_error(), NULL));
686 goto fail;
687 }
688
689 done:
690 ret = 0;
691
692 fail:
693 EVP_PKEY_CTX_free(ctx);
694 return ret;
695 }
696
697
698 static void dpp_auth_fail(struct dpp_authentication *auth, const char *txt)
699 {
700 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
701 }
702
703
704 struct wpabuf * dpp_alloc_msg(enum dpp_public_action_frame_type type,
705 size_t len)
706 {
707 struct wpabuf *msg;
708
709 msg = wpabuf_alloc(8 + len);
710 if (!msg)
711 return NULL;
712 wpabuf_put_u8(msg, WLAN_ACTION_PUBLIC);
713 wpabuf_put_u8(msg, WLAN_PA_VENDOR_SPECIFIC);
714 wpabuf_put_be24(msg, OUI_WFA);
715 wpabuf_put_u8(msg, DPP_OUI_TYPE);
716 wpabuf_put_u8(msg, 1); /* Crypto Suite */
717 wpabuf_put_u8(msg, type);
718 return msg;
719 }
720
721
722 const u8 * dpp_get_attr(const u8 *buf, size_t len, u16 req_id, u16 *ret_len)
723 {
724 u16 id, alen;
725 const u8 *pos = buf, *end = buf + len;
726
727 while (end - pos >= 4) {
728 id = WPA_GET_LE16(pos);
729 pos += 2;
730 alen = WPA_GET_LE16(pos);
731 pos += 2;
732 if (alen > end - pos)
733 return NULL;
734 if (id == req_id) {
735 *ret_len = alen;
736 return pos;
737 }
738 pos += alen;
739 }
740
741 return NULL;
742 }
743
744
745 int dpp_check_attrs(const u8 *buf, size_t len)
746 {
747 const u8 *pos, *end;
748 int wrapped_data = 0;
749
750 pos = buf;
751 end = buf + len;
752 while (end - pos >= 4) {
753 u16 id, alen;
754
755 id = WPA_GET_LE16(pos);
756 pos += 2;
757 alen = WPA_GET_LE16(pos);
758 pos += 2;
759 wpa_printf(MSG_MSGDUMP, "DPP: Attribute ID %04x len %u",
760 id, alen);
761 if (alen > end - pos) {
762 wpa_printf(MSG_DEBUG,
763 "DPP: Truncated message - not enough room for the attribute - dropped");
764 return -1;
765 }
766 if (wrapped_data) {
767 wpa_printf(MSG_DEBUG,
768 "DPP: An unexpected attribute included after the Wrapped Data attribute");
769 return -1;
770 }
771 if (id == DPP_ATTR_WRAPPED_DATA)
772 wrapped_data = 1;
773 pos += alen;
774 }
775
776 if (end != pos) {
777 wpa_printf(MSG_DEBUG,
778 "DPP: Unexpected octets (%d) after the last attribute",
779 (int) (end - pos));
780 return -1;
781 }
782
783 return 0;
784 }
785
786
787 void dpp_bootstrap_info_free(struct dpp_bootstrap_info *info)
788 {
789 if (!info)
790 return;
791 os_free(info->uri);
792 os_free(info->info);
793 EVP_PKEY_free(info->pubkey);
794 os_free(info);
795 }
796
797
798 const char * dpp_bootstrap_type_txt(enum dpp_bootstrap_type type)
799 {
800 switch (type) {
801 case DPP_BOOTSTRAP_QR_CODE:
802 return "QRCODE";
803 case DPP_BOOTSTRAP_PKEX:
804 return "PKEX";
805 }
806 return "??";
807 }
808
809
810 static int dpp_uri_valid_info(const char *info)
811 {
812 while (*info) {
813 unsigned char val = *info++;
814
815 if (val < 0x20 || val > 0x7e || val == 0x3b)
816 return 0;
817 }
818
819 return 1;
820 }
821
822
823 static int dpp_clone_uri(struct dpp_bootstrap_info *bi, const char *uri)
824 {
825 bi->uri = os_strdup(uri);
826 return bi->uri ? 0 : -1;
827 }
828
829
830 int dpp_parse_uri_chan_list(struct dpp_bootstrap_info *bi,
831 const char *chan_list)
832 {
833 const char *pos = chan_list, *pos2;
834 int opclass = -1, channel, freq;
835
836 while (pos && *pos && *pos != ';') {
837 pos2 = pos;
838 while (*pos2 >= '0' && *pos2 <= '9')
839 pos2++;
840 if (*pos2 == '/') {
841 opclass = atoi(pos);
842 pos = pos2 + 1;
843 }
844 if (opclass <= 0)
845 goto fail;
846 channel = atoi(pos);
847 if (channel <= 0)
848 goto fail;
849 while (*pos >= '0' && *pos <= '9')
850 pos++;
851 freq = ieee80211_chan_to_freq(NULL, opclass, channel);
852 wpa_printf(MSG_DEBUG,
853 "DPP: URI channel-list: opclass=%d channel=%d ==> freq=%d",
854 opclass, channel, freq);
855 if (freq < 0) {
856 wpa_printf(MSG_DEBUG,
857 "DPP: Ignore unknown URI channel-list channel (opclass=%d channel=%d)",
858 opclass, channel);
859 } else if (bi->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
860 wpa_printf(MSG_DEBUG,
861 "DPP: Too many channels in URI channel-list - ignore list");
862 bi->num_freq = 0;
863 break;
864 } else {
865 bi->freq[bi->num_freq++] = freq;
866 }
867
868 if (*pos == ';' || *pos == '\0')
869 break;
870 if (*pos != ',')
871 goto fail;
872 pos++;
873 }
874
875 return 0;
876 fail:
877 wpa_printf(MSG_DEBUG, "DPP: Invalid URI channel-list");
878 return -1;
879 }
880
881
882 int dpp_parse_uri_mac(struct dpp_bootstrap_info *bi, const char *mac)
883 {
884 if (!mac)
885 return 0;
886
887 if (hwaddr_aton2(mac, bi->mac_addr) < 0) {
888 wpa_printf(MSG_DEBUG, "DPP: Invalid URI mac");
889 return -1;
890 }
891
892 wpa_printf(MSG_DEBUG, "DPP: URI mac: " MACSTR, MAC2STR(bi->mac_addr));
893
894 return 0;
895 }
896
897
898 int dpp_parse_uri_info(struct dpp_bootstrap_info *bi, const char *info)
899 {
900 const char *end;
901
902 if (!info)
903 return 0;
904
905 end = os_strchr(info, ';');
906 if (!end)
907 end = info + os_strlen(info);
908 bi->info = os_malloc(end - info + 1);
909 if (!bi->info)
910 return -1;
911 os_memcpy(bi->info, info, end - info);
912 bi->info[end - info] = '\0';
913 wpa_printf(MSG_DEBUG, "DPP: URI(information): %s", bi->info);
914 if (!dpp_uri_valid_info(bi->info)) {
915 wpa_printf(MSG_DEBUG, "DPP: Invalid URI information payload");
916 return -1;
917 }
918
919 return 0;
920 }
921
922
923 static const struct dpp_curve_params *
924 dpp_get_curve_oid(const ASN1_OBJECT *poid)
925 {
926 ASN1_OBJECT *oid;
927 int i;
928
929 for (i = 0; dpp_curves[i].name; i++) {
930 oid = OBJ_txt2obj(dpp_curves[i].name, 0);
931 if (oid && OBJ_cmp(poid, oid) == 0)
932 return &dpp_curves[i];
933 }
934 return NULL;
935 }
936
937
938 static const struct dpp_curve_params * dpp_get_curve_nid(int nid)
939 {
940 int i, tmp;
941
942 if (!nid)
943 return NULL;
944 for (i = 0; dpp_curves[i].name; i++) {
945 tmp = OBJ_txt2nid(dpp_curves[i].name);
946 if (tmp == nid)
947 return &dpp_curves[i];
948 }
949 return NULL;
950 }
951
952
953 static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
954 {
955 const char *end;
956 u8 *data;
957 size_t data_len;
958 EVP_PKEY *pkey;
959 const unsigned char *p;
960 int res;
961 X509_PUBKEY *pub = NULL;
962 ASN1_OBJECT *ppkalg;
963 const unsigned char *pk;
964 int ppklen;
965 X509_ALGOR *pa;
966 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
967 (defined(LIBRESSL_VERSION_NUMBER) && \
968 LIBRESSL_VERSION_NUMBER < 0x20800000L)
969 ASN1_OBJECT *pa_oid;
970 #else
971 const ASN1_OBJECT *pa_oid;
972 #endif
973 const void *pval;
974 int ptype;
975 const ASN1_OBJECT *poid;
976 char buf[100];
977
978 end = os_strchr(info, ';');
979 if (!end)
980 return -1;
981
982 data = base64_decode((const unsigned char *) info, end - info,
983 &data_len);
984 if (!data) {
985 wpa_printf(MSG_DEBUG,
986 "DPP: Invalid base64 encoding on URI public-key");
987 return -1;
988 }
989 wpa_hexdump(MSG_DEBUG, "DPP: Base64 decoded URI public-key",
990 data, data_len);
991
992 if (sha256_vector(1, (const u8 **) &data, &data_len,
993 bi->pubkey_hash) < 0) {
994 wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
995 os_free(data);
996 return -1;
997 }
998 wpa_hexdump(MSG_DEBUG, "DPP: Public key hash",
999 bi->pubkey_hash, SHA256_MAC_LEN);
1000
1001 /* DER encoded ASN.1 SubjectPublicKeyInfo
1002 *
1003 * SubjectPublicKeyInfo ::= SEQUENCE {
1004 * algorithm AlgorithmIdentifier,
1005 * subjectPublicKey BIT STRING }
1006 *
1007 * AlgorithmIdentifier ::= SEQUENCE {
1008 * algorithm OBJECT IDENTIFIER,
1009 * parameters ANY DEFINED BY algorithm OPTIONAL }
1010 *
1011 * subjectPublicKey = compressed format public key per ANSI X9.63
1012 * algorithm = ecPublicKey (1.2.840.10045.2.1)
1013 * parameters = shall be present and shall be OBJECT IDENTIFIER; e.g.,
1014 * prime256v1 (1.2.840.10045.3.1.7)
1015 */
1016
1017 p = data;
1018 pkey = d2i_PUBKEY(NULL, &p, data_len);
1019 os_free(data);
1020
1021 if (!pkey) {
1022 wpa_printf(MSG_DEBUG,
1023 "DPP: Could not parse URI public-key SubjectPublicKeyInfo");
1024 return -1;
1025 }
1026
1027 if (EVP_PKEY_type(EVP_PKEY_id(pkey)) != EVP_PKEY_EC) {
1028 wpa_printf(MSG_DEBUG,
1029 "DPP: SubjectPublicKeyInfo does not describe an EC key");
1030 EVP_PKEY_free(pkey);
1031 return -1;
1032 }
1033
1034 res = X509_PUBKEY_set(&pub, pkey);
1035 if (res != 1) {
1036 wpa_printf(MSG_DEBUG, "DPP: Could not set pubkey");
1037 goto fail;
1038 }
1039
1040 res = X509_PUBKEY_get0_param(&ppkalg, &pk, &ppklen, &pa, pub);
1041 if (res != 1) {
1042 wpa_printf(MSG_DEBUG,
1043 "DPP: Could not extract SubjectPublicKeyInfo parameters");
1044 goto fail;
1045 }
1046 res = OBJ_obj2txt(buf, sizeof(buf), ppkalg, 0);
1047 if (res < 0 || (size_t) res >= sizeof(buf)) {
1048 wpa_printf(MSG_DEBUG,
1049 "DPP: Could not extract SubjectPublicKeyInfo algorithm");
1050 goto fail;
1051 }
1052 wpa_printf(MSG_DEBUG, "DPP: URI subjectPublicKey algorithm: %s", buf);
1053 if (os_strcmp(buf, "id-ecPublicKey") != 0) {
1054 wpa_printf(MSG_DEBUG,
1055 "DPP: Unsupported SubjectPublicKeyInfo algorithm");
1056 goto fail;
1057 }
1058
1059 X509_ALGOR_get0(&pa_oid, &ptype, (void *) &pval, pa);
1060 if (ptype != V_ASN1_OBJECT) {
1061 wpa_printf(MSG_DEBUG,
1062 "DPP: SubjectPublicKeyInfo parameters did not contain an OID");
1063 goto fail;
1064 }
1065 poid = pval;
1066 res = OBJ_obj2txt(buf, sizeof(buf), poid, 0);
1067 if (res < 0 || (size_t) res >= sizeof(buf)) {
1068 wpa_printf(MSG_DEBUG,
1069 "DPP: Could not extract SubjectPublicKeyInfo parameters OID");
1070 goto fail;
1071 }
1072 wpa_printf(MSG_DEBUG, "DPP: URI subjectPublicKey parameters: %s", buf);
1073 bi->curve = dpp_get_curve_oid(poid);
1074 if (!bi->curve) {
1075 wpa_printf(MSG_DEBUG,
1076 "DPP: Unsupported SubjectPublicKeyInfo curve: %s",
1077 buf);
1078 goto fail;
1079 }
1080
1081 wpa_hexdump(MSG_DEBUG, "DPP: URI subjectPublicKey", pk, ppklen);
1082
1083 X509_PUBKEY_free(pub);
1084 bi->pubkey = pkey;
1085 return 0;
1086 fail:
1087 X509_PUBKEY_free(pub);
1088 EVP_PKEY_free(pkey);
1089 return -1;
1090 }
1091
1092
1093 static struct dpp_bootstrap_info * dpp_parse_uri(const char *uri)
1094 {
1095 const char *pos = uri;
1096 const char *end;
1097 const char *chan_list = NULL, *mac = NULL, *info = NULL, *pk = NULL;
1098 struct dpp_bootstrap_info *bi;
1099
1100 wpa_hexdump_ascii(MSG_DEBUG, "DPP: URI", uri, os_strlen(uri));
1101
1102 if (os_strncmp(pos, "DPP:", 4) != 0) {
1103 wpa_printf(MSG_INFO, "DPP: Not a DPP URI");
1104 return NULL;
1105 }
1106 pos += 4;
1107
1108 for (;;) {
1109 end = os_strchr(pos, ';');
1110 if (!end)
1111 break;
1112
1113 if (end == pos) {
1114 /* Handle terminating ";;" and ignore unexpected ";"
1115 * for parsing robustness. */
1116 pos++;
1117 continue;
1118 }
1119
1120 if (pos[0] == 'C' && pos[1] == ':' && !chan_list)
1121 chan_list = pos + 2;
1122 else if (pos[0] == 'M' && pos[1] == ':' && !mac)
1123 mac = pos + 2;
1124 else if (pos[0] == 'I' && pos[1] == ':' && !info)
1125 info = pos + 2;
1126 else if (pos[0] == 'K' && pos[1] == ':' && !pk)
1127 pk = pos + 2;
1128 else
1129 wpa_hexdump_ascii(MSG_DEBUG,
1130 "DPP: Ignore unrecognized URI parameter",
1131 pos, end - pos);
1132 pos = end + 1;
1133 }
1134
1135 if (!pk) {
1136 wpa_printf(MSG_INFO, "DPP: URI missing public-key");
1137 return NULL;
1138 }
1139
1140 bi = os_zalloc(sizeof(*bi));
1141 if (!bi)
1142 return NULL;
1143
1144 if (dpp_clone_uri(bi, uri) < 0 ||
1145 dpp_parse_uri_chan_list(bi, chan_list) < 0 ||
1146 dpp_parse_uri_mac(bi, mac) < 0 ||
1147 dpp_parse_uri_info(bi, info) < 0 ||
1148 dpp_parse_uri_pk(bi, pk) < 0) {
1149 dpp_bootstrap_info_free(bi);
1150 bi = NULL;
1151 }
1152
1153 return bi;
1154 }
1155
1156
1157 struct dpp_bootstrap_info * dpp_parse_qr_code(const char *uri)
1158 {
1159 struct dpp_bootstrap_info *bi;
1160
1161 bi = dpp_parse_uri(uri);
1162 if (bi)
1163 bi->type = DPP_BOOTSTRAP_QR_CODE;
1164 return bi;
1165 }
1166
1167
1168 static void dpp_debug_print_key(const char *title, EVP_PKEY *key)
1169 {
1170 EC_KEY *eckey;
1171 BIO *out;
1172 size_t rlen;
1173 char *txt;
1174 int res;
1175 unsigned char *der = NULL;
1176 int der_len;
1177 const EC_GROUP *group;
1178 const EC_POINT *point;
1179
1180 out = BIO_new(BIO_s_mem());
1181 if (!out)
1182 return;
1183
1184 EVP_PKEY_print_private(out, key, 0, NULL);
1185 rlen = BIO_ctrl_pending(out);
1186 txt = os_malloc(rlen + 1);
1187 if (txt) {
1188 res = BIO_read(out, txt, rlen);
1189 if (res > 0) {
1190 txt[res] = '\0';
1191 wpa_printf(MSG_DEBUG, "%s: %s", title, txt);
1192 }
1193 os_free(txt);
1194 }
1195 BIO_free(out);
1196
1197 eckey = EVP_PKEY_get1_EC_KEY(key);
1198 if (!eckey)
1199 return;
1200
1201 group = EC_KEY_get0_group(eckey);
1202 point = EC_KEY_get0_public_key(eckey);
1203 if (group && point)
1204 dpp_debug_print_point(title, group, point);
1205
1206 der_len = i2d_ECPrivateKey(eckey, &der);
1207 if (der_len > 0)
1208 wpa_hexdump_key(MSG_DEBUG, "DPP: ECPrivateKey", der, der_len);
1209 OPENSSL_free(der);
1210 if (der_len <= 0) {
1211 der = NULL;
1212 der_len = i2d_EC_PUBKEY(eckey, &der);
1213 if (der_len > 0)
1214 wpa_hexdump(MSG_DEBUG, "DPP: EC_PUBKEY", der, der_len);
1215 OPENSSL_free(der);
1216 }
1217
1218 EC_KEY_free(eckey);
1219 }
1220
1221
1222 static EVP_PKEY * dpp_gen_keypair(const struct dpp_curve_params *curve)
1223 {
1224 EVP_PKEY_CTX *kctx = NULL;
1225 EC_KEY *ec_params = NULL;
1226 EVP_PKEY *params = NULL, *key = NULL;
1227 int nid;
1228
1229 wpa_printf(MSG_DEBUG, "DPP: Generating a keypair");
1230
1231 nid = OBJ_txt2nid(curve->name);
1232 if (nid == NID_undef) {
1233 wpa_printf(MSG_INFO, "DPP: Unsupported curve %s", curve->name);
1234 return NULL;
1235 }
1236
1237 ec_params = EC_KEY_new_by_curve_name(nid);
1238 if (!ec_params) {
1239 wpa_printf(MSG_ERROR,
1240 "DPP: Failed to generate EC_KEY parameters");
1241 goto fail;
1242 }
1243 EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
1244 params = EVP_PKEY_new();
1245 if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
1246 wpa_printf(MSG_ERROR,
1247 "DPP: Failed to generate EVP_PKEY parameters");
1248 goto fail;
1249 }
1250
1251 kctx = EVP_PKEY_CTX_new(params, NULL);
1252 if (!kctx ||
1253 EVP_PKEY_keygen_init(kctx) != 1 ||
1254 EVP_PKEY_keygen(kctx, &key) != 1) {
1255 wpa_printf(MSG_ERROR, "DPP: Failed to generate EC key");
1256 key = NULL;
1257 goto fail;
1258 }
1259
1260 if (wpa_debug_show_keys)
1261 dpp_debug_print_key("Own generated key", key);
1262
1263 fail:
1264 EC_KEY_free(ec_params);
1265 EVP_PKEY_free(params);
1266 EVP_PKEY_CTX_free(kctx);
1267 return key;
1268 }
1269
1270
1271 static const struct dpp_curve_params *
1272 dpp_get_curve_name(const char *name)
1273 {
1274 int i;
1275
1276 for (i = 0; dpp_curves[i].name; i++) {
1277 if (os_strcmp(name, dpp_curves[i].name) == 0 ||
1278 (dpp_curves[i].jwk_crv &&
1279 os_strcmp(name, dpp_curves[i].jwk_crv) == 0))
1280 return &dpp_curves[i];
1281 }
1282 return NULL;
1283 }
1284
1285
1286 static const struct dpp_curve_params *
1287 dpp_get_curve_jwk_crv(const char *name)
1288 {
1289 int i;
1290
1291 for (i = 0; dpp_curves[i].name; i++) {
1292 if (dpp_curves[i].jwk_crv &&
1293 os_strcmp(name, dpp_curves[i].jwk_crv) == 0)
1294 return &dpp_curves[i];
1295 }
1296 return NULL;
1297 }
1298
1299
1300 static EVP_PKEY * dpp_set_keypair(const struct dpp_curve_params **curve,
1301 const u8 *privkey, size_t privkey_len)
1302 {
1303 EVP_PKEY *pkey;
1304 EC_KEY *eckey;
1305 const EC_GROUP *group;
1306 int nid;
1307
1308 pkey = EVP_PKEY_new();
1309 if (!pkey)
1310 return NULL;
1311 eckey = d2i_ECPrivateKey(NULL, &privkey, privkey_len);
1312 if (!eckey) {
1313 wpa_printf(MSG_INFO,
1314 "DPP: OpenSSL: d2i_ECPrivateKey() failed: %s",
1315 ERR_error_string(ERR_get_error(), NULL));
1316 EVP_PKEY_free(pkey);
1317 return NULL;
1318 }
1319 group = EC_KEY_get0_group(eckey);
1320 if (!group) {
1321 EC_KEY_free(eckey);
1322 EVP_PKEY_free(pkey);
1323 return NULL;
1324 }
1325 nid = EC_GROUP_get_curve_name(group);
1326 *curve = dpp_get_curve_nid(nid);
1327 if (!*curve) {
1328 wpa_printf(MSG_INFO,
1329 "DPP: Unsupported curve (nid=%d) in pre-assigned key",
1330 nid);
1331 EC_KEY_free(eckey);
1332 EVP_PKEY_free(pkey);
1333 return NULL;
1334 }
1335
1336 if (EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
1337 EC_KEY_free(eckey);
1338 EVP_PKEY_free(pkey);
1339 return NULL;
1340 }
1341 return pkey;
1342 }
1343
1344
1345 typedef struct {
1346 /* AlgorithmIdentifier ecPublicKey with optional parameters present
1347 * as an OID identifying the curve */
1348 X509_ALGOR *alg;
1349 /* Compressed format public key per ANSI X9.63 */
1350 ASN1_BIT_STRING *pub_key;
1351 } DPP_BOOTSTRAPPING_KEY;
1352
1353 ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
1354 ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
1355 ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
1356 } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY);
1357
1358 IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY);
1359
1360
1361 static struct wpabuf * dpp_bootstrap_key_der(EVP_PKEY *key)
1362 {
1363 unsigned char *der = NULL;
1364 int der_len;
1365 EC_KEY *eckey;
1366 struct wpabuf *ret = NULL;
1367 size_t len;
1368 const EC_GROUP *group;
1369 const EC_POINT *point;
1370 BN_CTX *ctx;
1371 DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
1372 int nid;
1373
1374 ctx = BN_CTX_new();
1375 eckey = EVP_PKEY_get1_EC_KEY(key);
1376 if (!ctx || !eckey)
1377 goto fail;
1378
1379 group = EC_KEY_get0_group(eckey);
1380 point = EC_KEY_get0_public_key(eckey);
1381 if (!group || !point)
1382 goto fail;
1383 dpp_debug_print_point("DPP: bootstrap public key", group, point);
1384 nid = EC_GROUP_get_curve_name(group);
1385
1386 bootstrap = DPP_BOOTSTRAPPING_KEY_new();
1387 if (!bootstrap ||
1388 X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
1389 V_ASN1_OBJECT, (void *) OBJ_nid2obj(nid)) != 1)
1390 goto fail;
1391
1392 len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
1393 NULL, 0, ctx);
1394 if (len == 0)
1395 goto fail;
1396
1397 der = OPENSSL_malloc(len);
1398 if (!der)
1399 goto fail;
1400 len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
1401 der, len, ctx);
1402
1403 OPENSSL_free(bootstrap->pub_key->data);
1404 bootstrap->pub_key->data = der;
1405 der = NULL;
1406 bootstrap->pub_key->length = len;
1407 /* No unused bits */
1408 bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
1409 bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
1410
1411 der_len = i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der);
1412 if (der_len <= 0) {
1413 wpa_printf(MSG_ERROR,
1414 "DDP: Failed to build DER encoded public key");
1415 goto fail;
1416 }
1417
1418 ret = wpabuf_alloc_copy(der, der_len);
1419 fail:
1420 DPP_BOOTSTRAPPING_KEY_free(bootstrap);
1421 OPENSSL_free(der);
1422 EC_KEY_free(eckey);
1423 BN_CTX_free(ctx);
1424 return ret;
1425 }
1426
1427
1428 int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi)
1429 {
1430 struct wpabuf *der;
1431 int res;
1432 const u8 *addr[1];
1433 size_t len[1];
1434
1435 der = dpp_bootstrap_key_der(bi->pubkey);
1436 if (!der)
1437 return -1;
1438 wpa_hexdump_buf(MSG_DEBUG, "DPP: Compressed public key (DER)",
1439 der);
1440
1441 addr[0] = wpabuf_head(der);
1442 len[0] = wpabuf_len(der);
1443 res = sha256_vector(1, addr, len, bi->pubkey_hash);
1444 if (res < 0)
1445 wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
1446 else
1447 wpa_hexdump(MSG_DEBUG, "DPP: Public key hash", bi->pubkey_hash,
1448 SHA256_MAC_LEN);
1449 wpabuf_free(der);
1450 return res;
1451 }
1452
1453
1454 char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
1455 const u8 *privkey, size_t privkey_len)
1456 {
1457 unsigned char *base64 = NULL;
1458 char *pos, *end;
1459 size_t len;
1460 struct wpabuf *der = NULL;
1461 const u8 *addr[1];
1462 int res;
1463
1464 if (!curve) {
1465 bi->curve = &dpp_curves[0];
1466 } else {
1467 bi->curve = dpp_get_curve_name(curve);
1468 if (!bi->curve) {
1469 wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
1470 curve);
1471 return NULL;
1472 }
1473 }
1474 if (privkey)
1475 bi->pubkey = dpp_set_keypair(&bi->curve, privkey, privkey_len);
1476 else
1477 bi->pubkey = dpp_gen_keypair(bi->curve);
1478 if (!bi->pubkey)
1479 goto fail;
1480 bi->own = 1;
1481
1482 der = dpp_bootstrap_key_der(bi->pubkey);
1483 if (!der)
1484 goto fail;
1485 wpa_hexdump_buf(MSG_DEBUG, "DPP: Compressed public key (DER)",
1486 der);
1487
1488 addr[0] = wpabuf_head(der);
1489 len = wpabuf_len(der);
1490 res = sha256_vector(1, addr, &len, bi->pubkey_hash);
1491 if (res < 0) {
1492 wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
1493 goto fail;
1494 }
1495 wpa_hexdump(MSG_DEBUG, "DPP: Public key hash", bi->pubkey_hash,
1496 SHA256_MAC_LEN);
1497
1498 base64 = base64_encode(wpabuf_head(der), wpabuf_len(der), &len);
1499 wpabuf_free(der);
1500 der = NULL;
1501 if (!base64)
1502 goto fail;
1503 pos = (char *) base64;
1504 end = pos + len;
1505 for (;;) {
1506 pos = os_strchr(pos, '\n');
1507 if (!pos)
1508 break;
1509 os_memmove(pos, pos + 1, end - pos);
1510 }
1511 return (char *) base64;
1512 fail:
1513 os_free(base64);
1514 wpabuf_free(der);
1515 return NULL;
1516 }
1517
1518
1519 static int dpp_derive_k1(const u8 *Mx, size_t Mx_len, u8 *k1,
1520 unsigned int hash_len)
1521 {
1522 u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
1523 const char *info = "first intermediate key";
1524 int res;
1525
1526 /* k1 = HKDF(<>, "first intermediate key", M.x) */
1527
1528 /* HKDF-Extract(<>, M.x) */
1529 os_memset(salt, 0, hash_len);
1530 if (dpp_hmac(hash_len, salt, hash_len, Mx, Mx_len, prk) < 0)
1531 return -1;
1532 wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=M.x)",
1533 prk, hash_len);
1534
1535 /* HKDF-Expand(PRK, info, L) */
1536 res = dpp_hkdf_expand(hash_len, prk, hash_len, info, k1, hash_len);
1537 os_memset(prk, 0, hash_len);
1538 if (res < 0)
1539 return -1;
1540
1541 wpa_hexdump_key(MSG_DEBUG, "DPP: k1 = HKDF-Expand(PRK, info, L)",
1542 k1, hash_len);
1543 return 0;
1544 }
1545
1546
1547 static int dpp_derive_k2(const u8 *Nx, size_t Nx_len, u8 *k2,
1548 unsigned int hash_len)
1549 {
1550 u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
1551 const char *info = "second intermediate key";
1552 int res;
1553
1554 /* k2 = HKDF(<>, "second intermediate key", N.x) */
1555
1556 /* HKDF-Extract(<>, N.x) */
1557 os_memset(salt, 0, hash_len);
1558 res = dpp_hmac(hash_len, salt, hash_len, Nx, Nx_len, prk);
1559 if (res < 0)
1560 return -1;
1561 wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=N.x)",
1562 prk, hash_len);
1563
1564 /* HKDF-Expand(PRK, info, L) */
1565 res = dpp_hkdf_expand(hash_len, prk, hash_len, info, k2, hash_len);
1566 os_memset(prk, 0, hash_len);
1567 if (res < 0)
1568 return -1;
1569
1570 wpa_hexdump_key(MSG_DEBUG, "DPP: k2 = HKDF-Expand(PRK, info, L)",
1571 k2, hash_len);
1572 return 0;
1573 }
1574
1575
1576 static int dpp_derive_ke(struct dpp_authentication *auth, u8 *ke,
1577 unsigned int hash_len)
1578 {
1579 size_t nonce_len;
1580 u8 nonces[2 * DPP_MAX_NONCE_LEN];
1581 const char *info_ke = "DPP Key";
1582 u8 prk[DPP_MAX_HASH_LEN];
1583 int res;
1584 const u8 *addr[3];
1585 size_t len[3];
1586 size_t num_elem = 0;
1587
1588 if (!auth->Mx_len || !auth->Nx_len) {
1589 wpa_printf(MSG_DEBUG,
1590 "DPP: Mx/Nx not available - cannot derive ke");
1591 return -1;
1592 }
1593
1594 /* ke = HKDF(I-nonce | R-nonce, "DPP Key", M.x | N.x [| L.x]) */
1595
1596 /* HKDF-Extract(I-nonce | R-nonce, M.x | N.x [| L.x]) */
1597 nonce_len = auth->curve->nonce_len;
1598 os_memcpy(nonces, auth->i_nonce, nonce_len);
1599 os_memcpy(&nonces[nonce_len], auth->r_nonce, nonce_len);
1600 addr[num_elem] = auth->Mx;
1601 len[num_elem] = auth->Mx_len;
1602 num_elem++;
1603 addr[num_elem] = auth->Nx;
1604 len[num_elem] = auth->Nx_len;
1605 num_elem++;
1606 if (auth->peer_bi && auth->own_bi) {
1607 if (!auth->Lx_len) {
1608 wpa_printf(MSG_DEBUG,
1609 "DPP: Lx not available - cannot derive ke");
1610 return -1;
1611 }
1612 addr[num_elem] = auth->Lx;
1613 len[num_elem] = auth->secret_len;
1614 num_elem++;
1615 }
1616 res = dpp_hmac_vector(hash_len, nonces, 2 * nonce_len,
1617 num_elem, addr, len, prk);
1618 if (res < 0)
1619 return -1;
1620 wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM)",
1621 prk, hash_len);
1622
1623 /* HKDF-Expand(PRK, info, L) */
1624 res = dpp_hkdf_expand(hash_len, prk, hash_len, info_ke, ke, hash_len);
1625 os_memset(prk, 0, hash_len);
1626 if (res < 0)
1627 return -1;
1628
1629 wpa_hexdump_key(MSG_DEBUG, "DPP: ke = HKDF-Expand(PRK, info, L)",
1630 ke, hash_len);
1631 return 0;
1632 }
1633
1634
1635 static void dpp_build_attr_status(struct wpabuf *msg,
1636 enum dpp_status_error status)
1637 {
1638 wpa_printf(MSG_DEBUG, "DPP: Status %d", status);
1639 wpabuf_put_le16(msg, DPP_ATTR_STATUS);
1640 wpabuf_put_le16(msg, 1);
1641 wpabuf_put_u8(msg, status);
1642 }
1643
1644
1645 static void dpp_build_attr_r_bootstrap_key_hash(struct wpabuf *msg,
1646 const u8 *hash)
1647 {
1648 if (hash) {
1649 wpa_printf(MSG_DEBUG, "DPP: R-Bootstrap Key Hash");
1650 wpabuf_put_le16(msg, DPP_ATTR_R_BOOTSTRAP_KEY_HASH);
1651 wpabuf_put_le16(msg, SHA256_MAC_LEN);
1652 wpabuf_put_data(msg, hash, SHA256_MAC_LEN);
1653 }
1654 }
1655
1656
1657 static void dpp_build_attr_i_bootstrap_key_hash(struct wpabuf *msg,
1658 const u8 *hash)
1659 {
1660 if (hash) {
1661 wpa_printf(MSG_DEBUG, "DPP: I-Bootstrap Key Hash");
1662 wpabuf_put_le16(msg, DPP_ATTR_I_BOOTSTRAP_KEY_HASH);
1663 wpabuf_put_le16(msg, SHA256_MAC_LEN);
1664 wpabuf_put_data(msg, hash, SHA256_MAC_LEN);
1665 }
1666 }
1667
1668
1669 static struct wpabuf * dpp_auth_build_req(struct dpp_authentication *auth,
1670 const struct wpabuf *pi,
1671 size_t nonce_len,
1672 const u8 *r_pubkey_hash,
1673 const u8 *i_pubkey_hash,
1674 unsigned int neg_freq)
1675 {
1676 struct wpabuf *msg;
1677 u8 clear[4 + DPP_MAX_NONCE_LEN + 4 + 1];
1678 u8 wrapped_data[4 + DPP_MAX_NONCE_LEN + 4 + 1 + AES_BLOCK_SIZE];
1679 u8 *pos;
1680 const u8 *addr[2];
1681 size_t len[2], siv_len, attr_len;
1682 u8 *attr_start, *attr_end;
1683
1684 /* Build DPP Authentication Request frame attributes */
1685 attr_len = 2 * (4 + SHA256_MAC_LEN) + 4 + (pi ? wpabuf_len(pi) : 0) +
1686 4 + sizeof(wrapped_data);
1687 if (neg_freq > 0)
1688 attr_len += 4 + 2;
1689 #ifdef CONFIG_DPP2
1690 attr_len += 5;
1691 #endif /* CONFIG_DPP2 */
1692 #ifdef CONFIG_TESTING_OPTIONS
1693 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_REQ)
1694 attr_len += 5;
1695 #endif /* CONFIG_TESTING_OPTIONS */
1696 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_REQ, attr_len);
1697 if (!msg)
1698 return NULL;
1699
1700 attr_start = wpabuf_put(msg, 0);
1701
1702 /* Responder Bootstrapping Key Hash */
1703 dpp_build_attr_r_bootstrap_key_hash(msg, r_pubkey_hash);
1704
1705 /* Initiator Bootstrapping Key Hash */
1706 dpp_build_attr_i_bootstrap_key_hash(msg, i_pubkey_hash);
1707
1708 /* Initiator Protocol Key */
1709 if (pi) {
1710 wpabuf_put_le16(msg, DPP_ATTR_I_PROTOCOL_KEY);
1711 wpabuf_put_le16(msg, wpabuf_len(pi));
1712 wpabuf_put_buf(msg, pi);
1713 }
1714
1715 /* Channel */
1716 if (neg_freq > 0) {
1717 u8 op_class, channel;
1718
1719 if (ieee80211_freq_to_channel_ext(neg_freq, 0, 0, &op_class,
1720 &channel) ==
1721 NUM_HOSTAPD_MODES) {
1722 wpa_printf(MSG_INFO,
1723 "DPP: Unsupported negotiation frequency request: %d",
1724 neg_freq);
1725 wpabuf_free(msg);
1726 return NULL;
1727 }
1728 wpabuf_put_le16(msg, DPP_ATTR_CHANNEL);
1729 wpabuf_put_le16(msg, 2);
1730 wpabuf_put_u8(msg, op_class);
1731 wpabuf_put_u8(msg, channel);
1732 }
1733
1734 #ifdef CONFIG_DPP2
1735 /* Protocol Version */
1736 wpabuf_put_le16(msg, DPP_ATTR_PROTOCOL_VERSION);
1737 wpabuf_put_le16(msg, 1);
1738 wpabuf_put_u8(msg, 2);
1739 #endif /* CONFIG_DPP2 */
1740
1741 #ifdef CONFIG_TESTING_OPTIONS
1742 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_REQ) {
1743 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
1744 goto skip_wrapped_data;
1745 }
1746 #endif /* CONFIG_TESTING_OPTIONS */
1747
1748 /* Wrapped data ({I-nonce, I-capabilities}k1) */
1749 pos = clear;
1750
1751 #ifdef CONFIG_TESTING_OPTIONS
1752 if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_REQ) {
1753 wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
1754 goto skip_i_nonce;
1755 }
1756 if (dpp_test == DPP_TEST_INVALID_I_NONCE_AUTH_REQ) {
1757 wpa_printf(MSG_INFO, "DPP: TESTING - invalid I-nonce");
1758 WPA_PUT_LE16(pos, DPP_ATTR_I_NONCE);
1759 pos += 2;
1760 WPA_PUT_LE16(pos, nonce_len - 1);
1761 pos += 2;
1762 os_memcpy(pos, auth->i_nonce, nonce_len - 1);
1763 pos += nonce_len - 1;
1764 goto skip_i_nonce;
1765 }
1766 #endif /* CONFIG_TESTING_OPTIONS */
1767
1768 /* I-nonce */
1769 WPA_PUT_LE16(pos, DPP_ATTR_I_NONCE);
1770 pos += 2;
1771 WPA_PUT_LE16(pos, nonce_len);
1772 pos += 2;
1773 os_memcpy(pos, auth->i_nonce, nonce_len);
1774 pos += nonce_len;
1775
1776 #ifdef CONFIG_TESTING_OPTIONS
1777 skip_i_nonce:
1778 if (dpp_test == DPP_TEST_NO_I_CAPAB_AUTH_REQ) {
1779 wpa_printf(MSG_INFO, "DPP: TESTING - no I-capab");
1780 goto skip_i_capab;
1781 }
1782 #endif /* CONFIG_TESTING_OPTIONS */
1783
1784 /* I-capabilities */
1785 WPA_PUT_LE16(pos, DPP_ATTR_I_CAPABILITIES);
1786 pos += 2;
1787 WPA_PUT_LE16(pos, 1);
1788 pos += 2;
1789 auth->i_capab = auth->allowed_roles;
1790 *pos++ = auth->i_capab;
1791 #ifdef CONFIG_TESTING_OPTIONS
1792 if (dpp_test == DPP_TEST_ZERO_I_CAPAB) {
1793 wpa_printf(MSG_INFO, "DPP: TESTING - zero I-capabilities");
1794 pos[-1] = 0;
1795 }
1796 skip_i_capab:
1797 #endif /* CONFIG_TESTING_OPTIONS */
1798
1799 attr_end = wpabuf_put(msg, 0);
1800
1801 /* OUI, OUI type, Crypto Suite, DPP frame type */
1802 addr[0] = wpabuf_head_u8(msg) + 2;
1803 len[0] = 3 + 1 + 1 + 1;
1804 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
1805
1806 /* Attributes before Wrapped Data */
1807 addr[1] = attr_start;
1808 len[1] = attr_end - attr_start;
1809 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
1810
1811 siv_len = pos - clear;
1812 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext", clear, siv_len);
1813 if (aes_siv_encrypt(auth->k1, auth->curve->hash_len, clear, siv_len,
1814 2, addr, len, wrapped_data) < 0) {
1815 wpabuf_free(msg);
1816 return NULL;
1817 }
1818 siv_len += AES_BLOCK_SIZE;
1819 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
1820 wrapped_data, siv_len);
1821
1822 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
1823 wpabuf_put_le16(msg, siv_len);
1824 wpabuf_put_data(msg, wrapped_data, siv_len);
1825
1826 #ifdef CONFIG_TESTING_OPTIONS
1827 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_REQ) {
1828 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
1829 dpp_build_attr_status(msg, DPP_STATUS_OK);
1830 }
1831 skip_wrapped_data:
1832 #endif /* CONFIG_TESTING_OPTIONS */
1833
1834 wpa_hexdump_buf(MSG_DEBUG,
1835 "DPP: Authentication Request frame attributes", msg);
1836
1837 return msg;
1838 }
1839
1840
1841 static struct wpabuf * dpp_auth_build_resp(struct dpp_authentication *auth,
1842 enum dpp_status_error status,
1843 const struct wpabuf *pr,
1844 size_t nonce_len,
1845 const u8 *r_pubkey_hash,
1846 const u8 *i_pubkey_hash,
1847 const u8 *r_nonce, const u8 *i_nonce,
1848 const u8 *wrapped_r_auth,
1849 size_t wrapped_r_auth_len,
1850 const u8 *siv_key)
1851 {
1852 struct wpabuf *msg;
1853 #define DPP_AUTH_RESP_CLEAR_LEN 2 * (4 + DPP_MAX_NONCE_LEN) + 4 + 1 + \
1854 4 + 4 + DPP_MAX_HASH_LEN + AES_BLOCK_SIZE
1855 u8 clear[DPP_AUTH_RESP_CLEAR_LEN];
1856 u8 wrapped_data[DPP_AUTH_RESP_CLEAR_LEN + AES_BLOCK_SIZE];
1857 const u8 *addr[2];
1858 size_t len[2], siv_len, attr_len;
1859 u8 *attr_start, *attr_end, *pos;
1860
1861 auth->waiting_auth_conf = 1;
1862 auth->auth_resp_tries = 0;
1863
1864 /* Build DPP Authentication Response frame attributes */
1865 attr_len = 4 + 1 + 2 * (4 + SHA256_MAC_LEN) +
1866 4 + (pr ? wpabuf_len(pr) : 0) + 4 + sizeof(wrapped_data);
1867 #ifdef CONFIG_DPP2
1868 attr_len += 5;
1869 #endif /* CONFIG_DPP2 */
1870 #ifdef CONFIG_TESTING_OPTIONS
1871 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_RESP)
1872 attr_len += 5;
1873 #endif /* CONFIG_TESTING_OPTIONS */
1874 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP, attr_len);
1875 if (!msg)
1876 return NULL;
1877
1878 attr_start = wpabuf_put(msg, 0);
1879
1880 /* DPP Status */
1881 if (status != 255)
1882 dpp_build_attr_status(msg, status);
1883
1884 /* Responder Bootstrapping Key Hash */
1885 dpp_build_attr_r_bootstrap_key_hash(msg, r_pubkey_hash);
1886
1887 /* Initiator Bootstrapping Key Hash (mutual authentication) */
1888 dpp_build_attr_i_bootstrap_key_hash(msg, i_pubkey_hash);
1889
1890 /* Responder Protocol Key */
1891 if (pr) {
1892 wpabuf_put_le16(msg, DPP_ATTR_R_PROTOCOL_KEY);
1893 wpabuf_put_le16(msg, wpabuf_len(pr));
1894 wpabuf_put_buf(msg, pr);
1895 }
1896
1897 #ifdef CONFIG_DPP2
1898 /* Protocol Version */
1899 wpabuf_put_le16(msg, DPP_ATTR_PROTOCOL_VERSION);
1900 wpabuf_put_le16(msg, 1);
1901 wpabuf_put_u8(msg, 2);
1902 #endif /* CONFIG_DPP2 */
1903
1904 attr_end = wpabuf_put(msg, 0);
1905
1906 #ifdef CONFIG_TESTING_OPTIONS
1907 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_RESP) {
1908 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
1909 goto skip_wrapped_data;
1910 }
1911 #endif /* CONFIG_TESTING_OPTIONS */
1912
1913 /* Wrapped data ({R-nonce, I-nonce, R-capabilities, {R-auth}ke}k2) */
1914 pos = clear;
1915
1916 if (r_nonce) {
1917 /* R-nonce */
1918 WPA_PUT_LE16(pos, DPP_ATTR_R_NONCE);
1919 pos += 2;
1920 WPA_PUT_LE16(pos, nonce_len);
1921 pos += 2;
1922 os_memcpy(pos, r_nonce, nonce_len);
1923 pos += nonce_len;
1924 }
1925
1926 if (i_nonce) {
1927 /* I-nonce */
1928 WPA_PUT_LE16(pos, DPP_ATTR_I_NONCE);
1929 pos += 2;
1930 WPA_PUT_LE16(pos, nonce_len);
1931 pos += 2;
1932 os_memcpy(pos, i_nonce, nonce_len);
1933 #ifdef CONFIG_TESTING_OPTIONS
1934 if (dpp_test == DPP_TEST_I_NONCE_MISMATCH_AUTH_RESP) {
1935 wpa_printf(MSG_INFO, "DPP: TESTING - I-nonce mismatch");
1936 pos[nonce_len / 2] ^= 0x01;
1937 }
1938 #endif /* CONFIG_TESTING_OPTIONS */
1939 pos += nonce_len;
1940 }
1941
1942 #ifdef CONFIG_TESTING_OPTIONS
1943 if (dpp_test == DPP_TEST_NO_R_CAPAB_AUTH_RESP) {
1944 wpa_printf(MSG_INFO, "DPP: TESTING - no R-capab");
1945 goto skip_r_capab;
1946 }
1947 #endif /* CONFIG_TESTING_OPTIONS */
1948
1949 /* R-capabilities */
1950 WPA_PUT_LE16(pos, DPP_ATTR_R_CAPABILITIES);
1951 pos += 2;
1952 WPA_PUT_LE16(pos, 1);
1953 pos += 2;
1954 auth->r_capab = auth->configurator ? DPP_CAPAB_CONFIGURATOR :
1955 DPP_CAPAB_ENROLLEE;
1956 *pos++ = auth->r_capab;
1957 #ifdef CONFIG_TESTING_OPTIONS
1958 if (dpp_test == DPP_TEST_ZERO_R_CAPAB) {
1959 wpa_printf(MSG_INFO, "DPP: TESTING - zero R-capabilities");
1960 pos[-1] = 0;
1961 } else if (dpp_test == DPP_TEST_INCOMPATIBLE_R_CAPAB_AUTH_RESP) {
1962 wpa_printf(MSG_INFO,
1963 "DPP: TESTING - incompatible R-capabilities");
1964 if ((auth->i_capab & DPP_CAPAB_ROLE_MASK) ==
1965 (DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE))
1966 pos[-1] = 0;
1967 else
1968 pos[-1] = auth->configurator ? DPP_CAPAB_ENROLLEE :
1969 DPP_CAPAB_CONFIGURATOR;
1970 }
1971 skip_r_capab:
1972 #endif /* CONFIG_TESTING_OPTIONS */
1973
1974 if (wrapped_r_auth) {
1975 /* {R-auth}ke */
1976 WPA_PUT_LE16(pos, DPP_ATTR_WRAPPED_DATA);
1977 pos += 2;
1978 WPA_PUT_LE16(pos, wrapped_r_auth_len);
1979 pos += 2;
1980 os_memcpy(pos, wrapped_r_auth, wrapped_r_auth_len);
1981 pos += wrapped_r_auth_len;
1982 }
1983
1984 /* OUI, OUI type, Crypto Suite, DPP frame type */
1985 addr[0] = wpabuf_head_u8(msg) + 2;
1986 len[0] = 3 + 1 + 1 + 1;
1987 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
1988
1989 /* Attributes before Wrapped Data */
1990 addr[1] = attr_start;
1991 len[1] = attr_end - attr_start;
1992 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
1993
1994 siv_len = pos - clear;
1995 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext", clear, siv_len);
1996 if (aes_siv_encrypt(siv_key, auth->curve->hash_len, clear, siv_len,
1997 2, addr, len, wrapped_data) < 0) {
1998 wpabuf_free(msg);
1999 return NULL;
2000 }
2001 siv_len += AES_BLOCK_SIZE;
2002 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
2003 wrapped_data, siv_len);
2004
2005 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
2006 wpabuf_put_le16(msg, siv_len);
2007 wpabuf_put_data(msg, wrapped_data, siv_len);
2008
2009 #ifdef CONFIG_TESTING_OPTIONS
2010 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_RESP) {
2011 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
2012 dpp_build_attr_status(msg, DPP_STATUS_OK);
2013 }
2014 skip_wrapped_data:
2015 #endif /* CONFIG_TESTING_OPTIONS */
2016
2017 wpa_hexdump_buf(MSG_DEBUG,
2018 "DPP: Authentication Response frame attributes", msg);
2019 return msg;
2020 }
2021
2022
2023 static int dpp_channel_ok_init(struct hostapd_hw_modes *own_modes,
2024 u16 num_modes, unsigned int freq)
2025 {
2026 u16 m;
2027 int c, flag;
2028
2029 if (!own_modes || !num_modes)
2030 return 1;
2031
2032 for (m = 0; m < num_modes; m++) {
2033 for (c = 0; c < own_modes[m].num_channels; c++) {
2034 if ((unsigned int) own_modes[m].channels[c].freq !=
2035 freq)
2036 continue;
2037 flag = own_modes[m].channels[c].flag;
2038 if (!(flag & (HOSTAPD_CHAN_DISABLED |
2039 HOSTAPD_CHAN_NO_IR |
2040 HOSTAPD_CHAN_RADAR)))
2041 return 1;
2042 }
2043 }
2044
2045 wpa_printf(MSG_DEBUG, "DPP: Peer channel %u MHz not supported", freq);
2046 return 0;
2047 }
2048
2049
2050 static int freq_included(const unsigned int freqs[], unsigned int num,
2051 unsigned int freq)
2052 {
2053 while (num > 0) {
2054 if (freqs[--num] == freq)
2055 return 1;
2056 }
2057 return 0;
2058 }
2059
2060
2061 static void freq_to_start(unsigned int freqs[], unsigned int num,
2062 unsigned int freq)
2063 {
2064 unsigned int i;
2065
2066 for (i = 0; i < num; i++) {
2067 if (freqs[i] == freq)
2068 break;
2069 }
2070 if (i == 0 || i >= num)
2071 return;
2072 os_memmove(&freqs[1], &freqs[0], i * sizeof(freqs[0]));
2073 freqs[0] = freq;
2074 }
2075
2076
2077 static int dpp_channel_intersect(struct dpp_authentication *auth,
2078 struct hostapd_hw_modes *own_modes,
2079 u16 num_modes)
2080 {
2081 struct dpp_bootstrap_info *peer_bi = auth->peer_bi;
2082 unsigned int i, freq;
2083
2084 for (i = 0; i < peer_bi->num_freq; i++) {
2085 freq = peer_bi->freq[i];
2086 if (freq_included(auth->freq, auth->num_freq, freq))
2087 continue;
2088 if (dpp_channel_ok_init(own_modes, num_modes, freq))
2089 auth->freq[auth->num_freq++] = freq;
2090 }
2091 if (!auth->num_freq) {
2092 wpa_printf(MSG_INFO,
2093 "DPP: No available channels for initiating DPP Authentication");
2094 return -1;
2095 }
2096 auth->curr_freq = auth->freq[0];
2097 return 0;
2098 }
2099
2100
2101 static int dpp_channel_local_list(struct dpp_authentication *auth,
2102 struct hostapd_hw_modes *own_modes,
2103 u16 num_modes)
2104 {
2105 u16 m;
2106 int c, flag;
2107 unsigned int freq;
2108
2109 auth->num_freq = 0;
2110
2111 if (!own_modes || !num_modes) {
2112 auth->freq[0] = 2412;
2113 auth->freq[1] = 2437;
2114 auth->freq[2] = 2462;
2115 auth->num_freq = 3;
2116 return 0;
2117 }
2118
2119 for (m = 0; m < num_modes; m++) {
2120 for (c = 0; c < own_modes[m].num_channels; c++) {
2121 freq = own_modes[m].channels[c].freq;
2122 flag = own_modes[m].channels[c].flag;
2123 if (flag & (HOSTAPD_CHAN_DISABLED |
2124 HOSTAPD_CHAN_NO_IR |
2125 HOSTAPD_CHAN_RADAR))
2126 continue;
2127 if (freq_included(auth->freq, auth->num_freq, freq))
2128 continue;
2129 auth->freq[auth->num_freq++] = freq;
2130 if (auth->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
2131 m = num_modes;
2132 break;
2133 }
2134 }
2135 }
2136
2137 return auth->num_freq == 0 ? -1 : 0;
2138 }
2139
2140
2141 static int dpp_prepare_channel_list(struct dpp_authentication *auth,
2142 struct hostapd_hw_modes *own_modes,
2143 u16 num_modes)
2144 {
2145 int res;
2146 char freqs[DPP_BOOTSTRAP_MAX_FREQ * 6 + 10], *pos, *end;
2147 unsigned int i;
2148
2149 if (auth->peer_bi->num_freq > 0)
2150 res = dpp_channel_intersect(auth, own_modes, num_modes);
2151 else
2152 res = dpp_channel_local_list(auth, own_modes, num_modes);
2153 if (res < 0)
2154 return res;
2155
2156 /* Prioritize 2.4 GHz channels 6, 1, 11 (in this order) to hit the most
2157 * likely channels first. */
2158 freq_to_start(auth->freq, auth->num_freq, 2462);
2159 freq_to_start(auth->freq, auth->num_freq, 2412);
2160 freq_to_start(auth->freq, auth->num_freq, 2437);
2161
2162 auth->freq_idx = 0;
2163 auth->curr_freq = auth->freq[0];
2164
2165 pos = freqs;
2166 end = pos + sizeof(freqs);
2167 for (i = 0; i < auth->num_freq; i++) {
2168 res = os_snprintf(pos, end - pos, " %u", auth->freq[i]);
2169 if (os_snprintf_error(end - pos, res))
2170 break;
2171 pos += res;
2172 }
2173 *pos = '\0';
2174 wpa_printf(MSG_DEBUG, "DPP: Possible frequencies for initiating:%s",
2175 freqs);
2176
2177 return 0;
2178 }
2179
2180
2181 static int dpp_autogen_bootstrap_key(struct dpp_authentication *auth)
2182 {
2183 struct dpp_bootstrap_info *bi;
2184 char *pk = NULL;
2185 size_t len;
2186
2187 if (auth->own_bi)
2188 return 0; /* already generated */
2189
2190 bi = os_zalloc(sizeof(*bi));
2191 if (!bi)
2192 return -1;
2193 bi->type = DPP_BOOTSTRAP_QR_CODE;
2194 pk = dpp_keygen(bi, auth->peer_bi->curve->name, NULL, 0);
2195 if (!pk)
2196 goto fail;
2197
2198 len = 4; /* "DPP:" */
2199 len += 4 + os_strlen(pk);
2200 bi->uri = os_malloc(len + 1);
2201 if (!bi->uri)
2202 goto fail;
2203 os_snprintf(bi->uri, len + 1, "DPP:K:%s;;", pk);
2204 wpa_printf(MSG_DEBUG,
2205 "DPP: Auto-generated own bootstrapping key info: URI %s",
2206 bi->uri);
2207
2208 auth->tmp_own_bi = auth->own_bi = bi;
2209
2210 os_free(pk);
2211
2212 return 0;
2213 fail:
2214 os_free(pk);
2215 dpp_bootstrap_info_free(bi);
2216 return -1;
2217 }
2218
2219
2220 struct dpp_authentication * dpp_auth_init(void *msg_ctx,
2221 struct dpp_bootstrap_info *peer_bi,
2222 struct dpp_bootstrap_info *own_bi,
2223 u8 dpp_allowed_roles,
2224 unsigned int neg_freq,
2225 struct hostapd_hw_modes *own_modes,
2226 u16 num_modes)
2227 {
2228 struct dpp_authentication *auth;
2229 size_t nonce_len;
2230 size_t secret_len;
2231 struct wpabuf *pi = NULL;
2232 const u8 *r_pubkey_hash, *i_pubkey_hash;
2233 #ifdef CONFIG_TESTING_OPTIONS
2234 u8 test_hash[SHA256_MAC_LEN];
2235 #endif /* CONFIG_TESTING_OPTIONS */
2236
2237 auth = os_zalloc(sizeof(*auth));
2238 if (!auth)
2239 return NULL;
2240 auth->msg_ctx = msg_ctx;
2241 auth->initiator = 1;
2242 auth->waiting_auth_resp = 1;
2243 auth->allowed_roles = dpp_allowed_roles;
2244 auth->configurator = !!(dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR);
2245 auth->peer_bi = peer_bi;
2246 auth->own_bi = own_bi;
2247 auth->curve = peer_bi->curve;
2248
2249 if (dpp_autogen_bootstrap_key(auth) < 0 ||
2250 dpp_prepare_channel_list(auth, own_modes, num_modes) < 0)
2251 goto fail;
2252
2253 #ifdef CONFIG_TESTING_OPTIONS
2254 if (dpp_nonce_override_len > 0) {
2255 wpa_printf(MSG_INFO, "DPP: TESTING - override I-nonce");
2256 nonce_len = dpp_nonce_override_len;
2257 os_memcpy(auth->i_nonce, dpp_nonce_override, nonce_len);
2258 } else {
2259 nonce_len = auth->curve->nonce_len;
2260 if (random_get_bytes(auth->i_nonce, nonce_len)) {
2261 wpa_printf(MSG_ERROR,
2262 "DPP: Failed to generate I-nonce");
2263 goto fail;
2264 }
2265 }
2266 #else /* CONFIG_TESTING_OPTIONS */
2267 nonce_len = auth->curve->nonce_len;
2268 if (random_get_bytes(auth->i_nonce, nonce_len)) {
2269 wpa_printf(MSG_ERROR, "DPP: Failed to generate I-nonce");
2270 goto fail;
2271 }
2272 #endif /* CONFIG_TESTING_OPTIONS */
2273 wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", auth->i_nonce, nonce_len);
2274
2275 #ifdef CONFIG_TESTING_OPTIONS
2276 if (dpp_protocol_key_override_len) {
2277 const struct dpp_curve_params *tmp_curve;
2278
2279 wpa_printf(MSG_INFO,
2280 "DPP: TESTING - override protocol key");
2281 auth->own_protocol_key = dpp_set_keypair(
2282 &tmp_curve, dpp_protocol_key_override,
2283 dpp_protocol_key_override_len);
2284 } else {
2285 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
2286 }
2287 #else /* CONFIG_TESTING_OPTIONS */
2288 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
2289 #endif /* CONFIG_TESTING_OPTIONS */
2290 if (!auth->own_protocol_key)
2291 goto fail;
2292
2293 pi = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2294 if (!pi)
2295 goto fail;
2296
2297 /* ECDH: M = pI * BR */
2298 if (dpp_ecdh(auth->own_protocol_key, auth->peer_bi->pubkey,
2299 auth->Mx, &secret_len) < 0)
2300 goto fail;
2301 auth->secret_len = secret_len;
2302
2303 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (M.x)",
2304 auth->Mx, auth->secret_len);
2305 auth->Mx_len = auth->secret_len;
2306
2307 if (dpp_derive_k1(auth->Mx, auth->secret_len, auth->k1,
2308 auth->curve->hash_len) < 0)
2309 goto fail;
2310
2311 r_pubkey_hash = auth->peer_bi->pubkey_hash;
2312 i_pubkey_hash = auth->own_bi->pubkey_hash;
2313
2314 #ifdef CONFIG_TESTING_OPTIONS
2315 if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
2316 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
2317 r_pubkey_hash = NULL;
2318 } else if (dpp_test == DPP_TEST_INVALID_R_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
2319 wpa_printf(MSG_INFO,
2320 "DPP: TESTING - invalid R-Bootstrap Key Hash");
2321 os_memcpy(test_hash, r_pubkey_hash, SHA256_MAC_LEN);
2322 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
2323 r_pubkey_hash = test_hash;
2324 } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
2325 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
2326 i_pubkey_hash = NULL;
2327 } else if (dpp_test == DPP_TEST_INVALID_I_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
2328 wpa_printf(MSG_INFO,
2329 "DPP: TESTING - invalid I-Bootstrap Key Hash");
2330 os_memcpy(test_hash, i_pubkey_hash, SHA256_MAC_LEN);
2331 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
2332 i_pubkey_hash = test_hash;
2333 } else if (dpp_test == DPP_TEST_NO_I_PROTO_KEY_AUTH_REQ) {
2334 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Proto Key");
2335 wpabuf_free(pi);
2336 pi = NULL;
2337 } else if (dpp_test == DPP_TEST_INVALID_I_PROTO_KEY_AUTH_REQ) {
2338 wpa_printf(MSG_INFO, "DPP: TESTING - invalid I-Proto Key");
2339 wpabuf_free(pi);
2340 pi = wpabuf_alloc(2 * auth->curve->prime_len);
2341 if (!pi || dpp_test_gen_invalid_key(pi, auth->curve) < 0)
2342 goto fail;
2343 }
2344 #endif /* CONFIG_TESTING_OPTIONS */
2345
2346 auth->req_msg = dpp_auth_build_req(auth, pi, nonce_len, r_pubkey_hash,
2347 i_pubkey_hash, neg_freq);
2348 if (!auth->req_msg)
2349 goto fail;
2350
2351 out:
2352 wpabuf_free(pi);
2353 return auth;
2354 fail:
2355 dpp_auth_deinit(auth);
2356 auth = NULL;
2357 goto out;
2358 }
2359
2360
2361 static struct wpabuf * dpp_build_conf_req_attr(struct dpp_authentication *auth,
2362 const char *json)
2363 {
2364 size_t nonce_len;
2365 size_t json_len, clear_len;
2366 struct wpabuf *clear = NULL, *msg = NULL;
2367 u8 *wrapped;
2368 size_t attr_len;
2369
2370 wpa_printf(MSG_DEBUG, "DPP: Build configuration request");
2371
2372 nonce_len = auth->curve->nonce_len;
2373 if (random_get_bytes(auth->e_nonce, nonce_len)) {
2374 wpa_printf(MSG_ERROR, "DPP: Failed to generate E-nonce");
2375 goto fail;
2376 }
2377 wpa_hexdump(MSG_DEBUG, "DPP: E-nonce", auth->e_nonce, nonce_len);
2378 json_len = os_strlen(json);
2379 wpa_hexdump_ascii(MSG_DEBUG, "DPP: configAttr JSON", json, json_len);
2380
2381 /* { E-nonce, configAttrib }ke */
2382 clear_len = 4 + nonce_len + 4 + json_len;
2383 clear = wpabuf_alloc(clear_len);
2384 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
2385 #ifdef CONFIG_TESTING_OPTIONS
2386 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ)
2387 attr_len += 5;
2388 #endif /* CONFIG_TESTING_OPTIONS */
2389 msg = wpabuf_alloc(attr_len);
2390 if (!clear || !msg)
2391 goto fail;
2392
2393 #ifdef CONFIG_TESTING_OPTIONS
2394 if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_REQ) {
2395 wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
2396 goto skip_e_nonce;
2397 }
2398 if (dpp_test == DPP_TEST_INVALID_E_NONCE_CONF_REQ) {
2399 wpa_printf(MSG_INFO, "DPP: TESTING - invalid E-nonce");
2400 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
2401 wpabuf_put_le16(clear, nonce_len - 1);
2402 wpabuf_put_data(clear, auth->e_nonce, nonce_len - 1);
2403 goto skip_e_nonce;
2404 }
2405 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_REQ) {
2406 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
2407 goto skip_wrapped_data;
2408 }
2409 #endif /* CONFIG_TESTING_OPTIONS */
2410
2411 /* E-nonce */
2412 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
2413 wpabuf_put_le16(clear, nonce_len);
2414 wpabuf_put_data(clear, auth->e_nonce, nonce_len);
2415
2416 #ifdef CONFIG_TESTING_OPTIONS
2417 skip_e_nonce:
2418 if (dpp_test == DPP_TEST_NO_CONFIG_ATTR_OBJ_CONF_REQ) {
2419 wpa_printf(MSG_INFO, "DPP: TESTING - no configAttrib");
2420 goto skip_conf_attr_obj;
2421 }
2422 #endif /* CONFIG_TESTING_OPTIONS */
2423
2424 /* configAttrib */
2425 wpabuf_put_le16(clear, DPP_ATTR_CONFIG_ATTR_OBJ);
2426 wpabuf_put_le16(clear, json_len);
2427 wpabuf_put_data(clear, json, json_len);
2428
2429 #ifdef CONFIG_TESTING_OPTIONS
2430 skip_conf_attr_obj:
2431 #endif /* CONFIG_TESTING_OPTIONS */
2432
2433 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
2434 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
2435 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
2436
2437 /* No AES-SIV AD */
2438 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
2439 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
2440 wpabuf_head(clear), wpabuf_len(clear),
2441 0, NULL, NULL, wrapped) < 0)
2442 goto fail;
2443 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
2444 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
2445
2446 #ifdef CONFIG_TESTING_OPTIONS
2447 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ) {
2448 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
2449 dpp_build_attr_status(msg, DPP_STATUS_OK);
2450 }
2451 skip_wrapped_data:
2452 #endif /* CONFIG_TESTING_OPTIONS */
2453
2454 wpa_hexdump_buf(MSG_DEBUG,
2455 "DPP: Configuration Request frame attributes", msg);
2456 wpabuf_free(clear);
2457 return msg;
2458
2459 fail:
2460 wpabuf_free(clear);
2461 wpabuf_free(msg);
2462 return NULL;
2463 }
2464
2465
2466 static void dpp_write_adv_proto(struct wpabuf *buf)
2467 {
2468 /* Advertisement Protocol IE */
2469 wpabuf_put_u8(buf, WLAN_EID_ADV_PROTO);
2470 wpabuf_put_u8(buf, 8); /* Length */
2471 wpabuf_put_u8(buf, 0x7f);
2472 wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
2473 wpabuf_put_u8(buf, 5);
2474 wpabuf_put_be24(buf, OUI_WFA);
2475 wpabuf_put_u8(buf, DPP_OUI_TYPE);
2476 wpabuf_put_u8(buf, 0x01);
2477 }
2478
2479
2480 static void dpp_write_gas_query(struct wpabuf *buf, struct wpabuf *query)
2481 {
2482 /* GAS Query */
2483 wpabuf_put_le16(buf, wpabuf_len(query));
2484 wpabuf_put_buf(buf, query);
2485 }
2486
2487
2488 struct wpabuf * dpp_build_conf_req(struct dpp_authentication *auth,
2489 const char *json)
2490 {
2491 struct wpabuf *buf, *conf_req;
2492
2493 conf_req = dpp_build_conf_req_attr(auth, json);
2494 if (!conf_req) {
2495 wpa_printf(MSG_DEBUG,
2496 "DPP: No configuration request data available");
2497 return NULL;
2498 }
2499
2500 buf = gas_build_initial_req(0, 10 + 2 + wpabuf_len(conf_req));
2501 if (!buf) {
2502 wpabuf_free(conf_req);
2503 return NULL;
2504 }
2505
2506 dpp_write_adv_proto(buf);
2507 dpp_write_gas_query(buf, conf_req);
2508 wpabuf_free(conf_req);
2509 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: GAS Config Request", buf);
2510
2511 return buf;
2512 }
2513
2514
2515 static void dpp_auth_success(struct dpp_authentication *auth)
2516 {
2517 wpa_printf(MSG_DEBUG,
2518 "DPP: Authentication success - clear temporary keys");
2519 os_memset(auth->Mx, 0, sizeof(auth->Mx));
2520 auth->Mx_len = 0;
2521 os_memset(auth->Nx, 0, sizeof(auth->Nx));
2522 auth->Nx_len = 0;
2523 os_memset(auth->Lx, 0, sizeof(auth->Lx));
2524 auth->Lx_len = 0;
2525 os_memset(auth->k1, 0, sizeof(auth->k1));
2526 os_memset(auth->k2, 0, sizeof(auth->k2));
2527
2528 auth->auth_success = 1;
2529 }
2530
2531
2532 static int dpp_gen_r_auth(struct dpp_authentication *auth, u8 *r_auth)
2533 {
2534 struct wpabuf *pix, *prx, *bix, *brx;
2535 const u8 *addr[7];
2536 size_t len[7];
2537 size_t i, num_elem = 0;
2538 size_t nonce_len;
2539 u8 zero = 0;
2540 int res = -1;
2541
2542 /* R-auth = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
2543 nonce_len = auth->curve->nonce_len;
2544
2545 if (auth->initiator) {
2546 pix = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2547 prx = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
2548 if (auth->own_bi)
2549 bix = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
2550 else
2551 bix = NULL;
2552 brx = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
2553 } else {
2554 pix = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
2555 prx = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2556 if (auth->peer_bi)
2557 bix = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
2558 else
2559 bix = NULL;
2560 brx = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
2561 }
2562 if (!pix || !prx || !brx)
2563 goto fail;
2564
2565 addr[num_elem] = auth->i_nonce;
2566 len[num_elem] = nonce_len;
2567 num_elem++;
2568
2569 addr[num_elem] = auth->r_nonce;
2570 len[num_elem] = nonce_len;
2571 num_elem++;
2572
2573 addr[num_elem] = wpabuf_head(pix);
2574 len[num_elem] = wpabuf_len(pix) / 2;
2575 num_elem++;
2576
2577 addr[num_elem] = wpabuf_head(prx);
2578 len[num_elem] = wpabuf_len(prx) / 2;
2579 num_elem++;
2580
2581 if (bix) {
2582 addr[num_elem] = wpabuf_head(bix);
2583 len[num_elem] = wpabuf_len(bix) / 2;
2584 num_elem++;
2585 }
2586
2587 addr[num_elem] = wpabuf_head(brx);
2588 len[num_elem] = wpabuf_len(brx) / 2;
2589 num_elem++;
2590
2591 addr[num_elem] = &zero;
2592 len[num_elem] = 1;
2593 num_elem++;
2594
2595 wpa_printf(MSG_DEBUG, "DPP: R-auth hash components");
2596 for (i = 0; i < num_elem; i++)
2597 wpa_hexdump(MSG_DEBUG, "DPP: hash component", addr[i], len[i]);
2598 res = dpp_hash_vector(auth->curve, num_elem, addr, len, r_auth);
2599 if (res == 0)
2600 wpa_hexdump(MSG_DEBUG, "DPP: R-auth", r_auth,
2601 auth->curve->hash_len);
2602 fail:
2603 wpabuf_free(pix);
2604 wpabuf_free(prx);
2605 wpabuf_free(bix);
2606 wpabuf_free(brx);
2607 return res;
2608 }
2609
2610
2611 static int dpp_gen_i_auth(struct dpp_authentication *auth, u8 *i_auth)
2612 {
2613 struct wpabuf *pix = NULL, *prx = NULL, *bix = NULL, *brx = NULL;
2614 const u8 *addr[7];
2615 size_t len[7];
2616 size_t i, num_elem = 0;
2617 size_t nonce_len;
2618 u8 one = 1;
2619 int res = -1;
2620
2621 /* I-auth = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |] 1) */
2622 nonce_len = auth->curve->nonce_len;
2623
2624 if (auth->initiator) {
2625 pix = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2626 prx = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
2627 if (auth->own_bi)
2628 bix = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
2629 else
2630 bix = NULL;
2631 if (!auth->peer_bi)
2632 goto fail;
2633 brx = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
2634 } else {
2635 pix = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
2636 prx = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2637 if (auth->peer_bi)
2638 bix = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
2639 else
2640 bix = NULL;
2641 if (!auth->own_bi)
2642 goto fail;
2643 brx = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
2644 }
2645 if (!pix || !prx || !brx)
2646 goto fail;
2647
2648 addr[num_elem] = auth->r_nonce;
2649 len[num_elem] = nonce_len;
2650 num_elem++;
2651
2652 addr[num_elem] = auth->i_nonce;
2653 len[num_elem] = nonce_len;
2654 num_elem++;
2655
2656 addr[num_elem] = wpabuf_head(prx);
2657 len[num_elem] = wpabuf_len(prx) / 2;
2658 num_elem++;
2659
2660 addr[num_elem] = wpabuf_head(pix);
2661 len[num_elem] = wpabuf_len(pix) / 2;
2662 num_elem++;
2663
2664 addr[num_elem] = wpabuf_head(brx);
2665 len[num_elem] = wpabuf_len(brx) / 2;
2666 num_elem++;
2667
2668 if (bix) {
2669 addr[num_elem] = wpabuf_head(bix);
2670 len[num_elem] = wpabuf_len(bix) / 2;
2671 num_elem++;
2672 }
2673
2674 addr[num_elem] = &one;
2675 len[num_elem] = 1;
2676 num_elem++;
2677
2678 wpa_printf(MSG_DEBUG, "DPP: I-auth hash components");
2679 for (i = 0; i < num_elem; i++)
2680 wpa_hexdump(MSG_DEBUG, "DPP: hash component", addr[i], len[i]);
2681 res = dpp_hash_vector(auth->curve, num_elem, addr, len, i_auth);
2682 if (res == 0)
2683 wpa_hexdump(MSG_DEBUG, "DPP: I-auth", i_auth,
2684 auth->curve->hash_len);
2685 fail:
2686 wpabuf_free(pix);
2687 wpabuf_free(prx);
2688 wpabuf_free(bix);
2689 wpabuf_free(brx);
2690 return res;
2691 }
2692
2693
2694 static int dpp_auth_derive_l_responder(struct dpp_authentication *auth)
2695 {
2696 const EC_GROUP *group;
2697 EC_POINT *l = NULL;
2698 EC_KEY *BI = NULL, *bR = NULL, *pR = NULL;
2699 const EC_POINT *BI_point;
2700 BN_CTX *bnctx;
2701 BIGNUM *lx, *sum, *q;
2702 const BIGNUM *bR_bn, *pR_bn;
2703 int ret = -1;
2704
2705 /* L = ((bR + pR) modulo q) * BI */
2706
2707 bnctx = BN_CTX_new();
2708 sum = BN_new();
2709 q = BN_new();
2710 lx = BN_new();
2711 if (!bnctx || !sum || !q || !lx)
2712 goto fail;
2713 BI = EVP_PKEY_get1_EC_KEY(auth->peer_bi->pubkey);
2714 if (!BI)
2715 goto fail;
2716 BI_point = EC_KEY_get0_public_key(BI);
2717 group = EC_KEY_get0_group(BI);
2718 if (!group)
2719 goto fail;
2720
2721 bR = EVP_PKEY_get1_EC_KEY(auth->own_bi->pubkey);
2722 pR = EVP_PKEY_get1_EC_KEY(auth->own_protocol_key);
2723 if (!bR || !pR)
2724 goto fail;
2725 bR_bn = EC_KEY_get0_private_key(bR);
2726 pR_bn = EC_KEY_get0_private_key(pR);
2727 if (!bR_bn || !pR_bn)
2728 goto fail;
2729 if (EC_GROUP_get_order(group, q, bnctx) != 1 ||
2730 BN_mod_add(sum, bR_bn, pR_bn, q, bnctx) != 1)
2731 goto fail;
2732 l = EC_POINT_new(group);
2733 if (!l ||
2734 EC_POINT_mul(group, l, NULL, BI_point, sum, bnctx) != 1 ||
2735 EC_POINT_get_affine_coordinates_GFp(group, l, lx, NULL,
2736 bnctx) != 1) {
2737 wpa_printf(MSG_ERROR,
2738 "OpenSSL: failed: %s",
2739 ERR_error_string(ERR_get_error(), NULL));
2740 goto fail;
2741 }
2742
2743 if (dpp_bn2bin_pad(lx, auth->Lx, auth->secret_len) < 0)
2744 goto fail;
2745 wpa_hexdump_key(MSG_DEBUG, "DPP: L.x", auth->Lx, auth->secret_len);
2746 auth->Lx_len = auth->secret_len;
2747 ret = 0;
2748 fail:
2749 EC_POINT_clear_free(l);
2750 EC_KEY_free(BI);
2751 EC_KEY_free(bR);
2752 EC_KEY_free(pR);
2753 BN_clear_free(lx);
2754 BN_clear_free(sum);
2755 BN_free(q);
2756 BN_CTX_free(bnctx);
2757 return ret;
2758 }
2759
2760
2761 static int dpp_auth_derive_l_initiator(struct dpp_authentication *auth)
2762 {
2763 const EC_GROUP *group;
2764 EC_POINT *l = NULL, *sum = NULL;
2765 EC_KEY *bI = NULL, *BR = NULL, *PR = NULL;
2766 const EC_POINT *BR_point, *PR_point;
2767 BN_CTX *bnctx;
2768 BIGNUM *lx;
2769 const BIGNUM *bI_bn;
2770 int ret = -1;
2771
2772 /* L = bI * (BR + PR) */
2773
2774 bnctx = BN_CTX_new();
2775 lx = BN_new();
2776 if (!bnctx || !lx)
2777 goto fail;
2778 BR = EVP_PKEY_get1_EC_KEY(auth->peer_bi->pubkey);
2779 PR = EVP_PKEY_get1_EC_KEY(auth->peer_protocol_key);
2780 if (!BR || !PR)
2781 goto fail;
2782 BR_point = EC_KEY_get0_public_key(BR);
2783 PR_point = EC_KEY_get0_public_key(PR);
2784
2785 bI = EVP_PKEY_get1_EC_KEY(auth->own_bi->pubkey);
2786 if (!bI)
2787 goto fail;
2788 group = EC_KEY_get0_group(bI);
2789 bI_bn = EC_KEY_get0_private_key(bI);
2790 if (!group || !bI_bn)
2791 goto fail;
2792 sum = EC_POINT_new(group);
2793 l = EC_POINT_new(group);
2794 if (!sum || !l ||
2795 EC_POINT_add(group, sum, BR_point, PR_point, bnctx) != 1 ||
2796 EC_POINT_mul(group, l, NULL, sum, bI_bn, bnctx) != 1 ||
2797 EC_POINT_get_affine_coordinates_GFp(group, l, lx, NULL,
2798 bnctx) != 1) {
2799 wpa_printf(MSG_ERROR,
2800 "OpenSSL: failed: %s",
2801 ERR_error_string(ERR_get_error(), NULL));
2802 goto fail;
2803 }
2804
2805 if (dpp_bn2bin_pad(lx, auth->Lx, auth->secret_len) < 0)
2806 goto fail;
2807 wpa_hexdump_key(MSG_DEBUG, "DPP: L.x", auth->Lx, auth->secret_len);
2808 auth->Lx_len = auth->secret_len;
2809 ret = 0;
2810 fail:
2811 EC_POINT_clear_free(l);
2812 EC_POINT_clear_free(sum);
2813 EC_KEY_free(bI);
2814 EC_KEY_free(BR);
2815 EC_KEY_free(PR);
2816 BN_clear_free(lx);
2817 BN_CTX_free(bnctx);
2818 return ret;
2819 }
2820
2821
2822 static int dpp_auth_build_resp_ok(struct dpp_authentication *auth)
2823 {
2824 size_t nonce_len;
2825 size_t secret_len;
2826 struct wpabuf *msg, *pr = NULL;
2827 u8 r_auth[4 + DPP_MAX_HASH_LEN];
2828 u8 wrapped_r_auth[4 + DPP_MAX_HASH_LEN + AES_BLOCK_SIZE], *w_r_auth;
2829 size_t wrapped_r_auth_len;
2830 int ret = -1;
2831 const u8 *r_pubkey_hash, *i_pubkey_hash, *r_nonce, *i_nonce;
2832 enum dpp_status_error status = DPP_STATUS_OK;
2833 #ifdef CONFIG_TESTING_OPTIONS
2834 u8 test_hash[SHA256_MAC_LEN];
2835 #endif /* CONFIG_TESTING_OPTIONS */
2836
2837 wpa_printf(MSG_DEBUG, "DPP: Build Authentication Response");
2838 if (!auth->own_bi)
2839 return -1;
2840
2841 #ifdef CONFIG_TESTING_OPTIONS
2842 if (dpp_nonce_override_len > 0) {
2843 wpa_printf(MSG_INFO, "DPP: TESTING - override R-nonce");
2844 nonce_len = dpp_nonce_override_len;
2845 os_memcpy(auth->r_nonce, dpp_nonce_override, nonce_len);
2846 } else {
2847 nonce_len = auth->curve->nonce_len;
2848 if (random_get_bytes(auth->r_nonce, nonce_len)) {
2849 wpa_printf(MSG_ERROR,
2850 "DPP: Failed to generate R-nonce");
2851 goto fail;
2852 }
2853 }
2854 #else /* CONFIG_TESTING_OPTIONS */
2855 nonce_len = auth->curve->nonce_len;
2856 if (random_get_bytes(auth->r_nonce, nonce_len)) {
2857 wpa_printf(MSG_ERROR, "DPP: Failed to generate R-nonce");
2858 goto fail;
2859 }
2860 #endif /* CONFIG_TESTING_OPTIONS */
2861 wpa_hexdump(MSG_DEBUG, "DPP: R-nonce", auth->r_nonce, nonce_len);
2862
2863 EVP_PKEY_free(auth->own_protocol_key);
2864 #ifdef CONFIG_TESTING_OPTIONS
2865 if (dpp_protocol_key_override_len) {
2866 const struct dpp_curve_params *tmp_curve;
2867
2868 wpa_printf(MSG_INFO,
2869 "DPP: TESTING - override protocol key");
2870 auth->own_protocol_key = dpp_set_keypair(
2871 &tmp_curve, dpp_protocol_key_override,
2872 dpp_protocol_key_override_len);
2873 } else {
2874 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
2875 }
2876 #else /* CONFIG_TESTING_OPTIONS */
2877 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
2878 #endif /* CONFIG_TESTING_OPTIONS */
2879 if (!auth->own_protocol_key)
2880 goto fail;
2881
2882 pr = dpp_get_pubkey_point(auth->own_protocol_key, 0);
2883 if (!pr)
2884 goto fail;
2885
2886 /* ECDH: N = pR * PI */
2887 if (dpp_ecdh(auth->own_protocol_key, auth->peer_protocol_key,
2888 auth->Nx, &secret_len) < 0)
2889 goto fail;
2890
2891 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
2892 auth->Nx, auth->secret_len);
2893 auth->Nx_len = auth->secret_len;
2894
2895 if (dpp_derive_k2(auth->Nx, auth->secret_len, auth->k2,
2896 auth->curve->hash_len) < 0)
2897 goto fail;
2898
2899 if (auth->own_bi && auth->peer_bi) {
2900 /* Mutual authentication */
2901 if (dpp_auth_derive_l_responder(auth) < 0)
2902 goto fail;
2903 }
2904
2905 if (dpp_derive_ke(auth, auth->ke, auth->curve->hash_len) < 0)
2906 goto fail;
2907
2908 /* R-auth = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
2909 WPA_PUT_LE16(r_auth, DPP_ATTR_R_AUTH_TAG);
2910 WPA_PUT_LE16(&r_auth[2], auth->curve->hash_len);
2911 if (dpp_gen_r_auth(auth, r_auth + 4) < 0)
2912 goto fail;
2913 #ifdef CONFIG_TESTING_OPTIONS
2914 if (dpp_test == DPP_TEST_R_AUTH_MISMATCH_AUTH_RESP) {
2915 wpa_printf(MSG_INFO, "DPP: TESTING - R-auth mismatch");
2916 r_auth[4 + auth->curve->hash_len / 2] ^= 0x01;
2917 }
2918 #endif /* CONFIG_TESTING_OPTIONS */
2919 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
2920 r_auth, 4 + auth->curve->hash_len,
2921 0, NULL, NULL, wrapped_r_auth) < 0)
2922 goto fail;
2923 wrapped_r_auth_len = 4 + auth->curve->hash_len + AES_BLOCK_SIZE;
2924 wpa_hexdump(MSG_DEBUG, "DPP: {R-auth}ke",
2925 wrapped_r_auth, wrapped_r_auth_len);
2926 w_r_auth = wrapped_r_auth;
2927
2928 r_pubkey_hash = auth->own_bi->pubkey_hash;
2929 if (auth->peer_bi)
2930 i_pubkey_hash = auth->peer_bi->pubkey_hash;
2931 else
2932 i_pubkey_hash = NULL;
2933
2934 i_nonce = auth->i_nonce;
2935 r_nonce = auth->r_nonce;
2936
2937 #ifdef CONFIG_TESTING_OPTIONS
2938 if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
2939 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
2940 r_pubkey_hash = NULL;
2941 } else if (dpp_test ==
2942 DPP_TEST_INVALID_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
2943 wpa_printf(MSG_INFO,
2944 "DPP: TESTING - invalid R-Bootstrap Key Hash");
2945 os_memcpy(test_hash, r_pubkey_hash, SHA256_MAC_LEN);
2946 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
2947 r_pubkey_hash = test_hash;
2948 } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
2949 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
2950 i_pubkey_hash = NULL;
2951 } else if (dpp_test ==
2952 DPP_TEST_INVALID_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
2953 wpa_printf(MSG_INFO,
2954 "DPP: TESTING - invalid I-Bootstrap Key Hash");
2955 if (i_pubkey_hash)
2956 os_memcpy(test_hash, i_pubkey_hash, SHA256_MAC_LEN);
2957 else
2958 os_memset(test_hash, 0, SHA256_MAC_LEN);
2959 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
2960 i_pubkey_hash = test_hash;
2961 } else if (dpp_test == DPP_TEST_NO_R_PROTO_KEY_AUTH_RESP) {
2962 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Proto Key");
2963 wpabuf_free(pr);
2964 pr = NULL;
2965 } else if (dpp_test == DPP_TEST_INVALID_R_PROTO_KEY_AUTH_RESP) {
2966 wpa_printf(MSG_INFO, "DPP: TESTING - invalid R-Proto Key");
2967 wpabuf_free(pr);
2968 pr = wpabuf_alloc(2 * auth->curve->prime_len);
2969 if (!pr || dpp_test_gen_invalid_key(pr, auth->curve) < 0)
2970 goto fail;
2971 } else if (dpp_test == DPP_TEST_NO_R_AUTH_AUTH_RESP) {
2972 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Auth");
2973 w_r_auth = NULL;
2974 wrapped_r_auth_len = 0;
2975 } else if (dpp_test == DPP_TEST_NO_STATUS_AUTH_RESP) {
2976 wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
2977 status = 255;
2978 } else if (dpp_test == DPP_TEST_INVALID_STATUS_AUTH_RESP) {
2979 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
2980 status = 254;
2981 } else if (dpp_test == DPP_TEST_NO_R_NONCE_AUTH_RESP) {
2982 wpa_printf(MSG_INFO, "DPP: TESTING - no R-nonce");
2983 r_nonce = NULL;
2984 } else if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_RESP) {
2985 wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
2986 i_nonce = NULL;
2987 }
2988 #endif /* CONFIG_TESTING_OPTIONS */
2989
2990 msg = dpp_auth_build_resp(auth, status, pr, nonce_len,
2991 r_pubkey_hash, i_pubkey_hash,
2992 r_nonce, i_nonce,
2993 w_r_auth, wrapped_r_auth_len,
2994 auth->k2);
2995 if (!msg)
2996 goto fail;
2997 wpabuf_free(auth->resp_msg);
2998 auth->resp_msg = msg;
2999 ret = 0;
3000 fail:
3001 wpabuf_free(pr);
3002 return ret;
3003 }
3004
3005
3006 static int dpp_auth_build_resp_status(struct dpp_authentication *auth,
3007 enum dpp_status_error status)
3008 {
3009 struct wpabuf *msg;
3010 const u8 *r_pubkey_hash, *i_pubkey_hash, *i_nonce;
3011 #ifdef CONFIG_TESTING_OPTIONS
3012 u8 test_hash[SHA256_MAC_LEN];
3013 #endif /* CONFIG_TESTING_OPTIONS */
3014
3015 if (!auth->own_bi)
3016 return -1;
3017 wpa_printf(MSG_DEBUG, "DPP: Build Authentication Response");
3018
3019 r_pubkey_hash = auth->own_bi->pubkey_hash;
3020 if (auth->peer_bi)
3021 i_pubkey_hash = auth->peer_bi->pubkey_hash;
3022 else
3023 i_pubkey_hash = NULL;
3024
3025 i_nonce = auth->i_nonce;
3026
3027 #ifdef CONFIG_TESTING_OPTIONS
3028 if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
3029 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
3030 r_pubkey_hash = NULL;
3031 } else if (dpp_test ==
3032 DPP_TEST_INVALID_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
3033 wpa_printf(MSG_INFO,
3034 "DPP: TESTING - invalid R-Bootstrap Key Hash");
3035 os_memcpy(test_hash, r_pubkey_hash, SHA256_MAC_LEN);
3036 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
3037 r_pubkey_hash = test_hash;
3038 } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
3039 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
3040 i_pubkey_hash = NULL;
3041 } else if (dpp_test ==
3042 DPP_TEST_INVALID_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
3043 wpa_printf(MSG_INFO,
3044 "DPP: TESTING - invalid I-Bootstrap Key Hash");
3045 if (i_pubkey_hash)
3046 os_memcpy(test_hash, i_pubkey_hash, SHA256_MAC_LEN);
3047 else
3048 os_memset(test_hash, 0, SHA256_MAC_LEN);
3049 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
3050 i_pubkey_hash = test_hash;
3051 } else if (dpp_test == DPP_TEST_NO_STATUS_AUTH_RESP) {
3052 wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
3053 status = 255;
3054 } else if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_RESP) {
3055 wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
3056 i_nonce = NULL;
3057 }
3058 #endif /* CONFIG_TESTING_OPTIONS */
3059
3060 msg = dpp_auth_build_resp(auth, status, NULL, auth->curve->nonce_len,
3061 r_pubkey_hash, i_pubkey_hash,
3062 NULL, i_nonce, NULL, 0, auth->k1);
3063 if (!msg)
3064 return -1;
3065 wpabuf_free(auth->resp_msg);
3066 auth->resp_msg = msg;
3067 return 0;
3068 }
3069
3070
3071 struct dpp_authentication *
3072 dpp_auth_req_rx(void *msg_ctx, u8 dpp_allowed_roles, int qr_mutual,
3073 struct dpp_bootstrap_info *peer_bi,
3074 struct dpp_bootstrap_info *own_bi,
3075 unsigned int freq, const u8 *hdr, const u8 *attr_start,
3076 size_t attr_len)
3077 {
3078 EVP_PKEY *pi = NULL;
3079 EVP_PKEY_CTX *ctx = NULL;
3080 size_t secret_len;
3081 const u8 *addr[2];
3082 size_t len[2];
3083 u8 *unwrapped = NULL;
3084 size_t unwrapped_len = 0;
3085 const u8 *wrapped_data, *i_proto, *i_nonce, *i_capab, *i_bootstrap,
3086 *channel;
3087 u16 wrapped_data_len, i_proto_len, i_nonce_len, i_capab_len,
3088 i_bootstrap_len, channel_len;
3089 struct dpp_authentication *auth = NULL;
3090 #ifdef CONFIG_DPP2
3091 const u8 *version;
3092 u16 version_len;
3093 #endif /* CONFIG_DPP2 */
3094
3095 #ifdef CONFIG_TESTING_OPTIONS
3096 if (dpp_test == DPP_TEST_STOP_AT_AUTH_REQ) {
3097 wpa_printf(MSG_INFO,
3098 "DPP: TESTING - stop at Authentication Request");
3099 return NULL;
3100 }
3101 #endif /* CONFIG_TESTING_OPTIONS */
3102
3103 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
3104 &wrapped_data_len);
3105 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
3106 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
3107 "Missing or invalid required Wrapped Data attribute");
3108 return NULL;
3109 }
3110 wpa_hexdump(MSG_MSGDUMP, "DPP: Wrapped Data",
3111 wrapped_data, wrapped_data_len);
3112 attr_len = wrapped_data - 4 - attr_start;
3113
3114 auth = os_zalloc(sizeof(*auth));
3115 if (!auth)
3116 goto fail;
3117 auth->msg_ctx = msg_ctx;
3118 auth->peer_bi = peer_bi;
3119 auth->own_bi = own_bi;
3120 auth->curve = own_bi->curve;
3121 auth->curr_freq = freq;
3122
3123 auth->peer_version = 1; /* default to the first version */
3124 #ifdef CONFIG_DPP2
3125 version = dpp_get_attr(attr_start, attr_len, DPP_ATTR_PROTOCOL_VERSION,
3126 &version_len);
3127 if (version) {
3128 if (version_len < 1 || version[0] == 0) {
3129 dpp_auth_fail(auth,
3130 "Invalid Protocol Version attribute");
3131 goto fail;
3132 }
3133 auth->peer_version = version[0];
3134 wpa_printf(MSG_DEBUG, "DPP: Peer protocol version %u",
3135 auth->peer_version);
3136 }
3137 #endif /* CONFIG_DPP2 */
3138
3139 channel = dpp_get_attr(attr_start, attr_len, DPP_ATTR_CHANNEL,
3140 &channel_len);
3141 if (channel) {
3142 int neg_freq;
3143
3144 if (channel_len < 2) {
3145 dpp_auth_fail(auth, "Too short Channel attribute");
3146 goto fail;
3147 }
3148
3149 neg_freq = ieee80211_chan_to_freq(NULL, channel[0], channel[1]);
3150 wpa_printf(MSG_DEBUG,
3151 "DPP: Initiator requested different channel for negotiation: op_class=%u channel=%u --> freq=%d",
3152 channel[0], channel[1], neg_freq);
3153 if (neg_freq < 0) {
3154 dpp_auth_fail(auth,
3155 "Unsupported Channel attribute value");
3156 goto fail;
3157 }
3158
3159 if (auth->curr_freq != (unsigned int) neg_freq) {
3160 wpa_printf(MSG_DEBUG,
3161 "DPP: Changing negotiation channel from %u MHz to %u MHz",
3162 freq, neg_freq);
3163 auth->curr_freq = neg_freq;
3164 }
3165 }
3166
3167 i_proto = dpp_get_attr(attr_start, attr_len, DPP_ATTR_I_PROTOCOL_KEY,
3168 &i_proto_len);
3169 if (!i_proto) {
3170 dpp_auth_fail(auth,
3171 "Missing required Initiator Protocol Key attribute");
3172 goto fail;
3173 }
3174 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Protocol Key",
3175 i_proto, i_proto_len);
3176
3177 /* M = bR * PI */
3178 pi = dpp_set_pubkey_point(own_bi->pubkey, i_proto, i_proto_len);
3179 if (!pi) {
3180 dpp_auth_fail(auth, "Invalid Initiator Protocol Key");
3181 goto fail;
3182 }
3183 dpp_debug_print_key("Peer (Initiator) Protocol Key", pi);
3184
3185 if (dpp_ecdh(own_bi->pubkey, pi, auth->Mx, &secret_len) < 0)
3186 goto fail;
3187 auth->secret_len = secret_len;
3188
3189 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (M.x)",
3190 auth->Mx, auth->secret_len);
3191 auth->Mx_len = auth->secret_len;
3192
3193 if (dpp_derive_k1(auth->Mx, auth->secret_len, auth->k1,
3194 auth->curve->hash_len) < 0)
3195 goto fail;
3196
3197 addr[0] = hdr;
3198 len[0] = DPP_HDR_LEN;
3199 addr[1] = attr_start;
3200 len[1] = attr_len;
3201 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3202 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3203 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
3204 wrapped_data, wrapped_data_len);
3205 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
3206 unwrapped = os_malloc(unwrapped_len);
3207 if (!unwrapped)
3208 goto fail;
3209 if (aes_siv_decrypt(auth->k1, auth->curve->hash_len,
3210 wrapped_data, wrapped_data_len,
3211 2, addr, len, unwrapped) < 0) {
3212 dpp_auth_fail(auth, "AES-SIV decryption failed");
3213 goto fail;
3214 }
3215 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
3216 unwrapped, unwrapped_len);
3217
3218 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
3219 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
3220 goto fail;
3221 }
3222
3223 i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
3224 &i_nonce_len);
3225 if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
3226 dpp_auth_fail(auth, "Missing or invalid I-nonce");
3227 goto fail;
3228 }
3229 wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
3230 os_memcpy(auth->i_nonce, i_nonce, i_nonce_len);
3231
3232 i_capab = dpp_get_attr(unwrapped, unwrapped_len,
3233 DPP_ATTR_I_CAPABILITIES,
3234 &i_capab_len);
3235 if (!i_capab || i_capab_len < 1) {
3236 dpp_auth_fail(auth, "Missing or invalid I-capabilities");
3237 goto fail;
3238 }
3239 auth->i_capab = i_capab[0];
3240 wpa_printf(MSG_DEBUG, "DPP: I-capabilities: 0x%02x", auth->i_capab);
3241
3242 bin_clear_free(unwrapped, unwrapped_len);
3243 unwrapped = NULL;
3244
3245 switch (auth->i_capab & DPP_CAPAB_ROLE_MASK) {
3246 case DPP_CAPAB_ENROLLEE:
3247 if (!(dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR)) {
3248 wpa_printf(MSG_DEBUG,
3249 "DPP: Local policy does not allow Configurator role");
3250 goto not_compatible;
3251 }
3252 wpa_printf(MSG_DEBUG, "DPP: Acting as Configurator");
3253 auth->configurator = 1;
3254 break;
3255 case DPP_CAPAB_CONFIGURATOR:
3256 if (!(dpp_allowed_roles & DPP_CAPAB_ENROLLEE)) {
3257 wpa_printf(MSG_DEBUG,
3258 "DPP: Local policy does not allow Enrollee role");
3259 goto not_compatible;
3260 }
3261 wpa_printf(MSG_DEBUG, "DPP: Acting as Enrollee");
3262 auth->configurator = 0;
3263 break;
3264 case DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE:
3265 if (dpp_allowed_roles & DPP_CAPAB_ENROLLEE) {
3266 wpa_printf(MSG_DEBUG, "DPP: Acting as Enrollee");
3267 auth->configurator = 0;
3268 } else if (dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR) {
3269 wpa_printf(MSG_DEBUG, "DPP: Acting as Configurator");
3270 auth->configurator = 1;
3271 } else {
3272 wpa_printf(MSG_DEBUG,
3273 "DPP: Local policy does not allow Configurator/Enrollee role");
3274 goto not_compatible;
3275 }
3276 break;
3277 default:
3278 wpa_printf(MSG_DEBUG, "DPP: Unexpected role in I-capabilities");
3279 wpa_msg(auth->msg_ctx, MSG_INFO,
3280 DPP_EVENT_FAIL "Invalid role in I-capabilities 0x%02x",
3281 auth->i_capab & DPP_CAPAB_ROLE_MASK);
3282 goto fail;
3283 }
3284
3285 auth->peer_protocol_key = pi;
3286 pi = NULL;
3287 if (qr_mutual && !peer_bi && own_bi->type == DPP_BOOTSTRAP_QR_CODE) {
3288 char hex[SHA256_MAC_LEN * 2 + 1];
3289
3290 wpa_printf(MSG_DEBUG,
3291 "DPP: Mutual authentication required with QR Codes, but peer info is not yet available - request more time");
3292 if (dpp_auth_build_resp_status(auth,
3293 DPP_STATUS_RESPONSE_PENDING) < 0)
3294 goto fail;
3295 i_bootstrap = dpp_get_attr(attr_start, attr_len,
3296 DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
3297 &i_bootstrap_len);
3298 if (i_bootstrap && i_bootstrap_len == SHA256_MAC_LEN) {
3299 auth->response_pending = 1;
3300 os_memcpy(auth->waiting_pubkey_hash,
3301 i_bootstrap, i_bootstrap_len);
3302 wpa_snprintf_hex(hex, sizeof(hex), i_bootstrap,
3303 i_bootstrap_len);
3304 } else {
3305 hex[0] = '\0';
3306 }
3307
3308 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_SCAN_PEER_QR_CODE
3309 "%s", hex);
3310 return auth;
3311 }
3312 if (dpp_auth_build_resp_ok(auth) < 0)
3313 goto fail;
3314
3315 return auth;
3316
3317 not_compatible:
3318 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_NOT_COMPATIBLE
3319 "i-capab=0x%02x", auth->i_capab);
3320 if (dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR)
3321 auth->configurator = 1;
3322 else
3323 auth->configurator = 0;
3324 auth->peer_protocol_key = pi;
3325 pi = NULL;
3326 if (dpp_auth_build_resp_status(auth, DPP_STATUS_NOT_COMPATIBLE) < 0)
3327 goto fail;
3328
3329 auth->remove_on_tx_status = 1;
3330 return auth;
3331 fail:
3332 bin_clear_free(unwrapped, unwrapped_len);
3333 EVP_PKEY_free(pi);
3334 EVP_PKEY_CTX_free(ctx);
3335 dpp_auth_deinit(auth);
3336 return NULL;
3337 }
3338
3339
3340 int dpp_notify_new_qr_code(struct dpp_authentication *auth,
3341 struct dpp_bootstrap_info *peer_bi)
3342 {
3343 if (!auth || !auth->response_pending ||
3344 os_memcmp(auth->waiting_pubkey_hash, peer_bi->pubkey_hash,
3345 SHA256_MAC_LEN) != 0)
3346 return 0;
3347
3348 wpa_printf(MSG_DEBUG,
3349 "DPP: New scanned QR Code has matching public key that was needed to continue DPP Authentication exchange with "
3350 MACSTR, MAC2STR(auth->peer_mac_addr));
3351 auth->peer_bi = peer_bi;
3352
3353 if (dpp_auth_build_resp_ok(auth) < 0)
3354 return -1;
3355
3356 return 1;
3357 }
3358
3359
3360 static struct wpabuf * dpp_auth_build_conf(struct dpp_authentication *auth,
3361 enum dpp_status_error status)
3362 {
3363 struct wpabuf *msg;
3364 u8 i_auth[4 + DPP_MAX_HASH_LEN];
3365 size_t i_auth_len;
3366 u8 r_nonce[4 + DPP_MAX_NONCE_LEN];
3367 size_t r_nonce_len;
3368 const u8 *addr[2];
3369 size_t len[2], attr_len;
3370 u8 *wrapped_i_auth;
3371 u8 *wrapped_r_nonce;
3372 u8 *attr_start, *attr_end;
3373 const u8 *r_pubkey_hash, *i_pubkey_hash;
3374 #ifdef CONFIG_TESTING_OPTIONS
3375 u8 test_hash[SHA256_MAC_LEN];
3376 #endif /* CONFIG_TESTING_OPTIONS */
3377
3378 wpa_printf(MSG_DEBUG, "DPP: Build Authentication Confirmation");
3379
3380 i_auth_len = 4 + auth->curve->hash_len;
3381 r_nonce_len = 4 + auth->curve->nonce_len;
3382 /* Build DPP Authentication Confirmation frame attributes */
3383 attr_len = 4 + 1 + 2 * (4 + SHA256_MAC_LEN) +
3384 4 + i_auth_len + r_nonce_len + AES_BLOCK_SIZE;
3385 #ifdef CONFIG_TESTING_OPTIONS
3386 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_CONF)
3387 attr_len += 5;
3388 #endif /* CONFIG_TESTING_OPTIONS */
3389 msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_CONF, attr_len);
3390 if (!msg)
3391 goto fail;
3392
3393 attr_start = wpabuf_put(msg, 0);
3394
3395 r_pubkey_hash = auth->peer_bi->pubkey_hash;
3396 if (auth->own_bi)
3397 i_pubkey_hash = auth->own_bi->pubkey_hash;
3398 else
3399 i_pubkey_hash = NULL;
3400
3401 #ifdef CONFIG_TESTING_OPTIONS
3402 if (dpp_test == DPP_TEST_NO_STATUS_AUTH_CONF) {
3403 wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
3404 goto skip_status;
3405 } else if (dpp_test == DPP_TEST_INVALID_STATUS_AUTH_CONF) {
3406 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
3407 status = 254;
3408 }
3409 #endif /* CONFIG_TESTING_OPTIONS */
3410
3411 /* DPP Status */
3412 dpp_build_attr_status(msg, status);
3413
3414 #ifdef CONFIG_TESTING_OPTIONS
3415 skip_status:
3416 if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_CONF) {
3417 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
3418 r_pubkey_hash = NULL;
3419 } else if (dpp_test ==
3420 DPP_TEST_INVALID_R_BOOTSTRAP_KEY_HASH_AUTH_CONF) {
3421 wpa_printf(MSG_INFO,
3422 "DPP: TESTING - invalid R-Bootstrap Key Hash");
3423 os_memcpy(test_hash, r_pubkey_hash, SHA256_MAC_LEN);
3424 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
3425 r_pubkey_hash = test_hash;
3426 } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_CONF) {
3427 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
3428 i_pubkey_hash = NULL;
3429 } else if (dpp_test ==
3430 DPP_TEST_INVALID_I_BOOTSTRAP_KEY_HASH_AUTH_CONF) {
3431 wpa_printf(MSG_INFO,
3432 "DPP: TESTING - invalid I-Bootstrap Key Hash");
3433 if (i_pubkey_hash)
3434 os_memcpy(test_hash, i_pubkey_hash, SHA256_MAC_LEN);
3435 else
3436 os_memset(test_hash, 0, SHA256_MAC_LEN);
3437 test_hash[SHA256_MAC_LEN - 1] ^= 0x01;
3438 i_pubkey_hash = test_hash;
3439 }
3440 #endif /* CONFIG_TESTING_OPTIONS */
3441
3442 /* Responder Bootstrapping Key Hash */
3443 dpp_build_attr_r_bootstrap_key_hash(msg, r_pubkey_hash);
3444
3445 /* Initiator Bootstrapping Key Hash (mutual authentication) */
3446 dpp_build_attr_i_bootstrap_key_hash(msg, i_pubkey_hash);
3447
3448 #ifdef CONFIG_TESTING_OPTIONS
3449 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_CONF)
3450 goto skip_wrapped_data;
3451 if (dpp_test == DPP_TEST_NO_I_AUTH_AUTH_CONF)
3452 i_auth_len = 0;
3453 #endif /* CONFIG_TESTING_OPTIONS */
3454
3455 attr_end = wpabuf_put(msg, 0);
3456
3457 /* OUI, OUI type, Crypto Suite, DPP frame type */
3458 addr[0] = wpabuf_head_u8(msg) + 2;
3459 len[0] = 3 + 1 + 1 + 1;
3460 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3461
3462 /* Attributes before Wrapped Data */
3463 addr[1] = attr_start;
3464 len[1] = attr_end - attr_start;
3465 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3466
3467 if (status == DPP_STATUS_OK) {
3468 /* I-auth wrapped with ke */
3469 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
3470 wpabuf_put_le16(msg, i_auth_len + AES_BLOCK_SIZE);
3471 wrapped_i_auth = wpabuf_put(msg, i_auth_len + AES_BLOCK_SIZE);
3472
3473 #ifdef CONFIG_TESTING_OPTIONS
3474 if (dpp_test == DPP_TEST_NO_I_AUTH_AUTH_CONF)
3475 goto skip_i_auth;
3476 #endif /* CONFIG_TESTING_OPTIONS */
3477
3478 /* I-auth = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |]
3479 * 1) */
3480 WPA_PUT_LE16(i_auth, DPP_ATTR_I_AUTH_TAG);
3481 WPA_PUT_LE16(&i_auth[2], auth->curve->hash_len);
3482 if (dpp_gen_i_auth(auth, i_auth + 4) < 0)
3483 goto fail;
3484
3485 #ifdef CONFIG_TESTING_OPTIONS
3486 if (dpp_test == DPP_TEST_I_AUTH_MISMATCH_AUTH_CONF) {
3487 wpa_printf(MSG_INFO, "DPP: TESTING - I-auth mismatch");
3488 i_auth[4 + auth->curve->hash_len / 2] ^= 0x01;
3489 }
3490 skip_i_auth:
3491 #endif /* CONFIG_TESTING_OPTIONS */
3492 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
3493 i_auth, i_auth_len,
3494 2, addr, len, wrapped_i_auth) < 0)
3495 goto fail;
3496 wpa_hexdump(MSG_DEBUG, "DPP: {I-auth}ke",
3497 wrapped_i_auth, i_auth_len + AES_BLOCK_SIZE);
3498 } else {
3499 /* R-nonce wrapped with k2 */
3500 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
3501 wpabuf_put_le16(msg, r_nonce_len + AES_BLOCK_SIZE);
3502 wrapped_r_nonce = wpabuf_put(msg, r_nonce_len + AES_BLOCK_SIZE);
3503
3504 WPA_PUT_LE16(r_nonce, DPP_ATTR_R_NONCE);
3505 WPA_PUT_LE16(&r_nonce[2], auth->curve->nonce_len);
3506 os_memcpy(r_nonce + 4, auth->r_nonce, auth->curve->nonce_len);
3507
3508 if (aes_siv_encrypt(auth->k2, auth->curve->hash_len,
3509 r_nonce, r_nonce_len,
3510 2, addr, len, wrapped_r_nonce) < 0)
3511 goto fail;
3512 wpa_hexdump(MSG_DEBUG, "DPP: {R-nonce}k2",
3513 wrapped_r_nonce, r_nonce_len + AES_BLOCK_SIZE);
3514 }
3515
3516 #ifdef CONFIG_TESTING_OPTIONS
3517 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_CONF) {
3518 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
3519 dpp_build_attr_status(msg, DPP_STATUS_OK);
3520 }
3521 skip_wrapped_data:
3522 #endif /* CONFIG_TESTING_OPTIONS */
3523
3524 wpa_hexdump_buf(MSG_DEBUG,
3525 "DPP: Authentication Confirmation frame attributes",
3526 msg);
3527 if (status == DPP_STATUS_OK)
3528 dpp_auth_success(auth);
3529
3530 return msg;
3531
3532 fail:
3533 wpabuf_free(msg);
3534 return NULL;
3535 }
3536
3537
3538 static void
3539 dpp_auth_resp_rx_status(struct dpp_authentication *auth, const u8 *hdr,
3540 const u8 *attr_start, size_t attr_len,
3541 const u8 *wrapped_data, u16 wrapped_data_len,
3542 enum dpp_status_error status)
3543 {
3544 const u8 *addr[2];
3545 size_t len[2];
3546 u8 *unwrapped = NULL;
3547 size_t unwrapped_len = 0;
3548 const u8 *i_nonce, *r_capab;
3549 u16 i_nonce_len, r_capab_len;
3550
3551 if (status == DPP_STATUS_NOT_COMPATIBLE) {
3552 wpa_printf(MSG_DEBUG,
3553 "DPP: Responder reported incompatible roles");
3554 } else if (status == DPP_STATUS_RESPONSE_PENDING) {
3555 wpa_printf(MSG_DEBUG,
3556 "DPP: Responder reported more time needed");
3557 } else {
3558 wpa_printf(MSG_DEBUG,
3559 "DPP: Responder reported failure (status %d)",
3560 status);
3561 dpp_auth_fail(auth, "Responder reported failure");
3562 return;
3563 }
3564
3565 addr[0] = hdr;
3566 len[0] = DPP_HDR_LEN;
3567 addr[1] = attr_start;
3568 len[1] = attr_len;
3569 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3570 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3571 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
3572 wrapped_data, wrapped_data_len);
3573 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
3574 unwrapped = os_malloc(unwrapped_len);
3575 if (!unwrapped)
3576 goto fail;
3577 if (aes_siv_decrypt(auth->k1, auth->curve->hash_len,
3578 wrapped_data, wrapped_data_len,
3579 2, addr, len, unwrapped) < 0) {
3580 dpp_auth_fail(auth, "AES-SIV decryption failed");
3581 goto fail;
3582 }
3583 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
3584 unwrapped, unwrapped_len);
3585
3586 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
3587 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
3588 goto fail;
3589 }
3590
3591 i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
3592 &i_nonce_len);
3593 if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
3594 dpp_auth_fail(auth, "Missing or invalid I-nonce");
3595 goto fail;
3596 }
3597 wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
3598 if (os_memcmp(auth->i_nonce, i_nonce, i_nonce_len) != 0) {
3599 dpp_auth_fail(auth, "I-nonce mismatch");
3600 goto fail;
3601 }
3602
3603 r_capab = dpp_get_attr(unwrapped, unwrapped_len,
3604 DPP_ATTR_R_CAPABILITIES,
3605 &r_capab_len);
3606 if (!r_capab || r_capab_len < 1) {
3607 dpp_auth_fail(auth, "Missing or invalid R-capabilities");
3608 goto fail;
3609 }
3610 auth->r_capab = r_capab[0];
3611 wpa_printf(MSG_DEBUG, "DPP: R-capabilities: 0x%02x", auth->r_capab);
3612 if (status == DPP_STATUS_NOT_COMPATIBLE) {
3613 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_NOT_COMPATIBLE
3614 "r-capab=0x%02x", auth->r_capab);
3615 } else if (status == DPP_STATUS_RESPONSE_PENDING) {
3616 u8 role = auth->r_capab & DPP_CAPAB_ROLE_MASK;
3617
3618 if ((auth->configurator && role != DPP_CAPAB_ENROLLEE) ||
3619 (!auth->configurator && role != DPP_CAPAB_CONFIGURATOR)) {
3620 wpa_msg(auth->msg_ctx, MSG_INFO,
3621 DPP_EVENT_FAIL "Unexpected role in R-capabilities 0x%02x",
3622 role);
3623 } else {
3624 wpa_printf(MSG_DEBUG,
3625 "DPP: Continue waiting for full DPP Authentication Response");
3626 wpa_msg(auth->msg_ctx, MSG_INFO,
3627 DPP_EVENT_RESPONSE_PENDING "%s",
3628 auth->tmp_own_bi ? auth->tmp_own_bi->uri : "");
3629 }
3630 }
3631 fail:
3632 bin_clear_free(unwrapped, unwrapped_len);
3633 }
3634
3635
3636 struct wpabuf *
3637 dpp_auth_resp_rx(struct dpp_authentication *auth, const u8 *hdr,
3638 const u8 *attr_start, size_t attr_len)
3639 {
3640 EVP_PKEY *pr;
3641 size_t secret_len;
3642 const u8 *addr[2];
3643 size_t len[2];
3644 u8 *unwrapped = NULL, *unwrapped2 = NULL;
3645 size_t unwrapped_len = 0, unwrapped2_len = 0;
3646 const u8 *r_bootstrap, *i_bootstrap, *wrapped_data, *status, *r_proto,
3647 *r_nonce, *i_nonce, *r_capab, *wrapped2, *r_auth;
3648 u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len, status_len,
3649 r_proto_len, r_nonce_len, i_nonce_len, r_capab_len,
3650 wrapped2_len, r_auth_len;
3651 u8 r_auth2[DPP_MAX_HASH_LEN];
3652 u8 role;
3653 #ifdef CONFIG_DPP2
3654 const u8 *version;
3655 u16 version_len;
3656 #endif /* CONFIG_DPP2 */
3657
3658 #ifdef CONFIG_TESTING_OPTIONS
3659 if (dpp_test == DPP_TEST_STOP_AT_AUTH_RESP) {
3660 wpa_printf(MSG_INFO,
3661 "DPP: TESTING - stop at Authentication Response");
3662 return NULL;
3663 }
3664 #endif /* CONFIG_TESTING_OPTIONS */
3665
3666 if (!auth->initiator || !auth->peer_bi) {
3667 dpp_auth_fail(auth, "Unexpected Authentication Response");
3668 return NULL;
3669 }
3670
3671 auth->waiting_auth_resp = 0;
3672
3673 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
3674 &wrapped_data_len);
3675 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
3676 dpp_auth_fail(auth,
3677 "Missing or invalid required Wrapped Data attribute");
3678 return NULL;
3679 }
3680 wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
3681 wrapped_data, wrapped_data_len);
3682
3683 attr_len = wrapped_data - 4 - attr_start;
3684
3685 r_bootstrap = dpp_get_attr(attr_start, attr_len,
3686 DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
3687 &r_bootstrap_len);
3688 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
3689 dpp_auth_fail(auth,
3690 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
3691 return NULL;
3692 }
3693 wpa_hexdump(MSG_DEBUG, "DPP: Responder Bootstrapping Key Hash",
3694 r_bootstrap, r_bootstrap_len);
3695 if (os_memcmp(r_bootstrap, auth->peer_bi->pubkey_hash,
3696 SHA256_MAC_LEN) != 0) {
3697 dpp_auth_fail(auth,
3698 "Unexpected Responder Bootstrapping Key Hash value");
3699 wpa_hexdump(MSG_DEBUG,
3700 "DPP: Expected Responder Bootstrapping Key Hash",
3701 auth->peer_bi->pubkey_hash, SHA256_MAC_LEN);
3702 return NULL;
3703 }
3704
3705 i_bootstrap = dpp_get_attr(attr_start, attr_len,
3706 DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
3707 &i_bootstrap_len);
3708 if (i_bootstrap) {
3709 if (i_bootstrap_len != SHA256_MAC_LEN) {
3710 dpp_auth_fail(auth,
3711 "Invalid Initiator Bootstrapping Key Hash attribute");
3712 return NULL;
3713 }
3714 wpa_hexdump(MSG_MSGDUMP,
3715 "DPP: Initiator Bootstrapping Key Hash",
3716 i_bootstrap, i_bootstrap_len);
3717 if (!auth->own_bi ||
3718 os_memcmp(i_bootstrap, auth->own_bi->pubkey_hash,
3719 SHA256_MAC_LEN) != 0) {
3720 dpp_auth_fail(auth,
3721 "Initiator Bootstrapping Key Hash attribute did not match");
3722 return NULL;
3723 }
3724 } else if (auth->own_bi && auth->own_bi->type == DPP_BOOTSTRAP_PKEX) {
3725 /* PKEX bootstrapping mandates use of mutual authentication */
3726 dpp_auth_fail(auth,
3727 "Missing Initiator Bootstrapping Key Hash attribute");
3728 return NULL;
3729 }
3730
3731 auth->peer_version = 1; /* default to the first version */
3732 #ifdef CONFIG_DPP2
3733 version = dpp_get_attr(attr_start, attr_len, DPP_ATTR_PROTOCOL_VERSION,
3734 &version_len);
3735 if (version) {
3736 if (version_len < 1 || version[0] == 0) {
3737 dpp_auth_fail(auth,
3738 "Invalid Protocol Version attribute");
3739 return NULL;
3740 }
3741 auth->peer_version = version[0];
3742 wpa_printf(MSG_DEBUG, "DPP: Peer protocol version %u",
3743 auth->peer_version);
3744 }
3745 #endif /* CONFIG_DPP2 */
3746
3747 status = dpp_get_attr(attr_start, attr_len, DPP_ATTR_STATUS,
3748 &status_len);
3749 if (!status || status_len < 1) {
3750 dpp_auth_fail(auth,
3751 "Missing or invalid required DPP Status attribute");
3752 return NULL;
3753 }
3754 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
3755 auth->auth_resp_status = status[0];
3756 if (status[0] != DPP_STATUS_OK) {
3757 dpp_auth_resp_rx_status(auth, hdr, attr_start,
3758 attr_len, wrapped_data,
3759 wrapped_data_len, status[0]);
3760 return NULL;
3761 }
3762
3763 if (!i_bootstrap && auth->own_bi) {
3764 wpa_printf(MSG_DEBUG,
3765 "DPP: Responder decided not to use mutual authentication");
3766 auth->own_bi = NULL;
3767 }
3768
3769 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_AUTH_DIRECTION "mutual=%d",
3770 auth->own_bi != NULL);
3771
3772 r_proto = dpp_get_attr(attr_start, attr_len, DPP_ATTR_R_PROTOCOL_KEY,
3773 &r_proto_len);
3774 if (!r_proto) {
3775 dpp_auth_fail(auth,
3776 "Missing required Responder Protocol Key attribute");
3777 return NULL;
3778 }
3779 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Protocol Key",
3780 r_proto, r_proto_len);
3781
3782 /* N = pI * PR */
3783 pr = dpp_set_pubkey_point(auth->own_protocol_key, r_proto, r_proto_len);
3784 if (!pr) {
3785 dpp_auth_fail(auth, "Invalid Responder Protocol Key");
3786 return NULL;
3787 }
3788 dpp_debug_print_key("Peer (Responder) Protocol Key", pr);
3789
3790 if (dpp_ecdh(auth->own_protocol_key, pr, auth->Nx, &secret_len) < 0) {
3791 dpp_auth_fail(auth, "Failed to derive ECDH shared secret");
3792 goto fail;
3793 }
3794 EVP_PKEY_free(auth->peer_protocol_key);
3795 auth->peer_protocol_key = pr;
3796 pr = NULL;
3797
3798 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
3799 auth->Nx, auth->secret_len);
3800 auth->Nx_len = auth->secret_len;
3801
3802 if (dpp_derive_k2(auth->Nx, auth->secret_len, auth->k2,
3803 auth->curve->hash_len) < 0)
3804 goto fail;
3805
3806 addr[0] = hdr;
3807 len[0] = DPP_HDR_LEN;
3808 addr[1] = attr_start;
3809 len[1] = attr_len;
3810 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3811 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3812 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
3813 wrapped_data, wrapped_data_len);
3814 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
3815 unwrapped = os_malloc(unwrapped_len);
3816 if (!unwrapped)
3817 goto fail;
3818 if (aes_siv_decrypt(auth->k2, auth->curve->hash_len,
3819 wrapped_data, wrapped_data_len,
3820 2, addr, len, unwrapped) < 0) {
3821 dpp_auth_fail(auth, "AES-SIV decryption failed");
3822 goto fail;
3823 }
3824 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
3825 unwrapped, unwrapped_len);
3826
3827 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
3828 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
3829 goto fail;
3830 }
3831
3832 r_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_NONCE,
3833 &r_nonce_len);
3834 if (!r_nonce || r_nonce_len != auth->curve->nonce_len) {
3835 dpp_auth_fail(auth, "DPP: Missing or invalid R-nonce");
3836 goto fail;
3837 }
3838 wpa_hexdump(MSG_DEBUG, "DPP: R-nonce", r_nonce, r_nonce_len);
3839 os_memcpy(auth->r_nonce, r_nonce, r_nonce_len);
3840
3841 i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
3842 &i_nonce_len);
3843 if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
3844 dpp_auth_fail(auth, "Missing or invalid I-nonce");
3845 goto fail;
3846 }
3847 wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
3848 if (os_memcmp(auth->i_nonce, i_nonce, i_nonce_len) != 0) {
3849 dpp_auth_fail(auth, "I-nonce mismatch");
3850 goto fail;
3851 }
3852
3853 if (auth->own_bi) {
3854 /* Mutual authentication */
3855 if (dpp_auth_derive_l_initiator(auth) < 0)
3856 goto fail;
3857 }
3858
3859 r_capab = dpp_get_attr(unwrapped, unwrapped_len,
3860 DPP_ATTR_R_CAPABILITIES,
3861 &r_capab_len);
3862 if (!r_capab || r_capab_len < 1) {
3863 dpp_auth_fail(auth, "Missing or invalid R-capabilities");
3864 goto fail;
3865 }
3866 auth->r_capab = r_capab[0];
3867 wpa_printf(MSG_DEBUG, "DPP: R-capabilities: 0x%02x", auth->r_capab);
3868 role = auth->r_capab & DPP_CAPAB_ROLE_MASK;
3869 if ((auth->allowed_roles ==
3870 (DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE)) &&
3871 (role == DPP_CAPAB_CONFIGURATOR || role == DPP_CAPAB_ENROLLEE)) {
3872 /* Peer selected its role, so move from "either role" to the
3873 * role that is compatible with peer's selection. */
3874 auth->configurator = role == DPP_CAPAB_ENROLLEE;
3875 wpa_printf(MSG_DEBUG, "DPP: Acting as %s",
3876 auth->configurator ? "Configurator" : "Enrollee");
3877 } else if ((auth->configurator && role != DPP_CAPAB_ENROLLEE) ||
3878 (!auth->configurator && role != DPP_CAPAB_CONFIGURATOR)) {
3879 wpa_printf(MSG_DEBUG, "DPP: Incompatible role selection");
3880 wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL
3881 "Unexpected role in R-capabilities 0x%02x",
3882 role);
3883 if (role != DPP_CAPAB_ENROLLEE &&
3884 role != DPP_CAPAB_CONFIGURATOR)
3885 goto fail;
3886 bin_clear_free(unwrapped, unwrapped_len);
3887 auth->remove_on_tx_status = 1;
3888 return dpp_auth_build_conf(auth, DPP_STATUS_NOT_COMPATIBLE);
3889 }
3890
3891 wrapped2 = dpp_get_attr(unwrapped, unwrapped_len,
3892 DPP_ATTR_WRAPPED_DATA, &wrapped2_len);
3893 if (!wrapped2 || wrapped2_len < AES_BLOCK_SIZE) {
3894 dpp_auth_fail(auth,
3895 "Missing or invalid Secondary Wrapped Data");
3896 goto fail;
3897 }
3898
3899 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
3900 wrapped2, wrapped2_len);
3901
3902 if (dpp_derive_ke(auth, auth->ke, auth->curve->hash_len) < 0)
3903 goto fail;
3904
3905 unwrapped2_len = wrapped2_len - AES_BLOCK_SIZE;
3906 unwrapped2 = os_malloc(unwrapped2_len);
3907 if (!unwrapped2)
3908 goto fail;
3909 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
3910 wrapped2, wrapped2_len,
3911 0, NULL, NULL, unwrapped2) < 0) {
3912 dpp_auth_fail(auth, "AES-SIV decryption failed");
3913 goto fail;
3914 }
3915 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
3916 unwrapped2, unwrapped2_len);
3917
3918 if (dpp_check_attrs(unwrapped2, unwrapped2_len) < 0) {
3919 dpp_auth_fail(auth,
3920 "Invalid attribute in secondary unwrapped data");
3921 goto fail;
3922 }
3923
3924 r_auth = dpp_get_attr(unwrapped2, unwrapped2_len, DPP_ATTR_R_AUTH_TAG,
3925 &r_auth_len);
3926 if (!r_auth || r_auth_len != auth->curve->hash_len) {
3927 dpp_auth_fail(auth,
3928 "Missing or invalid Responder Authenticating Tag");
3929 goto fail;
3930 }
3931 wpa_hexdump(MSG_DEBUG, "DPP: Received Responder Authenticating Tag",
3932 r_auth, r_auth_len);
3933 /* R-auth' = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
3934 if (dpp_gen_r_auth(auth, r_auth2) < 0)
3935 goto fail;
3936 wpa_hexdump(MSG_DEBUG, "DPP: Calculated Responder Authenticating Tag",
3937 r_auth2, r_auth_len);
3938 if (os_memcmp(r_auth, r_auth2, r_auth_len) != 0) {
3939 dpp_auth_fail(auth, "Mismatching Responder Authenticating Tag");
3940 bin_clear_free(unwrapped, unwrapped_len);
3941 bin_clear_free(unwrapped2, unwrapped2_len);
3942 auth->remove_on_tx_status = 1;
3943 return dpp_auth_build_conf(auth, DPP_STATUS_AUTH_FAILURE);
3944 }
3945
3946 bin_clear_free(unwrapped, unwrapped_len);
3947 bin_clear_free(unwrapped2, unwrapped2_len);
3948
3949 #ifdef CONFIG_TESTING_OPTIONS
3950 if (dpp_test == DPP_TEST_AUTH_RESP_IN_PLACE_OF_CONF) {
3951 wpa_printf(MSG_INFO,
3952 "DPP: TESTING - Authentication Response in place of Confirm");
3953 if (dpp_auth_build_resp_ok(auth) < 0)
3954 return NULL;
3955 return wpabuf_dup(auth->resp_msg);
3956 }
3957 #endif /* CONFIG_TESTING_OPTIONS */
3958
3959 return dpp_auth_build_conf(auth, DPP_STATUS_OK);
3960
3961 fail:
3962 bin_clear_free(unwrapped, unwrapped_len);
3963 bin_clear_free(unwrapped2, unwrapped2_len);
3964 EVP_PKEY_free(pr);
3965 return NULL;
3966 }
3967
3968
3969 static int dpp_auth_conf_rx_failure(struct dpp_authentication *auth,
3970 const u8 *hdr,
3971 const u8 *attr_start, size_t attr_len,
3972 const u8 *wrapped_data,
3973 u16 wrapped_data_len,
3974 enum dpp_status_error status)
3975 {
3976 const u8 *addr[2];
3977 size_t len[2];
3978 u8 *unwrapped = NULL;
3979 size_t unwrapped_len = 0;
3980 const u8 *r_nonce;
3981 u16 r_nonce_len;
3982
3983 /* Authentication Confirm failure cases are expected to include
3984 * {R-nonce}k2 in the Wrapped Data attribute. */
3985
3986 addr[0] = hdr;
3987 len[0] = DPP_HDR_LEN;
3988 addr[1] = attr_start;
3989 len[1] = attr_len;
3990 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
3991 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
3992 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
3993 wrapped_data, wrapped_data_len);
3994 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
3995 unwrapped = os_malloc(unwrapped_len);
3996 if (!unwrapped) {
3997 dpp_auth_fail(auth, "Authentication failed");
3998 goto fail;
3999 }
4000 if (aes_siv_decrypt(auth->k2, auth->curve->hash_len,
4001 wrapped_data, wrapped_data_len,
4002 2, addr, len, unwrapped) < 0) {
4003 dpp_auth_fail(auth, "AES-SIV decryption failed");
4004 goto fail;
4005 }
4006 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
4007 unwrapped, unwrapped_len);
4008
4009 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
4010 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
4011 goto fail;
4012 }
4013
4014 r_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_NONCE,
4015 &r_nonce_len);
4016 if (!r_nonce || r_nonce_len != auth->curve->nonce_len) {
4017 dpp_auth_fail(auth, "DPP: Missing or invalid R-nonce");
4018 goto fail;
4019 }
4020 if (os_memcmp(r_nonce, auth->r_nonce, r_nonce_len) != 0) {
4021 wpa_hexdump(MSG_DEBUG, "DPP: Received R-nonce",
4022 r_nonce, r_nonce_len);
4023 wpa_hexdump(MSG_DEBUG, "DPP: Expected R-nonce",
4024 auth->r_nonce, r_nonce_len);
4025 dpp_auth_fail(auth, "R-nonce mismatch");
4026 goto fail;
4027 }
4028
4029 if (status == DPP_STATUS_NOT_COMPATIBLE)
4030 dpp_auth_fail(auth, "Peer reported incompatible R-capab role");
4031 else if (status == DPP_STATUS_AUTH_FAILURE)
4032 dpp_auth_fail(auth, "Peer reported authentication failure)");
4033
4034 fail:
4035 bin_clear_free(unwrapped, unwrapped_len);
4036 return -1;
4037 }
4038
4039
4040 int dpp_auth_conf_rx(struct dpp_authentication *auth, const u8 *hdr,
4041 const u8 *attr_start, size_t attr_len)
4042 {
4043 const u8 *r_bootstrap, *i_bootstrap, *wrapped_data, *status, *i_auth;
4044 u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len, status_len,
4045 i_auth_len;
4046 const u8 *addr[2];
4047 size_t len[2];
4048 u8 *unwrapped = NULL;
4049 size_t unwrapped_len = 0;
4050 u8 i_auth2[DPP_MAX_HASH_LEN];
4051
4052 #ifdef CONFIG_TESTING_OPTIONS
4053 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
4054 wpa_printf(MSG_INFO,
4055 "DPP: TESTING - stop at Authentication Confirm");
4056 return -1;
4057 }
4058 #endif /* CONFIG_TESTING_OPTIONS */
4059
4060 if (auth->initiator || !auth->own_bi) {
4061 dpp_auth_fail(auth, "Unexpected Authentication Confirm");
4062 return -1;
4063 }
4064
4065 auth->waiting_auth_conf = 0;
4066
4067 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
4068 &wrapped_data_len);
4069 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
4070 dpp_auth_fail(auth,
4071 "Missing or invalid required Wrapped Data attribute");
4072 return -1;
4073 }
4074 wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
4075 wrapped_data, wrapped_data_len);
4076
4077 attr_len = wrapped_data - 4 - attr_start;
4078
4079 r_bootstrap = dpp_get_attr(attr_start, attr_len,
4080 DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
4081 &r_bootstrap_len);
4082 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
4083 dpp_auth_fail(auth,
4084 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
4085 return -1;
4086 }
4087 wpa_hexdump(MSG_DEBUG, "DPP: Responder Bootstrapping Key Hash",
4088 r_bootstrap, r_bootstrap_len);
4089 if (os_memcmp(r_bootstrap, auth->own_bi->pubkey_hash,
4090 SHA256_MAC_LEN) != 0) {
4091 wpa_hexdump(MSG_DEBUG,
4092 "DPP: Expected Responder Bootstrapping Key Hash",
4093 auth->peer_bi->pubkey_hash, SHA256_MAC_LEN);
4094 dpp_auth_fail(auth,
4095 "Responder Bootstrapping Key Hash mismatch");
4096 return -1;
4097 }
4098
4099 i_bootstrap = dpp_get_attr(attr_start, attr_len,
4100 DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
4101 &i_bootstrap_len);
4102 if (i_bootstrap) {
4103 if (i_bootstrap_len != SHA256_MAC_LEN) {
4104 dpp_auth_fail(auth,
4105 "Invalid Initiator Bootstrapping Key Hash attribute");
4106 return -1;
4107 }
4108 wpa_hexdump(MSG_MSGDUMP,
4109 "DPP: Initiator Bootstrapping Key Hash",
4110 i_bootstrap, i_bootstrap_len);
4111 if (!auth->peer_bi ||
4112 os_memcmp(i_bootstrap, auth->peer_bi->pubkey_hash,
4113 SHA256_MAC_LEN) != 0) {
4114 dpp_auth_fail(auth,
4115 "Initiator Bootstrapping Key Hash mismatch");
4116 return -1;
4117 }
4118 } else if (auth->peer_bi) {
4119 /* Mutual authentication and peer did not include its
4120 * Bootstrapping Key Hash attribute. */
4121 dpp_auth_fail(auth,
4122 "Missing Initiator Bootstrapping Key Hash attribute");
4123 return -1;
4124 }
4125
4126 status = dpp_get_attr(attr_start, attr_len, DPP_ATTR_STATUS,
4127 &status_len);
4128 if (!status || status_len < 1) {
4129 dpp_auth_fail(auth,
4130 "Missing or invalid required DPP Status attribute");
4131 return -1;
4132 }
4133 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
4134 if (status[0] == DPP_STATUS_NOT_COMPATIBLE ||
4135 status[0] == DPP_STATUS_AUTH_FAILURE)
4136 return dpp_auth_conf_rx_failure(auth, hdr, attr_start,
4137 attr_len, wrapped_data,
4138 wrapped_data_len, status[0]);
4139
4140 if (status[0] != DPP_STATUS_OK) {
4141 dpp_auth_fail(auth, "Authentication failed");
4142 return -1;
4143 }
4144
4145 addr[0] = hdr;
4146 len[0] = DPP_HDR_LEN;
4147 addr[1] = attr_start;
4148 len[1] = attr_len;
4149 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
4150 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
4151 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
4152 wrapped_data, wrapped_data_len);
4153 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
4154 unwrapped = os_malloc(unwrapped_len);
4155 if (!unwrapped)
4156 return -1;
4157 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
4158 wrapped_data, wrapped_data_len,
4159 2, addr, len, unwrapped) < 0) {
4160 dpp_auth_fail(auth, "AES-SIV decryption failed");
4161 goto fail;
4162 }
4163 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
4164 unwrapped, unwrapped_len);
4165
4166 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
4167 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
4168 goto fail;
4169 }
4170
4171 i_auth = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_AUTH_TAG,
4172 &i_auth_len);
4173 if (!i_auth || i_auth_len != auth->curve->hash_len) {
4174 dpp_auth_fail(auth,
4175 "Missing or invalid Initiator Authenticating Tag");
4176 goto fail;
4177 }
4178 wpa_hexdump(MSG_DEBUG, "DPP: Received Initiator Authenticating Tag",
4179 i_auth, i_auth_len);
4180 /* I-auth' = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |] 1) */
4181 if (dpp_gen_i_auth(auth, i_auth2) < 0)
4182 goto fail;
4183 wpa_hexdump(MSG_DEBUG, "DPP: Calculated Initiator Authenticating Tag",
4184 i_auth2, i_auth_len);
4185 if (os_memcmp(i_auth, i_auth2, i_auth_len) != 0) {
4186 dpp_auth_fail(auth, "Mismatching Initiator Authenticating Tag");
4187 goto fail;
4188 }
4189
4190 bin_clear_free(unwrapped, unwrapped_len);
4191 dpp_auth_success(auth);
4192 return 0;
4193 fail:
4194 bin_clear_free(unwrapped, unwrapped_len);
4195 return -1;
4196 }
4197
4198
4199 static int bin_str_eq(const char *val, size_t len, const char *cmp)
4200 {
4201 return os_strlen(cmp) == len && os_memcmp(val, cmp, len) == 0;
4202 }
4203
4204
4205 struct dpp_configuration * dpp_configuration_alloc(const char *type)
4206 {
4207 struct dpp_configuration *conf;
4208 const char *end;
4209 size_t len;
4210
4211 conf = os_zalloc(sizeof(*conf));
4212 if (!conf)
4213 goto fail;
4214
4215 end = os_strchr(type, ' ');
4216 if (end)
4217 len = end - type;
4218 else
4219 len = os_strlen(type);
4220
4221 if (bin_str_eq(type, len, "psk"))
4222 conf->akm = DPP_AKM_PSK;
4223 else if (bin_str_eq(type, len, "sae"))
4224 conf->akm = DPP_AKM_SAE;
4225 else if (bin_str_eq(type, len, "psk-sae") ||
4226 bin_str_eq(type, len, "psk+sae"))
4227 conf->akm = DPP_AKM_PSK_SAE;
4228 else if (bin_str_eq(type, len, "sae-dpp") ||
4229 bin_str_eq(type, len, "dpp+sae"))
4230 conf->akm = DPP_AKM_SAE_DPP;
4231 else if (bin_str_eq(type, len, "psk-sae-dpp") ||
4232 bin_str_eq(type, len, "dpp+psk+sae"))
4233 conf->akm = DPP_AKM_PSK_SAE_DPP;
4234 else if (bin_str_eq(type, len, "dpp"))
4235 conf->akm = DPP_AKM_DPP;
4236 else
4237 goto fail;
4238
4239 return conf;
4240 fail:
4241 dpp_configuration_free(conf);
4242 return NULL;
4243 }
4244
4245
4246 int dpp_akm_psk(enum dpp_akm akm)
4247 {
4248 return akm == DPP_AKM_PSK || akm == DPP_AKM_PSK_SAE ||
4249 akm == DPP_AKM_PSK_SAE_DPP;
4250 }
4251
4252
4253 int dpp_akm_sae(enum dpp_akm akm)
4254 {
4255 return akm == DPP_AKM_SAE || akm == DPP_AKM_PSK_SAE ||
4256 akm == DPP_AKM_SAE_DPP || akm == DPP_AKM_PSK_SAE_DPP;
4257 }
4258
4259
4260 int dpp_akm_legacy(enum dpp_akm akm)
4261 {
4262 return akm == DPP_AKM_PSK || akm == DPP_AKM_PSK_SAE ||
4263 akm == DPP_AKM_SAE;
4264 }
4265
4266
4267 int dpp_akm_dpp(enum dpp_akm akm)
4268 {
4269 return akm == DPP_AKM_DPP || akm == DPP_AKM_SAE_DPP ||
4270 akm == DPP_AKM_PSK_SAE_DPP;
4271 }
4272
4273
4274 int dpp_akm_ver2(enum dpp_akm akm)
4275 {
4276 return akm == DPP_AKM_SAE_DPP || akm == DPP_AKM_PSK_SAE_DPP;
4277 }
4278
4279
4280 int dpp_configuration_valid(const struct dpp_configuration *conf)
4281 {
4282 if (conf->ssid_len == 0)
4283 return 0;
4284 if (dpp_akm_psk(conf->akm) && !conf->passphrase && !conf->psk_set)
4285 return 0;
4286 if (dpp_akm_sae(conf->akm) && !conf->passphrase)
4287 return 0;
4288 return 1;
4289 }
4290
4291
4292 void dpp_configuration_free(struct dpp_configuration *conf)
4293 {
4294 if (!conf)
4295 return;
4296 str_clear_free(conf->passphrase);
4297 os_free(conf->group_id);
4298 bin_clear_free(conf, sizeof(*conf));
4299 }
4300
4301
4302 static int dpp_configuration_parse(struct dpp_authentication *auth,
4303 const char *cmd)
4304 {
4305 const char *pos, *end;
4306 struct dpp_configuration *conf_sta = NULL, *conf_ap = NULL;
4307 struct dpp_configuration *conf = NULL;
4308
4309 pos = os_strstr(cmd, " conf=sta-");
4310 if (pos) {
4311 conf_sta = dpp_configuration_alloc(pos + 10);
4312 if (!conf_sta)
4313 goto fail;
4314 conf = conf_sta;
4315 }
4316
4317 pos = os_strstr(cmd, " conf=ap-");
4318 if (pos) {
4319 conf_ap = dpp_configuration_alloc(pos + 9);
4320 if (!conf_ap)
4321 goto fail;
4322 conf = conf_ap;
4323 }
4324
4325 if (!conf)
4326 return 0;
4327
4328 pos = os_strstr(cmd, " ssid=");
4329 if (pos) {
4330 pos += 6;
4331 end = os_strchr(pos, ' ');
4332 conf->ssid_len = end ? (size_t) (end - pos) : os_strlen(pos);
4333 conf->ssid_len /= 2;
4334 if (conf->ssid_len > sizeof(conf->ssid) ||
4335 hexstr2bin(pos, conf->ssid, conf->ssid_len) < 0)
4336 goto fail;
4337 } else {
4338 #ifdef CONFIG_TESTING_OPTIONS
4339 /* use a default SSID for legacy testing reasons */
4340 os_memcpy(conf->ssid, "test", 4);
4341 conf->ssid_len = 4;
4342 #else /* CONFIG_TESTING_OPTIONS */
4343 goto fail;
4344 #endif /* CONFIG_TESTING_OPTIONS */
4345 }
4346
4347 pos = os_strstr(cmd, " pass=");
4348 if (pos) {
4349 size_t pass_len;
4350
4351 pos += 6;
4352 end = os_strchr(pos, ' ');
4353 pass_len = end ? (size_t) (end - pos) : os_strlen(pos);
4354 pass_len /= 2;
4355 if (pass_len > 63 || pass_len < 8)
4356 goto fail;
4357 conf->passphrase = os_zalloc(pass_len + 1);
4358 if (!conf->passphrase ||
4359 hexstr2bin(pos, (u8 *) conf->passphrase, pass_len) < 0)
4360 goto fail;
4361 }
4362
4363 pos = os_strstr(cmd, " psk=");
4364 if (pos) {
4365 pos += 5;
4366 if (hexstr2bin(pos, conf->psk, PMK_LEN) < 0)
4367 goto fail;
4368 conf->psk_set = 1;
4369 }
4370
4371 pos = os_strstr(cmd, " group_id=");
4372 if (pos) {
4373 size_t group_id_len;
4374
4375 pos += 10;
4376 end = os_strchr(pos, ' ');
4377 group_id_len = end ? (size_t) (end - pos) : os_strlen(pos);
4378 conf->group_id = os_malloc(group_id_len + 1);
4379 if (!conf->group_id)
4380 goto fail;
4381 os_memcpy(conf->group_id, pos, group_id_len);
4382 conf->group_id[group_id_len] = '\0';
4383 }
4384
4385 pos = os_strstr(cmd, " expiry=");
4386 if (pos) {
4387 long int val;
4388
4389 pos += 8;
4390 val = strtol(pos, NULL, 0);
4391 if (val <= 0)
4392 goto fail;
4393 conf->netaccesskey_expiry = val;
4394 }
4395
4396 if (!dpp_configuration_valid(conf))
4397 goto fail;
4398
4399 auth->conf_sta = conf_sta;
4400 auth->conf_ap = conf_ap;
4401 return 0;
4402
4403 fail:
4404 dpp_configuration_free(conf_sta);
4405 dpp_configuration_free(conf_ap);
4406 return -1;
4407 }
4408
4409
4410 static struct dpp_configurator *
4411 dpp_configurator_get_id(struct dpp_global *dpp, unsigned int id)
4412 {
4413 struct dpp_configurator *conf;
4414
4415 if (!dpp)
4416 return NULL;
4417
4418 dl_list_for_each(conf, &dpp->configurator,
4419 struct dpp_configurator, list) {
4420 if (conf->id == id)
4421 return conf;
4422 }
4423 return NULL;
4424 }
4425
4426
4427 int dpp_set_configurator(struct dpp_global *dpp, void *msg_ctx,
4428 struct dpp_authentication *auth,
4429 const char *cmd)
4430 {
4431 const char *pos;
4432
4433 if (!cmd)
4434 return 0;
4435
4436 wpa_printf(MSG_DEBUG, "DPP: Set configurator parameters: %s", cmd);
4437
4438 pos = os_strstr(cmd, " configurator=");
4439 if (pos) {
4440 pos += 14;
4441 auth->conf = dpp_configurator_get_id(dpp, atoi(pos));
4442 if (!auth->conf) {
4443 wpa_printf(MSG_INFO,
4444 "DPP: Could not find the specified configurator");
4445 return -1;
4446 }
4447 }
4448
4449 if (dpp_configuration_parse(auth, cmd) < 0) {
4450 wpa_msg(msg_ctx, MSG_INFO,
4451 "DPP: Failed to set configurator parameters");
4452 return -1;
4453 }
4454 return 0;
4455 }
4456
4457
4458 void dpp_auth_deinit(struct dpp_authentication *auth)
4459 {
4460 if (!auth)
4461 return;
4462 dpp_configuration_free(auth->conf_ap);
4463 dpp_configuration_free(auth->conf_sta);
4464 EVP_PKEY_free(auth->own_protocol_key);
4465 EVP_PKEY_free(auth->peer_protocol_key);
4466 wpabuf_free(auth->req_msg);
4467 wpabuf_free(auth->resp_msg);
4468 wpabuf_free(auth->conf_req);
4469 os_free(auth->connector);
4470 wpabuf_free(auth->net_access_key);
4471 wpabuf_free(auth->c_sign_key);
4472 dpp_bootstrap_info_free(auth->tmp_own_bi);
4473 #ifdef CONFIG_TESTING_OPTIONS
4474 os_free(auth->config_obj_override);
4475 os_free(auth->discovery_override);
4476 os_free(auth->groups_override);
4477 #endif /* CONFIG_TESTING_OPTIONS */
4478 bin_clear_free(auth, sizeof(*auth));
4479 }
4480
4481
4482 static struct wpabuf *
4483 dpp_build_conf_start(struct dpp_authentication *auth,
4484 struct dpp_configuration *conf, size_t tailroom)
4485 {
4486 struct wpabuf *buf;
4487 char ssid[6 * sizeof(conf->ssid) + 1];
4488
4489 #ifdef CONFIG_TESTING_OPTIONS
4490 if (auth->discovery_override)
4491 tailroom += os_strlen(auth->discovery_override);
4492 #endif /* CONFIG_TESTING_OPTIONS */
4493
4494 buf = wpabuf_alloc(200 + tailroom);
4495 if (!buf)
4496 return NULL;
4497 wpabuf_put_str(buf, "{\"wi-fi_tech\":\"infra\",\"discovery\":");
4498 #ifdef CONFIG_TESTING_OPTIONS
4499 if (auth->discovery_override) {
4500 wpa_printf(MSG_DEBUG, "DPP: TESTING - discovery override: '%s'",
4501 auth->discovery_override);
4502 wpabuf_put_str(buf, auth->discovery_override);
4503 wpabuf_put_u8(buf, ',');
4504 return buf;
4505 }
4506 #endif /* CONFIG_TESTING_OPTIONS */
4507 wpabuf_put_str(buf, "{\"ssid\":\"");
4508 json_escape_string(ssid, sizeof(ssid),
4509 (const char *) conf->ssid, conf->ssid_len);
4510 wpabuf_put_str(buf, ssid);
4511 wpabuf_put_str(buf, "\"},");
4512
4513 return buf;
4514 }
4515
4516
4517 static int dpp_build_jwk(struct wpabuf *buf, const char *name, EVP_PKEY *key,
4518 const char *kid, const struct dpp_curve_params *curve)
4519 {
4520 struct wpabuf *pub;
4521 const u8 *pos;
4522 char *x = NULL, *y = NULL;
4523 int ret = -1;
4524
4525 pub = dpp_get_pubkey_point(key, 0);
4526 if (!pub)
4527 goto fail;
4528 pos = wpabuf_head(pub);
4529 x = (char *) base64_url_encode(pos, curve->prime_len, NULL, 0);
4530 pos += curve->prime_len;
4531 y = (char *) base64_url_encode(pos, curve->prime_len, NULL, 0);
4532 if (!x || !y)
4533 goto fail;
4534
4535 wpabuf_put_str(buf, "\"");
4536 wpabuf_put_str(buf, name);
4537 wpabuf_put_str(buf, "\":{\"kty\":\"EC\",\"crv\":\"");
4538 wpabuf_put_str(buf, curve->jwk_crv);
4539 wpabuf_put_str(buf, "\",\"x\":\"");
4540 wpabuf_put_str(buf, x);
4541 wpabuf_put_str(buf, "\",\"y\":\"");
4542 wpabuf_put_str(buf, y);
4543 if (kid) {
4544 wpabuf_put_str(buf, "\",\"kid\":\"");
4545 wpabuf_put_str(buf, kid);
4546 }
4547 wpabuf_put_str(buf, "\"}");
4548 ret = 0;
4549 fail:
4550 wpabuf_free(pub);
4551 os_free(x);
4552 os_free(y);
4553 return ret;
4554 }
4555
4556
4557 static void dpp_build_legacy_cred_params(struct wpabuf *buf,
4558 struct dpp_configuration *conf)
4559 {
4560 if (conf->passphrase && os_strlen(conf->passphrase) < 64) {
4561 char pass[63 * 6 + 1];
4562
4563 json_escape_string(pass, sizeof(pass), conf->passphrase,
4564 os_strlen(conf->passphrase));
4565 wpabuf_put_str(buf, "\"pass\":\"");
4566 wpabuf_put_str(buf, pass);
4567 wpabuf_put_str(buf, "\"");
4568 os_memset(pass, 0, sizeof(pass));
4569 } else if (conf->psk_set) {
4570 char psk[2 * sizeof(conf->psk) + 1];
4571
4572 wpa_snprintf_hex(psk, sizeof(psk),
4573 conf->psk, sizeof(conf->psk));
4574 wpabuf_put_str(buf, "\"psk_hex\":\"");
4575 wpabuf_put_str(buf, psk);
4576 wpabuf_put_str(buf, "\"");
4577 os_memset(psk, 0, sizeof(psk));
4578 }
4579 }
4580
4581
4582 static struct wpabuf *
4583 dpp_build_conf_obj_dpp(struct dpp_authentication *auth, int ap,
4584 struct dpp_configuration *conf)
4585 {
4586 struct wpabuf *buf = NULL;
4587 char *signed1 = NULL, *signed2 = NULL, *signed3 = NULL;
4588 size_t tailroom;
4589 const struct dpp_curve_params *curve;
4590 char jws_prot_hdr[100];
4591 size_t signed1_len, signed2_len, signed3_len;
4592 struct wpabuf *dppcon = NULL;
4593 unsigned char *signature = NULL;
4594 const unsigned char *p;
4595 size_t signature_len;
4596 EVP_MD_CTX *md_ctx = NULL;
4597 ECDSA_SIG *sig = NULL;
4598 char *dot = ".";
4599 const EVP_MD *sign_md;
4600 const BIGNUM *r, *s;
4601 size_t extra_len = 1000;
4602 int incl_legacy;
4603 enum dpp_akm akm;
4604
4605 if (!auth->conf) {
4606 wpa_printf(MSG_INFO,
4607 "DPP: No configurator specified - cannot generate DPP config object");
4608 goto fail;
4609 }
4610 curve = auth->conf->curve;
4611 if (curve->hash_len == SHA256_MAC_LEN) {
4612 sign_md = EVP_sha256();
4613 } else if (curve->hash_len == SHA384_MAC_LEN) {
4614 sign_md = EVP_sha384();
4615 } else if (curve->hash_len == SHA512_MAC_LEN) {
4616 sign_md = EVP_sha512();
4617 } else {
4618 wpa_printf(MSG_DEBUG, "DPP: Unknown signature algorithm");
4619 goto fail;
4620 }
4621
4622 akm = conf->akm;
4623 if (dpp_akm_ver2(akm) && auth->peer_version < 2) {
4624 wpa_printf(MSG_DEBUG,
4625 "DPP: Convert DPP+legacy credential to DPP-only for peer that does not support version 2");
4626 akm = DPP_AKM_DPP;
4627 }
4628
4629 #ifdef CONFIG_TESTING_OPTIONS
4630 if (auth->groups_override)
4631 extra_len += os_strlen(auth->groups_override);
4632 #endif /* CONFIG_TESTING_OPTIONS */
4633
4634 if (conf->group_id)
4635 extra_len += os_strlen(conf->group_id);
4636
4637 /* Connector (JSON dppCon object) */
4638 dppcon = wpabuf_alloc(extra_len + 2 * auth->curve->prime_len * 4 / 3);
4639 if (!dppcon)
4640 goto fail;
4641 #ifdef CONFIG_TESTING_OPTIONS
4642 if (auth->groups_override) {
4643 wpabuf_put_u8(dppcon, '{');
4644 if (auth->groups_override) {
4645 wpa_printf(MSG_DEBUG,
4646 "DPP: TESTING - groups override: '%s'",
4647 auth->groups_override);
4648 wpabuf_put_str(dppcon, "\"groups\":");
4649 wpabuf_put_str(dppcon, auth->groups_override);
4650 wpabuf_put_u8(dppcon, ',');
4651 }
4652 goto skip_groups;
4653 }
4654 #endif /* CONFIG_TESTING_OPTIONS */
4655 wpabuf_printf(dppcon, "{\"groups\":[{\"groupId\":\"%s\",",
4656 conf->group_id ? conf->group_id : "*");
4657 wpabuf_printf(dppcon, "\"netRole\":\"%s\"}],", ap ? "ap" : "sta");
4658 #ifdef CONFIG_TESTING_OPTIONS
4659 skip_groups:
4660 #endif /* CONFIG_TESTING_OPTIONS */
4661 if (dpp_build_jwk(dppcon, "netAccessKey", auth->peer_protocol_key, NULL,
4662 auth->curve) < 0) {
4663 wpa_printf(MSG_DEBUG, "DPP: Failed to build netAccessKey JWK");
4664 goto fail;
4665 }
4666 if (conf->netaccesskey_expiry) {
4667 struct os_tm tm;
4668
4669 if (os_gmtime(conf->netaccesskey_expiry, &tm) < 0) {
4670 wpa_printf(MSG_DEBUG,
4671 "DPP: Failed to generate expiry string");
4672 goto fail;
4673 }
4674 wpabuf_printf(dppcon,
4675 ",\"expiry\":\"%04u-%02u-%02uT%02u:%02u:%02uZ\"",
4676 tm.year, tm.month, tm.day,
4677 tm.hour, tm.min, tm.sec);
4678 }
4679 wpabuf_put_u8(dppcon, '}');
4680 wpa_printf(MSG_DEBUG, "DPP: dppCon: %s",
4681 (const char *) wpabuf_head(dppcon));
4682
4683 os_snprintf(jws_prot_hdr, sizeof(jws_prot_hdr),
4684 "{\"typ\":\"dppCon\",\"kid\":\"%s\",\"alg\":\"%s\"}",
4685 auth->conf->kid, curve->jws_alg);
4686 signed1 = (char *) base64_url_encode((unsigned char *) jws_prot_hdr,
4687 os_strlen(jws_prot_hdr),
4688 &signed1_len, 0);
4689 signed2 = (char *) base64_url_encode(wpabuf_head(dppcon),
4690 wpabuf_len(dppcon),
4691 &signed2_len, 0);
4692 if (!signed1 || !signed2)
4693 goto fail;
4694
4695 md_ctx = EVP_MD_CTX_create();
4696 if (!md_ctx)
4697 goto fail;
4698
4699 ERR_clear_error();
4700 if (EVP_DigestSignInit(md_ctx, NULL, sign_md, NULL,
4701 auth->conf->csign) != 1) {
4702 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignInit failed: %s",
4703 ERR_error_string(ERR_get_error(), NULL));
4704 goto fail;
4705 }
4706 if (EVP_DigestSignUpdate(md_ctx, signed1, signed1_len) != 1 ||
4707 EVP_DigestSignUpdate(md_ctx, dot, 1) != 1 ||
4708 EVP_DigestSignUpdate(md_ctx, signed2, signed2_len) != 1) {
4709 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignUpdate failed: %s",
4710 ERR_error_string(ERR_get_error(), NULL));
4711 goto fail;
4712 }
4713 if (EVP_DigestSignFinal(md_ctx, NULL, &signature_len) != 1) {
4714 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignFinal failed: %s",
4715 ERR_error_string(ERR_get_error(), NULL));
4716 goto fail;
4717 }
4718 signature = os_malloc(signature_len);
4719 if (!signature)
4720 goto fail;
4721 if (EVP_DigestSignFinal(md_ctx, signature, &signature_len) != 1) {
4722 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignFinal failed: %s",
4723 ERR_error_string(ERR_get_error(), NULL));
4724 goto fail;
4725 }
4726 wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (DER)",
4727 signature, signature_len);
4728 /* Convert to raw coordinates r,s */
4729 p = signature;
4730 sig = d2i_ECDSA_SIG(NULL, &p, signature_len);
4731 if (!sig)
4732 goto fail;
4733 ECDSA_SIG_get0(sig, &r, &s);
4734 if (dpp_bn2bin_pad(r, signature, curve->prime_len) < 0 ||
4735 dpp_bn2bin_pad(s, signature + curve->prime_len,
4736 curve->prime_len) < 0)
4737 goto fail;
4738 signature_len = 2 * curve->prime_len;
4739 wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (raw r,s)",
4740 signature, signature_len);
4741 signed3 = (char *) base64_url_encode(signature, signature_len,
4742 &signed3_len, 0);
4743 if (!signed3)
4744 goto fail;
4745
4746 incl_legacy = dpp_akm_psk(akm) || dpp_akm_sae(akm);
4747 tailroom = 1000;
4748 tailroom += 2 * curve->prime_len * 4 / 3 + os_strlen(auth->conf->kid);
4749 tailroom += signed1_len + signed2_len + signed3_len;
4750 if (incl_legacy)
4751 tailroom += 1000;
4752 buf = dpp_build_conf_start(auth, conf, tailroom);
4753 if (!buf)
4754 goto fail;
4755
4756 wpabuf_printf(buf, "\"cred\":{\"akm\":\"%s\",", dpp_akm_str(akm));
4757 if (incl_legacy) {
4758 dpp_build_legacy_cred_params(buf, conf);
4759 wpabuf_put_str(buf, ",");
4760 }
4761 wpabuf_put_str(buf, "\"signedConnector\":\"");
4762 wpabuf_put_str(buf, signed1);
4763 wpabuf_put_u8(buf, '.');
4764 wpabuf_put_str(buf, signed2);
4765 wpabuf_put_u8(buf, '.');
4766 wpabuf_put_str(buf, signed3);
4767 wpabuf_put_str(buf, "\",");
4768 if (dpp_build_jwk(buf, "csign", auth->conf->csign, auth->conf->kid,
4769 curve) < 0) {
4770 wpa_printf(MSG_DEBUG, "DPP: Failed to build csign JWK");
4771 goto fail;
4772 }
4773
4774 wpabuf_put_str(buf, "}}");
4775
4776 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object",
4777 wpabuf_head(buf), wpabuf_len(buf));
4778
4779 out:
4780 EVP_MD_CTX_destroy(md_ctx);
4781 ECDSA_SIG_free(sig);
4782 os_free(signed1);
4783 os_free(signed2);
4784 os_free(signed3);
4785 os_free(signature);
4786 wpabuf_free(dppcon);
4787 return buf;
4788 fail:
4789 wpa_printf(MSG_DEBUG, "DPP: Failed to build configuration object");
4790 wpabuf_free(buf);
4791 buf = NULL;
4792 goto out;
4793 }
4794
4795
4796 static struct wpabuf *
4797 dpp_build_conf_obj_legacy(struct dpp_authentication *auth, int ap,
4798 struct dpp_configuration *conf)
4799 {
4800 struct wpabuf *buf;
4801
4802 buf = dpp_build_conf_start(auth, conf, 1000);
4803 if (!buf)
4804 return NULL;
4805
4806 wpabuf_printf(buf, "\"cred\":{\"akm\":\"%s\",", dpp_akm_str(conf->akm));
4807 dpp_build_legacy_cred_params(buf, conf);
4808 wpabuf_put_str(buf, "}}");
4809
4810 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object (legacy)",
4811 wpabuf_head(buf), wpabuf_len(buf));
4812
4813 return buf;
4814 }
4815
4816
4817 static struct wpabuf *
4818 dpp_build_conf_obj(struct dpp_authentication *auth, int ap)
4819 {
4820 struct dpp_configuration *conf;
4821
4822 #ifdef CONFIG_TESTING_OPTIONS
4823 if (auth->config_obj_override) {
4824 wpa_printf(MSG_DEBUG, "DPP: Testing - Config Object override");
4825 return wpabuf_alloc_copy(auth->config_obj_override,
4826 os_strlen(auth->config_obj_override));
4827 }
4828 #endif /* CONFIG_TESTING_OPTIONS */
4829
4830 conf = ap ? auth->conf_ap : auth->conf_sta;
4831 if (!conf) {
4832 wpa_printf(MSG_DEBUG,
4833 "DPP: No configuration available for Enrollee(%s) - reject configuration request",
4834 ap ? "ap" : "sta");
4835 return NULL;
4836 }
4837
4838 if (dpp_akm_dpp(conf->akm))
4839 return dpp_build_conf_obj_dpp(auth, ap, conf);
4840 return dpp_build_conf_obj_legacy(auth, ap, conf);
4841 }
4842
4843
4844 static struct wpabuf *
4845 dpp_build_conf_resp(struct dpp_authentication *auth, const u8 *e_nonce,
4846 u16 e_nonce_len, int ap)
4847 {
4848 struct wpabuf *conf;
4849 size_t clear_len, attr_len;
4850 struct wpabuf *clear = NULL, *msg = NULL;
4851 u8 *wrapped;
4852 const u8 *addr[1];
4853 size_t len[1];
4854 enum dpp_status_error status;
4855
4856 conf = dpp_build_conf_obj(auth, ap);
4857 if (conf) {
4858 wpa_hexdump_ascii(MSG_DEBUG, "DPP: configurationObject JSON",
4859 wpabuf_head(conf), wpabuf_len(conf));
4860 }
4861 status = conf ? DPP_STATUS_OK : DPP_STATUS_CONFIGURE_FAILURE;
4862 auth->conf_resp_status = status;
4863
4864 /* { E-nonce, configurationObject}ke */
4865 clear_len = 4 + e_nonce_len;
4866 if (conf)
4867 clear_len += 4 + wpabuf_len(conf);
4868 clear = wpabuf_alloc(clear_len);
4869 attr_len = 4 + 1 + 4 + clear_len + AES_BLOCK_SIZE;
4870 #ifdef CONFIG_TESTING_OPTIONS
4871 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP)
4872 attr_len += 5;
4873 #endif /* CONFIG_TESTING_OPTIONS */
4874 msg = wpabuf_alloc(attr_len);
4875 if (!clear || !msg)
4876 goto fail;
4877
4878 #ifdef CONFIG_TESTING_OPTIONS
4879 if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_RESP) {
4880 wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
4881 goto skip_e_nonce;
4882 }
4883 if (dpp_test == DPP_TEST_E_NONCE_MISMATCH_CONF_RESP) {
4884 wpa_printf(MSG_INFO, "DPP: TESTING - E-nonce mismatch");
4885 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
4886 wpabuf_put_le16(clear, e_nonce_len);
4887 wpabuf_put_data(clear, e_nonce, e_nonce_len - 1);
4888 wpabuf_put_u8(clear, e_nonce[e_nonce_len - 1] ^ 0x01);
4889 goto skip_e_nonce;
4890 }
4891 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_RESP) {
4892 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
4893 goto skip_wrapped_data;
4894 }
4895 #endif /* CONFIG_TESTING_OPTIONS */
4896
4897 /* E-nonce */
4898 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
4899 wpabuf_put_le16(clear, e_nonce_len);
4900 wpabuf_put_data(clear, e_nonce, e_nonce_len);
4901
4902 #ifdef CONFIG_TESTING_OPTIONS
4903 skip_e_nonce:
4904 if (dpp_test == DPP_TEST_NO_CONFIG_OBJ_CONF_RESP) {
4905 wpa_printf(MSG_INFO, "DPP: TESTING - Config Object");
4906 goto skip_config_obj;
4907 }
4908 #endif /* CONFIG_TESTING_OPTIONS */
4909
4910 if (conf) {
4911 wpabuf_put_le16(clear, DPP_ATTR_CONFIG_OBJ);
4912 wpabuf_put_le16(clear, wpabuf_len(conf));
4913 wpabuf_put_buf(clear, conf);
4914 }
4915
4916 #ifdef CONFIG_TESTING_OPTIONS
4917 skip_config_obj:
4918 if (dpp_test == DPP_TEST_NO_STATUS_CONF_RESP) {
4919 wpa_printf(MSG_INFO, "DPP: TESTING - Status");
4920 goto skip_status;
4921 }
4922 if (dpp_test == DPP_TEST_INVALID_STATUS_CONF_RESP) {
4923 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
4924 status = 255;
4925 }
4926 #endif /* CONFIG_TESTING_OPTIONS */
4927
4928 /* DPP Status */
4929 dpp_build_attr_status(msg, status);
4930
4931 #ifdef CONFIG_TESTING_OPTIONS
4932 skip_status:
4933 #endif /* CONFIG_TESTING_OPTIONS */
4934
4935 addr[0] = wpabuf_head(msg);
4936 len[0] = wpabuf_len(msg);
4937 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
4938
4939 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
4940 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
4941 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
4942
4943 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
4944 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
4945 wpabuf_head(clear), wpabuf_len(clear),
4946 1, addr, len, wrapped) < 0)
4947 goto fail;
4948 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
4949 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
4950
4951 #ifdef CONFIG_TESTING_OPTIONS
4952 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP) {
4953 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
4954 dpp_build_attr_status(msg, DPP_STATUS_OK);
4955 }
4956 skip_wrapped_data:
4957 #endif /* CONFIG_TESTING_OPTIONS */
4958
4959 wpa_hexdump_buf(MSG_DEBUG,
4960 "DPP: Configuration Response attributes", msg);
4961 out:
4962 wpabuf_free(conf);
4963 wpabuf_free(clear);
4964
4965 return msg;
4966 fail:
4967 wpabuf_free(msg);
4968 msg = NULL;
4969 goto out;
4970 }
4971
4972
4973 struct wpabuf *
4974 dpp_conf_req_rx(struct dpp_authentication *auth, const u8 *attr_start,
4975 size_t attr_len)
4976 {
4977 const u8 *wrapped_data, *e_nonce, *config_attr;
4978 u16 wrapped_data_len, e_nonce_len, config_attr_len;
4979 u8 *unwrapped = NULL;
4980 size_t unwrapped_len = 0;
4981 struct wpabuf *resp = NULL;
4982 struct json_token *root = NULL, *token;
4983 int ap;
4984
4985 #ifdef CONFIG_TESTING_OPTIONS
4986 if (dpp_test == DPP_TEST_STOP_AT_CONF_REQ) {
4987 wpa_printf(MSG_INFO,
4988 "DPP: TESTING - stop at Config Request");
4989 return NULL;
4990 }
4991 #endif /* CONFIG_TESTING_OPTIONS */
4992
4993 if (dpp_check_attrs(attr_start, attr_len) < 0) {
4994 dpp_auth_fail(auth, "Invalid attribute in config request");
4995 return NULL;
4996 }
4997
4998 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
4999 &wrapped_data_len);
5000 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
5001 dpp_auth_fail(auth,
5002 "Missing or invalid required Wrapped Data attribute");
5003 return NULL;
5004 }
5005
5006 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
5007 wrapped_data, wrapped_data_len);
5008 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
5009 unwrapped = os_malloc(unwrapped_len);
5010 if (!unwrapped)
5011 return NULL;
5012 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
5013 wrapped_data, wrapped_data_len,
5014 0, NULL, NULL, unwrapped) < 0) {
5015 dpp_auth_fail(auth, "AES-SIV decryption failed");
5016 goto fail;
5017 }
5018 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
5019 unwrapped, unwrapped_len);
5020
5021 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
5022 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
5023 goto fail;
5024 }
5025
5026 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
5027 DPP_ATTR_ENROLLEE_NONCE,
5028 &e_nonce_len);
5029 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
5030 dpp_auth_fail(auth,
5031 "Missing or invalid Enrollee Nonce attribute");
5032 goto fail;
5033 }
5034 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
5035 os_memcpy(auth->e_nonce, e_nonce, e_nonce_len);
5036
5037 config_attr = dpp_get_attr(unwrapped, unwrapped_len,
5038 DPP_ATTR_CONFIG_ATTR_OBJ,
5039 &config_attr_len);
5040 if (!config_attr) {
5041 dpp_auth_fail(auth,
5042 "Missing or invalid Config Attributes attribute");
5043 goto fail;
5044 }
5045 wpa_hexdump_ascii(MSG_DEBUG, "DPP: Config Attributes",
5046 config_attr, config_attr_len);
5047
5048 root = json_parse((const char *) config_attr, config_attr_len);
5049 if (!root) {
5050 dpp_auth_fail(auth, "Could not parse Config Attributes");
5051 goto fail;
5052 }
5053
5054 token = json_get_member(root, "name");
5055 if (!token || token->type != JSON_STRING) {
5056 dpp_auth_fail(auth, "No Config Attributes - name");
5057 goto fail;
5058 }
5059 wpa_printf(MSG_DEBUG, "DPP: Enrollee name = '%s'", token->string);
5060
5061 token = json_get_member(root, "wi-fi_tech");
5062 if (!token || token->type != JSON_STRING) {
5063 dpp_auth_fail(auth, "No Config Attributes - wi-fi_tech");
5064 goto fail;
5065 }
5066 wpa_printf(MSG_DEBUG, "DPP: wi-fi_tech = '%s'", token->string);
5067 if (os_strcmp(token->string, "infra") != 0) {
5068 wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech '%s'",
5069 token->string);
5070 dpp_auth_fail(auth, "Unsupported wi-fi_tech");
5071 goto fail;
5072 }
5073
5074 token = json_get_member(root, "netRole");
5075 if (!token || token->type != JSON_STRING) {
5076 dpp_auth_fail(auth, "No Config Attributes - netRole");
5077 goto fail;
5078 }
5079 wpa_printf(MSG_DEBUG, "DPP: netRole = '%s'", token->string);
5080 if (os_strcmp(token->string, "sta") == 0) {
5081 ap = 0;
5082 } else if (os_strcmp(token->string, "ap") == 0) {
5083 ap = 1;
5084 } else {
5085 wpa_printf(MSG_DEBUG, "DPP: Unsupported netRole '%s'",
5086 token->string);
5087 dpp_auth_fail(auth, "Unsupported netRole");
5088 goto fail;
5089 }
5090
5091 resp = dpp_build_conf_resp(auth, e_nonce, e_nonce_len, ap);
5092
5093 fail:
5094 json_free(root);
5095 os_free(unwrapped);
5096 return resp;
5097 }
5098
5099
5100 static struct wpabuf *
5101 dpp_parse_jws_prot_hdr(const struct dpp_curve_params *curve,
5102 const u8 *prot_hdr, u16 prot_hdr_len,
5103 const EVP_MD **ret_md)
5104 {
5105 struct json_token *root, *token;
5106 struct wpabuf *kid = NULL;
5107
5108 root = json_parse((const char *) prot_hdr, prot_hdr_len);
5109 if (!root) {
5110 wpa_printf(MSG_DEBUG,
5111 "DPP: JSON parsing failed for JWS Protected Header");
5112 goto fail;
5113 }
5114
5115 if (root->type != JSON_OBJECT) {
5116 wpa_printf(MSG_DEBUG,
5117 "DPP: JWS Protected Header root is not an object");
5118 goto fail;
5119 }
5120
5121 token = json_get_member(root, "typ");
5122 if (!token || token->type != JSON_STRING) {
5123 wpa_printf(MSG_DEBUG, "DPP: No typ string value found");
5124 goto fail;
5125 }
5126 wpa_printf(MSG_DEBUG, "DPP: JWS Protected Header typ=%s",
5127 token->string);
5128 if (os_strcmp(token->string, "dppCon") != 0) {
5129 wpa_printf(MSG_DEBUG,
5130 "DPP: Unsupported JWS Protected Header typ=%s",
5131 token->string);
5132 goto fail;
5133 }
5134
5135 token = json_get_member(root, "alg");
5136 if (!token || token->type != JSON_STRING) {
5137 wpa_printf(MSG_DEBUG, "DPP: No alg string value found");
5138 goto fail;
5139 }
5140 wpa_printf(MSG_DEBUG, "DPP: JWS Protected Header alg=%s",
5141 token->string);
5142 if (os_strcmp(token->string, curve->jws_alg) != 0) {
5143 wpa_printf(MSG_DEBUG,
5144 "DPP: Unexpected JWS Protected Header alg=%s (expected %s based on C-sign-key)",
5145 token->string, curve->jws_alg);
5146 goto fail;
5147 }
5148 if (os_strcmp(token->string, "ES256") == 0 ||
5149 os_strcmp(token->string, "BS256") == 0)
5150 *ret_md = EVP_sha256();
5151 else if (os_strcmp(token->string, "ES384") == 0 ||
5152 os_strcmp(token->string, "BS384") == 0)
5153 *ret_md = EVP_sha384();
5154 else if (os_strcmp(token->string, "ES512") == 0 ||
5155 os_strcmp(token->string, "BS512") == 0)
5156 *ret_md = EVP_sha512();
5157 else
5158 *ret_md = NULL;
5159 if (!*ret_md) {
5160 wpa_printf(MSG_DEBUG,
5161 "DPP: Unsupported JWS Protected Header alg=%s",
5162 token->string);
5163 goto fail;
5164 }
5165
5166 kid = json_get_member_base64url(root, "kid");
5167 if (!kid) {
5168 wpa_printf(MSG_DEBUG, "DPP: No kid string value found");
5169 goto fail;
5170 }
5171 wpa_hexdump_buf(MSG_DEBUG, "DPP: JWS Protected Header kid (decoded)",
5172 kid);
5173
5174 fail:
5175 json_free(root);
5176 return kid;
5177 }
5178
5179
5180 static int dpp_parse_cred_legacy(struct dpp_authentication *auth,
5181 struct json_token *cred)
5182 {
5183 struct json_token *pass, *psk_hex;
5184
5185 wpa_printf(MSG_DEBUG, "DPP: Legacy akm=psk credential");
5186
5187 pass = json_get_member(cred, "pass");
5188 psk_hex = json_get_member(cred, "psk_hex");
5189
5190 if (pass && pass->type == JSON_STRING) {
5191 size_t len = os_strlen(pass->string);
5192
5193 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Legacy passphrase",
5194 pass->string, len);
5195 if (len < 8 || len > 63)
5196 return -1;
5197 os_strlcpy(auth->passphrase, pass->string,
5198 sizeof(auth->passphrase));
5199 } else if (psk_hex && psk_hex->type == JSON_STRING) {
5200 if (dpp_akm_sae(auth->akm) && !dpp_akm_psk(auth->akm)) {
5201 wpa_printf(MSG_DEBUG,
5202 "DPP: Unexpected psk_hex with akm=sae");
5203 return -1;
5204 }
5205 if (os_strlen(psk_hex->string) != PMK_LEN * 2 ||
5206 hexstr2bin(psk_hex->string, auth->psk, PMK_LEN) < 0) {
5207 wpa_printf(MSG_DEBUG, "DPP: Invalid psk_hex encoding");
5208 return -1;
5209 }
5210 wpa_hexdump_key(MSG_DEBUG, "DPP: Legacy PSK",
5211 auth->psk, PMK_LEN);
5212 auth->psk_set = 1;
5213 } else {
5214 wpa_printf(MSG_DEBUG, "DPP: No pass or psk_hex strings found");
5215 return -1;
5216 }
5217
5218 if (dpp_akm_sae(auth->akm) && !auth->passphrase[0]) {
5219 wpa_printf(MSG_DEBUG, "DPP: No pass for sae found");
5220 return -1;
5221 }
5222
5223 return 0;
5224 }
5225
5226
5227 static EVP_PKEY * dpp_parse_jwk(struct json_token *jwk,
5228 const struct dpp_curve_params **key_curve)
5229 {
5230 struct json_token *token;
5231 const struct dpp_curve_params *curve;
5232 struct wpabuf *x = NULL, *y = NULL;
5233 EC_GROUP *group;
5234 EVP_PKEY *pkey = NULL;
5235
5236 token = json_get_member(jwk, "kty");
5237 if (!token || token->type != JSON_STRING) {
5238 wpa_printf(MSG_DEBUG, "DPP: No kty in JWK");
5239 goto fail;
5240 }
5241 if (os_strcmp(token->string, "EC") != 0) {
5242 wpa_printf(MSG_DEBUG, "DPP: Unexpected JWK kty '%s'",
5243 token->string);
5244 goto fail;
5245 }
5246
5247 token = json_get_member(jwk, "crv");
5248 if (!token || token->type != JSON_STRING) {
5249 wpa_printf(MSG_DEBUG, "DPP: No crv in JWK");
5250 goto fail;
5251 }
5252 curve = dpp_get_curve_jwk_crv(token->string);
5253 if (!curve) {
5254 wpa_printf(MSG_DEBUG, "DPP: Unsupported JWK crv '%s'",
5255 token->string);
5256 goto fail;
5257 }
5258
5259 x = json_get_member_base64url(jwk, "x");
5260 if (!x) {
5261 wpa_printf(MSG_DEBUG, "DPP: No x in JWK");
5262 goto fail;
5263 }
5264 wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK x", x);
5265 if (wpabuf_len(x) != curve->prime_len) {
5266 wpa_printf(MSG_DEBUG,
5267 "DPP: Unexpected JWK x length %u (expected %u for curve %s)",
5268 (unsigned int) wpabuf_len(x),
5269 (unsigned int) curve->prime_len, curve->name);
5270 goto fail;
5271 }
5272
5273 y = json_get_member_base64url(jwk, "y");
5274 if (!y) {
5275 wpa_printf(MSG_DEBUG, "DPP: No y in JWK");
5276 goto fail;
5277 }
5278 wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK y", y);
5279 if (wpabuf_len(y) != curve->prime_len) {
5280 wpa_printf(MSG_DEBUG,
5281 "DPP: Unexpected JWK y length %u (expected %u for curve %s)",
5282 (unsigned int) wpabuf_len(y),
5283 (unsigned int) curve->prime_len, curve->name);
5284 goto fail;
5285 }
5286
5287 group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
5288 if (!group) {
5289 wpa_printf(MSG_DEBUG, "DPP: Could not prepare group for JWK");
5290 goto fail;
5291 }
5292
5293 pkey = dpp_set_pubkey_point_group(group, wpabuf_head(x), wpabuf_head(y),
5294 wpabuf_len(x));
5295 EC_GROUP_free(group);
5296 *key_curve = curve;
5297
5298 fail:
5299 wpabuf_free(x);
5300 wpabuf_free(y);
5301
5302 return pkey;
5303 }
5304
5305
5306 int dpp_key_expired(const char *timestamp, os_time_t *expiry)
5307 {
5308 struct os_time now;
5309 unsigned int year, month, day, hour, min, sec;
5310 os_time_t utime;
5311 const char *pos;
5312
5313 /* ISO 8601 date and time:
5314 * <date>T<time>
5315 * YYYY-MM-DDTHH:MM:SSZ
5316 * YYYY-MM-DDTHH:MM:SS+03:00
5317 */
5318 if (os_strlen(timestamp) < 19) {
5319 wpa_printf(MSG_DEBUG,
5320 "DPP: Too short timestamp - assume expired key");
5321 return 1;
5322 }
5323 if (sscanf(timestamp, "%04u-%02u-%02uT%02u:%02u:%02u",
5324 &year, &month, &day, &hour, &min, &sec) != 6) {
5325 wpa_printf(MSG_DEBUG,
5326 "DPP: Failed to parse expiration day - assume expired key");
5327 return 1;
5328 }
5329
5330 if (os_mktime(year, month, day, hour, min, sec, &utime) < 0) {
5331 wpa_printf(MSG_DEBUG,
5332 "DPP: Invalid date/time information - assume expired key");
5333 return 1;
5334 }
5335
5336 pos = timestamp + 19;
5337 if (*pos == 'Z' || *pos == '\0') {
5338 /* In UTC - no need to adjust */
5339 } else if (*pos == '-' || *pos == '+') {
5340 int items;
5341
5342 /* Adjust local time to UTC */
5343 items = sscanf(pos + 1, "%02u:%02u", &hour, &min);
5344 if (items < 1) {
5345 wpa_printf(MSG_DEBUG,
5346 "DPP: Invalid time zone designator (%s) - assume expired key",
5347 pos);
5348 return 1;
5349 }
5350 if (*pos == '-')
5351 utime += 3600 * hour;
5352 if (*pos == '+')
5353 utime -= 3600 * hour;
5354 if (items > 1) {
5355 if (*pos == '-')
5356 utime += 60 * min;
5357 if (*pos == '+')
5358 utime -= 60 * min;
5359 }
5360 } else {
5361 wpa_printf(MSG_DEBUG,
5362 "DPP: Invalid time zone designator (%s) - assume expired key",
5363 pos);
5364 return 1;
5365 }
5366 if (expiry)
5367 *expiry = utime;
5368
5369 if (os_get_time(&now) < 0) {
5370 wpa_printf(MSG_DEBUG,
5371 "DPP: Cannot get current time - assume expired key");
5372 return 1;
5373 }
5374
5375 if (now.sec > utime) {
5376 wpa_printf(MSG_DEBUG, "DPP: Key has expired (%lu < %lu)",
5377 utime, now.sec);
5378 return 1;
5379 }
5380
5381 return 0;
5382 }
5383
5384
5385 static int dpp_parse_connector(struct dpp_authentication *auth,
5386 const unsigned char *payload,
5387 u16 payload_len)
5388 {
5389 struct json_token *root, *groups, *netkey, *token;
5390 int ret = -1;
5391 EVP_PKEY *key = NULL;
5392 const struct dpp_curve_params *curve;
5393 unsigned int rules = 0;
5394
5395 root = json_parse((const char *) payload, payload_len);
5396 if (!root) {
5397 wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
5398 goto fail;
5399 }
5400
5401 groups = json_get_member(root, "groups");
5402 if (!groups || groups->type != JSON_ARRAY) {
5403 wpa_printf(MSG_DEBUG, "DPP: No groups array found");
5404 goto skip_groups;
5405 }
5406 for (token = groups->child; token; token = token->sibling) {
5407 struct json_token *id, *role;
5408
5409 id = json_get_member(token, "groupId");
5410 if (!id || id->type != JSON_STRING) {
5411 wpa_printf(MSG_DEBUG, "DPP: Missing groupId string");
5412 goto fail;
5413 }
5414
5415 role = json_get_member(token, "netRole");
5416 if (!role || role->type != JSON_STRING) {
5417 wpa_printf(MSG_DEBUG, "DPP: Missing netRole string");
5418 goto fail;
5419 }
5420 wpa_printf(MSG_DEBUG,
5421 "DPP: connector group: groupId='%s' netRole='%s'",
5422 id->string, role->string);
5423 rules++;
5424 }
5425 skip_groups:
5426
5427 if (!rules) {
5428 wpa_printf(MSG_DEBUG,
5429 "DPP: Connector includes no groups");
5430 goto fail;
5431 }
5432
5433 token = json_get_member(root, "expiry");
5434 if (!token || token->type != JSON_STRING) {
5435 wpa_printf(MSG_DEBUG,
5436 "DPP: No expiry string found - connector does not expire");
5437 } else {
5438 wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
5439 if (dpp_key_expired(token->string,
5440 &auth->net_access_key_expiry)) {
5441 wpa_printf(MSG_DEBUG,
5442 "DPP: Connector (netAccessKey) has expired");
5443 goto fail;
5444 }
5445 }
5446
5447 netkey = json_get_member(root, "netAccessKey");
5448 if (!netkey || netkey->type != JSON_OBJECT) {
5449 wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
5450 goto fail;
5451 }
5452
5453 key = dpp_parse_jwk(netkey, &curve);
5454 if (!key)
5455 goto fail;
5456 dpp_debug_print_key("DPP: Received netAccessKey", key);
5457
5458 if (EVP_PKEY_cmp(key, auth->own_protocol_key) != 1) {
5459 wpa_printf(MSG_DEBUG,
5460 "DPP: netAccessKey in connector does not match own protocol key");
5461 #ifdef CONFIG_TESTING_OPTIONS
5462 if (auth->ignore_netaccesskey_mismatch) {
5463 wpa_printf(MSG_DEBUG,
5464 "DPP: TESTING - skip netAccessKey mismatch");
5465 } else {
5466 goto fail;
5467 }
5468 #else /* CONFIG_TESTING_OPTIONS */
5469 goto fail;
5470 #endif /* CONFIG_TESTING_OPTIONS */
5471 }
5472
5473 ret = 0;
5474 fail:
5475 EVP_PKEY_free(key);
5476 json_free(root);
5477 return ret;
5478 }
5479
5480
5481 static int dpp_check_pubkey_match(EVP_PKEY *pub, struct wpabuf *r_hash)
5482 {
5483 struct wpabuf *uncomp;
5484 int res;
5485 u8 hash[SHA256_MAC_LEN];
5486 const u8 *addr[1];
5487 size_t len[1];
5488
5489 if (wpabuf_len(r_hash) != SHA256_MAC_LEN)
5490 return -1;
5491 uncomp = dpp_get_pubkey_point(pub, 1);
5492 if (!uncomp)
5493 return -1;
5494 addr[0] = wpabuf_head(uncomp);
5495 len[0] = wpabuf_len(uncomp);
5496 wpa_hexdump(MSG_DEBUG, "DPP: Uncompressed public key",
5497 addr[0], len[0]);
5498 res = sha256_vector(1, addr, len, hash);
5499 wpabuf_free(uncomp);
5500 if (res < 0)
5501 return -1;
5502 if (os_memcmp(hash, wpabuf_head(r_hash), SHA256_MAC_LEN) != 0) {
5503 wpa_printf(MSG_DEBUG,
5504 "DPP: Received hash value does not match calculated public key hash value");
5505 wpa_hexdump(MSG_DEBUG, "DPP: Calculated hash",
5506 hash, SHA256_MAC_LEN);
5507 return -1;
5508 }
5509 return 0;
5510 }
5511
5512
5513 static void dpp_copy_csign(struct dpp_authentication *auth, EVP_PKEY *csign)
5514 {
5515 unsigned char *der = NULL;
5516 int der_len;
5517
5518 der_len = i2d_PUBKEY(csign, &der);
5519 if (der_len <= 0)
5520 return;
5521 wpabuf_free(auth->c_sign_key);
5522 auth->c_sign_key = wpabuf_alloc_copy(der, der_len);
5523 OPENSSL_free(der);
5524 }
5525
5526
5527 static void dpp_copy_netaccesskey(struct dpp_authentication *auth)
5528 {
5529 unsigned char *der = NULL;
5530 int der_len;
5531 EC_KEY *eckey;
5532
5533 eckey = EVP_PKEY_get1_EC_KEY(auth->own_protocol_key);
5534 if (!eckey)
5535 return;
5536
5537 der_len = i2d_ECPrivateKey(eckey, &der);
5538 if (der_len <= 0) {
5539 EC_KEY_free(eckey);
5540 return;
5541 }
5542 wpabuf_free(auth->net_access_key);
5543 auth->net_access_key = wpabuf_alloc_copy(der, der_len);
5544 OPENSSL_free(der);
5545 EC_KEY_free(eckey);
5546 }
5547
5548
5549 struct dpp_signed_connector_info {
5550 unsigned char *payload;
5551 size_t payload_len;
5552 };
5553
5554 static enum dpp_status_error
5555 dpp_process_signed_connector(struct dpp_signed_connector_info *info,
5556 EVP_PKEY *csign_pub, const char *connector)
5557 {
5558 enum dpp_status_error ret = 255;
5559 const char *pos, *end, *signed_start, *signed_end;
5560 struct wpabuf *kid = NULL;
5561 unsigned char *prot_hdr = NULL, *signature = NULL;
5562 size_t prot_hdr_len = 0, signature_len = 0;
5563 const EVP_MD *sign_md = NULL;
5564 unsigned char *der = NULL;
5565 int der_len;
5566 int res;
5567 EVP_MD_CTX *md_ctx = NULL;
5568 ECDSA_SIG *sig = NULL;
5569 BIGNUM *r = NULL, *s = NULL;
5570 const struct dpp_curve_params *curve;
5571 EC_KEY *eckey;
5572 const EC_GROUP *group;
5573 int nid;
5574
5575 eckey = EVP_PKEY_get1_EC_KEY(csign_pub);
5576 if (!eckey)
5577 goto fail;
5578 group = EC_KEY_get0_group(eckey);
5579 if (!group)
5580 goto fail;
5581 nid = EC_GROUP_get_curve_name(group);
5582 curve = dpp_get_curve_nid(nid);
5583 if (!curve)
5584 goto fail;
5585 wpa_printf(MSG_DEBUG, "DPP: C-sign-key group: %s", curve->jwk_crv);
5586 os_memset(info, 0, sizeof(*info));
5587
5588 signed_start = pos = connector;
5589 end = os_strchr(pos, '.');
5590 if (!end) {
5591 wpa_printf(MSG_DEBUG, "DPP: Missing dot(1) in signedConnector");
5592 ret = DPP_STATUS_INVALID_CONNECTOR;
5593 goto fail;
5594 }
5595 prot_hdr = base64_url_decode((const unsigned char *) pos,
5596 end - pos, &prot_hdr_len);
5597 if (!prot_hdr) {
5598 wpa_printf(MSG_DEBUG,
5599 "DPP: Failed to base64url decode signedConnector JWS Protected Header");
5600 ret = DPP_STATUS_INVALID_CONNECTOR;
5601 goto fail;
5602 }
5603 wpa_hexdump_ascii(MSG_DEBUG,
5604 "DPP: signedConnector - JWS Protected Header",
5605 prot_hdr, prot_hdr_len);
5606 kid = dpp_parse_jws_prot_hdr(curve, prot_hdr, prot_hdr_len, &sign_md);
5607 if (!kid) {
5608 ret = DPP_STATUS_INVALID_CONNECTOR;
5609 goto fail;
5610 }
5611 if (wpabuf_len(kid) != SHA256_MAC_LEN) {
5612 wpa_printf(MSG_DEBUG,
5613 "DPP: Unexpected signedConnector JWS Protected Header kid length: %u (expected %u)",
5614 (unsigned int) wpabuf_len(kid), SHA256_MAC_LEN);
5615 ret = DPP_STATUS_INVALID_CONNECTOR;
5616 goto fail;
5617 }
5618
5619 pos = end + 1;
5620 end = os_strchr(pos, '.');
5621 if (!end) {
5622 wpa_printf(MSG_DEBUG,
5623 "DPP: Missing dot(2) in signedConnector");
5624 ret = DPP_STATUS_INVALID_CONNECTOR;
5625 goto fail;
5626 }
5627 signed_end = end - 1;
5628 info->payload = base64_url_decode((const unsigned char *) pos,
5629 end - pos, &info->payload_len);
5630 if (!info->payload) {
5631 wpa_printf(MSG_DEBUG,
5632 "DPP: Failed to base64url decode signedConnector JWS Payload");
5633 ret = DPP_STATUS_INVALID_CONNECTOR;
5634 goto fail;
5635 }
5636 wpa_hexdump_ascii(MSG_DEBUG,
5637 "DPP: signedConnector - JWS Payload",
5638 info->payload, info->payload_len);
5639 pos = end + 1;
5640 signature = base64_url_decode((const unsigned char *) pos,
5641 os_strlen(pos), &signature_len);
5642 if (!signature) {
5643 wpa_printf(MSG_DEBUG,
5644 "DPP: Failed to base64url decode signedConnector signature");
5645 ret = DPP_STATUS_INVALID_CONNECTOR;
5646 goto fail;
5647 }
5648 wpa_hexdump(MSG_DEBUG, "DPP: signedConnector - signature",
5649 signature, signature_len);
5650
5651 if (dpp_check_pubkey_match(csign_pub, kid) < 0) {
5652 ret = DPP_STATUS_NO_MATCH;
5653 goto fail;
5654 }
5655
5656 if (signature_len & 0x01) {
5657 wpa_printf(MSG_DEBUG,
5658 "DPP: Unexpected signedConnector signature length (%d)",
5659 (int) signature_len);
5660 ret = DPP_STATUS_INVALID_CONNECTOR;
5661 goto fail;
5662 }
5663
5664 /* JWS Signature encodes the signature (r,s) as two octet strings. Need
5665 * to convert that to DER encoded ECDSA_SIG for OpenSSL EVP routines. */
5666 r = BN_bin2bn(signature, signature_len / 2, NULL);
5667 s = BN_bin2bn(signature + signature_len / 2, signature_len / 2, NULL);
5668 sig = ECDSA_SIG_new();
5669 if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1)
5670 goto fail;
5671 r = NULL;
5672 s = NULL;
5673
5674 der_len = i2d_ECDSA_SIG(sig, &der);
5675 if (der_len <= 0) {
5676 wpa_printf(MSG_DEBUG, "DPP: Could not DER encode signature");
5677 goto fail;
5678 }
5679 wpa_hexdump(MSG_DEBUG, "DPP: DER encoded signature", der, der_len);
5680 md_ctx = EVP_MD_CTX_create();
5681 if (!md_ctx)
5682 goto fail;
5683
5684 ERR_clear_error();
5685 if (EVP_DigestVerifyInit(md_ctx, NULL, sign_md, NULL, csign_pub) != 1) {
5686 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestVerifyInit failed: %s",
5687 ERR_error_string(ERR_get_error(), NULL));
5688 goto fail;
5689 }
5690 if (EVP_DigestVerifyUpdate(md_ctx, signed_start,
5691 signed_end - signed_start + 1) != 1) {
5692 wpa_printf(MSG_DEBUG, "DPP: EVP_DigestVerifyUpdate failed: %s",
5693 ERR_error_string(ERR_get_error(), NULL));
5694 goto fail;
5695 }
5696 res = EVP_DigestVerifyFinal(md_ctx, der, der_len);
5697 if (res != 1) {
5698 wpa_printf(MSG_DEBUG,
5699 "DPP: EVP_DigestVerifyFinal failed (res=%d): %s",
5700 res, ERR_error_string(ERR_get_error(), NULL));
5701 ret = DPP_STATUS_INVALID_CONNECTOR;
5702 goto fail;
5703 }
5704
5705 ret = DPP_STATUS_OK;
5706 fail:
5707 EC_KEY_free(eckey);
5708 EVP_MD_CTX_destroy(md_ctx);
5709 os_free(prot_hdr);
5710 wpabuf_free(kid);
5711 os_free(signature);
5712 ECDSA_SIG_free(sig);
5713 BN_free(r);
5714 BN_free(s);
5715 OPENSSL_free(der);
5716 return ret;
5717 }
5718
5719
5720 static int dpp_parse_cred_dpp(struct dpp_authentication *auth,
5721 struct json_token *cred)
5722 {
5723 struct dpp_signed_connector_info info;
5724 struct json_token *token, *csign;
5725 int ret = -1;
5726 EVP_PKEY *csign_pub = NULL;
5727 const struct dpp_curve_params *key_curve = NULL;
5728 const char *signed_connector;
5729
5730 os_memset(&info, 0, sizeof(info));
5731
5732 if (dpp_akm_psk(auth->akm) || dpp_akm_sae(auth->akm)) {
5733 wpa_printf(MSG_DEBUG,
5734 "DPP: Legacy credential included in Connector credential");
5735 if (dpp_parse_cred_legacy(auth, cred) < 0)
5736 return -1;
5737 }
5738
5739 wpa_printf(MSG_DEBUG, "DPP: Connector credential");
5740
5741 csign = json_get_member(cred, "csign");
5742 if (!csign || csign->type != JSON_OBJECT) {
5743 wpa_printf(MSG_DEBUG, "DPP: No csign JWK in JSON");
5744 goto fail;
5745 }
5746
5747 csign_pub = dpp_parse_jwk(csign, &key_curve);
5748 if (!csign_pub) {
5749 wpa_printf(MSG_DEBUG, "DPP: Failed to parse csign JWK");
5750 goto fail;
5751 }
5752 dpp_debug_print_key("DPP: Received C-sign-key", csign_pub);
5753
5754 token = json_get_member(cred, "signedConnector");
5755 if (!token || token->type != JSON_STRING) {
5756 wpa_printf(MSG_DEBUG, "DPP: No signedConnector string found");
5757 goto fail;
5758 }
5759 wpa_hexdump_ascii(MSG_DEBUG, "DPP: signedConnector",
5760 token->string, os_strlen(token->string));
5761 signed_connector = token->string;
5762
5763 if (os_strchr(signed_connector, '"') ||
5764 os_strchr(signed_connector, '\n')) {
5765 wpa_printf(MSG_DEBUG,
5766 "DPP: Unexpected character in signedConnector");
5767 goto fail;
5768 }
5769
5770 if (dpp_process_signed_connector(&info, csign_pub,
5771 signed_connector) != DPP_STATUS_OK)
5772 goto fail;
5773
5774 if (dpp_parse_connector(auth, info.payload, info.payload_len) < 0) {
5775 wpa_printf(MSG_DEBUG, "DPP: Failed to parse connector");
5776 goto fail;
5777 }
5778
5779 os_free(auth->connector);
5780 auth->connector = os_strdup(signed_connector);
5781
5782 dpp_copy_csign(auth, csign_pub);
5783 dpp_copy_netaccesskey(auth);
5784
5785 ret = 0;
5786 fail:
5787 EVP_PKEY_free(csign_pub);
5788 os_free(info.payload);
5789 return ret;
5790 }
5791
5792
5793 const char * dpp_akm_str(enum dpp_akm akm)
5794 {
5795 switch (akm) {
5796 case DPP_AKM_DPP:
5797 return "dpp";
5798 case DPP_AKM_PSK:
5799 return "psk";
5800 case DPP_AKM_SAE:
5801 return "sae";
5802 case DPP_AKM_PSK_SAE:
5803 return "psk+sae";
5804 case DPP_AKM_SAE_DPP:
5805 return "dpp+sae";
5806 case DPP_AKM_PSK_SAE_DPP:
5807 return "dpp+psk+sae";
5808 default:
5809 return "??";
5810 }
5811 }
5812
5813
5814 static enum dpp_akm dpp_akm_from_str(const char *akm)
5815 {
5816 if (os_strcmp(akm, "psk") == 0)
5817 return DPP_AKM_PSK;
5818 if (os_strcmp(akm, "sae") == 0)
5819 return DPP_AKM_SAE;
5820 if (os_strcmp(akm, "psk+sae") == 0)
5821 return DPP_AKM_PSK_SAE;
5822 if (os_strcmp(akm, "dpp") == 0)
5823 return DPP_AKM_DPP;
5824 if (os_strcmp(akm, "dpp+sae") == 0)
5825 return DPP_AKM_SAE_DPP;
5826 if (os_strcmp(akm, "dpp+psk+sae") == 0)
5827 return DPP_AKM_PSK_SAE_DPP;
5828 return DPP_AKM_UNKNOWN;
5829 }
5830
5831
5832 static int dpp_parse_conf_obj(struct dpp_authentication *auth,
5833 const u8 *conf_obj, u16 conf_obj_len)
5834 {
5835 int ret = -1;
5836 struct json_token *root, *token, *discovery, *cred;
5837
5838 root = json_parse((const char *) conf_obj, conf_obj_len);
5839 if (!root)
5840 return -1;
5841 if (root->type != JSON_OBJECT) {
5842 dpp_auth_fail(auth, "JSON root is not an object");
5843 goto fail;
5844 }
5845
5846 token = json_get_member(root, "wi-fi_tech");
5847 if (!token || token->type != JSON_STRING) {
5848 dpp_auth_fail(auth, "No wi-fi_tech string value found");
5849 goto fail;
5850 }
5851 if (os_strcmp(token->string, "infra") != 0) {
5852 wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech value: '%s'",
5853 token->string);
5854 dpp_auth_fail(auth, "Unsupported wi-fi_tech value");
5855 goto fail;
5856 }
5857
5858 discovery = json_get_member(root, "discovery");
5859 if (!discovery || discovery->type != JSON_OBJECT) {
5860 dpp_auth_fail(auth, "No discovery object in JSON");
5861 goto fail;
5862 }
5863
5864 token = json_get_member(discovery, "ssid");
5865 if (!token || token->type != JSON_STRING) {
5866 dpp_auth_fail(auth, "No discovery::ssid string value found");
5867 goto fail;
5868 }
5869 wpa_hexdump_ascii(MSG_DEBUG, "DPP: discovery::ssid",
5870 token->string, os_strlen(token->string));
5871 if (os_strlen(token->string) > SSID_MAX_LEN) {
5872 dpp_auth_fail(auth, "Too long discovery::ssid string value");
5873 goto fail;
5874 }
5875 auth->ssid_len = os_strlen(token->string);
5876 os_memcpy(auth->ssid, token->string, auth->ssid_len);
5877
5878 cred = json_get_member(root, "cred");
5879 if (!cred || cred->type != JSON_OBJECT) {
5880 dpp_auth_fail(auth, "No cred object in JSON");
5881 goto fail;
5882 }
5883
5884 token = json_get_member(cred, "akm");
5885 if (!token || token->type != JSON_STRING) {
5886 dpp_auth_fail(auth, "No cred::akm string value found");
5887 goto fail;
5888 }
5889 auth->akm = dpp_akm_from_str(token->string);
5890
5891 if (dpp_akm_legacy(auth->akm)) {
5892 if (dpp_parse_cred_legacy(auth, cred) < 0)
5893 goto fail;
5894 } else if (dpp_akm_dpp(auth->akm)) {
5895 if (dpp_parse_cred_dpp(auth, cred) < 0)
5896 goto fail;
5897 } else {
5898 wpa_printf(MSG_DEBUG, "DPP: Unsupported akm: %s",
5899 token->string);
5900 dpp_auth_fail(auth, "Unsupported akm");
5901 goto fail;
5902 }
5903
5904 wpa_printf(MSG_DEBUG, "DPP: JSON parsing completed successfully");
5905 ret = 0;
5906 fail:
5907 json_free(root);
5908 return ret;
5909 }
5910
5911
5912 int dpp_conf_resp_rx(struct dpp_authentication *auth,
5913 const struct wpabuf *resp)
5914 {
5915 const u8 *wrapped_data, *e_nonce, *status, *conf_obj;
5916 u16 wrapped_data_len, e_nonce_len, status_len, conf_obj_len;
5917 const u8 *addr[1];
5918 size_t len[1];
5919 u8 *unwrapped = NULL;
5920 size_t unwrapped_len = 0;
5921 int ret = -1;
5922
5923 auth->conf_resp_status = 255;
5924
5925 if (dpp_check_attrs(wpabuf_head(resp), wpabuf_len(resp)) < 0) {
5926 dpp_auth_fail(auth, "Invalid attribute in config response");
5927 return -1;
5928 }
5929
5930 wrapped_data = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
5931 DPP_ATTR_WRAPPED_DATA,
5932 &wrapped_data_len);
5933 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
5934 dpp_auth_fail(auth,
5935 "Missing or invalid required Wrapped Data attribute");
5936 return -1;
5937 }
5938
5939 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
5940 wrapped_data, wrapped_data_len);
5941 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
5942 unwrapped = os_malloc(unwrapped_len);
5943 if (!unwrapped)
5944 return -1;
5945
5946 addr[0] = wpabuf_head(resp);
5947 len[0] = wrapped_data - 4 - (const u8 *) wpabuf_head(resp);
5948 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
5949
5950 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
5951 wrapped_data, wrapped_data_len,
5952 1, addr, len, unwrapped) < 0) {
5953 dpp_auth_fail(auth, "AES-SIV decryption failed");
5954 goto fail;
5955 }
5956 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
5957 unwrapped, unwrapped_len);
5958
5959 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
5960 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
5961 goto fail;
5962 }
5963
5964 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
5965 DPP_ATTR_ENROLLEE_NONCE,
5966 &e_nonce_len);
5967 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
5968 dpp_auth_fail(auth,
5969 "Missing or invalid Enrollee Nonce attribute");
5970 goto fail;
5971 }
5972 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
5973 if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
5974 dpp_auth_fail(auth, "Enrollee Nonce mismatch");
5975 goto fail;
5976 }
5977
5978 status = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
5979 DPP_ATTR_STATUS, &status_len);
5980 if (!status || status_len < 1) {
5981 dpp_auth_fail(auth,
5982 "Missing or invalid required DPP Status attribute");
5983 goto fail;
5984 }
5985 auth->conf_resp_status = status[0];
5986 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
5987 if (status[0] != DPP_STATUS_OK) {
5988 dpp_auth_fail(auth, "Configurator rejected configuration");
5989 goto fail;
5990 }
5991
5992 conf_obj = dpp_get_attr(unwrapped, unwrapped_len,
5993 DPP_ATTR_CONFIG_OBJ, &conf_obj_len);
5994 if (!conf_obj) {
5995 dpp_auth_fail(auth,
5996 "Missing required Configuration Object attribute");
5997 goto fail;
5998 }
5999 wpa_hexdump_ascii(MSG_DEBUG, "DPP: configurationObject JSON",
6000 conf_obj, conf_obj_len);
6001 if (dpp_parse_conf_obj(auth, conf_obj, conf_obj_len) < 0)
6002 goto fail;
6003
6004 ret = 0;
6005
6006 fail:
6007 os_free(unwrapped);
6008 return ret;
6009 }
6010
6011
6012 #ifdef CONFIG_DPP2
6013
6014 enum dpp_status_error dpp_conf_result_rx(struct dpp_authentication *auth,
6015 const u8 *hdr,
6016 const u8 *attr_start, size_t attr_len)
6017 {
6018 const u8 *wrapped_data, *status, *e_nonce;
6019 u16 wrapped_data_len, status_len, e_nonce_len;
6020 const u8 *addr[2];
6021 size_t len[2];
6022 u8 *unwrapped = NULL;
6023 size_t unwrapped_len = 0;
6024 enum dpp_status_error ret = 256;
6025
6026 wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
6027 &wrapped_data_len);
6028 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
6029 dpp_auth_fail(auth,
6030 "Missing or invalid required Wrapped Data attribute");
6031 goto fail;
6032 }
6033 wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
6034 wrapped_data, wrapped_data_len);
6035
6036 attr_len = wrapped_data - 4 - attr_start;
6037
6038 addr[0] = hdr;
6039 len[0] = DPP_HDR_LEN;
6040 addr[1] = attr_start;
6041 len[1] = attr_len;
6042 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
6043 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
6044 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
6045 wrapped_data, wrapped_data_len);
6046 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
6047 unwrapped = os_malloc(unwrapped_len);
6048 if (!unwrapped)
6049 goto fail;
6050 if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
6051 wrapped_data, wrapped_data_len,
6052 2, addr, len, unwrapped) < 0) {
6053 dpp_auth_fail(auth, "AES-SIV decryption failed");
6054 goto fail;
6055 }
6056 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
6057 unwrapped, unwrapped_len);
6058
6059 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
6060 dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
6061 goto fail;
6062 }
6063
6064 e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
6065 DPP_ATTR_ENROLLEE_NONCE,
6066 &e_nonce_len);
6067 if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
6068 dpp_auth_fail(auth,
6069 "Missing or invalid Enrollee Nonce attribute");
6070 goto fail;
6071 }
6072 wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
6073 if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
6074 dpp_auth_fail(auth, "Enrollee Nonce mismatch");
6075 wpa_hexdump(MSG_DEBUG, "DPP: Expected Enrollee Nonce",
6076 auth->e_nonce, e_nonce_len);
6077 goto fail;
6078 }
6079
6080 status = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_STATUS,
6081 &status_len);
6082 if (!status || status_len < 1) {
6083 dpp_auth_fail(auth,
6084 "Missing or invalid required DPP Status attribute");
6085 goto fail;
6086 }
6087 wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
6088 ret = status[0];
6089
6090 fail:
6091 bin_clear_free(unwrapped, unwrapped_len);
6092 return ret;
6093 }
6094
6095
6096 struct wpabuf * dpp_build_conf_result(struct dpp_authentication *auth,
6097 enum dpp_status_error status)
6098 {
6099 struct wpabuf *msg, *clear;
6100 size_t nonce_len, clear_len, attr_len;
6101 const u8 *addr[2];
6102 size_t len[2];
6103 u8 *wrapped;
6104
6105 nonce_len = auth->curve->nonce_len;
6106 clear_len = 5 + 4 + nonce_len;
6107 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
6108 clear = wpabuf_alloc(clear_len);
6109 msg = dpp_alloc_msg(DPP_PA_CONFIGURATION_RESULT, attr_len);
6110 if (!clear || !msg)
6111 goto fail;
6112
6113 /* DPP Status */
6114 dpp_build_attr_status(clear, status);
6115
6116 /* E-nonce */
6117 wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
6118 wpabuf_put_le16(clear, nonce_len);
6119 wpabuf_put_data(clear, auth->e_nonce, nonce_len);
6120
6121 /* OUI, OUI type, Crypto Suite, DPP frame type */
6122 addr[0] = wpabuf_head_u8(msg) + 2;
6123 len[0] = 3 + 1 + 1 + 1;
6124 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
6125
6126 /* Attributes before Wrapped Data (none) */
6127 addr[1] = wpabuf_put(msg, 0);
6128 len[1] = 0;
6129 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
6130
6131 /* Wrapped Data */
6132 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
6133 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
6134 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
6135
6136 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
6137 if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
6138 wpabuf_head(clear), wpabuf_len(clear),
6139 2, addr, len, wrapped) < 0)
6140 goto fail;
6141
6142 wpa_hexdump_buf(MSG_DEBUG, "DPP: Configuration Result attributes", msg);
6143 wpabuf_free(clear);
6144 return msg;
6145 fail:
6146 wpabuf_free(clear);
6147 wpabuf_free(msg);
6148 return NULL;
6149 }
6150
6151 #endif /* CONFIG_DPP2 */
6152
6153
6154 void dpp_configurator_free(struct dpp_configurator *conf)
6155 {
6156 if (!conf)
6157 return;
6158 EVP_PKEY_free(conf->csign);
6159 os_free(conf->kid);
6160 os_free(conf);
6161 }
6162
6163
6164 int dpp_configurator_get_key(const struct dpp_configurator *conf, char *buf,
6165 size_t buflen)
6166 {
6167 EC_KEY *eckey;
6168 int key_len, ret = -1;
6169 unsigned char *key = NULL;
6170
6171 if (!conf->csign)
6172 return -1;
6173
6174 eckey = EVP_PKEY_get1_EC_KEY(conf->csign);
6175 if (!eckey)
6176 return -1;
6177
6178 key_len = i2d_ECPrivateKey(eckey, &key);
6179 if (key_len > 0)
6180 ret = wpa_snprintf_hex(buf, buflen, key, key_len);
6181
6182 EC_KEY_free(eckey);
6183 OPENSSL_free(key);
6184 return ret;
6185 }
6186
6187
6188 struct dpp_configurator *
6189 dpp_keygen_configurator(const char *curve, const u8 *privkey,
6190 size_t privkey_len)
6191 {
6192 struct dpp_configurator *conf;
6193 struct wpabuf *csign_pub = NULL;
6194 u8 kid_hash[SHA256_MAC_LEN];
6195 const u8 *addr[1];
6196 size_t len[1];
6197
6198 conf = os_zalloc(sizeof(*conf));
6199 if (!conf)
6200 return NULL;
6201
6202 if (!curve) {
6203 conf->curve = &dpp_curves[0];
6204 } else {
6205 conf->curve = dpp_get_curve_name(curve);
6206 if (!conf->curve) {
6207 wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
6208 curve);
6209 os_free(conf);
6210 return NULL;
6211 }
6212 }
6213 if (privkey)
6214 conf->csign = dpp_set_keypair(&conf->curve, privkey,
6215 privkey_len);
6216 else
6217 conf->csign = dpp_gen_keypair(conf->curve);
6218 if (!conf->csign)
6219 goto fail;
6220 conf->own = 1;
6221
6222 csign_pub = dpp_get_pubkey_point(conf->csign, 1);
6223 if (!csign_pub) {
6224 wpa_printf(MSG_INFO, "DPP: Failed to extract C-sign-key");
6225 goto fail;
6226 }
6227
6228 /* kid = SHA256(ANSI X9.63 uncompressed C-sign-key) */
6229 addr[0] = wpabuf_head(csign_pub);
6230 len[0] = wpabuf_len(csign_pub);
6231 if (sha256_vector(1, addr, len, kid_hash) < 0) {
6232 wpa_printf(MSG_DEBUG,
6233 "DPP: Failed to derive kid for C-sign-key");
6234 goto fail;
6235 }
6236
6237 conf->kid = (char *) base64_url_encode(kid_hash, sizeof(kid_hash),
6238 NULL, 0);
6239 if (!conf->kid)
6240 goto fail;
6241 out:
6242 wpabuf_free(csign_pub);
6243 return conf;
6244 fail:
6245 dpp_configurator_free(conf);
6246 conf = NULL;
6247 goto out;
6248 }
6249
6250
6251 int dpp_configurator_own_config(struct dpp_authentication *auth,
6252 const char *curve, int ap)
6253 {
6254 struct wpabuf *conf_obj;
6255 int ret = -1;
6256
6257 if (!auth->conf) {
6258 wpa_printf(MSG_DEBUG, "DPP: No configurator specified");
6259 return -1;
6260 }
6261
6262 if (!curve) {
6263 auth->curve = &dpp_curves[0];
6264 } else {
6265 auth->curve = dpp_get_curve_name(curve);
6266 if (!auth->curve) {
6267 wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
6268 curve);
6269 return -1;
6270 }
6271 }
6272 wpa_printf(MSG_DEBUG,
6273 "DPP: Building own configuration/connector with curve %s",
6274 auth->curve->name);
6275
6276 auth->own_protocol_key = dpp_gen_keypair(auth->curve);
6277 if (!auth->own_protocol_key)
6278 return -1;
6279 dpp_copy_netaccesskey(auth);
6280 auth->peer_protocol_key = auth->own_protocol_key;
6281 dpp_copy_csign(auth, auth->conf->csign);
6282
6283 conf_obj = dpp_build_conf_obj(auth, ap);
6284 if (!conf_obj)
6285 goto fail;
6286 ret = dpp_parse_conf_obj(auth, wpabuf_head(conf_obj),
6287 wpabuf_len(conf_obj));
6288 fail:
6289 wpabuf_free(conf_obj);
6290 auth->peer_protocol_key = NULL;
6291 return ret;
6292 }
6293
6294
6295 static int dpp_compatible_netrole(const char *role1, const char *role2)
6296 {
6297 return (os_strcmp(role1, "sta") == 0 && os_strcmp(role2, "ap") == 0) ||
6298 (os_strcmp(role1, "ap") == 0 && os_strcmp(role2, "sta") == 0);
6299 }
6300
6301
6302 static int dpp_connector_compatible_group(struct json_token *root,
6303 const char *group_id,
6304 const char *net_role)
6305 {
6306 struct json_token *groups, *token;
6307
6308 groups = json_get_member(root, "groups");
6309 if (!groups || groups->type != JSON_ARRAY)
6310 return 0;
6311
6312 for (token = groups->child; token; token = token->sibling) {
6313 struct json_token *id, *role;
6314
6315 id = json_get_member(token, "groupId");
6316 if (!id || id->type != JSON_STRING)
6317 continue;
6318
6319 role = json_get_member(token, "netRole");
6320 if (!role || role->type != JSON_STRING)
6321 continue;
6322
6323 if (os_strcmp(id->string, "*") != 0 &&
6324 os_strcmp(group_id, "*") != 0 &&
6325 os_strcmp(id->string, group_id) != 0)
6326 continue;
6327
6328 if (dpp_compatible_netrole(role->string, net_role))
6329 return 1;
6330 }
6331
6332 return 0;
6333 }
6334
6335
6336 static int dpp_connector_match_groups(struct json_token *own_root,
6337 struct json_token *peer_root)
6338 {
6339 struct json_token *groups, *token;
6340
6341 groups = json_get_member(peer_root, "groups");
6342 if (!groups || groups->type != JSON_ARRAY) {
6343 wpa_printf(MSG_DEBUG, "DPP: No peer groups array found");
6344 return 0;
6345 }
6346
6347 for (token = groups->child; token; token = token->sibling) {
6348 struct json_token *id, *role;
6349
6350 id = json_get_member(token, "groupId");
6351 if (!id || id->type != JSON_STRING) {
6352 wpa_printf(MSG_DEBUG,
6353 "DPP: Missing peer groupId string");
6354 continue;
6355 }
6356
6357 role = json_get_member(token, "netRole");
6358 if (!role || role->type != JSON_STRING) {
6359 wpa_printf(MSG_DEBUG,
6360 "DPP: Missing peer groups::netRole string");
6361 continue;
6362 }
6363 wpa_printf(MSG_DEBUG,
6364 "DPP: peer connector group: groupId='%s' netRole='%s'",
6365 id->string, role->string);
6366 if (dpp_connector_compatible_group(own_root, id->string,
6367 role->string)) {
6368 wpa_printf(MSG_DEBUG,
6369 "DPP: Compatible group/netRole in own connector");
6370 return 1;
6371 }
6372 }
6373
6374 return 0;
6375 }
6376
6377
6378 static int dpp_derive_pmk(const u8 *Nx, size_t Nx_len, u8 *pmk,
6379 unsigned int hash_len)
6380 {
6381 u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
6382 const char *info = "DPP PMK";
6383 int res;
6384
6385 /* PMK = HKDF(<>, "DPP PMK", N.x) */
6386
6387 /* HKDF-Extract(<>, N.x) */
6388 os_memset(salt, 0, hash_len);
6389 if (dpp_hmac(hash_len, salt, hash_len, Nx, Nx_len, prk) < 0)
6390 return -1;
6391 wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=N.x)",
6392 prk, hash_len);
6393
6394 /* HKDF-Expand(PRK, info, L) */
6395 res = dpp_hkdf_expand(hash_len, prk, hash_len, info, pmk, hash_len);
6396 os_memset(prk, 0, hash_len);
6397 if (res < 0)
6398 return -1;
6399
6400 wpa_hexdump_key(MSG_DEBUG, "DPP: PMK = HKDF-Expand(PRK, info, L)",
6401 pmk, hash_len);
6402 return 0;
6403 }
6404
6405
6406 static int dpp_derive_pmkid(const struct dpp_curve_params *curve,
6407 EVP_PKEY *own_key, EVP_PKEY *peer_key, u8 *pmkid)
6408 {
6409 struct wpabuf *nkx, *pkx;
6410 int ret = -1, res;
6411 const u8 *addr[2];
6412 size_t len[2];
6413 u8 hash[SHA256_MAC_LEN];
6414
6415 /* PMKID = Truncate-128(H(min(NK.x, PK.x) | max(NK.x, PK.x))) */
6416 nkx = dpp_get_pubkey_point(own_key, 0);
6417 pkx = dpp_get_pubkey_point(peer_key, 0);
6418 if (!nkx || !pkx)
6419 goto fail;
6420 addr[0] = wpabuf_head(nkx);
6421 len[0] = wpabuf_len(nkx) / 2;
6422 addr[1] = wpabuf_head(pkx);
6423 len[1] = wpabuf_len(pkx) / 2;
6424 if (len[0] != len[1])
6425 goto fail;
6426 if (os_memcmp(addr[0], addr[1], len[0]) > 0) {
6427 addr[0] = wpabuf_head(pkx);
6428 addr[1] = wpabuf_head(nkx);
6429 }
6430 wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash payload 1", addr[0], len[0]);
6431 wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash payload 2", addr[1], len[1]);
6432 res = sha256_vector(2, addr, len, hash);
6433 if (res < 0)
6434 goto fail;
6435 wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash output", hash, SHA256_MAC_LEN);
6436 os_memcpy(pmkid, hash, PMKID_LEN);
6437 wpa_hexdump(MSG_DEBUG, "DPP: PMKID", pmkid, PMKID_LEN);
6438 ret = 0;
6439 fail:
6440 wpabuf_free(nkx);
6441 wpabuf_free(pkx);
6442 return ret;
6443 }
6444
6445
6446 enum dpp_status_error
6447 dpp_peer_intro(struct dpp_introduction *intro, const char *own_connector,
6448 const u8 *net_access_key, size_t net_access_key_len,
6449 const u8 *csign_key, size_t csign_key_len,
6450 const u8 *peer_connector, size_t peer_connector_len,
6451 os_time_t *expiry)
6452 {
6453 struct json_token *root = NULL, *netkey, *token;
6454 struct json_token *own_root = NULL;
6455 enum dpp_status_error ret = 255, res;
6456 EVP_PKEY *own_key = NULL, *peer_key = NULL;
6457 struct wpabuf *own_key_pub = NULL;
6458 const struct dpp_curve_params *curve, *own_curve;
6459 struct dpp_signed_connector_info info;
6460 const unsigned char *p;
6461 EVP_PKEY *csign = NULL;
6462 char *signed_connector = NULL;
6463 const char *pos, *end;
6464 unsigned char *own_conn = NULL;
6465 size_t own_conn_len;
6466 size_t Nx_len;
6467 u8 Nx[DPP_MAX_SHARED_SECRET_LEN];
6468
6469 os_memset(intro, 0, sizeof(*intro));
6470 os_memset(&info, 0, sizeof(info));
6471 if (expiry)
6472 *expiry = 0;
6473
6474 p = csign_key;
6475 csign = d2i_PUBKEY(NULL, &p, csign_key_len);
6476 if (!csign) {
6477 wpa_printf(MSG_ERROR,
6478 "DPP: Failed to parse local C-sign-key information");
6479 goto fail;
6480 }
6481
6482 own_key = dpp_set_keypair(&own_curve, net_access_key,
6483 net_access_key_len);
6484 if (!own_key) {
6485 wpa_printf(MSG_ERROR, "DPP: Failed to parse own netAccessKey");
6486 goto fail;
6487 }
6488
6489 pos = os_strchr(own_connector, '.');
6490 if (!pos) {
6491 wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the first dot (.)");
6492 goto fail;
6493 }
6494 pos++;
6495 end = os_strchr(pos, '.');
6496 if (!end) {
6497 wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the second dot (.)");
6498 goto fail;
6499 }
6500 own_conn = base64_url_decode((const unsigned char *) pos,
6501 end - pos, &own_conn_len);
6502 if (!own_conn) {
6503 wpa_printf(MSG_DEBUG,
6504 "DPP: Failed to base64url decode own signedConnector JWS Payload");
6505 goto fail;
6506 }
6507
6508 own_root = json_parse((const char *) own_conn, own_conn_len);
6509 if (!own_root) {
6510 wpa_printf(MSG_DEBUG, "DPP: Failed to parse local connector");
6511 goto fail;
6512 }
6513
6514 wpa_hexdump_ascii(MSG_DEBUG, "DPP: Peer signedConnector",
6515 peer_connector, peer_connector_len);
6516 signed_connector = os_malloc(peer_connector_len + 1);
6517 if (!signed_connector)
6518 goto fail;
6519 os_memcpy(signed_connector, peer_connector, peer_connector_len);
6520 signed_connector[peer_connector_len] = '\0';
6521
6522 res = dpp_process_signed_connector(&info, csign, signed_connector);
6523 if (res != DPP_STATUS_OK) {
6524 ret = res;
6525 goto fail;
6526 }
6527
6528 root = json_parse((const char *) info.payload, info.payload_len);
6529 if (!root) {
6530 wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
6531 ret = DPP_STATUS_INVALID_CONNECTOR;
6532 goto fail;
6533 }
6534
6535 if (!dpp_connector_match_groups(own_root, root)) {
6536 wpa_printf(MSG_DEBUG,
6537 "DPP: Peer connector does not include compatible group netrole with own connector");
6538 ret = DPP_STATUS_NO_MATCH;
6539 goto fail;
6540 }
6541
6542 token = json_get_member(root, "expiry");
6543 if (!token || token->type != JSON_STRING) {
6544 wpa_printf(MSG_DEBUG,
6545 "DPP: No expiry string found - connector does not expire");
6546 } else {
6547 wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
6548 if (dpp_key_expired(token->string, expiry)) {
6549 wpa_printf(MSG_DEBUG,
6550 "DPP: Connector (netAccessKey) has expired");
6551 ret = DPP_STATUS_INVALID_CONNECTOR;
6552 goto fail;
6553 }
6554 }
6555
6556 netkey = json_get_member(root, "netAccessKey");
6557 if (!netkey || netkey->type != JSON_OBJECT) {
6558 wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
6559 ret = DPP_STATUS_INVALID_CONNECTOR;
6560 goto fail;
6561 }
6562
6563 peer_key = dpp_parse_jwk(netkey, &curve);
6564 if (!peer_key) {
6565 ret = DPP_STATUS_INVALID_CONNECTOR;
6566 goto fail;
6567 }
6568 dpp_debug_print_key("DPP: Received netAccessKey", peer_key);
6569
6570 if (own_curve != curve) {
6571 wpa_printf(MSG_DEBUG,
6572 "DPP: Mismatching netAccessKey curves (%s != %s)",
6573 own_curve->name, curve->name);
6574 ret = DPP_STATUS_INVALID_CONNECTOR;
6575 goto fail;
6576 }
6577
6578 /* ECDH: N = nk * PK */
6579 if (dpp_ecdh(own_key, peer_key, Nx, &Nx_len) < 0)
6580 goto fail;
6581
6582 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
6583 Nx, Nx_len);
6584
6585 /* PMK = HKDF(<>, "DPP PMK", N.x) */
6586 if (dpp_derive_pmk(Nx, Nx_len, intro->pmk, curve->hash_len) < 0) {
6587 wpa_printf(MSG_ERROR, "DPP: Failed to derive PMK");
6588 goto fail;
6589 }
6590 intro->pmk_len = curve->hash_len;
6591
6592 /* PMKID = Truncate-128(H(min(NK.x, PK.x) | max(NK.x, PK.x))) */
6593 if (dpp_derive_pmkid(curve, own_key, peer_key, intro->pmkid) < 0) {
6594 wpa_printf(MSG_ERROR, "DPP: Failed to derive PMKID");
6595 goto fail;
6596 }
6597
6598 ret = DPP_STATUS_OK;
6599 fail:
6600 if (ret != DPP_STATUS_OK)
6601 os_memset(intro, 0, sizeof(*intro));
6602 os_memset(Nx, 0, sizeof(Nx));
6603 os_free(own_conn);
6604 os_free(signed_connector);
6605 os_free(info.payload);
6606 EVP_PKEY_free(own_key);
6607 wpabuf_free(own_key_pub);
6608 EVP_PKEY_free(peer_key);
6609 EVP_PKEY_free(csign);
6610 json_free(root);
6611 json_free(own_root);
6612 return ret;
6613 }
6614
6615
6616 static EVP_PKEY * dpp_pkex_get_role_elem(const struct dpp_curve_params *curve,
6617 int init)
6618 {
6619 EC_GROUP *group;
6620 size_t len = curve->prime_len;
6621 const u8 *x, *y;
6622 EVP_PKEY *res;
6623
6624 switch (curve->ike_group) {
6625 case 19:
6626 x = init ? pkex_init_x_p256 : pkex_resp_x_p256;
6627 y = init ? pkex_init_y_p256 : pkex_resp_y_p256;
6628 break;
6629 case 20:
6630 x = init ? pkex_init_x_p384 : pkex_resp_x_p384;
6631 y = init ? pkex_init_y_p384 : pkex_resp_y_p384;
6632 break;
6633 case 21:
6634 x = init ? pkex_init_x_p521 : pkex_resp_x_p521;
6635 y = init ? pkex_init_y_p521 : pkex_resp_y_p521;
6636 break;
6637 case 28:
6638 x = init ? pkex_init_x_bp_p256r1 : pkex_resp_x_bp_p256r1;
6639 y = init ? pkex_init_y_bp_p256r1 : pkex_resp_y_bp_p256r1;
6640 break;
6641 case 29:
6642 x = init ? pkex_init_x_bp_p384r1 : pkex_resp_x_bp_p384r1;
6643 y = init ? pkex_init_y_bp_p384r1 : pkex_resp_y_bp_p384r1;
6644 break;
6645 case 30:
6646 x = init ? pkex_init_x_bp_p512r1 : pkex_resp_x_bp_p512r1;
6647 y = init ? pkex_init_y_bp_p512r1 : pkex_resp_y_bp_p512r1;
6648 break;
6649 default:
6650 return NULL;
6651 }
6652
6653 group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
6654 if (!group)
6655 return NULL;
6656 res = dpp_set_pubkey_point_group(group, x, y, len);
6657 EC_GROUP_free(group);
6658 return res;
6659 }
6660
6661
6662 static EC_POINT * dpp_pkex_derive_Qi(const struct dpp_curve_params *curve,
6663 const u8 *mac_init, const char *code,
6664 const char *identifier, BN_CTX *bnctx,
6665 EC_GROUP **ret_group)
6666 {
6667 u8 hash[DPP_MAX_HASH_LEN];
6668 const u8 *addr[3];
6669 size_t len[3];
6670 unsigned int num_elem = 0;
6671 EC_POINT *Qi = NULL;
6672 EVP_PKEY *Pi = NULL;
6673 EC_KEY *Pi_ec = NULL;
6674 const EC_POINT *Pi_point;
6675 BIGNUM *hash_bn = NULL;
6676 const EC_GROUP *group = NULL;
6677 EC_GROUP *group2 = NULL;
6678
6679 /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
6680
6681 wpa_printf(MSG_DEBUG, "DPP: MAC-Initiator: " MACSTR, MAC2STR(mac_init));
6682 addr[num_elem] = mac_init;
6683 len[num_elem] = ETH_ALEN;
6684 num_elem++;
6685 if (identifier) {
6686 wpa_printf(MSG_DEBUG, "DPP: code identifier: %s",
6687 identifier);
6688 addr[num_elem] = (const u8 *) identifier;
6689 len[num_elem] = os_strlen(identifier);
6690 num_elem++;
6691 }
6692 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: code", code, os_strlen(code));
6693 addr[num_elem] = (const u8 *) code;
6694 len[num_elem] = os_strlen(code);
6695 num_elem++;
6696 if (dpp_hash_vector(curve, num_elem, addr, len, hash) < 0)
6697 goto fail;
6698 wpa_hexdump_key(MSG_DEBUG,
6699 "DPP: H(MAC-Initiator | [identifier |] code)",
6700 hash, curve->hash_len);
6701 Pi = dpp_pkex_get_role_elem(curve, 1);
6702 if (!Pi)
6703 goto fail;
6704 dpp_debug_print_key("DPP: Pi", Pi);
6705 Pi_ec = EVP_PKEY_get1_EC_KEY(Pi);
6706 if (!Pi_ec)
6707 goto fail;
6708 Pi_point = EC_KEY_get0_public_key(Pi_ec);
6709
6710 group = EC_KEY_get0_group(Pi_ec);
6711 if (!group)
6712 goto fail;
6713 group2 = EC_GROUP_dup(group);
6714 if (!group2)
6715 goto fail;
6716 Qi = EC_POINT_new(group2);
6717 if (!Qi) {
6718 EC_GROUP_free(group2);
6719 goto fail;
6720 }
6721 hash_bn = BN_bin2bn(hash, curve->hash_len, NULL);
6722 if (!hash_bn ||
6723 EC_POINT_mul(group2, Qi, NULL, Pi_point, hash_bn, bnctx) != 1)
6724 goto fail;
6725 if (EC_POINT_is_at_infinity(group, Qi)) {
6726 wpa_printf(MSG_INFO, "DPP: Qi is the point-at-infinity");
6727 goto fail;
6728 }
6729 dpp_debug_print_point("DPP: Qi", group, Qi);
6730 out:
6731 EC_KEY_free(Pi_ec);
6732 EVP_PKEY_free(Pi);
6733 BN_clear_free(hash_bn);
6734 if (ret_group && Qi)
6735 *ret_group = group2;
6736 else
6737 EC_GROUP_free(group2);
6738 return Qi;
6739 fail:
6740 EC_POINT_free(Qi);
6741 Qi = NULL;
6742 goto out;
6743 }
6744
6745
6746 static EC_POINT * dpp_pkex_derive_Qr(const struct dpp_curve_params *curve,
6747 const u8 *mac_resp, const char *code,
6748 const char *identifier, BN_CTX *bnctx,
6749 EC_GROUP **ret_group)
6750 {
6751 u8 hash[DPP_MAX_HASH_LEN];
6752 const u8 *addr[3];
6753 size_t len[3];
6754 unsigned int num_elem = 0;
6755 EC_POINT *Qr = NULL;
6756 EVP_PKEY *Pr = NULL;
6757 EC_KEY *Pr_ec = NULL;
6758 const EC_POINT *Pr_point;
6759 BIGNUM *hash_bn = NULL;
6760 const EC_GROUP *group = NULL;
6761 EC_GROUP *group2 = NULL;
6762
6763 /* Qr = H(MAC-Responder | | [identifier | ] code) * Pr */
6764
6765 wpa_printf(MSG_DEBUG, "DPP: MAC-Responder: " MACSTR, MAC2STR(mac_resp));
6766 addr[num_elem] = mac_resp;
6767 len[num_elem] = ETH_ALEN;
6768 num_elem++;
6769 if (identifier) {
6770 wpa_printf(MSG_DEBUG, "DPP: code identifier: %s",
6771 identifier);
6772 addr[num_elem] = (const u8 *) identifier;
6773 len[num_elem] = os_strlen(identifier);
6774 num_elem++;
6775 }
6776 wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: code", code, os_strlen(code));
6777 addr[num_elem] = (const u8 *) code;
6778 len[num_elem] = os_strlen(code);
6779 num_elem++;
6780 if (dpp_hash_vector(curve, num_elem, addr, len, hash) < 0)
6781 goto fail;
6782 wpa_hexdump_key(MSG_DEBUG,
6783 "DPP: H(MAC-Responder | [identifier |] code)",
6784 hash, curve->hash_len);
6785 Pr = dpp_pkex_get_role_elem(curve, 0);
6786 if (!Pr)
6787 goto fail;
6788 dpp_debug_print_key("DPP: Pr", Pr);
6789 Pr_ec = EVP_PKEY_get1_EC_KEY(Pr);
6790 if (!Pr_ec)
6791 goto fail;
6792 Pr_point = EC_KEY_get0_public_key(Pr_ec);
6793
6794 group = EC_KEY_get0_group(Pr_ec);
6795 if (!group)
6796 goto fail;
6797 group2 = EC_GROUP_dup(group);
6798 if (!group2)
6799 goto fail;
6800 Qr = EC_POINT_new(group2);
6801 if (!Qr) {
6802 EC_GROUP_free(group2);
6803 goto fail;
6804 }
6805 hash_bn = BN_bin2bn(hash, curve->hash_len, NULL);
6806 if (!hash_bn ||
6807 EC_POINT_mul(group2, Qr, NULL, Pr_point, hash_bn, bnctx) != 1)
6808 goto fail;
6809 if (EC_POINT_is_at_infinity(group, Qr)) {
6810 wpa_printf(MSG_INFO, "DPP: Qr is the point-at-infinity");
6811 goto fail;
6812 }
6813 dpp_debug_print_point("DPP: Qr", group, Qr);
6814 out:
6815 EC_KEY_free(Pr_ec);
6816 EVP_PKEY_free(Pr);
6817 BN_clear_free(hash_bn);
6818 if (ret_group && Qr)
6819 *ret_group = group2;
6820 else
6821 EC_GROUP_free(group2);
6822 return Qr;
6823 fail:
6824 EC_POINT_free(Qr);
6825 Qr = NULL;
6826 goto out;
6827 }
6828
6829
6830 #ifdef CONFIG_TESTING_OPTIONS
6831 static int dpp_test_gen_invalid_key(struct wpabuf *msg,
6832 const struct dpp_curve_params *curve)
6833 {
6834 BN_CTX *ctx;
6835 BIGNUM *x, *y;
6836 int ret = -1;
6837 EC_GROUP *group;
6838 EC_POINT *point;
6839
6840 group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
6841 if (!group)
6842 return -1;
6843
6844 ctx = BN_CTX_new();
6845 point = EC_POINT_new(group);
6846 x = BN_new();
6847 y = BN_new();
6848 if (!ctx || !point || !x || !y)
6849 goto fail;
6850
6851 if (BN_rand(x, curve->prime_len * 8, 0, 0) != 1)
6852 goto fail;
6853
6854 /* Generate a random y coordinate that results in a point that is not
6855 * on the curve. */
6856 for (;;) {
6857 if (BN_rand(y, curve->prime_len * 8, 0, 0) != 1)
6858 goto fail;
6859
6860 if (EC_POINT_set_affine_coordinates_GFp(group, point, x, y,
6861 ctx) != 1) {
6862 #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL)
6863 /* Unlike older OpenSSL versions, OpenSSL 1.1.1 and BoringSSL
6864 * return an error from EC_POINT_set_affine_coordinates_GFp()
6865 * when the point is not on the curve. */
6866 break;
6867 #else /* >=1.1.0 or OPENSSL_IS_BORINGSSL */
6868 goto fail;
6869 #endif /* >= 1.1.0 or OPENSSL_IS_BORINGSSL */
6870 }
6871
6872 if (!EC_POINT_is_on_curve(group, point, ctx))
6873 break;
6874 }
6875
6876 if (dpp_bn2bin_pad(x, wpabuf_put(msg, curve->prime_len),
6877 curve->prime_len) < 0 ||
6878 dpp_bn2bin_pad(y, wpabuf_put(msg, curve->prime_len),
6879 curve->prime_len) < 0)
6880 goto fail;
6881
6882 ret = 0;
6883 fail:
6884 if (ret < 0)
6885 wpa_printf(MSG_INFO, "DPP: Failed to generate invalid key");
6886 BN_free(x);
6887 BN_free(y);
6888 EC_POINT_free(point);
6889 BN_CTX_free(ctx);
6890 EC_GROUP_free(group);
6891
6892 return ret;
6893 }
6894 #endif /* CONFIG_TESTING_OPTIONS */
6895
6896
6897 static struct wpabuf * dpp_pkex_build_exchange_req(struct dpp_pkex *pkex)
6898 {
6899 EC_KEY *X_ec = NULL;
6900 const EC_POINT *X_point;
6901 BN_CTX *bnctx = NULL;
6902 EC_GROUP *group = NULL;
6903 EC_POINT *Qi = NULL, *M = NULL;
6904 struct wpabuf *M_buf = NULL;
6905 BIGNUM *Mx = NULL, *My = NULL;
6906 struct wpabuf *msg = NULL;
6907 size_t attr_len;
6908 const struct dpp_curve_params *curve = pkex->own_bi->curve;
6909
6910 wpa_printf(MSG_DEBUG, "DPP: Build PKEX Exchange Request");
6911
6912 /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
6913 bnctx = BN_CTX_new();
6914 if (!bnctx)
6915 goto fail;
6916 Qi = dpp_pkex_derive_Qi(curve, pkex->own_mac, pkex->code,
6917 pkex->identifier, bnctx, &group);
6918 if (!Qi)
6919 goto fail;
6920
6921 /* Generate a random ephemeral keypair x/X */
6922 #ifdef CONFIG_TESTING_OPTIONS
6923 if (dpp_pkex_ephemeral_key_override_len) {
6924 const struct dpp_curve_params *tmp_curve;
6925
6926 wpa_printf(MSG_INFO,
6927 "DPP: TESTING - override ephemeral key x/X");
6928 pkex->x = dpp_set_keypair(&tmp_curve,
6929 dpp_pkex_ephemeral_key_override,
6930 dpp_pkex_ephemeral_key_override_len);
6931 } else {
6932 pkex->x = dpp_gen_keypair(curve);
6933 }
6934 #else /* CONFIG_TESTING_OPTIONS */
6935 pkex->x = dpp_gen_keypair(curve);
6936 #endif /* CONFIG_TESTING_OPTIONS */
6937 if (!pkex->x)
6938 goto fail;
6939
6940 /* M = X + Qi */
6941 X_ec = EVP_PKEY_get1_EC_KEY(pkex->x);
6942 if (!X_ec)
6943 goto fail;
6944 X_point = EC_KEY_get0_public_key(X_ec);
6945 if (!X_point)
6946 goto fail;
6947 dpp_debug_print_point("DPP: X", group, X_point);
6948 M = EC_POINT_new(group);
6949 Mx = BN_new();
6950 My = BN_new();
6951 if (!M || !Mx || !My ||
6952 EC_POINT_add(group, M, X_point, Qi, bnctx) != 1 ||
6953 EC_POINT_get_affine_coordinates_GFp(group, M, Mx, My, bnctx) != 1)
6954 goto fail;
6955 dpp_debug_print_point("DPP: M", group, M);
6956
6957 /* Initiator -> Responder: group, [identifier,] M */
6958 attr_len = 4 + 2;
6959 if (pkex->identifier)
6960 attr_len += 4 + os_strlen(pkex->identifier);
6961 attr_len += 4 + 2 * curve->prime_len;
6962 msg = dpp_alloc_msg(DPP_PA_PKEX_EXCHANGE_REQ, attr_len);
6963 if (!msg)
6964 goto fail;
6965
6966 #ifdef CONFIG_TESTING_OPTIONS
6967 if (dpp_test == DPP_TEST_NO_FINITE_CYCLIC_GROUP_PKEX_EXCHANGE_REQ) {
6968 wpa_printf(MSG_INFO, "DPP: TESTING - no Finite Cyclic Group");
6969 goto skip_finite_cyclic_group;
6970 }
6971 #endif /* CONFIG_TESTING_OPTIONS */
6972
6973 /* Finite Cyclic Group attribute */
6974 wpabuf_put_le16(msg, DPP_ATTR_FINITE_CYCLIC_GROUP);
6975 wpabuf_put_le16(msg, 2);
6976 wpabuf_put_le16(msg, curve->ike_group);
6977
6978 #ifdef CONFIG_TESTING_OPTIONS
6979 skip_finite_cyclic_group:
6980 #endif /* CONFIG_TESTING_OPTIONS */
6981
6982 /* Code Identifier attribute */
6983 if (pkex->identifier) {
6984 wpabuf_put_le16(msg, DPP_ATTR_CODE_IDENTIFIER);
6985 wpabuf_put_le16(msg, os_strlen(pkex->identifier));
6986 wpabuf_put_str(msg, pkex->identifier);
6987 }
6988
6989 #ifdef CONFIG_TESTING_OPTIONS
6990 if (dpp_test == DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_REQ) {
6991 wpa_printf(MSG_INFO, "DPP: TESTING - no Encrypted Key");
6992 goto out;
6993 }
6994 #endif /* CONFIG_TESTING_OPTIONS */
6995
6996 /* M in Encrypted Key attribute */
6997 wpabuf_put_le16(msg, DPP_ATTR_ENCRYPTED_KEY);
6998 wpabuf_put_le16(msg, 2 * curve->prime_len);
6999
7000 #ifdef CONFIG_TESTING_OPTIONS
7001 if (dpp_test == DPP_TEST_INVALID_ENCRYPTED_KEY_PKEX_EXCHANGE_REQ) {
7002 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Encrypted Key");
7003 if (dpp_test_gen_invalid_key(msg, curve) < 0)
7004 goto fail;
7005 goto out;
7006 }
7007 #endif /* CONFIG_TESTING_OPTIONS */
7008
7009 if (dpp_bn2bin_pad(Mx, wpabuf_put(msg, curve->prime_len),
7010 curve->prime_len) < 0 ||
7011 dpp_bn2bin_pad(Mx, pkex->Mx, curve->prime_len) < 0 ||
7012 dpp_bn2bin_pad(My, wpabuf_put(msg, curve->prime_len),
7013 curve->prime_len) < 0)
7014 goto fail;
7015
7016 out:
7017 wpabuf_free(M_buf);
7018 EC_KEY_free(X_ec);
7019 EC_POINT_free(M);
7020 EC_POINT_free(Qi);
7021 BN_clear_free(Mx);
7022 BN_clear_free(My);
7023 BN_CTX_free(bnctx);
7024 EC_GROUP_free(group);
7025 return msg;
7026 fail:
7027 wpa_printf(MSG_INFO, "DPP: Failed to build PKEX Exchange Request");
7028 wpabuf_free(msg);
7029 msg = NULL;
7030 goto out;
7031 }
7032
7033
7034 static void dpp_pkex_fail(struct dpp_pkex *pkex, const char *txt)
7035 {
7036 wpa_msg(pkex->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
7037 }
7038
7039
7040 struct dpp_pkex * dpp_pkex_init(void *msg_ctx, struct dpp_bootstrap_info *bi,
7041 const u8 *own_mac,
7042 const char *identifier,
7043 const char *code)
7044 {
7045 struct dpp_pkex *pkex;
7046
7047 #ifdef CONFIG_TESTING_OPTIONS
7048 if (!is_zero_ether_addr(dpp_pkex_own_mac_override)) {
7049 wpa_printf(MSG_INFO, "DPP: TESTING - own_mac override " MACSTR,
7050 MAC2STR(dpp_pkex_own_mac_override));
7051 own_mac = dpp_pkex_own_mac_override;
7052 }
7053 #endif /* CONFIG_TESTING_OPTIONS */
7054
7055 pkex = os_zalloc(sizeof(*pkex));
7056 if (!pkex)
7057 return NULL;
7058 pkex->msg_ctx = msg_ctx;
7059 pkex->initiator = 1;
7060 pkex->own_bi = bi;
7061 os_memcpy(pkex->own_mac, own_mac, ETH_ALEN);
7062 if (identifier) {
7063 pkex->identifier = os_strdup(identifier);
7064 if (!pkex->identifier)
7065 goto fail;
7066 }
7067 pkex->code = os_strdup(code);
7068 if (!pkex->code)
7069 goto fail;
7070 pkex->exchange_req = dpp_pkex_build_exchange_req(pkex);
7071 if (!pkex->exchange_req)
7072 goto fail;
7073 return pkex;
7074 fail:
7075 dpp_pkex_free(pkex);
7076 return NULL;
7077 }
7078
7079
7080 static struct wpabuf *
7081 dpp_pkex_build_exchange_resp(struct dpp_pkex *pkex,
7082 enum dpp_status_error status,
7083 const BIGNUM *Nx, const BIGNUM *Ny)
7084 {
7085 struct wpabuf *msg = NULL;
7086 size_t attr_len;
7087 const struct dpp_curve_params *curve = pkex->own_bi->curve;
7088
7089 /* Initiator -> Responder: DPP Status, [identifier,] N */
7090 attr_len = 4 + 1;
7091 if (pkex->identifier)
7092 attr_len += 4 + os_strlen(pkex->identifier);
7093 attr_len += 4 + 2 * curve->prime_len;
7094 msg = dpp_alloc_msg(DPP_PA_PKEX_EXCHANGE_RESP, attr_len);
7095 if (!msg)
7096 goto fail;
7097
7098 #ifdef CONFIG_TESTING_OPTIONS
7099 if (dpp_test == DPP_TEST_NO_STATUS_PKEX_EXCHANGE_RESP) {
7100 wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
7101 goto skip_status;
7102 }
7103
7104 if (dpp_test == DPP_TEST_INVALID_STATUS_PKEX_EXCHANGE_RESP) {
7105 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
7106 status = 255;
7107 }
7108 #endif /* CONFIG_TESTING_OPTIONS */
7109
7110 /* DPP Status */
7111 dpp_build_attr_status(msg, status);
7112
7113 #ifdef CONFIG_TESTING_OPTIONS
7114 skip_status:
7115 #endif /* CONFIG_TESTING_OPTIONS */
7116
7117 /* Code Identifier attribute */
7118 if (pkex->identifier) {
7119 wpabuf_put_le16(msg, DPP_ATTR_CODE_IDENTIFIER);
7120 wpabuf_put_le16(msg, os_strlen(pkex->identifier));
7121 wpabuf_put_str(msg, pkex->identifier);
7122 }
7123
7124 if (status != DPP_STATUS_OK)
7125 goto skip_encrypted_key;
7126
7127 #ifdef CONFIG_TESTING_OPTIONS
7128 if (dpp_test == DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_RESP) {
7129 wpa_printf(MSG_INFO, "DPP: TESTING - no Encrypted Key");
7130 goto skip_encrypted_key;
7131 }
7132 #endif /* CONFIG_TESTING_OPTIONS */
7133
7134 /* N in Encrypted Key attribute */
7135 wpabuf_put_le16(msg, DPP_ATTR_ENCRYPTED_KEY);
7136 wpabuf_put_le16(msg, 2 * curve->prime_len);
7137
7138 #ifdef CONFIG_TESTING_OPTIONS
7139 if (dpp_test == DPP_TEST_INVALID_ENCRYPTED_KEY_PKEX_EXCHANGE_RESP) {
7140 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Encrypted Key");
7141 if (dpp_test_gen_invalid_key(msg, curve) < 0)
7142 goto fail;
7143 goto skip_encrypted_key;
7144 }
7145 #endif /* CONFIG_TESTING_OPTIONS */
7146
7147 if (dpp_bn2bin_pad(Nx, wpabuf_put(msg, curve->prime_len),
7148 curve->prime_len) < 0 ||
7149 dpp_bn2bin_pad(Nx, pkex->Nx, curve->prime_len) < 0 ||
7150 dpp_bn2bin_pad(Ny, wpabuf_put(msg, curve->prime_len),
7151 curve->prime_len) < 0)
7152 goto fail;
7153
7154 skip_encrypted_key:
7155 if (status == DPP_STATUS_BAD_GROUP) {
7156 /* Finite Cyclic Group attribute */
7157 wpabuf_put_le16(msg, DPP_ATTR_FINITE_CYCLIC_GROUP);
7158 wpabuf_put_le16(msg, 2);
7159 wpabuf_put_le16(msg, curve->ike_group);
7160 }
7161
7162 return msg;
7163 fail:
7164 wpabuf_free(msg);
7165 return NULL;
7166 }
7167
7168
7169 static int dpp_pkex_derive_z(const u8 *mac_init, const u8 *mac_resp,
7170 const u8 *Mx, size_t Mx_len,
7171 const u8 *Nx, size_t Nx_len,
7172 const char *code,
7173 const u8 *Kx, size_t Kx_len,
7174 u8 *z, unsigned int hash_len)
7175 {
7176 u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
7177 int res;
7178 u8 *info, *pos;
7179 size_t info_len;
7180
7181 /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
7182 */
7183
7184 /* HKDF-Extract(<>, IKM=K.x) */
7185 os_memset(salt, 0, hash_len);
7186 if (dpp_hmac(hash_len, salt, hash_len, Kx, Kx_len, prk) < 0)
7187 return -1;
7188 wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM)",
7189 prk, hash_len);
7190 info_len = 2 * ETH_ALEN + Mx_len + Nx_len + os_strlen(code);
7191 info = os_malloc(info_len);
7192 if (!info)
7193 return -1;
7194 pos = info;
7195 os_memcpy(pos, mac_init, ETH_ALEN);
7196 pos += ETH_ALEN;
7197 os_memcpy(pos, mac_resp, ETH_ALEN);
7198 pos += ETH_ALEN;
7199 os_memcpy(pos, Mx, Mx_len);
7200 pos += Mx_len;
7201 os_memcpy(pos, Nx, Nx_len);
7202 pos += Nx_len;
7203 os_memcpy(pos, code, os_strlen(code));
7204
7205 /* HKDF-Expand(PRK, info, L) */
7206 if (hash_len == 32)
7207 res = hmac_sha256_kdf(prk, hash_len, NULL, info, info_len,
7208 z, hash_len);
7209 else if (hash_len == 48)
7210 res = hmac_sha384_kdf(prk, hash_len, NULL, info, info_len,
7211 z, hash_len);
7212 else if (hash_len == 64)
7213 res = hmac_sha512_kdf(prk, hash_len, NULL, info, info_len,
7214 z, hash_len);
7215 else
7216 res = -1;
7217 os_free(info);
7218 os_memset(prk, 0, hash_len);
7219 if (res < 0)
7220 return -1;
7221
7222 wpa_hexdump_key(MSG_DEBUG, "DPP: z = HKDF-Expand(PRK, info, L)",
7223 z, hash_len);
7224 return 0;
7225 }
7226
7227
7228 static int dpp_pkex_identifier_match(const u8 *attr_id, u16 attr_id_len,
7229 const char *identifier)
7230 {
7231 if (!attr_id && identifier) {
7232 wpa_printf(MSG_DEBUG,
7233 "DPP: No PKEX code identifier received, but expected one");
7234 return 0;
7235 }
7236
7237 if (attr_id && !identifier) {
7238 wpa_printf(MSG_DEBUG,
7239 "DPP: PKEX code identifier received, but not expecting one");
7240 return 0;
7241 }
7242
7243 if (attr_id && identifier &&
7244 (os_strlen(identifier) != attr_id_len ||
7245 os_memcmp(identifier, attr_id, attr_id_len) != 0)) {
7246 wpa_printf(MSG_DEBUG, "DPP: PKEX code identifier mismatch");
7247 return 0;
7248 }
7249
7250 return 1;
7251 }
7252
7253
7254 struct dpp_pkex * dpp_pkex_rx_exchange_req(void *msg_ctx,
7255 struct dpp_bootstrap_info *bi,
7256 const u8 *own_mac,
7257 const u8 *peer_mac,
7258 const char *identifier,
7259 const char *code,
7260 const u8 *buf, size_t len)
7261 {
7262 const u8 *attr_group, *attr_id, *attr_key;
7263 u16 attr_group_len, attr_id_len, attr_key_len;
7264 const struct dpp_curve_params *curve = bi->curve;
7265 u16 ike_group;
7266 struct dpp_pkex *pkex = NULL;
7267 EC_POINT *Qi = NULL, *Qr = NULL, *M = NULL, *X = NULL, *N = NULL;
7268 BN_CTX *bnctx = NULL;
7269 EC_GROUP *group = NULL;
7270 BIGNUM *Mx = NULL, *My = NULL;
7271 EC_KEY *Y_ec = NULL, *X_ec = NULL;;
7272 const EC_POINT *Y_point;
7273 BIGNUM *Nx = NULL, *Ny = NULL;
7274 u8 Kx[DPP_MAX_SHARED_SECRET_LEN];
7275 size_t Kx_len;
7276 int res;
7277
7278 if (bi->pkex_t >= PKEX_COUNTER_T_LIMIT) {
7279 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7280 "PKEX counter t limit reached - ignore message");
7281 return NULL;
7282 }
7283
7284 #ifdef CONFIG_TESTING_OPTIONS
7285 if (!is_zero_ether_addr(dpp_pkex_peer_mac_override)) {
7286 wpa_printf(MSG_INFO, "DPP: TESTING - peer_mac override " MACSTR,
7287 MAC2STR(dpp_pkex_peer_mac_override));
7288 peer_mac = dpp_pkex_peer_mac_override;
7289 }
7290 if (!is_zero_ether_addr(dpp_pkex_own_mac_override)) {
7291 wpa_printf(MSG_INFO, "DPP: TESTING - own_mac override " MACSTR,
7292 MAC2STR(dpp_pkex_own_mac_override));
7293 own_mac = dpp_pkex_own_mac_override;
7294 }
7295 #endif /* CONFIG_TESTING_OPTIONS */
7296
7297 attr_id_len = 0;
7298 attr_id = dpp_get_attr(buf, len, DPP_ATTR_CODE_IDENTIFIER,
7299 &attr_id_len);
7300 if (!dpp_pkex_identifier_match(attr_id, attr_id_len, identifier))
7301 return NULL;
7302
7303 attr_group = dpp_get_attr(buf, len, DPP_ATTR_FINITE_CYCLIC_GROUP,
7304 &attr_group_len);
7305 if (!attr_group || attr_group_len != 2) {
7306 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7307 "Missing or invalid Finite Cyclic Group attribute");
7308 return NULL;
7309 }
7310 ike_group = WPA_GET_LE16(attr_group);
7311 if (ike_group != curve->ike_group) {
7312 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7313 "Mismatching PKEX curve: peer=%u own=%u",
7314 ike_group, curve->ike_group);
7315 pkex = os_zalloc(sizeof(*pkex));
7316 if (!pkex)
7317 goto fail;
7318 pkex->own_bi = bi;
7319 pkex->failed = 1;
7320 pkex->exchange_resp = dpp_pkex_build_exchange_resp(
7321 pkex, DPP_STATUS_BAD_GROUP, NULL, NULL);
7322 if (!pkex->exchange_resp)
7323 goto fail;
7324 return pkex;
7325 }
7326
7327 /* M in Encrypted Key attribute */
7328 attr_key = dpp_get_attr(buf, len, DPP_ATTR_ENCRYPTED_KEY,
7329 &attr_key_len);
7330 if (!attr_key || attr_key_len & 0x01 || attr_key_len < 2 ||
7331 attr_key_len / 2 > DPP_MAX_SHARED_SECRET_LEN) {
7332 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7333 "Missing Encrypted Key attribute");
7334 return NULL;
7335 }
7336
7337 /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
7338 bnctx = BN_CTX_new();
7339 if (!bnctx)
7340 goto fail;
7341 Qi = dpp_pkex_derive_Qi(curve, peer_mac, code, identifier, bnctx,
7342 &group);
7343 if (!Qi)
7344 goto fail;
7345
7346 /* X' = M - Qi */
7347 X = EC_POINT_new(group);
7348 M = EC_POINT_new(group);
7349 Mx = BN_bin2bn(attr_key, attr_key_len / 2, NULL);
7350 My = BN_bin2bn(attr_key + attr_key_len / 2, attr_key_len / 2, NULL);
7351 if (!X || !M || !Mx || !My ||
7352 EC_POINT_set_affine_coordinates_GFp(group, M, Mx, My, bnctx) != 1 ||
7353 EC_POINT_is_at_infinity(group, M) ||
7354 !EC_POINT_is_on_curve(group, M, bnctx) ||
7355 EC_POINT_invert(group, Qi, bnctx) != 1 ||
7356 EC_POINT_add(group, X, M, Qi, bnctx) != 1 ||
7357 EC_POINT_is_at_infinity(group, X) ||
7358 !EC_POINT_is_on_curve(group, X, bnctx)) {
7359 wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7360 "Invalid Encrypted Key value");
7361 bi->pkex_t++;
7362 goto fail;
7363 }
7364 dpp_debug_print_point("DPP: M", group, M);
7365 dpp_debug_print_point("DPP: X'", group, X);
7366
7367 pkex = os_zalloc(sizeof(*pkex));
7368 if (!pkex)
7369 goto fail;
7370 pkex->t = bi->pkex_t;
7371 pkex->msg_ctx = msg_ctx;
7372 pkex->own_bi = bi;
7373 os_memcpy(pkex->own_mac, own_mac, ETH_ALEN);
7374 os_memcpy(pkex->peer_mac, peer_mac, ETH_ALEN);
7375 if (identifier) {
7376 pkex->identifier = os_strdup(identifier);
7377 if (!pkex->identifier)
7378 goto fail;
7379 }
7380 pkex->code = os_strdup(code);
7381 if (!pkex->code)
7382 goto fail;
7383
7384 os_memcpy(pkex->Mx, attr_key, attr_key_len / 2);
7385
7386 X_ec = EC_KEY_new();
7387 if (!X_ec ||
7388 EC_KEY_set_group(X_ec, group) != 1 ||
7389 EC_KEY_set_public_key(X_ec, X) != 1)
7390 goto fail;
7391 pkex->x = EVP_PKEY_new();
7392 if (!pkex->x ||
7393 EVP_PKEY_set1_EC_KEY(pkex->x, X_ec) != 1)
7394 goto fail;
7395
7396 /* Qr = H(MAC-Responder | | [identifier | ] code) * Pr */
7397 Qr = dpp_pkex_derive_Qr(curve, own_mac, code, identifier, bnctx, NULL);
7398 if (!Qr)
7399 goto fail;
7400
7401 /* Generate a random ephemeral keypair y/Y */
7402 #ifdef CONFIG_TESTING_OPTIONS
7403 if (dpp_pkex_ephemeral_key_override_len) {
7404 const struct dpp_curve_params *tmp_curve;
7405
7406 wpa_printf(MSG_INFO,
7407 "DPP: TESTING - override ephemeral key y/Y");
7408 pkex->y = dpp_set_keypair(&tmp_curve,
7409 dpp_pkex_ephemeral_key_override,
7410 dpp_pkex_ephemeral_key_override_len);
7411 } else {
7412 pkex->y = dpp_gen_keypair(curve);
7413 }
7414 #else /* CONFIG_TESTING_OPTIONS */
7415 pkex->y = dpp_gen_keypair(curve);
7416 #endif /* CONFIG_TESTING_OPTIONS */
7417 if (!pkex->y)
7418 goto fail;
7419
7420 /* N = Y + Qr */
7421 Y_ec = EVP_PKEY_get1_EC_KEY(pkex->y);
7422 if (!Y_ec)
7423 goto fail;
7424 Y_point = EC_KEY_get0_public_key(Y_ec);
7425 if (!Y_point)
7426 goto fail;
7427 dpp_debug_print_point("DPP: Y", group, Y_point);
7428 N = EC_POINT_new(group);
7429 Nx = BN_new();
7430 Ny = BN_new();
7431 if (!N || !Nx || !Ny ||
7432 EC_POINT_add(group, N, Y_point, Qr, bnctx) != 1 ||
7433 EC_POINT_get_affine_coordinates_GFp(group, N, Nx, Ny, bnctx) != 1)
7434 goto fail;
7435 dpp_debug_print_point("DPP: N", group, N);
7436
7437 pkex->exchange_resp = dpp_pkex_build_exchange_resp(pkex, DPP_STATUS_OK,
7438 Nx, Ny);
7439 if (!pkex->exchange_resp)
7440 goto fail;
7441
7442 /* K = y * X' */
7443 if (dpp_ecdh(pkex->y, pkex->x, Kx, &Kx_len) < 0)
7444 goto fail;
7445
7446 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (K.x)",
7447 Kx, Kx_len);
7448
7449 /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
7450 */
7451 res = dpp_pkex_derive_z(pkex->peer_mac, pkex->own_mac,
7452 pkex->Mx, curve->prime_len,
7453 pkex->Nx, curve->prime_len, pkex->code,
7454 Kx, Kx_len, pkex->z, curve->hash_len);
7455 os_memset(Kx, 0, Kx_len);
7456 if (res < 0)
7457 goto fail;
7458
7459 pkex->exchange_done = 1;
7460
7461 out:
7462 BN_CTX_free(bnctx);
7463 EC_POINT_free(Qi);
7464 EC_POINT_free(Qr);
7465 BN_free(Mx);
7466 BN_free(My);
7467 BN_free(Nx);
7468 BN_free(Ny);
7469 EC_POINT_free(M);
7470 EC_POINT_free(N);
7471 EC_POINT_free(X);
7472 EC_KEY_free(X_ec);
7473 EC_KEY_free(Y_ec);
7474 EC_GROUP_free(group);
7475 return pkex;
7476 fail:
7477 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request processing failed");
7478 dpp_pkex_free(pkex);
7479 pkex = NULL;
7480 goto out;
7481 }
7482
7483
7484 static struct wpabuf *
7485 dpp_pkex_build_commit_reveal_req(struct dpp_pkex *pkex,
7486 const struct wpabuf *A_pub, const u8 *u)
7487 {
7488 const struct dpp_curve_params *curve = pkex->own_bi->curve;
7489 struct wpabuf *msg = NULL;
7490 size_t clear_len, attr_len;
7491 struct wpabuf *clear = NULL;
7492 u8 *wrapped;
7493 u8 octet;
7494 const u8 *addr[2];
7495 size_t len[2];
7496
7497 /* {A, u, [bootstrapping info]}z */
7498 clear_len = 4 + 2 * curve->prime_len + 4 + curve->hash_len;
7499 clear = wpabuf_alloc(clear_len);
7500 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
7501 #ifdef CONFIG_TESTING_OPTIONS
7502 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_REQ)
7503 attr_len += 5;
7504 #endif /* CONFIG_TESTING_OPTIONS */
7505 msg = dpp_alloc_msg(DPP_PA_PKEX_COMMIT_REVEAL_REQ, attr_len);
7506 if (!clear || !msg)
7507 goto fail;
7508
7509 #ifdef CONFIG_TESTING_OPTIONS
7510 if (dpp_test == DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_REQ) {
7511 wpa_printf(MSG_INFO, "DPP: TESTING - no Bootstrap Key");
7512 goto skip_bootstrap_key;
7513 }
7514 if (dpp_test == DPP_TEST_INVALID_BOOTSTRAP_KEY_PKEX_CR_REQ) {
7515 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Bootstrap Key");
7516 wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
7517 wpabuf_put_le16(clear, 2 * curve->prime_len);
7518 if (dpp_test_gen_invalid_key(clear, curve) < 0)
7519 goto fail;
7520 goto skip_bootstrap_key;
7521 }
7522 #endif /* CONFIG_TESTING_OPTIONS */
7523
7524 /* A in Bootstrap Key attribute */
7525 wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
7526 wpabuf_put_le16(clear, wpabuf_len(A_pub));
7527 wpabuf_put_buf(clear, A_pub);
7528
7529 #ifdef CONFIG_TESTING_OPTIONS
7530 skip_bootstrap_key:
7531 if (dpp_test == DPP_TEST_NO_I_AUTH_TAG_PKEX_CR_REQ) {
7532 wpa_printf(MSG_INFO, "DPP: TESTING - no I-Auth tag");
7533 goto skip_i_auth_tag;
7534 }
7535 if (dpp_test == DPP_TEST_I_AUTH_TAG_MISMATCH_PKEX_CR_REQ) {
7536 wpa_printf(MSG_INFO, "DPP: TESTING - I-Auth tag mismatch");
7537 wpabuf_put_le16(clear, DPP_ATTR_I_AUTH_TAG);
7538 wpabuf_put_le16(clear, curve->hash_len);
7539 wpabuf_put_data(clear, u, curve->hash_len - 1);
7540 wpabuf_put_u8(clear, u[curve->hash_len - 1] ^ 0x01);
7541 goto skip_i_auth_tag;
7542 }
7543 #endif /* CONFIG_TESTING_OPTIONS */
7544
7545 /* u in I-Auth tag attribute */
7546 wpabuf_put_le16(clear, DPP_ATTR_I_AUTH_TAG);
7547 wpabuf_put_le16(clear, curve->hash_len);
7548 wpabuf_put_data(clear, u, curve->hash_len);
7549
7550 #ifdef CONFIG_TESTING_OPTIONS
7551 skip_i_auth_tag:
7552 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_REQ) {
7553 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
7554 goto skip_wrapped_data;
7555 }
7556 #endif /* CONFIG_TESTING_OPTIONS */
7557
7558 addr[0] = wpabuf_head_u8(msg) + 2;
7559 len[0] = DPP_HDR_LEN;
7560 octet = 0;
7561 addr[1] = &octet;
7562 len[1] = sizeof(octet);
7563 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
7564 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
7565
7566 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
7567 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
7568 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
7569
7570 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
7571 if (aes_siv_encrypt(pkex->z, curve->hash_len,
7572 wpabuf_head(clear), wpabuf_len(clear),
7573 2, addr, len, wrapped) < 0)
7574 goto fail;
7575 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
7576 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
7577
7578 #ifdef CONFIG_TESTING_OPTIONS
7579 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_REQ) {
7580 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
7581 dpp_build_attr_status(msg, DPP_STATUS_OK);
7582 }
7583 skip_wrapped_data:
7584 #endif /* CONFIG_TESTING_OPTIONS */
7585
7586 out:
7587 wpabuf_free(clear);
7588 return msg;
7589
7590 fail:
7591 wpabuf_free(msg);
7592 msg = NULL;
7593 goto out;
7594 }
7595
7596
7597 struct wpabuf * dpp_pkex_rx_exchange_resp(struct dpp_pkex *pkex,
7598 const u8 *peer_mac,
7599 const u8 *buf, size_t buflen)
7600 {
7601 const u8 *attr_status, *attr_id, *attr_key, *attr_group;
7602 u16 attr_status_len, attr_id_len, attr_key_len, attr_group_len;
7603 EC_GROUP *group = NULL;
7604 BN_CTX *bnctx = NULL;
7605 struct wpabuf *msg = NULL, *A_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
7606 const struct dpp_curve_params *curve = pkex->own_bi->curve;
7607 EC_POINT *Qr = NULL, *Y = NULL, *N = NULL;
7608 BIGNUM *Nx = NULL, *Ny = NULL;
7609 EC_KEY *Y_ec = NULL;
7610 size_t Jx_len, Kx_len;
7611 u8 Jx[DPP_MAX_SHARED_SECRET_LEN], Kx[DPP_MAX_SHARED_SECRET_LEN];
7612 const u8 *addr[4];
7613 size_t len[4];
7614 u8 u[DPP_MAX_HASH_LEN];
7615 int res;
7616
7617 if (pkex->failed || pkex->t >= PKEX_COUNTER_T_LIMIT || !pkex->initiator)
7618 return NULL;
7619
7620 #ifdef CONFIG_TESTING_OPTIONS
7621 if (dpp_test == DPP_TEST_STOP_AT_PKEX_EXCHANGE_RESP) {
7622 wpa_printf(MSG_INFO,
7623 "DPP: TESTING - stop at PKEX Exchange Response");
7624 pkex->failed = 1;
7625 return NULL;
7626 }
7627
7628 if (!is_zero_ether_addr(dpp_pkex_peer_mac_override)) {
7629 wpa_printf(MSG_INFO, "DPP: TESTING - peer_mac override " MACSTR,
7630 MAC2STR(dpp_pkex_peer_mac_override));
7631 peer_mac = dpp_pkex_peer_mac_override;
7632 }
7633 #endif /* CONFIG_TESTING_OPTIONS */
7634
7635 os_memcpy(pkex->peer_mac, peer_mac, ETH_ALEN);
7636
7637 attr_status = dpp_get_attr(buf, buflen, DPP_ATTR_STATUS,
7638 &attr_status_len);
7639 if (!attr_status || attr_status_len != 1) {
7640 dpp_pkex_fail(pkex, "No DPP Status attribute");
7641 return NULL;
7642 }
7643 wpa_printf(MSG_DEBUG, "DPP: Status %u", attr_status[0]);
7644
7645 if (attr_status[0] == DPP_STATUS_BAD_GROUP) {
7646 attr_group = dpp_get_attr(buf, buflen,
7647 DPP_ATTR_FINITE_CYCLIC_GROUP,
7648 &attr_group_len);
7649 if (attr_group && attr_group_len == 2) {
7650 wpa_msg(pkex->msg_ctx, MSG_INFO, DPP_EVENT_FAIL
7651 "Peer indicated mismatching PKEX group - proposed %u",
7652 WPA_GET_LE16(attr_group));
7653 return NULL;
7654 }
7655 }
7656
7657 if (attr_status[0] != DPP_STATUS_OK) {
7658 dpp_pkex_fail(pkex, "PKEX failed (peer indicated failure)");
7659 return NULL;
7660 }
7661
7662 attr_id_len = 0;
7663 attr_id = dpp_get_attr(buf, buflen, DPP_ATTR_CODE_IDENTIFIER,
7664 &attr_id_len);
7665 if (!dpp_pkex_identifier_match(attr_id, attr_id_len,
7666 pkex->identifier)) {
7667 dpp_pkex_fail(pkex, "PKEX code identifier mismatch");
7668 return NULL;
7669 }
7670
7671 /* N in Encrypted Key attribute */
7672 attr_key = dpp_get_attr(buf, buflen, DPP_ATTR_ENCRYPTED_KEY,
7673 &attr_key_len);
7674 if (!attr_key || attr_key_len & 0x01 || attr_key_len < 2) {
7675 dpp_pkex_fail(pkex, "Missing Encrypted Key attribute");
7676 return NULL;
7677 }
7678
7679 /* Qr = H(MAC-Responder | [identifier |] code) * Pr */
7680 bnctx = BN_CTX_new();
7681 if (!bnctx)
7682 goto fail;
7683 Qr = dpp_pkex_derive_Qr(curve, pkex->peer_mac, pkex->code,
7684 pkex->identifier, bnctx, &group);
7685 if (!Qr)
7686 goto fail;
7687
7688 /* Y' = N - Qr */
7689 Y = EC_POINT_new(group);
7690 N = EC_POINT_new(group);
7691 Nx = BN_bin2bn(attr_key, attr_key_len / 2, NULL);
7692 Ny = BN_bin2bn(attr_key + attr_key_len / 2, attr_key_len / 2, NULL);
7693 if (!Y || !N || !Nx || !Ny ||
7694 EC_POINT_set_affine_coordinates_GFp(group, N, Nx, Ny, bnctx) != 1 ||
7695 EC_POINT_is_at_infinity(group, N) ||
7696 !EC_POINT_is_on_curve(group, N, bnctx) ||
7697 EC_POINT_invert(group, Qr, bnctx) != 1 ||
7698 EC_POINT_add(group, Y, N, Qr, bnctx) != 1 ||
7699 EC_POINT_is_at_infinity(group, Y) ||
7700 !EC_POINT_is_on_curve(group, Y, bnctx)) {
7701 dpp_pkex_fail(pkex, "Invalid Encrypted Key value");
7702 pkex->t++;
7703 goto fail;
7704 }
7705 dpp_debug_print_point("DPP: N", group, N);
7706 dpp_debug_print_point("DPP: Y'", group, Y);
7707
7708 pkex->exchange_done = 1;
7709
7710 /* ECDH: J = a * Y’ */
7711 Y_ec = EC_KEY_new();
7712 if (!Y_ec ||
7713 EC_KEY_set_group(Y_ec, group) != 1 ||
7714 EC_KEY_set_public_key(Y_ec, Y) != 1)
7715 goto fail;
7716 pkex->y = EVP_PKEY_new();
7717 if (!pkex->y ||
7718 EVP_PKEY_set1_EC_KEY(pkex->y, Y_ec) != 1)
7719 goto fail;
7720 if (dpp_ecdh(pkex->own_bi->pubkey, pkex->y, Jx, &Jx_len) < 0)
7721 goto fail;
7722
7723 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (J.x)",
7724 Jx, Jx_len);
7725
7726 /* u = HMAC(J.x, MAC-Initiator | A.x | Y’.x | X.x ) */
7727 A_pub = dpp_get_pubkey_point(pkex->own_bi->pubkey, 0);
7728 Y_pub = dpp_get_pubkey_point(pkex->y, 0);
7729 X_pub = dpp_get_pubkey_point(pkex->x, 0);
7730 if (!A_pub || !Y_pub || !X_pub)
7731 goto fail;
7732 addr[0] = pkex->own_mac;
7733 len[0] = ETH_ALEN;
7734 addr[1] = wpabuf_head(A_pub);
7735 len[1] = wpabuf_len(A_pub) / 2;
7736 addr[2] = wpabuf_head(Y_pub);
7737 len[2] = wpabuf_len(Y_pub) / 2;
7738 addr[3] = wpabuf_head(X_pub);
7739 len[3] = wpabuf_len(X_pub) / 2;
7740 if (dpp_hmac_vector(curve->hash_len, Jx, Jx_len, 4, addr, len, u) < 0)
7741 goto fail;
7742 wpa_hexdump(MSG_DEBUG, "DPP: u", u, curve->hash_len);
7743
7744 /* K = x * Y’ */
7745 if (dpp_ecdh(pkex->x, pkex->y, Kx, &Kx_len) < 0)
7746 goto fail;
7747
7748 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (K.x)",
7749 Kx, Kx_len);
7750
7751 /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
7752 */
7753 res = dpp_pkex_derive_z(pkex->own_mac, pkex->peer_mac,
7754 pkex->Mx, curve->prime_len,
7755 attr_key /* N.x */, attr_key_len / 2,
7756 pkex->code, Kx, Kx_len,
7757 pkex->z, curve->hash_len);
7758 os_memset(Kx, 0, Kx_len);
7759 if (res < 0)
7760 goto fail;
7761
7762 msg = dpp_pkex_build_commit_reveal_req(pkex, A_pub, u);
7763 if (!msg)
7764 goto fail;
7765
7766 out:
7767 wpabuf_free(A_pub);
7768 wpabuf_free(X_pub);
7769 wpabuf_free(Y_pub);
7770 EC_POINT_free(Qr);
7771 EC_POINT_free(Y);
7772 EC_POINT_free(N);
7773 BN_free(Nx);
7774 BN_free(Ny);
7775 EC_KEY_free(Y_ec);
7776 BN_CTX_free(bnctx);
7777 EC_GROUP_free(group);
7778 return msg;
7779 fail:
7780 wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response processing failed");
7781 goto out;
7782 }
7783
7784
7785 static struct wpabuf *
7786 dpp_pkex_build_commit_reveal_resp(struct dpp_pkex *pkex,
7787 const struct wpabuf *B_pub, const u8 *v)
7788 {
7789 const struct dpp_curve_params *curve = pkex->own_bi->curve;
7790 struct wpabuf *msg = NULL;
7791 const u8 *addr[2];
7792 size_t len[2];
7793 u8 octet;
7794 u8 *wrapped;
7795 struct wpabuf *clear = NULL;
7796 size_t clear_len, attr_len;
7797
7798 /* {B, v [bootstrapping info]}z */
7799 clear_len = 4 + 2 * curve->prime_len + 4 + curve->hash_len;
7800 clear = wpabuf_alloc(clear_len);
7801 attr_len = 4 + clear_len + AES_BLOCK_SIZE;
7802 #ifdef CONFIG_TESTING_OPTIONS
7803 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_RESP)
7804 attr_len += 5;
7805 #endif /* CONFIG_TESTING_OPTIONS */
7806 msg = dpp_alloc_msg(DPP_PA_PKEX_COMMIT_REVEAL_RESP, attr_len);
7807 if (!clear || !msg)
7808 goto fail;
7809
7810 #ifdef CONFIG_TESTING_OPTIONS
7811 if (dpp_test == DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_RESP) {
7812 wpa_printf(MSG_INFO, "DPP: TESTING - no Bootstrap Key");
7813 goto skip_bootstrap_key;
7814 }
7815 if (dpp_test == DPP_TEST_INVALID_BOOTSTRAP_KEY_PKEX_CR_RESP) {
7816 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Bootstrap Key");
7817 wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
7818 wpabuf_put_le16(clear, 2 * curve->prime_len);
7819 if (dpp_test_gen_invalid_key(clear, curve) < 0)
7820 goto fail;
7821 goto skip_bootstrap_key;
7822 }
7823 #endif /* CONFIG_TESTING_OPTIONS */
7824
7825 /* B in Bootstrap Key attribute */
7826 wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
7827 wpabuf_put_le16(clear, wpabuf_len(B_pub));
7828 wpabuf_put_buf(clear, B_pub);
7829
7830 #ifdef CONFIG_TESTING_OPTIONS
7831 skip_bootstrap_key:
7832 if (dpp_test == DPP_TEST_NO_R_AUTH_TAG_PKEX_CR_RESP) {
7833 wpa_printf(MSG_INFO, "DPP: TESTING - no R-Auth tag");
7834 goto skip_r_auth_tag;
7835 }
7836 if (dpp_test == DPP_TEST_R_AUTH_TAG_MISMATCH_PKEX_CR_RESP) {
7837 wpa_printf(MSG_INFO, "DPP: TESTING - R-Auth tag mismatch");
7838 wpabuf_put_le16(clear, DPP_ATTR_R_AUTH_TAG);
7839 wpabuf_put_le16(clear, curve->hash_len);
7840 wpabuf_put_data(clear, v, curve->hash_len - 1);
7841 wpabuf_put_u8(clear, v[curve->hash_len - 1] ^ 0x01);
7842 goto skip_r_auth_tag;
7843 }
7844 #endif /* CONFIG_TESTING_OPTIONS */
7845
7846 /* v in R-Auth tag attribute */
7847 wpabuf_put_le16(clear, DPP_ATTR_R_AUTH_TAG);
7848 wpabuf_put_le16(clear, curve->hash_len);
7849 wpabuf_put_data(clear, v, curve->hash_len);
7850
7851 #ifdef CONFIG_TESTING_OPTIONS
7852 skip_r_auth_tag:
7853 if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_RESP) {
7854 wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
7855 goto skip_wrapped_data;
7856 }
7857 #endif /* CONFIG_TESTING_OPTIONS */
7858
7859 addr[0] = wpabuf_head_u8(msg) + 2;
7860 len[0] = DPP_HDR_LEN;
7861 octet = 1;
7862 addr[1] = &octet;
7863 len[1] = sizeof(octet);
7864 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
7865 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
7866
7867 wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
7868 wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
7869 wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
7870
7871 wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
7872 if (aes_siv_encrypt(pkex->z, curve->hash_len,
7873 wpabuf_head(clear), wpabuf_len(clear),
7874 2, addr, len, wrapped) < 0)
7875 goto fail;
7876 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
7877 wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
7878
7879 #ifdef CONFIG_TESTING_OPTIONS
7880 if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_RESP) {
7881 wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
7882 dpp_build_attr_status(msg, DPP_STATUS_OK);
7883 }
7884 skip_wrapped_data:
7885 #endif /* CONFIG_TESTING_OPTIONS */
7886
7887 out:
7888 wpabuf_free(clear);
7889 return msg;
7890
7891 fail:
7892 wpabuf_free(msg);
7893 msg = NULL;
7894 goto out;
7895 }
7896
7897
7898 struct wpabuf * dpp_pkex_rx_commit_reveal_req(struct dpp_pkex *pkex,
7899 const u8 *hdr,
7900 const u8 *buf, size_t buflen)
7901 {
7902 const struct dpp_curve_params *curve = pkex->own_bi->curve;
7903 size_t Jx_len, Lx_len;
7904 u8 Jx[DPP_MAX_SHARED_SECRET_LEN];
7905 u8 Lx[DPP_MAX_SHARED_SECRET_LEN];
7906 const u8 *wrapped_data, *b_key, *peer_u;
7907 u16 wrapped_data_len, b_key_len, peer_u_len = 0;
7908 const u8 *addr[4];
7909 size_t len[4];
7910 u8 octet;
7911 u8 *unwrapped = NULL;
7912 size_t unwrapped_len = 0;
7913 struct wpabuf *msg = NULL, *A_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
7914 struct wpabuf *B_pub = NULL;
7915 u8 u[DPP_MAX_HASH_LEN], v[DPP_MAX_HASH_LEN];
7916
7917 #ifdef CONFIG_TESTING_OPTIONS
7918 if (dpp_test == DPP_TEST_STOP_AT_PKEX_CR_REQ) {
7919 wpa_printf(MSG_INFO,
7920 "DPP: TESTING - stop at PKEX CR Request");
7921 pkex->failed = 1;
7922 return NULL;
7923 }
7924 #endif /* CONFIG_TESTING_OPTIONS */
7925
7926 if (!pkex->exchange_done || pkex->failed ||
7927 pkex->t >= PKEX_COUNTER_T_LIMIT || pkex->initiator)
7928 goto fail;
7929
7930 wrapped_data = dpp_get_attr(buf, buflen, DPP_ATTR_WRAPPED_DATA,
7931 &wrapped_data_len);
7932 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
7933 dpp_pkex_fail(pkex,
7934 "Missing or invalid required Wrapped Data attribute");
7935 goto fail;
7936 }
7937
7938 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
7939 wrapped_data, wrapped_data_len);
7940 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
7941 unwrapped = os_malloc(unwrapped_len);
7942 if (!unwrapped)
7943 goto fail;
7944
7945 addr[0] = hdr;
7946 len[0] = DPP_HDR_LEN;
7947 octet = 0;
7948 addr[1] = &octet;
7949 len[1] = sizeof(octet);
7950 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
7951 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
7952
7953 if (aes_siv_decrypt(pkex->z, curve->hash_len,
7954 wrapped_data, wrapped_data_len,
7955 2, addr, len, unwrapped) < 0) {
7956 dpp_pkex_fail(pkex,
7957 "AES-SIV decryption failed - possible PKEX code mismatch");
7958 pkex->failed = 1;
7959 pkex->t++;
7960 goto fail;
7961 }
7962 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
7963 unwrapped, unwrapped_len);
7964
7965 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
7966 dpp_pkex_fail(pkex, "Invalid attribute in unwrapped data");
7967 goto fail;
7968 }
7969
7970 b_key = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_BOOTSTRAP_KEY,
7971 &b_key_len);
7972 if (!b_key || b_key_len != 2 * curve->prime_len) {
7973 dpp_pkex_fail(pkex, "No valid peer bootstrapping key found");
7974 goto fail;
7975 }
7976 pkex->peer_bootstrap_key = dpp_set_pubkey_point(pkex->x, b_key,
7977 b_key_len);
7978 if (!pkex->peer_bootstrap_key) {
7979 dpp_pkex_fail(pkex, "Peer bootstrapping key is invalid");
7980 goto fail;
7981 }
7982 dpp_debug_print_key("DPP: Peer bootstrap public key",
7983 pkex->peer_bootstrap_key);
7984
7985 /* ECDH: J' = y * A' */
7986 if (dpp_ecdh(pkex->y, pkex->peer_bootstrap_key, Jx, &Jx_len) < 0)
7987 goto fail;
7988
7989 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (J.x)",
7990 Jx, Jx_len);
7991
7992 /* u' = HMAC(J'.x, MAC-Initiator | A'.x | Y.x | X'.x) */
7993 A_pub = dpp_get_pubkey_point(pkex->peer_bootstrap_key, 0);
7994 Y_pub = dpp_get_pubkey_point(pkex->y, 0);
7995 X_pub = dpp_get_pubkey_point(pkex->x, 0);
7996 if (!A_pub || !Y_pub || !X_pub)
7997 goto fail;
7998 addr[0] = pkex->peer_mac;
7999 len[0] = ETH_ALEN;
8000 addr[1] = wpabuf_head(A_pub);
8001 len[1] = wpabuf_len(A_pub) / 2;
8002 addr[2] = wpabuf_head(Y_pub);
8003 len[2] = wpabuf_len(Y_pub) / 2;
8004 addr[3] = wpabuf_head(X_pub);
8005 len[3] = wpabuf_len(X_pub) / 2;
8006 if (dpp_hmac_vector(curve->hash_len, Jx, Jx_len, 4, addr, len, u) < 0)
8007 goto fail;
8008
8009 peer_u = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_AUTH_TAG,
8010 &peer_u_len);
8011 if (!peer_u || peer_u_len != curve->hash_len ||
8012 os_memcmp(peer_u, u, curve->hash_len) != 0) {
8013 dpp_pkex_fail(pkex, "No valid u (I-Auth tag) found");
8014 wpa_hexdump(MSG_DEBUG, "DPP: Calculated u'",
8015 u, curve->hash_len);
8016 wpa_hexdump(MSG_DEBUG, "DPP: Received u", peer_u, peer_u_len);
8017 pkex->t++;
8018 goto fail;
8019 }
8020 wpa_printf(MSG_DEBUG, "DPP: Valid u (I-Auth tag) received");
8021
8022 /* ECDH: L = b * X' */
8023 if (dpp_ecdh(pkex->own_bi->pubkey, pkex->x, Lx, &Lx_len) < 0)
8024 goto fail;
8025
8026 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (L.x)",
8027 Lx, Lx_len);
8028
8029 /* v = HMAC(L.x, MAC-Responder | B.x | X'.x | Y.x) */
8030 B_pub = dpp_get_pubkey_point(pkex->own_bi->pubkey, 0);
8031 if (!B_pub)
8032 goto fail;
8033 addr[0] = pkex->own_mac;
8034 len[0] = ETH_ALEN;
8035 addr[1] = wpabuf_head(B_pub);
8036 len[1] = wpabuf_len(B_pub) / 2;
8037 addr[2] = wpabuf_head(X_pub);
8038 len[2] = wpabuf_len(X_pub) / 2;
8039 addr[3] = wpabuf_head(Y_pub);
8040 len[3] = wpabuf_len(Y_pub) / 2;
8041 if (dpp_hmac_vector(curve->hash_len, Lx, Lx_len, 4, addr, len, v) < 0)
8042 goto fail;
8043 wpa_hexdump(MSG_DEBUG, "DPP: v", v, curve->hash_len);
8044
8045 msg = dpp_pkex_build_commit_reveal_resp(pkex, B_pub, v);
8046 if (!msg)
8047 goto fail;
8048
8049 out:
8050 os_free(unwrapped);
8051 wpabuf_free(A_pub);
8052 wpabuf_free(B_pub);
8053 wpabuf_free(X_pub);
8054 wpabuf_free(Y_pub);
8055 return msg;
8056 fail:
8057 wpa_printf(MSG_DEBUG,
8058 "DPP: PKEX Commit-Reveal Request processing failed");
8059 goto out;
8060 }
8061
8062
8063 int dpp_pkex_rx_commit_reveal_resp(struct dpp_pkex *pkex, const u8 *hdr,
8064 const u8 *buf, size_t buflen)
8065 {
8066 const struct dpp_curve_params *curve = pkex->own_bi->curve;
8067 const u8 *wrapped_data, *b_key, *peer_v;
8068 u16 wrapped_data_len, b_key_len, peer_v_len = 0;
8069 const u8 *addr[4];
8070 size_t len[4];
8071 u8 octet;
8072 u8 *unwrapped = NULL;
8073 size_t unwrapped_len = 0;
8074 int ret = -1;
8075 u8 v[DPP_MAX_HASH_LEN];
8076 size_t Lx_len;
8077 u8 Lx[DPP_MAX_SHARED_SECRET_LEN];
8078 struct wpabuf *B_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
8079
8080 #ifdef CONFIG_TESTING_OPTIONS
8081 if (dpp_test == DPP_TEST_STOP_AT_PKEX_CR_RESP) {
8082 wpa_printf(MSG_INFO,
8083 "DPP: TESTING - stop at PKEX CR Response");
8084 pkex->failed = 1;
8085 goto fail;
8086 }
8087 #endif /* CONFIG_TESTING_OPTIONS */
8088
8089 if (!pkex->exchange_done || pkex->failed ||
8090 pkex->t >= PKEX_COUNTER_T_LIMIT || !pkex->initiator)
8091 goto fail;
8092
8093 wrapped_data = dpp_get_attr(buf, buflen, DPP_ATTR_WRAPPED_DATA,
8094 &wrapped_data_len);
8095 if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
8096 dpp_pkex_fail(pkex,
8097 "Missing or invalid required Wrapped Data attribute");
8098 goto fail;
8099 }
8100
8101 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
8102 wrapped_data, wrapped_data_len);
8103 unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
8104 unwrapped = os_malloc(unwrapped_len);
8105 if (!unwrapped)
8106 goto fail;
8107
8108 addr[0] = hdr;
8109 len[0] = DPP_HDR_LEN;
8110 octet = 1;
8111 addr[1] = &octet;
8112 len[1] = sizeof(octet);
8113 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
8114 wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
8115
8116 if (aes_siv_decrypt(pkex->z, curve->hash_len,
8117 wrapped_data, wrapped_data_len,
8118 2, addr, len, unwrapped) < 0) {
8119 dpp_pkex_fail(pkex,
8120 "AES-SIV decryption failed - possible PKEX code mismatch");
8121 pkex->t++;
8122 goto fail;
8123 }
8124 wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
8125 unwrapped, unwrapped_len);
8126
8127 if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
8128 dpp_pkex_fail(pkex, "Invalid attribute in unwrapped data");
8129 goto fail;
8130 }
8131
8132 b_key = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_BOOTSTRAP_KEY,
8133 &b_key_len);
8134 if (!b_key || b_key_len != 2 * curve->prime_len) {
8135 dpp_pkex_fail(pkex, "No valid peer bootstrapping key found");
8136 goto fail;
8137 }
8138 pkex->peer_bootstrap_key = dpp_set_pubkey_point(pkex->x, b_key,
8139 b_key_len);
8140 if (!pkex->peer_bootstrap_key) {
8141 dpp_pkex_fail(pkex, "Peer bootstrapping key is invalid");
8142 goto fail;
8143 }
8144 dpp_debug_print_key("DPP: Peer bootstrap public key",
8145 pkex->peer_bootstrap_key);
8146
8147 /* ECDH: L' = x * B' */
8148 if (dpp_ecdh(pkex->x, pkex->peer_bootstrap_key, Lx, &Lx_len) < 0)
8149 goto fail;
8150
8151 wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (L.x)",
8152 Lx, Lx_len);
8153
8154 /* v' = HMAC(L.x, MAC-Responder | B'.x | X.x | Y'.x) */
8155 B_pub = dpp_get_pubkey_point(pkex->peer_bootstrap_key, 0);
8156 X_pub = dpp_get_pubkey_point(pkex->x, 0);
8157 Y_pub = dpp_get_pubkey_point(pkex->y, 0);
8158 if (!B_pub || !X_pub || !Y_pub)
8159 goto fail;
8160 addr[0] = pkex->peer_mac;
8161 len[0] = ETH_ALEN;
8162 addr[1] = wpabuf_head(B_pub);
8163 len[1] = wpabuf_len(B_pub) / 2;
8164 addr[2] = wpabuf_head(X_pub);
8165 len[2] = wpabuf_len(X_pub) / 2;
8166 addr[3] = wpabuf_head(Y_pub);
8167 len[3] = wpabuf_len(Y_pub) / 2;
8168 if (dpp_hmac_vector(curve->hash_len, Lx, Lx_len, 4, addr, len, v) < 0)
8169 goto fail;
8170
8171 peer_v = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_AUTH_TAG,
8172 &peer_v_len);
8173 if (!peer_v || peer_v_len != curve->hash_len ||
8174 os_memcmp(peer_v, v, curve->hash_len) != 0) {
8175 dpp_pkex_fail(pkex, "No valid v (R-Auth tag) found");
8176 wpa_hexdump(MSG_DEBUG, "DPP: Calculated v'",
8177 v, curve->hash_len);
8178 wpa_hexdump(MSG_DEBUG, "DPP: Received v", peer_v, peer_v_len);
8179 pkex->t++;
8180 goto fail;
8181 }
8182 wpa_printf(MSG_DEBUG, "DPP: Valid v (R-Auth tag) received");
8183
8184 ret = 0;
8185 out:
8186 wpabuf_free(B_pub);
8187 wpabuf_free(X_pub);
8188 wpabuf_free(Y_pub);
8189 os_free(unwrapped);
8190 return ret;
8191 fail:
8192 goto out;
8193 }
8194
8195
8196 void dpp_pkex_free(struct dpp_pkex *pkex)
8197 {
8198 if (!pkex)
8199 return;
8200
8201 os_free(pkex->identifier);
8202 os_free(pkex->code);
8203 EVP_PKEY_free(pkex->x);
8204 EVP_PKEY_free(pkex->y);
8205 EVP_PKEY_free(pkex->peer_bootstrap_key);
8206 wpabuf_free(pkex->exchange_req);
8207 wpabuf_free(pkex->exchange_resp);
8208 os_free(pkex);
8209 }
8210
8211
8212 #ifdef CONFIG_TESTING_OPTIONS
8213 char * dpp_corrupt_connector_signature(const char *connector)
8214 {
8215 char *tmp, *pos, *signed3 = NULL;
8216 unsigned char *signature = NULL;
8217 size_t signature_len = 0, signed3_len;
8218
8219 tmp = os_zalloc(os_strlen(connector) + 5);
8220 if (!tmp)
8221 goto fail;
8222 os_memcpy(tmp, connector, os_strlen(connector));
8223
8224 pos = os_strchr(tmp, '.');
8225 if (!pos)
8226 goto fail;
8227
8228 pos = os_strchr(pos + 1, '.');
8229 if (!pos)
8230 goto fail;
8231 pos++;
8232
8233 wpa_printf(MSG_DEBUG, "DPP: Original base64url encoded signature: %s",
8234 pos);
8235 signature = base64_url_decode((const unsigned char *) pos,
8236 os_strlen(pos), &signature_len);
8237 if (!signature || signature_len == 0)
8238 goto fail;
8239 wpa_hexdump(MSG_DEBUG, "DPP: Original Connector signature",
8240 signature, signature_len);
8241 signature[signature_len - 1] ^= 0x01;
8242 wpa_hexdump(MSG_DEBUG, "DPP: Corrupted Connector signature",
8243 signature, signature_len);
8244 signed3 = (char *) base64_url_encode(signature, signature_len,
8245 &signed3_len, 0);
8246 if (!signed3)
8247 goto fail;
8248 os_memcpy(pos, signed3, signed3_len);
8249 pos[signed3_len] = '\0';
8250 wpa_printf(MSG_DEBUG, "DPP: Corrupted base64url encoded signature: %s",
8251 pos);
8252
8253 out:
8254 os_free(signature);
8255 os_free(signed3);
8256 return tmp;
8257 fail:
8258 os_free(tmp);
8259 tmp = NULL;
8260 goto out;
8261 }
8262 #endif /* CONFIG_TESTING_OPTIONS */
8263
8264
8265 #ifdef CONFIG_DPP2
8266
8267 struct dpp_pfs * dpp_pfs_init(const u8 *net_access_key,
8268 size_t net_access_key_len)
8269 {
8270 struct wpabuf *pub = NULL;
8271 EVP_PKEY *own_key;
8272 struct dpp_pfs *pfs;
8273
8274 pfs = os_zalloc(sizeof(*pfs));
8275 if (!pfs)
8276 return NULL;
8277
8278 own_key = dpp_set_keypair(&pfs->curve, net_access_key,
8279 net_access_key_len);
8280 if (!own_key) {
8281 wpa_printf(MSG_ERROR, "DPP: Failed to parse own netAccessKey");
8282 goto fail;
8283 }
8284 EVP_PKEY_free(own_key);
8285
8286 pfs->ecdh = crypto_ecdh_init(pfs->curve->ike_group);
8287 if (!pfs->ecdh)
8288 goto fail;
8289
8290 pub = crypto_ecdh_get_pubkey(pfs->ecdh, 0);
8291 pub = wpabuf_zeropad(pub, pfs->curve->prime_len);
8292 if (!pub)
8293 goto fail;
8294
8295 pfs->ie = wpabuf_alloc(5 + wpabuf_len(pub));
8296 if (!pfs->ie)
8297 goto fail;
8298 wpabuf_put_u8(pfs->ie, WLAN_EID_EXTENSION);
8299 wpabuf_put_u8(pfs->ie, 1 + 2 + wpabuf_len(pub));
8300 wpabuf_put_u8(pfs->ie, WLAN_EID_EXT_OWE_DH_PARAM);
8301 wpabuf_put_le16(pfs->ie, pfs->curve->ike_group);
8302 wpabuf_put_buf(pfs->ie, pub);
8303 wpabuf_free(pub);
8304 wpa_hexdump_buf(MSG_DEBUG, "DPP: Diffie-Hellman Parameter element",
8305 pfs->ie);
8306
8307 return pfs;
8308 fail:
8309 wpabuf_free(pub);
8310 dpp_pfs_free(pfs);
8311 return NULL;
8312 }
8313
8314
8315 int dpp_pfs_process(struct dpp_pfs *pfs, const u8 *peer_ie, size_t peer_ie_len)
8316 {
8317 if (peer_ie_len < 2)
8318 return -1;
8319 if (WPA_GET_LE16(peer_ie) != pfs->curve->ike_group) {
8320 wpa_printf(MSG_DEBUG, "DPP: Peer used different group for PFS");
8321 return -1;
8322 }
8323
8324 pfs->secret = crypto_ecdh_set_peerkey(pfs->ecdh, 0, peer_ie + 2,
8325 peer_ie_len - 2);
8326 pfs->secret = wpabuf_zeropad(pfs->secret, pfs->curve->prime_len);
8327 if (!pfs->secret) {
8328 wpa_printf(MSG_DEBUG, "DPP: Invalid peer DH public key");
8329 return -1;
8330 }
8331 wpa_hexdump_buf_key(MSG_DEBUG, "DPP: DH shared secret", pfs->secret);
8332 return 0;
8333 }
8334
8335
8336 void dpp_pfs_free(struct dpp_pfs *pfs)
8337 {
8338 if (!pfs)
8339 return;
8340 crypto_ecdh_deinit(pfs->ecdh);
8341 wpabuf_free(pfs->ie);
8342 wpabuf_clear_free(pfs->secret);
8343 os_free(pfs);
8344 }
8345
8346 #endif /* CONFIG_DPP2 */
8347
8348
8349 static unsigned int dpp_next_id(struct dpp_global *dpp)
8350 {
8351 struct dpp_bootstrap_info *bi;
8352 unsigned int max_id = 0;
8353
8354 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
8355 if (bi->id > max_id)
8356 max_id = bi->id;
8357 }
8358 return max_id + 1;
8359 }
8360
8361
8362 static int dpp_bootstrap_del(struct dpp_global *dpp, unsigned int id)
8363 {
8364 struct dpp_bootstrap_info *bi, *tmp;
8365 int found = 0;
8366
8367 if (!dpp)
8368 return -1;
8369
8370 dl_list_for_each_safe(bi, tmp, &dpp->bootstrap,
8371 struct dpp_bootstrap_info, list) {
8372 if (id && bi->id != id)
8373 continue;
8374 found = 1;
8375 dl_list_del(&bi->list);
8376 dpp_bootstrap_info_free(bi);
8377 }
8378
8379 if (id == 0)
8380 return 0; /* flush succeeds regardless of entries found */
8381 return found ? 0 : -1;
8382 }
8383
8384
8385 struct dpp_bootstrap_info * dpp_add_qr_code(struct dpp_global *dpp,
8386 const char *uri)
8387 {
8388 struct dpp_bootstrap_info *bi;
8389
8390 if (!dpp)
8391 return NULL;
8392
8393 bi = dpp_parse_qr_code(uri);
8394 if (!bi)
8395 return NULL;
8396
8397 bi->id = dpp_next_id(dpp);
8398 dl_list_add(&dpp->bootstrap, &bi->list);
8399 return bi;
8400 }
8401
8402
8403 int dpp_bootstrap_gen(struct dpp_global *dpp, const char *cmd)
8404 {
8405 char *chan = NULL, *mac = NULL, *info = NULL, *pk = NULL, *curve = NULL;
8406 char *key = NULL;
8407 u8 *privkey = NULL;
8408 size_t privkey_len = 0;
8409 size_t len;
8410 int ret = -1;
8411 struct dpp_bootstrap_info *bi;
8412
8413 if (!dpp)
8414 return -1;
8415
8416 bi = os_zalloc(sizeof(*bi));
8417 if (!bi)
8418 goto fail;
8419
8420 if (os_strstr(cmd, "type=qrcode"))
8421 bi->type = DPP_BOOTSTRAP_QR_CODE;
8422 else if (os_strstr(cmd, "type=pkex"))
8423 bi->type = DPP_BOOTSTRAP_PKEX;
8424 else
8425 goto fail;
8426
8427 chan = get_param(cmd, " chan=");
8428 mac = get_param(cmd, " mac=");
8429 info = get_param(cmd, " info=");
8430 curve = get_param(cmd, " curve=");
8431 key = get_param(cmd, " key=");
8432
8433 if (key) {
8434 privkey_len = os_strlen(key) / 2;
8435 privkey = os_malloc(privkey_len);
8436 if (!privkey ||
8437 hexstr2bin(key, privkey, privkey_len) < 0)
8438 goto fail;
8439 }
8440
8441 pk = dpp_keygen(bi, curve, privkey, privkey_len);
8442 if (!pk)
8443 goto fail;
8444
8445 len = 4; /* "DPP:" */
8446 if (chan) {
8447 if (dpp_parse_uri_chan_list(bi, chan) < 0)
8448 goto fail;
8449 len += 3 + os_strlen(chan); /* C:...; */
8450 }
8451 if (mac) {
8452 if (dpp_parse_uri_mac(bi, mac) < 0)
8453 goto fail;
8454 len += 3 + os_strlen(mac); /* M:...; */
8455 }
8456 if (info) {
8457 if (dpp_parse_uri_info(bi, info) < 0)
8458 goto fail;
8459 len += 3 + os_strlen(info); /* I:...; */
8460 }
8461 len += 4 + os_strlen(pk);
8462 bi->uri = os_malloc(len + 1);
8463 if (!bi->uri)
8464 goto fail;
8465 os_snprintf(bi->uri, len + 1, "DPP:%s%s%s%s%s%s%s%s%sK:%s;;",
8466 chan ? "C:" : "", chan ? chan : "", chan ? ";" : "",
8467 mac ? "M:" : "", mac ? mac : "", mac ? ";" : "",
8468 info ? "I:" : "", info ? info : "", info ? ";" : "",
8469 pk);
8470 bi->id = dpp_next_id(dpp);
8471 dl_list_add(&dpp->bootstrap, &bi->list);
8472 ret = bi->id;
8473 bi = NULL;
8474 fail:
8475 os_free(curve);
8476 os_free(pk);
8477 os_free(chan);
8478 os_free(mac);
8479 os_free(info);
8480 str_clear_free(key);
8481 bin_clear_free(privkey, privkey_len);
8482 dpp_bootstrap_info_free(bi);
8483 return ret;
8484 }
8485
8486
8487 struct dpp_bootstrap_info *
8488 dpp_bootstrap_get_id(struct dpp_global *dpp, unsigned int id)
8489 {
8490 struct dpp_bootstrap_info *bi;
8491
8492 if (!dpp)
8493 return NULL;
8494
8495 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
8496 if (bi->id == id)
8497 return bi;
8498 }
8499 return NULL;
8500 }
8501
8502
8503 int dpp_bootstrap_remove(struct dpp_global *dpp, const char *id)
8504 {
8505 unsigned int id_val;
8506
8507 if (os_strcmp(id, "*") == 0) {
8508 id_val = 0;
8509 } else {
8510 id_val = atoi(id);
8511 if (id_val == 0)
8512 return -1;
8513 }
8514
8515 return dpp_bootstrap_del(dpp, id_val);
8516 }
8517
8518
8519 struct dpp_bootstrap_info *
8520 dpp_pkex_finish(struct dpp_global *dpp, struct dpp_pkex *pkex, const u8 *peer,
8521 unsigned int freq)
8522 {
8523 struct dpp_bootstrap_info *bi;
8524
8525 bi = os_zalloc(sizeof(*bi));
8526 if (!bi)
8527 return NULL;
8528 bi->id = dpp_next_id(dpp);
8529 bi->type = DPP_BOOTSTRAP_PKEX;
8530 os_memcpy(bi->mac_addr, peer, ETH_ALEN);
8531 bi->num_freq = 1;
8532 bi->freq[0] = freq;
8533 bi->curve = pkex->own_bi->curve;
8534 bi->pubkey = pkex->peer_bootstrap_key;
8535 pkex->peer_bootstrap_key = NULL;
8536 if (dpp_bootstrap_key_hash(bi) < 0) {
8537 dpp_bootstrap_info_free(bi);
8538 return NULL;
8539 }
8540 dpp_pkex_free(pkex);
8541 dl_list_add(&dpp->bootstrap, &bi->list);
8542 return bi;
8543 }
8544
8545
8546 const char * dpp_bootstrap_get_uri(struct dpp_global *dpp, unsigned int id)
8547 {
8548 struct dpp_bootstrap_info *bi;
8549
8550 bi = dpp_bootstrap_get_id(dpp, id);
8551 if (!bi)
8552 return NULL;
8553 return bi->uri;
8554 }
8555
8556
8557 int dpp_bootstrap_info(struct dpp_global *dpp, int id,
8558 char *reply, int reply_size)
8559 {
8560 struct dpp_bootstrap_info *bi;
8561 char pkhash[2 * SHA256_MAC_LEN + 1];
8562
8563 bi = dpp_bootstrap_get_id(dpp, id);
8564 if (!bi)
8565 return -1;
8566 wpa_snprintf_hex(pkhash, sizeof(pkhash), bi->pubkey_hash,
8567 SHA256_MAC_LEN);
8568 return os_snprintf(reply, reply_size, "type=%s\n"
8569 "mac_addr=" MACSTR "\n"
8570 "info=%s\n"
8571 "num_freq=%u\n"
8572 "curve=%s\n"
8573 "pkhash=%s\n",
8574 dpp_bootstrap_type_txt(bi->type),
8575 MAC2STR(bi->mac_addr),
8576 bi->info ? bi->info : "",
8577 bi->num_freq,
8578 bi->curve->name,
8579 pkhash);
8580 }
8581
8582
8583 void dpp_bootstrap_find_pair(struct dpp_global *dpp, const u8 *i_bootstrap,
8584 const u8 *r_bootstrap,
8585 struct dpp_bootstrap_info **own_bi,
8586 struct dpp_bootstrap_info **peer_bi)
8587 {
8588 struct dpp_bootstrap_info *bi;
8589
8590 *own_bi = NULL;
8591 *peer_bi = NULL;
8592 if (!dpp)
8593 return;
8594
8595 dl_list_for_each(bi, &dpp->bootstrap, struct dpp_bootstrap_info, list) {
8596 if (!*own_bi && bi->own &&
8597 os_memcmp(bi->pubkey_hash, r_bootstrap,
8598 SHA256_MAC_LEN) == 0) {
8599 wpa_printf(MSG_DEBUG,
8600 "DPP: Found matching own bootstrapping information");
8601 *own_bi = bi;
8602 }
8603
8604 if (!*peer_bi && !bi->own &&
8605 os_memcmp(bi->pubkey_hash, i_bootstrap,
8606 SHA256_MAC_LEN) == 0) {
8607 wpa_printf(MSG_DEBUG,
8608 "DPP: Found matching peer bootstrapping information");
8609 *peer_bi = bi;
8610 }
8611
8612 if (*own_bi && *peer_bi)
8613 break;
8614 }
8615
8616 }
8617
8618
8619 static unsigned int dpp_next_configurator_id(struct dpp_global *dpp)
8620 {
8621 struct dpp_configurator *conf;
8622 unsigned int max_id = 0;
8623
8624 dl_list_for_each(conf, &dpp->configurator, struct dpp_configurator,
8625 list) {
8626 if (conf->id > max_id)
8627 max_id = conf->id;
8628 }
8629 return max_id + 1;
8630 }
8631
8632
8633 int dpp_configurator_add(struct dpp_global *dpp, const char *cmd)
8634 {
8635 char *curve = NULL;
8636 char *key = NULL;
8637 u8 *privkey = NULL;
8638 size_t privkey_len = 0;
8639 int ret = -1;
8640 struct dpp_configurator *conf = NULL;
8641
8642 curve = get_param(cmd, " curve=");
8643 key = get_param(cmd, " key=");
8644
8645 if (key) {
8646 privkey_len = os_strlen(key) / 2;
8647 privkey = os_malloc(privkey_len);
8648 if (!privkey ||
8649 hexstr2bin(key, privkey, privkey_len) < 0)
8650 goto fail;
8651 }
8652
8653 conf = dpp_keygen_configurator(curve, privkey, privkey_len);
8654 if (!conf)
8655 goto fail;
8656
8657 conf->id = dpp_next_configurator_id(dpp);
8658 dl_list_add(&dpp->configurator, &conf->list);
8659 ret = conf->id;
8660 conf = NULL;
8661 fail:
8662 os_free(curve);
8663 str_clear_free(key);
8664 bin_clear_free(privkey, privkey_len);
8665 dpp_configurator_free(conf);
8666 return ret;
8667 }
8668
8669
8670 static int dpp_configurator_del(struct dpp_global *dpp, unsigned int id)
8671 {
8672 struct dpp_configurator *conf, *tmp;
8673 int found = 0;
8674
8675 if (!dpp)
8676 return -1;
8677
8678 dl_list_for_each_safe(conf, tmp, &dpp->configurator,
8679 struct dpp_configurator, list) {
8680 if (id && conf->id != id)
8681 continue;
8682 found = 1;
8683 dl_list_del(&conf->list);
8684 dpp_configurator_free(conf);
8685 }
8686
8687 if (id == 0)
8688 return 0; /* flush succeeds regardless of entries found */
8689 return found ? 0 : -1;
8690 }
8691
8692
8693 int dpp_configurator_remove(struct dpp_global *dpp, const char *id)
8694 {
8695 unsigned int id_val;
8696
8697 if (os_strcmp(id, "*") == 0) {
8698 id_val = 0;
8699 } else {
8700 id_val = atoi(id);
8701 if (id_val == 0)
8702 return -1;
8703 }
8704
8705 return dpp_configurator_del(dpp, id_val);
8706 }
8707
8708
8709 int dpp_configurator_get_key_id(struct dpp_global *dpp, unsigned int id,
8710 char *buf, size_t buflen)
8711 {
8712 struct dpp_configurator *conf;
8713
8714 conf = dpp_configurator_get_id(dpp, id);
8715 if (!conf)
8716 return -1;
8717
8718 return dpp_configurator_get_key(conf, buf, buflen);
8719 }
8720
8721
8722 #ifdef CONFIG_DPP2
8723
8724 static void dpp_connection_free(struct dpp_connection *conn)
8725 {
8726 if (conn->sock >= 0) {
8727 wpa_printf(MSG_DEBUG, "DPP: Close Controller socket %d",
8728 conn->sock);
8729 eloop_unregister_sock(conn->sock, EVENT_TYPE_READ);
8730 eloop_unregister_sock(conn->sock, EVENT_TYPE_WRITE);
8731 close(conn->sock);
8732 }
8733 wpabuf_free(conn->msg);
8734 wpabuf_free(conn->msg_out);
8735 dpp_auth_deinit(conn->auth);
8736 os_free(conn);
8737 }
8738
8739
8740 static void dpp_connection_remove(struct dpp_connection *conn)
8741 {
8742 dl_list_del(&conn->list);
8743 dpp_connection_free(conn);
8744 }
8745
8746
8747 static void dpp_tcp_init_flush(struct dpp_global *dpp)
8748 {
8749 struct dpp_connection *conn, *tmp;
8750
8751 dl_list_for_each_safe(conn, tmp, &dpp->tcp_init, struct dpp_connection,
8752 list)
8753 dpp_connection_remove(conn);
8754 }
8755
8756
8757 static void dpp_relay_controller_free(struct dpp_relay_controller *ctrl)
8758 {
8759 struct dpp_connection *conn, *tmp;
8760
8761 dl_list_for_each_safe(conn, tmp, &ctrl->conn, struct dpp_connection,
8762 list)
8763 dpp_connection_remove(conn);
8764 os_free(ctrl);
8765 }
8766
8767
8768 static void dpp_relay_flush_controllers(struct dpp_global *dpp)
8769 {
8770 struct dpp_relay_controller *ctrl, *tmp;
8771
8772 if (!dpp)
8773 return;
8774
8775 dl_list_for_each_safe(ctrl, tmp, &dpp->controllers,
8776 struct dpp_relay_controller, list) {
8777 dl_list_del(&ctrl->list);
8778 dpp_relay_controller_free(ctrl);
8779 }
8780 }
8781
8782 #endif /* CONFIG_DPP2 */
8783
8784
8785 struct dpp_global * dpp_global_init(struct dpp_global_config *config)
8786 {
8787 struct dpp_global *dpp;
8788
8789 dpp = os_zalloc(sizeof(*dpp));
8790 if (!dpp)
8791 return NULL;
8792 dpp->msg_ctx = config->msg_ctx;
8793 #ifdef CONFIG_DPP2
8794 dpp->cb_ctx = config->cb_ctx;
8795 dpp->process_conf_obj = config->process_conf_obj;
8796 #endif /* CONFIG_DPP2 */
8797
8798 dl_list_init(&dpp->bootstrap);
8799 dl_list_init(&dpp->configurator);
8800 #ifdef CONFIG_DPP2
8801 dl_list_init(&dpp->controllers);
8802 dl_list_init(&dpp->tcp_init);
8803 #endif /* CONFIG_DPP2 */
8804
8805 return dpp;
8806 }
8807
8808
8809 void dpp_global_clear(struct dpp_global *dpp)
8810 {
8811 if (!dpp)
8812 return;
8813
8814 dpp_bootstrap_del(dpp, 0);
8815 dpp_configurator_del(dpp, 0);
8816 #ifdef CONFIG_DPP2
8817 dpp_tcp_init_flush(dpp);
8818 dpp_relay_flush_controllers(dpp);
8819 dpp_controller_stop(dpp);
8820 #endif /* CONFIG_DPP2 */
8821 }
8822
8823
8824 void dpp_global_deinit(struct dpp_global *dpp)
8825 {
8826 dpp_global_clear(dpp);
8827 os_free(dpp);
8828 }
8829
8830
8831 #ifdef CONFIG_DPP2
8832
8833 static void dpp_controller_rx(int sd, void *eloop_ctx, void *sock_ctx);
8834 static void dpp_conn_tx_ready(int sock, void *eloop_ctx, void *sock_ctx);
8835 static void dpp_controller_auth_success(struct dpp_connection *conn,
8836 int initiator);
8837
8838
8839 int dpp_relay_add_controller(struct dpp_global *dpp,
8840 struct dpp_relay_config *config)
8841 {
8842 struct dpp_relay_controller *ctrl;
8843
8844 if (!dpp)
8845 return -1;
8846
8847 ctrl = os_zalloc(sizeof(*ctrl));
8848 if (!ctrl)
8849 return -1;
8850 dl_list_init(&ctrl->conn);
8851 ctrl->global = dpp;
8852 os_memcpy(&ctrl->ipaddr, config->ipaddr, sizeof(*config->ipaddr));
8853 os_memcpy(ctrl->pkhash, config->pkhash, SHA256_MAC_LEN);
8854 ctrl->cb_ctx = config->cb_ctx;
8855 ctrl->tx = config->tx;
8856 ctrl->gas_resp_tx = config->gas_resp_tx;
8857 dl_list_add(&dpp->controllers, &ctrl->list);
8858 return 0;
8859 }
8860
8861
8862 static struct dpp_relay_controller *
8863 dpp_relay_controller_get(struct dpp_global *dpp, const u8 *pkhash)
8864 {
8865 struct dpp_relay_controller *ctrl;
8866
8867 if (!dpp)
8868 return NULL;
8869
8870 dl_list_for_each(ctrl, &dpp->controllers, struct dpp_relay_controller,
8871 list) {
8872 if (os_memcmp(pkhash, ctrl->pkhash, SHA256_MAC_LEN) == 0)
8873 return ctrl;
8874 }
8875
8876 return NULL;
8877 }
8878
8879
8880 static void dpp_controller_gas_done(struct dpp_connection *conn)
8881 {
8882 struct dpp_authentication *auth = conn->auth;
8883
8884 if (auth->peer_version >= 2 &&
8885 auth->conf_resp_status == DPP_STATUS_OK) {
8886 wpa_printf(MSG_DEBUG, "DPP: Wait for Configuration Result");
8887 auth->waiting_conf_result = 1;
8888 return;
8889 }
8890
8891 wpa_msg(conn->ctrl->global->msg_ctx, MSG_INFO, DPP_EVENT_CONF_SENT);
8892 dpp_connection_remove(conn);
8893 }
8894
8895
8896 static int dpp_tcp_send(struct dpp_connection *conn)
8897 {
8898 int res;
8899
8900 if (!conn->msg_out) {
8901 eloop_unregister_sock(conn->sock, EVENT_TYPE_WRITE);
8902 conn->write_eloop = 0;
8903 return -1;
8904 }
8905 res = send(conn->sock,
8906 wpabuf_head_u8(conn->msg_out) + conn->msg_out_pos,
8907 wpabuf_len(conn->msg_out) - conn->msg_out_pos, 0);
8908 if (res < 0) {
8909 wpa_printf(MSG_DEBUG, "DPP: Failed to send buffer: %s",
8910 strerror(errno));
8911 dpp_connection_remove(conn);
8912 return -1;
8913 }
8914
8915 conn->msg_out_pos += res;
8916 if (wpabuf_len(conn->msg_out) > conn->msg_out_pos) {
8917 wpa_printf(MSG_DEBUG,
8918 "DPP: %u/%u bytes of message sent to Controller",
8919 (unsigned int) conn->msg_out_pos,
8920 (unsigned int) wpabuf_len(conn->msg_out));
8921 if (!conn->write_eloop &&
8922 eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
8923 dpp_conn_tx_ready, conn, NULL) == 0)
8924 conn->write_eloop = 1;
8925 return 1;
8926 }
8927
8928 wpa_printf(MSG_DEBUG, "DPP: Full message sent over TCP");
8929 wpabuf_free(conn->msg_out);
8930 conn->msg_out = NULL;
8931 conn->msg_out_pos = 0;
8932 eloop_unregister_sock(conn->sock, EVENT_TYPE_WRITE);
8933 conn->write_eloop = 0;
8934 if (!conn->read_eloop &&
8935 eloop_register_sock(conn->sock, EVENT_TYPE_READ,
8936 dpp_controller_rx, conn, NULL) == 0)
8937 conn->read_eloop = 1;
8938 if (conn->on_tcp_tx_complete_remove) {
8939 dpp_connection_remove(conn);
8940 } else if (conn->ctrl && conn->on_tcp_tx_complete_gas_done &&
8941 conn->auth) {
8942 dpp_controller_gas_done(conn);
8943 } else if (conn->on_tcp_tx_complete_auth_ok) {
8944 conn->on_tcp_tx_complete_auth_ok = 0;
8945 dpp_controller_auth_success(conn, 1);
8946 }
8947
8948 return 0;
8949 }
8950
8951
8952 static void dpp_controller_start_gas_client(struct dpp_connection *conn)
8953 {
8954 struct dpp_authentication *auth = conn->auth;
8955 struct wpabuf *buf;
8956 char json[100];
8957 int netrole_ap = 0; /* TODO: make this configurable */
8958
8959 os_snprintf(json, sizeof(json),
8960 "{\"name\":\"Test\","
8961 "\"wi-fi_tech\":\"infra\","
8962 "\"netRole\":\"%s\"}",
8963 netrole_ap ? "ap" : "sta");
8964 #ifdef CONFIG_TESTING_OPTIONS
8965 if (dpp_test == DPP_TEST_INVALID_CONFIG_ATTR_OBJ_CONF_REQ) {
8966 wpa_printf(MSG_INFO, "DPP: TESTING - invalid Config Attr");
8967 json[29] = 'k'; /* replace "infra" with "knfra" */
8968 }
8969 #endif /* CONFIG_TESTING_OPTIONS */
8970 wpa_printf(MSG_DEBUG, "DPP: GAS Config Attributes: %s", json);
8971
8972 buf = dpp_build_conf_req(auth, json);
8973 if (!buf) {
8974 wpa_printf(MSG_DEBUG,
8975 "DPP: No configuration request data available");
8976 return;
8977 }
8978
8979 wpabuf_free(conn->msg_out);
8980 conn->msg_out_pos = 0;
8981 conn->msg_out = wpabuf_alloc(4 + wpabuf_len(buf) - 1);
8982 if (!conn->msg_out) {
8983 wpabuf_free(buf);
8984 return;
8985 }
8986 wpabuf_put_be32(conn->msg_out, wpabuf_len(buf) - 1);
8987 wpabuf_put_data(conn->msg_out, wpabuf_head_u8(buf) + 1,
8988 wpabuf_len(buf) - 1);
8989 wpabuf_free(buf);
8990
8991 if (dpp_tcp_send(conn) == 1) {
8992 if (!conn->write_eloop) {
8993 if (eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
8994 dpp_conn_tx_ready,
8995 conn, NULL) < 0)
8996 return;
8997 conn->write_eloop = 1;
8998 }
8999 }
9000 }
9001
9002
9003 static void dpp_controller_auth_success(struct dpp_connection *conn,
9004 int initiator)
9005 {
9006 struct dpp_authentication *auth = conn->auth;
9007
9008 if (!auth)
9009 return;
9010
9011 wpa_printf(MSG_DEBUG, "DPP: Authentication succeeded");
9012 wpa_msg(conn->global->msg_ctx, MSG_INFO,
9013 DPP_EVENT_AUTH_SUCCESS "init=%d", initiator);
9014 #ifdef CONFIG_TESTING_OPTIONS
9015 if (dpp_test == DPP_TEST_STOP_AT_AUTH_CONF) {
9016 wpa_printf(MSG_INFO,
9017 "DPP: TESTING - stop at Authentication Confirm");
9018 if (auth->configurator) {
9019 /* Prevent GAS response */
9020 auth->auth_success = 0;
9021 }
9022 return;
9023 }
9024 #endif /* CONFIG_TESTING_OPTIONS */
9025
9026 if (!auth->configurator)
9027 dpp_controller_start_gas_client(conn);
9028 }
9029
9030
9031 static void dpp_conn_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
9032 {
9033 struct dpp_connection *conn = eloop_ctx;
9034
9035 wpa_printf(MSG_DEBUG, "DPP: TCP socket %d ready for TX", sock);
9036 dpp_tcp_send(conn);
9037 }
9038
9039
9040 static int dpp_ipaddr_to_sockaddr(struct sockaddr *addr, socklen_t *addrlen,
9041 const struct hostapd_ip_addr *ipaddr,
9042 int port)
9043 {
9044 struct sockaddr_in *dst;
9045 #ifdef CONFIG_IPV6
9046 struct sockaddr_in6 *dst6;
9047 #endif /* CONFIG_IPV6 */
9048
9049 switch (ipaddr->af) {
9050 case AF_INET:
9051 dst = (struct sockaddr_in *) addr;
9052 os_memset(dst, 0, sizeof(*dst));
9053 dst->sin_family = AF_INET;
9054 dst->sin_addr.s_addr = ipaddr->u.v4.s_addr;
9055 dst->sin_port = htons(port);
9056 *addrlen = sizeof(*dst);
9057 break;
9058 #ifdef CONFIG_IPV6
9059 case AF_INET6:
9060 dst6 = (struct sockaddr_in6 *) addr;
9061 os_memset(dst6, 0, sizeof(*dst6));
9062 dst6->sin6_family = AF_INET6;
9063 os_memcpy(&dst6->sin6_addr, &ipaddr->u.v6,
9064 sizeof(struct in6_addr));
9065 dst6->sin6_port = htons(port);
9066 *addrlen = sizeof(*dst6);
9067 break;
9068 #endif /* CONFIG_IPV6 */
9069 default:
9070 return -1;
9071 }
9072
9073 return 0;
9074 }
9075
9076
9077 static struct dpp_connection *
9078 dpp_relay_new_conn(struct dpp_relay_controller *ctrl, const u8 *src,
9079 unsigned int freq)
9080 {
9081 struct dpp_connection *conn;
9082 struct sockaddr_storage addr;
9083 socklen_t addrlen;
9084 char txt[100];
9085
9086 if (dl_list_len(&ctrl->conn) >= 15) {
9087 wpa_printf(MSG_DEBUG,
9088 "DPP: Too many ongoing Relay connections to the Controller - cannot start a new one");
9089 return NULL;
9090 }
9091
9092 if (dpp_ipaddr_to_sockaddr((struct sockaddr *) &addr, &addrlen,
9093 &ctrl->ipaddr, DPP_TCP_PORT) < 0)
9094 return NULL;
9095
9096 conn = os_zalloc(sizeof(*conn));
9097 if (!conn)
9098 return NULL;
9099
9100 conn->global = ctrl->global;
9101 conn->relay = ctrl;
9102 os_memcpy(conn->mac_addr, src, ETH_ALEN);
9103 conn->freq = freq;
9104
9105 conn->sock = socket(AF_INET, SOCK_STREAM, 0);
9106 if (conn->sock < 0)
9107 goto fail;
9108 wpa_printf(MSG_DEBUG, "DPP: TCP relay socket %d connection to %s",
9109 conn->sock, hostapd_ip_txt(&ctrl->ipaddr, txt, sizeof(txt)));
9110
9111 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) != 0) {
9112 wpa_printf(MSG_DEBUG, "DPP: fnctl(O_NONBLOCK) failed: %s",
9113 strerror(errno));
9114 goto fail;
9115 }
9116
9117 if (connect(conn->sock, (struct sockaddr *) &addr, addrlen) < 0) {
9118 if (errno != EINPROGRESS) {
9119 wpa_printf(MSG_DEBUG, "DPP: Failed to connect: %s",
9120 strerror(errno));
9121 goto fail;
9122 }
9123
9124 /*
9125 * Continue connecting in the background; eloop will call us
9126 * once the connection is ready (or failed).
9127 */
9128 }
9129
9130 if (eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
9131 dpp_conn_tx_ready, conn, NULL) < 0)
9132 goto fail;
9133 conn->write_eloop = 1;
9134
9135 /* TODO: eloop timeout to clear a connection if it does not complete
9136 * properly */
9137
9138 dl_list_add(&ctrl->conn, &conn->list);
9139 return conn;
9140 fail:
9141 dpp_connection_free(conn);
9142 return NULL;
9143 }
9144
9145
9146 static struct wpabuf * dpp_tcp_encaps(const u8 *hdr, const u8 *buf, size_t len)
9147 {
9148 struct wpabuf *msg;
9149
9150 msg = wpabuf_alloc(4 + 1 + DPP_HDR_LEN + len);
9151 if (!msg)
9152 return NULL;
9153 wpabuf_put_be32(msg, 1 + DPP_HDR_LEN + len);
9154 wpabuf_put_u8(msg, WLAN_PA_VENDOR_SPECIFIC);
9155 wpabuf_put_data(msg, hdr, DPP_HDR_LEN);
9156 wpabuf_put_data(msg, buf, len);
9157 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: Outgoing TCP message", msg);
9158 return msg;
9159 }
9160
9161
9162 static int dpp_relay_tx(struct dpp_connection *conn, const u8 *hdr,
9163 const u8 *buf, size_t len)
9164 {
9165 u8 type = hdr[DPP_HDR_LEN - 1];
9166
9167 wpa_printf(MSG_DEBUG,
9168 "DPP: Continue already established Relay/Controller connection for this session");
9169 wpabuf_free(conn->msg_out);
9170 conn->msg_out_pos = 0;
9171 conn->msg_out = dpp_tcp_encaps(hdr, buf, len);
9172 if (!conn->msg_out) {
9173 dpp_connection_remove(conn);
9174 return -1;
9175 }
9176
9177 /* TODO: for proto ver 1, need to do remove connection based on GAS Resp
9178 * TX status */
9179 if (type == DPP_PA_CONFIGURATION_RESULT)
9180 conn->on_tcp_tx_complete_remove = 1;
9181 dpp_tcp_send(conn);
9182 return 0;
9183 }
9184
9185
9186 int dpp_relay_rx_action(struct dpp_global *dpp, const u8 *src, const u8 *hdr,
9187 const u8 *buf, size_t len, unsigned int freq,
9188 const u8 *i_bootstrap, const u8 *r_bootstrap)
9189 {
9190 struct dpp_relay_controller *ctrl;
9191 struct dpp_connection *conn;
9192 u8 type = hdr[DPP_HDR_LEN - 1];
9193
9194 /* Check if there is an already started session for this peer and if so,
9195 * continue that session (send this over TCP) and return 0.
9196 */
9197 if (type != DPP_PA_PEER_DISCOVERY_REQ &&
9198 type != DPP_PA_PEER_DISCOVERY_RESP) {
9199 dl_list_for_each(ctrl, &dpp->controllers,
9200 struct dpp_relay_controller, list) {
9201 dl_list_for_each(conn, &ctrl->conn,
9202 struct dpp_connection, list) {
9203 if (os_memcmp(src, conn->mac_addr,
9204 ETH_ALEN) == 0)
9205 return dpp_relay_tx(conn, hdr, buf, len);
9206 }
9207 }
9208 }
9209
9210 if (!r_bootstrap)
9211 return -1;
9212
9213 ctrl = dpp_relay_controller_get(dpp, r_bootstrap);
9214 if (!ctrl)
9215 return -1;
9216
9217 wpa_printf(MSG_DEBUG,
9218 "DPP: Authentication Request for a configured Controller");
9219 conn = dpp_relay_new_conn(ctrl, src, freq);
9220 if (!conn)
9221 return -1;
9222
9223 conn->msg_out = dpp_tcp_encaps(hdr, buf, len);
9224 if (!conn->msg_out) {
9225 dpp_connection_remove(conn);
9226 return -1;
9227 }
9228 /* Message will be sent in dpp_conn_tx_ready() */
9229
9230 return 0;
9231 }
9232
9233
9234 int dpp_relay_rx_gas_req(struct dpp_global *dpp, const u8 *src, const u8 *data,
9235 size_t data_len)
9236 {
9237 struct dpp_relay_controller *ctrl;
9238 struct dpp_connection *conn, *found = NULL;
9239 struct wpabuf *msg;
9240
9241 /* Check if there is a successfully completed authentication for this
9242 * and if so, continue that session (send this over TCP) and return 0.
9243 */
9244 dl_list_for_each(ctrl, &dpp->controllers,
9245 struct dpp_relay_controller, list) {
9246 if (found)
9247 break;
9248 dl_list_for_each(conn, &ctrl->conn,
9249 struct dpp_connection, list) {
9250 if (os_memcmp(src, conn->mac_addr,
9251 ETH_ALEN) == 0) {
9252 found = conn;
9253 break;
9254 }
9255 }
9256 }
9257
9258 if (!found)
9259 return -1;
9260
9261 msg = wpabuf_alloc(4 + 1 + data_len);
9262 if (!msg)
9263 return -1;
9264 wpabuf_put_be32(msg, 1 + data_len);
9265 wpabuf_put_u8(msg, WLAN_PA_GAS_INITIAL_REQ);
9266 wpabuf_put_data(msg, data, data_len);
9267 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: Outgoing TCP message", msg);
9268
9269 wpabuf_free(conn->msg_out);
9270 conn->msg_out_pos = 0;
9271 conn->msg_out = msg;
9272 dpp_tcp_send(conn);
9273 return 0;
9274 }
9275
9276
9277 static void dpp_controller_free(struct dpp_controller *ctrl)
9278 {
9279 struct dpp_connection *conn, *tmp;
9280
9281 if (!ctrl)
9282 return;
9283
9284 dl_list_for_each_safe(conn, tmp, &ctrl->conn, struct dpp_connection,
9285 list)
9286 dpp_connection_remove(conn);
9287
9288 if (ctrl->sock >= 0) {
9289 close(ctrl->sock);
9290 eloop_unregister_sock(ctrl->sock, EVENT_TYPE_READ);
9291 }
9292 os_free(ctrl->configurator_params);
9293 os_free(ctrl);
9294 }
9295
9296
9297 static int dpp_controller_rx_auth_req(struct dpp_connection *conn,
9298 const u8 *hdr, const u8 *buf, size_t len)
9299 {
9300 const u8 *r_bootstrap, *i_bootstrap;
9301 u16 r_bootstrap_len, i_bootstrap_len;
9302 struct dpp_bootstrap_info *own_bi = NULL, *peer_bi = NULL;
9303
9304 if (!conn->ctrl)
9305 return 0;
9306
9307 wpa_printf(MSG_DEBUG, "DPP: Authentication Request");
9308
9309 r_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
9310 &r_bootstrap_len);
9311 if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
9312 wpa_printf(MSG_INFO,
9313 "Missing or invalid required Responder Bootstrapping Key Hash attribute");
9314 return -1;
9315 }
9316 wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Bootstrapping Key Hash",
9317 r_bootstrap, r_bootstrap_len);
9318
9319 i_bootstrap = dpp_get_attr(buf, len, DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
9320 &i_bootstrap_len);
9321 if (!i_bootstrap || i_bootstrap_len != SHA256_MAC_LEN) {
9322 wpa_printf(MSG_INFO,
9323 "Missing or invalid required Initiator Bootstrapping Key Hash attribute");
9324 return -1;
9325 }
9326 wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Bootstrapping Key Hash",
9327 i_bootstrap, i_bootstrap_len);
9328
9329 /* Try to find own and peer bootstrapping key matches based on the
9330 * received hash values */
9331 dpp_bootstrap_find_pair(conn->ctrl->global, i_bootstrap, r_bootstrap,
9332 &own_bi, &peer_bi);
9333 if (!own_bi) {
9334 wpa_printf(MSG_INFO,
9335 "No matching own bootstrapping key found - ignore message");
9336 return -1;
9337 }
9338
9339 if (conn->auth) {
9340 wpa_printf(MSG_INFO,
9341 "Already in DPP authentication exchange - ignore new one");
9342 return 0;
9343 }
9344
9345 conn->auth = dpp_auth_req_rx(conn->ctrl->global->msg_ctx,
9346 conn->ctrl->allowed_roles,
9347 conn->ctrl->qr_mutual,
9348 peer_bi, own_bi, -1, hdr, buf, len);
9349 if (!conn->auth) {
9350 wpa_printf(MSG_DEBUG, "DPP: No response generated");
9351 return -1;
9352 }
9353
9354 if (dpp_set_configurator(conn->ctrl->global, conn->ctrl->global->msg_ctx,
9355 conn->auth,
9356 conn->ctrl->configurator_params) < 0) {
9357 dpp_connection_remove(conn);
9358 return -1;
9359 }
9360
9361 wpabuf_free(conn->msg_out);
9362 conn->msg_out_pos = 0;
9363 conn->msg_out = wpabuf_alloc(4 + wpabuf_len(conn->auth->resp_msg) - 1);
9364 if (!conn->msg_out)
9365 return -1;
9366 wpabuf_put_be32(conn->msg_out, wpabuf_len(conn->auth->resp_msg) - 1);
9367 wpabuf_put_data(conn->msg_out, wpabuf_head_u8(conn->auth->resp_msg) + 1,
9368 wpabuf_len(conn->auth->resp_msg) - 1);
9369
9370 if (dpp_tcp_send(conn) == 1) {
9371 if (!conn->write_eloop) {
9372 if (eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
9373 dpp_conn_tx_ready,
9374 conn, NULL) < 0)
9375 return -1;
9376 conn->write_eloop = 1;
9377 }
9378 }
9379
9380 return 0;
9381 }
9382
9383
9384 static int dpp_controller_rx_auth_resp(struct dpp_connection *conn,
9385 const u8 *hdr, const u8 *buf, size_t len)
9386 {
9387 struct dpp_authentication *auth = conn->auth;
9388 struct wpabuf *msg;
9389
9390 if (!auth)
9391 return -1;
9392
9393 wpa_printf(MSG_DEBUG, "DPP: Authentication Response");
9394
9395 msg = dpp_auth_resp_rx(auth, hdr, buf, len);
9396 if (!msg) {
9397 if (auth->auth_resp_status == DPP_STATUS_RESPONSE_PENDING) {
9398 wpa_printf(MSG_DEBUG,
9399 "DPP: Start wait for full response");
9400 return -1;
9401 }
9402 wpa_printf(MSG_DEBUG, "DPP: No confirm generated");
9403 dpp_connection_remove(conn);
9404 return -1;
9405 }
9406
9407 wpabuf_free(conn->msg_out);
9408 conn->msg_out_pos = 0;
9409 conn->msg_out = wpabuf_alloc(4 + wpabuf_len(msg) - 1);
9410 if (!conn->msg_out) {
9411 wpabuf_free(msg);
9412 return -1;
9413 }
9414 wpabuf_put_be32(conn->msg_out, wpabuf_len(msg) - 1);
9415 wpabuf_put_data(conn->msg_out, wpabuf_head_u8(msg) + 1,
9416 wpabuf_len(msg) - 1);
9417 wpabuf_free(msg);
9418
9419 conn->on_tcp_tx_complete_auth_ok = 1;
9420 if (dpp_tcp_send(conn) == 1) {
9421 if (!conn->write_eloop) {
9422 if (eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
9423 dpp_conn_tx_ready,
9424 conn, NULL) < 0)
9425 return -1;
9426 conn->write_eloop = 1;
9427 }
9428 }
9429
9430 return 0;
9431 }
9432
9433
9434 static int dpp_controller_rx_auth_conf(struct dpp_connection *conn,
9435 const u8 *hdr, const u8 *buf, size_t len)
9436 {
9437 struct dpp_authentication *auth = conn->auth;
9438
9439 wpa_printf(MSG_DEBUG, "DPP: Authentication Confirmation");
9440
9441 if (!auth) {
9442 wpa_printf(MSG_DEBUG,
9443 "DPP: No DPP Authentication in progress - drop");
9444 return -1;
9445 }
9446
9447 if (dpp_auth_conf_rx(auth, hdr, buf, len) < 0) {
9448 wpa_printf(MSG_DEBUG, "DPP: Authentication failed");
9449 return -1;
9450 }
9451
9452 dpp_controller_auth_success(conn, 0);
9453 return 0;
9454 }
9455
9456
9457 static int dpp_controller_rx_conf_result(struct dpp_connection *conn,
9458 const u8 *hdr, const u8 *buf,
9459 size_t len)
9460 {
9461 struct dpp_authentication *auth = conn->auth;
9462 enum dpp_status_error status;
9463
9464 if (!conn->ctrl)
9465 return 0;
9466
9467 wpa_printf(MSG_DEBUG, "DPP: Configuration Result");
9468
9469 if (!auth || !auth->waiting_conf_result) {
9470 wpa_printf(MSG_DEBUG,
9471 "DPP: No DPP Configuration waiting for result - drop");
9472 return -1;
9473 }
9474
9475 status = dpp_conf_result_rx(auth, hdr, buf, len);
9476 if (status == DPP_STATUS_OK)
9477 wpa_msg(conn->ctrl->global->msg_ctx, MSG_INFO,
9478 DPP_EVENT_CONF_SENT);
9479 else
9480 wpa_msg(conn->ctrl->global->msg_ctx, MSG_INFO,
9481 DPP_EVENT_CONF_FAILED);
9482 return -1; /* to remove the completed connection */
9483 }
9484
9485
9486 static int dpp_controller_rx_action(struct dpp_connection *conn, const u8 *msg,
9487 size_t len)
9488 {
9489 const u8 *pos, *end;
9490 u8 type;
9491
9492 wpa_printf(MSG_DEBUG, "DPP: Received DPP Action frame over TCP");
9493 pos = msg;
9494 end = msg + len;
9495
9496 if (end - pos < DPP_HDR_LEN ||
9497 WPA_GET_BE24(pos) != OUI_WFA ||
9498 pos[3] != DPP_OUI_TYPE) {
9499 wpa_printf(MSG_DEBUG, "DPP: Unrecognized header");
9500 return -1;
9501 }
9502
9503 if (pos[4] != 1) {
9504 wpa_printf(MSG_DEBUG, "DPP: Unsupported Crypto Suite %u",
9505 pos[4]);
9506 return -1;
9507 }
9508 type = pos[5];
9509 wpa_printf(MSG_DEBUG, "DPP: Received message type %u", type);
9510 pos += DPP_HDR_LEN;
9511
9512 wpa_hexdump(MSG_MSGDUMP, "DPP: Received message attributes",
9513 pos, end - pos);
9514 if (dpp_check_attrs(pos, end - pos) < 0)
9515 return -1;
9516
9517 if (conn->relay) {
9518 wpa_printf(MSG_DEBUG, "DPP: Relay - send over WLAN");
9519 conn->relay->tx(conn->relay->cb_ctx, conn->mac_addr,
9520 conn->freq, msg, len);
9521 return 0;
9522 }
9523
9524 switch (type) {
9525 case DPP_PA_AUTHENTICATION_REQ:
9526 return dpp_controller_rx_auth_req(conn, msg, pos, end - pos);
9527 case DPP_PA_AUTHENTICATION_RESP:
9528 return dpp_controller_rx_auth_resp(conn, msg, pos, end - pos);
9529 case DPP_PA_AUTHENTICATION_CONF:
9530 return dpp_controller_rx_auth_conf(conn, msg, pos, end - pos);
9531 case DPP_PA_CONFIGURATION_RESULT:
9532 return dpp_controller_rx_conf_result(conn, msg, pos, end - pos);
9533 default:
9534 /* TODO: missing messages types */
9535 wpa_printf(MSG_DEBUG,
9536 "DPP: Unsupported frame subtype %d", type);
9537 return -1;
9538 }
9539 }
9540
9541
9542 static int dpp_controller_rx_gas_req(struct dpp_connection *conn, const u8 *msg,
9543 size_t len)
9544 {
9545 const u8 *pos, *end, *next;
9546 u8 dialog_token;
9547 const u8 *adv_proto;
9548 u16 slen;
9549 struct wpabuf *resp, *buf;
9550 struct dpp_authentication *auth = conn->auth;
9551
9552 if (len < 1 + 2)
9553 return -1;
9554
9555 wpa_printf(MSG_DEBUG,
9556 "DPP: Received DPP Configuration Request over TCP");
9557
9558 if (!conn->ctrl || !auth || !auth->auth_success) {
9559 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
9560 return -1;
9561 }
9562
9563 pos = msg;
9564 end = msg + len;
9565
9566 dialog_token = *pos++;
9567 adv_proto = pos++;
9568 slen = *pos++;
9569 if (*adv_proto != WLAN_EID_ADV_PROTO ||
9570 slen > end - pos || slen < 2)
9571 return -1;
9572
9573 next = pos + slen;
9574 pos++; /* skip QueryRespLenLimit and PAME-BI */
9575
9576 if (slen != 8 || *pos != WLAN_EID_VENDOR_SPECIFIC ||
9577 pos[1] != 5 || WPA_GET_BE24(&pos[2]) != OUI_WFA ||
9578 pos[5] != DPP_OUI_TYPE || pos[6] != 0x01)
9579 return -1;
9580
9581 pos = next;
9582 /* Query Request */
9583 if (end - pos < 2)
9584 return -1;
9585 slen = WPA_GET_LE16(pos);
9586 pos += 2;
9587 if (slen > end - pos)
9588 return -1;
9589
9590 resp = dpp_conf_req_rx(auth, pos, slen);
9591 if (!resp)
9592 return -1;
9593
9594 buf = wpabuf_alloc(4 + 18 + wpabuf_len(resp));
9595 if (!buf) {
9596 wpabuf_free(resp);
9597 return -1;
9598 }
9599
9600 wpabuf_put_be32(buf, 18 + wpabuf_len(resp));
9601
9602 wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_RESP);
9603 wpabuf_put_u8(buf, dialog_token);
9604 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
9605 wpabuf_put_le16(buf, 0); /* GAS Comeback Delay */
9606
9607 dpp_write_adv_proto(buf);
9608 dpp_write_gas_query(buf, resp);
9609 wpabuf_free(resp);
9610
9611 /* Send Config Response over TCP; GAS fragmentation is taken care of by
9612 * the Relay */
9613 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: Outgoing TCP message", buf);
9614 wpabuf_free(conn->msg_out);
9615 conn->msg_out_pos = 0;
9616 conn->msg_out = buf;
9617 conn->on_tcp_tx_complete_gas_done = 1;
9618 dpp_tcp_send(conn);
9619 return 0;
9620 }
9621
9622
9623 static int dpp_tcp_rx_gas_resp(struct dpp_connection *conn, struct wpabuf *resp)
9624 {
9625 struct dpp_authentication *auth = conn->auth;
9626 int res;
9627 struct wpabuf *msg, *encaps;
9628 enum dpp_status_error status;
9629
9630 wpa_printf(MSG_DEBUG,
9631 "DPP: Configuration Response for local stack from TCP");
9632
9633 res = dpp_conf_resp_rx(auth, resp);
9634 wpabuf_free(resp);
9635 if (res < 0) {
9636 wpa_printf(MSG_DEBUG, "DPP: Configuration attempt failed");
9637 return -1;
9638 }
9639
9640 if (conn->global->process_conf_obj)
9641 res = conn->global->process_conf_obj(conn->global->cb_ctx,
9642 auth);
9643 else
9644 res = 0;
9645
9646 if (auth->peer_version < 2 || auth->conf_resp_status != DPP_STATUS_OK)
9647 return -1;
9648
9649 #ifdef CONFIG_DPP2
9650 wpa_printf(MSG_DEBUG, "DPP: Send DPP Configuration Result");
9651 status = res < 0 ? DPP_STATUS_CONFIG_REJECTED : DPP_STATUS_OK;
9652 msg = dpp_build_conf_result(auth, status);
9653 if (!msg)
9654 return -1;
9655
9656 encaps = wpabuf_alloc(4 + wpabuf_len(msg) - 1);
9657 if (!encaps) {
9658 wpabuf_free(msg);
9659 return -1;
9660 }
9661 wpabuf_put_be32(encaps, wpabuf_len(msg) - 1);
9662 wpabuf_put_data(encaps, wpabuf_head_u8(msg) + 1, wpabuf_len(msg) - 1);
9663 wpabuf_free(msg);
9664 wpa_hexdump_buf(MSG_MSGDUMP, "DPP: Outgoing TCP message", encaps);
9665
9666 wpabuf_free(conn->msg_out);
9667 conn->msg_out_pos = 0;
9668 conn->msg_out = encaps;
9669 conn->on_tcp_tx_complete_remove = 1;
9670 dpp_tcp_send(conn);
9671
9672 /* This exchange will be terminated in the TX status handler */
9673
9674 return 0;
9675 #else /* CONFIG_DPP2 */
9676 return -1;
9677 #endif /* CONFIG_DPP2 */
9678 }
9679
9680
9681 static int dpp_rx_gas_resp(struct dpp_connection *conn, const u8 *msg,
9682 size_t len)
9683 {
9684 struct wpabuf *buf;
9685 u8 dialog_token;
9686 const u8 *pos, *end, *next, *adv_proto;
9687 u16 status, slen;
9688
9689 if (len < 5 + 2)
9690 return -1;
9691
9692 wpa_printf(MSG_DEBUG,
9693 "DPP: Received DPP Configuration Response over TCP");
9694
9695 pos = msg;
9696 end = msg + len;
9697
9698 dialog_token = *pos++;
9699 status = WPA_GET_LE16(pos);
9700 if (status != WLAN_STATUS_SUCCESS) {
9701 wpa_printf(MSG_DEBUG, "DPP: Unexpected Status Code %u", status);
9702 return -1;
9703 }
9704 pos += 2;
9705 pos += 2; /* ignore GAS Comeback Delay */
9706
9707 adv_proto = pos++;
9708 slen = *pos++;
9709 if (*adv_proto != WLAN_EID_ADV_PROTO ||
9710 slen > end - pos || slen < 2)
9711 return -1;
9712
9713 next = pos + slen;
9714 pos++; /* skip QueryRespLenLimit and PAME-BI */
9715
9716 if (slen != 8 || *pos != WLAN_EID_VENDOR_SPECIFIC ||
9717 pos[1] != 5 || WPA_GET_BE24(&pos[2]) != OUI_WFA ||
9718 pos[5] != DPP_OUI_TYPE || pos[6] != 0x01)
9719 return -1;
9720
9721 pos = next;
9722 /* Query Response */
9723 if (end - pos < 2)
9724 return -1;
9725 slen = WPA_GET_LE16(pos);
9726 pos += 2;
9727 if (slen > end - pos)
9728 return -1;
9729
9730 buf = wpabuf_alloc(slen);
9731 if (!buf)
9732 return -1;
9733 wpabuf_put_data(buf, pos, slen);
9734
9735 if (!conn->relay && !conn->ctrl)
9736 return dpp_tcp_rx_gas_resp(conn, buf);
9737
9738 if (!conn->relay) {
9739 wpa_printf(MSG_DEBUG, "DPP: No matching exchange in progress");
9740 wpabuf_free(buf);
9741 return -1;
9742 }
9743 wpa_printf(MSG_DEBUG, "DPP: Relay - send over WLAN");
9744 conn->relay->gas_resp_tx(conn->relay->cb_ctx, conn->mac_addr,
9745 dialog_token, 0, buf);
9746
9747 return 0;
9748 }
9749
9750
9751 static void dpp_controller_rx(int sd, void *eloop_ctx, void *sock_ctx)
9752 {
9753 struct dpp_connection *conn = eloop_ctx;
9754 int res;
9755 const u8 *pos;
9756
9757 wpa_printf(MSG_DEBUG, "DPP: TCP data available for reading (sock %d)",
9758 sd);
9759
9760 if (conn->msg_len_octets < 4) {
9761 u32 msglen;
9762
9763 res = recv(sd, &conn->msg_len[conn->msg_len_octets],
9764 4 - conn->msg_len_octets, 0);
9765 if (res < 0) {
9766 wpa_printf(MSG_DEBUG, "DPP: recv failed: %s",
9767 strerror(errno));
9768 dpp_connection_remove(conn);
9769 return;
9770 }
9771 if (res == 0) {
9772 wpa_printf(MSG_DEBUG,
9773 "DPP: No more data available over TCP");
9774 dpp_connection_remove(conn);
9775 return;
9776 }
9777 wpa_printf(MSG_DEBUG,
9778 "DPP: Received %d/%d octet(s) of message length field",
9779 res, (int) (4 - conn->msg_len_octets));
9780 conn->msg_len_octets += res;
9781
9782 if (conn->msg_len_octets < 4) {
9783 wpa_printf(MSG_DEBUG,
9784 "DPP: Need %d more octets of message length field",
9785 (int) (4 - conn->msg_len_octets));
9786 return;
9787 }
9788
9789 msglen = WPA_GET_BE32(conn->msg_len);
9790 wpa_printf(MSG_DEBUG, "DPP: Message length: %u", msglen);
9791 if (msglen > 65535) {
9792 wpa_printf(MSG_INFO, "DPP: Unexpectedly long message");
9793 dpp_connection_remove(conn);
9794 return;
9795 }
9796
9797 wpabuf_free(conn->msg);
9798 conn->msg = wpabuf_alloc(msglen);
9799 }
9800
9801 if (!conn->msg) {
9802 wpa_printf(MSG_DEBUG,
9803 "DPP: No buffer available for receiving the message");
9804 dpp_connection_remove(conn);
9805 return;
9806 }
9807
9808 wpa_printf(MSG_DEBUG, "DPP: Need %u more octets of message payload",
9809 (unsigned int) wpabuf_tailroom(conn->msg));
9810
9811 res = recv(sd, wpabuf_put(conn->msg, 0), wpabuf_tailroom(conn->msg), 0);
9812 if (res < 0) {
9813 wpa_printf(MSG_DEBUG, "DPP: recv failed: %s", strerror(errno));
9814 dpp_connection_remove(conn);
9815 return;
9816 }
9817 if (res == 0) {
9818 wpa_printf(MSG_DEBUG, "DPP: No more data available over TCP");
9819 dpp_connection_remove(conn);
9820 return;
9821 }
9822 wpa_printf(MSG_DEBUG, "DPP: Received %d octets", res);
9823 wpabuf_put(conn->msg, res);
9824
9825 if (wpabuf_tailroom(conn->msg) > 0) {
9826 wpa_printf(MSG_DEBUG,
9827 "DPP: Need %u more octets of message payload",
9828 (unsigned int) wpabuf_tailroom(conn->msg));
9829 return;
9830 }
9831
9832 conn->msg_len_octets = 0;
9833 wpa_hexdump_buf(MSG_DEBUG, "DPP: Received TCP message", conn->msg);
9834 if (wpabuf_len(conn->msg) < 1) {
9835 dpp_connection_remove(conn);
9836 return;
9837 }
9838
9839 pos = wpabuf_head(conn->msg);
9840 switch (*pos) {
9841 case WLAN_PA_VENDOR_SPECIFIC:
9842 if (dpp_controller_rx_action(conn, pos + 1,
9843 wpabuf_len(conn->msg) - 1) < 0)
9844 dpp_connection_remove(conn);
9845 break;
9846 case WLAN_PA_GAS_INITIAL_REQ:
9847 if (dpp_controller_rx_gas_req(conn, pos + 1,
9848 wpabuf_len(conn->msg) - 1) < 0)
9849 dpp_connection_remove(conn);
9850 break;
9851 case WLAN_PA_GAS_INITIAL_RESP:
9852 if (dpp_rx_gas_resp(conn, pos + 1,
9853 wpabuf_len(conn->msg) - 1) < 0)
9854 dpp_connection_remove(conn);
9855 break;
9856 default:
9857 wpa_printf(MSG_DEBUG, "DPP: Ignore unsupported message type %u",
9858 *pos);
9859 break;
9860 }
9861 }
9862
9863
9864 static void dpp_controller_tcp_cb(int sd, void *eloop_ctx, void *sock_ctx)
9865 {
9866 struct dpp_controller *ctrl = eloop_ctx;
9867 struct sockaddr_in addr;
9868 socklen_t addr_len = sizeof(addr);
9869 int fd;
9870 struct dpp_connection *conn;
9871
9872 wpa_printf(MSG_DEBUG, "DPP: New TCP connection");
9873
9874 fd = accept(ctrl->sock, (struct sockaddr *) &addr, &addr_len);
9875 if (fd < 0) {
9876 wpa_printf(MSG_DEBUG,
9877 "DPP: Failed to accept new connection: %s",
9878 strerror(errno));
9879 return;
9880 }
9881 wpa_printf(MSG_DEBUG, "DPP: Connection from %s:%d",
9882 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
9883
9884 conn = os_zalloc(sizeof(*conn));
9885 if (!conn)
9886 goto fail;
9887
9888 conn->global = ctrl->global;
9889 conn->ctrl = ctrl;
9890 conn->sock = fd;
9891
9892 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) != 0) {
9893 wpa_printf(MSG_DEBUG, "DPP: fnctl(O_NONBLOCK) failed: %s",
9894 strerror(errno));
9895 goto fail;
9896 }
9897
9898 if (eloop_register_sock(conn->sock, EVENT_TYPE_READ,
9899 dpp_controller_rx, conn, NULL) < 0)
9900 goto fail;
9901 conn->read_eloop = 1;
9902
9903 /* TODO: eloop timeout to expire connections that do not complete in
9904 * reasonable time */
9905 dl_list_add(&ctrl->conn, &conn->list);
9906 return;
9907
9908 fail:
9909 close(fd);
9910 os_free(conn);
9911 }
9912
9913
9914 int dpp_tcp_init(struct dpp_global *dpp, struct dpp_authentication *auth,
9915 const struct hostapd_ip_addr *addr, int port)
9916 {
9917 struct dpp_connection *conn;
9918 struct sockaddr_storage saddr;
9919 socklen_t addrlen;
9920 const u8 *hdr, *pos, *end;
9921 char txt[100];
9922
9923 wpa_printf(MSG_DEBUG, "DPP: Initialize TCP connection to %s port %d",
9924 hostapd_ip_txt(addr, txt, sizeof(txt)), port);
9925 if (dpp_ipaddr_to_sockaddr((struct sockaddr *) &saddr, &addrlen,
9926 addr, port) < 0) {
9927 dpp_auth_deinit(auth);
9928 return -1;
9929 }
9930
9931 conn = os_zalloc(sizeof(*conn));
9932 if (!conn) {
9933 dpp_auth_deinit(auth);
9934 return -1;
9935 }
9936
9937 conn->global = dpp;
9938 conn->auth = auth;
9939 conn->sock = socket(AF_INET, SOCK_STREAM, 0);
9940 if (conn->sock < 0)
9941 goto fail;
9942
9943 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) != 0) {
9944 wpa_printf(MSG_DEBUG, "DPP: fnctl(O_NONBLOCK) failed: %s",
9945 strerror(errno));
9946 goto fail;
9947 }
9948
9949 if (connect(conn->sock, (struct sockaddr *) &saddr, addrlen) < 0) {
9950 if (errno != EINPROGRESS) {
9951 wpa_printf(MSG_DEBUG, "DPP: Failed to connect: %s",
9952 strerror(errno));
9953 goto fail;
9954 }
9955
9956 /*
9957 * Continue connecting in the background; eloop will call us
9958 * once the connection is ready (or failed).
9959 */
9960 }
9961
9962 if (eloop_register_sock(conn->sock, EVENT_TYPE_WRITE,
9963 dpp_conn_tx_ready, conn, NULL) < 0)
9964 goto fail;
9965 conn->write_eloop = 1;
9966
9967 hdr = wpabuf_head(auth->req_msg);
9968 end = hdr + wpabuf_len(auth->req_msg);
9969 hdr += 2; /* skip Category and Actiom */
9970 pos = hdr + DPP_HDR_LEN;
9971 conn->msg_out = dpp_tcp_encaps(hdr, pos, end - pos);
9972 if (!conn->msg_out)
9973 goto fail;
9974 /* Message will be sent in dpp_conn_tx_ready() */
9975
9976 /* TODO: eloop timeout to clear a connection if it does not complete
9977 * properly */
9978 dl_list_add(&dpp->tcp_init, &conn->list);
9979 return 0;
9980 fail:
9981 dpp_connection_free(conn);
9982 return -1;
9983 }
9984
9985
9986 int dpp_controller_start(struct dpp_global *dpp,
9987 struct dpp_controller_config *config)
9988 {
9989 struct dpp_controller *ctrl;
9990 int on = 1;
9991 struct sockaddr_in sin;
9992 int port;
9993
9994 if (!dpp || dpp->controller)
9995 return -1;
9996
9997 ctrl = os_zalloc(sizeof(*ctrl));
9998 if (!ctrl)
9999 return -1;
10000 ctrl->global = dpp;
10001 if (config->configurator_params)
10002 ctrl->configurator_params =
10003 os_strdup(config->configurator_params);
10004 dl_list_init(&ctrl->conn);
10005 /* TODO: configure these somehow */
10006 ctrl->allowed_roles = DPP_CAPAB_ENROLLEE | DPP_CAPAB_CONFIGURATOR;
10007 ctrl->qr_mutual = 0;
10008
10009 ctrl->sock = socket(AF_INET, SOCK_STREAM, 0);
10010 if (ctrl->sock < 0)
10011 goto fail;
10012
10013 if (setsockopt(ctrl->sock, SOL_SOCKET, SO_REUSEADDR,
10014 &on, sizeof(on)) < 0) {
10015 wpa_printf(MSG_DEBUG,
10016 "DPP: setsockopt(SO_REUSEADDR) failed: %s",
10017 strerror(errno));
10018 /* try to continue anyway */
10019 }
10020
10021 if (fcntl(ctrl->sock, F_SETFL, O_NONBLOCK) < 0) {
10022 wpa_printf(MSG_INFO, "DPP: fnctl(O_NONBLOCK) failed: %s",
10023 strerror(errno));
10024 goto fail;
10025 }
10026
10027 /* TODO: IPv6 */
10028 os_memset(&sin, 0, sizeof(sin));
10029 sin.sin_family = AF_INET;
10030 sin.sin_addr.s_addr = INADDR_ANY;
10031 port = config->tcp_port ? config->tcp_port : DPP_TCP_PORT;
10032 sin.sin_port = htons(port);
10033 if (bind(ctrl->sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
10034 wpa_printf(MSG_INFO,
10035 "DPP: Failed to bind Controller TCP port: %s",
10036 strerror(errno));
10037 goto fail;
10038 }
10039 if (listen(ctrl->sock, 10 /* max backlog */) < 0 ||
10040 fcntl(ctrl->sock, F_SETFL, O_NONBLOCK) < 0 ||
10041 eloop_register_sock(ctrl->sock, EVENT_TYPE_READ,
10042 dpp_controller_tcp_cb, ctrl, NULL))
10043 goto fail;
10044
10045 dpp->controller = ctrl;
10046 wpa_printf(MSG_DEBUG, "DPP: Controller started on TCP port %d", port);
10047 return 0;
10048 fail:
10049 dpp_controller_free(ctrl);
10050 return -1;
10051 }
10052
10053
10054 void dpp_controller_stop(struct dpp_global *dpp)
10055 {
10056 if (dpp) {
10057 dpp_controller_free(dpp->controller);
10058 dpp->controller = NULL;
10059 }
10060 }
10061
10062 #endif /* CONFIG_DPP2 */