genserv.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env 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. # exit on first fail
  26. set -eu
  27. OPENSSL=openssl
  28. if [ -f /usr/local/ssl/bin/openssl ]; then
  29. OPENSSL=/usr/local/ssl/bin/openssl
  30. fi
  31. USAGE='echo Usage is genserv.sh <prefix> <caprefix>'
  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. NOTOK=
  42. PREFIX="${1:-}"
  43. if [ -z "$PREFIX" ]; then
  44. echo 'No configuration prefix'
  45. NOTOK=1
  46. else
  47. if [ ! -f "$PREFIX-sv.prm" ]; then
  48. echo "No configuration file $PREFIX-sv.prm"
  49. NOTOK=1
  50. fi
  51. fi
  52. CAPREFIX="${2:-}"
  53. if [ -z "$CAPREFIX" ]; then
  54. echo No CA prefix
  55. NOTOK=1
  56. else
  57. if [ ! -f "$CAPREFIX-ca.cacert" ]; then
  58. echo "No CA certificate file $CAPREFIX-ca.caert"
  59. NOTOK=1
  60. fi
  61. if [ ! -f "$CAPREFIX-ca.key" ]; then
  62. echo "No $CAPREFIX key"
  63. NOTOK=1
  64. fi
  65. fi
  66. if [ -n "$NOTOK" ]; then
  67. echo 'Sorry, I cannot do that for you.'
  68. $USAGE
  69. exit
  70. fi
  71. if [ -z "${SERIAL:-}" ]; then
  72. SERIAL="$(date +'%s')${RANDOM:(-4)}"
  73. fi
  74. echo "SERIAL=$SERIAL PREFIX=$PREFIX CAPREFIX=$CAPREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE"
  75. set -x
  76. if [ "$DHP" = YES ]; then
  77. "$OPENSSL" dhparam -2 -out "$PREFIX-sv.dhp" "$KEYSIZE"
  78. fi
  79. if [ "$REQ" = YES ]; then
  80. "$OPENSSL" req -config "$PREFIX-sv.prm" -newkey "rsa:$KEYSIZE" -keyout "$PREFIX-sv.key" -out "$PREFIX-sv.csr" -passout fd:0 <<EOF
  81. pass:secret
  82. EOF
  83. fi
  84. "$OPENSSL" rsa -in "$PREFIX-sv.key" -out "$PREFIX-sv.key" -passin fd:0 <<EOF
  85. pass:secret
  86. EOF
  87. echo 'pseudo secrets generated'
  88. "$OPENSSL" rsa -in "$PREFIX-sv.key" -pubout -outform DER -out "$PREFIX-sv.pub.der"
  89. "$OPENSSL" rsa -in "$PREFIX-sv.key" -pubout -outform PEM -out "$PREFIX-sv.pub.pem"
  90. "$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. if [ "$P12" = YES ]; then
  92. "$OPENSSL" pkcs12 -export -des3 -out "$PREFIX-sv.p12" -caname "$CAPREFIX" -name "$PREFIX" -inkey "$PREFIX-sv.key" -in "$PREFIX-sv.crt" -certfile "$CAPREFIX-ca.crt"
  93. fi
  94. "$OPENSSL" x509 -noout -text -hash -in "$PREFIX-sv.crt" -nameopt multiline
  95. # revoke server cert
  96. touch "$CAPREFIX-ca.db"
  97. echo 01 > "$CAPREFIX-ca.cnt"
  98. "$OPENSSL" ca -config "$CAPREFIX-ca.cnf" -revoke "$PREFIX-sv.crt"
  99. # issue CRL
  100. "$OPENSSL" ca -config "$CAPREFIX-ca.cnf" -gencrl -out "$PREFIX-sv.crl"
  101. "$OPENSSL" x509 -in "$PREFIX-sv.crt" -outform der -out "$PREFIX-sv.der"
  102. # all together now
  103. touch "$PREFIX-sv.dhp"
  104. cat "$PREFIX-sv.prm" "$PREFIX-sv.key" "$PREFIX-sv.crt" "$PREFIX-sv.dhp" > "$PREFIX-sv.pem"
  105. chmod o-r "$PREFIX-sv.prm"
  106. "$OPENSSL" x509 -in "$PREFIX-sv.pem" -pubkey -noout | \
  107. "$OPENSSL" pkey -pubin -outform der | "$OPENSSL" dgst -sha256 -binary | \
  108. "$OPENSSL" enc -base64 > "$PREFIX-sv.pubkey-pinned"
  109. echo "$PREFIX-sv.pem done"