How to fix Error: DH parameter is too small in Node.js

Node.jsINTERMEDIATEHIGHUpdated Dec 9, 2025ยท 210 views

This error occurs when a Node.js TLS client refuses to connect because the server offers Diffie-Hellman key exchange parameters smaller than the minimum security threshold (typically 1024 bits). Modern OpenSSL versions reject weak DH groups to protect against cryptographic attacks like Logjam.

Quick fix

Fixes DH_KEY_TOO_SMALL

# Test the TLS connection
node -e "require('https').get('https://example.com', (res) => console.log('OK'))"
  1. Identify the server with weak DH parameters
  2. Fix the server configuration (recommended)
  3. Adjust minDHSize on the client (temporary workaround)
  4. Disable DHE ciphers and use ECDHE instead
node.js โ€” error
# Test the TLS connection
node -e "require('https').get('https://example.com', (res) => console.log('OK'))"
Error: DH parameter is too small

What this error means

This error indicates that the TLS/SSL handshake failed because the server attempted to use Diffie-Hellman (DH) key exchange parameters that are too small to meet current security standards. Diffie-Hellman is a cryptographic protocol used to establish shared secrets over an insecure channel during TLS connections. Modern versions of OpenSSL (which Node.js uses for TLS) have minimum security requirements for DH parameters. OpenSSL 1.1.0 and later reject DH keys smaller than 1024 bits by default, and best practices recommend 2048 bits or higher. When a server offers weak DH parameters (512 or 768 bits), the Node.js client terminates the connection to prevent potential security vulnerabilities. This is a deliberate security measure introduced to protect against the Logjam attack, a cryptographic vulnerability that allows attackers to downgrade TLS connections and potentially decrypt traffic when weak DH groups are used.

Typical symptoms

  • TLS/SSL connection fails when making HTTPS requests to certain servers
  • Error message contains "dh key too small" or "DH parameter is too small"
  • OpenSSL error code SSL_R_DH_KEY_TOO_SMALL appears in logs
  • Connection succeeds with older Node.js versions but fails with newer ones
  • npm install or package downloads fail with TLS errors
  • Docker containers running Node.js applications cannot connect to external services

Common causes

  1. The remote server is configured with weak DH parameters (512 or 768 bits)
  2. The server is using outdated TLS configuration that predates modern security standards
  3. Legacy systems or embedded devices with old OpenSSL/TLS implementations
  4. The server administrator has not updated DH parameters since the Logjam vulnerability disclosure
  5. Proxy or load balancer in the connection path uses weak DH groups
  6. OpenSSL security level is set to 2 or higher, enforcing stricter requirements

How to fix "Error: DH parameter is too small"

1Identify the server with weak DH parameters

First, verify which server is causing the issue. The error usually includes the hostname or IP address:

bash
# Test the TLS connection
node -e "require('https').get('https://example.com', (res) => console.log('OK'))"

If you see the DH key error, that server needs to be fixed. You can also use OpenSSL directly to test:

bash
openssl s_client -connect example.com:443 -cipher "DHE"
2Fix the server configuration (recommended)

The best solution is to upgrade the server's DH parameters to at least 2048 bits. Generate new DH parameters:

bash
openssl dhparam -out /path/to/dhparam.pem 2048

Then configure your web server to use them. For Nginx:

nginx
ssl_dhparam /path/to/dhparam.pem;

For Apache:

apache
SSLOpenSSLConfCmd DHParameters /path/to/dhparam.pem

For Node.js TLS servers:

javascript
const tls = require('tls');
const fs = require('fs');

const options = {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem'),
  dhparam: fs.readFileSync('dhparam.pem')
};

tls.createServer(options, (socket) => {
  // server code
});
3Adjust minDHSize on the client (temporary workaround)

If you cannot control the server, you can temporarily lower the minimum DH size on the client side. Warning: This reduces security.

javascript
const https = require('https');
const tls = require('tls');

const options = {
  hostname: 'example.com',
  port: 443,
  path: '/',
  method: 'GET',
  minDHSize: 512  // Lower threshold (not recommended for production)
};

https.get(options, (res) => {
  console.log('Connected');
});

Or set it globally:

javascript
process.env.NODE_OPTIONS = '--tls-min-dh-size=512';
4Disable DHE ciphers and use ECDHE instead

A more secure alternative is to disable DHE ciphers entirely and rely on ECDHE (Elliptic Curve Diffie-Hellman):

javascript
const https = require('https');

const options = {
  hostname: 'example.com',
  port: 443,
  path: '/',
  method: 'GET',
  ciphers: 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:!DHE'
};

https.get(options, (res) => {
  console.log('Connected using ECDHE');
});

This approach maintains strong security by avoiding weak DH groups altogether.

5Lower OpenSSL security level (not recommended)

As a last resort for testing only, you can lower the OpenSSL security level. This should never be used in production.

In Docker:

dockerfile
FROM node:18
RUN sed -i "s/SECLEVEL=2/SECLEVEL=1/g" /etc/ssl/openssl.cnf

Or set via environment variable:

bash
export OPENSSL_CONF=/path/to/custom-openssl.cnf

This allows connections to servers with 1024-bit DH parameters but should only be temporary.

Advanced notes

Logjam Vulnerability Context

The Logjam attack (CVE-2015-4000) demonstrated that 512-bit and 768-bit DH groups are vulnerable to downgrade attacks. An attacker with sufficient computational resources can break these weak groups and decrypt TLS traffic. This led to OpenSSL and other TLS implementations raising minimum DH size requirements.

Security Level Settings

OpenSSL 1.1.0 introduced security levels:
- Level 1: 80-bit security, allows 1024-bit DH (deprecated)
- Level 2: 112-bit security, requires 2048-bit DH (default)
- Level 3: 128-bit security, requires 3072-bit DH

Node.js typically defaults to Level 2, which is why 1024-bit DH parameters are rejected.

DH vs ECDHE Performance

ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) offers equivalent security with much smaller key sizes and better performance. A 256-bit ECDHE key provides roughly equivalent security to a 3072-bit DH key. Modern TLS configurations should prefer ECDHE over traditional DHE.

Cipher Suite Recommendations

Prefer cipher suites in this order:
1. ECDHE with AES-GCM (e.g., ECDHE-RSA-AES256-GCM-SHA384)
2. ECDHE with ChaCha20-Poly1305
3. Traditional DH with 2048+ bit parameters (legacy compatibility only)

Enterprise Considerations

In enterprise environments with legacy systems, a phased approach works best:
1. Audit all systems to identify those using weak DH parameters
2. Upgrade servers to 2048-bit DH parameters
3. Transition to ECDHE-only cipher suites
4. Remove DHE cipher support entirely once all clients are updated

Frequently asked questions

What does "DH_KEY_TOO_SMALL" mean?
This error indicates that the TLS/SSL handshake failed because the server attempted to use Diffie-Hellman (DH) key exchange parameters that are too small to meet current security standards. Diffie-Hellman is a cryptographic protocol used to establish shared secrets over an insecure channel during TLS connections. js uses for TLS) have minimum security requirements for DH parameters.
How do I fix "DH_KEY_TOO_SMALL"?
This error occurs when a Node.js TLS client refuses to connect because the server offers Diffie-Hellman key exchange parameters smaller than the minimum security threshold (typically 1024 bits). Modern OpenSSL versions reject weak DH groups to protect against cryptographic attacks like Logjam.
What causes "DH_KEY_TOO_SMALL"?
The remote server is configured with weak DH parameters (512 or 768 bits) The server is using outdated TLS configuration that predates modern security standards Legacy systems or embedded devices with old OpenSSL/TLS implementations
Is the "DH_KEY_TOO_SMALL" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading