moved building of attributes to imv_attestation_build
[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 attr = tcg_pts_attr_req_file_meta_create(FALSE, SOLIDUS_UTF, "/etc/tnc_config");
146 attr->set_noskip_flag(attr, TRUE);
147 msg->add_attribute(msg, attr);
148
149 /* Send Request File Measurement attribute */
150 enumerator = pts_db->create_file_enumerator(pts_db, platform_info);
151 if (!enumerator)
152 {
153 break;
154 }
155 while (enumerator->enumerate(enumerator, &id, &type, &pathname))
156 {
157 is_dir = (type != 0);
158 request_id = attestation_state->add_request(attestation_state,
159 id, is_dir);
160 DBG2(DBG_IMV, "measurement request %d for %s '%s'",
161 request_id, is_dir ? "directory" : "file", pathname);
162 attr = tcg_pts_attr_req_file_meas_create(is_dir, request_id,
163 delimiter, pathname);
164 attr->set_noskip_flag(attr, TRUE);
165 msg->add_attribute(msg, attr);
166 }
167 enumerator->destroy(enumerator);
168 break;
169 }
170 case IMV_ATTESTATION_STATE_COMP_EVID:
171 {
172 pts_attr_req_funct_comp_evid_flag_t flags;
173 u_int32_t sub_comp_depth;
174 pts_qualifier_t qualifier;
175 pts_funct_comp_name_t name;
176
177 attestation_state->set_handshake_state(attestation_state,
178 IMV_ATTESTATION_STATE_END);
179
180 flags = PTS_REQ_FUNC_COMP_FLAG_PCR;
181 sub_comp_depth = 0;
182 qualifier.kernel = FALSE;
183 qualifier.sub_component = FALSE;
184 qualifier.type = PTS_FUNC_COMP_TYPE_ALL;
185 name = PTS_FUNC_COMP_NAME_BIOS;
186
187 /* Send Request Functional Component Evidence attribute */
188 attr = tcg_pts_attr_req_funct_comp_evid_create(flags, sub_comp_depth,
189 PEN_TCG, qualifier, name);
190 attr->set_noskip_flag(attr, TRUE);
191 msg->add_attribute(msg, attr);
192 /* Send Generate Attestation Evidence attribute */
193 attr = tcg_pts_attr_gen_attest_evid_create();
194 attr->set_noskip_flag(attr, TRUE);
195 msg->add_attribute(msg, attr);
196
197 break;
198 }
199 default:
200 DBG1(DBG_IMV, "Attestation IMV is in unknown state: \"%s\"",
201 handshake_state);
202 return FALSE;
203 }
204 return TRUE;
205 }