|
826 | 826 | }; |
827 | 827 | } |
828 | 828 |
|
| 829 | + if ( |
| 830 | + typeof _cryptoDiffieHellmanSessionCreate !== 'undefined' && |
| 831 | + typeof _cryptoDiffieHellmanSessionCall !== 'undefined' |
| 832 | + ) { |
| 833 | + function serializeDhKeyObject(value) { |
| 834 | + if (value.type === 'secret') { |
| 835 | + return { |
| 836 | + type: 'secret', |
| 837 | + raw: Buffer.from(value.export()).toString('base64'), |
| 838 | + }; |
| 839 | + } |
| 840 | + return { |
| 841 | + type: value.type, |
| 842 | + pem: value._pem || value.export({ |
| 843 | + type: value.type === 'private' ? 'pkcs8' : 'spki', |
| 844 | + format: 'pem', |
| 845 | + }), |
| 846 | + }; |
| 847 | + } |
| 848 | + |
| 849 | + function serializeDhValue(value) { |
| 850 | + if ( |
| 851 | + value === null || |
| 852 | + typeof value === 'string' || |
| 853 | + typeof value === 'number' || |
| 854 | + typeof value === 'boolean' |
| 855 | + ) { |
| 856 | + return value; |
| 857 | + } |
| 858 | + if (Buffer.isBuffer(value)) { |
| 859 | + return { |
| 860 | + __type: 'buffer', |
| 861 | + value: Buffer.from(value).toString('base64'), |
| 862 | + }; |
| 863 | + } |
| 864 | + if (value instanceof ArrayBuffer) { |
| 865 | + return { |
| 866 | + __type: 'buffer', |
| 867 | + value: Buffer.from(new Uint8Array(value)).toString('base64'), |
| 868 | + }; |
| 869 | + } |
| 870 | + if (ArrayBuffer.isView(value)) { |
| 871 | + return { |
| 872 | + __type: 'buffer', |
| 873 | + value: Buffer.from(value.buffer, value.byteOffset, value.byteLength).toString('base64'), |
| 874 | + }; |
| 875 | + } |
| 876 | + if (typeof value === 'bigint') { |
| 877 | + return { |
| 878 | + __type: 'bigint', |
| 879 | + value: value.toString(), |
| 880 | + }; |
| 881 | + } |
| 882 | + if ( |
| 883 | + value && |
| 884 | + typeof value === 'object' && |
| 885 | + (value.type === 'public' || value.type === 'private' || value.type === 'secret') && |
| 886 | + typeof value.export === 'function' |
| 887 | + ) { |
| 888 | + return { |
| 889 | + __type: 'keyObject', |
| 890 | + value: serializeDhKeyObject(value), |
| 891 | + }; |
| 892 | + } |
| 893 | + if (Array.isArray(value)) { |
| 894 | + return value.map(serializeDhValue); |
| 895 | + } |
| 896 | + if (value && typeof value === 'object') { |
| 897 | + var output = {}; |
| 898 | + var keys = Object.keys(value); |
| 899 | + for (var i = 0; i < keys.length; i++) { |
| 900 | + if (value[keys[i]] !== undefined) { |
| 901 | + output[keys[i]] = serializeDhValue(value[keys[i]]); |
| 902 | + } |
| 903 | + } |
| 904 | + return output; |
| 905 | + } |
| 906 | + return String(value); |
| 907 | + } |
| 908 | + |
| 909 | + function restoreDhValue(value) { |
| 910 | + if (!value || typeof value !== 'object') { |
| 911 | + return value; |
| 912 | + } |
| 913 | + if (value.__type === 'buffer') { |
| 914 | + return Buffer.from(value.value, 'base64'); |
| 915 | + } |
| 916 | + if (value.__type === 'bigint') { |
| 917 | + return BigInt(value.value); |
| 918 | + } |
| 919 | + if (Array.isArray(value)) { |
| 920 | + return value.map(restoreDhValue); |
| 921 | + } |
| 922 | + var output = {}; |
| 923 | + var keys = Object.keys(value); |
| 924 | + for (var i = 0; i < keys.length; i++) { |
| 925 | + output[keys[i]] = restoreDhValue(value[keys[i]]); |
| 926 | + } |
| 927 | + return output; |
| 928 | + } |
| 929 | + |
| 930 | + function createDhSession(type, name, argsLike) { |
| 931 | + var args = []; |
| 932 | + for (var i = 0; i < argsLike.length; i++) { |
| 933 | + args.push(serializeDhValue(argsLike[i])); |
| 934 | + } |
| 935 | + return _cryptoDiffieHellmanSessionCreate.applySync(undefined, [ |
| 936 | + JSON.stringify({ |
| 937 | + type: type, |
| 938 | + name: name, |
| 939 | + args: args, |
| 940 | + }), |
| 941 | + ]); |
| 942 | + } |
| 943 | + |
| 944 | + function callDhSession(sessionId, method, argsLike) { |
| 945 | + var args = []; |
| 946 | + for (var i = 0; i < argsLike.length; i++) { |
| 947 | + args.push(serializeDhValue(argsLike[i])); |
| 948 | + } |
| 949 | + var response = JSON.parse(_cryptoDiffieHellmanSessionCall.applySync(undefined, [ |
| 950 | + sessionId, |
| 951 | + JSON.stringify({ |
| 952 | + method: method, |
| 953 | + args: args, |
| 954 | + }), |
| 955 | + ])); |
| 956 | + if (response && response.hasResult === false) { |
| 957 | + return undefined; |
| 958 | + } |
| 959 | + return restoreDhValue(response && response.result); |
| 960 | + } |
| 961 | + |
| 962 | + function SandboxDiffieHellman(sessionId) { |
| 963 | + this._sessionId = sessionId; |
| 964 | + } |
| 965 | + |
| 966 | + Object.defineProperty(SandboxDiffieHellman.prototype, 'verifyError', { |
| 967 | + get: function getVerifyError() { |
| 968 | + return callDhSession(this._sessionId, 'verifyError', []); |
| 969 | + }, |
| 970 | + }); |
| 971 | + |
| 972 | + SandboxDiffieHellman.prototype.generateKeys = function generateKeys(encoding) { |
| 973 | + if (arguments.length === 0) return callDhSession(this._sessionId, 'generateKeys', []); |
| 974 | + return callDhSession(this._sessionId, 'generateKeys', [encoding]); |
| 975 | + }; |
| 976 | + SandboxDiffieHellman.prototype.computeSecret = function computeSecret(key, inputEncoding, outputEncoding) { |
| 977 | + return callDhSession(this._sessionId, 'computeSecret', Array.prototype.slice.call(arguments)); |
| 978 | + }; |
| 979 | + SandboxDiffieHellman.prototype.getPrime = function getPrime(encoding) { |
| 980 | + if (arguments.length === 0) return callDhSession(this._sessionId, 'getPrime', []); |
| 981 | + return callDhSession(this._sessionId, 'getPrime', [encoding]); |
| 982 | + }; |
| 983 | + SandboxDiffieHellman.prototype.getGenerator = function getGenerator(encoding) { |
| 984 | + if (arguments.length === 0) return callDhSession(this._sessionId, 'getGenerator', []); |
| 985 | + return callDhSession(this._sessionId, 'getGenerator', [encoding]); |
| 986 | + }; |
| 987 | + SandboxDiffieHellman.prototype.getPublicKey = function getPublicKey(encoding) { |
| 988 | + if (arguments.length === 0) return callDhSession(this._sessionId, 'getPublicKey', []); |
| 989 | + return callDhSession(this._sessionId, 'getPublicKey', [encoding]); |
| 990 | + }; |
| 991 | + SandboxDiffieHellman.prototype.getPrivateKey = function getPrivateKey(encoding) { |
| 992 | + if (arguments.length === 0) return callDhSession(this._sessionId, 'getPrivateKey', []); |
| 993 | + return callDhSession(this._sessionId, 'getPrivateKey', [encoding]); |
| 994 | + }; |
| 995 | + SandboxDiffieHellman.prototype.setPublicKey = function setPublicKey(key, encoding) { |
| 996 | + return callDhSession(this._sessionId, 'setPublicKey', Array.prototype.slice.call(arguments)); |
| 997 | + }; |
| 998 | + SandboxDiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) { |
| 999 | + return callDhSession(this._sessionId, 'setPrivateKey', Array.prototype.slice.call(arguments)); |
| 1000 | + }; |
| 1001 | + |
| 1002 | + function SandboxECDH(sessionId) { |
| 1003 | + SandboxDiffieHellman.call(this, sessionId); |
| 1004 | + } |
| 1005 | + SandboxECDH.prototype = Object.create(SandboxDiffieHellman.prototype); |
| 1006 | + SandboxECDH.prototype.constructor = SandboxECDH; |
| 1007 | + SandboxECDH.prototype.getPublicKey = function getPublicKey(encoding, format) { |
| 1008 | + return callDhSession(this._sessionId, 'getPublicKey', Array.prototype.slice.call(arguments)); |
| 1009 | + }; |
| 1010 | + |
| 1011 | + result.createDiffieHellman = function createDiffieHellman() { |
| 1012 | + return new SandboxDiffieHellman(createDhSession('dh', undefined, arguments)); |
| 1013 | + }; |
| 1014 | + |
| 1015 | + result.getDiffieHellman = function getDiffieHellman(name) { |
| 1016 | + return new SandboxDiffieHellman(createDhSession('group', name, [])); |
| 1017 | + }; |
| 1018 | + |
| 1019 | + result.createDiffieHellmanGroup = result.getDiffieHellman; |
| 1020 | + |
| 1021 | + result.createECDH = function createECDH(curve) { |
| 1022 | + return new SandboxECDH(createDhSession('ecdh', curve, [])); |
| 1023 | + }; |
| 1024 | + |
| 1025 | + if (typeof _cryptoDiffieHellman !== 'undefined') { |
| 1026 | + result.diffieHellman = function diffieHellman(options) { |
| 1027 | + var resultJson = _cryptoDiffieHellman.applySync(undefined, [ |
| 1028 | + JSON.stringify(serializeDhValue(options)), |
| 1029 | + ]); |
| 1030 | + return restoreDhValue(JSON.parse(resultJson)); |
| 1031 | + }; |
| 1032 | + } |
| 1033 | + |
| 1034 | + result.DiffieHellman = SandboxDiffieHellman; |
| 1035 | + result.DiffieHellmanGroup = SandboxDiffieHellman; |
| 1036 | + result.ECDH = SandboxECDH; |
| 1037 | + } |
| 1038 | + |
829 | 1039 | // Overlay host-backed generateKeyPairSync/generateKeyPair and KeyObject helpers |
830 | 1040 | if (typeof _cryptoGenerateKeyPairSync !== 'undefined') { |
831 | 1041 | function restoreBridgeValue(value) { |
|
0 commit comments