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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
/*
* Copyright (C) Vadim Zhestkov
* Copyright (C) F5, Inc.
*/
#include <qjs.h>
#include "njs_hash.h"
typedef void (*qjs_hash_init)(njs_hash_t *ctx);
typedef void (*qjs_hash_update)(njs_hash_t *ctx, const void *data, size_t size);
typedef void (*qjs_hash_final)(u_char result[32], njs_hash_t *ctx);
typedef JSValue (*qjs_digest_encode)(JSContext *cx, const njs_str_t *src);
typedef struct {
njs_str_t name;
size_t size;
qjs_hash_init init;
qjs_hash_update update;
qjs_hash_final final;
} qjs_hash_alg_t;
typedef struct {
njs_hash_t ctx;
qjs_hash_alg_t *alg;
} qjs_digest_t;
typedef struct {
u_char opad[64];
njs_hash_t ctx;
qjs_hash_alg_t *alg;
} qjs_hmac_t;
typedef struct {
njs_str_t name;
qjs_digest_encode encode;
} qjs_crypto_enc_t;
static qjs_hash_alg_t *qjs_crypto_algorithm(JSContext *cx, JSValueConst val);
static qjs_crypto_enc_t *qjs_crypto_encoding(JSContext *cx, JSValueConst val);
static JSValue qjs_buffer_digest(JSContext *cx, const njs_str_t *src);
static JSValue qjs_crypto_create_hash(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue qjs_hash_prototype_update(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int hmac);
static JSValue qjs_hash_prototype_digest(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int hmac);
static JSValue qjs_hash_prototype_copy(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue qjs_crypto_create_hmac(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static void qjs_hash_finalizer(JSRuntime *rt, JSValue val);
static void qjs_hmac_finalizer(JSRuntime *rt, JSValue val);
static int qjs_crypto_module_init(JSContext *cx, JSModuleDef *m);
static JSModuleDef * qjs_crypto_init(JSContext *cx, const char *module_name);
static qjs_hash_alg_t qjs_hash_algorithms[] = {
{
njs_str("md5"),
16,
njs_md5_init,
njs_md5_update,
njs_md5_final
},
{
njs_str("sha1"),
20,
njs_sha1_init,
njs_sha1_update,
njs_sha1_final
},
{
njs_str("sha256"),
32,
njs_sha2_init,
njs_sha2_update,
njs_sha2_final
},
{
njs_null_str,
0,
NULL,
NULL,
NULL
}
};
static qjs_crypto_enc_t qjs_encodings[] = {
{
njs_str("buffer"),
qjs_buffer_digest
},
{
njs_str("hex"),
qjs_string_hex
},
{
njs_str("base64"),
qjs_string_base64
},
{
njs_str("base64url"),
qjs_string_base64url
},
{
njs_null_str,
NULL
}
};
static const JSCFunctionListEntry qjs_crypto_export[] = {
JS_CFUNC_DEF("createHash", 1, qjs_crypto_create_hash),
JS_CFUNC_DEF("createHmac", 2, qjs_crypto_create_hmac),
};
static const JSCFunctionListEntry qjs_hash_proto_proto[] = {
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Hash", JS_PROP_CONFIGURABLE),
JS_CFUNC_MAGIC_DEF("update", 2, qjs_hash_prototype_update, 0),
JS_CFUNC_MAGIC_DEF("digest", 1, qjs_hash_prototype_digest, 0),
JS_CFUNC_DEF("copy", 0, qjs_hash_prototype_copy),
JS_CFUNC_DEF("constructor", 1, qjs_crypto_create_hash),
};
static const JSCFunctionListEntry qjs_hmac_proto_proto[] = {
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Hmac", JS_PROP_CONFIGURABLE),
JS_CFUNC_MAGIC_DEF("update", 2, qjs_hash_prototype_update, 1),
JS_CFUNC_MAGIC_DEF("digest", 1, qjs_hash_prototype_digest, 1),
JS_CFUNC_DEF("constructor", 2, qjs_crypto_create_hmac),
};
static JSClassDef qjs_hash_class = {
"Hash",
.finalizer = qjs_hash_finalizer,
};
static JSClassDef qjs_hmac_class = {
"Hmac",
.finalizer = qjs_hmac_finalizer,
};
qjs_module_t qjs_crypto_module = {
.name = "crypto",
.init = qjs_crypto_init,
};
static JSValue
qjs_crypto_create_hash(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv)
{
JSValue obj;
qjs_digest_t *dgst;
qjs_hash_alg_t *alg;
alg = qjs_crypto_algorithm(cx, argv[0]);
if (alg == NULL) {
return JS_EXCEPTION;
}
dgst = js_malloc(cx, sizeof(qjs_digest_t));
if (dgst == NULL) {
return JS_ThrowOutOfMemory(cx);
}
dgst->alg = alg;
alg->init(&dgst->ctx);
obj = JS_NewObjectClass(cx, QJS_CORE_CLASS_CRYPTO_HASH);
if (JS_IsException(obj)) {
js_free(cx, dgst);
return obj;
}
JS_SetOpaque(obj, dgst);
return obj;
}
static JSValue
qjs_hash_prototype_update(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv, int hmac)
{
njs_str_t str, content;
njs_hash_t *uctx;
qjs_hmac_t *hctx;
qjs_bytes_t bytes;
qjs_digest_t *dgst;
const qjs_buffer_encoding_t *enc;
void (*update)(njs_hash_t *ctx, const void *data, size_t size);
if (!hmac) {
dgst = JS_GetOpaque2(cx, this_val, QJS_CORE_CLASS_CRYPTO_HASH);
if (dgst == NULL) {
return JS_ThrowTypeError(cx, "\"this\" is not a hash object");
}
if (dgst->alg == NULL) {
return JS_ThrowTypeError(cx, "Digest already called");
}
update = dgst->alg->update;
uctx = &dgst->ctx;
} else {
hctx = JS_GetOpaque2(cx, this_val, QJS_CORE_CLASS_CRYPTO_HMAC);
if (hctx == NULL) {
return JS_ThrowTypeError(cx, "\"this\" is not a hmac object");
}
if (hctx->alg == NULL) {
return JS_ThrowTypeError(cx, "Digest already called");
}
update = hctx->alg->update;
uctx = &hctx->ctx;
}
if (JS_IsString(argv[0])) {
enc = qjs_buffer_encoding(cx, argv[1], 1);
if (enc == NULL) {
return JS_EXCEPTION;
}
str.start = (u_char *) JS_ToCStringLen(cx, &str.length, argv[0]);
if (str.start == NULL) {
return JS_EXCEPTION;
}
if (enc->decode_length != NULL) {
content.length = enc->decode_length(cx, &str);
content.start = js_malloc(cx, content.length);
if (content.start == NULL) {
JS_FreeCString(cx, (const char *) str.start);
return JS_ThrowOutOfMemory(cx);
}
if (enc->decode(cx, &str, &content) != 0) {
JS_FreeCString(cx, (const char *) str.start);
JS_FreeCString(cx, (const char *) content.start);
return JS_EXCEPTION;
}
JS_FreeCString(cx, (const char *) str.start);
update(uctx, content.start, content.length);
js_free(cx, content.start);
} else {
update(uctx, str.start, str.length);
JS_FreeCString(cx, (const char *) str.start);
}
} else if (qjs_is_typed_array(cx, argv[0])) {
if (qjs_to_bytes(cx, &bytes, argv[0]) != 0) {
return JS_EXCEPTION;
}
update(uctx, bytes.start, bytes.length);
} else {
return JS_ThrowTypeError(cx,
"data is not a string or Buffer-like object");
}
return JS_DupValue(cx, this_val);
}
static JSValue
qjs_hash_prototype_digest(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv, int hmac)
{
njs_str_t str;
qjs_hmac_t *hctx;
qjs_digest_t *dgst;
qjs_hash_alg_t *alg;
qjs_crypto_enc_t *enc;
u_char hash1[32],digest[32];
if (!hmac) {
dgst = JS_GetOpaque2(cx, this_val, QJS_CORE_CLASS_CRYPTO_HASH);
if (dgst == NULL) {
return JS_ThrowTypeError(cx, "\"this\" is not a hash object");
}
alg = dgst->alg;
if (alg == NULL) {
return JS_ThrowTypeError(cx, "Digest already called");
}
dgst->alg = NULL;
alg->final(digest, &dgst->ctx);
} else {
hctx = JS_GetOpaque2(cx, this_val, QJS_CORE_CLASS_CRYPTO_HMAC);
if (hctx == NULL) {
return JS_ThrowTypeError(cx, "\"this\" is not a hmac object");
}
alg = hctx->alg;
if (alg == NULL) {
return JS_ThrowTypeError(cx, "Digest already called");
}
hctx->alg = NULL;
alg->final(hash1, &hctx->ctx);
alg->init(&hctx->ctx);
alg->update(&hctx->ctx, hctx->opad, 64);
alg->update(&hctx->ctx, hash1, alg->size);
alg->final(digest, &hctx->ctx);
}
str.start = digest;
str.length = alg->size;
if (argc == 0) {
return qjs_buffer_digest(cx, &str);
}
enc = qjs_crypto_encoding(cx, argv[0]);
if (enc == NULL) {
return JS_EXCEPTION;
}
return enc->encode(cx, &str);
}
static JSValue
qjs_hash_prototype_copy(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv)
{
JSValue obj;
qjs_digest_t *dgst, *copy;
dgst = JS_GetOpaque2(cx, this_val, QJS_CORE_CLASS_CRYPTO_HASH);
if (dgst == NULL) {
return JS_EXCEPTION;
}
if (dgst->alg == NULL) {
return JS_ThrowTypeError(cx, "Digest already called");
}
copy = js_malloc(cx, sizeof(qjs_digest_t));
if (copy == NULL) {
return JS_ThrowOutOfMemory(cx);
}
memcpy(copy, dgst, sizeof(qjs_digest_t));
obj = JS_NewObjectClass(cx, QJS_CORE_CLASS_CRYPTO_HASH);
if (JS_IsException(obj)) {
js_free(cx, copy);
return obj;
}
JS_SetOpaque(obj, copy);
return obj;
}
static JSValue
qjs_crypto_create_hmac(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv)
{
int i;
JS_BOOL key_is_string;
JSValue obj;
njs_str_t key;
qjs_hmac_t *hmac;
qjs_bytes_t bytes;
qjs_hash_alg_t *alg;
u_char digest[32], key_buf[64];
alg = qjs_crypto_algorithm(cx, argv[0]);
if (alg == NULL) {
return JS_EXCEPTION;
}
key_is_string = JS_IsString(argv[1]);
if (key_is_string) {
key.start = (u_char *) JS_ToCStringLen(cx, &key.length, argv[1]);
if (key.start == NULL) {
return JS_EXCEPTION;
}
} else if (qjs_is_typed_array(cx, argv[1])) {
if (qjs_to_bytes(cx, &bytes, argv[1]) != 0) {
return JS_EXCEPTION;
}
key.start = bytes.start;
key.length = bytes.length;
} else {
return JS_ThrowTypeError(cx,
"key is not a string or Buffer-like object");
}
hmac = js_malloc(cx, sizeof(qjs_hmac_t));
if (hmac == NULL) {
if (key_is_string) {
JS_FreeCString(cx, (const char *) key.start);
}
return JS_ThrowOutOfMemory(cx);
}
hmac->alg = alg;
if (key.length > sizeof(key_buf)) {
alg->init(&hmac->ctx);
alg->update(&hmac->ctx, key.start, key.length);
alg->final(digest, &hmac->ctx);
memcpy(key_buf, digest, alg->size);
memset(key_buf + alg->size, 0, sizeof(key_buf) - alg->size);
} else {
memcpy(key_buf, key.start, key.length);
memset(key_buf + key.length, 0, sizeof(key_buf) - key.length);
}
if (key_is_string) {
JS_FreeCString(cx, (const char *) key.start);
}
for (i = 0; i < 64; i++) {
hmac->opad[i] = key_buf[i] ^ 0x5c;
}
for (i = 0; i < 64; i++) {
key_buf[i] ^= 0x36;
}
alg->init(&hmac->ctx);
alg->update(&hmac->ctx, key_buf, 64);
obj = JS_NewObjectClass(cx, QJS_CORE_CLASS_CRYPTO_HMAC);
if (JS_IsException(obj)) {
js_free(cx, hmac);
return obj;
}
JS_SetOpaque(obj, hmac);
return obj;
}
static void
qjs_hash_finalizer(JSRuntime *rt, JSValue val)
{
qjs_digest_t *dgst;
dgst = JS_GetOpaque(val, QJS_CORE_CLASS_CRYPTO_HASH);
if (dgst != NULL) {
js_free_rt(rt, dgst);
}
}
static void
qjs_hmac_finalizer(JSRuntime *rt, JSValue val)
{
qjs_hmac_t *hmac;
hmac = JS_GetOpaque(val, QJS_CORE_CLASS_CRYPTO_HMAC);
if (hmac != NULL) {
js_free_rt(rt, hmac);
}
}
static qjs_hash_alg_t *
qjs_crypto_algorithm(JSContext *cx, JSValueConst val)
{
njs_str_t name;
qjs_hash_alg_t *a, *alg;
name.start = (u_char *) JS_ToCStringLen(cx, &name.length, val);
if (name.start == NULL) {
JS_ThrowTypeError(cx, "algorithm must be a string");
return NULL;
}
alg = NULL;
for (a = &qjs_hash_algorithms[0]; a->name.start != NULL; a++) {
if (njs_strstr_eq(&name, &a->name)) {
alg = a;
break;
}
}
JS_FreeCString(cx, (const char *) name.start);
if (alg == NULL) {
JS_ThrowTypeError(cx, "not supported algorithm");
}
return alg;
}
static qjs_crypto_enc_t *
qjs_crypto_encoding(JSContext *cx, JSValueConst val)
{
njs_str_t name;
qjs_crypto_enc_t *e, *enc;
if (JS_IsNullOrUndefined(val)) {
return &qjs_encodings[0];
}
name.start = (u_char *) JS_ToCStringLen(cx, &name.length, val);
if (name.start == NULL) {
return NULL;
}
enc = NULL;
for (e = &qjs_encodings[1]; e->name.start != NULL; e++) {
if (njs_strstr_eq(&name, &e->name)) {
enc = e;
break;
}
}
JS_FreeCString(cx, (const char *) name.start);
if (enc == NULL) {
JS_ThrowTypeError(cx, "Unknown digest encoding");
}
return enc;
}
static JSValue
qjs_buffer_digest(JSContext *cx, const njs_str_t *src)
{
return qjs_buffer_create(cx, src->start, src->length);
}
static int
qjs_crypto_module_init(JSContext *cx, JSModuleDef *m)
{
JSValue crypto_obj;
crypto_obj = JS_NewObject(cx);
if (JS_IsException(crypto_obj)) {
return -1;
}
JS_SetPropertyFunctionList(cx, crypto_obj, qjs_crypto_export,
njs_nitems(qjs_crypto_export));
if (JS_SetModuleExport(cx, m, "default", crypto_obj) != 0) {
return -1;
}
return JS_SetModuleExportList(cx, m, qjs_crypto_export,
njs_nitems(qjs_crypto_export));
}
static JSModuleDef *
qjs_crypto_init(JSContext *cx, const char *module_name)
{
JSValue proto;
JSModuleDef *m;
if (!JS_IsRegisteredClass(JS_GetRuntime(cx),
QJS_CORE_CLASS_CRYPTO_HASH))
{
if (JS_NewClass(JS_GetRuntime(cx), QJS_CORE_CLASS_CRYPTO_HASH,
&qjs_hash_class) < 0)
{
return NULL;
}
proto = JS_NewObject(cx);
if (JS_IsException(proto)) {
return NULL;
}
JS_SetPropertyFunctionList(cx, proto, qjs_hash_proto_proto,
njs_nitems(qjs_hash_proto_proto));
JS_SetClassProto(cx, QJS_CORE_CLASS_CRYPTO_HASH, proto);
if (JS_NewClass(JS_GetRuntime(cx), QJS_CORE_CLASS_CRYPTO_HMAC,
&qjs_hmac_class) < 0)
{
return NULL;
}
proto = JS_NewObject(cx);
if (JS_IsException(proto)) {
return NULL;
}
JS_SetPropertyFunctionList(cx, proto, qjs_hmac_proto_proto,
njs_nitems(qjs_hmac_proto_proto));
JS_SetClassProto(cx, QJS_CORE_CLASS_CRYPTO_HMAC, proto);
}
m = JS_NewCModule(cx, module_name, qjs_crypto_module_init);
if (m == NULL) {
return NULL;
}
if (JS_AddModuleExport(cx, m, "default") < 0) {
return NULL;
}
if (JS_AddModuleExportList(cx, m, qjs_crypto_export,
njs_nitems(qjs_crypto_export)) != 0)
{
return NULL;
}
return m;
}
|