Skip to content

Commit 3a16bd0

Browse files
ravi-prevastimg236
authored andcommitted
rpi-eeprom-digest: support specifying keys via PKCS#11 URI
In production setups, it is quite normal that the private key does not exist as a file in the file system, but is kept inside some HSM, remote signing service or similar, and only accessed via some pkcs#11 interface; moreover, by design, the private key _cannot_ be extracted from the HSM or signing service. In such a case, the user will have set OPENSSL_CONF to some configuration file setting up the appropriate engine, and the "key" is simply the pkcs#11 URI, e.g. "pkcs11:model=foo;object=bar". In order to support this use case, automatically infer the appropriate options to pass to openssl-dgst if "${KEY}" begins with "pkcs11:". Doing this at the top level avoids duplicating the logic in both writeSig and verifySig. While here, this also adds a sanity check that -v can only be used while also providing a (public) key to check against. This drops the -keyform argument in the non-pkcs#11 case, as openssl automatically infers the type, and this then in fact allows one to use a private key in e.g. DER format. Signed-off-by: Rasmus Villemoes <[email protected]>
1 parent 28a2c02 commit 3a16bd0

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

rpi-eeprom-digest

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ Options:
5959
-k Optional RSA private key.
6060
6161
RSA signing
62-
If a private key in PEM format is supplied then the RSA signature of the
63-
sha256 digest is included in the .sig file. Currently, the bootloader only
64-
supports sha256 digests signed with a 2048bit RSA key.
65-
The bootloader only verifies RSA signatures in signed boot mode
66-
and only for the EEPROM config file and the signed image.
62+
If a private key in PEM format or a pkcs#11 URI is supplied then the
63+
RSA signature of the sha256 digest is included in the .sig
64+
file. Currently, the bootloader only supports sha256 digests signed
65+
with a 2048bit RSA key. The bootloader only verifies RSA signatures
66+
in signed boot mode and only for the EEPROM config file and the signed
67+
image.
6768
6869
Examples:
6970
@@ -78,6 +79,9 @@ rpi-eeprom-digest -k private.pem -i boot.img -o boot.sig
7879
# As used by update-pieeprom.sh in usbboot/secure-boot-recovery
7980
rpi-eeprom-digest -k private.pem -i bootconf.txt -o bootconf.sig
8081
82+
# Similarly, but specifying the key with a PKCS#11 URI
83+
rpi-eeprom-digest -k pkcs11:token=deadbeef;object=bl-key;type=private;pin-value=1234 -i bootconf.txt -o bootconf.sig
84+
8185
# To verify the signature of an existing .sig file using the public key.
8286
# N.B The key file must be the PUBLIC key in PEM format.
8387
rpi-eeprom-digest -k public.pem -i boot.bin -v boot.sig
@@ -99,8 +103,7 @@ writeSig() {
99103
fi
100104

101105
if [ -n "${KEY}" ]; then
102-
[ -f "${KEY}" ] || die "RSA private \"${KEY}\" not found"
103-
"${OPENSSL}" dgst -sign "${KEY}" -keyform PEM -sha256 -out "${SIG_TMP}" "${IMAGE}"
106+
"${OPENSSL}" dgst ${ENGINE_OPTS} -sign "${KEY}" -sha256 -out "${SIG_TMP}" "${IMAGE}"
104107
echo "rsa2048: $(xxd -c 4096 -p < "${SIG_TMP}")" >> "${OUTPUT}"
105108
fi
106109
}
@@ -113,7 +116,7 @@ verifySig() {
113116
[ -n "${sig_hex}" ] || die "No RSA signature in ${sig_file}"
114117

115118
echo ${sig_hex} | xxd -c 4096 -p -r > "${TMP_DIR}/sig.bin"
116-
"${OPENSSL}" dgst -verify "${KEY}" -signature "${TMP_DIR}/sig.bin" "${IMAGE}" || die "${IMAGE} not verified"
119+
"${OPENSSL}" dgst ${ENGINE_OPTS} -verify "${KEY}" -signature "${TMP_DIR}/sig.bin" "${IMAGE}" || die "${IMAGE} not verified"
117120
}
118121

119122
OUTPUT=""
@@ -142,6 +145,18 @@ checkDependencies
142145

143146
[ -n "${IMAGE}" ] || usage
144147
[ -f "${IMAGE}" ] || die "Source image \"${IMAGE}\" not found"
148+
[ "${VERIFY}" != 1 ] || [ -n "${KEY}" ] || die "Option -v also requires passing public key via -k"
149+
150+
if [ -n "${KEY}" ] ; then
151+
if [ -f "${KEY}" ] ; then
152+
ENGINE_OPTS=
153+
elif echo "${KEY}" | grep -q "^pkcs11:" ; then
154+
ENGINE_OPTS="-engine pkcs11 -keyform engine"
155+
else
156+
die "RSA key \"${KEY}\" not found"
157+
fi
158+
fi
159+
145160
if [ "${VERIFY}" = 1 ]; then
146161
verifySig "${SIGNATURE}"
147162
else

0 commit comments

Comments
 (0)