Skip to content

Commit ec4d743

Browse files
Benjamin ShaiChromeos LUCI
Benjamin Shai
authored and
Chromeos LUCI
committed
vb21: load private key before signing
When we load the private key, it calls out to PKCS11 and sets up the modulus and ID of the key. Make sure to do that before signing data, so that PKCS11 keys have the ID copied into the signature block. BUG=b:413430417 BRANCH=None TEST=manual TEST=make runtests Change-Id: Iba05e451827aecb706fb433a3e03c413955c144e Signed-off-by: Benjamin Shai <[email protected]> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/6489297 Reviewed-by: Jakub "Kuba" Czapiga <[email protected]> Commit-Queue: Jakub "Kuba" Czapiga <[email protected]> Tested-by: Jakub "Kuba" Czapiga <[email protected]>
1 parent 39f6104 commit ec4d743

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

host/lib21/host_signature.c

+22-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <openssl/rsa.h>
9+
#include <unistd.h>
910

1011
#include "2common.h"
1112
#include "2rsa.h"
@@ -17,6 +18,7 @@
1718
#include "host_misc.h"
1819
#include "host_p11.h"
1920
#include "host_signature21.h"
21+
#include "util_misc.h"
2022

2123
vb2_error_t vb2_digest_info(enum vb2_hash_algorithm hash_alg,
2224
const uint8_t **buf_ptr, uint32_t *size_ptr)
@@ -69,9 +71,26 @@ vb2_error_t vb2_digest_info(enum vb2_hash_algorithm hash_alg,
6971
}
7072

7173
vb2_error_t vb21_sign_data(struct vb21_signature **sig_ptr, const uint8_t *data,
72-
uint32_t size, const struct vb2_private_key *key,
74+
uint32_t size, struct vb2_private_key *key,
7375
const char *desc)
7476
{
77+
/* Preinitialize these fields used in the error handling. */
78+
vb2_error_t rv;
79+
*sig_ptr = NULL;
80+
uint8_t *sig_digest = NULL;
81+
82+
if (key->key_location == PRIVATE_KEY_P11) {
83+
/* Load keyb from the key to force PKCS11 fields to initialize. */
84+
uint8_t *keyb_data;
85+
uint32_t keyb_size;
86+
if (vb_keyb_from_private_key(key, &keyb_data, &keyb_size)) {
87+
fprintf(stderr, "Couldn't extract the public key\n");
88+
rv = VB2_ERROR_UNKNOWN;
89+
goto done;
90+
}
91+
free(keyb_data);
92+
}
93+
7594
struct vb21_signature s = {
7695
.c.magic = VB21_MAGIC_SIGNATURE,
7796
.c.struct_version_major = VB21_SIGNATURE_VERSION_MAJOR,
@@ -83,17 +102,13 @@ vb2_error_t vb21_sign_data(struct vb21_signature **sig_ptr, const uint8_t *data,
83102
.id = key->id,
84103
};
85104

86-
vb2_error_t rv;
87105
struct vb2_digest_context dc;
88106
uint32_t digest_size;
89107
const uint8_t *info = NULL;
90108
uint32_t info_size = 0;
91109
uint32_t sig_digest_size;
92-
uint8_t *sig_digest = NULL;
93110
uint8_t *buf = NULL;
94111

95-
*sig_ptr = NULL;
96-
97112
/* Use key description if no description supplied */
98113
if (!desc)
99114
desc = key->desc;
@@ -230,7 +245,7 @@ vb2_error_t vb21_sig_size_for_keys(uint32_t *size_ptr,
230245
}
231246

232247
vb2_error_t vb21_sign_object(uint8_t *buf, uint32_t sig_offset,
233-
const struct vb2_private_key *key,
248+
struct vb2_private_key *key,
234249
const char *desc)
235250
{
236251
struct vb21_struct_common *c = (struct vb21_struct_common *)buf;
@@ -253,7 +268,7 @@ vb2_error_t vb21_sign_object(uint8_t *buf, uint32_t sig_offset,
253268
}
254269

255270
vb2_error_t vb21_sign_object_multiple(uint8_t *buf, uint32_t sig_offset,
256-
const struct vb2_private_key **key_list,
271+
struct vb2_private_key **key_list,
257272
uint32_t key_count)
258273
{
259274
struct vb21_struct_common *c = (struct vb21_struct_common *)buf;

host/lib21/include/host_signature21.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ vb2_error_t vb2_digest_info(enum vb2_hash_algorithm hash_alg,
3737
* @return VB2_SUCCESS, or non-zero error code on failure.
3838
*/
3939
vb2_error_t vb21_sign_data(struct vb21_signature **sig_ptr, const uint8_t *data,
40-
uint32_t size, const struct vb2_private_key *key,
40+
uint32_t size, struct vb2_private_key *key,
4141
const char *desc);
4242

4343
/**
@@ -76,7 +76,7 @@ vb2_error_t vb21_sig_size_for_keys(uint32_t *size_ptr,
7676
* @param desc If non-null, description to use for signature
7777
*/
7878
vb2_error_t vb21_sign_object(uint8_t *buf, uint32_t sig_offset,
79-
const struct vb2_private_key *key,
79+
struct vb2_private_key *key,
8080
const char *desc);
8181

8282
/**
@@ -90,7 +90,7 @@ vb2_error_t vb21_sign_object(uint8_t *buf, uint32_t sig_offset,
9090
* @param key_count Number of keys in list
9191
*/
9292
vb2_error_t vb21_sign_object_multiple(uint8_t *buf, uint32_t sig_offset,
93-
const struct vb2_private_key **key_list,
93+
struct vb2_private_key **key_list,
9494
uint32_t key_count);
9595

9696
#endif /* VBOOT_REFERENCE_HOST_SIGNATURE2_H_ */

tests/vb21_host_common_tests.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ static void test_sig_size(void)
207207
static void test_verify_hash(void)
208208
{
209209
struct vb21_signature *sig;
210-
const struct vb2_private_key *prik;
210+
struct vb2_private_key *prik;
211211
struct vb2_public_key pubk;
212212
uint8_t workbuf[VB2_VERIFY_DATA_WORKBUF_BYTES]
213213
__attribute__((aligned(VB2_WORKBUF_ALIGN)));
214214
struct vb2_workbuf wb;
215215

216216
vb2_workbuf_init(&wb, workbuf, sizeof(workbuf));
217217

218-
TEST_SUCC(vb2_private_key_hash(&prik, VB2_HASH_SHA256),
218+
TEST_SUCC(vb2_private_key_hash((const struct vb2_private_key **)&prik, VB2_HASH_SHA256),
219219
"create private hash key");
220220
TEST_SUCC(vb2_public_key_hash(&pubk, VB2_HASH_SHA256),
221221
"create hash key");

tests/vb21_host_sig_tests.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ static void sig_tests(const struct alg_combo *combo,
4040
const char *pemfile,
4141
const char *keybfile)
4242
{
43-
struct vb2_private_key *prik, prik2;
44-
const struct vb2_private_key *prihash, *priks[2];
43+
struct vb2_private_key *prik, prik2, *prihash, *priks[2];
4544
struct vb2_public_key *pubk, pubhash;
4645
struct vb21_signature *sig, *sig2;
4746
uint32_t size;
@@ -70,7 +69,8 @@ static void sig_tests(const struct alg_combo *combo,
7069
pubk->hash_alg = combo->hash_alg;
7170
vb2_public_key_set_desc(pubk, test_desc);
7271

73-
TEST_SUCC(vb2_private_key_hash(&prihash, combo->hash_alg),
72+
TEST_SUCC(vb2_private_key_hash((const struct vb2_private_key **)&prihash,
73+
combo->hash_alg),
7474
"Private hash key");
7575
TEST_SUCC(vb2_public_key_hash(&pubhash, combo->hash_alg),
7676
"Public hash key");
@@ -134,7 +134,8 @@ static void sig_tests(const struct alg_combo *combo,
134134
free(buf);
135135

136136
/* Multiply sign an object */
137-
TEST_SUCC(vb21_sig_size_for_keys(&size, priks, 2), "Sigs size");
137+
TEST_SUCC(vb21_sig_size_for_keys(&size, (const struct vb2_private_key **)priks, 2),
138+
"Sigs size");
138139
bufsize = c_sig_offs + size;
139140
buf = calloc(1, bufsize);
140141
memset(buf + sizeof(*c), 0x12, 24);

0 commit comments

Comments
 (0)