File not Found, Invalid path, Invalid Delimiter PTS errors case checks implemented
[strongswan.git] / src / libimcv / plugins / imc_attestation / imc_attestation.c
index 3747f38..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>
+#include <tcg/tcg_pts_attr_get_tpm_version_info.h>
 #include <tcg/tcg_pts_attr_tpm_version_info.h>
+#include <tcg/tcg_pts_attr_get_aik.h>
 #include <tcg/tcg_pts_attr_aik.h>
+#include <tcg/tcg_pts_attr_req_funct_comp_evid.h>
+#include <tcg/tcg_pts_attr_gen_attest_evid.h>
 #include <tcg/tcg_pts_attr_simple_comp_evid.h>
 #include <tcg/tcg_pts_attr_simple_evid_final.h>
+#include <tcg/tcg_pts_attr_req_file_meas.h>
 #include <tcg/tcg_pts_attr_file_meas.h>
 
 #include <tncif_pa_subtypes.h>
 #include <debug.h>
 #include <utils/linked_list.h>
 
-#include <trousers/tss.h>
-#include <trousers/trousers.h>
-
 /* IMC definitions */
 
 static const char imc_name[] = "Attestation";
 
 #define IMC_VENDOR_ID                          PEN_TCG
 #define IMC_SUBTYPE                                    PA_SUBTYPE_TCG_PTS
-#define IMC_ATTESTATION_BUF_SIZE       32768
 
 static imc_agent_t *imc_attestation;
 
@@ -51,16 +57,6 @@ static imc_agent_t *imc_attestation;
  * Supported PTS measurement algorithms
  */
 static pts_meas_algorithms_t supported_algorithms = 0;
-
-/**
- * Selected PTS measurement algorithm after attribute exchange
- */
-static pts_meas_algorithms_t selected_algorithm = PTS_MEAS_ALGO_SHA256;
-
-/**
- * List of files and directories to measure
- */
-static linked_list_t *files, *directories;
  
 /**
  * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
@@ -75,13 +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 ||
-               !tcg_pts_probe_meas_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");
@@ -122,207 +124,52 @@ TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
        }
 }
 
-/**
- * Get the TPM Version Information
- */
-static TSS_RESULT get_tpm_version_info(BYTE *tpm_version_info)
-{
-       TSS_HCONTEXT    hContext;
-       TSS_HTPM        hTPM;
-       TSS_RESULT uiResult;
-       UINT32 uiResultLen;
-       /* TODO: Needed for parsing version info on IMV side */
-       //TPM_CAP_VERSION_INFO versionInfo;
-       //UINT64 offset = 0;
-
-       uiResult = Tspi_Context_Create(&hContext);
-       if (uiResult != TSS_SUCCESS) {
-               DBG1(DBG_IMC,"Error 0x%x on Tspi_Context_Create\n", uiResult);
-               return uiResult;
-       }
-       uiResult = Tspi_Context_Connect(hContext, NULL);
-       if (uiResult != TSS_SUCCESS) {
-               DBG1(DBG_IMC,"Error 0x%x on Tspi_Context_Connect\n", uiResult);
-               return uiResult;
-       }
-       uiResult = Tspi_Context_GetTpmObject (hContext, &hTPM);
-       if (uiResult != TSS_SUCCESS) {
-               DBG1(DBG_IMC,"Error 0x%x on Tspi_Context_GetTpmObject\n", uiResult);
-               return uiResult;
-       }
-
-       uiResult = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL,  0, NULL, &uiResultLen,
-                                         &tpm_version_info);
-       if (uiResult != TSS_SUCCESS) {
-               DBG1(DBG_IMC,"Error 0x%x on Tspi_TPM_GetCapability\n", uiResult);
-               return uiResult;
-       }
-}
 
 /**
- * Get Hash Measurement of a file
+ * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
  */
-static TNC_Result hash_file(char *path, unsigned char *out)
+TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
+                                                                 TNC_ConnectionID connection_id)
 {
-       BYTE buffer[IMC_ATTESTATION_BUF_SIZE];
-       FILE *file;
-       int bytes_read;
-       hasher_t *hasher;
-       hash_algorithm_t hash_alg;
-       
-       /* Create a hasher */
-       hash_alg = tcg_pts_meas_to_hash_algorithm(selected_algorithm);
-       hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
-       if (!hasher)
-       {
-               DBG1(DBG_IMC, "hasher %N not available", hash_algorithm_names, hash_alg);
-               return TNC_RESULT_FATAL;
-       }
+       imc_state_t *state;
+       imc_attestation_state_t *attestation_state;
+       pts_t *pts;
+       char *platform_info;
+       TNC_Result result = TNC_RESULT_SUCCESS;
 
-       file = fopen(path, "rb");
-       if (!file)
-       {
-               DBG1(DBG_IMC,"file '%s' can not be opened", path);
-               hasher->destroy(hasher);
-               return TNC_RESULT_FATAL;
-       }
-       while (TRUE)
+       if (!imc_attestation)
        {
-               bytes_read = fread(buffer, 1, sizeof(buffer), file);
-               if (bytes_read > 0)
-               {
-                       hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
-               }
-               else
-               {
-                       hasher->get_hash(hasher, chunk_empty, out);
-                       break;
-               }
+               DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
+               return TNC_RESULT_NOT_INITIALIZED;
        }
-       fclose(file);
-       hasher->destroy(hasher);
-
-       return TNC_RESULT_SUCCESS;
-}
-
-static TNC_Result send_message(TNC_ConnectionID connection_id)
-{
-       pa_tnc_msg_t *msg;
-       pa_tnc_attr_t *attr;
-       imc_state_t *state;
-       imc_attestation_state_t *attestation_state;
-       imc_attestation_handshake_state_t handshake_state;
-       TNC_Result result;
 
+       /* 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;
-       handshake_state = attestation_state->get_handshake_state(attestation_state);
-       
-       /* Switch on the attribute type IMC has received */
-       switch (handshake_state)
-       {
-               case IMC_ATTESTATION_STATE_REQ_PROTO_CAP:
-               {
-                       pts_proto_caps_flag_t flags;
-                       flags = PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_V;
-                       attr = tcg_pts_attr_proto_caps_create(flags, FALSE);
-                       break;
-               }
-               case IMC_ATTESTATION_STATE_REQ_MEAS_ALGO:
-               {
-                       pts_meas_algorithms_t algorithm;
-                       algorithm = PTS_MEAS_ALGO_SHA1;
-                       /* Save the selected algorithm for further attributes creation */
-                       selected_algorithm = algorithm;
-                       attr = tcg_pts_attr_meas_algo_create(algorithm, TRUE);
-                       break;
-               }
-               case IMC_ATTESTATION_STATE_GET_TPM_INFO:
-               {
-                       TSS_RESULT uiResult;
-                       BYTE *tpm_version_info;
-
-                       uiResult = get_tpm_version_info(tpm_version_info);
-                       if (uiResult != TSS_SUCCESS) {
-                               DBG1(DBG_IMC,"Error 0x%x on get_tpm_version_info\n", uiResult);
-                               return uiResult;
-                       }
+       pts = attestation_state->get_pts(attestation_state);
 
-                       attr = tcg_pts_attr_tpm_version_info_create(
-                               chunk_create((char *)tpm_version_info,
-                                            strlen(tpm_version_info)));
-                       break;
-               }
-               /* TODO: working on */
-               /*case IMC_ATTESTATION_STATE_REQ_FILE_MEAS:
-               {
-                       enumerator_t *enumerator;
-                       measurement_req_entry_t *entry;
-                       
-                       enumerator = enumerator_create_single(file_list, NULL);
-                       while (enumerator->enumerate(enumerator, &entry))
-                       {
-                               attr = tcg_pts_attr_req_file_meas_create(false, 
-                                       entry.request_id, delimiter, 
-                                       chunk_create(entry.path,strlen(entry.path)));
-                               attr->set_noskip_flag(attr, TRUE);
-                               msg->add_attribute(msg, attr);
-                       }
-                       
-                       enumerator = enumerator_create_single(file_list, NULL);
-                       while (enumerator->enumerate(enumerator, &entry))
-                       {
-                               attr = tcg_pts_attr_req_file_meas_create(false, 
-                                       entry.request_id, delimiter, 
-                                       chunk_create(entry.path,strlen(entry.path)));
-                               attr->set_noskip_flag(attr, TRUE);
-                               msg->add_attribute(msg, attr);
-                       }
-                       break;
-               }*/
-               case IMC_ATTESTATION_STATE_GET_AIK:
-               case IMC_ATTESTATION_STATE_REQ_FUNCT_COMP_EVID:
-               case IMC_ATTESTATION_STATE_GEN_ATTEST_EVID:
-               case IMC_ATTESTATION_STATE_REQ_FILE_METADATA:
-               case IMC_ATTESTATION_STATE_REQ_IML:
-               case IMC_ATTESTATION_STATE_INIT:
-                       DBG1(DBG_IMC, "Attestation IMC has nothing to send: \"%s\"", handshake_state);
-                       return TNC_RESULT_FATAL;
-               default:
-                       DBG1(DBG_IMC, "Attestation IMC is in unknown state: \"%s\"", handshake_state);
-                       return TNC_RESULT_FATAL;
+       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);
        }
-       
-       
-       attr->set_noskip_flag(attr, TRUE);
-       msg = pa_tnc_msg_create();
-       msg->add_attribute(msg, attr);
-       msg->build(msg);
-       result = imc_attestation->send_message(imc_attestation, connection_id,
-                                                                       msg->get_encoding(msg));        
-       msg->destroy(msg);
 
        return result;
 }
 
 /**
- * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
- */
-TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
-                                                                 TNC_ConnectionID connection_id)
-{
-       if (!imc_attestation)
-       {
-               DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
-               return TNC_RESULT_NOT_INITIALIZED;
-       }
-       return send_message(connection_id);
-}
-
-/**
  * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
  */
 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
@@ -333,7 +180,11 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
 {
        pa_tnc_msg_t *pa_tnc_msg;
        pa_tnc_attr_t *attr;
+       linked_list_t *attr_list;
+       imc_state_t *state;
+       imc_attestation_state_t *attestation_state;
        enumerator_t *enumerator;
+       pts_t *pts;
        TNC_Result result;
        bool fatal_error = FALSE;
 
@@ -343,7 +194,15 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                return TNC_RESULT_NOT_INITIALIZED;
        }
 
-       /* parse received PA-TNC message and automatically handle any errors */ 
+       /* 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);
+
+       /* 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);
@@ -353,6 +212,8 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
        {
                return result;
        }
+       
+       attr_list = linked_list_create();
 
        /* analyze PA-TNC attributes */
        enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
@@ -389,28 +250,160 @@ TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
                }
                else if (attr->get_vendor_id(attr) == PEN_TCG)
                {
-                       /**
-                        * Handle TCG PTS attributes
-                        */
-                       switch(attr->get_type(attr))
+                       switch (attr->get_type(attr))
                        {
                                case TCG_PTS_REQ_PROTO_CAPS:
+                               {
+                                       tcg_pts_attr_proto_caps_t *attr_cast;
+                                       pts_proto_caps_flag_t imc_flags, imv_flags;
+
+                                       attr_cast = (tcg_pts_attr_proto_caps_t*)attr;
+                                       imv_flags = attr_cast->get_flags(attr_cast);
+                                       imc_flags = pts->get_proto_caps(pts);
+                                       pts->set_proto_caps(pts, imc_flags & imv_flags);
+
+                                       /* Send PTS Protocol Capabilities attribute */
+                                       attr = tcg_pts_attr_proto_caps_create(imc_flags & imv_flags,
+                                                                                                                 FALSE);
+                                       attr_list->insert_last(attr_list, attr);
                                        break;
+                               }
                                case TCG_PTS_MEAS_ALGO:
+                               {
+                                       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);
+
+                                       if ((supported_algorithms & PTS_MEAS_ALGO_SHA384) &&
+                                               (selected_algorithm & PTS_MEAS_ALGO_SHA384))
+                                       {
+                                               pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA384);
+                                       }
+                                       else if ((supported_algorithms & 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))
+                                       {
+                                               pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA1);
+                                       }
+                                       else
+                                       {
+                                               attr = pts_hash_alg_error_create(supported_algorithms);
+                                               attr_list->insert_last(attr_list, attr);
+                                               break;
+                                       }
+
+                                       /* 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, attr_info;
+
+                                       if (!pts->get_tpm_version_info(pts, &tpm_version_info))
+                                       {
+                                               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 */
+                                       attr = tcg_pts_attr_tpm_version_info_create(tpm_version_info);
+                                       attr_list->insert_last(attr_list, attr);
                                        break;
+                               }
+       
                                case TCG_PTS_GET_AIK:
+                               {
+                                       certificate_t *aik;
+
+                                       aik = pts->get_aik(pts);
+                                       if (!aik)
+                                       {
+                                               DBG1(DBG_IMC, "no AIK certificate or public key available");
+                                               break;
+                                       }
+       
+                                       /* 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;
                                case TCG_PTS_GEN_ATTEST_EVID:
                                        break;
                                case TCG_PTS_REQ_FILE_MEAS:
+                               {
+                                       tcg_pts_attr_req_file_meas_t *attr_cast;
+                                       char *pathname;
+                                       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;
+                                       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);
+                                       
+                                       if (pts->is_path_valid(pts, pathname, &pts_error) && pts_error)
+                                       {
+                                               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 (!pts->is_path_valid(pts, pathname, &pts_error))
+                                       {
+                                               break;
+                                       }
+                                       
+                                       if (delimiter != SOLIDUS_UTF && delimiter != REVERSE_SOLIDUS_UTF)
+                                       {
+                                               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;
+                                       }
+
+                                       /* 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)
+                                       {
+                                               /* 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;
-                               
+                               }
                                /* TODO: Not implemented yet */
                                case TCG_PTS_DH_NONCE_PARAMS_REQ:
                                case TCG_PTS_DH_NONCE_FINISH:
@@ -427,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:
@@ -440,15 +433,32 @@ 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);
 
-       /* if no error occurred then always return the same response */
-       return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
+       result = TNC_RESULT_SUCCESS;
+       
+       if (attr_list->get_count(attr_list))
+       {
+               pa_tnc_msg = pa_tnc_msg_create();
+
+               enumerator = attr_list->create_enumerator(attr_list);
+               while (enumerator->enumerate(enumerator, &attr))
+               {
+                       pa_tnc_msg->add_attribute(pa_tnc_msg, attr);
+               }
+               enumerator->destroy(enumerator);
+
+               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);
+       }
+       attr_list->destroy(attr_list);
+
+       return result;
 }
 
 /**
@@ -475,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;