2
0

genserv.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. command -v "$OPENSSL"
  32. "$OPENSSL" version
  33. USAGE='echo Usage is genserv.sh <prefix> <caprefix>'
  34. HOME=$(pwd)
  35. cd "$HOME"
  36. KEYSIZE=2048
  37. DURATION=300
  38. # The -sha256 option was introduced in OpenSSL 1.0.1
  39. DIGESTALGO=-sha256
  40. REQ=YES
  41. P12=NO
  42. DHP=NO
  43. NOTOK=
  44. PREFIX="${1:-}"
  45. if [ -z "$PREFIX" ]; then
  46. echo 'No configuration prefix'
  47. NOTOK=1
  48. else
  49. if [ ! -f "$PREFIX-sv.prm" ]; then
  50. echo "No configuration file $PREFIX-sv.prm"
  51. NOTOK=1
  52. fi
  53. fi
  54. CAPREFIX="${2:-}"
  55. if [ -z "$CAPREFIX" ]; then
  56. echo 'No CA prefix'
  57. NOTOK=1
  58. else
  59. if [ ! -f "$CAPREFIX-ca.cacert" ]; then
  60. echo "No CA certificate file $CAPREFIX-ca.caert"
  61. NOTOK=1
  62. fi
  63. if [ ! -f "$CAPREFIX-ca.key" ]; then
  64. echo "No $CAPREFIX key"
  65. NOTOK=1
  66. fi
  67. fi
  68. if [ -n "$NOTOK" ]; then
  69. echo 'Sorry, I cannot do that for you.'
  70. $USAGE
  71. exit
  72. fi
  73. echo "PREFIX=$PREFIX CAPREFIX=$CAPREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE"
  74. set -x
  75. if [ "$DHP" = YES ]; then
  76. "$OPENSSL" dhparam -2 -out "$PREFIX-sv.dhp" "$KEYSIZE"
  77. fi
  78. if [ "$REQ" = YES ]; then
  79. "$OPENSSL" req -config "$PREFIX-sv.prm" -newkey "rsa:$KEYSIZE" -keyout "$PREFIX-sv.key" -out "$PREFIX-sv.csr" -passout fd:0 <<EOF
  80. pass:secret
  81. EOF
  82. fi
  83. "$OPENSSL" rsa -in "$PREFIX-sv.key" -out "$PREFIX-sv.key" -passin fd:0 <<EOF
  84. pass:secret
  85. EOF
  86. echo 'pseudo secrets generated'
  87. "$OPENSSL" rsa -in "$PREFIX-sv.key" -pubout -outform DER -out "$PREFIX-sv.pub.der"
  88. "$OPENSSL" rsa -in "$PREFIX-sv.key" -pubout -outform PEM -out "$PREFIX-sv.pub.pem"
  89. "$OPENSSL" x509 -extfile "$PREFIX-sv.prm" -days "$DURATION" -CA "$CAPREFIX-ca.cacert" -CAkey "$CAPREFIX-ca.key" -CAcreateserial -in "$PREFIX-sv.csr" -req -text -nameopt multiline "$DIGESTALGO" > "$PREFIX-sv.crt"
  90. if [ "$P12" = YES ]; then
  91. "$OPENSSL" pkcs12 -export -des3 -out "$PREFIX-sv.p12" -caname "$CAPREFIX" -name "$PREFIX" -inkey "$PREFIX-sv.key" -in "$PREFIX-sv.crt" -certfile "$CAPREFIX-ca.crt"
  92. fi
  93. "$OPENSSL" x509 -noout -text -hash -in "$PREFIX-sv.crt" -nameopt multiline
  94. # revoke server cert
  95. touch "$CAPREFIX-ca.db"
  96. echo 01 > "$CAPREFIX-ca.cnt"
  97. "$OPENSSL" ca -config "$CAPREFIX-ca.cnf" -revoke "$PREFIX-sv.crt"
  98. # issue CRL
  99. "$OPENSSL" ca -config "$CAPREFIX-ca.cnf" -gencrl -out "$PREFIX-sv.crl"
  100. "$OPENSSL" x509 -in "$PREFIX-sv.crt" -outform der -out "$PREFIX-sv.der"
  101. # all together now
  102. touch "$PREFIX-sv.dhp"
  103. cat "$PREFIX-sv.prm" "$PREFIX-sv.key" "$PREFIX-sv.crt" "$PREFIX-sv.dhp" > "$PREFIX-sv.pem"
  104. chmod o-r "$PREFIX-sv.prm"
  105. "$OPENSSL" x509 -in "$PREFIX-sv.pem" -pubkey -noout | \
  106. "$OPENSSL" pkey -pubin -outform der | "$OPENSSL" dgst -sha256 -binary | \
  107. "$OPENSSL" enc -base64 > "$PREFIX-sv.pubkey-pinned"
  108. echo "$PREFIX-sv.pem done"