| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * RSA-OAEP (SHA-256) + AES-256-GCM hybrid encryption helpers. |
| 5 | * |
| 6 | * The server generates a 2048-bit RSA key pair once on startup and stores it |
| 7 | * under ./keys/. All login credential decryption and token encryption use |
| 8 | * these routines. |
| 9 | * |
| 10 | * Hybrid scheme (encrypt): |
| 11 | * 1. Generate random 256-bit AES key + 96-bit IV. |
| 12 | * 2. Encrypt plaintext with AES-256-GCM → ciphertext + 128-bit auth tag. |
| 13 | * 3. Encrypt AES key with RSA-OAEP (SHA-256) → encryptedKey. |
| 14 | * 4. Return { encryptedKey, iv, ciphertext, tag } all base64-encoded. |
| 15 | * |
| 16 | * Hybrid scheme (decrypt): |
| 17 | * 1. Decrypt AES key with RSA private key. |
| 18 | * 2. Decrypt ciphertext with AES-256-GCM. |
| 19 | */ |
| 20 | |
| 21 | const crypto = require('crypto'); |
| 22 | const fs = require('fs'); |
| 23 | const path = require('path'); |
| 24 | |
| 25 | const KEYS_DIR = path.join(__dirname, '../keys'); |
| 26 | const PRIV_KEY_PATH = path.join(KEYS_DIR, 'server.key'); |
| 27 | const PUB_KEY_PATH = path.join(KEYS_DIR, 'server.pub'); |
| 28 | |
| 29 | let _serverPrivateKey = null; |
| 30 | let _serverPublicKey = null; |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | // Key management |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | function ensureServerKeys () |
| 37 | { |
| 38 | if (!fs.existsSync(KEYS_DIR)) |
| 39 | { |
| 40 | fs.mkdirSync(KEYS_DIR, { recursive: true }); |
| 41 | } |
| 42 | |
| 43 | if (fs.existsSync(PRIV_KEY_PATH) && fs.existsSync(PUB_KEY_PATH)) |
| 44 | { |
| 45 | _serverPrivateKey = fs.readFileSync(PRIV_KEY_PATH, 'utf8'); |
| 46 | _serverPublicKey = fs.readFileSync(PUB_KEY_PATH, 'utf8'); |
| 47 | console.log('Server RSA keys loaded.'); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | console.log('Generating server RSA key pair (2048-bit) …'); |
| 52 | const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { |
| 53 | modulusLength: 2048, |
| 54 | publicKeyEncoding: { type: 'spki', format: 'pem' }, |
| 55 | privateKeyEncoding: { type: 'pkcs8', format: 'pem' }, |
| 56 | }); |
| 57 | |
| 58 | fs.writeFileSync(PRIV_KEY_PATH, privateKey, { mode: 0o600 }); |
| 59 | fs.writeFileSync(PUB_KEY_PATH, publicKey); |
| 60 | |
| 61 | _serverPrivateKey = privateKey; |
| 62 | _serverPublicKey = publicKey; |
| 63 | console.log('Server RSA key pair generated and saved.'); |
| 64 | } |
| 65 | |
| 66 | function getServerPublicKeyPem () |
| 67 | { |
| 68 | return _serverPublicKey; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the server public key as a JWK object (for the browser's |
| 73 | * SubtleCrypto.importKey({ format: 'jwk' }) call). |
| 74 | */ |
| 75 | function getServerPublicKeyJwk () |
| 76 | { |
| 77 | const keyObj = crypto.createPublicKey(_serverPublicKey); |
| 78 | return keyObj.export({ format: 'jwk' }); |
| 79 | } |
| 80 | |
| 81 | // --------------------------------------------------------------------------- |
| 82 | // Hybrid decrypt (server receives encrypted payload from client) |
| 83 | // --------------------------------------------------------------------------- |
| 84 | |
| 85 | /** |
| 86 | * payload: { encryptedKey, iv, ciphertext, tag } — all base64 strings |
| 87 | * Returns the decrypted plaintext string. |
| 88 | */ |
| 89 | function hybridDecrypt (payload) |
| 90 | { |
| 91 | const encryptedKey = Buffer.from(payload.encryptedKey, 'base64'); |
| 92 | const iv = Buffer.from(payload.iv, 'base64'); |
| 93 | const ciphertext = Buffer.from(payload.ciphertext, 'base64'); |
| 94 | const tag = Buffer.from(payload.tag, 'base64'); |
| 95 | |
| 96 | // 1. Decrypt AES key with server RSA private key |
| 97 | const aesKey = crypto.privateDecrypt( |
| 98 | { |
| 99 | key: _serverPrivateKey, |
| 100 | padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 101 | oaepHash: 'sha256', |
| 102 | }, |
| 103 | encryptedKey, |
| 104 | ); |
| 105 | |
| 106 | // 2. Decrypt ciphertext with AES-256-GCM |
| 107 | const decipher = crypto.createDecipheriv('aes-256-gcm', aesKey, iv); |
| 108 | decipher.setAuthTag(tag); |
| 109 | |
| 110 | const plain = Buffer.concat([decipher.update(ciphertext), decipher.final()]); |
| 111 | return plain.toString('utf8'); |
| 112 | } |
| 113 | |
| 114 | // --------------------------------------------------------------------------- |
| 115 | // Hybrid encrypt (server sends encrypted payload to client) |
| 116 | // --------------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Encrypts `plaintext` (string) for the recipient identified by their RSA |
| 120 | * public key in JWK format. |
| 121 | * Returns { encryptedKey, iv, ciphertext, tag } — all base64 strings. |
| 122 | */ |
| 123 | function hybridEncryptForClient (plaintext, clientPublicKeyJwk) |
| 124 | { |
| 125 | // Import client public key |
| 126 | const clientPubKey = crypto.createPublicKey({ key: clientPublicKeyJwk, format: 'jwk' }); |
| 127 | |
| 128 | // 1. Generate random AES-256 key + IV |
| 129 | const aesKey = crypto.randomBytes(32); |
| 130 | const iv = crypto.randomBytes(12); |
| 131 | |
| 132 | // 2. Encrypt plaintext with AES-256-GCM |
| 133 | const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv); |
| 134 | const ciphertext = Buffer.concat([ |
| 135 | cipher.update(Buffer.from(plaintext, 'utf8')), |
| 136 | cipher.final(), |
| 137 | ]); |
| 138 | const tag = cipher.getAuthTag(); |
| 139 | |
| 140 | // 3. Encrypt AES key with client's RSA public key |
| 141 | const encryptedKey = crypto.publicEncrypt( |
| 142 | { |
| 143 | key: clientPubKey, |
| 144 | padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 145 | oaepHash: 'sha256', |
| 146 | }, |
| 147 | aesKey, |
| 148 | ); |
| 149 | |
| 150 | return { |
| 151 | encryptedKey: encryptedKey.toString('base64'), |
| 152 | iv: iv.toString('base64'), |
| 153 | ciphertext: ciphertext.toString('base64'), |
| 154 | tag: tag.toString('base64'), |
| 155 | }; |
| 156 | } |
| 157 | |
| 158 | module.exports = { |
| 159 | ensureServerKeys, |
| 160 | getServerPublicKeyPem, |
| 161 | getServerPublicKeyJwk, |
| 162 | hybridDecrypt, |
| 163 | hybridEncryptForClient, |
| 164 | }; |