micro509 0.17.0 → 0.18.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 (137) hide show
  1. package/CHANGELOG.md +123 -6
  2. package/README.md +3 -0
  3. package/dist/crypto/crypto.d.ts +2 -3
  4. package/dist/crypto.d.ts +6 -0
  5. package/dist/der/der.d.ts +14 -4
  6. package/dist/der/der.js +19 -19
  7. package/dist/der.d.ts +26 -2
  8. package/dist/der.js +4 -4
  9. package/dist/index.d.ts +82 -15
  10. package/dist/index.js +19 -19
  11. package/dist/internal/asn1/asn1.d.ts +1 -2
  12. package/dist/internal/asn1/der.d.ts +1 -2
  13. package/dist/internal/asn1/oids.js +71 -353
  14. package/dist/internal/asn1/oids.js.map +1 -1
  15. package/dist/internal/asn1/oids.json.js +357 -0
  16. package/dist/internal/asn1/oids.json.js.map +1 -0
  17. package/dist/internal/crypto/algorithm-names.js +1 -1
  18. package/dist/internal/crypto/pbes2.d.ts +1 -2
  19. package/dist/internal/crypto/pbes2.js +70 -70
  20. package/dist/internal/crypto/prime-ec.js +37 -0
  21. package/dist/internal/crypto/prime-ec.js.map +1 -0
  22. package/dist/internal/crypto/rsa-pss.js +50 -50
  23. package/dist/internal/crypto/sig-verify.d.ts +1 -2
  24. package/dist/internal/crypto/sig-verify.js +60 -60
  25. package/dist/internal/crypto/signing.d.ts +1 -2
  26. package/dist/internal/crypto/signing.js +26 -26
  27. package/dist/internal/jose/json-data.js +108 -0
  28. package/dist/internal/jose/json-data.js.map +1 -0
  29. package/dist/internal/jose/strict-json.d.ts +1 -2
  30. package/dist/internal/shared/dn.d.ts +1 -2
  31. package/dist/internal/shared/dn.js +1 -1
  32. package/dist/internal/shared/ip.d.ts +1 -2
  33. package/dist/internal/verify/name-constraints-engine.js +64 -64
  34. package/dist/internal/verify/policy-engine.js +1 -1
  35. package/dist/internal/verify/verify-path.d.ts +1 -2
  36. package/dist/internal/verify/verify-path.js +19 -19
  37. package/dist/internal/x509/directory-name.js +10 -10
  38. package/dist/internal/x509/extension-errors.d.ts +1 -2
  39. package/dist/internal/x509/extension-registry.js +18 -18
  40. package/dist/internal/x509/general-name-profile.js +9 -9
  41. package/dist/internal/x509/general-name.js +32 -32
  42. package/dist/internal/x509/name-fields.js +1 -1
  43. package/dist/jose/base64url.d.ts +4 -0
  44. package/dist/jose/base64url.js +14 -0
  45. package/dist/jose/base64url.js.map +1 -0
  46. package/dist/jose/errors.d.ts +9 -3
  47. package/dist/jose/errors.js.map +1 -1
  48. package/dist/jose/jwe.d.ts +64 -0
  49. package/dist/jose/jwe.js +188 -0
  50. package/dist/jose/jwe.js.map +1 -0
  51. package/dist/jose/jwks.d.ts +38 -10
  52. package/dist/jose/jwks.js +156 -96
  53. package/dist/jose/jwks.js.map +1 -1
  54. package/dist/jose/jws.d.ts +8 -6
  55. package/dist/jose/jws.js +216 -170
  56. package/dist/jose/jws.js.map +1 -1
  57. package/dist/jose/jwt.d.ts +8 -2
  58. package/dist/jose/jwt.js +98 -98
  59. package/dist/jose/remote-jwks.d.ts +1 -2
  60. package/dist/jose/remote-jwks.js +106 -108
  61. package/dist/jose/remote-jwks.js.map +1 -1
  62. package/dist/jose.d.ts +15 -4
  63. package/dist/jose.js +7 -5
  64. package/dist/kdf/argon2-arithmetic.js +15 -0
  65. package/dist/kdf/argon2-arithmetic.js.map +1 -0
  66. package/dist/kdf/argon2-core.js +486 -0
  67. package/dist/kdf/argon2-core.js.map +1 -0
  68. package/dist/kdf/argon2-memory.js +20 -0
  69. package/dist/kdf/argon2-memory.js.map +1 -0
  70. package/dist/kdf/argon2-portable.js +18 -0
  71. package/dist/kdf/argon2-portable.js.map +1 -0
  72. package/dist/kdf/argon2.d.ts +22 -11
  73. package/dist/kdf/argon2.js +32 -13
  74. package/dist/kdf/argon2.js.map +1 -1
  75. package/dist/kdf.d.ts +6 -0
  76. package/dist/keys/keys.d.ts +31 -6
  77. package/dist/keys/keys.js +315 -233
  78. package/dist/keys/keys.js.map +1 -1
  79. package/dist/keys.d.ts +11 -2
  80. package/dist/keys.js +2 -2
  81. package/dist/pem/pem.d.ts +1 -2
  82. package/dist/pem/pem.js +40 -25
  83. package/dist/pem/pem.js.map +1 -1
  84. package/dist/pem.d.ts +6 -0
  85. package/dist/pkcs/pfx.d.ts +2 -3
  86. package/dist/pkcs/pfx.js +142 -142
  87. package/dist/pkcs/pkcs12-mac.d.ts +1 -2
  88. package/dist/pkcs/pkcs12-mac.js +122 -122
  89. package/dist/pkcs/pkcs7.d.ts +14 -3
  90. package/dist/pkcs/pkcs7.js +268 -248
  91. package/dist/pkcs/pkcs7.js.map +1 -1
  92. package/dist/pkcs.d.ts +10 -1
  93. package/dist/pkcs.js +4 -4
  94. package/dist/result/result.d.ts +1 -2
  95. package/dist/result.d.ts +6 -0
  96. package/dist/revocation/chain.d.ts +2 -3
  97. package/dist/revocation/chain.js +52 -52
  98. package/dist/revocation/crl.d.ts +2 -3
  99. package/dist/revocation/crl.js +319 -319
  100. package/dist/revocation/crl.js.map +1 -1
  101. package/dist/revocation/ocsp.d.ts +2 -3
  102. package/dist/revocation/ocsp.js +395 -395
  103. package/dist/revocation/revocation.d.ts +2 -3
  104. package/dist/revocation/revocation.js +22 -22
  105. package/dist/revocation.d.ts +12 -3
  106. package/dist/revocation.js +4 -4
  107. package/dist/ssh/ssh.d.ts +2 -3
  108. package/dist/ssh/ssh.js +165 -170
  109. package/dist/ssh/ssh.js.map +1 -1
  110. package/dist/ssh/wire.d.ts +1 -2
  111. package/dist/ssh/wire.js +10 -10
  112. package/dist/ssh.d.ts +8 -0
  113. package/dist/verify/identity.d.ts +1 -2
  114. package/dist/verify/identity.js +31 -31
  115. package/dist/verify/name-constraints.d.ts +9 -2
  116. package/dist/verify/policy.d.ts +9 -2
  117. package/dist/verify/verify.d.ts +3 -4
  118. package/dist/verify/verify.js +121 -121
  119. package/dist/verify.d.ts +9 -0
  120. package/dist/x509/certificate.d.ts +4 -5
  121. package/dist/x509/certificate.js +40 -40
  122. package/dist/x509/csr.d.ts +3 -4
  123. package/dist/x509/csr.js +15 -15
  124. package/dist/x509/extensions.d.ts +1 -2
  125. package/dist/x509/extensions.js +165 -165
  126. package/dist/x509/extensions.js.map +1 -1
  127. package/dist/x509/fingerprint.d.ts +1 -2
  128. package/dist/x509/name-text.d.ts +1 -2
  129. package/dist/x509/name.d.ts +1 -2
  130. package/dist/x509/name.js +15 -15
  131. package/dist/x509/parse.d.ts +3 -4
  132. package/dist/x509/parse.js +423 -423
  133. package/dist/x509.d.ts +17 -4
  134. package/dist/x509.js +8 -8
  135. package/package.json +27 -34
  136. package/dist/internal/asn1/oids2.js +0 -75
  137. package/dist/internal/asn1/oids2.js.map +0 -1
package/dist/ssh/ssh.js CHANGED
@@ -1,11 +1,12 @@
1
- import { failureResult as e, isResultError as t, rethrowIfInvariant as n, successResult as r, throwMicro509Error as i, unwrap as a } from "../result/result.js";
2
- import { concatBytes as o } from "../internal/asn1/der.js";
3
- import { toArrayBuffer as s } from "../internal/asn1/asn1.js";
4
- import { getCrypto as c } from "../internal/crypto/webcrypto.js";
5
- import { base64Decode as l, base64Encode as u } from "../internal/shared/base64.js";
6
- import { pemDecodeOrThrow as d, pemEncode as f } from "../pem/pem.js";
7
- import { exportPrivateJwk as p, exportPublicJwk as m, importPrivateJwkOrThrow as h, importPublicJwkOrThrow as g } from "../keys/keys.js";
8
- import { asSshErrorCode as _, bigintToBytes as v, bytesEqual as y, bytesToBigint as b, createSshReader as x, decodeUtf8 as S, sshFail as C, sshMpint as w, sshString as T, sshTextString as E, sshUint32 as D } from "./wire.js";
1
+ import { toArrayBuffer as e } from "../internal/asn1/asn1.js";
2
+ import { concatBytes as t } from "../internal/asn1/der.js";
3
+ import { PRIME_EC_CURVES as n } from "../internal/crypto/prime-ec.js";
4
+ import { getCrypto as r } from "../internal/crypto/webcrypto.js";
5
+ import { base64Decode as i, base64Encode as a } from "../internal/shared/base64.js";
6
+ import { exportPrivateJwk as o, exportPublicJwk as s, importPrivateJwkOrThrow as c, importPublicJwkOrThrow as l } from "../keys/keys.js";
7
+ import { pemDecodeOrThrow as u, pemEncode as d } from "../pem/pem.js";
8
+ import { failureResult as f, isResultError as p, rethrowIfInvariant as m, successResult as h, throwMicro509Error as g, unwrap as _ } from "../result/result.js";
9
+ import { asSshErrorCode as v, bigintToBytes as y, bytesEqual as b, bytesToBigint as x, createSshReader as S, decodeUtf8 as C, sshFail as w, sshMpint as T, sshString as E, sshTextString as D, sshUint32 as O } from "./wire.js";
9
10
  // src/ssh/ssh.ts
10
11
  /**
11
12
  * OpenSSH public-key lines, unencrypted `openssh-key-v1` private keys, and
@@ -39,7 +40,7 @@ import { asSshErrorCode as _, bigintToBytes as v, bytesEqual as y, bytesToBigint
39
40
  *
40
41
  * @module
41
42
  */
42
- const O = 32768, k = 2049, A = 8192, j = 16384, M = "OPENSSH PRIVATE KEY", N = Uint8Array.from("openssh-key-v1", (e) => e.charCodeAt(0)), P = [
43
+ const k = 32768, A = 2049, j = 8192, M = 16384, N = "OPENSSH PRIVATE KEY", P = Uint8Array.from("openssh-key-v1", (e) => e.charCodeAt(0)), F = [
43
44
  "ssh-rsa",
44
45
  "ssh-ed25519",
45
46
  "ecdsa-sha2-nistp256",
@@ -47,49 +48,43 @@ const O = 32768, k = 2049, A = 8192, j = 16384, M = "OPENSSH PRIVATE KEY", N = U
47
48
  "ecdsa-sha2-nistp521"
48
49
  ];
49
50
  function asSshKeyType(e) {
50
- return P.find((t) => t === e);
51
+ return F.find((t) => t === e);
51
52
  }
52
- const F = {
53
+ const I = {
53
54
  "P-256": {
54
55
  identifier: "nistp256",
55
- size: 32,
56
56
  hash: "SHA-256",
57
- prime: 2n ** 256n - 2n ** 224n + 2n ** 192n + 2n ** 96n - 1n,
58
- b: 41058363725152142129326129780047268409114441015993725554835256314039467401291n
57
+ ...n["P-256"]
59
58
  },
60
59
  "P-384": {
61
60
  identifier: "nistp384",
62
- size: 48,
63
61
  hash: "SHA-384",
64
- prime: 2n ** 384n - 2n ** 128n - 2n ** 96n + 2n ** 32n - 1n,
65
- b: 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n
62
+ ...n["P-384"]
66
63
  },
67
64
  "P-521": {
68
65
  identifier: "nistp521",
69
- size: 66,
70
66
  hash: "SHA-512",
71
- prime: 2n ** 521n - 1n,
72
- b: 1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n
67
+ ...n["P-521"]
73
68
  }
74
- }, I = [
69
+ }, L = [
75
70
  "P-256",
76
71
  "P-384",
77
72
  "P-521"
78
- ], L = {
73
+ ], R = {
79
74
  "ecdsa-sha2-nistp256": "P-256",
80
75
  "ecdsa-sha2-nistp384": "P-384",
81
76
  "ecdsa-sha2-nistp521": "P-521"
82
77
  };
83
78
  function resolveImportOptions(e) {
84
- if (typeof e != "object" || !e || Array.isArray(e)) return C("invalid_options", "SSH import options must be an object");
79
+ if (typeof e != "object" || !e || Array.isArray(e)) return w("invalid_options", "SSH import options must be an object");
85
80
  let t = e.minRsaModulusBits === void 0 ? 2048 : e.minRsaModulusBits;
86
- if (!Number.isSafeInteger(t) || t < 1 || t > j) return C("invalid_options", "minRsaModulusBits must be an integer from 1 to 16384");
81
+ if (!Number.isSafeInteger(t) || t < 1 || t > M) return w("invalid_options", "minRsaModulusBits must be an integer from 1 to 16384");
87
82
  let n = e.acceptCompressedEcPoints !== void 0 && e.acceptCompressedEcPoints;
88
- if (typeof n != "boolean") return C("invalid_options", "acceptCompressedEcPoints must be a boolean");
83
+ if (typeof n != "boolean") return w("invalid_options", "acceptCompressedEcPoints must be a boolean");
89
84
  let r = e.rsa === void 0 ? {} : e.rsa;
90
- if (typeof r != "object" || !r || Array.isArray(r)) return C("invalid_options", "rsa must be an object");
85
+ if (typeof r != "object" || !r || Array.isArray(r)) return w("invalid_options", "rsa must be an object");
91
86
  let i = r.scheme === void 0 ? "pkcs1-v1_5" : r.scheme, a = r.hash === void 0 ? "SHA-256" : r.hash;
92
- return i !== "pkcs1-v1_5" && i !== "pss" ? C("invalid_options", "Unsupported RSA signature scheme") : a !== "SHA-256" && a !== "SHA-384" && a !== "SHA-512" ? C("invalid_options", "Unsupported RSA signature hash") : {
87
+ return i !== "pkcs1-v1_5" && i !== "pss" ? w("invalid_options", "Unsupported RSA signature scheme") : a !== "SHA-256" && a !== "SHA-384" && a !== "SHA-512" ? w("invalid_options", "Unsupported RSA signature hash") : {
93
88
  minRsaModulusBits: t,
94
89
  acceptCompressedEcPoints: n,
95
90
  rsaScheme: i,
@@ -103,7 +98,7 @@ function keyTypeOf(e) {
103
98
  switch (e.kind) {
104
99
  case "rsa": return "ssh-rsa";
105
100
  case "ed25519": return "ssh-ed25519";
106
- case "ecdsa": return `ecdsa-sha2-${F[e.curve].identifier}`;
101
+ case "ecdsa": return `ecdsa-sha2-${I[e.curve].identifier}`;
107
102
  default: return unhandledKey(e);
108
103
  }
109
104
  }
@@ -125,11 +120,11 @@ function algorithmOf(e) {
125
120
  }
126
121
  }
127
122
  function toBase64Url(e) {
128
- return u(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
123
+ return a(e).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
129
124
  }
130
125
  function fromBase64Url(e) {
131
126
  let t = e.replace(/-/g, "+").replace(/_/g, "/");
132
- return l(t.padEnd(Math.ceil(t.length / 4) * 4, "="));
127
+ return i(t.padEnd(Math.ceil(t.length / 4) * 4, "="));
133
128
  }
134
129
  function modPow(e, t, n) {
135
130
  let r = 1n, i = e % n, a = t;
@@ -137,57 +132,57 @@ function modPow(e, t, n) {
137
132
  return r;
138
133
  }
139
134
  function decompressPoint(e, t, n) {
140
- let { prime: r, b: i, size: a } = F[e], o = b(n);
141
- if (o >= r) return C("malformed", "ECDSA point coordinate is out of range");
135
+ let { prime: r, b: i, size: a } = I[e], o = x(n);
136
+ if (o >= r) return w("malformed", "ECDSA point coordinate is out of range");
142
137
  let s = (((o * o - 3n) * o + i) % r + r) % r, c = modPow(s, (r + 1n) / 4n, r);
143
- if (c * c % r !== s) return C("malformed", "ECDSA point is not on the curve");
138
+ if (c * c % r !== s) return w("malformed", "ECDSA point is not on the curve");
144
139
  let l = t === 3, u = (c & 1n) == 1n === l ? c : r - c;
145
- return v(u, a);
140
+ return y(u, a);
146
141
  }
147
142
  function readPoint(e, t, n) {
148
- let { size: r } = F[e], i = t[0];
143
+ let { size: r } = I[e], i = t[0];
149
144
  if (i === 4 && t.length === 1 + 2 * r) return {
150
145
  x: t.subarray(1, 1 + r),
151
146
  y: t.subarray(1 + r)
152
147
  };
153
148
  if ((i === 2 || i === 3) && t.length === 1 + r) {
154
- if (!n.acceptCompressedEcPoints) return C("unsupported", "Compressed ECDSA points need acceptCompressedEcPoints");
149
+ if (!n.acceptCompressedEcPoints) return w("unsupported", "Compressed ECDSA points need acceptCompressedEcPoints");
155
150
  let r = t.subarray(1);
156
151
  return {
157
152
  x: r,
158
153
  y: decompressPoint(e, i, r)
159
154
  };
160
155
  }
161
- return C("malformed", "ECDSA point encoding is invalid");
156
+ return w("malformed", "ECDSA point encoding is invalid");
162
157
  }
163
158
  function readRsaPublic(e, t, n) {
164
- let r = e.mpint(k), i = e.mpint(k), [a, o] = t === "e-n" ? [r, i] : [i, r], s = bitLength(o);
165
- if (s > j) return C("limit_exceeded", `RSA modulus of ${s} bits exceeds ${j}`);
166
- if (s < n.minRsaModulusBits) return C("unsupported", `RSA modulus of ${s} bits is below ${n.minRsaModulusBits}`);
167
- let c = b(a);
168
- return c < 3n || (c & 1n) == 0n || c >= b(o) ? C("malformed", "RSA public exponent is invalid") : {
159
+ let r = e.mpint(A), i = e.mpint(A), [a, o] = t === "e-n" ? [r, i] : [i, r], s = bitLength(o);
160
+ if (s > M) return w("limit_exceeded", `RSA modulus of ${s} bits exceeds ${M}`);
161
+ if (s < n.minRsaModulusBits) return w("unsupported", `RSA modulus of ${s} bits is below ${n.minRsaModulusBits}`);
162
+ let c = x(a);
163
+ return c < 3n || (c & 1n) == 0n || c >= x(o) ? w("malformed", "RSA public exponent is invalid") : {
169
164
  kind: "rsa",
170
165
  e: a,
171
166
  n: o
172
167
  };
173
168
  }
174
169
  function readEcdsaPublic(e, t, n) {
175
- return e.name() === F[t].identifier ? {
170
+ return e.name() === I[t].identifier ? {
176
171
  kind: "ecdsa",
177
172
  curve: t,
178
- ...readPoint(t, e.string(A), n)
179
- } : C("key_mismatch", "ECDSA curve identifier disagrees with the key type");
173
+ ...readPoint(t, e.string(j), n)
174
+ } : w("key_mismatch", "ECDSA curve identifier disagrees with the key type");
180
175
  }
181
176
  function readEd25519Public(e) {
182
- let t = e.string(A);
177
+ let t = e.string(j);
183
178
  return t.length === 32 ? {
184
179
  kind: "ed25519",
185
180
  x: t
186
- } : C("malformed", "Ed25519 public key is not 32 bytes");
181
+ } : w("malformed", "Ed25519 public key is not 32 bytes");
187
182
  }
188
183
  function decodePublicBlob(e, t) {
189
- let n = x(e), r = asSshKeyType(n.name());
190
- if (r === void 0) return C("unsupported", "Unsupported SSH key type");
184
+ let n = S(e), r = asSshKeyType(n.name());
185
+ if (r === void 0) return w("unsupported", "Unsupported SSH key type");
191
186
  let i;
192
187
  switch (r) {
193
188
  case "ssh-rsa":
@@ -199,38 +194,38 @@ function decodePublicBlob(e, t) {
199
194
  case "ecdsa-sha2-nistp256":
200
195
  case "ecdsa-sha2-nistp384":
201
196
  case "ecdsa-sha2-nistp521":
202
- i = readEcdsaPublic(n, L[r], t);
197
+ i = readEcdsaPublic(n, R[r], t);
203
198
  break;
204
199
  default: return unhandledKey(r);
205
200
  }
206
- return n.remaining() === 0 ? i : C("malformed", "SSH public key has trailing data");
201
+ return n.remaining() === 0 ? i : w("malformed", "SSH public key has trailing data");
207
202
  }
208
203
  function ecPoint(e) {
209
- return o([
204
+ return t([
210
205
  Uint8Array.of(4),
211
206
  e.x,
212
207
  e.y
213
208
  ]);
214
209
  }
215
210
  function encodePublicBlob(e) {
216
- let t = E(keyTypeOf(e));
211
+ let n = D(keyTypeOf(e));
217
212
  switch (e.kind) {
218
- case "rsa": return o([
219
- t,
220
- w(e.e),
221
- w(e.n)
213
+ case "rsa": return t([
214
+ n,
215
+ T(e.e),
216
+ T(e.n)
222
217
  ]);
223
- case "ed25519": return o([t, T(e.x)]);
224
- case "ecdsa": return o([
225
- t,
226
- E(F[e.curve].identifier),
227
- T(ecPoint(e))
218
+ case "ed25519": return t([n, E(e.x)]);
219
+ case "ecdsa": return t([
220
+ n,
221
+ D(I[e.curve].identifier),
222
+ E(ecPoint(e))
228
223
  ]);
229
224
  default: return unhandledKey(e);
230
225
  }
231
226
  }
232
227
  function samePublicMaterial(e, t) {
233
- return y(encodePublicBlob(e), encodePublicBlob(t));
228
+ return b(encodePublicBlob(e), encodePublicBlob(t));
234
229
  }
235
230
  function publicJwk(e) {
236
231
  switch (e.kind) {
@@ -256,20 +251,20 @@ function publicJwk(e) {
256
251
  function privateJwk(e) {
257
252
  switch (e.kind) {
258
253
  case "rsa": {
259
- let t = b(e.d), n = b(e.p), r = b(e.q);
254
+ let t = x(e.d), n = x(e.p), r = x(e.q);
260
255
  return {
261
256
  ...publicJwk(e.pub),
262
257
  d: toBase64Url(e.d),
263
258
  p: toBase64Url(e.p),
264
259
  q: toBase64Url(e.q),
265
- dp: toBase64Url(v(t % (n - 1n))),
266
- dq: toBase64Url(v(t % (r - 1n))),
260
+ dp: toBase64Url(y(t % (n - 1n))),
261
+ dq: toBase64Url(y(t % (r - 1n))),
267
262
  qi: toBase64Url(e.iqmp)
268
263
  };
269
264
  }
270
265
  case "ecdsa": return {
271
266
  ...publicJwk(e.pub),
272
- d: toBase64Url(v(b(e.d), F[e.pub.curve].size))
267
+ d: toBase64Url(y(x(e.d), I[e.pub.curve].size))
273
268
  };
274
269
  case "ed25519": return {
275
270
  ...publicJwk(e.pub),
@@ -301,76 +296,76 @@ function signParams(e, t) {
301
296
  } : { name: "RSASSA-PKCS1-v1_5" };
302
297
  case "ecdsa": return {
303
298
  name: "ECDSA",
304
- hash: F[e.curve].hash
299
+ hash: I[e.curve].hash
305
300
  };
306
301
  case "ed25519": return { name: "Ed25519" };
307
302
  default: return unhandledKey(e);
308
303
  }
309
304
  }
310
305
  async function importPublicMaterial(e, t) {
311
- return await g(publicJwk(e), importInputFor(e, t));
306
+ return await l(publicJwk(e), importInputFor(e, t));
312
307
  }
313
- async function importPrivateMaterial(e, t, r) {
308
+ async function importPrivateMaterial(e, t, n) {
314
309
  try {
315
- return await h(privateJwk(e), importInputFor(t, r));
310
+ return await c(privateJwk(e), importInputFor(t, n));
316
311
  } catch (e) {
317
- return n(e), C("key_mismatch", "Private key material is invalid for the embedded public key");
312
+ return m(e), w("key_mismatch", "Private key material is invalid for the embedded public key");
318
313
  }
319
314
  }
320
- async function assertKeyPairConsistent(e, t, n, r) {
321
- let { subtle: i } = c(), a = c().getRandomValues(/* @__PURE__ */ new Uint8Array(32)), o = signParams(n, r), s = await i.sign(o, e, a);
322
- await i.verify(o, t, s, a) || C("key_mismatch", "Private key does not match the embedded public key");
315
+ async function assertKeyPairConsistent(e, t, n, i) {
316
+ let { subtle: a } = r(), o = r().getRandomValues(/* @__PURE__ */ new Uint8Array(32)), s = signParams(n, i), c = await a.sign(s, e, o);
317
+ await a.verify(s, t, c, o) || w("key_mismatch", "Private key does not match the embedded public key");
323
318
  }
324
- async function sha256Fingerprint(e) {
325
- let t = new Uint8Array(await c().subtle.digest("SHA-256", s(e)));
326
- return `SHA256:${u(t).replace(/=+$/, "")}`;
319
+ async function sha256Fingerprint(t) {
320
+ let n = new Uint8Array(await r().subtle.digest("SHA-256", e(t)));
321
+ return `SHA256:${a(n).replace(/=+$/, "")}`;
327
322
  }
328
323
  function textOf(e, t) {
329
- return typeof e == "string" ? e.length > t || new TextEncoder().encode(e).length > t ? C("limit_exceeded", `SSH key input exceeds ${t} bytes`) : e : e.length > t ? C("limit_exceeded", `SSH key input exceeds ${t} bytes`) : S(e);
324
+ return typeof e == "string" ? e.length > t || new TextEncoder().encode(e).length > t ? w("limit_exceeded", `SSH key input exceeds ${t} bytes`) : e : e.length > t ? w("limit_exceeded", `SSH key input exceeds ${t} bytes`) : C(e);
330
325
  }
331
- const R = /^([^ \t]+)[ \t]+([A-Za-z0-9+/]+={0,2})(?:[ \t]+(.*))?$/s;
326
+ const z = /^([^ \t]+)[ \t]+([A-Za-z0-9+/]+={0,2})(?:[ \t]+(.*))?$/s;
332
327
  async function decodePublicKeyLine(e, t) {
333
328
  let n = textOf(e, 16384).replace(/\r?\n$/, "");
334
- if (/[\r\n]/.test(n)) return C("malformed", "SSH public key must be a single line");
335
- let r = R.exec(n);
336
- if (r === null) return C("malformed", "SSH public key line is not \"<type> <base64> [comment]\"");
337
- let [, i = "", a = "", o = ""] = r;
338
- assertImportedComment(o);
339
- let s = asSshKeyType(i);
340
- if (s === void 0) return C("unsupported", "Unsupported SSH key type");
341
- let c = decodePublicBlob(l(a), t);
342
- return keyTypeOf(c) === s ? {
343
- keyType: s,
344
- algorithm: algorithmOf(c),
345
- publicKey: await importPublicMaterial(c, t),
346
- comment: o,
347
- fingerprint: await sha256Fingerprint(encodePublicBlob(c))
348
- } : C("key_mismatch", "Line key type disagrees with the key blob");
329
+ if (/[\r\n]/.test(n)) return w("malformed", "SSH public key must be a single line");
330
+ let r = z.exec(n);
331
+ if (r === null) return w("malformed", "SSH public key line is not \"<type> <base64> [comment]\"");
332
+ let [, a = "", o = "", s = ""] = r;
333
+ assertImportedComment(s);
334
+ let c = asSshKeyType(a);
335
+ if (c === void 0) return w("unsupported", "Unsupported SSH key type");
336
+ let l = decodePublicBlob(i(o), t);
337
+ return keyTypeOf(l) === c ? {
338
+ keyType: c,
339
+ algorithm: algorithmOf(l),
340
+ publicKey: await importPublicMaterial(l, t),
341
+ comment: s,
342
+ fingerprint: await sha256Fingerprint(encodePublicBlob(l))
343
+ } : w("key_mismatch", "Line key type disagrees with the key blob");
349
344
  }
350
345
  function readPrivateMaterial(e, t, n) {
351
- if (e.name() !== t) return C("key_mismatch", "Private section key type disagrees with the public key");
346
+ if (e.name() !== t) return w("key_mismatch", "Private section key type disagrees with the public key");
352
347
  switch (t) {
353
348
  case "ssh-rsa": return {
354
349
  kind: "rsa",
355
350
  pub: readRsaPublic(e, "n-e", n),
356
- d: e.mpint(k),
357
- iqmp: e.mpint(k),
358
- p: e.mpint(k),
359
- q: e.mpint(k)
351
+ d: e.mpint(A),
352
+ iqmp: e.mpint(A),
353
+ p: e.mpint(A),
354
+ q: e.mpint(A)
360
355
  };
361
356
  case "ssh-ed25519": {
362
- let t = readEd25519Public(e), n = e.string(A);
363
- return n.length === 64 ? y(n.subarray(32), t.x) ? {
357
+ let t = readEd25519Public(e), n = e.string(j);
358
+ return n.length === 64 ? b(n.subarray(32), t.x) ? {
364
359
  kind: "ed25519",
365
360
  pub: t,
366
361
  seed: n.subarray(0, 32)
367
- } : C("key_mismatch", "Ed25519 private key suffix differs from the public key") : C("malformed", "Ed25519 private key is not 64 bytes");
362
+ } : w("key_mismatch", "Ed25519 private key suffix differs from the public key") : w("malformed", "Ed25519 private key is not 64 bytes");
368
363
  }
369
364
  case "ecdsa-sha2-nistp256":
370
365
  case "ecdsa-sha2-nistp384":
371
366
  case "ecdsa-sha2-nistp521": {
372
- let r = readEcdsaPublic(e, L[t], n), i = e.mpint(k);
373
- return i.length > F[r.curve].size ? C("malformed", "ECDSA private scalar is larger than the curve order") : {
367
+ let r = readEcdsaPublic(e, R[t], n), i = e.mpint(A);
368
+ return i.length > I[r.curve].size ? w("malformed", "ECDSA private scalar is larger than the curve order") : {
374
369
  kind: "ecdsa",
375
370
  pub: r,
376
371
  d: i
@@ -380,17 +375,17 @@ function readPrivateMaterial(e, t, n) {
380
375
  }
381
376
  }
382
377
  function assertRsaConsistent(e) {
383
- let t = b(e.pub.n), n = b(e.pub.e), r = b(e.d), i = b(e.p), a = b(e.q), o = b(e.iqmp);
384
- (i < 3n || a < 3n || i * a !== t) && C("key_mismatch", "RSA primes do not multiply to the modulus"), (r >= t || r * n % (i - 1n) != 1n || r * n % (a - 1n) != 1n) && C("key_mismatch", "RSA private exponent does not match the public exponent"), (o >= i || o * a % i !== 1n) && C("key_mismatch", "RSA coefficient is not the inverse of q modulo p");
378
+ let t = x(e.pub.n), n = x(e.pub.e), r = x(e.d), i = x(e.p), a = x(e.q), o = x(e.iqmp);
379
+ (i < 3n || a < 3n || i * a !== t) && w("key_mismatch", "RSA primes do not multiply to the modulus"), (r >= t || r * n % (i - 1n) != 1n || r * n % (a - 1n) != 1n) && w("key_mismatch", "RSA private exponent does not match the public exponent"), (o >= i || o * a % i !== 1n) && w("key_mismatch", "RSA coefficient is not the inverse of q modulo p");
385
380
  }
386
381
  function assertPadding(e) {
387
382
  let t = e.raw(e.remaining());
388
- t.length >= 8 && C("malformed", "OpenSSH private key padding is too long"), t.forEach((e, t) => {
389
- e !== t + 1 && C("malformed", "OpenSSH private key padding is invalid");
383
+ t.length >= 8 && w("malformed", "OpenSSH private key padding is too long"), t.forEach((e, t) => {
384
+ e !== t + 1 && w("malformed", "OpenSSH private key padding is invalid");
390
385
  });
391
386
  }
392
387
  async function decodePrivateKey(e, t) {
393
- let n = textOf(e, O), r = d(M, n);
388
+ let n = textOf(e, k), r = u(N, n);
394
389
  try {
395
390
  return await decodePrivateBinary(r, t);
396
391
  } finally {
@@ -398,21 +393,21 @@ async function decodePrivateKey(e, t) {
398
393
  }
399
394
  }
400
395
  async function decodePrivateBinary(e, t) {
401
- let n = x(e), r = n.raw(N.length + 1);
402
- if (!y(r.subarray(0, N.length), N) || r[N.length] !== 0) return C("malformed", "Missing openssh-key-v1 magic");
396
+ let n = S(e), r = n.raw(P.length + 1);
397
+ if (!b(r.subarray(0, P.length), P) || r[P.length] !== 0) return w("malformed", "Missing openssh-key-v1 magic");
403
398
  let i = n.name(), a = n.name();
404
- if (i !== "none" || a !== "none") return C("unsupported", "Encrypted OpenSSH private keys are not supported");
405
- if (n.string(A).length !== 0) return C("malformed", "KDF options must be empty for kdf none");
399
+ if (i !== "none" || a !== "none") return w("unsupported", "Encrypted OpenSSH private keys are not supported");
400
+ if (n.string(j).length !== 0) return w("malformed", "KDF options must be empty for kdf none");
406
401
  let o = n.uint32();
407
- if (o === 0) return C("malformed", "OpenSSH private key file holds no keys");
408
- if (o > 1) return C("unsupported", "OpenSSH private key files with several keys are not supported");
409
- let s = decodePublicBlob(n.string(A), t), c = n.string(O);
410
- if (n.remaining() !== 0) return C("malformed", "OpenSSH private key file has trailing data");
411
- if (c.length === 0 || c.length % 8 != 0) return C("malformed", "OpenSSH private section is not a multiple of the block size");
412
- let l = x(c);
413
- if (l.uint32() !== l.uint32()) return C("malformed", "OpenSSH check integers differ");
414
- let u = readPrivateMaterial(l, keyTypeOf(s), t), d = l.text(A);
415
- if (assertImportedComment(d), assertPadding(l), !samePublicMaterial(s, u.pub)) return C("key_mismatch", "Private section public key differs from the file public key");
402
+ if (o === 0) return w("malformed", "OpenSSH private key file holds no keys");
403
+ if (o > 1) return w("unsupported", "OpenSSH private key files with several keys are not supported");
404
+ let s = decodePublicBlob(n.string(j), t), c = n.string(k);
405
+ if (n.remaining() !== 0) return w("malformed", "OpenSSH private key file has trailing data");
406
+ if (c.length === 0 || c.length % 8 != 0) return w("malformed", "OpenSSH private section is not a multiple of the block size");
407
+ let l = S(c);
408
+ if (l.uint32() !== l.uint32()) return w("malformed", "OpenSSH check integers differ");
409
+ let u = readPrivateMaterial(l, keyTypeOf(s), t), d = l.text(j);
410
+ if (assertImportedComment(d), assertPadding(l), !samePublicMaterial(s, u.pub)) return w("key_mismatch", "Private section public key differs from the file public key");
416
411
  u.kind === "rsa" && assertRsaConsistent(u);
417
412
  let f = await importPublicMaterial(s, t), p = await importPrivateMaterial(u, s, t);
418
413
  return await assertKeyPairConsistent(p, f, s, t), {
@@ -424,15 +419,15 @@ async function decodePrivateBinary(e, t) {
424
419
  fingerprint: await sha256Fingerprint(encodePublicBlob(s))
425
420
  };
426
421
  }
427
- async function toSshResult(i) {
422
+ async function toSshResult(e) {
428
423
  try {
429
- return r(await i());
430
- } catch (r) {
431
- if (t(r)) {
432
- let t = _(r.code);
433
- if (t !== void 0) return e(t, r.error.message);
424
+ return h(await e());
425
+ } catch (e) {
426
+ if (p(e)) {
427
+ let t = v(e.code);
428
+ if (t !== void 0) return f(t, e.error.message);
434
429
  }
435
- return n(r), e("malformed", r instanceof Error ? r.message : "Malformed SSH key");
430
+ return m(e), f("malformed", e instanceof Error ? e.message : "Malformed SSH key");
436
431
  }
437
432
  }
438
433
  /**
@@ -465,7 +460,7 @@ function importSshPublicKey(e, t = {}) {
465
460
  * @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
466
461
  */
467
462
  async function importSshPublicKeyOrThrow(e, t = {}) {
468
- return a(await importSshPublicKey(e, t));
463
+ return _(await importSshPublicKey(e, t));
469
464
  }
470
465
  /**
471
466
  * Import an unencrypted single-key `openssh-key-v1` private key file.
@@ -505,19 +500,19 @@ function importOpenSshPrivateKey(e, t = {}) {
505
500
  * @throws {ResultError} Carrying an {@linkcode SshErrorCode} as `code`
506
501
  */
507
502
  async function importOpenSshPrivateKeyOrThrow(e, t = {}) {
508
- return a(await importOpenSshPrivateKey(e, t));
503
+ return _(await importOpenSshPrivateKey(e, t));
509
504
  }
510
505
  function exportFail(e, t) {
511
- return i(e, t);
506
+ return g(e, t);
512
507
  }
513
508
  function validCommentText(e) {
514
509
  return typeof e == "string" && e.isWellFormed() && !/[\r\n\0]/.test(e);
515
510
  }
516
511
  function oversizedComment(e) {
517
- return e.length > A || new TextEncoder().encode(e).length > A;
512
+ return e.length > j || new TextEncoder().encode(e).length > j;
518
513
  }
519
514
  function assertImportedComment(e) {
520
- validCommentText(e) || C("malformed", "Comment must be well-formed Unicode without CR, LF or NUL"), oversizedComment(e) && C("limit_exceeded", "Comment exceeds 8192 UTF-8 bytes");
515
+ validCommentText(e) || w("malformed", "Comment must be well-formed Unicode without CR, LF or NUL"), oversizedComment(e) && w("limit_exceeded", "Comment exceeds 8192 UTF-8 bytes");
521
516
  }
522
517
  function assertComment(e) {
523
518
  (!validCommentText(e) || oversizedComment(e)) && exportFail("invalid_comment", "Comment must be well-formed Unicode without CR, LF or NUL and at most 8192 UTF-8 bytes");
@@ -541,7 +536,7 @@ function publicMaterialFromJwk(e, t) {
541
536
  x: jwkBytes(e, "x")
542
537
  };
543
538
  if (e.kty === "EC" && t === "ECDSA") {
544
- let t = I.find((t) => t === e.crv);
539
+ let t = L.find((t) => t === e.crv);
545
540
  if (t !== void 0) return {
546
541
  kind: "ecdsa",
547
542
  curve: t,
@@ -566,7 +561,7 @@ function publicMaterialFromJwk(e, t) {
566
561
  async function exportSshPublicKey(e, t = {}) {
567
562
  let n = t.comment === void 0 ? "" : t.comment;
568
563
  assertComment(n), assertExportable(e, "public");
569
- let r = publicMaterialFromJwk(await m(e), e.algorithm.name), i = `${keyTypeOf(r)} ${u(encodePublicBlob(r))}`;
564
+ let r = publicMaterialFromJwk(await s(e), e.algorithm.name), i = `${keyTypeOf(r)} ${a(encodePublicBlob(r))}`;
570
565
  return n === "" ? i : `${i} ${n}`;
571
566
  }
572
567
  /**
@@ -585,47 +580,47 @@ async function exportSshPublicKey(e, t = {}) {
585
580
  *
586
581
  * @see {@linkcode importOpenSshPrivateKey} for the inverse operation
587
582
  */
588
- async function exportOpenSshPrivateKey(e, t = {}) {
589
- let n = t.comment === void 0 ? "" : t.comment;
590
- assertComment(n), assertExportable(e, "private");
591
- let r = await p(e), i = publicMaterialFromJwk(r, e.algorithm.name), a = privateFields(i, r), s = c().getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0, l = o([
592
- D(s),
593
- D(s),
594
- E(keyTypeOf(i)),
595
- a,
596
- E(n)
597
- ]), u = (8 - l.length % 8) % 8, d = o([l, Uint8Array.from({ length: u }, (e, t) => t + 1)]), m = o([
598
- N,
583
+ async function exportOpenSshPrivateKey(e, n = {}) {
584
+ let i = n.comment === void 0 ? "" : n.comment;
585
+ assertComment(i), assertExportable(e, "private");
586
+ let a = await o(e), s = publicMaterialFromJwk(a, e.algorithm.name), c = privateFields(s, a), l = r().getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0, u = t([
587
+ O(l),
588
+ O(l),
589
+ D(keyTypeOf(s)),
590
+ c,
591
+ D(i)
592
+ ]), f = (8 - u.length % 8) % 8, p = t([u, Uint8Array.from({ length: f }, (e, t) => t + 1)]), m = t([
593
+ P,
599
594
  Uint8Array.of(0),
600
- E("none"),
601
- E("none"),
602
- D(0),
603
- D(1),
604
- T(encodePublicBlob(i)),
605
- T(d)
595
+ D("none"),
596
+ D("none"),
597
+ O(0),
598
+ O(1),
599
+ E(encodePublicBlob(s)),
600
+ E(p)
606
601
  ]);
607
602
  try {
608
- return f(M, m);
603
+ return d(N, m);
609
604
  } finally {
610
- l.fill(0), d.fill(0), m.fill(0), a.fill(0);
605
+ u.fill(0), p.fill(0), m.fill(0), c.fill(0);
611
606
  }
612
607
  }
613
- function privateFields(e, t) {
608
+ function privateFields(e, n) {
614
609
  switch (e.kind) {
615
- case "rsa": return o([
616
- w(e.n),
617
- w(e.e),
618
- w(jwkBytes(t, "d")),
619
- w(jwkBytes(t, "qi")),
620
- w(jwkBytes(t, "p")),
621
- w(jwkBytes(t, "q"))
610
+ case "rsa": return t([
611
+ T(e.n),
612
+ T(e.e),
613
+ T(jwkBytes(n, "d")),
614
+ T(jwkBytes(n, "qi")),
615
+ T(jwkBytes(n, "p")),
616
+ T(jwkBytes(n, "q"))
622
617
  ]);
623
- case "ecdsa": return o([
624
- E(F[e.curve].identifier),
625
- T(ecPoint(e)),
626
- w(jwkBytes(t, "d"))
618
+ case "ecdsa": return t([
619
+ D(I[e.curve].identifier),
620
+ E(ecPoint(e)),
621
+ T(jwkBytes(n, "d"))
627
622
  ]);
628
- case "ed25519": return o([T(e.x), T(o([jwkBytes(t, "d"), e.x]))]);
623
+ case "ed25519": return t([E(e.x), E(t([jwkBytes(n, "d"), e.x]))]);
629
624
  default: return unhandledKey(e);
630
625
  }
631
626
  }
@@ -641,7 +636,7 @@ function privateFields(e, t) {
641
636
  * @throws {ResultError} Carrying an {@linkcode SshExportErrorCode} when a `CryptoKey` cannot be encoded
642
637
  */
643
638
  async function sshFingerprintSha256(e) {
644
- return e instanceof Uint8Array ? await sha256Fingerprint(e) : (e.type === "secret" && exportFail("invalid_key", "Expected a public or private key"), assertExportable(e, e.type), await sha256Fingerprint(encodePublicBlob(publicMaterialFromJwk(e.type === "private" ? await p(e) : await m(e), e.algorithm.name))));
639
+ return e instanceof Uint8Array ? await sha256Fingerprint(e) : (e.type === "secret" && exportFail("invalid_key", "Expected a public or private key"), assertExportable(e, e.type), await sha256Fingerprint(encodePublicBlob(publicMaterialFromJwk(e.type === "private" ? await o(e) : await s(e), e.algorithm.name))));
645
640
  }
646
641
  export { exportOpenSshPrivateKey, exportSshPublicKey, importOpenSshPrivateKey, importOpenSshPrivateKeyOrThrow, importSshPublicKey, importSshPublicKeyOrThrow, sshFingerprintSha256 };
647
642