node-opcua-crypto 1.11.0 → 2.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.
Files changed (108) hide show
  1. package/.fossa.yml +18 -18
  2. package/.github/FUNDING.yml +12 -12
  3. package/.github/workflows/main.yml +32 -32
  4. package/.prettierrc.js +6 -6
  5. package/LICENSE +23 -22
  6. package/README.md +14 -14
  7. package/dist/source/asn1.d.ts +73 -73
  8. package/dist/source/asn1.js +359 -359
  9. package/dist/source/buffer_utils.d.ts +6 -6
  10. package/dist/source/buffer_utils.js +21 -21
  11. package/dist/source/certificate_matches_private_key.d.ts +2 -0
  12. package/dist/source/certificate_matches_private_key.js +22 -0
  13. package/dist/source/certificate_matches_private_key.js.map +1 -0
  14. package/dist/source/common.d.ts +12 -12
  15. package/dist/source/common.js +2 -2
  16. package/dist/source/crypto_explore_certificate.d.ts +107 -107
  17. package/dist/source/crypto_explore_certificate.js +601 -600
  18. package/dist/source/crypto_explore_certificate.js.map +1 -1
  19. package/dist/source/crypto_utils.d.ts +80 -78
  20. package/dist/source/crypto_utils.js +312 -280
  21. package/dist/source/crypto_utils.js.map +1 -1
  22. package/dist/source/derived_keys.d.ts +72 -72
  23. package/dist/source/derived_keys.js +248 -248
  24. package/dist/source/explore_certificate.d.ts +30 -30
  25. package/dist/source/explore_certificate.js +43 -43
  26. package/dist/source/explore_certificate_revocation_list.d.ts +28 -28
  27. package/dist/source/explore_certificate_revocation_list.js +69 -69
  28. package/dist/source/explore_certificate_signing_request.d.ts +13 -13
  29. package/dist/source/explore_certificate_signing_request.js +44 -44
  30. package/dist/source/explore_private_key.d.ts +29 -29
  31. package/dist/source/explore_private_key.js +96 -96
  32. package/dist/source/explore_private_key.js.map +1 -1
  33. package/dist/source/index.d.ts +13 -13
  34. package/dist/source/index.js +29 -29
  35. package/dist/source/oid_map.d.ts +7 -7
  36. package/dist/source/oid_map.js +303 -303
  37. package/dist/source/public_private_match.d.ts +3 -3
  38. package/dist/source/public_private_match.js +37 -16
  39. package/dist/source/public_private_match.js.map +1 -1
  40. package/dist/source/verify_certificate_signature.d.ts +10 -10
  41. package/dist/source/verify_certificate_signature.js +101 -101
  42. package/dist/source_nodejs/index.d.ts +3 -3
  43. package/dist/source_nodejs/index.js +19 -19
  44. package/dist/source_nodejs/read.d.ts +29 -29
  45. package/dist/source_nodejs/read.js +94 -94
  46. package/dist/source_nodejs/read.js.map +1 -1
  47. package/dist/source_nodejs/read_certificate_revocation_list.d.ts +2 -2
  48. package/dist/source_nodejs/read_certificate_revocation_list.js +27 -27
  49. package/dist/source_nodejs/read_certificate_signing_request.d.ts +3 -3
  50. package/dist/source_nodejs/read_certificate_signing_request.js +27 -27
  51. package/index.d.ts +2 -2
  52. package/index.js +4 -4
  53. package/index_web.js +3 -3
  54. package/package.json +12 -12
  55. package/source/asn1.ts +404 -404
  56. package/source/buffer_utils.ts +18 -18
  57. package/source/common.ts +13 -13
  58. package/source/crypto_explore_certificate.ts +764 -763
  59. package/source/crypto_utils.ts +359 -321
  60. package/source/derived_keys.ts +287 -287
  61. package/source/explore_certificate.ts +66 -66
  62. package/source/explore_certificate_revocation_list.ts +122 -122
  63. package/source/explore_certificate_signing_request.ts +58 -58
  64. package/source/explore_private_key.ts +121 -121
  65. package/source/index.ts +13 -13
  66. package/source/oid_map.ts +310 -310
  67. package/source/public_private_match.ts +41 -18
  68. package/source/verify_certificate_signature.ts +105 -105
  69. package/source_nodejs/index.ts +2 -2
  70. package/source_nodejs/read.ts +95 -95
  71. package/source_nodejs/read_certificate_revocation_list.ts +14 -14
  72. package/source_nodejs/read_certificate_signing_request.ts +17 -17
  73. package/test_certificate.ts +34 -34
  74. package/tsconfig.json +18 -18
  75. package/tslint.json +34 -34
  76. package/dist/asn1.d.ts +0 -69
  77. package/dist/asn1.js +0 -349
  78. package/dist/asn1.js.map +0 -1
  79. package/dist/buffer_utils.d.ts +0 -6
  80. package/dist/buffer_utils.js +0 -22
  81. package/dist/buffer_utils.js.map +0 -1
  82. package/dist/common.d.ts +0 -11
  83. package/dist/common.js +0 -3
  84. package/dist/common.js.map +0 -1
  85. package/dist/crypto_explore_certificate.d.ts +0 -95
  86. package/dist/crypto_explore_certificate.js +0 -547
  87. package/dist/crypto_explore_certificate.js.map +0 -1
  88. package/dist/crypto_utils.d.ts +0 -106
  89. package/dist/crypto_utils.js +0 -370
  90. package/dist/crypto_utils.js.map +0 -1
  91. package/dist/derived_keys.d.ts +0 -72
  92. package/dist/derived_keys.js +0 -247
  93. package/dist/derived_keys.js.map +0 -1
  94. package/dist/explore_certificate.d.ts +0 -30
  95. package/dist/explore_certificate.js +0 -44
  96. package/dist/explore_certificate.js.map +0 -1
  97. package/dist/explore_certificate_revocation_list.d.ts +0 -30
  98. package/dist/explore_certificate_revocation_list.js +0 -67
  99. package/dist/explore_certificate_revocation_list.js.map +0 -1
  100. package/dist/index.d.ts +0 -10
  101. package/dist/index.js +0 -23
  102. package/dist/index.js.map +0 -1
  103. package/dist/oid_map.d.ts +0 -7
  104. package/dist/oid_map.js +0 -262
  105. package/dist/oid_map.js.map +0 -1
  106. package/dist/verify_cerficate_signature.d.ts +0 -10
  107. package/dist/verify_cerficate_signature.js +0 -102
  108. package/dist/verify_cerficate_signature.js.map +0 -1
@@ -1,18 +1,41 @@
1
- import { exploreCertificate } from ".";
2
- import { Certificate } from "./common";
3
- import { PrivateKey } from "./common";
4
- import { explorePrivateKey } from "./explore_private_key";
5
-
6
- export function publicKeyAndPrivateKeyMatches(certificate: Certificate,privateKey: PrivateKey): boolean {
7
-
8
- const i = exploreCertificate(certificate);
9
- const j = explorePrivateKey(privateKey);
10
-
11
- const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
12
- const modulus2 = j.modulus;
13
-
14
- if (modulus1.length != modulus2.length) {
15
- return false;
16
- }
17
- return modulus1.toString("hex") === modulus2.toString("hex");
18
- }
1
+ import { explorePrivateKey } from "./explore_private_key";
2
+ import { Certificate, CertificatePEM, PrivateKey, PrivateKeyPEM } from "./common";
3
+ import { privateDecrypt_long, publicEncrypt_long, toPem } from "./crypto_utils";
4
+ import { exploreCertificate } from "./crypto_explore_certificate";
5
+
6
+ export function publicKeyAndPrivateKeyMatches(certificate: Certificate,privateKey: PrivateKey): boolean {
7
+
8
+ const i = exploreCertificate(certificate);
9
+ const j = explorePrivateKey(privateKey);
10
+
11
+ const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
12
+ const modulus2 = j.modulus;
13
+
14
+ if (modulus1.length != modulus2.length) {
15
+ return false;
16
+ }
17
+ return modulus1.toString("hex") === modulus2.toString("hex");
18
+ }
19
+
20
+
21
+
22
+ /**
23
+ * check that the given certificate matches the given private key
24
+ * @param certificate
25
+ * @param privateKey
26
+ */
27
+ function certificateMatchesPrivateKeyPEM(certificate: CertificatePEM, privateKey: PrivateKeyPEM, blockSize: number): boolean {
28
+ const initialBuffer = Buffer.from("Lorem Ipsum");
29
+ const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize, 11);
30
+ const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
31
+ const finalString = decryptedBuffer.toString("utf-8");
32
+ return initialBuffer.toString("utf-8") === finalString;
33
+ }
34
+
35
+ export function certificateMatchesPrivateKey(certificate: Certificate, privateKey: PrivateKey): boolean {
36
+ const e = explorePrivateKey(privateKey);
37
+ const blockSize = e.modulus.length;
38
+ const certificatePEM = toPem(certificate, "CERTIFICATE");
39
+ const privateKeyPEM = toPem(privateKey, "RSA PRIVATE KEY");
40
+ return certificateMatchesPrivateKeyPEM(certificatePEM, privateKeyPEM, blockSize);
41
+ }
@@ -1,105 +1,105 @@
1
- // tslint:disable: no-console
2
-
3
- // Now that we got a hash of the original certificate,
4
- // we need to verify if we can obtain the same hash by using the same hashing function
5
- // (in this case SHA-384). In order to do that, we need to extract just the body of
6
- // the signed certificate. Which, in our case, is everything but the signature.
7
- // The start of the body is always the first digit of the second line of the following command:
8
- import * as crypto from "crypto";
9
-
10
- import { Certificate, PrivateKey } from "./common";
11
- import { split_der, exploreCertificate } from "./crypto_explore_certificate";
12
- import { toPem } from "./crypto_utils";
13
- import { _readAlgorithmIdentifier, _readSignatureValueBin, TagType, readTag, _readStruct, _getBlock } from "./asn1";
14
-
15
- export function verifyCertificateOrClrSignature(certificateOrCrl: Buffer, parentCertificate: Certificate): boolean {
16
- const block_info = readTag(certificateOrCrl, 0);
17
- const blocks = _readStruct(certificateOrCrl, block_info);
18
- const bufferToBeSigned = certificateOrCrl.slice(block_info.position, blocks[1].position - 2);
19
-
20
- //xx console.log("bufferToBeSigned = ", bufferToBeSigned.length, bufferToBeSigned.toString("hex").substr(0, 50), bufferToBeSigned.toString("hex").substr(-10));
21
- const signatureAlgorithm = _readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
22
- const signatureValue = _readSignatureValueBin(certificateOrCrl, blocks[2]);
23
-
24
- const p = split_der(parentCertificate)[0];
25
- //xx const publicKey = extractPublicKeyFromCertificateSync(p);
26
- const certPem = toPem(p, "CERTIFICATE");
27
- const verify = crypto.createVerify(signatureAlgorithm.identifier);
28
- verify.update(bufferToBeSigned);
29
- verify.end();
30
- return verify.verify(certPem, signatureValue);
31
- }
32
-
33
- export function verifyCertificateSignature(certificate: Certificate, parentCertificate: Certificate): boolean {
34
- return verifyCertificateOrClrSignature(certificate, parentCertificate);
35
- }
36
- export function verifyCertificateRevocationListSignature(
37
- certificateRevocationList: Certificate,
38
- parentCertificate: Certificate
39
- ): boolean {
40
- return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
41
- }
42
-
43
- export type _VerifyStatus = "BadCertificateIssuerUseNotAllowed" | "BadCertificateInvalid" | "Good";
44
- export async function verifyCertificateChain(certificateChain: Certificate[]): Promise<{ status: _VerifyStatus; reason: string }> {
45
- // verify that all the certificate
46
- // second certificate must be used for CertificateSign
47
-
48
- for (let index = 1; index < certificateChain.length; index++) {
49
- const cert = certificateChain[index - 1];
50
- const certParent = certificateChain[index];
51
-
52
- // parent child must have keyCertSign
53
- const certParentInfo = exploreCertificate(certParent);
54
- const keyUsage = certParentInfo.tbsCertificate.extensions!.keyUsage!;
55
-
56
- // istanbul ignore next
57
- if (!keyUsage.keyCertSign) {
58
- return {
59
- status: "BadCertificateIssuerUseNotAllowed",
60
- reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing",
61
- };
62
- }
63
-
64
- const parentSignChild = verifyCertificateSignature(cert, certParent);
65
- if (!parentSignChild) {
66
- return {
67
- status: "BadCertificateInvalid",
68
- reason: "One of the certificate in the chain is not signing the previous certificate",
69
- };
70
- }
71
- const certInfo = exploreCertificate(cert);
72
-
73
- // istanbul ignore next
74
- if (!certInfo.tbsCertificate.extensions) {
75
- return {
76
- status: "BadCertificateInvalid",
77
- reason: "Cannot find X409 Extension 3 in certificate",
78
- };
79
- }
80
-
81
- // istanbul ignore next
82
- if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
83
- return {
84
- status: "BadCertificateInvalid",
85
- reason: "Cannot find X409 Extension 3 in certificate (parent)",
86
- };
87
- }
88
-
89
- // istanbul ignore next
90
- if (
91
- certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !==
92
- certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier
93
- ) {
94
- return {
95
- status: "BadCertificateInvalid",
96
- reason:
97
- "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate",
98
- };
99
- }
100
- }
101
- return {
102
- status: "Good",
103
- reason: `certificate chain is valid(length = ${certificateChain.length})`,
104
- };
105
- }
1
+ // tslint:disable: no-console
2
+
3
+ // Now that we got a hash of the original certificate,
4
+ // we need to verify if we can obtain the same hash by using the same hashing function
5
+ // (in this case SHA-384). In order to do that, we need to extract just the body of
6
+ // the signed certificate. Which, in our case, is everything but the signature.
7
+ // The start of the body is always the first digit of the second line of the following command:
8
+ import * as crypto from "crypto";
9
+
10
+ import { Certificate, PrivateKey } from "./common";
11
+ import { split_der, exploreCertificate } from "./crypto_explore_certificate";
12
+ import { toPem } from "./crypto_utils";
13
+ import { _readAlgorithmIdentifier, _readSignatureValueBin, TagType, readTag, _readStruct, _getBlock } from "./asn1";
14
+
15
+ export function verifyCertificateOrClrSignature(certificateOrCrl: Buffer, parentCertificate: Certificate): boolean {
16
+ const block_info = readTag(certificateOrCrl, 0);
17
+ const blocks = _readStruct(certificateOrCrl, block_info);
18
+ const bufferToBeSigned = certificateOrCrl.slice(block_info.position, blocks[1].position - 2);
19
+
20
+ //xx console.log("bufferToBeSigned = ", bufferToBeSigned.length, bufferToBeSigned.toString("hex").substr(0, 50), bufferToBeSigned.toString("hex").substr(-10));
21
+ const signatureAlgorithm = _readAlgorithmIdentifier(certificateOrCrl, blocks[1]);
22
+ const signatureValue = _readSignatureValueBin(certificateOrCrl, blocks[2]);
23
+
24
+ const p = split_der(parentCertificate)[0];
25
+ //xx const publicKey = extractPublicKeyFromCertificateSync(p);
26
+ const certPem = toPem(p, "CERTIFICATE");
27
+ const verify = crypto.createVerify(signatureAlgorithm.identifier);
28
+ verify.update(bufferToBeSigned);
29
+ verify.end();
30
+ return verify.verify(certPem, signatureValue);
31
+ }
32
+
33
+ export function verifyCertificateSignature(certificate: Certificate, parentCertificate: Certificate): boolean {
34
+ return verifyCertificateOrClrSignature(certificate, parentCertificate);
35
+ }
36
+ export function verifyCertificateRevocationListSignature(
37
+ certificateRevocationList: Certificate,
38
+ parentCertificate: Certificate
39
+ ): boolean {
40
+ return verifyCertificateOrClrSignature(certificateRevocationList, parentCertificate);
41
+ }
42
+
43
+ export type _VerifyStatus = "BadCertificateIssuerUseNotAllowed" | "BadCertificateInvalid" | "Good";
44
+ export async function verifyCertificateChain(certificateChain: Certificate[]): Promise<{ status: _VerifyStatus; reason: string }> {
45
+ // verify that all the certificate
46
+ // second certificate must be used for CertificateSign
47
+
48
+ for (let index = 1; index < certificateChain.length; index++) {
49
+ const cert = certificateChain[index - 1];
50
+ const certParent = certificateChain[index];
51
+
52
+ // parent child must have keyCertSign
53
+ const certParentInfo = exploreCertificate(certParent);
54
+ const keyUsage = certParentInfo.tbsCertificate.extensions!.keyUsage!;
55
+
56
+ // istanbul ignore next
57
+ if (!keyUsage.keyCertSign) {
58
+ return {
59
+ status: "BadCertificateIssuerUseNotAllowed",
60
+ reason: "One of the certificate in the chain has not keyUsage set for Certificate Signing",
61
+ };
62
+ }
63
+
64
+ const parentSignChild = verifyCertificateSignature(cert, certParent);
65
+ if (!parentSignChild) {
66
+ return {
67
+ status: "BadCertificateInvalid",
68
+ reason: "One of the certificate in the chain is not signing the previous certificate",
69
+ };
70
+ }
71
+ const certInfo = exploreCertificate(cert);
72
+
73
+ // istanbul ignore next
74
+ if (!certInfo.tbsCertificate.extensions) {
75
+ return {
76
+ status: "BadCertificateInvalid",
77
+ reason: "Cannot find X409 Extension 3 in certificate",
78
+ };
79
+ }
80
+
81
+ // istanbul ignore next
82
+ if (!certParentInfo.tbsCertificate.extensions || !certInfo.tbsCertificate.extensions.authorityKeyIdentifier) {
83
+ return {
84
+ status: "BadCertificateInvalid",
85
+ reason: "Cannot find X409 Extension 3 in certificate (parent)",
86
+ };
87
+ }
88
+
89
+ // istanbul ignore next
90
+ if (
91
+ certParentInfo.tbsCertificate.extensions.subjectKeyIdentifier !==
92
+ certInfo.tbsCertificate.extensions.authorityKeyIdentifier.keyIdentifier
93
+ ) {
94
+ return {
95
+ status: "BadCertificateInvalid",
96
+ reason:
97
+ "subjectKeyIdentifier authorityKeyIdentifier in child certificate do not match subjectKeyIdentifier of parent certificate",
98
+ };
99
+ }
100
+ }
101
+ return {
102
+ status: "Good",
103
+ reason: `certificate chain is valid(length = ${certificateChain.length})`,
104
+ };
105
+ }
@@ -1,3 +1,3 @@
1
- export * from "./read";
2
- export * from "./read_certificate_revocation_list";
1
+ export * from "./read";
2
+ export * from "./read_certificate_revocation_list";
3
3
  export * from "./read_certificate_signing_request";
@@ -1,95 +1,95 @@
1
- import * as assert from "assert";
2
- import * as fs from "fs";
3
- import * as path from "path";
4
- import { Certificate, CertificatePEM, DER, PEM, PrivateKey, PrivateKeyPEM, PublicKey, PublicKeyPEM } from "../source/common";
5
- import { convertPEMtoDER, identifyPemType } from "../source/crypto_utils";
6
- const sshpk = require("sshpk");
7
-
8
- /**
9
- * @method readKeyPem
10
- * @param filename
11
- */
12
- export function readKeyPem(filename: string): string {
13
- const raw_key = fs.readFileSync(filename, "utf8");
14
- const pemType = identifyPemType(raw_key);
15
- assert(typeof pemType === "string"); // must have a valid pem type
16
- return raw_key;
17
- }
18
-
19
- function _readPemFile(filename: string): PEM {
20
- assert(typeof filename === "string");
21
- return fs.readFileSync(filename, "ascii");
22
- }
23
-
24
- function _readPemOrDerFileAsDER(filename: string): DER {
25
- if (filename.match(/.*\.der/)) {
26
- return fs.readFileSync(filename) as Buffer;
27
- }
28
- const raw_key: string = _readPemFile(filename);
29
- return convertPEMtoDER(raw_key);
30
- }
31
-
32
- /**
33
- * read a DER or PEM certificate from file
34
- */
35
- export function readCertificate(filename: string): Certificate {
36
- return _readPemOrDerFileAsDER(filename) as Certificate;
37
- }
38
-
39
- /**
40
- * read a DER or PEM certificate from file
41
- */
42
- export function readPublicKey(filename: string): PublicKey {
43
- return _readPemOrDerFileAsDER(filename) as PublicKey;
44
- }
45
-
46
- /**
47
- * read a DER or PEM certificate from file
48
- */
49
- export function readPrivateKey(filename: string): PrivateKey {
50
- return _readPemOrDerFileAsDER(filename) as PrivateKey;
51
- }
52
-
53
- export function readCertificatePEM(filename: string): CertificatePEM {
54
- return _readPemFile(filename);
55
- }
56
-
57
- export function readPublicKeyPEM(filename: string): PublicKeyPEM {
58
- return _readPemFile(filename);
59
- }
60
-
61
- export function readPrivateKeyPEM(filename: string): PrivateKeyPEM {
62
- return _readPemFile(filename);
63
- }
64
- let __certificate_store = path.join(__dirname, "../../certificates/");
65
-
66
- export function setCertificateStore(store: string): string {
67
- const old_store = __certificate_store;
68
- __certificate_store = store;
69
- return old_store;
70
- }
71
-
72
- export function read_sshkey_as_pem(filename: string): PublicKeyPEM {
73
- if (filename.substr(0, 1) !== ".") {
74
- filename = __certificate_store + filename;
75
- }
76
- const key: string = fs.readFileSync(filename, "ascii");
77
- const sshKey = sshpk.parseKey(key, "ssh");
78
-
79
- return sshKey.toString("pkcs8") as PEM;
80
- }
81
-
82
- /**
83
- *
84
- * @param filename
85
- */
86
- export function readPrivateRsaKey(filename: string): PrivateKeyPEM {
87
- if (filename.substr(0, 1) !== "." && !fs.existsSync(filename)) {
88
- filename = __certificate_store + filename;
89
- }
90
- return fs.readFileSync(filename, "ascii") as string;
91
- }
92
-
93
- export function readPublicRsaKey(filename: string): PublicKeyPEM {
94
- return readPrivateRsaKey(filename);
95
- }
1
+ import * as assert from "assert";
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import { Certificate, CertificatePEM, DER, PEM, PrivateKey, PrivateKeyPEM, PublicKey, PublicKeyPEM } from "../source/common";
5
+ import { convertPEMtoDER, identifyPemType } from "../source/crypto_utils";
6
+ const sshpk = require("sshpk");
7
+
8
+ /**
9
+ * @method readKeyPem
10
+ * @param filename
11
+ */
12
+ export function readKeyPem(filename: string): string {
13
+ const raw_key = fs.readFileSync(filename, "utf8");
14
+ const pemType = identifyPemType(raw_key);
15
+ assert(typeof pemType === "string"); // must have a valid pem type
16
+ return raw_key;
17
+ }
18
+
19
+ function _readPemFile(filename: string): PEM {
20
+ assert(typeof filename === "string");
21
+ return fs.readFileSync(filename, "ascii");
22
+ }
23
+
24
+ function _readPemOrDerFileAsDER(filename: string): DER {
25
+ if (filename.match(/.*\.der/)) {
26
+ return fs.readFileSync(filename) as Buffer;
27
+ }
28
+ const raw_key: string = _readPemFile(filename);
29
+ return convertPEMtoDER(raw_key);
30
+ }
31
+
32
+ /**
33
+ * read a DER or PEM certificate from file
34
+ */
35
+ export function readCertificate(filename: string): Certificate {
36
+ return _readPemOrDerFileAsDER(filename) as Certificate;
37
+ }
38
+
39
+ /**
40
+ * read a DER or PEM certificate from file
41
+ */
42
+ export function readPublicKey(filename: string): PublicKey {
43
+ return _readPemOrDerFileAsDER(filename) as PublicKey;
44
+ }
45
+
46
+ /**
47
+ * read a DER or PEM certificate from file
48
+ */
49
+ export function readPrivateKey(filename: string): PrivateKey {
50
+ return _readPemOrDerFileAsDER(filename) as PrivateKey;
51
+ }
52
+
53
+ export function readCertificatePEM(filename: string): CertificatePEM {
54
+ return _readPemFile(filename);
55
+ }
56
+
57
+ export function readPublicKeyPEM(filename: string): PublicKeyPEM {
58
+ return _readPemFile(filename);
59
+ }
60
+
61
+ export function readPrivateKeyPEM(filename: string): PrivateKeyPEM {
62
+ return _readPemFile(filename);
63
+ }
64
+ let __certificate_store = path.join(__dirname, "../../certificates/");
65
+
66
+ export function setCertificateStore(store: string): string {
67
+ const old_store = __certificate_store;
68
+ __certificate_store = store;
69
+ return old_store;
70
+ }
71
+
72
+ export function read_sshkey_as_pem(filename: string): PublicKeyPEM {
73
+ if (filename.substring(0, 1) !== ".") {
74
+ filename = __certificate_store + filename;
75
+ }
76
+ const key: string = fs.readFileSync(filename, "ascii");
77
+ const sshKey = sshpk.parseKey(key, "ssh");
78
+
79
+ return sshKey.toString("pkcs8") as PEM;
80
+ }
81
+
82
+ /**
83
+ *
84
+ * @param filename
85
+ */
86
+ export function readPrivateRsaKey(filename: string): PrivateKeyPEM {
87
+ if (filename.substring(0, 1) !== "." && !fs.existsSync(filename)) {
88
+ filename = __certificate_store + filename;
89
+ }
90
+ return fs.readFileSync(filename, "ascii") as string;
91
+ }
92
+
93
+ export function readPublicRsaKey(filename: string): PublicKeyPEM {
94
+ return readPrivateRsaKey(filename);
95
+ }
@@ -1,14 +1,14 @@
1
- import * as fs from "fs";
2
- import { promisify } from "util";
3
- import { convertPEMtoDER } from "../source/crypto_utils";
4
- import { CertificateRevocationList } from "../source/common";
5
-
6
- export async function readCertificateRevocationList(filename: string): Promise<CertificateRevocationList> {
7
- const crl = await promisify(fs.readFile)(filename);
8
- if (crl[0] === 0x30 && crl[1] === 0x82) {
9
- // der format
10
- return crl as CertificateRevocationList;
11
- }
12
- const raw_crl = crl.toString();
13
- return convertPEMtoDER(raw_crl);
14
- }
1
+ import * as fs from "fs";
2
+ import { promisify } from "util";
3
+ import { convertPEMtoDER } from "../source/crypto_utils";
4
+ import { CertificateRevocationList } from "../source/common";
5
+
6
+ export async function readCertificateRevocationList(filename: string): Promise<CertificateRevocationList> {
7
+ const crl = await promisify(fs.readFile)(filename);
8
+ if (crl[0] === 0x30 && crl[1] === 0x82) {
9
+ // der format
10
+ return crl as CertificateRevocationList;
11
+ }
12
+ const raw_crl = crl.toString();
13
+ return convertPEMtoDER(raw_crl);
14
+ }
@@ -1,17 +1,17 @@
1
- import * as fs from "fs";
2
- import { promisify } from "util";
3
- import { convertPEMtoDER } from "../source/crypto_utils";
4
- import { CertificateRevocationList } from "../source/common";
5
- import { assert } from "console";
6
-
7
- export type CertificateSigningRequest = Buffer;
8
-
9
- export async function readCertificateSigningRequest(filename: string): Promise<CertificateSigningRequest> {
10
- const csr = await promisify(fs.readFile)(filename);
11
- if (csr[0] === 0x30 && csr[1] === 0x82) {
12
- // der format
13
- return csr as CertificateRevocationList;
14
- }
15
- const raw_crl = csr.toString();
16
- return convertPEMtoDER(raw_crl);
17
- }
1
+ import * as fs from "fs";
2
+ import { promisify } from "util";
3
+ import { convertPEMtoDER } from "../source/crypto_utils";
4
+ import { CertificateRevocationList } from "../source/common";
5
+ import { assert } from "console";
6
+
7
+ export type CertificateSigningRequest = Buffer;
8
+
9
+ export async function readCertificateSigningRequest(filename: string): Promise<CertificateSigningRequest> {
10
+ const csr = await promisify(fs.readFile)(filename);
11
+ if (csr[0] === 0x30 && csr[1] === 0x82) {
12
+ // der format
13
+ return csr as CertificateRevocationList;
14
+ }
15
+ const raw_crl = csr.toString();
16
+ return convertPEMtoDER(raw_crl);
17
+ }
@@ -1,34 +1,34 @@
1
- // tslint:disable: no-console
2
- import * as fs from "fs";
3
- import { exploreCertificate, readCertificate } from ".";
4
-
5
- async function testCertificate(filename: string): Promise<void> {
6
- const cert1 = await readCertificate(filename);
7
- try {
8
- const info = exploreCertificate(cert1);
9
- // console.log(info);
10
- } catch (err) {
11
- console.log(filename, "err = ", err.message);
12
- }
13
- }
14
- async function testCertificate1(filename: string): Promise<void> {
15
- const cert1 = fs.readFileSync(filename);
16
- try {
17
- const info = exploreCertificate(cert1);
18
- // console.log(info);
19
- } catch (err) {
20
- console.log(filename, "err = ", err.message);
21
- console.log(err);
22
- throw err;
23
- }
24
- }
25
-
26
- (async () => {
27
- try {
28
- testCertificate1("./read.cer");
29
- testCertificate1("./unsol.cer");
30
- testCertificate1("./write.cer");
31
- } catch (err) {
32
- console.log("???? ERR !!!! ", err.message);
33
- }
34
- })();
1
+ // tslint:disable: no-console
2
+ import * as fs from "fs";
3
+ import { exploreCertificate, readCertificate } from ".";
4
+
5
+ async function testCertificate(filename: string): Promise<void> {
6
+ const cert1 = await readCertificate(filename);
7
+ try {
8
+ const info = exploreCertificate(cert1);
9
+ // console.log(info);
10
+ } catch (err) {
11
+ console.log(filename, "err = ", err.message);
12
+ }
13
+ }
14
+ async function testCertificate1(filename: string): Promise<void> {
15
+ const cert1 = fs.readFileSync(filename);
16
+ try {
17
+ const info = exploreCertificate(cert1);
18
+ // console.log(info);
19
+ } catch (err) {
20
+ console.log(filename, "err = ", err.message);
21
+ console.log(err);
22
+ throw err;
23
+ }
24
+ }
25
+
26
+ (async () => {
27
+ try {
28
+ testCertificate1("./read.cer");
29
+ testCertificate1("./unsol.cer");
30
+ testCertificate1("./write.cer");
31
+ } catch (err) {
32
+ console.log("???? ERR !!!! ", err.message);
33
+ }
34
+ })();
package/tsconfig.json CHANGED
@@ -1,18 +1,18 @@
1
- {
2
- "compilerOptions": {
3
- "skipLibCheck": true,
4
- "target": "es6",
5
- "moduleResolution": "node",
6
- "module": "commonjs",
7
- "declaration": true,
8
- "outDir": "./dist",
9
- "sourceMap": true,
10
- "strict": true,
11
- "listFiles": false,
12
- "traceResolution": false,
13
- "incremental": true,
14
- "types": ["node", "mocha", "should"],
15
- "rootDir": "."
16
- },
17
- "files": ["source/index.ts", "source_nodejs/index.ts"]
18
- }
1
+ {
2
+ "compilerOptions": {
3
+ "skipLibCheck": true,
4
+ "target": "es6",
5
+ "moduleResolution": "node",
6
+ "module": "commonjs",
7
+ "declaration": true,
8
+ "outDir": "./dist",
9
+ "sourceMap": true,
10
+ "strict": true,
11
+ "listFiles": false,
12
+ "traceResolution": false,
13
+ "incremental": true,
14
+ "types": ["node", "mocha", "should"],
15
+ "rootDir": "."
16
+ },
17
+ "files": ["source/index.ts", "source_nodejs/index.ts"]
18
+ }