micro509 0.18.0 → 0.19.1

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 (113) hide show
  1. package/CHANGELOG.md +109 -1
  2. package/dist/crypto/crypto.d.ts +11 -2
  3. package/dist/crypto/crypto.js +30 -17
  4. package/dist/crypto/crypto.js.map +1 -1
  5. package/dist/crypto.d.ts +2 -2
  6. package/dist/der/der.js.map +1 -1
  7. package/dist/index.d.ts +7 -6
  8. package/dist/internal/asn1/asn1.js +10 -2
  9. package/dist/internal/asn1/asn1.js.map +1 -1
  10. package/dist/internal/asn1/ber.js.map +1 -1
  11. package/dist/internal/asn1/decode-refusal.js.map +1 -1
  12. package/dist/internal/asn1/der.js.map +1 -1
  13. package/dist/internal/asn1/oids.js.map +1 -1
  14. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  15. package/dist/internal/crypto/ecdsa.js.map +1 -1
  16. package/dist/internal/crypto/hash.js +64 -64
  17. package/dist/internal/crypto/hash.js.map +1 -1
  18. package/dist/internal/crypto/pbes2.js +3 -3
  19. package/dist/internal/crypto/pbes2.js.map +1 -1
  20. package/dist/internal/crypto/prime-ec.js.map +1 -1
  21. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  22. package/dist/internal/crypto/sig-verify.d.ts +40 -0
  23. package/dist/internal/crypto/sig-verify.js +152 -65
  24. package/dist/internal/crypto/sig-verify.js.map +1 -1
  25. package/dist/internal/crypto/signing.js.map +1 -1
  26. package/dist/internal/jose/base64url.js.map +1 -1
  27. package/dist/internal/jose/json-data.js.map +1 -1
  28. package/dist/internal/jose/strict-json.js.map +1 -1
  29. package/dist/internal/shared/base64.js.map +1 -1
  30. package/dist/internal/shared/dn.js +1 -1
  31. package/dist/internal/shared/dn.js.map +1 -1
  32. package/dist/internal/shared/idna-tables.js +3 -3
  33. package/dist/internal/shared/idna.js.map +1 -1
  34. package/dist/internal/shared/ip.js.map +1 -1
  35. package/dist/internal/shared/mailbox.js.map +1 -1
  36. package/dist/internal/shared/punycode.js +1 -1
  37. package/dist/internal/shared/punycode.js.map +1 -1
  38. package/dist/internal/shared/uri-host.js.map +1 -1
  39. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  40. package/dist/internal/verify/policy-engine.js.map +1 -1
  41. package/dist/internal/verify/spki-distrust.js.map +1 -1
  42. package/dist/internal/verify/verify-path.js +222 -116
  43. package/dist/internal/verify/verify-path.js.map +1 -1
  44. package/dist/internal/x509/directory-name.js.map +1 -1
  45. package/dist/internal/x509/extension-bits.js.map +1 -1
  46. package/dist/internal/x509/extension-errors.js.map +1 -1
  47. package/dist/internal/x509/extension-registry.js.map +1 -1
  48. package/dist/internal/x509/general-name-profile.js.map +1 -1
  49. package/dist/internal/x509/general-name.js.map +1 -1
  50. package/dist/internal/x509/name-fields.js.map +1 -1
  51. package/dist/internal/x509/or-address.js.map +1 -1
  52. package/dist/jose/base64url.js.map +1 -1
  53. package/dist/jose/errors.js.map +1 -1
  54. package/dist/jose/jwe.js.map +1 -1
  55. package/dist/jose/jwks.js.map +1 -1
  56. package/dist/jose/jws.js.map +1 -1
  57. package/dist/jose/jwt.js.map +1 -1
  58. package/dist/jose/remote-jwks.js.map +1 -1
  59. package/dist/kdf/argon2-arithmetic.js.map +1 -1
  60. package/dist/kdf/argon2-core.js.map +1 -1
  61. package/dist/kdf/argon2-memory.js.map +1 -1
  62. package/dist/kdf/argon2-portable.js +1 -1
  63. package/dist/kdf/argon2-portable.js.map +1 -1
  64. package/dist/kdf/argon2.js +2 -2
  65. package/dist/kdf/argon2.js.map +1 -1
  66. package/dist/keys/keys.d.ts +7 -2
  67. package/dist/keys/keys.js +10 -6
  68. package/dist/keys/keys.js.map +1 -1
  69. package/dist/pem/pem.d.ts +20 -7
  70. package/dist/pem/pem.js +24 -20
  71. package/dist/pem/pem.js.map +1 -1
  72. package/dist/pem.d.ts +2 -2
  73. package/dist/pkcs/pfx.js.map +1 -1
  74. package/dist/pkcs/pkcs12-mac.js +3 -3
  75. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  76. package/dist/pkcs/pkcs7.d.ts +10 -0
  77. package/dist/pkcs/pkcs7.js +106 -88
  78. package/dist/pkcs/pkcs7.js.map +1 -1
  79. package/dist/result/result.js.map +1 -1
  80. package/dist/revocation/chain.d.ts +67 -13
  81. package/dist/revocation/chain.js +429 -301
  82. package/dist/revocation/chain.js.map +1 -1
  83. package/dist/revocation/crl.d.ts +65 -11
  84. package/dist/revocation/crl.js +472 -182
  85. package/dist/revocation/crl.js.map +1 -1
  86. package/dist/revocation/ocsp.d.ts +79 -24
  87. package/dist/revocation/ocsp.js +417 -231
  88. package/dist/revocation/ocsp.js.map +1 -1
  89. package/dist/revocation/revocation.d.ts +68 -13
  90. package/dist/revocation/revocation.js +194 -73
  91. package/dist/revocation/revocation.js.map +1 -1
  92. package/dist/revocation.d.ts +4 -4
  93. package/dist/ssh/ssh.js.map +1 -1
  94. package/dist/ssh/wire.js.map +1 -1
  95. package/dist/verify/identity.d.ts +2 -2
  96. package/dist/verify/identity.js +2 -2
  97. package/dist/verify/identity.js.map +1 -1
  98. package/dist/verify/verify.d.ts +84 -32
  99. package/dist/verify/verify.js +221 -175
  100. package/dist/verify/verify.js.map +1 -1
  101. package/dist/verify.d.ts +3 -2
  102. package/dist/x509/certificate.js.map +1 -1
  103. package/dist/x509/csr.js.map +1 -1
  104. package/dist/x509/extensions.js +1 -1
  105. package/dist/x509/extensions.js.map +1 -1
  106. package/dist/x509/fingerprint.js.map +1 -1
  107. package/dist/x509/name-text.js +1 -1
  108. package/dist/x509/name-text.js.map +1 -1
  109. package/dist/x509/name.js.map +1 -1
  110. package/dist/x509/parse.d.ts +6 -2
  111. package/dist/x509/parse.js +121 -121
  112. package/dist/x509/parse.js.map +1 -1
  113. package/package.json +16 -16
@@ -1 +1 @@
1
- {"version":3,"file":"fingerprint.js","names":["parseCertificateFromSource","certificate","digest","getCrypto","algorithm","toArrayBuffer","der","bytes","toHex","source","parsed","value"],"sources":["../../src/x509/fingerprint.ts"],"sourcesContent":["/**\n * Certificate fingerprints: a cryptographic hash over the DER encoding.\n *\n * This is the identifier that `openssl x509 -fingerprint` and every TLS UI show.\n * The helper accepts the same PEM / DER / parsed-certificate source union as\n * the verification APIs and returns the digest in the forms callers actually\n * paste around: raw bytes, lowercase hex, and uppercase colon-separated hex.\n *\n * @module\n */\n\nimport { toArrayBuffer, toHex } from '#micro509/internal/asn1/asn1';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\nimport type { ParsedCertificate } from '#micro509/x509/parse';\nimport { parseCertificateFromSource } from '#micro509/x509/parse';\n\n/**\n * A PEM string, raw DER bytes, or an already-parsed certificate.\n *\n * Mirrors the source union accepted by the verification, revocation, and\n * PKCS APIs so a fingerprint can be taken from whatever a caller already holds.\n */\nexport type CertificateFingerprintSource = string | Uint8Array | ParsedCertificate;\n\n/**\n * Digest algorithms supported by {@linkcode certificateFingerprint}.\n *\n * SHA-1 is intentionally included: legacy ecosystems (PGP-adjacent tooling,\n * older certificate pinning) still identify certificates by their SHA-1\n * fingerprint. Prefer SHA-256 for anything new.\n */\nexport type CertificateFingerprintAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';\n\n/** The three rendered forms of a certificate fingerprint. */\nexport interface CertificateFingerprint {\n\t/** Raw digest bytes. */\n\treadonly bytes: Uint8Array;\n\t/** Lowercase hex, no separators (e.g. `\"a1b2c3…\"`). */\n\treadonly hex: string;\n\t/** Uppercase hex, colon-separated (e.g. `\"A1:B2:C3:…\"`, `openssl x509 -fingerprint` style). */\n\treadonly colonHex: string;\n}\n\n/**\n * Compute a certificate fingerprint — a hash over the DER encoding.\n *\n * The certificate is parsed (validating the input and, for PEM, decoding it to\n * DER) before hashing, so the digest is always taken over the canonical DER of\n * a well-formed certificate. Malformed input throws, matching the other DER/PEM\n * boundaries in the library.\n *\n * **Asynchronous:** hashing uses WebCrypto's `crypto.subtle.digest`. Await the\n * returned promise before reading `bytes`, `hex`, or `colonHex`.\n *\n * @example\n * ```ts\n * const fingerprint = await certificateFingerprint(pemString);\n * console.log(fingerprint.colonHex); // \"AB:CD:…\" — matches `openssl x509 -fingerprint -sha256`\n * ```\n *\n * @example\n * ```ts\n * // Reuse an already-parsed certificate and request SHA-1.\n * const parsed = parseCertificatePemOrThrow(pemString);\n * const legacy = await certificateFingerprint(parsed, 'SHA-1');\n * console.log(legacy.hex);\n * ```\n *\n * @param certificate PEM string, DER bytes, or a {@linkcode ParsedCertificate}.\n * @param algorithm Digest algorithm to use. Defaults to `'SHA-256'`.\n */\nexport async function certificateFingerprint(\n\tcertificate: CertificateFingerprintSource,\n\talgorithm: CertificateFingerprintAlgorithm = 'SHA-256',\n): Promise<CertificateFingerprint> {\n\tconst { der } = parseCertificateFromSource(certificate);\n\tconst digest = await getCrypto().subtle.digest(algorithm, toArrayBuffer(der));\n\tconst bytes = new Uint8Array(digest);\n\treturn {\n\t\tbytes,\n\t\thex: toHex(bytes),\n\t\tcolonHex: toColonHex(bytes),\n\t};\n}\n\n/**\n * Compute the SHA-256 fingerprint of a certificate's complete DER\n * SubjectPublicKeyInfo. Unlike {@linkcode certificateFingerprint}, this\n * identifies the public key rather than the certificate. Cross-signed\n * certificates carrying the same SPKI therefore have the same fingerprint.\n *\n * The SPKI is read from the reparsed certificate DER. No cryptographic key\n * import is performed, so this works for algorithms micro509 cannot use with\n * WebCrypto.\n */\nexport async function subjectPublicKeyInfoFingerprint(\n\tcertificate: CertificateFingerprintSource,\n): Promise<CertificateFingerprint> {\n\tconst source =\n\t\ttypeof certificate === 'string' || certificate instanceof Uint8Array\n\t\t\t? certificate\n\t\t\t: certificate.der;\n\tconst parsed = parseCertificateFromSource(source);\n\tconst digest = await getCrypto().subtle.digest(\n\t\t'SHA-256',\n\t\ttoArrayBuffer(parsed.subjectPublicKeyInfoDer),\n\t);\n\tconst bytes = new Uint8Array(digest);\n\treturn { bytes, hex: toHex(bytes), colonHex: toColonHex(bytes) };\n}\n\n/** Renders bytes as uppercase, colon-separated hex (openssl fingerprint style). */\nfunction toColonHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes, (value) => value.toString(16).padStart(2, '0').toUpperCase()).join(':');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,eAAsB,uBACrB,GACA,IAA6C,WACX;CAClC,IAAM,EAAE,WAAQA,EAA2BC,CAAW,GAChDC,IAAS,MAAMC,EAAU,CAAC,CAAC,OAAO,OAAOC,GAAWC,EAAcC,CAAG,CAAC,GACtEC,IAAQ,IAAI,WAAWL,CAAM;CACnC,OAAO;EACN;EACA,KAAKM,EAAMD,CAAK;EAChB,UAAU,WAAWA,CAAK;CAC3B;AACD;;;;;;;;;;;AAYA,eAAsB,gCACrB,GACkC;CAClC,IAAME,IACL,OAAOR,KAAgB,YAAYA,aAAuB,aACvDA,IACAA,EAAY,KACVS,IAASV,EAA2BS,CAAM,GAC1CP,IAAS,MAAMC,EAAU,CAAC,CAAC,OAAO,OACvC,WACAE,EAAcK,EAAO,uBAAuB,CAC7C,GACMH,IAAQ,IAAI,WAAWL,CAAM;CACnC,OAAO;EAAE;EAAO,KAAKM,EAAMD,CAAK;EAAG,UAAU,WAAWA,CAAK;CAAE;AAChE;;AAGA,SAAS,WAAW,GAA2B;CAC9C,OAAO,MAAM,KAAKA,IAAQ,MAAUI,EAAM,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,GAAG;AAChG"}
1
+ {"version":3,"file":"fingerprint.js","names":["toArrayBuffer","toHex","getCrypto","parseCertificateFromSource","certificate","algorithm","der","digest","bytes","source","parsed","value"],"sources":["../../src/x509/fingerprint.ts"],"sourcesContent":["/**\n * Certificate fingerprints: a cryptographic hash over the DER encoding.\n *\n * This is the identifier that `openssl x509 -fingerprint` and every TLS UI show.\n * The helper accepts the same PEM / DER / parsed-certificate source union as\n * the verification APIs and returns the digest in the forms callers actually\n * paste around: raw bytes, lowercase hex, and uppercase colon-separated hex.\n *\n * @module\n */\n\nimport { toArrayBuffer, toHex } from '#micro509/internal/asn1/asn1';\nimport { getCrypto } from '#micro509/internal/crypto/webcrypto';\nimport type { ParsedCertificate } from '#micro509/x509/parse';\nimport { parseCertificateFromSource } from '#micro509/x509/parse';\n\n/**\n * A PEM string, raw DER bytes, or an already-parsed certificate.\n *\n * Mirrors the source union accepted by the verification, revocation, and\n * PKCS APIs so a fingerprint can be taken from whatever a caller already holds.\n */\nexport type CertificateFingerprintSource = string | Uint8Array | ParsedCertificate;\n\n/**\n * Digest algorithms supported by {@linkcode certificateFingerprint}.\n *\n * SHA-1 is intentionally included: legacy ecosystems (PGP-adjacent tooling,\n * older certificate pinning) still identify certificates by their SHA-1\n * fingerprint. Prefer SHA-256 for anything new.\n */\nexport type CertificateFingerprintAlgorithm = 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';\n\n/** The three rendered forms of a certificate fingerprint. */\nexport interface CertificateFingerprint {\n\t/** Raw digest bytes. */\n\treadonly bytes: Uint8Array;\n\t/** Lowercase hex, no separators (e.g. `\"a1b2c3…\"`). */\n\treadonly hex: string;\n\t/** Uppercase hex, colon-separated (e.g. `\"A1:B2:C3:…\"`, `openssl x509 -fingerprint` style). */\n\treadonly colonHex: string;\n}\n\n/**\n * Compute a certificate fingerprint — a hash over the DER encoding.\n *\n * The certificate is parsed (validating the input and, for PEM, decoding it to\n * DER) before hashing, so the digest is always taken over the canonical DER of\n * a well-formed certificate. Malformed input throws, matching the other DER/PEM\n * boundaries in the library.\n *\n * **Asynchronous:** hashing uses WebCrypto's `crypto.subtle.digest`. Await the\n * returned promise before reading `bytes`, `hex`, or `colonHex`.\n *\n * @example\n * ```ts\n * const fingerprint = await certificateFingerprint(pemString);\n * console.log(fingerprint.colonHex); // \"AB:CD:…\" — matches `openssl x509 -fingerprint -sha256`\n * ```\n *\n * @example\n * ```ts\n * // Reuse an already-parsed certificate and request SHA-1.\n * const parsed = parseCertificatePemOrThrow(pemString);\n * const legacy = await certificateFingerprint(parsed, 'SHA-1');\n * console.log(legacy.hex);\n * ```\n *\n * @param certificate PEM string, DER bytes, or a {@linkcode ParsedCertificate}.\n * @param algorithm Digest algorithm to use. Defaults to `'SHA-256'`.\n */\nexport async function certificateFingerprint(\n\tcertificate: CertificateFingerprintSource,\n\talgorithm: CertificateFingerprintAlgorithm = 'SHA-256',\n): Promise<CertificateFingerprint> {\n\tconst { der } = parseCertificateFromSource(certificate);\n\tconst digest = await getCrypto().subtle.digest(algorithm, toArrayBuffer(der));\n\tconst bytes = new Uint8Array(digest);\n\treturn {\n\t\tbytes,\n\t\thex: toHex(bytes),\n\t\tcolonHex: toColonHex(bytes),\n\t};\n}\n\n/**\n * Compute the SHA-256 fingerprint of a certificate's complete DER\n * SubjectPublicKeyInfo. Unlike {@linkcode certificateFingerprint}, this\n * identifies the public key rather than the certificate. Cross-signed\n * certificates carrying the same SPKI therefore have the same fingerprint.\n *\n * The SPKI is read from the reparsed certificate DER. No cryptographic key\n * import is performed, so this works for algorithms micro509 cannot use with\n * WebCrypto.\n */\nexport async function subjectPublicKeyInfoFingerprint(\n\tcertificate: CertificateFingerprintSource,\n): Promise<CertificateFingerprint> {\n\tconst source =\n\t\ttypeof certificate === 'string' || certificate instanceof Uint8Array\n\t\t\t? certificate\n\t\t\t: certificate.der;\n\tconst parsed = parseCertificateFromSource(source);\n\tconst digest = await getCrypto().subtle.digest(\n\t\t'SHA-256',\n\t\ttoArrayBuffer(parsed.subjectPublicKeyInfoDer),\n\t);\n\tconst bytes = new Uint8Array(digest);\n\treturn { bytes, hex: toHex(bytes), colonHex: toColonHex(bytes) };\n}\n\n/** Renders bytes as uppercase, colon-separated hex (openssl fingerprint style). */\nfunction toColonHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes, (value) => value.toString(16).padStart(2, '0').toUpperCase()).join(':');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,eAAsB,uBACrBI,GACAC,IAA6C,WACX;CAClC,IAAM,EAAEC,WAAQH,EAA2BC,CAAW,GAChDG,IAAS,MAAML,EAAU,CAAC,CAAC,OAAO,OAAOG,GAAWL,EAAcM,CAAG,CAAC,GACtEE,IAAQ,IAAI,WAAWD,CAAM;CACnC,OAAO;EACNC;EACA,KAAKP,EAAMO,CAAK;EAChB,UAAU,WAAWA,CAAK;CAC3B;AACD;;;;;;;;;;;AAYA,eAAsB,gCACrBJ,GACkC;CAClC,IAAMK,IACL,OAAOL,KAAgB,YAAYA,aAAuB,aACvDA,IACAA,EAAY,KACVM,IAASP,EAA2BM,CAAM,GAC1CF,IAAS,MAAML,EAAU,CAAC,CAAC,OAAO,OACvC,WACAF,EAAcU,EAAO,uBAAuB,CAC7C,GACMF,IAAQ,IAAI,WAAWD,CAAM;CACnC,OAAO;EAAEC;EAAO,KAAKP,EAAMO,CAAK;EAAG,UAAU,WAAWA,CAAK;CAAE;AAChE;;AAGA,SAAS,WAAWA,GAA2B;CAC9C,OAAO,MAAM,KAAKA,IAAQG,MAAUA,EAAM,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,GAAG;AAChG"}
@@ -110,7 +110,7 @@ function subjectAltNameText(e) {
110
110
  case "directoryName": return directoryNameText(e.derHex);
111
111
  case "otherName": return `${e.typeId}:${t(e.value)}`;
112
112
  case "x400Address":
113
- case "ediPartyName": return t(e.value);
113
+ case "ediPartyName":
114
114
  case "unknown": return t(e.value);
115
115
  default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
116
116
  }
@@ -1 +1 @@
1
- {"version":3,"file":"name-text.js","names":["_exhaustive","text","name","options","rdn","attribute","ESCAPED_CHARACTERS","DN_ATTRIBUTE_KEYWORDS","characters","value","character","code","index","toHex","parseDistinguishedNameDer","hexToBytes","derHex"],"sources":["../../src/x509/name-text.ts"],"sourcesContent":["/**\n * Human-readable text for parsed names.\n *\n * Renders a {@linkcode ParsedName} or {@linkcode SubjectAltName} as a display string,\n * covering every variant of the union without the caller narrowing it by hand.\n *\n * @module micro509/x509/name-text\n */\n\nimport { hexToBytes, toHex } from '#micro509/internal/asn1/asn1';\nimport { DN_ATTRIBUTE_KEYWORDS } from '#micro509/internal/x509/name-fields';\nimport type { SubjectAltName } from '#micro509/x509/extensions';\nimport type {\n\tParsedName,\n\tParsedNameAttribute,\n\tParsedRelativeDistinguishedName,\n} from '#micro509/x509/parse';\nimport { parseDistinguishedNameDer } from '#micro509/x509/parse';\n\n/** Options for {@linkcode subjectAltNameToString}. */\nexport interface SubjectAltNameTextOptions {\n\t/** Prepend `{@linkcode subjectAltNameLabel}:` to the value, as `openssl x509 -text` does. Defaults to `false`. */\n\treadonly prefix?: boolean;\n}\n\n/**\n * Renders one {@linkcode SubjectAltName} as text.\n *\n * | variant | rendering |\n * | --- | --- |\n * | `dns`, `ip`, `email`, `uri`, `srv` | the value itself |\n * | `directoryName` | {@linkcode distinguishedNameToString} of the embedded name, or the DER hex if it does not decode |\n * | `unknown` | lowercase hex of the raw content bytes |\n *\n * @example Render every SAN of a parsed certificate\n * ```ts\n * const names = (parsed.subjectAltNames ?? []).map((name) => subjectAltNameToString(name));\n * // ['example.com', '192.0.2.1', 'CN=Example CA,C=US']\n *\n * subjectAltNameToString({ type: 'dns', value: 'example.com' }, { prefix: true }); // 'DNS:example.com'\n * ```\n */\nexport function subjectAltNameToString(\n\tname: SubjectAltName,\n\toptions?: SubjectAltNameTextOptions,\n): string {\n\tconst text = subjectAltNameText(name);\n\treturn options?.prefix === true ? `${subjectAltNameLabel(name)}:${text}` : text;\n}\n\n/**\n * The `openssl x509 -text` label for a {@linkcode SubjectAltName} variant —\n * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `SmtpUTF8Mailbox`, `UPN`, `KRB5PrincipalName`, `DirName`, `othername`, `X400Name`,\n * `EdiPartyName`, `Registered ID`, or `[tag <n>]` for raw `unknown` input.\n */\nexport function subjectAltNameLabel(name: SubjectAltName): string {\n\tswitch (name.type) {\n\t\tcase 'dns':\n\t\t\treturn 'DNS';\n\t\tcase 'ip':\n\t\t\treturn 'IP Address';\n\t\tcase 'email':\n\t\t\treturn 'email';\n\t\tcase 'uri':\n\t\t\treturn 'URI';\n\t\tcase 'srv':\n\t\t\treturn 'SRV';\n\t\tcase 'smtpUtf8Mailbox':\n\t\t\treturn 'SmtpUTF8Mailbox';\n\t\tcase 'upn':\n\t\t\treturn 'UPN';\n\t\tcase 'krb5PrincipalName':\n\t\t\treturn 'KRB5PrincipalName';\n\t\tcase 'directoryName':\n\t\t\treturn 'DirName';\n\t\tcase 'otherName':\n\t\t\treturn 'othername';\n\t\tcase 'x400Address':\n\t\t\treturn 'X400Name';\n\t\tcase 'ediPartyName':\n\t\t\treturn 'EdiPartyName';\n\t\tcase 'registeredID':\n\t\t\treturn 'Registered ID';\n\t\tcase 'unknown':\n\t\t\treturn `[tag ${String(name.tag)}]`;\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = name;\n\t\t\tthrow new Error(`Unhandled SubjectAltName type: ${String(_exhaustive)}`);\n\t\t}\n\t}\n}\n\n/**\n * Renders a {@linkcode ParsedName} as an RFC 4514 distinguished name string, e.g. `CN=Example CA,O=Acme,C=US`.\n *\n * RDNs are emitted in reverse encoding order and multi-valued RDNs are joined with `+`,\n * both per RFC 4514 [§2](https://datatracker.ietf.org/doc/html/rfc4514#section-2).\n * Attribute types outside the RFC 4514 [§3](https://datatracker.ietf.org/doc/html/rfc4514#section-3)\n * keyword table use the short names `openssl` prints, falling back to the dotted OID.\n *\n * @example\n * ```ts\n * distinguishedNameToString(parsed.subject); // 'CN=example.com,O=Acme\\\\, Inc.,C=US'\n * ```\n */\nexport function distinguishedNameToString(name: ParsedName): string {\n\treturn [...name.rdns]\n\t\t.reverse()\n\t\t.map((rdn) => relativeDistinguishedNameToString(rdn))\n\t\t.join(',');\n}\n\n/** Renders one {@linkcode ParsedRelativeDistinguishedName}, joining a multi-valued RDN's attributes with `+`. */\nexport function relativeDistinguishedNameToString(rdn: ParsedRelativeDistinguishedName): string {\n\treturn rdn.attributes.map((attribute) => attributeToString(attribute)).join('+');\n}\n\n/** Characters RFC 4514 [§2.4](https://datatracker.ietf.org/doc/html/rfc4514#section-2.4) escapes wherever they appear. */\nconst ESCAPED_CHARACTERS = new Set(['\"', '+', ',', ';', '<', '>', '\\\\']);\n\nfunction attributeToString(attribute: ParsedNameAttribute): string {\n\tconst keyword = DN_ATTRIBUTE_KEYWORDS[attribute.oid] ?? attribute.oid;\n\treturn `${keyword}=${escapeAttributeValue(attribute.value)}`;\n}\n\nfunction escapeAttributeValue(value: string): string {\n\tconst characters = [...value];\n\treturn characters\n\t\t.map((character, index) => {\n\t\t\tif (ESCAPED_CHARACTERS.has(character)) {\n\t\t\t\treturn `\\\\${character}`;\n\t\t\t}\n\t\t\tconst code = character.codePointAt(0) ?? 0;\n\t\t\tif (code < 0x20 || code === 0x7f) {\n\t\t\t\treturn `\\\\${code.toString(16).padStart(2, '0')}`;\n\t\t\t}\n\t\t\tif (character === ' ' && (index === 0 || index === characters.length - 1)) {\n\t\t\t\treturn '\\\\ ';\n\t\t\t}\n\t\t\treturn character === '#' && index === 0 ? '\\\\#' : character;\n\t\t})\n\t\t.join('');\n}\n\nfunction subjectAltNameText(name: SubjectAltName): string {\n\tswitch (name.type) {\n\t\tcase 'dns':\n\t\tcase 'ip':\n\t\tcase 'email':\n\t\tcase 'uri':\n\t\tcase 'srv':\n\t\tcase 'smtpUtf8Mailbox':\n\t\tcase 'upn':\n\t\tcase 'registeredID':\n\t\t\treturn name.value;\n\t\tcase 'krb5PrincipalName':\n\t\t\treturn `${name.nameString.join('/')}@${name.realm}`;\n\t\tcase 'directoryName':\n\t\t\treturn directoryNameText(name.derHex);\n\t\tcase 'otherName':\n\t\t\treturn `${name.typeId}:${toHex(name.value)}`;\n\t\tcase 'x400Address':\n\t\tcase 'ediPartyName':\n\t\t\treturn toHex(name.value);\n\t\tcase 'unknown':\n\t\t\treturn toHex(name.value);\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = name;\n\t\t\tthrow new Error(`Unhandled SubjectAltName type: ${String(_exhaustive)}`);\n\t\t}\n\t}\n}\n\nfunction directoryNameText(derHex: string): string {\n\ttry {\n\t\treturn distinguishedNameToString(parseDistinguishedNameDer(hexToBytes(derHex)));\n\t} catch {\n\t\treturn derHex;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,uBACf,GACA,GACS;CACT,IAAMC,IAAO,mBAAmBC,CAAI;CACpC,OAAOC,GAAS,WAAW,KAAO,GAAG,oBAAoBD,CAAI,EAAE,GAAGD,MAASA;AAC5E;;;;;;AAOA,SAAgB,oBAAoB,GAA8B;CACjE,QAAQC,EAAK,MAAb;EACC,KAAK,OACJ,OAAO;EACR,KAAK,MACJ,OAAO;EACR,KAAK,SACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,mBACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,qBACJ,OAAO;EACR,KAAK,iBACJ,OAAO;EACR,KAAK,aACJ,OAAO;EACR,KAAK,eACJ,OAAO;EACR,KAAK,gBACJ,OAAO;EACR,KAAK,gBACJ,OAAO;EACR,KAAK,WACJ,OAAO,QAAQ,OAAOA,EAAK,GAAG,EAAE;EACjC,SAEC,MAAU,MAAM,kCAAkC,OAAOF,CAAW,GAAG;CAEzE;AACD;;;;;;;;;;;;;;AAeA,SAAgB,0BAA0B,GAA0B;CACnE,OAAO,CAAC,GAAGE,EAAK,IAAI,CAAC,CACnB,QAAQ,CAAC,CACT,KAAK,MAAQ,kCAAkCE,CAAG,CAAC,CAAC,CACpD,KAAK,GAAG;AACX;;AAGA,SAAgB,kCAAkC,GAA8C;CAC/F,OAAOA,EAAI,WAAW,KAAK,MAAc,kBAAkBC,CAAS,CAAC,CAAC,CAAC,KAAK,G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EAA0BC,EAAWC,CAAM,CAAC,CAAC;CAC/E,QAAQ;EACP,OAAOA;CACR;AACD"}
1
+ {"version":3,"file":"name-text.js","names":["_exhaustive","hexToBytes","toHex","DN_ATTRIBUTE_KEYWORDS","parseDistinguishedNameDer","name","options","text","rdn","attribute","ESCAPED_CHARACTERS","value","characters","character","index","code","derHex"],"sources":["../../src/x509/name-text.ts"],"sourcesContent":["/**\n * Human-readable text for parsed names.\n *\n * Renders a {@linkcode ParsedName} or {@linkcode SubjectAltName} as a display string,\n * covering every variant of the union without the caller narrowing it by hand.\n *\n * @module micro509/x509/name-text\n */\n\nimport { hexToBytes, toHex } from '#micro509/internal/asn1/asn1';\nimport { DN_ATTRIBUTE_KEYWORDS } from '#micro509/internal/x509/name-fields';\nimport type { SubjectAltName } from '#micro509/x509/extensions';\nimport type {\n\tParsedName,\n\tParsedNameAttribute,\n\tParsedRelativeDistinguishedName,\n} from '#micro509/x509/parse';\nimport { parseDistinguishedNameDer } from '#micro509/x509/parse';\n\n/** Options for {@linkcode subjectAltNameToString}. */\nexport interface SubjectAltNameTextOptions {\n\t/** Prepend `{@linkcode subjectAltNameLabel}:` to the value, as `openssl x509 -text` does. Defaults to `false`. */\n\treadonly prefix?: boolean;\n}\n\n/**\n * Renders one {@linkcode SubjectAltName} as text.\n *\n * | variant | rendering |\n * | --- | --- |\n * | `dns`, `ip`, `email`, `uri`, `srv` | the value itself |\n * | `directoryName` | {@linkcode distinguishedNameToString} of the embedded name, or the DER hex if it does not decode |\n * | `unknown` | lowercase hex of the raw content bytes |\n *\n * @example Render every SAN of a parsed certificate\n * ```ts\n * const names = (parsed.subjectAltNames ?? []).map((name) => subjectAltNameToString(name));\n * // ['example.com', '192.0.2.1', 'CN=Example CA,C=US']\n *\n * subjectAltNameToString({ type: 'dns', value: 'example.com' }, { prefix: true }); // 'DNS:example.com'\n * ```\n */\nexport function subjectAltNameToString(\n\tname: SubjectAltName,\n\toptions?: SubjectAltNameTextOptions,\n): string {\n\tconst text = subjectAltNameText(name);\n\treturn options?.prefix === true ? `${subjectAltNameLabel(name)}:${text}` : text;\n}\n\n/**\n * The `openssl x509 -text` label for a {@linkcode SubjectAltName} variant —\n * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `SmtpUTF8Mailbox`, `UPN`, `KRB5PrincipalName`, `DirName`, `othername`, `X400Name`,\n * `EdiPartyName`, `Registered ID`, or `[tag <n>]` for raw `unknown` input.\n */\nexport function subjectAltNameLabel(name: SubjectAltName): string {\n\tswitch (name.type) {\n\t\tcase 'dns':\n\t\t\treturn 'DNS';\n\t\tcase 'ip':\n\t\t\treturn 'IP Address';\n\t\tcase 'email':\n\t\t\treturn 'email';\n\t\tcase 'uri':\n\t\t\treturn 'URI';\n\t\tcase 'srv':\n\t\t\treturn 'SRV';\n\t\tcase 'smtpUtf8Mailbox':\n\t\t\treturn 'SmtpUTF8Mailbox';\n\t\tcase 'upn':\n\t\t\treturn 'UPN';\n\t\tcase 'krb5PrincipalName':\n\t\t\treturn 'KRB5PrincipalName';\n\t\tcase 'directoryName':\n\t\t\treturn 'DirName';\n\t\tcase 'otherName':\n\t\t\treturn 'othername';\n\t\tcase 'x400Address':\n\t\t\treturn 'X400Name';\n\t\tcase 'ediPartyName':\n\t\t\treturn 'EdiPartyName';\n\t\tcase 'registeredID':\n\t\t\treturn 'Registered ID';\n\t\tcase 'unknown':\n\t\t\treturn `[tag ${String(name.tag)}]`;\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = name;\n\t\t\tthrow new Error(`Unhandled SubjectAltName type: ${String(_exhaustive)}`);\n\t\t}\n\t}\n}\n\n/**\n * Renders a {@linkcode ParsedName} as an RFC 4514 distinguished name string, e.g. `CN=Example CA,O=Acme,C=US`.\n *\n * RDNs are emitted in reverse encoding order and multi-valued RDNs are joined with `+`,\n * both per RFC 4514 [§2](https://datatracker.ietf.org/doc/html/rfc4514#section-2).\n * Attribute types outside the RFC 4514 [§3](https://datatracker.ietf.org/doc/html/rfc4514#section-3)\n * keyword table use the short names `openssl` prints, falling back to the dotted OID.\n *\n * @example\n * ```ts\n * distinguishedNameToString(parsed.subject); // 'CN=example.com,O=Acme\\\\, Inc.,C=US'\n * ```\n */\nexport function distinguishedNameToString(name: ParsedName): string {\n\treturn [...name.rdns]\n\t\t.reverse()\n\t\t.map((rdn) => relativeDistinguishedNameToString(rdn))\n\t\t.join(',');\n}\n\n/** Renders one {@linkcode ParsedRelativeDistinguishedName}, joining a multi-valued RDN's attributes with `+`. */\nexport function relativeDistinguishedNameToString(rdn: ParsedRelativeDistinguishedName): string {\n\treturn rdn.attributes.map((attribute) => attributeToString(attribute)).join('+');\n}\n\n/** Characters RFC 4514 [§2.4](https://datatracker.ietf.org/doc/html/rfc4514#section-2.4) escapes wherever they appear. */\nconst ESCAPED_CHARACTERS = new Set(['\"', '+', ',', ';', '<', '>', '\\\\']);\n\nfunction attributeToString(attribute: ParsedNameAttribute): string {\n\tconst keyword = DN_ATTRIBUTE_KEYWORDS[attribute.oid] ?? attribute.oid;\n\treturn `${keyword}=${escapeAttributeValue(attribute.value)}`;\n}\n\nfunction escapeAttributeValue(value: string): string {\n\tconst characters = [...value];\n\treturn characters\n\t\t.map((character, index) => {\n\t\t\tif (ESCAPED_CHARACTERS.has(character)) {\n\t\t\t\treturn `\\\\${character}`;\n\t\t\t}\n\t\t\tconst code = character.codePointAt(0) ?? 0;\n\t\t\tif (code < 0x20 || code === 0x7f) {\n\t\t\t\treturn `\\\\${code.toString(16).padStart(2, '0')}`;\n\t\t\t}\n\t\t\tif (character === ' ' && (index === 0 || index === characters.length - 1)) {\n\t\t\t\treturn '\\\\ ';\n\t\t\t}\n\t\t\treturn character === '#' && index === 0 ? '\\\\#' : character;\n\t\t})\n\t\t.join('');\n}\n\nfunction subjectAltNameText(name: SubjectAltName): string {\n\tswitch (name.type) {\n\t\tcase 'dns':\n\t\tcase 'ip':\n\t\tcase 'email':\n\t\tcase 'uri':\n\t\tcase 'srv':\n\t\tcase 'smtpUtf8Mailbox':\n\t\tcase 'upn':\n\t\tcase 'registeredID':\n\t\t\treturn name.value;\n\t\tcase 'krb5PrincipalName':\n\t\t\treturn `${name.nameString.join('/')}@${name.realm}`;\n\t\tcase 'directoryName':\n\t\t\treturn directoryNameText(name.derHex);\n\t\tcase 'otherName':\n\t\t\treturn `${name.typeId}:${toHex(name.value)}`;\n\t\tcase 'x400Address':\n\t\tcase 'ediPartyName':\n\t\t\treturn toHex(name.value);\n\t\tcase 'unknown':\n\t\t\treturn toHex(name.value);\n\t\tdefault: {\n\t\t\tconst _exhaustive: never = name;\n\t\t\tthrow new Error(`Unhandled SubjectAltName type: ${String(_exhaustive)}`);\n\t\t}\n\t}\n}\n\nfunction directoryNameText(derHex: string): string {\n\ttry {\n\t\treturn distinguishedNameToString(parseDistinguishedNameDer(hexToBytes(derHex)));\n\t} catch {\n\t\treturn derHex;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,SAAgB,uBACfK,GACAC,GACS;CACT,IAAMC,IAAO,mBAAmBF,CAAI;CACpC,OAAOC,GAAS,WAAW,KAAO,GAAG,oBAAoBD,CAAI,EAAE,GAAGE,MAASA;AAC5E;;;;;;AAOA,SAAgB,oBAAoBF,GAA8B;CACjE,QAAQA,EAAK,MAAb;EACC,KAAK,OACJ,OAAO;EACR,KAAK,MACJ,OAAO;EACR,KAAK,SACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,mBACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,qBACJ,OAAO;EACR,KAAK,iBACJ,OAAO;EACR,KAAK,aACJ,OAAO;EACR,KAAK,eACJ,OAAO;EACR,KAAK,gBACJ,OAAO;EACR,KAAK,gBACJ,OAAO;EACR,KAAK,WACJ,OAAO,QAAQ,OAAOA,EAAK,GAAG,EAAE;EACjC,SAEC,MAAU,MAAM,kCAAkC,OAAOL,CAAW,GAAG;CAEzE;AACD;;;;;;;;;;;;;;AA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AAO,GAAGA,EAAK,OAAO,GAAGH,EAAMG,EAAK,KAAK;EAC1C,KAAK;EACL,KAAK;EAEL,KAAK,WACJ,OAAOH,EAAMG,EAAK,KAAK;EACxB,SAEC,MAAU,MAAM,kCAAkC,OAAOL,CAAW,GAAG;CAEzE;AACD;AAEA,SAAS,kBAAkBgB,GAAwB;CAClD,IAAI;EACH,OAAO,0BAA0BZ,EAA0BH,EAAWe,CAAM,CAAC,CAAC;CAC/E,QAAQ;EACP,OAAOA;CACR;AACD"}
@@ -1 +1 @@
1
- {"version":3,"file":"name.js","names":["code","message","attributes","input","sequence","setOf","attribute","definition","NAME_FIELD_DEFINITIONS","length","hasLoneSurrogate","objectIdentifier","key","NAME_OBJECT_ORDER","value","canonicalDnKey","compareDistinguishedNames","isWithinDirectoryNameSubtree","nameFieldKeyFromOid"],"sources":["../../src/x509/name.ts"],"sourcesContent":["/**\n * Distinguished-name input and DER encoding helpers.\n *\n * Encodes X.509 `Name` values used for certificate subjects, issuers,\n * and CSR subjects.\\\n * A `Name` is a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs), and each\n * RDN is a `SET OF` one or more name attributes.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * Use {@linkcode encodeName} for the common case where each attribute occupies\n * its own RDN.\\\n * Use {@linkcode encodeRelativeDistinguishedName} when you need one\n * multi-valued RDN.\n *\n * `NameObject` favors convenience: populated fields are emitted in the\n * canonical order from `NAME_OBJECT_ORDER`.\\\n * `NameAttribute` arrays favor control: caller order is preserved, but each\n * entry still becomes its own single-attribute RDN.\n *\n * Attribute OIDs and ASN.1 string encodings come from\n * `NAME_FIELD_DEFINITIONS`.\n *\n * @example\n * ```ts\n * import { encodeName } from 'micro509/x509';\n *\n * const subjectDer = encodeName({\n * \tcountry: 'US',\n * \torganization: 'ACME Inc',\n * \tcommonName: 'example.com',\n * });\n *\n * // DER for: C=US, O=ACME Inc, CN=example.com\n * ```\n *\n * @example\n * ```ts\n * import { encodeName } from 'micro509/x509';\n *\n * const subjectDer = encodeName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'organization', value: 'ACME Inc' },\n * \t{ type: 'country', value: 'US' },\n * ]);\n *\n * // preserves caller-supplied attribute order\n * ```\n *\n * @example\n * ```ts\n * import { encodeRelativeDistinguishedName } from 'micro509/x509';\n *\n * const rdnDer = encodeRelativeDistinguishedName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'serialNumber', value: 'device-7' },\n * ]);\n *\n * // one multi-valued RDN containing CN + serialNumber\n * ```\n *\n * @module\n */\n\nimport { hasLoneSurrogate, objectIdentifier, sequence, setOf } from '#micro509/internal/asn1/der';\nimport { NAME_FIELD_DEFINITIONS, NAME_OBJECT_ORDER } from '#micro509/internal/x509/name-fields';\nimport { throwMicro509Error } from '#micro509/result/result';\n\n/** Machine-readable reason a distinguished-name encoder rejected its construction input. */\nexport type NameEncoderErrorCode =\n\t| 'relative_distinguished_name_empty'\n\t| 'unsupported_name_field'\n\t| 'name_attribute_empty'\n\t| 'name_attribute_too_long'\n\t| 'name_attribute_lone_surrogate'\n\t| 'invalid_country_code';\n\n/** Throws a {@link ResultError} for a distinguished-name encoder input-validation failure. */\nfunction throwNameEncoderError(code: NameEncoderErrorCode, message: string): never {\n\tthrowMicro509Error(code, message);\n}\n\n/**\n * Union of recognized distinguished-name attribute type shorthand names.\n *\n * Each key maps to an OID + ASN.1 string encoding in `NAME_FIELD_DEFINITIONS`.\n */\nexport type NameFieldKey =\n\t| 'commonName'\n\t| 'surname'\n\t| 'serialNumber'\n\t| 'country'\n\t| 'locality'\n\t| 'state'\n\t| 'street'\n\t| 'organization'\n\t| 'organizationalUnit'\n\t| 'title'\n\t| 'givenName'\n\t| 'emailAddress';\n\n/**\n * Convenience object form of an X.501 distinguished name.\n *\n * Populated fields are emitted in the order defined by\n * `NAME_OBJECT_ORDER`.\\\n * Each populated field becomes its own single-attribute RDN.\n *\n * For caller-controlled ordering, pass a {@linkcode NameAttribute} array to {@linkcode encodeName}.\\\n * For multi-valued RDNs, use {@linkcode encodeRelativeDistinguishedName}.\n */\nexport interface NameObject {\n\t/** Subject or issuer common name (CN). */\n\treadonly commonName?: string;\n\t/** Subject surname (SN). */\n\treadonly surname?: string;\n\t/** Device or entity serial number — not the certificate serial. */\n\treadonly serialNumber?: string;\n\t/** ISO 3166 two-letter country code (C). Must be exactly 2 characters. */\n\treadonly country?: string;\n\t/** City or locality (L). */\n\treadonly locality?: string;\n\t/** State or province (ST). */\n\treadonly state?: string;\n\t/** Street address. */\n\treadonly street?: string;\n\t/** Organization name (O). */\n\treadonly organization?: string;\n\t/** Organizational unit (OU). Deprecated in modern CA practice. */\n\treadonly organizationalUnit?: string;\n\t/** Job title or functional designation. */\n\treadonly title?: string;\n\t/** First / given name (GN). */\n\treadonly givenName?: string;\n\t/** PKCS #9 emailAddress attribute (RFC 2985 §5.2.1). Encoded as IA5String, not UTF-8. RFC 5280 §4.1.2.6 deprecates it in favour of a subjectAltName rfc822Name. */\n\treadonly emailAddress?: string;\n}\n\n/**\n * Single name attribute within a distinguished name.\n *\n * RFC 5280 / X.501 call this structure an `AttributeTypeAndValue`.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n * {@linkcode encodeName} places each attribute in its own single-attribute RDN.\\\n * {@linkcode encodeRelativeDistinguishedName} packs several attributes into one RDN.\n */\nexport interface NameAttribute {\n\t/** Which attribute type this pair represents. */\n\treadonly type: NameFieldKey;\n\t/** The string value for this attribute (encoding chosen per field definition). */\n\treadonly value: string;\n}\n\n/**\n * Input for {@linkcode encodeName}.\n *\n * Accepts either a {@linkcode NameObject} convenience shape or an ordered array of {@linkcode NameAttribute} pairs.\\\n * Both forms encode one attribute per RDN.\n */\nexport type NameInput = NameObject | readonly NameAttribute[];\n\n/**\n * Input for {@linkcode encodeRelativeDistinguishedName}.\n *\n * Each entry becomes one name attribute inside the RDN's `SET OF`.\\\n * Use this shape for multi-valued RDNs.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n */\nexport type RelativeDistinguishedNameInput = readonly NameAttribute[];\n\nexport { nameFieldKeyFromOid } from '#micro509/internal/x509/name-fields';\n\n/**\n * DER-encodes an X.509 `Name`.\n *\n * Returns a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs).\\\n * Each RDN emitted by this helper contains exactly one name attribute.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * {@linkcode NameObject} input emits populated fields in the canonical order from `NAME_OBJECT_ORDER`.\\\n * {@linkcode NameAttribute} array input preserves caller-supplied ordering,\n * but each entry still becomes its own single-attribute RDN.\n *\n * Attribute OIDs and ASN.1 string encodings come from `NAME_FIELD_DEFINITIONS`.\\\n * Empty strings and `undefined` fields are ignored when the input is a {@linkcode NameObject}.\n *\n * @example\n * ```ts\n * const der = encodeName({ country: 'US', commonName: 'example.com' });\n *\n * // emits two single-attribute RDNs: C=US, then CN=example.com\n * ```\n *\n * @example\n * ```ts\n * const der = encodeName([\n * \t{ type: 'country', value: 'US' },\n * \t{ type: 'commonName', value: 'example.com' },\n * ]);\n *\n * // preserves caller order: C first, then CN\n * ```\n *\n * @param input Name fields in convenience-object form or caller-ordered attribute form.\n * @returns DER-encoded X.509 `Name` bytes.\n * @throws {ResultError} If a value exceeds its RFC 5280 A.1 bound, an ordered attribute value is empty, a field key is unsupported, or a country code is not two characters. An input with no attributes encodes an empty Name (`30 00`) rather than throwing.\n */\nexport function encodeName(input: NameInput): Uint8Array {\n\tconst attributes = isNameAttributes(input) ? input : nameObjectToAttributes(input);\n\tif (attributes.length === 0) {\n\t\treturn sequence([]);\n\t}\n\n\treturn sequence(attributes.map(encodeNameAttributeAsSet));\n}\n\n/** True when a {@linkcode NameInput} resolves to zero attributes (empty DN). */\nexport function isNameInputEmpty(input: NameInput): boolean {\n\tconst attributes = isNameAttributes(input) ? input : nameObjectToAttributes(input);\n\treturn attributes.length === 0;\n}\n\n/**\n * DER-encodes a single RelativeDistinguishedName (RDN).\n *\n * Returns a DER `SET OF` name attributes for one X.509 name segment.\\\n * Use this when you need a multi-valued RDN.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * Attribute OIDs and ASN.1 string encodings come from\n * `NAME_FIELD_DEFINITIONS`.\n *\n * @example\n * ```ts\n * const rdn = encodeRelativeDistinguishedName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'serialNumber', value: 'device-7' },\n * ]);\n *\n * // emits one RDN with both attributes in the same SET\n * ```\n *\n * @param attributes Attribute list to encode inside one RDN.\n * @returns DER-encoded RelativeDistinguishedName bytes.\n * @throws {ResultError} If the attribute list is empty, contains an unsupported field key, an empty or over-long value, or an invalid country code.\n */\nexport function encodeRelativeDistinguishedName(\n\tattributes: RelativeDistinguishedNameInput,\n): Uint8Array {\n\tif (attributes.length === 0) {\n\t\tthrowNameEncoderError(\n\t\t\t'relative_distinguished_name_empty',\n\t\t\t'Relative distinguished name must contain at least one attribute',\n\t\t);\n\t}\n\treturn setOf(attributes.map(encodeNameAttribute));\n}\n\n/** Narrows a {@linkcode NameInput} to the ordered-attribute-array form. */\nfunction isNameAttributes(input: NameInput): input is readonly NameAttribute[] {\n\treturn Array.isArray(input);\n}\n\n/** Wraps a single attribute in a SET OF for the Name SEQUENCE. */\nfunction encodeNameAttributeAsSet(attribute: NameAttribute): Uint8Array {\n\treturn setOf([encodeNameAttribute(attribute)]);\n}\n\n/**\n * DER-encodes one name attribute SEQUENCE.\n *\n * Throws for unsupported field keys, empty or over-long values, and invalid country codes.\n */\nfunction encodeNameAttribute(attribute: NameAttribute): Uint8Array {\n\tconst definition = NAME_FIELD_DEFINITIONS[attribute.type];\n\tif (definition === undefined) {\n\t\tthrowNameEncoderError('unsupported_name_field', `Unsupported name field: ${attribute.type}`);\n\t}\n\tconst length = [...attribute.value].length;\n\tif (length === 0) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_empty',\n\t\t\t`Name attribute ${attribute.type} must not be empty`,\n\t\t);\n\t}\n\tif (hasLoneSurrogate(attribute.value)) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_lone_surrogate',\n\t\t\t`Name attribute ${attribute.type} holds a lone UTF-16 surrogate`,\n\t\t);\n\t}\n\tif (attribute.type === 'country' && attribute.value.length !== 2) {\n\t\tthrowNameEncoderError('invalid_country_code', 'Country must be a 2-character code');\n\t}\n\tif (definition.maxLength !== undefined && length > definition.maxLength) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_too_long',\n\t\t\t`Name attribute ${attribute.type} must be at most ${definition.maxLength} characters`,\n\t\t);\n\t}\n\treturn sequence([objectIdentifier(definition.oid), definition.encode(attribute.value)]);\n}\n\n/** Converts a {@linkcode NameObject} to ordered attributes per `NAME_OBJECT_ORDER`. */\nfunction nameObjectToAttributes(input: NameObject): readonly NameAttribute[] {\n\tconst attributes: NameAttribute[] = [];\n\tfor (const key of NAME_OBJECT_ORDER) {\n\t\tconst value = input[key];\n\t\tif (typeof value === 'string' && value.length > 0) {\n\t\t\tattributes.push({ type: key, value });\n\t\t}\n\t}\n\treturn attributes;\n}\n\nexport {\n\tcanonicalDnKey,\n\tcompareDistinguishedNames,\n\tisWithinDirectoryNameSubtree,\n} from '#micro509/internal/shared/dn';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EA,SAAS,sBAAsB,GAA4B,GAAwB;CAClF,EAAmBA,GAAMC,CAAO;AACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkIA,SAAgB,WAAW,GAA8B;CACxD,IAAMC,IAAa,iBAAiBC,CAAK,IAAIA,IAAQ,uBAAuBA,CAAK;CAKjF,OAJID,EAAW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QX,EAAMS;EACpB,AAAI,OAAOE,KAAU,YAAYA,EAAM,SAAS,KAC/CZ,EAAW,KAAK;GAAE,MAAMU;GAAK;EAAM,CAAC;CAEtC;CACA,OAAOV;AACR"}
1
+ {"version":3,"file":"name.js","names":["hasLoneSurrogate","objectIdentifier","sequence","setOf","NAME_FIELD_DEFINITIONS","NAME_OBJECT_ORDER","nameFieldKeyFromOid","throwMicro509Error","canonicalDnKey","compareDistinguishedNames","isWithinDirectoryNameSubtree","code","message","input","attributes","attribute","definition","length","key","value"],"sources":["../../src/x509/name.ts"],"sourcesContent":["/**\n * Distinguished-name input and DER encoding helpers.\n *\n * Encodes X.509 `Name` values used for certificate subjects, issuers,\n * and CSR subjects.\\\n * A `Name` is a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs), and each\n * RDN is a `SET OF` one or more name attributes.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * Use {@linkcode encodeName} for the common case where each attribute occupies\n * its own RDN.\\\n * Use {@linkcode encodeRelativeDistinguishedName} when you need one\n * multi-valued RDN.\n *\n * `NameObject` favors convenience: populated fields are emitted in the\n * canonical order from `NAME_OBJECT_ORDER`.\\\n * `NameAttribute` arrays favor control: caller order is preserved, but each\n * entry still becomes its own single-attribute RDN.\n *\n * Attribute OIDs and ASN.1 string encodings come from\n * `NAME_FIELD_DEFINITIONS`.\n *\n * @example\n * ```ts\n * import { encodeName } from 'micro509/x509';\n *\n * const subjectDer = encodeName({\n * \tcountry: 'US',\n * \torganization: 'ACME Inc',\n * \tcommonName: 'example.com',\n * });\n *\n * // DER for: C=US, O=ACME Inc, CN=example.com\n * ```\n *\n * @example\n * ```ts\n * import { encodeName } from 'micro509/x509';\n *\n * const subjectDer = encodeName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'organization', value: 'ACME Inc' },\n * \t{ type: 'country', value: 'US' },\n * ]);\n *\n * // preserves caller-supplied attribute order\n * ```\n *\n * @example\n * ```ts\n * import { encodeRelativeDistinguishedName } from 'micro509/x509';\n *\n * const rdnDer = encodeRelativeDistinguishedName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'serialNumber', value: 'device-7' },\n * ]);\n *\n * // one multi-valued RDN containing CN + serialNumber\n * ```\n *\n * @module\n */\n\nimport { hasLoneSurrogate, objectIdentifier, sequence, setOf } from '#micro509/internal/asn1/der';\nimport { NAME_FIELD_DEFINITIONS, NAME_OBJECT_ORDER } from '#micro509/internal/x509/name-fields';\nimport { throwMicro509Error } from '#micro509/result/result';\n\n/** Machine-readable reason a distinguished-name encoder rejected its construction input. */\nexport type NameEncoderErrorCode =\n\t| 'relative_distinguished_name_empty'\n\t| 'unsupported_name_field'\n\t| 'name_attribute_empty'\n\t| 'name_attribute_too_long'\n\t| 'name_attribute_lone_surrogate'\n\t| 'invalid_country_code';\n\n/** Throws a {@link ResultError} for a distinguished-name encoder input-validation failure. */\nfunction throwNameEncoderError(code: NameEncoderErrorCode, message: string): never {\n\tthrowMicro509Error(code, message);\n}\n\n/**\n * Union of recognized distinguished-name attribute type shorthand names.\n *\n * Each key maps to an OID + ASN.1 string encoding in `NAME_FIELD_DEFINITIONS`.\n */\nexport type NameFieldKey =\n\t| 'commonName'\n\t| 'surname'\n\t| 'serialNumber'\n\t| 'country'\n\t| 'locality'\n\t| 'state'\n\t| 'street'\n\t| 'organization'\n\t| 'organizationalUnit'\n\t| 'title'\n\t| 'givenName'\n\t| 'emailAddress';\n\n/**\n * Convenience object form of an X.501 distinguished name.\n *\n * Populated fields are emitted in the order defined by\n * `NAME_OBJECT_ORDER`.\\\n * Each populated field becomes its own single-attribute RDN.\n *\n * For caller-controlled ordering, pass a {@linkcode NameAttribute} array to {@linkcode encodeName}.\\\n * For multi-valued RDNs, use {@linkcode encodeRelativeDistinguishedName}.\n */\nexport interface NameObject {\n\t/** Subject or issuer common name (CN). */\n\treadonly commonName?: string;\n\t/** Subject surname (SN). */\n\treadonly surname?: string;\n\t/** Device or entity serial number — not the certificate serial. */\n\treadonly serialNumber?: string;\n\t/** ISO 3166 two-letter country code (C). Must be exactly 2 characters. */\n\treadonly country?: string;\n\t/** City or locality (L). */\n\treadonly locality?: string;\n\t/** State or province (ST). */\n\treadonly state?: string;\n\t/** Street address. */\n\treadonly street?: string;\n\t/** Organization name (O). */\n\treadonly organization?: string;\n\t/** Organizational unit (OU). Deprecated in modern CA practice. */\n\treadonly organizationalUnit?: string;\n\t/** Job title or functional designation. */\n\treadonly title?: string;\n\t/** First / given name (GN). */\n\treadonly givenName?: string;\n\t/** PKCS #9 emailAddress attribute (RFC 2985 §5.2.1). Encoded as IA5String, not UTF-8. RFC 5280 §4.1.2.6 deprecates it in favour of a subjectAltName rfc822Name. */\n\treadonly emailAddress?: string;\n}\n\n/**\n * Single name attribute within a distinguished name.\n *\n * RFC 5280 / X.501 call this structure an `AttributeTypeAndValue`.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n * {@linkcode encodeName} places each attribute in its own single-attribute RDN.\\\n * {@linkcode encodeRelativeDistinguishedName} packs several attributes into one RDN.\n */\nexport interface NameAttribute {\n\t/** Which attribute type this pair represents. */\n\treadonly type: NameFieldKey;\n\t/** The string value for this attribute (encoding chosen per field definition). */\n\treadonly value: string;\n}\n\n/**\n * Input for {@linkcode encodeName}.\n *\n * Accepts either a {@linkcode NameObject} convenience shape or an ordered array of {@linkcode NameAttribute} pairs.\\\n * Both forms encode one attribute per RDN.\n */\nexport type NameInput = NameObject | readonly NameAttribute[];\n\n/**\n * Input for {@linkcode encodeRelativeDistinguishedName}.\n *\n * Each entry becomes one name attribute inside the RDN's `SET OF`.\\\n * Use this shape for multi-valued RDNs.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n */\nexport type RelativeDistinguishedNameInput = readonly NameAttribute[];\n\nexport { nameFieldKeyFromOid } from '#micro509/internal/x509/name-fields';\n\n/**\n * DER-encodes an X.509 `Name`.\n *\n * Returns a DER `SEQUENCE` of RelativeDistinguishedNames (RDNs).\\\n * Each RDN emitted by this helper contains exactly one name attribute.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * {@linkcode NameObject} input emits populated fields in the canonical order from `NAME_OBJECT_ORDER`.\\\n * {@linkcode NameAttribute} array input preserves caller-supplied ordering,\n * but each entry still becomes its own single-attribute RDN.\n *\n * Attribute OIDs and ASN.1 string encodings come from `NAME_FIELD_DEFINITIONS`.\\\n * Empty strings and `undefined` fields are ignored when the input is a {@linkcode NameObject}.\n *\n * @example\n * ```ts\n * const der = encodeName({ country: 'US', commonName: 'example.com' });\n *\n * // emits two single-attribute RDNs: C=US, then CN=example.com\n * ```\n *\n * @example\n * ```ts\n * const der = encodeName([\n * \t{ type: 'country', value: 'US' },\n * \t{ type: 'commonName', value: 'example.com' },\n * ]);\n *\n * // preserves caller order: C first, then CN\n * ```\n *\n * @param input Name fields in convenience-object form or caller-ordered attribute form.\n * @returns DER-encoded X.509 `Name` bytes.\n * @throws {ResultError} If a value exceeds its RFC 5280 A.1 bound, an ordered attribute value is empty, a field key is unsupported, or a country code is not two characters. An input with no attributes encodes an empty Name (`30 00`) rather than throwing.\n */\nexport function encodeName(input: NameInput): Uint8Array {\n\tconst attributes = isNameAttributes(input) ? input : nameObjectToAttributes(input);\n\tif (attributes.length === 0) {\n\t\treturn sequence([]);\n\t}\n\n\treturn sequence(attributes.map(encodeNameAttributeAsSet));\n}\n\n/** True when a {@linkcode NameInput} resolves to zero attributes (empty DN). */\nexport function isNameInputEmpty(input: NameInput): boolean {\n\tconst attributes = isNameAttributes(input) ? input : nameObjectToAttributes(input);\n\treturn attributes.length === 0;\n}\n\n/**\n * DER-encodes a single RelativeDistinguishedName (RDN).\n *\n * Returns a DER `SET OF` name attributes for one X.509 name segment.\\\n * Use this when you need a multi-valued RDN.\n *\n * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}\n *\n * Attribute OIDs and ASN.1 string encodings come from\n * `NAME_FIELD_DEFINITIONS`.\n *\n * @example\n * ```ts\n * const rdn = encodeRelativeDistinguishedName([\n * \t{ type: 'commonName', value: 'example.com' },\n * \t{ type: 'serialNumber', value: 'device-7' },\n * ]);\n *\n * // emits one RDN with both attributes in the same SET\n * ```\n *\n * @param attributes Attribute list to encode inside one RDN.\n * @returns DER-encoded RelativeDistinguishedName bytes.\n * @throws {ResultError} If the attribute list is empty, contains an unsupported field key, an empty or over-long value, or an invalid country code.\n */\nexport function encodeRelativeDistinguishedName(\n\tattributes: RelativeDistinguishedNameInput,\n): Uint8Array {\n\tif (attributes.length === 0) {\n\t\tthrowNameEncoderError(\n\t\t\t'relative_distinguished_name_empty',\n\t\t\t'Relative distinguished name must contain at least one attribute',\n\t\t);\n\t}\n\treturn setOf(attributes.map(encodeNameAttribute));\n}\n\n/** Narrows a {@linkcode NameInput} to the ordered-attribute-array form. */\nfunction isNameAttributes(input: NameInput): input is readonly NameAttribute[] {\n\treturn Array.isArray(input);\n}\n\n/** Wraps a single attribute in a SET OF for the Name SEQUENCE. */\nfunction encodeNameAttributeAsSet(attribute: NameAttribute): Uint8Array {\n\treturn setOf([encodeNameAttribute(attribute)]);\n}\n\n/**\n * DER-encodes one name attribute SEQUENCE.\n *\n * Throws for unsupported field keys, empty or over-long values, and invalid country codes.\n */\nfunction encodeNameAttribute(attribute: NameAttribute): Uint8Array {\n\tconst definition = NAME_FIELD_DEFINITIONS[attribute.type];\n\tif (definition === undefined) {\n\t\tthrowNameEncoderError('unsupported_name_field', `Unsupported name field: ${attribute.type}`);\n\t}\n\tconst length = [...attribute.value].length;\n\tif (length === 0) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_empty',\n\t\t\t`Name attribute ${attribute.type} must not be empty`,\n\t\t);\n\t}\n\tif (hasLoneSurrogate(attribute.value)) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_lone_surrogate',\n\t\t\t`Name attribute ${attribute.type} holds a lone UTF-16 surrogate`,\n\t\t);\n\t}\n\tif (attribute.type === 'country' && attribute.value.length !== 2) {\n\t\tthrowNameEncoderError('invalid_country_code', 'Country must be a 2-character code');\n\t}\n\tif (definition.maxLength !== undefined && length > definition.maxLength) {\n\t\tthrowNameEncoderError(\n\t\t\t'name_attribute_too_long',\n\t\t\t`Name attribute ${attribute.type} must be at most ${definition.maxLength} characters`,\n\t\t);\n\t}\n\treturn sequence([objectIdentifier(definition.oid), definition.encode(attribute.value)]);\n}\n\n/** Converts a {@linkcode NameObject} to ordered attributes per `NAME_OBJECT_ORDER`. */\nfunction nameObjectToAttributes(input: NameObject): readonly NameAttribute[] {\n\tconst attributes: NameAttribute[] = [];\n\tfor (const key of NAME_OBJECT_ORDER) {\n\t\tconst value = input[key];\n\t\tif (typeof value === 'string' && value.length > 0) {\n\t\t\tattributes.push({ type: key, value });\n\t\t}\n\t}\n\treturn attributes;\n}\n\nexport {\n\tcanonicalDnKey,\n\tcompareDistinguishedNames,\n\tisWithinDirectoryNameSubtree,\n} from '#micro509/internal/shared/dn';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EA,SAAS,sBAAsBW,GAA4BC,GAAwB;CAClFL,EAAmBI,GAAMC,CAAO;AACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkIA,SAAgB,WAAWC,GAA8B;CACxD,IAAMC,IAAa,iBAAiBD,CAAK,IAAIA,IAAQ,uBAAuBA,CAAK;CAKjF,OAJIC,EAAW,WAAW,IAClBZ,EAAS,CAAC,CAAC,IAGZA,EAASY,EAAW,IAAI,wBAAwB,CAAC;AACzD;;AAGA,SAAgB,iBAAiBD,GAA2B;CAE3D,QADmB,iBAAiBA,CAAK,IAAIA,IAAQ,uBAAuBA,CAAK,EAAA,CAC/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Map = Record<nev
242
242
  readonly tbsCertificateDer: Uint8Array;
243
243
  /** DER encoding of the SubjectPublicKeyInfo, used for key import. */
244
244
  readonly subjectPublicKeyInfoDer: Uint8Array;
245
- /** Raw signature bytes (BIT STRING content, padding removed). */
245
+ /** Signature bytes: the `signatureValue` BIT STRING content after its unused-bit count. */
246
246
  readonly signatureValue: Uint8Array;
247
+ /** Unused bits in the final octet of {@linkcode signatureValue}, 0 through 7. */
248
+ readonly signatureValueUnusedBits: number;
247
249
  /** Distinguished name of the certificate issuer. */
248
250
  readonly issuer: ParsedName;
249
251
  /** Distinguished name of the certificate subject. */
@@ -316,8 +318,10 @@ export interface ParsedCertificateSigningRequest<TMap extends ExtensionDecoderMa
316
318
  readonly certificationRequestInfoDer: Uint8Array;
317
319
  /** DER encoding of the SubjectPublicKeyInfo. */
318
320
  readonly subjectPublicKeyInfoDer: Uint8Array;
319
- /** Raw signature bytes (BIT STRING content, padding removed). */
321
+ /** Signature bytes: the `signatureValue` BIT STRING content after its unused-bit count. */
320
322
  readonly signatureValue: Uint8Array;
323
+ /** Unused bits in the final octet of {@linkcode signatureValue}, 0 through 7. */
324
+ readonly signatureValueUnusedBits: number;
321
325
  /** Distinguished name the requester wants on the certificate. */
322
326
  readonly subject: ParsedName;
323
327
  /** OID of the algorithm used to sign this CSR. */