@blamejs/pki 0.1.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.
- package/CHANGELOG.md +32 -0
- package/LICENSE +201 -0
- package/MIGRATING.md +9 -0
- package/NOTICE +21 -0
- package/README.md +252 -0
- package/bin/pki.js +76 -0
- package/index.js +50 -0
- package/lib/asn1-der.js +576 -0
- package/lib/constants.js +115 -0
- package/lib/framework-error.js +130 -0
- package/lib/oid.js +219 -0
- package/lib/vendor/MANIFEST.json +4 -0
- package/lib/vendor/README.md +41 -0
- package/lib/webcrypto.js +542 -0
- package/lib/x509.js +320 -0
- package/package.json +74 -0
- package/sbom.cdx.json +61 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@blamejs/pki` are documented here. The format
|
|
4
|
+
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this
|
|
5
|
+
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## v0.1.1 — 2026-07-04
|
|
8
|
+
|
|
9
|
+
First published release of the 0.1.x foundation.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- First release published to npm. The toolkit surface is the 0.1.x foundation — pki.asn1 (strict DER codec), pki.oid (OID ↔ name registry), pki.x509.parse (DER/PEM certificate parsing), and pki.webcrypto (a W3C SubtleCrypto engine over node:crypto with ML-DSA/SLH-DSA signatures alongside the full classical set) — now available on npm with a SLSA provenance attestation, and served as the pkijs.com documentation container.
|
|
14
|
+
|
|
15
|
+
## v0.1.0 — 2026-07-04
|
|
16
|
+
|
|
17
|
+
Initial foundation — a PQC-first WebCrypto engine, a strict DER codec, an OID registry, and X.509 certificate parsing.
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- pki.webcrypto — a zero-dependency W3C Web Cryptography API (Crypto / SubtleCrypto / CryptoKey) built on Node's native node:crypto. PQC-first without being PQC-only: ML-DSA-44/65/87 and SLH-DSA signatures sit alongside the full classical set — RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP, ECDSA, ECDH, Ed25519/Ed448, AES-GCM/CBC/CTR/KW, HMAC, HKDF, PBKDF2, and the SHA family (including legacy SHA-1) — plus ML-KEM key generation. Every key and signature it emits is OpenSSL/NSS-interoperable.
|
|
22
|
+
- pki.asn1 — a strict, fail-closed DER decoder and canonical encoder with a navigable node tree, typed readers (integer, boolean, OID, bit string, octet string, time, string), and value builders. Rejects indefinite length, non-minimal encodings, and trailing bytes, and enforces size and depth caps (X.690).
|
|
23
|
+
- pki.oid — a two-way OID ↔ name registry with dotted/arc conversion, seeded with RFC 5280 attribute types and extensions, the classical signature/public-key/digest algorithms, and the NIST post-quantum arcs (ML-DSA, ML-KEM, SLH-DSA).
|
|
24
|
+
- pki.x509.parse — parse DER or PEM X.509 certificates into structured, validated fields: version, serial, signature algorithm, issuer/subject distinguished names, validity window as Date values, subject public-key info, and the extension list, with the exact tbsCertificate bytes exposed for downstream verification.
|
|
25
|
+
- pki.C — functional scale constants (C.TIME.*, C.BYTES.*) and shared codec limits.
|
|
26
|
+
- pki.errors — a PkiError taxonomy with a defineClass factory and stable domain/reason codes.
|
|
27
|
+
- pki command-line front-end (version, oid, parse).
|
|
28
|
+
|
|
29
|
+
### Security
|
|
30
|
+
|
|
31
|
+
- The DER decoder is fail-closed: non-DER shapes are rejected and size/depth caps are enforced before the parser walks the input, so a hostile length prefix cannot become a decoder denial-of-service.
|
|
32
|
+
- The crypto engine is fail-closed: an unknown algorithm, curve, or format is rejected rather than silently downgraded, and every sign/verify path returns a real verdict or throws.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 blamejs contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/MIGRATING.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Migrating
|
|
2
|
+
|
|
3
|
+
Operator-facing migration recipes per breaking change. The bulk of this file is auto-generated from `deprecate()`-marked surface in the toolkit — the running process warns about each (with `PKI_DEPRECATIONS=warn` set, or by default outside production) before the noted removal version. Re-run `node scripts/gen-migrating.js` before each release; the file is committed so operators can diff it against the prior tag.
|
|
4
|
+
|
|
5
|
+
**Out-of-band breaking changes** (on-disk format breaks, wire-encoding changes) cannot be expressed as `deprecate()` calls because there is no in-process runtime to warn from. They are hardcoded in the OUT_OF_BAND_BREAKS table inside `scripts/gen-migrating.js` so the operator sees the full upgrade path here without grepping the CHANGELOG.
|
|
6
|
+
|
|
7
|
+
## No active deprecations
|
|
8
|
+
|
|
9
|
+
The toolkit has no `deprecate()`-marked surface awaiting removal.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
@blamejs/pki
|
|
2
|
+
Copyright 2026 blamejs contributors
|
|
3
|
+
|
|
4
|
+
This product includes software developed by the blamejs project
|
|
5
|
+
(https://pkijs.com).
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0. See LICENSE for the full text.
|
|
8
|
+
|
|
9
|
+
================================================================================
|
|
10
|
+
Third-Party Components
|
|
11
|
+
================================================================================
|
|
12
|
+
|
|
13
|
+
@blamejs/pki ships with zero npm runtime dependencies and currently vendors no
|
|
14
|
+
third-party code. Its cryptography runs entirely on Node's built-in node:crypto
|
|
15
|
+
(classical and FIPS 203/204/205 post-quantum algorithms via the platform
|
|
16
|
+
OpenSSL that the Node engine floor ships).
|
|
17
|
+
|
|
18
|
+
Should a third-party library ever be vendored under lib/vendor/ — only when a
|
|
19
|
+
required operation is confirmed missing from the Node engine floor — it will
|
|
20
|
+
retain its original copyright and license, be attributed here, and be tracked
|
|
21
|
+
with a pinned SHA-256 in lib/vendor/MANIFEST.json.
|
package/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<img src="assets/pkijs-logo.png" alt="@blamejs/pki" width="200" />
|
|
4
|
+
|
|
5
|
+
# @blamejs/pki
|
|
6
|
+
|
|
7
|
+
**A pure-JavaScript PKI toolkit that owns its stack.**
|
|
8
|
+
|
|
9
|
+
X.509, ASN.1/DER, OID, CMS, OCSP, timestamping, and PKCS formats — with an
|
|
10
|
+
in-house, fail-closed DER codec and a post-quantum-first algorithm registry.
|
|
11
|
+
No npm runtime dependencies. No TypeScript. No Web Crypto ceiling.
|
|
12
|
+
|
|
13
|
+
[](https://www.npmjs.com/package/@blamejs/pki)
|
|
14
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
15
|
+
[](https://nodejs.org)
|
|
16
|
+
[](#security-posture)
|
|
17
|
+
[](#security-posture)
|
|
18
|
+
|
|
19
|
+
[pkijs.com](https://pkijs.com) · [Roadmap](ROADMAP.md) · [Security](SECURITY.md) · [Changelog](CHANGELOG.md)
|
|
20
|
+
|
|
21
|
+
</div>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Why this toolkit
|
|
26
|
+
|
|
27
|
+
Most JavaScript PKI code inherits its ASN.1 parser and its algorithm coverage
|
|
28
|
+
from somewhere else — an external DER library with its own CVE history, or the
|
|
29
|
+
Web Crypto API with its limits on streaming, opaque keys, and algorithm reach.
|
|
30
|
+
`@blamejs/pki` owns those layers:
|
|
31
|
+
|
|
32
|
+
- **Its own DER codec.** Strict, canonical, bounded. Malformed input is rejected
|
|
33
|
+
in bounded time, not walked into a stack overflow.
|
|
34
|
+
- **An OID-named algorithm registry.** Every algorithm, attribute, and extension
|
|
35
|
+
is named through one two-way OID table (`pki.oid`), so a new signature or KEM
|
|
36
|
+
algorithm — including a post-quantum one — is a registry entry, not a special
|
|
37
|
+
case in `parse`. OID-driven sign/verify resolution rides the same table as the
|
|
38
|
+
signing surface lands.
|
|
39
|
+
- **Fail-closed everywhere.** Every parse, sign, and verify path throws on
|
|
40
|
+
failure. No path returns zero, a default, or partial output in place of a real
|
|
41
|
+
verdict.
|
|
42
|
+
- **Zero dependencies in your `package.json`.** The cryptography runs on Node's
|
|
43
|
+
built-in `node:crypto` — the full classical set plus post-quantum ML-DSA and
|
|
44
|
+
SLH-DSA signatures via the platform OpenSSL 3.5. ML-KEM key generation is
|
|
45
|
+
available today, with KEM encapsulation on the roadmap. Nothing is vendored,
|
|
46
|
+
nothing is installed; `npm audit` has nothing to say because there is no
|
|
47
|
+
dependency tree.
|
|
48
|
+
|
|
49
|
+
## Install
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm i @blamejs/pki
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Requires Node.js 24.18+ (runs on the shipped runtime — no build step, no
|
|
56
|
+
transpilation).
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
var pki = require("@blamejs/pki");
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Quickstart
|
|
63
|
+
|
|
64
|
+
### Parse an X.509 certificate
|
|
65
|
+
|
|
66
|
+
`pki.x509.parse` accepts a DER `Buffer` or a PEM string/Buffer and returns a
|
|
67
|
+
fully-decoded, validated certificate — distinguished names rendered and
|
|
68
|
+
structured, the validity window as real `Date`s, algorithms and extensions
|
|
69
|
+
named through the OID registry, and the exact signed `tbsBytes` for a downstream
|
|
70
|
+
verifier.
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
var pki = require("@blamejs/pki");
|
|
74
|
+
var fs = require("node:fs");
|
|
75
|
+
|
|
76
|
+
var pem = fs.readFileSync("cert.pem", "utf8");
|
|
77
|
+
var cert = pki.x509.parse(pem);
|
|
78
|
+
|
|
79
|
+
cert.subject.dn; // "CN=example.com, O=Example Org, C=US"
|
|
80
|
+
cert.issuer.dn; // "CN=example.com, O=Example Org, C=US"
|
|
81
|
+
cert.serialNumberHex; // "7057e1ebeec2e5f7…"
|
|
82
|
+
cert.signatureAlgorithm.name; // "sha256WithRSAEncryption"
|
|
83
|
+
cert.subjectPublicKeyInfo.algorithm.name; // "rsaEncryption"
|
|
84
|
+
cert.validity.notAfter; // Date — 2027-07-04T07:16:15.000Z
|
|
85
|
+
|
|
86
|
+
cert.extensions.forEach(function (ext) {
|
|
87
|
+
ext.name; // "subjectKeyIdentifier" (or null when the OID is unknown)
|
|
88
|
+
ext.critical; // boolean
|
|
89
|
+
ext.value; // Buffer — the raw extnValue OCTET STRING contents
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Malformed bytes throw a typed error rather than returning a half-parsed object:
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
try {
|
|
97
|
+
pki.x509.parse(Buffer.from([0x30, 0x03, 0x02, 0x01, 0x00]));
|
|
98
|
+
} catch (e) {
|
|
99
|
+
e.constructor.name; // "CertificateError"
|
|
100
|
+
e.code; // e.g. "x509/not-a-certificate" — stable domain/reason string
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Convert PEM ↔ DER
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
var der = pki.x509.pemDecode(pem, "CERTIFICATE"); // Buffer of DER bytes
|
|
108
|
+
var out = pki.x509.pemEncode(der, "CERTIFICATE"); // 64-column PEM string
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Decode and build ASN.1 / DER directly
|
|
112
|
+
|
|
113
|
+
The codec under every structure is public. Decode returns a zero-copy node tree;
|
|
114
|
+
the builders emit canonical DER.
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// Build a canonical-DER SEQUENCE, then decode it back.
|
|
118
|
+
var der = pki.asn1.build.sequence([
|
|
119
|
+
pki.asn1.build.oid("2.5.4.3"), // commonName
|
|
120
|
+
pki.asn1.build.utf8("example.com"),
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
var node = pki.asn1.decode(der);
|
|
124
|
+
node.tagNumber === pki.asn1.TAGS.SEQUENCE; // true
|
|
125
|
+
node.children.length; // 2
|
|
126
|
+
pki.asn1.read.oid(node.children[0]); // "2.5.4.3"
|
|
127
|
+
pki.asn1.read.string(node.children[1]); // "example.com"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The decoder is strict by construction — non-DER shapes are refused, not
|
|
131
|
+
tolerated:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
try {
|
|
135
|
+
pki.asn1.decode(Buffer.from([0x30, 0x80, 0x00, 0x00])); // indefinite length
|
|
136
|
+
} catch (e) {
|
|
137
|
+
e.constructor.name; // "Asn1Error"
|
|
138
|
+
e.code; // "asn1/indefinite-length"
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Size and depth are bounded before a byte is walked; override the caps per call
|
|
143
|
+
when you need to:
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
pki.asn1.decode(der, { maxBytes: pki.C.BYTES.mib(4), maxDepth: 32 });
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Resolve object identifiers
|
|
150
|
+
|
|
151
|
+
Every algorithm, attribute type, and extension is named by an OID. The registry
|
|
152
|
+
is a two-way map, seeded with the RFC 5280 set, the classical algorithm set, and
|
|
153
|
+
the NIST post-quantum arcs (ML-DSA, ML-KEM, SLH-DSA).
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
pki.oid.name("1.2.840.113549.1.1.11"); // "sha256WithRSAEncryption"
|
|
157
|
+
pki.oid.byName("sha256"); // "2.16.840.1.101.3.4.2.1"
|
|
158
|
+
pki.oid.toArcs("2.5.4.3"); // [2, 5, 4, 3]
|
|
159
|
+
|
|
160
|
+
// Extend it with your own arc:
|
|
161
|
+
pki.oid.register("1.3.6.1.4.1.99999.1", "acmeCorpExtension");
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Sign with post-quantum ML-DSA — or any classical algorithm
|
|
165
|
+
|
|
166
|
+
`pki.webcrypto` is a standard W3C WebCrypto (`SubtleCrypto`) engine over
|
|
167
|
+
`node:crypto`. The post-quantum suite lives in the same API as RSA, ECDSA, and
|
|
168
|
+
EdDSA — pick the algorithm, the rest is identical:
|
|
169
|
+
|
|
170
|
+
```js
|
|
171
|
+
var subtle = pki.webcrypto.subtle;
|
|
172
|
+
var data = Buffer.from("sign me");
|
|
173
|
+
|
|
174
|
+
// FIPS 204 ML-DSA-65 — a post-quantum signature.
|
|
175
|
+
var kp = await subtle.generateKey({ name: "ML-DSA-65" }, true, ["sign", "verify"]);
|
|
176
|
+
var sig = await subtle.sign({ name: "ML-DSA-65" }, kp.privateKey, data);
|
|
177
|
+
var ok = await subtle.verify({ name: "ML-DSA-65" }, kp.publicKey, sig, data); // true
|
|
178
|
+
|
|
179
|
+
// The classical set — ECDSA, RSA-PSS, Ed25519, AES-GCM, ECDH, HKDF, … — is the
|
|
180
|
+
// same call shape, and every key it exports is OpenSSL/NSS-interoperable.
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## What ships today (v0.1.0)
|
|
184
|
+
|
|
185
|
+
The core codec and certificate-reading surface are here and stable. Everything
|
|
186
|
+
is callable today; nothing below is a stub.
|
|
187
|
+
|
|
188
|
+
| Namespace | What it does |
|
|
189
|
+
|---|---|
|
|
190
|
+
| `pki.asn1` | Strict, bounded DER codec — `decode` (zero-copy node tree), `encode`, `build.*` canonical-DER value builders, `read.*` typed readers, `TAGS`, OID-content encode/decode |
|
|
191
|
+
| `pki.oid` | Two-way OID ↔ name registry — `name`, `byName`, `register`, `toArcs`/`fromArcs`, `toDER`/`fromDER`; seeded with RFC 5280 + NIST PQC arcs |
|
|
192
|
+
| `pki.webcrypto` | A W3C WebCrypto (`SubtleCrypto`) engine over `node:crypto` — `sign`/`verify`/`encrypt`/`decrypt`/`deriveBits`/`digest`/`generateKey`/`importKey`/`exportKey` across RSA, ECDSA, ECDH, Ed25519/Ed448, AES, HMAC, HKDF, PBKDF2, SHA — **and** post-quantum ML-DSA-44/65/87 and SLH-DSA signatures, plus ML-KEM-512/768/1024 key generation (KEM encapsulation on the roadmap). Zero-dependency, OpenSSL-interoperable |
|
|
193
|
+
| `pki.x509` | Parse DER / PEM certificates into structured, validated fields — `parse`, `pemDecode`, `pemEncode` |
|
|
194
|
+
| `pki.C` / `pki.constants` | Version-stable constants — functional scale helpers (`C.TIME.*`, `C.BYTES.*`), codec `LIMITS`, `version` |
|
|
195
|
+
| `pki.errors` | The `PkiError` taxonomy — `defineClass` plus `ConstantsError` / `Asn1Error` / `OidError` / `PemError` / `CertificateError`, each carrying a stable `code` in `domain/reason` form |
|
|
196
|
+
| `pki` CLI | `pki version`, `pki oid <dotted\|name>`, `pki parse <cert>` |
|
|
197
|
+
|
|
198
|
+
### CLI
|
|
199
|
+
|
|
200
|
+
```sh
|
|
201
|
+
pki version # @blamejs/pki v0.1.0
|
|
202
|
+
pki oid 1.2.840.113549.1.1.11 # sha256WithRSAEncryption
|
|
203
|
+
pki oid sha256 # 2.16.840.1.101.3.4.2.1
|
|
204
|
+
pki parse cert.pem # structured JSON summary of a certificate
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### What's coming
|
|
208
|
+
|
|
209
|
+
Certificate build/sign/verify, CRLs, CSRs (PKCS#10), certification-path
|
|
210
|
+
validation, CMS (SignedData / EnvelopedData / EncryptedData / AuthenticatedData),
|
|
211
|
+
OCSP, RFC 3161 timestamping, PKCS#8 / SPKI / PBES2 / PKCS#12, and the
|
|
212
|
+
post-quantum certificate and CMS surface (ML-DSA / ML-KEM / SLH-DSA and hybrid
|
|
213
|
+
composites) are on the roadmap and ride this same core. See
|
|
214
|
+
[ROADMAP.md](ROADMAP.md) for the full plan and current status of each area, and
|
|
215
|
+
[CHANGELOG.md](CHANGELOG.md) for what has landed.
|
|
216
|
+
|
|
217
|
+
## Security posture
|
|
218
|
+
|
|
219
|
+
- **Zero npm runtime dependencies, nothing vendored.** The cryptography runs on
|
|
220
|
+
Node's built-in `node:crypto`; the toolkit vendors no third-party code — a
|
|
221
|
+
platform built-in ships zero bytes and stays OpenSSL-interoperable by
|
|
222
|
+
construction. There is no dependency tree, transitive or vendored, to
|
|
223
|
+
compromise or keep current.
|
|
224
|
+
- **Fail-closed DER.** The decoder rejects every non-canonical shape — indefinite
|
|
225
|
+
length, non-minimal length or tag encodings, trailing bytes, over-long or
|
|
226
|
+
over-deep input — with a typed `Asn1Error` before it walks the structure. Size
|
|
227
|
+
and depth caps are enforced up front, so adversarial input is bounded work, not
|
|
228
|
+
a stack overflow.
|
|
229
|
+
- **Fail-closed verification.** Every verify path throws on failure. A default
|
|
230
|
+
that accepts-on-error is treated as a bug, not an ergonomic.
|
|
231
|
+
- **PQC-first crypto.** Post-quantum ML-DSA and SLH-DSA signatures run in the
|
|
232
|
+
WebCrypto engine (`pki.webcrypto`) alongside the classical set today, and
|
|
233
|
+
ML-KEM key generation is available with KEM encapsulation on the roadmap.
|
|
234
|
+
Every algorithm is named in the OID registry (`pki.oid`); OID-driven
|
|
235
|
+
sign/verify resolution rides that registry as the signing surface lands, and
|
|
236
|
+
there is no classical-only default where a post-quantum option exists.
|
|
237
|
+
- **Signed releases.** Release tags are annotated and SSH-signed; published
|
|
238
|
+
tarballs carry provenance and an SBOM. See
|
|
239
|
+
[SECURITY.md → Verifying release authenticity](SECURITY.md#verifying-release-authenticity).
|
|
240
|
+
|
|
241
|
+
Report a vulnerability privately — see [SECURITY.md](SECURITY.md). For usage
|
|
242
|
+
questions and support channels, see [SUPPORT.md](SUPPORT.md).
|
|
243
|
+
|
|
244
|
+
## Documentation
|
|
245
|
+
|
|
246
|
+
Full primitive-by-primitive reference lives at [pkijs.com](https://pkijs.com),
|
|
247
|
+
generated from the source comment blocks so it cannot drift from the shipped API.
|
|
248
|
+
|
|
249
|
+
## License
|
|
250
|
+
|
|
251
|
+
[Apache-2.0](LICENSE). Third-party attribution — currently none, since the
|
|
252
|
+
toolkit vendors nothing — is tracked in [NOTICE](NOTICE).
|
package/bin/pki.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Copyright (c) blamejs contributors
|
|
4
|
+
"use strict";
|
|
5
|
+
/**
|
|
6
|
+
* pki — command-line front-end for @blamejs/pki.
|
|
7
|
+
*
|
|
8
|
+
* pki version
|
|
9
|
+
* pki oid <dotted|name> resolve an OID <-> name
|
|
10
|
+
* pki parse <cert.pem|cert.der> parse an X.509 certificate and print
|
|
11
|
+
* its fields as JSON
|
|
12
|
+
*
|
|
13
|
+
* The CLI is a thin operator convenience over the library surface; it
|
|
14
|
+
* validates its arguments (entry-point tier — bad input exits non-zero
|
|
15
|
+
* with a message) and never does anything the public API can't.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
var fs = require("node:fs");
|
|
19
|
+
var pki = require("../index.js");
|
|
20
|
+
|
|
21
|
+
function fail(msg) {
|
|
22
|
+
process.stderr.write("pki: " + msg + "\n");
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function cmdVersion() {
|
|
27
|
+
process.stdout.write("@blamejs/pki v" + pki.version + "\n");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function cmdOid(arg) {
|
|
31
|
+
if (!arg) fail("usage: pki oid <dotted|name>");
|
|
32
|
+
if (/^\d+(\.\d+)+$/.test(arg)) {
|
|
33
|
+
var name = pki.oid.name(arg);
|
|
34
|
+
process.stdout.write((name || "(unregistered)") + "\n");
|
|
35
|
+
} else {
|
|
36
|
+
var dotted = pki.oid.byName(arg);
|
|
37
|
+
if (!dotted) fail("unknown OID name: " + arg);
|
|
38
|
+
process.stdout.write(dotted + "\n");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function cmdParse(file) {
|
|
43
|
+
if (!file) fail("usage: pki parse <cert.pem|cert.der>");
|
|
44
|
+
var bytes;
|
|
45
|
+
try { bytes = fs.readFileSync(file); } catch (e) { return fail("cannot read " + file + ": " + e.message); }
|
|
46
|
+
var cert;
|
|
47
|
+
try { cert = pki.x509.parse(bytes); } catch (e) { return fail(e.code + ": " + e.message); }
|
|
48
|
+
var view = {
|
|
49
|
+
version: cert.version,
|
|
50
|
+
serialNumber: cert.serialNumberHex,
|
|
51
|
+
subject: cert.subject.dn,
|
|
52
|
+
issuer: cert.issuer.dn,
|
|
53
|
+
notBefore: cert.validity.notBefore.toISOString(),
|
|
54
|
+
notAfter: cert.validity.notAfter.toISOString(),
|
|
55
|
+
signatureAlgorithm: cert.signatureAlgorithm.name || cert.signatureAlgorithm.oid,
|
|
56
|
+
publicKeyAlgorithm: cert.subjectPublicKeyInfo.algorithm.name || cert.subjectPublicKeyInfo.algorithm.oid,
|
|
57
|
+
extensions: cert.extensions.map(function (e) { return { oid: e.oid, name: e.name, critical: e.critical }; }),
|
|
58
|
+
};
|
|
59
|
+
process.stdout.write(JSON.stringify(view, null, 2) + "\n");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function main(argv) {
|
|
63
|
+
var cmd = argv[0];
|
|
64
|
+
switch (cmd) {
|
|
65
|
+
case "version": case "--version": case "-v": return cmdVersion();
|
|
66
|
+
case "oid": return cmdOid(argv[1]);
|
|
67
|
+
case "parse": return cmdParse(argv[1]);
|
|
68
|
+
case undefined: case "help": case "--help": case "-h":
|
|
69
|
+
process.stdout.write("usage: pki <version|oid|parse> [args]\n");
|
|
70
|
+
return;
|
|
71
|
+
default:
|
|
72
|
+
return fail("unknown command: " + cmd);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
main(process.argv.slice(2));
|
package/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (c) blamejs contributors
|
|
3
|
+
"use strict";
|
|
4
|
+
/**
|
|
5
|
+
* @blamejs/pki — public API entry point.
|
|
6
|
+
*
|
|
7
|
+
* A pure-JavaScript PKI toolkit that owns its stack. Zero npm runtime
|
|
8
|
+
* dependencies; strict, fail-closed parsing; post-quantum-first crypto.
|
|
9
|
+
*
|
|
10
|
+
* Public surface lives on the exported object below. Notable groupings:
|
|
11
|
+
*
|
|
12
|
+
* Core: C / constants (scale helpers + version), errors
|
|
13
|
+
* (PkiError taxonomy), asn1 (strict DER codec), oid
|
|
14
|
+
* (OID ↔ name registry), webcrypto (W3C SubtleCrypto
|
|
15
|
+
* engine over node:crypto — PQC-first, classical-capable)
|
|
16
|
+
* Certificates: x509 (parse DER / PEM certificates)
|
|
17
|
+
*
|
|
18
|
+
* The surface grows per ROADMAP.md — CMS, OCSP, CRL, CSR, PKCS#8/#12,
|
|
19
|
+
* timestamping, path validation, and the post-quantum algorithm set are
|
|
20
|
+
* targeted additions that ride the same core.
|
|
21
|
+
*
|
|
22
|
+
* See LICENSE (Apache-2.0) and NOTICE for vendored attribution.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var constants = require("./lib/constants");
|
|
26
|
+
var errors = require("./lib/framework-error");
|
|
27
|
+
var asn1 = require("./lib/asn1-der");
|
|
28
|
+
var oid = require("./lib/oid");
|
|
29
|
+
var webcrypto = require("./lib/webcrypto");
|
|
30
|
+
var x509 = require("./lib/x509");
|
|
31
|
+
|
|
32
|
+
module.exports = {
|
|
33
|
+
version: constants.version,
|
|
34
|
+
// `C` is the terse call-site alias; `constants` the discoverable name.
|
|
35
|
+
C: constants,
|
|
36
|
+
constants: constants,
|
|
37
|
+
errors: errors,
|
|
38
|
+
asn1: asn1,
|
|
39
|
+
oid: oid,
|
|
40
|
+
x509: x509,
|
|
41
|
+
// A ready W3C Crypto instance (globalThis.crypto shape) + the classes
|
|
42
|
+
// for constructing more. PQC-first, classical-capable, zero-dep.
|
|
43
|
+
webcrypto: webcrypto.webcrypto,
|
|
44
|
+
WebCrypto: {
|
|
45
|
+
Crypto: webcrypto.Crypto,
|
|
46
|
+
SubtleCrypto: webcrypto.SubtleCrypto,
|
|
47
|
+
CryptoKey: webcrypto.CryptoKey,
|
|
48
|
+
WebCryptoError: webcrypto.WebCryptoError,
|
|
49
|
+
},
|
|
50
|
+
};
|