Change the way private key and cert are embedded.
OpenSSL seems to serialize bigints as signed value, which means the ECC key may end up being 33 bytes instead of the 32 bytes we're expecting, causing build to fail. The shell script extraction is now replaced by a build.rs script that uses OpenSSL to extract the content and do sanity checks. Forcing generating cryptographic materials now always generate a key and a certificate (useful to compile/flash multiple keys without them being considered as clones). The self-signed CA is left untouched.
This commit is contained in:
@@ -36,6 +36,7 @@ generate_crypto_materials () {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
force_generate="$1"
|
||||
mkdir -p crypto_data
|
||||
if [ ! -f "${ca_priv_key}" ]
|
||||
then
|
||||
@@ -60,12 +61,12 @@ generate_crypto_materials () {
|
||||
-sha256
|
||||
fi
|
||||
|
||||
if [ ! -f "${opensk_key}" ]
|
||||
if [ "${force_generate}" = "Y" -o ! -f "${opensk_key}" ]
|
||||
then
|
||||
"${openssl}" ecparam -genkey -name prime256v1 -out "${opensk_key}"
|
||||
fi
|
||||
|
||||
if [ ! -f "${opensk_cert_name}.pem" ]
|
||||
if [ "${force_generate}" = "Y" -o ! -f "${opensk_cert_name}.pem" ]
|
||||
then
|
||||
"${openssl}" req \
|
||||
-new \
|
||||
@@ -83,87 +84,4 @@ generate_crypto_materials () {
|
||||
-out "${opensk_cert_name}.pem" \
|
||||
-sha256
|
||||
fi
|
||||
|
||||
local cert_mtime=$(stat --printf="%Y" "${opensk_cert_name}.pem")
|
||||
local rust_file_mtime=0
|
||||
# Only take into consideration the mtime of the file if it exists and if we're not forcing
|
||||
# the rust file to be re-generated.
|
||||
if [ -f "${rust_file}" -a "x$1" != "xY" ]
|
||||
then
|
||||
rust_file_mtime=$(stat --printf="%Y" "${rust_file}")
|
||||
fi
|
||||
if [ $cert_mtime -gt $rust_file_mtime ]
|
||||
then
|
||||
local cert_size=$("${openssl}" x509 \
|
||||
-in "${opensk_cert_name}.pem" \
|
||||
-outform der 2>/dev/null \
|
||||
| wc -c)
|
||||
local cert_serial_hex=$("${openssl}" x509 \
|
||||
-in "${opensk_cert_name}.pem" \
|
||||
-noout \
|
||||
-serial \
|
||||
| cut -d'=' -f2)
|
||||
# Pad with zeroes in case the serial is too short. We don't care if the
|
||||
# serial is longer than 32 characters as we will only process the first 32
|
||||
# characters in the loop later.
|
||||
cert_serial_hex="${cert_serial_hex}00000000000000000000000000000000"
|
||||
|
||||
# Create header
|
||||
echo "// This file had been generated by OpenSK deploy.sh script" > "${rust_file}"
|
||||
echo "" >> "${rust_file}"
|
||||
|
||||
echo "pub const AAGUID: [u8; 16] = [" >> "${rust_file}"
|
||||
for i in `seq 0 2 30`
|
||||
do
|
||||
echo -n "0x${cert_serial_hex:$i:2}, " >> "${rust_file}"
|
||||
done
|
||||
echo "" >> "${rust_file}"
|
||||
echo "];" >> "${rust_file}"
|
||||
echo "" >> "${rust_file}"
|
||||
|
||||
echo "pub const ATTESTATION_CERTIFICATE: [u8; ${cert_size}] = [" >> "${rust_file}"
|
||||
"${openssl}" x509 \
|
||||
-in "${opensk_cert_name}.pem" \
|
||||
-outform der 2>/dev/null \
|
||||
| xxd -i >> "${rust_file}"
|
||||
echo "];" >> "${rust_file}"
|
||||
echo "" >> "${rust_file}"
|
||||
|
||||
# Private key is tricky to extract as we want the raw value and not the DER encoding
|
||||
# Example output of openssl ec -in file.key -noout -text:
|
||||
# read EC key
|
||||
# Private-Key: (256 bit)
|
||||
# priv:
|
||||
# 47:b3:58:b8:f0:09:1d:72:b1:03:34:62:9a:c7:b2:
|
||||
# b2:e1:06:28:15:69:d4:82:b5:4e:21:6d:98:bf:65:
|
||||
# 98:34
|
||||
# pub:
|
||||
# 04:32:84:a1:3c:90:db:3f:db:d7:fb:ff:e9:00:c8:
|
||||
# 8a:a1:79:2e:95:2e:7c:86:ec:19:03:97:6e:7c:d6:
|
||||
# 67:eb:28:56:f1:d8:dd:cb:ae:ce:b9:cb:e4:6d:9d:
|
||||
# 1d:76:96:fc:48:9b:2d:d5:80:86:04:3d:f9:fe:6c:
|
||||
# f3:9a:45:bc:b1
|
||||
# ASN1 OID: prime256v1
|
||||
# NIST CURVE: P-256
|
||||
#
|
||||
# The awk script starts printing lines after seeing a line starting with
|
||||
# "priv:" and stops printing as soon as it reaches a line that doesn't start
|
||||
# with a space.
|
||||
# The sed script then converts the output into a proper hex-encode byte
|
||||
# array by replacing the initial spaces on each line with "0x", replacing
|
||||
# the semicolons at the end of each line by commas and replacing all
|
||||
# remainging semicolons by ". 0x".
|
||||
echo "pub const ATTESTATION_PRIVATE_KEY: [u8; 32] = [" >> "${rust_file}"
|
||||
"${openssl}" ec \
|
||||
-in "${opensk_key}" \
|
||||
-noout \
|
||||
-text 2>/dev/null \
|
||||
| awk '/^priv:/{p=1;next}/^[^ ]/{p=0}p' \
|
||||
| sed -e 's/^ */0x/;s/:$/,/;s/:/, 0x/g' >> "${rust_file}"
|
||||
echo "];" >> "${rust_file}"
|
||||
echo "" >> "${rust_file}"
|
||||
|
||||
# If the tool is installed, prettify the file. It will catch syntax errors earlier.
|
||||
which rustfmt > /dev/null 2>&1 && rustfmt "${rust_file}"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user