/src/njs/external/njs_crypto_module.c
Line | Count | Source |
1 | | |
2 | | /* |
3 | | * Copyright (C) Dmitry Volyntsev |
4 | | * Copyright (C) NGINX, Inc. |
5 | | */ |
6 | | |
7 | | |
8 | | #include <njs.h> |
9 | | #include <njs_string.h> |
10 | | #include <njs_buffer.h> |
11 | | #include "njs_openssl.h" |
12 | | |
13 | | |
14 | | typedef njs_int_t (*njs_digest_encode)(njs_vm_t *vm, njs_value_t *value, |
15 | | const njs_str_t *src); |
16 | | |
17 | | |
18 | | typedef struct { |
19 | | EVP_MD_CTX *ctx; |
20 | | } njs_digest_t; |
21 | | |
22 | | typedef struct { |
23 | | HMAC_CTX *ctx; |
24 | | } njs_hmac_t; |
25 | | |
26 | | |
27 | | typedef struct { |
28 | | njs_str_t name; |
29 | | njs_digest_encode encode; |
30 | | } njs_crypto_enc_t; |
31 | | |
32 | | |
33 | | static const EVP_MD *njs_crypto_algorithm(njs_vm_t *vm, njs_value_t *value); |
34 | | static njs_crypto_enc_t *njs_crypto_encoding(njs_vm_t *vm, njs_value_t *value); |
35 | | static njs_int_t njs_buffer_digest(njs_vm_t *vm, njs_value_t *value, |
36 | | const njs_str_t *src); |
37 | | static njs_int_t njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, |
38 | | njs_uint_t nargs, njs_index_t unused, njs_value_t *retval); |
39 | | static njs_int_t njs_hash_prototype_update(njs_vm_t *vm, njs_value_t *args, |
40 | | njs_uint_t nargs, njs_index_t hmac, njs_value_t *retval); |
41 | | static njs_int_t njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args, |
42 | | njs_uint_t nargs, njs_index_t hmac, njs_value_t *retval); |
43 | | static njs_int_t njs_hash_prototype_copy(njs_vm_t *vm, njs_value_t *args, |
44 | | njs_uint_t nargs, njs_index_t hmac, njs_value_t *retval); |
45 | | static njs_int_t njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, |
46 | | njs_uint_t nargs, njs_index_t unused, njs_value_t *retval); |
47 | | |
48 | | static void njs_crypto_cleanup_digest(void *data); |
49 | | static void njs_crypto_cleanup_hmac(void *data); |
50 | | |
51 | | static njs_int_t njs_crypto_init(njs_vm_t *vm); |
52 | | |
53 | | |
54 | | static njs_crypto_enc_t njs_encodings[] = { |
55 | | |
56 | | { |
57 | | njs_str("buffer"), |
58 | | njs_buffer_digest |
59 | | }, |
60 | | |
61 | | { |
62 | | njs_str("hex"), |
63 | | njs_string_hex |
64 | | }, |
65 | | |
66 | | { |
67 | | njs_str("base64"), |
68 | | njs_string_base64 |
69 | | }, |
70 | | |
71 | | { |
72 | | njs_str("base64url"), |
73 | | njs_string_base64url |
74 | | }, |
75 | | |
76 | | { |
77 | | njs_null_str, |
78 | | NULL |
79 | | } |
80 | | }; |
81 | | |
82 | | |
83 | | static njs_external_t njs_ext_crypto_hash[] = { |
84 | | |
85 | | { |
86 | | .flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL, |
87 | | .name.symbol = NJS_SYMBOL_TO_STRING_TAG, |
88 | | .u.property = { |
89 | | .value = "Hash", |
90 | | } |
91 | | }, |
92 | | |
93 | | { |
94 | | .flags = NJS_EXTERN_METHOD, |
95 | | .name.string = njs_str("update"), |
96 | | .writable = 1, |
97 | | .configurable = 1, |
98 | | .u.method = { |
99 | | .native = njs_hash_prototype_update, |
100 | | .magic8 = 0, |
101 | | } |
102 | | }, |
103 | | |
104 | | { |
105 | | .flags = NJS_EXTERN_METHOD, |
106 | | .name.string = njs_str("digest"), |
107 | | .writable = 1, |
108 | | .configurable = 1, |
109 | | .u.method = { |
110 | | .native = njs_hash_prototype_digest, |
111 | | .magic8 = 0, |
112 | | } |
113 | | }, |
114 | | |
115 | | { |
116 | | .flags = NJS_EXTERN_METHOD, |
117 | | .name.string = njs_str("copy"), |
118 | | .writable = 1, |
119 | | .configurable = 1, |
120 | | .u.method = { |
121 | | .native = njs_hash_prototype_copy, |
122 | | } |
123 | | }, |
124 | | |
125 | | { |
126 | | .flags = NJS_EXTERN_METHOD, |
127 | | .name.string = njs_str("constructor"), |
128 | | .writable = 1, |
129 | | .configurable = 1, |
130 | | .u.method = { |
131 | | .native = njs_crypto_create_hash, |
132 | | } |
133 | | }, |
134 | | }; |
135 | | |
136 | | |
137 | | static njs_external_t njs_ext_crypto_hmac[] = { |
138 | | |
139 | | { |
140 | | .flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL, |
141 | | .name.symbol = NJS_SYMBOL_TO_STRING_TAG, |
142 | | .u.property = { |
143 | | .value = "Hmac", |
144 | | } |
145 | | }, |
146 | | |
147 | | { |
148 | | .flags = NJS_EXTERN_METHOD, |
149 | | .name.string = njs_str("update"), |
150 | | .writable = 1, |
151 | | .configurable = 1, |
152 | | .u.method = { |
153 | | .native = njs_hash_prototype_update, |
154 | | .magic8 = 1, |
155 | | } |
156 | | }, |
157 | | |
158 | | { |
159 | | .flags = NJS_EXTERN_METHOD, |
160 | | .name.string = njs_str("digest"), |
161 | | .writable = 1, |
162 | | .configurable = 1, |
163 | | .u.method = { |
164 | | .native = njs_hash_prototype_digest, |
165 | | .magic8 = 1, |
166 | | } |
167 | | }, |
168 | | |
169 | | { |
170 | | .flags = NJS_EXTERN_METHOD, |
171 | | .name.string = njs_str("constructor"), |
172 | | .writable = 1, |
173 | | .configurable = 1, |
174 | | .u.method = { |
175 | | .native = njs_crypto_create_hmac, |
176 | | .magic8 = 0, |
177 | | } |
178 | | }, |
179 | | }; |
180 | | |
181 | | |
182 | | static njs_external_t njs_ext_crypto_crypto[] = { |
183 | | |
184 | | { |
185 | | .flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL, |
186 | | .name.symbol = NJS_SYMBOL_TO_STRING_TAG, |
187 | | .u.property = { |
188 | | .value = "crypto", |
189 | | } |
190 | | }, |
191 | | |
192 | | { |
193 | | .flags = NJS_EXTERN_METHOD, |
194 | | .name.string = njs_str("createHash"), |
195 | | .writable = 1, |
196 | | .configurable = 1, |
197 | | .enumerable = 1, |
198 | | .u.method = { |
199 | | .native = njs_crypto_create_hash, |
200 | | .magic8 = 0, |
201 | | } |
202 | | }, |
203 | | |
204 | | { |
205 | | .flags = NJS_EXTERN_METHOD, |
206 | | .name.string = njs_str("createHmac"), |
207 | | .writable = 1, |
208 | | .configurable = 1, |
209 | | .enumerable = 1, |
210 | | .u.method = { |
211 | | .native = njs_crypto_create_hmac, |
212 | | .magic8 = 0, |
213 | | } |
214 | | }, |
215 | | }; |
216 | | |
217 | | |
218 | | static njs_int_t njs_crypto_hash_proto_id; |
219 | | static njs_int_t njs_crypto_hmac_proto_id; |
220 | | |
221 | | |
222 | | njs_module_t njs_crypto_module = { |
223 | | .name = njs_str("crypto"), |
224 | | .preinit = NULL, |
225 | | .init = njs_crypto_init, |
226 | | }; |
227 | | |
228 | | |
229 | | static njs_int_t |
230 | | njs_crypto_create_hash(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, |
231 | | njs_index_t unused, njs_value_t *retval) |
232 | 0 | { |
233 | 0 | njs_digest_t *dgst; |
234 | 0 | const EVP_MD *md; |
235 | 0 | njs_mp_cleanup_t *cln; |
236 | |
|
237 | 0 | md = njs_crypto_algorithm(vm, njs_arg(args, nargs, 1)); |
238 | 0 | if (njs_slow_path(md == NULL)) { |
239 | 0 | return NJS_ERROR; |
240 | 0 | } |
241 | | |
242 | 0 | dgst = njs_mp_alloc(njs_vm_memory_pool(vm), sizeof(njs_digest_t)); |
243 | 0 | if (njs_slow_path(dgst == NULL)) { |
244 | 0 | njs_vm_memory_error(vm); |
245 | 0 | return NJS_ERROR; |
246 | 0 | } |
247 | | |
248 | 0 | dgst->ctx = njs_evp_md_ctx_new(); |
249 | 0 | if (njs_slow_path(dgst->ctx == NULL)) { |
250 | 0 | njs_vm_memory_error(vm); |
251 | 0 | return NJS_ERROR; |
252 | 0 | } |
253 | | |
254 | 0 | cln = njs_mp_cleanup_add(njs_vm_memory_pool(vm), 0); |
255 | 0 | if (njs_slow_path(cln == NULL)) { |
256 | 0 | njs_evp_md_ctx_free(dgst->ctx); |
257 | 0 | njs_vm_memory_error(vm); |
258 | 0 | return NJS_ERROR; |
259 | 0 | } |
260 | | |
261 | 0 | cln->handler = njs_crypto_cleanup_digest; |
262 | 0 | cln->data = dgst; |
263 | |
|
264 | 0 | if (EVP_DigestInit_ex(dgst->ctx, md, NULL) <= 0) { |
265 | 0 | njs_vm_error(vm, "EVP_DigestInit_ex() failed"); |
266 | 0 | return NJS_ERROR; |
267 | 0 | } |
268 | | |
269 | 0 | return njs_vm_external_create(vm, retval, njs_crypto_hash_proto_id, |
270 | 0 | dgst, 0); |
271 | 0 | } |
272 | | |
273 | | |
274 | | static njs_int_t |
275 | | njs_hash_prototype_update(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, |
276 | | njs_index_t hmac, njs_value_t *retval) |
277 | 0 | { |
278 | 0 | njs_str_t data; |
279 | 0 | njs_int_t ret; |
280 | 0 | njs_hmac_t *ctx; |
281 | 0 | njs_value_t *this, *value; |
282 | 0 | njs_digest_t *dgst; |
283 | 0 | njs_opaque_value_t result; |
284 | 0 | const njs_buffer_encoding_t *enc; |
285 | |
|
286 | 0 | this = njs_argument(args, 0); |
287 | |
|
288 | 0 | if (!hmac) { |
289 | 0 | dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, this); |
290 | 0 | if (njs_slow_path(dgst == NULL)) { |
291 | 0 | njs_vm_type_error(vm, "\"this\" is not a hash object"); |
292 | 0 | return NJS_ERROR; |
293 | 0 | } |
294 | | |
295 | 0 | if (njs_slow_path(dgst->ctx == NULL)) { |
296 | 0 | njs_vm_error(vm, "Digest already called"); |
297 | 0 | return NJS_ERROR; |
298 | 0 | } |
299 | | |
300 | 0 | ctx = NULL; |
301 | |
|
302 | 0 | } else { |
303 | 0 | ctx = njs_vm_external(vm, njs_crypto_hmac_proto_id, this); |
304 | 0 | if (njs_slow_path(ctx == NULL)) { |
305 | 0 | njs_vm_type_error(vm, "\"this\" is not a hmac object"); |
306 | 0 | return NJS_ERROR; |
307 | 0 | } |
308 | | |
309 | 0 | if (njs_slow_path(ctx->ctx == NULL)) { |
310 | 0 | njs_vm_error(vm, "Digest already called"); |
311 | 0 | return NJS_ERROR; |
312 | 0 | } |
313 | | |
314 | 0 | dgst = NULL; |
315 | 0 | } |
316 | | |
317 | 0 | value = njs_arg(args, nargs, 1); |
318 | |
|
319 | 0 | if (njs_value_is_string(value)) { |
320 | 0 | enc = njs_buffer_encoding(vm, njs_arg(args, nargs, 2), 1); |
321 | 0 | if (njs_slow_path(enc == NULL)) { |
322 | 0 | return NJS_ERROR; |
323 | 0 | } |
324 | | |
325 | 0 | ret = njs_buffer_decode_string(vm, value, njs_value_arg(&result), enc); |
326 | 0 | if (njs_slow_path(ret != NJS_OK)) { |
327 | 0 | return NJS_ERROR; |
328 | 0 | } |
329 | | |
330 | 0 | njs_value_string_get(vm, njs_value_arg(&result), &data); |
331 | |
|
332 | 0 | } else if (njs_value_is_buffer(value)) { |
333 | 0 | ret = njs_value_buffer_get(vm, value, &data); |
334 | 0 | if (njs_slow_path(ret != NJS_OK)) { |
335 | 0 | return NJS_ERROR; |
336 | 0 | } |
337 | |
|
338 | 0 | } else { |
339 | 0 | njs_vm_type_error(vm, "data is not a string or Buffer-like object"); |
340 | |
|
341 | 0 | return NJS_ERROR; |
342 | 0 | } |
343 | | |
344 | 0 | if (!hmac) { |
345 | 0 | if (EVP_DigestUpdate(dgst->ctx, data.start, data.length) <= 0) { |
346 | 0 | njs_vm_error(vm, "EVP_DigestUpdate() failed"); |
347 | 0 | return NJS_ERROR; |
348 | 0 | } |
349 | |
|
350 | 0 | } else { |
351 | 0 | if (HMAC_Update(ctx->ctx, data.start, data.length) <= 0) { |
352 | 0 | njs_vm_error(vm, "HMAC_Update() failed"); |
353 | 0 | return NJS_ERROR; |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | 0 | njs_value_assign(retval, this); |
358 | |
|
359 | 0 | return NJS_OK; |
360 | 0 | } |
361 | | |
362 | | |
363 | | static njs_int_t |
364 | | njs_hash_prototype_digest(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, |
365 | | njs_index_t hmac, njs_value_t *retval) |
366 | 0 | { |
367 | 0 | njs_str_t str; |
368 | 0 | njs_hmac_t *ctx; |
369 | 0 | njs_value_t *this; |
370 | 0 | njs_digest_t *dgst; |
371 | 0 | unsigned int len; |
372 | 0 | njs_crypto_enc_t *enc; |
373 | 0 | u_char digest[EVP_MAX_MD_SIZE]; |
374 | |
|
375 | 0 | this = njs_argument(args, 0); |
376 | |
|
377 | 0 | if (!hmac) { |
378 | 0 | dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, this); |
379 | 0 | if (njs_slow_path(dgst == NULL)) { |
380 | 0 | njs_vm_type_error(vm, "\"this\" is not a hash object"); |
381 | 0 | return NJS_ERROR; |
382 | 0 | } |
383 | | |
384 | 0 | if (njs_slow_path(dgst->ctx == NULL)) { |
385 | 0 | goto exception; |
386 | 0 | } |
387 | | |
388 | 0 | ctx = NULL; |
389 | |
|
390 | 0 | } else { |
391 | 0 | ctx = njs_vm_external(vm, njs_crypto_hmac_proto_id, this); |
392 | 0 | if (njs_slow_path(ctx == NULL)) { |
393 | 0 | njs_vm_type_error(vm, "\"this\" is not a hmac object"); |
394 | 0 | return NJS_ERROR; |
395 | 0 | } |
396 | | |
397 | 0 | if (njs_slow_path(ctx->ctx == NULL)) { |
398 | 0 | goto exception; |
399 | 0 | } |
400 | | |
401 | 0 | dgst = NULL; |
402 | 0 | } |
403 | | |
404 | 0 | enc = njs_crypto_encoding(vm, njs_arg(args, nargs, 1)); |
405 | 0 | if (njs_slow_path(enc == NULL)) { |
406 | 0 | return NJS_ERROR; |
407 | 0 | } |
408 | | |
409 | 0 | if (!hmac) { |
410 | 0 | if (EVP_DigestFinal_ex(dgst->ctx, digest, &len) <= 0) { |
411 | 0 | njs_vm_error(vm, "EVP_DigestFinal_ex() failed"); |
412 | 0 | return NJS_ERROR; |
413 | 0 | } |
414 | | |
415 | 0 | njs_evp_md_ctx_free(dgst->ctx); |
416 | 0 | dgst->ctx = NULL; |
417 | |
|
418 | 0 | } else { |
419 | 0 | if (HMAC_Final(ctx->ctx, digest, &len) <= 0) { |
420 | 0 | njs_vm_error(vm, "HMAC_Final() failed"); |
421 | 0 | return NJS_ERROR; |
422 | 0 | } |
423 | | |
424 | 0 | njs_hmac_ctx_free(ctx->ctx); |
425 | 0 | ctx->ctx = NULL; |
426 | 0 | } |
427 | | |
428 | 0 | str.start = digest; |
429 | 0 | str.length = len; |
430 | |
|
431 | 0 | return enc->encode(vm, retval, &str); |
432 | | |
433 | 0 | exception: |
434 | |
|
435 | 0 | njs_vm_error(vm, "Digest already called"); |
436 | |
|
437 | 0 | return NJS_ERROR; |
438 | 0 | } |
439 | | |
440 | | |
441 | | static njs_int_t |
442 | | njs_hash_prototype_copy(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, |
443 | | njs_index_t unused, njs_value_t *retval) |
444 | 0 | { |
445 | 0 | njs_digest_t *dgst, *copy; |
446 | 0 | njs_mp_cleanup_t *cln; |
447 | |
|
448 | 0 | dgst = njs_vm_external(vm, njs_crypto_hash_proto_id, njs_argument(args, 0)); |
449 | 0 | if (njs_slow_path(dgst == NULL)) { |
450 | 0 | njs_vm_type_error(vm, "\"this\" is not a hash object"); |
451 | 0 | return NJS_ERROR; |
452 | 0 | } |
453 | | |
454 | 0 | if (njs_slow_path(dgst->ctx == NULL)) { |
455 | 0 | njs_vm_error(vm, "Digest already called"); |
456 | 0 | return NJS_ERROR; |
457 | 0 | } |
458 | | |
459 | 0 | copy = njs_mp_alloc(njs_vm_memory_pool(vm), sizeof(njs_digest_t)); |
460 | 0 | if (njs_slow_path(copy == NULL)) { |
461 | 0 | njs_vm_memory_error(vm); |
462 | 0 | return NJS_ERROR; |
463 | 0 | } |
464 | | |
465 | 0 | copy->ctx = njs_evp_md_ctx_new(); |
466 | 0 | if (njs_slow_path(copy->ctx == NULL)) { |
467 | 0 | njs_vm_memory_error(vm); |
468 | 0 | return NJS_ERROR; |
469 | 0 | } |
470 | | |
471 | 0 | cln = njs_mp_cleanup_add(njs_vm_memory_pool(vm), 0); |
472 | 0 | if (njs_slow_path(cln == NULL)) { |
473 | 0 | njs_evp_md_ctx_free(copy->ctx); |
474 | 0 | njs_vm_memory_error(vm); |
475 | 0 | return NJS_ERROR; |
476 | 0 | } |
477 | | |
478 | 0 | cln->handler = njs_crypto_cleanup_digest; |
479 | 0 | cln->data = copy; |
480 | |
|
481 | 0 | if (EVP_MD_CTX_copy_ex(copy->ctx, dgst->ctx) <= 0) { |
482 | 0 | njs_vm_error(vm, "EVP_MD_CTX_copy_ex() failed"); |
483 | 0 | return NJS_ERROR; |
484 | 0 | } |
485 | | |
486 | 0 | return njs_vm_external_create(vm, retval, |
487 | 0 | njs_crypto_hash_proto_id, copy, 0); |
488 | 0 | } |
489 | | |
490 | | |
491 | | static njs_int_t |
492 | | njs_crypto_create_hmac(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, |
493 | | njs_index_t unused, njs_value_t *retval) |
494 | 0 | { |
495 | 0 | njs_int_t ret; |
496 | 0 | njs_str_t key; |
497 | 0 | njs_hmac_t *ctx; |
498 | 0 | njs_value_t *value; |
499 | 0 | const EVP_MD *md; |
500 | 0 | njs_mp_cleanup_t *cln; |
501 | 0 | njs_opaque_value_t result; |
502 | 0 | const njs_buffer_encoding_t *enc; |
503 | |
|
504 | 0 | md = njs_crypto_algorithm(vm, njs_arg(args, nargs, 1)); |
505 | 0 | if (njs_slow_path(md == NULL)) { |
506 | 0 | return NJS_ERROR; |
507 | 0 | } |
508 | | |
509 | 0 | value = njs_arg(args, nargs, 2); |
510 | |
|
511 | 0 | if (njs_value_is_string(value)) { |
512 | 0 | enc = njs_buffer_encoding(vm, njs_value_arg(&njs_value_undefined), 1); |
513 | 0 | if (njs_slow_path(enc == NULL)) { |
514 | 0 | return NJS_ERROR; |
515 | 0 | } |
516 | | |
517 | 0 | ret = njs_buffer_decode_string(vm, value, njs_value_arg(&result), enc); |
518 | 0 | if (njs_slow_path(ret != NJS_OK)) { |
519 | 0 | return NJS_ERROR; |
520 | 0 | } |
521 | | |
522 | 0 | njs_value_string_get(vm, njs_value_arg(&result), &key); |
523 | |
|
524 | 0 | } else if (njs_value_is_buffer(value)) { |
525 | 0 | ret = njs_value_buffer_get(vm, value, &key); |
526 | 0 | if (njs_slow_path(ret != NJS_OK)) { |
527 | 0 | return NJS_ERROR; |
528 | 0 | } |
529 | |
|
530 | 0 | } else { |
531 | 0 | njs_vm_type_error(vm, "key is not a string or Buffer-like object"); |
532 | |
|
533 | 0 | return NJS_ERROR; |
534 | 0 | } |
535 | | |
536 | 0 | ctx = njs_mp_alloc(njs_vm_memory_pool(vm), sizeof(njs_hmac_t)); |
537 | 0 | if (njs_slow_path(ctx == NULL)) { |
538 | 0 | njs_vm_memory_error(vm); |
539 | 0 | return NJS_ERROR; |
540 | 0 | } |
541 | | |
542 | 0 | ctx->ctx = njs_hmac_ctx_new(); |
543 | 0 | if (njs_slow_path(ctx->ctx == NULL)) { |
544 | 0 | njs_vm_memory_error(vm); |
545 | 0 | return NJS_ERROR; |
546 | 0 | } |
547 | | |
548 | 0 | cln = njs_mp_cleanup_add(njs_vm_memory_pool(vm), 0); |
549 | 0 | if (njs_slow_path(cln == NULL)) { |
550 | 0 | njs_hmac_ctx_free(ctx->ctx); |
551 | 0 | njs_vm_memory_error(vm); |
552 | 0 | return NJS_ERROR; |
553 | 0 | } |
554 | | |
555 | 0 | cln->handler = njs_crypto_cleanup_hmac; |
556 | 0 | cln->data = ctx; |
557 | |
|
558 | 0 | if (HMAC_Init_ex(ctx->ctx, key.start, (int) key.length, md, NULL) <= 0) { |
559 | 0 | njs_vm_error(vm, "HMAC_Init_ex() failed"); |
560 | 0 | return NJS_ERROR; |
561 | 0 | } |
562 | | |
563 | 0 | return njs_vm_external_create(vm, retval, njs_crypto_hmac_proto_id, |
564 | 0 | ctx, 0); |
565 | 0 | } |
566 | | |
567 | | |
568 | | static const EVP_MD * |
569 | | njs_crypto_algorithm(njs_vm_t *vm, njs_value_t *value) |
570 | 0 | { |
571 | 0 | njs_str_t name; |
572 | 0 | const EVP_MD *md; |
573 | |
|
574 | 0 | if (njs_slow_path(!njs_value_is_string(value))) { |
575 | 0 | njs_vm_type_error(vm, "algorithm must be a string"); |
576 | 0 | return NULL; |
577 | 0 | } |
578 | | |
579 | 0 | njs_value_string_get(vm, value, &name); |
580 | |
|
581 | 0 | if (njs_slow_path(njs_strlen(name.start) != name.length)) { |
582 | 0 | njs_vm_type_error(vm, "not supported algorithm: \"%V\"", &name); |
583 | 0 | return NULL; |
584 | 0 | } |
585 | | |
586 | 0 | md = EVP_get_digestbyname((const char *) name.start); |
587 | 0 | if (njs_slow_path(md == NULL)) { |
588 | 0 | njs_vm_type_error(vm, "not supported algorithm: \"%V\"", &name); |
589 | 0 | return NULL; |
590 | 0 | } |
591 | | |
592 | 0 | return md; |
593 | 0 | } |
594 | | |
595 | | |
596 | | static njs_crypto_enc_t * |
597 | | njs_crypto_encoding(njs_vm_t *vm, njs_value_t *value) |
598 | 0 | { |
599 | 0 | njs_str_t name; |
600 | 0 | njs_crypto_enc_t *e; |
601 | |
|
602 | 0 | if (njs_slow_path(!njs_value_is_string(value))) { |
603 | 0 | if (!njs_value_is_undefined(value)) { |
604 | 0 | njs_vm_type_error(vm, "encoding must be a string"); |
605 | 0 | return NULL; |
606 | 0 | } |
607 | | |
608 | 0 | return &njs_encodings[0]; |
609 | 0 | } |
610 | | |
611 | 0 | njs_value_string_get(vm, value, &name); |
612 | |
|
613 | 0 | for (e = &njs_encodings[1]; e->name.length != 0; e++) { |
614 | 0 | if (njs_strstr_eq(&name, &e->name)) { |
615 | 0 | return e; |
616 | 0 | } |
617 | 0 | } |
618 | | |
619 | 0 | njs_vm_type_error(vm, "Unknown digest encoding: \"%V\"", &name); |
620 | |
|
621 | 0 | return NULL; |
622 | 0 | } |
623 | | |
624 | | |
625 | | static njs_int_t |
626 | | njs_buffer_digest(njs_vm_t *vm, njs_value_t *value, const njs_str_t *src) |
627 | 0 | { |
628 | 0 | return njs_buffer_new(vm, value, src->start, src->length); |
629 | 0 | } |
630 | | |
631 | | |
632 | | static void |
633 | | njs_crypto_cleanup_digest(void *data) |
634 | 0 | { |
635 | 0 | njs_digest_t *dgst = data; |
636 | |
|
637 | 0 | if (dgst->ctx != NULL) { |
638 | 0 | njs_evp_md_ctx_free(dgst->ctx); |
639 | 0 | dgst->ctx = NULL; |
640 | 0 | } |
641 | 0 | } |
642 | | |
643 | | |
644 | | static void |
645 | | njs_crypto_cleanup_hmac(void *data) |
646 | 0 | { |
647 | 0 | njs_hmac_t *ctx = data; |
648 | |
|
649 | 0 | if (ctx->ctx != NULL) { |
650 | 0 | njs_hmac_ctx_free(ctx->ctx); |
651 | 0 | ctx->ctx = NULL; |
652 | 0 | } |
653 | 0 | } |
654 | | |
655 | | |
656 | | static njs_int_t |
657 | | njs_crypto_init(njs_vm_t *vm) |
658 | 8.43k | { |
659 | 8.43k | njs_int_t ret, proto_id; |
660 | 8.43k | njs_mod_t *module; |
661 | 8.43k | njs_opaque_value_t value; |
662 | | |
663 | 8.43k | njs_crypto_hash_proto_id = |
664 | 8.43k | njs_vm_external_prototype(vm, njs_ext_crypto_hash, |
665 | 8.43k | njs_nitems(njs_ext_crypto_hash)); |
666 | 8.43k | if (njs_slow_path(njs_crypto_hash_proto_id < 0)) { |
667 | 0 | return NJS_ERROR; |
668 | 0 | } |
669 | | |
670 | 8.43k | njs_crypto_hmac_proto_id = |
671 | 8.43k | njs_vm_external_prototype(vm, njs_ext_crypto_hmac, |
672 | 8.43k | njs_nitems(njs_ext_crypto_hmac)); |
673 | 8.43k | if (njs_slow_path(njs_crypto_hmac_proto_id < 0)) { |
674 | 0 | return NJS_ERROR; |
675 | 0 | } |
676 | | |
677 | 8.43k | proto_id = njs_vm_external_prototype(vm, njs_ext_crypto_crypto, |
678 | 8.43k | njs_nitems(njs_ext_crypto_crypto)); |
679 | 8.43k | if (njs_slow_path(proto_id < 0)) { |
680 | 0 | return NJS_ERROR; |
681 | 0 | } |
682 | | |
683 | 8.43k | ret = njs_vm_external_create(vm, njs_value_arg(&value), proto_id, NULL, 1); |
684 | 8.43k | if (njs_slow_path(ret != NJS_OK)) { |
685 | 0 | return NJS_ERROR; |
686 | 0 | } |
687 | | |
688 | 8.43k | module = njs_vm_add_module(vm, &njs_str_value("crypto"), |
689 | 8.43k | njs_value_arg(&value)); |
690 | 8.43k | if (njs_slow_path(module == NULL)) { |
691 | 0 | return NJS_ERROR; |
692 | 0 | } |
693 | | |
694 | 8.43k | return NJS_OK; |
695 | 8.43k | } |