@andrewheberle/serverless-ssh-ca 0.22.0 → 0.23.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/README.md +55 -1
- package/dist/api/operations.d.ts +49 -0
- package/dist/api/operations.d.ts.map +1 -0
- package/dist/api/operations.js +175 -0
- package/dist/api/operations.js.map +1 -0
- package/dist/api/schema.d.ts +71 -0
- package/dist/api/schema.d.ts.map +1 -0
- package/dist/api/schema.js +107 -0
- package/dist/api/schema.js.map +1 -0
- package/dist/api/v3/index.d.ts.map +1 -1
- package/dist/api/v3/index.js +13 -202
- package/dist/api/v3/index.js.map +1 -1
- package/dist/api/v3/schema.d.ts +23 -68
- package/dist/api/v3/schema.d.ts.map +1 -1
- package/dist/api/v3/schema.js +10 -100
- package/dist/api/v3/schema.js.map +1 -1
- package/dist/api/v4/index.d.ts +8 -0
- package/dist/api/v4/index.d.ts.map +1 -0
- package/dist/api/v4/index.js +155 -0
- package/dist/api/v4/index.js.map +1 -0
- package/dist/api/v4/schema.d.ts +268 -0
- package/dist/api/v4/schema.d.ts.map +1 -0
- package/dist/api/v4/schema.js +182 -0
- package/dist/api/v4/schema.js.map +1 -0
- package/dist/certificate.d.ts +9 -6
- package/dist/certificate.d.ts.map +1 -1
- package/dist/certificate.js +55 -99
- package/dist/certificate.js.map +1 -1
- package/dist/db/index.d.ts +17 -5
- package/dist/db/index.d.ts.map +1 -1
- package/dist/db/index.js +39 -23
- package/dist/db/index.js.map +1 -1
- package/dist/krl.d.ts +1 -1
- package/dist/krl.d.ts.map +1 -1
- package/dist/krl.js +9 -163
- package/dist/krl.js.map +1 -1
- package/dist/proof.d.ts +36 -7
- package/dist/proof.d.ts.map +1 -1
- package/dist/proof.js +53 -21
- package/dist/proof.js.map +1 -1
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +3 -1
- package/dist/router.js.map +1 -1
- package/dist/ssh/certificate.d.ts +93 -0
- package/dist/ssh/certificate.d.ts.map +1 -0
- package/dist/ssh/certificate.js +178 -0
- package/dist/ssh/certificate.js.map +1 -0
- package/dist/ssh/encoding.d.ts +41 -0
- package/dist/ssh/encoding.d.ts.map +1 -0
- package/dist/ssh/encoding.js +70 -0
- package/dist/ssh/encoding.js.map +1 -0
- package/dist/ssh/keys.d.ts +80 -0
- package/dist/ssh/keys.d.ts.map +1 -0
- package/dist/ssh/keys.js +249 -0
- package/dist/ssh/keys.js.map +1 -0
- package/dist/ssh/signer.d.ts +35 -0
- package/dist/ssh/signer.d.ts.map +1 -0
- package/dist/ssh/signer.js +99 -0
- package/dist/ssh/signer.js.map +1 -0
- package/dist/types.d.ts +12 -17
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts +40 -21
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +87 -54
- package/dist/utils.js.map +1 -1
- package/openapi.json +1542 -1
- package/package.json +4 -3
- package/src/api/operations.ts +211 -0
- package/src/api/schema.ts +137 -0
- package/src/api/v3/index.ts +20 -248
- package/src/api/v3/schema.ts +26 -125
- package/src/api/v4/index.ts +265 -0
- package/src/api/v4/schema.ts +235 -0
- package/src/certificate.ts +62 -123
- package/src/db/index.ts +41 -26
- package/src/krl.ts +10 -195
- package/src/proof.ts +65 -31
- package/src/router.ts +3 -1
- package/src/ssh/certificate.ts +236 -0
- package/src/ssh/encoding.ts +81 -0
- package/src/ssh/keys.ts +294 -0
- package/src/ssh/signer.ts +129 -0
- package/src/types.ts +12 -18
- package/src/utils.ts +99 -69
package/src/ssh/keys.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha2.js"
|
|
2
|
+
import { base64, base64nopad, base64urlnopad } from "@scure/base"
|
|
3
|
+
import * as P from "micro-packed"
|
|
4
|
+
import { toFixedWidth } from "../sshsig/sig_parser.js"
|
|
5
|
+
import { decodePublicKey, SSHBytes, SSHText } from "./encoding.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Thrown when a key cannot be parsed
|
|
9
|
+
*/
|
|
10
|
+
export class KeyParseError extends Error {
|
|
11
|
+
constructor(message: string, cause?: unknown) {
|
|
12
|
+
super(message)
|
|
13
|
+
this.name = "KeyParseError"
|
|
14
|
+
this.cause = cause
|
|
15
|
+
|
|
16
|
+
Object.setPrototypeOf(this, KeyParseError.prototype)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Thrown when a private key is of a type that cannot be used to sign
|
|
22
|
+
*/
|
|
23
|
+
export class UnsupportedKeyError extends Error {
|
|
24
|
+
constructor(message: string) {
|
|
25
|
+
super(message)
|
|
26
|
+
this.name = "UnsupportedKeyError"
|
|
27
|
+
|
|
28
|
+
Object.setPrototypeOf(this, UnsupportedKeyError.prototype)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const equalBytes = (a: Uint8Array, b: Uint8Array): boolean => {
|
|
33
|
+
return a.length === b.length && a.every((v, i) => v === b[i])
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An SSH public key
|
|
38
|
+
*/
|
|
39
|
+
export class PublicKey {
|
|
40
|
+
/** The key type, for example "ssh-ed25519" */
|
|
41
|
+
readonly type: string
|
|
42
|
+
/** The wire encoding of the key (RFC 4253 section 6.6) */
|
|
43
|
+
readonly blob: Uint8Array
|
|
44
|
+
/** The comment of the key, if any */
|
|
45
|
+
readonly comment: string | undefined
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param blob The wire encoding of the key
|
|
49
|
+
* @param comment The comment of the key
|
|
50
|
+
* @throws {@link KeyParseError} when the key cannot be decoded or its type is not supported
|
|
51
|
+
*/
|
|
52
|
+
constructor(blob: Uint8Array, comment?: string) {
|
|
53
|
+
try {
|
|
54
|
+
this.type = decodePublicKey(blob).type
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw new KeyParseError(err instanceof Error ? err.message : "public key could not be decoded", err)
|
|
57
|
+
}
|
|
58
|
+
this.blob = blob
|
|
59
|
+
this.comment = comment === "" ? undefined : comment
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parses a public key in OpenSSH format, `${type} ${base64}[ ${comment}]`
|
|
64
|
+
*
|
|
65
|
+
* @param text The public key
|
|
66
|
+
* @throws {@link KeyParseError} when the key cannot be parsed or its type is not supported
|
|
67
|
+
*/
|
|
68
|
+
static parse(text: string): PublicKey {
|
|
69
|
+
const [type, data, ...comment] = text.trim().split(/\s+/)
|
|
70
|
+
if (type === undefined || type === "" || data === undefined) {
|
|
71
|
+
throw new KeyParseError("public key was not in OpenSSH format")
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let blob: Uint8Array
|
|
75
|
+
try {
|
|
76
|
+
blob = base64.decode(data)
|
|
77
|
+
} catch (err) {
|
|
78
|
+
throw new KeyParseError("public key was not valid base64", err)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const key = new PublicKey(blob, comment.join(" "))
|
|
82
|
+
if (key.type !== type) {
|
|
83
|
+
throw new KeyParseError(`public key type ${type} did not match encoded type ${key.type}`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return key
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the SHA256 fingerprint of the key, as shown by `ssh-keygen -l`,
|
|
91
|
+
* for example "SHA256:79mq0wtpQMTvS4+Of8VzLN0qmYWNUyTXYmqKwhEgSLs"
|
|
92
|
+
*/
|
|
93
|
+
fingerprint(): string {
|
|
94
|
+
return `SHA256:${base64nopad.encode(sha256(this.blob))}`
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns whether this is the same key as another, ignoring comments
|
|
99
|
+
*/
|
|
100
|
+
equals(other: PublicKey): boolean {
|
|
101
|
+
return equalBytes(this.blob, other.blob)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Returns a copy of the key with a different comment
|
|
106
|
+
*/
|
|
107
|
+
withComment(comment: string | undefined): PublicKey {
|
|
108
|
+
return new PublicKey(this.blob, comment)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Returns the key in OpenSSH format, `${type} ${base64}[ ${comment}]`
|
|
113
|
+
*/
|
|
114
|
+
toString(): string {
|
|
115
|
+
const key = `${this.type} ${base64.encode(this.blob)}`
|
|
116
|
+
|
|
117
|
+
return this.comment === undefined ? key : `${key} ${this.comment}`
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The ECDSA curves that can be used to sign */
|
|
122
|
+
export type EcdsaCurve = "nistp256" | "nistp384" | "nistp521"
|
|
123
|
+
|
|
124
|
+
const EcdsaCurves: Readonly<Record<EcdsaCurve, { namedCurve: string, size: number }>> = {
|
|
125
|
+
nistp256: { namedCurve: "P-256", size: 32 },
|
|
126
|
+
nistp384: { namedCurve: "P-384", size: 48 },
|
|
127
|
+
nistp521: { namedCurve: "P-521", size: 66 },
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A private key that can be used to sign, with its key material as a JWK for
|
|
132
|
+
* WebCrypto
|
|
133
|
+
*/
|
|
134
|
+
export type PrivateKey = {
|
|
135
|
+
readonly type: "ed25519"
|
|
136
|
+
readonly publicKey: PublicKey
|
|
137
|
+
readonly jwk: JsonWebKey
|
|
138
|
+
} | {
|
|
139
|
+
readonly type: "ecdsa"
|
|
140
|
+
readonly curve: EcdsaCurve
|
|
141
|
+
readonly publicKey: PublicKey
|
|
142
|
+
readonly jwk: JsonWebKey
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// "openssh-key-v1\0"
|
|
146
|
+
const AUTH_MAGIC = new TextEncoder().encode("openssh-key-v1\0")
|
|
147
|
+
|
|
148
|
+
/** The container of an OpenSSH private key (PROTOCOL.key) */
|
|
149
|
+
const OpenSshPrivateKey = P.struct({
|
|
150
|
+
magic: P.magicBytes(AUTH_MAGIC),
|
|
151
|
+
cipherName: SSHText,
|
|
152
|
+
kdfName: SSHText,
|
|
153
|
+
kdfOptions: SSHBytes,
|
|
154
|
+
publicKeys: P.array(P.U32BE, SSHBytes),
|
|
155
|
+
privateKeys: SSHBytes,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
/** The start of the (decrypted) private section of an OpenSSH private key */
|
|
159
|
+
const PrivateSectionStart = P.struct({
|
|
160
|
+
check1: P.U32BE,
|
|
161
|
+
check2: P.U32BE,
|
|
162
|
+
type: SSHText,
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const Ed25519PrivateKey = P.struct({
|
|
166
|
+
check1: P.U32BE,
|
|
167
|
+
check2: P.U32BE,
|
|
168
|
+
type: SSHText,
|
|
169
|
+
publicKey: SSHBytes,
|
|
170
|
+
// the seed followed by the public key
|
|
171
|
+
privateKey: SSHBytes,
|
|
172
|
+
comment: SSHText,
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
const EcdsaPrivateKey = P.struct({
|
|
176
|
+
check1: P.U32BE,
|
|
177
|
+
check2: P.U32BE,
|
|
178
|
+
type: SSHText,
|
|
179
|
+
curve: SSHText,
|
|
180
|
+
publicKey: SSHBytes,
|
|
181
|
+
// an mpint
|
|
182
|
+
privateKey: SSHBytes,
|
|
183
|
+
comment: SSHText,
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Returns a short name for the type of a key, for example "rsa" for "ssh-rsa"
|
|
188
|
+
*/
|
|
189
|
+
const keyTypeName = (type: string): string => {
|
|
190
|
+
return type.replace(/^ssh-/, "").replace(/^ecdsa-sha2-.*$/, "ecdsa")
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Parses an unencrypted Ed25519 or ECDSA private key in OpenSSH format, as
|
|
195
|
+
* created by `ssh-keygen`
|
|
196
|
+
*
|
|
197
|
+
* @param text The private key, starting with `-----BEGIN OPENSSH PRIVATE KEY-----`
|
|
198
|
+
* @returns The private key
|
|
199
|
+
* @throws {@link KeyParseError} when the key cannot be parsed or is encrypted
|
|
200
|
+
* @throws {@link UnsupportedKeyError} when the key is not an Ed25519 or ECDSA key
|
|
201
|
+
*/
|
|
202
|
+
export const parsePrivateKey = (text: string): PrivateKey => {
|
|
203
|
+
const match = /-----BEGIN OPENSSH PRIVATE KEY-----([\s\S]*?)-----END OPENSSH PRIVATE KEY-----/.exec(text)
|
|
204
|
+
if (match === null) {
|
|
205
|
+
throw new KeyParseError("private key was not in OpenSSH format, as created by ssh-keygen (starting with -----BEGIN OPENSSH PRIVATE KEY-----)")
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let container: P.UnwrapCoder<typeof OpenSshPrivateKey>
|
|
209
|
+
try {
|
|
210
|
+
container = OpenSshPrivateKey.decode(base64.decode(match[1]!.replace(/\s+/g, "")))
|
|
211
|
+
} catch (err) {
|
|
212
|
+
throw new KeyParseError("private key could not be decoded", err)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (container.cipherName !== "none" || container.kdfName !== "none") {
|
|
216
|
+
throw new KeyParseError("encrypted private keys are not supported")
|
|
217
|
+
}
|
|
218
|
+
if (container.publicKeys.length !== 1) {
|
|
219
|
+
throw new KeyParseError(`private key must contain one key, not ${container.publicKeys.length}`)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// the private section is padded to a block size, so may have trailing bytes
|
|
223
|
+
const options = { allowUnreadBytes: true }
|
|
224
|
+
let start: P.UnwrapCoder<typeof PrivateSectionStart>
|
|
225
|
+
try {
|
|
226
|
+
start = PrivateSectionStart.decode(container.privateKeys, options)
|
|
227
|
+
} catch (err) {
|
|
228
|
+
throw new KeyParseError("private key could not be decoded", err)
|
|
229
|
+
}
|
|
230
|
+
if (start.check1 !== start.check2) {
|
|
231
|
+
throw new KeyParseError("private key check values did not match")
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
if (start.type === "ssh-ed25519") {
|
|
236
|
+
const key = Ed25519PrivateKey.decode(container.privateKeys, options)
|
|
237
|
+
if (key.publicKey.length !== 32 || key.privateKey.length !== 64) {
|
|
238
|
+
throw new KeyParseError("Ed25519 private key was an unexpected length")
|
|
239
|
+
}
|
|
240
|
+
const publicKey = new PublicKey(container.publicKeys[0]!, key.comment)
|
|
241
|
+
if (!equalBytes(P.struct({ type: SSHText, key: SSHBytes }).encode({ type: start.type, key: key.publicKey }), publicKey.blob)) {
|
|
242
|
+
throw new KeyParseError("private key did not match its public key")
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
type: "ed25519",
|
|
247
|
+
publicKey,
|
|
248
|
+
jwk: {
|
|
249
|
+
kty: "OKP",
|
|
250
|
+
crv: "Ed25519",
|
|
251
|
+
d: base64urlnopad.encode(key.privateKey.subarray(0, 32)),
|
|
252
|
+
x: base64urlnopad.encode(key.publicKey),
|
|
253
|
+
},
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (start.type.startsWith("ecdsa-sha2-")) {
|
|
258
|
+
const key = EcdsaPrivateKey.decode(container.privateKeys, options)
|
|
259
|
+
const curve = EcdsaCurves[key.curve as EcdsaCurve]
|
|
260
|
+
if (curve === undefined || start.type !== `ecdsa-sha2-${key.curve}`) {
|
|
261
|
+
throw new UnsupportedKeyError(`ECDSA curve ${key.curve} is not supported`)
|
|
262
|
+
}
|
|
263
|
+
if (key.publicKey.length !== 1 + curve.size * 2 || key.publicKey[0] !== 0x04) {
|
|
264
|
+
throw new KeyParseError(`unexpected ECDSA public key encoding for ${key.curve}`)
|
|
265
|
+
}
|
|
266
|
+
const publicKey = new PublicKey(container.publicKeys[0]!, key.comment)
|
|
267
|
+
if (!equalBytes(P.struct({ type: SSHText, curve: SSHText, key: SSHBytes }).encode({ type: start.type, curve: key.curve, key: key.publicKey }), publicKey.blob)) {
|
|
268
|
+
throw new KeyParseError("private key did not match its public key")
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
type: "ecdsa",
|
|
273
|
+
curve: key.curve as EcdsaCurve,
|
|
274
|
+
publicKey,
|
|
275
|
+
jwk: {
|
|
276
|
+
kty: "EC",
|
|
277
|
+
crv: curve.namedCurve,
|
|
278
|
+
// d is an mpint, so may be short or have a leading sign byte
|
|
279
|
+
d: base64urlnopad.encode(toFixedWidth(key.privateKey, curve.size)),
|
|
280
|
+
x: base64urlnopad.encode(key.publicKey.subarray(1, 1 + curve.size)),
|
|
281
|
+
y: base64urlnopad.encode(key.publicKey.subarray(1 + curve.size)),
|
|
282
|
+
},
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} catch (err) {
|
|
286
|
+
if (err instanceof KeyParseError || err instanceof UnsupportedKeyError) {
|
|
287
|
+
throw err
|
|
288
|
+
}
|
|
289
|
+
throw new KeyParseError("private key could not be decoded", err)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
throw new UnsupportedKeyError(`CA key type ${keyTypeName(start.type)} is not supported, the CA key must be Ed25519 or ECDSA`)
|
|
293
|
+
}
|
|
294
|
+
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import * as P from "micro-packed"
|
|
2
|
+
import { toFixedWidth } from "../sshsig/sig_parser.js"
|
|
3
|
+
import { decodePublicKey, SSHBytes, SSHText, SSHUnsignedMpint } from "./encoding.js"
|
|
4
|
+
import type { EcdsaCurve, PrivateKey, PublicKey } from "./keys.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Signs data with a CA key using WebCrypto, returning signatures in the SSH
|
|
8
|
+
* wire format
|
|
9
|
+
*/
|
|
10
|
+
export interface Signer {
|
|
11
|
+
/** The wire encoding of the public key of the signer */
|
|
12
|
+
readonly publicKey: Uint8Array
|
|
13
|
+
/**
|
|
14
|
+
* Signs data
|
|
15
|
+
*
|
|
16
|
+
* @param data The data to sign
|
|
17
|
+
* @returns The signature in the SSH wire format (RFC 4253 section 6.6)
|
|
18
|
+
*/
|
|
19
|
+
sign(data: Uint8Array<ArrayBuffer>): Promise<Uint8Array>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** An Ed25519 signature: string algorithm, string signature */
|
|
23
|
+
const Ed25519Signature = P.struct({
|
|
24
|
+
algorithm: SSHText,
|
|
25
|
+
signature: SSHBytes,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
/** An ECDSA signature: string algorithm, string (mpint r, mpint s) */
|
|
29
|
+
const EcdsaSignature = P.struct({
|
|
30
|
+
algorithm: SSHText,
|
|
31
|
+
signature: P.prefix(P.U32BE, P.struct({
|
|
32
|
+
r: SSHUnsignedMpint,
|
|
33
|
+
s: SSHUnsignedMpint,
|
|
34
|
+
})),
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const EcdsaCurves: Readonly<Record<EcdsaCurve, { namedCurve: string, hash: string, size: number }>> = {
|
|
38
|
+
nistp256: { namedCurve: "P-256", hash: "SHA-256", size: 32 },
|
|
39
|
+
nistp384: { namedCurve: "P-384", hash: "SHA-384", size: 48 },
|
|
40
|
+
nistp521: { namedCurve: "P-521", hash: "SHA-512", size: 66 },
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates a {@link Signer} for an Ed25519 or ECDSA (P-256, P-384 or P-521)
|
|
45
|
+
* private key
|
|
46
|
+
*
|
|
47
|
+
* @param key The private key to sign with
|
|
48
|
+
* @returns The signer
|
|
49
|
+
*/
|
|
50
|
+
export const createSigner = async (key: PrivateKey): Promise<Signer> => {
|
|
51
|
+
const publicKey = key.publicKey.blob
|
|
52
|
+
|
|
53
|
+
if (key.type === "ed25519") {
|
|
54
|
+
const cryptoKey = await crypto.subtle.importKey("jwk", key.jwk, { name: "Ed25519" }, false, ["sign"])
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
publicKey,
|
|
58
|
+
sign: async (data) => Ed25519Signature.encode({
|
|
59
|
+
algorithm: "ssh-ed25519",
|
|
60
|
+
signature: new Uint8Array(await crypto.subtle.sign({ name: "Ed25519" }, cryptoKey, data)),
|
|
61
|
+
}),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const curve = EcdsaCurves[key.curve]
|
|
66
|
+
const cryptoKey = await crypto.subtle.importKey("jwk", key.jwk, { name: "ECDSA", namedCurve: curve.namedCurve }, false, ["sign"])
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
publicKey,
|
|
70
|
+
sign: async (data) => {
|
|
71
|
+
// WebCrypto returns r || s, each the size of the curve
|
|
72
|
+
const signature = new Uint8Array(await crypto.subtle.sign({ name: "ECDSA", hash: { name: curve.hash } }, cryptoKey, data))
|
|
73
|
+
|
|
74
|
+
return EcdsaSignature.encode({
|
|
75
|
+
// the algorithm is named for the curve of the key, never
|
|
76
|
+
// worked out from the signature
|
|
77
|
+
algorithm: `ecdsa-sha2-${key.curve}`,
|
|
78
|
+
signature: {
|
|
79
|
+
r: signature.subarray(0, curve.size),
|
|
80
|
+
s: signature.subarray(curve.size),
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Verifies a signature in the SSH wire format made with an Ed25519 or ECDSA
|
|
89
|
+
* key
|
|
90
|
+
*
|
|
91
|
+
* @param publicKey The key the signature must have been made with
|
|
92
|
+
* @param signature The signature in the SSH wire format
|
|
93
|
+
* @param data The data that was signed
|
|
94
|
+
* @returns true if the signature is valid, false if it is not or the key type is not supported
|
|
95
|
+
*/
|
|
96
|
+
export const verify = async (publicKey: PublicKey, signature: Uint8Array, data: Uint8Array<ArrayBuffer>): Promise<boolean> => {
|
|
97
|
+
try {
|
|
98
|
+
const { type, fields } = decodePublicKey(publicKey.blob)
|
|
99
|
+
|
|
100
|
+
if (type === "ssh-ed25519") {
|
|
101
|
+
const { algorithm, signature: raw } = Ed25519Signature.decode(signature)
|
|
102
|
+
if (algorithm !== type) {
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
const cryptoKey = await crypto.subtle.importKey("raw", new Uint8Array(fields[0]!), { name: "Ed25519" }, false, ["verify"])
|
|
106
|
+
|
|
107
|
+
return await crypto.subtle.verify({ name: "Ed25519" }, cryptoKey, new Uint8Array(raw), data)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const curveName = type.replace(/^ecdsa-sha2-/, "") as EcdsaCurve
|
|
111
|
+
const curve = EcdsaCurves[curveName]
|
|
112
|
+
if (curve !== undefined && type === `ecdsa-sha2-${curveName}`) {
|
|
113
|
+
const { algorithm, signature: { r, s } } = EcdsaSignature.decode(signature)
|
|
114
|
+
if (algorithm !== type || r.length > curve.size || s.length > curve.size) {
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
117
|
+
// fields are the curve name and the public point
|
|
118
|
+
const cryptoKey = await crypto.subtle.importKey("raw", new Uint8Array(fields[1]!), { name: "ECDSA", namedCurve: curve.namedCurve }, false, ["verify"])
|
|
119
|
+
const raw = P.utils.concatBytes(toFixedWidth(r, curve.size), toFixedWidth(s, curve.size))
|
|
120
|
+
|
|
121
|
+
return await crypto.subtle.verify({ name: "ECDSA", hash: { name: curve.hash } }, cryptoKey, new Uint8Array(raw), data)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return false
|
|
125
|
+
} catch {
|
|
126
|
+
// a signature or key that cannot be decoded or imported is not valid
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -20,12 +20,6 @@ export type CertificateRequestJWTPayload = {
|
|
|
20
20
|
[key: string]: string | string[]
|
|
21
21
|
} & JWTPayload
|
|
22
22
|
|
|
23
|
-
export type SSHExtension = {
|
|
24
|
-
critical: boolean;
|
|
25
|
-
name: string;
|
|
26
|
-
data: Buffer<ArrayBuffer>
|
|
27
|
-
}
|
|
28
|
-
|
|
29
23
|
/**
|
|
30
24
|
* The result of executing a statement via {@link CaPreparedStatement.run} or
|
|
31
25
|
* {@link CaDatabase.batch}, modelled on a Cloudflare D1 result.
|
|
@@ -99,23 +93,23 @@ export interface CaSecret {
|
|
|
99
93
|
* {@link CaSecret} for the non-string bindings.
|
|
100
94
|
*/
|
|
101
95
|
export interface SshCaBindings {
|
|
102
|
-
|
|
96
|
+
CERTIFICATE_REQUEST_TIME_SKEW_MAX: string
|
|
97
|
+
JWT_ALGORITHMS: string | string[]
|
|
98
|
+
JWT_AUD?: string | string[]
|
|
99
|
+
JWT_ISSUER: string
|
|
100
|
+
JWT_JWKS_URL: string
|
|
101
|
+
JWT_SSH_CERTIFICATE_PRINCIPALS_CLAIM: string
|
|
102
|
+
LOG_LEVEL?: string
|
|
103
103
|
DB_CERTIFICATE_RETENTION: string
|
|
104
|
+
DB: CaDatabase
|
|
105
|
+
ISSUER_DN: string
|
|
104
106
|
PRIVATE_KEY: CaSecret
|
|
105
107
|
SSH_CERTIFICATE_EXTENSIONS: string
|
|
106
|
-
SSH_CERTIFICATE_LIFETIME: string
|
|
107
|
-
SSH_CERTIFICATE_INCLUDE_SELF?: string | boolean
|
|
108
108
|
SSH_CERTIFICATE_INCLUDE_SELF_EMAIL?: string | boolean
|
|
109
|
+
SSH_CERTIFICATE_INCLUDE_SELF?: string | boolean
|
|
110
|
+
SSH_CERTIFICATE_LIFETIME: string
|
|
109
111
|
SSH_CERTIFICATE_PRINCIPALS?: string | string[]
|
|
110
|
-
SSH_HOST_CERTIFICATE_LIFETIME: string
|
|
111
112
|
SSH_HOST_CERTIFICATE_ALLOWED_EMAILS?: string | string[]
|
|
112
113
|
SSH_HOST_CERTIFICATE_ALLOWED_ROLES?: string | string[]
|
|
113
|
-
|
|
114
|
-
JWT_JWKS_URL: string
|
|
115
|
-
JWT_AUD?: string | string[]
|
|
116
|
-
JWT_ISSUER: string
|
|
117
|
-
JWT_ALGORITHMS: string | string[]
|
|
118
|
-
JWT_SSH_CERTIFICATE_PRINCIPALS_CLAIM: string
|
|
119
|
-
CERTIFICATE_REQUEST_TIME_SKEW_MAX: string
|
|
120
|
-
LOG_LEVEL?: string
|
|
114
|
+
SSH_HOST_CERTIFICATE_LIFETIME: string
|
|
121
115
|
}
|