micro509 0.1.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/LICENSE +22 -0
- package/README.md +220 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +1 -0
- package/dist/internal/asn1/asn1.js +2 -0
- package/dist/internal/asn1/asn1.js.map +1 -0
- package/dist/internal/asn1/der.js +2 -0
- package/dist/internal/asn1/der.js.map +1 -0
- package/dist/internal/asn1/oids.js +2 -0
- package/dist/internal/asn1/oids.js.map +1 -0
- package/dist/internal/crypto/algorithm-names.js +2 -0
- package/dist/internal/crypto/algorithm-names.js.map +1 -0
- package/dist/internal/crypto/ecdsa.js +2 -0
- package/dist/internal/crypto/ecdsa.js.map +1 -0
- package/dist/internal/crypto/hash.js +2 -0
- package/dist/internal/crypto/hash.js.map +1 -0
- package/dist/internal/crypto/pbes2.d.ts +23 -0
- package/dist/internal/crypto/pbes2.js +2 -0
- package/dist/internal/crypto/pbes2.js.map +1 -0
- package/dist/internal/crypto/rsa-pss.js +2 -0
- package/dist/internal/crypto/rsa-pss.js.map +1 -0
- package/dist/internal/crypto/sig-verify.js +2 -0
- package/dist/internal/crypto/sig-verify.js.map +1 -0
- package/dist/internal/crypto/signing.d.ts +16 -0
- package/dist/internal/crypto/signing.js +2 -0
- package/dist/internal/crypto/signing.js.map +1 -0
- package/dist/internal/crypto/webcrypto.js +2 -0
- package/dist/internal/crypto/webcrypto.js.map +1 -0
- package/dist/internal/shared/base64.js +2 -0
- package/dist/internal/shared/base64.js.map +1 -0
- package/dist/internal/shared/dn.js +2 -0
- package/dist/internal/shared/dn.js.map +1 -0
- package/dist/internal/shared/ip.js +2 -0
- package/dist/internal/shared/ip.js.map +1 -0
- package/dist/internal/verify/name-constraints-engine.js +2 -0
- package/dist/internal/verify/name-constraints-engine.js.map +1 -0
- package/dist/internal/verify/policy-engine.js +2 -0
- package/dist/internal/verify/policy-engine.js.map +1 -0
- package/dist/internal/verify/verify-path.js +2 -0
- package/dist/internal/verify/verify-path.js.map +1 -0
- package/dist/internal/x509/extension-bits.d.ts +18 -0
- package/dist/internal/x509/extension-bits.js +2 -0
- package/dist/internal/x509/extension-bits.js.map +1 -0
- package/dist/internal/x509/extension-registry.js +2 -0
- package/dist/internal/x509/extension-registry.js.map +1 -0
- package/dist/internal/x509/name-fields.js +2 -0
- package/dist/internal/x509/name-fields.js.map +1 -0
- package/dist/keys/keys.d.ts +431 -0
- package/dist/keys/keys.js +5 -0
- package/dist/keys/keys.js.map +1 -0
- package/dist/keys.d.ts +3 -0
- package/dist/keys.js +1 -0
- package/dist/pem/pem.d.ts +56 -0
- package/dist/pem/pem.js +6 -0
- package/dist/pem/pem.js.map +1 -0
- package/dist/pem.d.ts +2 -0
- package/dist/pem.js +1 -0
- package/dist/pkcs/pfx.d.ts +177 -0
- package/dist/pkcs/pfx.js +2 -0
- package/dist/pkcs/pfx.js.map +1 -0
- package/dist/pkcs/pkcs12-mac.d.ts +41 -0
- package/dist/pkcs/pkcs12-mac.js +2 -0
- package/dist/pkcs/pkcs12-mac.js.map +1 -0
- package/dist/pkcs/pkcs7.d.ts +131 -0
- package/dist/pkcs/pkcs7.js +2 -0
- package/dist/pkcs/pkcs7.js.map +1 -0
- package/dist/pkcs.d.ts +5 -0
- package/dist/pkcs.js +1 -0
- package/dist/result/result.d.ts +68 -0
- package/dist/result/result.js +2 -0
- package/dist/result/result.js.map +1 -0
- package/dist/result.d.ts +2 -0
- package/dist/result.js +1 -0
- package/dist/revocation/chain.d.ts +180 -0
- package/dist/revocation/chain.js +2 -0
- package/dist/revocation/chain.js.map +1 -0
- package/dist/revocation/crl.d.ts +316 -0
- package/dist/revocation/crl.js +2 -0
- package/dist/revocation/crl.js.map +1 -0
- package/dist/revocation/ocsp.d.ts +332 -0
- package/dist/revocation/ocsp.js +2 -0
- package/dist/revocation/ocsp.js.map +1 -0
- package/dist/revocation/revocation.d.ts +168 -0
- package/dist/revocation/revocation.js +2 -0
- package/dist/revocation/revocation.js.map +1 -0
- package/dist/revocation.d.ts +5 -0
- package/dist/revocation.js +1 -0
- package/dist/verify/identity.d.ts +129 -0
- package/dist/verify/identity.js +2 -0
- package/dist/verify/identity.js.map +1 -0
- package/dist/verify/name-constraints.d.ts +18 -0
- package/dist/verify/policy.d.ts +39 -0
- package/dist/verify/verify.d.ts +404 -0
- package/dist/verify/verify.js +2 -0
- package/dist/verify/verify.js.map +1 -0
- package/dist/verify.d.ts +5 -0
- package/dist/verify.js +1 -0
- package/dist/x509/certificate.d.ts +191 -0
- package/dist/x509/certificate.js +2 -0
- package/dist/x509/certificate.js.map +1 -0
- package/dist/x509/csr.d.ts +55 -0
- package/dist/x509/csr.js +2 -0
- package/dist/x509/csr.js.map +1 -0
- package/dist/x509/extensions.d.ts +550 -0
- package/dist/x509/extensions.js +2 -0
- package/dist/x509/extensions.js.map +1 -0
- package/dist/x509/name.d.ts +140 -0
- package/dist/x509/name.js +2 -0
- package/dist/x509/name.js.map +1 -0
- package/dist/x509/parse.d.ts +377 -0
- package/dist/x509/parse.js +2 -0
- package/dist/x509/parse.js.map +1 -0
- package/dist/x509.d.ts +8 -0
- package/dist/x509.js +1 -0
- package/package.json +153 -0
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DnsServiceIdentityInput, IpServiceIdentityInput, MatchServiceIdentityErrorCode, MatchServiceIdentityEvaluation, MatchServiceIdentityFailure, MatchServiceIdentityFailureDetails, MatchServiceIdentityFailureResult, MatchServiceIdentityInput, MatchServiceIdentityResult, MatchServiceIdentitySuccess, MatchableServiceIdentityInput, ServiceIdentityInput, ServiceIdentityType, SrvServiceIdentityInput, UriServiceIdentityInput, VerifyServiceIdentityInput, matchCertificateServiceIdentity, matchServiceIdentity } from "./verify/identity.js";
|
|
2
|
+
import { InitialNameConstraintsInput } from "./verify/name-constraints.js";
|
|
3
|
+
import { ConstrainedPolicy, PolicyValidationInput, PolicyValidationOutcome } from "./verify/policy.js";
|
|
4
|
+
import { BuildCandidatePathInput, BuildCandidatePathResult, CandidatePath, CertificateSource, ChainRevocationInput, CsrSource, EkuCheckFailure, EkuCheckPurpose, EkuCheckResult, TrustAnchor, ValidateCandidatePathInput, ValidateCandidatePathResult, ValidateCandidatePathSuccess, ValidateForCaInput, ValidateForCodeSigningInput, ValidateForTlsClientInput, ValidateForTlsServerInput, VerifiedCertificateChain, VerifyCertificateChainInput, VerifyChainFailure, VerifyChainResult, VerifyErrorCode, VerifyFailureDetails, VerifyPurpose, VerifyRequestFailure, VerifyRequestResult, buildCandidatePath, checkExtendedKeyUsage, trustAnchorFromCertificate, validateCandidatePath, validateForCa, validateForCodeSigning, validateForTlsClient, validateForTlsServer, verifyCertificateChain, verifyCertificateSigningRequest } from "./verify/verify.js";
|
|
5
|
+
export { type BuildCandidatePathInput, type BuildCandidatePathResult, type CandidatePath, type CertificateSource, type ChainRevocationInput, type ConstrainedPolicy, type CsrSource, type DnsServiceIdentityInput, type EkuCheckFailure, type EkuCheckPurpose, type EkuCheckResult, type InitialNameConstraintsInput, type IpServiceIdentityInput, type MatchServiceIdentityErrorCode, type MatchServiceIdentityEvaluation, type MatchServiceIdentityFailure, type MatchServiceIdentityFailureDetails, type MatchServiceIdentityFailureResult, type MatchServiceIdentityInput, type MatchServiceIdentityResult, type MatchServiceIdentitySuccess, type MatchableServiceIdentityInput, type PolicyValidationInput, type PolicyValidationOutcome, type ServiceIdentityInput, type ServiceIdentityType, type SrvServiceIdentityInput, type TrustAnchor, type UriServiceIdentityInput, type ValidateCandidatePathInput, type ValidateCandidatePathResult, type ValidateCandidatePathSuccess, type ValidateForCaInput, type ValidateForCodeSigningInput, type ValidateForTlsClientInput, type ValidateForTlsServerInput, type VerifiedCertificateChain, type VerifyCertificateChainInput, type VerifyChainFailure, type VerifyChainResult, type VerifyErrorCode, type VerifyFailureDetails, type VerifyPurpose, type VerifyRequestFailure, type VerifyRequestResult, type VerifyServiceIdentityInput, buildCandidatePath, checkExtendedKeyUsage, matchCertificateServiceIdentity, matchServiceIdentity, trustAnchorFromCertificate, validateCandidatePath, validateForCa, validateForCodeSigning, validateForTlsClient, validateForTlsServer, verifyCertificateChain, verifyCertificateSigningRequest };
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{matchCertificateServiceIdentity as e,matchServiceIdentity as t}from"./verify/identity.js";import{buildCandidatePath as n,checkExtendedKeyUsage as r,trustAnchorFromCertificate as i,validateCandidatePath as a,validateForCa as o,validateForCodeSigning as s,validateForTlsClient as c,validateForTlsServer as l,verifyCertificateChain as u,verifyCertificateSigningRequest as d}from"./verify/verify.js";export{n as buildCandidatePath,r as checkExtendedKeyUsage,e as matchCertificateServiceIdentity,t as matchServiceIdentity,i as trustAnchorFromCertificate,a as validateCandidatePath,o as validateForCa,s as validateForCodeSigning,c as validateForTlsClient,l as validateForTlsServer,u as verifyCertificateChain,d as verifyCertificateSigningRequest};
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { KeyAlgorithmInput, KeyPairMaterial } from "../keys/keys.js";
|
|
2
|
+
import { NameAttribute, NameFieldKey, NameInput, NameObject, RelativeDistinguishedNameInput, encodeName, encodeRelativeDistinguishedName } from "./name.js";
|
|
3
|
+
import { 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, 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 } from "./extensions.js";
|
|
4
|
+
import { SignatureProfileInput } from "../internal/crypto/signing.js";
|
|
5
|
+
|
|
6
|
+
//#region src/x509/certificate.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* Configures the certificate validity window.
|
|
9
|
+
*
|
|
10
|
+
* If `notAfter` is omitted, it is derived from `notBefore` plus `days`. If both
|
|
11
|
+
* `notAfter` and `days` are omitted, the certificate is valid for 30 days.
|
|
12
|
+
*/
|
|
13
|
+
interface ValidityInput {
|
|
14
|
+
/**
|
|
15
|
+
* Start of the validity window.
|
|
16
|
+
*
|
|
17
|
+
* Defaults to the current time.
|
|
18
|
+
*/
|
|
19
|
+
readonly notBefore?: Date;
|
|
20
|
+
/**
|
|
21
|
+
* End of the validity window.
|
|
22
|
+
*
|
|
23
|
+
* Must be later than `notBefore`.
|
|
24
|
+
*/
|
|
25
|
+
readonly notAfter?: Date;
|
|
26
|
+
/**
|
|
27
|
+
* Number of days to add to `notBefore` when `notAfter` is omitted.
|
|
28
|
+
*/
|
|
29
|
+
readonly days?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Input for {@linkcode createCertificate}.
|
|
33
|
+
*/
|
|
34
|
+
interface CreateCertificateInput {
|
|
35
|
+
/**
|
|
36
|
+
* Issuer distinguished name.
|
|
37
|
+
*/
|
|
38
|
+
readonly issuer: NameInput;
|
|
39
|
+
/**
|
|
40
|
+
* Subject distinguished name.
|
|
41
|
+
*/
|
|
42
|
+
readonly subject: NameInput;
|
|
43
|
+
/**
|
|
44
|
+
* Subject public key to encode into the certificate.
|
|
45
|
+
*/
|
|
46
|
+
readonly publicKey: CryptoKey;
|
|
47
|
+
/**
|
|
48
|
+
* Private key used to sign the certificate.
|
|
49
|
+
*/
|
|
50
|
+
readonly signerPrivateKey: CryptoKey;
|
|
51
|
+
/**
|
|
52
|
+
* Issuer public key.
|
|
53
|
+
*
|
|
54
|
+
* Provide this when extension builders need issuer key material, such as
|
|
55
|
+
* authority key identifier derivation.
|
|
56
|
+
*/
|
|
57
|
+
readonly issuerPublicKey?: CryptoKey;
|
|
58
|
+
/**
|
|
59
|
+
* Validity window configuration.
|
|
60
|
+
*/
|
|
61
|
+
readonly validity?: ValidityInput;
|
|
62
|
+
/**
|
|
63
|
+
* DER integer bytes for the certificate serial number.
|
|
64
|
+
*
|
|
65
|
+
* When omitted, a random positive 16-byte serial number is generated.
|
|
66
|
+
*/
|
|
67
|
+
readonly serialNumber?: Uint8Array;
|
|
68
|
+
/**
|
|
69
|
+
* X.509 extensions to encode into the certificate.
|
|
70
|
+
*/
|
|
71
|
+
readonly extensions?: CertificateExtensionsInput;
|
|
72
|
+
/**
|
|
73
|
+
* Signature algorithm override.
|
|
74
|
+
*
|
|
75
|
+
* When omitted, the library selects a compatible profile from the signing
|
|
76
|
+
* key.
|
|
77
|
+
*/
|
|
78
|
+
readonly signature?: SignatureProfileInput;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Input for {@linkcode createSelfSignedCertificate}.
|
|
82
|
+
*/
|
|
83
|
+
interface CreateSelfSignedCertificateInput {
|
|
84
|
+
/**
|
|
85
|
+
* Subject distinguished name used as both subject and issuer.
|
|
86
|
+
*/
|
|
87
|
+
readonly subject: NameInput;
|
|
88
|
+
/**
|
|
89
|
+
* Key generation parameters.
|
|
90
|
+
*
|
|
91
|
+
* Ignored when `keyPair` is provided.
|
|
92
|
+
*/
|
|
93
|
+
readonly algorithm?: KeyAlgorithmInput;
|
|
94
|
+
/**
|
|
95
|
+
* Existing key pair to reuse for both subject and issuer.
|
|
96
|
+
*
|
|
97
|
+
* When omitted, a new key pair is generated.
|
|
98
|
+
*/
|
|
99
|
+
readonly keyPair?: KeyPairMaterial;
|
|
100
|
+
/**
|
|
101
|
+
* Validity window configuration.
|
|
102
|
+
*/
|
|
103
|
+
readonly validity?: ValidityInput;
|
|
104
|
+
/**
|
|
105
|
+
* DER integer bytes for the certificate serial number.
|
|
106
|
+
*/
|
|
107
|
+
readonly serialNumber?: Uint8Array;
|
|
108
|
+
/**
|
|
109
|
+
* X.509 extensions to encode into the certificate.
|
|
110
|
+
*/
|
|
111
|
+
readonly extensions?: CertificateExtensionsInput;
|
|
112
|
+
/**
|
|
113
|
+
* Signature algorithm override.
|
|
114
|
+
*/
|
|
115
|
+
readonly signature?: SignatureProfileInput;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Encoded certificate material in common interchange formats.
|
|
119
|
+
*/
|
|
120
|
+
interface CertificateMaterial {
|
|
121
|
+
/**
|
|
122
|
+
* DER-encoded certificate bytes.
|
|
123
|
+
*/
|
|
124
|
+
readonly der: Uint8Array;
|
|
125
|
+
/**
|
|
126
|
+
* PEM-encoded certificate.
|
|
127
|
+
*/
|
|
128
|
+
readonly pem: string;
|
|
129
|
+
/**
|
|
130
|
+
* Base64 encoding of {@linkcode der} without PEM armor.
|
|
131
|
+
*/
|
|
132
|
+
readonly base64: string;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Result returned by {@linkcode createSelfSignedCertificate}.
|
|
136
|
+
*/
|
|
137
|
+
interface SelfSignedCertificateResult {
|
|
138
|
+
/**
|
|
139
|
+
* Encoded certificate outputs.
|
|
140
|
+
*/
|
|
141
|
+
readonly certificate: CertificateMaterial;
|
|
142
|
+
/**
|
|
143
|
+
* Key pair used to issue the certificate.
|
|
144
|
+
*/
|
|
145
|
+
readonly keyPair: KeyPairMaterial;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create a self-signed certificate.
|
|
149
|
+
*
|
|
150
|
+
* Reuses `input.keyPair` when provided; otherwise generates a new key pair from
|
|
151
|
+
* `input.algorithm`. The returned certificate uses `input.subject` as both
|
|
152
|
+
* issuer and subject.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const { certificate, keyPair } = await createSelfSignedCertificate({
|
|
157
|
+
* subject: { commonName: 'example.com' },
|
|
158
|
+
* algorithm: { kind: 'ecdsa', curve: 'P-256' },
|
|
159
|
+
* });
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @param input Certificate subject, key, validity, and extension settings.
|
|
163
|
+
* @returns The certificate plus the key pair used to sign it.
|
|
164
|
+
*/
|
|
165
|
+
declare function createSelfSignedCertificate(input: CreateSelfSignedCertificateInput): Promise<SelfSignedCertificateResult>;
|
|
166
|
+
/**
|
|
167
|
+
* Create an X.509 certificate signed by `input.signerPrivateKey`.
|
|
168
|
+
*
|
|
169
|
+
* The certificate encodes `input.subject`, `input.publicKey`, and any supplied
|
|
170
|
+
* extensions. When `serialNumber` is omitted, a random positive serial number is
|
|
171
|
+
* generated. When `validity` is omitted, the certificate is valid from now for
|
|
172
|
+
* 30 days.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const certificate = await createCertificate({
|
|
177
|
+
* issuer: { commonName: 'Example Root CA' },
|
|
178
|
+
* subject: { commonName: 'example.com' },
|
|
179
|
+
* publicKey: leafKeys.publicKey,
|
|
180
|
+
* signerPrivateKey: issuerKeys.privateKey,
|
|
181
|
+
* issuerPublicKey: issuerKeys.publicKey,
|
|
182
|
+
* });
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @param input Issuer, subject, key, validity, and extension settings.
|
|
186
|
+
* @returns The encoded certificate material.
|
|
187
|
+
*/
|
|
188
|
+
declare function createCertificate(input: CreateCertificateInput): Promise<CertificateMaterial>;
|
|
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 ParsedNameConstraintForm, type PolicyConstraints, type PolicyInformation, type PolicyMapping, type PolicyMappings, type PolicyNoticeReference, type PolicyQualifierInfo, SelfSignedCertificateResult, 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
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{bitString as e,explicitContext as t,integer as n,integerFromNumber as r,sequence as i,time as a}from"../internal/asn1/der.js";import{getCrypto as o}from"../internal/crypto/webcrypto.js";import{base64Encode as s}from"../internal/shared/base64.js";import{pemEncode as c}from"../pem/pem.js";import{exportSpkiDer as l,generateKeyPair as u}from"../keys/keys.js";import{encodeName as d,isNameInputEmpty as f}from"./name.js";import{buildCertificateExtensions as p}from"./extensions.js";import{encodeAlgorithmIdentifier as m,getSignatureAlgorithm as h,signBytes as g}from"../internal/crypto/signing.js";async function _(e){let t=e.keyPair??await u(e.algorithm);return{certificate:await v({issuer:e.subject,subject:e.subject,publicKey:t.publicKey,signerPrivateKey:t.privateKey,issuerPublicKey:t.publicKey,...e.validity===void 0?{}:{validity:e.validity},...e.serialNumber===void 0?{}:{serialNumber:e.serialNumber},...e.extensions===void 0?{}:{extensions:e.extensions},...e.signature===void 0?{}:{signature:e.signature}}),keyPair:t}}async function v(o){let s=await l(o.publicKey),c=o.issuerPublicKey?await l(o.issuerPublicKey):void 0,u=h(o.signerPrivateKey,o.signature),_=b(o.validity),v=f(o.subject),x=p(s,c,o.extensions,v),C=i([t(0,r(2)),n(o.serialNumber??S()),m(u),d(o.issuer),i([a(_.notBefore),a(_.notAfter)]),d(o.subject),s,t(3,i(x))]),w=await g(o.signerPrivateKey,u,C);return y(i([C,m(u),e(w)]))}function y(e){return{der:e,pem:c(`CERTIFICATE`,e),base64:s(e)}}function b(e){let t=e?.notBefore??new Date,n=e?.notAfter??x(t,e?.days??30);if(n.getTime()<=t.getTime())throw Error(`notAfter must be after notBefore`);return{notBefore:t,notAfter:n}}function x(e,t){let n=new Date(e.getTime());return n.setUTCDate(n.getUTCDate()+t),n}function S(){let e=o().getRandomValues(new Uint8Array(16));return e[0]=(e[0]??0)&127,(e[0]??0)===0&&(e[0]=1),e}export{v as createCertificate,_ as createSelfSignedCertificate};
|
|
2
|
+
//# sourceMappingURL=certificate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"certificate.js","names":[],"sources":["../../src/x509/certificate.ts"],"sourcesContent":["/**\n * Create X.509 certificates from typed names, extensions, and WebCrypto keys.\n *\n * Use this module to issue a certificate from an existing issuer key pair or to\n * generate a self-signed certificate in one step.\n *\n * @module\n */\n\nimport {\n\tbitString,\n\texplicitContext,\n\tinteger,\n\tintegerFromNumber,\n\tsequence,\n\ttime,\n} from '#micro509/internal/asn1/der.ts';\nimport {\n\tencodeAlgorithmIdentifier,\n\tgetSignatureAlgorithm,\n\ttype SignatureProfileInput,\n\tsignBytes,\n} from '#micro509/internal/crypto/signing.ts';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto.ts';\nimport { base64Encode } from '#micro509/internal/shared/base64.ts';\nimport {\n\texportSpkiDer,\n\tgenerateKeyPair,\n\ttype KeyAlgorithmInput,\n\ttype KeyPairMaterial,\n} from '#micro509/keys/keys.ts';\nimport { pemEncode } from '#micro509/pem/pem.ts';\nimport { buildCertificateExtensions, type CertificateExtensionsInput } from './extensions.ts';\nimport { encodeName, isNameInputEmpty, type NameInput } from './name.ts';\n\nexport type * from './extensions.ts';\nexport type * from './name.ts';\n\n/**\n * Configures the certificate validity window.\n *\n * If `notAfter` is omitted, it is derived from `notBefore` plus `days`. If both\n * `notAfter` and `days` are omitted, the certificate is valid for 30 days.\n */\nexport interface ValidityInput {\n\t/**\n\t * Start of the validity window.\n\t *\n\t * Defaults to the current time.\n\t */\n\treadonly notBefore?: Date;\n\t/**\n\t * End of the validity window.\n\t *\n\t * Must be later than `notBefore`.\n\t */\n\treadonly notAfter?: Date;\n\t/**\n\t * Number of days to add to `notBefore` when `notAfter` is omitted.\n\t */\n\treadonly days?: number;\n}\n\n/**\n * Input for {@linkcode createCertificate}.\n */\nexport interface CreateCertificateInput {\n\t/**\n\t * Issuer distinguished name.\n\t */\n\treadonly issuer: NameInput;\n\t/**\n\t * Subject distinguished name.\n\t */\n\treadonly subject: NameInput;\n\t/**\n\t * Subject public key to encode into the certificate.\n\t */\n\treadonly publicKey: CryptoKey;\n\t/**\n\t * Private key used to sign the certificate.\n\t */\n\treadonly signerPrivateKey: CryptoKey;\n\t/**\n\t * Issuer public key.\n\t *\n\t * Provide this when extension builders need issuer key material, such as\n\t * authority key identifier derivation.\n\t */\n\treadonly issuerPublicKey?: CryptoKey;\n\t/**\n\t * Validity window configuration.\n\t */\n\treadonly validity?: ValidityInput;\n\t/**\n\t * DER integer bytes for the certificate serial number.\n\t *\n\t * When omitted, a random positive 16-byte serial number is generated.\n\t */\n\treadonly serialNumber?: Uint8Array;\n\t/**\n\t * X.509 extensions to encode into the certificate.\n\t */\n\treadonly extensions?: CertificateExtensionsInput;\n\t/**\n\t * Signature algorithm override.\n\t *\n\t * When omitted, the library selects a compatible profile from the signing\n\t * key.\n\t */\n\treadonly signature?: SignatureProfileInput;\n}\n\n/**\n * Input for {@linkcode createSelfSignedCertificate}.\n */\nexport interface CreateSelfSignedCertificateInput {\n\t/**\n\t * Subject distinguished name used as both subject and issuer.\n\t */\n\treadonly subject: NameInput;\n\t/**\n\t * Key generation parameters.\n\t *\n\t * Ignored when `keyPair` is provided.\n\t */\n\treadonly algorithm?: KeyAlgorithmInput;\n\t/**\n\t * Existing key pair to reuse for both subject and issuer.\n\t *\n\t * When omitted, a new key pair is generated.\n\t */\n\treadonly keyPair?: KeyPairMaterial;\n\t/**\n\t * Validity window configuration.\n\t */\n\treadonly validity?: ValidityInput;\n\t/**\n\t * DER integer bytes for the certificate serial number.\n\t */\n\treadonly serialNumber?: Uint8Array;\n\t/**\n\t * X.509 extensions to encode into the certificate.\n\t */\n\treadonly extensions?: CertificateExtensionsInput;\n\t/**\n\t * Signature algorithm override.\n\t */\n\treadonly signature?: SignatureProfileInput;\n}\n\n/**\n * Encoded certificate material in common interchange formats.\n */\nexport interface CertificateMaterial {\n\t/**\n\t * DER-encoded certificate bytes.\n\t */\n\treadonly der: Uint8Array;\n\t/**\n\t * PEM-encoded certificate.\n\t */\n\treadonly pem: string;\n\t/**\n\t * Base64 encoding of {@linkcode der} without PEM armor.\n\t */\n\treadonly base64: string;\n}\n\n/**\n * Result returned by {@linkcode createSelfSignedCertificate}.\n */\nexport interface SelfSignedCertificateResult {\n\t/**\n\t * Encoded certificate outputs.\n\t */\n\treadonly certificate: CertificateMaterial;\n\t/**\n\t * Key pair used to issue the certificate.\n\t */\n\treadonly keyPair: KeyPairMaterial;\n}\n\n/**\n * Create a self-signed certificate.\n *\n * Reuses `input.keyPair` when provided; otherwise generates a new key pair from\n * `input.algorithm`. The returned certificate uses `input.subject` as both\n * issuer and subject.\n *\n * @example\n * ```ts\n * const { certificate, keyPair } = await createSelfSignedCertificate({\n * \tsubject: { commonName: 'example.com' },\n * \talgorithm: { kind: 'ecdsa', curve: 'P-256' },\n * });\n * ```\n *\n * @param input Certificate subject, key, validity, and extension settings.\n * @returns The certificate plus the key pair used to sign it.\n */\nexport async function createSelfSignedCertificate(\n\tinput: CreateSelfSignedCertificateInput,\n): Promise<SelfSignedCertificateResult> {\n\tconst keyPair = input.keyPair ?? (await generateKeyPair(input.algorithm));\n\tconst certificateInput = {\n\t\tissuer: input.subject,\n\t\tsubject: input.subject,\n\t\tpublicKey: keyPair.publicKey,\n\t\tsignerPrivateKey: keyPair.privateKey,\n\t\tissuerPublicKey: keyPair.publicKey,\n\t\t...(input.validity !== undefined ? { validity: input.validity } : {}),\n\t\t...(input.serialNumber !== undefined ? { serialNumber: input.serialNumber } : {}),\n\t\t...(input.extensions !== undefined ? { extensions: input.extensions } : {}),\n\t\t...(input.signature !== undefined ? { signature: input.signature } : {}),\n\t} satisfies CreateCertificateInput;\n\tconst certificate = await createCertificate(certificateInput);\n\n\treturn { certificate, keyPair };\n}\n\n/**\n * Create an X.509 certificate signed by `input.signerPrivateKey`.\n *\n * The certificate encodes `input.subject`, `input.publicKey`, and any supplied\n * extensions. When `serialNumber` is omitted, a random positive serial number is\n * generated. When `validity` is omitted, the certificate is valid from now for\n * 30 days.\n *\n * @example\n * ```ts\n * const certificate = await createCertificate({\n * \tissuer: { commonName: 'Example Root CA' },\n * \tsubject: { commonName: 'example.com' },\n * \tpublicKey: leafKeys.publicKey,\n * \tsignerPrivateKey: issuerKeys.privateKey,\n * \tissuerPublicKey: issuerKeys.publicKey,\n * });\n * ```\n *\n * @param input Issuer, subject, key, validity, and extension settings.\n * @returns The encoded certificate material.\n */\nexport async function createCertificate(\n\tinput: CreateCertificateInput,\n): Promise<CertificateMaterial> {\n\tconst subjectPublicKeyInfo = await exportSpkiDer(input.publicKey);\n\tconst issuerPublicKeyInfo = input.issuerPublicKey\n\t\t? await exportSpkiDer(input.issuerPublicKey)\n\t\t: undefined;\n\tconst signatureAlgorithm = getSignatureAlgorithm(input.signerPrivateKey, input.signature);\n\tconst validity = resolveValidity(input.validity);\n\tconst subjectIsEmpty = isNameInputEmpty(input.subject);\n\tconst extensions = buildCertificateExtensions(\n\t\tsubjectPublicKeyInfo,\n\t\tissuerPublicKeyInfo,\n\t\tinput.extensions,\n\t\tsubjectIsEmpty,\n\t);\n\tconst tbsCertificate = sequence([\n\t\texplicitContext(0, integerFromNumber(2)),\n\t\tinteger(input.serialNumber ?? randomSerialNumber()),\n\t\tencodeAlgorithmIdentifier(signatureAlgorithm),\n\t\tencodeName(input.issuer),\n\t\tsequence([time(validity.notBefore), time(validity.notAfter)]),\n\t\tencodeName(input.subject),\n\t\tsubjectPublicKeyInfo,\n\t\texplicitContext(3, sequence(extensions)),\n\t]);\n\n\tconst signatureValue = await signBytes(\n\t\tinput.signerPrivateKey,\n\t\tsignatureAlgorithm,\n\t\ttbsCertificate,\n\t);\n\tconst certificateDer = sequence([\n\t\ttbsCertificate,\n\t\tencodeAlgorithmIdentifier(signatureAlgorithm),\n\t\tbitString(signatureValue),\n\t]);\n\n\treturn materializeCertificate(certificateDer);\n}\n\n/**\n * Convert DER bytes into all exported certificate encodings.\n *\n * @param der DER-encoded certificate bytes.\n * @returns DER, PEM, and base64 views of the same certificate.\n */\nfunction materializeCertificate(der: Uint8Array): CertificateMaterial {\n\treturn {\n\t\tder,\n\t\tpem: pemEncode('CERTIFICATE', der),\n\t\tbase64: base64Encode(der),\n\t};\n}\n\n/**\n * Normalized validity window used during certificate creation.\n */\ninterface ResolvedValidity {\n\t/**\n\t * Start of the validity window.\n\t */\n\treadonly notBefore: Date;\n\t/**\n\t * End of the validity window.\n\t */\n\treadonly notAfter: Date;\n}\n\n/**\n * Resolve defaults for a certificate validity window.\n *\n * @param input Optional validity settings.\n * @returns A concrete `notBefore` and `notAfter` pair.\n */\nfunction resolveValidity(input: ValidityInput | undefined): ResolvedValidity {\n\tconst notBefore = input?.notBefore ?? new Date();\n\tconst notAfter = input?.notAfter ?? addDays(notBefore, input?.days ?? 30);\n\tif (notAfter.getTime() <= notBefore.getTime()) {\n\t\tthrow new Error('notAfter must be after notBefore');\n\t}\n\treturn { notBefore, notAfter };\n}\n\n/**\n * Return a new date offset by a whole number of UTC days.\n *\n * @param date Base date.\n * @param days Number of days to add.\n * @returns The shifted date.\n */\nfunction addDays(date: Date, days: number): Date {\n\tconst out = new Date(date.getTime());\n\tout.setUTCDate(out.getUTCDate() + days);\n\treturn out;\n}\n\n/**\n * Generate a random positive serial number suitable for certificate issuance.\n *\n * @returns A 16-byte positive serial number.\n */\nfunction randomSerialNumber(): Uint8Array {\n\tconst serial = getCrypto().getRandomValues(new Uint8Array(16));\n\tconst first = serial[0] ?? 0;\n\tserial[0] = first & 0x7f;\n\tif ((serial[0] ?? 0) === 0) {\n\t\tserial[0] = 1;\n\t}\n\treturn serial;\n}\n"],"mappings":"0lBAyMA,eAAsB,EACrB,EACuC,CACvC,IAAM,EAAU,EAAM,SAAY,MAAM,EAAgB,EAAM,SAAS,EAcvE,MAAO,CAAE,YAAA,MAFiB,EAAkB,CAV3C,OAAQ,EAAM,QACd,QAAS,EAAM,QACf,UAAW,EAAQ,UACnB,iBAAkB,EAAQ,WAC1B,gBAAiB,EAAQ,UACzB,GAAI,EAAM,WAAa,IAAA,GAA2C,CAAC,EAAhC,CAAE,SAAU,EAAM,QAAS,EAC9D,GAAI,EAAM,eAAiB,IAAA,GAAmD,CAAC,EAAxC,CAAE,aAAc,EAAM,YAAa,EAC1E,GAAI,EAAM,aAAe,IAAA,GAA+C,CAAC,EAApC,CAAE,WAAY,EAAM,UAAW,EACpE,GAAI,EAAM,YAAc,IAAA,GAA6C,CAAC,EAAlC,CAAE,UAAW,EAAM,SAAU,CAEP,CAAC,EAEtC,SAAQ,CAC/B,CAwBA,eAAsB,EACrB,EAC+B,CAC/B,IAAM,EAAuB,MAAM,EAAc,EAAM,SAAS,EAC1D,EAAsB,EAAM,gBAC/B,MAAM,EAAc,EAAM,eAAe,EACzC,IAAA,GACG,EAAqB,EAAsB,EAAM,iBAAkB,EAAM,SAAS,EAClF,EAAW,EAAgB,EAAM,QAAQ,EACzC,EAAiB,EAAiB,EAAM,OAAO,EAC/C,EAAa,EAClB,EACA,EACA,EAAM,WACN,CACD,EACM,EAAiB,EAAS,CAC/B,EAAgB,EAAG,EAAkB,CAAC,CAAC,EACvC,EAAQ,EAAM,cAAgB,EAAmB,CAAC,EAClD,EAA0B,CAAkB,EAC5C,EAAW,EAAM,MAAM,EACvB,EAAS,CAAC,EAAK,EAAS,SAAS,EAAG,EAAK,EAAS,QAAQ,CAAC,CAAC,EAC5D,EAAW,EAAM,OAAO,EACxB,EACA,EAAgB,EAAG,EAAS,CAAU,CAAC,CACxC,CAAC,EAEK,EAAiB,MAAM,EAC5B,EAAM,iBACN,EACA,CACD,EAOA,OAAO,EANgB,EAAS,CAC/B,EACA,EAA0B,CAAkB,EAC5C,EAAU,CAAc,CACzB,CAE2C,CAAC,CAC7C,CAQA,SAAS,EAAuB,EAAsC,CACrE,MAAO,CACN,MACA,IAAK,EAAU,cAAe,CAAG,EACjC,OAAQ,EAAa,CAAG,CACzB,CACD,CAsBA,SAAS,EAAgB,EAAoD,CAC5E,IAAM,EAAY,GAAO,WAAa,IAAI,KACpC,EAAW,GAAO,UAAY,EAAQ,EAAW,GAAO,MAAQ,EAAE,EACxE,GAAI,EAAS,QAAQ,GAAK,EAAU,QAAQ,EAC3C,MAAU,MAAM,kCAAkC,EAEnD,MAAO,CAAE,YAAW,UAAS,CAC9B,CASA,SAAS,EAAQ,EAAY,EAAoB,CAChD,IAAM,EAAM,IAAI,KAAK,EAAK,QAAQ,CAAC,EAEnC,OADA,EAAI,WAAW,EAAI,WAAW,EAAI,CAAI,EAC/B,CACR,CAOA,SAAS,GAAiC,CACzC,IAAM,EAAS,EAAU,CAAC,CAAC,gBAAgB,IAAI,WAAW,EAAE,CAAC,EAM7D,MAJA,GAAO,IADO,EAAO,IAAM,GACP,KACf,EAAO,IAAM,KAAO,IACxB,EAAO,GAAK,GAEN,CACR"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NameAttribute, NameFieldKey, NameInput, NameObject, RelativeDistinguishedNameInput, encodeName, encodeRelativeDistinguishedName } from "./name.js";
|
|
2
|
+
import { 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, 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 } from "./extensions.js";
|
|
3
|
+
import { SignatureProfileInput } from "../internal/crypto/signing.js";
|
|
4
|
+
|
|
5
|
+
//#region src/x509/csr.d.ts
|
|
6
|
+
/** Input for {@linkcode createCertificateSigningRequest}. */
|
|
7
|
+
interface CreateCsrInput {
|
|
8
|
+
/** Distinguished name for the CSR subject (e.g. `{ commonName: 'example.com' }`). */
|
|
9
|
+
readonly subject: NameInput;
|
|
10
|
+
/** WebCrypto public key to embed in the CSR's SubjectPublicKeyInfo. */
|
|
11
|
+
readonly publicKey: CryptoKey;
|
|
12
|
+
/** WebCrypto private key used to self-sign the CSR (proves key possession). */
|
|
13
|
+
readonly signerPrivateKey: CryptoKey;
|
|
14
|
+
/** Requested X.509v3 extensions to include in the CSR attributes. */
|
|
15
|
+
readonly extensions?: CertificateExtensionsInput;
|
|
16
|
+
/** Override the signature algorithm profile (hash, salt length, etc.). */
|
|
17
|
+
readonly signature?: SignatureProfileInput;
|
|
18
|
+
}
|
|
19
|
+
/** DER, PEM, and base64 encodings of a CSR produced by {@linkcode createCertificateSigningRequest}. */
|
|
20
|
+
interface CsrMaterial {
|
|
21
|
+
/** Raw DER-encoded PKCS#10 CertificationRequest. */
|
|
22
|
+
readonly der: Uint8Array;
|
|
23
|
+
/** PEM-armored CSR (`-----BEGIN CERTIFICATE REQUEST-----`). */
|
|
24
|
+
readonly pem: string;
|
|
25
|
+
/** Base64-encoded DER (no PEM armor). */
|
|
26
|
+
readonly base64: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a PKCS#10 Certificate Signing Request signed with the given private key.
|
|
30
|
+
*
|
|
31
|
+
* The CSR embeds the public key's SPKI, the subject name, and any requested extensions
|
|
32
|
+
* as attributes. The signature proves possession of the private key.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { createCertificateSigningRequest } from 'micro509';
|
|
37
|
+
*
|
|
38
|
+
* const keyPair = await crypto.subtle.generateKey(
|
|
39
|
+
* { name: 'ECDSA', namedCurve: 'P-256' },
|
|
40
|
+
* true,
|
|
41
|
+
* ['sign', 'verify'],
|
|
42
|
+
* );
|
|
43
|
+
* const csr = await createCertificateSigningRequest({
|
|
44
|
+
* subject: { commonName: 'example.com' },
|
|
45
|
+
* publicKey: keyPair.publicKey,
|
|
46
|
+
* signerPrivateKey: keyPair.privateKey,
|
|
47
|
+
* extensions: { subjectAltNames: [{ type: 'dns', value: 'example.com' }] },
|
|
48
|
+
* });
|
|
49
|
+
* console.log(csr.pem);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function createCertificateSigningRequest(input: CreateCsrInput): Promise<CsrMaterial>;
|
|
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 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
|
package/dist/x509/csr.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{bitString as e,concatBytes as t,implicitConstructedContext as n,integerFromNumber as r,objectIdentifier as i,sequence as a,setOf as o}from"../internal/asn1/der.js";import{OIDS as s}from"../internal/asn1/oids.js";import{base64Encode as c}from"../internal/shared/base64.js";import{pemEncode as l}from"../pem/pem.js";import{exportSpkiDer as u}from"../keys/keys.js";import{encodeName as d}from"./name.js";import{buildRequestedExtensions as f}from"./extensions.js";import{encodeAlgorithmIdentifier as p,getSignatureAlgorithm as m,signBytes as h}from"../internal/crypto/signing.js";async function g(i){let o=m(i.signerPrivateKey,i.signature),s=await u(i.publicKey),f=_(i.extensions),g=a([r(0),d(i.subject),s,n(0,t(f))]),v=await h(i.signerPrivateKey,o,g),y=a([g,p(o),e(v)]);return{der:y,pem:l(`CERTIFICATE REQUEST`,y),base64:c(y)}}function _(e){let t=f(e);return t.length===0?[]:[a([i(s.extensionRequest),o([a(t)])])]}export{g as createCertificateSigningRequest};
|
|
2
|
+
//# sourceMappingURL=csr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csr.js","names":[],"sources":["../../src/x509/csr.ts"],"sourcesContent":["/**\n * PKCS#10 Certificate Signing Request (CSR) creation.\n *\n * Builds a CSR from a subject name, WebCrypto key pair, and optional extensions,\n * producing DER, PEM, and base64 outputs.\n *\n * @module\n */\n\nimport {\n\tbitString,\n\tconcatBytes,\n\timplicitConstructedContext,\n\tintegerFromNumber,\n\tobjectIdentifier,\n\tsequence,\n\tsetOf,\n} from '#micro509/internal/asn1/der.ts';\nimport { OIDS } from '#micro509/internal/asn1/oids.ts';\nimport {\n\tencodeAlgorithmIdentifier,\n\tgetSignatureAlgorithm,\n\ttype SignatureProfileInput,\n\tsignBytes,\n} from '#micro509/internal/crypto/signing.ts';\nimport { base64Encode } from '#micro509/internal/shared/base64.ts';\nimport { exportSpkiDer } from '#micro509/keys/keys.ts';\nimport { pemEncode } from '#micro509/pem/pem.ts';\nimport { buildRequestedExtensions, type CertificateExtensionsInput } from './extensions.ts';\nimport { encodeName, type NameInput } from './name.ts';\n\nexport type * from './extensions.ts';\nexport type * from './name.ts';\n\n/** Input for {@linkcode createCertificateSigningRequest}. */\nexport interface CreateCsrInput {\n\t/** Distinguished name for the CSR subject (e.g. `{ commonName: 'example.com' }`). */\n\treadonly subject: NameInput;\n\t/** WebCrypto public key to embed in the CSR's SubjectPublicKeyInfo. */\n\treadonly publicKey: CryptoKey;\n\t/** WebCrypto private key used to self-sign the CSR (proves key possession). */\n\treadonly signerPrivateKey: CryptoKey;\n\t/** Requested X.509v3 extensions to include in the CSR attributes. */\n\treadonly extensions?: CertificateExtensionsInput;\n\t/** Override the signature algorithm profile (hash, salt length, etc.). */\n\treadonly signature?: SignatureProfileInput;\n}\n\n/** DER, PEM, and base64 encodings of a CSR produced by {@linkcode createCertificateSigningRequest}. */\nexport interface CsrMaterial {\n\t/** Raw DER-encoded PKCS#10 CertificationRequest. */\n\treadonly der: Uint8Array;\n\t/** PEM-armored CSR (`-----BEGIN CERTIFICATE REQUEST-----`). */\n\treadonly pem: string;\n\t/** Base64-encoded DER (no PEM armor). */\n\treadonly base64: string;\n}\n\n/**\n * Creates a PKCS#10 Certificate Signing Request signed with the given private key.\n *\n * The CSR embeds the public key's SPKI, the subject name, and any requested extensions\n * as attributes. The signature proves possession of the private key.\n *\n * @example\n * ```ts\n * import { createCertificateSigningRequest } from 'micro509';\n *\n * const keyPair = await crypto.subtle.generateKey(\n * { name: 'ECDSA', namedCurve: 'P-256' },\n * true,\n * ['sign', 'verify'],\n * );\n * const csr = await createCertificateSigningRequest({\n * subject: { commonName: 'example.com' },\n * publicKey: keyPair.publicKey,\n * signerPrivateKey: keyPair.privateKey,\n * extensions: { subjectAltNames: [{ type: 'dns', value: 'example.com' }] },\n * });\n * console.log(csr.pem);\n * ```\n */\nexport async function createCertificateSigningRequest(input: CreateCsrInput): Promise<CsrMaterial> {\n\tconst signatureAlgorithm = getSignatureAlgorithm(input.signerPrivateKey, input.signature);\n\tconst spki = await exportSpkiDer(input.publicKey);\n\tconst attributes = buildAttributes(input.extensions);\n\tconst certificationRequestInfo = sequence([\n\t\tintegerFromNumber(0),\n\t\tencodeName(input.subject),\n\t\tspki,\n\t\timplicitConstructedContext(0, concatBytes(attributes)),\n\t]);\n\tconst normalizedSignature = await signBytes(\n\t\tinput.signerPrivateKey,\n\t\tsignatureAlgorithm,\n\t\tcertificationRequestInfo,\n\t);\n\n\tconst der = sequence([\n\t\tcertificationRequestInfo,\n\t\tencodeAlgorithmIdentifier(signatureAlgorithm),\n\t\tbitString(normalizedSignature),\n\t]);\n\treturn {\n\t\tder,\n\t\tpem: pemEncode('CERTIFICATE REQUEST', der),\n\t\tbase64: base64Encode(der),\n\t};\n}\n\n/** Encodes extensions into a CSR extensionRequest attribute, or returns empty. */\nfunction buildAttributes(extensions: CertificateExtensionsInput | undefined): Uint8Array[] {\n\tconst builtExtensions = buildRequestedExtensions(extensions);\n\tif (builtExtensions.length === 0) {\n\t\treturn [];\n\t}\n\treturn [sequence([objectIdentifier(OIDS.extensionRequest), setOf([sequence(builtExtensions)])])];\n}\n"],"mappings":"ukBAkFA,eAAsB,EAAgC,EAA6C,CAClG,IAAM,EAAqB,EAAsB,EAAM,iBAAkB,EAAM,SAAS,EAClF,EAAO,MAAM,EAAc,EAAM,SAAS,EAC1C,EAAa,EAAgB,EAAM,UAAU,EAC7C,EAA2B,EAAS,CACzC,EAAkB,CAAC,EACnB,EAAW,EAAM,OAAO,EACxB,EACA,EAA2B,EAAG,EAAY,CAAU,CAAC,CACtD,CAAC,EACK,EAAsB,MAAM,EACjC,EAAM,iBACN,EACA,CACD,EAEM,EAAM,EAAS,CACpB,EACA,EAA0B,CAAkB,EAC5C,EAAU,CAAmB,CAC9B,CAAC,EACD,MAAO,CACN,MACA,IAAK,EAAU,sBAAuB,CAAG,EACzC,OAAQ,EAAa,CAAG,CACzB,CACD,CAGA,SAAS,EAAgB,EAAkE,CAC1F,IAAM,EAAkB,EAAyB,CAAU,EAI3D,OAHI,EAAgB,SAAW,EACvB,CAAC,EAEF,CAAC,EAAS,CAAC,EAAiB,EAAK,gBAAgB,EAAG,EAAM,CAAC,EAAS,CAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAChG"}
|