File not Found, Invalid path, Invalid Delimiter PTS errors case checks implemented
[strongswan.git] / src / libimcv / plugins / imc_attestation / imc_attestation.c
index 937a7ec..58fb178 100644 (file)
 #include <pa_tnc/pa_tnc_msg.h>
 #include <ietf/ietf_attr.h>
 #include <ietf/ietf_attr_pa_tnc_error.h>
+#include <ietf/ietf_attr_product_info.h>
+
+#include <libpts.h>
+
+#include <pts/pts_error.h>
 
 #include <tcg/tcg_pts_attr_proto_caps.h>
 #include <tcg/tcg_pts_attr_meas_algo.h>
@@ -38,7 +43,6 @@
 #include <pen/pen.h>
 #include <debug.h>
 #include <utils/linked_list.h>
-#include <crypto/hashers/hasher.h>
 
 /* IMC definitions */
 
@@ -67,12 +71,19 @@ TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
                DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
                return TNC_RESULT_ALREADY_INITIALIZED;
        }
+       if (!pts_meas_probe_algorithms(&supported_algorithms))
+       {
+               return TNC_RESULT_FATAL;
+       }
        imc_attestation = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
-                                                               imc_id, actual_version);
-       if (!imc_attestation || !pts_meas_probe_algorithms(&supported_algorithms))
+                                                                          imc_id, actual_version);
+       if (!imc_attestation)
        {
                return TNC_RESULT_FATAL;
        }
+
+       libpts_init();
+
        if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
        {
                DBG1(DBG_IMC, "no common IF-IMC version");
@@ -120,12 +131,42 @@ TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
                                                                  TNC_ConnectionID connection_id)
 {
+       imc_state_t *state;
+       imc_attestation_state_t *attestation_state;
+       pts_t *pts;
+       char *platform_info;
+       TNC_Result result = TNC_RESULT_SUCCESS;
+
        if (!imc_attestation)
        {
                DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
                return TNC_RESULT_NOT_INITIALIZED;
        }
-       return TNC_RESULT_SUCCESS;
+
+       /* get current IMC state */
+       if (!imc_attestation->get_state(imc_attestation, connection_id, &state))
+       {
+               return TNC_RESULT_FATAL;
+       }
+       attestation_state = (imc_attestation_state_t*)state;
+       pts = attestation_state->get_pts(attestation_state);
+
+       platform_info = pts->get_platform_info(pts);
+       if (platform_info)
+       {
+               pa_tnc_msg_t *pa_tnc_msg;
+               pa_tnc_attr_t *attr;
+
+               pa_tnc_msg = pa_tnc_msg_create();
+               attr = ietf_attr_product_info_create(0, 0, platform_info);
+               pa_tnc_msg->add_attribute(pa_tnc_msg, attr);
+               pa_tnc_msg->build(pa_tnc_msg);
+               result = imc_attestation->send_message(imc_attestation, connection_id,
+                                                                       pa_tnc_msg->get_encoding(pa_tnc_msg));
+               pa_tnc_msg->destroy(pa_tnc_msg);
+       }
+
+       return result;
 }
 
 /**
@@ -161,7 +202,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
        attestation_state = (imc_attestation_state_t*)state;
        pts = attestation_state->get_pts(attestation_state);
 
-       /* parse received PA-TNC message and automatically handle any errors */ 
+       /* parse received PA-TNC message and automatically handle any errors */
        result = imc_attestation->receive_message(imc_attestation, connection_id,
                                                                           chunk_create(msg, msg_len), msg_type,
                                                                           &pa_tnc_msg);
@@ -221,7 +262,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                        imc_flags = pts->get_proto_caps(pts);
                                        pts->set_proto_caps(pts, imc_flags & imv_flags);
 
-                                       /* Send PTS Protocol Capabilities attribute */ 
+                                       /* Send PTS Protocol Capabilities attribute */
                                        attr = tcg_pts_attr_proto_caps_create(imc_flags & imv_flags,
                                                                                                                  FALSE);
                                        attr_list->insert_last(attr_list, attr);
@@ -231,7 +272,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                {
                                        tcg_pts_attr_meas_algo_t *attr_cast;
                                        pts_meas_algorithms_t selected_algorithm;
-                                       
+       
                                        attr_cast = (tcg_pts_attr_meas_algo_t*)attr;
                                        selected_algorithm = attr_cast->get_algorithms(attr_cast);
 
@@ -241,63 +282,67 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                                pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA384);
                                        }
                                        else if ((supported_algorithms & PTS_MEAS_ALGO_SHA256) &&
-                                               (selected_algorithm & PTS_MEAS_ALGO_SHA256))
+                                                        (selected_algorithm & PTS_MEAS_ALGO_SHA256))
                                        {
                                                pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA256);
                                        }
 
                                        else if ((supported_algorithms & PTS_MEAS_ALGO_SHA1) &&
-                                               (selected_algorithm & PTS_MEAS_ALGO_SHA1))
+                                                        (selected_algorithm & PTS_MEAS_ALGO_SHA1))
                                        {
                                                pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA1);
                                        }
                                        else
                                        {
-                                               /* TODO send a TCG_PTS_H_ALG_NOT_SUPPORTED error */
+                                               attr = pts_hash_alg_error_create(supported_algorithms);
+                                               attr_list->insert_last(attr_list, attr);
+                                               break;
                                        }
 
-                                       /* Send Measurement Algorithm Selection attribute */ 
+                                       /* Send Measurement Algorithm Selection attribute */
                                        selected_algorithm = pts->get_meas_algorithm(pts);
                                        attr = tcg_pts_attr_meas_algo_create(selected_algorithm,
                                                                                                                 TRUE);
                                        attr_list->insert_last(attr_list, attr);
                                        break;
                                }
-                                       
+       
                                case TCG_PTS_GET_TPM_VERSION_INFO:
                                {
-                                       chunk_t tpm_version_info;
+                                       chunk_t tpm_version_info, attr_info;
 
                                        if (!pts->get_tpm_version_info(pts, &tpm_version_info))
                                        {
-                                               /* TODO return TCG_PTS_TPM_VERS_NOT_SUPPORTED error attribute */
+                                               attr_info = attr->get_value(attr);
+                                               attr = ietf_attr_pa_tnc_error_create(PEN_TCG,
+                                                                       TCG_PTS_TPM_VERS_NOT_SUPPORTED, attr_info);
+                                               attr_list->insert_last(attr_list, attr);
                                                break;
                                        }
-                                       
-                                       /* Send TPM Version Info attribute */ 
+       
+                                       /* Send TPM Version Info attribute */
                                        attr = tcg_pts_attr_tpm_version_info_create(tpm_version_info);
                                        attr_list->insert_last(attr_list, attr);
                                        break;
                                }
-                               
+       
                                case TCG_PTS_GET_AIK:
                                {
-                                       chunk_t aik;
-                                       bool is_naked_key;
+                                       certificate_t *aik;
 
-                                       if (!pts->get_aik(pts, &aik, &is_naked_key))
+                                       aik = pts->get_aik(pts);
+                                       if (!aik)
                                        {
-                                               DBG1(DBG_IMC,"Obtaining AIK Certificate failed");
+                                               DBG1(DBG_IMC, "no AIK certificate or public key available");
                                                break;
                                        }
-                                       
-                                       /* Send AIK attribute */ 
-                                       attr_to_send = tcg_pts_attr_aik_create(is_naked_key, aik);
-                                       attr_to_send = (pa_tnc_attr_t*)attr_to_send;
-                                       attr_list->insert_last(attr_list,attr_to_send);
+       
+                                       /* Send AIK attribute */
+                                       attr = tcg_pts_attr_aik_create(aik);
+                                       attr_list->insert_last(attr_list, attr);
                                        break;
                                }
-                               
+       
                                /* PTS-based Attestation Evidence */
                                case TCG_PTS_REQ_FUNCT_COMP_EVID:
                                        break;
@@ -306,80 +351,56 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                case TCG_PTS_REQ_FILE_MEAS:
                                {
                                        tcg_pts_attr_req_file_meas_t *attr_cast;
-                                       tcg_pts_attr_file_meas_t *attr_out;
                                        char *pathname;
-                                       u_int16_t request_id, meas_len;
-                                       pts_meas_algorithms_t selected_algorithm;
-                                       chunk_t file_hash;
-                                       bool directory_flag;
-                                       linked_list_t *file_measurements;
-
+                                       u_int16_t request_id;
+                                       bool is_directory;
+                                       u_int32_t delimiter;
+                                       pts_file_meas_t *measurements;
+                                       pts_error_code_t pts_error;
+                                       chunk_t attr_info;
+                                       
+                                       attr_info = attr->get_value(attr);
                                        attr_cast = (tcg_pts_attr_req_file_meas_t*)attr;
-                                       directory_flag = attr_cast->get_directory_flag(attr_cast);
+                                       is_directory = attr_cast->get_directory_flag(attr_cast);
                                        request_id = attr_cast->get_request_id(attr_cast);
+                                       delimiter = attr_cast->get_delimiter(attr_cast);
                                        pathname = attr_cast->get_pathname(attr_cast);
                                        
-                                       DBG2(DBG_IMC, "%s to be measured: '%s'", 
-                                            directory_flag ? "directory" : "file", pathname);
-                                       
-                                       /* Send File Measurement attribute */
-                                       selected_algorithm = pts->get_meas_algorithm(pts);
-                                       meas_len = HASH_SIZE_SHA1;
-                                       if(selected_algorithm & PTS_MEAS_ALGO_SHA384) 
+                                       if (pts->is_path_valid(pts, pathname, &pts_error) && pts_error)
                                        {
-                                               meas_len = HASH_SIZE_SHA384;
+                                               attr_info = attr->get_value(attr);
+                                               attr = ietf_attr_pa_tnc_error_create(PEN_TCG,
+                                                                                               pts_error, attr_info);
+                                               attr_list->insert_last(attr_list, attr);
+                                               break;
                                        }
-                                       else if (selected_algorithm & PTS_MEAS_ALGO_SHA256)
+                                       else if (!pts->is_path_valid(pts, pathname, &pts_error))
                                        {
-                                               meas_len = HASH_SIZE_SHA256;
+                                               break;
                                        }
-
-                                       /** 
-                                       * Hash the file or directory and add them as attribute
-                                       */
                                        
-                                       attr = directory_flag ? 
-                                               tcg_pts_attr_file_meas_create(0, request_id, meas_len) :
-                                               tcg_pts_attr_file_meas_create(1, request_id, meas_len);
-                                       attr->set_noskip_flag(attr, TRUE);
-                                       attr_out = (tcg_pts_attr_file_meas_t*)attr;
-                                       
-                                       if (!directory_flag)
+                                       if (delimiter != SOLIDUS_UTF && delimiter != REVERSE_SOLIDUS_UTF)
                                        {
-                                               if (!pts->hash_file(pts, pathname, &file_hash))
-                                               {
-                                                       DBG1(DBG_IMC, "Hashing the given file has failed");
-                                                       return TNC_RESULT_FATAL;
-                                               }
-                                               attr_out->add_file_meas(attr_out, file_hash, pathname);
+                                               attr_info = attr->get_value(attr);
+                                               attr = ietf_attr_pa_tnc_error_create(PEN_TCG,
+                                                                                               TCG_PTS_INVALID_DELIMITER, attr_info);
+                                               attr_list->insert_last(attr_list, attr);
+                                               break;
                                        }
-                                       else
+
+                                       /* Do PTS File Measurements and send them to PTS-IMV */
+                                       DBG2(DBG_IMC, "measurement request %d for %s '%s'",
+                                                request_id, is_directory ? "directory" : "file",
+                                                pathname);
+                                       measurements = pts->do_measurements(pts, request_id,
+                                                                                       pathname, is_directory);
+                                       if (!measurements)
                                        {
-                                               enumerator_t *meas_enumerator;
-                                               file_meas_entry_t *meas_entry;
-                                               u_int64_t num_of_files = 0 ;
-
-                                               if (!pts->hash_directory(pts, pathname, &file_measurements))
-                                               {
-                                                       DBG1(DBG_IMC, "Hashing the files in a given directory has failed");
-                                                       return TNC_RESULT_FATAL;
-                                               }
-                                               
-                                               meas_enumerator = file_measurements->create_enumerator(file_measurements);
-                                               while (meas_enumerator->enumerate(meas_enumerator, &meas_entry))
-                                               {
-                                                       num_of_files++;
-                                                       attr_out->add_file_meas(attr_out,
-                                                                                                       meas_entry->measurement,
-                                                                                                       meas_entry->filename);
-                                               }
-                                               
-                                               attr_out->set_number_of_files(attr_out, num_of_files);
-                                               meas_enumerator->destroy(meas_enumerator);
-                                               file_measurements->destroy(file_measurements);
-                                               
+                                               /* TODO handle error codes from measurements */
+                                               return TNC_RESULT_FATAL;
                                        }
-                                       
+                                       attr = tcg_pts_attr_file_meas_create(measurements);
+                                       attr->set_noskip_flag(attr, TRUE);
                                        attr_list->insert_last(attr_list, attr);
                                        break;
                                }
@@ -399,7 +420,7 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                case TCG_PTS_MEAS_ALGO_SELECTION:
                                case TCG_PTS_TPM_VERSION_INFO:
                                case TCG_PTS_TEMPL_REF_MANI_SET_META:
-                               case TCG_PTS_AIK:                               
+                               case TCG_PTS_AIK:
                                case TCG_PTS_SIMPLE_COMP_EVID:
                                case TCG_PTS_SIMPLE_EVID_FINAL:
                                case TCG_PTS_VERIFICATION_RESULT:
@@ -412,15 +433,13 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                                                tcg_attr_names, attr->get_type(attr));
                                        break;
                        }
-                       
-                       
                }
        }
        enumerator->destroy(enumerator);
        pa_tnc_msg->destroy(pa_tnc_msg);
 
        result = TNC_RESULT_SUCCESS;
-               
+       
        if (attr_list->get_count(attr_list))
        {
                pa_tnc_msg = pa_tnc_msg_create();
@@ -435,10 +454,9 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                pa_tnc_msg->build(pa_tnc_msg);
                result = imc_attestation->send_message(imc_attestation, connection_id,
                                                        pa_tnc_msg->get_encoding(pa_tnc_msg));
-       
-               attr_list->destroy(attr_list);
                pa_tnc_msg->destroy(pa_tnc_msg);
        }
+       attr_list->destroy(attr_list);
 
        return result;
 }
@@ -467,6 +485,9 @@ TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
                DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
                return TNC_RESULT_NOT_INITIALIZED;
        }
+
+       libpts_deinit();
+
        imc_attestation->destroy(imc_attestation);
        imc_attestation = NULL;