From 1fee4498061ad145fa11f9ab5f8f9e061d92f5ee Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Wed, 25 Nov 2015 06:55:34 +0200
Subject: [PATCH] init now creates the key and cert in 2 steps, skipping any
 file that already exists.

---
 Makefile |  2 +-
 ssl-ca   | 43 +++++++++++++++++++++++++++++--------------
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index 1b7d0e0..6c6d41a 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ install:
 	chmod 755 /usr/local/bin/ssl-ca
 
 clean:
-	rm -rf openssl.cnf certs keys CA.key CA.crt
+	rm -rf openssl.cnf certs keys CA.key CA.crt CA.p12
 
 test: clean
 	./ssl-ca init
diff --git a/ssl-ca b/ssl-ca
index d3fced5..d4952df 100755
--- a/ssl-ca
+++ b/ssl-ca
@@ -59,20 +59,35 @@ init () {
     else
         echo "$config" > "openssl.cnf"
     fi
-    openssl req \
-        -x509 \
-        -config openssl.cnf \
-        -new \
-        -newkey rsa:2048 \
-        -keyout CA.key \
-        -extensions v3_ca \
-        -out CA.crt
-    openssl pkcs12 \
-        -export \
-        -in CA.crt \
-        -inkey CA.key \
-        -out CA.p12 \
-        -passout pass:
+    if [ -e CA.key ]
+    then
+        echo CA.key already exists, skipping.
+    else
+        openssl genrsa -out CA.key 2048
+    fi
+    if [ -e CA.crt ]
+    then
+        echo CA.crt already exists, skipping.
+    else
+        openssl req \
+            -x509 \
+            -config openssl.cnf \
+            -new \
+            -key CA.key \
+            -extensions v3_ca \
+            -out CA.crt
+    fi
+    if [ -e CA.p12 ]
+    then
+        echo PKCS12 file already exists, skipping.
+    else
+        openssl pkcs12 \
+            -export \
+            -in CA.crt \
+            -inkey CA.key \
+            -out CA.p12 \
+            -passout pass:
+    fi
 }
 
 sign_key () {
-- 
GitLab