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