summaryrefslogtreecommitdiff
path: root/futility/vb2_helper.c
blob: 355416175902b7f7a18b36675fc164595c5eef3f (plain)
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
/*
 * Copyright 2015 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "2sysincludes.h"
#include "2common.h"
#include "2guid.h"
#include "2rsa.h"
#include "util_misc.h"
#include "vb2_common.h"
#include "vb2_struct.h"

#include "host_common.h"
#include "host_key2.h"
#include "host_misc2.h"

#include "file_type.h"
#include "futility.h"
#include "traversal.h"

enum futil_file_type recognize_vb2_key(uint8_t *buf, uint32_t len)
{
	struct vb2_public_key pubkey;
	struct vb2_private_key *privkey = 0;

	/* The pubkey points into buf, so nothing to free */
	if (VB2_SUCCESS == vb2_unpack_key(&pubkey, buf, len))
		return FILE_TYPE_VB2_PUBKEY;

	/* The private key unpacks into new structs */
	if (VB2_SUCCESS == vb2_private_key_unpack(&privkey, buf, len)) {
		vb2_private_key_free(privkey);
		return FILE_TYPE_VB2_PRIVKEY;
	}

	return FILE_TYPE_UNKNOWN;
}

static void vb2_print_public_key_sha1sum(struct vb2_public_key *key)
{
	struct vb2_packed_key *pkey;
	uint8_t *digest;
	int i;

	if (vb2_public_key_pack(&pkey, key)) {
		printf("<error>");
		return;
	}

	digest = DigestBuf((uint8_t *)pkey + pkey->key_offset,
			   pkey->key_size, SHA1_DIGEST_ALGORITHM);
	for (i = 0; i < SHA1_DIGEST_SIZE; i++)
		printf("%02x", digest[i]);

	free(digest);
	free(pkey);
}

int futil_cb_show_vb2_pubkey(struct futil_traverse_state_s *state)
{
	struct vb2_public_key key;
	char guid_str[VB2_GUID_MIN_STRLEN];
	const struct vb2_text_vs_enum *entry;

	/* The key's members will point into the state buffer after this. Don't
	 * free anything. */
	if (VB2_SUCCESS != vb2_unpack_key(&key, state->my_area->buf,
					  state->my_area->len))
		return 1;

	if (VB2_SUCCESS != vb2_guid_to_str(key.guid, guid_str,
					   sizeof(guid_str)))
		return 1;

	printf("Public Key file:       %s\n", state->in_filename);
	printf("  Vboot API:           2.1\n");
	printf("  Desc:                \"%s\"\n", key.desc);
	entry = vb2_lookup_by_num(vb2_text_vs_sig, key.sig_alg);
	printf("  Signature Algorithm: %d %s\n", key.sig_alg,
	       entry ? entry->name : "(invalid)");
	entry = vb2_lookup_by_num(vb2_text_vs_hash, key.hash_alg);
	printf("  Hash Algorithm:      %d %s\n", key.hash_alg,
	       entry ? entry->name : "(invalid)");
	printf("  GUID:                %s\n", guid_str);
	printf("  Version:             0x%08x\n", key.version);
	printf("  Key sha1sum:         ");
	vb2_print_public_key_sha1sum(&key);
	printf("\n");

	return 0;
}

static void vb2_print_private_key_sha1sum(struct vb2_private_key *key)
{
	uint8_t *buf, *digest;
	uint32_t buflen;
	int i;

	if (vb_keyb_from_rsa(key->rsa_private_key, &buf, &buflen)) {
		printf("<error>");
		return;
	}

	digest = DigestBuf(buf, buflen, SHA1_DIGEST_ALGORITHM);
	for (i = 0; i < SHA1_DIGEST_SIZE; i++)
		printf("%02x", digest[i]);

	free(digest);
	free(buf);
}

int futil_cb_show_vb2_privkey(struct futil_traverse_state_s *state)
{
	struct vb2_private_key *key = 0;
	char guid_str[VB2_GUID_MIN_STRLEN];
	const struct vb2_text_vs_enum *entry;

	if (VB2_SUCCESS != vb2_private_key_unpack(&key, state->my_area->buf,
						  state->my_area->len))
		return 1;

	if (VB2_SUCCESS != vb2_guid_to_str(&key->guid, guid_str,
					   sizeof(guid_str))) {
		vb2_private_key_free(key);
		return 1;
	}


	printf("Private key file:      %s\n", state->in_filename);
	printf("  Vboot API:           2.1\n");
	printf("  Desc:                \"%s\"\n", key->desc ? key->desc : "");
	entry = vb2_lookup_by_num(vb2_text_vs_sig, key->sig_alg);
	printf("  Signature Algorithm: %d %s\n", key->sig_alg,
	       entry ? entry->name : "(invalid)");
	entry = vb2_lookup_by_num(vb2_text_vs_hash, key->hash_alg);
	printf("  Hash Algorithm:      %d %s\n", key->hash_alg,
	       entry ? entry->name : "(invalid)");
	printf("  GUID:                %s\n", guid_str);
	printf("  Key sha1sum:         ");
	vb2_print_private_key_sha1sum(key);
	printf("\n");

	vb2_private_key_free(key);
	return 0;
}