-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light_JWT.cpp
470 lines (379 loc) · 13.1 KB
/
Light_JWT.cpp
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "Light_JWT.h"
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
int f_rng(void *data, unsigned char *output, size_t len)
{
const char *pers = "your_application_name";
mbedtls_entropy_init(&entropy);
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, strlen(pers));
return mbedtls_ctr_drbg_random(&ctr_drbg, output, len);
}
// base64_encode copied from https://github.com/ReneNyffenegger/cpp-base64
static const char base64url_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-_";
String LightJWT::base64UrlEncodeRaw(String textToEncode)
{
String base64url_encoded;
const unsigned char *bytes_to_encode = (const unsigned char *)textToEncode.c_str();
unsigned int in_len = textToEncode.length();
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] =
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] =
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i < 4); i++)
{
base64url_encoded += base64url_chars[char_array_4[i]];
}
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
{
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] =
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] =
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
{
base64url_encoded += base64url_chars[char_array_4[j]];
}
}
return base64url_encoded;
}
unsigned long LightJWT::getCurrentEpochTimeInSeconds()
{
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
// Serial.println("Failed to obtain time");
return (0);
}
time(&now);
return now;
}
String LightJWT::getHeader(JWT_ALG_type algType)
{
String header = "{\"alg\":\"{{ALG}}\",\"typ\":\"JWT\"}";
switch (algType)
{
case JWT_ALG_RS256:
header.replace("{{ALG}}", "RS256");
break;
case JWT_ALG_ES256:
header.replace("{{ALG}}", "ES256");
break;
default:
header.replace("{{ALG}}", "RS256");
break;
}
return header;
}
String LightJWT::getPayload(String issuer,
String audience,
String scope,
unsigned long expirationInSecond)
{
String payload = "{\"iss\":\"{{ISS}}\",\"aud\":\"{{AUD}}\",\"scope\":\"{{SCOPE}}\",\"iat\":{{IAT}},\"exp\":{{EXP}}}";
unsigned long iat = LightJWT().getCurrentEpochTimeInSeconds(); /* 1657550511 */
payload.replace("{{ISS}}", issuer);
payload.replace("{{AUD}}", audience);
payload.replace("{{SCOPE}}", scope);
payload.replace("{{IAT}}", String(iat));
payload.replace("{{EXP}}", String((iat + expirationInSecond)));
return payload;
}
String LightJWT::JWT(
JWT_ALG_type algType,
String payload,
const char *privateKey)
{
/* Header */
String header = LightJWT().base64UrlEncodeRaw(LightJWT()
.getHeader(algType));
/* Payload */
payload = LightJWT().base64UrlEncodeRaw(payload);
// Serial.print("--------- Payload: ");
// Serial.println(payload);
String headerPayload = String(header + "." + payload);
/* Sign */
/* Get SHA256 of header+payload: sha256_b64h_b64p = SHA256(header_base64url . payload_base64url) */
unsigned char sha256_b64h_b64p[32] = {0};
mbedtls_sha256_context sha_ctx;
mbedtls_sha256_init(&sha_ctx);
mbedtls_sha256_starts(&sha_ctx, 0);
// Serial.print("headerPayload: ");
// Serial.println(headerPayload);
// Serial.println("-----------");
size_t headerPayloadSize = headerPayload.length();
unsigned char uHeaderPayload[headerPayloadSize];
strcpy((char *)uHeaderPayload, headerPayload.c_str());
// unsigned char uHeaderPayload[headerPayloadSize] = {0};
// std::copy(headerPayload.c_str(), headerPayload.c_str() + headerPayload.length(), uHeaderPayload);
// Serial.print("uHeaderPayload: ");
// Serial.println((char*)uHeaderPayload);
mbedtls_sha256_update(&sha_ctx, uHeaderPayload, headerPayloadSize);
mbedtls_sha256_finish(&sha_ctx, sha256_b64h_b64p);
mbedtls_sha256_free(&sha_ctx);
// Serial.print("sha256_b64h_b64p: ");
// Serial.println((char *)(sha256_b64h_b64p));
// Serial.println(LightJWT().base64UrlEncodeRaw((char *)(sha256_b64h_b64p)));
/* Load Private Key */
// Serial.print("privateKey: ");
// Serial.println(privateKey);
size_t privateKeySize = strlen(privateKey);
unsigned char uPrivateKey[privateKeySize];
strcpy((char *)uPrivateKey, privateKey);
// Serial.print("uPrivateKey: ");
// Serial.println((char*)uPrivateKey);
mbedtls_pk_context pkContext;
mbedtls_pk_init(&pkContext);
mbedtls_pk_parse_key(
&pkContext,
uPrivateKey,
privateKeySize + 1,
NULL, 0, f_rng, NULL);
// auto ecContext = mbedtls_pk_ec(pkContext);
String sign;
switch (algType)
{
case JWT_ALG_RS256:
{
auto rsa = mbedtls_pk_rsa(pkContext);
if (rsa == NULL)
{
// Serial.println("RSA Context is invalid");
return String("INVALID-RSA-CONTEXT");
}
int keyValid = mbedtls_rsa_check_privkey(rsa);
// Serial.print("keyValid: ");
// Serial.println(keyValid);
if (keyValid != 0)
return String("INVALID-PRIVATE-KEY");
// Serial.println("Encrypting sha256_b64h_b64p...");
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
int success = mbedtls_rsa_pkcs1_sign(rsa, f_rng, NULL, MBEDTLS_MD_SHA256, 32, sha256_b64h_b64p, buf);
// Serial.print("Encrypted? ");
// Serial.println(success);
if (success != 0)
return String("CAN-NOT-SIGN");
unsigned char signature[LightJWT().encode_base64_length(rsa->len)];
LightJWT().encode_base64(buf, rsa->len, signature);
// Serial.print("signature(b64): ");
// Serial.println((char *)signature);
sign = String((char *)signature);
mbedtls_rsa_free(rsa);
mbedtls_pk_free(&pkContext);
break;
}
case JWT_ALG_ES256:
{
auto ecdsa = mbedtls_pk_ec(pkContext);
if (ecdsa == NULL)
{
// Serial.println("ECDSA Context is invalid");
return String("INVALID-ECDSA-CONTEXT");
}
// Serial.println("Encrypting sha256_b64h_b64p...");
unsigned char signature[64];
size_t signatureLength;
mbedtls_mpi r, s;
mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s);
int ret = mbedtls_ecdsa_sign_det_ext(&ecdsa->grp, &r, &s, &ecdsa->d, sha256_b64h_b64p, 32, MBEDTLS_MD_SHA256, f_rng, NULL);
if (ret != 0)
{
return String("CAN-NOT-SIGN");
}
// Write the signature in P1363 format
mbedtls_mpi_write_binary(&r, signature, mbedtls_mpi_size(&r));
mbedtls_mpi_write_binary(&s, signature + mbedtls_mpi_size(&r), mbedtls_mpi_size(&s));
signatureLength = mbedtls_mpi_size(&r) + mbedtls_mpi_size(&s);
/* mbedtls_ecdsa_write_signature writes signature out as DER format */
// int success = mbedtls_ecdsa_write_signature(ecdsa, MBEDTLS_MD_SHA256, sha256_b64h_b64p, 32, buf, &signatureLength, NULL, NULL);
/* Print signature as hex */
// Serial.print("signature (hex) (" + String(signatureLength) + ") : [");
// for (size_t i = 0; i < signatureLength; i++)
// {
// Serial.printf("%02x", signature[i]);
// }
// Serial.println("]");
unsigned char _signature[LightJWT().encode_base64_length(signatureLength)];
LightJWT().encode_base64(signature, signatureLength, _signature);
// Serial.print("signature(b64): ");
// Serial.println((char *)_signature);
sign = String((char *)_signature);
mbedtls_ecdsa_free(ecdsa);
mbedtls_pk_free(&pkContext);
mbedtls_mpi_free(&r);
mbedtls_mpi_free(&s);
break;
}
default:
return String("INVALID-ALG-TYPE");
}
return String(headerPayload + "." + sign);
}
unsigned char LightJWT::binary_to_base64(unsigned char v)
{
// Capital letters - 'A' is ascii 65 and base64 0
if (v < 26)
return v + 'A';
// Lowercase letters - 'a' is ascii 97 and base64 26
if (v < 52)
return v + 71;
// Digits - '0' is ascii 48 and base64 52
if (v < 62)
return v - 4;
#ifdef BASE64_URL
// '-' is ascii 45 and base64 62
if (v == 62)
return '-';
#else
// '+' is ascii 43 and base64 62
if (v == 62)
return '+';
#endif
#ifdef BASE64_URL
// '_' is ascii 95 and base64 62
if (v == 63)
return '_';
#else
// '/' is ascii 47 and base64 63
if (v == 63)
return '/';
#endif
return 64;
}
unsigned char LightJWT::base64_to_binary(unsigned char c)
{
// Capital letters - 'A' is ascii 65 and base64 0
if ('A' <= c && c <= 'Z')
return c - 'A';
// Lowercase letters - 'a' is ascii 97 and base64 26
if ('a' <= c && c <= 'z')
return c - 71;
// Digits - '0' is ascii 48 and base64 52
if ('0' <= c && c <= '9')
return c + 4;
#ifdef BASE64_URL
// '-' is ascii 45 and base64 62
if (c == '-')
return 62;
#else
// '+' is ascii 43 and base64 62
if (c == '+')
return 62;
#endif
#ifdef BASE64_URL
// '_' is ascii 95 and base64 62
if (c == '_')
return 63;
#else
// '/' is ascii 47 and base64 63
if (c == '/')
return 63;
#endif
return 255;
}
unsigned int LightJWT::encode_base64_length(unsigned int input_length)
{
return (input_length + 2) / 3 * 4;
}
unsigned int LightJWT::decode_base64_length(unsigned char input[])
{
return decode_base64_length(input, -1);
}
unsigned int LightJWT::decode_base64_length(unsigned char input[], unsigned int input_length)
{
unsigned char *start = input;
while (base64_to_binary(input[0]) < 64 && (unsigned int)(input - start) < input_length)
{
++input;
}
input_length = (unsigned int)(input - start);
return input_length / 4 * 3 + (input_length % 4 ? input_length % 4 - 1 : 0);
}
unsigned int LightJWT::encode_base64(unsigned char input[], unsigned int input_length, unsigned char output[])
{
unsigned int full_sets = input_length / 3;
// While there are still full sets of 24 bits...
for (unsigned int i = 0; i < full_sets; ++i)
{
output[0] = binary_to_base64(input[0] >> 2);
output[1] = binary_to_base64((input[0] & 0x03) << 4 | input[1] >> 4);
output[2] = binary_to_base64((input[1] & 0x0F) << 2 | input[2] >> 6);
output[3] = binary_to_base64(input[2] & 0x3F);
input += 3;
output += 4;
}
switch (input_length % 3)
{
case 0:
output[0] = '\0';
break;
case 1:
output[0] = binary_to_base64(input[0] >> 2);
output[1] = binary_to_base64((input[0] & 0x03) << 4);
output[2] = '=';
output[3] = '=';
output[4] = '\0';
break;
case 2:
output[0] = binary_to_base64(input[0] >> 2);
output[1] = binary_to_base64((input[0] & 0x03) << 4 | input[1] >> 4);
output[2] = binary_to_base64((input[1] & 0x0F) << 2);
output[3] = '=';
output[4] = '\0';
break;
}
return encode_base64_length(input_length);
}
unsigned int LightJWT::decode_base64(unsigned char input[], unsigned char output[])
{
return decode_base64(input, -1, output);
}
unsigned int LightJWT::decode_base64(unsigned char input[], unsigned int input_length, unsigned char output[])
{
unsigned int output_length = decode_base64_length(input, input_length);
// While there are still full sets of 24 bits...
for (unsigned int i = 2; i < output_length; i += 3)
{
output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4;
output[1] = base64_to_binary(input[1]) << 4 | base64_to_binary(input[2]) >> 2;
output[2] = base64_to_binary(input[2]) << 6 | base64_to_binary(input[3]);
input += 4;
output += 3;
}
switch (output_length % 3)
{
case 1:
output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4;
break;
case 2:
output[0] = base64_to_binary(input[0]) << 2 | base64_to_binary(input[1]) >> 4;
output[1] = base64_to_binary(input[1]) << 4 | base64_to_binary(input[2]) >> 2;
break;
}
return output_length;
}