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.
Files changed (109) hide show
  1. package/dist/index.d.ts +5 -5
  2. package/dist/index.js +15 -1
  3. package/dist/internal/asn1/asn1.js +280 -2
  4. package/dist/internal/asn1/der.js +378 -2
  5. package/dist/internal/asn1/oids.js +103 -2
  6. package/dist/internal/crypto/algorithm-names.js +58 -2
  7. package/dist/internal/crypto/ecdsa.js +68 -2
  8. package/dist/internal/crypto/hash.js +286 -2
  9. package/dist/internal/crypto/pbes2.js +213 -2
  10. package/dist/internal/crypto/rsa-pss.js +184 -2
  11. package/dist/internal/crypto/sig-verify.js +192 -2
  12. package/dist/internal/crypto/signing.d.ts +1 -2
  13. package/dist/internal/crypto/signing.js +123 -2
  14. package/dist/internal/crypto/webcrypto.js +14 -2
  15. package/dist/internal/shared/base64.js +16 -2
  16. package/dist/internal/shared/dn.js +84 -2
  17. package/dist/internal/shared/ip.js +164 -2
  18. package/dist/internal/verify/name-constraints-engine.js +450 -2
  19. package/dist/internal/verify/policy-engine.js +382 -2
  20. package/dist/internal/verify/verify-path.js +298 -2
  21. package/dist/internal/x509/extension-bits.js +132 -2
  22. package/dist/internal/x509/extension-registry.js +229 -2
  23. package/dist/internal/x509/name-fields.js +93 -2
  24. package/dist/keys/keys.d.ts +231 -7
  25. package/dist/keys/keys.js +1087 -5
  26. package/dist/keys.d.ts +2 -2
  27. package/dist/keys.js +2 -1
  28. package/dist/pem/pem.d.ts +2 -2
  29. package/dist/pem/pem.js +172 -6
  30. package/dist/pem.js +2 -1
  31. package/dist/pkcs/pfx.d.ts +4 -5
  32. package/dist/pkcs/pfx.js +395 -2
  33. package/dist/pkcs/pkcs12-mac.d.ts +2 -2
  34. package/dist/pkcs/pkcs12-mac.js +175 -2
  35. package/dist/pkcs/pkcs7.d.ts +1 -2
  36. package/dist/pkcs/pkcs7.js +653 -2
  37. package/dist/pkcs.d.ts +2 -2
  38. package/dist/pkcs.js +4 -1
  39. package/dist/result/result.d.ts +1 -2
  40. package/dist/result/result.js +111 -2
  41. package/dist/result.js +2 -1
  42. package/dist/revocation/chain.d.ts +4 -5
  43. package/dist/revocation/chain.js +536 -2
  44. package/dist/revocation/crl.d.ts +2 -2
  45. package/dist/revocation/crl.js +1119 -2
  46. package/dist/revocation/ocsp.d.ts +3 -2
  47. package/dist/revocation/ocsp.js +958 -2
  48. package/dist/revocation/revocation.d.ts +1 -2
  49. package/dist/revocation/revocation.js +261 -2
  50. package/dist/revocation.d.ts +2 -2
  51. package/dist/revocation.js +5 -1
  52. package/dist/verify/identity.d.ts +1 -2
  53. package/dist/verify/identity.js +270 -2
  54. package/dist/verify/name-constraints.d.ts +1 -2
  55. package/dist/verify/policy.d.ts +1 -2
  56. package/dist/verify/verify.d.ts +1 -2
  57. package/dist/verify/verify.js +764 -2
  58. package/dist/verify.js +3 -1
  59. package/dist/x509/certificate.d.ts +1 -2
  60. package/dist/x509/certificate.js +151 -2
  61. package/dist/x509/csr.d.ts +1 -2
  62. package/dist/x509/csr.js +71 -2
  63. package/dist/x509/extensions.d.ts +1 -2
  64. package/dist/x509/extensions.js +387 -2
  65. package/dist/x509/name.d.ts +1 -2
  66. package/dist/x509/name.js +173 -2
  67. package/dist/x509/parse.d.ts +84 -2
  68. package/dist/x509/parse.js +1090 -2
  69. package/dist/x509.d.ts +2 -2
  70. package/dist/x509.js +6 -1
  71. package/package.json +74 -34
  72. package/dist/internal/asn1/asn1.js.map +0 -1
  73. package/dist/internal/asn1/der.js.map +0 -1
  74. package/dist/internal/asn1/oids.js.map +0 -1
  75. package/dist/internal/crypto/algorithm-names.js.map +0 -1
  76. package/dist/internal/crypto/ecdsa.js.map +0 -1
  77. package/dist/internal/crypto/hash.js.map +0 -1
  78. package/dist/internal/crypto/pbes2.d.ts +0 -23
  79. package/dist/internal/crypto/pbes2.js.map +0 -1
  80. package/dist/internal/crypto/rsa-pss.js.map +0 -1
  81. package/dist/internal/crypto/sig-verify.js.map +0 -1
  82. package/dist/internal/crypto/signing.js.map +0 -1
  83. package/dist/internal/crypto/webcrypto.js.map +0 -1
  84. package/dist/internal/shared/base64.js.map +0 -1
  85. package/dist/internal/shared/dn.js.map +0 -1
  86. package/dist/internal/shared/ip.js.map +0 -1
  87. package/dist/internal/verify/name-constraints-engine.js.map +0 -1
  88. package/dist/internal/verify/policy-engine.js.map +0 -1
  89. package/dist/internal/verify/verify-path.js.map +0 -1
  90. package/dist/internal/x509/extension-bits.js.map +0 -1
  91. package/dist/internal/x509/extension-registry.js.map +0 -1
  92. package/dist/internal/x509/name-fields.js.map +0 -1
  93. package/dist/keys/keys.js.map +0 -1
  94. package/dist/pem/pem.js.map +0 -1
  95. package/dist/pkcs/pfx.js.map +0 -1
  96. package/dist/pkcs/pkcs12-mac.js.map +0 -1
  97. package/dist/pkcs/pkcs7.js.map +0 -1
  98. package/dist/result/result.js.map +0 -1
  99. package/dist/revocation/chain.js.map +0 -1
  100. package/dist/revocation/crl.js.map +0 -1
  101. package/dist/revocation/ocsp.js.map +0 -1
  102. package/dist/revocation/revocation.js.map +0 -1
  103. package/dist/verify/identity.js.map +0 -1
  104. package/dist/verify/verify.js.map +0 -1
  105. package/dist/x509/certificate.js.map +0 -1
  106. package/dist/x509/csr.js.map +0 -1
  107. package/dist/x509/extensions.js.map +0 -1
  108. package/dist/x509/name.js.map +0 -1
  109. package/dist/x509/parse.js.map +0 -1
package/dist/verify.js CHANGED
@@ -1 +1,3 @@
1
- import{matchCertificateServiceIdentity as e,matchServiceIdentity as t}from"./verify/identity.js";import{VERIFY_ERROR_CODES as n,buildCandidatePath as r,checkExtendedKeyUsage as i,trustAnchorFromCertificate as a,validateCandidatePath as o,validateForCa as s,validateForCodeSigning as c,validateForTlsClient as l,validateForTlsServer as u,verifyCertificateChain as d,verifyCertificateSigningRequest as f}from"./verify/verify.js";export{n as VERIFY_ERROR_CODES,r as buildCandidatePath,i as checkExtendedKeyUsage,e as matchCertificateServiceIdentity,t as matchServiceIdentity,a as trustAnchorFromCertificate,o as validateCandidatePath,s as validateForCa,c as validateForCodeSigning,l as validateForTlsClient,u as validateForTlsServer,d as verifyCertificateChain,f as verifyCertificateSigningRequest};
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 };
@@ -1,2 +1,151 @@
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
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 };
@@ -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 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
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 };