#!/usr/bin/sh 
# 
# mkcert.sh -- generate public key and certificate for SSL communications
#

# Step 1 Setup the environment variables with the appropriate values
# NOTE: Make sure the key password will never become known to 
#       others as it allows private key retrieval from the keystore.
#       

KEY_ALIAS=cimind
KEY_SIZE=2048
KEY_PASS=cimindkeypass
VAL_DAYS=180
KEYSTORE_NAME=cimind.key
TRUSTSTORE_NAME=cimind.trust
OWNER_DN="CN=Bapu Patil, OU=PAT, O=HPQ"
CERT_FILE=cimind.cert
CERT_PASS=cimindcertpass

rm -f $KEYSTORE_NAME $CERT_FILE $TRUSTSTORE_NAME

# Step 2 Create new public key in specified keystore

${JAVAPATH}/keytool -genkey \
    -alias $KEY_ALIAS \
    -dname "$OWNER_DN" \
    -keyalg rsa \
    -keysize $KEY_SIZE \
    -keypass $KEY_PASS \
    -validity $VAL_DAYS \
    -keystore $KEYSTORE_NAME \
    -storepass $KEY_PASS \
    -v

# Step 2 Export self-singed certificate from keystore

${JAVAPATH}/keytool -export \
    -alias $KEY_ALIAS \
    -file  $CERT_FILE \
    -keystore $KEYSTORE_NAME \
    -storepass $KEY_PASS \
    -rfc -v

# Step 3 Import certificate into truststore

${JAVAPATH}/keytool -import \
    -alias $KEY_ALIAS \
    -file  $CERT_FILE \
    -keystore $TRUSTSTORE_NAME \
    -storepass $CERT_PASS \
    -noprompt -v
