micro509 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +123 -6
- package/README.md +3 -0
- package/dist/crypto/crypto.d.ts +2 -3
- package/dist/crypto.d.ts +6 -0
- package/dist/der/der.d.ts +14 -4
- package/dist/der/der.js +19 -19
- package/dist/der.d.ts +26 -2
- package/dist/der.js +4 -4
- package/dist/index.d.ts +82 -15
- package/dist/index.js +19 -19
- package/dist/internal/asn1/asn1.d.ts +1 -2
- package/dist/internal/asn1/der.d.ts +1 -2
- package/dist/internal/asn1/oids.js +71 -353
- package/dist/internal/asn1/oids.js.map +1 -1
- package/dist/internal/asn1/oids.json.js +357 -0
- package/dist/internal/asn1/oids.json.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +1 -1
- package/dist/internal/crypto/pbes2.d.ts +1 -2
- package/dist/internal/crypto/pbes2.js +70 -70
- package/dist/internal/crypto/prime-ec.js +37 -0
- package/dist/internal/crypto/prime-ec.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +50 -50
- package/dist/internal/crypto/sig-verify.d.ts +1 -2
- package/dist/internal/crypto/sig-verify.js +60 -60
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +26 -26
- package/dist/internal/jose/json-data.js +108 -0
- package/dist/internal/jose/json-data.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +1 -2
- package/dist/internal/shared/dn.d.ts +1 -2
- package/dist/internal/shared/dn.js +1 -1
- package/dist/internal/shared/ip.d.ts +1 -2
- package/dist/internal/verify/name-constraints-engine.js +64 -64
- package/dist/internal/verify/policy-engine.js +1 -1
- package/dist/internal/verify/verify-path.d.ts +1 -2
- package/dist/internal/verify/verify-path.js +19 -19
- package/dist/internal/x509/directory-name.js +10 -10
- package/dist/internal/x509/extension-errors.d.ts +1 -2
- package/dist/internal/x509/extension-registry.js +18 -18
- package/dist/internal/x509/general-name-profile.js +9 -9
- package/dist/internal/x509/general-name.js +32 -32
- package/dist/internal/x509/name-fields.js +1 -1
- package/dist/jose/base64url.d.ts +4 -0
- package/dist/jose/base64url.js +14 -0
- package/dist/jose/base64url.js.map +1 -0
- package/dist/jose/errors.d.ts +9 -3
- package/dist/jose/errors.js.map +1 -1
- package/dist/jose/jwe.d.ts +64 -0
- package/dist/jose/jwe.js +188 -0
- package/dist/jose/jwe.js.map +1 -0
- package/dist/jose/jwks.d.ts +38 -10
- package/dist/jose/jwks.js +156 -96
- package/dist/jose/jwks.js.map +1 -1
- package/dist/jose/jws.d.ts +8 -6
- package/dist/jose/jws.js +216 -170
- package/dist/jose/jws.js.map +1 -1
- package/dist/jose/jwt.d.ts +8 -2
- package/dist/jose/jwt.js +98 -98
- package/dist/jose/remote-jwks.d.ts +1 -2
- package/dist/jose/remote-jwks.js +106 -108
- package/dist/jose/remote-jwks.js.map +1 -1
- package/dist/jose.d.ts +15 -4
- package/dist/jose.js +7 -5
- package/dist/kdf/argon2-arithmetic.js +15 -0
- package/dist/kdf/argon2-arithmetic.js.map +1 -0
- package/dist/kdf/argon2-core.js +486 -0
- package/dist/kdf/argon2-core.js.map +1 -0
- package/dist/kdf/argon2-memory.js +20 -0
- package/dist/kdf/argon2-memory.js.map +1 -0
- package/dist/kdf/argon2-portable.js +18 -0
- package/dist/kdf/argon2-portable.js.map +1 -0
- package/dist/kdf/argon2.d.ts +22 -11
- package/dist/kdf/argon2.js +32 -13
- package/dist/kdf/argon2.js.map +1 -1
- package/dist/kdf.d.ts +6 -0
- package/dist/keys/keys.d.ts +31 -6
- package/dist/keys/keys.js +315 -233
- package/dist/keys/keys.js.map +1 -1
- package/dist/keys.d.ts +11 -2
- package/dist/keys.js +2 -2
- package/dist/pem/pem.d.ts +1 -2
- package/dist/pem/pem.js +40 -25
- package/dist/pem/pem.js.map +1 -1
- package/dist/pem.d.ts +6 -0
- package/dist/pkcs/pfx.d.ts +2 -3
- package/dist/pkcs/pfx.js +142 -142
- package/dist/pkcs/pkcs12-mac.d.ts +1 -2
- package/dist/pkcs/pkcs12-mac.js +122 -122
- package/dist/pkcs/pkcs7.d.ts +14 -3
- package/dist/pkcs/pkcs7.js +268 -248
- package/dist/pkcs/pkcs7.js.map +1 -1
- package/dist/pkcs.d.ts +10 -1
- package/dist/pkcs.js +4 -4
- package/dist/result/result.d.ts +1 -2
- package/dist/result.d.ts +6 -0
- package/dist/revocation/chain.d.ts +2 -3
- package/dist/revocation/chain.js +52 -52
- package/dist/revocation/crl.d.ts +2 -3
- package/dist/revocation/crl.js +319 -319
- package/dist/revocation/crl.js.map +1 -1
- package/dist/revocation/ocsp.d.ts +2 -3
- package/dist/revocation/ocsp.js +395 -395
- package/dist/revocation/revocation.d.ts +2 -3
- package/dist/revocation/revocation.js +22 -22
- package/dist/revocation.d.ts +12 -3
- package/dist/revocation.js +4 -4
- package/dist/ssh/ssh.d.ts +2 -3
- package/dist/ssh/ssh.js +165 -170
- package/dist/ssh/ssh.js.map +1 -1
- package/dist/ssh/wire.d.ts +1 -2
- package/dist/ssh/wire.js +10 -10
- package/dist/ssh.d.ts +8 -0
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +31 -31
- package/dist/verify/name-constraints.d.ts +9 -2
- package/dist/verify/policy.d.ts +9 -2
- package/dist/verify/verify.d.ts +3 -4
- package/dist/verify/verify.js +121 -121
- package/dist/verify.d.ts +9 -0
- package/dist/x509/certificate.d.ts +4 -5
- package/dist/x509/certificate.js +40 -40
- package/dist/x509/csr.d.ts +3 -4
- package/dist/x509/csr.js +15 -15
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +165 -165
- package/dist/x509/extensions.js.map +1 -1
- package/dist/x509/fingerprint.d.ts +1 -2
- package/dist/x509/name-text.d.ts +1 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +15 -15
- package/dist/x509/parse.d.ts +3 -4
- package/dist/x509/parse.js +423 -423
- package/dist/x509.d.ts +17 -4
- package/dist/x509.js +8 -8
- package/package.json +27 -34
- package/dist/internal/asn1/oids2.js +0 -75
- package/dist/internal/asn1/oids2.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { OIDS as f } from "../asn1/
|
|
1
|
+
import { childrenOf as e, decodeNonNegativeIntegerNumber as t, decodeObjectIdentifier as n, requireElement as r } from "../asn1/asn1.js";
|
|
2
|
+
import { DECODE_LIMIT_CODES as i, rethrowDecodeRefusal as a } from "../asn1/decode-refusal.js";
|
|
3
|
+
import { explicitContext as o, integerFromNumber as s, nullValue as c, objectIdentifier as l, readRootElement as u, sequence as d } from "../asn1/der.js";
|
|
4
|
+
import { OIDS as f } from "../asn1/oids.js";
|
|
5
5
|
/** Return the canonical {@linkcode RsaPssParameters} profile for a given hash algorithm. */
|
|
6
6
|
function rsaPssParametersForHash(e) {
|
|
7
7
|
switch (e) {
|
|
@@ -28,12 +28,12 @@ function rsaPssParametersForHash(e) {
|
|
|
28
28
|
}
|
|
29
29
|
/** DER-encode an {@linkcode RsaPssParameters} profile as an ASN.1 RSASSA-PSS-params SEQUENCE. */
|
|
30
30
|
function encodeRsaPssParameters(e) {
|
|
31
|
-
let t = hashOidForName(e.hash),
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
...e.trailerField === 1 ? [] : [
|
|
31
|
+
let t = hashOidForName(e.hash), n = encodeHashAlgorithmIdentifier(t);
|
|
32
|
+
return d([
|
|
33
|
+
o(0, n),
|
|
34
|
+
o(1, d([l(f.mgf1), encodeHashAlgorithmIdentifier(t)])),
|
|
35
|
+
o(2, s(e.saltLength)),
|
|
36
|
+
...e.trailerField === 1 ? [] : [o(3, s(e.trailerField))]
|
|
37
37
|
]);
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
@@ -44,23 +44,23 @@ function encodeRsaPssParameters(e) {
|
|
|
44
44
|
*
|
|
45
45
|
* @throws `limit_exceeded` on an OID sub-identifier over 64 octets.
|
|
46
46
|
*/
|
|
47
|
-
function parseRsaPssParameters(
|
|
48
|
-
if (
|
|
47
|
+
function parseRsaPssParameters(e) {
|
|
48
|
+
if (e === void 0) return unsupportedResult("default_hash_sha1");
|
|
49
49
|
try {
|
|
50
|
-
let
|
|
51
|
-
if (
|
|
52
|
-
return validateRsaPssParameterProfile(parseRsaPssParameterFields(
|
|
53
|
-
} catch (
|
|
54
|
-
return
|
|
50
|
+
let t = u(e, { maxDepth: 64 });
|
|
51
|
+
if (t.tag !== 48) throw Error("RSA-PSS parameters must be a SEQUENCE");
|
|
52
|
+
return validateRsaPssParameterProfile(parseRsaPssParameterFields(e, t));
|
|
53
|
+
} catch (e) {
|
|
54
|
+
return a(e, i), {
|
|
55
55
|
ok: !1,
|
|
56
56
|
code: "malformed_rsa_pss_parameters",
|
|
57
|
-
reason:
|
|
57
|
+
reason: e instanceof Error ? e.message : "Malformed RSA-PSS parameters"
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
/** Parses the optional context-specific RSA-PSS fields into their defaulted state. */
|
|
62
|
-
function parseRsaPssParameterFields(
|
|
63
|
-
let
|
|
62
|
+
function parseRsaPssParameterFields(t, n) {
|
|
63
|
+
let r = {
|
|
64
64
|
hashOid: f.sha1,
|
|
65
65
|
maskGenAlgorithm: {
|
|
66
66
|
oid: f.mgf1,
|
|
@@ -73,8 +73,8 @@ function parseRsaPssParameterFields(e, t) {
|
|
|
73
73
|
sawSaltLength: !1,
|
|
74
74
|
sawTrailerField: !1
|
|
75
75
|
};
|
|
76
|
-
for (let
|
|
77
|
-
return
|
|
76
|
+
for (let i of e(t, n)) parseRsaPssParameterField(t, i, r);
|
|
77
|
+
return r;
|
|
78
78
|
}
|
|
79
79
|
function parseRsaPssParameterField(e, t, n) {
|
|
80
80
|
switch (t.tag) {
|
|
@@ -113,43 +113,43 @@ function validateRsaPssParameterProfile(e) {
|
|
|
113
113
|
} : unsupportedResult("unsupported_trailer_field") : unsupportedResult("unsupported_salt_length");
|
|
114
114
|
}
|
|
115
115
|
/** Unwrap an explicit context tag and assert it contains exactly one child element. */
|
|
116
|
-
function requireSingleExplicitChild(
|
|
117
|
-
let
|
|
118
|
-
if (
|
|
119
|
-
return
|
|
116
|
+
function requireSingleExplicitChild(t, n, i) {
|
|
117
|
+
let a = e(t, n);
|
|
118
|
+
if (a.length !== 1) throw Error(`RSA-PSS ${i} must wrap exactly one value`);
|
|
119
|
+
return r(a[0], `RSA-PSS ${i}`);
|
|
120
120
|
}
|
|
121
121
|
/** Extract a non-negative integer from an explicit context-tagged wrapper. */
|
|
122
|
-
function parseExplicitInteger(e,
|
|
123
|
-
let
|
|
124
|
-
if (
|
|
125
|
-
return
|
|
122
|
+
function parseExplicitInteger(e, n, r, i) {
|
|
123
|
+
let a = requireSingleExplicitChild(e, n, r);
|
|
124
|
+
if (a.tag !== 2) throw Error(`RSA-PSS ${r} must be an INTEGER`);
|
|
125
|
+
return t(a.value, i);
|
|
126
126
|
}
|
|
127
127
|
/** Parse an AlgorithmIdentifier SEQUENCE and return the algorithm OID. */
|
|
128
|
-
function parseHashAlgorithmIdentifier(
|
|
129
|
-
if (
|
|
130
|
-
let
|
|
131
|
-
if (
|
|
132
|
-
let
|
|
133
|
-
if (
|
|
134
|
-
if (
|
|
135
|
-
return
|
|
128
|
+
function parseHashAlgorithmIdentifier(t, i, a) {
|
|
129
|
+
if (i.tag !== 48) throw Error(`RSA-PSS ${a} must be a SEQUENCE`);
|
|
130
|
+
let o = e(t, i);
|
|
131
|
+
if (o.length === 0 || o.length > 2) throw Error(`Malformed RSA-PSS ${a} AlgorithmIdentifier`);
|
|
132
|
+
let s = r(o[0], `${a} OID`);
|
|
133
|
+
if (s.tag !== 6) throw Error(`Malformed RSA-PSS ${a} AlgorithmIdentifier`);
|
|
134
|
+
if (o.length === 2 && r(o[1], `${a} parameters`).tag !== 5) throw Error(`RSA-PSS ${a} AlgorithmIdentifier parameters must be NULL when present`);
|
|
135
|
+
return n(s.value);
|
|
136
136
|
}
|
|
137
137
|
/** Parse an MGF AlgorithmIdentifier SEQUENCE into OID and optional inner hash OID. */
|
|
138
|
-
function parseMaskGenAlgorithmIdentifier(
|
|
139
|
-
if (
|
|
140
|
-
let
|
|
141
|
-
if (
|
|
142
|
-
let
|
|
143
|
-
if (
|
|
144
|
-
let
|
|
145
|
-
return
|
|
146
|
-
oid:
|
|
147
|
-
hashOid: parseHashAlgorithmIdentifier(
|
|
148
|
-
} : { oid:
|
|
138
|
+
function parseMaskGenAlgorithmIdentifier(t, i) {
|
|
139
|
+
if (i.tag !== 48) throw Error("RSA-PSS maskGenAlgorithm must be a SEQUENCE");
|
|
140
|
+
let a = e(t, i);
|
|
141
|
+
if (a.length === 0 || a.length > 2) throw Error("Malformed RSA-PSS maskGenAlgorithm AlgorithmIdentifier");
|
|
142
|
+
let o = r(a[0], "maskGenAlgorithm OID");
|
|
143
|
+
if (o.tag !== 6) throw Error("Malformed RSA-PSS maskGenAlgorithm AlgorithmIdentifier");
|
|
144
|
+
let s = n(o.value);
|
|
145
|
+
return s === f.mgf1 ? {
|
|
146
|
+
oid: s,
|
|
147
|
+
hashOid: parseHashAlgorithmIdentifier(t, r(a[1], "maskGenAlgorithm parameters"), "MGF1 hashAlgorithm")
|
|
148
|
+
} : { oid: s };
|
|
149
149
|
}
|
|
150
150
|
/** DER-encode a hash AlgorithmIdentifier as `SEQUENCE { OID, NULL }`. */
|
|
151
151
|
function encodeHashAlgorithmIdentifier(e) {
|
|
152
|
-
return
|
|
152
|
+
return d([l(e), c()]);
|
|
153
153
|
}
|
|
154
154
|
/** Map a hash algorithm name to its ASN.1 OID string. */
|
|
155
155
|
function hashOidForName(e) {
|
|
@@ -24,5 +24,4 @@ export interface VerifySignedDataSuccess {
|
|
|
24
24
|
readonly valid: boolean;
|
|
25
25
|
}
|
|
26
26
|
/** Result of a full signature verification: validity, unsupported params, or a runtime verification failure. */
|
|
27
|
-
export type VerifySignedDataResult = VerifySignedDataSuccess | VerifySignatureConfigFailure | VerifySignedDataFailure;
|
|
28
|
-
//# sourceMappingURL=sig-verify.d.ts.map
|
|
27
|
+
export type VerifySignedDataResult = VerifySignedDataSuccess | VerifySignatureConfigFailure | VerifySignedDataFailure;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { toArrayBuffer as e } from "../asn1/asn1.js";
|
|
2
|
+
import { DECODE_LIMIT_CODES as t, rethrowDecodeRefusal as n } from "../asn1/decode-refusal.js";
|
|
3
|
+
import { readElement as r } from "../asn1/der.js";
|
|
4
|
+
import { OIDS as i } from "../asn1/oids.js";
|
|
5
|
+
import { alternateEcdsaSignatureEncoding as a, derEcdsaSignatureToRaw as o } from "./ecdsa.js";
|
|
6
6
|
import { parseRsaPssParameters as s } from "./rsa-pss.js";
|
|
7
7
|
import { getCrypto as c } from "./webcrypto.js";
|
|
8
8
|
import { importSpkiDerOrThrow as l } from "../../keys/keys.js";
|
|
@@ -14,46 +14,46 @@ import { importSpkiDerOrThrow as l } from "../../keys/keys.js";
|
|
|
14
14
|
* @module
|
|
15
15
|
*/
|
|
16
16
|
/** Non-throwing variant of {@linkcode getVerifySignatureConfig} — returns a typed result union. */
|
|
17
|
-
function getVerifySignatureConfigResult(e, t, n, r,
|
|
17
|
+
function getVerifySignatureConfigResult(e, t, n, r, a = "issuer") {
|
|
18
18
|
switch (e) {
|
|
19
|
-
case
|
|
19
|
+
case i.sha256WithRSAEncryption: {
|
|
20
20
|
let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-256 with RSA");
|
|
21
|
-
return e === void 0 ? requireRsaPublicKey(n, "SHA-256", "pkcs1-v1_5",
|
|
21
|
+
return e === void 0 ? requireRsaPublicKey(n, "SHA-256", "pkcs1-v1_5", a) : e;
|
|
22
22
|
}
|
|
23
|
-
case
|
|
23
|
+
case i.sha384WithRSAEncryption: {
|
|
24
24
|
let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-384 with RSA");
|
|
25
|
-
return e === void 0 ? requireRsaPublicKey(n, "SHA-384", "pkcs1-v1_5",
|
|
25
|
+
return e === void 0 ? requireRsaPublicKey(n, "SHA-384", "pkcs1-v1_5", a) : e;
|
|
26
26
|
}
|
|
27
|
-
case
|
|
27
|
+
case i.sha512WithRSAEncryption: {
|
|
28
28
|
let e = requireDerNullOrAbsentSignatureAlgorithmParameters(t, "SHA-512 with RSA");
|
|
29
|
-
return e === void 0 ? requireRsaPublicKey(n, "SHA-512", "pkcs1-v1_5",
|
|
29
|
+
return e === void 0 ? requireRsaPublicKey(n, "SHA-512", "pkcs1-v1_5", a) : e;
|
|
30
30
|
}
|
|
31
|
-
case
|
|
32
|
-
case
|
|
31
|
+
case i.rsassaPss: return requireRsaPssVerifyConfig(t, n, a);
|
|
32
|
+
case i.ecdsaWithSHA256: {
|
|
33
33
|
let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-256");
|
|
34
|
-
return e === void 0 ? requireEcPublicKey(n, r, "SHA-256",
|
|
34
|
+
return e === void 0 ? requireEcPublicKey(n, r, "SHA-256", a) : e;
|
|
35
35
|
}
|
|
36
|
-
case
|
|
36
|
+
case i.ecdsaWithSHA384: {
|
|
37
37
|
let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-384");
|
|
38
|
-
return e === void 0 ? requireEcPublicKey(n, r, "SHA-384",
|
|
38
|
+
return e === void 0 ? requireEcPublicKey(n, r, "SHA-384", a) : e;
|
|
39
39
|
}
|
|
40
|
-
case
|
|
40
|
+
case i.ecdsaWithSHA512: {
|
|
41
41
|
let e = requireAbsentSignatureAlgorithmParameters(t, "ECDSA with SHA-512");
|
|
42
|
-
return e === void 0 ? requireEcPublicKey(n, r, "SHA-512",
|
|
42
|
+
return e === void 0 ? requireEcPublicKey(n, r, "SHA-512", a) : e;
|
|
43
43
|
}
|
|
44
|
-
case
|
|
44
|
+
case i.ed25519: {
|
|
45
45
|
let e = requireAbsentSignatureAlgorithmParameters(t, "Ed25519");
|
|
46
|
-
return e === void 0 ? n ===
|
|
46
|
+
return e === void 0 ? n === i.ed25519 ? ok({
|
|
47
47
|
importAlgorithm: { kind: "ed25519" },
|
|
48
48
|
verifyParams: { name: "Ed25519" }
|
|
49
|
-
}) : unsupported("Ed25519", `requires Ed25519 ${
|
|
49
|
+
}) : unsupported("Ed25519", `requires Ed25519 ${a} public key`) : e;
|
|
50
50
|
}
|
|
51
51
|
default: return unsupported(e, "unrecognized signature algorithm OID");
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
/** Validate that the public key OID is `rsaEncryption` and return an RSA verification config result. */
|
|
55
|
-
function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer",
|
|
56
|
-
return e ===
|
|
55
|
+
function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer", a) {
|
|
56
|
+
return e === i.rsaEncryption ? ok({
|
|
57
57
|
importAlgorithm: {
|
|
58
58
|
kind: "rsa",
|
|
59
59
|
hash: t,
|
|
@@ -61,15 +61,15 @@ function requireRsaPublicKey(e, t, n = "pkcs1-v1_5", r = "issuer", i) {
|
|
|
61
61
|
},
|
|
62
62
|
verifyParams: n === "pss" ? {
|
|
63
63
|
name: "RSA-PSS",
|
|
64
|
-
saltLength:
|
|
64
|
+
saltLength: a ?? 0
|
|
65
65
|
} : { name: "RSASSA-PKCS1-v1_5" }
|
|
66
66
|
}) : unsupported("RSA", `requires RSA ${r} public key`);
|
|
67
67
|
}
|
|
68
68
|
/** Validate that the public key OID is `ecPublicKey`, resolve the curve, and return an ECDSA verification config result. */
|
|
69
69
|
function requireEcPublicKey(e, t, n, r = "issuer") {
|
|
70
|
-
if (e !==
|
|
70
|
+
if (e !== i.ecPublicKey) return unsupported("ECDSA", `requires EC ${r} public key`);
|
|
71
71
|
switch (t) {
|
|
72
|
-
case
|
|
72
|
+
case i.prime256v1: return ok({
|
|
73
73
|
importAlgorithm: {
|
|
74
74
|
kind: "ecdsa",
|
|
75
75
|
curve: "P-256"
|
|
@@ -80,7 +80,7 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
|
|
|
80
80
|
},
|
|
81
81
|
ecdsaRawSignatureBytes: 64
|
|
82
82
|
});
|
|
83
|
-
case
|
|
83
|
+
case i.secp384r1: return ok({
|
|
84
84
|
importAlgorithm: {
|
|
85
85
|
kind: "ecdsa",
|
|
86
86
|
curve: "P-384"
|
|
@@ -91,7 +91,7 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
|
|
|
91
91
|
},
|
|
92
92
|
ecdsaRawSignatureBytes: 96
|
|
93
93
|
});
|
|
94
|
-
case
|
|
94
|
+
case i.secp521r1: return ok({
|
|
95
95
|
importAlgorithm: {
|
|
96
96
|
kind: "ecdsa",
|
|
97
97
|
curve: "P-521"
|
|
@@ -115,73 +115,73 @@ function requireEcPublicKey(e, t, n, r = "issuer") {
|
|
|
115
115
|
* @throws `limit_exceeded` on algorithm parameters or a public key that exceed
|
|
116
116
|
* a micro509 decoding limit.
|
|
117
117
|
*/
|
|
118
|
-
async function verifySignedDataDetailed(
|
|
119
|
-
let u = getVerifySignatureConfigResult(
|
|
118
|
+
async function verifySignedDataDetailed(e, r, i, a, o, s, c) {
|
|
119
|
+
let u = getVerifySignatureConfigResult(e, r, i, a);
|
|
120
120
|
if (!u.ok) return u;
|
|
121
121
|
try {
|
|
122
122
|
return await verifyWithConfiguredKey(await l(o, u.value.importAlgorithm), u.value, s, c, "auto");
|
|
123
|
-
} catch (
|
|
124
|
-
return
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return n(e, t), verificationError(e instanceof Error ? e.message : "signature verification failed");
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
/** Execute verification for imported SPKI and caller-owned keys with the same configuration. */
|
|
128
|
-
async function verifyWithConfiguredKey(
|
|
128
|
+
async function verifyWithConfiguredKey(r, i, s, l, u) {
|
|
129
129
|
try {
|
|
130
|
-
if (u === "der" &&
|
|
131
|
-
s =
|
|
132
|
-
} catch (
|
|
133
|
-
return
|
|
130
|
+
if (u === "der" && i.ecdsaRawSignatureBytes !== void 0) try {
|
|
131
|
+
s = o(s, i.ecdsaRawSignatureBytes / 2);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
return n(e, t), {
|
|
134
134
|
ok: !0,
|
|
135
135
|
valid: !1
|
|
136
136
|
};
|
|
137
137
|
}
|
|
138
|
-
let d = c().subtle, f =
|
|
139
|
-
if (await d.verify(
|
|
138
|
+
let d = c().subtle, f = e(s), p = e(l);
|
|
139
|
+
if (await d.verify(i.verifyParams, r, f, p)) return {
|
|
140
140
|
ok: !0,
|
|
141
141
|
valid: !0
|
|
142
142
|
};
|
|
143
|
-
if (u === "auto" &&
|
|
144
|
-
let
|
|
145
|
-
if (
|
|
143
|
+
if (u === "auto" && i.ecdsaRawSignatureBytes !== void 0) {
|
|
144
|
+
let t = a(s, i.ecdsaRawSignatureBytes / 2);
|
|
145
|
+
if (t !== void 0) return {
|
|
146
146
|
ok: !0,
|
|
147
|
-
valid: await d.verify(
|
|
147
|
+
valid: await d.verify(i.verifyParams, r, e(t), p)
|
|
148
148
|
};
|
|
149
149
|
}
|
|
150
150
|
return {
|
|
151
151
|
ok: !0,
|
|
152
152
|
valid: !1
|
|
153
153
|
};
|
|
154
|
-
} catch (
|
|
155
|
-
return
|
|
154
|
+
} catch (e) {
|
|
155
|
+
return n(e, t), verificationError(e instanceof Error ? e.message : "signature verification failed");
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
/** Resolve a caller-owned key without exporting it, then run the shared verifier. */
|
|
159
|
-
async function verifySignedDataWithKeyDetailed(e, t, n, r,
|
|
159
|
+
async function verifySignedDataWithKeyDetailed(e, t, n, r, a, o) {
|
|
160
160
|
if (e.type !== "public" || !e.usages.includes("verify")) return verificationError("Signature verification requires a public key with verify usage");
|
|
161
161
|
let s = e.algorithm, c, l;
|
|
162
162
|
switch (s.name) {
|
|
163
163
|
case "RSASSA-PKCS1-v1_5":
|
|
164
164
|
case "RSA-PSS":
|
|
165
|
-
c =
|
|
165
|
+
c = i.rsaEncryption;
|
|
166
166
|
break;
|
|
167
167
|
case "ECDSA":
|
|
168
|
-
if (c =
|
|
168
|
+
if (c = i.ecPublicKey, "namedCurve" in s) switch (s.namedCurve) {
|
|
169
169
|
case "P-256":
|
|
170
|
-
l =
|
|
170
|
+
l = i.prime256v1;
|
|
171
171
|
break;
|
|
172
172
|
case "P-384":
|
|
173
|
-
l =
|
|
173
|
+
l = i.secp384r1;
|
|
174
174
|
break;
|
|
175
|
-
case "P-521": l =
|
|
175
|
+
case "P-521": l = i.secp521r1;
|
|
176
176
|
}
|
|
177
177
|
break;
|
|
178
178
|
case "Ed25519":
|
|
179
|
-
c =
|
|
179
|
+
c = i.ed25519;
|
|
180
180
|
break;
|
|
181
181
|
default: return unsupported(s.name, "unsupported public key algorithm");
|
|
182
182
|
}
|
|
183
183
|
let u = getVerifySignatureConfigResult(t, n, c, l);
|
|
184
|
-
return u.ok ?
|
|
184
|
+
return u.ok ? o !== "raw" && o !== "der" ? unsupported(t, "signature encoding must be raw or der") : o === "der" && u.value.ecdsaRawSignatureBytes === void 0 ? unsupported(t, "DER signature encoding requires ECDSA") : s.name === u.value.verifyParams.name ? u.value.importAlgorithm.kind === "rsa" && (!("hash" in s) || typeof s.hash != "object" || s.hash === null || !("name" in s.hash) || s.hash.name !== u.value.importAlgorithm.hash) ? unsupported(t, "signature hash does not match the RSA public key") : await verifyWithConfiguredKey(e, u.value, r, a, o) : unsupported(t, "signature scheme does not match the public key") : u;
|
|
185
185
|
}
|
|
186
186
|
/** Parse RSA-PSS algorithm parameters and build a verification config, or return failure. */
|
|
187
187
|
function requireRsaPssVerifyConfig(e, t, n) {
|
|
@@ -189,14 +189,14 @@ function requireRsaPssVerifyConfig(e, t, n) {
|
|
|
189
189
|
return r.ok ? requireRsaPublicKey(t, r.value.hash, "pss", n, r.value.saltLength) : unsupported("RSA-PSS", r.reason);
|
|
190
190
|
}
|
|
191
191
|
/** RFC 4055 §5 requires generators to encode NULL and implementations to accept absent parameters too. */
|
|
192
|
-
function requireDerNullOrAbsentSignatureAlgorithmParameters(
|
|
193
|
-
if (
|
|
192
|
+
function requireDerNullOrAbsentSignatureAlgorithmParameters(e, i) {
|
|
193
|
+
if (e === void 0) return;
|
|
194
194
|
let a = "signature AlgorithmIdentifier parameters must be DER NULL or absent";
|
|
195
195
|
try {
|
|
196
|
-
let
|
|
197
|
-
return
|
|
198
|
-
} catch (
|
|
199
|
-
return
|
|
196
|
+
let t = r(e);
|
|
197
|
+
return t.tag !== 5 || t.length !== 0 || t.end !== e.length ? unsupported(i, a) : void 0;
|
|
198
|
+
} catch (e) {
|
|
199
|
+
return n(e, t), unsupported(i, a);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
function requireAbsentSignatureAlgorithmParameters(e, t) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { nullValue as e, objectIdentifier as t, sequence as n } from "../asn1/der.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { OIDS as r } from "../asn1/oids.js";
|
|
3
|
+
import { ecdsaSignatureToDer as i } from "./ecdsa.js";
|
|
4
4
|
import { encodeRsaPssParameters as a, rsaPssParametersForHash as o } from "./rsa-pss.js";
|
|
5
5
|
import { getCrypto as s } from "./webcrypto.js";
|
|
6
6
|
// src/internal/crypto/signing.ts
|
|
@@ -12,35 +12,35 @@ import { getCrypto as s } from "./webcrypto.js";
|
|
|
12
12
|
*/
|
|
13
13
|
/** Resolve a private key and optional profile into a {@linkcode SignatureAlgorithmIdentifier}. */
|
|
14
14
|
function getSignatureAlgorithm(t, n = {}) {
|
|
15
|
-
let
|
|
15
|
+
let i = t.algorithm;
|
|
16
16
|
if (n.kind === "rsa-pss") return getRsaPssSignatureAlgorithm(t, n.saltLength);
|
|
17
|
-
if (
|
|
18
|
-
if (!hasHash(
|
|
19
|
-
switch (
|
|
17
|
+
if (i.name === "RSASSA-PKCS1-v1_5") {
|
|
18
|
+
if (!hasHash(i)) throw Error("RSA key is missing hash metadata");
|
|
19
|
+
switch (i.hash.name) {
|
|
20
20
|
case "SHA-256": return {
|
|
21
|
-
algorithmOid:
|
|
21
|
+
algorithmOid: r.sha256WithRSAEncryption,
|
|
22
22
|
parameters: e(),
|
|
23
23
|
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
24
24
|
};
|
|
25
25
|
case "SHA-384": return {
|
|
26
|
-
algorithmOid:
|
|
26
|
+
algorithmOid: r.sha384WithRSAEncryption,
|
|
27
27
|
parameters: e(),
|
|
28
28
|
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
29
29
|
};
|
|
30
30
|
case "SHA-512": return {
|
|
31
|
-
algorithmOid:
|
|
31
|
+
algorithmOid: r.sha512WithRSAEncryption,
|
|
32
32
|
parameters: e(),
|
|
33
33
|
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
34
34
|
};
|
|
35
|
-
default: throw Error(`Unsupported RSA hash: ${
|
|
35
|
+
default: throw Error(`Unsupported RSA hash: ${i.hash.name}`);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
if (
|
|
39
|
-
if (
|
|
40
|
-
if (!hasNamedCurve(
|
|
41
|
-
switch (
|
|
38
|
+
if (i.name === "RSA-PSS") throw Error("RSA-PSS signing requires an explicit signature profile");
|
|
39
|
+
if (i.name === "ECDSA") {
|
|
40
|
+
if (!hasNamedCurve(i)) throw Error("ECDSA key is missing namedCurve metadata");
|
|
41
|
+
switch (i.namedCurve) {
|
|
42
42
|
case "P-256": return {
|
|
43
|
-
algorithmOid:
|
|
43
|
+
algorithmOid: r.ecdsaWithSHA256,
|
|
44
44
|
signParams: {
|
|
45
45
|
name: "ECDSA",
|
|
46
46
|
hash: "SHA-256"
|
|
@@ -48,7 +48,7 @@ function getSignatureAlgorithm(t, n = {}) {
|
|
|
48
48
|
ecdsaRawSignatureBytes: 64
|
|
49
49
|
};
|
|
50
50
|
case "P-384": return {
|
|
51
|
-
algorithmOid:
|
|
51
|
+
algorithmOid: r.ecdsaWithSHA384,
|
|
52
52
|
signParams: {
|
|
53
53
|
name: "ECDSA",
|
|
54
54
|
hash: "SHA-384"
|
|
@@ -56,31 +56,31 @@ function getSignatureAlgorithm(t, n = {}) {
|
|
|
56
56
|
ecdsaRawSignatureBytes: 96
|
|
57
57
|
};
|
|
58
58
|
case "P-521": return {
|
|
59
|
-
algorithmOid:
|
|
59
|
+
algorithmOid: r.ecdsaWithSHA512,
|
|
60
60
|
signParams: {
|
|
61
61
|
name: "ECDSA",
|
|
62
62
|
hash: "SHA-512"
|
|
63
63
|
},
|
|
64
64
|
ecdsaRawSignatureBytes: 132
|
|
65
65
|
};
|
|
66
|
-
default: throw Error(`Unsupported curve: ${
|
|
66
|
+
default: throw Error(`Unsupported curve: ${i.namedCurve}`);
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
-
if (
|
|
70
|
-
algorithmOid:
|
|
69
|
+
if (i.name === "Ed25519") return {
|
|
70
|
+
algorithmOid: r.ed25519,
|
|
71
71
|
signParams: { name: "Ed25519" }
|
|
72
72
|
};
|
|
73
|
-
throw Error(`Unsupported signing key algorithm: ${
|
|
73
|
+
throw Error(`Unsupported signing key algorithm: ${i.name}`);
|
|
74
74
|
}
|
|
75
75
|
/** Build an RSA-PSS {@linkcode SignatureAlgorithmIdentifier} from an RSA-PSS private key. */
|
|
76
76
|
function getRsaPssSignatureAlgorithm(e, t) {
|
|
77
77
|
let n = e.algorithm;
|
|
78
78
|
if (n.name !== "RSA-PSS") throw Error("RSA-PSS signature profile requires an RSA-PSS private key");
|
|
79
79
|
if (!hasHash(n)) throw Error("RSA-PSS key is missing hash metadata");
|
|
80
|
-
let
|
|
81
|
-
if (t !== void 0 && t !== s.saltLength) throw Error(`Unsupported RSA-PSS saltLength ${t} for ${
|
|
80
|
+
let i = rsaPssHashFromWebCryptoName(n.hash.name), s = o(i);
|
|
81
|
+
if (t !== void 0 && t !== s.saltLength) throw Error(`Unsupported RSA-PSS saltLength ${t} for ${i}; expected ${s.saltLength}`);
|
|
82
82
|
return {
|
|
83
|
-
algorithmOid:
|
|
83
|
+
algorithmOid: r.rsassaPss,
|
|
84
84
|
parameters: a(s),
|
|
85
85
|
signParams: {
|
|
86
86
|
name: "RSA-PSS",
|
|
@@ -95,8 +95,8 @@ function encodeAlgorithmIdentifier(e) {
|
|
|
95
95
|
}
|
|
96
96
|
/** Sign `data` and return a DER-encoded signature. ECDSA raw signatures are auto-converted to DER. */
|
|
97
97
|
async function signBytes(e, t, n) {
|
|
98
|
-
let
|
|
99
|
-
return t.ecdsaRawSignatureBytes === void 0 ? a :
|
|
98
|
+
let r = new Uint8Array(n), a = new Uint8Array(await s().subtle.sign(t.signParams, e, r));
|
|
99
|
+
return t.ecdsaRawSignatureBytes === void 0 ? a : i(a, t.ecdsaRawSignatureBytes);
|
|
100
100
|
}
|
|
101
101
|
/** Type guard: does this key algorithm carry a `hash` property (RSA keys). */
|
|
102
102
|
function hasHash(e) {
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// src/internal/jose/json-data.ts
|
|
2
|
+
const e = Symbol("JSON data refusal");
|
|
3
|
+
function refuse(t) {
|
|
4
|
+
throw {
|
|
5
|
+
marker: e,
|
|
6
|
+
code: t
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function isRefusal(t) {
|
|
10
|
+
return typeof t == "object" && !!t && "marker" in t && t.marker === e;
|
|
11
|
+
}
|
|
12
|
+
function bytes(e, t) {
|
|
13
|
+
e.bytes += t, e.bytes > e.maxBytes && refuse("limit_exceeded");
|
|
14
|
+
}
|
|
15
|
+
function node(e) {
|
|
16
|
+
++e.nodes > e.maxNodes && refuse("limit_exceeded");
|
|
17
|
+
}
|
|
18
|
+
/** Counts the UTF-8 bytes of the shortest JSON string spelling without allocating it. */
|
|
19
|
+
function string(e, t) {
|
|
20
|
+
t.length > e.maxBytes - e.bytes && refuse("limit_exceeded"), bytes(e, 2);
|
|
21
|
+
for (let n = 0; n < t.length; n++) {
|
|
22
|
+
let r = t.charCodeAt(n);
|
|
23
|
+
if (r >= 55296 && r <= 56319) {
|
|
24
|
+
let r = t.charCodeAt(++n);
|
|
25
|
+
r >= 56320 && r <= 57343 || refuse("invalid_json"), bytes(e, 4);
|
|
26
|
+
} else bytes(e, characterBytes(r));
|
|
27
|
+
}
|
|
28
|
+
return t;
|
|
29
|
+
}
|
|
30
|
+
function characterBytes(e) {
|
|
31
|
+
return e >= 56320 && e <= 57343 && refuse("invalid_json"), e === 34 || e === 92 ? 2 : e < 32 ? [
|
|
32
|
+
8,
|
|
33
|
+
9,
|
|
34
|
+
10,
|
|
35
|
+
12,
|
|
36
|
+
13
|
|
37
|
+
].includes(e) ? 2 : 6 : e < 128 ? 1 : e < 2048 ? 2 : 3;
|
|
38
|
+
}
|
|
39
|
+
/** Reflection can execute proxy traps or encounter a revoked proxy; that is an input refusal. */
|
|
40
|
+
function inspect(e) {
|
|
41
|
+
try {
|
|
42
|
+
return e();
|
|
43
|
+
} catch {
|
|
44
|
+
return refuse("invalid_json");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function arrayLength(e, t) {
|
|
48
|
+
let n = inspect(() => Object.getOwnPropertyDescriptor(e, "length"))?.value;
|
|
49
|
+
return (typeof n != "number" || !Number.isSafeInteger(n) || n < 0) && refuse("invalid_json"), n > t.maxNodes - t.nodes && refuse("limit_exceeded"), n;
|
|
50
|
+
}
|
|
51
|
+
function dataMember(e, t) {
|
|
52
|
+
let n = inspect(() => Object.getOwnPropertyDescriptor(e, t));
|
|
53
|
+
return n !== void 0 && "value" in n && n.enumerable || refuse("invalid_json"), n.value;
|
|
54
|
+
}
|
|
55
|
+
function checkPrototype(e, t) {
|
|
56
|
+
let n = inspect(() => Object.getPrototypeOf(e));
|
|
57
|
+
(t ? n !== Array.prototype : n !== null && n !== Object.prototype) && refuse("invalid_json");
|
|
58
|
+
}
|
|
59
|
+
function copy(e, t, n) {
|
|
60
|
+
if (node(t), e === null || typeof e == "boolean") return bytes(t, e === null || e ? 4 : 5), e;
|
|
61
|
+
if (typeof e == "string") return string(t, e);
|
|
62
|
+
if (typeof e == "number") return Number.isFinite(e) || refuse("invalid_json"), bytes(t, String(e).length), e;
|
|
63
|
+
typeof e != "object" && refuse("invalid_json"), n >= t.maxDepth && refuse("limit_exceeded"), t.active.has(e) && refuse("invalid_json");
|
|
64
|
+
let r = inspect(() => Array.isArray(e));
|
|
65
|
+
checkPrototype(e, r), t.active.add(e), r && arrayLength(e, t);
|
|
66
|
+
let i = inspect(() => Reflect.ownKeys(e));
|
|
67
|
+
i.length > t.maxNodes - t.nodes + +!!r && refuse("limit_exceeded"), bytes(t, 2);
|
|
68
|
+
let a = r ? copyArray(e, i, t, n) : copyObject(e, i, t, n);
|
|
69
|
+
return t.active.delete(e), a;
|
|
70
|
+
}
|
|
71
|
+
function copyArray(e, t, n, r) {
|
|
72
|
+
let i = arrayLength(e, n);
|
|
73
|
+
t.length !== i + 1 && refuse("invalid_json");
|
|
74
|
+
let a = [];
|
|
75
|
+
for (let t = 0; t < i; t++) t > 0 && bytes(n, 1), a.push(copy(dataMember(e, String(t)), n, r + 1));
|
|
76
|
+
return Object.freeze(a);
|
|
77
|
+
}
|
|
78
|
+
function copyObject(e, t, n, r) {
|
|
79
|
+
let i = {};
|
|
80
|
+
for (let [a, o] of t.entries()) typeof o != "string" && refuse("invalid_json"), node(n), string(n, o), bytes(n, a > 0 ? 2 : 1), Object.defineProperty(i, o, {
|
|
81
|
+
value: copy(dataMember(e, o), n, r + 1),
|
|
82
|
+
enumerable: !0
|
|
83
|
+
});
|
|
84
|
+
return Object.freeze(i);
|
|
85
|
+
}
|
|
86
|
+
/** Copies plain JSON data without invoking getters, coercion or `toJSON`. */
|
|
87
|
+
function snapshotJsonData(e, t) {
|
|
88
|
+
try {
|
|
89
|
+
return {
|
|
90
|
+
ok: !0,
|
|
91
|
+
value: copy(e, {
|
|
92
|
+
...t,
|
|
93
|
+
bytes: 0,
|
|
94
|
+
nodes: 0,
|
|
95
|
+
active: /* @__PURE__ */ new Set()
|
|
96
|
+
}, 0)
|
|
97
|
+
};
|
|
98
|
+
} catch (e) {
|
|
99
|
+
if (isRefusal(e)) return {
|
|
100
|
+
ok: !1,
|
|
101
|
+
code: e.code
|
|
102
|
+
};
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export { snapshotJsonData };
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=json-data.js.map
|