typevault 0.9.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typevault",
3
- "version": "0.9.1",
3
+ "version": "0.10.0",
4
4
  "description": "CLI-first secrets vault for agents",
5
5
  "homepage": "https://github.com/typeclaw/typevault#readme",
6
6
  "bugs": {
@@ -3,7 +3,7 @@ import { TypeVaultError } from './errors'
3
3
 
4
4
  export type FieldType = 'secret' | 'text' | 'totp' | 'oauth' | 'passkey'
5
5
  /**
6
- * A WebAuthn credential. The ids and the PKCS#8 P-256 private key are base64url without padding. A discoverable
6
+ * A WebAuthn credential. The ids and the PKCS#8 private key (P-256, Ed25519 or RSA, as the site chose) are base64url without padding. A discoverable
7
7
  * credential (a site can find it with no allow-list, for username-less sign-in) always has its user handle.
8
8
  */
9
9
  export type Passkey = {
@@ -8,6 +8,16 @@ const P256_ALGORITHM = hex.decode('301306072a8648ce3d020106082a8648ce3d030107')
8
8
  // The prime256v1 OID, as ECPrivateKey's optional parameters repeat it.
9
9
  const P256_CURVE = hex.decode('06082a8648ce3d030107')
10
10
 
11
+ // An Ed25519 key in PKCS#8 as RFC 8410 and Chromium write it: version 0, id-Ed25519, then the 32-byte seed.
12
+ const ED25519_PREFIX = hex.decode('302e020100300506032b657004220420')
13
+ // The AlgorithmIdentifier of an RSA key: rsaEncryption, then NULL parameters.
14
+ const RSA_ALGORITHM = hex.decode('300d06092a864886f70d0101010500')
15
+ // BoringSSL, which Chromium's virtual authenticator imports keys with, takes RSA moduli of 512 to 16384 bits and
16
+ // public exponents of at most 33 bits.
17
+ const MIN_RSA_BITS = 512
18
+ const MAX_RSA_BITS = 16_384
19
+ const MAX_RSA_EXPONENT_BITS = 33
20
+
11
21
  // Chrome DevTools restores a credential's signature counter only up to UINT32_MAX.
12
22
  export const MAX_SIGN_COUNT = 4_294_967_295
13
23
  // WebAuthn, and Chrome DevTools with it, caps a user handle at 64 bytes.
@@ -27,6 +37,78 @@ export function decodeBase64Url(text: string): Uint8Array | undefined {
27
37
  }
28
38
  }
29
39
 
40
+ /**
41
+ * True for a private key Chromium's virtual authenticator creates and imports: ES256 (P-256), EdDSA (Ed25519) or
42
+ * RS256 (RSA), each in PKCS#8. A site picks the algorithm, so a passkey may hold any of the three.
43
+ */
44
+ export function isPasskeyPrivateKey(pkcs8: Uint8Array): boolean {
45
+ return isP256PrivateKey(pkcs8) || isEd25519PrivateKey(pkcs8) || isRsaPrivateKey(pkcs8)
46
+ }
47
+
48
+ function isEd25519PrivateKey(pkcs8: Uint8Array): boolean {
49
+ return pkcs8.length === ED25519_PREFIX.length + 32 && equal(pkcs8.subarray(0, ED25519_PREFIX.length), ED25519_PREFIX)
50
+ }
51
+
52
+ // A two-prime RSA key in DER as Chromium's BoringSSL imports it: rsaEncryption with NULL parameters, then an
53
+ // RSAPrivateKey of version 0 and eight minimal positive integers, nothing left over, and numbers that agree with one
54
+ // another the way BoringSSL's own key check requires. Pure, so the browser prompt page can bundle it.
55
+ function isRsaPrivateKey(pkcs8: Uint8Array): boolean {
56
+ const outer = tlv(pkcs8, 0)
57
+ if (outer?.tag !== 0x30 || outer.next !== pkcs8.length) return false
58
+ const version = tlv(pkcs8, outer.start)
59
+ if (version?.tag !== 0x02 || !equal(pkcs8.subarray(version.start, version.next), [0x00])) return false
60
+ const algorithm = tlv(pkcs8, version.next)
61
+ if (algorithm?.tag !== 0x30 || !equal(pkcs8.subarray(version.next, algorithm.next), RSA_ALGORITHM)) return false
62
+ const wrapped = tlv(pkcs8, algorithm.next)
63
+ if (wrapped?.tag !== 0x04 || wrapped.next !== outer.next) return false
64
+ const rsaKey = tlv(pkcs8, wrapped.start)
65
+ if (rsaKey?.tag !== 0x30 || rsaKey.next !== wrapped.next) return false
66
+ const numbers: bigint[] = []
67
+ let at = rsaKey.start
68
+ for (let i = 0; i < 9; i++) {
69
+ const field = tlv(pkcs8, at)
70
+ if (field?.tag !== 0x02 || field.next > rsaKey.next) return false
71
+ const value = positiveInteger(pkcs8.subarray(field.start, field.next), i === 0)
72
+ if (value === undefined) return false
73
+ numbers.push(value)
74
+ at = field.next
75
+ }
76
+ if (at !== rsaKey.next) return false
77
+ const [keyVersion, n, e, d, p, q, dp, dq, qi] = numbers as [
78
+ bigint,
79
+ bigint,
80
+ bigint,
81
+ bigint,
82
+ bigint,
83
+ bigint,
84
+ bigint,
85
+ bigint,
86
+ bigint,
87
+ ]
88
+ const bits = n.toString(2).length
89
+ if (keyVersion !== 0n || bits < MIN_RSA_BITS || bits > MAX_RSA_BITS || n % 2n === 0n) return false
90
+ if (e.toString(2).length > MAX_RSA_EXPONENT_BITS || d >= n) return false
91
+ if (p <= 1n || q <= 1n || p * q !== n || e < 3n || e % 2n === 0n) return false
92
+ return (
93
+ (e * d) % (p - 1n) === 1n % (p - 1n) &&
94
+ (e * d) % (q - 1n) === 1n % (q - 1n) &&
95
+ dp === d % (p - 1n) &&
96
+ dq === d % (q - 1n) &&
97
+ qi < p &&
98
+ (qi * q) % p === 1n
99
+ )
100
+ }
101
+
102
+ // A DER INTEGER's content as a non-negative number: minimal (no needless leading zero byte) and not negative. Only the
103
+ // version may be zero.
104
+ function positiveInteger(content: Uint8Array, mayBeZero: boolean): bigint | undefined {
105
+ const first = content[0]
106
+ if (first === undefined || first >= 0x80) return undefined
107
+ if (first === 0x00 && content.length > 1 && (content[1] ?? 0) < 0x80) return undefined
108
+ const value = BigInt(`0x${hex.encode(content)}`)
109
+ return value === 0n && !mayBeZero ? undefined : value
110
+ }
111
+
30
112
  /**
31
113
  * True for a PKCS#8 ECDSA P-256 private key that Chromium's virtual authenticator imports: BoringSSL's parser reads
32
114
  * every field, so the whole structure is checked, not just the scalar. That is a 32-byte scalar in range, the curve
@@ -3,7 +3,7 @@ import { z } from 'zod'
3
3
  import { fromUtf8, utf8 } from './encoding'
4
4
  import { TypeVaultError } from './errors'
5
5
  import { type EntryName, type Field, isFieldName, isUsableEnvName, parseEntryName } from './naming'
6
- import { decodeBase64Url, isP256PrivateKey, isUserHandle, MAX_SIGN_COUNT } from './passkey'
6
+ import { decodeBase64Url, isPasskeyPrivateKey, isUserHandle, MAX_SIGN_COUNT } from './passkey'
7
7
 
8
8
  /** The plaintext sealed inside an entry file: everything except the tier is secret. */
9
9
  export type EntryPayload = {
@@ -45,8 +45,8 @@ export const FieldSchema = z.discriminatedUnion('type', [
45
45
  credential_id: Base64UrlSchema,
46
46
  private_key: z.string().refine((key) => {
47
47
  const bytes = decodeBase64Url(key)
48
- return bytes !== undefined && isP256PrivateKey(bytes)
49
- }, 'expected a PKCS#8 P-256 private key in base64url'),
48
+ return bytes !== undefined && isPasskeyPrivateKey(bytes)
49
+ }, 'expected a PKCS#8 P-256, Ed25519 or RSA private key in base64url'),
50
50
  discoverable: z.boolean(),
51
51
  user_handle: z
52
52
  .string()