58

I want to silently, non interactively, create an SSL certificate. I.e., without get prompted for any data.

The normal way I create the certificate would be:

openssl req -x509 -nodes -days 7300 -newkey rsa:2048 \
    -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem 

I tried the following:

openssl genrsa -out server.key 2048
touch openssl.cnf

cat >> openssl.cnf <<EOF
[ req ]
prompt = no
distinguished_name = req_distinguished_name

[ req_distinguished_name ]
C = GB
ST = Test State
L = Test Locality
O = Org Name
OU = Org Unit Name
CN = Common Name
emailAddress = [email protected]
EOF

openssl req -x509 -config openssl.cnf -nodes -days 7300 \
    -signkey server.key -out /etc/ssl/private/pure-ftpd.pem 

But I still get a prompt for data.

Mateusz Piotrowski
  • 4,623
  • 5
  • 36
  • 70
TheNiceGuy
  • 845
  • 2
  • 7
  • 10
  • 1
    Can you provide an explanation or output of what is happening instead of the desired result? – phemmer Dec 08 '13 at 01:48
  • I get the help as a output- Anything is wrong with the parameters here: `openssl req -x509 -config openssl.cnf -nodes -days 7300 -signkey server.key -out /etc/ssl/private/pure-ftpd.pem ` – TheNiceGuy Dec 08 '13 at 02:17
  • It's best to provide the output of errors when you're having issues. I'm guessing your issue is because of `-signkey`. This is not a valid `openssl req` option on my system. The error message will have this as the very first line: `unknown option -signkey` – phemmer Dec 08 '13 at 03:36
  • Well that signkey should tell SSL to use the provided key as i know? – TheNiceGuy Dec 08 '13 at 03:53
  • Your "normal way to create the certificate" doesn't prompt for any data. Are you not wanting it to output anything? Then use `2> /dev/null`. – wingedsubmariner Dec 08 '13 at 05:50
  • How does this not work? What behavior do you see, what did you expect instead? If there are error messages, copy-paste them. If the output files are not what you wanted, how do they differ? – Gilles 'SO- stop being evil' Dec 08 '13 at 21:53

2 Answers2

116

The thing you're missing is to include the certificate subject in the -subj flag. I prefer this to creating a config file because it's easier to integrate into a workflow and doesn't require cleaning up afterward.

One step key and csr generation:

openssl req -new -newkey rsa:4096 -nodes \
    -keyout www.example.com.key -out www.example.com.csr \
    -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com"

One step self signed passwordless certificate generation:

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
    -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" \
    -keyout www.example.com.key  -out www.example.com.cert

Neither of these commands will prompt for any data.

See my answer to this nearly identical question on Super User.


After many years, and by popular demand, here's how to do it with ECDSA.

This is necessarily two steps because EC keys require generating parameters, which (at the time of this writing) must be done separately from signing request*.

openssl ecparam -out www.example.com.key -name prime256v1 -genkey
openssl req -new -days 365 -nodes -x509 \
    -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" \
    -key www.example.com.key -out www.example.com.cert

* You can either just generate ec parameters and use req -newkey ec:<file with ec params>, or do it like I did above. There isn't really a significant difference.

bahamat
  • 38,658
  • 4
  • 70
  • 103
4

The command you are looking for is:

openssl req -new -x509 -config openssl.cnf -nodes -days 7300 -key server.key -out /etc/ssl/private/pure-ftpd.pem

Changes from your version:

  • -new is required to generate anything
  • -key used in place of -signkey
bahamat
  • 38,658
  • 4
  • 70
  • 103
robbat2
  • 3,599
  • 20
  • 32