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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
#!/bin/bash
#-----------------------------------------------------------------------------
#
# File: hsm_add_key.sh
#
# Description: This script adds a key to an existing HAB/AHAB PKI tree to be
# used with the HAB/AHAB code signing feature. Both the private
# key and corresponding certificate are generated. This script
# can generate new SRKs, CSF keys and Images keys for HAB4 and
# SRKs and SGKs for AHAB.
# This script is not intended for generating CA root keys.
#
# Copyright 2021-2023 NXP
#
#
#-----------------------------------------------------------------------------
stty erase
if [ $# -gt 0 ]; then
interactive="n"
else
interactive="y"
fi
usage()
{
echo
echo "Usage:"
echo "Interactive Mode:"
echo "$0"
echo
echo "Command Line Mode:"
echo "$0 -ver <4/a> -key-name <new key name> -id <new key object id> -kt <ecc/rsa> -kl <key length> [-md <message digest>] -duration <years> -srk <y/n> [-srk-ca <y/n>] -signing-key-label <CA/SRK signing key label> -signing-crt <CA/SRK signing cert>"
echo "Options:"
echo " -kl: -kt = ecc then Supported key lengths: p256, p384, p521"
echo " : -kt = rsa then Supported key lengths: 2048, 3072, 4096"
echo " -md: -ver = a then Supported message digests: sha256, sha384, sha512"
echo " : -ver = 4 then md = sha256 is fixed"
echo
echo "$0 -h | --help : This text"
echo
}
max_param=22
min_param=18
num_param=1
# Check USR_PIN is defined, if not then set default
if [[ -v "${USR_PIN}" || -z "${USR_PIN}" ]]
then
USR_PIN=123456
fi
# Throw error if PKCS11_MODULE_PATH undefined
if [[ -v "${PKCS11_MODULE_PATH}" || -z "${PKCS11_MODULE_PATH}" ]]
then
echo "ERROR: PKCS11_MODULE_PATH must be set"
echo
exit 1
fi
if [ "$interactive" = "n" ]
then
# Validate command line parameters
if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
usage
exit 0
elif [ $# -lt $min_param ] || [ $# -gt $max_param ]
then
echo "ERROR: Correct parameters required in non-interactive mode"
usage
exit 1
fi
# Process command line inputs
while [ $num_param -le $max_param ] && [ "$1" != "" ]
do
case $1 in
-ver)
shift
ver=$1
shift
;;
-key-name)
shift
key_name=$1
shift
;;
-id)
shift
id=$1
shift
;;
-kt)
shift
kt=$1
shift
;;
-kl)
shift
kl=$1
shift
;;
-md)
if [ "$ver" = "4" ]
then
echo "ERROR: Message digest is fixed for HAB4"
usage
exit 1
fi
shift
md=$1
shift
;;
-duration)
shift
duration=$1
shift
;;
-srk)
shift
srk=$1
shift
if [ "$srk" = "y" ] && [ "$1" != "-srk-ca" ]
then
echo "ERROR: SRK CA option required when SRK is selected"
usage
exit 1
fi
;;
-srk-ca)
shift
srk_ca=$1
shift
;;
-signing-key-label)
shift
signing_key_label=$1
shift
;;
-signing-crt)
shift
signing_crt=$1
shift
;;
*)
echo "ERROR: Invalid parameter: $1"
usage
exit 1
;;
esac
num_param=$(( num_param + 2 ))
done
fi # interactive=n
if [ $(uname -m) = "x86_64" ]
then
bin_path="linux64/bin"
else
bin_path="linux32/bin"
fi
if [ "$interactive" = "y" ]
then
printf "Which version of HAB/AHAB do you want to generate the key for (4 = HAB4 / a = AHAB)?: \b"
read ver
fi
case $ver in
4) ;;
a) ;;
*)
echo Error - HAB/AHAB version selected must be either 4 or a
usage
exit 1
esac
if [ "$interactive" = "y" ]
then
printf "Enter new key name (e.g. SRK5): \b"
read key_name
fi
if [ "$interactive" = "y" ]
then
printf "Enter new key object id (e.g. 1001): \b"
read id
fi
if [ $ver = "4" ] || [ $ver = "a" ]
then
if [ "$interactive" = "y" ]
then
printf "Enter new key type (ecc / rsa): \b"
read kt
fi
# Confirm that a valid key type has been entered
case $kt in
rsa) ;;
ecc) ;;
*)
echo Invalid key type. Supported key types: rsa, ecc
usage
exit 1 ;;
esac
fi
if [ $kt = "rsa" ]
then
if [ "$interactive" = "y" ]
then
printf "Enter new key length in bits: \b"
read kl
fi
# Confirm that a valid key length has been entered
case $kl in
2048) ;;
3072) ;;
4096) ;;
*)
echo Invalid key length. Supported key lengths: 2048, 3072, 4096
usage
exit 1 ;;
esac
else
if [ "$interactive" = "y" ]
then
printf "Enter new key length (p256 / p384 / p521): \b"
read kl
fi
# Confirm that a valid key length has been entered
case $kl in
p256)
cn="prime256v1" ;;
p384)
cn="secp384r1" ;;
p521)
cn="secp521r1" ;;
*)
echo Invalid key length. Supported key lengths: 256, 384, 521
usage
exit 1 ;;
esac
fi
if [ $ver = "a" ]
then
if [ "$interactive" = "y" ]
then
printf "Enter new message digest (sha256, sha384, sha512): \b"
read md
fi
# Confirm that a valid message digest has been entered
case $md in
sha256) ;;
sha384) ;;
sha512) ;;
*)
echo Invalid message digest. Supported message digests: sha256, sha384, sha512
usage
exit 1 ;;
esac
# HAB4
else
md="sha256"
fi
if [ "$interactive" = "y" ]
then
printf "Enter certificate duration (years): \b"
read duration
fi
# Compute validity period
val_period=$((duration*365))
# ---------------- Add SRK Key and Certificate -------------------
if [ "$interactive" = "y" ]
then
printf "Is this an SRK key?: \b"
read srk
fi
if [ $srk = "y" ]
then
# Check if SRKs should be generated as CA certs or user certs
if [ $ver = "4" ] || [ $ver = "a" ]
then
if [ "$interactive" = "y" ]
then
printf "Do you want the SRK to have the CA flag set (y/n)?: \b"
read srk_ca
fi
if [ $srk_ca = "y" ]
then
ca="ca"
else
ca="usr"
fi
fi
if [ "$interactive" = "y" ]
then
printf "Enter CA signing key label: \b"
read signing_key_label
printf "Enter CA signing certificate name: \b"
read signing_crt
fi
# Not SRK
else
ca="usr"
if [ $ver = "4" ] || [ $ver = "a" ]
then
if [ "$interactive" = "y" ]
then
# ------------ Add CSF/IMG/SGK Key and Certificate ---------------
printf "Enter SRK signing key label: \b"
read signing_key_label
printf "Enter SRK signing certificate name: \b"
read signing_crt
fi
fi
fi
keys_dir=../keys
crts_dir=../crts
ca_dir=../ca
if [ ! -d "${ca_dir}" ]
then
ca_dir=/usr/share/doc/imx-code-signing-tool/pki_scripts/ca
fi
mkdir -p "$crts_dir"
# Generate outputs
if [ $kt = "ecc" ]
then
key_label=${key_name}_${md}_${cn}_v3_${ca}
key_type=EC:${cn}
else
key_label=${key_name}_${md}_${kl}_65537_v3_${ca}
key_type=rsa:${kl}
fi
# # Generate Key
pkcs11-tool --module $PKCS11_MODULE_PATH -l \
--pin $USR_PIN \
--keypairgen \
--key-type $key_type \
--label $key_label \
--id $id
# # Generate certificate signing request
openssl req -engine pkcs11 -new -batch \
-subj "/CN=${key_label}/" \
-keyform engine \
-key "label_${key_label}" \
-out ./${key_label}_req.pem \
-passin pass:$USR_PIN
# # Generate certificate
openssl ca -engine pkcs11 -batch \
-md ${md} -outdir ./ \
-in ./${key_label}_req.pem \
-cert "${signing_crt}" \
-keyform engine \
-keyfile "label_${signing_key_label}" \
-extfile $ca_dir/v3_${ca}.cnf \
-out "../crts/${key_label}_crt.pem" \
-notext \
-days ${val_period} \
-config $ca_dir/openssl.cnf \
-passin pass:$USR_PIN
# # Convert certificate to DER format
openssl x509 -outform DER \
-in "../crts/${key_label}_crt.pem" \
-out "../crts/${key_label}_crt.der"
# # Store DER certificate back to the HSM
pkcs11-tool --module $PKCS11_MODULE_PATH -l \
--write-object "../crts/${key_label}_crt.der" \
--type cert \
--label ${key_label} \
--pin $USR_PIN \
--id $id
# Clean up
\rm -f *_req.pem
# Switch back to initial working directory, if needed.
if [ "${crt_dir}" != "${keys_dir}" ]
then
cd "${crt_dir}"
if [ $? -ge 1 ]
then
echo ERROR: "Cannot change directory to ${crt_dir}"
exit 1
fi
fi
exit 0
|