I'm trying to create bash script to generate certificates via openssl in a loop with different parameters. Of course it's easy to write many lines of code with different parameters, something like:
#!/bin/bash
openssl req -x509 -sha256 -nodes -days 1 -newkey rsa:1024 -keyout private1024_1.key -out RSA_1024_1_SHA256.crt -subj "/C=XX/ST=XXX/L=XXX /O=xxx/OU=xxx/CN=xx.xx/emailAddress=xx@xx.xx"
openssl req -x509 -sha256 -nodes -days 3 -newkey rsa:1024 -keyout private1024_3.key -out RSA_1024_3_SHA256.crt -subj "/C=XX/ST=XXX/L=XXX /O=xxx/OU=xxx/CN=xx.xx/emailAddress=xx@xx.xx"
but I don't think that's a good practice. So I would like to create some loops with variables where I can set:
- number of days;
- type of rsa(1024/2048/4096);
- name of private key according to type of rsa and numbers of day;
- name of certificate according to type of rsa and numbers of day.
About -subj I think just make a separate table of variables and change them if need. I'm not so close with coding, but hope that it's possible to realize this idea with loops.
I will be grateful for any tips or patterns.
Realize next(and it's working):
#!/bin/bash
# Certificate details; replace items in angle brackets with your own info
subj="
C=XX
ST=XXX
O=XXXX
localityName=XXX xx
commonName=xxx.xx
organizationalUnitName=xxx xx
emailAddress=test@xxx.xx
"
declare -a days=(1 3 5 10 15 30 365)
declare -a rsatype=(1024 2048 4096)
declare -a sha=(sha1 md5 sha256 sha512)
dd=7
rst=3
shat=4
for ((i = 0; i < dd; i++))
do
for((j = 0; j < rst; j++))
do
for((k = 0; k < shat; k++))
do
keyout=private${rsatype[$j]}_${days[$i]}.key
out=RSA_${rsatype[$j]}_${days[$i]}_${sha[$k]}.crt
openssl req -x509 -${sha[$k]} -nodes -days ${days[$i]} -newkey rsa:"${rsatype[$j]}" -keyout "$keyout" -out "$out" -subj "$(echo -n "$subj" | tr "\n" "/")"
done
done
done