visaljs 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +45 -2
- package/cli.js +8084 -0
- package/node/include/js_node.h +46 -0
- package/node/prelude/async_hooks.js +88 -0
- package/node/prelude/buffer.js +31 -0
- package/node/prelude/crypto.js +114 -0
- package/node/prelude/events.js +227 -0
- package/node/prelude/fs.js +79 -0
- package/node/prelude/http.js +536 -0
- package/node/prelude/http2.js +30 -0
- package/node/prelude/internal/crypto/asym.js +333 -0
- package/node/prelude/internal/crypto/cipher.js +205 -0
- package/node/prelude/internal/crypto/hash.js +137 -0
- package/node/prelude/internal/crypto/kdf.js +182 -0
- package/node/prelude/internal/crypto/keys.js +603 -0
- package/node/prelude/internal/crypto/random.js +118 -0
- package/node/prelude/internal/crypto/util.js +76 -0
- package/node/prelude/internal/errors.js +88 -0
- package/node/prelude/internal/querystring.js +105 -0
- package/node/prelude/module.js +53 -0
- package/node/prelude/net.js +51 -0
- package/node/prelude/os.js +129 -0
- package/node/prelude/path.js +257 -0
- package/node/prelude/process.js +21 -0
- package/node/prelude/querystring.js +469 -0
- package/node/prelude/stream.js +1327 -0
- package/node/prelude/stream_promises.js +7 -0
- package/node/prelude/stream_web.js +6 -0
- package/node/prelude/string_decoder.js +236 -0
- package/node/prelude/tty.js +18 -0
- package/node/prelude/url.js +1156 -0
- package/node/prelude/util.js +64 -0
- package/node/prelude/zlib.js +81 -0
- package/node/src/host_fs.c +126 -0
- package/node/src/host_http.c +486 -0
- package/node/src/host_os.c +505 -0
- package/node/src/host_sys.c +78 -0
- package/node/third_party/libuv/LICENSE +19 -0
- package/node/third_party/libuv/include/uv/aix.h +32 -0
- package/node/third_party/libuv/include/uv/bsd.h +34 -0
- package/node/third_party/libuv/include/uv/darwin.h +61 -0
- package/node/third_party/libuv/include/uv/errno.h +483 -0
- package/node/third_party/libuv/include/uv/linux.h +34 -0
- package/node/third_party/libuv/include/uv/os390.h +33 -0
- package/node/third_party/libuv/include/uv/posix.h +31 -0
- package/node/third_party/libuv/include/uv/sunos.h +44 -0
- package/node/third_party/libuv/include/uv/threadpool.h +37 -0
- package/node/third_party/libuv/include/uv/tree.h +521 -0
- package/node/third_party/libuv/include/uv/unix.h +512 -0
- package/node/third_party/libuv/include/uv/version.h +43 -0
- package/node/third_party/libuv/include/uv/win.h +698 -0
- package/node/third_party/libuv/include/uv.h +1990 -0
- package/node/third_party/libuv/src/fs-poll.c +290 -0
- package/node/third_party/libuv/src/heap-inl.h +245 -0
- package/node/third_party/libuv/src/idna.c +560 -0
- package/node/third_party/libuv/src/idna.h +31 -0
- package/node/third_party/libuv/src/inet.c +298 -0
- package/node/third_party/libuv/src/queue.h +90 -0
- package/node/third_party/libuv/src/random.c +123 -0
- package/node/third_party/libuv/src/strscpy.c +38 -0
- package/node/third_party/libuv/src/strscpy.h +39 -0
- package/node/third_party/libuv/src/strtok.c +52 -0
- package/node/third_party/libuv/src/strtok.h +27 -0
- package/node/third_party/libuv/src/thread-common.c +175 -0
- package/node/third_party/libuv/src/threadpool.c +419 -0
- package/node/third_party/libuv/src/timer.c +200 -0
- package/node/third_party/libuv/src/unix/async.c +418 -0
- package/node/third_party/libuv/src/unix/bsd-ifaddrs.c +164 -0
- package/node/third_party/libuv/src/unix/core.c +2104 -0
- package/node/third_party/libuv/src/unix/darwin-proctitle.c +176 -0
- package/node/third_party/libuv/src/unix/darwin-stub.h +97 -0
- package/node/third_party/libuv/src/unix/darwin-syscalls.h +17 -0
- package/node/third_party/libuv/src/unix/darwin.c +237 -0
- package/node/third_party/libuv/src/unix/dl.c +80 -0
- package/node/third_party/libuv/src/unix/fs.c +2303 -0
- package/node/third_party/libuv/src/unix/fsevents.c +906 -0
- package/node/third_party/libuv/src/unix/getaddrinfo.c +252 -0
- package/node/third_party/libuv/src/unix/getnameinfo.c +121 -0
- package/node/third_party/libuv/src/unix/internal.h +530 -0
- package/node/third_party/libuv/src/unix/kqueue.c +667 -0
- package/node/third_party/libuv/src/unix/linux.c +2761 -0
- package/node/third_party/libuv/src/unix/loop-watcher.c +68 -0
- package/node/third_party/libuv/src/unix/loop.c +240 -0
- package/node/third_party/libuv/src/unix/pipe.c +555 -0
- package/node/third_party/libuv/src/unix/poll.c +159 -0
- package/node/third_party/libuv/src/unix/process.c +1123 -0
- package/node/third_party/libuv/src/unix/procfs-exepath.c +45 -0
- package/node/third_party/libuv/src/unix/proctitle.c +157 -0
- package/node/third_party/libuv/src/unix/random-devurandom.c +93 -0
- package/node/third_party/libuv/src/unix/random-getentropy.c +57 -0
- package/node/third_party/libuv/src/unix/random-getrandom.c +86 -0
- package/node/third_party/libuv/src/unix/random-sysctl-linux.c +99 -0
- package/node/third_party/libuv/src/unix/signal.c +581 -0
- package/node/third_party/libuv/src/unix/stream.c +1567 -0
- package/node/third_party/libuv/src/unix/tcp.c +673 -0
- package/node/third_party/libuv/src/unix/thread.c +975 -0
- package/node/third_party/libuv/src/unix/tty.c +510 -0
- package/node/third_party/libuv/src/unix/udp.c +1434 -0
- package/node/third_party/libuv/src/uv-common.c +1051 -0
- package/node/third_party/libuv/src/uv-common.h +474 -0
- package/node/third_party/libuv/src/uv-data-getter-setters.c +119 -0
- package/node/third_party/libuv/src/version.c +45 -0
- package/node/third_party/llhttp/LICENSE +22 -0
- package/node/third_party/llhttp/api.c +509 -0
- package/node/third_party/llhttp/http.c +170 -0
- package/node/third_party/llhttp/llhttp.c +10099 -0
- package/node/third_party/llhttp/llhttp.h +907 -0
- package/package.json +25 -4
- package/runtime/include/js_atoms.gen.h +68 -0
- package/runtime/include/js_atoms.h +54 -0
- package/runtime/include/js_bigint.h +93 -0
- package/runtime/include/js_buffer.h +79 -0
- package/runtime/include/js_builtins.h +168 -0
- package/runtime/include/js_console.h +58 -0
- package/runtime/include/js_convert.h +22 -0
- package/runtime/include/js_core.def +188 -0
- package/runtime/include/js_crypto.h +58 -0
- package/runtime/include/js_error.h +37 -0
- package/runtime/include/js_exception.h +65 -0
- package/runtime/include/js_function.h +167 -0
- package/runtime/include/js_gc.h +195 -0
- package/runtime/include/js_generator.h +96 -0
- package/runtime/include/js_host.h +19 -0
- package/runtime/include/js_jobs.h +84 -0
- package/runtime/include/js_module.h +16 -0
- package/runtime/include/js_object.h +384 -0
- package/runtime/include/js_ops.h +111 -0
- package/runtime/include/js_promise.h +45 -0
- package/runtime/include/js_regexp.h +25 -0
- package/runtime/include/js_runtime.h +30 -0
- package/runtime/include/js_symbol.h +62 -0
- package/runtime/include/js_web.h +110 -0
- package/runtime/include/jscell.h +48 -0
- package/runtime/include/jscontext.h +240 -0
- package/runtime/include/jsnum.h +42 -0
- package/runtime/include/jsstring.h +161 -0
- package/runtime/include/jsvalue.h +142 -0
- package/runtime/src/arguments.c +115 -0
- package/runtime/src/array.c +1145 -0
- package/runtime/src/arraybuffer.c +608 -0
- package/runtime/src/async_iter.c +479 -0
- package/runtime/src/atoms.c +157 -0
- package/runtime/src/base64.c +107 -0
- package/runtime/src/bigint.c +1189 -0
- package/runtime/src/blob.c +371 -0
- package/runtime/src/buffer.c +1357 -0
- package/runtime/src/builtin_table.c +100 -0
- package/runtime/src/builtins.c +987 -0
- package/runtime/src/builtins_util.h +53 -0
- package/runtime/src/collections.c +912 -0
- package/runtime/src/components.c +28 -0
- package/runtime/src/console.c +637 -0
- package/runtime/src/convert.c +82 -0
- package/runtime/src/core_all.c +21 -0
- package/runtime/src/crypto.c +116 -0
- package/runtime/src/crypto_asym.c +644 -0
- package/runtime/src/crypto_cipher.c +671 -0
- package/runtime/src/crypto_hash.c +260 -0
- package/runtime/src/crypto_host.c +414 -0
- package/runtime/src/crypto_internal.h +304 -0
- package/runtime/src/crypto_kdf.c +143 -0
- package/runtime/src/crypto_key.c +171 -0
- package/runtime/src/crypto_pkey.c +794 -0
- package/runtime/src/crypto_subtle.c +1675 -0
- package/runtime/src/date.c +940 -0
- package/runtime/src/domexception.c +169 -0
- package/runtime/src/elements.c +312 -0
- package/runtime/src/elements.h +45 -0
- package/runtime/src/encoding.c +485 -0
- package/runtime/src/error.c +297 -0
- package/runtime/src/events.c +793 -0
- package/runtime/src/exception.c +56 -0
- package/runtime/src/fetch.c +772 -0
- package/runtime/src/function.c +530 -0
- package/runtime/src/gc.c +803 -0
- package/runtime/src/generator.c +218 -0
- package/runtime/src/global.c +119 -0
- package/runtime/src/headers.c +557 -0
- package/runtime/src/inspect.c +1792 -0
- package/runtime/src/internal.h +80 -0
- package/runtime/src/iterate.c +94 -0
- package/runtime/src/iterator.c +409 -0
- package/runtime/src/jobs.c +337 -0
- package/runtime/src/jsnum.c +353 -0
- package/runtime/src/json.c +1028 -0
- package/runtime/src/math.c +243 -0
- package/runtime/src/module.c +336 -0
- package/runtime/src/number.c +176 -0
- package/runtime/src/object.c +1265 -0
- package/runtime/src/ops.c +322 -0
- package/runtime/src/os.c +196 -0
- package/runtime/src/os.h +29 -0
- package/runtime/src/process.c +432 -0
- package/runtime/src/promise.c +721 -0
- package/runtime/src/proxy.c +570 -0
- package/runtime/src/reflect.c +194 -0
- package/runtime/src/regexp.c +1141 -0
- package/runtime/src/regexp_anchor.h +34 -0
- package/runtime/src/regexp_message.h +32 -0
- package/runtime/src/shape.c +294 -0
- package/runtime/src/shape.h +46 -0
- package/runtime/src/streams.c +1046 -0
- package/runtime/src/streams.h +47 -0
- package/runtime/src/string.c +364 -0
- package/runtime/src/string_builtins.c +837 -0
- package/runtime/src/symbol.c +143 -0
- package/runtime/src/test_host.c +59 -0
- package/runtime/src/typedarray.c +1330 -0
- package/runtime/src/tznames.gen.h +712 -0
- package/runtime/src/uri.c +169 -0
- package/runtime/src/url.c +2116 -0
- package/runtime/src/weak.c +486 -0
- package/runtime/src/web.c +214 -0
- package/runtime/src/wrapper.c +59 -0
- package/runtime/src/writable.c +1486 -0
- package/runtime/third_party/ada-idna/LICENSE-MIT +18 -0
- package/runtime/third_party/ada-idna/README.md +27 -0
- package/runtime/third_party/ada-idna/convert.mjs +83 -0
- package/runtime/third_party/ada-idna/idna.c +416 -0
- package/runtime/third_party/ada-idna/idna.h +15 -0
- package/runtime/third_party/ada-idna/idna_tables.h +2657 -0
- package/runtime/third_party/bearssl/LICENSE.txt +21 -0
- package/runtime/third_party/bearssl/README.txt +136 -0
- package/runtime/third_party/bearssl/VERSION +1 -0
- package/runtime/third_party/bearssl/inc/bearssl.h +183 -0
- package/runtime/third_party/bearssl/inc/bearssl_aead.h +1059 -0
- package/runtime/third_party/bearssl/inc/bearssl_block.h +2618 -0
- package/runtime/third_party/bearssl/inc/bearssl_ec.h +967 -0
- package/runtime/third_party/bearssl/inc/bearssl_hash.h +1346 -0
- package/runtime/third_party/bearssl/inc/bearssl_hmac.h +241 -0
- package/runtime/third_party/bearssl/inc/bearssl_kdf.h +284 -0
- package/runtime/third_party/bearssl/inc/bearssl_pem.h +294 -0
- package/runtime/third_party/bearssl/inc/bearssl_prf.h +150 -0
- package/runtime/third_party/bearssl/inc/bearssl_rand.h +397 -0
- package/runtime/third_party/bearssl/inc/bearssl_rsa.h +1655 -0
- package/runtime/third_party/bearssl/inc/bearssl_ssl.h +4296 -0
- package/runtime/third_party/bearssl/inc/bearssl_x509.h +1474 -0
- package/runtime/third_party/bearssl/src/aead/ccm.c +346 -0
- package/runtime/third_party/bearssl/src/aead/eax.c +525 -0
- package/runtime/third_party/bearssl/src/aead/gcm.c +318 -0
- package/runtime/third_party/bearssl/src/codec/ccopy.c +44 -0
- package/runtime/third_party/bearssl/src/codec/dec16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/dec64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc16le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc32le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64be.c +38 -0
- package/runtime/third_party/bearssl/src/codec/enc64le.c +38 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.c +526 -0
- package/runtime/third_party/bearssl/src/codec/pemdec.t0 +314 -0
- package/runtime/third_party/bearssl/src/codec/pemenc.c +173 -0
- package/runtime/third_party/bearssl/src/config.h +247 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m15.c +121 -0
- package/runtime/third_party/bearssl/src/ec/ec_all_m31.c +171 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i15.c +398 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_i31.c +390 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m15.c +1478 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m31.c +800 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m62.c +605 -0
- package/runtime/third_party/bearssl/src/ec/ec_c25519_m64.c +831 -0
- package/runtime/third_party/bearssl/src/ec/ec_curve25519.c +46 -0
- package/runtime/third_party/bearssl/src/ec/ec_default.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ec_keygen.c +86 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m15.c +2124 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m31.c +1469 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m62.c +1765 -0
- package/runtime/third_party/bearssl/src/ec/ec_p256_m64.c +1781 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i15.c +824 -0
- package/runtime/third_party/bearssl/src/ec/ec_prime_i31.c +826 -0
- package/runtime/third_party/bearssl/src/ec/ec_pubkey.c +85 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp256r1.c +51 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp384r1.c +57 -0
- package/runtime/third_party/bearssl/src/ec/ec_secp521r1.c +64 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_atr.c +134 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_sign_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_asn1.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_default_vrfy_raw.c +36 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_sign_raw.c +174 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i15_vrfy_raw.c +166 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_bits.c +47 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_asn1.c +45 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_sign_raw.c +173 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_asn1.c +48 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_i31_vrfy_raw.c +165 -0
- package/runtime/third_party/bearssl/src/ec/ecdsa_rta.c +121 -0
- package/runtime/third_party/bearssl/src/hash/dig_oid.c +84 -0
- package/runtime/third_party/bearssl/src/hash/dig_size.c +50 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul.c +345 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul32.c +251 -0
- package/runtime/third_party/bearssl/src/hash/ghash_ctmul64.c +154 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pclmul.c +389 -0
- package/runtime/third_party/bearssl/src/hash/ghash_pwr8.c +411 -0
- package/runtime/third_party/bearssl/src/hash/md5.c +208 -0
- package/runtime/third_party/bearssl/src/hash/md5sha1.c +141 -0
- package/runtime/third_party/bearssl/src/hash/mgf1.c +56 -0
- package/runtime/third_party/bearssl/src/hash/multihash.c +166 -0
- package/runtime/third_party/bearssl/src/hash/sha1.c +191 -0
- package/runtime/third_party/bearssl/src/hash/sha2big.c +285 -0
- package/runtime/third_party/bearssl/src/hash/sha2small.c +341 -0
- package/runtime/third_party/bearssl/src/inner.h +2577 -0
- package/runtime/third_party/bearssl/src/int/i15_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i15_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i15_decode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_decred.c +100 -0
- package/runtime/third_party/bearssl/src/int/i15_encode.c +56 -0
- package/runtime/third_party/bearssl/src/int/i15_fmont.c +59 -0
- package/runtime/third_party/bearssl/src/int/i15_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i15_moddiv.c +465 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow.c +50 -0
- package/runtime/third_party/bearssl/src/int/i15_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i15_montmul.c +184 -0
- package/runtime/third_party/bearssl/src/int/i15_mulacc.c +61 -0
- package/runtime/third_party/bearssl/src/int/i15_muladd.c +173 -0
- package/runtime/third_party/bearssl/src/int/i15_ninv15.c +38 -0
- package/runtime/third_party/bearssl/src/int/i15_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i15_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i15_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i15_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i31_add.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i31_decmod.c +124 -0
- package/runtime/third_party/bearssl/src/int/i31_decode.c +57 -0
- package/runtime/third_party/bearssl/src/int/i31_decred.c +103 -0
- package/runtime/third_party/bearssl/src/int/i31_encode.c +79 -0
- package/runtime/third_party/bearssl/src/int/i31_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i31_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_moddiv.c +488 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i31_modpow2.c +160 -0
- package/runtime/third_party/bearssl/src/int/i31_montmul.c +127 -0
- package/runtime/third_party/bearssl/src/int/i31_mulacc.c +74 -0
- package/runtime/third_party/bearssl/src/int/i31_muladd.c +157 -0
- package/runtime/third_party/bearssl/src/int/i31_ninv31.c +39 -0
- package/runtime/third_party/bearssl/src/int/i31_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i31_rshift.c +47 -0
- package/runtime/third_party/bearssl/src/int/i31_sub.c +46 -0
- package/runtime/third_party/bearssl/src/int/i31_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i32_add.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_bitlen.c +44 -0
- package/runtime/third_party/bearssl/src/int/i32_decmod.c +77 -0
- package/runtime/third_party/bearssl/src/int/i32_decode.c +63 -0
- package/runtime/third_party/bearssl/src/int/i32_decred.c +107 -0
- package/runtime/third_party/bearssl/src/int/i32_div32.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_encode.c +72 -0
- package/runtime/third_party/bearssl/src/int/i32_fmont.c +60 -0
- package/runtime/third_party/bearssl/src/int/i32_iszero.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_modpow.c +65 -0
- package/runtime/third_party/bearssl/src/int/i32_montmul.c +69 -0
- package/runtime/third_party/bearssl/src/int/i32_mulacc.c +56 -0
- package/runtime/third_party/bearssl/src/int/i32_muladd.c +138 -0
- package/runtime/third_party/bearssl/src/int/i32_ninv32.c +39 -0
- package/runtime/third_party/bearssl/src/int/i32_reduce.c +66 -0
- package/runtime/third_party/bearssl/src/int/i32_sub.c +51 -0
- package/runtime/third_party/bearssl/src/int/i32_tmont.c +36 -0
- package/runtime/third_party/bearssl/src/int/i62_modpow2.c +493 -0
- package/runtime/third_party/bearssl/src/kdf/hkdf.c +107 -0
- package/runtime/third_party/bearssl/src/kdf/shake.c +590 -0
- package/runtime/third_party/bearssl/src/mac/hmac.c +122 -0
- package/runtime/third_party/bearssl/src/mac/hmac_ct.c +193 -0
- package/runtime/third_party/bearssl/src/rand/aesctr_drbg.c +206 -0
- package/runtime/third_party/bearssl/src/rand/hmac_drbg.c +157 -0
- package/runtime/third_party/bearssl/src/rand/sysrng.c +252 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_keygen.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_modulus.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_decrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_oaep_encrypt.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pkcs1_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_priv.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_privexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_sign.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pss_vrfy.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pub.c +38 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_default_pubexp.c +36 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_keygen.c +583 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_priv.c +209 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_privexp.c +320 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pub.c +113 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i15_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_keygen_inner.c +608 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_modulus.c +99 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_privexp.c +318 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i31_pubexp.c +152 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_decrypt.c +41 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_oaep_encrypt.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_sign.c +37 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pkcs1_vrfy.c +43 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_priv.c +160 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_sign.c +40 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pss_vrfy.c +44 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i32_pub.c +77 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_keygen.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_decrypt.c +61 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_oaep_encrypt.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_sign.c +57 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pkcs1_vrfy.c +63 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_priv.c +223 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_sign.c +60 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pss_vrfy.c +64 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_i62_pub.c +125 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_pad.c +112 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_oaep_unpad.c +145 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_pad.c +106 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_pss_sig_unpad.c +121 -0
- package/runtime/third_party/bearssl/src/rsa/rsa_ssl_decrypt.c +52 -0
- package/runtime/third_party/bearssl/src/settings.c +306 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_dec.c +254 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_big_enc.c +157 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_common.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct.c +328 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64.c +398 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcdec.c +104 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_cbcenc.c +81 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctr.c +114 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_ctrcbc.c +433 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_dec.c +159 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct64_enc.c +115 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcdec.c +111 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_cbcenc.c +91 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctr.c +116 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_ctrcbc.c +422 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_dec.c +170 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_ct_enc.c +112 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8.c +445 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcdec.c +670 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_cbcenc.c +417 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctr.c +717 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_pwr8_ctrcbc.c +946 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcdec.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctr.c +84 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_ctrcbc.c +142 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_dec.c +176 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_small_enc.c +129 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni.c +240 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcdec.c +223 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_cbcenc.c +122 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctr.c +211 -0
- package/runtime/third_party/bearssl/src/symcipher/aes_x86ni_ctrcbc.c +596 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_ct.c +106 -0
- package/runtime/third_party/bearssl/src/symcipher/chacha20_sse2.c +237 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct.c +411 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcdec.c +87 -0
- package/runtime/third_party/bearssl/src/symcipher/des_ct_cbcenc.c +69 -0
- package/runtime/third_party/bearssl/src/symcipher/des_support.c +166 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab.c +310 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcdec.c +85 -0
- package/runtime/third_party/bearssl/src/symcipher/des_tab_cbcenc.c +67 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul.c +260 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmul32.c +297 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_ctmulq.c +475 -0
- package/runtime/third_party/bearssl/src/symcipher/poly1305_i15.c +221 -0
- package/runtime/third_party/bearssl/src/x509/asn1.t0 +757 -0
- package/runtime/third_party/bearssl/src/x509/asn1enc.c +93 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_pk8der.c +110 -0
- package/runtime/third_party/bearssl/src/x509/encode_ec_rawder.c +161 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_pk8der.c +97 -0
- package/runtime/third_party/bearssl/src/x509/encode_rsa_rawder.c +96 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.c +653 -0
- package/runtime/third_party/bearssl/src/x509/skey_decoder.t0 +376 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.c +776 -0
- package/runtime/third_party/bearssl/src/x509/x509_decoder.t0 +324 -0
- package/runtime/third_party/bearssl/src/x509/x509_knownkey.c +105 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.c +1722 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal.t0 +1526 -0
- package/runtime/third_party/bearssl/src/x509/x509_minimal_full.c +59 -0
- package/runtime/third_party/quickjs-dtoa/LICENSE +22 -0
- package/runtime/third_party/quickjs-dtoa/README.md +23 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.c +1640 -0
- package/runtime/third_party/quickjs-dtoa/dtoa.h +83 -0
- package/runtime/third_party/quickjs-dtoa/dtoa_compat.h +89 -0
- package/runtime/third_party/quickjs-regexp/LICENSE +22 -0
- package/runtime/third_party/quickjs-regexp/README.md +22 -0
- package/runtime/third_party/quickjs-regexp/cutils.c +641 -0
- package/runtime/third_party/quickjs-regexp/cutils.h +457 -0
- package/runtime/third_party/quickjs-regexp/libregexp-opcode.h +73 -0
- package/runtime/third_party/quickjs-regexp/libregexp.c +3448 -0
- package/runtime/third_party/quickjs-regexp/libregexp.h +65 -0
- package/runtime/third_party/quickjs-regexp/libunicode-table.h +5206 -0
- package/runtime/third_party/quickjs-regexp/libunicode.c +2124 -0
- package/runtime/third_party/quickjs-regexp/libunicode.h +196 -0
- package/runtime/third_party/v8-ieee754/LICENSE +61 -0
- package/runtime/third_party/v8-ieee754/README.md +22 -0
- package/runtime/third_party/v8-ieee754/convert.py +84 -0
- package/runtime/third_party/v8-ieee754/ieee754.c +3030 -0
- package/runtime/third_party/v8-ieee754/ieee754.h +27 -0
- package/runtime/tools/regexp_compile.c +90 -0
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
// node:crypto's keys (ADR 0018): KeyObject and its subclasses, as Node's lib/internal/crypto/keys.js
|
|
2
|
+
// defines them (the same symbols, so they inspect as Node's do), over the runtime's key handles,
|
|
3
|
+
// which CryptoKey shares. PEM (base64 and its labels) and JWK (base64url and its members) are
|
|
4
|
+
// here; DER is the runtime's.
|
|
5
|
+
import {
|
|
6
|
+
cryptoHashInfo, cryptoKeyEquals, cryptoRandomFill, cryptoKeyExport, cryptoKeyFromParts, cryptoKeyGenerate, cryptoKeyHandleOf, cryptoKeyInfo,
|
|
7
|
+
cryptoKeyParse, cryptoKeyParts, cryptoKeyPublic, cryptoKeySecret, cryptoKeySecretBytes, cryptoToCryptoKey,
|
|
8
|
+
} from "visaljs:host";
|
|
9
|
+
import { codeError, invalidArgType, invalidArgValue, notSupported, outOfRange, validateFunction, validateString } from "../errors.js";
|
|
10
|
+
import { isAnyArrayBuffer, isArrayBufferView, kHandle, validateNumber, validateObject } from "./util.js";
|
|
11
|
+
|
|
12
|
+
const kKeyType = Symbol("kKeyType");
|
|
13
|
+
const kAsymmetricKeyType = Symbol("kAsymmetricKeyType");
|
|
14
|
+
const kAsymmetricKeyDetails = Symbol("kAsymmetricKeyDetails");
|
|
15
|
+
|
|
16
|
+
const later = (fn) => setTimeout(fn, 0);
|
|
17
|
+
const isStringOrBuffer = (v) => typeof v === "string" || isArrayBufferView(v) || isAnyArrayBuffer(v);
|
|
18
|
+
|
|
19
|
+
/* ---- errors ------------------------------------------------------------------------------------ */
|
|
20
|
+
|
|
21
|
+
const keyObjectType = (actual, expected) =>
|
|
22
|
+
codeError(TypeError, "ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE", `Invalid key object type ${actual}, expected ${expected}.`);
|
|
23
|
+
const incompatible = (encoding, why) =>
|
|
24
|
+
codeError(Error, "ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS", `The selected key encoding ${encoding} ${why}.`);
|
|
25
|
+
const invalidJwk = () => codeError(TypeError, "ERR_CRYPTO_INVALID_JWK", "Invalid JWK data");
|
|
26
|
+
|
|
27
|
+
/* the runtime's error codes for a key it couldn't decode or encode */
|
|
28
|
+
function keyError(code) {
|
|
29
|
+
switch (code) {
|
|
30
|
+
case 1: return codeError(Error, "ERR_OSSL_UNSUPPORTED", "error:1E08010C:DECODER routines::unsupported");
|
|
31
|
+
case 2: return codeError(Error, "ERR_OSSL_ASN1_WRONG_TAG", "error:068000A8:asn1 encoding routines::wrong tag");
|
|
32
|
+
case 3: return notSupported("This key type");
|
|
33
|
+
default: return new Error("Key operation failed");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** validateOneOf: `The property 'options.format' must be one of: undefined, 'buffer', 'jwk'. Received 'pem'` */
|
|
38
|
+
function validateOneOf(value, name, oneOf) {
|
|
39
|
+
if (!oneOf.includes(value)) {
|
|
40
|
+
const allowed = oneOf.map((v) => (typeof v === "string" ? `'${v}'` : String(v))).join(", ");
|
|
41
|
+
throw invalidArgValue(name, "must be one of: " + allowed, value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function validateUint32(value, name) {
|
|
46
|
+
validateNumber(value, name);
|
|
47
|
+
if (!Number.isInteger(value)) throw outOfRange(name, "an integer", value);
|
|
48
|
+
if (value < 0 || value > 4294967295) throw outOfRange(name, ">= 0 && <= 4294967295", value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function validateIntegerIn(value, name, min, max) {
|
|
52
|
+
validateNumber(value, name);
|
|
53
|
+
if (!Number.isInteger(value)) throw outOfRange(name, "an integer", value);
|
|
54
|
+
if (value < min || value > max) throw outOfRange(name, `>= ${min} && <= ${max}`, value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* ---- secret keys ------------------------------------------------------------------------------- */
|
|
58
|
+
|
|
59
|
+
/** A secret key's bytes from what the APIs accept: a string in `encoding`, a buffer or view, a
|
|
60
|
+
* secret KeyObject or CryptoKey. */
|
|
61
|
+
export function prepareSecretKey(key, encoding, bufferOnly = false) {
|
|
62
|
+
if (!bufferOnly) {
|
|
63
|
+
if (key instanceof KeyObject) {
|
|
64
|
+
if (key.type !== "secret") throw keyObjectType(key.type, "secret");
|
|
65
|
+
return cryptoKeySecretBytes(key[kHandle]);
|
|
66
|
+
}
|
|
67
|
+
const handle = cryptoKeyHandleOf(key);
|
|
68
|
+
if (handle !== undefined) {
|
|
69
|
+
const type = cryptoKeyInfo(handle)[0];
|
|
70
|
+
if (type !== "secret") throw keyObjectType(type, "secret");
|
|
71
|
+
return cryptoKeySecretBytes(handle);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (typeof key === "string") {
|
|
75
|
+
return Buffer.from(key, encoding === undefined || encoding === "buffer" || !Buffer.isEncoding(encoding) ? "utf8" : encoding);
|
|
76
|
+
}
|
|
77
|
+
if (isArrayBufferView(key) || isAnyArrayBuffer(key)) return key;
|
|
78
|
+
const expected = bufferOnly
|
|
79
|
+
? "an instance of ArrayBuffer, Buffer, TypedArray, or DataView"
|
|
80
|
+
: "of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey";
|
|
81
|
+
throw invalidArgType("key", expected, key);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/* ---- KeyObject --------------------------------------------------------------------------------- */
|
|
85
|
+
|
|
86
|
+
function isKeyHandle(handle) {
|
|
87
|
+
try {
|
|
88
|
+
cryptoKeyInfo(handle);
|
|
89
|
+
return true;
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export class KeyObject {
|
|
96
|
+
constructor(type, handle) {
|
|
97
|
+
if (type !== "secret" && type !== "public" && type !== "private") throw invalidArgValue("type", "is invalid", type);
|
|
98
|
+
if (typeof handle !== "object" || handle === null || !isKeyHandle(handle)) throw invalidArgType("handle", "of type object", handle);
|
|
99
|
+
this[kKeyType] = type;
|
|
100
|
+
Object.defineProperty(this, kHandle, { value: handle, enumerable: false, configurable: false, writable: false });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get type() {
|
|
104
|
+
return this[kKeyType];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static from(key) {
|
|
108
|
+
const handle = cryptoKeyHandleOf(key);
|
|
109
|
+
if (handle === undefined) throw invalidArgType("key", "an instance of CryptoKey", key);
|
|
110
|
+
return wrapHandle(handle);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
equals(otherKeyObject) {
|
|
114
|
+
if (!(otherKeyObject instanceof KeyObject)) throw invalidArgType("otherKeyObject", "an instance of KeyObject", otherKeyObject);
|
|
115
|
+
return otherKeyObject.type === this.type && cryptoKeyEquals(this[kHandle], otherKeyObject[kHandle]);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
class SecretKeyObject extends KeyObject {
|
|
120
|
+
constructor(handle) {
|
|
121
|
+
super("secret", handle);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get symmetricKeySize() {
|
|
125
|
+
return cryptoKeySecretBytes(this[kHandle]).length;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export(options) {
|
|
129
|
+
if (options !== undefined) {
|
|
130
|
+
validateObject(options, "options");
|
|
131
|
+
validateOneOf(options.format, "options.format", [undefined, "buffer", "jwk"]);
|
|
132
|
+
if (options.format === "jwk") return { kty: "oct", k: cryptoKeySecretBytes(this[kHandle]).toString("base64url") };
|
|
133
|
+
}
|
|
134
|
+
return cryptoKeySecretBytes(this[kHandle]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
toCryptoKey(algorithm, extractable, keyUsages) {
|
|
138
|
+
return cryptoToCryptoKey("raw", cryptoKeySecretBytes(this[kHandle]), algorithm, extractable, keyUsages);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/* asymmetricKeyDetails: Node's normalized form of the key's details */
|
|
143
|
+
function keyDetails(handle) {
|
|
144
|
+
const [, kind, namedCurve, pss] = cryptoKeyInfo(handle);
|
|
145
|
+
if (kind === "ec") return { namedCurve };
|
|
146
|
+
const [n, e] = cryptoKeyParts(handle);
|
|
147
|
+
let bits = (n.length - 1) * 8;
|
|
148
|
+
for (let top = n[0]; top; top >>= 1) bits++;
|
|
149
|
+
const details = { modulusLength: bits, publicExponent: BigInt("0x" + e.toString("hex")) };
|
|
150
|
+
if (pss) {
|
|
151
|
+
details.hashAlgorithm = pss[0];
|
|
152
|
+
details.mgf1HashAlgorithm = pss[1];
|
|
153
|
+
details.saltLength = pss[2];
|
|
154
|
+
}
|
|
155
|
+
return details;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
class AsymmetricKeyObject extends KeyObject {
|
|
159
|
+
get asymmetricKeyType() {
|
|
160
|
+
return this[kAsymmetricKeyType] ||= cryptoKeyInfo(this[kHandle])[1];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
get asymmetricKeyDetails() {
|
|
164
|
+
switch (this.asymmetricKeyType) {
|
|
165
|
+
case "rsa":
|
|
166
|
+
case "rsa-pss":
|
|
167
|
+
case "ec":
|
|
168
|
+
return this[kAsymmetricKeyDetails] ||= keyDetails(this[kHandle]);
|
|
169
|
+
default:
|
|
170
|
+
return {};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
toCryptoKey(algorithm, extractable, keyUsages) {
|
|
175
|
+
const der = cryptoKeyExport(this[kHandle], this.type === "public" ? 0 : 2);
|
|
176
|
+
return cryptoToCryptoKey(this.type === "public" ? "spki" : "pkcs8", der, algorithm, extractable, keyUsages);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
class PublicKeyObject extends AsymmetricKeyObject {
|
|
181
|
+
constructor(handle) {
|
|
182
|
+
super("public", handle);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export(options) {
|
|
186
|
+
if (options && options.format === "jwk") return exportJwk(this[kHandle], false);
|
|
187
|
+
const { format, type } = parseKeyEncoding(options, this.asymmetricKeyType, true);
|
|
188
|
+
return exportKey(this[kHandle], format, type, false);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
class PrivateKeyObject extends AsymmetricKeyObject {
|
|
193
|
+
constructor(handle) {
|
|
194
|
+
super("private", handle);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export(options) {
|
|
198
|
+
if (options && options.format === "jwk") {
|
|
199
|
+
if (options.passphrase !== undefined) throw incompatible("jwk", "does not support encryption");
|
|
200
|
+
return exportJwk(this[kHandle], true);
|
|
201
|
+
}
|
|
202
|
+
const { format, type, cipher } = parseKeyEncoding(options, this.asymmetricKeyType, false);
|
|
203
|
+
if (cipher != null) throw notSupported("Encrypting a private key");
|
|
204
|
+
return exportKey(this[kHandle], format, type, true);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function wrapHandle(handle) {
|
|
209
|
+
switch (cryptoKeyInfo(handle)[0]) {
|
|
210
|
+
case "secret": return new SecretKeyObject(handle);
|
|
211
|
+
case "public": return new PublicKeyObject(handle);
|
|
212
|
+
default: return new PrivateKeyObject(handle);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* ---- encodings: PEM and DER -------------------------------------------------------------------- */
|
|
217
|
+
|
|
218
|
+
/* the runtime's encodings */
|
|
219
|
+
const SPKI = 0, PKCS1 = 1, PKCS8 = 2, SEC1 = 3, CERT = 4;
|
|
220
|
+
const ENCODING_NAMES = { pkcs1: PKCS1, spki: SPKI, pkcs8: PKCS8, sec1: SEC1 };
|
|
221
|
+
|
|
222
|
+
function option(name, objName) {
|
|
223
|
+
return objName === undefined ? `options.${name}` : `options.${objName}.${name}`;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function parseKeyFormat(formatStr, defaultFormat, optionName) {
|
|
227
|
+
if (formatStr === undefined && defaultFormat !== undefined) return defaultFormat;
|
|
228
|
+
if (formatStr === "pem" || formatStr === "der" || formatStr === "jwk") return formatStr;
|
|
229
|
+
throw invalidArgValue(optionName, "is invalid", formatStr);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function parseKeyType(typeStr, required, keyType, isPublic, optionName) {
|
|
233
|
+
if (typeStr === undefined && !required) return undefined;
|
|
234
|
+
if (typeStr === "pkcs1") {
|
|
235
|
+
if (keyType !== undefined && keyType !== "rsa") throw incompatible(typeStr, "can only be used for RSA keys");
|
|
236
|
+
return typeStr;
|
|
237
|
+
}
|
|
238
|
+
if (typeStr === "spki" && isPublic !== false) return typeStr;
|
|
239
|
+
if (typeStr === "pkcs8" && isPublic !== true) return typeStr;
|
|
240
|
+
if (typeStr === "sec1" && isPublic !== true) {
|
|
241
|
+
if (keyType !== undefined && keyType !== "ec") throw incompatible(typeStr, "can only be used for EC keys");
|
|
242
|
+
return typeStr;
|
|
243
|
+
}
|
|
244
|
+
throw invalidArgValue(optionName, "is invalid", typeStr);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function parseKeyEncoding(enc, keyType, isPublic, objName) {
|
|
248
|
+
validateObject(enc, "options");
|
|
249
|
+
const isInput = keyType === undefined;
|
|
250
|
+
const format = parseKeyFormat(enc.format, isInput ? "pem" : undefined, option("format", objName));
|
|
251
|
+
const isRequired = (!isInput || format === "der") && format !== "jwk";
|
|
252
|
+
const type = parseKeyType(enc.type, isRequired, keyType, isPublic, option("type", objName));
|
|
253
|
+
let cipher, passphrase;
|
|
254
|
+
if (isPublic !== true) {
|
|
255
|
+
({ cipher, passphrase } = enc);
|
|
256
|
+
if (!isInput) {
|
|
257
|
+
if (cipher != null) {
|
|
258
|
+
if (typeof cipher !== "string") throw invalidArgValue(option("cipher", objName), "is invalid", cipher);
|
|
259
|
+
if (format === "der" && (type === "pkcs1" || type === "sec1")) throw incompatible(type, "does not support encryption");
|
|
260
|
+
} else if (passphrase !== undefined) {
|
|
261
|
+
throw invalidArgValue(option("cipher", objName), "is invalid", cipher);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return { format, type, cipher, passphrase };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const PEM_LABELS = { [SPKI]: "PUBLIC KEY", [PKCS8]: "PRIVATE KEY", [SEC1]: "EC PRIVATE KEY" };
|
|
269
|
+
|
|
270
|
+
function exportKey(handle, format, type, isPrivate) {
|
|
271
|
+
const encoding = ENCODING_NAMES[type];
|
|
272
|
+
const der = cryptoKeyExport(handle, encoding);
|
|
273
|
+
if (typeof der === "number") throw keyError(der);
|
|
274
|
+
if (format === "der") return der;
|
|
275
|
+
const label = encoding === PKCS1 ? (isPrivate ? "RSA PRIVATE KEY" : "RSA PUBLIC KEY") : PEM_LABELS[encoding];
|
|
276
|
+
const body = der.toString("base64").replace(/(.{64})/g, "$1\n").replace(/\n$/, "");
|
|
277
|
+
return `-----BEGIN ${label}-----\n${body}\n-----END ${label}-----\n`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/* A PEM's label → the encoding it holds, and whether it is private */
|
|
281
|
+
const PEM_TYPES = {
|
|
282
|
+
"PUBLIC KEY": [SPKI, false],
|
|
283
|
+
"RSA PUBLIC KEY": [PKCS1, false],
|
|
284
|
+
"CERTIFICATE": [CERT, false],
|
|
285
|
+
"PRIVATE KEY": [PKCS8, true],
|
|
286
|
+
"RSA PRIVATE KEY": [PKCS1, true],
|
|
287
|
+
"EC PRIVATE KEY": [SEC1, true],
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/** A key handle from PEM or DER: the private key when `wantPrivate`, else a public one (a
|
|
291
|
+
* private key's public part, as createPublicKey takes it). */
|
|
292
|
+
function parseKey(data, format, type, wantPrivate) {
|
|
293
|
+
if (format === "der") {
|
|
294
|
+
const encoding = ENCODING_NAMES[type];
|
|
295
|
+
const isPrivate = encoding === PKCS8 || encoding === SEC1 || (encoding === PKCS1 && wantPrivate);
|
|
296
|
+
const handle = cryptoKeyParse(data, encoding, isPrivate);
|
|
297
|
+
if (typeof handle === "number") throw keyError(handle);
|
|
298
|
+
return wantPrivate || !isPrivate ? handle : cryptoKeyPublic(handle);
|
|
299
|
+
}
|
|
300
|
+
const text = typeof data === "string" ? data : Buffer.from(isAnyArrayBuffer(data) ? new Uint8Array(data) : data).toString("latin1");
|
|
301
|
+
const m = /-----BEGIN ([A-Z0-9 ]+)-----\r?\n([\s\S]*?)-----END \1-----/.exec(text);
|
|
302
|
+
if (m === null) throw keyError(1);
|
|
303
|
+
if (m[1] === "ENCRYPTED PRIVATE KEY" || m[2].includes(":")) throw notSupported("An encrypted private key");
|
|
304
|
+
const known = PEM_TYPES[m[1]];
|
|
305
|
+
if (known === undefined || (wantPrivate && !known[1])) throw keyError(1);
|
|
306
|
+
const handle = cryptoKeyParse(Buffer.from(m[2], "base64"), known[0], known[1]);
|
|
307
|
+
if (typeof handle === "number") throw keyError(handle);
|
|
308
|
+
return known[1] && !wantPrivate ? cryptoKeyPublic(handle) : handle;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ---- JWK --------------------------------------------------------------------------------------- */
|
|
312
|
+
|
|
313
|
+
const b64u = (buf) => buf.toString("base64url");
|
|
314
|
+
const CURVE_SIZES = { "P-256": 32, "P-384": 48, "P-521": 66 };
|
|
315
|
+
const JWK_CURVES = { prime256v1: "P-256", secp384r1: "P-384", secp521r1: "P-521" };
|
|
316
|
+
|
|
317
|
+
function exportJwk(handle, isPrivate) {
|
|
318
|
+
const [, kind, namedCurve] = cryptoKeyInfo(handle);
|
|
319
|
+
const parts = cryptoKeyParts(handle);
|
|
320
|
+
if (kind === "rsa") {
|
|
321
|
+
const jwk = { kty: "RSA", n: b64u(parts[0]), e: b64u(parts[1]) };
|
|
322
|
+
if (isPrivate) ["d", "p", "q", "dp", "dq", "qi"].forEach((name, i) => (jwk[name] = b64u(parts[i + 2])));
|
|
323
|
+
return jwk;
|
|
324
|
+
}
|
|
325
|
+
if (kind === "ec") {
|
|
326
|
+
const size = (parts[0].length - 1) / 2;
|
|
327
|
+
const jwk = { kty: "EC", x: b64u(parts[0].subarray(1, 1 + size)), y: b64u(parts[0].subarray(1 + size)), crv: JWK_CURVES[namedCurve] };
|
|
328
|
+
if (isPrivate) jwk.d = b64u(parts[1]);
|
|
329
|
+
return jwk;
|
|
330
|
+
}
|
|
331
|
+
if (kind === "x25519") {
|
|
332
|
+
return isPrivate ? { crv: "X25519", d: b64u(parts[1]), x: b64u(parts[0]), kty: "OKP" } : { crv: "X25519", x: b64u(parts[0]), kty: "OKP" };
|
|
333
|
+
}
|
|
334
|
+
throw codeError(Error, "ERR_CRYPTO_JWK_UNSUPPORTED_KEY_TYPE", "Unsupported JWK Key Type.");
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function fromParts(kind, isPrivate, crv, parts) {
|
|
338
|
+
const handle = cryptoKeyFromParts(kind, isPrivate, crv, parts);
|
|
339
|
+
if (typeof handle === "number") throw handle === 3 ? notSupported("This curve") : invalidJwk();
|
|
340
|
+
return handle;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function keyFromJwk(key, isPublic) {
|
|
344
|
+
validateObject(key, "key");
|
|
345
|
+
validateOneOf(key.kty, "key.kty", ["RSA", "EC", "OKP"]);
|
|
346
|
+
const b64 = (s) => Buffer.from(s, "base64");
|
|
347
|
+
if (key.kty === "OKP") {
|
|
348
|
+
validateString(key.crv, "key.crv");
|
|
349
|
+
validateOneOf(key.crv, "key.crv", ["Ed25519", "Ed448", "X25519", "X448"]);
|
|
350
|
+
validateString(key.x, "key.x");
|
|
351
|
+
if (!isPublic) validateString(key.d, "key.d");
|
|
352
|
+
const data = isPublic ? b64(key.x) : b64(key.d);
|
|
353
|
+
if (key.crv !== "X25519") throw notSupported(key.crv + " keys");
|
|
354
|
+
if (data.byteLength !== 32) throw invalidJwk();
|
|
355
|
+
return isPublic ? fromParts("x25519", false, undefined, [data]) : fromParts("x25519", true, undefined, [undefined, data]);
|
|
356
|
+
}
|
|
357
|
+
if (key.kty === "EC") {
|
|
358
|
+
validateString(key.crv, "key.crv");
|
|
359
|
+
validateOneOf(key.crv, "key.crv", ["P-256", "secp256k1", "P-384", "P-521"]);
|
|
360
|
+
validateString(key.x, "key.x");
|
|
361
|
+
validateString(key.y, "key.y");
|
|
362
|
+
if (!isPublic) validateString(key.d, "key.d");
|
|
363
|
+
const size = CURVE_SIZES[key.crv];
|
|
364
|
+
const x = b64(key.x);
|
|
365
|
+
const y = b64(key.y);
|
|
366
|
+
if (size !== undefined && (x.length !== size || y.length !== size)) throw invalidJwk();
|
|
367
|
+
const point = Buffer.concat([Buffer.from([4]), x, y]);
|
|
368
|
+
return isPublic ? fromParts("ec", false, key.crv, [point]) : fromParts("ec", true, key.crv, [point, b64(key.d)]);
|
|
369
|
+
}
|
|
370
|
+
validateString(key.n, "key.n");
|
|
371
|
+
validateString(key.e, "key.e");
|
|
372
|
+
const names = ["n", "e"];
|
|
373
|
+
if (!isPublic) {
|
|
374
|
+
for (const name of ["d", "p", "q", "dp", "dq", "qi"]) {
|
|
375
|
+
validateString(key[name], "key." + name);
|
|
376
|
+
names.push(name);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return fromParts("rsa", !isPublic, undefined, names.map((name) => b64(key[name])));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/* ---- createSecretKey, createPublicKey, createPrivateKey ---------------------------------------- */
|
|
383
|
+
|
|
384
|
+
export function createSecretKey(key, encoding) {
|
|
385
|
+
const bytes = prepareSecretKey(key, encoding, true);
|
|
386
|
+
return new SecretKeyObject(cryptoKeySecret(bytes));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/* Where an asymmetric key is wanted (Node's kCreatePublic, …) */
|
|
390
|
+
const CREATE_PUBLIC = 0, CREATE_PRIVATE = 1, CONSUME_PUBLIC = 2, CONSUME_PRIVATE = 3;
|
|
391
|
+
|
|
392
|
+
/* a KeyObject (or CryptoKey) given where an asymmetric key is wanted */
|
|
393
|
+
function handleOfKeyObject(key, ctx) {
|
|
394
|
+
if (ctx === CREATE_PRIVATE) throw invalidArgType("key", "of type string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView", key);
|
|
395
|
+
if (key.type !== "private") {
|
|
396
|
+
if (ctx === CONSUME_PRIVATE || ctx === CREATE_PUBLIC) throw keyObjectType(key.type, "private");
|
|
397
|
+
if (key.type !== "public") throw keyObjectType(key.type, "private or public");
|
|
398
|
+
}
|
|
399
|
+
return key[kHandle];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** prepareAsymmetricKey: a handle (for CREATE_PUBLIC from a PEM or DER private key, its public
|
|
403
|
+
* part), and whether it came from a KeyObject or CryptoKey. */
|
|
404
|
+
function prepareAsymmetricKey(key, ctx) {
|
|
405
|
+
const wantPrivate = ctx === CREATE_PRIVATE || ctx === CONSUME_PRIVATE;
|
|
406
|
+
const asKeyObject = (v) => {
|
|
407
|
+
if (v instanceof KeyObject) return v;
|
|
408
|
+
const handle = cryptoKeyHandleOf(v);
|
|
409
|
+
return handle === undefined ? undefined : wrapHandle(handle);
|
|
410
|
+
};
|
|
411
|
+
let keyObject = asKeyObject(key);
|
|
412
|
+
if (keyObject) return { key: handleOfKeyObject(keyObject, ctx), keyObject: true };
|
|
413
|
+
if (isStringOrBuffer(key)) return { key: parseKey(key, "pem", undefined, wantPrivate) };
|
|
414
|
+
if (typeof key === "object" && key !== null) {
|
|
415
|
+
const { key: data, encoding, format } = key;
|
|
416
|
+
keyObject = asKeyObject(data);
|
|
417
|
+
if (keyObject) return { key: handleOfKeyObject(keyObject, ctx), keyObject: true };
|
|
418
|
+
if (format === "jwk") {
|
|
419
|
+
validateObject(data, "key.key");
|
|
420
|
+
return { key: keyFromJwk(data, !wantPrivate) };
|
|
421
|
+
}
|
|
422
|
+
if (!isStringOrBuffer(data)) {
|
|
423
|
+
throw invalidArgType("key.key", "of type string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView", data);
|
|
424
|
+
}
|
|
425
|
+
const { format: fmt, type } = parseKeyEncoding(key, undefined, wantPrivate ? false : undefined);
|
|
426
|
+
const bytes = typeof data === "string" && fmt === "der" ? Buffer.from(data, encoding ?? "utf8") : data;
|
|
427
|
+
return { key: parseKey(bytes, fmt, type, wantPrivate) };
|
|
428
|
+
}
|
|
429
|
+
const expected = ctx === CREATE_PRIVATE
|
|
430
|
+
? "of type string or an instance of ArrayBuffer, Buffer, TypedArray, or DataView"
|
|
431
|
+
: "of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey";
|
|
432
|
+
throw invalidArgType("key", expected, key);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** The handle of a private key from what sign, privateDecrypt, … take. */
|
|
436
|
+
export function preparePrivateKey(key) {
|
|
437
|
+
return prepareAsymmetricKey(key, CONSUME_PRIVATE).key;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** The handle of a public or private key from what verify, publicEncrypt, … take. */
|
|
441
|
+
export function preparePublicOrPrivateKey(key) {
|
|
442
|
+
return prepareAsymmetricKey(key, CONSUME_PUBLIC).key;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export function createPublicKey(key) {
|
|
446
|
+
const { key: handle, keyObject } = prepareAsymmetricKey(key, CREATE_PUBLIC);
|
|
447
|
+
return new PublicKeyObject(keyObject ? cryptoKeyPublic(handle) : handle);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function createPrivateKey(key) {
|
|
451
|
+
return new PrivateKeyObject(prepareAsymmetricKey(key, CREATE_PRIVATE).key);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** The asymmetric key type of a handle ('rsa', 'ec', …). */
|
|
455
|
+
export function asymmetricKeyTypeOf(handle) {
|
|
456
|
+
return cryptoKeyInfo(handle)[1];
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** A key's RSA-PSS parameters: [hash, mgf1, saltLength], or null. */
|
|
460
|
+
export function pssParamsOf(handle) {
|
|
461
|
+
return cryptoKeyInfo(handle)[3];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/* ---- generateKey, generateKeyPair -------------------------------------------------------------- */
|
|
465
|
+
|
|
466
|
+
function secretKeyJob(type, options) {
|
|
467
|
+
validateString(type, "type");
|
|
468
|
+
validateObject(options, "options");
|
|
469
|
+
const { length } = options;
|
|
470
|
+
switch (type) {
|
|
471
|
+
case "hmac":
|
|
472
|
+
validateIntegerIn(length, "options.length", 8, 2 ** 31 - 1);
|
|
473
|
+
break;
|
|
474
|
+
case "aes":
|
|
475
|
+
validateOneOf(length, "options.length", [128, 192, 256]);
|
|
476
|
+
break;
|
|
477
|
+
default:
|
|
478
|
+
throw invalidArgValue("type", "must be a supported key type", type);
|
|
479
|
+
}
|
|
480
|
+
return () => {
|
|
481
|
+
const bytes = Buffer.alloc(Math.floor(length / 8));
|
|
482
|
+
cryptoRandomFill(bytes, 0, bytes.length);
|
|
483
|
+
return new SecretKeyObject(cryptoKeySecret(bytes));
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export function generateKeySync(type, options) {
|
|
488
|
+
return secretKeyJob(type, options)();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
export function generateKey(type, options, callback) {
|
|
492
|
+
if (typeof options === "function") {
|
|
493
|
+
callback = options;
|
|
494
|
+
options = undefined;
|
|
495
|
+
}
|
|
496
|
+
validateFunction(callback, "callback");
|
|
497
|
+
const job = secretKeyJob(type, options);
|
|
498
|
+
const key = job();
|
|
499
|
+
later(() => callback(null, key));
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function pairEncodings(keyType, options) {
|
|
503
|
+
const { publicKeyEncoding, privateKeyEncoding } = options ?? {};
|
|
504
|
+
let pub, priv;
|
|
505
|
+
if (publicKeyEncoding != null) {
|
|
506
|
+
if (typeof publicKeyEncoding !== "object") throw invalidArgValue("options.publicKeyEncoding", "is invalid", publicKeyEncoding);
|
|
507
|
+
pub = publicKeyEncoding.format === "jwk" ? { format: "jwk" } : parseKeyEncoding(publicKeyEncoding, keyType, true, "publicKeyEncoding");
|
|
508
|
+
}
|
|
509
|
+
if (privateKeyEncoding != null) {
|
|
510
|
+
if (typeof privateKeyEncoding !== "object") throw invalidArgValue("options.privateKeyEncoding", "is invalid", privateKeyEncoding);
|
|
511
|
+
priv = privateKeyEncoding.format === "jwk" ? { format: "jwk" } : parseKeyEncoding(privateKeyEncoding, keyType, false, "privateKeyEncoding");
|
|
512
|
+
if (priv.cipher != null) throw notSupported("Encrypting a private key");
|
|
513
|
+
}
|
|
514
|
+
return { pub, priv };
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function keyPairJob(type, options) {
|
|
518
|
+
validateString(type, "type");
|
|
519
|
+
const encodings = pairEncodings(type, options);
|
|
520
|
+
if (options !== undefined) validateObject(options, "options");
|
|
521
|
+
let generate;
|
|
522
|
+
switch (type) {
|
|
523
|
+
case "rsa":
|
|
524
|
+
case "rsa-pss": {
|
|
525
|
+
validateObject(options, "options");
|
|
526
|
+
const { modulusLength } = options;
|
|
527
|
+
validateUint32(modulusLength, "options.modulusLength");
|
|
528
|
+
let { publicExponent } = options;
|
|
529
|
+
if (publicExponent == null) publicExponent = 0x10001;
|
|
530
|
+
else validateUint32(publicExponent, "options.publicExponent");
|
|
531
|
+
let pss = null;
|
|
532
|
+
if (type === "rsa-pss") {
|
|
533
|
+
const { hashAlgorithm, mgf1HashAlgorithm, saltLength } = options;
|
|
534
|
+
if (saltLength !== undefined) validateIntegerIn(saltLength, "options.saltLength", 0, 2147483647);
|
|
535
|
+
if (hashAlgorithm !== undefined) validateString(hashAlgorithm, "options.hashAlgorithm");
|
|
536
|
+
if (mgf1HashAlgorithm !== undefined) validateString(mgf1HashAlgorithm, "options.mgf1HashAlgorithm");
|
|
537
|
+
if (hashAlgorithm !== undefined || mgf1HashAlgorithm !== undefined || saltLength !== undefined) {
|
|
538
|
+
const hash = hashAlgorithm ?? "sha1";
|
|
539
|
+
pss = [hash, mgf1HashAlgorithm ?? hash, saltLength ?? cryptoHashInfo(hash)];
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (modulusLength < 512) throw new Error("error:1C8000AB:Provider routines::key size too small");
|
|
543
|
+
if (modulusLength > 4096) throw notSupported("An RSA key of more than 4096 bits");
|
|
544
|
+
generate = () => cryptoKeyGenerate(type, modulusLength, publicExponent, pss);
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
case "ec": {
|
|
548
|
+
validateObject(options, "options");
|
|
549
|
+
const { namedCurve, paramEncoding } = options;
|
|
550
|
+
validateString(namedCurve, "options.namedCurve");
|
|
551
|
+
if (paramEncoding != null && paramEncoding !== "named") {
|
|
552
|
+
if (paramEncoding === "explicit") throw notSupported("Explicit EC parameters");
|
|
553
|
+
throw invalidArgValue("options.paramEncoding", "is invalid", paramEncoding);
|
|
554
|
+
}
|
|
555
|
+
generate = () => cryptoKeyGenerate("ec", namedCurve);
|
|
556
|
+
generate.curve = namedCurve;
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
case "x25519":
|
|
560
|
+
generate = () => cryptoKeyGenerate("x25519");
|
|
561
|
+
break;
|
|
562
|
+
case "ed25519":
|
|
563
|
+
case "ed448":
|
|
564
|
+
case "x448":
|
|
565
|
+
case "dsa":
|
|
566
|
+
case "dh":
|
|
567
|
+
throw notSupported(`A ${type} key`);
|
|
568
|
+
default:
|
|
569
|
+
throw invalidArgValue("type", "must be a supported key type", type);
|
|
570
|
+
}
|
|
571
|
+
return () => {
|
|
572
|
+
const handle = generate();
|
|
573
|
+
if (typeof handle === "number") {
|
|
574
|
+
if (type === "ec") throw codeError(TypeError, "ERR_CRYPTO_INVALID_CURVE", "Invalid EC curve name");
|
|
575
|
+
throw new Error("Key generation failed");
|
|
576
|
+
}
|
|
577
|
+
const privateKey = new PrivateKeyObject(handle);
|
|
578
|
+
const publicKey = new PublicKeyObject(cryptoKeyPublic(handle));
|
|
579
|
+
const encode = (key, enc) => (enc === undefined ? key : enc.format === "jwk" ? key.export({ format: "jwk" }) : key.export(enc));
|
|
580
|
+
return { publicKey: encode(publicKey, encodings.pub), privateKey: encode(privateKey, encodings.priv) };
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
export function generateKeyPairSync(type, options) {
|
|
585
|
+
return keyPairJob(type, options)();
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function generateKeyPair(type, options, callback) {
|
|
589
|
+
if (typeof options === "function") {
|
|
590
|
+
callback = options;
|
|
591
|
+
options = undefined;
|
|
592
|
+
}
|
|
593
|
+
validateFunction(callback, "callback");
|
|
594
|
+
const job = keyPairJob(type, options);
|
|
595
|
+
let result;
|
|
596
|
+
try {
|
|
597
|
+
result = job();
|
|
598
|
+
} catch (e) {
|
|
599
|
+
later(() => callback(e));
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
later(() => callback(null, result.publicKey, result.privateKey));
|
|
603
|
+
}
|