micro509 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +15 -1
- package/dist/internal/asn1/asn1.js +280 -2
- package/dist/internal/asn1/der.js +378 -2
- package/dist/internal/asn1/oids.js +103 -2
- package/dist/internal/crypto/algorithm-names.js +58 -2
- package/dist/internal/crypto/ecdsa.js +68 -2
- package/dist/internal/crypto/hash.js +286 -2
- package/dist/internal/crypto/pbes2.js +213 -2
- package/dist/internal/crypto/rsa-pss.js +184 -2
- package/dist/internal/crypto/sig-verify.js +192 -2
- package/dist/internal/crypto/signing.d.ts +1 -2
- package/dist/internal/crypto/signing.js +123 -2
- package/dist/internal/crypto/webcrypto.js +14 -2
- package/dist/internal/shared/base64.js +16 -2
- package/dist/internal/shared/dn.js +84 -2
- package/dist/internal/shared/ip.js +164 -2
- package/dist/internal/verify/name-constraints-engine.js +450 -2
- package/dist/internal/verify/policy-engine.js +382 -2
- package/dist/internal/verify/verify-path.js +298 -2
- package/dist/internal/x509/extension-bits.js +132 -2
- package/dist/internal/x509/extension-registry.js +229 -2
- package/dist/internal/x509/name-fields.js +93 -2
- package/dist/keys/keys.d.ts +231 -7
- package/dist/keys/keys.js +1087 -5
- package/dist/keys.d.ts +2 -2
- package/dist/keys.js +2 -1
- package/dist/pem/pem.d.ts +2 -2
- package/dist/pem/pem.js +172 -6
- package/dist/pem.js +2 -1
- package/dist/pkcs/pfx.d.ts +4 -5
- package/dist/pkcs/pfx.js +395 -2
- package/dist/pkcs/pkcs12-mac.d.ts +2 -2
- package/dist/pkcs/pkcs12-mac.js +175 -2
- package/dist/pkcs/pkcs7.d.ts +1 -2
- package/dist/pkcs/pkcs7.js +653 -2
- package/dist/pkcs.d.ts +2 -2
- package/dist/pkcs.js +4 -1
- package/dist/result/result.d.ts +1 -2
- package/dist/result/result.js +111 -2
- package/dist/result.js +2 -1
- package/dist/revocation/chain.d.ts +4 -5
- package/dist/revocation/chain.js +536 -2
- package/dist/revocation/crl.d.ts +2 -2
- package/dist/revocation/crl.js +1119 -2
- package/dist/revocation/ocsp.d.ts +3 -2
- package/dist/revocation/ocsp.js +958 -2
- package/dist/revocation/revocation.d.ts +1 -2
- package/dist/revocation/revocation.js +261 -2
- package/dist/revocation.d.ts +2 -2
- package/dist/revocation.js +5 -1
- package/dist/verify/identity.d.ts +1 -2
- package/dist/verify/identity.js +270 -2
- package/dist/verify/name-constraints.d.ts +1 -2
- package/dist/verify/policy.d.ts +1 -2
- package/dist/verify/verify.d.ts +1 -2
- package/dist/verify/verify.js +764 -2
- package/dist/verify.js +3 -1
- package/dist/x509/certificate.d.ts +1 -2
- package/dist/x509/certificate.js +151 -2
- package/dist/x509/csr.d.ts +1 -2
- package/dist/x509/csr.js +71 -2
- package/dist/x509/extensions.d.ts +1 -2
- package/dist/x509/extensions.js +387 -2
- package/dist/x509/name.d.ts +1 -2
- package/dist/x509/name.js +173 -2
- package/dist/x509/parse.d.ts +84 -2
- package/dist/x509/parse.js +1090 -2
- package/dist/x509.d.ts +2 -2
- package/dist/x509.js +6 -1
- package/package.json +74 -34
- package/dist/internal/asn1/asn1.js.map +0 -1
- package/dist/internal/asn1/der.js.map +0 -1
- package/dist/internal/asn1/oids.js.map +0 -1
- package/dist/internal/crypto/algorithm-names.js.map +0 -1
- package/dist/internal/crypto/ecdsa.js.map +0 -1
- package/dist/internal/crypto/hash.js.map +0 -1
- package/dist/internal/crypto/pbes2.d.ts +0 -23
- package/dist/internal/crypto/pbes2.js.map +0 -1
- package/dist/internal/crypto/rsa-pss.js.map +0 -1
- package/dist/internal/crypto/sig-verify.js.map +0 -1
- package/dist/internal/crypto/signing.js.map +0 -1
- package/dist/internal/crypto/webcrypto.js.map +0 -1
- package/dist/internal/shared/base64.js.map +0 -1
- package/dist/internal/shared/dn.js.map +0 -1
- package/dist/internal/shared/ip.js.map +0 -1
- package/dist/internal/verify/name-constraints-engine.js.map +0 -1
- package/dist/internal/verify/policy-engine.js.map +0 -1
- package/dist/internal/verify/verify-path.js.map +0 -1
- package/dist/internal/x509/extension-bits.js.map +0 -1
- package/dist/internal/x509/extension-registry.js.map +0 -1
- package/dist/internal/x509/name-fields.js.map +0 -1
- package/dist/keys/keys.js.map +0 -1
- package/dist/pem/pem.js.map +0 -1
- package/dist/pkcs/pfx.js.map +0 -1
- package/dist/pkcs/pkcs12-mac.js.map +0 -1
- package/dist/pkcs/pkcs7.js.map +0 -1
- package/dist/result/result.js.map +0 -1
- package/dist/revocation/chain.js.map +0 -1
- package/dist/revocation/crl.js.map +0 -1
- package/dist/revocation/ocsp.js.map +0 -1
- package/dist/revocation/revocation.js.map +0 -1
- package/dist/verify/identity.js.map +0 -1
- package/dist/verify/verify.js.map +0 -1
- package/dist/x509/certificate.js.map +0 -1
- package/dist/x509/csr.js.map +0 -1
- package/dist/x509/extensions.js.map +0 -1
- package/dist/x509/name.js.map +0 -1
- package/dist/x509/parse.js.map +0 -1
|
@@ -1,2 +1,184 @@
|
|
|
1
|
-
import{explicitContext
|
|
2
|
-
|
|
1
|
+
import { explicitContext, integerFromNumber, nullValue, objectIdentifier, readRootElement, sequence } from "../asn1/der.js";
|
|
2
|
+
import { childrenOf, decodeNonNegativeIntegerNumber, decodeObjectIdentifier, requireElement } from "../asn1/asn1.js";
|
|
3
|
+
import { OIDS } from "../asn1/oids.js";
|
|
4
|
+
//#region src/internal/crypto/rsa-pss.ts
|
|
5
|
+
/**
|
|
6
|
+
* RSA-PSS `AlgorithmIdentifier` parameter parsing, validation, and encoding.
|
|
7
|
+
*
|
|
8
|
+
* Only the three hash-matched profiles (SHA-256/384/512 with MGF1 and matching salt
|
|
9
|
+
* length) are accepted; SHA-1 defaults and mismatched MGF hashes are rejected.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
/** RFC 3447 default salt length when hash is SHA-1 (used to detect the unsupported default). */
|
|
14
|
+
const SHA1_SALT_LENGTH = 20;
|
|
15
|
+
/** Return the canonical {@linkcode RsaPssParameters} profile for a given hash algorithm. */
|
|
16
|
+
function rsaPssParametersForHash(hash) {
|
|
17
|
+
switch (hash) {
|
|
18
|
+
case "SHA-256": return {
|
|
19
|
+
hash: "SHA-256",
|
|
20
|
+
mgfHash: "SHA-256",
|
|
21
|
+
saltLength: 32,
|
|
22
|
+
trailerField: 1
|
|
23
|
+
};
|
|
24
|
+
case "SHA-384": return {
|
|
25
|
+
hash: "SHA-384",
|
|
26
|
+
mgfHash: "SHA-384",
|
|
27
|
+
saltLength: 48,
|
|
28
|
+
trailerField: 1
|
|
29
|
+
};
|
|
30
|
+
case "SHA-512": return {
|
|
31
|
+
hash: "SHA-512",
|
|
32
|
+
mgfHash: "SHA-512",
|
|
33
|
+
saltLength: 64,
|
|
34
|
+
trailerField: 1
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** DER-encode an {@linkcode RsaPssParameters} profile as an ASN.1 RSASSA-PSS-params SEQUENCE. */
|
|
39
|
+
function encodeRsaPssParameters(parameters) {
|
|
40
|
+
const hashOid = hashOidForName(parameters.hash);
|
|
41
|
+
return sequence([
|
|
42
|
+
explicitContext(0, encodeHashAlgorithmIdentifier(hashOid)),
|
|
43
|
+
explicitContext(1, sequence([objectIdentifier(OIDS.mgf1), encodeHashAlgorithmIdentifier(hashOid)])),
|
|
44
|
+
explicitContext(2, integerFromNumber(parameters.saltLength)),
|
|
45
|
+
...parameters.trailerField === 1 ? [] : [explicitContext(3, integerFromNumber(parameters.trailerField))]
|
|
46
|
+
]);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Parse and validate DER-encoded RSASSA-PSS-params.
|
|
50
|
+
*
|
|
51
|
+
* Returns a typed result: success with a supported profile, unsupported with
|
|
52
|
+
* a reason code, or malformed with an error message.
|
|
53
|
+
*/
|
|
54
|
+
function parseRsaPssParameters(parametersDer) {
|
|
55
|
+
if (parametersDer === void 0) return unsupportedResult("default_hash_sha1");
|
|
56
|
+
try {
|
|
57
|
+
const element = readRootElement(parametersDer, { maxDepth: 64 });
|
|
58
|
+
if (element.tag !== 48) throw new Error("RSA-PSS parameters must be a SEQUENCE");
|
|
59
|
+
const children = childrenOf(parametersDer, element);
|
|
60
|
+
let hashOid = OIDS.sha1;
|
|
61
|
+
let maskGenAlgorithm = {
|
|
62
|
+
oid: OIDS.mgf1,
|
|
63
|
+
hashOid: OIDS.sha1
|
|
64
|
+
};
|
|
65
|
+
let saltLength = SHA1_SALT_LENGTH;
|
|
66
|
+
let trailerField = 1;
|
|
67
|
+
let sawHash = false;
|
|
68
|
+
let sawMaskGen = false;
|
|
69
|
+
let sawSaltLength = false;
|
|
70
|
+
let sawTrailerField = false;
|
|
71
|
+
for (const child of children) switch (child.tag) {
|
|
72
|
+
case 160:
|
|
73
|
+
if (sawHash) throw new Error("RSA-PSS parameters contain duplicate hashAlgorithm");
|
|
74
|
+
hashOid = parseHashAlgorithmIdentifier(parametersDer, requireSingleExplicitChild(parametersDer, child, "hashAlgorithm"), "hashAlgorithm");
|
|
75
|
+
sawHash = true;
|
|
76
|
+
break;
|
|
77
|
+
case 161:
|
|
78
|
+
if (sawMaskGen) throw new Error("RSA-PSS parameters contain duplicate maskGenAlgorithm");
|
|
79
|
+
maskGenAlgorithm = parseMaskGenAlgorithmIdentifier(parametersDer, requireSingleExplicitChild(parametersDer, child, "maskGenAlgorithm"));
|
|
80
|
+
sawMaskGen = true;
|
|
81
|
+
break;
|
|
82
|
+
case 162:
|
|
83
|
+
if (sawSaltLength) throw new Error("RSA-PSS parameters contain duplicate saltLength");
|
|
84
|
+
saltLength = parseExplicitInteger(parametersDer, child, "saltLength", "RSA-PSS saltLength");
|
|
85
|
+
sawSaltLength = true;
|
|
86
|
+
break;
|
|
87
|
+
case 163:
|
|
88
|
+
if (sawTrailerField) throw new Error("RSA-PSS parameters contain duplicate trailerField");
|
|
89
|
+
trailerField = parseExplicitInteger(parametersDer, child, "trailerField", "RSA-PSS trailerField");
|
|
90
|
+
sawTrailerField = true;
|
|
91
|
+
break;
|
|
92
|
+
default: throw new Error(`RSA-PSS parameters contain unexpected field tag 0x${child.tag.toString(16)}`);
|
|
93
|
+
}
|
|
94
|
+
const supportedHash = hashNameFromOid(hashOid);
|
|
95
|
+
if (supportedHash === void 0) {
|
|
96
|
+
if (hashOid === OIDS.sha1) return unsupportedResult("default_hash_sha1");
|
|
97
|
+
return unsupportedResult("unsupported_hash");
|
|
98
|
+
}
|
|
99
|
+
if (maskGenAlgorithm.oid !== OIDS.mgf1) return unsupportedResult("unsupported_mgf_algorithm");
|
|
100
|
+
const mgfHash = hashNameFromOid(maskGenAlgorithm.hashOid);
|
|
101
|
+
if (mgfHash === void 0) return unsupportedResult("unsupported_mgf_hash");
|
|
102
|
+
if (mgfHash !== supportedHash) return unsupportedResult("mgf_hash_mismatch");
|
|
103
|
+
const supported = rsaPssParametersForHash(supportedHash);
|
|
104
|
+
if (saltLength !== supported.saltLength) return unsupportedResult("unsupported_salt_length");
|
|
105
|
+
if (trailerField !== 1) return unsupportedResult("unsupported_trailer_field");
|
|
106
|
+
return {
|
|
107
|
+
ok: true,
|
|
108
|
+
value: supported
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
code: "malformed_rsa_pss_parameters",
|
|
114
|
+
reason: error instanceof Error ? error.message : "Malformed RSA-PSS parameters"
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Unwrap an explicit context tag and assert it contains exactly one child element. */
|
|
119
|
+
function requireSingleExplicitChild(source, element, label) {
|
|
120
|
+
const children = childrenOf(source, element);
|
|
121
|
+
if (children.length !== 1) throw new Error(`RSA-PSS ${label} must wrap exactly one value`);
|
|
122
|
+
return requireElement(children[0], `RSA-PSS ${label}`);
|
|
123
|
+
}
|
|
124
|
+
/** Extract a non-negative integer from an explicit context-tagged wrapper. */
|
|
125
|
+
function parseExplicitInteger(source, element, label, integerLabel) {
|
|
126
|
+
const integerElement = requireSingleExplicitChild(source, element, label);
|
|
127
|
+
if (integerElement.tag !== 2) throw new Error(`RSA-PSS ${label} must be an INTEGER`);
|
|
128
|
+
return decodeNonNegativeIntegerNumber(integerElement.value, integerLabel);
|
|
129
|
+
}
|
|
130
|
+
/** Parse an AlgorithmIdentifier SEQUENCE and return the algorithm OID. */
|
|
131
|
+
function parseHashAlgorithmIdentifier(source, element, label) {
|
|
132
|
+
if (element.tag !== 48) throw new Error(`RSA-PSS ${label} must be a SEQUENCE`);
|
|
133
|
+
const children = childrenOf(source, element);
|
|
134
|
+
if (children.length === 0 || children.length > 2) throw new Error(`Malformed RSA-PSS ${label} AlgorithmIdentifier`);
|
|
135
|
+
const oid = requireElement(children[0], `${label} OID`);
|
|
136
|
+
if (oid.tag !== 6) throw new Error(`Malformed RSA-PSS ${label} AlgorithmIdentifier`);
|
|
137
|
+
if (children.length === 2 && requireElement(children[1], `${label} parameters`).tag !== 5) throw new Error(`RSA-PSS ${label} AlgorithmIdentifier parameters must be NULL when present`);
|
|
138
|
+
return decodeObjectIdentifier(oid.value);
|
|
139
|
+
}
|
|
140
|
+
/** Parse an MGF AlgorithmIdentifier SEQUENCE into OID and optional inner hash OID. */
|
|
141
|
+
function parseMaskGenAlgorithmIdentifier(source, element) {
|
|
142
|
+
if (element.tag !== 48) throw new Error("RSA-PSS maskGenAlgorithm must be a SEQUENCE");
|
|
143
|
+
const children = childrenOf(source, element);
|
|
144
|
+
if (children.length === 0 || children.length > 2) throw new Error("Malformed RSA-PSS maskGenAlgorithm AlgorithmIdentifier");
|
|
145
|
+
const oidElement = requireElement(children[0], "maskGenAlgorithm OID");
|
|
146
|
+
if (oidElement.tag !== 6) throw new Error("Malformed RSA-PSS maskGenAlgorithm AlgorithmIdentifier");
|
|
147
|
+
const oid = decodeObjectIdentifier(oidElement.value);
|
|
148
|
+
if (oid !== OIDS.mgf1) return { oid };
|
|
149
|
+
return {
|
|
150
|
+
oid,
|
|
151
|
+
hashOid: parseHashAlgorithmIdentifier(source, requireElement(children[1], "maskGenAlgorithm parameters"), "MGF1 hashAlgorithm")
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** DER-encode a hash AlgorithmIdentifier as `SEQUENCE { OID, NULL }`. */
|
|
155
|
+
function encodeHashAlgorithmIdentifier(oid) {
|
|
156
|
+
return sequence([objectIdentifier(oid), nullValue()]);
|
|
157
|
+
}
|
|
158
|
+
/** Map a hash algorithm name to its ASN.1 OID string. */
|
|
159
|
+
function hashOidForName(hash) {
|
|
160
|
+
switch (hash) {
|
|
161
|
+
case "SHA-256": return OIDS.sha256;
|
|
162
|
+
case "SHA-384": return OIDS.sha384;
|
|
163
|
+
case "SHA-512": return OIDS.sha512;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** Reverse-map a hash OID to a supported {@linkcode RsaPssHash} name, or `undefined` if unsupported. */
|
|
167
|
+
function hashNameFromOid(oid) {
|
|
168
|
+
switch (oid) {
|
|
169
|
+
case OIDS.sha256: return "SHA-256";
|
|
170
|
+
case OIDS.sha384: return "SHA-384";
|
|
171
|
+
case OIDS.sha512: return "SHA-512";
|
|
172
|
+
default: return;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** Build an unsupported-parameters failure result. */
|
|
176
|
+
function unsupportedResult(reason) {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
code: "unsupported_rsa_pss_parameters",
|
|
180
|
+
reason
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
//#endregion
|
|
184
|
+
export { encodeRsaPssParameters, parseRsaPssParameters, rsaPssParametersForHash };
|
|
@@ -1,2 +1,192 @@
|
|
|
1
|
-
import{readElement
|
|
2
|
-
|
|
1
|
+
import { readElement } from "../asn1/der.js";
|
|
2
|
+
import { toArrayBuffer } from "../asn1/asn1.js";
|
|
3
|
+
import { OIDS } from "../asn1/oids.js";
|
|
4
|
+
import { getCrypto } from "./webcrypto.js";
|
|
5
|
+
import { importSpkiDerOrThrow } from "../../keys/keys.js";
|
|
6
|
+
import { parseRsaPssParameters } from "./rsa-pss.js";
|
|
7
|
+
import { alternateEcdsaSignatureEncoding } from "./ecdsa.js";
|
|
8
|
+
//#region src/internal/crypto/sig-verify.ts
|
|
9
|
+
/**
|
|
10
|
+
* Signature-verification helpers that resolve parsed algorithm OIDs into WebCrypto
|
|
11
|
+
* verification configuration for higher-level validators.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
/** Non-throwing variant of {@linkcode getVerifySignatureConfig} — returns a typed result union. */
|
|
16
|
+
function getVerifySignatureConfigResult(signatureAlgorithmOid, signatureAlgorithmParametersDer, publicKeyAlgorithmOid, publicKeyParametersOid, context = "issuer") {
|
|
17
|
+
switch (signatureAlgorithmOid) {
|
|
18
|
+
case OIDS.sha256WithRSAEncryption: {
|
|
19
|
+
const parameters = requireDerNullSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "SHA-256 with RSA");
|
|
20
|
+
if (parameters !== void 0) return parameters;
|
|
21
|
+
return requireRsaPublicKey(publicKeyAlgorithmOid, "SHA-256", "pkcs1-v1_5", context);
|
|
22
|
+
}
|
|
23
|
+
case OIDS.sha384WithRSAEncryption: {
|
|
24
|
+
const parameters = requireDerNullSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "SHA-384 with RSA");
|
|
25
|
+
if (parameters !== void 0) return parameters;
|
|
26
|
+
return requireRsaPublicKey(publicKeyAlgorithmOid, "SHA-384", "pkcs1-v1_5", context);
|
|
27
|
+
}
|
|
28
|
+
case OIDS.sha512WithRSAEncryption: {
|
|
29
|
+
const parameters = requireDerNullSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "SHA-512 with RSA");
|
|
30
|
+
if (parameters !== void 0) return parameters;
|
|
31
|
+
return requireRsaPublicKey(publicKeyAlgorithmOid, "SHA-512", "pkcs1-v1_5", context);
|
|
32
|
+
}
|
|
33
|
+
case OIDS.rsassaPss: return requireRsaPssVerifyConfig(signatureAlgorithmParametersDer, publicKeyAlgorithmOid, context);
|
|
34
|
+
case OIDS.ecdsaWithSHA256: {
|
|
35
|
+
const parameters = requireAbsentSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "ECDSA with SHA-256");
|
|
36
|
+
if (parameters !== void 0) return parameters;
|
|
37
|
+
return requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, "SHA-256", context);
|
|
38
|
+
}
|
|
39
|
+
case OIDS.ecdsaWithSHA384: {
|
|
40
|
+
const parameters = requireAbsentSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "ECDSA with SHA-384");
|
|
41
|
+
if (parameters !== void 0) return parameters;
|
|
42
|
+
return requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, "SHA-384", context);
|
|
43
|
+
}
|
|
44
|
+
case OIDS.ecdsaWithSHA512: {
|
|
45
|
+
const parameters = requireAbsentSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "ECDSA with SHA-512");
|
|
46
|
+
if (parameters !== void 0) return parameters;
|
|
47
|
+
return requireEcPublicKey(publicKeyAlgorithmOid, publicKeyParametersOid, "SHA-512", context);
|
|
48
|
+
}
|
|
49
|
+
case OIDS.ed25519: {
|
|
50
|
+
const parameters = requireAbsentSignatureAlgorithmParameters(signatureAlgorithmParametersDer, "Ed25519");
|
|
51
|
+
if (parameters !== void 0) return parameters;
|
|
52
|
+
if (publicKeyAlgorithmOid !== OIDS.ed25519) return unsupported("Ed25519", `requires Ed25519 ${context} public key`);
|
|
53
|
+
return ok({
|
|
54
|
+
importAlgorithm: { kind: "ed25519" },
|
|
55
|
+
verifyParams: { name: "Ed25519" }
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
default: return unsupported(signatureAlgorithmOid, "unrecognized signature algorithm OID");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Validate that the public key OID is `rsaEncryption` and return an RSA verification config result. */
|
|
62
|
+
function requireRsaPublicKey(algorithmOid, hash, scheme = "pkcs1-v1_5", context = "issuer", saltLength) {
|
|
63
|
+
if (algorithmOid !== OIDS.rsaEncryption) return unsupported("RSA", `requires RSA ${context} public key`);
|
|
64
|
+
return ok({
|
|
65
|
+
importAlgorithm: {
|
|
66
|
+
kind: "rsa",
|
|
67
|
+
hash,
|
|
68
|
+
scheme
|
|
69
|
+
},
|
|
70
|
+
verifyParams: scheme === "pss" ? {
|
|
71
|
+
name: "RSA-PSS",
|
|
72
|
+
saltLength: saltLength ?? 0
|
|
73
|
+
} : { name: "RSASSA-PKCS1-v1_5" }
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** Validate that the public key OID is `ecPublicKey`, resolve the curve, and return an ECDSA verification config result. */
|
|
77
|
+
function requireEcPublicKey(algorithmOid, parametersOid, hash, context = "issuer") {
|
|
78
|
+
if (algorithmOid !== OIDS.ecPublicKey) return unsupported("ECDSA", `requires EC ${context} public key`);
|
|
79
|
+
switch (parametersOid) {
|
|
80
|
+
case OIDS.prime256v1: return ok({
|
|
81
|
+
importAlgorithm: {
|
|
82
|
+
kind: "ecdsa",
|
|
83
|
+
curve: "P-256"
|
|
84
|
+
},
|
|
85
|
+
verifyParams: {
|
|
86
|
+
name: "ECDSA",
|
|
87
|
+
hash
|
|
88
|
+
},
|
|
89
|
+
ecdsaRawSignatureBytes: 64
|
|
90
|
+
});
|
|
91
|
+
case OIDS.secp384r1: return ok({
|
|
92
|
+
importAlgorithm: {
|
|
93
|
+
kind: "ecdsa",
|
|
94
|
+
curve: "P-384"
|
|
95
|
+
},
|
|
96
|
+
verifyParams: {
|
|
97
|
+
name: "ECDSA",
|
|
98
|
+
hash
|
|
99
|
+
},
|
|
100
|
+
ecdsaRawSignatureBytes: 96
|
|
101
|
+
});
|
|
102
|
+
case OIDS.secp521r1: return ok({
|
|
103
|
+
importAlgorithm: {
|
|
104
|
+
kind: "ecdsa",
|
|
105
|
+
curve: "P-521"
|
|
106
|
+
},
|
|
107
|
+
verifyParams: {
|
|
108
|
+
name: "ECDSA",
|
|
109
|
+
hash
|
|
110
|
+
},
|
|
111
|
+
ecdsaRawSignatureBytes: 132
|
|
112
|
+
});
|
|
113
|
+
default: return unsupported("ECDSA", `unsupported EC curve OID: ${parametersOid ?? "missing"}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Non-throwing variant of {@linkcode verifySignedData} — returns a typed
|
|
118
|
+
* {@linkcode VerifySignedDataResult} instead of throwing on unsupported algorithms
|
|
119
|
+
* or runtime verification failures.
|
|
120
|
+
*
|
|
121
|
+
* Tries both DER and raw ECDSA encodings when the first attempt fails.
|
|
122
|
+
*/
|
|
123
|
+
async function verifySignedDataDetailed(signatureAlgorithmOid, signatureAlgorithmParametersDer, publicKeyAlgorithmOid, publicKeyParametersOid, subjectPublicKeyInfoDer, signature, signedData) {
|
|
124
|
+
const config = getVerifySignatureConfigResult(signatureAlgorithmOid, signatureAlgorithmParametersDer, publicKeyAlgorithmOid, publicKeyParametersOid);
|
|
125
|
+
if (!config.ok) return config;
|
|
126
|
+
try {
|
|
127
|
+
const key = await importSpkiDerOrThrow(subjectPublicKeyInfoDer, config.value.importAlgorithm);
|
|
128
|
+
const subtle = getCrypto().subtle;
|
|
129
|
+
const signatureView = toArrayBuffer(signature);
|
|
130
|
+
const dataView = toArrayBuffer(signedData);
|
|
131
|
+
if (await subtle.verify(config.value.verifyParams, key, signatureView, dataView)) return {
|
|
132
|
+
ok: true,
|
|
133
|
+
valid: true
|
|
134
|
+
};
|
|
135
|
+
if (config.value.ecdsaRawSignatureBytes !== void 0) {
|
|
136
|
+
const alternate = alternateEcdsaSignatureEncoding(signature, config.value.ecdsaRawSignatureBytes / 2);
|
|
137
|
+
if (alternate !== void 0) return {
|
|
138
|
+
ok: true,
|
|
139
|
+
valid: await subtle.verify(config.value.verifyParams, key, toArrayBuffer(alternate), dataView)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
ok: true,
|
|
144
|
+
valid: false
|
|
145
|
+
};
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return verificationError(error instanceof Error ? error.message : "signature verification failed");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Parse RSA-PSS algorithm parameters and build a verification config, or return failure. */
|
|
151
|
+
function requireRsaPssVerifyConfig(signatureAlgorithmParametersDer, publicKeyAlgorithmOid, context) {
|
|
152
|
+
const parameters = parseRsaPssParameters(signatureAlgorithmParametersDer);
|
|
153
|
+
if (!parameters.ok) return unsupported("RSA-PSS", parameters.reason);
|
|
154
|
+
return requireRsaPublicKey(publicKeyAlgorithmOid, parameters.value.hash, "pss", context, parameters.value.saltLength);
|
|
155
|
+
}
|
|
156
|
+
function requireDerNullSignatureAlgorithmParameters(parametersDer, algorithm) {
|
|
157
|
+
if (parametersDer === void 0) return unsupported(algorithm, "signature AlgorithmIdentifier parameters must be DER NULL");
|
|
158
|
+
try {
|
|
159
|
+
const element = readElement(parametersDer);
|
|
160
|
+
if (element.tag !== 5 || element.length !== 0 || element.end !== parametersDer.length) return unsupported(algorithm, "signature AlgorithmIdentifier parameters must be DER NULL");
|
|
161
|
+
return;
|
|
162
|
+
} catch {
|
|
163
|
+
return unsupported(algorithm, "signature AlgorithmIdentifier parameters must be DER NULL");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function requireAbsentSignatureAlgorithmParameters(parametersDer, algorithm) {
|
|
167
|
+
if (parametersDer !== void 0) return unsupported(algorithm, "signature AlgorithmIdentifier parameters must be absent");
|
|
168
|
+
}
|
|
169
|
+
/** Wrap a config in a success result. */
|
|
170
|
+
function ok(value) {
|
|
171
|
+
return {
|
|
172
|
+
ok: true,
|
|
173
|
+
value
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/** Build an unsupported-algorithm failure result. */
|
|
177
|
+
function unsupported(algorithm, reason) {
|
|
178
|
+
return {
|
|
179
|
+
ok: false,
|
|
180
|
+
code: "unsupported_signature_algorithm_parameters",
|
|
181
|
+
reason: `${algorithm} parameters unsupported: ${reason}`
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function verificationError(reason) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
code: "verification_error",
|
|
188
|
+
reason
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
export { getVerifySignatureConfigResult, requireEcPublicKey, requireRsaPublicKey, verifySignedDataDetailed };
|
|
@@ -1,2 +1,123 @@
|
|
|
1
|
-
import{nullValue
|
|
2
|
-
|
|
1
|
+
import { nullValue, objectIdentifier, sequence } from "../asn1/der.js";
|
|
2
|
+
import { OIDS } from "../asn1/oids.js";
|
|
3
|
+
import { getCrypto } from "./webcrypto.js";
|
|
4
|
+
import { encodeRsaPssParameters, rsaPssParametersForHash } from "./rsa-pss.js";
|
|
5
|
+
import { ecdsaSignatureToDer } from "./ecdsa.js";
|
|
6
|
+
//#region src/internal/crypto/signing.ts
|
|
7
|
+
/**
|
|
8
|
+
* Signing helpers that map WebCrypto keys and signature profiles to on-wire
|
|
9
|
+
* `AlgorithmIdentifier` values. Used by certificate and CSR builders.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
/** Resolve a private key and optional profile into a {@linkcode SignatureAlgorithmIdentifier}. */
|
|
14
|
+
function getSignatureAlgorithm(privateKey, profile = {}) {
|
|
15
|
+
const algorithm = privateKey.algorithm;
|
|
16
|
+
if (profile.kind === "rsa-pss") return getRsaPssSignatureAlgorithm(privateKey, profile.saltLength);
|
|
17
|
+
if (algorithm.name === "RSASSA-PKCS1-v1_5") {
|
|
18
|
+
if (!hasHash(algorithm)) throw new Error("RSA key is missing hash metadata");
|
|
19
|
+
switch (algorithm.hash.name) {
|
|
20
|
+
case "SHA-256": return {
|
|
21
|
+
algorithmOid: OIDS.sha256WithRSAEncryption,
|
|
22
|
+
parameters: nullValue(),
|
|
23
|
+
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
24
|
+
};
|
|
25
|
+
case "SHA-384": return {
|
|
26
|
+
algorithmOid: OIDS.sha384WithRSAEncryption,
|
|
27
|
+
parameters: nullValue(),
|
|
28
|
+
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
29
|
+
};
|
|
30
|
+
case "SHA-512": return {
|
|
31
|
+
algorithmOid: OIDS.sha512WithRSAEncryption,
|
|
32
|
+
parameters: nullValue(),
|
|
33
|
+
signParams: { name: "RSASSA-PKCS1-v1_5" }
|
|
34
|
+
};
|
|
35
|
+
default: throw new Error(`Unsupported RSA hash: ${algorithm.hash.name}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (algorithm.name === "RSA-PSS") throw new Error("RSA-PSS signing requires an explicit signature profile");
|
|
39
|
+
if (algorithm.name === "ECDSA") {
|
|
40
|
+
if (!hasNamedCurve(algorithm)) throw new Error("ECDSA key is missing namedCurve metadata");
|
|
41
|
+
switch (algorithm.namedCurve) {
|
|
42
|
+
case "P-256": return {
|
|
43
|
+
algorithmOid: OIDS.ecdsaWithSHA256,
|
|
44
|
+
signParams: {
|
|
45
|
+
name: "ECDSA",
|
|
46
|
+
hash: "SHA-256"
|
|
47
|
+
},
|
|
48
|
+
ecdsaRawSignatureBytes: 64
|
|
49
|
+
};
|
|
50
|
+
case "P-384": return {
|
|
51
|
+
algorithmOid: OIDS.ecdsaWithSHA384,
|
|
52
|
+
signParams: {
|
|
53
|
+
name: "ECDSA",
|
|
54
|
+
hash: "SHA-384"
|
|
55
|
+
},
|
|
56
|
+
ecdsaRawSignatureBytes: 96
|
|
57
|
+
};
|
|
58
|
+
case "P-521": return {
|
|
59
|
+
algorithmOid: OIDS.ecdsaWithSHA512,
|
|
60
|
+
signParams: {
|
|
61
|
+
name: "ECDSA",
|
|
62
|
+
hash: "SHA-512"
|
|
63
|
+
},
|
|
64
|
+
ecdsaRawSignatureBytes: 132
|
|
65
|
+
};
|
|
66
|
+
default: throw new Error(`Unsupported curve: ${algorithm.namedCurve}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (algorithm.name === "Ed25519") return {
|
|
70
|
+
algorithmOid: OIDS.ed25519,
|
|
71
|
+
signParams: { name: "Ed25519" }
|
|
72
|
+
};
|
|
73
|
+
throw new Error(`Unsupported signing key algorithm: ${algorithm.name}`);
|
|
74
|
+
}
|
|
75
|
+
/** Build an RSA-PSS {@linkcode SignatureAlgorithmIdentifier} from an RSA-PSS private key. */
|
|
76
|
+
function getRsaPssSignatureAlgorithm(privateKey, saltLength) {
|
|
77
|
+
const algorithm = privateKey.algorithm;
|
|
78
|
+
if (algorithm.name !== "RSA-PSS") throw new Error("RSA-PSS signature profile requires an RSA-PSS private key");
|
|
79
|
+
if (!hasHash(algorithm)) throw new Error("RSA-PSS key is missing hash metadata");
|
|
80
|
+
const hash = rsaPssHashFromWebCryptoName(algorithm.hash.name);
|
|
81
|
+
const parameters = rsaPssParametersForHash(hash);
|
|
82
|
+
if (saltLength !== void 0 && saltLength !== parameters.saltLength) throw new Error(`Unsupported RSA-PSS saltLength ${saltLength} for ${hash}; expected ${parameters.saltLength}`);
|
|
83
|
+
return {
|
|
84
|
+
algorithmOid: OIDS.rsassaPss,
|
|
85
|
+
parameters: encodeRsaPssParameters(parameters),
|
|
86
|
+
signParams: {
|
|
87
|
+
name: "RSA-PSS",
|
|
88
|
+
saltLength: parameters.saltLength
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** DER-encode a {@linkcode SignatureAlgorithmIdentifier} as an ASN.1 `AlgorithmIdentifier` SEQUENCE. */
|
|
93
|
+
function encodeAlgorithmIdentifier(input) {
|
|
94
|
+
const parts = [objectIdentifier(input.algorithmOid)];
|
|
95
|
+
if (input.parameters !== void 0) parts.push(input.parameters);
|
|
96
|
+
return sequence(parts);
|
|
97
|
+
}
|
|
98
|
+
/** Sign `data` and return a DER-encoded signature. ECDSA raw signatures are auto-converted to DER. */
|
|
99
|
+
async function signBytes(privateKey, algorithm, data) {
|
|
100
|
+
const view = new Uint8Array(data);
|
|
101
|
+
const signature = new Uint8Array(await getCrypto().subtle.sign(algorithm.signParams, privateKey, view));
|
|
102
|
+
if (algorithm.ecdsaRawSignatureBytes !== void 0) return ecdsaSignatureToDer(signature, algorithm.ecdsaRawSignatureBytes);
|
|
103
|
+
return signature;
|
|
104
|
+
}
|
|
105
|
+
/** Type guard: does this key algorithm carry a `hash` property (RSA keys). */
|
|
106
|
+
function hasHash(algorithm) {
|
|
107
|
+
return "hash" in algorithm;
|
|
108
|
+
}
|
|
109
|
+
/** Type guard: does this key algorithm carry a `namedCurve` property (EC keys). */
|
|
110
|
+
function hasNamedCurve(algorithm) {
|
|
111
|
+
return "namedCurve" in algorithm;
|
|
112
|
+
}
|
|
113
|
+
/** Narrow a WebCrypto hash name string to the supported {@linkcode RsaPssHash} union. */
|
|
114
|
+
function rsaPssHashFromWebCryptoName(hash) {
|
|
115
|
+
switch (hash) {
|
|
116
|
+
case "SHA-256": return "SHA-256";
|
|
117
|
+
case "SHA-384": return "SHA-384";
|
|
118
|
+
case "SHA-512": return "SHA-512";
|
|
119
|
+
default: throw new Error(`Unsupported RSA hash: ${hash}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
export { encodeAlgorithmIdentifier, getSignatureAlgorithm, signBytes };
|
|
@@ -1,2 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/internal/crypto/webcrypto.ts
|
|
2
|
+
/**
|
|
3
|
+
* Shared WebCrypto runtime access helper.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/** Return the global `Crypto` object. Throws when WebCrypto is unavailable. */
|
|
8
|
+
function getCrypto() {
|
|
9
|
+
const c = globalThis.crypto;
|
|
10
|
+
if (c?.subtle === void 0) throw new Error("WebCrypto subtle API is required");
|
|
11
|
+
return c;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { getCrypto };
|
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/internal/shared/base64.ts
|
|
2
|
+
/** Encode raw bytes as standard base64 without line breaks. */
|
|
3
|
+
function base64Encode(bytes) {
|
|
4
|
+
const parts = [];
|
|
5
|
+
for (const byte of bytes) parts.push(String.fromCharCode(byte));
|
|
6
|
+
return btoa(parts.join(""));
|
|
7
|
+
}
|
|
8
|
+
/** Decode a standard base64 string into raw bytes. */
|
|
9
|
+
function base64Decode(value) {
|
|
10
|
+
const binary = atob(value);
|
|
11
|
+
const bytes = new Uint8Array(binary.length);
|
|
12
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
13
|
+
return bytes;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { base64Decode, base64Encode };
|
|
@@ -1,2 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/internal/shared/dn.ts
|
|
2
|
+
/** RFC 5280 §7.1 semantic equality: same RDN count, each pair matches attribute-by-attribute. */
|
|
3
|
+
function compareDistinguishedNames(left, right) {
|
|
4
|
+
if (left.rdns.length !== right.rdns.length) return false;
|
|
5
|
+
for (let index = 0; index < left.rdns.length; index += 1) {
|
|
6
|
+
const leftRdn = left.rdns[index];
|
|
7
|
+
const rightRdn = right.rdns[index];
|
|
8
|
+
if (leftRdn === void 0 || rightRdn === void 0) return false;
|
|
9
|
+
if (!compareRelativeDistinguishedNames(leftRdn, rightRdn)) return false;
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Produces a deterministic string key for a parsed DN, suitable as a Map key.
|
|
15
|
+
* Semantically equal DNs (per RFC 5280 §7.1) produce identical keys.
|
|
16
|
+
*
|
|
17
|
+
* Format: RDNs joined by `,`, attributes within each RDN sorted by OID then
|
|
18
|
+
* by prepared value, joined by `+`.
|
|
19
|
+
*/
|
|
20
|
+
function canonicalDnKey(name) {
|
|
21
|
+
return name.rdns.map(canonicalRdnKey).join(",");
|
|
22
|
+
}
|
|
23
|
+
/** True when `subject` equals or is subordinate to `constraint` (RDN prefix match). */
|
|
24
|
+
function isWithinDirectoryNameSubtree(subject, constraint) {
|
|
25
|
+
if (constraint.rdns.length > subject.rdns.length) return false;
|
|
26
|
+
for (let index = 0; index < constraint.rdns.length; index += 1) {
|
|
27
|
+
const subjectRdn = subject.rdns[index];
|
|
28
|
+
const constraintRdn = constraint.rdns[index];
|
|
29
|
+
if (subjectRdn === void 0 || constraintRdn === void 0) return false;
|
|
30
|
+
if (!compareRelativeDistinguishedNames(subjectRdn, constraintRdn)) return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
/** Order-independent RDN equality: same attribute count, each pair matched exactly once. */
|
|
35
|
+
function compareRelativeDistinguishedNames(left, right) {
|
|
36
|
+
if (left.attributes.length !== right.attributes.length) return false;
|
|
37
|
+
const matched = new Array(right.attributes.length).fill(false);
|
|
38
|
+
for (const leftAttribute of left.attributes) {
|
|
39
|
+
let found = false;
|
|
40
|
+
for (let index = 0; index < right.attributes.length; index += 1) {
|
|
41
|
+
const rightAttribute = right.attributes[index];
|
|
42
|
+
if (rightAttribute === void 0 || matched[index]) continue;
|
|
43
|
+
if (!compareNameAttributeValue(leftAttribute, rightAttribute)) continue;
|
|
44
|
+
matched[index] = true;
|
|
45
|
+
found = true;
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
if (!found) return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
/** Compares two AttributeTypeAndValue pairs using RFC 5280 §7.1 string-prep for DirectoryString tags. */
|
|
53
|
+
function compareNameAttributeValue(left, right) {
|
|
54
|
+
if (left.oid !== right.oid) return false;
|
|
55
|
+
if (isDirectoryStringTag(left.valueTag) && isDirectoryStringTag(right.valueTag)) {
|
|
56
|
+
const preparedLeft = prepareNameCompareString(left.value);
|
|
57
|
+
const preparedRight = prepareNameCompareString(right.value);
|
|
58
|
+
if (preparedLeft === void 0 || preparedRight === void 0) return false;
|
|
59
|
+
return preparedLeft === preparedRight;
|
|
60
|
+
}
|
|
61
|
+
return left.valueTag === right.valueTag && left.value === right.value;
|
|
62
|
+
}
|
|
63
|
+
/** True for UTF8String (0x0C) and PrintableString (0x13) — the DirectoryString types we normalize. */
|
|
64
|
+
function isDirectoryStringTag(tag) {
|
|
65
|
+
return tag === 12 || tag === 19;
|
|
66
|
+
}
|
|
67
|
+
/** NFKC-normalizes, lowercases, trims, and collapses whitespace for RFC 5280 §7.1 comparison. */
|
|
68
|
+
function prepareNameCompareString(value) {
|
|
69
|
+
const normalized = value.normalize("NFKC");
|
|
70
|
+
if (/[^\P{Cc}\t\n\r]/u.test(normalized)) return;
|
|
71
|
+
return normalized.toLowerCase().trim().replace(/\s+/gu, " ");
|
|
72
|
+
}
|
|
73
|
+
/** Canonical string for a single RDN: attributes sorted, values prepared. */
|
|
74
|
+
function canonicalRdnKey(rdn) {
|
|
75
|
+
return rdn.attributes.map((attr) => {
|
|
76
|
+
const val = isDirectoryStringTag(attr.valueTag) ? prepareNameCompareString(attr.value) ?? `[raw:${attr.valueTag}]${attr.value}` : `[${String(attr.valueTag)}]${attr.value}`;
|
|
77
|
+
return `${attr.oid}=${escapeCanonicalDnValue(val)}`;
|
|
78
|
+
}).sort().join("+");
|
|
79
|
+
}
|
|
80
|
+
function escapeCanonicalDnValue(value) {
|
|
81
|
+
return value.replaceAll("\\", "\\\\").replaceAll(",", "\\,").replaceAll("+", "\\+").replaceAll("=", "\\=");
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { canonicalDnKey, compareDistinguishedNames, compareNameAttributeValue, compareRelativeDistinguishedNames, isDirectoryStringTag, isWithinDirectoryNameSubtree, prepareNameCompareString };
|