genserv.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) EdelWeb for EdelKey and OpenEvidence
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. OPENSSL=openssl
  26. if [ -f /usr/local/ssl/bin/openssl ] ; then
  27. OPENSSL=/usr/local/ssl/bin/openssl
  28. fi
  29. USAGE="echo Usage is genserv.sh <prefix> <caprefix>"
  30. # exit on first fail
  31. set -e
  32. HOME=`pwd`
  33. cd $HOME
  34. KEYSIZE=2048
  35. DURATION=3000
  36. # The -sha256 option was introduced in OpenSSL 1.0.1
  37. DIGESTALGO=-sha256
  38. REQ=YES
  39. P12=NO
  40. DHP=NO
  41. PREFIX=$1
  42. if [ ".$PREFIX" = . ] ; then
  43. echo No configuration prefix
  44. NOTOK=1
  45. else
  46. if [ ! -f $PREFIX-sv.prm ] ; then
  47. echo No configuration file $PREFIX-sv.prm
  48. NOTOK=1
  49. fi
  50. fi
  51. CAPREFIX=$2
  52. if [ ".$CAPREFIX" = . ] ; then
  53. echo No CA prefix
  54. NOTOK=1
  55. else
  56. if [ ! -f $CAPREFIX-ca.cacert ] ; then
  57. echo No CA certificate file $CAPREFIX-ca.caert
  58. NOTOK=1
  59. fi
  60. if [ ! -f $CAPREFIX-ca.key ] ; then
  61. echo No $CAPREFIX key
  62. NOTOK=1
  63. fi
  64. fi
  65. if [ ".$NOTOK" != . ] ; then
  66. echo "Sorry, I can't do that for you."
  67. $USAGE
  68. exit
  69. fi
  70. if [ ".$SERIAL" = . ] ; then
  71. GETSERIAL="\$t = time ;\$d = \$t . substr(\$t+$$ ,-4,4)-1;print \$d"
  72. SERIAL=`/usr/bin/env perl -e "$GETSERIAL"`
  73. fi
  74. echo SERIAL=$SERIAL PREFIX=$PREFIX CAPREFIX=$CAPREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE
  75. if [ "$DHP." = YES. ] ; then
  76. echo "openssl dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE"
  77. $OPENSSL dhparam -2 -out $PREFIX-sv.dhp $KEYSIZE
  78. fi
  79. if [ "$REQ." = YES. ] ; then
  80. echo "openssl req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout XXX"
  81. $OPENSSL req -config $PREFIX-sv.prm -newkey rsa:$KEYSIZE -keyout $PREFIX-sv.key -out $PREFIX-sv.csr -passout pass:secret
  82. fi
  83. echo "openssl rsa -in $PREFIX-sv.key -out $PREFIX-sv.key"
  84. $OPENSSL rsa -in $PREFIX-sv.key -out $PREFIX-sv.key -passin pass:secret
  85. echo pseudo secrets generated
  86. echo "openssl rsa -in $PREFIX-sv.key -pubout -outform DER -out $PREFIX-sv.pub.der"
  87. $OPENSSL rsa -in $PREFIX-sv.key -pubout -outform DER -out $PREFIX-sv.pub.der
  88. echo "openssl rsa -in $PREFIX-sv.key -pubout -outform PEM -out $PREFIX-sv.pub.pem"
  89. $OPENSSL rsa -in $PREFIX-sv.key -pubout -outform PEM -out $PREFIX-sv.pub.pem
  90. echo "openssl x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -text -nameopt multiline $DIGESTALGO > $PREFIX-sv.crt "
  91. $OPENSSL x509 -set_serial $SERIAL -extfile $PREFIX-sv.prm -days $DURATION -CA $CAPREFIX-ca.cacert -CAkey $CAPREFIX-ca.key -in $PREFIX-sv.csr -req -text -nameopt multiline $DIGESTALGO > $PREFIX-sv.crt
  92. if [ "$P12." = YES. ] ; then
  93. echo "$OPENSSL pkcs12 -export -des3 -out $PREFIX-sv.p12 -caname $CAPREFIX -name $PREFIX -inkey $PREFIX-sv.key -in $PREFIX-sv.crt -certfile $CAPREFIX-ca.crt "
  94. $OPENSSL pkcs12 -export -des3 -out $PREFIX-sv.p12 -caname $CAPREFIX -name $PREFIX -inkey $PREFIX-sv.key -in $PREFIX-sv.crt -certfile $CAPREFIX-ca.crt
  95. fi
  96. echo "openssl x509 -noout -text -hash -in $PREFIX-sv.selfcert -nameopt multiline"
  97. $OPENSSL x509 -noout -text -hash -in $PREFIX-sv.crt -nameopt multiline
  98. # revoke server cert
  99. touch $CAPREFIX-ca.db
  100. echo 01 > $CAPREFIX-ca.cnt
  101. echo "openssl ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt"
  102. $OPENSSL ca -config $CAPREFIX-ca.cnf -revoke $PREFIX-sv.crt
  103. # issue CRL
  104. echo "openssl ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl"
  105. $OPENSSL ca -config $CAPREFIX-ca.cnf -gencrl -out $PREFIX-sv.crl
  106. echo "openssl x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der "
  107. $OPENSSL x509 -in $PREFIX-sv.crt -outform der -out $PREFIX-sv.der
  108. # all together now
  109. touch $PREFIX-sv.dhp
  110. cat $PREFIX-sv.prm $PREFIX-sv.key $PREFIX-sv.crt $PREFIX-sv.dhp >$PREFIX-sv.pem
  111. chmod o-r $PREFIX-sv.prm
  112. $OPENSSL x509 -in $PREFIX-sv.pem -pubkey -noout | \
  113. $OPENSSL pkey -pubin -outform der | $OPENSSL dgst -sha256 -binary | \
  114. $OPENSSL enc -base64 >$PREFIX-sv.pubkey-pinned
  115. echo "$PREFIX-sv.pem done"