genroot.sh 2.4 KB

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