|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Author: lework |
| 4 | +# Desc: Use cfssl tool to conveniently generate self-signed certificates. |
| 5 | +# Date: 2020/07/01 |
| 6 | + |
| 7 | +set -o errexit # Exit on most errors (see the manual) |
| 8 | +set -o errtrace # Make sure any error trap is inherited |
| 9 | +set -o nounset # Disallow expansion of unset variables |
| 10 | +set -o pipefail # Use last non-zero exit code in a pipeline |
| 11 | + |
| 12 | + |
| 13 | +###################################################################################################### |
| 14 | +# environment configuration |
| 15 | +###################################################################################################### |
| 16 | + |
| 17 | +# Colors |
| 18 | +RED='\033[0;31m' |
| 19 | +GREEN='\033[0;32m' |
| 20 | +YELLOW='\033[0;33m' |
| 21 | +BLUE='\033[0;36m' |
| 22 | +PLAIN='\033[0m' |
| 23 | + |
| 24 | + |
| 25 | +CFSSL_VERSION="1.4.1" |
| 26 | + |
| 27 | + |
| 28 | +###################################################################################################### |
| 29 | +# function |
| 30 | +###################################################################################################### |
| 31 | + |
| 32 | +echo_title() { |
| 33 | + echo -e "${GREEN}$1${PLAIN}" |
| 34 | +} |
| 35 | + |
| 36 | +function check() { |
| 37 | + for bin in cfssl cfssl-certinfo cfssljson |
| 38 | + do |
| 39 | + if ! $(command -v ${bin} > /dev/null 2>&1);then |
| 40 | + echo_title "[Installing] $bin..." |
| 41 | + curl -sSL https://github.com/cloudflare/cfssl/releases/download/v${CFSSL_VERSION}/{$bin}_${CFSSL_VERSION}_linux_amd64 > /tmp/${bin} |
| 42 | + sudo install /tmp/${bin} /usr/local/bin/${bin} |
| 43 | + fi |
| 44 | + done |
| 45 | + |
| 46 | + if ! $(command -v openssl > /dev/null 2>&1);then |
| 47 | + echo_title "[Installing] openssl..." |
| 48 | + command -v yum > /dev/null 2>&1 && yum -y install openssl |
| 49 | + command -v apt-get > /dev/null 2>&1 && apt-get install openssl -y |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +function ca() { |
| 55 | + project=${1:-demo} |
| 56 | + server_hostname="${2:-server.${project}.com}" |
| 57 | + client_hostname="${3:-client.${project}.com}" |
| 58 | + |
| 59 | + [ ! -d "${project}_ca" ] && mkdir "${project}_ca" |
| 60 | + cd "${project}_ca" |
| 61 | + |
| 62 | + echo_title "\n[Generating] cfssl config..." |
| 63 | + cat << EOF > cfssl-config.json |
| 64 | +{ |
| 65 | + "signing": { |
| 66 | + "default": { |
| 67 | + "expiry": "87600h", |
| 68 | + "usages": [ |
| 69 | + "signing", |
| 70 | + "digital signature", |
| 71 | + "key encipherment", |
| 72 | + "server auth", |
| 73 | + "client auth" |
| 74 | + ] |
| 75 | + }, |
| 76 | + "profiles": { |
| 77 | + "peer": { |
| 78 | + "expiry": "87600h", |
| 79 | + "usages": [ |
| 80 | + "signing", |
| 81 | + "digital signature", |
| 82 | + "key encipherment", |
| 83 | + "server auth", |
| 84 | + "client auth" |
| 85 | + ] |
| 86 | + }, |
| 87 | + "server": { |
| 88 | + "expiry": "87600h", |
| 89 | + "usages": [ |
| 90 | + "signing", |
| 91 | + "digital signature", |
| 92 | + "key encipherment", |
| 93 | + "server auth" |
| 94 | + ] |
| 95 | + }, |
| 96 | + "client": { |
| 97 | + "expiry": "87600h", |
| 98 | + "usages": [ |
| 99 | + "signing", |
| 100 | + "digital signature", |
| 101 | + "key encipherment", |
| 102 | + "client auth" |
| 103 | + ] |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +EOF |
| 109 | + |
| 110 | + echo_title "\n[Generating] ca csr..." |
| 111 | + cat << EOF > ca-csr.json |
| 112 | +{ |
| 113 | + "CN": "${project^^} CA", |
| 114 | + "key": { |
| 115 | + "algo": "ecdsa", |
| 116 | + "size": 256 |
| 117 | + }, |
| 118 | + "names": [ |
| 119 | + { |
| 120 | + "C": "CN", |
| 121 | + "ST": "Shanghai", |
| 122 | + "L": "Shanghai", |
| 123 | + "O": "${project}", |
| 124 | + "OU": "${project^^} Service" |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +EOF |
| 129 | + |
| 130 | + echo_title "\n[Generating] csr..." |
| 131 | + cat << EOF > csr.json |
| 132 | +{ |
| 133 | + "key": { |
| 134 | + "algo": "ecdsa", |
| 135 | + "size": 256 |
| 136 | + }, |
| 137 | + "names": [ |
| 138 | + { |
| 139 | + "C": "CN", |
| 140 | + "ST": "Shanghai", |
| 141 | + "L": "Shanghai", |
| 142 | + "O": "${project}", |
| 143 | + "OU": "${project^^} Service" |
| 144 | + } |
| 145 | + ] |
| 146 | +} |
| 147 | +EOF |
| 148 | + |
| 149 | + echo_title "\n[Generating] certificate authority..." |
| 150 | + cfssl gencert -initca ca-csr.json | cfssljson -bare ca |
| 151 | + |
| 152 | + echo_title "\n[Generating] server certificate..." |
| 153 | + cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=cfssl-config.json \ |
| 154 | + -hostname="${server_hostname},localhost,127.0.0.1" csr.json \ |
| 155 | + | cfssljson -bare server |
| 156 | + |
| 157 | + echo_title "\n[Generating] client certificate..." |
| 158 | + cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=cfssl-config.json \ |
| 159 | + -hostname="${client_hostname},localhost,127.0.0.1" csr.json \ |
| 160 | + | cfssljson -bare client |
| 161 | + |
| 162 | + echo_title "\n[Generating] server and client node certificate..." |
| 163 | + cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=cfssl-config.json \ |
| 164 | + -hostname="${server_hostname},${client_hostname},localhost,127.0.0.1" csr.json \ |
| 165 | + | cfssljson -bare dev |
| 166 | + |
| 167 | + echo_title "\n[Generating] user certificates..." |
| 168 | + cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=cfssl-config.json \ |
| 169 | + -profile=client csr.json | cfssljson -bare user |
| 170 | + openssl pkcs12 -export -inkey user-key.pem -in user.pem -out user.pfx -password pass: |
| 171 | + |
| 172 | + echo_title "\n[Generating] The $(pwd) directory file list..." |
| 173 | + ls -al . |
| 174 | +} |
| 175 | + |
| 176 | + |
| 177 | +usage_help() { |
| 178 | + cat <<EOM |
| 179 | +
|
| 180 | +Use cfssl tool to conveniently generate self-signed certificates. |
| 181 | + |
| 182 | + Usage: |
| 183 | + $(basename $0) [ -h | --help ] [project_name server_hostname client_hostname] |
| 184 | +
|
| 185 | + Example: |
| 186 | + $(basename $0) # Generate demo self-signed certificate |
| 187 | + $(basename $0) -h # View help. |
| 188 | + $(basename $0) project web-server.project.com,api-server.project.com rpc-client.project.com,api-client.project.com |
| 189 | +EOM |
| 190 | + exit 1 |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +###################################################################################################### |
| 196 | +# main |
| 197 | +###################################################################################################### |
| 198 | + |
| 199 | + |
| 200 | +case ${1-} in |
| 201 | + -h | --help ) usage_help |
| 202 | + ;; |
| 203 | + * ) check |
| 204 | + ca $@ |
| 205 | +esac |
0 commit comments