micro509 0.14.0 → 0.15.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 (161) hide show
  1. package/CHANGELOG.md +2064 -0
  2. package/README.md +105 -225
  3. package/dist/crypto/crypto.d.ts +42 -11
  4. package/dist/crypto/crypto.js +53 -22
  5. package/dist/crypto/crypto.js.map +1 -1
  6. package/dist/crypto.d.ts +2 -2
  7. package/dist/crypto.js +1 -2
  8. package/dist/der/der.d.ts +41 -33
  9. package/dist/der/der.js +26 -24
  10. package/dist/der/der.js.map +1 -1
  11. package/dist/index.d.ts +10 -10
  12. package/dist/index.js +19 -19
  13. package/dist/internal/asn1/asn1.d.ts +3 -6
  14. package/dist/internal/asn1/asn1.js +343 -43
  15. package/dist/internal/asn1/asn1.js.map +1 -1
  16. package/dist/internal/asn1/ber.js +146 -0
  17. package/dist/internal/asn1/ber.js.map +1 -0
  18. package/dist/internal/asn1/decode-refusal.js +29 -0
  19. package/dist/internal/asn1/decode-refusal.js.map +1 -0
  20. package/dist/internal/asn1/der.d.ts +38 -41
  21. package/dist/internal/asn1/der.js +131 -52
  22. package/dist/internal/asn1/der.js.map +1 -1
  23. package/dist/internal/asn1/oids.js +83 -53
  24. package/dist/internal/asn1/oids.js.map +1 -1
  25. package/dist/internal/asn1/oids2.js +1 -2
  26. package/dist/internal/asn1/oids2.js.map +1 -1
  27. package/dist/internal/crypto/algorithm-names.js +1 -2
  28. package/dist/internal/crypto/algorithm-names.js.map +1 -1
  29. package/dist/internal/crypto/ecdsa.js +13 -8
  30. package/dist/internal/crypto/ecdsa.js.map +1 -1
  31. package/dist/internal/crypto/hash.js +1 -2
  32. package/dist/internal/crypto/hash.js.map +1 -1
  33. package/dist/internal/crypto/pbes2.d.ts +24 -11
  34. package/dist/internal/crypto/pbes2.js +79 -26
  35. package/dist/internal/crypto/pbes2.js.map +1 -1
  36. package/dist/internal/crypto/rsa-pss.js +46 -44
  37. package/dist/internal/crypto/rsa-pss.js.map +1 -1
  38. package/dist/internal/crypto/sig-verify.d.ts +4 -7
  39. package/dist/internal/crypto/sig-verify.js +107 -60
  40. package/dist/internal/crypto/sig-verify.js.map +1 -1
  41. package/dist/internal/crypto/signing.d.ts +1 -4
  42. package/dist/internal/crypto/signing.js +28 -29
  43. package/dist/internal/crypto/signing.js.map +1 -1
  44. package/dist/internal/crypto/webcrypto.js +1 -2
  45. package/dist/internal/crypto/webcrypto.js.map +1 -1
  46. package/dist/internal/shared/base64.js +1 -2
  47. package/dist/internal/shared/base64.js.map +1 -1
  48. package/dist/internal/shared/dn.d.ts +3 -6
  49. package/dist/internal/shared/dn.js +99 -53
  50. package/dist/internal/shared/dn.js.map +1 -1
  51. package/dist/internal/shared/idna-tables.js +7217 -0
  52. package/dist/internal/shared/idna-tables.js.map +1 -0
  53. package/dist/internal/shared/idna.js +296 -0
  54. package/dist/internal/shared/idna.js.map +1 -0
  55. package/dist/internal/shared/ip.d.ts +4 -7
  56. package/dist/internal/shared/ip.js +1 -2
  57. package/dist/internal/shared/ip.js.map +1 -1
  58. package/dist/internal/shared/mailbox.js +26 -0
  59. package/dist/internal/shared/mailbox.js.map +1 -0
  60. package/dist/internal/shared/punycode.js +101 -0
  61. package/dist/internal/shared/punycode.js.map +1 -0
  62. package/dist/internal/shared/rfc3454-tables.js +1 -2
  63. package/dist/internal/shared/rfc3454-tables.js.map +1 -1
  64. package/dist/internal/shared/rfc4518-tables.js +119 -0
  65. package/dist/internal/shared/rfc4518-tables.js.map +1 -0
  66. package/dist/internal/shared/uri-host.js +102 -0
  67. package/dist/internal/shared/uri-host.js.map +1 -0
  68. package/dist/internal/verify/name-constraints-engine.js +246 -131
  69. package/dist/internal/verify/name-constraints-engine.js.map +1 -1
  70. package/dist/internal/verify/policy-engine.js +6 -21
  71. package/dist/internal/verify/policy-engine.js.map +1 -1
  72. package/dist/internal/verify/verify-path.d.ts +1 -4
  73. package/dist/internal/verify/verify-path.js +267 -131
  74. package/dist/internal/verify/verify-path.js.map +1 -1
  75. package/dist/internal/x509/directory-name.js +1 -2
  76. package/dist/internal/x509/directory-name.js.map +1 -1
  77. package/dist/internal/x509/extension-bits.js +2 -3
  78. package/dist/internal/x509/extension-bits.js.map +1 -1
  79. package/dist/internal/x509/extension-errors.d.ts +1 -4
  80. package/dist/internal/x509/extension-errors.js +1 -2
  81. package/dist/internal/x509/extension-errors.js.map +1 -1
  82. package/dist/internal/x509/extension-registry.js +107 -89
  83. package/dist/internal/x509/extension-registry.js.map +1 -1
  84. package/dist/internal/x509/general-name-profile.js +129 -0
  85. package/dist/internal/x509/general-name-profile.js.map +1 -0
  86. package/dist/internal/x509/general-name-tags.js +1 -2
  87. package/dist/internal/x509/general-name-tags.js.map +1 -1
  88. package/dist/internal/x509/general-name.js +128 -42
  89. package/dist/internal/x509/general-name.js.map +1 -1
  90. package/dist/internal/x509/name-fields.js +1 -2
  91. package/dist/internal/x509/name-fields.js.map +1 -1
  92. package/dist/internal/x509/or-address.js +217 -0
  93. package/dist/internal/x509/or-address.js.map +1 -0
  94. package/dist/keys/keys.d.ts +112 -97
  95. package/dist/keys/keys.js +211 -204
  96. package/dist/keys/keys.js.map +1 -1
  97. package/dist/keys.d.ts +3 -3
  98. package/dist/pem/pem.d.ts +14 -17
  99. package/dist/pem/pem.js +16 -17
  100. package/dist/pem/pem.js.map +1 -1
  101. package/dist/pem.js +1 -2
  102. package/dist/pkcs/pfx.d.ts +57 -32
  103. package/dist/pkcs/pfx.js +209 -173
  104. package/dist/pkcs/pfx.js.map +1 -1
  105. package/dist/pkcs/pkcs12-mac.d.ts +104 -27
  106. package/dist/pkcs/pkcs12-mac.js +266 -79
  107. package/dist/pkcs/pkcs12-mac.js.map +1 -1
  108. package/dist/pkcs/pkcs7.d.ts +43 -37
  109. package/dist/pkcs/pkcs7.js +218 -217
  110. package/dist/pkcs/pkcs7.js.map +1 -1
  111. package/dist/pkcs.d.ts +4 -3
  112. package/dist/result/result.d.ts +27 -20
  113. package/dist/result/result.js +8 -3
  114. package/dist/result/result.js.map +1 -1
  115. package/dist/result.d.ts +2 -2
  116. package/dist/result.js +1 -2
  117. package/dist/revocation/chain.d.ts +101 -23
  118. package/dist/revocation/chain.js +436 -182
  119. package/dist/revocation/chain.js.map +1 -1
  120. package/dist/revocation/crl.d.ts +131 -67
  121. package/dist/revocation/crl.js +444 -323
  122. package/dist/revocation/crl.js.map +1 -1
  123. package/dist/revocation/ocsp.d.ts +94 -59
  124. package/dist/revocation/ocsp.js +331 -293
  125. package/dist/revocation/ocsp.js.map +1 -1
  126. package/dist/revocation/revocation.d.ts +47 -35
  127. package/dist/revocation/revocation.js +88 -57
  128. package/dist/revocation/revocation.js.map +1 -1
  129. package/dist/revocation.d.ts +3 -3
  130. package/dist/verify/identity.d.ts +16 -19
  131. package/dist/verify/identity.js +118 -89
  132. package/dist/verify/identity.js.map +1 -1
  133. package/dist/verify/name-constraints.d.ts +1 -4
  134. package/dist/verify/policy.d.ts +9 -7
  135. package/dist/verify/verify.d.ts +84 -46
  136. package/dist/verify/verify.js +300 -152
  137. package/dist/verify/verify.js.map +1 -1
  138. package/dist/verify.d.ts +2 -2
  139. package/dist/x509/certificate.d.ts +20 -21
  140. package/dist/x509/certificate.js +62 -46
  141. package/dist/x509/certificate.js.map +1 -1
  142. package/dist/x509/csr.d.ts +5 -7
  143. package/dist/x509/csr.js +10 -11
  144. package/dist/x509/csr.js.map +1 -1
  145. package/dist/x509/extensions.d.ts +154 -66
  146. package/dist/x509/extensions.js +317 -151
  147. package/dist/x509/extensions.js.map +1 -1
  148. package/dist/x509/fingerprint.d.ts +4 -7
  149. package/dist/x509/fingerprint.js +1 -2
  150. package/dist/x509/fingerprint.js.map +1 -1
  151. package/dist/x509/name-text.d.ts +7 -9
  152. package/dist/x509/name-text.js +18 -4
  153. package/dist/x509/name-text.js.map +1 -1
  154. package/dist/x509/name.d.ts +9 -11
  155. package/dist/x509/name.js +18 -19
  156. package/dist/x509/name.js.map +1 -1
  157. package/dist/x509/parse.d.ts +84 -69
  158. package/dist/x509/parse.js +421 -400
  159. package/dist/x509/parse.js.map +1 -1
  160. package/dist/x509.d.ts +2 -2
  161. package/package.json +76 -59
@@ -1,8 +1,7 @@
1
1
  import { SubjectAltName } from "./extensions.js";
2
2
  import { ParsedName, ParsedRelativeDistinguishedName } from "./parse.js";
3
- //#region src/x509/name-text.d.ts
4
3
  /** Options for {@linkcode subjectAltNameToString}. */
5
- interface SubjectAltNameTextOptions {
4
+ export interface SubjectAltNameTextOptions {
6
5
  /** Prepend `{@linkcode subjectAltNameLabel}:` to the value, as `openssl x509 -text` does. Defaults to `false`. */
7
6
  readonly prefix?: boolean;
8
7
  }
@@ -23,12 +22,13 @@ interface SubjectAltNameTextOptions {
23
22
  * subjectAltNameToString({ type: 'dns', value: 'example.com' }, { prefix: true }); // 'DNS:example.com'
24
23
  * ```
25
24
  */
26
- declare function subjectAltNameToString(name: SubjectAltName, options?: SubjectAltNameTextOptions): string;
25
+ export declare function subjectAltNameToString(name: SubjectAltName, options?: SubjectAltNameTextOptions): string;
27
26
  /**
28
27
  * The `openssl x509 -text` label for a {@linkcode SubjectAltName} variant —
29
- * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `DirName`, or `[tag <n>]` for an unrecognized tag.
28
+ * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `SmtpUTF8Mailbox`, `UPN`, `KRB5PrincipalName`, `DirName`, `othername`, `X400Name`,
29
+ * `EdiPartyName`, `Registered ID`, or `[tag <n>]` for raw `unknown` input.
30
30
  */
31
- declare function subjectAltNameLabel(name: SubjectAltName): string;
31
+ export declare function subjectAltNameLabel(name: SubjectAltName): string;
32
32
  /**
33
33
  * Renders a {@linkcode ParsedName} as an RFC 4514 distinguished name string, e.g. `CN=Example CA,O=Acme,C=US`.
34
34
  *
@@ -42,9 +42,7 @@ declare function subjectAltNameLabel(name: SubjectAltName): string;
42
42
  * distinguishedNameToString(parsed.subject); // 'CN=example.com,O=Acme\\, Inc.,C=US'
43
43
  * ```
44
44
  */
45
- declare function distinguishedNameToString(name: ParsedName): string;
45
+ export declare function distinguishedNameToString(name: ParsedName): string;
46
46
  /** Renders one {@linkcode ParsedRelativeDistinguishedName}, joining a multi-valued RDN's attributes with `+`. */
47
- declare function relativeDistinguishedNameToString(rdn: ParsedRelativeDistinguishedName): string;
48
- //#endregion
49
- export { SubjectAltNameTextOptions, distinguishedNameToString, relativeDistinguishedNameToString, subjectAltNameLabel, subjectAltNameToString };
47
+ export declare function relativeDistinguishedNameToString(rdn: ParsedRelativeDistinguishedName): string;
50
48
  //# sourceMappingURL=name-text.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import { hexToBytes as e, toHex as t } from "../internal/asn1/asn1.js";
2
2
  import { DN_ATTRIBUTE_KEYWORDS as n } from "../internal/x509/name-fields.js";
3
3
  import { parseDistinguishedNameDer as r } from "./parse.js";
4
- //#region src/x509/name-text.ts
4
+ // src/x509/name-text.ts
5
5
  /**
6
6
  * Human-readable text for parsed names.
7
7
  *
@@ -33,7 +33,8 @@ function subjectAltNameToString(e, t) {
33
33
  }
34
34
  /**
35
35
  * The `openssl x509 -text` label for a {@linkcode SubjectAltName} variant —
36
- * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `DirName`, or `[tag <n>]` for an unrecognized tag.
36
+ * `DNS`, `IP Address`, `email`, `URI`, `SRV`, `SmtpUTF8Mailbox`, `UPN`, `KRB5PrincipalName`, `DirName`, `othername`, `X400Name`,
37
+ * `EdiPartyName`, `Registered ID`, or `[tag <n>]` for raw `unknown` input.
37
38
  */
38
39
  function subjectAltNameLabel(e) {
39
40
  switch (e.type) {
@@ -42,7 +43,14 @@ function subjectAltNameLabel(e) {
42
43
  case "email": return "email";
43
44
  case "uri": return "URI";
44
45
  case "srv": return "SRV";
46
+ case "smtpUtf8Mailbox": return "SmtpUTF8Mailbox";
47
+ case "upn": return "UPN";
48
+ case "krb5PrincipalName": return "KRB5PrincipalName";
45
49
  case "directoryName": return "DirName";
50
+ case "otherName": return "othername";
51
+ case "x400Address": return "X400Name";
52
+ case "ediPartyName": return "EdiPartyName";
53
+ case "registeredID": return "Registered ID";
46
54
  case "unknown": return `[tag ${String(e.tag)}]`;
47
55
  default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
48
56
  }
@@ -94,8 +102,15 @@ function subjectAltNameText(e) {
94
102
  case "ip":
95
103
  case "email":
96
104
  case "uri":
97
- case "srv": return e.value;
105
+ case "srv":
106
+ case "smtpUtf8Mailbox":
107
+ case "upn":
108
+ case "registeredID": return e.value;
109
+ case "krb5PrincipalName": return `${e.nameString.join("/")}@${e.realm}`;
98
110
  case "directoryName": return directoryNameText(e.derHex);
111
+ case "otherName": return `${e.typeId}:${t(e.value)}`;
112
+ case "x400Address":
113
+ case "ediPartyName": return t(e.value);
99
114
  case "unknown": return t(e.value);
100
115
  default: throw Error(`Unhandled SubjectAltName type: ${String(e)}`);
101
116
  }
@@ -107,7 +122,6 @@ function directoryNameText(t) {
107
122
  return t;
108
123
  }
109
124
  }
110
- //#endregion
111
125
  export { distinguishedNameToString, relativeDistinguishedNameToString, subjectAltNameLabel, subjectAltNameToString };
112
126
 
113
127
  //# sourceMappingURL=name-text.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"name-text.js","names":["_exhaustive"],"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`, `DirName`, or `[tag <n>]` for an unrecognized tag.\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 'directoryName':\n\t\t\treturn 'DirName';\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\t\treturn name.value;\n\t\tcase 'directoryName':\n\t\t\treturn directoryNameText(name.derHex);\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,IAAM,IAAO,mBAAmB,CAAI;CACpC,OAAO,GAAS,WAAW,KAAO,GAAG,oBAAoB,CAAI,EAAE,GAAG,MAAS;AAC5E;;;;;AAMA,SAAgB,oBAAoB,GAA8B;CACjE,QAAQ,EAAK,MAAb;EACC,KAAK,OACJ,OAAO;EACR,KAAK,MACJ,OAAO;EACR,KAAK,SACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,OACJ,OAAO;EACR,KAAK,iBACJ,OAAO;EACR,KAAK,WACJ,OAAO,QAAQ,OAAO,EAAK,GAAG,EAAE;EACjC,SAEC,MAAU,MAAM,kCAAkC,OAAOA,CAAW,GAAG;CAEzE;AACD;;;;;;;;;;;;;;AAeA,SAAgB,0BAA0B,GAA0B;CACnE,OAAO,CAAC,GAAG,EAAK,IAAI,CAAC,CACnB,QAAQ,CAAC,CACT,KAAK,MAAQ,kCAAkC,CAAG,CAAC,CAAC,CACpD,KAAK,GAAG;AACX;;AAGA,SAAgB,kCAAkC,GAA8C;CAC/F,OAAO,EAAI,WAAW,KAAK,MAAc,kBAAkB,CAAS,CAAC,CAAC,CAAC,KAAK,GAAG;AAChF;;AAGA,MAAM,oBAAqB,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AAAI,CAAC;AAEvE,SAAS,kBAAkB,GAAwC;CAElE,OAAO,GADS,EAAsB,EAAU,QAAQ,EAAU,IAChD,GAAG,qBAAqB,EAAU,KAAK;AAC1D;AAEA,SAAS,qBAAqB,GAAuB;CACpD,IAAM,IAAa,CAAC,GAAG,CAAK;CAC5B,OAAO,EACL,KAAK,GAAW,MAAU;EAC1B,IAAI,EAAmB,IAAI,CAAS,GACnC,OAAO,KAAK;EAEb,IAAM,IAAO,EAAU,YAAY,CAAC,KAAK;EAOzC,OANI,IAAO,MAAQ,MAAS,MACpB,KAAK,EAAK,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,MAE1C,MAAc,QAAQ,MAAU,KAAK,MAAU,EAAW,SAAS,KAC/D,QAED,MAAc,OAAO,MAAU,IAAI,QAAQ;CACnD,CAAC,CAAC,CACD,KAAK,EAAE;AACV;AAEA,SAAS,mBAAmB,GAA8B;CACzD,QAAQ,EAAK,MAAb;EACC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,OACJ,OAAO,EAAK;EACb,KAAK,iBACJ,OAAO,kBAAkB,EAAK,MAAM;EACrC,KAAK,WACJ,OAAO,EAAM,EAAK,KAAK;EACxB,SAEC,MAAU,MAAM,kCAAkC,OAAOA,CAAW,GAAG;CAEzE;AACD;AAEA,SAAS,kBAAkB,GAAwB;CAClD,IAAI;EACH,OAAO,0BAA0B,EAA0B,EAAW,CAAM,CAAC,CAAC;CAC/E,QAAQ;EACP,OAAO;CACR;AACD"}
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,GAAG;AAChF;;AAGA,MAAMC,oBAAqB,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;CAAK;AAAI,CAAC;AAEvE,SAAS,kBAAkB,GAAwC;CAElE,OAAO,GADSC,EAAsBF,EAAU,QAAQA,EAAU,IAChD,GAAG,qBAAqBA,EAAU,KAAK;AAC1D;AAEA,SAAS,qBAAqB,GAAuB;CACpD,IAAMG,IAAa,CAAC,GAAGC,CAAK;CAC5B,OAAOD,EACL,KAAK,GAAW,MAAU;EAC1B,IAAIF,EAAmB,IAAII,CAAS,GACnC,OAAO,KAAKA;EAEb,IAAMC,IAAOD,EAAU,YAAY,CAAC,KAAK;EAOzC,OANIC,IAAO,MAAQA,MAAS,MACpB,KAAKA,EAAK,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG,MAE1CD,MAAc,QAAQE,MAAU,KAAKA,MAAUJ,EAAW,SAAS,KAC/D,QAEDE,MAAc,OAAOE,MAAU,IAAI,QAAQF;CACnD,CAAC,CAAC,CACD,KAAK,EAAE;AACV;AAEA,SAAS,mBAAmB,GAA8B;CACzD,QAAQR,EAAK,MAAb;EACC,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,gBACJ,OAAOA,EAAK;EACb,KAAK,qBACJ,OAAO,GAAGA,EAAK,WAAW,KAAK,GAAG,EAAE,GAAGA,EAAK;EAC7C,KAAK,iBACJ,OAAO,kBAAkBA,EAAK,MAAM;EACrC,KAAK,aACJ,OAAO,GAAGA,EAAK,OAAO,GAAGW,EAAMX,EAAK,KAAK;EAC1C,KAAK;EACL,KAAK,gBACJ,OAAOW,EAAMX,EAAK,KAAK;EACxB,KAAK,WACJ,OAAOW,EAAMX,EAAK,KAAK;EACxB,SAEC,MAAU,MAAM,kCAAkC,OAAOF,CAAW,GAAG;CAEzE;AACD;AAEA,SAAS,kBAAkB,GAAwB;CAClD,IAAI;EACH,OAAO,0BAA0Bc,EAA0BC,EAAWC,CAAM,CAAC,CAAC;CAC/E,QAAQ;EACP,OAAOA;CACR;AACD"}
@@ -1,13 +1,12 @@
1
1
  import { canonicalDnKey, compareDistinguishedNames, isWithinDirectoryNameSubtree } from "../internal/shared/dn.js";
2
- //#region src/x509/name.d.ts
3
2
  /** Machine-readable reason a distinguished-name encoder rejected its construction input. */
4
- type NameEncoderErrorCode = "relative_distinguished_name_empty" | "unsupported_name_field" | "name_attribute_empty" | "name_attribute_too_long" | "invalid_country_code";
3
+ export type NameEncoderErrorCode = "relative_distinguished_name_empty" | "unsupported_name_field" | "name_attribute_empty" | "name_attribute_too_long" | "name_attribute_lone_surrogate" | "invalid_country_code";
5
4
  /**
6
5
  * Union of recognized distinguished-name attribute type shorthand names.
7
6
  *
8
7
  * Each key maps to an OID + ASN.1 string encoding in `NAME_FIELD_DEFINITIONS`.
9
8
  */
10
- type NameFieldKey = "commonName" | "surname" | "serialNumber" | "country" | "locality" | "state" | "street" | "organization" | "organizationalUnit" | "title" | "givenName" | "emailAddress";
9
+ export type NameFieldKey = "commonName" | "surname" | "serialNumber" | "country" | "locality" | "state" | "street" | "organization" | "organizationalUnit" | "title" | "givenName" | "emailAddress";
11
10
  /**
12
11
  * Convenience object form of an X.501 distinguished name.
13
12
  *
@@ -18,7 +17,7 @@ type NameFieldKey = "commonName" | "surname" | "serialNumber" | "country" | "loc
18
17
  * For caller-controlled ordering, pass a {@linkcode NameAttribute} array to {@linkcode encodeName}.\
19
18
  * For multi-valued RDNs, use {@linkcode encodeRelativeDistinguishedName}.
20
19
  */
21
- interface NameObject {
20
+ export interface NameObject {
22
21
  /** Subject or issuer common name (CN). */
23
22
  readonly commonName?: string;
24
23
  /** Subject surname (SN). */
@@ -53,7 +52,7 @@ interface NameObject {
53
52
  * {@linkcode encodeName} places each attribute in its own single-attribute RDN.\
54
53
  * {@linkcode encodeRelativeDistinguishedName} packs several attributes into one RDN.
55
54
  */
56
- interface NameAttribute {
55
+ export interface NameAttribute {
57
56
  /** Which attribute type this pair represents. */
58
57
  readonly type: NameFieldKey;
59
58
  /** The string value for this attribute (encoding chosen per field definition). */
@@ -65,7 +64,7 @@ interface NameAttribute {
65
64
  * Accepts either a {@linkcode NameObject} convenience shape or an ordered array of {@linkcode NameAttribute} pairs.\
66
65
  * Both forms encode one attribute per RDN.
67
66
  */
68
- type NameInput = NameObject | readonly NameAttribute[];
67
+ export type NameInput = NameObject | readonly NameAttribute[];
69
68
  /**
70
69
  * Input for {@linkcode encodeRelativeDistinguishedName}.
71
70
  *
@@ -74,7 +73,7 @@ type NameInput = NameObject | readonly NameAttribute[];
74
73
  *
75
74
  * @see {@link https://datatracker.ietf.org/doc/html/rfc5280#appendix-A.1 RFC 5280 Appendix A.1}
76
75
  */
77
- type RelativeDistinguishedNameInput = readonly NameAttribute[];
76
+ export type RelativeDistinguishedNameInput = readonly NameAttribute[];
78
77
  /**
79
78
  * DER-encodes an X.509 `Name`.
80
79
  *
@@ -111,7 +110,7 @@ type RelativeDistinguishedNameInput = readonly NameAttribute[];
111
110
  * @returns DER-encoded X.509 `Name` bytes.
112
111
  * @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.
113
112
  */
114
- declare function encodeName(input: NameInput): Uint8Array;
113
+ export declare function encodeName(input: NameInput): Uint8Array;
115
114
  /**
116
115
  * DER-encodes a single RelativeDistinguishedName (RDN).
117
116
  *
@@ -137,7 +136,6 @@ declare function encodeName(input: NameInput): Uint8Array;
137
136
  * @returns DER-encoded RelativeDistinguishedName bytes.
138
137
  * @throws {ResultError} If the attribute list is empty, contains an unsupported field key, an empty or over-long value, or an invalid country code.
139
138
  */
140
- declare function encodeRelativeDistinguishedName(attributes: RelativeDistinguishedNameInput): Uint8Array;
141
- //#endregion
142
- export { NameAttribute, NameEncoderErrorCode, NameFieldKey, NameInput, NameObject, RelativeDistinguishedNameInput, canonicalDnKey, compareDistinguishedNames, encodeName, encodeRelativeDistinguishedName, isWithinDirectoryNameSubtree };
139
+ export declare function encodeRelativeDistinguishedName(attributes: RelativeDistinguishedNameInput): Uint8Array;
140
+ export { canonicalDnKey, compareDistinguishedNames, isWithinDirectoryNameSubtree };
143
141
  //# sourceMappingURL=name.d.ts.map
package/dist/x509/name.js CHANGED
@@ -1,8 +1,8 @@
1
- import { objectIdentifier as e, sequence as t, setOf as n } from "../internal/asn1/der.js";
2
- import { throwMicro509Error as r } from "../result/result.js";
3
- import { NAME_FIELD_DEFINITIONS as i, NAME_OBJECT_ORDER as a, nameFieldKeyFromOid as o } from "../internal/x509/name-fields.js";
4
- import { canonicalDnKey as s, compareDistinguishedNames as c, isWithinDirectoryNameSubtree as l } from "../internal/shared/dn.js";
5
- //#region src/x509/name.ts
1
+ import { throwMicro509Error as e } from "../result/result.js";
2
+ import { hasLoneSurrogate as t, objectIdentifier as n, sequence as r, setOf as i } from "../internal/asn1/der.js";
3
+ import { NAME_FIELD_DEFINITIONS as a, NAME_OBJECT_ORDER as o, nameFieldKeyFromOid as s } from "../internal/x509/name-fields.js";
4
+ import { canonicalDnKey as c, compareDistinguishedNames as l, isWithinDirectoryNameSubtree as u } from "../internal/shared/dn.js";
5
+ // src/x509/name.ts
6
6
  /**
7
7
  * Distinguished-name input and DER encoding helpers.
8
8
  *
@@ -67,8 +67,8 @@ import { canonicalDnKey as s, compareDistinguishedNames as c, isWithinDirectoryN
67
67
  * @module
68
68
  */
69
69
  /** Throws a {@link ResultError} for a distinguished-name encoder input-validation failure. */
70
- function throwNameEncoderError(e, t) {
71
- r(e, t);
70
+ function throwNameEncoderError(t, n) {
71
+ e(t, n);
72
72
  }
73
73
  /**
74
74
  * DER-encodes an X.509 `Name`.
@@ -107,8 +107,8 @@ function throwNameEncoderError(e, t) {
107
107
  * @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.
108
108
  */
109
109
  function encodeName(e) {
110
- let n = isNameAttributes(e) ? e : nameObjectToAttributes(e);
111
- return n.length === 0 ? t([]) : t(n.map(encodeNameAttributeAsSet));
110
+ let t = isNameAttributes(e) ? e : nameObjectToAttributes(e);
111
+ return t.length === 0 ? r([]) : r(t.map(encodeNameAttributeAsSet));
112
112
  }
113
113
  /** True when a {@linkcode NameInput} resolves to zero attributes (empty DN). */
114
114
  function isNameInputEmpty(e) {
@@ -140,7 +140,7 @@ function isNameInputEmpty(e) {
140
140
  * @throws {ResultError} If the attribute list is empty, contains an unsupported field key, an empty or over-long value, or an invalid country code.
141
141
  */
142
142
  function encodeRelativeDistinguishedName(e) {
143
- return e.length === 0 && throwNameEncoderError("relative_distinguished_name_empty", "Relative distinguished name must contain at least one attribute"), n(e.map(encodeNameAttribute));
143
+ return e.length === 0 && throwNameEncoderError("relative_distinguished_name_empty", "Relative distinguished name must contain at least one attribute"), i(e.map(encodeNameAttribute));
144
144
  }
145
145
  /** Narrows a {@linkcode NameInput} to the ordered-attribute-array form. */
146
146
  function isNameAttributes(e) {
@@ -148,23 +148,23 @@ function isNameAttributes(e) {
148
148
  }
149
149
  /** Wraps a single attribute in a SET OF for the Name SEQUENCE. */
150
150
  function encodeNameAttributeAsSet(e) {
151
- return n([encodeNameAttribute(e)]);
151
+ return i([encodeNameAttribute(e)]);
152
152
  }
153
153
  /**
154
154
  * DER-encodes one name attribute SEQUENCE.
155
155
  *
156
156
  * Throws for unsupported field keys, empty or over-long values, and invalid country codes.
157
157
  */
158
- function encodeNameAttribute(n) {
159
- let r = i[n.type];
160
- r === void 0 && throwNameEncoderError("unsupported_name_field", `Unsupported name field: ${n.type}`);
161
- let a = [...n.value].length;
162
- return a === 0 && throwNameEncoderError("name_attribute_empty", `Name attribute ${n.type} must not be empty`), n.type === "country" && n.value.length !== 2 && throwNameEncoderError("invalid_country_code", "Country must be a 2-character code"), r.maxLength !== void 0 && a > r.maxLength && throwNameEncoderError("name_attribute_too_long", `Name attribute ${n.type} must be at most ${r.maxLength} characters`), t([e(r.oid), r.encode(n.value)]);
158
+ function encodeNameAttribute(e) {
159
+ let i = a[e.type];
160
+ i === void 0 && throwNameEncoderError("unsupported_name_field", `Unsupported name field: ${e.type}`);
161
+ let o = [...e.value].length;
162
+ return o === 0 && throwNameEncoderError("name_attribute_empty", `Name attribute ${e.type} must not be empty`), t(e.value) && throwNameEncoderError("name_attribute_lone_surrogate", `Name attribute ${e.type} holds a lone UTF-16 surrogate`), e.type === "country" && e.value.length !== 2 && throwNameEncoderError("invalid_country_code", "Country must be a 2-character code"), i.maxLength !== void 0 && o > i.maxLength && throwNameEncoderError("name_attribute_too_long", `Name attribute ${e.type} must be at most ${i.maxLength} characters`), r([n(i.oid), i.encode(e.value)]);
163
163
  }
164
164
  /** Converts a {@linkcode NameObject} to ordered attributes per `NAME_OBJECT_ORDER`. */
165
165
  function nameObjectToAttributes(e) {
166
166
  let t = [];
167
- for (let n of a) {
167
+ for (let n of o) {
168
168
  let r = e[n];
169
169
  typeof r == "string" && r.length > 0 && t.push({
170
170
  type: n,
@@ -173,7 +173,6 @@ function nameObjectToAttributes(e) {
173
173
  }
174
174
  return t;
175
175
  }
176
- //#endregion
177
- export { s as canonicalDnKey, c as compareDistinguishedNames, encodeName, encodeRelativeDistinguishedName, isNameInputEmpty, l as isWithinDirectoryNameSubtree, o as nameFieldKeyFromOid };
176
+ export { c as canonicalDnKey, l as compareDistinguishedNames, encodeName, encodeRelativeDistinguishedName, isNameInputEmpty, u as isWithinDirectoryNameSubtree, s as nameFieldKeyFromOid };
178
177
 
179
178
  //# sourceMappingURL=name.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"name.js","names":[],"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 { 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| '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 (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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA,SAAS,sBAAsB,GAA4B,GAAwB;CAClF,EAAmB,GAAM,CAAO;AACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkIA,SAAgB,WAAW,GAA8B;CACxD,IAAM,IAAa,iBAAiB,CAAK,IAAI,IAAQ,uBAAuB,CAAK;CAKjF,OAJI,EAAW,WAAW,IAClB,EAAS,CAAC,CAAC,IAGZ,EAAS,EAAW,IAAI,wBAAwB,CAAC;AACzD;;AAGA,SAAgB,iBAAiB,GAA2B;CAE3D,QADmB,iBAAiB,CAAK,IAAI,IAAQ,uBAAuB,CAAK,EAAA,CAC/D,WAAW;AAC9B;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,gCACf,GACa;CAOb,OANI,EAAW,WAAW,KACzB,sBACC,qCACA,iEACD,GAEM,EAAM,EAAW,IAAI,mBAAmB,CAAC;AACjD;;AAGA,SAAS,iBAAiB,GAAqD;CAC9E,OAAO,MAAM,QAAQ,CAAK;AAC3B;;AAGA,SAAS,yBAAyB,GAAsC;CACvE,OAAO,EAAM,CAAC,oBAAoB,CAAS,CAAC,CAAC;AAC9C;;;;;;AAOA,SAAS,oBAAoB,GAAsC;CAClE,IAAM,IAAa,EAAuB,EAAU;CACpD,AAAI,MAAe,KAAA,KAClB,sBAAsB,0BAA0B,2BAA2B,EAAU,MAAM;CAE5F,IAAM,IAAS,CAAC,GAAG,EAAU,KAAK,CAAC,CAAC;CAgBpC,OAfI,MAAW,KACd,sBACC,wBACA,kBAAkB,EAAU,KAAK,mBAClC,GAEG,EAAU,SAAS,aAAa,EAAU,MAAM,WAAW,KAC9D,sBAAsB,wBAAwB,oCAAoC,GAE/E,EAAW,cAAc,KAAA,KAAa,IAAS,EAAW,aAC7D,sBACC,2BACA,kBAAkB,EAAU,KAAK,mBAAmB,EAAW,UAAU,YAC1E,GAEM,EAAS,CAAC,EAAiB,EAAW,GAAG,GAAG,EAAW,OAAO,EAAU,KAAK,CAAC,CAAC;AACvF;;AAGA,SAAS,uBAAuB,GAA6C;CAC5E,IAAM,IAA8B,CAAC;CACrC,KAAK,IAAM,KAAO,GAAmB;EACpC,IAAM,IAAQ,EAAM;EACpB,AAAI,OAAO,KAAU,YAAY,EAAM,SAAS,KAC/C,EAAW,KAAK;GAAE,MAAM;GAAK;EAAM,CAAC;CAEtC;CACA,OAAO;AACR"}
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,WAAW,IAClBE,EAAS,CAAC,CAAC,IAGZA,EAASF,EAAW,IAAI,wBAAwB,CAAC;AACzD;;AAGA,SAAgB,iBAAiB,GAA2B;CAE3D,QADmB,iBAAiBC,CAAK,IAAIA,IAAQ,uBAAuBA,CAAK,EAAA,CAC/D,WAAW;AAC9B;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,gCACf,GACa;CAOb,OANID,EAAW,WAAW,KACzB,sBACC,qCACA,iEACD,GAEMG,EAAMH,EAAW,IAAI,mBAAmB,CAAC;AACjD;;AAGA,SAAS,iBAAiB,GAAqD;CAC9E,OAAO,MAAM,QAAQC,CAAK;AAC3B;;AAGA,SAAS,yBAAyB,GAAsC;CACvE,OAAOE,EAAM,CAAC,oBAAoBC,CAAS,CAAC,CAAC;AAC9C;;;;;;AAOA,SAAS,oBAAoB,GAAsC;CAClE,IAAMC,IAAaC,EAAuBF,EAAU;CACpD,AAAIC,MAAe,KAAA,KAClB,sBAAsB,0BAA0B,2BAA2BD,EAAU,MAAM;CAE5F,IAAMG,IAAS,CAAC,GAAGH,EAAU,KAAK,CAAC,CAAC;CAsBpC,OArBIG,MAAW,KACd,sBACC,wBACA,kBAAkBH,EAAU,KAAK,mBAClC,GAEGI,EAAiBJ,EAAU,KAAK,KACnC,sBACC,iCACA,kBAAkBA,EAAU,KAAK,+BAClC,GAEGA,EAAU,SAAS,aAAaA,EAAU,MAAM,WAAW,KAC9D,sBAAsB,wBAAwB,oCAAoC,GAE/EC,EAAW,cAAc,KAAA,KAAaE,IAASF,EAAW,aAC7D,sBACC,2BACA,kBAAkBD,EAAU,KAAK,mBAAmBC,EAAW,UAAU,YAC1E,GAEMH,EAAS,CAACO,EAAiBJ,EAAW,GAAG,GAAGA,EAAW,OAAOD,EAAU,KAAK,CAAC,CAAC;AACvF;;AAGA,SAAS,uBAAuB,GAA6C;CAC5E,IAAMJ,IAA8B,CAAC;CACrC,KAAK,IAAMU,KAAOC,GAAmB;EACpC,IAAMC,IAAQX,EAAMS;EACpB,AAAI,OAAOE,KAAU,YAAYA,EAAM,SAAS,KAC/CZ,EAAW,KAAK;GAAE,MAAMU;GAAK;EAAM,CAAC;CAEtC;CACA,OAAOV;AACR"}