xref: /curl/tests/certs/scripts/genroot.sh (revision 9d32724c)
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
26# exit on first fail
27set -eu
28
29OPENSSL=openssl
30if [ -f /usr/local/ssl/bin/openssl ]; then
31  OPENSSL=/usr/local/ssl/bin/openssl
32fi
33
34command -v "$OPENSSL"
35"$OPENSSL" version
36
37USAGE='echo Usage is genroot.sh <name>'
38
39HOME=$(pwd)
40cd "$HOME"
41
42KEYSIZE=2048
43DURATION=6000
44# The -sha256 option was introduced in OpenSSL 1.0.1
45DIGESTALGO=-sha256
46
47NOTOK=
48
49PREFIX="${1:-}"
50if [ -z "$PREFIX" ]; then
51  echo 'No configuration prefix'
52  NOTOK=1
53else
54  if [ ! -f "$PREFIX-ca.prm" ]; then
55    echo "No configuration file $PREFIX-ca.prm"
56    NOTOK=1
57  fi
58fi
59
60if [ -n "$NOTOK" ]; then
61  echo 'Sorry, I cannot do that for you.'
62  $USAGE
63  exit
64fi
65
66echo "PREFIX=$PREFIX DURATION=$DURATION KEYSIZE=$KEYSIZE"
67
68set -x
69
70"$OPENSSL" genrsa -out "$PREFIX-ca.key" -passout fd:0 "$KEYSIZE" <<EOF
71pass:secret
72EOF
73"$OPENSSL" req -config "$PREFIX-ca.prm" -new -key "$PREFIX-ca.key" -out "$PREFIX-ca.csr" -passin fd:0 <<EOF
74pass:secret
75EOF
76"$OPENSSL" x509 -extfile "$PREFIX-ca.prm" -days "$DURATION" -req -signkey "$PREFIX-ca.key" -in "$PREFIX-ca.csr" -out "$PREFIX-raw-ca.cacert" "$DIGESTALGO"
77"$OPENSSL" x509 -text -in "$PREFIX-raw-ca.cacert" -nameopt multiline > "$PREFIX-ca.cacert"
78"$OPENSSL" x509 -in "$PREFIX-ca.cacert" -outform der -out "$PREFIX-ca.der"
79"$OPENSSL" x509 -in "$PREFIX-ca.cacert" -text -nameopt multiline > "$PREFIX-ca.crt"
80"$OPENSSL" x509 -noout -text -in "$PREFIX-ca.cacert" -nameopt multiline
81# "$OPENSSL" rsa -in "../keys/$PREFIX-ca.key" -text -noout -pubout
82