11import * as crypto from 'crypto' ;
2+ import * as fs from 'fs' ;
23import { getSSHConfig } from '../config' ;
34
45/**
@@ -9,6 +10,7 @@ export class SSHKeyManager {
910 private static readonly KEY_EXPIRY_HOURS = 24 ; // 24 hours max retention
1011 private static readonly IV_LENGTH = 16 ;
1112 private static readonly TAG_LENGTH = 16 ;
13+ private static readonly AAD = Buffer . from ( 'ssh-key-proxy' ) ;
1214
1315 /**
1416 * Get the encryption key from environment or generate a secure one
@@ -22,7 +24,6 @@ export class SSHKeyManager {
2224
2325 // For development, use a key derived from the SSH host key
2426 const hostKeyPath = getSSHConfig ( ) . hostKey . privateKeyPath ;
25- const fs = require ( 'fs' ) ;
2627 const hostKey = fs . readFileSync ( hostKeyPath ) ;
2728
2829 // Create a consistent key from the host key
@@ -43,20 +44,17 @@ export class SSHKeyManager {
4344 const iv = crypto . randomBytes ( this . IV_LENGTH ) ;
4445
4546 const cipher = crypto . createCipheriv ( this . ALGORITHM , encryptionKey , iv ) ;
46- cipher . setAAD ( Buffer . from ( 'ssh-key-proxy' ) ) ;
47+ cipher . setAAD ( this . AAD ) ;
4748
4849 let encrypted = cipher . update ( keyBuffer ) ;
4950 encrypted = Buffer . concat ( [ encrypted , cipher . final ( ) ] ) ;
5051
5152 const tag = cipher . getAuthTag ( ) ;
5253 const result = Buffer . concat ( [ iv , tag , encrypted ] ) ;
5354
54- const expiryTime = new Date ( ) ;
55- expiryTime . setHours ( expiryTime . getHours ( ) + this . KEY_EXPIRY_HOURS ) ;
56-
5755 return {
5856 encryptedKey : result . toString ( 'base64' ) ,
59- expiryTime,
57+ expiryTime : new Date ( Date . now ( ) + this . KEY_EXPIRY_HOURS * 60 * 60 * 1000 ) ,
6058 } ;
6159 }
6260
@@ -82,7 +80,7 @@ export class SSHKeyManager {
8280 const encrypted = data . subarray ( this . IV_LENGTH + this . TAG_LENGTH ) ;
8381
8482 const decipher = crypto . createDecipheriv ( this . ALGORITHM , encryptionKey , iv ) ;
85- decipher . setAAD ( Buffer . from ( 'ssh-key-proxy' ) ) ;
83+ decipher . setAAD ( this . AAD ) ;
8684 decipher . setAuthTag ( tag ) ;
8785
8886 let decrypted = decipher . update ( encrypted ) ;
0 commit comments