Replaced static string path with Metadata requests retrieved from pts database
[strongswan.git] / src / libimcv / plugins / imv_attestation / imv_attestation_build.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_attestation_build.h"
17 #include "imv_attestation_state.h"
18
19 #include <tcg/tcg_pts_attr_proto_caps.h>
20 #include <tcg/tcg_pts_attr_meas_algo.h>
21 #include <tcg/tcg_pts_attr_dh_nonce_params_req.h>
22 #include <tcg/tcg_pts_attr_dh_nonce_finish.h>
23 #include <tcg/tcg_pts_attr_get_tpm_version_info.h>
24 #include <tcg/tcg_pts_attr_get_aik.h>
25 #include <tcg/tcg_pts_attr_req_funct_comp_evid.h>
26 #include <tcg/tcg_pts_attr_gen_attest_evid.h>
27 #include <tcg/tcg_pts_attr_req_file_meas.h>
28 #include <tcg/tcg_pts_attr_req_file_meta.h>
29
30 #include <debug.h>
31
32 bool imv_attestation_build(pa_tnc_msg_t *msg,
33 imv_attestation_state_t *attestation_state,
34 pts_meas_algorithms_t supported_algorithms,
35 pts_dh_group_t supported_dh_groups,
36 pts_database_t *pts_db)
37 {
38 imv_attestation_handshake_state_t handshake_state;
39 pts_t *pts;
40 pa_tnc_attr_t *attr;
41
42 handshake_state = attestation_state->get_handshake_state(attestation_state);
43 pts = attestation_state->get_pts(attestation_state);
44
45 if (handshake_state == IMV_ATTESTATION_STATE_NONCE_REQ &&
46 !(pts->get_proto_caps(pts) & PTS_PROTO_CAPS_T))
47 {
48 DBG1(DBG_IMV, "PTS-IMC has no TPM capability - "
49 "advancing to PTS measurement phase");
50 handshake_state = IMV_ATTESTATION_STATE_MEAS;
51 }
52
53 /* Switch on the attribute type IMV has received */
54 switch (handshake_state)
55 {
56 case IMV_ATTESTATION_STATE_INIT:
57 {
58 pts_proto_caps_flag_t flags;
59
60 /* Send Request Protocol Capabilities attribute */
61 flags = pts->get_proto_caps(pts);
62 attr = tcg_pts_attr_proto_caps_create(flags, TRUE);
63 attr->set_noskip_flag(attr, TRUE);
64 msg->add_attribute(msg, attr);
65
66 /* Send Measurement Algorithms attribute */
67 attr = tcg_pts_attr_meas_algo_create(supported_algorithms, FALSE);
68 attr->set_noskip_flag(attr, TRUE);
69 msg->add_attribute(msg, attr);
70
71 attestation_state->set_handshake_state(attestation_state,
72 IMV_ATTESTATION_STATE_NONCE_REQ);
73 break;
74 }
75 case IMV_ATTESTATION_STATE_NONCE_REQ:
76 {
77 int min_nonce_len;
78
79 /* Send DH nonce parameters request attribute */
80 min_nonce_len = lib->settings->get_int(lib->settings,
81 "libimcv.plugins.imv-attestation.min_nonce_len", 0);
82 attr = tcg_pts_attr_dh_nonce_params_req_create(min_nonce_len,
83 supported_dh_groups);
84 attr->set_noskip_flag(attr, TRUE);
85 msg->add_attribute(msg, attr);
86
87 attestation_state->set_handshake_state(attestation_state,
88 IMV_ATTESTATION_STATE_TPM_INIT);
89 break;
90 }
91 case IMV_ATTESTATION_STATE_TPM_INIT:
92 {
93 pts_meas_algorithms_t selected_algorithm;
94 chunk_t initiator_value, initiator_nonce;
95
96 /* Send DH nonce finish attribute */
97 selected_algorithm = pts->get_meas_algorithm(pts);
98 pts->get_my_public_value(pts, &initiator_value, &initiator_nonce);
99 attr = tcg_pts_attr_dh_nonce_finish_create(selected_algorithm,
100 initiator_value, initiator_nonce);
101 attr->set_noskip_flag(attr, TRUE);
102 msg->add_attribute(msg, attr);
103
104 /* Send Get TPM Version attribute */
105 attr = tcg_pts_attr_get_tpm_version_info_create();
106 attr->set_noskip_flag(attr, TRUE);
107 msg->add_attribute(msg, attr);
108
109 /* Send Get AIK attribute */
110 attr = tcg_pts_attr_get_aik_create();
111 attr->set_noskip_flag(attr, TRUE);
112 msg->add_attribute(msg, attr);
113
114 attestation_state->set_handshake_state(attestation_state,
115 IMV_ATTESTATION_STATE_MEAS);
116 break;
117 }
118 case IMV_ATTESTATION_STATE_MEAS:
119 {
120
121 enumerator_t *enumerator;
122 u_int32_t delimiter = SOLIDUS_UTF;
123 char *platform_info, *pathname;
124 u_int16_t request_id;
125 int id, type;
126 bool is_dir;
127
128 attestation_state->set_handshake_state(attestation_state,
129 IMV_ATTESTATION_STATE_COMP_EVID);
130
131 /* Get Platform and OS of the PTS-IMC */
132 platform_info = pts->get_platform_info(pts);
133
134 if (!pts_db || !platform_info)
135 {
136 DBG1(DBG_IMV, "%s%s%s not available",
137 (pts_db) ? "" : "pts database",
138 (!pts_db && !platform_info) ? "and" : "",
139 (platform_info) ? "" : "platform info");
140 break;
141 }
142 DBG1(DBG_IMV, "platform is '%s'", platform_info);
143
144 /* Send Request File Metadata attribute */
145 enumerator = pts_db->create_file_meta_enumerator(pts_db, platform_info);
146 if (!enumerator)
147 {
148 break;
149 }
150 while (enumerator->enumerate(enumerator, &type, &pathname))
151 {
152 is_dir = (type != 0);
153 DBG2(DBG_IMV, "metadata request for %s '%s'",
154 is_dir ? "directory" : "file", pathname);
155 attr = tcg_pts_attr_req_file_meta_create(is_dir, delimiter, pathname);
156 attr->set_noskip_flag(attr, TRUE);
157 msg->add_attribute(msg, attr);
158 }
159 enumerator->destroy(enumerator);
160
161 /* Send Request File Measurement attribute */
162 enumerator = pts_db->create_file_enumerator(pts_db, platform_info);
163 if (!enumerator)
164 {
165 break;
166 }
167 while (enumerator->enumerate(enumerator, &id, &type, &pathname))
168 {
169 is_dir = (type != 0);
170 request_id = attestation_state->add_request(attestation_state,
171 id, is_dir);
172 DBG2(DBG_IMV, "measurement request %d for %s '%s'",
173 request_id, is_dir ? "directory" : "file", pathname);
174 attr = tcg_pts_attr_req_file_meas_create(is_dir, request_id,
175 delimiter, pathname);
176 attr->set_noskip_flag(attr, TRUE);
177 msg->add_attribute(msg, attr);
178 }
179 enumerator->destroy(enumerator);
180 break;
181 }
182 case IMV_ATTESTATION_STATE_COMP_EVID:
183 {
184 pts_attr_req_funct_comp_evid_flag_t flags;
185 u_int32_t sub_comp_depth;
186 pts_qualifier_t qualifier;
187 pts_funct_comp_name_t name;
188
189 attestation_state->set_handshake_state(attestation_state,
190 IMV_ATTESTATION_STATE_END);
191
192 flags = PTS_REQ_FUNC_COMP_FLAG_PCR;
193 sub_comp_depth = 0;
194 qualifier.kernel = FALSE;
195 qualifier.sub_component = FALSE;
196 qualifier.type = PTS_FUNC_COMP_TYPE_ALL;
197 name = PTS_FUNC_COMP_NAME_BIOS;
198
199 /* Send Request Functional Component Evidence attribute */
200 attr = tcg_pts_attr_req_funct_comp_evid_create(flags, sub_comp_depth,
201 PEN_TCG, qualifier, name);
202 attr->set_noskip_flag(attr, TRUE);
203 msg->add_attribute(msg, attr);
204 /* Send Generate Attestation Evidence attribute */
205 attr = tcg_pts_attr_gen_attest_evid_create();
206 attr->set_noskip_flag(attr, TRUE);
207 msg->add_attribute(msg, attr);
208
209 break;
210 }
211 default:
212 DBG1(DBG_IMV, "Attestation IMV is in unknown state: \"%s\"",
213 handshake_state);
214 return FALSE;
215 }
216 return TRUE;
217 }