-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcclient_to_cel.c
196 lines (170 loc) · 4.49 KB
/
pcclient_to_cel.c
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
/*
* pcclient_to_cel
*
* Author: David Safford <david.safford@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2 of the
* License.
*
* Reads pcclient log from stdin, and puts CEL encapsulated events to stdout
*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <tss2/tss2_tpm2_types.h>
#include "cel.h"
/* max values for this parser */
#define MAX_EVENT_SIZE 2048
#define MAX_DIGEST_SIZE 48
#define MAX_NUM_DIGESTS 3
/* in an event2, collect all the digests for CEL encapsulation */
struct digest {
uint16_t algid;
uint32_t len;
uint8_t data[MAX_DIGEST_SIZE];
} digests[MAX_NUM_DIGESTS];
/* sequence numbers are per PCR */
uint32_t seqs[TPM2_MAX_PCRS];
void tlv_put(uint8_t t, uint32_t l, unsigned char v[])
{
uint32_t nl;
ssize_t ret;
nl = htonl(l);
ret = write(1, &t, 1);
ret = write(1, &nl, 4);
ret = write(1, v, l);
(void)ret;
}
void raw_put(uint8_t *v, int l)
{
ssize_t ret;
ret = write(1, v, l);
(void)ret;
}
bool read_uint32(uint32_t *t) {
uint32_t tmp;
ssize_t l;
l = read(0, &tmp, 4);
if (l != 4)
return false;
*t = tmp; /* assuming we are pcclient native...*/
return true;
}
bool read_digest(int i) {
if (read(0, &digests[i].algid, 2) != 2)
return false;
if (digests[i].algid == TPM2_ALG_SHA1)
digests[i].len = SHA1_HASH_LEN;
else if (digests[i].algid == TPM2_ALG_SHA256)
digests[i].len = SHA256_HASH_LEN;
else if (digests[i].algid == TPM2_ALG_SHA384)
digests[i].len = SHA384_HASH_LEN;
else
return false;
if (read(0, digests[i].data, digests[i].len) != digests[i].len)
return false;
return true;
}
bool read_event2() {
uint32_t pcr;
uint32_t event_type;
uint8_t *event_data;
uint32_t event_size;
uint32_t digest_count;
int i, len = 0;
uint8_t t;
uint32_t tmp;
if (!read_uint32(&pcr))
return false;
if (!read_uint32(&event_type))
return false;
if (!read_uint32(&digest_count))
return false;
for (i=0;i<digest_count;i++) {
if(!read_digest(i))
return false;
len += digests[i].len + 5;
}
if (!read_uint32(&event_size))
return false;
if (!(event_data = malloc(event_size)))
return false;
if (read(0, event_data, event_size) != event_size)
return false;
/* CEL_SEQ */
tmp = htonl(seqs[pcr]++);
tlv_put(CEL_SEQ, 4, (unsigned char *) &tmp);
/* CEL_PCR */
tmp = htonl(pcr);
tlv_put(CEL_PCR, 4, (unsigned char *) &tmp);
t = CEL_DIGEST;
raw_put(&t, 1);
tmp = htonl(len);
raw_put((unsigned char *)&tmp, 4);
for (i=0;i<digest_count;i++){
t = digests[i].algid;
tlv_put(t, digests[i].len, (unsigned char *)digests[i].data);
}
t = CEL_CONTENT_PCCLIENT_STD;
raw_put(&t, 1);
tmp = htonl(5 + 5 + 4 + event_size);
raw_put((unsigned char *) &tmp, 4);
tmp = htonl(event_type);
tlv_put(PCCLIENT_EVENT_TYPE, 4, (unsigned char *) &tmp);
tlv_put(PCCLIENT_EVENT_CONTENT, event_size, (unsigned char *)event_data);
return true;
}
bool read_specid(void) {
uint32_t pcr;
uint32_t event_type;
uint8_t sha1[SHA1_HASH_LEN];
uint32_t event_size;
uint8_t *event_data;
uint32_t tmp;
uint8_t t;
if (!read_uint32(&pcr))
return false;
if (!read_uint32(&event_type))
return false;
if (read(0, sha1, SHA1_HASH_LEN) != SHA1_HASH_LEN)
return false;
if (!read_uint32(&event_size))
return false;
if (!(event_data = malloc(event_size)))
return false;
if (read(0, event_data, event_size) != event_size)
return false;
/* CEL_SEQ */
tmp = htonl(seqs[pcr]++);
tlv_put(CEL_SEQ, 4, (unsigned char *) &tmp);
/* CEL_PCR */
tmp = htonl(pcr);
tlv_put(CEL_PCR, 4, (unsigned char *) &tmp);
/* CEL_DIGEST */
t = CEL_DIGEST;
raw_put(&t, 1);
tmp = htonl(SHA1_HASH_LEN + 5);
raw_put((unsigned char *)&tmp, 4);
tlv_put(TPM2_ALG_SHA1, SHA1_HASH_LEN, (unsigned char *)sha1);
/* CEL_CONTENT */
t = CEL_CONTENT_PCCLIENT_STD;
raw_put(&t, 1);
tmp = htonl(event_size + 5 + 5 + 4);
raw_put((unsigned char *) &tmp, 4);
tmp = htonl(event_type);
tlv_put(PCCLIENT_EVENT_TYPE, 4, (unsigned char *) &tmp);
tlv_put(PCCLIENT_EVENT_CONTENT, event_size, (unsigned char *)event_data);
return true;
}
/* Read one specid event, and then all event2 records. */
int main(int argc, char *argv[]) {
if (!read_specid())
return -1;
while (read_event2())
;
}