| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | set -eo pipefail |
| 4 | |
| 5 | |
| 6 | generate_keyscip-shell shell asymptote 3.11+ds-1 generate_key().() { |
| 7 | local path=${1} |
| 8 | local base=${2} |
| 9 | local type=${3:-'rsa'} # "ec" or "rsa" |
| 10 | |
| 11 | mkdirscip-shell system . . mkdir. -p ${path} |
| 12 | if [[ "${type}" == "rsa" ]]; then |
| 13 | opensslscip-shell system . . openssl. genrsa -out "${path}/${base}-key.pem" |
| 14 | elif [[ "${type}" == "ec" ]]; then |
| 15 | opensslscip-shell system . . openssl. ecparam -genkey -param_enc named_curve -name prime256v1 -out "${path}/${base}-key.pem" |
| 16 | else |
| 17 | echoscip-shell system . . echo. "Error: usage: type (param \$2) should be 'rsa' or 'ec'" >&2 && exit 1 |
| 18 | fi |
| 19 | echoscip-shell system . . echo. "generated ${path}/${base}-key.pem" |
| 20 | } |
| 21 | |
| 22 | generate_cascip-shell shell asymptote 3.11+ds-1 generate_ca().() { |
| 23 | local path="${1}" |
| 24 | local base="${2:-'root-ca'}" |
| 25 | local type="${3:-'rsa'}" # "ec" or "rsa" |
| 26 | local org="${4:-'/O=machinezone/O=IXWebSocket'}" |
| 27 | |
| 28 | mkdirscip-shell system . . mkdir. -p ${path} |
| 29 | |
| 30 | generate_keyscip-shell shell asymptote 3.11+ds-1 generate_key(). "${path}" "${base}" "${type}" |
| 31 | |
| 32 | opensslscip-shell system . . openssl. req -new -x509 -sha256 -days 3650 \ |
| 33 | -reqexts v3_req -extensions v3_ca \ |
| 34 | -subj "${org}/CN=${base}" \ |
| 35 | -key "${path}/${base}-key.pem" \ |
| 36 | -out "${path}/${base}-crt.pem" |
| 37 | |
| 38 | echoscip-shell system . . echo. "generated ${path}/${base}-crt.pem" |
| 39 | } |
| 40 | |
| 41 | generate_certscip-shell shell asymptote 3.11+ds-1 generate_cert().() { |
| 42 | local path="$1" |
| 43 | local base="$2" |
| 44 | local cabase="$3" |
| 45 | local type="${4:-'rsa'}" # "ec" or "rsa" |
| 46 | local org="${5:-'/O=machinezone/O=IXWebSocket'}" |
| 47 | local san="${6:-'DNS:localhost,DNS:127.0.0.1'}" |
| 48 | |
| 49 | mkdirscip-shell system . . mkdir. -p ${path} |
| 50 | |
| 51 | generate_keyscip-shell shell asymptote 3.11+ds-1 generate_key(). "${path}" "${base}" "${type}" |
| 52 | |
| 53 | opensslscip-shell system . . openssl. req -new -sha256 \ |
| 54 | -key "${path}/${base}-key.pem" \ |
| 55 | -subj "${org}/CN=${base}" \ |
| 56 | -out "${path}/${base}.csr" |
| 57 | |
| 58 | |
| 59 | if [scip-shell system . . `[`. "${base}" == "${cabase}" ]; then |
| 60 | # self-signed |
| 61 | opensslscip-shell system . . openssl. x509 -req -in "${path}/${base}.csr" \ |
| 62 | -signkey "${path}/${base}-key.pem" -days 365 -sha256 \ |
| 63 | -extfile <(printfscip-shell system . . printf. "subjectAltName=${san}") \ |
| 64 | -outform PEM -out "${path}/${base}-crt.pem" |
| 65 | else |
| 66 | opensslscip-shell system . . openssl. x509 -req -in ${path}/${base}.csr \ |
| 67 | -CA "${path}/${cabase}-crt.pem" \ |
| 68 | -CAkey "${path}/${cabase}-key.pem" \ |
| 69 | -CAcreateserial -days 365 -sha256 \ |
| 70 | -extfile <(printfscip-shell system . . printf. "subjectAltName=${san}") \ |
| 71 | -outform PEM -out "${path}/${base}-crt.pem" |
| 72 | fi |
| 73 | |
| 74 | rmscip-shell system . . rm. -f ${path}/${base}.csr |
| 75 | echoscip-shell system . . echo. "generated ${path}/${base}-crt.pem" |
| 76 | } |
| 77 | |
| 78 | # main |
| 79 | |
| 80 | outdir=${1:-'./.certs'} |
| 81 | type=${2:-'rsa'} |
| 82 | org=${3:-'/O=machinezone/O=IXWebSocket'} |
| 83 | |
| 84 | if ! whichscip-shell system . . which. openssl &>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 85 | |
| 86 | if ! grepscip-shell system . . grep. -qa -E 'docker|lxc' /proc/1/cgroupscip-shell filesystem . . `/proc/1/cgroup`.; then |
| 87 | # launch a container with openssl and run this script there |
| 88 | docker run --rm -i -v $(pwdscip-shell system . . pwd.):/work alpine sh -c "apk add bash openssl && /work/generate_certs.sh /work/${outdir} && chown -R $(idscip-shell system . . id. -u):$(idscip-shell system . . id. -u) /work/${outdir}" |
| 89 | else |
| 90 | echoscip-shell system . . echo. "Please install openssl in this container to generate test certs, or launch outside of docker" >&2 && exit 1 |
| 91 | fi |
| 92 | else |
| 93 | |
| 94 | generate_cascip-shell shell asymptote 3.11+ds-1 generate_ca(). "${outdir}" "trusted-ca" "${type}" "${org}" |
| 95 | |
| 96 | generate_certscip-shell shell asymptote 3.11+ds-1 generate_cert(). "${outdir}" "trusted-server" "trusted-ca" "${type}" "${org}" |
| 97 | generate_certscip-shell shell asymptote 3.11+ds-1 generate_cert(). "${outdir}" "trusted-client" "trusted-ca" "${type}" "${org}" |
| 98 | |
| 99 | generate_cascip-shell shell asymptote 3.11+ds-1 generate_ca(). "${outdir}" "untrusted-ca" "${type}" "${org}" |
| 100 | |
| 101 | generate_certscip-shell shell asymptote 3.11+ds-1 generate_cert(). "${outdir}" "untrusted-client" "untrusted-ca" "${type}" "${org}" |
| 102 | generate_certscip-shell shell asymptote 3.11+ds-1 generate_cert(). "${outdir}" "selfsigned-client" "selfsigned-client" "${type}" "${org}" |
| 103 | |
| 104 | fi |