Is it possible to create a certificate with openssl setting common and alt names not in…
- User
Is it possible to create a certificate with openssl setting common and alt names not in config but in options for openssl command?
- Assistant
Yes, it is possible to create a certificate with OpenSSL by specifying the common name (CN) and subject alternative names (SANs) directly in the command line without using a configuration file, although it requires some additional steps or scripts since OpenSSL does not natively support passing SANs directly via command line options.
Here's a general approach using OpenSSL with a temporary configuration file or inline configuration:
-
Create a Temporary Configuration File Inline: You can create a temporary configuration file that includes your required SANs and use it directly in your OpenSSL command.
For example:
SAN="DNS:example.com,DNS:www.example.com" \ openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr \ -subj "/CN=example.com" \ -reqexts SAN -config <(cat /etc/ssl/openssl.cnf \ <(printf "[SAN]\nsubjectAltName=$SAN"))This command generates a new CSR with a private key and includes SANs by using a temporary configuration file, created inline with the
<(...)process substitution. -
Using Environment Variables: Some systems allow the use of environment variables to replace parts of the OpenSSL configuration, but this is not consistent across all OpenSSL versions.
-
OpenSSL 1.1.1+ Usage: With recent versions of OpenSSL (1.1.1 and later), you can use the
-addextoption to add extensions directly on the command line, like SANs:openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr \ -subj "/CN=example.com" \ -addext "subjectAltName = DNS:example.com, DNS:www.example.com"
Note that using
-addextis a bit simpler and more direct if your OpenSSL version supports it. Always verify which version of OpenSSL you are using to determine the best method to employ, and read the man pages or documentation to understand the nuances and limitations of each version. -
- User
-subj "nymame" is ok but -addext “subjectAltName = DNS:example.com, DNS:www.example.com” results error.
- Assistant
If you're encountering issues when using the
-addextflag with OpenSSL, it could be due to a version incompatibility or syntax issue. Here's a step-by-step approach to troubleshoot and ensure proper usage:Verify OpenSSL Version
First, check your OpenSSL version because the
-addextoption is available starting from OpenSSL 1.1.1:openssl versionIf your version is earlier than 1.1.1, the
-addextoption won't be available and you would need to upgrade your OpenSSL installation.Correct Syntax for
-addextIf you're using OpenSSL 1.1.1 or later, ensure that you are typing the command correctly. Here is the correct syntax:
openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr \ -subj "/CN=example.com" \ -addext "subjectAltName=DNS:example.com,DNS:www.example.com"Common Issues
-
Quotation Marks: Ensure that you are using the correct quotation marks around the
-addextparameter. Depending on your shell, you might need to adjust them. Try replacing the curly quotes with straight quotes if copied from a formatted source:- Use
"or'for quoting, ensuring there are no angled or curly quotes from rich text editors.
- Use
-
Shell Compatibility: Your shell might interpret certain characters, like parentheses or commas, differently. Make sure you’re running the command in a standard shell environment.
-
OpenSSL Error Messages: If there's a specific error message, it can give clues to the issue. Common errors related to
-addextare often related to syntax or features not available in older OpenSSL versions.
Alternative Method for Older Versions
If upgrading OpenSSL is not an option, you might need to revert to using a temporary configuration file:
-
Create Configuration File:
cat > san.cnf <<EOF [req] distinguished_name = req_distinguished_name req_extensions = v3_req prompt = no [req_distinguished_name] CN = example.com [v3_req] keyUsage = critical, digitalSignature, keyEncipherment extendedKeyUsage = serverAuth subjectAltName = @alt_names [alt_names] DNS.1 = example.com DNS.2 = www.example.com EOF -
Generate CSR:
openssl req -new -newkey rsa:2048 -nodes -keyout example.key -out example.csr -config san.cnf
This approach guarantees compatibility since it leverages OpenSSL’s robust configuration file mechanism. Adjust file paths and DNS names according to your requirements.
-