Shell Script to provide "answers" to SSL Cert Request


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to provide "answers" to SSL Cert Request
# 1  
Old 04-15-2009
Shell Script to provide "answers" to SSL Cert Request

Hello,

I need assistance with creating a shell script to generate SSL Certificate Requests on remote hosts. Below is my stab at this, but I cannot figure out how to pass the requested arguments into the openssl command correctly. I have a major problem with redirecting the "answers" into the openssl cert request. "hostlist" would contain any hosts that need the certificate signed.


Code:
#!/bin/sh
#
Country=US
State=CA
City=San Jose
Organization=Engineering
Host=""
Email=" "
for i in `cat hostlist`
do
  Host="$i" 
  ssh $i "sudo openssl genrsa -rand -des3 -out /tmp/serverkey.$i 1024 -config /use/share/ssl/openssl.cnf"
  echo $Country > /tmp/cert-data
  echo $State >> /tmp/cert-data
  echo $City >> /tmp/cert-data
  echo $Organization >> /tmp/cert-data
  echo $Host >> /tmp/cert-data
  echo $Email >> /tmp/cert-data
  scp /tmp/cert-data certuser@$i:/tmp/cert-data
  ssh $i sudo openssl req -new -key /tmp/serverkey.$i -out /tmp/server.csr.$i -config /usr/share/ssl/openssl.cnf < /tmp/cert-data
done


Once I get that to work, I can scp the /tmp/serverkey.$i to my Cert Authority and sign it.

Thank You.

Last edited by azvelocat; 04-15-2009 at 06:16 PM..
# 2  
Old 04-15-2009
You can use "-batch" openssl option to avoid being prompted for values when generating CSR.

Generate CSR and KEY files on local system and scp them instead of running the command on a remote system. You may also modify your original script with commands provided to perform the tasks remotely if for some reason that is a requirement:
  • Save the below as openssl.cnf and edit the last section to match your requirements. In your FOR loop for each server you need to update the value of commonName_default to servers FQDN.
Code:
#-------------openssl.cnf----------------
[ req ]
default_bits = 1024 # Size of keys
default_keyfile = key.pem # name of generated keys
default_md = md5 # message digest algorithm
string_mask = nombstr # permitted characters
distinguished_name = req_distinguished_name

[ req_distinguished_name ]
# Variable name   Prompt string
0.organizationName = Organization Name (company)
organizationalUnitName = Organizational Unit Name (department, division)
emailAddress = Email Address
emailAddress_max = 40
localityName = Locality Name (city, district)
stateOrProvinceName = State or Province Name (full name)
countryName = Country Name (2 letter code)
countryName_min = 2
countryName_max = 2
commonName = Common Name (hostname, IP, or your name)
commonName_max = 64


#-------------------Edit this section------------------------------
countryName_default     = US
stateOrProvinceName_default = CA
localityName_default        = San Francisco
0.organizationName_default  = example_company
organizationalUnitName_default  = Information Systems
commonName_default          = server_name
emailAddress_default            = admin@example.com

  • Generate key
Code:
openssl genrsa -out <hostname>.key 1024

  • Generate Certificate Signing Request
Code:
openssl req -new -nodes -key <hostname>.key -out <hostname>.csr -config openssl.cnf -batch

  • Copy CSR and KEY files
Code:
scp <hostname>.key certuser@hostname:/<path>
scp <hostname>.csr certuser@hostname:/<path>

  • Delete files from local system.
Code:
rm -f <hostname>.key
rm -f <hostname>.csr


Last edited by yonix; 04-15-2009 at 10:17 PM..
# 3  
Old 04-16-2009
Awesome yonix...batch mode kicked butt.

I had to create the following to dynamically update my openssl.cnf file for each host when generating my cert signing request (all of the $VARs are just locations to files and dirs):

Code:
for i in `cat $HOSTLIST`
do
  echo '======'
  echo "$i"
  /usr/local/bin/sudo sh -c " sed 's/commonName_default          = .*/commonName_default          = $i/g' $OPENSSLCNF > $SIGNREQS/tmpfile && /usr/local/bin/sudo mv $SIGNREQS/tmpfile $OPENSSLCNF"
  /usr/local/bin/sudo openssl req -new -nodes -key $CERTREQS/$i.key -out $SIGNREQS/$i.csr -config $OPENSSLCNF -batch
done

Also I used the "yes" command when running my signing script, as I didn't want to have to enter "y's" over and over.... like this:

Code:
   yes | /usr/local/bin/sudo ./sign-certreq


This worked out great. Thanks for the assistance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

3. Shell Programming and Scripting

script that answers y unless output has a string "STRING" in it

Hi all, I have the following script which I use to chek the output of jobs submitted to a PBS server. #!/bin/sh # #recover.sh # check() { echo "Do you want to proceed?" read answer if ; then echo "... proceeding ..." else echo "--------- Aborting -----------"... (0 Replies)
Discussion started by: faizlo
0 Replies

4. OS X (Apple)

Weird "security" bahavior with SSL certificates

Hello, I have been attempting to automate the addition of SSL certificates to keychains on a MAC using the "security" command. I've noticed two things, 1 of which I don't understand. 1. If I use something like "security add-trusted-cert -d -k /System/Library/Keychains/SystemRootCertificates... (1 Reply)
Discussion started by: prafulnama
1 Replies

5. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

9. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies

10. Shell Programming and Scripting

how to request a "read" or "delivered" receipt for mails

Dears, I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails. The script send mails on behalve of an specific... (1 Reply)
Discussion started by: plelie2
1 Replies
Login or Register to Ask a Question