@andrewheberle/serverless-ssh-ca 0.17.13 → 0.17.15
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 +6 -17
- package/dist/api/v3/index.d.ts +1 -0
- package/dist/api/v3/index.d.ts.map +1 -0
- package/dist/api/v3/index.js +1 -0
- package/dist/api/v3/index.js.map +1 -0
- package/dist/api/v3/schema.d.ts +9 -0
- package/dist/api/v3/schema.d.ts.map +1 -0
- package/dist/api/v3/schema.js +4 -4
- package/dist/api/v3/schema.js.map +1 -0
- package/dist/certificate.d.ts +1 -0
- package/dist/certificate.d.ts.map +1 -0
- package/dist/certificate.js +1 -0
- package/dist/certificate.js.map +1 -0
- package/dist/{db.d.ts → db/index.d.ts} +6 -6
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +151 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/migrations/0001_initial_schema.d.ts +3 -0
- package/dist/db/migrations/0001_initial_schema.d.ts.map +1 -0
- package/dist/db/migrations/0001_initial_schema.js +24 -0
- package/dist/db/migrations/0001_initial_schema.js.map +1 -0
- package/dist/db/migrations/index.d.ts +3 -0
- package/dist/db/migrations/index.d.ts.map +1 -0
- package/dist/db/migrations/index.js +5 -0
- package/dist/db/migrations/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/krl.d.ts +1 -0
- package/dist/krl.d.ts.map +1 -0
- package/dist/krl.js +1 -0
- package/dist/krl.js.map +1 -0
- package/dist/logger.d.ts +1 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +1 -0
- package/dist/logger.js.map +1 -0
- package/dist/proof.d.ts +2 -0
- package/dist/proof.d.ts.map +1 -0
- package/dist/proof.js +2 -1
- package/dist/proof.js.map +1 -0
- package/dist/router.d.ts +1 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +1 -0
- package/dist/router.js.map +1 -0
- package/dist/sshsig/armor.d.ts +1 -0
- package/dist/sshsig/armor.d.ts.map +1 -0
- package/dist/sshsig/armor.js +1 -0
- package/dist/sshsig/armor.js.map +1 -0
- package/dist/sshsig/formats.d.ts +1 -0
- package/dist/sshsig/formats.d.ts.map +1 -0
- package/dist/sshsig/formats.js +1 -0
- package/dist/sshsig/formats.js.map +1 -0
- package/dist/sshsig/index.d.ts +1 -0
- package/dist/sshsig/index.d.ts.map +1 -0
- package/dist/sshsig/index.js +1 -0
- package/dist/sshsig/index.js.map +1 -0
- package/dist/sshsig/reader.d.ts +1 -0
- package/dist/sshsig/reader.d.ts.map +1 -0
- package/dist/sshsig/reader.js +1 -0
- package/dist/sshsig/reader.js.map +1 -0
- package/dist/sshsig/sig.d.ts +1 -0
- package/dist/sshsig/sig.d.ts.map +1 -0
- package/dist/sshsig/sig.js +1 -0
- package/dist/sshsig/sig.js.map +1 -0
- package/dist/sshsig/sig_parser.d.ts +1 -0
- package/dist/sshsig/sig_parser.d.ts.map +1 -0
- package/dist/sshsig/sig_parser.js +1 -0
- package/dist/sshsig/sig_parser.js.map +1 -0
- package/dist/sshsig/verifier.d.ts +1 -0
- package/dist/sshsig/verifier.d.ts.map +1 -0
- package/dist/sshsig/verifier.js +1 -0
- package/dist/sshsig/verifier.js.map +1 -0
- package/dist/sshsig/writer.d.ts +1 -0
- package/dist/sshsig/writer.d.ts.map +1 -0
- package/dist/sshsig/writer.js +1 -0
- package/dist/sshsig/writer.js.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +4 -0
- package/dist/utils.js.map +1 -0
- package/dist/verify.d.ts +1 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +1 -0
- package/dist/verify.js.map +1 -0
- package/package.json +16 -8
- package/src/api/v3/index.ts +419 -0
- package/src/api/v3/schema.ts +265 -0
- package/src/certificate.ts +214 -0
- package/src/db/index.ts +194 -0
- package/src/db/migrations/0001_initial_schema.ts +25 -0
- package/src/db/migrations/index.ts +6 -0
- package/src/index.ts +18 -0
- package/src/krl.ts +414 -0
- package/src/logger.ts +6 -0
- package/src/proof.ts +187 -0
- package/src/router.ts +35 -0
- package/src/sshsig/armor.ts +24 -0
- package/src/sshsig/formats.ts +226 -0
- package/src/sshsig/index.ts +47 -0
- package/src/sshsig/reader.ts +59 -0
- package/src/sshsig/sig.ts +17 -0
- package/src/sshsig/sig_parser.ts +90 -0
- package/src/sshsig/verifier.ts +85 -0
- package/src/sshsig/writer.ts +56 -0
- package/src/types.ts +49 -0
- package/src/utils.ts +414 -0
- package/src/verify.ts +25 -0
- package/LICENSE +0 -201
- package/dist/db.js +0 -111
package/src/utils.ts
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import type { SshCaBindings } from "./types"
|
|
2
|
+
import { JWKInvalid, JWKSInvalid, JWSInvalid, JWTClaimValidationFailed, JWTInvalid } from "jose/errors"
|
|
3
|
+
import { Certificate, Key, KeyParseError, CertificateParseError, parseCertificate, parseKey, parsePrivateKey, PrivateKey } from "sshpk"
|
|
4
|
+
import z from "zod"
|
|
5
|
+
import { verifyJWT } from "./verify"
|
|
6
|
+
import { CertificateRequestJWTPayload } from "./types"
|
|
7
|
+
import { RenewalProofOfPossession, ProofOfPossession, PossessionParseError } from "./proof"
|
|
8
|
+
import type { isRevoked as IsRevokedFn } from "./db"
|
|
9
|
+
import { logger } from "./logger"
|
|
10
|
+
import { ms } from "itty-time"
|
|
11
|
+
|
|
12
|
+
export const fatalIssue = (ctx: z.RefinementCtx, message: string, val: unknown) => {
|
|
13
|
+
ctx.issues.push({
|
|
14
|
+
code: "custom",
|
|
15
|
+
message: message,
|
|
16
|
+
input: val
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
return z.NEVER
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const transformProofOfPossession = (env: SshCaBindings, val: string, ctx: z.core.$RefinementCtx<string>): ProofOfPossession | never => {
|
|
23
|
+
const l = logger(env)
|
|
24
|
+
try {
|
|
25
|
+
const proof = new ProofOfPossession(val, { logger: l, skew: ms(env.CERTIFICATE_REQUEST_TIME_SKEW_MAX) })
|
|
26
|
+
|
|
27
|
+
return proof
|
|
28
|
+
} catch (err) {
|
|
29
|
+
l.error("proof of possession parsing error", "error", err)
|
|
30
|
+
switch (true) {
|
|
31
|
+
case (err instanceof PossessionParseError):
|
|
32
|
+
ctx.issues.push({
|
|
33
|
+
code: "custom",
|
|
34
|
+
message: err.message,
|
|
35
|
+
input: val
|
|
36
|
+
})
|
|
37
|
+
break
|
|
38
|
+
default:
|
|
39
|
+
ctx.issues.push({
|
|
40
|
+
code: "custom",
|
|
41
|
+
message: "proof of possession transform unhandled error",
|
|
42
|
+
input: val
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return z.NEVER
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type AccessToken = {
|
|
51
|
+
email: string
|
|
52
|
+
sub: string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const transformAuthorizationHeader = async (env: SshCaBindings, val: string, ctx: z.RefinementCtx): Promise<AccessToken | never> => {
|
|
56
|
+
if (!val.startsWith("Bearer ") || val.length <= 7) {
|
|
57
|
+
return fatalIssue(ctx, "authorization header must be a Bearer token", val)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const jwt = val.replace("Bearer ", "")
|
|
61
|
+
const l = logger(env)
|
|
62
|
+
|
|
63
|
+
if (jwt === "") {
|
|
64
|
+
return fatalIssue(ctx, "request did not contain a JWT", val)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
// verify jwt
|
|
69
|
+
const { payload } = await verifyJWT(env, jwt)
|
|
70
|
+
|
|
71
|
+
if (payload.email === undefined) {
|
|
72
|
+
return fatalIssue(ctx, "JWT was verified but was missing required email claim", val)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
email: payload.email,
|
|
77
|
+
sub: payload.sub,
|
|
78
|
+
}
|
|
79
|
+
} catch (err) {
|
|
80
|
+
switch (true) {
|
|
81
|
+
case (err instanceof JWSInvalid):
|
|
82
|
+
return fatalIssue(ctx, "the access token was invalid", val)
|
|
83
|
+
case (err instanceof JWTInvalid):
|
|
84
|
+
return fatalIssue(ctx, "the access token failed verification", val)
|
|
85
|
+
case (err instanceof JWKInvalid):
|
|
86
|
+
return fatalIssue(ctx, "the access token JWK was invalid", val)
|
|
87
|
+
case (err instanceof JWKSInvalid):
|
|
88
|
+
return fatalIssue(ctx, "the access token JWKS was invalid", val)
|
|
89
|
+
case (err instanceof JWTClaimValidationFailed):
|
|
90
|
+
return fatalIssue(ctx, "claim validtion of the JWT failed", val)
|
|
91
|
+
default:
|
|
92
|
+
l.error("unhandled access token validation error", "in", "transformAuthorizationHeader", "error", err)
|
|
93
|
+
return fatalIssue(ctx, "unhandled access token validation error", val)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const transformPublicKey = (val: string | Buffer<ArrayBufferLike>, ctx: z.RefinementCtx): Key | never => {
|
|
99
|
+
try {
|
|
100
|
+
const key = typeof val === "string" ? parseKey(Buffer.from(val, "base64")) : parseKey(val)
|
|
101
|
+
|
|
102
|
+
return key
|
|
103
|
+
} catch (err) {
|
|
104
|
+
switch (true) {
|
|
105
|
+
case (err instanceof TypeError):
|
|
106
|
+
ctx.issues.push({
|
|
107
|
+
code: "custom",
|
|
108
|
+
message: err.message,
|
|
109
|
+
input: val
|
|
110
|
+
})
|
|
111
|
+
break
|
|
112
|
+
case (err instanceof KeyParseError):
|
|
113
|
+
ctx.issues.push({
|
|
114
|
+
code: "custom",
|
|
115
|
+
message: err.message,
|
|
116
|
+
input: val
|
|
117
|
+
})
|
|
118
|
+
break
|
|
119
|
+
default:
|
|
120
|
+
ctx.issues.push({
|
|
121
|
+
code: "custom",
|
|
122
|
+
message: "unhandled error parsing public_key",
|
|
123
|
+
input: val
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return z.NEVER
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const identityPrincipals = (env: SshCaBindings, payload: CertificateRequestJWTPayload, claim?: string): string[] => {
|
|
132
|
+
if (claim === undefined) {
|
|
133
|
+
claim = env.JWT_SSH_CERTIFICATE_PRINCIPALS_CLAIM
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const l = logger(env)
|
|
137
|
+
|
|
138
|
+
if (claim === "__proto__" || claim === "prototype" || claim === "constructor") {
|
|
139
|
+
l.warn("invalid principals claim configured", "claim", claim)
|
|
140
|
+
return []
|
|
141
|
+
}
|
|
142
|
+
const p = payload[claim]
|
|
143
|
+
|
|
144
|
+
if (p === undefined) {
|
|
145
|
+
l.warn("claim was missing despite being set in CA config", "claim", claim)
|
|
146
|
+
return []
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (p === "") {
|
|
150
|
+
l.warn("claim was present but was an empty string", "claim", claim)
|
|
151
|
+
return []
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// make sure its always a string[] and replace any spaces with underscores
|
|
155
|
+
const principals = (typeof p === "string" ? [p] : p)
|
|
156
|
+
.map((value) => value.replaceAll(" ", "_"))
|
|
157
|
+
|
|
158
|
+
l.info("identity token included principals", "principals", principals)
|
|
159
|
+
|
|
160
|
+
return principals
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type IdentityToken = {
|
|
164
|
+
sub: string
|
|
165
|
+
principals: string[]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export const parseIdentity = async (env: SshCaBindings, jwt: string | undefined, claim?: string): Promise<IdentityToken> => {
|
|
169
|
+
if (jwt === undefined) {
|
|
170
|
+
throw new Error("missing identity token")
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const aud = env.JWT_AUD === undefined || env.JWT_AUD as string === "" ? undefined : split(env.JWT_AUD as string)
|
|
174
|
+
const { payload } = await verifyJWT(env, jwt, { aud: aud })
|
|
175
|
+
|
|
176
|
+
const principals = identityPrincipals(env, payload, claim)
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
sub: payload.sub,
|
|
180
|
+
principals: principals,
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const transformIdentityToken = async (env: SshCaBindings, val: string, ctx: z.RefinementCtx): Promise<IdentityToken | never> => {
|
|
185
|
+
try {
|
|
186
|
+
const identity = await parseIdentity(env, val, env.JWT_SSH_CERTIFICATE_PRINCIPALS_CLAIM)
|
|
187
|
+
|
|
188
|
+
return identity
|
|
189
|
+
} catch (err) {
|
|
190
|
+
ctx.issues.push({
|
|
191
|
+
code: "custom",
|
|
192
|
+
message: "problem parsing identity token",
|
|
193
|
+
input: val
|
|
194
|
+
})
|
|
195
|
+
return z.NEVER
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
*
|
|
201
|
+
* @param val A SSH certificate either a base64 encoded "string" or a "Buffer\<ArrayBufferLike\>"
|
|
202
|
+
* @param ctx The Zod context for any errors
|
|
203
|
+
* @returns
|
|
204
|
+
*/
|
|
205
|
+
export const transformCertificate = (val: string | Buffer<ArrayBufferLike>, ctx: z.RefinementCtx): Certificate | never => {
|
|
206
|
+
try {
|
|
207
|
+
const cert = typeof val === "string" ? parseCertificate(Buffer.from(val, "base64"), "openssh") : parseCertificate(val, "openssh")
|
|
208
|
+
|
|
209
|
+
return cert
|
|
210
|
+
} catch (err) {
|
|
211
|
+
switch (true) {
|
|
212
|
+
case (err instanceof TypeError):
|
|
213
|
+
ctx.issues.push({
|
|
214
|
+
code: "custom",
|
|
215
|
+
message: err.message,
|
|
216
|
+
input: val
|
|
217
|
+
})
|
|
218
|
+
break
|
|
219
|
+
case (err instanceof CertificateParseError):
|
|
220
|
+
ctx.issues.push({
|
|
221
|
+
code: "custom",
|
|
222
|
+
message: err.message,
|
|
223
|
+
input: val
|
|
224
|
+
})
|
|
225
|
+
break
|
|
226
|
+
default:
|
|
227
|
+
ctx.issues.push({
|
|
228
|
+
code: "custom",
|
|
229
|
+
message: "unhandled error parsing certificate",
|
|
230
|
+
input: val
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return z.NEVER
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
type ParsedCertificateRequest = {
|
|
239
|
+
proof: ProofOfPossession
|
|
240
|
+
public_key: Key
|
|
241
|
+
lifetime: number
|
|
242
|
+
identity: IdentityToken
|
|
243
|
+
extensions: string[]
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export const refineCertificateRequest = async (env: SshCaBindings, val: ParsedCertificateRequest, ctx: z.RefinementCtx): Promise<never> => {
|
|
247
|
+
const l = logger(env)
|
|
248
|
+
try {
|
|
249
|
+
// check proof of possession fingerprint matches public key
|
|
250
|
+
if (!val.proof.matches(val.public_key)) {
|
|
251
|
+
return fatalIssue(ctx, "proof of possession fingerprint did not match public_key", val)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// verify proof of possession signature
|
|
255
|
+
const verified = await val.proof.verify()
|
|
256
|
+
if (!verified) {
|
|
257
|
+
return fatalIssue(ctx, "proof of possession signature validation failed", val)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return z.NEVER
|
|
261
|
+
} catch (err) {
|
|
262
|
+
l.error("proof of possession verification unhandled error", "in", "refineCertificateRequest", "error", err)
|
|
263
|
+
return fatalIssue(ctx, "proof of possession verification unhandled error", val)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export const transformRenewalProofOfPossession = (env: SshCaBindings, val: string, ctx: z.RefinementCtx): RenewalProofOfPossession | never => {
|
|
268
|
+
const l = logger(env)
|
|
269
|
+
try {
|
|
270
|
+
return new RenewalProofOfPossession(val, { logger: l, skew: ms(env.CERTIFICATE_REQUEST_TIME_SKEW_MAX) })
|
|
271
|
+
} catch (err) {
|
|
272
|
+
if (err instanceof PossessionParseError) {
|
|
273
|
+
return fatalIssue(ctx, err.message, val)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
l.error("proof of possession verification unhandled error", "in", "transformRenewalProofOfPossession", "error", err)
|
|
277
|
+
return fatalIssue(ctx, "proof of possession transform unhandled error", val)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
type HostCertificateRequest = {
|
|
282
|
+
public_key: Key
|
|
283
|
+
lifetime: number
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
type ParsedHostCertificateRequest = {
|
|
287
|
+
principals: string[]
|
|
288
|
+
proof: ProofOfPossession
|
|
289
|
+
} & HostCertificateRequest
|
|
290
|
+
|
|
291
|
+
export const refineHostCertificateRequest = async (env: SshCaBindings, val: ParsedHostCertificateRequest, ctx: z.RefinementCtx): Promise<never> => {
|
|
292
|
+
const l = logger(env).with("in", "refineHostCertificateRequest")
|
|
293
|
+
|
|
294
|
+
l.debug("started")
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
// check proof of possession fingerprint matches public key
|
|
298
|
+
l.debug("checking val.proof.matches()", "public_key", val.public_key.toString("ssh"))
|
|
299
|
+
if (!val.proof.matches(val.public_key)) {
|
|
300
|
+
l.warn("proof of possession fingerprint did not match public_key")
|
|
301
|
+
return fatalIssue(ctx, "proof of possession fingerprint did not match public_key", val)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// verify proof of possession signature
|
|
305
|
+
l.debug("await val.proof.verify()")
|
|
306
|
+
const verified = await val.proof.verify()
|
|
307
|
+
if (!verified) {
|
|
308
|
+
l.warn("proof of possession signature validation failed")
|
|
309
|
+
return fatalIssue(ctx, "proof of possession signature validation failed", val)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
l.debug("done")
|
|
313
|
+
|
|
314
|
+
return z.NEVER
|
|
315
|
+
} catch (err) {
|
|
316
|
+
l.error("proof of possession verification unhandled error", "error", err)
|
|
317
|
+
return fatalIssue(ctx, "proof of possession verification unhandled error", val)
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
type ParsedHostCertificateRenewal = {
|
|
322
|
+
certificate: Certificate
|
|
323
|
+
proof: RenewalProofOfPossession
|
|
324
|
+
} & HostCertificateRequest
|
|
325
|
+
|
|
326
|
+
export const refineHostCertificateRenewal = async (env: SshCaBindings, isRevoked: typeof IsRevokedFn, val: ParsedHostCertificateRenewal, ctx: z.RefinementCtx): Promise<never> => {
|
|
327
|
+
const l = logger(env)
|
|
328
|
+
try {
|
|
329
|
+
// ensure certificate is signed by CA
|
|
330
|
+
const issuer = await getPublic(env)
|
|
331
|
+
if (!val.certificate.isSignedByKey(issuer)) {
|
|
332
|
+
return fatalIssue(ctx, "the provided certificate was not signed by this CA", val)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// check certificate is not expired
|
|
336
|
+
if (val.certificate.isExpired()) {
|
|
337
|
+
return fatalIssue(ctx, "the provided certificate is expired", val)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ensure certificate presented for renewal is not revoked
|
|
341
|
+
const serial = val.certificate.serial.readBigUInt64BE(0)
|
|
342
|
+
if (await isRevoked(env, serial)) {
|
|
343
|
+
return fatalIssue(ctx, "the provided certificate is revoked", val)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// check proof of possession fingerprint matches public key and certificate subject key
|
|
347
|
+
if (!val.proof.matches(val.public_key, val.certificate.subjectKey)) {
|
|
348
|
+
return fatalIssue(ctx, "proof of possession fingerprints did not match public_key and/or certificate", val)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// verify proof of possession signature
|
|
352
|
+
const verified = await val.proof.verify()
|
|
353
|
+
if (!verified) {
|
|
354
|
+
return fatalIssue(ctx, "proof of possession signature validation failed", val)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return z.NEVER
|
|
358
|
+
} catch (err) {
|
|
359
|
+
l.error("proof of possession verification unhandled error", "in", "refineHostCertificateRenewal", "error", err)
|
|
360
|
+
return fatalIssue(ctx, "proof of possession verification unhandled error", val)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
type ParsedRevokeCertificate = {
|
|
365
|
+
serial: bigint
|
|
366
|
+
public_key: Key
|
|
367
|
+
proof: ProofOfPossession
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export const refineRevokeCertificate = async (env: SshCaBindings, val: ParsedRevokeCertificate, ctx: z.RefinementCtx): Promise<never> => {
|
|
371
|
+
const l = logger(env)
|
|
372
|
+
try {
|
|
373
|
+
// check proof of possession fingerprint matches public key
|
|
374
|
+
if (!val.proof.matches(val.public_key)) {
|
|
375
|
+
return fatalIssue(ctx, "proof of possession fingerprint did not match public_key", val)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// verify proof of possession signature
|
|
379
|
+
const verified = await val.proof.verify()
|
|
380
|
+
if (!verified) {
|
|
381
|
+
return fatalIssue(ctx, "proof of possession signature validation failed", val)
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return z.NEVER
|
|
385
|
+
} catch (err) {
|
|
386
|
+
l.error("proof of possession verification unhandled error", "in", "refineRevokeCertificate", "error", err)
|
|
387
|
+
return fatalIssue(ctx, "proof of possession verification unhandled error", val)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export const split = (v: string): string[] => {
|
|
392
|
+
if (v === "" || v === undefined) {
|
|
393
|
+
return []
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return v.split(",")
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export const getPublic = async (env: SshCaBindings, key?: PrivateKey): Promise<Key> => {
|
|
400
|
+
if (key === undefined) {
|
|
401
|
+
// grab private key from secret store
|
|
402
|
+
const secret = await env.PRIVATE_KEY.get()
|
|
403
|
+
|
|
404
|
+
// parse it
|
|
405
|
+
key = parsePrivateKey(secret)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// convert to a public key and add comment
|
|
409
|
+
const pub = key.toPublic()
|
|
410
|
+
pub.comment = env.ISSUER_DN
|
|
411
|
+
|
|
412
|
+
// return in ssh format trimmed of any whitespace
|
|
413
|
+
return pub
|
|
414
|
+
}
|
package/src/verify.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createRemoteJWKSet, jwtVerify } from "jose"
|
|
2
|
+
import { CertificateRequestJWTPayload } from "./types"
|
|
3
|
+
import type { SshCaBindings } from "./types"
|
|
4
|
+
import { split } from "./utils"
|
|
5
|
+
|
|
6
|
+
type VerifyOptions = {
|
|
7
|
+
aud?: string | string[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const verifyJWT = async (env: SshCaBindings, jwt: string, opts: VerifyOptions = {}) => {
|
|
11
|
+
const JWKS = createRemoteJWKSet(new URL(env.JWT_JWKS_URL))
|
|
12
|
+
|
|
13
|
+
if (opts.aud !== undefined) {
|
|
14
|
+
return await jwtVerify<CertificateRequestJWTPayload>(jwt, JWKS, {
|
|
15
|
+
issuer: env.JWT_ISSUER,
|
|
16
|
+
algorithms: split(env.JWT_ALGORITHMS),
|
|
17
|
+
audience: opts.aud
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return await jwtVerify<CertificateRequestJWTPayload>(jwt, JWKS, {
|
|
22
|
+
issuer: env.JWT_ISSUER,
|
|
23
|
+
algorithms: split(env.JWT_ALGORITHMS),
|
|
24
|
+
})
|
|
25
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
Apache License
|
|
2
|
-
Version 2.0, January 2004
|
|
3
|
-
http://www.apache.org/licenses/
|
|
4
|
-
|
|
5
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
-
|
|
7
|
-
1. Definitions.
|
|
8
|
-
|
|
9
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
-
|
|
12
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
-
the copyright owner that is granting the License.
|
|
14
|
-
|
|
15
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
-
other entities that control, are controlled by, or are under common
|
|
17
|
-
control with that entity. For the purposes of this definition,
|
|
18
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
-
direction or management of such entity, whether by contract or
|
|
20
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for reasonable and customary use in describing the
|
|
141
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright 2026 Andrew Heberle
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|