node-opcua-crypto 3.0.0-beta.3 → 3.0.0
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/README.md +111 -6
- package/dist-cjs/source/crypto_utils.js +42 -107
- package/dist-cjs/source/crypto_utils.js.map +1 -1
- package/dist-cjs/source/crypto_utils2.js +106 -0
- package/dist-cjs/source/crypto_utils2.js.map +1 -0
- package/dist-cjs/source/derived_keys.js +26 -26
- package/dist-cjs/source/derived_keys.js.map +1 -1
- package/dist-cjs/source/index.js +1 -0
- package/dist-cjs/source/index.js.map +1 -1
- package/dist-cjs/source/verify_certificate_signature.js +2 -2
- package/dist-cjs/source/verify_certificate_signature.js.map +1 -1
- package/dist-esm/source/crypto_utils.js +17 -75
- package/dist-esm/source/crypto_utils.js.map +1 -1
- package/dist-esm/source/crypto_utils2.js +69 -0
- package/dist-esm/source/crypto_utils2.js.map +1 -0
- package/dist-esm/source/derived_keys.js +6 -6
- package/dist-esm/source/derived_keys.js.map +1 -1
- package/dist-esm/source/index.js +1 -0
- package/dist-esm/source/index.js.map +1 -1
- package/dist-esm/source/verify_certificate_signature.js +1 -1
- package/dist-esm/source/verify_certificate_signature.js.map +1 -1
- package/dist-types/source/common.d.ts +1 -1
- package/dist-types/source/crypto_utils.d.ts +3 -22
- package/dist-types/source/crypto_utils2.d.ts +24 -0
- package/dist-types/source/index.d.ts +1 -0
- package/package.json +2 -1
- package/web/esbuild.mjs +5 -1
- package/web/index.html +77 -9
- package/web/main.ts +90 -0
- package/web/main.js +0 -28
package/README.md
CHANGED
|
@@ -1,14 +1,119 @@
|
|
|
1
1
|
# node-opcua-crypto
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
NodeOPCUA Crypto is a powerful JavaScript module for handling security and cryptography for OPCUA. It's written in TypeScript and runs smoothly on Node.js and in the browser.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmtrends.com/node-opcua-crypto)
|
|
6
|
+
[](https://www.npmjs.com/package/node-opcua-crypto?activeTab=versions)
|
|
7
|
+
[](https://github.com/node-opcua/node-opcua-crypto/actions/workflows/main.yml)
|
|
4
8
|
[](https://codeclimate.com/github/node-opcua/node-opcua-crypto)
|
|
5
|
-
[](https://codeclimate.com/github/node-opcua/node-opcua-crypto/coverage)
|
|
6
9
|
[](https://coveralls.io/github/node-opcua/node-opcua-crypto?branch=master)
|
|
7
|
-
|
|
8
10
|
[](https://packagephobia.com/result?p=node-opcua-crypto)
|
|
9
|
-
|
|
10
11
|
[](https://app.fossa.com/projects/custom%2B20248%2Fgithub.com%2Fnode-opcua%2Fnode-opcua-crypto?ref=badge_shield)
|
|
12
|
+
<!-- [](https://codeclimate.com/github/node-opcua/node-opcua-crypto/coverage) -->
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
* Comprehensive set of cryptographic functionalities.
|
|
17
|
+
* Supports both Node.js and browser environments.
|
|
18
|
+
* Compatible with TypeScript for robust, type-safe coding.
|
|
19
|
+
* Implements advanced security standards for OPCUA.
|
|
20
|
+
|
|
21
|
+
## Getting Started
|
|
22
|
+
|
|
23
|
+
To use NodeOPCUA Crypto in your project, follow these steps:
|
|
24
|
+
|
|
25
|
+
#### Installation
|
|
26
|
+
|
|
27
|
+
``` bash
|
|
28
|
+
npm install nodeopcua-crypto
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Usage
|
|
32
|
+
|
|
33
|
+
``` bash
|
|
34
|
+
|
|
35
|
+
import { generatePrivateKey, privateKeyToPEM, CertificatePurpose, createSelfSignedCertificate } from "node-opcua-crypto";
|
|
36
|
+
|
|
37
|
+
async function demonstratePrivateKeyAndSelfSignedCertificateCreation() {
|
|
38
|
+
|
|
39
|
+
// create the Private Key
|
|
40
|
+
const privateKey = await generatePrivateKey();
|
|
41
|
+
|
|
42
|
+
// convert the private key to a PEM format
|
|
43
|
+
const { privPem } = await privateKeyToPEM(privateKey);
|
|
44
|
+
|
|
45
|
+
console.log(privPem);
|
|
46
|
+
|
|
47
|
+
// create a self-sign certificate
|
|
48
|
+
const { cert } = await createSelfSignedCertificate({
|
|
49
|
+
privateKey,
|
|
50
|
+
notAfter: new Date(2025, 1, 1),
|
|
51
|
+
notBefore: new Date(2019, 1, 1),
|
|
52
|
+
subject: "CN=Test",
|
|
53
|
+
dns: ["DNS1", "DNS2"],
|
|
54
|
+
ip: ["192.168.1.1"],
|
|
55
|
+
applicationUri: "urn:HOSTNAME:ServerDescription",
|
|
56
|
+
purpose: CertificatePurpose.ForApplication,
|
|
57
|
+
});
|
|
58
|
+
console.log(cert);
|
|
59
|
+
}
|
|
60
|
+
demonstratePrivateKeyAndSelfSignedCertificateCreation();
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Please refer to the examples directory for more specific use cases and comprehensive samples.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
## Support
|
|
13
69
|
|
|
14
|
-
|
|
70
|
+
For any inquiries or issues related to NodeOPCUA Crypto, you can contact us at contact@sterfive.com. Please note that priority support is available to NodeOPCUA Support Subscription members.
|
|
71
|
+
|
|
72
|
+
### Getting professional support
|
|
73
|
+
|
|
74
|
+
NodeOPCUA PKI is developed and maintained by sterfive.com.
|
|
75
|
+
|
|
76
|
+
To get professional support, consider subscribing to the node-opcua membership community:
|
|
77
|
+
|
|
78
|
+
[](https://support.sterfive.com)
|
|
79
|
+
|
|
80
|
+
or contact [sterfive](https://www.sterfive.com) for dedicated consulting and more advanced support.
|
|
81
|
+
|
|
82
|
+
## Contributing
|
|
83
|
+
|
|
84
|
+
We appreciate contributions from the community. To contribute:
|
|
85
|
+
|
|
86
|
+
* Fork the repository.
|
|
87
|
+
* Create a new branch.
|
|
88
|
+
* Commit your changes.
|
|
89
|
+
* Submit a pull request.
|
|
90
|
+
* Sign the CLA (Contributor Licence Agreement) form
|
|
91
|
+
|
|
92
|
+
For more detailed instructions, refer to the CONTRIBUTING.md file.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
NodeOPCUA Crypto is MIT licensed. See the LICENSE file for full license details.
|
|
97
|
+
|
|
98
|
+
Copyright © 2023 Sterfive.com.
|
|
99
|
+
|
|
100
|
+
## Disclaimer
|
|
101
|
+
|
|
102
|
+
NodeOPCUA Crypto is provided as-is, and while we strive to ensure its quality and security, Sterfive.com cannot be held liable for any damage caused directly or indirectly by the usage of this module.
|
|
103
|
+
|
|
104
|
+
Please report any issues or vulnerabilities you find via the issue tracker.
|
|
105
|
+
|
|
106
|
+
Thank you for considering NodeOPCUA Crypto for your OPCUA cryptography needs. We look forward to seeing what you build with i
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
## :heart: Supporting the development effort - Sponsors & Backers</span>
|
|
111
|
+
|
|
112
|
+
If you like node-opcua-pki and if you are relying on it in one of your projects, please consider becoming a backer and [sponsoring us](https://github.com/sponsors/node-opcua), this will help us to maintain a high-quality stack and constant evolution of this module.
|
|
113
|
+
|
|
114
|
+
If your company would like to participate and influence the development of future versions of node-opcua please contact [sterfive](mailto:contact@sterfive.com).
|
|
115
|
+
|
|
116
|
+
<!--
|
|
117
|
+
https://app.fossa.com/reports/489947c3-2e83-48e5-8351-192f553ded57
|
|
118
|
+
https://linuxctl.com/2017/02/x509-certificate-manual-signature-verification/
|
|
119
|
+
-->
|
|
@@ -3,10 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.extractPublicKeyFromCertificate = exports.extractPublicKeyFromCertificateSync = exports.
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
6
|
+
exports.extractPublicKeyFromCertificate = exports.extractPublicKeyFromCertificateSync = exports.coerceCertificatePem = exports.privateDecrypt_long = exports.publicEncrypt_long = exports.privateDecrypt = exports.publicEncrypt = exports.privateDecrypt_native = exports.publicEncrypt_native = exports.PaddingAlgorithm = exports.RSA_PKCS1_PADDING = exports.RSA_PKCS1_OAEP_PADDING = exports.makeSHA1Thumbprint = exports.verifyMessageChunkSignature = exports.makeMessageChunkSignature = exports.hexDump = exports.convertPEMtoDER = exports.toPem = exports.identifyPemType = void 0;
|
|
7
|
+
var constants_1 = __importDefault(require("constants"));
|
|
8
|
+
var assert_1 = __importDefault(require("assert"));
|
|
9
|
+
var crypto_1 = require("crypto");
|
|
10
10
|
var hexy = require("hexy").hexy;
|
|
11
11
|
var buffer_utils_js_1 = require("./buffer_utils.js");
|
|
12
12
|
var crypto_explore_certificate_js_1 = require("./crypto_explore_certificate.js");
|
|
@@ -21,42 +21,16 @@ function identifyPemType(rawKey) {
|
|
|
21
21
|
return !match ? undefined : match[2];
|
|
22
22
|
}
|
|
23
23
|
exports.identifyPemType = identifyPemType;
|
|
24
|
-
function convertPEMtoDER(raw_key) {
|
|
25
|
-
var match;
|
|
26
|
-
var pemType;
|
|
27
|
-
var base64str;
|
|
28
|
-
var parts = [];
|
|
29
|
-
PEM_REGEX.lastIndex = 0;
|
|
30
|
-
while ((match = PEM_REGEX.exec(raw_key)) !== null) {
|
|
31
|
-
pemType = match[2];
|
|
32
|
-
base64str = match[3];
|
|
33
|
-
base64str = base64str.replace(/\r?\n/g, "");
|
|
34
|
-
parts.push(Buffer.from(base64str, "base64"));
|
|
35
|
-
}
|
|
36
|
-
return (0, crypto_explore_certificate_js_1.combine_der)(parts);
|
|
37
|
-
}
|
|
38
|
-
exports.convertPEMtoDER = convertPEMtoDER;
|
|
39
24
|
function toPem(raw_key, pem) {
|
|
40
|
-
(0,
|
|
41
|
-
(0,
|
|
42
|
-
if (raw_key instanceof node_crypto_1.KeyObject) {
|
|
43
|
-
if (pem === "RSA PRIVATE KEY") {
|
|
44
|
-
return raw_key.export({ format: "pem", type: "pkcs1" }).toString();
|
|
45
|
-
}
|
|
46
|
-
else if (pem === "PRIVATE KEY") {
|
|
47
|
-
return raw_key.export({ format: "pem", type: "pkcs8" }).toString();
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
throw new Error("Unsupported case!");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
25
|
+
(0, assert_1.default)(raw_key, "expecting a key");
|
|
26
|
+
(0, assert_1.default)(typeof pem === "string");
|
|
53
27
|
var pemType = identifyPemType(raw_key);
|
|
54
28
|
if (pemType) {
|
|
55
29
|
return raw_key instanceof Buffer ? raw_key.toString("utf8") : raw_key;
|
|
56
30
|
}
|
|
57
31
|
else {
|
|
58
32
|
pemType = pem;
|
|
59
|
-
(0,
|
|
33
|
+
(0, assert_1.default)(["CERTIFICATE REQUEST", "CERTIFICATE", "RSA PRIVATE KEY", "PUBLIC KEY", "X509 CRL"].indexOf(pemType) >= 0);
|
|
60
34
|
var b = raw_key.toString("base64");
|
|
61
35
|
var str = "-----BEGIN " + pemType + "-----\n";
|
|
62
36
|
while (b.length) {
|
|
@@ -69,6 +43,21 @@ function toPem(raw_key, pem) {
|
|
|
69
43
|
}
|
|
70
44
|
}
|
|
71
45
|
exports.toPem = toPem;
|
|
46
|
+
function convertPEMtoDER(raw_key) {
|
|
47
|
+
var match;
|
|
48
|
+
var pemType;
|
|
49
|
+
var base64str;
|
|
50
|
+
var parts = [];
|
|
51
|
+
PEM_REGEX.lastIndex = 0;
|
|
52
|
+
while ((match = PEM_REGEX.exec(raw_key)) !== null) {
|
|
53
|
+
pemType = match[2];
|
|
54
|
+
base64str = match[3];
|
|
55
|
+
base64str = base64str.replace(/\r?\n/g, "");
|
|
56
|
+
parts.push(Buffer.from(base64str, "base64"));
|
|
57
|
+
}
|
|
58
|
+
return (0, crypto_explore_certificate_js_1.combine_der)(parts);
|
|
59
|
+
}
|
|
60
|
+
exports.convertPEMtoDER = convertPEMtoDER;
|
|
72
61
|
function hexDump(buffer, width) {
|
|
73
62
|
if (!buffer) {
|
|
74
63
|
return "<>";
|
|
@@ -83,43 +72,43 @@ function hexDump(buffer, width) {
|
|
|
83
72
|
}
|
|
84
73
|
exports.hexDump = hexDump;
|
|
85
74
|
function makeMessageChunkSignature(chunk, options) {
|
|
86
|
-
var signer = (0,
|
|
75
|
+
var signer = (0, crypto_1.createSign)(options.algorithm);
|
|
87
76
|
signer.update(chunk);
|
|
88
77
|
var signature = signer.sign(options.privateKey);
|
|
89
|
-
(0,
|
|
78
|
+
(0, assert_1.default)(!options.signatureLength || signature.length === options.signatureLength);
|
|
90
79
|
return signature;
|
|
91
80
|
}
|
|
92
81
|
exports.makeMessageChunkSignature = makeMessageChunkSignature;
|
|
93
82
|
function verifyMessageChunkSignature(blockToVerify, signature, options) {
|
|
94
|
-
(0,
|
|
95
|
-
(0,
|
|
96
|
-
(0,
|
|
97
|
-
(0,
|
|
98
|
-
var verify = (0,
|
|
83
|
+
(0, assert_1.default)(blockToVerify instanceof Buffer);
|
|
84
|
+
(0, assert_1.default)(signature instanceof Buffer);
|
|
85
|
+
(0, assert_1.default)(typeof options.publicKey === "string");
|
|
86
|
+
(0, assert_1.default)(identifyPemType(options.publicKey));
|
|
87
|
+
var verify = (0, crypto_1.createVerify)(options.algorithm);
|
|
99
88
|
verify.update(blockToVerify);
|
|
100
89
|
return verify.verify(options.publicKey, signature);
|
|
101
90
|
}
|
|
102
91
|
exports.verifyMessageChunkSignature = verifyMessageChunkSignature;
|
|
103
92
|
function makeSHA1Thumbprint(buffer) {
|
|
104
|
-
return (0,
|
|
93
|
+
return (0, crypto_1.createHash)("sha1").update(buffer).digest();
|
|
105
94
|
}
|
|
106
95
|
exports.makeSHA1Thumbprint = makeSHA1Thumbprint;
|
|
107
|
-
exports.RSA_PKCS1_OAEP_PADDING =
|
|
108
|
-
exports.RSA_PKCS1_PADDING =
|
|
96
|
+
exports.RSA_PKCS1_OAEP_PADDING = constants_1.default.RSA_PKCS1_OAEP_PADDING;
|
|
97
|
+
exports.RSA_PKCS1_PADDING = constants_1.default.RSA_PKCS1_PADDING;
|
|
109
98
|
var PaddingAlgorithm;
|
|
110
99
|
(function (PaddingAlgorithm) {
|
|
111
100
|
PaddingAlgorithm[PaddingAlgorithm["RSA_PKCS1_OAEP_PADDING"] = 4] = "RSA_PKCS1_OAEP_PADDING";
|
|
112
101
|
PaddingAlgorithm[PaddingAlgorithm["RSA_PKCS1_PADDING"] = 1] = "RSA_PKCS1_PADDING";
|
|
113
102
|
})(PaddingAlgorithm = exports.PaddingAlgorithm || (exports.PaddingAlgorithm = {}));
|
|
114
|
-
(0,
|
|
115
|
-
(0,
|
|
103
|
+
(0, assert_1.default)(PaddingAlgorithm.RSA_PKCS1_OAEP_PADDING === constants_1.default.RSA_PKCS1_OAEP_PADDING);
|
|
104
|
+
(0, assert_1.default)(PaddingAlgorithm.RSA_PKCS1_PADDING === constants_1.default.RSA_PKCS1_PADDING);
|
|
116
105
|
function publicEncrypt_native(buffer, publicKey, algorithm) {
|
|
117
106
|
if (algorithm === undefined) {
|
|
118
107
|
algorithm = PaddingAlgorithm.RSA_PKCS1_PADDING;
|
|
119
108
|
}
|
|
120
|
-
(0,
|
|
121
|
-
(0,
|
|
122
|
-
return (0,
|
|
109
|
+
(0, assert_1.default)(algorithm === exports.RSA_PKCS1_PADDING || algorithm === exports.RSA_PKCS1_OAEP_PADDING);
|
|
110
|
+
(0, assert_1.default)(buffer instanceof Buffer, "Expecting a buffer");
|
|
111
|
+
return (0, crypto_1.publicEncrypt)({
|
|
123
112
|
key: publicKey,
|
|
124
113
|
padding: algorithm,
|
|
125
114
|
}, buffer);
|
|
@@ -129,10 +118,10 @@ function privateDecrypt_native(buffer, privateKey, algorithm) {
|
|
|
129
118
|
if (algorithm === undefined) {
|
|
130
119
|
algorithm = PaddingAlgorithm.RSA_PKCS1_PADDING;
|
|
131
120
|
}
|
|
132
|
-
(0,
|
|
133
|
-
(0,
|
|
121
|
+
(0, assert_1.default)(algorithm === exports.RSA_PKCS1_PADDING || algorithm === exports.RSA_PKCS1_OAEP_PADDING);
|
|
122
|
+
(0, assert_1.default)(buffer instanceof Buffer, "Expecting a buffer");
|
|
134
123
|
try {
|
|
135
|
-
return (0,
|
|
124
|
+
return (0, crypto_1.privateDecrypt)({
|
|
136
125
|
key: privateKey,
|
|
137
126
|
padding: algorithm,
|
|
138
127
|
}, buffer);
|
|
@@ -186,69 +175,15 @@ function coerceCertificatePem(certificate) {
|
|
|
186
175
|
if (certificate instanceof Buffer) {
|
|
187
176
|
certificate = toPem(certificate, "CERTIFICATE");
|
|
188
177
|
}
|
|
189
|
-
(0,
|
|
178
|
+
(0, assert_1.default)(typeof certificate === "string");
|
|
190
179
|
return certificate;
|
|
191
180
|
}
|
|
192
181
|
exports.coerceCertificatePem = coerceCertificatePem;
|
|
193
|
-
function coercePublicKeyPem(publicKey) {
|
|
194
|
-
if (publicKey instanceof node_crypto_1.KeyObject) {
|
|
195
|
-
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
196
|
-
}
|
|
197
|
-
(0, node_assert_1.default)(typeof publicKey === "string");
|
|
198
|
-
return publicKey;
|
|
199
|
-
}
|
|
200
|
-
exports.coercePublicKeyPem = coercePublicKeyPem;
|
|
201
|
-
function coerceRsaPublicKeyPem(publicKey) {
|
|
202
|
-
if (publicKey instanceof node_crypto_1.KeyObject) {
|
|
203
|
-
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
204
|
-
}
|
|
205
|
-
(0, node_assert_1.default)(typeof publicKey === "string");
|
|
206
|
-
return publicKey;
|
|
207
|
-
}
|
|
208
|
-
exports.coerceRsaPublicKeyPem = coerceRsaPublicKeyPem;
|
|
209
|
-
function coercePrivateKey(privateKey) {
|
|
210
|
-
if (typeof privateKey === "string") {
|
|
211
|
-
return (0, node_crypto_1.createPrivateKey)(privateKey);
|
|
212
|
-
}
|
|
213
|
-
return privateKey;
|
|
214
|
-
}
|
|
215
|
-
exports.coercePrivateKey = coercePrivateKey;
|
|
216
|
-
function coercePrivateKeyPem(privateKey) {
|
|
217
|
-
if (privateKey instanceof Buffer) {
|
|
218
|
-
var o = (0, node_crypto_1.createPrivateKey)({ key: privateKey, format: "der", type: "pkcs1" });
|
|
219
|
-
var e = o.export({ format: "der", type: "pkcs1" });
|
|
220
|
-
privateKey = toPem(e, "RSA PRIVATE KEY");
|
|
221
|
-
}
|
|
222
|
-
(0, node_assert_1.default)(typeof privateKey === "string");
|
|
223
|
-
return privateKey;
|
|
224
|
-
}
|
|
225
|
-
exports.coercePrivateKeyPem = coercePrivateKeyPem;
|
|
226
|
-
function rsaLengthPrivateKey(key) {
|
|
227
|
-
key = coercePrivateKey(key);
|
|
228
|
-
var key2 = key.export({ type: "pkcs1", format: "pem" }).toString();
|
|
229
|
-
var a = jsrsasign.KEYUTIL.getKey(key2);
|
|
230
|
-
return a.n.toString(16).length / 2;
|
|
231
|
-
}
|
|
232
|
-
exports.rsaLengthPrivateKey = rsaLengthPrivateKey;
|
|
233
|
-
function rsaLengthPublicKey(key) {
|
|
234
|
-
key = coercePublicKeyPem(key);
|
|
235
|
-
(0, node_assert_1.default)(typeof key === "string");
|
|
236
|
-
var a = jsrsasign.KEYUTIL.getKey(key);
|
|
237
|
-
return a.n.toString(16).length / 2;
|
|
238
|
-
}
|
|
239
|
-
exports.rsaLengthPublicKey = rsaLengthPublicKey;
|
|
240
|
-
function rsaLengthRsaPublicKey(key) {
|
|
241
|
-
key = coerceRsaPublicKeyPem(key);
|
|
242
|
-
(0, node_assert_1.default)(typeof key === "string");
|
|
243
|
-
var a = jsrsasign.KEYUTIL.getKey(key);
|
|
244
|
-
return a.n.toString(16).length / 2;
|
|
245
|
-
}
|
|
246
|
-
exports.rsaLengthRsaPublicKey = rsaLengthRsaPublicKey;
|
|
247
182
|
function extractPublicKeyFromCertificateSync(certificate) {
|
|
248
183
|
certificate = coerceCertificatePem(certificate);
|
|
249
184
|
var key = jsrsasign.KEYUTIL.getKey(certificate);
|
|
250
185
|
var publicKeyAsPem = jsrsasign.KEYUTIL.getPEM(key);
|
|
251
|
-
(0,
|
|
186
|
+
(0, assert_1.default)(typeof publicKeyAsPem === "string");
|
|
252
187
|
return publicKeyAsPem;
|
|
253
188
|
}
|
|
254
189
|
exports.extractPublicKeyFromCertificateSync = extractPublicKeyFromCertificateSync;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto_utils.js","sourceRoot":"","sources":["../../source/crypto_utils.ts"],"names":[],"mappings":";;;;;;AA2BA,
|
|
1
|
+
{"version":3,"file":"crypto_utils.js","sourceRoot":"","sources":["../../source/crypto_utils.ts"],"names":[],"mappings":";;;;;;AA2BA,wDAAkC;AAClC,kDAA4B;AAC5B,iCAOgB;AAER,IAAA,IAAI,GAAK,OAAO,CAAC,MAAM,CAAC,KAApB,CAAqB;AAEjC,qDAAkE;AAElE,iFAA8D;AAE9D,IAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAEvC,IAAM,SAAS,GAAG,gFAAgF,CAAC;AAEnG,IAAM,cAAc,GAAG,0BAA0B,CAAC;AASlD,SAAgB,eAAe,CAAC,MAAuB;IACnD,IAAI,MAAM,YAAY,MAAM,EAAE;QAC1B,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;KACpC;IACD,IAAM,KAAK,GAAG,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACzC,CAAC;AAND,0CAMC;AAGD,SAAgB,KAAK,CAAC,OAAwB,EAAE,GAAW;IACvD,IAAA,gBAAM,EAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;IACnC,IAAA,gBAAM,EAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC;IAChC,IAAI,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IACvC,IAAI,OAAO,EAAE;QACT,OAAO,OAAO,YAAY,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,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,CAAC;QACT,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;AACP,CAAC;AAdD,0EAcC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.rsaLengthRsaPublicKey = exports.rsaLengthPublicKey = exports.coerceRsaPublicKeyPem = exports.coercePublicKeyPem = exports.toPem2 = exports.rsaLengthPrivateKey = exports.coercePrivateKeyPem = exports.coercePrivateKey = void 0;
|
|
30
|
+
var assert_1 = __importDefault(require("assert"));
|
|
31
|
+
var crypto = __importStar(require("crypto"));
|
|
32
|
+
var createPrivateKey = crypto.createPrivateKey;
|
|
33
|
+
var crypto_utils_js_1 = require("./crypto_utils.js");
|
|
34
|
+
var jsrsasign = require("jsrsasign");
|
|
35
|
+
function coercePrivateKey(privateKey) {
|
|
36
|
+
if (typeof privateKey === "string") {
|
|
37
|
+
return createPrivateKey(privateKey);
|
|
38
|
+
}
|
|
39
|
+
return privateKey;
|
|
40
|
+
}
|
|
41
|
+
exports.coercePrivateKey = coercePrivateKey;
|
|
42
|
+
function coercePrivateKeyPem(privateKey) {
|
|
43
|
+
if (privateKey instanceof Buffer) {
|
|
44
|
+
var o = createPrivateKey({ key: privateKey, format: "der", type: "pkcs1" });
|
|
45
|
+
var e = o.export({ format: "der", type: "pkcs1" });
|
|
46
|
+
privateKey = (0, crypto_utils_js_1.toPem)(e, "RSA PRIVATE KEY");
|
|
47
|
+
}
|
|
48
|
+
(0, assert_1.default)(typeof privateKey === "string");
|
|
49
|
+
return privateKey;
|
|
50
|
+
}
|
|
51
|
+
exports.coercePrivateKeyPem = coercePrivateKeyPem;
|
|
52
|
+
function rsaLengthPrivateKey(key) {
|
|
53
|
+
key = coercePrivateKey(key);
|
|
54
|
+
var key2 = key.export({ type: "pkcs1", format: "pem" }).toString();
|
|
55
|
+
var a = jsrsasign.KEYUTIL.getKey(key2);
|
|
56
|
+
return a.n.toString(16).length / 2;
|
|
57
|
+
}
|
|
58
|
+
exports.rsaLengthPrivateKey = rsaLengthPrivateKey;
|
|
59
|
+
function toPem2(raw_key, pem) {
|
|
60
|
+
(0, assert_1.default)(raw_key, "expecting a key");
|
|
61
|
+
(0, assert_1.default)(typeof pem === "string");
|
|
62
|
+
if (raw_key instanceof crypto.KeyObject) {
|
|
63
|
+
if (pem === "RSA PRIVATE KEY") {
|
|
64
|
+
return raw_key.export({ format: "pem", type: "pkcs1" }).toString();
|
|
65
|
+
}
|
|
66
|
+
else if (pem === "PRIVATE KEY") {
|
|
67
|
+
return raw_key.export({ format: "pem", type: "pkcs8" }).toString();
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw new Error("Unsupported case!");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return (0, crypto_utils_js_1.toPem)(raw_key, pem);
|
|
74
|
+
}
|
|
75
|
+
exports.toPem2 = toPem2;
|
|
76
|
+
function coercePublicKeyPem(publicKey) {
|
|
77
|
+
if (publicKey instanceof crypto.KeyObject) {
|
|
78
|
+
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
79
|
+
}
|
|
80
|
+
(0, assert_1.default)(typeof publicKey === "string");
|
|
81
|
+
return publicKey;
|
|
82
|
+
}
|
|
83
|
+
exports.coercePublicKeyPem = coercePublicKeyPem;
|
|
84
|
+
function coerceRsaPublicKeyPem(publicKey) {
|
|
85
|
+
if (publicKey instanceof crypto.KeyObject) {
|
|
86
|
+
return publicKey.export({ format: "pem", type: "spki" }).toString();
|
|
87
|
+
}
|
|
88
|
+
(0, assert_1.default)(typeof publicKey === "string");
|
|
89
|
+
return publicKey;
|
|
90
|
+
}
|
|
91
|
+
exports.coerceRsaPublicKeyPem = coerceRsaPublicKeyPem;
|
|
92
|
+
function rsaLengthPublicKey(key) {
|
|
93
|
+
key = coercePublicKeyPem(key);
|
|
94
|
+
(0, assert_1.default)(typeof key === "string");
|
|
95
|
+
var a = jsrsasign.KEYUTIL.getKey(key);
|
|
96
|
+
return a.n.toString(16).length / 2;
|
|
97
|
+
}
|
|
98
|
+
exports.rsaLengthPublicKey = rsaLengthPublicKey;
|
|
99
|
+
function rsaLengthRsaPublicKey(key) {
|
|
100
|
+
key = coerceRsaPublicKeyPem(key);
|
|
101
|
+
(0, assert_1.default)(typeof key === "string");
|
|
102
|
+
var a = jsrsasign.KEYUTIL.getKey(key);
|
|
103
|
+
return a.n.toString(16).length / 2;
|
|
104
|
+
}
|
|
105
|
+
exports.rsaLengthRsaPublicKey = rsaLengthRsaPublicKey;
|
|
106
|
+
//# sourceMappingURL=crypto_utils2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto_utils2.js","sourceRoot":"","sources":["../../source/crypto_utils2.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,kDAA4B;AAO5B,6CAAkC;AAC1B,IAAA,gBAAgB,GAAK,MAAM,iBAAX,CAAY;AAIpC,qDAA0C;AAE1C,IAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAEvC,SAAgB,gBAAgB,CAAC,UAAsC;IACnE,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE;QAChC,OAAO,gBAAgB,CAAC,UAAU,CAAC,CAAC;KACvC;IACD,OAAO,UAAU,CAAC;AACtB,CAAC;AALD,4CAKC;AAED,SAAgB,mBAAmB,CAAC,UAAsC;IACtE,IAAI,UAAU,YAAY,MAAM,EAAE;QAC9B,IAAM,CAAC,GAAG,gBAAgB,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QAE9E,IAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QACrD,UAAU,GAAG,IAAA,uBAAK,EAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC;KAC5C;IACD,IAAA,gBAAM,EAAC,OAAO,UAAU,KAAK,QAAQ,CAAC,CAAC;IACvC,OAAO,UAAU,CAAC;AACtB,CAAC;AATD,kDASC;AAOD,SAAgB,mBAAmB,CAAC,GAA+B;IAC/D,GAAG,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAK5B,IAAM,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,KAA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B,CAAC;AAND,gDAMC;AACD,SAAgB,qBAAqB,CAAC,SAAmC;IACrE,IAAI,SAAS,YAAY,MAAM,CAAC,SAAS,EAAE;QACvC,OAAO,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC;KACvE;IACD,IAAA,gBAAM,EAAC,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC;IACtC,OAAO,SAAS,CAAC;AACrB,CAAC;AAND,sDAMC;AAED,SAAgB,kBAAkB,CAAC,GAA6B;IAC5D,GAAG,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAA,gBAAM,EAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC;IAChC,IAAM,CAAC,GAAG,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACxC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AACvC,CAAC;AALD,gDAKC;AACD,SAAgB,qBAAqB,CAAC,GAA6B;IAC/D,GAAG,GAAG,qBAAqB,CAAC,GAAG,CAAC,CAAC;IACjC,IAAA,gBAAM,EAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC;IAChC,IAAM,CAAC,GAAG,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACxC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AACvC,CAAC;AALD,sDAKC"}
|
|
@@ -4,20 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.verifyChunkSignatureWithDerivedKeys = exports.makeMessageChunkSignatureWithDerivedKeys = exports.decryptBufferWithDerivedKeys = exports.encryptBufferWithDerivedKeys = exports.computePaddingFooter = exports.verifyChunkSignature = exports.removePadding = exports.reduceLength = exports.computeDerivedKeys = exports.makePseudoRandomBuffer = void 0;
|
|
7
|
-
var
|
|
8
|
-
var
|
|
7
|
+
var assert_1 = __importDefault(require("assert"));
|
|
8
|
+
var crypto_1 = require("crypto");
|
|
9
9
|
var buffer_utils_js_1 = require("./buffer_utils.js");
|
|
10
10
|
var crypto_utils_js_1 = require("./crypto_utils.js");
|
|
11
11
|
var explore_certificate_js_1 = require("./explore_certificate.js");
|
|
12
12
|
function HMAC_HASH(sha1or256, secret, message) {
|
|
13
|
-
return (0,
|
|
13
|
+
return (0, crypto_1.createHmac)(sha1or256, secret).update(message).digest();
|
|
14
14
|
}
|
|
15
15
|
function plus(buf1, buf2) {
|
|
16
16
|
return Buffer.concat([buf1, buf2]);
|
|
17
17
|
}
|
|
18
18
|
function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
|
|
19
|
-
(0,
|
|
20
|
-
(0,
|
|
19
|
+
(0, assert_1.default)(seed instanceof Buffer);
|
|
20
|
+
(0, assert_1.default)(sha1or256 === "SHA1" || sha1or256 === "SHA256");
|
|
21
21
|
var a = [];
|
|
22
22
|
a[0] = seed;
|
|
23
23
|
var index = 1;
|
|
@@ -31,12 +31,12 @@ function makePseudoRandomBuffer(secret, seed, minLength, sha1or256) {
|
|
|
31
31
|
}
|
|
32
32
|
exports.makePseudoRandomBuffer = makePseudoRandomBuffer;
|
|
33
33
|
function computeDerivedKeys(secret, seed, options) {
|
|
34
|
-
(0,
|
|
35
|
-
(0,
|
|
36
|
-
(0,
|
|
37
|
-
(0,
|
|
34
|
+
(0, assert_1.default)(Number.isFinite(options.signatureLength));
|
|
35
|
+
(0, assert_1.default)(Number.isFinite(options.encryptingKeyLength));
|
|
36
|
+
(0, assert_1.default)(Number.isFinite(options.encryptingBlockSize));
|
|
37
|
+
(0, assert_1.default)(typeof options.algorithm === "string");
|
|
38
38
|
options.sha1or256 = options.sha1or256 || "SHA1";
|
|
39
|
-
(0,
|
|
39
|
+
(0, assert_1.default)(typeof options.sha1or256 === "string");
|
|
40
40
|
var offset1 = options.signingKeyLength;
|
|
41
41
|
var offset2 = offset1 + options.encryptingKeyLength;
|
|
42
42
|
var minLength = offset2 + options.encryptingBlockSize;
|
|
@@ -64,7 +64,7 @@ function removePadding(buffer) {
|
|
|
64
64
|
}
|
|
65
65
|
exports.removePadding = removePadding;
|
|
66
66
|
function verifyChunkSignature(chunk, options) {
|
|
67
|
-
(0,
|
|
67
|
+
(0, assert_1.default)(chunk instanceof Buffer);
|
|
68
68
|
var signatureLength = options.signatureLength || 0;
|
|
69
69
|
if (signatureLength === 0) {
|
|
70
70
|
var cert = (0, explore_certificate_js_1.exploreCertificateInfo)(options.publicKey);
|
|
@@ -76,7 +76,7 @@ function verifyChunkSignature(chunk, options) {
|
|
|
76
76
|
}
|
|
77
77
|
exports.verifyChunkSignature = verifyChunkSignature;
|
|
78
78
|
function computePaddingFooter(buffer, derivedKeys) {
|
|
79
|
-
(0,
|
|
79
|
+
(0, assert_1.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "encryptingBlockSize"));
|
|
80
80
|
var paddingSize = derivedKeys.encryptingBlockSize - ((buffer.length + 1) % derivedKeys.encryptingBlockSize);
|
|
81
81
|
var padding = (0, buffer_utils_js_1.createFastUninitializedBuffer)(paddingSize + 1);
|
|
82
82
|
padding.fill(paddingSize);
|
|
@@ -84,16 +84,16 @@ function computePaddingFooter(buffer, derivedKeys) {
|
|
|
84
84
|
}
|
|
85
85
|
exports.computePaddingFooter = computePaddingFooter;
|
|
86
86
|
function derivedKeys_algorithm(derivedKeys) {
|
|
87
|
-
(0,
|
|
87
|
+
(0, assert_1.default)(Object.prototype.hasOwnProperty.call(derivedKeys, "algorithm"));
|
|
88
88
|
var algorithm = derivedKeys.algorithm || "aes-128-cbc";
|
|
89
|
-
(0,
|
|
89
|
+
(0, assert_1.default)(algorithm === "aes-128-cbc" || algorithm === "aes-256-cbc");
|
|
90
90
|
return algorithm;
|
|
91
91
|
}
|
|
92
92
|
function encryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
93
93
|
var algorithm = derivedKeys_algorithm(derivedKeys);
|
|
94
94
|
var key = derivedKeys.encryptingKey;
|
|
95
95
|
var initVector = derivedKeys.initializationVector;
|
|
96
|
-
var cipher = (0,
|
|
96
|
+
var cipher = (0, crypto_1.createCipheriv)(algorithm, key, initVector);
|
|
97
97
|
cipher.setAutoPadding(false);
|
|
98
98
|
var encrypted_chunks = [];
|
|
99
99
|
encrypted_chunks.push(cipher.update(buffer));
|
|
@@ -105,7 +105,7 @@ function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
|
105
105
|
var algorithm = derivedKeys_algorithm(derivedKeys);
|
|
106
106
|
var key = derivedKeys.encryptingKey;
|
|
107
107
|
var initVector = derivedKeys.initializationVector;
|
|
108
|
-
var cipher = (0,
|
|
108
|
+
var cipher = (0, crypto_1.createDecipheriv)(algorithm, key, initVector);
|
|
109
109
|
cipher.setAutoPadding(false);
|
|
110
110
|
var decrypted_chunks = [];
|
|
111
111
|
decrypted_chunks.push(cipher.update(buffer));
|
|
@@ -114,20 +114,20 @@ function decryptBufferWithDerivedKeys(buffer, derivedKeys) {
|
|
|
114
114
|
}
|
|
115
115
|
exports.decryptBufferWithDerivedKeys = decryptBufferWithDerivedKeys;
|
|
116
116
|
function makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys) {
|
|
117
|
-
(0,
|
|
118
|
-
(0,
|
|
119
|
-
(0,
|
|
120
|
-
(0,
|
|
121
|
-
var signature = (0,
|
|
122
|
-
(0,
|
|
117
|
+
(0, assert_1.default)(message instanceof Buffer);
|
|
118
|
+
(0, assert_1.default)(derivedKeys.signingKey instanceof Buffer);
|
|
119
|
+
(0, assert_1.default)(typeof derivedKeys.sha1or256 === "string");
|
|
120
|
+
(0, assert_1.default)(derivedKeys.sha1or256 === "SHA1" || derivedKeys.sha1or256 === "SHA256");
|
|
121
|
+
var signature = (0, crypto_1.createHmac)(derivedKeys.sha1or256, derivedKeys.signingKey).update(message).digest();
|
|
122
|
+
(0, assert_1.default)(signature.length === derivedKeys.signatureLength);
|
|
123
123
|
return signature;
|
|
124
124
|
}
|
|
125
125
|
exports.makeMessageChunkSignatureWithDerivedKeys = makeMessageChunkSignatureWithDerivedKeys;
|
|
126
126
|
function verifyChunkSignatureWithDerivedKeys(chunk, derivedKeys) {
|
|
127
|
-
var message = chunk.
|
|
128
|
-
var
|
|
129
|
-
var
|
|
130
|
-
return
|
|
127
|
+
var message = chunk.subarray(0, chunk.length - derivedKeys.signatureLength);
|
|
128
|
+
var expectedSignature = chunk.subarray(chunk.length - derivedKeys.signatureLength);
|
|
129
|
+
var computedSignature = makeMessageChunkSignatureWithDerivedKeys(message, derivedKeys);
|
|
130
|
+
return computedSignature.toString("hex") === expectedSignature.toString("hex");
|
|
131
131
|
}
|
|
132
132
|
exports.verifyChunkSignatureWithDerivedKeys = verifyChunkSignatureWithDerivedKeys;
|
|
133
133
|
//# sourceMappingURL=derived_keys.js.map
|