/api/key/register Endpoint에서, 제공된 공개 키가 RSA 4096의 공개 키가 맞는지 검증하도록 개선

This commit is contained in:
static
2024-12-29 00:36:13 +09:00
parent 75ab5f5859
commit f6432ff290

View File

@@ -1,5 +1,5 @@
import { error } from "@sveltejs/kit";
import { randomBytes, publicEncrypt } from "crypto";
import { randomBytes, publicEncrypt, createPublicKey } from "crypto";
import ms from "ms";
import { promisify } from "util";
import {
@@ -29,6 +29,15 @@ export const registerPubKey = async (userId: number, ip: string, pubKey: string)
error(409, "Public key already registered");
}
const pubKeyPem = `-----BEGIN PUBLIC KEY-----\n${pubKey}\n-----END PUBLIC KEY-----`;
const pubKeyObject = createPublicKey(pubKeyPem);
if (
pubKeyObject.asymmetricKeyType !== "rsa" ||
pubKeyObject.asymmetricKeyDetails?.modulusLength !== 4096
) {
error(400, "Invalid public key");
}
const clientId = await createClient(pubKey, userId);
return await generateChallenge(userId, ip, clientId, pubKey);
};