T09 · Insecure Skill Coding Practices
Warning
- Location
- index.js:74
- Finding
- Incorrect RSA-OAEP plaintext-size calculation causes encryption failures## Vulnerability Details **File Location**: `index.js:74-92` **Vulnerability Type**: Incorrect cryptographic boundary validation **Risk Level**: Medium ```javascript const maxDirectSize = (RSA_KEY_SIZE / 8) - 42; // PKCS#1 v1.5 padding overhead if (messageBuffer.length <= maxDirectSize) { // Direct RSA encryption for small messages const encrypted = crypto.publicEncrypt( { key: recipientPublicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: HASH_ALGORITHM }, messageBuffer ); return { type: 'direct', encrypted: encrypted.toString('base64'), algorithm: 'RSA-OAEP' }; } ``` ### Technical Analysis The implementation calculates the maximum direct RSA plaintext size by subtracting 42 bytes, which is the OAEP overhead associated with SHA-1. However, encryption is configured to use SHA-256. The RSA-OAEP maximum plaintext size is: `modulusBytes - (2 × hashLength) - 2` For a 2048-bit RSA key and SHA-256, this is `256 - 64 - 2 = 190` bytes. The implementation incorrectly allows up to 214 bytes. Consequently, messages between 191 and 214 bytes are routed to direct RSA encryption and rejected by the underlying cryptographic implementation. The calculation also relies on the fixed `RSA_KEY_SIZE` constant rather than inspecting the supplied public key. This produces additional incorrect decisions when callers use keys of a different modulus size. ### Attack Path 1. An attacker supplies or causes the application to encrypt a message between 191 and 214 bytes. 2. The size check incorrectly selects the direct RSA encryption branch. 3. `crypto.publicEncrypt` attempts RSA-OAEP encryption with SHA-256. 4. OpenSSL rejects the oversized OAEP plaintext and throws an exception. 5. If the caller does not isolate the exception, the current operation fails and may terminate a request handler or service process. ### Impact Assessment ...[truncated 362 chars]
- Remediation
- ## Remediation Suggestions - Derive the modulus length from `recipientPublicKey` rather than using the fixed `RSA_KEY_SIZE` constant. - Calculate the OAEP boundary using the configured SHA-256 digest length: `modulusBytes - (2 * 32) - 2`. - Prefer hybrid RSA-OAEP and AES-GCM encryption for all message sizes, eliminating the fragile direct-encryption branch. - Validate inputs and convert cryptographic exceptions into controlled application errors. - Add boundary tests for 189, 190, 191, 213, and 214-byte messages and for every supported RSA key size.
