1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
38
39
40
41
45
46
47
48
49
50
51
52
53
54
64
65
66
67
71
72
76
77
78
79
80
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
102
103
104
105
106
111
112
113
114
115
116
117
118
119
120
121
124
125
126
127
128
129
130
135
136
137
138
139
140
141
142
143
144
145
146
150
151
152
156
157
158
159
163
164
165
166
170
171
172
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
207
208
209
210
211
212
213
214
219
220
221
222
223
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
258
259
260
261
262
263
264
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
302
303
304
305
306
307
308
309
310
314
315
316
317
318
319
320
321
322
323
324
325
326
327
330
331
332
333
334
335
336
337
338
341
342
343
344
345
346
347
348
349
350
351
352
353
354
357
358
361
362
363
364
365
366
367
368
369
370
371
372
373
374
380
381
382
383
389
392
393
400
401
407
408
409
410
411
412
413
414
415
416
/* ... */
#ifdef ESP_PLATFORM
#include "mbedtls/bignum.h"
#endif
#include "utils/includes.h"
#include "utils/common.h"
#include "crypto.h"
#include "common/defs.h"
#ifdef CONFIG_CRYPTO_MBEDTLS
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include <mbedtls/error.h>
#include <mbedtls/x509_crt.h>
#include <mbedtls/platform.h>
#include <mbedtls/sha256.h>6 includes
struct crypto_public_key;
struct crypto_private_key;
#ifdef DEBUG_PRINT
static void crypto_dump_verify_info(u32 flags)
{
char dump_buffer[1024];
mbedtls_x509_crt_verify_info(dump_buffer, 1024, " ! ", flags);
wpa_printf(MSG_ERROR, "%s", dump_buffer);
}{...}
/* ... */#else
static void crypto_dump_verify_info(u32 flags) { }
#endif
static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len)
{
return os_get_random(buf, len);
}{ ... }
int crypto_verify_cert(const u8 *cert_start, int certlen, const u8 *ca_cert_start, int ca_certlen)
{
int ret;
u32 flags = 0;
mbedtls_x509_crt *cert = os_zalloc(sizeof(mbedtls_x509_crt));
mbedtls_x509_crt *ca_cert = os_zalloc(sizeof(mbedtls_x509_crt));
if (!cert || !ca_cert) {
if (cert) {
os_free(cert);
}{...}
if (ca_cert) {
os_free(ca_cert);
}{...}
wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__);
return -1;
}{...}
mbedtls_x509_crt_init(cert);
mbedtls_x509_crt_init(ca_cert);
ret = mbedtls_x509_crt_parse(cert, cert_start, certlen);
if (ret < 0) {
wpa_printf(MSG_ERROR, "peer cert parsing failed");
goto cleanup;
}{...}
ret = mbedtls_x509_crt_parse(ca_cert, ca_cert_start, ca_certlen);
if (ret < 0) {
wpa_printf(MSG_ERROR, "CA cert parsing failed");
goto cleanup;
}{...}
ret = mbedtls_x509_crt_verify(cert, ca_cert, NULL, NULL, &flags, NULL, NULL);
if (ret != 0) {
crypto_dump_verify_info(flags);
}{...}
cleanup:
mbedtls_x509_crt_free(cert);
mbedtls_x509_crt_free(ca_cert);
os_free(cert);
os_free(ca_cert);
return ret;
}{ ... }
struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
{
int ret;
mbedtls_pk_context *pkey = os_zalloc(sizeof(*pkey));
if (!pkey) {
return NULL;
}{...}
mbedtls_pk_init(pkey);
ret = mbedtls_pk_parse_public_key(pkey, key, len);
if (ret < 0) {
wpa_printf(MSG_ERROR, "failed to parse public key");
os_free(pkey);
return NULL;
}{...}
return (struct crypto_public_key *)pkey;
}{ ... }
struct crypto_private_key * crypto_private_key_import(const u8 *key,
size_t len,
const char *passwd)
{
int ret;
mbedtls_pk_context *pkey = os_zalloc(sizeof(mbedtls_pk_context));
if (!pkey) {
return NULL;
}{...}
mbedtls_pk_init(pkey);
ret = mbedtls_pk_parse_key(pkey, key, len, (const unsigned char *)passwd,
passwd ? os_strlen(passwd) : 0, crypto_rng_wrapper, NULL);
if (ret < 0) {
wpa_printf(MSG_ERROR, "failed to parse private key");
os_free(pkey);
pkey = NULL;
}{...}
return (struct crypto_private_key *)pkey;
}{ ... }
struct crypto_public_key *crypto_public_key_from_cert(const u8 *buf,
size_t len)
{
int ret;
mbedtls_x509_crt *cert;
mbedtls_pk_context *kctx = os_zalloc(sizeof(*kctx));
if (!kctx) {
wpa_printf(MSG_ERROR, "failed to allocate memory");
return NULL;
}{...}
cert = os_zalloc(sizeof(mbedtls_x509_crt));
if (!cert) {
wpa_printf(MSG_ERROR, "failed to allocate memory");
goto fail;
}{...}
mbedtls_x509_crt_init(cert);
ret = mbedtls_x509_crt_parse(cert, buf, len);
if (ret < 0) {
wpa_printf(MSG_ERROR, "cert parsing failed");
goto fail;
}{...}
mbedtls_pk_init(kctx);
if (mbedtls_pk_setup(kctx, mbedtls_pk_info_from_type(mbedtls_pk_get_type(&cert->pk))) != 0) {
wpa_printf(MSG_ERROR, "key setup failed");
goto fail;
}{...}
ret = mbedtls_rsa_copy(mbedtls_pk_rsa(*kctx), mbedtls_pk_rsa(cert->pk));
if (ret < 0) {
wpa_printf(MSG_ERROR, "key copy failed");
goto fail;
}{...}
cleanup:
mbedtls_x509_crt_free(cert);
os_free(cert);
return (struct crypto_public_key *)kctx;
fail:
os_free(kctx);
kctx = NULL;
goto cleanup;
}{ ... }
int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
const u8 *in, size_t inlen,
u8 *out, size_t *outlen)
{
int ret;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
const char *pers = "rsa_encrypt";
mbedtls_entropy_context *entropy = os_zalloc(sizeof(*entropy));
mbedtls_ctr_drbg_context *ctr_drbg = os_zalloc(sizeof(*ctr_drbg));
if (!pkey || !entropy || !ctr_drbg) {
if (entropy) {
os_free(entropy);
}{...}
if (ctr_drbg) {
os_free(ctr_drbg);
}{...}
wpa_printf(MSG_ERROR, "failed to allocate memory");
return -1;
}{...}
mbedtls_entropy_init(entropy);
mbedtls_ctr_drbg_init(ctr_drbg);
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
entropy, (const unsigned char *) pers,
strlen(pers));
if (ret != 0) {
wpa_printf(MSG_ERROR, " failed ! mbedtls_ctr_drbg_seed returned %d",
ret);
goto cleanup;
}{...}
ret = mbedtls_rsa_pkcs1_encrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random,
ctr_drbg, inlen, in, out);
if (ret != 0) {
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_encrypt returned -0x%04x", -ret);
goto cleanup;
}{...}
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
cleanup:
mbedtls_ctr_drbg_free(ctr_drbg);
mbedtls_entropy_free(entropy);
os_free(entropy);
os_free(ctr_drbg);
return ret;
}{ ... }
int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
const u8 *in, size_t inlen,
u8 *out, size_t *outlen)
{
int ret;
size_t i;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
const char *pers = "rsa_decrypt";
mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy));
mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg));
if (!pkey || !entropy || !ctr_drbg) {
if (entropy) {
os_free(entropy);
}{...}
if (ctr_drbg) {
os_free(ctr_drbg);
}{...}
return -1;
}{...}
mbedtls_ctr_drbg_init(ctr_drbg);
mbedtls_entropy_init(entropy);
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
entropy, (const unsigned char *) pers,
strlen(pers));
if (ret < 0) {
goto cleanup;
}{...}
i = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random,
ctr_drbg, &i, in, out, *outlen);
*outlen = i;
cleanup:
mbedtls_ctr_drbg_free(ctr_drbg);
mbedtls_entropy_free(entropy);
os_free(entropy);
os_free(ctr_drbg);
return ret;
}{ ... }
int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
const u8 *in, size_t inlen,
u8 *out, size_t *outlen)
{
int ret;
const char *pers = "rsa_encrypt";
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
mbedtls_entropy_context *entropy = os_malloc(sizeof(*entropy));
mbedtls_ctr_drbg_context *ctr_drbg = os_malloc(sizeof(*ctr_drbg));
if (!pkey || !entropy || !ctr_drbg) {
if (entropy) {
os_free(entropy);
}{...}
if (ctr_drbg) {
os_free(ctr_drbg);
}{...}
return -1;
}{...}
mbedtls_ctr_drbg_init(ctr_drbg);
mbedtls_entropy_init(entropy);
ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func,
entropy, (const unsigned char *) pers,
strlen(pers));
if ((ret = mbedtls_rsa_pkcs1_sign(mbedtls_pk_rsa(*pkey), mbedtls_ctr_drbg_random, ctr_drbg,
(mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id),
inlen, in, out)) != 0) {
wpa_printf(MSG_ERROR, " failed ! mbedtls_rsa_pkcs1_sign returned %d", ret);
goto cleanup;
}{...}
*outlen = mbedtls_rsa_get_len(mbedtls_pk_rsa(*pkey));
cleanup:
mbedtls_ctr_drbg_free(ctr_drbg);
mbedtls_entropy_free(entropy);
os_free(entropy);
os_free(ctr_drbg);
return ret;
}{ ... }
void crypto_public_key_free(struct crypto_public_key *key)
{
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
if (!pkey) {
return;
}{...}
mbedtls_pk_free(pkey);
os_free(pkey);
}{ ... }
void crypto_private_key_free(struct crypto_private_key *key)
{
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
if (!pkey) {
return;
}{...}
mbedtls_pk_free(pkey);
os_free(pkey);
}{ ... }
int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
const u8 *crypt, size_t crypt_len,
u8 *plain, size_t *plain_len)
{
size_t len;
u8 *pos;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
len = mbedtls_pk_rsa(*pkey)->MBEDTLS_PRIVATE(len);
if (len != crypt_len) {
return -1;
}{...}
if (mbedtls_rsa_public(mbedtls_pk_rsa(*pkey), crypt, plain) < 0) {
return -1;
}{...}
/* ... */
if (len < 3 + 8 + 16 ||
plain[0] != 0x00 || plain[1] != 0x01) {
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
"structure");
wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len);
return -1;
}{...}
pos = plain + 3;
if (plain[2] != 0xff) {
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
"PS (BT=01)");
wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len);
return -1;
}{...}
while (pos < plain + len && *pos == 0xff) {
pos++;
}{...}
if (pos - plain - 2 < 8) {
wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
"padding");
wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len);
return -1;
}{...}
if (pos + 16 >= plain + len || *pos != 0x00) {
wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
"structure (2)");
wpa_hexdump_key(MSG_DEBUG, "Signature EB", plain, len);
return -1;
}{...}
pos++;
len -= pos - plain;
os_memmove(plain, pos, len);
*plain_len = len;
return 0;
}{ ... }
#endif/* ... */