node-opcua-crypto 2.2.0 → 3.0.0-beta.1

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 (194) hide show
  1. package/{dist → dist-cjs}/source/asn1.js +95 -151
  2. package/dist-cjs/source/asn1.js.map +1 -0
  3. package/dist-cjs/source/buffer_utils.js +9 -0
  4. package/dist-cjs/source/buffer_utils.js.map +1 -0
  5. package/dist-cjs/source/common.js +11 -0
  6. package/dist-cjs/source/common.js.map +1 -0
  7. package/dist-cjs/source/crypto_explore_certificate.js +367 -0
  8. package/dist-cjs/source/crypto_explore_certificate.js.map +1 -0
  9. package/{dist → dist-cjs}/source/crypto_utils.js +80 -141
  10. package/dist-cjs/source/crypto_utils.js.map +1 -0
  11. package/dist-cjs/source/derived_keys.js +133 -0
  12. package/dist-cjs/source/derived_keys.js.map +1 -0
  13. package/{dist → dist-cjs}/source/explore_certificate.js +10 -17
  14. package/dist-cjs/source/explore_certificate.js.map +1 -0
  15. package/dist-cjs/source/explore_certificate_revocation_list.js +69 -0
  16. package/dist-cjs/source/explore_certificate_revocation_list.js.map +1 -0
  17. package/dist-cjs/source/explore_certificate_signing_request.js +45 -0
  18. package/dist-cjs/source/explore_certificate_signing_request.js.map +1 -0
  19. package/dist-cjs/source/explore_private_key.js +75 -0
  20. package/dist-cjs/source/explore_private_key.js.map +1 -0
  21. package/dist-cjs/source/index.js +32 -0
  22. package/dist-cjs/source/index.js.map +1 -0
  23. package/{dist → dist-cjs}/source/oid_map.js +0 -4
  24. package/dist-cjs/source/oid_map.js.map +1 -0
  25. package/dist-cjs/source/public_private_match.js +32 -0
  26. package/dist-cjs/source/public_private_match.js.map +1 -0
  27. package/dist-cjs/source/subject.js +92 -0
  28. package/dist-cjs/source/subject.js.map +1 -0
  29. package/dist-cjs/source/verify_certificate_signature.js +120 -0
  30. package/dist-cjs/source/verify_certificate_signature.js.map +1 -0
  31. package/dist-cjs/source/x509/_build_public_key.js +69 -0
  32. package/dist-cjs/source/x509/_build_public_key.js.map +1 -0
  33. package/dist-cjs/source/x509/_crypto.js +50 -0
  34. package/dist-cjs/source/x509/_crypto.js.map +1 -0
  35. package/dist-cjs/source/x509/_get_attributes.js +35 -0
  36. package/dist-cjs/source/x509/_get_attributes.js.map +1 -0
  37. package/dist-cjs/source/x509/create_certificate_signing_request.js +92 -0
  38. package/dist-cjs/source/x509/create_certificate_signing_request.js.map +1 -0
  39. package/dist-cjs/source/x509/create_key_pair.js +123 -0
  40. package/dist-cjs/source/x509/create_key_pair.js.map +1 -0
  41. package/dist-cjs/source/x509/create_self_signed_certificate.js +102 -0
  42. package/dist-cjs/source/x509/create_self_signed_certificate.js.map +1 -0
  43. package/dist-cjs/source_nodejs/generate_private_key_filename.js +67 -0
  44. package/dist-cjs/source_nodejs/generate_private_key_filename.js.map +1 -0
  45. package/{dist → dist-cjs}/source_nodejs/index.js +4 -3
  46. package/{dist → dist-cjs}/source_nodejs/index.js.map +1 -1
  47. package/{dist → dist-cjs}/source_nodejs/read.js +32 -43
  48. package/dist-cjs/source_nodejs/read.js.map +1 -0
  49. package/dist-cjs/source_nodejs/read_certificate_revocation_list.js +64 -0
  50. package/dist-cjs/source_nodejs/read_certificate_revocation_list.js.map +1 -0
  51. package/dist-cjs/source_nodejs/read_certificate_signing_request.js +64 -0
  52. package/dist-cjs/source_nodejs/read_certificate_signing_request.js.map +1 -0
  53. package/dist-esm/source/asn1.js +275 -0
  54. package/dist-esm/source/asn1.js.map +1 -0
  55. package/dist-esm/source/buffer_utils.js +6 -0
  56. package/dist-esm/source/buffer_utils.js.map +1 -0
  57. package/dist-esm/source/common.js +8 -0
  58. package/dist-esm/source/common.js.map +1 -0
  59. package/dist-esm/source/crypto_explore_certificate.js +356 -0
  60. package/dist-esm/source/crypto_explore_certificate.js.map +1 -0
  61. package/dist-esm/source/crypto_utils.js +242 -0
  62. package/dist-esm/source/crypto_utils.js.map +1 -0
  63. package/dist-esm/source/derived_keys.js +117 -0
  64. package/dist-esm/source/derived_keys.js.map +1 -0
  65. package/dist-esm/source/explore_certificate.js +29 -0
  66. package/dist-esm/source/explore_certificate.js.map +1 -0
  67. package/dist-esm/source/explore_certificate_revocation_list.js +64 -0
  68. package/dist-esm/source/explore_certificate_revocation_list.js.map +1 -0
  69. package/dist-esm/source/explore_certificate_signing_request.js +40 -0
  70. package/dist-esm/source/explore_certificate_signing_request.js.map +1 -0
  71. package/dist-esm/source/explore_private_key.js +71 -0
  72. package/dist-esm/source/explore_private_key.js.map +1 -0
  73. package/dist-esm/source/index.js +16 -0
  74. package/dist-esm/source/index.js.map +1 -0
  75. package/{source/oid_map.ts → dist-esm/source/oid_map.js} +2 -15
  76. package/dist-esm/source/oid_map.js.map +1 -0
  77. package/dist-esm/source/public_private_match.js +27 -0
  78. package/dist-esm/source/public_private_match.js.map +1 -0
  79. package/dist-esm/source/subject.js +89 -0
  80. package/dist-esm/source/subject.js.map +1 -0
  81. package/dist-esm/source/verify_certificate_signature.js +110 -0
  82. package/dist-esm/source/verify_certificate_signature.js.map +1 -0
  83. package/dist-esm/source/x509/_build_public_key.js +65 -0
  84. package/dist-esm/source/x509/_build_public_key.js.map +1 -0
  85. package/dist-esm/source/x509/_crypto.js +23 -0
  86. package/dist-esm/source/x509/_crypto.js.map +1 -0
  87. package/dist-esm/source/x509/_get_attributes.js +31 -0
  88. package/dist-esm/source/x509/_get_attributes.js.map +1 -0
  89. package/dist-esm/source/x509/create_certificate_signing_request.js +88 -0
  90. package/dist-esm/source/x509/create_certificate_signing_request.js.map +1 -0
  91. package/dist-esm/source/x509/create_key_pair.js +115 -0
  92. package/dist-esm/source/x509/create_key_pair.js.map +1 -0
  93. package/dist-esm/source/x509/create_self_signed_certificate.js +98 -0
  94. package/dist-esm/source/x509/create_self_signed_certificate.js.map +1 -0
  95. package/dist-esm/source_nodejs/generate_private_key_filename.js +60 -0
  96. package/dist-esm/source_nodejs/generate_private_key_filename.js.map +1 -0
  97. package/dist-esm/source_nodejs/index.js +5 -0
  98. package/dist-esm/source_nodejs/index.js.map +1 -0
  99. package/dist-esm/source_nodejs/read.js +81 -0
  100. package/dist-esm/source_nodejs/read.js.map +1 -0
  101. package/dist-esm/source_nodejs/read_certificate_revocation_list.js +57 -0
  102. package/dist-esm/source_nodejs/read_certificate_revocation_list.js.map +1 -0
  103. package/dist-esm/source_nodejs/read_certificate_signing_request.js +57 -0
  104. package/dist-esm/source_nodejs/read_certificate_signing_request.js.map +1 -0
  105. package/{dist → dist-types}/source/common.d.ts +6 -0
  106. package/{dist → dist-types}/source/crypto_explore_certificate.d.ts +3 -3
  107. package/{dist → dist-types}/source/crypto_utils.d.ts +2 -2
  108. package/{dist → dist-types}/source/derived_keys.d.ts +2 -2
  109. package/{dist → dist-types}/source/explore_certificate.d.ts +3 -6
  110. package/{dist → dist-types}/source/explore_certificate_revocation_list.d.ts +2 -2
  111. package/{dist → dist-types}/source/explore_certificate_signing_request.d.ts +2 -2
  112. package/{dist → dist-types}/source/explore_private_key.d.ts +1 -1
  113. package/dist-types/source/index.d.ts +18 -0
  114. package/{dist → dist-types}/source/public_private_match.d.ts +1 -1
  115. package/dist-types/source/subject.d.ts +27 -0
  116. package/{dist → dist-types}/source/verify_certificate_signature.d.ts +1 -1
  117. package/dist-types/source/x509/_build_public_key.d.ts +1 -0
  118. package/dist-types/source/x509/_crypto.d.ts +2 -0
  119. package/dist-types/source/x509/_get_attributes.d.ts +8 -0
  120. package/dist-types/source/x509/create_certificate_signing_request.d.ts +18 -0
  121. package/dist-types/source/x509/create_key_pair.d.ts +8 -0
  122. package/dist-types/source/x509/create_self_signed_certificate.d.ts +17 -0
  123. package/dist-types/source_nodejs/generate_private_key_filename.d.ts +1 -0
  124. package/dist-types/source_nodejs/index.d.ts +4 -0
  125. package/{dist → dist-types}/source_nodejs/read.d.ts +1 -1
  126. package/{dist → dist-types}/source_nodejs/read_certificate_revocation_list.d.ts +1 -1
  127. package/index.d.ts +2 -2
  128. package/index.js +2 -2
  129. package/index_web.d.ts +1 -0
  130. package/index_web.js +1 -1
  131. package/package.json +31 -7
  132. package/web/esbuild.mjs +15 -0
  133. package/web/index.html +9 -0
  134. package/web/main.js +28 -0
  135. package/web/rollup.config.mjs +20 -0
  136. package/.fossa.yml +0 -18
  137. package/.github/FUNDING.yml +0 -12
  138. package/.github/workflows/main.yml +0 -106
  139. package/.prettierrc.js +0 -7
  140. package/dist/source/asn1.js.map +0 -1
  141. package/dist/source/buffer_utils.js +0 -22
  142. package/dist/source/buffer_utils.js.map +0 -1
  143. package/dist/source/common.js +0 -3
  144. package/dist/source/common.js.map +0 -1
  145. package/dist/source/crypto_explore_certificate.js +0 -602
  146. package/dist/source/crypto_explore_certificate.js.map +0 -1
  147. package/dist/source/crypto_utils.js.map +0 -1
  148. package/dist/source/derived_keys.js +0 -249
  149. package/dist/source/derived_keys.js.map +0 -1
  150. package/dist/source/explore_certificate.js.map +0 -1
  151. package/dist/source/explore_certificate_revocation_list.js +0 -70
  152. package/dist/source/explore_certificate_revocation_list.js.map +0 -1
  153. package/dist/source/explore_certificate_signing_request.js +0 -45
  154. package/dist/source/explore_certificate_signing_request.js.map +0 -1
  155. package/dist/source/explore_private_key.js +0 -98
  156. package/dist/source/explore_private_key.js.map +0 -1
  157. package/dist/source/index.d.ts +0 -13
  158. package/dist/source/index.js +0 -30
  159. package/dist/source/index.js.map +0 -1
  160. package/dist/source/oid_map.js.map +0 -1
  161. package/dist/source/public_private_match.js +0 -37
  162. package/dist/source/public_private_match.js.map +0 -1
  163. package/dist/source/verify_certificate_signature.js +0 -102
  164. package/dist/source/verify_certificate_signature.js.map +0 -1
  165. package/dist/source_nodejs/index.d.ts +0 -3
  166. package/dist/source_nodejs/read.js.map +0 -1
  167. package/dist/source_nodejs/read_certificate_revocation_list.js +0 -28
  168. package/dist/source_nodejs/read_certificate_revocation_list.js.map +0 -1
  169. package/dist/source_nodejs/read_certificate_signing_request.js +0 -28
  170. package/dist/source_nodejs/read_certificate_signing_request.js.map +0 -1
  171. package/source/asn1.ts +0 -404
  172. package/source/buffer_utils.ts +0 -18
  173. package/source/common.ts +0 -15
  174. package/source/crypto_explore_certificate.ts +0 -764
  175. package/source/crypto_utils.ts +0 -376
  176. package/source/derived_keys.ts +0 -287
  177. package/source/explore_certificate.ts +0 -66
  178. package/source/explore_certificate_revocation_list.ts +0 -122
  179. package/source/explore_certificate_signing_request.ts +0 -58
  180. package/source/explore_private_key.ts +0 -119
  181. package/source/index.ts +0 -13
  182. package/source/public_private_match.ts +0 -37
  183. package/source/verify_certificate_signature.ts +0 -105
  184. package/source_nodejs/index.ts +0 -3
  185. package/source_nodejs/read.ts +0 -105
  186. package/source_nodejs/read_certificate_revocation_list.ts +0 -14
  187. package/source_nodejs/read_certificate_signing_request.ts +0 -17
  188. package/test_certificate.ts +0 -34
  189. package/tsconfig.json +0 -18
  190. package/tslint.json +0 -35
  191. /package/{dist → dist-types}/source/asn1.d.ts +0 -0
  192. /package/{dist → dist-types}/source/buffer_utils.d.ts +0 -0
  193. /package/{dist → dist-types}/source/oid_map.d.ts +0 -0
  194. /package/{dist → dist-types}/source_nodejs/read_certificate_signing_request.d.ts +0 -0
@@ -1,66 +0,0 @@
1
- /**
2
- * @module node_opcua_crypto
3
- */
4
-
5
- import { Certificate, CertificatePEM } from "./common";
6
- import { exploreCertificate, SubjectPublicKey } from "./crypto_explore_certificate";
7
- import { DirectoryName } from "./asn1";
8
- import { convertPEMtoDER } from "./crypto_utils";
9
- import * as assert from "assert";
10
-
11
- export type PublicKeyLength = 64 | 96 | 128 | 256 | 384 | 512;
12
-
13
- /**
14
- * A structure exposing useful information about a certificate
15
- */
16
- export interface CertificateInfo {
17
- /** the public key length in bits */
18
- publicKeyLength: PublicKeyLength;
19
- /** the date at which the certificate starts to be valid */
20
- notBefore: Date;
21
- /** the date after which the certificate is not valid any more */
22
- notAfter: Date;
23
- /** info about certificate owner */
24
- subject: DirectoryName;
25
- /** public key */
26
- publicKey: SubjectPublicKey;
27
- }
28
-
29
- export function coerceCertificate(certificate: Certificate | CertificatePEM): Certificate {
30
- if (typeof certificate === "string") {
31
- certificate = convertPEMtoDER(certificate);
32
- }
33
- assert(certificate instanceof Buffer);
34
- return certificate;
35
- }
36
-
37
- /**
38
- * @method exploreCertificateInfo
39
- * returns useful information about the certificate such as public key length, start date and end of validity date,
40
- * and CN
41
- * @param certificate the certificate to explore
42
- */
43
- export function exploreCertificateInfo(certificate: Certificate | CertificatePEM): CertificateInfo {
44
- certificate = coerceCertificate(certificate);
45
-
46
- const certInfo = exploreCertificate(certificate);
47
- const data: CertificateInfo = {
48
- publicKeyLength: certInfo.tbsCertificate.subjectPublicKeyInfo.keyLength,
49
- notBefore: certInfo.tbsCertificate.validity.notBefore,
50
- notAfter: certInfo.tbsCertificate.validity.notAfter,
51
- publicKey: certInfo.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey,
52
- subject: certInfo.tbsCertificate.subject,
53
- };
54
- // istanbul ignore next
55
- if (
56
- !(
57
- data.publicKeyLength === 512 ||
58
- data.publicKeyLength === 384 ||
59
- data.publicKeyLength === 256 ||
60
- data.publicKeyLength === 128
61
- )
62
- ) {
63
- throw new Error("Invalid public key length (expecting 128,256,384 or 512)" + data.publicKeyLength);
64
- }
65
- return data;
66
- }
@@ -1,122 +0,0 @@
1
- import {
2
- _readStruct,
3
- readTag,
4
- _readBitString,
5
- AlgorithmIdentifier,
6
- _readAlgorithmIdentifier,
7
- _readSignatureValue,
8
- _readSignatureValueBin,
9
- BlockInfo,
10
- _readObjectIdentifier,
11
- DirectoryName,
12
- _readValue,
13
- _readTime,
14
- _readLongIntegerValue,
15
- formatBuffer2DigitHexWithColum,
16
- _getBlock,
17
- _readDirectoryName,
18
- _findBlockAtIndex,
19
- _readIntegerValue,
20
- TagType,
21
- } from "./asn1";
22
- import { CertificateRevocationList } from "./common";
23
- import { makeSHA1Thumbprint, convertPEMtoDER } from "./crypto_utils";
24
-
25
- export type Version = string;
26
- export type Name = string;
27
- export type CertificateSerialNumber = string;
28
- export type Extensions = Record<string, unknown>;
29
- export interface RevokedCertificate {
30
- userCertificate: CertificateSerialNumber;
31
- revocationDate: Date;
32
- crlEntryExtensions?: Extensions;
33
- }
34
- export interface TBSCertList {
35
- version?: Version; //OPTIONAL; // must be 2
36
- signature: AlgorithmIdentifier;
37
- issuer: Name;
38
- issuerFingerprint: string; // 00:AA:BB:etc ...
39
- thisUpdate: Date;
40
- nextUpdate?: Date; // Time OPTIONAL,
41
- revokedCertificates: RevokedCertificate[];
42
- // crlExtensions[0] EXPLICIT Extensions OPTIONAL
43
- }
44
- export interface CertificateRevocationListInfo {
45
- tbsCertList: TBSCertList;
46
- signatureAlgorithm: AlgorithmIdentifier;
47
- signatureValue: Buffer;
48
- }
49
-
50
- export function readNameForCrl(buffer: Buffer, block: BlockInfo): DirectoryName {
51
- return _readDirectoryName(buffer, block);
52
- }
53
-
54
- function _readTbsCertList(buffer: Buffer, blockInfo: BlockInfo): TBSCertList {
55
- const blocks = _readStruct(buffer, blockInfo);
56
-
57
- const hasOptionalVersion = blocks[0].tag === TagType.INTEGER;
58
-
59
- if (hasOptionalVersion) {
60
- const version = _readIntegerValue(buffer, blocks[0]);
61
- const signature = _readAlgorithmIdentifier(buffer, blocks[1]);
62
- const issuer = readNameForCrl(buffer, blocks[2]);
63
- const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[2])));
64
-
65
- const thisUpdate = _readTime(buffer, blocks[3]);
66
- const nextUpdate = _readTime(buffer, blocks[4]);
67
-
68
- const revokedCertificates: RevokedCertificate[] = [];
69
-
70
- if (blocks[5] && blocks[5].tag < 0x80) {
71
- const list = _readStruct(buffer, blocks[5]);
72
- for (const r of list) {
73
- // sometime blocks[5] doesn't exits .. in this case
74
- const rr = _readStruct(buffer, r);
75
- const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
76
- const revocationDate = _readTime(buffer, rr[1]);
77
- revokedCertificates.push({
78
- revocationDate,
79
- userCertificate,
80
- });
81
- }
82
- }
83
-
84
- const ext0 = _findBlockAtIndex(blocks, 0);
85
- return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates } as TBSCertList;
86
- } else {
87
-
88
- const signature = _readAlgorithmIdentifier(buffer, blocks[0]);
89
- const issuer = readNameForCrl(buffer, blocks[1]);
90
- const issuerFingerprint = formatBuffer2DigitHexWithColum(makeSHA1Thumbprint(_getBlock(buffer, blocks[1])));
91
-
92
- const thisUpdate = _readTime(buffer, blocks[2]);
93
- const nextUpdate = _readTime(buffer, blocks[3]);
94
-
95
- const revokedCertificates: RevokedCertificate[] = [];
96
-
97
- if (blocks[4] && blocks[4].tag < 0x80) {
98
- const list = _readStruct(buffer, blocks[4]);
99
- for (const r of list) {
100
- // sometime blocks[5] doesn't exits .. in this case
101
- const rr = _readStruct(buffer, r);
102
- const userCertificate = formatBuffer2DigitHexWithColum(_readLongIntegerValue(buffer, rr[0]));
103
- const revocationDate = _readTime(buffer, rr[1]);
104
- revokedCertificates.push({
105
- revocationDate,
106
- userCertificate,
107
- });
108
- }
109
- }
110
- return { issuer, issuerFingerprint, thisUpdate, nextUpdate, signature, revokedCertificates } as TBSCertList;
111
- }
112
- }
113
- // see https://tools.ietf.org/html/rfc5280
114
-
115
- export function exploreCertificateRevocationList(crl: CertificateRevocationList): CertificateRevocationListInfo {
116
- const blockInfo = readTag(crl, 0);
117
- const blocks = _readStruct(crl, blockInfo);
118
- const tbsCertList = _readTbsCertList(crl, blocks[0]);
119
- const signatureAlgorithm = _readAlgorithmIdentifier(crl, blocks[1]);
120
- const signatureValue = _readSignatureValueBin(crl, blocks[2]);
121
- return { tbsCertList, signatureAlgorithm, signatureValue };
122
- }
@@ -1,58 +0,0 @@
1
- import * as assert from "assert";
2
- import { BlockInfo, readTag, _findBlockAtIndex, _getBlock, _readObjectIdentifier, _readStruct, _readVersionValue } from "./asn1";
3
-
4
- import { BasicConstraints, X509KeyUsage, _readExtension } from "./crypto_explore_certificate";
5
-
6
- export interface ExtensionRequest {
7
- basicConstraints: BasicConstraints;
8
- keyUsage: X509KeyUsage;
9
- subjectAltName: any;
10
- }
11
- export interface CertificateSigningRequestInfo {
12
- extensionRequest: ExtensionRequest;
13
- }
14
-
15
- function _readExtensionRequest(buffer: Buffer): ExtensionRequest {
16
- const block = readTag(buffer, 0);
17
-
18
- const inner_blocks = _readStruct(buffer, block);
19
- const extensions = inner_blocks.map((block1) => _readExtension(buffer, block1));
20
-
21
- const result: any = {};
22
- for (const e of extensions) {
23
- result[e.identifier.name] = e.value;
24
- }
25
- const { basicConstraints, keyUsage, subjectAltName } = result;
26
- return { basicConstraints, keyUsage, subjectAltName };
27
- }
28
-
29
- export function readCertificationRequestInfo(buffer: Buffer, block: BlockInfo): CertificateSigningRequestInfo {
30
- const blocks = _readStruct(buffer, block);
31
- if (blocks.length === 4) {
32
- const extensionRequestBlock = _findBlockAtIndex(blocks, 0);
33
- if (!extensionRequestBlock) {
34
- throw new Error("cannot find extensionRequest block");
35
- }
36
- const blocks1 = _readStruct(buffer, extensionRequestBlock);
37
- const blocks2 = _readStruct(buffer, blocks1[0]);
38
- const identifier = _readObjectIdentifier(buffer, blocks2[0]);
39
- if (identifier.name !== "extensionRequest") {
40
- throw new Error(" Cannot find extension Request in ASN1 block");
41
- }
42
- const buf = _getBlock(buffer, blocks2[1]);
43
-
44
- const extensionRequest = _readExtensionRequest(buf);
45
-
46
- return { extensionRequest };
47
- }
48
- throw new Error("Invalid CSR or ");
49
- }
50
-
51
- // see https://tools.ietf.org/html/rfc2986 : Certification Request Syntax Specification Version 1.7
52
-
53
- export function exploreCertificateSigningRequest(crl: Buffer): CertificateSigningRequestInfo {
54
- const blockInfo = readTag(crl, 0);
55
- const blocks = _readStruct(crl, blockInfo);
56
- const csrInfo = readCertificationRequestInfo(crl, blocks[0]);
57
- return csrInfo;
58
- }
@@ -1,119 +0,0 @@
1
- import * as assert from "assert";
2
- import { BlockInfo, readTag, TagType, _readIntegerAsByteString, _readStruct } from "./asn1";
3
- import { PrivateKey } from "./common";
4
-
5
- // tslint:disable:no-empty-interface
6
- export interface PrivateKeyInternals {
7
- /***/
8
- version: Buffer;
9
- modulus: Buffer;
10
- publicExponent: Buffer;
11
- privateExponent: Buffer;
12
- prime1: Buffer;
13
- prime2: Buffer;
14
- exponent1: Buffer;
15
- exponent2: Buffer;
16
- }
17
-
18
- function f(buffer: Buffer, b: BlockInfo) {
19
- return buffer.subarray(b.position + 1, b.position + b.length);
20
- }
21
- const doDebug = !!process.env.DEBUG;
22
- /**
23
- *
24
- * @param privateKey RSAPrivateKey ::= SEQUENCE {
25
- * version Version,
26
- * modulus INTEGER, -- n
27
- * publicExponent INTEGER, -- e
28
- * privateExponent INTEGER, -- d
29
- * prime1 INTEGER, -- p
30
- * prime2 INTEGER, -- q
31
- * exponent1 INTEGER, -- d mod (p-1)
32
- * exponent2 INTEGER, -- d mod (q-1)
33
- * coefficient INTEGER, -- (inverse of q) mod p
34
- * otherPrimeInfos OtherPrimeInfos OPTIONAL
35
- }
36
- */
37
- export function explorePrivateKey(privateKey1: PrivateKey): PrivateKeyInternals {
38
- const privateKey = privateKey1.export({ format: "der", type: "pkcs1" }) as Buffer;
39
- assert(privateKey instanceof Buffer);
40
- const block_info = readTag(privateKey, 0);
41
- const blocks = _readStruct(privateKey, block_info);
42
-
43
- if (blocks.length === 9) {
44
- // alice_rsa
45
- const version = f(privateKey, blocks[0]); // _readIntegerAsByteString(privateKey, blocks1[0]);
46
- const modulus = f(privateKey, blocks[1]);
47
- const publicExponent = f(privateKey, blocks[2]);
48
- const privateExponent = f(privateKey, blocks[3]);
49
- const prime1 = f(privateKey, blocks[4]);
50
- const prime2 = f(privateKey, blocks[5]);
51
- const exponent1 = f(privateKey, blocks[6]);
52
- const exponent2 = f(privateKey, blocks[7]);
53
-
54
- return {
55
- version,
56
- modulus,
57
- publicExponent,
58
- privateExponent,
59
- prime1,
60
- prime2,
61
- exponent1,
62
- exponent2,
63
- };
64
- }
65
- /* istanbul ignore next */
66
- if (doDebug) {
67
- // tslint:disable:no-console
68
- console.log("-------------------- private key:");
69
- console.log(block_info);
70
-
71
- // tslint:disable:no-console
72
- console.log(
73
- blocks.map((b) => ({
74
- tag: TagType[b.tag] + " 0x" + b.tag.toString(16),
75
- l: b.length,
76
- p: b.position,
77
- buff: privateKey.subarray(b.position, b.position + b.length).toString("hex"),
78
- }))
79
- );
80
- }
81
-
82
- const b = blocks[2];
83
- const bb = privateKey.subarray(b.position, b.position + b.length);
84
- const block_info1 = readTag(bb, 0);
85
- const blocks1 = _readStruct(bb, block_info1);
86
-
87
- /* istanbul ignore next */
88
- if (doDebug) {
89
- // tslint:disable:no-console
90
- console.log(
91
- blocks1.map((b) => ({
92
- tag: TagType[b.tag] + " 0x" + b.tag.toString(16),
93
- l: b.length,
94
- p: b.position,
95
- buff: bb.subarray(b.position, b.position + b.length).toString("hex"),
96
- }))
97
- );
98
- }
99
-
100
- const version = f(bb, blocks1[0]);
101
- const modulus = f(bb, blocks1[1]);
102
- const publicExponent = f(bb, blocks1[2]);
103
- const privateExponent = f(bb, blocks1[3]);
104
- const prime1 = f(bb, blocks1[4]);
105
- const prime2 = f(bb, blocks1[5]);
106
- const exponent1 = f(bb, blocks1[6]);
107
- const exponent2 = f(bb, blocks1[7]);
108
-
109
- return {
110
- version,
111
- modulus,
112
- publicExponent,
113
- privateExponent,
114
- prime1,
115
- prime2,
116
- exponent1,
117
- exponent2,
118
- };
119
- }
package/source/index.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * @module node_opcua_crypto
3
- */
4
- export * from "./common";
5
- export * from "./derived_keys";
6
- export * from "./explore_certificate";
7
- export * from "./crypto_utils";
8
- export * from "./crypto_explore_certificate";
9
- export * from "./verify_certificate_signature";
10
- export * from "./explore_certificate_revocation_list";
11
- export * from "./explore_certificate_signing_request";
12
- export * from "./explore_private_key";
13
- export * from "./public_private_match";
@@ -1,37 +0,0 @@
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
- const i = exploreCertificate(certificate);
8
- const j = explorePrivateKey(privateKey);
9
-
10
- const modulus1 = i.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey.modulus;
11
- const modulus2 = j.modulus;
12
-
13
- if (modulus1.length != modulus2.length) {
14
- return false;
15
- }
16
- return modulus1.toString("hex") === modulus2.toString("hex");
17
- }
18
-
19
- /**
20
- * check that the given certificate matches the given private key
21
- * @param certificate
22
- * @param privateKey
23
- */
24
- function certificateMatchesPrivateKeyPEM(certificate: CertificatePEM, privateKey: PrivateKey, blockSize: number): boolean {
25
- const initialBuffer = Buffer.from("Lorem Ipsum");
26
- const encryptedBuffer = publicEncrypt_long(initialBuffer, certificate, blockSize, 11);
27
- const decryptedBuffer = privateDecrypt_long(encryptedBuffer, privateKey, blockSize);
28
- const finalString = decryptedBuffer.toString("utf-8");
29
- return initialBuffer.toString("utf-8") === finalString;
30
- }
31
-
32
- export function certificateMatchesPrivateKey(certificate: Certificate, privateKey: PrivateKey): boolean {
33
- const e = explorePrivateKey(privateKey);
34
- const blockSize = e.modulus.length;
35
- const certificatePEM = toPem(certificate, "CERTIFICATE");
36
- return certificateMatchesPrivateKeyPEM(certificatePEM, privateKey, blockSize);
37
- }
@@ -1,105 +0,0 @@
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 +0,0 @@
1
- export * from "./read";
2
- export * from "./read_certificate_revocation_list";
3
- export * from "./read_certificate_signing_request";
@@ -1,105 +0,0 @@
1
- import * as assert from "assert";
2
- import * as fs from "fs";
3
- import * as path from "path";
4
- import { createPrivateKey, createPublicKey } from "crypto";
5
- import { Certificate, CertificatePEM, DER, PEM, PrivateKey, PrivateKeyPEM, PublicKey, PublicKeyPEM } from "../source/common";
6
- import { convertPEMtoDER, identifyPemType } from "../source/crypto_utils";
7
-
8
- const sshpk = require("sshpk");
9
-
10
- function _readPemFile(filename: string): PEM {
11
- assert(typeof filename === "string");
12
- return fs.readFileSync(filename, "ascii");
13
- }
14
-
15
- function _readPemOrDerFileAsDER(filename: string): DER {
16
- if (filename.match(/.*\.der/)) {
17
- return fs.readFileSync(filename) as Buffer;
18
- }
19
- const raw_key: string = _readPemFile(filename);
20
- return convertPEMtoDER(raw_key);
21
- }
22
-
23
- /**
24
- * read a DER or PEM certificate from file
25
- */
26
- export function readCertificate(filename: string): Certificate {
27
- return _readPemOrDerFileAsDER(filename) as Certificate;
28
- }
29
-
30
- /**
31
- * read a DER or PEM certificate from file
32
- */
33
- export function readPublicKey(filename: string): PublicKey {
34
- if (filename.match(/.*\.der/)) {
35
- const der = fs.readFileSync(filename) as Buffer;
36
- return createPublicKey(der);
37
- } else {
38
- const raw_key: string = _readPemFile(filename);
39
- return createPublicKey(raw_key);
40
- }
41
- }
42
-
43
- function myCreatePrivateKey(rawKey: string | Buffer) {
44
- // see https://askubuntu.com/questions/1409458/openssl-config-cuases-error-in-node-js-crypto-how-should-the-config-be-updated
45
- const backup = process.env.OPENSSL_CONF;
46
- process.env.OPENSSL_CONF = "/dev/null";
47
- const retValue = createPrivateKey(rawKey);
48
- process.env.OPENSSL_CONF = backup;
49
- return retValue;
50
- }
51
- /**
52
- * read a DER or PEM certificate from file
53
- */
54
- export function readPrivateKey(filename: string): PrivateKey {
55
- if (filename.match(/.*\.der/)) {
56
- const der = fs.readFileSync(filename) as Buffer;
57
- return myCreatePrivateKey(der);
58
- } else {
59
- const raw_key: string = _readPemFile(filename);
60
- return myCreatePrivateKey(raw_key);
61
- }
62
- }
63
-
64
- export function readCertificatePEM(filename: string): CertificatePEM {
65
- return _readPemFile(filename);
66
- }
67
-
68
- export function readPublicKeyPEM(filename: string): PublicKeyPEM {
69
- return _readPemFile(filename);
70
- }
71
-
72
- export function readPrivateKeyPEM(filename: string): PrivateKeyPEM {
73
- return _readPemFile(filename);
74
- }
75
- let __certificate_store = path.join(__dirname, "../../certificates/");
76
-
77
- export function setCertificateStore(store: string): string {
78
- const old_store = __certificate_store;
79
- __certificate_store = store;
80
- return old_store;
81
- }
82
-
83
- /**
84
- *
85
- * @param filename
86
- */
87
- export function readPrivateRsaKey(filename: string): PrivateKey {
88
- if (filename.substring(0, 1) !== "." && !fs.existsSync(filename)) {
89
- filename = __certificate_store + filename;
90
- }
91
- const content = fs.readFileSync(filename, "ascii");
92
- const sshKey = sshpk.parsePrivateKey(content, "auto");
93
- const key = sshKey.toString("pkcs1") as PEM;
94
- return createPrivateKey({ format: "pem", type: "pkcs1", key });
95
- }
96
-
97
- export function readPublicRsaKey(filename: string): PublicKey {
98
- if (filename.substring(0, 1) !== "." && !fs.existsSync(filename)) {
99
- filename = __certificate_store + filename;
100
- }
101
- const content = fs.readFileSync(filename, "ascii");
102
- const sshKey = sshpk.parseKey(content, "ssh");
103
- const key = sshKey.toString("pkcs1") as PEM;
104
- return createPublicKey({ format: "pem", type: "pkcs1", key });
105
- }
@@ -1,14 +0,0 @@
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 +0,0 @@
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
- }