T09 · Insecure Skill Coding Practices
- Location
index.js:74- Finding
Incorrect RSA-OAEP plaintext-size calculation causes encryption failures
- Content
View full analysis
Vulnerability Details
File Location:
index.js:74-92
Vulnerability Type: Incorrect cryptographic boundary validation
Risk Level: Mediumjavascript 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) - 2For a 2048-bit RSA key and SHA-256, this is
256 - 64 - 2 = 190bytes. 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_SIZEconstant rather than inspecting the supplied public key. This produces additional incorrect decisions when callers use keys of a different modulus size.Attack Path
- An attacker supplies or causes the application to encrypt a message between 191 and 214 bytes.
- The size check incorrectly selects the direct RSA encryption branch.
crypto.publicEncryptattempts RSA-OAEP encryption with SHA-256.- OpenSSL rejects the oversized OAEP plaintext and throws an exception.
- 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
View remediation
Remediation Suggestions
- Derive the modulus length from
recipientPublicKeyrather than using the fixedRSA_KEY_SIZEconstant. - 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.
- Derive the modulus length from
