Wait for the arrival of the TCPG_PTS_DH_NONCE_PARAMS_RESP
[strongswan.git] / src / libpts / plugins / imv_attestation / imv_attestation_build.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu
3 * Copyright (C) 2011-2014 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "imv_attestation_build.h"
18 #include "imv_attestation_state.h"
19
20 #include <tcg/pts/tcg_pts_attr_dh_nonce_params_req.h>
21 #include <tcg/pts/tcg_pts_attr_dh_nonce_finish.h>
22 #include <tcg/pts/tcg_pts_attr_get_tpm_version_info.h>
23 #include <tcg/pts/tcg_pts_attr_get_aik.h>
24 #include <tcg/pts/tcg_pts_attr_req_func_comp_evid.h>
25 #include <tcg/pts/tcg_pts_attr_gen_attest_evid.h>
26
27 #include <utils/debug.h>
28
29 bool imv_attestation_build(imv_msg_t *out_msg, imv_state_t *state,
30 pts_dh_group_t supported_dh_groups,
31 pts_database_t *pts_db)
32 {
33 imv_attestation_state_t *attestation_state;
34 imv_attestation_handshake_state_t handshake_state;
35 pts_t *pts;
36 pa_tnc_attr_t *attr = NULL;
37
38 attestation_state = (imv_attestation_state_t*)state;
39 handshake_state = attestation_state->get_handshake_state(attestation_state);
40 pts = attestation_state->get_pts(attestation_state);
41
42 switch (handshake_state)
43 {
44 case IMV_ATTESTATION_STATE_NONCE_REQ:
45 {
46 int min_nonce_len;
47
48 /* Send DH nonce parameters request attribute */
49 min_nonce_len = lib->settings->get_int(lib->settings,
50 "%s.plugins.imv-attestation.min_nonce_len", 0, lib->ns);
51 attr = tcg_pts_attr_dh_nonce_params_req_create(min_nonce_len,
52 supported_dh_groups);
53 attr->set_noskip_flag(attr, TRUE);
54 out_msg->add_attribute(out_msg, attr);
55
56 attestation_state->set_handshake_state(attestation_state,
57 IMV_ATTESTATION_STATE_TPM_INIT);
58 break;
59 }
60 case IMV_ATTESTATION_STATE_TPM_INIT:
61 {
62 pts_meas_algorithms_t selected_algorithm;
63 chunk_t initiator_value, initiator_nonce;
64
65 if (!(state->get_action_flags(state) & IMV_ATTESTATION_DH_NONCE))
66 {
67 break;
68 }
69
70 /* Send DH nonce finish attribute */
71 selected_algorithm = pts->get_meas_algorithm(pts);
72 pts->get_my_public_value(pts, &initiator_value, &initiator_nonce);
73 attr = tcg_pts_attr_dh_nonce_finish_create(selected_algorithm,
74 initiator_value, initiator_nonce);
75 attr->set_noskip_flag(attr, TRUE);
76 out_msg->add_attribute(out_msg, attr);
77
78 /* Send Get TPM Version attribute */
79 attr = tcg_pts_attr_get_tpm_version_info_create();
80 attr->set_noskip_flag(attr, TRUE);
81 out_msg->add_attribute(out_msg, attr);
82
83 /* Send Get AIK attribute */
84 attr = tcg_pts_attr_get_aik_create();
85 attr->set_noskip_flag(attr, TRUE);
86 out_msg->add_attribute(out_msg, attr);
87
88 attestation_state->set_handshake_state(attestation_state,
89 IMV_ATTESTATION_STATE_COMP_EVID);
90 break;
91 }
92 case IMV_ATTESTATION_STATE_COMP_EVID:
93 {
94 tcg_pts_attr_req_func_comp_evid_t *attr_cast;
95 enumerator_t *enumerator;
96 pts_comp_func_name_t *name;
97 uint8_t flags;
98 uint32_t depth;
99 bool first_component = TRUE;
100
101 attestation_state->set_handshake_state(attestation_state,
102 IMV_ATTESTATION_STATE_END);
103
104 if (!pts->get_aik_id(pts))
105 {
106 attestation_state->set_measurement_error(attestation_state,
107 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
108 return FALSE;
109 }
110
111 enumerator = attestation_state->create_component_enumerator(
112 attestation_state);
113 while (enumerator->enumerate(enumerator, &flags, &depth, &name))
114 {
115 if (first_component)
116 {
117 attr = tcg_pts_attr_req_func_comp_evid_create();
118 attr->set_noskip_flag(attr, TRUE);
119 first_component = FALSE;
120 DBG2(DBG_IMV, "evidence request by");
121 }
122 name->log(name, " ");
123
124 /* TODO check flags against negotiated_caps */
125 attr_cast = (tcg_pts_attr_req_func_comp_evid_t *)attr;
126 attr_cast->add_component(attr_cast, flags, depth, name);
127 }
128 enumerator->destroy(enumerator);
129
130 if (attr)
131 {
132 /* Send Request Functional Component Evidence attribute */
133 out_msg->add_attribute(out_msg, attr);
134
135 /* Send Generate Attestation Evidence attribute */
136 attr = tcg_pts_attr_gen_attest_evid_create();
137 attr->set_noskip_flag(attr, TRUE);
138 out_msg->add_attribute(out_msg, attr);
139
140 attestation_state->set_handshake_state(attestation_state,
141 IMV_ATTESTATION_STATE_EVID_FINAL);
142 }
143 break;
144 }
145 default:
146 break;
147 }
148
149 return TRUE;
150 }