When developing, we may need to use certificate. Instead of using Let’s Encrypt or even pay for one, we can use openssl & keytool command line to generate self-signed certificates.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env bash
#######################################################
# This scripts is used to generate the rootCA and the #
# user bi-keys                                        #
#######################################################

set -e

rootca_home="./root_ca"
validity=36500
out="$(pwd)"
name="localhost"
subject="/C=FR/ST=IDF/L=Paris/O=Foobar/OU=Engineering/CN="
password="super-secret-password"

show_help() {
  cat << EOF
Generate certificate

Usage: ${0##*/} <flags> <args>

Examples:
    # Generate RSA bi-keys
      ${0##*/} rsa
    # Create certificate with name "some.domain.name"
      ${0##*/} --name "some.domain.name" rsa
    # Using all flags
      ${0##*/} -n "some.domain.name" -s "/C=FR/ST=IDF/L=Paris/O=Foobar/OU=Engineering/CN=" -o "\$(pwd)/certificates" -p "S3cR3t" rsa

Available commands:
    rsa                   Generate CRT, KEY, P12 and JKS files with RSA algorithm
    ecdsa                 Generate CRT and KEY files with ECDSA algorithm

Flags:
    -h, --help            Display help
    -n, --name            Certificate name (default: ${name})
    -s, --subject         Certificate subject (default: ${subject})
    -o, --out             Output directory (default: ${out})

EOF
}

info() {
  echo -e "\033[0;36mINFO: ${1}\033[0m"
}

generate_root_ca() {
  local folder="${out}/${rootca_home}"
  if [ ! -d "${folder}" ]; then
    info "Generating the Certification Authority"
    mkdir -p "${folder}"

    # Since we do not have any certification authority, we will generate our own.
    openssl genrsa -out "${folder}/rootCA.key" 2048
    openssl req -new -nodes -x509 \
            -days ${validity} \
            -key "${folder}/rootCA.key" \
            -out "${folder}/rootCA.pem" \
            -subj "${subject}rootca"
  fi
}

generate_rsa() {
  local certificate_name=$1
  local folder="${out}/${certificate_name}"
  local rootca_folder="${out}/${rootca_home}"

  clean_folder "${certificate_name}" "rsa"

  info "${certificate_name} - Generate RSA private key"
  openssl genrsa -out "${folder}/${certificate_name}.rsa.key" 2048

  generate_self_signed_certificate "${certificate_name}" rsa

  info "${certificate_name} - Generate certificate signing request for our RSA certificate"
  openssl req -new \
          -key "${folder}/${certificate_name}.rsa.key" \
          -out "${folder}/${certificate_name}.rsa.csr" \
          -subj "${subject}${certificate_name}"

  info "${certificate_name} - Sign the RSA CSR using CA root key"
  openssl x509 -req \
          -in "${folder}/${certificate_name}.rsa.csr" \
          -CA "${rootca_folder}/rootCA.pem" \
          -CAkey "${rootca_folder}/rootCA.key" \
          -CAcreateserial \
          -out "${folder}/${certificate_name}.rsa.crt" \
          -days ${validity} \
          -sha256
}

generate_ecdsa() {
  local certificate_name=$1
  local folder="${out}/${certificate_name}"
  local rootca_folder="${out}/${rootca_home}"

  clean_folder "${certificate_name}" "ecdsa"

  info "${certificate_name} - Generate ECDSA private key"
  openssl ecparam -name prime256v1 -genkey -out "${folder}/${certificate_name}.ecdsa.key"

  generate_self_signed_certificate "${certificate_name}" ecdsa

  info "${certificate_name} - Generate certificate signing request for our ECDSA certificate"
  openssl req -new -nodes \
          -key "${folder}/${certificate_name}.ecdsa.key" \
          -out "${folder}/${certificate_name}.ecdsa.csr" \
          -subj "${subject}${certificate_name}"

  info "${certificate_name} - Sign the RSA CSR using CA root key"
  openssl x509 -req \
          -in "${folder}/${certificate_name}.ecdsa.csr" \
          -CA "${rootca_folder}/rootCA.pem" \
          -CAkey "${rootca_folder}/rootCA.key" \
          -CAcreateserial \
          -out "${folder}/${certificate_name}.ecdsa.crt" \
          -days ${validity} \
          -sha256
}

generate_p12() {
  local certificate_name=$1
  local folder="${out}/${certificate_name}"
  local rootca_folder="${out}/${rootca_home}"

  info "${certificate_name} - Generate the P12 with password '${password}'"
  openssl pkcs12 -export \
          -in "${folder}/${certificate_name}.rsa.crt" \
          -inkey "${folder}/${certificate_name}.rsa.key" \
          -out "${folder}/${certificate_name}.rsa.p12" \
          -password "pass:${password}"
}

generate_self_signed_certificate() {
  local certificate_name=$1
  local certificate_type=$2
  local folder="${out}/${certificate_name}"

  info "${certificate_name}.${certificate_type} - Generate self-signed certificate"
  openssl req -new -x509 \
          -days ${validity} \
          -key "${folder}/${certificate_name}.${certificate_type}.key" \
          -out "${folder}/${certificate_name}.self-signed.${certificate_type}.crt" \
          -subj "${subject}${certificate_name}"
}

clean_folder() {
  local certificate_name=$1
  local certificate_type=$2
  local folder="${out}/${certificate_name}"

  info "${certificate_name} - Delete existing ${certificate_type} files"
  mkdir -p "${folder}"
  if [ -f "${folder}/${certificate_name}.${certificate_type}.crt" ]; then
    for f in ${folder}/*.${certificate_type}.*; do
      rm "${f}"
    done
  fi
}

main() {
  # Flags in bash tutorial here: /usr/share/doc/util-linux/examples/getopt-parse.bash
  TEMP=$(getopt -o 'hn:s:p⭕' --long 'help,name:,subject:,password:,out:' -n "${0##*/}" -- "$@")
  eval set -- "$TEMP"
  unset TEMP
  while true; do
    case "${1}" in
      '-h'|'--help')
        show_help
        exit
        ;;
      '-n'|'--name')
        name="${2}"
        shift 2
        continue
        ;;
      '-s'|'--subject')
        subject="${2}"
        shift 2
        continue
        ;;
      '-p'|'--password')
        password="${2}"
        shift 2
        continue
        ;;
      '-o'|'--out')
        out="${2}"
        shift 2
        continue
        ;;
      '--')
        shift
        break
        ;;
      *)
        break
        ;;
    esac

    shift
  done

  case "${1}" in
    'rsa')
      generate_root_ca
      generate_rsa "${name}"
      generate_p12 "${name}"
      info "Certificate generation FINISHED!!!"
      ;;
    'ecdsa')
      generate_root_ca
      generate_ecdsa "${name}"
      info "Certificate generation FINISHED!!!"
      ;;
    'p12')
      generate_root_ca
      ;;
    *)
      show_help
      ;;
  esac
}

main "$@"