Keywords: OpenSSL | Key Generation | Passphrase Protection | Command Line | RSA Encryption
Abstract: This technical article provides a comprehensive guide on generating passphrase-protected RSA key pairs using OpenSSL from the command line. It begins by explaining the security implications of generating keys without passphrase protection, then details three methods for supplying passphrases: direct command-line arguments, file input, and standard input. The article includes step-by-step commands for generating encrypted private keys and extracting corresponding public keys, with security considerations for each approach. Practical examples and best practices help developers implement secure key generation in various environments.
Fundamentals of OpenSSL Key Generation
When generating keys with OpenSSL, passphrase protection serves as a critical security layer. If no passphrase is provided during key generation, the resulting private key file remains completely unencrypted. This means anyone with access to the file can use the private key directly, without any additional authentication. This unprotected state offers insufficient security against even casual attackers, as the key becomes fully exposed if the file is compromised.
Command-Line Passphrase Input Methods
OpenSSL offers multiple approaches for specifying passphrases via the command line, each with distinct security implications.
Direct Command-Line Arguments
The most straightforward method involves specifying the passphrase directly using the -passout pass:password parameter:
openssl genrsa -aes128 -passout pass:foobar -out privkey.pem 2048
While convenient, this approach carries significant security risks. On Unix-like systems, command-line arguments are typically visible to other processes, meaning the passphrase could be captured by other programs running on the same machine. Similar risks exist on Windows systems, particularly when using functions like exec() to execute commands.
File Input Method
A more secure alternative involves using a file to provide the passphrase:
openssl genrsa -aes128 -passout file:passphrase.txt 2048
This method requires writing the passphrase to a temporary file first, then protecting the file with appropriate permissions. The passphrase file should be deleted immediately after key generation to minimize exposure. File permissions should restrict access to the current user only, typically achieved with the chmod 600 passphrase.txt command.
Standard Input Method
Another secure approach utilizes standard input for passphrase entry:
openssl genrsa -aes128 -passout stdin 2048
When executing this command, the system prompts the user to enter the passphrase, which doesn't appear in command history or process lists. This method proves most secure in interactive environments but requires additional handling in automated scripts.
Complete Key Pair Generation Workflow
Generating a complete RSA key pair involves two distinct steps: first creating an encrypted private key, then extracting the corresponding public key from it.
Generating Encrypted Private Key
Create a 2048-bit RSA private key encrypted with AES-128:
openssl genrsa -aes128 -passout pass:foobar -out privkey.pem 2048
Here, -aes128 specifies the AES-128 encryption algorithm, -passout defines the passphrase output method, -out designates the output file, and the final number indicates the key length. Alternatively, -aes256 can be used for stronger encryption protection.
Extracting Public Key
Derive the corresponding public key from the encrypted private key file:
openssl rsa -in privkey.pem -passin pass:foobar -pubout -out pubkey.pem
The -passin parameter must match the passphrase used during private key generation; otherwise, decryption fails. The -pubout option directs output of the public key portion. Public key files don't require passphrase protection since they're designed to be publicly shared.
Security Best Practices
Selecting an appropriate passphrase input method requires careful consideration of the specific use case and security requirements.
For development environments or low-security scenarios, direct command-line arguments might suffice, but their security limitations must be acknowledged. In production environments or high-security contexts, file input or standard input methods should be prioritized, especially when commands need to be automated through scripts.
Passphrase files should reside in secure locations with proper file permissions and be promptly deleted after use. For long-term key storage, regular passphrase rotation is recommended, along with ensuring passphrase strength sufficient to resist brute-force attacks.
Encryption Algorithm Selection
OpenSSL supports various encryption algorithms for private key protection. Beyond the mentioned AES-128 and AES-256, other options include -aes192, -des3, and more. AES-256 currently offers the highest security level but incurs greater computational overhead. Algorithm selection involves balancing security needs against performance considerations.
Key length represents another crucial security parameter. While 2048-bit keys are currently considered secure, 3072-bit or longer keys are recommended for data requiring long-term protection. Longer keys provide enhanced security but increase computational costs during both generation and usage.