genroot.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 genroot.sh <name>'
  32. HOME=$(pwd)
  33. cd "$HOME"
  34. KEYSIZE=2048
  35. DURATION=6000
  36. # The -sha256 option was introduced in OpenSSL 1.0.1
  37. DIGESTALGO=-sha256
  38. NOTOK=
  39. PREFIX="${1:-}"
  40. if [ -z "$PREFIX" ]; then
  41. echo 'No configuration prefix'
  42. NOTOK=1
  43. else
  44. if [ ! -f "$PREFIX-ca.prm" ]; then
  45. echo "No configuration file $PREFIX-ca.prm"
  46. NOTOK=1
  47. fi
  48. fi
  49. if [ -n "$NOTOK" ]; then
  50. echo 'Sorry, I cannot do that for you.'
  51. $USAGE
  52. exit
  53. fi
  54. SERIAL="$(date +'%s')${RANDOM:(-4)}"
  55. echo "SERIAL=$SERIAL PREFIX=$PREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE"
  56. set -x
  57. "$OPENSSL" genrsa -out "$PREFIX-ca.key" -passout fd:0 "$KEYSIZE" <<EOF
  58. pass:secret
  59. EOF
  60. "$OPENSSL" req -config "$PREFIX-ca.prm" -new -key "$PREFIX-ca.key" -out "$PREFIX-ca.csr" -passin fd:0 <<EOF
  61. pass:secret
  62. EOF
  63. "$OPENSSL" x509 -set_serial "$SERIAL" -extfile "$PREFIX-ca.prm" -days "$DURATION" -req -signkey "$PREFIX-ca.key" -in "$PREFIX-ca.csr" -out "$PREFIX-$SERIAL-ca.cacert" "$DIGESTALGO"
  64. "$OPENSSL" x509 -text -in "$PREFIX-$SERIAL-ca.cacert" -nameopt multiline > "$PREFIX-ca.cacert"
  65. "$OPENSSL" x509 -in "$PREFIX-ca.cacert" -outform der -out "$PREFIX-ca.der"
  66. "$OPENSSL" x509 -in "$PREFIX-ca.cacert" -text -nameopt multiline > "$PREFIX-ca.crt"
  67. "$OPENSSL" x509 -noout -text -in "$PREFIX-ca.cacert" -nameopt multiline
  68. # "$OPENSSL" rsa -in "../keys/$PREFIX-ca.key" -text -noout -pubout