micro509 0.7.2 → 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/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
package/dist/verify.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
import{matchCertificateServiceIdentity
|
|
1
|
+
import { matchCertificateServiceIdentity, matchServiceIdentity } from "./verify/identity.js";
|
|
2
|
+
import { VERIFY_ERROR_CODES, buildCandidatePath, checkExtendedKeyUsage, trustAnchorFromCertificate, validateCandidatePath, validateForCa, validateForCodeSigning, validateForTlsClient, validateForTlsServer, verifyCertificateChain, verifyCertificateSigningRequest } from "./verify/verify.js";
|
|
3
|
+
export { VERIFY_ERROR_CODES, buildCandidatePath, checkExtendedKeyUsage, matchCertificateServiceIdentity, matchServiceIdentity, trustAnchorFromCertificate, validateCandidatePath, validateForCa, validateForCodeSigning, validateForTlsClient, validateForTlsServer, verifyCertificateChain, verifyCertificateSigningRequest };
|
|
@@ -187,5 +187,4 @@ declare function createSelfSignedCertificate(input: CreateSelfSignedCertificateI
|
|
|
187
187
|
*/
|
|
188
188
|
declare function createCertificate(input: CreateCertificateInput): Promise<CertificateMaterial>;
|
|
189
189
|
//#endregion
|
|
190
|
-
export { type AuthorityInfoAccessMethod, type AuthorityInformationAccess, type BasicConstraints, type CertificateExtensionsInput, CertificateMaterial, type CertificatePolicies, type CpsPolicyQualifierInfo, CreateCertificateInput, CreateSelfSignedCertificateInput, type CustomAuthorityInfoAccessMethod, type CustomExtendedKeyUsage, type CustomExtension, type CustomPolicyQualifierInfo, type DistributionPoint, type DistributionPointName, type DistributionPointReason, type ExtendedKeyUsage, type GeneralName, type GeneralSubtree, type InhibitAnyPolicy, type IssuingDistributionPoint, type IssuingDistributionPointBase, type IssuingDistributionPointForAttributeCerts, type IssuingDistributionPointForCaCerts, type IssuingDistributionPointForUserCerts, type KeyUsage, type KnownAuthorityInfoAccessMethod, type KnownExtendedKeyUsage, type NameConstraintForm, type NameConstraints, type NameInput, type NameObject, type ParsedBitFlags, type ParsedNameConstraintForm, type PolicyConstraints, type PolicyInformation, type PolicyMapping, type PolicyMappings, type PolicyNoticeReference, type PolicyQualifierInfo, SelfSignedCertificateResult, type SignatureProfileInput, type SubjectAltName, type UnsupportedNameConstraintForm, type UserNoticePolicyQualifierInfo, ValidityInput, type buildCertificateExtensions, type buildRequestedExtensions, createCertificate, createSelfSignedCertificate, type encodeAuthorityInfoAccess, type encodeBasicConstraints, type encodeCertificatePolicies, type encodeCrlDistributionPoints, type encodeExtendedKeyUsage, type encodeExtension, type encodeInhibitAnyPolicy, type encodeKeyUsage, type encodeName, type encodeNameConstraints, type encodePolicyConstraints, type encodePolicyMappings, type encodeRelativeDistinguishedName, type encodeSubjectAltName, type getAuthorityInfoAccessMethodOid, type getExtendedKeyUsageOid, type parseAuthorityInfoAccessMethodOid, type parseExtendedKeyUsageOid };
|
|
191
|
-
//# sourceMappingURL=certificate.d.ts.map
|
|
190
|
+
export { type AuthorityInfoAccessMethod, type AuthorityInformationAccess, type BasicConstraints, type CertificateExtensionsInput, CertificateMaterial, type CertificatePolicies, type CpsPolicyQualifierInfo, CreateCertificateInput, CreateSelfSignedCertificateInput, type CustomAuthorityInfoAccessMethod, type CustomExtendedKeyUsage, type CustomExtension, type CustomPolicyQualifierInfo, type DistributionPoint, type DistributionPointName, type DistributionPointReason, type ExtendedKeyUsage, type GeneralName, type GeneralSubtree, type InhibitAnyPolicy, type IssuingDistributionPoint, type IssuingDistributionPointBase, type IssuingDistributionPointForAttributeCerts, type IssuingDistributionPointForCaCerts, type IssuingDistributionPointForUserCerts, type KeyUsage, type KnownAuthorityInfoAccessMethod, type KnownExtendedKeyUsage, type NameConstraintForm, type NameConstraints, type NameInput, type NameObject, type ParsedBitFlags, type ParsedNameConstraintForm, type PolicyConstraints, type PolicyInformation, type PolicyMapping, type PolicyMappings, type PolicyNoticeReference, type PolicyQualifierInfo, SelfSignedCertificateResult, type SignatureProfileInput, type SubjectAltName, type UnsupportedNameConstraintForm, type UserNoticePolicyQualifierInfo, ValidityInput, type buildCertificateExtensions, type buildRequestedExtensions, createCertificate, createSelfSignedCertificate, type encodeAuthorityInfoAccess, type encodeBasicConstraints, type encodeCertificatePolicies, type encodeCrlDistributionPoints, type encodeExtendedKeyUsage, type encodeExtension, type encodeInhibitAnyPolicy, type encodeKeyUsage, type encodeName, type encodeNameConstraints, type encodePolicyConstraints, type encodePolicyMappings, type encodeRelativeDistinguishedName, type encodeSubjectAltName, type getAuthorityInfoAccessMethodOid, type getExtendedKeyUsageOid, type parseAuthorityInfoAccessMethodOid, type parseExtendedKeyUsageOid };
|
package/dist/x509/certificate.js
CHANGED
|
@@ -1,2 +1,151 @@
|
|
|
1
|
-
import{bitString
|
|
2
|
-
|
|
1
|
+
import { bitString, explicitContext, integer, integerFromNumber, sequence, time } from "../internal/asn1/der.js";
|
|
2
|
+
import { getCrypto } from "../internal/crypto/webcrypto.js";
|
|
3
|
+
import { base64Encode } from "../internal/shared/base64.js";
|
|
4
|
+
import { pemEncode } from "../pem/pem.js";
|
|
5
|
+
import { exportSpkiDer, generateKeyPair } from "../keys/keys.js";
|
|
6
|
+
import { encodeName, isNameInputEmpty } from "./name.js";
|
|
7
|
+
import { buildCertificateExtensions } from "./extensions.js";
|
|
8
|
+
import { encodeAlgorithmIdentifier, getSignatureAlgorithm, signBytes } from "../internal/crypto/signing.js";
|
|
9
|
+
//#region src/x509/certificate.ts
|
|
10
|
+
/**
|
|
11
|
+
* Create X.509 certificates from typed names, extensions, and WebCrypto keys.
|
|
12
|
+
*
|
|
13
|
+
* Use this module to issue a certificate from an existing issuer key pair or to
|
|
14
|
+
* generate a self-signed certificate in one step.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Create a self-signed certificate.
|
|
20
|
+
*
|
|
21
|
+
* Reuses `input.keyPair` when provided; otherwise generates a new key pair from
|
|
22
|
+
* `input.algorithm`. The returned certificate uses `input.subject` as both
|
|
23
|
+
* issuer and subject.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const { certificate, keyPair } = await createSelfSignedCertificate({
|
|
28
|
+
* subject: { commonName: 'example.com' },
|
|
29
|
+
* algorithm: { kind: 'ecdsa', curve: 'P-256' },
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param input Certificate subject, key, validity, and extension settings.
|
|
34
|
+
* @returns The certificate plus the key pair used to sign it.
|
|
35
|
+
*/
|
|
36
|
+
async function createSelfSignedCertificate(input) {
|
|
37
|
+
const keyPair = input.keyPair ?? await generateKeyPair(input.algorithm);
|
|
38
|
+
return {
|
|
39
|
+
certificate: await createCertificate({
|
|
40
|
+
issuer: input.subject,
|
|
41
|
+
subject: input.subject,
|
|
42
|
+
publicKey: keyPair.publicKey,
|
|
43
|
+
signerPrivateKey: keyPair.privateKey,
|
|
44
|
+
issuerPublicKey: keyPair.publicKey,
|
|
45
|
+
...input.validity !== void 0 ? { validity: input.validity } : {},
|
|
46
|
+
...input.serialNumber !== void 0 ? { serialNumber: input.serialNumber } : {},
|
|
47
|
+
...input.extensions !== void 0 ? { extensions: input.extensions } : {},
|
|
48
|
+
...input.signature !== void 0 ? { signature: input.signature } : {}
|
|
49
|
+
}),
|
|
50
|
+
keyPair
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create an X.509 certificate signed by `input.signerPrivateKey`.
|
|
55
|
+
*
|
|
56
|
+
* The certificate encodes `input.subject`, `input.publicKey`, and any supplied
|
|
57
|
+
* extensions. When `serialNumber` is omitted, a random positive serial number is
|
|
58
|
+
* generated. When `validity` is omitted, the certificate is valid from now for
|
|
59
|
+
* 30 days.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const certificate = await createCertificate({
|
|
64
|
+
* issuer: { commonName: 'Example Root CA' },
|
|
65
|
+
* subject: { commonName: 'example.com' },
|
|
66
|
+
* publicKey: leafKeys.publicKey,
|
|
67
|
+
* signerPrivateKey: issuerKeys.privateKey,
|
|
68
|
+
* issuerPublicKey: issuerKeys.publicKey,
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @param input Issuer, subject, key, validity, and extension settings.
|
|
73
|
+
* @returns The encoded certificate material.
|
|
74
|
+
*/
|
|
75
|
+
async function createCertificate(input) {
|
|
76
|
+
const subjectPublicKeyInfo = await exportSpkiDer(input.publicKey);
|
|
77
|
+
const issuerPublicKeyInfo = input.issuerPublicKey ? await exportSpkiDer(input.issuerPublicKey) : void 0;
|
|
78
|
+
const signatureAlgorithm = getSignatureAlgorithm(input.signerPrivateKey, input.signature);
|
|
79
|
+
const validity = resolveValidity(input.validity);
|
|
80
|
+
const subjectIsEmpty = isNameInputEmpty(input.subject);
|
|
81
|
+
const extensions = buildCertificateExtensions(subjectPublicKeyInfo, issuerPublicKeyInfo, input.extensions, subjectIsEmpty);
|
|
82
|
+
const tbsCertificate = sequence([
|
|
83
|
+
explicitContext(0, integerFromNumber(2)),
|
|
84
|
+
integer(input.serialNumber ?? randomSerialNumber()),
|
|
85
|
+
encodeAlgorithmIdentifier(signatureAlgorithm),
|
|
86
|
+
encodeName(input.issuer),
|
|
87
|
+
sequence([time(validity.notBefore), time(validity.notAfter)]),
|
|
88
|
+
encodeName(input.subject),
|
|
89
|
+
subjectPublicKeyInfo,
|
|
90
|
+
explicitContext(3, sequence(extensions))
|
|
91
|
+
]);
|
|
92
|
+
const signatureValue = await signBytes(input.signerPrivateKey, signatureAlgorithm, tbsCertificate);
|
|
93
|
+
return materializeCertificate(sequence([
|
|
94
|
+
tbsCertificate,
|
|
95
|
+
encodeAlgorithmIdentifier(signatureAlgorithm),
|
|
96
|
+
bitString(signatureValue)
|
|
97
|
+
]));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convert DER bytes into all exported certificate encodings.
|
|
101
|
+
*
|
|
102
|
+
* @param der DER-encoded certificate bytes.
|
|
103
|
+
* @returns DER, PEM, and base64 views of the same certificate.
|
|
104
|
+
*/
|
|
105
|
+
function materializeCertificate(der) {
|
|
106
|
+
return {
|
|
107
|
+
der,
|
|
108
|
+
pem: pemEncode("CERTIFICATE", der),
|
|
109
|
+
base64: base64Encode(der)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolve defaults for a certificate validity window.
|
|
114
|
+
*
|
|
115
|
+
* @param input Optional validity settings.
|
|
116
|
+
* @returns A concrete `notBefore` and `notAfter` pair.
|
|
117
|
+
*/
|
|
118
|
+
function resolveValidity(input) {
|
|
119
|
+
const notBefore = input?.notBefore ?? /* @__PURE__ */ new Date();
|
|
120
|
+
const notAfter = input?.notAfter ?? addDays(notBefore, input?.days ?? 30);
|
|
121
|
+
if (notAfter.getTime() <= notBefore.getTime()) throw new Error("notAfter must be after notBefore");
|
|
122
|
+
return {
|
|
123
|
+
notBefore,
|
|
124
|
+
notAfter
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Return a new date offset by a whole number of UTC days.
|
|
129
|
+
*
|
|
130
|
+
* @param date Base date.
|
|
131
|
+
* @param days Number of days to add.
|
|
132
|
+
* @returns The shifted date.
|
|
133
|
+
*/
|
|
134
|
+
function addDays(date, days) {
|
|
135
|
+
const out = new Date(date.getTime());
|
|
136
|
+
out.setUTCDate(out.getUTCDate() + days);
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Generate a random positive serial number suitable for certificate issuance.
|
|
141
|
+
*
|
|
142
|
+
* @returns A 16-byte positive serial number.
|
|
143
|
+
*/
|
|
144
|
+
function randomSerialNumber() {
|
|
145
|
+
const serial = getCrypto().getRandomValues(/* @__PURE__ */ new Uint8Array(16));
|
|
146
|
+
serial[0] = (serial[0] ?? 0) & 127;
|
|
147
|
+
if ((serial[0] ?? 0) === 0) serial[0] = 1;
|
|
148
|
+
return serial;
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
export { createCertificate, createSelfSignedCertificate };
|
package/dist/x509/csr.d.ts
CHANGED
|
@@ -51,5 +51,4 @@ interface CsrMaterial {
|
|
|
51
51
|
*/
|
|
52
52
|
declare function createCertificateSigningRequest(input: CreateCsrInput): Promise<CsrMaterial>;
|
|
53
53
|
//#endregion
|
|
54
|
-
export { type AuthorityInfoAccessMethod, type AuthorityInformationAccess, type BasicConstraints, type CertificateExtensionsInput, type CertificatePolicies, type CpsPolicyQualifierInfo, CreateCsrInput, CsrMaterial, type CustomAuthorityInfoAccessMethod, type CustomExtendedKeyUsage, type CustomExtension, type CustomPolicyQualifierInfo, type DistributionPoint, type DistributionPointName, type DistributionPointReason, type ExtendedKeyUsage, type GeneralName, type GeneralSubtree, type InhibitAnyPolicy, type IssuingDistributionPoint, type IssuingDistributionPointBase, type IssuingDistributionPointForAttributeCerts, type IssuingDistributionPointForCaCerts, type IssuingDistributionPointForUserCerts, type KeyUsage, type KnownAuthorityInfoAccessMethod, type KnownExtendedKeyUsage, type NameConstraintForm, type NameConstraints, type NameInput, type NameObject, type ParsedBitFlags, type ParsedNameConstraintForm, type PolicyConstraints, type PolicyInformation, type PolicyMapping, type PolicyMappings, type PolicyNoticeReference, type PolicyQualifierInfo, type SubjectAltName, type UnsupportedNameConstraintForm, type UserNoticePolicyQualifierInfo, type buildCertificateExtensions, type buildRequestedExtensions, createCertificateSigningRequest, type encodeAuthorityInfoAccess, type encodeBasicConstraints, type encodeCertificatePolicies, type encodeCrlDistributionPoints, type encodeExtendedKeyUsage, type encodeExtension, type encodeInhibitAnyPolicy, type encodeKeyUsage, type encodeName, type encodeNameConstraints, type encodePolicyConstraints, type encodePolicyMappings, type encodeRelativeDistinguishedName, type encodeSubjectAltName, type getAuthorityInfoAccessMethodOid, type getExtendedKeyUsageOid, type parseAuthorityInfoAccessMethodOid, type parseExtendedKeyUsageOid };
|
|
55
|
-
//# sourceMappingURL=csr.d.ts.map
|
|
54
|
+
export { type AuthorityInfoAccessMethod, type AuthorityInformationAccess, type BasicConstraints, type CertificateExtensionsInput, type CertificatePolicies, type CpsPolicyQualifierInfo, CreateCsrInput, CsrMaterial, type CustomAuthorityInfoAccessMethod, type CustomExtendedKeyUsage, type CustomExtension, type CustomPolicyQualifierInfo, type DistributionPoint, type DistributionPointName, type DistributionPointReason, type ExtendedKeyUsage, type GeneralName, type GeneralSubtree, type InhibitAnyPolicy, type IssuingDistributionPoint, type IssuingDistributionPointBase, type IssuingDistributionPointForAttributeCerts, type IssuingDistributionPointForCaCerts, type IssuingDistributionPointForUserCerts, type KeyUsage, type KnownAuthorityInfoAccessMethod, type KnownExtendedKeyUsage, type NameConstraintForm, type NameConstraints, type NameInput, type NameObject, type ParsedBitFlags, type ParsedNameConstraintForm, type PolicyConstraints, type PolicyInformation, type PolicyMapping, type PolicyMappings, type PolicyNoticeReference, type PolicyQualifierInfo, type SubjectAltName, type UnsupportedNameConstraintForm, type UserNoticePolicyQualifierInfo, type buildCertificateExtensions, type buildRequestedExtensions, createCertificateSigningRequest, type encodeAuthorityInfoAccess, type encodeBasicConstraints, type encodeCertificatePolicies, type encodeCrlDistributionPoints, type encodeExtendedKeyUsage, type encodeExtension, type encodeInhibitAnyPolicy, type encodeKeyUsage, type encodeName, type encodeNameConstraints, type encodePolicyConstraints, type encodePolicyMappings, type encodeRelativeDistinguishedName, type encodeSubjectAltName, type getAuthorityInfoAccessMethodOid, type getExtendedKeyUsageOid, type parseAuthorityInfoAccessMethodOid, type parseExtendedKeyUsageOid };
|
package/dist/x509/csr.js
CHANGED
|
@@ -1,2 +1,71 @@
|
|
|
1
|
-
import{bitString
|
|
2
|
-
|
|
1
|
+
import { bitString, concatBytes, implicitConstructedContext, integerFromNumber, objectIdentifier, sequence, setOf } from "../internal/asn1/der.js";
|
|
2
|
+
import { OIDS } from "../internal/asn1/oids.js";
|
|
3
|
+
import { base64Encode } from "../internal/shared/base64.js";
|
|
4
|
+
import { pemEncode } from "../pem/pem.js";
|
|
5
|
+
import { exportSpkiDer } from "../keys/keys.js";
|
|
6
|
+
import { encodeName } from "./name.js";
|
|
7
|
+
import { buildRequestedExtensions } from "./extensions.js";
|
|
8
|
+
import { encodeAlgorithmIdentifier, getSignatureAlgorithm, signBytes } from "../internal/crypto/signing.js";
|
|
9
|
+
//#region src/x509/csr.ts
|
|
10
|
+
/**
|
|
11
|
+
* PKCS#10 Certificate Signing Request (CSR) creation.
|
|
12
|
+
*
|
|
13
|
+
* Builds a CSR from a subject name, WebCrypto key pair, and optional extensions,
|
|
14
|
+
* producing DER, PEM, and base64 outputs.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Creates a PKCS#10 Certificate Signing Request signed with the given private key.
|
|
20
|
+
*
|
|
21
|
+
* The CSR embeds the public key's SPKI, the subject name, and any requested extensions
|
|
22
|
+
* as attributes. The signature proves possession of the private key.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { createCertificateSigningRequest } from 'micro509';
|
|
27
|
+
*
|
|
28
|
+
* const keyPair = await crypto.subtle.generateKey(
|
|
29
|
+
* { name: 'ECDSA', namedCurve: 'P-256' },
|
|
30
|
+
* true,
|
|
31
|
+
* ['sign', 'verify'],
|
|
32
|
+
* );
|
|
33
|
+
* const csr = await createCertificateSigningRequest({
|
|
34
|
+
* subject: { commonName: 'example.com' },
|
|
35
|
+
* publicKey: keyPair.publicKey,
|
|
36
|
+
* signerPrivateKey: keyPair.privateKey,
|
|
37
|
+
* extensions: { subjectAltNames: [{ type: 'dns', value: 'example.com' }] },
|
|
38
|
+
* });
|
|
39
|
+
* console.log(csr.pem);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
async function createCertificateSigningRequest(input) {
|
|
43
|
+
const signatureAlgorithm = getSignatureAlgorithm(input.signerPrivateKey, input.signature);
|
|
44
|
+
const spki = await exportSpkiDer(input.publicKey);
|
|
45
|
+
const attributes = buildAttributes(input.extensions);
|
|
46
|
+
const certificationRequestInfo = sequence([
|
|
47
|
+
integerFromNumber(0),
|
|
48
|
+
encodeName(input.subject),
|
|
49
|
+
spki,
|
|
50
|
+
implicitConstructedContext(0, concatBytes(attributes))
|
|
51
|
+
]);
|
|
52
|
+
const normalizedSignature = await signBytes(input.signerPrivateKey, signatureAlgorithm, certificationRequestInfo);
|
|
53
|
+
const der = sequence([
|
|
54
|
+
certificationRequestInfo,
|
|
55
|
+
encodeAlgorithmIdentifier(signatureAlgorithm),
|
|
56
|
+
bitString(normalizedSignature)
|
|
57
|
+
]);
|
|
58
|
+
return {
|
|
59
|
+
der,
|
|
60
|
+
pem: pemEncode("CERTIFICATE REQUEST", der),
|
|
61
|
+
base64: base64Encode(der)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Encodes extensions into a CSR extensionRequest attribute, or returns empty. */
|
|
65
|
+
function buildAttributes(extensions) {
|
|
66
|
+
const builtExtensions = buildRequestedExtensions(extensions);
|
|
67
|
+
if (builtExtensions.length === 0) return [];
|
|
68
|
+
return [sequence([objectIdentifier(OIDS.extensionRequest), setOf([sequence(builtExtensions)])])];
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { createCertificateSigningRequest };
|
|
@@ -561,5 +561,4 @@ declare function getAuthorityInfoAccessMethodOid(method: AuthorityInfoAccessMeth
|
|
|
561
561
|
*/
|
|
562
562
|
declare function parseAuthorityInfoAccessMethodOid(oid: string): AuthorityInfoAccessMethod;
|
|
563
563
|
//#endregion
|
|
564
|
-
export { AuthorityInfoAccessMethod, AuthorityInformationAccess, BasicConstraints, CertificateExtensionsInput, CertificatePolicies, CpsPolicyQualifierInfo, CustomAuthorityInfoAccessMethod, CustomExtendedKeyUsage, CustomExtension, CustomPolicyQualifierInfo, DistributionPoint, DistributionPointName, DistributionPointReason, ExtendedKeyUsage, GeneralName, GeneralSubtree, InhibitAnyPolicy, IssuingDistributionPoint, IssuingDistributionPointBase, IssuingDistributionPointForAttributeCerts, IssuingDistributionPointForCaCerts, IssuingDistributionPointForUserCerts, KeyUsage, KnownAuthorityInfoAccessMethod, KnownExtendedKeyUsage, NameConstraintForm, NameConstraints, ParsedBitFlags, ParsedNameConstraintForm, PolicyConstraints, PolicyInformation, PolicyMapping, PolicyMappings, PolicyNoticeReference, PolicyQualifierInfo, SubjectAltName, UnsupportedNameConstraintForm, UserNoticePolicyQualifierInfo, buildCertificateExtensions, buildRequestedExtensions, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid };
|
|
565
|
-
//# sourceMappingURL=extensions.d.ts.map
|
|
564
|
+
export { AuthorityInfoAccessMethod, AuthorityInformationAccess, BasicConstraints, CertificateExtensionsInput, CertificatePolicies, CpsPolicyQualifierInfo, CustomAuthorityInfoAccessMethod, CustomExtendedKeyUsage, CustomExtension, CustomPolicyQualifierInfo, DistributionPoint, DistributionPointName, DistributionPointReason, ExtendedKeyUsage, GeneralName, GeneralSubtree, InhibitAnyPolicy, IssuingDistributionPoint, IssuingDistributionPointBase, IssuingDistributionPointForAttributeCerts, IssuingDistributionPointForCaCerts, IssuingDistributionPointForUserCerts, KeyUsage, KnownAuthorityInfoAccessMethod, KnownExtendedKeyUsage, NameConstraintForm, NameConstraints, ParsedBitFlags, ParsedNameConstraintForm, PolicyConstraints, PolicyInformation, PolicyMapping, PolicyMappings, PolicyNoticeReference, PolicyQualifierInfo, SubjectAltName, UnsupportedNameConstraintForm, UserNoticePolicyQualifierInfo, buildCertificateExtensions, buildRequestedExtensions, encodeAuthorityInfoAccess, encodeBasicConstraints, encodeCertificatePolicies, encodeCrlDistributionPoints, encodeExtendedKeyUsage, encodeExtension, encodeInhibitAnyPolicy, encodeKeyUsage, encodeNameConstraints, encodePolicyConstraints, encodePolicyMappings, encodeSubjectAltName, getAuthorityInfoAccessMethodOid, getExtendedKeyUsageOid, parseAuthorityInfoAccessMethodOid, parseExtendedKeyUsageOid };
|