Skip to content

Commit 234510b

Browse files
authored
Update key sizes in aspire extension to 4096 bits (#11861)
* update key sizes to 4096 * make createSelfSignedCert async * revert generateToken change
1 parent 3534830 commit 234510b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

extension/src/dcp/AspireDcpServer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import express, { Request, Response, NextFunction } from 'express';
22
import https from 'https';
33
import WebSocket, { WebSocketServer } from 'ws';
4-
import { createSelfSignedCert, generateToken } from '../utils/security';
4+
import { createSelfSignedCertAsync, generateToken } from '../utils/security';
55
import { extensionLogOutputChannel } from '../utils/logging';
66
import { AspireResourceDebugSession, DcpServerConnectionInfo, ErrorDetails, ErrorResponse, ProcessRestartedNotification, RunSessionNotification, RunSessionPayload, ServiceLogsNotification, SessionMessageNotification, SessionTerminatedNotification } from './types';
77
import { AspireDebugSession } from '../debugger/AspireDebugSession';
@@ -38,7 +38,7 @@ export default class AspireDcpServer {
3838
const wsBySession = new Map<string, WebSocket>();
3939
const pendingNotificationQueueByDcpId = new Map<string, RunSessionNotification[]>();
4040

41-
return new Promise((resolve, reject) => {
41+
return new Promise(async (resolve, reject) => {
4242
const token = generateToken();
4343

4444
const app = express();
@@ -174,7 +174,7 @@ export default class AspireDcpServer {
174174
});
175175

176176

177-
const { key, cert, certBase64 } = createSelfSignedCert();
177+
const { key, cert, certBase64 } = await createSelfSignedCertAsync();
178178
const server = https.createServer({ key, cert }, app);
179179
const wss = new WebSocketServer({ noServer: true });
180180

extension/src/server/AspireRpcServer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { invalidTokenProvided, rpcServerAddressError, rpcServerError } from '../
55
import { addInteractionServiceEndpoints, IInteractionService } from './interactionService';
66
import { ICliRpcClient } from './rpcClient';
77
import * as tls from 'tls';
8-
import { createSelfSignedCert, generateToken } from '../utils/security';
8+
import { createSelfSignedCertAsync, generateToken } from '../utils/security';
99
import { extensionLogOutputChannel } from '../utils/logging';
1010
import { getSupportedCapabilities } from '../capabilities';
1111
import { timingSafeEqual } from 'crypto';
@@ -51,9 +51,9 @@ export default class AspireRpcServer {
5151
this.server.close();
5252
}
5353

54-
static create(rpcClientFactory: (rpcServerConnectionInfo: RpcServerConnectionInfo, connection: MessageConnection, token: string, debugSessionId: string | null) => ICliRpcClient): Promise<AspireRpcServer> {
54+
static async create(rpcClientFactory: (rpcServerConnectionInfo: RpcServerConnectionInfo, connection: MessageConnection, token: string, debugSessionId: string | null) => ICliRpcClient): Promise<AspireRpcServer> {
5555
const token = generateToken();
56-
const { key, cert } = createSelfSignedCert();
56+
const { key, cert } = await createSelfSignedCertAsync();
5757

5858
function withAuthentication(callback: (...params: any[]) => any) {
5959
return (...params: any[]) => {

extension/src/utils/security.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import { randomBytes, X509Certificate } from 'crypto';
22
import forge from 'node-forge';
33

4-
export function createSelfSignedCert(commonName: string = 'localhost') {
4+
interface SelfSignedCert {
5+
key: string;
6+
cert: string;
7+
certBase64: string;
8+
}
9+
10+
export async function createSelfSignedCertAsync(commonName: string = 'localhost'): Promise<SelfSignedCert> {
511
const pki = forge.pki;
6-
const keys = pki.rsa.generateKeyPair(2048);
12+
const keys = await new Promise<forge.pki.rsa.KeyPair>((resolve, reject) => {
13+
// 4096 bits provides enough entropy. Follows modern industry practice
14+
pki.rsa.generateKeyPair({ bits: 4096, workers: -1 }, (err, keypair) => {
15+
if (err) {
16+
reject(err);
17+
} else {
18+
resolve(keypair);
19+
}
20+
});
21+
});
22+
723
const cert = pki.createCertificate();
824
cert.publicKey = keys.publicKey;
925
cert.serialNumber = (Math.floor(Math.random() * 1e16)).toString();

0 commit comments

Comments
 (0)