gentestcert.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/bash
  2. # Script for generating RSA CA and server certs based on it.
  3. #
  4. SERVER_PEM='test_sign_bynoca_srv.pem'
  5. INTCA_PEM='test_int_not_cacert.pem'
  6. CA_PEM='test_ca.pem'
  7. CURRENT=$(cd $(dirname $0);pwd)
  8. # OpenSSL configuration files
  9. OPENSSL_BASE_CA_CONF='wolfssl_base.conf'
  10. OPENSSL_CA_CONF='wolfssl_ca.conf'
  11. OPENSSL_INTCA_CONF='wolfssl_int_ca.conf'
  12. OPENSSL_SRV_CONF='wolfssl_srv.conf'
  13. # SEt ver
  14. CA_NAME="test_ca"
  15. INTCA_NAME="int_ca"
  16. SRVCERT_NAME="server_ext"
  17. CRT_HOSTNAME="WOLFSSL"
  18. CRT_DN="${CRT_HOSTNAME%% *}"
  19. CRT_ALT_NAME="$(echo $CRT_HOSTNAME | sed -e "s/^/DNS:/" -e "s/ /,DNS:/g")"
  20. CA_HOME=$(cd $(dirname $0);pwd)/pki/$CA_NAME
  21. INT_CA_HOME="$CA_HOME/gen_int/$CRT_DN"
  22. SRV_CRT_HOME="$CA_HOME/gen_srv/$CRT_DN"
  23. Prepare_folder_file(){
  24. mkdir -m 700 pki
  25. # Create folders for CA
  26. mkdir "$CA_HOME"/{,certs,db,gen_srv,gen_int}
  27. mkdir -m 700 "$CA_HOME/private"
  28. # Create folders for Intermediate CA
  29. mkdir "$INT_CA_HOME"
  30. mkdir "$INT_CA_HOME"/{,certs,db}
  31. mkdir -m 700 "$INT_CA_HOME/private"
  32. # Create folders for Server
  33. mkdir "$SRV_CRT_HOME"
  34. mkdir -m 700 "$SRV_CRT_HOME/private"
  35. # Create and populate openssl CA files
  36. touch "$CA_HOME"/db/index
  37. openssl rand -hex 16 > "$CA_HOME"/db/serial
  38. touch "$INT_CA_HOME"/db/index
  39. openssl rand -hex 16 > "$INT_CA_HOME"/db/serial
  40. # Copy openssl config and private key
  41. cp "$OPENSSL_CA_CONF" "$CA_HOME"
  42. cp ./"$CA_NAME".key ./pki/$CA_NAME/private/"$CA_NAME".key
  43. cp "$OPENSSL_INTCA_CONF" "$INT_CA_HOME"
  44. cp ./"$INTCA_NAME".key "$INT_CA_HOME"/private/"$INTCA_NAME".key
  45. cp "$OPENSSL_SRV_CONF" "$SRV_CRT_HOME"
  46. cp ./server.key "$SRV_CRT_HOME"/private/server.key
  47. }
  48. Generate_conf(){
  49. # copy conf from base
  50. cp $OPENSSL_BASE_CA_CONF $OPENSSL_CA_CONF
  51. cp $OPENSSL_BASE_CA_CONF $OPENSSL_INTCA_CONF
  52. # Replace contents
  53. # For CA
  54. sed -i "s/_CA_NAME_/$CA_NAME/" "$OPENSSL_CA_CONF"
  55. sed -i "s/_CERT_NAME_/$INTCA_NAME/" "$OPENSSL_CA_CONF"
  56. sed -i "s/_CA_DEPART_/Development/" "$OPENSSL_CA_CONF"
  57. # For Intermediate CA
  58. sed -i "s/_CA_NAME_/$INTCA_NAME/" "$OPENSSL_INTCA_CONF"
  59. sed -i "s/_CERT_NAME_/$SRVCERT_NAME/" "$OPENSSL_INTCA_CONF"
  60. sed -i "s/_CA_DEPART_/Product_Support/" "$OPENSSL_INTCA_CONF"
  61. }
  62. cleanup_files(){
  63. rm -f wolfssl_ca.conf
  64. rm -f wolfssl_int_ca.conf
  65. rm -rf pki/
  66. }
  67. # clean up
  68. if [ "$1" = "clean" ]; then
  69. echo "Cleaning temp files"
  70. cleanup_files
  71. exit 0
  72. fi
  73. if [ "$1" = "cleanall" ]; then
  74. echo "Cleaning all files"
  75. rm -f ./"$SERVER_PEM"
  76. rm -f ./"$INTCA_PEM"
  77. rm -f ./"$CA_PEM"
  78. cleanup_files
  79. exit 0
  80. fi
  81. # Generate OpenSSL Conf files
  82. Generate_conf
  83. # Prepare folders and files
  84. Prepare_folder_file
  85. ##########################################
  86. ## Create CA, Intermediate and Server Cert
  87. ##########################################
  88. # Generate CA
  89. cd "$CA_HOME"
  90. # Generate CA private key and csr - use config file info
  91. openssl req -new -config "$OPENSSL_CA_CONF" \
  92. -out "$CA_NAME.csr" -key "private/$CA_NAME.key"
  93. # Self-sign CA certificate - use config file info
  94. # Note: Use extension from config "ca_ext" section
  95. openssl ca -selfsign -config "$OPENSSL_CA_CONF" \
  96. -notext -in "$CA_NAME.csr" -out "$CA_NAME.crt" -extensions ca_ext -batch
  97. # Generate Intermediate CA
  98. # cd into Cert generation folder
  99. cd "$INT_CA_HOME"
  100. # Create private key and csr
  101. openssl req -new -config "$OPENSSL_INTCA_CONF" \
  102. -out "$INTCA_NAME.csr" -key "private/$INTCA_NAME.key"
  103. cd "$CA_HOME"
  104. # Sign certificate with CA
  105. openssl ca -config "$OPENSSL_CA_CONF" -notext \
  106. -in "$INT_CA_HOME/$INTCA_NAME.csr" -out "$INT_CA_HOME/$INTCA_NAME.crt" \
  107. -extensions "$INTCA_NAME" -batch
  108. # cd into Cert generation folder
  109. cd "$SRV_CRT_HOME"
  110. # Create private key and csr
  111. openssl req -new -config "$OPENSSL_SRV_CONF" \
  112. -out server.csr -key private/server.key
  113. # cd into intermediate CA home
  114. cd "$CA_HOME/gen_int/WOLFSSL/"
  115. # Sign certificate with CA
  116. openssl ca -config "$OPENSSL_INTCA_CONF" -notext \
  117. -in "$SRV_CRT_HOME/server.csr" -out "$SRV_CRT_HOME/server.crt" \
  118. -extensions server_ext -batch
  119. # cp generate certificates
  120. cd $CURRENT
  121. # CA
  122. openssl x509 -in ./pki/$CA_NAME/$CA_NAME.crt -inform PEM -noout -text > ./pki/$CA_NAME/$CA_NAME.pem
  123. cat ./pki/$CA_NAME/$CA_NAME.crt >> ./pki/$CA_NAME/$CA_NAME.pem
  124. mv ./pki/$CA_NAME/$CA_NAME.pem $CA_PEM
  125. # Intermediate CA
  126. openssl x509 -in $INT_CA_HOME/$INTCA_NAME.crt -inform PEM -noout -text > $INT_CA_HOME/$INTCA_NAME.pem
  127. cat $INT_CA_HOME/$INTCA_NAME.crt >> $INT_CA_HOME/$INTCA_NAME.pem
  128. mv $INT_CA_HOME/$INTCA_NAME.pem $INTCA_PEM
  129. # Server
  130. openssl x509 -in $SRV_CRT_HOME/server.crt -inform PEM -noout -text > $SRV_CRT_HOME/server.pem
  131. cat $SRV_CRT_HOME/server.crt >> $SRV_CRT_HOME/server.pem
  132. mv $SRV_CRT_HOME/server.pem $SERVER_PEM
  133. # clean up
  134. cleanup_files
  135. echo "Completed"