1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include "unity.h"
13 #include <string.h>
14 #include "utils/common.h"
15 #include "utils/includes.h"
16 #include "crypto/crypto.h"
17 #include "../src/common/defs.h"
18 #include "../src/common/dpp.h"
19 
20 #ifdef CONFIG_ESP_WIFI_TESTING_OPTIONS
21 struct dpp_global {
22 	void *msg_ctx;
23 	struct dl_list bootstrap; /* struct dpp_bootstrap_info */
24 	struct dl_list configurator; /* struct dpp_configurator */
25 };
26 
27 extern u8 dpp_protocol_key_override[600];
28 extern size_t dpp_protocol_key_override_len;
29 extern u8 dpp_nonce_override[DPP_MAX_NONCE_LEN];
30 extern size_t dpp_nonce_override_len;
31 #define MAX_FRAME_SIZE 1200
32 
33 TEST_CASE("Test vectors DPP responder p256", "[wpa_dpp]")
34 {
35 	/* Global variables */
36 	char command[1200] = {0};
37 	const u8 *frame;
38 	int len = 0;
39 	struct dpp_authentication *auth_instance = NULL;
40 	u8 auth[MAX_FRAME_SIZE] = {0};
41 	char prefix[] = "30310201010420";
42 	char postfix[] = "a00a06082a8648ce3d030107";
43 	size_t hex_len;
44 	int ret = 0;
45 	int id;
46 
47 	/* DPP global config initialization */
48 	struct dpp_global_config dpp_conf;
49 	memset(&dpp_conf, 0, sizeof(dpp_conf));
50 	struct dpp_global *dpp = dpp_global_init(&dpp_conf);
51 
52 	/* bootstrap generation test */
53 	ESP_LOGI("DPP Test", "bootstrap generation test");
54 	{
55 		char key[1000] = {0};
56 		const char *uri;
57 
58 		char private_bootstrap_key[] = "54ce181a98525f217216f59b245f60e9df30ac7f6b26c939418cfc3c42d1afa0";
59 		char bootstrap_info[] = "DPP:K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgACCcWFqRtN+f0loEUgGIXDnMXPrjl92u2pV97Ff6DjUD8=;;";
60 
61 		sprintf(key, "%s%s%s", prefix, private_bootstrap_key, postfix);
62 
63 		sprintf(command, "type=qrcode key=%s", key);
64 		id = dpp_bootstrap_gen(dpp, command);
65 		uri =  dpp_bootstrap_get_uri(dpp, id);
66 		printf("uri is =%s\n", uri);
67 		printf("is  be =%s\n", bootstrap_info);
68 		TEST_ASSERT((strcmp(uri, bootstrap_info) == 0));
69 	}
70 	ESP_LOGI("DPP Test", "bootstap generation passed");
71 	ESP_LOGI("DPP Test", "Overwrite Protocol key, responder nounce");
72 	{
73 		char protocol_key[] = "f798ed2e19286f6a6efe210b1863badb99af2a14b497634dbfd2a97394fb5aa5";
74 		char nounce[] = "3d0cfb011ca916d796f7029ff0b43393";
75 
76 		/* Overwrite protocol key */
77 		memset(command, 0, 1200);
78 		sprintf(command, "%s%s%s", prefix, protocol_key, postfix);
79 
80 		hex_len = os_strlen(command);
81 		ret = 0;
82 
83 		if (hex_len > 2 * sizeof(dpp_protocol_key_override))
84 			ret = -1;
85 		else if (hexstr2bin(command, dpp_protocol_key_override,
86 					hex_len / 2))
87 			ret = -1;
88 		else
89 			dpp_protocol_key_override_len = hex_len / 2;
90 
91 		TEST_ASSERT(ret == 0);
92 
93 		/* Overwrite nounce */
94 		hex_len = os_strlen(nounce);
95 
96 		if (hex_len > 2 * sizeof(dpp_nonce_override))
97 			ret = -1;
98 		else if (hexstr2bin(nounce, dpp_nonce_override, hex_len / 2))
99 			ret = -1;
100 		else
101 			dpp_nonce_override_len = hex_len / 2;
102 
103 		TEST_ASSERT(ret == 0);
104 
105 	}
106 	ESP_LOGI("DPP Test", "Overwritten Protocol key, responder nounce.. ");
107 	ESP_LOGI("DPP Test", "Enqueue Auth request");
108 	{
109 		char auth_req[] = "d00012001ac459c40d649f8664c1b8771ac459c40d6400120409506f9a1a010002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d011020005d467a09760292fc15d31792b0a5b050db8bf6ad807d71b2d93f4d1c2e65d8810310400050a532ae2a07207276418d2fa630295d45569be425aa634f02014d00a7d1f61ae14f35a5a858bccad90d126c46594c49ef82655e78888e15a32d916ac217249118100200510104102900868f478fc599ac3fa8152b975eff8be4e71b189dbefbc3185b1d7f3864e896f913cba3d9601326f278";
110 
111 		char auth_resp[] = "d00012349f8664c1b8771ac459c40d649f8664c1b87712340409506f9a1a0101001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d091040005e3fb3576884887f17c3203d8a3a6c2fac722ef0e2201b61ac73bc655c709a902d4b030669fb9eff8b0a79fa7c1a172ac2a92c626256963f9274dc90682c81e504107500da553cdf80da3e27054c5e1f809ac303c63948b9bb5690ad12f357d75dfbc362cbae89e472dd6851925534024310aff5ae403831e98a7efc7deb9516164329c227039ae73c509147d156ae085f56c242bf7decc1f3b68d81697c6197453cb6faff7b062f7861073148052db539895bc6583d08b4aa";
112 		u8 *tmp;
113 
114 		hex_len = os_strlen(auth_req);
115 		if (hex_len > 2 * MAX_FRAME_SIZE)
116 			ret = -1;
117 		else if (hexstr2bin(auth_req, auth, hex_len / 2))
118 			ret = -1;
119 		else
120 			len = hex_len / 2;
121 		frame = auth;
122 		frame += 26;
123 		len -= 26;
124 		auth_instance = dpp_auth_req_rx(NULL, 1, 0 , NULL,
125 				dpp_bootstrap_get_id(dpp, id), 2412, frame, frame+6, len-6);
126 
127 		/* auth response u8 */
128 		hex_len = os_strlen(auth_resp);
129 		if (hex_len > 2 * MAX_FRAME_SIZE)
130 			ret = -1;
131 		else if (hexstr2bin(auth_resp, auth, hex_len / 2))
132 			ret = -1;
133 		else
134 			len = hex_len / 2;
135 		tmp = auth;
136 		tmp += 26;
137 		len -= 26;
138 
139 		frame = wpabuf_head_u8(auth_instance->resp_msg);
140 		len = wpabuf_len(auth_instance->resp_msg);
141 
142 		TEST_ASSERT(memcmp(frame + 28, tmp  + 26, len - 26) == 0);
143 	}
144 	ESP_LOGI("DPP Test", "Auth request parsing passed");
145 	ESP_LOGI("DPP Test", "Enqueue Auth confirm parsing passed");
146 	{
147 		char auth_confirm[] = "d00012341ac459c40d649f8664c1b8771ac459c40d6412340409506f9a1a0102001001000002102000922ddd7a3ed69f46125d772bbe6017cd4e03870dc014509e38b54628e157a87d0410340054e07e62c74526dfd97e029dc781e0771e573ebc73c94227b5de8350fc6a1974b40f54c9fe1a1c9288a91fce4ee6c1f2ff069741";
148 		hex_len = os_strlen(auth_confirm);
149 		os_memset(auth, 0, 1200);
150 		if (hex_len > 2 * MAX_FRAME_SIZE)
151 			ret = -1;
152 		else if (hexstr2bin(auth_confirm, auth, hex_len / 2))
153 			ret = -1;
154 		else
155 			len = hex_len / 2;
156 		frame = auth;
157 		frame = auth + 26;
158 		len = len - 26;
159 		dpp_auth_conf_rx(auth_instance, frame, frame+6, len-6);
160 		TEST_ASSERT(auth_instance->auth_success == 1);
161 	}
162 	ESP_LOGI("DPP Test", "Auth confirm parsing passed");
163 	/* deinit for memory passing */
164 	{
165 		dpp_auth_deinit(auth_instance);
166 		dpp_global_deinit(dpp);
167 	}
168 	ESP_LOGI("DPP Test", "Test case passed");
169 }
170 #endif
171