ItsPkiItss_TestCases.ttcn 452 KB
Newer Older
/**
 *  @Author   ETSI / STF545 / TTF T025
 *  @version  $Url$
 *            $Id$
 *  @desc     Testcases  file for PKI/ITS-S Protocol
 *  @reference   ETSI TS ITS-00546v006
 *  @copyright   ETSI Copyright Notification
 *               No part may be reproduced except as authorized by written permission.
 *               The copyright and the foregoing restriction extend to reproduction in all media.
 *               All rights reserved.
 */
module ItsPkiItss_TestCases {

  // Libcommon
  import from LibCommon_Time all;
  import from LibCommon_VerdictControl all;
  import from LibCommon_Sync all;
  import from LibCommon_BasicTypesAndValues all;
  import from LibCommon_DataStrings all;

  // LibIts
  import from Ieee1609Dot2BaseTypes language "ASN.1:1997" all;
  import from Ieee1609Dot2 language "ASN.1:1997" all;
  import from EtsiTs102941BaseTypes language "ASN.1:1997" all;
  import from EtsiTs102941TypesEnrolment language "ASN.1:1997" all;
  import from EtsiTs102941TypesAuthorization language "ASN.1:1997" all;
  import from EtsiTs102941TypesAuthorizationValidation language "ASN.1:1997" all;
  import from EtsiTs102941MessagesCa language "ASN.1:1997" all;
  import from EtsiTs102941TrustLists language "ASN.1:1997" all;
  import from EtsiTs103097Module language "ASN.1:1997" all;
  import from Ieee1609Dot2Dot1AcaRaInterface language "ASN.1:1997" all;
  import from Ieee1609Dot2Dot1EeRaInterface language "ASN.1:1997" all;
  import from ETSI_ITS_CDD language "ASN.1:1997" all;
  import from CAM_PDU_Descriptions language "ASN.1:1997" all;

  // LibItsCommon
  import from LibItsCommon_TypesAndValues all;
  import from LibItsCommon_Functions all;
  import from LibItsCommon_TypesAndValues all;
  import from LibItsCommon_ASN1_NamedNumbers all;

  // LibItsGeoNetworking
  import from LibItsGeoNetworking_TypesAndValues all;
  import from LibItsGeoNetworking_Functions all;
  import from LibItsGeoNetworking_Templates all;
  import from LibItsGeoNetworking_Pics all;
  import from LibItsGeoNetworking_Pixits all;
  import from LibItsGeoNetworking_TestSystem all;

  // LibItsCam
  import from LibItsCam_TypesAndValues all;
  import from LibItsCam_Templates all;

  // LibItsSecurity
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_TestSystem all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Functions all;
  import from LibItsSecurity_Pixits all;
  import from LibItsSecurity_Pics all;

  // LibHttp
  import from LibHttp_TypesAndValues all;
  import from LibHttp_Templates all;
  import from LibHttp_Functions all;
  import from LibHttp_TestSystem all;
  import from LibHttp_Pics all;
  import from LibHttp_BinaryTemplates all;

  // LibHelpers
  import from LibHelpers_Functions all;

  // LibItsPki
  import from LibItsPki_TypesAndValues all;
  import from LibItsPki_Templates all;
  import from LibItsPki_Functions all;
  import from LibItsPki_TestSystem all;
  import from LibItsPki_Pics all;
  import from LibItsPki_Pixits all;
  import from LibItsPki_EncdecDeclarations all;

  // ItsPki
  import from ItsPki_Pixits all;

  /**
   * @desc 5.2  ITS-S behaviour
   */
  group itss_behavior {
    group itss_states {
      const charstring c_stInitial := "initial";
      const charstring c_stEnrolled := "enrolled";
      const charstring c_stAuthorized := "authorized";
    }


    group itss_helpers {

      /**
       * @desc Send an HTTP error message 500 Internal error.
       *       Note: To be refined
       */
      function f_send_500_Internal_Error(
                                         in Headers p_headers, 
                                         in template (omit) charstring p_error_message := omit
                                         ) runs on ItsPkiHttp {
        f_http_send(
                    p_headers,
                    m_http_response(
                                    m_http_response_500_internal_error(
                                                                       p_headers
                    )));
      } // End function f_send_500_Internal_Error

      /**
       * @desc The purpose of this function is verify the EC request and extract InnerEcRequest and build the InnerEcResponse for the HTTP response
       *       Note: This function accepts additional parameters to alter the reponse
       */
      function f_verify_http_ec_request_from_iut_itss(
                                                      in Request p_request,
                                                      in Headers p_headers,
                                                      out InnerEcRequest p_inner_ec_request,
                                                      out InnerEcResponse p_inner_ec_response,
                                                      out HttpMessage p_response,
                                                      out integer p_result,
                                                      in template (present) octetstring p_its_id := PICS_ITS_S_CANONICAL_ID,
                                                      in template (present) SignerIdentifier p_signer := m_signerIdentifier_self,
                                                      in EnrolmentResponseCode p_force_response_code := ok
                                                      ) runs on ItsPkiHttp {
        // Local variables
        var Ieee1609Dot2Data v_ieee1609dot2_signed_and_encrypted_data;
        var Ieee1609Dot2Data v_ieee1609dot2_signed_data;
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var Oct16 v_request_hash;
        var HashedId8 v_bfk_hashed_id8;
        var Oct16 v_aes_enc_key;
        var template (value) HttpMessage v_response;
        var EtsiTs103097Certificate v_ec_certificate;
        var HashedId8 v_ec_certificate_hashed_id8;
        var PublicVerificationKey v_canonical_key;

        log(">>> f_verify_http_ec_request_from_iut_itss: ", p_request);

        p_result := 0;

        if(false == f_get_canonical_itss_key(v_canonical_key)){
          log(">>> f_verify_http_ec_request_from_iut_itss: error getting canonical key");
          v_response := m_http_response(m_http_response_500_internal_error(p_headers));
          p_result := -1;
          return;
        }

        if(not(f_read_pki_request_message(
                                          p_request.body.binary_body.ieee1609dot2_data,
                                          vc_eaPrivateEncKey, vc_eaWholeHash/*salt*/,
                                          v_request_hash, v_aes_enc_key,
                                          v_ieee1609dot2_signed_data,
                                          v_etsi_ts_102941_data
                                          ))) {
          f_http_build_inner_ec_response(-, cantparse, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
          p_result := -1;
          goto L_Done;
        }

        log("f_verify_http_ec_request_from_iut_itss: match ", match(v_etsi_ts_102941_data.content, mw_enrolmentRequest(mw_innerEcRequestSignedForPop(mw_signedData(sha256, mw_toBeSignedData(-, mw_headerInfo_inner_pki_request), p_signer))))); // TODO In TITAN, this is the only way to get the unmatching in log
        if (false == match(v_etsi_ts_102941_data.content, mw_enrolmentRequest(mw_innerEcRequestSignedForPop(mw_signedData(sha256, mw_toBeSignedData(-, mw_headerInfo_inner_pki_request), p_signer))))) {
          // Send error message
          f_http_build_inner_ec_response(-, badcontenttype, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
          // Set verdict
          p_result := -2;
          goto L_Done;
        }
        // Verify signature of mw_innerEcRequestSignedForPop
        if (false == f_verify_inner_ec_request_signed_for_pop(v_etsi_ts_102941_data, p_inner_ec_request)) {
          // Send error message
          f_http_build_inner_ec_response(p_inner_ec_request/*Not required*/, cantparse, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
          v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));
          // Set verdict
          p_result := -3;
          goto L_Done;
        }

        log("f_verify_http_ec_request_from_iut_itss: matching: ", match(p_inner_ec_request, mw_innerEcRequest(p_its_id, -, mw_certificate_subject_attributes({mw_appPermissions(c_its_aid_SCR, ?)})))); // TODO In TITAN, this is the only way to get the unmatching in log
        if (false == match(p_inner_ec_request, mw_innerEcRequest(p_its_id, -, mw_certificate_subject_attributes_optional_assuranceLevel({mw_appPermissions(c_its_aid_SCR, ?)})))) {
          // Send error message: Not enrolmentrequest
          f_http_build_inner_ec_response(p_inner_ec_request, badcontenttype, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
          v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));
          // Set verdict
          p_result := -4;
          goto L_Done;
        }

        // TODO Check ValidityPeriod

        // Send OK message
        log("f_verify_http_ec_request_from_iut_itss: Receive ", p_inner_ec_request);
        if (p_force_response_code == ok) { 
          // Send EC certificate with code ok
          log("====================================== vc_ec_keys_counter= ", vc_ec_keys_counter);
          f_http_build_inner_ec_response(p_inner_ec_request, ok, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
          if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256)) {
            if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_0)) {
              vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_0;
              vc_ec_compressed_modes[vc_ec_keys_counter] := 0;
            } else {
              vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_1;
              vc_ec_compressed_modes[vc_ec_keys_counter] := 1;
            }
          } else if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1)) {                
            if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_0)) {
              vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_0;
              vc_ec_compressed_modes[vc_ec_keys_counter] := 0;
            } else {
              vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_1;
              vc_ec_compressed_modes[vc_ec_keys_counter] := 1;
            }
          } else {
            log("*** " & testcasename() & ": FAIL: Not implemented yet ***");
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          vc_ec_hashed_id8[vc_ec_keys_counter] := v_ec_certificate_hashed_id8;
          vc_ec_keys_counter := vc_ec_keys_counter + 1;
          vc_ec_certificates[vc_ec_counter] := v_ec_certificate;
          vc_ec_counter := vc_ec_counter + 1;
          log("====================================== vc_ec_keys_counter= ", vc_ec_keys_counter);
        } else {
              log("f_verify_http_ec_request_from_iut_itss: Succeed but force error code ", p_force_response_code);
          f_http_build_inner_ec_response(p_inner_ec_request, p_force_response_code, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
        }
        
        label L_Done;
        v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));

        // Set verdict
        p_result := 0;
        p_response := valueof(v_response);
        log("<<< f_verify_http_ec_request_from_iut_itss: p_response: ", p_response);
        log("<<< f_verify_http_ec_request_from_iut_itss: p_result: ", p_result);
      } // End of function f_verify_http_ec_request_from_iut_itss

      /**
       * @desc The purpose of this function is verify the AT request and extract InnerAtRequest and build the InnerAtResponse for the HTTP response
       *       Note: This function accepts additional parameters to alter the reponse
       */
      function f_verify_http_at_request_from_iut_itss(
                                                      in Request p_request,
                                                      in Headers p_headers,
                                                      in template (omit) EtsiTs103097Certificate p_ec_certificate,
                                                      out InnerAtRequest p_inner_at_request,
                                                      out InnerAtResponse p_inner_at_response,
                                                      out HttpMessage p_response,
                                                      out integer p_result,
                                                      in template octetstring p_its_id := PICS_ITS_S_CANONICAL_ID,
                                                      in AuthorizationResponseCode p_force_response_code := ok
                                                      ) runs on ItsPkiHttp {
        // Local variables
        var Ieee1609Dot2Data v_ieee1609dot2_data;
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var Oct16 v_request_hash;
        var Oct16 v_aes_enc_key;
        var template (value) HttpMessage v_response;
        var octetstring v_msg;

        log(">>> f_verify_http_at_request_from_iut_itss:", p_request);

        p_result := 0;

        // 1. Calculate the request Hash
        v_msg := bit2oct(encvalue(p_request.body.binary_body.ieee1609dot2_data));
        log(">>> f_verify_http_at_request_from_iut_itss: Encoded request: ", v_msg);
        v_request_hash := substr(f_hashWithSha256(v_msg), 0, 16);
        log(">>> f_verify_http_at_request_from_iut_itss: p_request_hash= ", v_request_hash);
 
        // 2. Decrypt the InnerAtRequest
        if (false == f_decrypt(vc_aaPrivateEncKey, // AA private encryption key
                               p_request.body.binary_body.ieee1609dot2_data , // data to be decrypted
                               vc_aaWholeHash, // salt
                               v_ieee1609dot2_data, // decrypted message
                               v_aes_enc_key)) {
          log("f_verify_http_at_request_from_iut_itss: Failed to decrypt message");
          // Send error message, unable to decypt it
          p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request"))); // Initialize v_reponse with an error message
          // Set verdict
          p_result := -1;
          return;
        }

        // check if signed
        var bitstring v_msg_bit;
        if(ispresent(v_ieee1609dot2_data.content.signedData)) {
          if(not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data)
            or not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData)) {
            log("f_verify_http_at_request_from_iut_itss: Invalid message payload");
            p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
            p_result := -1;
            return;
          }
          log("f_verify_http_at_request_from_iut_itss: v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData= ", v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);
          v_msg_bit := oct2bit(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);
        } else {
          if(not ispresent(v_ieee1609dot2_data.content.unsecuredData)) {
            log("f_verify_http_at_request_from_iut_itss: Invalid message payload");
            p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
            p_result := -1;
            return;
          }
          v_msg_bit := oct2bit(v_ieee1609dot2_data.content.unsecuredData);
        }
        if (decvalue(v_msg_bit, v_etsi_ts_102941_data) != 0) {
          log("f_verify_http_at_request_from_iut_itss: Can not decode v_etsi_ts_102941_data");
          p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
          p_result := -1;
          return;
        }

        if (match(v_etsi_ts_102941_data.content, mw_authorizationRequest(mw_innerAtRequest)) == false) {
          // Send error message
          f_http_build_authorization_response(-, its_aa_cantparse, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
          // Set verdict
          p_result := -2;
          return;
        }

            if (f_verify_inner_at_request_signed_for_pop(v_etsi_ts_102941_data, p_ec_certificate, p_inner_at_request) == false) {
              // Send error message
          f_http_build_authorization_response(p_inner_at_request, its_aa_cantparse, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
          p_response := valueof(m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers)));
              // Set verdict
              p_result := -3;
          return;
        }

        log("f_verify_http_at_request_from_iut_itss: match ", match(p_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature)));
              if (match(p_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature)) == false) { // TODO To be refined
          // Send error message: No authorization request
          f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
          p_response := valueof(m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers)));
                // Set verdict
                p_result := -4;
          return;
        }

        // Verify PoP signature of outer message
        if(ispresent(v_ieee1609dot2_data.content.signedData)){
          log("f_verify_http_at_request_from_iut_itss: v_ieee1609dot2_data.content.signedData.tbsData= ", v_ieee1609dot2_data.content.signedData.tbsData);
          v_msg := bit2oct(encvalue(v_ieee1609dot2_data.content.signedData.tbsData));
          log("f_verify_http_at_request_from_iut_itss: v_msg= ", v_msg);
          if (false == f_verifyEcdsa(v_msg, int2oct(0, 32), v_ieee1609dot2_data.content.signedData.signature_, p_inner_at_request.publicKeys.verificationKey)) {
            f_http_build_authorization_response(p_inner_at_request, invalidsignature, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
            p_response := valueof(m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers)));
            // Set verdict
            p_result := -6;
            return;
          }
        }
        
        var PublicVerificationKey v_verification_tag;
        var octetstring v_encoded_tag;
        var octetstring v_key_tag;

        // Build the tag
        v_encoded_tag := bit2oct(encvalue(p_inner_at_request.publicKeys.verificationKey));
        if (ispresent(p_inner_at_request.publicKeys.encryptionKey)) {
          v_encoded_tag := v_encoded_tag & bit2oct(encvalue(p_inner_at_request.publicKeys.encryptionKey));
        }
        // Verify HMAC-SHA256
        log("f_verify_http_at_request_from_iut_itss: v_encoded_tag= ", v_encoded_tag);
        v_key_tag := substr(
                            fx_hmac_sha256( // TODO Rename and use a wrapper function
                                            p_inner_at_request.hmacKey,
                                            v_encoded_tag
                                            ),
                            0,
                            16); // Leftmost 128 bits of the HMAC-SHA256 tag computed previously
        log("f_verify_http_at_request_from_iut_itss: v_key_tag: ", v_key_tag);
        log("f_verify_http_at_request_from_iut_itss: keyTag= ", p_inner_at_request.sharedAtRequest.keyTag);
        log("f_verify_http_at_request_from_iut_itss: matching: ", match(p_inner_at_request.sharedAtRequest.keyTag, v_key_tag));
        if (match(p_inner_at_request.sharedAtRequest.keyTag, v_key_tag) == false) {
          // Send error message: No enrolment request
          f_http_build_authorization_response(p_inner_at_request, its_aa_keysdontmatch, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
          p_response := valueof(m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers)));
          // Set verdict
          p_result := -5;
          return;
        }

        // Send OK message
        log("f_verify_http_at_request_from_iut_itss: Receive ", p_inner_at_request);
        if (p_force_response_code == ok) {
          f_http_build_authorization_response(p_inner_at_request, ok, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
        } else {
          log("f_verify_http_at_request_from_iut_itss: Succeed built force error code ", p_force_response_code);
          f_http_build_authorization_response(p_inner_at_request, p_force_response_code, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
        }
        p_response := valueof(m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers)));
        log("<<< f_verify_http_at_request_from_iut_itss: p_result: ", p_result);
        log("<<< f_verify_http_at_request_from_iut_itss: p_response: ", p_response);
      } // End of function f_verify_http_at_request_from_iut_itss

      /**
       * @desc The purpose of this function is verify the AT request and extract InnerAtRequest , verify ecSignature and build the InnerAtResponse for the HTTP response
       *       Note: This function accepts additional parameters to alter the response
       */
      function f_verify_http_at_request_from_iut_itss_for_ecSignature(
                                                      in Request p_request,
                                                      in Headers p_headers,
                                                      in EtsiTs103097Certificate p_ec_certificate,
                                                      out InnerAtRequest p_inner_at_request,
                                                      out InnerAtResponse p_inner_at_response,
                                                      out HttpMessage p_response,
                                                      out integer p_result,
                                                      out boolean p_contains_ecsignature,
                                                      in template octetstring p_its_id := PICS_ITS_S_CANONICAL_ID,
                                                      in AuthorizationResponseCode p_force_response_code := ok
                                                      ) runs on ItsPkiHttp {
        // Local variables
        var Ieee1609Dot2Data v_ieee1609dot2_data;
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var Ieee1609Dot2Data v_ieee1609dot2_signed_data; //new variable
        var HashedId8 v_ec_certificate_hashed_id8 //new variable
        // var EcHashedId8 vc_ec_hashed_id8; Already defined in LibItsPki_TestSystem
        var Oct16 v_request_hash;
        var HashedId8 v_bfk_hashed_id8;
        var Oct16 v_aes_enc_key;
        var template (value) HttpMessage v_response;
        var octetstring v_msg;


        log(">>> f_verify_http_at_request_from_iut_itss_for_ecSignature:", p_request);

        p_result := 0;

        // 1. Calculate the request Hash
        v_msg := bit2oct(encvalue(p_request.body.binary_body.ieee1609dot2_data));
        log(">>> f_verify_http_at_request_from_iut_itss_for_ecSignature: ", v_msg);
        v_request_hash := substr(f_hashWithSha256(v_msg), 0, 16);
        log(">>> f_verify_http_at_request_from_iut_itss_for_ecSignature: p_request_hash= ", v_request_hash);
 
        // 2. Decrypt the InnerAtRequest
        if (false == f_decrypt(vc_aaPrivateEncKey, // AA private encryption key
                               p_request.body.binary_body.ieee1609dot2_data , // data to be decrypted
                               vc_aaWholeHash, // salt
                               v_ieee1609dot2_data, // decrypted message
                               v_aes_enc_key)) {
          log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Failed to decrypt message");
          // Send error message, unable to decypt it
          p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request"))); // Initialize v_reponse with an error message
          // Set verdict
          p_result := -1;
          return;
        }

        // check if signed
        var bitstring v_msg_bit;
        if(ispresent(v_ieee1609dot2_data.content.signedData)){
          if(not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data)
            or not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData))
          {
            log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Invalid message payload");
            p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
            p_result := -1;
            return;
          }
          log("f_verify_http_at_request_from_iut_itss_for_ecSignature: v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData= ", v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);
          v_msg_bit := oct2bit(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);
        } else {
            log("f_verify_http_at_request_from_iut_itss_for_ecSignature: matching: ", match(v_etsi_ts_102941_data.content, mw_authorizationRequest(mw_innerAtRequest))); // to get the unmatching in log
          if (match(v_etsi_ts_102941_data.content, mw_authorizationRequest(mw_innerAtRequest)) == false) {
            // Send error message
          f_http_build_authorization_response(-, its_aa_cantparse, v_request_hash, vc_aaPrivateKey, vc_aaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
            // Set verdict
            p_result := -2;
          } else {
            // Extract InnerAtRequest and Verify signature of mw_innerATRequestSigned
            var InnerAtRequest v_inner_at_request := v_etsi_ts_102941_data.content.authorizationRequest;
            log("f_verify_http_at_request_from_iut_itss_for_ecSignature: match ", match(v_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature))); // to get the unmatching in log
                if (match(v_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature)) == false) { // TODO To be refined
                  // Send error message: No authorization request
                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                  // Set verdict
                  p_result := -3;
                  } else {

                    // Check ecSignature
                    if (ispresent(v_inner_at_request.ecSignature.ecSignature) == false) {
                        // Send error message: No signed external payload present
                        f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                        v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                        // Set verdict
                        p_result := -4;
                        // Verify ecSignature of mw_ec_signature (encrypted)
                    } else {
                        p_contains_ecsignature  := true;
                        v_ieee1609dot2_signed_data := v_inner_at_request.ecSignature.ecSignature;  
                        
                        log("f_verify_http_at_request_from_iut_itss_for_ecSignature: match ", match(v_inner_at_request.ecSignature,mw_ec_signature_ext_payload))                  
                        if (match(v_inner_at_request.ecSignature, mw_ec_signature_ext_payload) == false) {
                            // Send error message: Present signed external payload doesnot match mw_ec_signature_ext_payload template
                            f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                            v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                            // Set verdict
                            p_result := -5;
                        } else {
                            // Check signer in ecSignature
                            if (isvalue(v_ieee1609dot2_signed_data.content.signedData)) {
                                // Verify digest of signerIdentifier_digest
                                if (match(v_ieee1609dot2_signed_data.content.signedData.signer,mw_signerIdentifier_digest) == false) {
                                    var HashedId8 v_digest := v_ieee1609dot2_signed_data.content.signedData.signer.digest; // check if it is hashId8
                                    //log("f_verify_http_at_request_from_iut_itss_for_ecSignature: match ", match(p_ec_certificate.issuer, v_digest));
                                    var bitstring v_enc_value;
                                    var octetstring v_ec_hash;
                                    var Oct8 v_ec_hashed_id8;
                                    v_enc_value := encvalue(p_ec_certificate);
                                    if (ischosen(p_ec_certificate.issuer.sha256AndDigest)) {
                                      v_ec_hash := f_hashWithSha256(bit2oct(v_enc_value));
                                      v_ec_hashed_id8 := f_hashedId8FromSha256(v_ec_hash);
                                    } else {
                                      v_ec_hash := f_hashWithSha384(bit2oct(v_enc_value));
                                      v_ec_hashed_id8 := f_hashedId8FromSha384(v_ec_hash);
                                    }

                                    if (match(v_ec_hashed_id8, v_digest) == false) {
                                        // Send error message: Signer not contains HashedId8 of the CERT_EC certificate
                                        f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                        v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                        // Set verdict
                                        p_result := -6;
                                    } else {
                                        // Check _signature in ecSignature
                                        if (isvalue(v_ieee1609dot2_signed_data.content.signedData.signature_)) {
                                            //
                                            var EtsiTs103097Certificate v_certificate;
                                            var EtsiTs102941Data v_etsi_ts_102941_ext_payload_data;
                                            var octetstring v_encoded_tbsData
                                            var Oct32            v_issuer;
                                            v_certificate := v_ieee1609dot2_signed_data.content.signedData.signer.certificate[0];
                                            v_encoded_tbsData := bit2oct(encvalue(v_ieee1609dot2_signed_data.content.signedData.tbsData));
                                            var octetstring v_enc := bit2oct(encvalue(v_certificate));
                                            v_issuer := f_hashWithSha256(v_enc);
                                            if (f_verifyEcdsa(v_encoded_tbsData,v_issuer,v_ieee1609dot2_signed_data.content.signedData.signature_,v_certificate.toBeSigned.verifyKeyIndicator.verificationKey) == false) {
                                                // Send error message: Unable to verfy signature using EC's verification public key
                                                f_http_build_authorization_response(p_inner_at_request, invalidsignature, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                                v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                                // Set verdict
                                                p_result := -7;
                                            } else {
                                                // Send OK message
                                                log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Receive ", p_inner_at_request);
                                                if (p_force_response_code == ok) {
                                                    f_http_build_authorization_response(p_inner_at_request, ok, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                                } else {
                                                log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Succeed built force error code ", p_force_response_code);
                                                    f_http_build_authorization_response(p_inner_at_request, p_force_response_code, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                                }
                                                v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                            }
                                        } 
                                    }
                                }
                            }
                        }
                    }
                    // Check 
                  }
          }
        }
      }

      /**
       * @desc The purpose of this function is verify the AT request and extract InnerAtRequest , verify encrypted ecSignature and build the InnerAtResponse for the HTTP response
       *       Note: This function accepts additional parameters to alter the response
       */
      function f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature(
                                                      in Request p_request,
                                                      in Headers p_headers,
                                                      in EtsiTs103097Certificate p_ec_certificate,
                                                      out InnerAtRequest p_inner_at_request,
                                                      out InnerAtResponse p_inner_at_response,
                                                      out HttpMessage p_response,
                                                      out integer p_result,
                                                      in template octetstring p_its_id := PICS_ITS_S_CANONICAL_ID,
                                                      in AuthorizationResponseCode p_force_response_code := ok
                                                      ) runs on ItsPkiHttp {
        // Local variables
        var Ieee1609Dot2Data v_ieee1609dot2_data;
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var EtsiTs103097Data  p_decryptedSecuredMessage;
        var EncryptedData   v_encrypted_data;
        var EtsiTs103097Data v_etsi_ts_103097_data;
        var Oct16 v_request_hash;
        var HashedId8 v_bfk_hashed_id8;
        var Oct16 v_aes_enc_key;
        var template (value) HttpMessage v_response;
        var octetstring v_msg;

        log(">>> f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature:", p_request);

        p_result := 0;

        // 1. Calculate the request Hash
        v_msg := bit2oct(encvalue(p_request.body.binary_body.ieee1609dot2_data));
        log(">>> f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: Encoded request: ", v_msg);
        v_request_hash := substr(f_hashWithSha256(v_msg), 0, 16);
        log(">>> f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: p_request_hash= ", v_request_hash);
 
        // 2. Decrypt the InnerAtRequest
        if (false == f_decrypt(vc_aaPrivateEncKey, // AA private encryption key
                               p_request.body.binary_body.ieee1609dot2_data , // data to be decrypted
                               vc_aaWholeHash, // salt
                               v_ieee1609dot2_data, // decrypted message
                               v_aes_enc_key)) {
          log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: Failed to decrypt message");
          // Send error message, unable to decypt it
          p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request"))); // Initialize v_reponse with an error message
          // Set verdict
          p_result := -1;
          return;
        }

        // check if signed
        var bitstring v_msg_bit;
        if(ispresent(v_ieee1609dot2_data.content.signedData)){
          if(not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data)
            or not ispresent(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData))
          {
            log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: Invalid message payload");
            p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
            p_result := -2;
            return;
          }
          log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData= ", v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);
          v_msg_bit := oct2bit(v_ieee1609dot2_data.content.signedData.tbsData.payload.data.content.unsecuredData);

          if (decvalue(v_msg_bit, v_etsi_ts_102941_data) != 0) {
            log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: Can not decode v_etsi_ts_102941_data");
            p_response := valueof(m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")));
            p_result := -3;
            return;
          }
        } else {
          log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: matching: ", match(v_etsi_ts_102941_data.content, mw_authorizationRequest(mw_innerAtRequest))); // to get the unmatching in log
          if (match(v_etsi_ts_102941_data.content, mw_authorizationRequest(mw_innerAtRequest)) == false) {
            // Send error message
            f_http_build_authorization_response(-, its_aa_cantparse, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
            // Set verdict
            p_result := -4;
            return;
          } else {
            // Extract InnerAtRequest and Verify signature of mw_innerATRequest
            var InnerAtRequest v_inner_at_request := v_etsi_ts_102941_data.content.authorizationRequest;
            log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: match ", match(v_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature))); // to get the unmatching in log
                if (match(v_inner_at_request, mw_innerAtRequest(mw_publicKeys, -, mw_shared_at_request, mw_ec_signature)) == false) { // TODO To be refined
                  // Send error message: No authorization request
                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                  // Set verdict
                  p_result := -5;
                  return;
                } else {
                    // Check ecSignature
                    var EcSignature v_ecsignature := v_inner_at_request.ecSignature;
                    if (ischosen(v_ecsignature.encryptedEcSignature) == false) {
                        // Send error message: No encrypted ecSignature present
                        f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                        v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                        // Set verdict
                        p_result := -6;
                        return;
                        // Verify ecSignature of mw_ec_signature (encrypted)
                    } else {
                        //v_encrypted_data := v_inner_at_request.ecSignature.encryptedEcSignature; 
                        v_encrypted_data := v_ecsignature.encryptedEcSignature.content.encryptedData; 
                        //var RecipientInfo v_recipientInfo := v_encrypted_data.content.encryptedData.recipients[0];
                        var RecipientInfo v_recipientInfo := v_encrypted_data.recipients[0];
                        var PKRecipientInfo v_pKRecipientInfo := v_recipientInfo.certRecipInfo;
                        log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: match ", match(v_ecsignature,mw_ec_signature))               
                        if (match(v_ecsignature, mw_ec_signature) == false) {
                            // Send error message: Present encrypted ecSignature doesnot matches with mw_ec_signature template
                            f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                            v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                            // Set verdict
                            p_result := -7;
                            return;
                        } else {
                            // Check in encrypted ecSignature
                            if (isvalue(v_ecsignature.encryptedEcSignature.content.encryptedData)) {
                                var octetstring v_decryptedSecuredMessage;
                                // Verify recipient 
                                if (not isvalue(v_encrypted_data.recipients[0]) and sizeof(v_encrypted_data.recipients) > 1) { // RecipientInfo should not contains certRecipInfo > 1
                                  // Send error message: recipients containing more than one element of type RecipientInfo
                                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                  // Set verdict
                                  p_result := -8;
                                  return;
                                } else {
                                  // RecipientInfo should contains certRecipInfo
                                  if (isvalue(v_recipientInfo.certRecipInfo)) {
                                    // verify that recipientId should be hashedId8 of EA_CERT
                                    log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: match ", match(vc_eaHashedId8, v_pKRecipientInfo.recipientId));
                                    if (match(vc_eaHashedId8, v_pKRecipientInfo.recipientId) == false) {
                                      // Send error message: recipientId not contains the HashedId8 of the CERT_EA
                                      f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                      v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                      // Set verdict
                                      p_result := -8;
                                      return;
                                    } else {
                                        // Further verify the encKey of supported type  
                                        if (isvalue(v_pKRecipientInfo.encKey)) {
                                          if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256)) {
                                              var SymmetricCiphertext v_ciphertext := v_encrypted_data.ciphertext;
                                              var Oct16 p_aes_sym_enc_key;
                                              //var octetstring v_decryptedSecuredMessage;
                                              log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: v_ciphertext=", v_ciphertext);
                                                if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_0)) {
                                                    v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                                                                                                                      v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                                                      vc_eaPrivateEncKey,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_0,
                                                                                                                      0,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.c,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.t,
                                                                                                                      v_ciphertext.aes128ccm.nonce,
                                                                                                                      vc_eaWholeHash,
                                                                                                                      p_aes_sym_enc_key
                                                                                                                      );
                                                } else if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_1)) {
                                                    v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                                                                                                                      v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                                                      vc_eaPrivateEncKey,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_1,
                                                                                                                      1,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.c,
                                                                                                                      v_pKRecipientInfo.encKey.eciesNistP256.t,
                                                                                                                      v_ciphertext.aes128ccm.nonce,
                                                                                                                      vc_eaWholeHash,
                                                                                                                      p_aes_sym_enc_key
                                                                                                                      );
                                                } else {
                                                  // send error msg : Non canonical ephemeral encryption keys
                                                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                                  // Set verdict
                                                  p_result := -7;
                                                  return;
                                                }

                                          } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1)) {
                                                var SymmetricCiphertext v_ciphertext := v_encrypted_data.ciphertext;
                                                //var octetstring  v_decryptedSecuredMessage;
                                                var Oct16 p_aes_sym_enc_key;
                                                if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_0)) {
                                                  v_decryptedSecuredMessage := f_decryptWithEciesBrainpoolp256r1WithSha256(
                                                                                                                        v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                                                        vc_eaPrivateEncKey,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_0,
                                                                                                                        0,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.c,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.t,
                                                                                                                        v_ciphertext.aes128ccm.nonce,
                                                                                                                        vc_eaWholeHash,                                                                                                                                         p_aes_sym_enc_key
                                                                                                                    );
                                                } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_1)) {
                                                  v_decryptedSecuredMessage := f_decryptWithEciesBrainpoolp256r1WithSha256(
                                                                                                                        v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                                                        vc_eaPrivateEncKey,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_1,
                                                                                                                        1,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.c,
                                                                                                                        v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.t,
                                                                                                                        v_ciphertext.aes128ccm.nonce,
                                                                                                                        vc_eaWholeHash,
                                                                                                                        p_aes_sym_enc_key
                                                                                                                    );
                                                } else {
                                                  // send error msg : Non canonical ephemeral encryption keys
                                                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                                  // Set verdict
                                                  p_result := -7;
                                                  return;
                                                }
                                          } else {
                                            // send error message : encKey does not support any type 
                                            f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                            v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                            // Set verdict
                                            p_result := -7;
                                            return;
                                          }
                                        }
                                    }
                                  }
                                }

                                // verify containing cyphertext
                                if (ispresent(v_encrypted_data.ciphertext) == false){
                                  // Send error message: ciphertext doesn't exist
                                  f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                  v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                  // Set verdict
                                  p_result := -8;
                                  return;
                                } else {
                                    //v_ieee1609dot2_signed_data :=  v_encrypted_data.ciphertext;
                                    if (isbound(v_decryptedSecuredMessage)) {
                                      var bitstring v_decode := oct2bit(v_decryptedSecuredMessage);
                                      if (decvalue(v_decode, p_decryptedSecuredMessage) != 0) {
                                        log("*** " & testcasename() & ":ERROR: Faild to decode secured message ***");
                                        f_http_build_authorization_response(p_inner_at_request, its_aa_badcontenttype, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                        v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                        // Set verdict
                                        p_result := -7;
                                        return;
                                      } else {
                                        log("f_verify_http_at_request_from_iut_itss_for_encrypted_ecSignature: match ", match(p_decryptedSecuredMessage,mw_etsiTs103097Data_signed));
                                        //check if encrypted data is of type  
                                        if(match(p_decryptedSecuredMessage,mw_etsiTs103097Data_signed)){
                                          // Send OK message
                                          log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Receive ", p_inner_at_request);
                                          if (p_force_response_code == ok) {
                                              f_http_build_authorization_response(p_inner_at_request, ok, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                          } else {
                                          log("f_verify_http_at_request_from_iut_itss_for_ecSignature: Succeed built force error code ", p_force_response_code);
                                              f_http_build_authorization_response(p_inner_at_request, p_force_response_code, v_request_hash, -, -, v_aes_enc_key, p_inner_at_response, v_ieee1609dot2_data);
                                          }
                                          v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_data)), p_headers));
                                        }
                                      }
                                    }
                                }
                            }
                        }
                    }
                  }
          }
        }
      }

      function f_verify_http_bfk_authorization_request(
                                                       in Request p_request,
                                                       in Headers p_headers,
                                                       in EtsiTs103097Certificate p_ec_certificate,
                                                       out EeRaCertRequest p_bfk_authorization_request,
                                                       out RaEeCertInfo p_ra_ee_cert_info,
                                                       out HttpMessage p_response,
                                                       out integer p_result,
                                                       in template octetstring p_its_id := PICS_ITS_S_CANONICAL_ID,
                                                       in template SignerIdentifier p_signer := m_signerIdentifier_self,
                                                       in EnrolmentResponseCode p_force_response_code := ok
                                                       ) runs on ItsPkiHttp {
        // Local variables
        var Ieee1609Dot2Data v_ieee1609dot2_signed_and_encrypted_data;
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var Oct16 v_request_hash;
        var HashedId8 v_bfk_request_hash;
        var Oct16 v_aes_enc_key;
        var template (value) HttpMessage v_response;

        log(">>> f_verify_http_bfk_authorization_request: ", p_request);

        p_result := 0;

        if (f_verify_pki_request_message_with_certificate(vc_eaPrivateEncKey, vc_eaWholeHash/*salt*/, p_ec_certificate, p_request.body.binary_body.ieee1609dot2_data, true, v_request_hash, v_bfk_request_hash, v_etsi_ts_102941_data, v_aes_enc_key) == false) { // Cannot decrypt the message
          // Send error message
          v_response := m_http_response(m_http_response_ko_no_body(p_headers, 400, "Bad request")); // Initialize v_reponse with an error message
          // Set verdict
          p_result := -1;
        } else { // TODO Add checks on date, 
          log("f_verify_http_bfk_authorization_request: match ", match(v_etsi_ts_102941_data.content, mw_butterflyAuthorizationRequest(mw_ee_ra_cert_request))); // TODO In TITAN, this is the only way to get the unmatching in log
          if (match(v_etsi_ts_102941_data.content, mw_butterflyAuthorizationRequest(mw_ee_ra_cert_request)) == false) {
            // Send error message
            f_http_build_butterfly_authorization_response_message(p_bfk_authorization_request/*Not required*/, v_bfk_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, p_ra_ee_cert_info, v_ieee1609dot2_signed_and_encrypted_data);
            // Set verdict
            p_result := -2;
          } else {
            // TODO Add checks
            p_bfk_authorization_request := v_etsi_ts_102941_data.content.butterflyAuthorizationRequest;
            f_http_build_butterfly_authorization_response_message(p_bfk_authorization_request, v_bfk_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, p_ra_ee_cert_info, v_ieee1609dot2_signed_and_encrypted_data);
            //       vc_ec_hashed_id8[vc_ec_keys_counter] := v_ec_certificate_hashed_id8;
            //       vc_ec_keys_counter := vc_ec_keys_counter + 1;
            //       vc_ec_certificates[vc_ec_counter] := v_ec_certificate;
            //       vc_ec_counter := vc_ec_counter + 1;
            //       log("====================================== vc_ec_keys_counter= ", vc_ec_keys_counter);

            //       
            // Verify signature of mw_innerEcRequestSignedForPop
            // if (f_verify_inner_ec_request_signed_for_pop(v_etsi_ts_102941_data, p_inner_ec_request) == false) {
            //   // Send error message
            //   f_http_build_inner_ec_response(p_inner_ec_request/*Not required*/, cantparse, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
            //   v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));
            //   // Set verdict
            //   p_result := -3;
            // } else {
            //   log("f_verify_http_bfk_authorization_request: matching: ", match(p_inner_ec_request, mw_innerEcRequest(p_its_id, -, mw_certificate_subject_attributes({mw_appPermissions(c_its_aid_SCR, ?)})))); // TODO In TITAN, this is the only way to get the unmatching in log
            //   if (match(p_inner_ec_request, mw_innerEcRequest(p_its_id, -, mw_certificate_subject_attributes_optional_assuranceLevel({mw_appPermissions(c_its_aid_SCR, ?)}))) == false) {
            //     // Send error message: Not enrolmentrequest
            //     f_http_build_inner_ec_response(p_inner_ec_request, badcontenttype, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
            //     v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));
            //     // Set verdict
            //     p_result := -4;
            //   } else {
            //     // TODO Check ValidityPeriod
            //     // Send OK message
            //     log("f_verify_http_bfk_authorization_request: Receive ", p_inner_ec_request);
            //     if (p_force_response_code == ok) { 
            //       // Send EC certificate with code ok
            //       log("====================================== vc_ec_keys_counter= ", vc_ec_keys_counter);
            //       f_http_build_inner_ec_response(p_inner_ec_request, ok, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
            //       if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256)) {
            //         if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_0)) {
            //           vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_0;
            //           vc_ec_compressed_modes[vc_ec_keys_counter] := 0;
            //         } else {
            //           vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256.compressed_y_1;
            //           vc_ec_compressed_modes[vc_ec_keys_counter] := 1;
            //         }
            //       } else if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1)) {                
            //         if (ispresent(p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_0)) {
            //           vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_0;
            //           vc_ec_compressed_modes[vc_ec_keys_counter] := 0;
            //         } else {
            //           vc_ec_public_compressed_key[vc_ec_keys_counter] := p_inner_ec_request.publicKeys.verificationKey.ecdsaBrainpoolP256r1.compressed_y_1;
            //           vc_ec_compressed_modes[vc_ec_keys_counter] := 1;
            //         }
            //       } else {
            //         log("*** " & testcasename() & ": FAIL: Not implemented yet ***");
            //         f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            //       }
            //       vc_ec_hashed_id8[vc_ec_keys_counter] := v_ec_certificate_hashed_id8;
            //       vc_ec_keys_counter := vc_ec_keys_counter + 1;
            //       vc_ec_certificates[vc_ec_counter] := v_ec_certificate;
            //       vc_ec_counter := vc_ec_counter + 1;
            //       log("====================================== vc_ec_keys_counter= ", vc_ec_keys_counter);
            //     } else {
            //       log("f_verify_http_bfk_authorization_request: Succeed but force error code ", p_force_response_code);
            //       f_http_build_inner_ec_response(p_inner_ec_request, p_force_response_code, v_request_hash, vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key, v_ec_certificate, v_ec_certificate_hashed_id8, p_inner_ec_response, v_ieee1609dot2_signed_and_encrypted_data);
            //     }
                v_response := m_http_response(m_http_response_ok(m_http_message_body_binary(m_binary_body_ieee1609dot2_data(v_ieee1609dot2_signed_and_encrypted_data)), p_headers));
          }
        }

        p_response := valueof(v_response);
        log("<<< f_verify_http_bfk_authorization_request: p_response: ", p_response);
        log("<<< f_verify_http_bfk_authorization_request: p_result: ", p_result);
      } // End of function f_verify_http_bfk_authorization_request

      /**
       * @desc Await ITS CA message using the default AT certificate
       */
      altstep a_await_cam_with_current_cert(
                                            in EtsiTs103097Certificate p_certificate
                                            ) runs on ItsPkiItss {
        [PICS_SEC_SHA256 == true] geoNetworkingPort.receive(
                                                            mw_geoNwInd(
                                                                        mw_geoNwSecPdu(
                                                                                       mw_etsiTs103097Data_signed(
                                                                                                                  mw_signedData(
                                                                                                                                sha256,
                                                                                                                                mw_toBeSignedData(
                                                                                                                                                  mw_signedDataPayload,
                                                                                                                                                  mw_headerInfo_cam
                                                                                                                                                  ),
                                                                                                                                mw_signerIdentifier_certificate(
                                                                                                                                                                mw_etsiTs103097Certificate(
                                                                                                                                                                                           mw_issuerIdentifier_sha256AndDigest(
                                                                                                                                                                                                                               p_certificate.issuer.sha256AndDigest
                                                                                                                                                                                                                               ),
                                                                                                                                                                                           mw_toBeSignedCertificate_at(
                                                                                                                                                                                                                       -,
                                                                                                                                                                                                                       p_certificate.toBeSigned.verifyKeyIndicator
                                                                                                                                                                                                                       )
                                                                                                                                                                                           )
                                                                                                                                                                )
                                                                                                                                )
                                                                                                                  ), 
                                                                                       mw_geoNwShbPacket
                                                                                       ))) {
        }
        [PICS_SEC_SHA384 == true] geoNetworkingPort.receive(
                                                            mw_geoNwInd(
                                                                        mw_geoNwSecPdu(
                                                                                       mw_etsiTs103097Data_signed(
                                                                                                                  mw_signedData(
                                                                                                                                sha384, 
                                                                                                                                mw_toBeSignedData(
                                                                                                                                                  mw_signedDataPayload,
                                                                                                                                                  mw_headerInfo_cam
                                                                                                                                                  ),
                                                                                                                                mw_signerIdentifier_certificate(
                                                                                                                                                                mw_etsiTs103097Certificate(
                                                                                                                                                                                           mw_issuerIdentifier_sha384AndDigest(
                                                                                                                                                                                                                               p_certificate.issuer.sha384AndDigest
                                                                                                                                                                                                                               ),
                                                                                                                                                                                           mw_toBeSignedCertificate_at(
                                                                                                                                                                                                                       -,
                                                                                                                                                                                                                       p_certificate.toBeSigned.verifyKeyIndicator
                                                                                                                                                                                                                       )
                                                                                                                                                                                           )
                                                                                                                                                                )
                                                                                                                                )
                                                                                                                  ),
                                                                                       mw_geoNwShbPacket
                                                                                       ))) {
        }
      } // End of altstep a_await_cam_with_current_cert

    } // End of group itss_helpers

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.2.1  Enrollment request
    group itss_enrolment_request {

      /**
       * @desc Check that IUT sends an enrolment request when triggered.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Initial conditions: 
       *     with {
       *         the IUT being in the "initial state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Enrolment Certificate (EC)
       *         }
       *         then {
       *             the IUT sends to EA an EnrolmentRequestMessage
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_01_BV
       * @reference ETSI TS 102 941 [2], clause 6.1.3
       */
      testcase TC_SECPKI_ITSS_ENR_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_01_BV

      group f_TC_SECPKI_ITSS_ENR_01_BV {

        function f_TC_SECPKI_ITSS_ENR_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;

          // Test component configuration
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "itss: FAIL: Unexpected GeoNet message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "itss: INFO: No GeoNet message received. Continue ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
          } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerEnrolmentRequestPrimitive();
          tc_ac.start; // TDOD To refined, use altstep
          alt {
            [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
              tc_ac.stop;
              log("*** " & testcasename() & "itss: INFO: IUT is in enrolment state ***");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "itss: DBG: IUT state update not recieved. Assuming it was OK... ***");
              //f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "itss: FAIL: Unexpected GeoNet message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "itss: PASS: Enrolment trigger sent succesfully ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_01_BV_itss

        function f_TC_SECPKI_ITSS_ENR_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              var Oct16 v_request_hash;
              var Oct16 v_aes_enc_key;
              var Ieee1609Dot2Data v_outer_encrypted_message;
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest v_inner_ec_request;
              var InnerEcResponse v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;

              tc_ac.stop;

              if (not(f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request))) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": INCONC: Canonical key is not set properly ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout); // to emulate inconc
              } else {
                f_send_500_Internal_Error(v_headers); // we dont care about response
                // Set verdict
                log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_01_BV

      /**
       * @desc If the enrolment request of the IUT is an initial enrolment request, the itsId 
       *       (contained in the InnerECRequest) shall be set to the canonical identifier, the 
       *       signer (contained in the outer EtsiTs1030971Data-Signed) shall be set to self and 
       *       the outer signature shall be computed using the canonical private key.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                          containing InnerECRequestSignedForPOP
       *                             containing InnerEcRequest
       *                                 containing itsId
       *                                     indicating the canonical identifier of the ITS-S 
       *                 and containing signer
       *                     declared as self
       *                 and containing signature 
       *                     computed using the canonical private key
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_02_BV
       * @reference ETSI TS 102 941, clause 6.1.3
       */
      testcase TC_SECPKI_ITSS_ENR_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_02_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_02_BV

      group f_TC_SECPKI_ITSS_ENR_02_BV {
        function f_TC_SECPKI_ITSS_ENR_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request)
            {
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest   v_inner_ec_request;
              var InnerEcResponse  v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var Oct16 v_request_hash, v_aes_enc_key;
              var PublicVerificationKey v_canonical_key;
              tc_ac.stop;

              if( not f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request))
              {
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_send_500_Internal_Error(v_headers);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if(not f_http_build_error_ec_response(unknownits, v_request_hash, vc_eaPrivateKey,
                                                    vc_eaWholeHash, v_aes_enc_key, v_response_message))
              {
                log("*** " & testcasename() & ": INCOMC: Can't generate enrolment response***");
                f_send_500_Internal_Error(v_headers);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              }
              f_http_send( v_headers, m_http_response( m_http_response_ok( m_http_message_body_binary( m_binary_body_ieee1609dot2_data( v_response_message)),
                                                                           v_headers)));
              if( not isvalue(v_inner_ec_request) ){
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if(not match(v_inner_ec_request.itsId, LibItsPki_Pics.PICS_ITS_S_CANONICAL_ID)){
                log("*** " & testcasename() & ": FAIL: Canonical ID mismatched ***");
                log("*** " & testcasename() & ": FAIL:     ", match(v_inner_ec_request.itsId, LibItsPki_Pics.PICS_ITS_S_CANONICAL_ID));
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              if (not ischosen(v_decrypted_message.content.signedData)) {
                log("*** " & testcasename() & ": FAIL: EC request shall contain signed message ***");
                log("*** " & testcasename() & ": FAIL:     inner data content=", v_decrypted_message.content);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              if (not ischosen(v_decrypted_message.content.signedData.signer.self_)) {
                log("*** " & testcasename() & ": FAIL: EC request shall be self-signed by cannonical key ***");
                log("*** " & testcasename() & ": FAIL:     signerInfo=", v_decrypted_message.content.signedData.signer);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not f_get_canonical_itss_key(v_canonical_key)){
                log("*** " & testcasename() & ": INCONC: Unknown ITS-S canonical public key ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout); // emulate inconc
              }
              
              if (not(f_verifyEcdsa(bit2oct(encvalue(v_decrypted_message.content.signedData.tbsData)),
                                    int2oct(0, 32), // issuer is emtpy string 
                                    v_decrypted_message.content.signedData.signature_,
                                    v_canonical_key))){
                log("*** " & testcasename() & ": FAIL: EC request signature verification failed ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // Set verdict
              log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_02_BV

      /**
       * @desc In presence of a valid EC, the enrolment request of the IUT is a rekeying enrolment 
       *       request with the itsId (contained in the InnerECRequest) and the SignerIdentifier 
       *       (contained in the outer EtsiTs1030971Data-Signed) both declared as digest containing 
       *       the HashedId8 of the EC and the outer signature computed using the current valid EC 
       *       private key corresponding to the verification public key.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_REENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                          containing InnerECRequestSignedForPOP
       *                             containing InnerEcRequest
       *                                 containing itsId
       *                                     declared as digest containing the HashedId8 of the EC identifier
       *                 and containing signer
       *                     declared as digest containing the HashedId8 of the EC identifier 
       *                 and containing signature 
       *                     computed using the current valid EC private key corresponding to the verification public key
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_03_BV
       * @reference ETSI TS 102 941, clause 6.1.3
       */
      testcase TC_SECPKI_ITSS_ENR_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_REENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_REENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_03_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_03_BV

      group f_TC_SECPKI_ITSS_ENR_03_BV {

        function f_TC_SECPKI_ITSS_ENR_03_BV_itss(
                                                 in float p_delay := 0.0
        ) runs on ItsPkiItss system ItsPkiItssSystem {
          // Test component configuration

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "_itss: ERROR: Unexpected GeoNet message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
              }
            [] tc_noac.timeout {
              // Trigger the first enrolment
              log("*** " & testcasename() & "_itss: INFO: No GeoNet message received. Continue ***");
              f_sendUtTriggerEnrolmentRequestPrimitive();
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              } 
            } // End of 'alt' statement

          // Test Body
          // Give time to the IUT to setup enrol state
          tc_ac.start; // TDOD To refined, use altstep
          alt {
            [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
              tc_ac.stop;
              log("*** " & testcasename() & "_itss: INFO: IUT is in enrolment state ***");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved. Assuming it was OK... ***");
              //f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          if(p_delay > 0.0){
            log("*** " & testcasename() & "_itss: INFO: Wait for second enrolment trigger ***");
            f_sleep(p_delay);
          }

          // Trigger the second enrolment
          f_sendUtTriggerEnrolmentRequestPrimitive();

          log("*** " & testcasename() & "_itss: PASS: Re-enrolment trigger sent succesfully ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_03_BV_itss

        function f_TC_SECPKI_ITSS_ENR_03_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;
          var HashedId8   v_ec_cert_digest;
          var octetstring v_ec_cert_hash;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);

          if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request))) {
            log("*** " & testcasename() & ": FAIL: First InnerEcRequest failed ***");
            log("*** " & testcasename() & ":      v_request := ", v_request);
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
                        
          var HashAlgorithm ha := f_getHashAlgorithmOfVerificationKeyIndicator(v_inner_ec_response.certificate.toBeSigned.verifyKeyIndicator);
          if (ha == sha256) {
            v_ec_cert_hash := f_hashWithSha256(bit2oct(encvalue(v_inner_ec_response.certificate)));
          } else if (ha == sha384) {
            v_ec_cert_hash := f_hashWithSha384(bit2oct(encvalue(v_inner_ec_response.certificate)));
          } else {
            log("*** " & testcasename() & ": FAIL: Unknown EC certificate hash alg=", ha);
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          v_ec_cert_digest := substr(v_ec_cert_hash, lengthof(v_ec_cert_hash) - 8, 8);

          log("*** " & testcasename() & ": INFO: First InnerEcRequest received ***");
          log(">>>>> v_inner_ec_response=", v_inner_ec_response);
          log(">>>>> v_ec_cert_hash=", v_ec_cert_hash);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              var integer v_result;
              var Oct16 v_request_hash, v_aes_enc_key;
              var Ieee1609Dot2Data v_decrypted_message;
              var EtsiTs102941Data v_etsi_ts_102941_data;
              var InnerEcResponse  v_inner_ec_response2;
              var Ieee1609Dot2Data v_response_message;

              tc_ac.stop;

              // Verify IUT response
              if (not(f_read_ec_request_from_iut_itss(
                                                      v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_etsi_ts_102941_data,
                                                      v_inner_ec_request))) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              
              // send error response to prevent enrolment repetition
              if( not f_http_build_error_ec_response(unknownits, v_request_hash,
                                                  vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                  v_response_message) ) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));
              
              if( not isvalue(v_inner_ec_request) ){
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not(match(v_inner_ec_request.itsId, v_ec_cert_digest))) {
                log("*** " & testcasename() & ": FAIL: ITS-S ID is not equal to the EC HashedId8 ***");
                log("*** " & testcasename() & ": FAIL:     ", match(v_inner_ec_request.itsId, v_ec_cert_digest));
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not(ischosen(v_decrypted_message.content.signedData))) {
                log("*** " & testcasename() & ": FAIL: EC request shall contain signed message ***");
                log("*** " & testcasename() & ": FAIL:     inner data content=", v_decrypted_message.content);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not(ischosen(v_decrypted_message.content.signedData.signer.digest))) {
                log("*** " & testcasename() & ": FAIL: re-keying EC request shall be signed by the previous EC certificate digest***");
                log("*** " & testcasename() & ": FAIL:     signerInfo=", v_decrypted_message.content.signedData.signer);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              
              if (not(f_verifyEcdsa(bit2oct(encvalue(v_decrypted_message.content.signedData.tbsData)),
                                    v_ec_cert_hash,
                                    v_decrypted_message.content.signedData.signature_,
                                    v_inner_ec_response.certificate.toBeSigned.verifyKeyIndicator.verificationKey))) {
                log("*** " & testcasename() & ": FAIL: EC request signature verification failed ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              // Set verdict
              log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_03_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_03_BV

      /**
       * @desc If the EC is revoked, the IUT returns to the state 'initialized'.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_CRL
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is informed about a revocation of its EC
       *         }
       *         then {
       *             the IUT returns to the "initialized" state
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_04_BV
       * @reference ETSI TS 102 941, clause 6.1.3, 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_CRL) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_CRL required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration

        // Synchronization

        // Cleanup
        setverdict(inconc);

      } // End of TC_SECPKI_ITSS_ENR_04_BV

      /**
       * @desc If the EC expires, the IUT returns to the state 'initialized'.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       * with
       * 	the IUT being in the 'enrolled' state
       * 	and the EC of the IUT expires 
       * ensure that
       * 	when
       *             the IUT is requested to send an EnrollmentRequestMessage
       * 	then
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *               containing an encrypted EtsiTs103097Data-Signed
       *                 containing EtsiTs102941Data
       *                   containing InnerECRequestSignedForPOP
       *                     containing InnerEcRequest
       *                       containing itsId
       *                         indicating the canonical identifier of the ITS-S 
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_05_BV
       * @reference ETSI TS 102 941, clause 6.1.3, 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_05_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var Oct32      v_private_key;
        var Oct32      v_public_key_x;
        var Oct32      v_public_key_y;
        var Oct32      v_public_compressed_key;
        var integer    v_compressed_mode;
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_05_BV_itss()); // wait 15 sec before the second enrolment
        v_ea.start(f_TC_SECPKI_ITSS_ENR_05_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of TC_SECPKI_ITSS_ENR_05_BV

      group f_TC_SECPKI_ITSS_ENR_05_BV {

        function f_TC_SECPKI_ITSS_ENR_05_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Test component configuration
          
          // Test adapter configuration
          
          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              // Trigger the first enrolment
              log("*** " & testcasename() & "_itss: INFO: No CA message received ***");
            } 
          } // End of 'alt' statement
          log("*** " & testcasename() & "_itss: INFO: Trigger 1st emrolment ***");
          f_sendUtTriggerEnrolmentRequestPrimitive();
          
          // wait a couple of seconds and run second enrolment to check re-enrolment procedure
          f_sleep(PX_RE_ENROLMENT_DELAY);
          log("*** " & testcasename() & "_itss: INFO: Trigger 2nd emrolment ***");
          f_sendUtTriggerEnrolmentRequestPrimitive();

          // wait 5 seconds after expiration of EC cert
          f_sleep(PX_CERT_EXPIRATION_DELAY + 5.0);

          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          // Trigger the 3rd enrolment
          log("*** " & testcasename() & "_itss: INFO: Trigger 3rd emrolment ***");
          f_sendUtTriggerEnrolmentRequestPrimitive();

          log("*** " & testcasename() & "_itss: PASS: Re-enrolment trigger sent succesfully ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_03_BV_itss

        function f_TC_SECPKI_ITSS_ENR_05_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;
          var HashedId8   v_ec_cert_digest;
          var EtsiTs103097Certificate v_ec_cert;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          
          // Preamble
          if (not(f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request,
                                                   ok,
                                                   m_certificateSubjectAttributes_id_omit(
                                                                                          -,
                                                                                          valueof(m_validityPeriod(f_getCurrentTime() / 1000, 
                                                                                                  m_duration_in_seconds(float2int(PX_CERT_EXPIRATION_DELAY))))
                                                   )))) {
            log("*** " & testcasename() & ": FAIL: First InnerEcRequest failed ***");
            log("*** " & testcasename() & ":      v_request := ", v_request);
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          log(">>>>> 1st v_inner_ec_response=", v_inner_ec_response);

          v_ec_cert := v_inner_ec_response.certificate;
          v_ec_cert_digest := f_calculateDigestFromCertificate(v_ec_cert);

          if (not(ischosen(v_ec_cert.toBeSigned.validityPeriod.duration.seconds))) {
            log("*** " & testcasename() & ": INCONC: wrong EC certificate validity period error");
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          log("*** " & testcasename() & ": INFO: First InnerEcRequest processed. EC = " & oct2str(v_ec_cert_digest) & "***");
          log("*** " & testcasename() & ": INFO:                                      expires in " 
               & int2str(v_ec_cert.toBeSigned.validityPeriod.start_ + v_ec_cert.toBeSigned.validityPeriod.duration.seconds - (f_getCurrentTime()/1000))
               & " seconds ***"
          );
          
          // Wait for 2nd request with EC data
          if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request, deniedrequest))) {
            log("*** " & testcasename() & ": FAIL: 2nd InnerEcRequest failed ***");
            log("*** " & testcasename() & ":      v_request := ", v_request);
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          log(">>>>> 2nd v_inner_ec_response=", v_inner_ec_response);

          if (not(match(v_inner_ec_request.itsId, v_ec_cert_digest))) {
            log("*** " & testcasename() & ": FAIL: ITS-S ID of 2nd request must be equal to the EC HashedId8 ***");
            log("*** " & testcasename() & ": FAIL:     ", match(v_inner_ec_request.itsId, v_ec_cert_digest));
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
          }
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request, deniedrequest))){
            log("*** " & testcasename() & ": FAIL: 3rd InnerEcRequest failed ***");
            log("*** " & testcasename() & ":      v_request := ", v_request);
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          }
          log(">>>>> 3rd v_inner_ec_response=", v_inner_ec_response);

          if(match(v_inner_ec_request.itsId, v_ec_cert_digest)){
            if(v_ec_cert.toBeSigned.validityPeriod.start_ + v_ec_cert.toBeSigned.validityPeriod.duration.seconds < (f_getCurrentTime()/1000)){
              log("*** " & testcasename() & ": INCONC: EC certificate is not expired yet ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
            log("*** " & testcasename() & ": FAIL: ITS-S ID of 3rd request must NOT be equal to the expired EC HashedId8 ***");
            log("*** " & testcasename() & ": FAIL:     ", match(v_inner_ec_request.itsId, v_ec_cert_digest));
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          }
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
          
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_05_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_05_BV

      /**
       * @desc For each enrolment request, the ITS-S shall generate a new verification key pair 
               corresponding to an approved signature algorithm as specified in TS 103 097.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and not PICS_SECPKI_REENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send multiple EnrolmentRequestMessage
       *         }
       *         then {
       *             each EnrolmentRequestMessage
       *                 contains a different and unique verification key pair within the InnerECRequest
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_06_BV
       * @reference ETSI TS 102 941, clause 6.1.3
       */
      testcase TC_SECPKI_ITSS_ENR_06_BV_1() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_06_BV_mtc(c_stInitial);
      } // End of TC_SECPKI_ITSS_ENR_06_BV_1
      testcase TC_SECPKI_ITSS_ENR_06_BV_2() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_06_BV_mtc(c_stEnrolled);
      } // End of TC_SECPKI_ITSS_ENR_06_BV_2

      group f_TC_SECPKI_ITSS_ENR_06_BV_mtc {

        function f_TC_SECPKI_ITSS_ENR_06_BV_mtc(
                                                in charstring p_state
                                                ) runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;

          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
            setverdict(inconc);
            stop;
          }

          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);

          // Start components
          v_itss.start(f_TC_SECPKI_ITSS_ENR_06_BV_itss(PX_RE_ENROLMENT_COUNTER, p_state));
          v_ea.start(f_TC_SECPKI_ITSS_ENR_06_BV_pki(PX_RE_ENROLMENT_COUNTER, p_state));

          // Synchronization
          f_serverSyncClientsTimed(2, c_prDone, PX_TSYNC_TIME_LIMIT);
        
          for(var integer v_i := 0; v_i < PX_RE_ENROLMENT_COUNTER; v_i := v_i + 1){
            f_serverSyncClientsTimed(2, c_nextTry, PX_TSYNC_TIME_LIMIT);
          }
          f_serverSyncClientsTimed(2, c_tbDone, PX_TSYNC_TIME_LIMIT);

          // Cleanup
          f_serverWaitForAllClientsToStop();        
        } // End of f_TC_SECPKI_ITSS_ENR_06_BV_mtc

        function f_TC_SECPKI_ITSS_ENR_06_BV_itss(
                                                integer p_max_try,
                                                charstring p_state
                                                ) runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var EtsiTs103097Certificate v_certificate;

          // Test component configuration
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } 
          } // End of 'alt' statement

          // Test Body
          // Send first enrolment
          for(var integer v_i := 0; v_i < p_max_try; v_i := v_i + 1){
            f_sleep(PX_RE_ENROLMENT_DELAY);
            f_sendUtTriggerEnrolmentRequestPrimitive();
            f_clientSync(c_nextTry, e_success);
          }

          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_06_BV_itss

        function f_TC_SECPKI_ITSS_ENR_06_BV_pki(
                                                integer p_max_try, 
                                                charstring p_state
                                                ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var ListOfPublicVerificationKey v_generated_keys;

          // Test component configuration
          f_cfHttpUp();

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          for (var integer v_i := 0; v_i < p_max_try; v_i := v_i + 1) {
            var HttpMessage v_request;
            var InnerEcRequest v_inner_ec_request;
            var InnerEcResponse v_inner_ec_response;
            var PublicVerificationKey v_key;
            var EnrolmentResponseCode v_response_code;

            if(p_state == c_stInitial){
              v_response_code := deniedrequest;
            } else {
              v_response_code := ok;
            }
            
            if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request, v_response_code))) {
              log("*** " & testcasename() & ": ERROR: EC request receiving error ***");
              setverdict(inconc);
              f_clientSync(c_nextTry, e_error);
            }

            v_key := v_inner_ec_request.publicKeys.verificationKey;

            if (isbound(v_generated_keys) and match(v_generated_keys, superset(v_key))) {
              log("*** " & testcasename() & ": LOG: ", match(v_generated_keys, superset(v_key)), "***");
              log("*** " & testcasename() & ": FAIL: Duplication of generated public keys ***");
              setverdict(fail);
              f_clientSync(c_nextTry, e_error);
            }
            v_generated_keys[v_i] := v_key;

            f_clientSync(c_nextTry, e_success);
          }
          log("*** " & testcasename() & ": PASS: No identical verification keys received in " & int2str(p_max_try) & " messages ***");
          f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_06_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_06_BV

      /**
       * @desc Within the InnerECRequest, the requestedSubjectAttributes shall not contain a certIssuePermissions field.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                         containing InnerECRequestSignedForPOP
       *                             containing InnerEcRequest
       *                                 containing requestedSubjectAttributes
       *                                     not containing certIssuePermissions 
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_07_BV
       * @reference ETSI TS 102 941, clause 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_07_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var Oct32      v_private_key;
        var Oct32      v_public_key_x;
        var Oct32      v_public_key_y;
        var Oct32      v_public_compressed_key;
        var integer    v_compressed_mode;
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_07_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
      } // End of TC_SECPKI_ITSS_ENR_07_BV

      group f_SECPKI_ITSS_ENR_07_BV {

        function f_TC_SECPKI_ITSS_ENR_07_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request)
            {
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest   v_inner_ec_request;
              var InnerEcResponse  v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var Oct16 v_request_hash, v_aes_enc_key;
              var PublicVerificationKey v_canonical_key;
              tc_ac.stop;

              if( not f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                       v_request_hash, v_aes_enc_key,
                                                       v_decrypted_message,
                                                       v_pki_request,
                                                      v_inner_ec_request)) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // send any response to prevent enrolment repetition
              // negative is simpler
              if( not f_http_build_error_ec_response ( unknownits,v_request_hash,
                                                          vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                          v_response_message) )
              {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));

              if (not isvalue(v_inner_ec_request)) {
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (ispresent(v_inner_ec_request.requestedSubjectAttributes.certIssuePermissions)) {
                log("*** " & testcasename() & ": FAIL: InnerEcRequest shall not contain certIssuePermissions attribute ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // Set verdict
              log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_07_BV_pki

      } // End of group f_SECPKI_ITSS_ENR_07_BV

      /**
       * @desc In the headerInfo of the tbsData of the InnerECRequestSignedForPOP all other components 
       *       of the component tbsdata.headerInfo except generationTime and psid are not used and absent. 
       *       The psid shall be set to "secured certificate request" as assigned in ETSI TS 102 965 and 
       *       the generationTime shall be present.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                         containing InnerECRequestSignedForPOP
       *                             containing tbsData
       *                                 containing headerInfo
       *                                     containing psid
       *                                         indicating AID_CERT_REQ
       *                                     and containing generationTime
       *                                     and not containing any other component of tbsdata.headerInfo
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_08_BV
       * @reference ETSI TS 102 941, clause 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_08_BV_1() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_08_BV_mtc(c_stInitial);
      } // End of TC_SECPKI_ITSS_ENR_08_BV_1
      testcase TC_SECPKI_ITSS_ENR_08_BV_2() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_08_BV_mtc(c_stEnrolled);
      } // End of TC_SECPKI_ITSS_ENR_08_BV_2

      group f_SECPKI_ITSS_ENR_08_BV {

        function f_TC_SECPKI_ITSS_ENR_08_BV_mtc(in charstring p_itss_state) runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;

          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
            setverdict(inconc);
            stop;
          }

          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);

          // Start components
          if(p_itss_state == c_stEnrolled) {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_03_BV_itss());
          } else {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
          }
          v_ea.start(f_TC_SECPKI_ITSS_ENR_08_BV_pki(p_itss_state));

          // Synchronization
          f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

          // Cleanup
          f_cfMtcDown01(v_itss, v_ea);
        } // End of TC_SECPKI_ITSS_ENR_08_BV

        function f_TC_SECPKI_ITSS_ENR_08_BV_pki(
                                                in charstring p_itss_state
                                                ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);

          if (p_itss_state == c_stEnrolled) {
            var InnerEcRequest v_inner_ec_request;
            var InnerEcResponse v_inner_ec_response;
            if(not f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request)){
              log("*** " & testcasename() & ": INFO: First InnerEcRequest error ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            log("*** " & testcasename() & ": INFO: First InnerEcRequest received ***");
          }
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              var Oct16 v_request_hash;
              var Oct16 v_aes_enc_key;
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest v_inner_ec_request;
              var InnerEcResponse v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              tc_ac.stop;

              f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request);

              if (not(isvalue(v_pki_request))) {
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // send any response to prevent enrolment repetition
              // negative is simpler
              if( not f_http_build_error_ec_response ( unknownits,v_request_hash,
                                                  vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                  v_response_message) )
              {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));

              if( not ischosen(v_pki_request.content.enrolmentRequest)) {
                log("*** " & testcasename() & ": FAIL: Not an enrolment request ***");
                log("*** " & testcasename() & ":       EtsiTs102941Data.content=", v_pki_request.content);
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not(match(v_pki_request.content.enrolmentRequest,
                            mw_etsiTs103097Data_signed(
                                                       mw_signedData(
                                                                     -,
                                                                     mw_toBeSignedData(
                                                                                       -,
                                                                                       mw_headerInfo_inner_pki_request
                  )))))) {
                log("*** " & testcasename() & ": FAIL: Wrong InnerECRequestSignedForPOP headers ***");
                log("*** " & testcasename() & ":       ", 
                      match( v_pki_request.content.enrolmentRequest, 
                             m_etsiTs103097Data_signed(mw_signedData( -,mw_toBeSignedData(-,mw_headerInfo_inner_pki_request())))));
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // Set verdict
              log("*** " & testcasename() & ": PASS: EC Request received with proper InnerECRequestSignedForPOP headers ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_08_BV_pki

      } // End of group f_SECPKI_ITSS_ENR_08


      /**
       * @desc In the headerInfo of the tbsData of the outer EtsiTs102941Data-Signed all other components 
       *       of the component tbsdata.headerInfo except generationTime and psid are not used and absent. 
       *       The psid shall be set to "secured certificate request" as assigned in ETSI TS 102 965 and 
       *       the generationTime shall be present.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                         containing InnerECRequestSignedForPOP
       *                             containing tbsData
       *                                 containing headerInfo
       *                                     containing psid
       *                                         indicating AID_CERT_REQ
       *                                     and containing generationTime
       *                                     and not containing any other component of tbsdata.headerInfo
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_09_BV
       * @reference ETSI TS 102 941, clause 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_09_BV_1() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_09_BV_mtc(c_stInitial);
      } // End of TC_SECPKI_ITSS_ENR_09_BV_1
      testcase TC_SECPKI_ITSS_ENR_09_BV_2() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_09_BV_mtc(c_stEnrolled);
      } // End of TC_SECPKI_ITSS_ENR_09_BV_2

      group f_TC_SECPKI_ITSS_ENR_09_BV {

        function f_TC_SECPKI_ITSS_ENR_09_BV_mtc(in charstring p_itss_state) runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;

          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
            setverdict(inconc);
            stop;
          }

          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);

          // Start components
          if(p_itss_state == c_stEnrolled) {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_03_BV_itss());
          } else {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
          }
          v_ea.start(f_TC_SECPKI_ITSS_ENR_09_BV_pki(p_itss_state));

          // Synchronization
          f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

          // Cleanup
          f_cfMtcDown01(v_itss, v_ea);
        } // End of f_TC_SECPKI_ITSS_ENR_09_BV_mtc

        function f_TC_SECPKI_ITSS_ENR_09_BV_pki(
                                                in charstring p_itss_state
                                                ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);          
          
          if (p_itss_state == c_stEnrolled) {
            var InnerEcRequest v_inner_ec_request;
            var InnerEcResponse v_inner_ec_response;
            if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request))) {
              log("*** " & testcasename() & ": INFO: First InnerEcRequest error ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            log("*** " & testcasename() & ": INFO: First InnerEcRequest received ***");
          }
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;          
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest   v_inner_ec_request;
              var InnerEcResponse  v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var Oct16 v_request_hash, v_aes_enc_key;
              var PublicVerificationKey v_canonical_key;

              tc_ac.stop;

              if (not(f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request))) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // send error response to prevent enrolment repetition
              if( not f_http_build_error_ec_response ( unknownits,v_request_hash,
                                                  vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                  v_response_message) )
              {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));

              // check signed message structure
              if ( not ( match( v_decrypted_message,
                            mw_etsiTs103097Data_signed(
                                                       mw_signedData(
                                                                     -,
                                                                     mw_toBeSignedData(
                                                                                       -,
                                                                                       mw_headerInfo_outer_pki_request
                          
              )))))) {
                log("*** " & testcasename() & ": FAIL: Invalid outer signed structure in EC request ***");
                log("*** " & testcasename() & ":       ",
                  match(v_decrypted_message,m_etsiTs103097Data_signed(mw_signedData(-,mw_toBeSignedData(-,mw_headerInfo_outer_pki_request()))))
                );
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // Set verdict
              log("*** " & testcasename() & ": PASS: EC request contains valid outer signed structure ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
      
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_09_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_09_BV

      /**
       * @desc The EtsiTs103097Data-Encrypted containing the correctly encrypted ciphertext and a recipients 
       *       component containing one instance of RecipientInfo of choice certRecipInfo containing the 
       *       hashedId8 of the EA certificate in recipientId and the encrypted data encryption key in encKey. 
       *       The data encryption key is encrypted using the public key found in the EA certificate referenced 
       *       in the recipientId.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing recipients
       *                     containing exactly one instance of RecipientInfo of choice certRecipInfo
       *                         containing recipientId
       *                             indicating the hashedId8
       *                                 referencing to the EA certificate
       *                                     containing encryptionKey (KEY)
       *                      and containing encKey
       *                          being a symmetric key (SYMKEY) encrypted using the key KEY
       *                  containing ciphertext
       *                      which is encrypted using the symmetric key SYMKEY contained in encKey
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v1.2.1 SECPKI_ITSS_ENR_10_BV
       * @reference ETSI TS 102 941, clause 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_10_BV_1() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_10_BV_mtc(c_stInitial);
      } // End of TC_SECPKI_ITSS_ENR_10_BV_1
      testcase TC_SECPKI_ITSS_ENR_10_BV_2() runs on ItsMtc system ItsPkiItssSystem {
        f_TC_SECPKI_ITSS_ENR_10_BV_mtc(c_stEnrolled);
      } // End of TC_SECPKI_ITSS_ENR_10_BV_2

      group f_TC_SECPKI_ITSS_ENR_10_BV {

        function f_TC_SECPKI_ITSS_ENR_10_BV_mtc(in charstring p_itss_state) runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;

          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
            setverdict(inconc);
            stop;
          }

          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);

          // Start components
          if(p_itss_state == c_stEnrolled) {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_03_BV_itss());
          } else {
            v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
          }
          v_ea.start(f_TC_SECPKI_ITSS_ENR_10_BV_pki(p_itss_state));

          // Synchronization
          f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

          // Cleanup
          f_cfMtcDown01(v_itss, v_ea);
        } // End of f_TC_SECPKI_ITSS_ENR_10_BV_mtc

        function f_TC_SECPKI_ITSS_ENR_10_BV_pki(
                                                in charstring p_itss_state
                                                ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);          
          
          if (p_itss_state == c_stEnrolled) {
            var InnerEcRequest v_inner_ec_request;
            var InnerEcResponse v_inner_ec_response;
            if (not(f_await_ec_request_send_response( v_inner_ec_request, v_inner_ec_response, v_request))) {
              log("*** " & testcasename() & ": INFO: First InnerEcRequest error ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            log("*** " & testcasename() & ": INFO: First InnerEcRequest received ***");
          }
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;          
          alt {
            [] a_await_ec_http_request_from_iut(  
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                      PICS_HTTP_POST_URI_EC,
                                                                                      -,
                                                                                      mw_http_message_body_binary(
                                                                                                                  mw_binary_body_ieee1609dot2_data(
                                                                                                                                                   mw_enrolmentRequestMessage(
                                                                                                                                                                              mw_encryptedData(
                                                                                                                                                                                               { mw_recipientInfo_certRecipInfo(
                                                                                                                                                                                                                                mw_pKRecipientInfo(vc_eaHashedId8)
                                                                                                                                                                                                                                )
                                                                                                                                                                                               }
                                                )))))),
                                                v_request) {
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest   v_inner_ec_request;
              var InnerEcResponse  v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var Oct16 v_request_hash, v_aes_enc_key;
              var PublicVerificationKey v_canonical_key;
              tc_ac.stop;

              log("*** " & testcasename() & ": PASS: EC request sent to proper EA recipient ***");

              if (not(f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request))) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // send any response to prevent enrolment repetition
              // negative is just simpler
              if( not f_http_build_error_ec_response ( unknownits,v_request_hash,
                                                          vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                          v_response_message) )
              {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));

              // Set verdict
              log("*** " & testcasename() & ": PASS: EC request can be decrypted ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              f_send_500_Internal_Error(v_headers); // we don't care about response
              log("*** " & testcasename() & ": FAIL: EC request sent to wrong recipient ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
      
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_10_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_10_BV

      /**
       * @desc In the inner signed data structure (InnerECRequestSignedForPOP), the signature is computed 
       *       on InnerECRequest with the private key corresponding to the new verificationKey to prove 
       *       possession of the generated verification key pair.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing EtsiTs103097Data
       *                         containing InnerECRequestSignedForPOP
       *                             containing tbsData
       *                                 containing InnerEcRequest
       *                                     containing verificationKey (VKEY)
       *                              containing signature
       *                                  computed on InnerECRequest
       *                                      using the private key corresponding to VKEY
       *                                          contained in InnerECRequest
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_11_BV
       * @reference ETSI TS 102 941, clause 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_11_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_11_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
      } // End of TC_SECPKI_ITSS_ENR_11_BV
      
      group f_TC_SECPKI_ITSS_ENR_11_BV {
        function f_TC_SECPKI_ITSS_ENR_11_BV_pki ( ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          
          // Test adapter configuration
          
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);          
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body
          tc_ac.start;          
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request)
            {
              var Ieee1609Dot2Data v_decrypted_message;
              var InnerEcRequest   v_inner_ec_request;
              var InnerEcResponse  v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var Oct16 v_request_hash, v_aes_enc_key;
              var PublicVerificationKey v_canonical_key;
              tc_ac.stop;

              if (not f_read_ec_request_from_iut_itss(v_request.request.body.binary_body.ieee1609dot2_data,
                                                      v_request_hash, v_aes_enc_key,
                                                      v_decrypted_message,
                                                      v_pki_request,
                                                      v_inner_ec_request))
              {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // send any response to prevent enrolment repetition
              // negative is easier
              if( not f_http_build_error_ec_response ( unknownits,v_request_hash,
                                                  vc_eaPrivateKey, vc_eaWholeHash, v_aes_enc_key,
                                                  v_response_message) ) {
                f_send_500_Internal_Error(v_headers);
                log("*** " & testcasename() & ": FAIL: Can't build enrolment response ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
              f_http_send(v_headers, m_http_response(
                                                      m_http_response_ok(
                                                              m_http_message_body_binary(
                                                                      m_binary_body_ieee1609dot2_data(v_response_message)
                                                              ), v_headers)));


              if (not isbound(v_inner_ec_request))
              {
                log("*** " & testcasename() & ": FAIL: Can't parse enrolment request***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              if (not f_verifyEcdsa(bit2oct(encvalue(v_pki_request.content.enrolmentRequest.content.signedData.tbsData)),
                                    int2oct(0, 32),
                                    v_pki_request.content.enrolmentRequest.content.signedData.signature_,
                                    v_inner_ec_request.publicKeys.verificationKey))
              {
                log("*** " & testcasename() & ": FAIL: EC request signature verification failed ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }

              // Set verdict
              log("*** " & testcasename() & ": PASS: PoP signature verified correctly ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }

            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
      
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_11_BV_pki
      } // End of group g_TC_SECPKI_ITSS_ENR_11_BV

      /**
       * @desc Check that signing of Enrolment HttpRequest message is permitted by the EC certificate.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_REENROLMENT 
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is in enrolled state
       *             the IUT is requested to send an EnrolmentRequestMessage
       *         }
       *         then {
       *             the IUT sends an EtsiTs103097Data-Encrypted
       *                 containing an encrypted EtsiTs103097Data-Signed
       *                     containing signer
       *                         containing digest
       *                             indicating HashedId8 of the EC certificate
       *                                 containing appPermissions
       *                                     containing an item of type PsidSsp
       *                                         containing psid
       *                                             indicating AID_CERT_REQ
       *                                         and containing ssp
       *                                             containing opaque[0] (version) 
       *                                                 indicating 1
       *                                             containing opaque[1] (value) 
       *                                                 indicating "Enrolment Request" (bit 1) set to 1
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_12_BV
       * @reference ETSI TS 102 941, clause 6.1.3, 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_12_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_REENROLMENT ) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SECPKI_REENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_03_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
      } // End of TC_SECPKI_ITSS_ENR_12_BV

    } // End of group itss_enrolment_request

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.2.2  Enrollment response handling
    group itss_enrolment_response {

      /**
       * @desc If an enrolment request fails, the IUT returns to the state 'initialized'.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an EnrolmentRequestMessage
       *             and the EnrolmentResponseMessage is received
       *                 containing a responseCode different than 0
       *         }
       *         then {
       *             the IUT returns to the "initialized" state
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_RECV_01_BV
       * @reference ETSI TS 102 941, clause 6.1.3, 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_RECV_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_RECV_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_RECV_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of TC_SECPKI_ITSS_ENR_RECV_01_BV

      group f_TC_SECPKI_ITSS_ENR_RECV_01_BV {

        function f_TC_SECPKI_ITSS_ENR_RECV_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var boolean                 v_iut_state_ok := false;

          // Test component configuration

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          tc_ac.start; // TDOD To refined, use altstep
          alt {
            [] utPort.receive(UtPkiTriggerInd: { state := 0 }) {
              tc_ac.stop;
              v_iut_state_ok := true;
              log("*** " & testcasename() & "_itss: INFO: IUT is in initialized state ***");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved ***");
              //f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_noac.timeout {
              if (v_iut_state_ok == true) {
                log("*** " & testcasename() & "_itss: : PASS: Enrolment trigger processed succesfully ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("Did not receive IUT state update");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              }
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_RECV_01_BV_itss

        function f_TC_SECPKI_ITSS_ENR_RECV_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers     v_headers;
          var HttpMessage v_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                     PICS_HTTP_POST_URI_EC,
                                                                                     -,
                                                                                     mw_http_message_body_binary(
                                                                                                                 mw_binary_body_ieee1609dot2_data(
                                                                                                                                                  mw_enrolmentRequestMessage(
                                                                                                                                                                            mw_encryptedData(
                                                                                                                                                                                             { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_eaHashedId8)), * },
                                                                                                                                                                                             mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                             )))))),
                                                v_request
                                                ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerEcRequest v_inner_ec_request;
              var InnerEcResponse v_inner_ec_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_ec_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_request, v_inner_ec_response, v_response, v_result, -, -, cantparse);
              // Send response forcing error code
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              }
              // Set verdict
              if (v_result == 0) {
                log("*** " & testcasename() & ": INFO: InnerEcRequest received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_RECV_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_RECV_01_BV

      /**
       * @desc The IUT is capable of parsing and handling of positive EnrolmentResponse messages 
       *       containing the requested EC. In case of a successful enrolment, the IUT switches 
       *       to the state 'enrolled'.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is requested to send an initial EnrolmentRequestMessage
       *             and when the IUT receives a subsequent EnrolmentResponseMessage as an answer of the EA
       *                 containing a responseCode
       *                     indicating 0
       *                 and containing an enrolment certificate
       *         }
       *         then {
       *             the IUT switches to the "enrolled" state
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_RECV_02_BV
       * @reference ETSI TS 102 941, clause 6.1.3, 6.2.3.2.1
       */
      testcase TC_SECPKI_ITSS_ENR_RECV_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_ENR_RECV_02_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_RECV_02_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_RECV_02_BV

      group f_TC_SECPKI_ITSS_ENR_RECV_02_BV {

        function f_TC_SECPKI_ITSS_ENR_RECV_02_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var boolean                 v_iut_state_ok := false;

          // Test component configuration
//TODO:DELETE          vc_hashedId8ToBeUsed := ""; // No certificates //PX_IUT_DEFAULT_CERTIFICATE
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          tc_ac.start; // TDOD To refined, use altstep
          alt {
            [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
              tc_ac.stop;
              v_iut_state_ok := true;
              log("*** " & testcasename() & "_itss: INFO: IUT is in enrol state ***");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved ***");
              //f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_noac.timeout {
              if (v_iut_state_ok == true) {
                log("*** " & testcasename() & "_itss: : PASS: Enrolment trigger processed succesfully ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("Did not receive IUT state update");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              }
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_RECV_02_BV_itss

        function f_TC_SECPKI_ITSS_ENR_RECV_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                     PICS_HTTP_POST_URI_EC,
                                                                                     -,
                                                                                     mw_http_message_body_binary(
                                                                                                                 mw_binary_body_ieee1609dot2_data(
                                                                                                                                                  mw_enrolmentRequestMessage(
                                                                                                                                                                            mw_encryptedData(
                                                                                                                                                                                             { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_eaHashedId8)), * },
                                                                                                                                                                                             mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                             )))))),
                                                v_request
                                                ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerEcRequest v_inner_ec_request;
              var InnerEcResponse v_inner_ec_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_ec_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_request, v_inner_ec_response, v_response, v_result);
              // Send response forcing error code
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              }
              // Set verdict
              if (v_result == 0) {
                log("*** " & testcasename() & ": INFO: InnerEcRequest received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_RECV_02_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_RECV_02_BV

    } // End of group itss_enrolment_response

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.2.3  Enrollment request repetition
    group itss_enrollment_request_repetition {

      /**
       * @desc Check that IUT repeats an enrollment request when response has not been received
       * <pre>
       * Pics Selection: PICS_SECPKI_ENROLLMENT_RETRY
       * Initial conditions: {
       *     the IUT being in the 'initialized' state
       *     and the IUT already sent the Enrollment Request at the time T1
       *     and the IUT has not yet received the Enrollment Response
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT local time is reached the T1 + PIXIT_ENR_TIMEOUT_TH1  
       *         }
       *         then {
       *             the IUT sends to EA an EnrollmentRequestMessage
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_ENR_REP_01_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      group g_TC_SECPKI_ITSS_ENR_REP_01_BV {

        testcase TC_SECPKI_ITSS_ENR_REP_01_BV() runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;

          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_ENROLLMENT_RETRY) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SECPKI_ENROLLMENT_RETRY required for executing the TC ***");
            setverdict(inconc);
            stop;
          }

          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);

          // Start component
          v_itss.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss());
          v_ea.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_pki());

          // Synchronization
          f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

          // Cleanup
          f_cfMtcDown01(v_itss, v_ea);

        } // End of testcase TC_SECPKI_ITSS_ENR_REP_01_BV

        function f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss()
             runs on ItsPkiItss system ItsPkiItssSystem {
          // Test component configuration
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "_itss: ERROR: Unexpected GeoNet message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
              }
            [] tc_noac.timeout {
              // Trigger the first enrolment
              log("*** " & testcasename() & "_itss: INFO: No GeoNet message received. Continue ***");
              f_sendUtTriggerEnrolmentRequestPrimitive(-, -, -, -, -, false);
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          // Nothing to do in test body. Just wait for ENR repetition in PKI component          
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss

        function f_TC_SECPKI_ITSS_ENR_REP_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_initial_request;
          var HttpMessage v_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);

          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              log("*** " & testcasename() & ": INFO: First enrolment request received ***");
              tc_ac.stop;
              f_http_restart("inner_ec_request");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: First enrolment request not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
  
          // Test Body
          tc_wait.start(PX_EC_REPETITION_TIMEOUT);
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              log("*** " & testcasename() & ": PASS: EC repetition has been received after a ", tc_wait.read, " ***");
              tc_wait.stop;
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] tc_wait.timeout {
              log("*** " & testcasename() & ": FAIL: EC repetition was not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
          }
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_REP_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_REP_01_BV

      /**
       * @desc Check that IUT uses the same message to perform enrollment retry
       * <pre>
       * Pics Selection: PICS_SECPKI_ENROLLMENT_RETRY
       * Initial conditions: {
       *     the IUT being in the 'initialized' state
       *     and the IUT already sent the Enrollment Request (M)
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to re-send an Enrollment Request 
       *         }
       *         then {
       *             the IUT sends M to EA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_ENR_REP_02_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      group f_TC_SECPKI_ITSS_ENR_REP_02_BV {
        testcase TC_SECPKI_ITSS_ENR_REP_02_BV() runs on ItsMtc system ItsPkiItssSystem {
          // Local variables
          var ItsPkiItss v_itss;
          var ItsPkiHttp v_ea;
          
          // Test control
          if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_ENROLLMENT_RETRY) {
            log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SECPKI_ENROLLMENT_RETRY required for executing the TC ***");
            setverdict(inconc);
            stop;
          }
          
          // Test component configuration
          f_cfMtcUp01(v_itss, v_ea);
          
          // Start component
          v_itss.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss());
          v_ea.start(f_TC_SECPKI_ITSS_ENR_REP_02_BV_pki());
          
          // Synchronization
          f_serverSync2ClientsAndStop({c_prDone, c_tbDone});
          
          // Cleanup
          f_cfMtcDown01(v_itss, v_ea);
          
        } // End of testcase TC_SECPKI_ITSS_ENR_REP_02_BV
              
        function f_TC_SECPKI_ITSS_ENR_REP_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request1;
          var HttpMessage v_request2;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request1) {
              log("*** " & testcasename() & ": INFO: First enrolment request received ***");
              tc_ac.stop;
              f_http_restart("inner_ec_request");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: First enrolment request not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          
          // Test Body          
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(
                                      mw_http_request(
                                            mw_http_request_post(
                                                    PICS_HTTP_POST_URI_EC,
                                                    -,
                                                    v_request1.request.body)),

                                      v_request2)
            {
              tc_ac.stop;
              log("*** " & testcasename() & ": PASS: Same enrolment request received ***");
              // send error respond to prevent future requests
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_success);
            }
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request2) {
              tc_ac.stop;
              log("*** " & testcasename() & ": FAIL: 2nd enrolment request is not identical ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_error);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: First enrolment request not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_REP_02_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_REP_02_BV

      /**
       * @desc Check that IUT stops sending the Enrollment Request message if Enrollment Response message has been received
       * <pre>
       * Pics Selection: PICS_SECPKI_ENROLLMENT_RETRY
       * Initial conditions: {
       *     the IUT being in the 'initialized' state
       *     and the IUT has sent the Enrollment Request more than 1 time
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT receives an Enrollment Response
       *         }
       *         then {
       *             the IUT stops sending Enrollment Requests to EA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_ENR_REP_03_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_ENR_REP_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_ENROLLMENT_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SECPKI_ENROLLMENT_RETRY required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_REP_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_REP_03_BV
      
      group f_TC_SECPKI_ITSS_ENR_REP_03_BV {
        
        function f_TC_SECPKI_ITSS_ENR_REP_03_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var HttpMessage v_initial_request;
          var integer v_rep_count := 0;
          var integer c_rep_answer := 2; // answer to the n-th request repetition

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_initial_request) {
              log("*** " & testcasename() & ": INFO: First enrolment request received ***");
              tc_ac.stop;
              f_http_restart("inner_ec_request");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: First enrolment request not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);

          // Test Body
          tc_ac.start;
          alt {
            // match initial request
            [] a_await_ec_http_request_from_iut(
                                                mw_http_ec_request_generic(
                                                                            v_initial_request.request.body.binary_body.ieee1609dot2_data
                                                                          ), 
                                                v_request
                                                ) {
              v_rep_count := v_rep_count + 1;
              tc_ac.stop;
              if (v_rep_count < c_rep_answer) {
                // skip repetition
                log("*** " & testcasename() & ": ", v_rep_count, " EC repetition has been received. Continue ***");
                f_http_restart("inner_ec_request");
                tc_ac.start;
                repeat;
              }
              
              if(v_rep_count == c_rep_answer) {
                // answer this repetition
                log("*** " & testcasename() & ": ", v_rep_count, " EC repetition has been received. Respond ***");
                var InnerEcRequest v_inner_ec_request;
                var InnerEcResponse v_inner_ec_response;
                var HttpMessage v_response;
                var integer v_result;

                f_verify_http_ec_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_request, v_inner_ec_response, v_response, v_result);
                // Send response
                if (isvalue(v_response)) {
                  httpPort.send(v_response);
                } else { // Send HTTP error 500
                  f_send_500_Internal_Error(v_headers);
                }
                // Set verdict
                if (v_result == 0) {
                  log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                } else {
                  log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                }  
                repeat;
              }

              // repetition request received after sending the response
              log("*** " & testcasename() & ": FAIL: Repetition request received after response ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);  
            }                                     
            [] a_await_ec_http_request_from_iut( mw_http_ec_request_generic, v_request ) {
              tc_ac.stop;
              f_send_500_Internal_Error(v_headers);
              log("*** " & testcasename() & ": INCONC: New request received during repetition period ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
            [] tc_ac.timeout {
              if(v_rep_count < c_rep_answer) {
                log("*** " & testcasename() & ": INCONC: Timeout occured during the repetition period ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              }
              
              log("*** " & testcasename() & ": PASS: EC repetition request was not received after response ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          } // End of alt statment

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_REP_03_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_REP_03_BV

      /**
       * @desc Check that IUT stops sending the Enrollment Request message if maximum number of retry has been reached
       * <pre>
       * Pics Selection: PICS_SECPKI_ENROLLMENT_RETRY
       * Initial conditions: {
       *     the IUT being in the 'initialized' state
       *     and the IUT has started sending the Enrollment Request
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT sent the PIXIT_ENR_MAX_N1 Enrollment Request messages
       *         }
       *         then {
       *             the IUT stops sending Enrollment Requests
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_ENR_REP_04_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_ENR_REP_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_ENROLLMENT_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SECPKI_ENROLLMENT_RETRY required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_REP_04_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_REP_04_BV  

      group f_TC_SECPKI_ITSS_ENR_REP_04_BV {

        function f_TC_SECPKI_ITSS_ENR_REP_04_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_initial_request;
          var HttpMessage v_request;
          var integer v_count := 0;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);

          // Test Body
          tc_ac.start;
          alt{
            [v_count <= PX_ENR_MAX_N1] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_initial_request) {
              v_count := v_count + 1;
              repeat;
            }
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_initial_request){
              // count exceed
              log("*** " & testcasename() & ": FAIL: Too many repetitions of Enrolment requests received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_error);
            }
            [] tc_ac.timeout{
              if(v_count > PX_ENR_MAX_N1){
                log("*** " & testcasename() & ": PASS: Maximum number of Enrollment request repetitions received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              } else {
                log("*** " & testcasename() & ": INCONC: Expected message not received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);             
              }
            }
          }
          // Postamble
          f_cfHttpDown();

        } // End of function f_TC_SECPKI_ITSS_ENR_REP_04_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_REP_04_BV

      /**
       * @desc Check that IUT stops sending the Enrollment Request message if timeout has been reached
       * <pre>
       * Pics Selection: PICS_SECPKI_ENROLLMENT_RETRY
       * Initial conditions: {
       *     the IUT being in the 'initialized' state
       *     and the IUT has started sending the Enrollment Request at the time T1
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT sent the PIXIT_ENR_TIMEOUT_TH2 Enrollment Request messages
       *         }
       *         then {
       *             the IUT stops sending an Enrollment Request messages
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_ENR_REP_05_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_ENR_REP_05_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SECPKI_ENROLLMENT_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SECPKI_ENROLLMENT_RETRY required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_ENR_REP_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_REP_05_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_ENR_REP_05_BV

      group f_TC_SECPKI_ITSS_ENR_REP_05_BV {

        function f_TC_SECPKI_ITSS_ENR_REP_05_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var HttpMessage v_initial_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);

          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_initial_request) {
              log("*** " & testcasename() & ": INFO: First enrolment request received ***");
              tc_ac.stop;
              f_http_restart("inner_ec_request");
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: First enrolment request not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
          // Test body
          tc_wait.start(PX_EC_REPETITION_TIMEOUT_TH2);
          alt {
            [] a_await_ec_http_request_from_iut(mw_http_ec_request_generic, v_request) {
              log("*** " & testcasename() & ": FAIL: No EC repetition should be received ***");
              tc_wait.stop;
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_wait.timeout {
              log("*** " & testcasename() & ": PASS: EC repetition not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          }

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_REP_05_BV_pki

      } // End of group f_TC_SECPKI_ITSS_ENR_REP_05_BV
      
    } // End of group itss_enrollment_request_repetition

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.3.1  Authorization request
    group itss_authorization_request {

        function f_TC_SECPKI_ITSS_AUTH_itss(
                                            in integer p_execution_count := 1,
                                            in boolean p_force_enrolment := PX_TRIGGER_EC_BEFORE_AT
        ) runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_sendUtTriggerEnrolmentRequestPrimitive();
                tc_ac.start;
                alt {
                  [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
                    tc_ac.stop;
                    log("*** " & testcasename() & "_itss: INFO: IUT is in enrol state ***");
                  }
                  [LibItsPki_Pics.PICS_UT_STATE_INDICATION ] tc_ac.timeout {
                    log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved ***");
                    f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
                  }
                  [not LibItsPki_Pics.PICS_UT_STATE_INDICATION ] tc_ac.timeout {
                    log("*** " & testcasename() & "_itss: DBG: IUT doesn't support state indication. Assume it was well done.***");
                  }
                } // End of 'alt' statement
              } else {
                log("*** " & testcasename() & "_itss: DBG: Assume IUT already in enrolled state.***");
              }
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          var integer v_i := 0;
          label l_repeat;
          log("*** " & testcasename() & "_itss: DBG: Send UT Authorisation request " & int2str(v_i) & " out of " & int2str(p_execution_count) & ". ***");
          f_sendUtTriggerAuthorizationRequestPrimitive();
          v_i := v_i + 1;
          if( v_i < p_execution_count ){
            log("*** " & testcasename() & "_itss: Sync on " & c_nextTry & ". ***");
            f_clientSync(c_nextTry , e_success );
            f_sleep ( 1.0 );
            goto l_repeat;
          }
          log("*** " & testcasename() & "_itss: Success. ***");
          f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_04_BV_itss

        type record TC_SECPKI_ITSS_AUTH_04_BV_pki_test_data {
          OctSet nonces,
          OctSet symmKeys
        };
        type set of PublicVerificationKey SetOfPublicVerificationKey;
        type set of BasePublicEncryptionKey SetOfPublicEncryptionKey;

        type record TC_SECPKI_ITSS_AUTH_06_BV_pki_test_data {
          SetOfPublicVerificationKey vKeys,
          SetOfPublicEncryptionKey eKeys,
          OctSet hmacKeys
        };

        type record SECPKI_ITSS_TestData {
          integer counter,
          Oct16   aesKey,
          union {
            integer none_,
            TC_SECPKI_ITSS_AUTH_04_BV_pki_test_data tc04,
            TC_SECPKI_ITSS_AUTH_06_BV_pki_test_data tc06
          } anyData
        };
        const SECPKI_ITSS_TestData SECPKI_ITSS_TestData_init_value := {0, '00000000000000000000000000000000'O, {none_:=0}};

        type function F_Test_Ieee1609Dot2Data (inout SECPKI_ITSS_TestData p_data, in Ieee1609Dot2Data p_value)
             runs on ItsPkiHttp return boolean;
        type function F_Test_EtsiTs102941Data (inout SECPKI_ITSS_TestData p_data,  in EtsiTs102941Data p_value)
             runs on ItsPkiHttp return boolean;
        
        function f_TC_SECPKI_ITSS_AUTH_pki_simple(
          in SECPKI_ITSS_TestData p_data := SECPKI_ITSS_TestData_init_value,//< Data to be sent to callbacks
          in F_Test_Ieee1609Dot2Data pf_test_encryptedData := null, //< function to be called when outer message is decrypted
          in F_Test_Ieee1609Dot2Data pf_test_signedData := null,    //< function to be called to test signed data
          in F_Test_EtsiTs102941Data pf_test_pkiRequest := null,    //< function to be called to test pki request

          in integer p_execution_count := 1,                        //< count of messages to be tested
          in boolean p_force_enrolment := PX_TRIGGER_EC_BEFORE_AT,
          in template (present) HttpMessage  pmw_at_request := mw_http_at_request_generic,
          in AuthorizationResponseCode p_responseCode := ok          

        ) runs on ItsPkiHttp system ItsPkiItssSystem {
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(p_data, pf_test_encryptedData, pf_test_signedData, pf_test_pkiRequest,
                                    p_execution_count, p_force_enrolment, pmw_at_request, p_responseCode);
          f_cfHttpDown();
        }

        function f_TC_SECPKI_ITSS_AUTH_pki(
          inout SECPKI_ITSS_TestData p_data,                        //< Data to be sent to callbacks
          in F_Test_Ieee1609Dot2Data pf_test_encryptedData := null, //< function to be called when outer message is decrypted
          in F_Test_Ieee1609Dot2Data pf_test_signedData := null,    //< function to be called to test signed data
          in F_Test_EtsiTs102941Data pf_test_pkiRequest := null,    //< function to be called to test pki request

          in integer p_execution_count := 1,                        //< count of messages to be tested
          in boolean p_force_enrolment := PX_TRIGGER_EC_BEFORE_AT,
          in template (present) HttpMessage  pmw_at_request := mw_http_at_request_generic,
          in AuthorizationResponseCode p_responseCode := ok          
        
        ) runs on ItsPkiHttp system ItsPkiItssSystem {
        
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var HttpMessage v_response;
          var InnerAtRequest v_inner_at_request;

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (p_force_enrolment) {
            var InnerEcRequest v_inner_ec_request;
            var InnerEcResponse v_inner_ec_response;
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          }

          // Test body
          var charstring c_interSyncPoint;
          if (p_execution_count > 1) { c_interSyncPoint := c_nextTry; }
          else                       { c_interSyncPoint := c_tbDone; }
          p_data.counter := 0;
          log("*** " & testcasename() & ": Wait for Authorization message " & int2str(p_data.counter) & " ***");
            [] a_await_at_http_request_from_iut(pmw_at_request, v_request) {
              var Oct16 v_request_hash;
              var Ieee1609Dot2Data v_request_message_signed;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
              var InnerAtResponse v_at_response;
              var boolean v_result;

              tc_ac.stop;

              if( pf_test_encryptedData != null ){
                if( not pf_test_encryptedData.apply( p_data, v_request.request.body.binary_body.ieee1609dot2_data )){
                  log("*** " & testcasename() & ": FAIL: Encrypted data check failed ***");
                  f_send_500_Internal_Error(v_headers);
                  f_selfOrClientSyncAndVerdict(c_interSyncPoint, e_error);
                }
              }

              if(pf_test_signedData == null and pf_test_pkiRequest == null) {
                // don't need to parse the rest of the message
                f_send_500_Internal_Error(v_headers);
              } else {
                v_result := f_read_pki_request_message (
                                                        v_request.request.body.binary_body.ieee1609dot2_data,
                                                        vc_aaPrivateEncKey, vc_aaWholeHash,
                                                        v_request_hash, p_data.aesKey,
                                                        v_request_message_signed, 
                                                        v_pki_request);
                
                if( isbound(v_request_message_signed) ){
                  if(pf_test_signedData != null){
                    v_result := pf_test_signedData.apply(p_data, v_request_message_signed );
                  }
                }
                
                if(not v_result) {
                  log("*** " & testcasename() & ": FAIL: Can't parse Auth request ***");
                  f_send_500_Internal_Error(v_headers);
                  f_selfOrClientSyncAndVerdict(c_interSyncPoint, e_error);
                }

                log("*** " & testcasename() & ": The " & int2str(p_data.counter) & " Authorization message received ***");

                var template (omit) InnerAtRequest v_innerAtRequest := omit;
                
                if(pf_test_pkiRequest != null){
                  // send response to prevent auth repetition
                  if( ischosen ( v_pki_request.content.authorizationRequest ) ) {
                    v_innerAtRequest := v_pki_request.content.authorizationRequest;
                  }
                }
                if(p_responseCode == ok and not isvalue(v_innerAtRequest)){
                  p_responseCode := its_aa_cantparse;
                }

                if( not f_http_build_authorization_response ( v_innerAtRequest , p_responseCode , 
                                                              v_request_hash,
                                                              vc_aaPrivateKey, vc_aaWholeHash, p_data.aesKey,
                                                              v_at_response, v_response_message) )
                {
                  f_send_500_Internal_Error(v_headers);
                }else{
                  f_http_send(v_headers, 
                            m_http_response(
                                    m_http_response_ok(
                                            m_http_message_body_binary(
                                                    m_binary_body_ieee1609dot2_data(v_response_message)
                                            ), v_headers
                                    )));
                } 

                if( pf_test_pkiRequest != null) {
                  if( not pf_test_pkiRequest.apply( p_data, v_pki_request ) ) {
                    f_selfOrClientSyncAndVerdict(c_interSyncPoint, e_error);
                  }
                }
              }

              p_data.counter := p_data.counter + 1;
              if( p_data.counter < p_execution_count ) {
                f_clientSync(c_nextTry, e_success);
                log("*** " & testcasename() & ": Wait for Auth message " & int2str(p_data.counter) & " ***");
                tc_ac.start;
                repeat;
              }

              log("*** " & testcasename() & ": PASS: Everything OK ***");

            [] a_await_at_http_request_from_iut(?, v_request) {
              log("*** " & testcasename() & ": ERROR: Strange message received ***");
              f_selfOrClientSyncAndVerdict(c_interSyncPoint, e_error);
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_interSyncPoint, e_timeout);
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
        } // End of function f_TC_SECPKI_ITSS_AUTH_pki


      /**
       * @desc Check that the ITS-S send the Authorization HttpRequest message to the Authorization Authority (AA) to request an authorization ticket.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT being in the "operational state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_01_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.0
       */
      testcase TC_SECPKI_ITSS_AUTH_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_aa;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_aa);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_aa);

      } // End of testcase TC_SECPKI_ITSS_AUTH_01_BV

      group f_TC_SECPKI_ITSS_AUTH_01_BV {

        function f_TC_SECPKI_ITSS_AUTH_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;
          var template (omit) EtsiTs103097Certificate v_ec_cert := omit;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              v_ec_cert := v_inner_ec_response.certificate;
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          }

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_at_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                     PICS_HTTP_POST_URI_AT,
                                                                                     -,
                                                                                     mw_http_message_body_binary(
                                                                                                                 mw_binary_body_ieee1609dot2_data(
                                                                                                                                                  mw_authorizationRequestMessage(
                                                                                                                                                                                 mw_encryptedData(
                                                                                                                                                                                                  { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_aaHashedId8)), * },
                                                                                                                                                                                                  mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                                 )))))),
                                                v_request
                                                ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerAtRequest v_inner_at_request;
              var InnerAtResponse v_inner_at_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_at_request_from_iut_itss(v_request.request, v_headers, v_ec_cert, v_inner_at_request, v_inner_at_response, v_response, v_result);
              log("f_TC_SECPKI_ITSS_AUTH_01_BV_pki: v_result: ", v_result);
              log("f_TC_SECPKI_ITSS_AUTH_01_BV_pki: v_response: ", v_response);
              // Send response
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              }
              // Set verdict
              if (v_result == 0) {
                var octetstring v_msg;
                var octetstring v_hashed_id8;

                log("*** " & testcasename() & ": PASS: InnerAtResponse received ***");
                v_msg := bit2oct(encvalue(v_inner_at_response.certificate));
                if (ischosen(v_inner_at_response.certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaBrainpoolP384r1)) {
                  v_hashed_id8 := f_hashedId8FromSha384(f_hashWithSha384(v_msg));
                } else {
                  v_hashed_id8 := f_hashedId8FromSha256(f_hashWithSha256(v_msg));
                }
                infoPort.send(InfoPortData : { hashed_id8 := v_hashed_id8, at_certificate := v_inner_at_response.certificate });
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_01_BV

      /**
       * @desc Check that the AuthorizationRequest message is encrypted and sent to only one Authorization Authority.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT being in the "operational state"
       *             authorized with CERT_AA certificate
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                 containing content.encryptedData.recipients
       *                     indicating size 1
       *                     and containing the instance of RecipientInfo
       *                         containing certRecipInfo
       *                             containing recipientId
       *                                 indicating HashedId8 of the CERT_AA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_02_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_02_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      group f_TC_SECPKI_ITSS_AUTH_02_BV {

        function f_TC_SECPKI_ITSS_AUTH_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {

          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          var SECPKI_ITSS_TestData v_data;
          f_TC_SECPKI_ITSS_AUTH_pki(
                  v_data, -, -, -, -, -,
                  mw_http_request(
                                  mw_http_request_post(
                                                      PICS_HTTP_POST_URI_AT,
                                                      -,
                                                      mw_http_message_body_binary(
                                                                                    mw_binary_body_ieee1609dot2_data(
                                                                                                                    mw_authorizationRequestMessage(
                                                                                                                                                    mw_encryptedData(
                                                                                                                                                                    { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_aaHashedId8)), * },
                                                                                                                                                                    ?
                                                                                                                                                                    )))))),
                  ok
          );

          // Postamble
          f_cfHttpDown();
        } // end of function f_TC_SECPKI_ITSS_AUTH_02_BV_pki
      } // end of group f_TC_SECPKI_ITSS_AUTH_02_BV

      /**
       * @desc Check that the AuthorizationRequest message is encrypted using the encryptionKey found in the AA certificate referenced in recipientId.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *             authorized with AA certificate
       *                 containing encryptionKey (AA_ENC_PUB_KEY)
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                 containing content.encryptedData
       *                     containing ciphertext
       *                         containing data
       *                             encrypted using AA_ENC_PUB_KEY
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_03_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_03_BV_pki_check_encryptedData),
                                                    refers(f_TC_SECPKI_ITSS_AUTH_03_BV_pki_check_signedData)));
        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup

      } // End of testcase TC_SECPKI_ITSS_AUTH_03_BV
      
      group f_TC_SECPKI_ITSS_AUTH_03_BV {

        function f_TC_SECPKI_ITSS_AUTH_03_BV_pki_check_encryptedData (inout SECPKI_ITSS_TestData p_data, in Ieee1609Dot2Data p_value) runs on ItsPkiHttp return boolean {
          var template (present) EtsiTs103097Data mw :=
                m_etsiTs103097Data_encrypted(
                                             mw_encryptedData(
                                                              {
                                                                [0] := mw_recipientInfo_certRecipInfo(
                                                                                                      mw_pKRecipientInfo(
                                                                                                                          vc_aaHashedId8
                                                                                                      )
                                                                )
                                                              }
                                             )
                );
/*
                                Ieee1609Dot2Data :  {
                                                      protocolVersion := ?,
                                                      content := {
                                                                  encryptedData := {
                                                                                    recipients := {
                                                                                                  [0] := {
                                                                                                          certRecipInfo := {
                                                                                                                            recipientId := vc_aaHashedId8,
                                                                                                                            encKey := ?
                                                                                                          }
                                                                                                  }
                                                                                    },
                                                                                    ciphertext := ?
                                                                  }
                                                      }
                                };
*/                                
          if(not match (p_value, mw)){
            log("*** " & testcasename() & ": FAIL: Not an encrypted message or wrong encryption target used (not an AA digest) ***");
            log(match(p_value, mw));
            return false;
          }
          return true;
        } // end of function f_TC_SECPKI_ITSS_AUTH_03_BV_pki_check_encryptedData
        function f_TC_SECPKI_ITSS_AUTH_03_BV_pki_check_signedData ( inout SECPKI_ITSS_TestData p_data, in Ieee1609Dot2Data p_value) runs on ItsPkiHttp return boolean {
          // need for this function to check an actual decryption
          return true;
        }
      } // end of group f_TC_SECPKI_ITSS_AUTH_03_BV

      /**
       * @desc Check that the AuthorizationRequest message is never reused the same encryption key and nonce.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT being in the "operational state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                 containing content.encryptedData
       *                     containing ciphertext.aes128Ccm.nonce
       *                         indicating value not equal to the nonce in N previous messages
       *                 and containing recipients[0].certRecipInfo.encKey
       *                     containing encrypted symmetric key (S_KEY)
       *                         indicating symmetric key not equal to the key was used in N previous messages
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_04_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_aa;
        var SECPKI_ITSS_TestData v_data := SECPKI_ITSS_TestData_init_value;
        v_data.anyData.tc04 := {{},{}};

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss(PX_RE_AUTHORIZATION_COUNTER));
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(v_data,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_04_BV_pki_check_encryptedData),
                                                    refers(f_TC_SECPKI_ITSS_AUTH_04_BV_pki_check_signedData),
                                                    null,
                                                    PX_RE_AUTHORIZATION_COUNTER
        ));
        f_serverSyncClientsTimed (2, c_prDone, PX_TSYNC_TIME_LIMIT );

        for (var integer v_i := 1; v_i < PX_RE_AUTHORIZATION_COUNTER; v_i := v_i + 1) {
          f_serverSyncClientsTimed (2, c_nextTry, PX_TSYNC_TIME_LIMIT );
        }
        
        f_serverSyncClientsTimed (2, c_tbDone, PX_TSYNC_TIME_LIMIT );
        f_serverWaitForAllClientsToStop();

      } // End of testcase TC_SECPKI_ITSS_AUTH_04_BV

      group f_TC_SECPKI_ITSS_AUTH_04_BV {

        function f_TC_SECPKI_ITSS_AUTH_04_BV_pki_check_encryptedData (inout SECPKI_ITSS_TestData p_data,  in Ieee1609Dot2Data p_value) runs on ItsPkiHttp return boolean {
          if( not isbound (p_value.content.encryptedData.ciphertext.aes128ccm.nonce)){
            log("*** " & testcasename() & ": FAIL: At step " & int2str(p_data.counter) &": nonce not found in a message ***");
            return false;
          }
          var octetstring v_nonce := p_value.content.encryptedData.ciphertext.aes128ccm.nonce;
          if (isbound(p_data.anyData.tc04.nonces) and match(p_data.anyData.tc04.nonces, superset(v_nonce))) {
            log("*** " & testcasename() & ": FAIL: At step " & int2str(p_data.counter) & " duplicate nonce found ***");
            log("    nonce is: ", v_nonce);
            return false;
          }
          log("*** " & testcasename() & ": LOG: At step " & int2str(p_data.counter) &" nonces are unique ***");
          p_data.anyData.tc04.nonces[p_data.counter] := v_nonce;
          return true;
        }
        function f_TC_SECPKI_ITSS_AUTH_04_BV_pki_check_signedData (inout SECPKI_ITSS_TestData p_data,  in Ieee1609Dot2Data p_value) runs on ItsPkiHttp return boolean {
          if (isbound(p_data.anyData.tc04.symmKeys) and match(p_data.anyData.tc04.symmKeys, superset(p_data.aesKey))) {
            log("*** " & testcasename() & ": FAIL: At step " & int2str(p_data.counter) &": duplicate symmetric encryption keys found ***");
            log("    key is: ", p_data.aesKey);
            return false;
          }
          log("*** " & testcasename() & ": LOG: At step " & int2str(p_data.counter) &": symmetric encryption keys are unique ***");
          p_data.anyData.tc04.symmKeys[p_data.counter] := p_data.aesKey;
          return true;
        }
      } // End of group f_TC_SECPKI_ITSS_AUTH_04_BV

      /**
       * @desc Check that the Authozation request protocol version is set to 1.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT being in the "operational state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to requested a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                 containing version
       *                     indicating value 1
       *                 and containing content
       *                     containing autihorizationRequest
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_05_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_05_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_05_BV_pki_check_authRequest)));

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_05_BV

      group f_TC_SECPKI_ITSS_AUTH_05_BV {

        function f_TC_SECPKI_ITSS_AUTH_05_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          if(not match (p_value, mw_etsiTs102941Data_authorization_request)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid message received ***");
            return false;
          }
          return true;
        }
      } // End of group f_TC_SECPKI_ITSS_AUTH_05_BV_pki

      /**
       * @desc Check that for each authorization request the ITS-S generates a new verification key pair
       *       Check that for each authorization request the ITS-S generates a new encryption key pair
       *       Check that for each authorization request the ITS-S generates a new hmac-key
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         the IUT being in the "operational state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
	     *	            containing EtsiTs102941Data
	     *		            containing authorizationRequest
	     *			            containing publicKeys
       *                    containing verificationKey
	     *					            indicating value not equal to the field verificationKey of N previous messages
	     *				            and not containing encryptionKey
	     *				            or containing encryptionKey
	     *					            indicating value not equal to the field encryptionKey of N previous messages
	     *			            and containing hmacKey
	     *				            indicating value not equal to the field hmacKey of N previous messages
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_06_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_06_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_aa;
        var SECPKI_ITSS_TestData v_data := SECPKI_ITSS_TestData_init_value;
        v_data.anyData.tc06 := {{},{},{}};

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss(PX_RE_AUTHORIZATION_COUNTER));
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(v_data, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_05_BV_pki_check_authRequest),
                                                    PX_RE_AUTHORIZATION_COUNTER
        ));
        f_serverSyncClientsTimed ( 2, c_prDone, PX_TSYNC_TIME_LIMIT );

        for (var integer v_i := 1; v_i < PX_RE_AUTHORIZATION_COUNTER; v_i := v_i + 1) {
          f_serverSyncClientsTimed ( 2, c_nextTry, PX_TSYNC_TIME_LIMIT );
        }
        
        f_serverSyncClientsTimed ( 2, c_tbDone, PX_TSYNC_TIME_LIMIT );
        f_serverWaitForAllClientsToStop();

      } // End of testcase TC_SECPKI_ITSS_AUTH_06_BV

      group f_TC_SECPKI_ITSS_AUTH_06_BV {

        function f_TC_SECPKI_ITSS_AUTH_06_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          if (not isvalue(p_value.content.authorizationRequest)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid message received ***");
            return false;
          }
          if (isbound(p_data.anyData.tc06.vKeys) and match(p_data.anyData.tc06.vKeys, superset(p_value.content.authorizationRequest.publicKeys.verificationKey))) {
            log("*** " & testcasename() & "_pki: FAIL: At step " & int2str(p_data.counter) & " duplicate verification key found ***");
            return false;
          }
          log("*** " & testcasename() & "_pki: LOG: At step " & int2str(p_data.counter) &" verification keys are unique ***");
          p_data.anyData.tc06.vKeys[p_data.counter] := p_value.content.authorizationRequest.publicKeys.verificationKey;
          if( ispresent (p_value.content.authorizationRequest.publicKeys.encryptionKey)){
            if (isbound(p_data.anyData.tc06.eKeys) and match(p_data.anyData.tc06.eKeys, superset(p_value.content.authorizationRequest.publicKeys.encryptionKey.publicKey))) {
              log("*** " & testcasename() & "_pki: FAIL: At step " & int2str(p_data.counter) & " duplicate encryption key found ***");
              return false;
            log("*** " & testcasename() & "_pki: LOG: At step " & int2str(p_data.counter) &" encryption keys are unique ***");
            p_data.anyData.tc06.eKeys[p_data.counter] := p_value.content.authorizationRequest.publicKeys.encryptionKey.publicKey;
          if (isbound(p_data.anyData.tc06.hmacKeys) and match(p_data.anyData.tc06.hmacKeys, superset(p_value.content.authorizationRequest.hmacKey))) {
            log("*** " & testcasename() & "_pki: FAIL: At step " & int2str(p_data.counter) & " duplicate HMAC key found ***");
            return false;
          }
          log("*** " & testcasename() & "_pki: LOG: At step " & int2str(p_data.counter) &" HMAC keys are unique ***");
          p_data.anyData.tc06.hmacKeys[p_data.counter] := p_value.content.authorizationRequest.hmacKey;
      } // End of group f_TC_SECPKI_ITSS_AUTH_06_BV

      /**
       * @desc Check that ITS-S sends Authorization request with a keyTag field computed as described in ETSI TS 102 941 [1], clause 6.2.3.3.1
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         the IUT being in the "operational state"
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request a new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	            containing EtsiTs102941Data
       *		            containing authorizationRequest
       *			            containing sharedAtRequest
       *                    containing keyTag
       *					            indicating properly calculated value
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_07_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_07_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_07_BV_pki_check_authRequest))
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_07_BV

      group f_TC_SECPKI_ITSS_AUTH_07_BV {

        function f_TC_SECPKI_ITSS_AUTH_07_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          // calculate keyTag
          
          var octetstring v_encoded_tag;
          var octetstring v_key_tag;

          v_encoded_tag := bit2oct(encvalue(p_value.content.authorizationRequest.publicKeys.verificationKey));
          if (ispresent(p_value.content.authorizationRequest.publicKeys.encryptionKey)) {
            v_encoded_tag := v_encoded_tag & bit2oct(encvalue(p_value.content.authorizationRequest.publicKeys.encryptionKey));
          }
          v_key_tag := substr(
                            fx_hmac_sha256( // TODO Rename and use a wrapper function
                                            p_value.content.authorizationRequest.hmacKey,
                                            v_encoded_tag
                                            ),
                            0,
                            16); // Leftmost 128 bits of the HMAC-SHA256 tag computed previously

          var template (present) EtsiTs102941Data mw := 
                        mw_etsiTs102941Data_authorization_request(
                                                                  mw_innerAtRequest(
                                                                                    -,
                                                                                    -,
                                                                                    mw_shared_at_request(
                                                                                                          -,
                                                                                                          v_key_tag,
                                                                                                          -
                                                                                    ),
                                                                                    -
                                                                  )
                        );
          if(not match (p_value, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Wrong keyTag value ***");
            log(match (p_value, mw));
            return false;
          }
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_07_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Test component configuration
          var SECPKI_ITSS_TestData v_data;
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_07_BV_pki_check_authRequest));
          f_cfHttpDown();
        }
*/        
      } // End of group f_TC_SECPKI_ITSS_AUTH_07_BV_pki

      /**
       * @desc Check that ITS-S sends Authorization request with eaId of EA certificate
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT is enrolled by the EC, signed with the CERT_EA certificate
       *         the IUT being in the "operational" state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	                containing EtsiTs102941Data
       *		                 containing authorizationRequest
       *			                  containing sharedAtRequest
       *                          containing eaId
       *					                  indicating HashedId8 of CERT_EA certificate
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_08_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_08_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss()); // force enrolment
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_08_BV_pki_check_authRequest))
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup

      } // End of testcase TC_SECPKI_ITSS_AUTH_08_BV

      group f_TC_SECPKI_ITSS_AUTH_08_BV {
        
        function f_TC_SECPKI_ITSS_AUTH_08_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          var template (present) EtsiTs102941Data mw := 
                        mw_etsiTs102941Data_authorization_request(
                                                                  mw_innerAtRequest(
                                                                                    -,
                                                                                    -,
                                                                                    mw_shared_at_request(vc_eaHashedId8)
                                                                  )
                        );
          if(not match (p_value, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid message received ***");
            log(match(p_value, mw));
            return false;
          }
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_08_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_08_BV_pki_check_authRequest));
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_08_BV_pki
      } // End of group f_TC_SECPKI_ITSS_AUTH_08_BV  

      /**
       * @desc Check that ITS-S sends Authorization request with the certificateFormat equal to 1
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	                containing EtsiTs102941Data
       *		                 containing authorizationRequest
       *			                  containing sharedAtRequest
       *                          containing certificateFormat
       *					                     indicating 1
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_09_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_09_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_09_BV_pki_check_authRequest))
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      } // End of testcase TC_SECPKI_ITSS_AUTH_09_BV
        function f_TC_SECPKI_ITSS_AUTH_09_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          if(not match (p_value, mw_etsiTs102941Data_authorization_request)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid message received ***");
            log(match(p_value, mw_etsiTs102941Data_authorization_request));
            return false;
      } // End of group f_TC_SECPKI_ITSS_AUTH_09_BV  

      /**
       * @desc Check that ITS-S sends Authorization request certificate attributes are properly set
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	              containing EtsiTs102941Data
       *		               containing authorizationRequest
       *			                containing sharedAtRequest
       *                          containing requestedSubjectAttributes
       *					                   containing appPermissions
       *                             and not containing certIssuePermissions
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_10_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_10_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_10_BV_pki_check_authRequest))
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup

      } // End of testcase TC_SECPKI_ITSS_AUTH_08_BV

      group f_TC_SECPKI_ITSS_AUTH_10_BV {

        function f_TC_SECPKI_ITSS_AUTH_10_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var template (present) EtsiTs102941Data mw := 
                        mw_etsiTs102941Data_authorization_request(
                                                                  mw_innerAtRequest( -, -,
                                                                                    mw_shared_at_request( -, -, 
                                                                                                          mw_certificate_subject_attributes
                                                                                    )
                                                                  )
                        );
  
          if(not match (p_value, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid message received ***");
            log(match (p_value, mw));
            return false;
          }
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_10_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_10_BV_pki_check_authRequest));
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_10_BV_pki
       * @desc Check that ITS-S sends Authorization request containing EC signature calculated over the
       *       sharedATRequest using supported hash algorithm
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	                containing EtsiTs102941Data
       *		                 containing authorizationRequest
       *			                  containing ecSignature
       *                           containing structure of type EtsiTs103097Data-SignedExternalPayload
       *                                  containing payload
       *                                     containing extDataHash
       *                                        indicating supported hash algorithm 
			 *                                        and indicating hash of sharedATRequest
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_11_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
       testcase TC_SECPKI_ITSS_AUTH_11_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_11_BV_pki_check_authRequest)));

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup

      } // End of testcase TC_SECPKI_ITSS_AUTH_11_BV
      
      group f_TC_SECPKI_ITSS_AUTH_11_BV {
   
        function f_TC_SECPKI_ITSS_AUTH_11_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          if(ischosen(p_value.content.authorizationRequest.ecSignature.encryptedEcSignature)){
            // contains encrypted signature
            if( not f_decrypt(vc_eaPrivateEncKey, // EA private encryption key
                              p_value.content.authorizationRequest.ecSignature.encryptedEcSignature,
                               vc_eaWholeHash, // salt
                               v_ecSignature, // decrypted message
                               v_aes_enc_key)) {
              return false;
            v_ecSignature := p_value.content.authorizationRequest.ecSignature.ecSignature;
          
          var template (present) EtsiTs103097Data mw := 
                        mw_etsiTs103097Data_signed(
                                                   mw_signedData( -,
                                                                  mw_toBeSignedData(
                                                                                    mw_signedDataPayload_ext(mw_etsiTs103097SupportedAlgHashedData)
                                                                  )
                                                   )
                        );

          if(not match (v_ecSignature, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Wrong ecSignature format ***");
            log(match(v_ecSignature, mw));
            return false;
          }
          var HashedData v_extDataHash := v_ecSignature.content.signedData.tbsData.payload.extDataHash;
          // check that the hash is calculated well
          var template (value) HashedData mw_hashedData;
          if(ischosen(v_extDataHash.sha256HashedData)){
            mw_hashedData.sha256HashedData := f_hashWithSha256(bit2oct(encvalue(p_value.content.authorizationRequest.sharedAtRequest)));
          }else if(ischosen(v_extDataHash.sha384HashedData)){
            mw_hashedData.sha384HashedData := f_hashWithSha384(bit2oct(encvalue(p_value.content.authorizationRequest.sharedAtRequest)));
          }else{
            return false;
          }
          if( not match(v_extDataHash, mw_hashedData)){
            log("*** " & testcasename() & "_pki: FAIL: SharedAtRequest hash mismatch ***");
            log(match(v_extDataHash, mw_hashedData));
            return false;
          }
          
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_11_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Test component configuration
          var SECPKI_ITSS_TestData v_data;
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_11_BV_pki_check_authRequest));
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_10_BV_pki
*/
      } // End of group f_TC_SECPKI_ITSS_AUTH_11_BV 

       * @desc Check that the ecSignature psid is set to the proper ITS_AID
       *       Check that the ecSignature generation time is present
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	                containing EtsiTs102941Data
       *		                 containing authorizationRequest
       *			                  containing ecSignature
       *                            containing structure of type EtsiTs103097Data-SignedExternalPayload
       *                                containing tbsData
       *                                    containing headerInfo
       *                                        containing psid
       *                                            indicating AID_PKI_CERT_REQUEST
       *                                         and containing generationTime
       *					                               and not containing any other headers
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_12_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_12_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_12_BV_pki_check_authRequest))
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      group f_TC_SECPKI_ITSS_AUTH_12_BV {

        function f_TC_SECPKI_ITSS_AUTH_12_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          if(ischosen(p_value.content.authorizationRequest.ecSignature.encryptedEcSignature)){
            // contains encrypted signature
            if( not f_decrypt(vc_eaPrivateEncKey, // EA private encryption key
                              p_value.content.authorizationRequest.ecSignature.encryptedEcSignature,
                               vc_eaWholeHash, // salt
                               v_ecSignature, // decrypted message
                               v_aes_enc_key)) {
              return false;
            }
          } else {
            v_ecSignature := p_value.content.authorizationRequest.ecSignature.ecSignature;
          }
          var template (present) EtsiTs103097Data mw := 
                        mw_etsiTs103097Data_signed(
                                                   mw_signedData( -,
                                                                  mw_toBeSignedData( -,
                                                                                     mw_headerInfo_ecSignature
                                                                  )
                                                   )
                        );
          if(not match (v_ecSignature, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid header info in ecSignature received ***");
            log(match (v_ecSignature, mw));
            return false;
          }
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_12_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_12_BV_pki_check_authRequest));
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_10_BV_pki
*/
      } // End of group f_TC_SECPKI_ITSS_AUTH_11_BV 

      /**
       * @desc Check that ITS-S sends Authorization request containing EC signature
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the AA
       *	                containing EtsiTs102941Data
       *		                 containing authorizationRequest
       *			                  containing ecSignature
       *                           containing structure of type EtsiTs103097Data-SignedExternalPayload
       *                              containing hashId
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_13_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
       testcase TC_SECPKI_ITSS_AUTH_13_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null, refers(f_TC_SECPKI_ITSS_AUTH_13_BV_pki_check_authRequest)));

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_13_BV

      group f_TC_SECPKI_ITSS_AUTH_13_BV {
        function f_TC_SECPKI_ITSS_AUTH_13_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          if(ischosen(p_value.content.authorizationRequest.ecSignature.encryptedEcSignature)){
            // contains encrypted signature
            if( not f_decrypt(vc_eaPrivateEncKey, // EA private encryption key
                              p_value.content.authorizationRequest.ecSignature.encryptedEcSignature,
                               vc_eaWholeHash, // salt
                               v_ecSignature, // decrypted message
                               v_aes_enc_key)) {
              return false;
            }
          } else {
            v_ecSignature := p_value.content.authorizationRequest.ecSignature.ecSignature;
          }

          var template (present) EtsiTs103097Data mw := mw_etsiTs103097Data_signed( mw_signedData( mw_validEtsiTs103097HashAlgorithm) );

          if(not match (v_ecSignature, mw)) {
            log("*** " & testcasename() & "_pki: FAIL: Unsupported ecSignature hash algorithm ***");
            log(match (v_ecSignature, mw));
            return false;
          }
          return true;
        }
/*
        function f_TC_SECPKI_ITSS_AUTH_13_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {

          // Test component configuration
          var SECPKI_ITSS_TestData v_data;
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
          f_TC_SECPKI_ITSS_AUTH_pki(v_data, null, null,
                                    refers(f_TC_SECPKI_ITSS_AUTH_13_BV_pki_check_authRequest));
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_10_BV_pki
*/
      } // End of group f_TC_SECPKI_ITSS_AUTH_11_BV 

      /**
       * @desc Check that the ecSignature of the Authorization request is signed with EC certificate
       *        Check that the signature over tbsData computed using the private key corresponding to
       *        the EC's verification public key.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION
       * Initial conditions: 
       *     with {
       *         the IUT is enrolled with CERT_EC certificate
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                containing EtsiTs102941Data
       *                  containing authorizationRequest
       *                    containing ecSignature
       *                      containing structure of type EtsiTs103097Data-SignedExternalPayload
       *                        containing signer
       *                          indicating HashedId8 of the CERT_EC certificate
       *                        containing signature
       *                          indicating signature over sharedATRequest calculated with CERT_EC verificationKey 
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_14_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_14_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss(1, true)); // force enrollment
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_14_BV_pki_check_authRequest),
                                                    -, true // force enrollment
                                                    )
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      }  // End of testcase TC_SECPKI_ITSS_AUTH_14_BV

      group f_TC_SECPKI_ITSS_AUTH_14_BV {
        function f_TC_SECPKI_ITSS_AUTH_14_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          if(ischosen(p_value.content.authorizationRequest.ecSignature.encryptedEcSignature)){
            // contains encrypted signature
            if( not f_decrypt(vc_eaPrivateEncKey, // EA private encryption key
                              p_value.content.authorizationRequest.ecSignature.encryptedEcSignature,
                               vc_eaWholeHash, // salt
                               v_ecSignature, // decrypted message
                               v_aes_enc_key)) {
              return false;
            v_ecSignature := p_value.content.authorizationRequest.ecSignature.ecSignature;
          }
          var template (present) EtsiTs103097Data mw := mw_etsiTs103097Data_signed(
                                                                                    mw_signedData(-, -, 
                                                                                                  mw_signerIdentifier_digest( (all from(vc_ec_hashed_id8)) )
                                                                                  )
                                                        );
          if(not match (v_ecSignature, mw)){
            log("*** " & testcasename() & "_pki: FAIL: EC digest mismatch.***");
            log(match (v_ecSignature, mw));
            return false;
          // check signature
          for(var integer v_i := 0; v_i < vc_ec_counter; v_i := v_i + 1){
            if(vc_ec_hashed_id8[v_i] == v_ecSignature.content.signedData.signer.digest){
              var EtsiTs103097Certificate v_ec_cert := vc_ec_certificates[v_i];
              if(not f_verifySignedMessageECDSA(v_ecSignature, vc_ec_certificates[v_i])){
                log("*** " & testcasename() & "_pki: FAIL: EC signature verification failed.***");
                return false;
      /**
       * @desc Check that the encrypted ecSignature of the Authorization request is encrypted using the EA encryptionKey
       *        Check that the encrypted ecSignature of the Authorization request was done from the 
       *          EtsiTs103097Data-SignedExternalPayload structure
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE, PICS_SECPKI_AUTHORIZATION and PICS_SECPKI_AUTH_PRIVACY
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *         and the EA in 'operational' state
       *         authorized with CERT_EA certificate
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                containing EtsiTs102941Data
       *                  containing authorizationRequest
       *                    containing ecSignature
       *                      containing encryptedEcSignature
       *                        containing recipients
       *                        containing only one element of type RecipientInfo
       *                          containing certRecipInfo
       *                            containing recipientId
       *                              indicating HashedId8 of the CERT_EA
       *                            and containing encKey
       *                              indicating encryption key of supported type
       *                       and containing cyphertext
       *                        containing encrypted representation of structure EtsiTs103097Data-SignedExternalPayload
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_15_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_15_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTH_PRIVACY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE, PICS_SECPKI_AUTHORIZATION are required for executing the TC ***");
          setverdict(inconc);
          stop;
        }
        if (not PICS_SECPKI_AUTH_PRIVACY) {
          log("*** " & testcasename() & ": PICS_SECPKI_AUTH_PRIVACY is required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_15_BV_pki_check_authRequest)
                                                   )
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      }

      group f_TC_SECPKI_ITSS_AUTH_15_BV {

        function f_TC_SECPKI_ITSS_AUTH_15_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          template (present) EcSignature mw1 := 
              mw_ec_signature ( // strange, but this is a signature with privace template
                                mw_etsiTs103097Data_encrypted(
                                                              mw_encryptedData(
                                                                                { 
                                                                                  mw_recipientInfo_certRecipInfo( 
                                                                                                                  mw_pKRecipientInfo(vc_eaHashedId8)
                                                                                  )
                                                                                },
                                                                                mw_symmetricCiphertext_aes128ccm
                                                              )
                                )
              );
          if(not match(p_value.content.authorizationRequest.ecSignature, mw1)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid EC signature with mandatory privacy.***");
            log(match(p_value.content.authorizationRequest.ecSignature, mw1));
            return false;
          if( not f_decrypt(vc_eaPrivateEncKey, // EA private encryption key
                            p_value.content.authorizationRequest.ecSignature.encryptedEcSignature,
                            vc_eaWholeHash, // salt
                            v_ecSignature, // decrypted message
                            v_aes_enc_key)) {
            return false;
          }
          var template (present) EtsiTs103097Data mw2 := 
                        mw_etsiTs103097Data_signed(
                                                   mw_signedData( -,
                                                                  mw_toBeSignedData(
                                                                                    mw_signedDataPayload_ext(mw_etsiTs103097SupportedAlgHashedData)
                                                                  )
                                                   )
                        );
          if(not match (v_ecSignature, mw2)){
            log("*** " & testcasename() & "_pki: FAIL: Wrong ecSignature format ***");
            log(match(v_ecSignature, mw2));
            return false;
          }
          return true;
        }

      } // End of group f_TC_SECPKI_ITSS_AUTH_15_BV

      /**
       * @desc Check that the ecSignature of the Authorization request is not encrypted
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE, PICS_SECPKI_AUTHORIZATION and not PICS_SECPKI_AUTH_PRIVACY
       * Initial conditions: 
       *     with {
       *         the IUT in 'enrolled' state
       *         and the AA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request new Authorization Ticket (AT)
       *         }
       *         then {
       *             the IUT sends EtsiTs103097Data to the AA
       *                containing EtsiTs102941Data
       *                  containing authorizationRequest
       *                    containing ecSignature
       *                      containing ecSignature
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_AUTH_16_BV
       * @reference ETSI TS 102 941 [2], clause 6.2.3.3.1
       */
      testcase TC_SECPKI_ITSS_AUTH_16_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }
        if (PICS_SECPKI_AUTH_PRIVACY) {
          log("*** " & testcasename() & ": PICS_SECPKI_AUTH_PRIVACY should not be set for executing the TC ***");
          setverdict(inconc);
          stop;
        }
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_itss());
        v_aa.start(f_TC_SECPKI_ITSS_AUTH_pki_simple(-, null, null,
                                                    refers(f_TC_SECPKI_ITSS_AUTH_16_BV_pki_check_authRequest)
                                                   )
        );

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
      }  // End of testcase TC_SECPKI_ITSS_AUTH_16_BV

      group f_TC_SECPKI_ITSS_AUTH_16_BV {

        function f_TC_SECPKI_ITSS_AUTH_16_BV_pki_check_authRequest (inout SECPKI_ITSS_TestData p_data, in EtsiTs102941Data p_value) runs on ItsPkiHttp return boolean {
          
          var EtsiTs103097Data v_ecSignature;
          var octetstring      v_aes_enc_key;

          template (present) EcSignature mw := 
              mw_ec_signature_ext_payload ( 
                                          mw_etsiTs103097Data_signed(
                                                                    mw_signedData( -,
                                                                                    mw_toBeSignedData(
                                                                                                      mw_signedDataPayload_ext(mw_etsiTs103097SupportedAlgHashedData)
                                                                                    )
                                                                    )
                                          )
          );
          if(not match(p_value.content.authorizationRequest.ecSignature, mw)){
            log("*** " & testcasename() & "_pki: FAIL: Invalid EC signature in no-privacy mode.***");
            log(match(p_value.content.authorizationRequest.ecSignature, mw));
            return false;

      } // End of group f_TC_SECPKI_ITSS_AUTH_16_BV

    } // End of group itss_authorization_request

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.3.2  Authorization response handling
    group itss_authorization_response {
      // Void
    } // End of group itss_authorization_response

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.3.3  Authorization request repetition
    group itss_authorization_request_repetition {

      /**
       * @desc Check that IUT repeats an authorization request when response has not been received
       * <pre>
       * Pics Selection: PICS_SECPKI_AUTHORIZATION_RETRY
       * Initial conditions: {
       *     the IUT being in the 'enrolled' state
       *     and the IUT already sent the Authorization Request at the time T1
       *     and the IUT has not yet received the Authorization Response
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT local time is reached the T1 + PIXIT_AUTH_TIMEOUT_TH1 
       *         }
       *         then {
       *             the IUT sends to EA an AuthorizationRequestMessage
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_AUTH_REP_01_BV
       * @reference ETSI TS 103 601, clause 5.2
       */
      testcase TC_SECPKI_ITSS_AUTH_REP_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTHORIZATION_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_REP_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_REP_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_REP_01_BV

      group f_TC_SECPKI_ITSS_AUTH_REP_01_BV {

        function f_TC_SECPKI_ITSS_AUTH_REP_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
            } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerAuthorizationRequestPrimitive();
          tc_ac.start;
          alt {
            [v_start_awaiting == true] a_await_cam_with_current_cert(
                                                                     v_info_port_data.at_certificate
            ) {
              log("*** " & testcasename() & ": PASS: IUT started to send CA message using new AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] geoNetworkingPort.receive { 
              log("*** " & testcasename() & ": FAIL: IUT started to send CA message using wrong AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] infoPort.receive(InfoPortData:?) -> value v_info_port_data {
              log("*** " & testcasename() & ": INFO: Received new AT certificate ***");
              v_start_awaiting := true;
              repeat;
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : PASS: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_01_BV_itss

        function f_TC_SECPKI_ITSS_AUTH_REP_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (PX_TRIGGER_EC_BEFORE_AT) {
            f_await_ec_request_send_error_response(v_request);
            log("*** " & testcasename() & ": INFO: Reply with 400 Bad Request error message ***");
          }
          // Wait for the repetition
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            v_inner_ec_response.certificate := omit;
          }

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_at_http_response_from_iut(
                                                 mw_http_request(
                                                                 mw_http_request_post(
                                                                                      PICS_HTTP_POST_URI_EC,
                                                                                      -,
                                                                                      mw_http_message_body_binary(
                                                                                                                  mw_binary_body_ieee1609dot2_data(
                                                                                                                                                   mw_enrolmentRequestMessage(
                                                                                                                                                                              mw_encryptedData(
                                                                                                                                                                                               { *, mw_recipientInfo_pskRecipInfo(vc_aaHashedId8), * },
                                                                                                                                                                                               mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                               )))))),
                                                 v_request
                                                 ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerAtRequest v_inner_at_request;
              var InnerAtResponse v_inner_at_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_at_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_response.certificate, v_inner_at_request, v_inner_at_response, v_response, v_result);
              // Send response
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              }
              // Set verdict
              if (v_result == 0) {
                var octetstring v_msg;
                var octetstring v_hashed_id8;

                log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                v_msg := bit2oct(encvalue(v_inner_at_response.certificate));
                if (ischosen(v_inner_at_response.certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaBrainpoolP384r1)) {
                  v_hashed_id8 := f_hashedId8FromSha384(f_hashWithSha384(v_msg));
                } else {
                  v_hashed_id8 := f_hashedId8FromSha256(f_hashWithSha256(v_msg));
                }
                infoPort.send(InfoPortData : { hashed_id8 := v_hashed_id8, at_certificate := v_inner_at_response.certificate });
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_REP_01_BV

      /**
       * @desc Check that IUT uses the same message to perform authorization retry
       * <pre>
       * Pics Selection: PICS_SECPKI_AUTHORIZATION_RETRY
       * Initial conditions: {
       *     the IUT being in the 'enrolled' state
       *     and the IUT already sent the Authorization Request at the time T1
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to re-send an AuthorizationRequestMessage to AA
       *         }
       *         then {
       *             the IUT sends M to AA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_AUTH_REP_02_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_AUTH_REP_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTHORIZATION_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_REP_02_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_REP_02_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_REP_02_BV

      group f_TC_SECPKI_ITSS_AUTH_REP_02_BV {

        function f_TC_SECPKI_ITSS_AUTH_REP_02_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
            } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerAuthorizationRequestPrimitive();
          tc_ac.start;
          alt {
            [v_start_awaiting == true] a_await_cam_with_current_cert(
                                                                     v_info_port_data.at_certificate
            ) {
              log("*** " & testcasename() & ": PASS: IUT started to send CA message using new AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] geoNetworkingPort.receive { 
              log("*** " & testcasename() & ": FAIL: IUT started to send CA message using wrong AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] infoPort.receive(InfoPortData:?) -> value v_info_port_data {
              log("*** " & testcasename() & ": INFO: Received new AT certificate ***");
              v_start_awaiting := true;
              repeat;
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : PASS: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_02_BV_itss

        function f_TC_SECPKI_ITSS_AUTH_REP_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_initial_request;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (PX_TRIGGER_EC_BEFORE_AT) {
            f_await_ec_request_send_error_response(v_initial_request);
            log("*** " & testcasename() & ": INFO: Reply with 400 Bad Request error message ***");
          }
          // Wait for the repetition
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          }

          // Test Body
          if (f_verify_repeated_request(v_request, v_initial_request) == false) {
            log("*** " & testcasename() & ": FAIL: Repeatition request are different ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          } else {
            log("*** " & testcasename() & ": PASS: Repeatition request are different ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
          }
          // Postamble
          // Process the next request
          tc_ac.start;
          alt {
            [] a_await_at_http_response_from_iut(
                                                 mw_http_request(
                                                                 mw_http_request_post(
                                                                                      PICS_HTTP_POST_URI_EC,
                                                                                      -,
                                                                                      mw_http_message_body_binary(
                                                                                                                  mw_binary_body_ieee1609dot2_data(
                                                                                                                                                   mw_enrolmentRequestMessage(
                                                                                                                                                                              mw_encryptedData(
                                                                                                                                                                                               { *, mw_recipientInfo_pskRecipInfo(vc_aaHashedId8), * },
                                                                                                                                                                                               mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                               )))))),
                                                 v_request
                                                 ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerAtRequest v_inner_at_request;
              var InnerAtResponse v_inner_at_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_at_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_response.certificate, v_inner_at_request, v_inner_at_response, v_response, v_result);
              // Send response
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              }
              // Set verdict
              if (v_result == 0) {
                var octetstring v_msg;
                var octetstring v_hashed_id8;

                log("*** " & testcasename() & ": INFO: InnerEcRequest received ***");
                v_msg := bit2oct(encvalue(v_inner_at_response.certificate));
                if (ischosen(v_inner_at_response.certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaBrainpoolP384r1)) {
                  v_hashed_id8 := f_hashedId8FromSha384(f_hashWithSha384(v_msg));
                } else {
                  v_hashed_id8 := f_hashedId8FromSha256(f_hashWithSha256(v_msg));
                }
                infoPort.send(InfoPortData : { hashed_id8 := v_hashed_id8, at_certificate := v_inner_at_response.certificate });
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("*** " & testcasename() & ": INCONC: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_02_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_REP_02_BV

      /**
       * @desc Check that IUT stops sending the Authorization Request message if Authorization Response message has been received
       * <pre>
       * Pics Selection: PICS_SECPKI_AUTHORIZATION_RETRY
       * Initial conditions: {
       *     the IUT being in the 'enrolled' state
       *     and the IUT has sent the Authorization Request more than 1 time
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT receives an Authorization Response
       *         }
       *         then {
       *             the IUT stops sending Authorization Requests to AA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_AUTH_REP_03_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_AUTH_REP_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTHORIZATION_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_REP_03_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_REP_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_REP_03_BV

      group f_TC_SECPKI_ITSS_AUTH_REP_03_BV {

        function f_TC_SECPKI_ITSS_AUTH_REP_03_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
            } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerAuthorizationRequestPrimitive();
          tc_ac.start;
          alt {
            [v_start_awaiting == true] a_await_cam_with_current_cert(
                                                                     v_info_port_data.at_certificate
            ) {
              log("*** " & testcasename() & ": PASS: IUT started to send CA message using new AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] geoNetworkingPort.receive { 
              log("*** " & testcasename() & ": FAIL: IUT started to send CA message using wrong AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] infoPort.receive(InfoPortData:?) -> value v_info_port_data {
              log("*** " & testcasename() & ": INFO: Received new AT certificate ***");
              v_start_awaiting := true;
              repeat;
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : PASS: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_03_BV_itss

        function f_TC_SECPKI_ITSS_AUTH_REP_03_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_initial_request;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (PX_TRIGGER_EC_BEFORE_AT) {
            f_await_ec_request_send_error_response(v_initial_request);
            log("*** " & testcasename() & ": INFO: Reply with 400 Bad Request error message ***");
          }
          // Wait for the repetition
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          }

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_at_http_response_from_iut(
                                                 mw_http_request(
                                                                 mw_http_request_post(
                                                                                      PICS_HTTP_POST_URI_EC,
                                                                                      -,
                                                                                      mw_http_message_body_binary(
                                                                                                                  mw_binary_body_ieee1609dot2_data(
                                                                                                                                                   mw_enrolmentRequestMessage(
                                                                                                                                                                              mw_encryptedData(
                                                                                                                                                                                               { *, mw_recipientInfo_pskRecipInfo(vc_aaHashedId8), * },
                                                                                                                                                                                               mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                               )))))),
                                                 v_request
                                                 ) {
              var HttpMessage v_response;
              var integer v_result;
              var InnerAtRequest v_inner_at_request;
              var InnerAtResponse v_inner_at_response;

              tc_ac.stop;

              // Verify IUT response
              if (f_verify_repeated_request(v_request, v_initial_request) == false) {
                log("*** " & testcasename() & ": FAIL: Repeatition request are different ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              } else {
                f_verify_http_at_request_from_iut_itss(v_request.request, v_headers, v_inner_ec_response.certificate, v_inner_at_request, v_inner_at_response, v_response, v_result);
                // Send response
                if (isvalue(v_response)) {
                  httpPort.send(v_response);
                }
                // Set verdict
                if (v_result == 0) {
                  var octetstring v_msg;
                  var octetstring v_hashed_id8;

                  log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                  v_msg := bit2oct(encvalue(v_inner_at_response.certificate));
                  if (ischosen(v_inner_at_response.certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaBrainpoolP384r1)) {
                    v_hashed_id8 := f_hashedId8FromSha384(f_hashWithSha384(v_msg));
                  } else {
                    v_hashed_id8 := f_hashedId8FromSha256(f_hashWithSha256(v_msg));
                  }
                  infoPort.send(InfoPortData : { hashed_id8 := v_hashed_id8, at_certificate := v_inner_at_response.certificate });
                  f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
                } else {
                  log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                  f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
                }
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_03_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_REP_03_BV

      /**
       * @desc Check that IUT stops sending the Authorization Request message if maximum number of retry has been reached
       * <pre>
       * Pics Selection: PICS_SECPKI_AUTHORIZATION_RETRY
       * Initial conditions: {
       *     the IUT being in the 'enrolled' state
       *     and the IUT has sent the Authorization Request
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT sent the PIXIT_AUTH_MAX_N1 Authorization Request messages
       *         }
       *         then {
       *             the IUT stops sending Authorization Requests
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_AUTH_REP_04_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_AUTH_REP_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTHORIZATION_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_REP_04_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_REP_04_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_AUTH_REP_04_BV

      group f_TC_SECPKI_ITSS_AUTH_REP_04_BV {

        function f_TC_SECPKI_ITSS_AUTH_REP_04_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
            } // End of 'alt' statement

          // Test Body
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_04_BV_itss

        function f_TC_SECPKI_ITSS_AUTH_REP_04_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_initial_request;
          var HttpMessage v_request;
          var InnerEcRequest v_inner_ec_request;
          var InnerEcResponse v_inner_ec_response;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          for (var integer v_i := 0; v_i < PX_AUTH_MAX_N1; v_i := v_i + 1) {
            if (PX_TRIGGER_EC_BEFORE_AT) {
              f_await_ec_request_send_error_response(v_initial_request);
              log("*** " & testcasename() & ": INFO: Reply with 400 Bad Request error message ***");
            }
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          } // End of 'for' staement

          // Do not expect any repetition
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment not expected due to number of error ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            } else {
              log("*** " & testcasename() & ": INCONC: No more enrolment request done ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          } else {
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          }

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_04_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_REP_04_BV

      /**
       * @desc Check that IUT stops sending the Authorization Request message if timeout has been reached
       * <pre>
       * Pics Selection: PICS_SECPKI_AUTHORIZATION_RETRY
       * Initial conditions: {
       *     the IUT being in the 'enrolled' state
       *     and the IUT has started sending the Authorization Request at the time T1
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT local time is reached the T1 + PIXIT_AUTH_TIMEOUT_TH2
       *         }
       *         then {
       *             the IUT stops sending an Authorization Request messages
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_AUTH_REP_05_BV
       * @reference ETSI TS 103 601, clause 5.1.2
       */
      testcase TC_SECPKI_ITSS_AUTH_REP_05_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_AUTHORIZATION or not PICS_SECPKI_AUTHORIZATION_RETRY) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_AUTHORIZATION required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start component
        v_itss.start(f_TC_SECPKI_ITSS_AUTH_REP_05_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_AUTH_REP_05_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
      }

      group f_TC_SECPKI_ITSS_AUTH_REP_05_BV {

        function f_TC_SECPKI_ITSS_AUTH_REP_05_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
                f_selfOrClientSyncAndVerdict(c_prDone, e_success);
              }
            } // End of 'alt' statement

          // Test Body
          geoNetworkingPort.clear;
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_05_BV_itss

        function f_TC_SECPKI_ITSS_AUTH_REP_05_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var HttpMessage v_at_request;
          var InnerEcResponse v_inner_ec_response;
          var InnerEcRequest v_inner_ec_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(-, "inner_at_response", v_headers);
          if (PX_TRIGGER_EC_BEFORE_AT) {
            f_await_ec_request_send_error_response(v_request);
            log("*** " & testcasename() & ": INFO: Reply with 400 Bad Request error message ***");
          } else {
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          }
          
          // Do not expect any repetition
          if (PX_TRIGGER_EC_BEFORE_AT) {
            if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
              log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            } else {
              log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          }

          // Test Body
          tc_wait.start(PX_AT_REPETITION_TIMEOUT_TH2); // PX_AT_REPETITION_TIMEOUT
            [] a_await_at_http_request_from_iut(mw_http_at_request_generic, v_request) {
              log("*** " & testcasename() & ": FAIL: AT repetition has been received after a ", tc_wait.read, " ***"); //fails in  TC5
              tc_wait.stop;
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_wait.timeout {
              if (f_await_at_request_send_no_response(v_at_request) == true) {
                log("*** " & testcasename() & ": FAIL: AT repetition was received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              } else {
                log("*** " & testcasename() & ": PASS: AT repetition was not received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_AUTH_REP_05_BV_pki

      } // End of group f_TC_SECPKI_ITSS_AUTH_REP_05_BV

    } // End of group itss_authorization_request_repetition

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.3.4  Authorization using butterfly key expansion mechanism
    group itss_authorization_request_bfk {

      /**
       * @desc Check that the ITS-S send the EtsiTs103097Data to the Enrollment Authority (EA) to request a batch of authorization tickets
               Check that this message is encrypted and addressed to a single recipient.
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SEC_BFK_AUTH
       * Initial conditions: 
       *     with {
       *         the IUT being in the "initial state" and
       *         the EA in 'operational' state
       *             authorized with enrollment certificate CERT_IUT_A_EA
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request a new batch of authorization tickets
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the EA
       *                 containing content.encryptedData
       *                     containing recipients
       *                         indicating size 1
       *                         and containing the instance of RecipientInfo
       *                             containing certRecipInfo
       *                             containing recipientId
       *                             indicating HashedId8 of the CERT_IUT_A_EA
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_01_BV
       * @reference ETSI TS 102 941 [1], clause 6.2.3.5.1
       */
      testcase TC_SECPKI_ITSS_BFK_AUTH_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SEC_BFK_AUTH) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SEC_BFK_AUTH required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_BFK_AUTH_01_BV

      group f_TC_SECPKI_ITSS_BFK_AUTH_01_BV {

        function f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              tc_ac.start; // TDOD To refined, use altstep
              alt {
                [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
                  tc_ac.stop;
                  log("*** " & testcasename() & "_itss: INFO: IUT is in enrol state ***");
                }
                [] tc_ac.timeout {
                  log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved ***");
                  f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
                }
              } // End of 'alt' statement
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerAuthorizationRequestPrimitive(); // TODO FSCOM Add BFK mode
          tc_ac.start;
          alt {
            [v_start_awaiting == true] a_await_cam_with_current_cert(
                                                                     v_info_port_data.at_certificate
            ) {
              log("*** " & testcasename() & ": PASS: IUT started to send CA message using new AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] geoNetworkingPort.receive { 
              log("*** " & testcasename() & ": FAIL: IUT started to send CA message using wrong AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] infoPort.receive(InfoPortData:?) -> value v_info_port_data {
              log("*** " & testcasename() & ": INFO: Received new AT certificate ***");
              v_start_awaiting := true;
              repeat;
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : PASS: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_itss

        function f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcResponse v_inner_ec_response;
          var InnerEcRequest v_inner_ec_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
            log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          } else {
            log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
            f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
          }

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                     PICS_HTTP_POST_URI_BFK_AUTH,
                                                                                     -,
                                                                                     mw_http_message_body_binary(
                                                                                                                 mw_binary_body_ieee1609dot2_data(
                                                                                                                                                  mw_enrolmentRequestMessage(
                                                                                                                                                                            mw_encryptedData(
                                                                                                                                                                                             { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_eaHashedId8)), * },
                                                                                                                                                                                             mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                             )))))),
                                                v_request
                                                ) {
              var HttpMessage v_response;
              var integer v_result;
              var EeRaCertRequest v_bfk_authorization_request;
              var RaEeCertInfo v_bfk_authorization_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_bfk_authorization_request(v_request.request, v_headers, v_inner_ec_response.certificate, v_bfk_authorization_request, v_bfk_authorization_response, v_response, v_result);
              // Send response
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              } else { // Send HTTP error 500
                f_send_500_Internal_Error(v_headers);
              }
              // Set verdict
              if (v_result == 0) {
                log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_BFK_AUTH_01_BV

      /**
       * @desc Check that the ButterflyAuthorizationRequestMessage is signed using the EC certificate
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SEC_BFK_AUTH
       * Initial conditions: 
       *     with {
       *         the IUT being in the 'enrolled' state
       *             with certificate CERT_EC
       *                 issued by CA authorized with CERT_IUT_A_EA
       *         and the EA in 'operational' state
       *             authorized with enrollment certificate CERT_IUT_A_EA
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request a new batch of authorization tickets
       *         }
       *         then {
       *             the IUT sends a EtsiTs103097Data to the EA
       *                 containing content.encryptedData.cipherText
       *                     containing encrypted representation of EtsiTs103097Data
       *                         containing signedData
       *                             containing tbsData
       *                                 containing psid
       *                                     indicating AID_PKI_CERT_REQUEST
       *                                 and containing generationTime
       *                                 and not containing any other field
       *                                 and containing payload.data
       *                                     indicating EtsiTs102941Data
       *                                         containing version
       *                                             indicating ‘1’
       *                                         and containing content
       *                                             containing butterflyAuthorizationRequest
       *                                                 indicating EeRaCertRequest
       *                                 and containing signer
       *                                     containing digest
       *                                         indicating HashedId8 of the CERT_EC
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_02_BV
       * @reference ETSI TS 102 941 [1], clause 6.2.3.5.2
       */
      testcase TC_SECPKI_ITSS_BFK_AUTH_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SEC_BFK_AUTH) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SEC_BFK_AUTH required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_BFK_AUTH_02_BV

      /**
       * @desc Check that the ButterflyAuthorizationRequestMessage contains all required elements
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SEC_BFK_AUTH
       * Initial conditions: 
       *     with {
       *         the IUT being in the 'enrolled' state
       *         and the EA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request a new batch of authorization tickets (AT)
       *         }
       *         then {
       *             the IUT sends to the EA a EtsiTs103097Data
       *                 containing the EeRaCertRequest
       *                     containing version
       *                         indicating ‘2’
       *                     and containing generationTime
       *                         indicating current ITS timestamp
       *                     and containing certificateType
       *                         indicating ‘explicit”
       *                     and containing tbsCert
       *                         containing id
       *                             indicating ‘none’
       *                         and containing cracaId
       *                             indicating ‘000000’H
       *                         and containing crlSeries
       *                             indicating ‘0’
       *                     and containing additionalParams
       *                         containing original
       *                         or containing unified
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_03_BV
       * @reference ETSI TS 102 941 [1], clause 6.2.3.5.2
       */
      testcase TC_SECPKI_ITSS_BFK_AUTH_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SEC_BFK_AUTH) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SEC_BFK_AUTH required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_BFK_AUTH_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_BFK_AUTH_04_BV

      /**
       * @desc Check that the ButterflyAuthorizationRequestMessage contains newlly generated caterpillar public key
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SEC_BFK_AUTH
       * Initial conditions: 
       *     with {
       *         the IUT being in the 'authorized' state
       *         and the IUT already sent one or more Butterfly Authorization Requests
       *         and the EA in 'operational' state
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to request a new batch of authorization tickets (AT)
       *         }
       *         then {
       *             the IUT sends to the EA a EtsiTs103097Data
       *                 containing the EeRaCertRequest
       *                     containing version
       *                         indicating ‘2’
       *                     and containing generationTime
       *                         indicating current ITS timestamp
       *                     and containing certificateType
       *                         indicating ‘explicit”
       *                     and containing tbsCert
       *                         containing id
       *                             indicating ‘none’
       *                         and containing cracaId
       *                             indicating ‘000000’H
       *                         and containing crlSeries
       *                             indicating ‘0’
       *                     and containing additionalParams
       *                         containing original
       *                         or containing unified
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_ENR_03_BV
       * @reference ETSI TS 102 941 [1], clause 6.2.3.5.2
       */
      testcase TC_SECPKI_ITSS_BFK_AUTH_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SEC_BFK_AUTH) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SEC_BFK_AUTH required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_BFK_AUTH_04_BV

      group f_TC_SECPKI_ITSS_BFK_AUTH_04_BV {

        function f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var HashedId8               v_certificate_digest;
          var EtsiTs103097Certificate v_certificate;
          var InfoPortData            v_info_port_data;
          var boolean                 v_start_awaiting := false;

          // Test component configuration
          vc_hashedId8ToBeUsed := PX_IUT_DEFAULT_CERTIFICATE;
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("No CA message expected");
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              f_sendUtTriggerEnrolmentRequestPrimitive();
              tc_ac.start; // TDOD To refined, use altstep
              alt {
                [] utPort.receive(UtPkiTriggerInd: { state := 1 }) {
                  tc_ac.stop;
                  log("*** " & testcasename() & "_itss: INFO: IUT is in enrol state ***");
                }
                [] tc_ac.timeout {
                  log("*** " & testcasename() & "_itss: DBG: IUT state update not recieved ***");
                  f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
                }
              } // End of 'alt' statement
              log("*** " & testcasename() & "_itss: : INFO: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
          } // End of 'alt' statement

          // Test Body
          f_sendUtTriggerAuthorizationRequestPrimitive(); // TODO FSCOM Add BFK mode
          tc_ac.start;
          alt {
            [v_start_awaiting == true] a_await_cam_with_current_cert(
                                                                     v_info_port_data.at_certificate
            ) {
              log("*** " & testcasename() & ": PASS: IUT started to send CA message using new AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
            [] geoNetworkingPort.receive { 
              log("*** " & testcasename() & ": FAIL: IUT started to send CA message using wrong AT certificate ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] infoPort.receive(InfoPortData:?) -> value v_info_port_data {
              log("*** " & testcasename() & ": INFO: Received new AT certificate ***");
              v_start_awaiting := true;
              repeat;
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & "_itss: : PASS: No CA message received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_itss

        function f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
          var InnerEcResponse v_inner_ec_response;
          var InnerEcRequest v_inner_ec_request;

          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);

          // Test adapter configuration

          // Preamble
          if (f_await_ec_request_send_response(v_inner_ec_request, v_inner_ec_response, v_request) == true) {
            log("*** " & testcasename() & ": INFO: Enrolment succeed ***");
            f_selfOrClientSyncAndVerdict(c_prDone, e_success);
          } else {
            log("*** " & testcasename() & ": INCONC: Enrolment failed ***");
            f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
          }

          // Test Body
          tc_ac.start;
          alt {
            [] a_await_ec_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_post(
                                                                                     PICS_HTTP_POST_URI_EC,
                                                                                     -,
                                                                                     mw_http_message_body_binary(
                                                                                                                 mw_binary_body_ieee1609dot2_data(
                                                                                                                                                  mw_enrolmentRequestMessage(
                                                                                                                                                                            mw_encryptedData(
                                                                                                                                                                                             { *, mw_recipientInfo_certRecipInfo(mw_pKRecipientInfo(vc_eaHashedId8)), * },
                                                                                                                                                                                             mw_symmetricCiphertext_aes128ccm
                                                                                                                                                                                             )))))),
                                                v_request
                                                ) {
              var HttpMessage v_response;
              var integer v_result;
              var EeRaCertRequest v_bfk_authorization_request;
              var RaEeCertInfo v_bfk_authorization_response;

              tc_ac.stop;

              // Verify IUT response
              f_verify_http_bfk_authorization_request(v_request.request, v_headers, v_inner_ec_response.certificate, v_bfk_authorization_request, v_bfk_authorization_response, v_response, v_result);
              // Send response
              if (isvalue(v_response)) {
                httpPort.send(v_response);
              } else { // Send HTTP error 500
                f_send_500_Internal_Error(v_headers);
              }
              // Set verdict
              if (v_result == 0) {
                log("*** " & testcasename() & ": PASS: InnerEcRequest received ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
              } else {
                log("*** " & testcasename() & ": FAIL: Failed to verify EA an EnrolmentRequestMessage ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_BFK_AUTH_04_BV_pki

      } // End of group f_TC_SECPKI_ITSS_BFK_AUTH_04_BV

     /**
       * @desc Check that IUT downloads the AT certificates batch after receiving of positive ButterflyAuthorizationResponse message
       * <pre>
       * Pics Selection: PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT and PICS_SEC_BFK_AUTH
       * Initial conditions: 
       *     with {
       *         the IUT being in the 'enrolled' state
       *         and the EA in 'operational' state
       *         and the IUT has sent the ButterflyAuthorizationRequestMessage
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT receives an EtsiTs102941Data as an answer of the EA
       *                 containing butterflyAuthorizationResponse
       *                     indicating RaEeCertInfo
       *                         containing generationTime
       *                             indicating GEN_TIME
       *                         and containing currentI
       *                             indicating VALUE_I
       *                         and containing requestHash
       *                             indicating REQ_HASH
       *                         and containing nextDlTime
       *                             indicating time between GEN_TIME and current time 
       *         }
       *         then {
       *             the IUT send the ButterflyAtDownloadRequestMessage
       *                 containing butterflyAtDownloadRequest
       *                     indicating EeRaDownloadRequest
       *                         containing generationTime
       *                             indicating value more than GEN_TIME
       *                         and containing filename
       *                             indicating string REQ_HASH + “_” + VALUE_I + “.zip”
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 v2.0.1 SECPKI_ITSS_BFK_CERTDNL_01_BV
       * @reference ETSI TS 102 941 [1], clauses 6.2.3.5.3 and 6.2.3.5.6
       */
      testcase TC_SECPKI_ITSS_BFK_CERTDNL_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_ea;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_SECPKI_ENROLMENT or not PICS_SEC_BFK_AUTH) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_SECPKI_ENROLMENT or PICS_SEC_BFK_AUTH required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);

        // Start components
        v_itss.start(TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_itss());
        v_ea.start(TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);

      } // End of testcase TC_SECPKI_ITSS_BFK_CERTDNL_01_BV

      group f_TC_SECPKI_ITSS_BFK_CERTDNL_01_BV {

        function TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // FIXME TODO
        } // End of function TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_itss

        function TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // FIXME TODO
        } // End of function TC_SECPKI_ITSS_BFK_CERTDNL_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_BFK_CERTDNL_01_BV
7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220
    } // End of group itss_authorization_request_bfk

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.4  CTL handling
    group itss_ctl_handling {

      /**
       * @desc Check that the IUT trust the new RCA from the received ECTL
       * <pre>
       * Pics Selection:
       * Initial conditions: {
       *     the IUT does not trust the CERT_RCA_NEW
       *     the IUT has received the TLM CTL
       *         containing the CERT_RCA_NEW
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT received a CAM
       *                 signed with AT certificate
       *                     signed with AA certificate
       *                         signed with CERT_RCA_NEW
       *         }
       *         then {
       *             the IUT accepts this CAM
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTL_01_BV
       * @reference ETSI TS 102 941, clause 6.3.5
       */
      testcase TC_SECPKI_ITSS_CTL_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_cpoc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_cpoc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTL_01_BV_itss());
        v_cpoc.start(f_TC_SECPKI_ITSS_CTL_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_cpoc);

      } // End of testcase TC_SECPKI_ITSS_CTL_01_BV

      group f_TC_SECPKI_ITSS_CTL_01_BV {

        function f_TC_SECPKI_ITSS_CTL_01_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;
          var integer i;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerUpdateEctl(""); // FIXME Create PIXIT for ETCL URI
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          v_securedGnPdu := f_sendSecuredCam(cc_ectl_rca_new);
          // Check that the CAM message is forwarde to Facilies layer
          f_sleep(PX_TAC);
          for(i := 0; i < lengthof(vc_utInds) and not match(vc_utInds[i].rawPayload, valueof(v_securedGnPdu.gnPacket.packet.payload)); i := i + 1) {
            // empty on purpose 
          }
          if(i < lengthof(vc_utInds)) {
            log("*** " & testcasename() & ": PASS: CA message was transmitted to upper layer ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
          } else {
            log("*** " & testcasename() & ": FAIL: CA message was not transmitted to upper layer ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          }

          // Postamble
          f_cfDown_itss();

        } // End of function f_TC_SECPKI_ITSS_CTL_01_BV_itss

        function f_TC_SECPKI_ITSS_CTL_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_tlm();
        } // End of function f_TC_SECPKI_ITSS_CTL_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTL_01_BV

      /**
       * @desc Check that the IUT untrust the RCA when it is deleted from ECTL
       * <pre>
       * Pics Selection:
       * Initial conditions: {
       *     the IUT does not trust the CERT_RCA
       *     the IUT has received the TLM CTL
       *         not containing the CERT_RCA
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT received a CAM
       *                 signed with AT certificate
       *                     signed with AA certificate
       *                         signed with CERT_RCA
       *         }
       *         then {
       *             the IUT rejects this CAM
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTL_02_BV
       * @reference ETSI TS 102 941, clause 6.3.5
       */
      testcase TC_SECPKI_ITSS_CTL_02_BV() runs on ItsPkiItss system ItsPkiItssSystem {
        // Local variables
        var GeoNetworkingPdu v_securedGnPdu;
        var integer i;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfUp_itss();

        // Test adapter configuration

        // Preamble
        geoNetworkingPort.clear;
        tc_ac.start;
        alt {
          [] geoNetworkingPort.receive(
                                       mw_geoNwInd(
                                                   mw_geoNwSecPdu(
                                                                  mw_etsiTs103097Data_signed(
                                                                                             mw_signedData
                                                                                             )))) {
            tc_ac.stop;

            f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
          }
          [] tc_ac.timeout {
            log("*** " & testcasename() & ": INCONC: Expected message not received ***");
            f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
          }
        } // End of 'alt' statement

        // Test Body
        v_securedGnPdu := f_sendSecuredCam(cc_ectl_rca_untrust);
        // Check that the CAM message is forwarde to Facilies layer
        f_sleep(PX_TAC);
        for(i := 0; i < lengthof(vc_utInds) and not match(vc_utInds[i].rawPayload, valueof(v_securedGnPdu.gnPacket.packet.payload)); i := i + 1) {
          // empty on purpose 
        }
        if(i < lengthof(vc_utInds)) {
          log("*** " & testcasename() & ": FAIL: CA message was not transmitted to upper layer ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
        } else {
          log("*** " & testcasename() & ": PASS: CA message was transmitted to upper layer ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
        }

        // Postamble
        f_cfDown_itss();

      } // End of testcase TC_SECPKI_ITSS_CTL_02_BV

      /**
       * @desc Check that the IUT trust the AA when it is received in RCA CTL
       * <pre>
       * Pics Selection:
       * Initial conditions: {
       *     the IUT is trusting the CERT_AA_NEW
       *     the IUT has received the RCA CTL
       *         containing the CERT_AA_NEW
       *         and signed by CERT_RCA
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT received a CAM
       *                 signed with AT certificate
       *                     signed with AA_NEW certificate
       *         }
       *         then {
       *             the IUT accepts this CAM
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTL_03_BV
       * @reference ETSI TS 102 941, clause 6.3.5
       */
      testcase TC_SECPKI_ITSS_CTL_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_dc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp04(v_itss, v_dc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTL_03_BV_itss());
        v_dc.start(f_TC_SECPKI_ITSS_CTL_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown04(v_itss, v_dc);

      } // End of testcase TC_SECPKI_ITSS_CTL_03_BV

      group f_TC_SECPKI_ITSS_CTL_03_BV {

        function f_TC_SECPKI_ITSS_CTL_03_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;
          var integer i;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerRequestForRcaCtl(""); // FIXME Create PIXIT for RCA DC
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          v_securedGnPdu := f_sendSecuredCam(cc_ectl_aa_new);
          // Check that the CAM message is forwarde to Facilies layer
          f_sleep(PX_TAC);
          for(i := 0; i < lengthof(vc_utInds) and not match(vc_utInds[i].rawPayload, valueof(v_securedGnPdu.gnPacket.packet.payload)); i := i + 1) {
            // empty on purpose 
          }
          if(i < lengthof(vc_utInds)) {
            log("*** " & testcasename() & ": PASS: CA message was transmitted to upper layer ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
          } else {
            log("*** " & testcasename() & ": FAIL: CA message was not transmitted to upper layer ***");
            f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
          }

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_CTL_03_BV_itss

        function f_TC_SECPKI_ITSS_CTL_03_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_ca();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_dc_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_get(
                                                                                    PICS_HTTP_GET_URI_DC
                                                                                    )),
                                                v_response
                                                ) {
              var HashedId8 v_aa_hashed_id8; // Used for signature
              var Oct32 v_aa_private_key;
              var EtsiTs103097Certificate v_aa_new; // The CERT_AA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash("CERT_IUT_AA", v_aa_hashed_id8);
              f_readSigningKey("CERT_IUT_AA", v_aa_private_key);
              f_readCertificate(cc_ectl_aa_new, v_aa_new);
              // Build the ToBeSignedRcaCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_rca_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_aa(
                                                                                                            m_aa_entry(
                                                                                                                       v_aa_new,
                                                                                                                       "" // FIXME PICS_AA_ENTRY_URL
                                                                                                                       )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_aa_hashed_id8, v_aa_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_aa_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_AA_NEW
              f_init_default_headers_list(-, "ca_request", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_ca();
        } // End of function f_TC_SECPKI_ITSS_CTL_03_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTL_03_BV

      /**
       * @desc Check that the IUT requests new ECTL when current one is expired
       * <pre>
       * Pics Selection:
       * Initial conditions: {
       *     the IUT already downloaded the TLM CTL
       *         containing nextUpdate
       *             indicating timestamp T1
       *         and containing CPOC URL
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the T1 < CURRENT TIME
       *         }
       *         then {
       *             the IUT sends a request to the CPOC for a new CTL
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTL_04_BV
       * @reference ETSI TS 102 941, clause 6.3.5
       */
      testcase TC_SECPKI_ITSS_CTL_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_tlm;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_tlm);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTL_04_BV_itss());
        v_tlm.start(f_TC_SECPKI_ITSS_CTL_04_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_tlm);

      } // End of testcase TC_SECPKI_ITSS_CTL_04_BV

      group f_TC_SECPKI_ITSS_CTL_04_BV {

        function f_TC_SECPKI_ITSS_CTL_04_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;
          var integer i;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerRequestForRcaCtl(""); // FIXME Create PIXIT for RCA DC
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          log("*** " & testcasename() & ": PASS: Trigger was sent ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_CTL_04_BV_itss

        function f_TC_SECPKI_ITSS_CTL_04_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variables
          var Headers v_headers;
          var HttpMessage v_response;
          var float v_t1 := 30.0; // nextUpdate expiry in second
                                  // := (f_getCurrentTime()/* - 1072915200000*/) / 1000; // nextUpdate expiry in second

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          f_init_default_headers_list(PICS_HEADER_CTL_CONTENT_TYPE, "tlm_request", v_headers);
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 30,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictTestBody(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          tc_wait.start(v_t1);
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              tc_wait.stop;

              // TODO Send an error response (400 Bad request?)
              log("*** " & testcasename() & ": FAIL: ITSS TLM CTL request received before the tiemer expiry ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_error);
            }
            [] tc_wait.timeout {
              log("*** " & testcasename() & ": PASS: No ITSS TLM CTL request done before the timer expiry ***");
              f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_success);
            }
          } // End of 'alt' statement

          // TODO Wait for the request after timer expiry

          // Postamble
          f_cfHttpDown_tlm();

        } // End of testcase TC_SECPKI_ITSS_CTL_04_BV

      } // End of group f_TC_SECPKI_ITSS_CTL_04_BV

      /**
       * @desc Check that the IUT requests new RCA CTL when current one is expired
       * <pre>
       * Pics Selection:
       * Initial conditions: {
       *     the IUT already downloaded the RCA CTL
       *         containing nextUpdate
       *             indicating timestamp T1
       *         and containing RCA DC URL
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the T1 < CURRENT TIME
       *         }
       *         then {
       *             the IUT sends a request to the RCA DC for a new CTL
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTL_05_BV
       * @reference ETSI TS 102 941, clause 6.3.5
       */
      testcase TC_SECPKI_ITSS_CTL_05_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_dc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp04(v_itss, v_dc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTL_05_BV_itss());
        v_dc.start(f_TC_SECPKI_ITSS_CTL_05_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown04(v_itss, v_dc);

      } // End of testcase TC_SECPKI_ITSS_CTL_05_BV

      group f_TC_SECPKI_ITSS_CTL_05_BV {

        function f_TC_SECPKI_ITSS_CTL_05_BV_itss() runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;
          var integer i;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerRequestForRcaCtl(""); // FIXME Create PIXIT for RCA DC
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          log("*** " & testcasename() & ": PASS: Trigger was sent ***");
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_CTL_05_BV_itss

        function f_TC_SECPKI_ITSS_CTL_05_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;
          var float v_t1 := 30.0; // nextUpdate expiry in second
                                  // := (f_getCurrentTime()/* - 1072915200000*/) / 1000; // nextUpdate expiry in second

          // Test component configuration
          f_cfHttpUp_ca();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_dc_http_request_from_iut(
                                                mw_http_request(
                                                                mw_http_request_get(
                                                                                    PICS_HTTP_GET_URI_DC
                                                                                    )),
                                                v_response
                                                ) {
              var HashedId8 v_aa_hashed_id8; // Used for signature
              var Oct32 v_aa_private_key;
              var EtsiTs103097Certificate v_aa_new; // The CERT_AA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash("CERT_IUT_AA", v_aa_hashed_id8);
              f_readSigningKey("CERT_IUT_AA", v_aa_private_key);
              f_readCertificate(cc_ectl_aa_new, v_aa_new);
              // Build the ToBeSignedRcaCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_rca_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 30,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_aa(
                                                                                                            m_aa_entry(
                                                                                                                       v_aa_new,
                                                                                                                       "" // FIXME PICS_AA_ENTRY_URL
                                                                                                                       )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_aa_hashed_id8, v_aa_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_aa_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_AA_NEW
              f_init_default_headers_list(-, "ca_request", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          tc_wait.start(v_t1);
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              tc_wait.stop;

              // TODO Send an error response (400 Bad request?)
              log("*** " & testcasename() & ": FAIL: ITSS RCA DC request received before the tiemer expiry ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_error);
            }
            [] tc_wait.timeout {
              log("*** " & testcasename() & ": PASS: No RCA DC done before the timer expiry ***");
              f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // TODO Wait for the request after timer expiry

          // Postamble
          f_cfHttpDown_ca();

        } // End of testcase TC_SECPKI_ITSS_CTL_05_BV

      } // End of group f_TC_SECPKI_ITSS_CTL_05_BV

      group f_TC_SECPKI_ITSS_CTL_xx {

        function f_sendSecuredCam(in charstring p_certificate_id) runs on ItsPkiItss return GeoNetworkingPdu {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;

          v_securedGnPdu := f_prepareSecuredCam(p_certificate_id, valueof(m_headerInfo_cam(-, (f_getCurrentTime() * 1000)/*us*/)), valueof(m_signerIdentifier_digest), f_getTsStationId());
          log("f_sendSecuredCam: v_securedGnPdu= ", v_securedGnPdu);
          geoNetworkingPort.send(valueof(m_geoNwReq_linkLayerBroadcast(v_securedGnPdu)));
          return v_securedGnPdu;
        }

        function f_prepareSecuredCam(
                                     in charstring p_configId,
                                     in HeaderInfo p_headerInfo,
                                     in SignerIdentifier p_signerIdentifier,
                                     in template (value) StationId p_station_id := f_getTsStationId()
        ) runs on ItsPkiItss return GeoNetworkingPdu {
            // Local variables
            var GnNonSecuredPacket v_gnNonSecuredPacket;
            var octetstring v_gnPayload;
            var EtsiTs103097Data v_securedMessage;
            var LongPosVector v_longPosVector := valueof(m_dummyLongPosVector);

            log(">>> f_prepareSecuredCam");

            // Build signed Ieee1609Dot2Data
            v_longPosVector.latitude := f_getTsLatitude();
            v_longPosVector.longitude := f_getTsLongitude();
            v_longPosVector.gnAddr := f_getTsGnLocalAddress(c_compNodeB);
            v_gnNonSecuredPacket := valueof(m_geoNwShbPacket(
                                                             v_longPosVector
                                                             ));
            // Encode CAM payload
            v_gnPayload := valueof(
                                   bit2oct(
                                           encvalue(
                                                    m_camMsg_vehicle_HF_BV(
                                                                           valueof(p_station_id),
                                                                           f_getCurrentTime() mod 65536, // See ETSI EN 302 637-2 V1.3.0 - Clause B.3 generationDelatTime
                                                                           m_tsPosition
                                                                           ))));
            // Add BTP/CAM payload
            v_gnNonSecuredPacket.payload := int2oct(2001, 2) & int2oct(0, 2) & v_gnPayload;
            // Encode it
            log("f_prepareSecuredCam: v_gnNonSecuredPacket= ", v_gnNonSecuredPacket);
            v_gnPayload := bit2oct(
                                   encvalue(
                                            v_gnNonSecuredPacket
                                            )
                                   );
            log("f_prepareSecuredCam: v_gnPayload= ", v_gnPayload);
            f_buildGnSecuredCam(
                                v_securedMessage,
                                valueof(m_toBeSignedData(
                                                         m_signedDataPayload(
                                                                             m_etsiTs103097Data_unsecured(
                                                                                                          v_gnPayload
                                                                                                          )), 
                                                         p_headerInfo
                                                         )),
                                p_signerIdentifier,
                                p_configId
                                );

            // Return secured Gn packet
            return valueof(m_geoNwSecPdu(v_gnNonSecuredPacket, v_securedMessage));
        } // End of function f_prepareSecuredCam

      } // End of group f_TC_SECPKI_ITSS_CTL_xx

    } // End of group itss_ctl_handling

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.5  CTL distribution
    group itss_ctl_distribution {

      /**
       * @desc Check that the IUT retransmits the newly received Delta CTL
       * <pre>
       * Pics Selection: PICS_UC_SEC_05_2
       * Initial conditions: {
       *     the IUT is configured to redistribute the Delta CTL
       *     and the IUT doesn’t contain an CTL information
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT has received the Delta CTL
       *         }
       *         then {
       *             the IUT is started to broadcast the received Delta CTL
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTLDIST_01_BV
       * @reference ETSI TS 103 601, clause 4.2.1.4
       */
      testcase TC_SECPKI_ITSS_CTLDIST_01_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_cpoc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_UC_SEC_05_2) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_UC_SEC_05_2 required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_cpoc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTLDIST_01_BV_itss());
        v_cpoc.start(f_TC_SECPKI_ITSS_CTLDIST_01_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_cpoc);

      } // End of testcase TC_SECPKI_ITSS_CTLDIST_01_BV

      group f_TC_SECPKI_ITSS_CTLDIST_01_BV {

        function f_TC_SECPKI_ITSS_CTLDIST_01_BV_itss()  runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingInd v_geonetworking_message;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerUpdateEctl(""); // FIXME Create PIXIT for ETCL URI
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) -> value v_geonetworking_message {
              tc_ac.stop;

              if (f_verify_broadcasted_delta_ctl(v_geonetworking_message.msgIn) == false) {
                log("*** " & testcasename() & ": FAIL: Delta CTL was not successfully broadcasted ***");
                f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_error);
              } else {
                log("*** " & testcasename() & ": PASS: Delta CTL was successfully broadcasted ***");
                f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_success);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();

        } // End of function TC_SECPKI_ITSS_CTLDIST_01_BV_itss

        function f_TC_SECPKI_ITSS_CTLDIST_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_tlm();
        } // End of function f_TC_SECPKI_ITSS_CTLDIST_01_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTLDIST_01_BV

      /**
       * @desc Check that the IUT retransmits the updated Delta CTL
       * <pre>
       * Pics Selection: PICS_UC_SEC_05_2
       * Initial conditions: {
       *     the IUT is configured to redistribute the Delta CTL
       *     and the IUT contains an CTL information
       *         containing ctlSequence (SN)
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT has received the Delta CTL
       *                 containing ctlSequence
       *                    indicating value greater than SN
       *         }
       *         then {
       *             the IUT is started to broadcast the received Delta CTL
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTLDIST_02_BV
       * @reference ETSI TS 103 601, clause 4.2.1.4
       */
      testcase TC_SECPKI_ITSS_CTLDIST_02_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_cpoc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_UC_SEC_05_2) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_UC_SEC_05_2 required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_cpoc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTLDIST_02_BV_itss());
        v_cpoc.start(f_TC_SECPKI_ITSS_CTLDIST_02_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_cpoc);

      } // End of testcase TC_SECPKI_ITSS_CTLDIST_02_BV

      group f_TC_SECPKI_ITSS_CTLDIST_02_BV {

        function f_TC_SECPKI_ITSS_CTLDIST_02_BV_itss()  runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingInd v_geonetworking_message;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerUpdateEctl(""); // FIXME Create PIXIT for ETCL URI
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) -> value v_geonetworking_message {
              tc_ac.stop;

              if (f_verify_broadcasted_delta_ctl(v_geonetworking_message.msgIn) == false) {
                log("*** " & testcasename() & ": FAIL: Delta CTL was not successfully broadcasted ***");
                f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_error);
              } else {
                log("*** " & testcasename() & ": PASS: Delta CTL was successfully broadcasted ***");
                f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_success);
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();

        } // End of function TC_SECPKI_ITSS_CTLDIST_02_BV_itss

        function f_TC_SECPKI_ITSS_CTLDIST_02_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_tlm();
        } // End of function f_TC_SECPKI_ITSS_CTLDIST_02_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTLDIST_02_BV

      /**
       * @desc Check that the IUT is using the proper BTP port to broadcast the Delta CTL
       * <pre>
       * Pics Selection: PICS_UC_SEC_05_2
       * Initial conditions: {
       *     the IUT is configured to support P2P X_DISTRIBUTION distribution
       *     and the IUT has received the Delta X_DISTRIBUTION message
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT is triggered to broadcast the Delta X_DISTRIBUTION message
       *         }
       *         then {
       *             the IUT sends the X_MESSAGE
       *                 using the BTP port 2014
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTLDIST_03_BV
       * @reference ETSI TS 103 601, clause 5.4.4
       */
      testcase TC_SECPKI_ITSS_CTLDIST_03_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_cpoc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_UC_SEC_05_2) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_UC_SEC_05_2 required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_cpoc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTLDIST_03_BV_itss());
        v_cpoc.start(f_TC_SECPKI_ITSS_CTLDIST_03_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_cpoc);

      } // End of testcase TC_SECPKI_ITSS_CTLDIST_03_BV

      group f_TC_SECPKI_ITSS_CTLDIST_03_BV {

        function f_TC_SECPKI_ITSS_CTLDIST_03_BV_itss()  runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingInd v_geonetworking_message;
          var octetstring v_payload;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerUpdateEctl(""); // FIXME Create PIXIT for ETCL URI
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) -> value v_geonetworking_message {
              tc_ac.stop;

              if (f_verify_and_extract_payload(v_geonetworking_message.msgIn, -, v_payload) == false) {
                log("*** " & testcasename() & ": INCONC: Failed to verifiy payload ***");
                f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
              } else {
                // TODO Check payload
                if (substr(v_payload, 0, 2) == int2oct(2014, 2)) { // TODO Reorganize CAM/DENM/BTP test suites
                  log("*** " & testcasename() & ": PASS: Delta CTL was successfully broadcasted on the correct BTP port ***");
                  f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_success);
                } else {
                  log("*** " & testcasename() & ": FAIL: Delta CTL was not broadcasted on BTP port 114 ***");
                  f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_error);
                }
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();

        } // End of function TC_SECPKI_ITSS_CTLDIST_03_BV_itss

        function f_TC_SECPKI_ITSS_CTLDIST_03_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_tlm();
        } // End of function f_TC_SECPKI_ITSS_CTLDIST_03_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTLDIST_03_BV

      /**
       * @desc Check that the IUT stops to redistribute the Delta CTL if anorther node is also sending it
       * <pre>
       * Pics Selection: PICS_UC_SEC_05_2
       * Initial conditions: {
       *     the IUT is configured to support P2P Delta X_DISTRIBUTION distribution
       *     and the IUT has started broadcasting the Delta X_DISTRIBUTION message
       *         signed with X_CERTIFICATE
       *         and containing ctlSequence (SN)
       *     }
       * Expected behaviour:
       *     ensure that {
       *         when {
       *             the IUT has received the Delta X_DISTRIBUTION
       *                 signed with X_CERTIFICATE
       *                 and containing ctlSequence
       *                     indicating value equal or higher than SN
       *         }
       *         then {
       *             the IUT stops broadfcasting the Delta X_DISTRIBUTION
       *                 signed with X_CERTIFICATE
       *                 and containing ctlSequence (SN)
       *         }
       *     }
       * </pre>
       * 
       * @see       ETSI TS 103 525-2 TP SECPKI_ITSS_CTLDIST_04_BV
       * @reference ETSI TS 103 601, clause 5.4.4
       */
      testcase TC_SECPKI_ITSS_CTLDIST_04_BV() runs on ItsMtc system ItsPkiItssSystem {
        // Local variables
        var ItsPkiItss v_itss;
        var ItsPkiHttp v_cpoc;

        // Test control
        if (not PICS_IUT_ITS_S_ROLE or not PICS_UC_SEC_05_2) {
          log("*** " & testcasename() & ": PICS_IUT_ITS_S_ROLE and PICS_UC_SEC_05_2 required for executing the TC ***");
          setverdict(inconc);
          stop;
        }

        // Test component configuration
        f_cfMtcUp03(v_itss, v_cpoc);

        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_CTLDIST_04_BV_itss());
        v_cpoc.start(f_TC_SECPKI_ITSS_CTLDIST_04_BV_pki());

        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});

        // Cleanup
        f_cfMtcDown03(v_itss, v_cpoc);

      } // End of testcase TC_SECPKI_ITSS_CTLDIST_04_BV

      group f_TC_SECPKI_ITSS_CTLDIST_04_BV {

        function f_broadcast_delta_ctl(
                                       in charstring p_certificate_id,
                                       out Ieee1609Dot2Data p_delta_ctl
                                       ) runs on ItsPkiItss return GeoNetworkingPdu {
          // Local variables
          var GeoNetworkingPdu v_securedGnPdu;

          log(">>> f_broadcast_delta_ctl");

          v_securedGnPdu := f_prepareSecuredCam(p_certificate_id, valueof(m_headerInfo_cam(-, (f_getCurrentTime() * 1000)/*us*/)), valueof(m_signerIdentifier_digest), f_getTsStationId());
          log("f_sendSecuredCam: v_securedGnPdu= ", v_securedGnPdu);
          geoNetworkingPort.send(valueof(m_geoNwReq_linkLayerBroadcast(v_securedGnPdu)));

          return v_securedGnPdu;
        } // End of function f_broadcast_delta_ctl

        function f_TC_SECPKI_ITSS_CTLDIST_04_BV_itss()  runs on ItsPkiItss system ItsPkiItssSystem {
          // Local variables
          var GeoNetworkingInd v_geonetworking_message;
          var octetstring v_payload;
          var Ieee1609Dot2Data v_delta_ctl;

          // Test component configuration
          f_cfUp_itss();

          // Test adapter configuration

          // Preamble
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) {
              tc_ac.stop;

              f_sendUtTriggerUpdateEctl(""); // FIXME Create PIXIT for ETCL URI
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
            }
          } // End of 'alt' statement
          // Check that the IUT is boradcasting the Delta CTL
          geoNetworkingPort.clear;
          tc_ac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) -> value v_geonetworking_message {
              tc_ac.stop;

              if (f_verify_and_extract_payload(v_geonetworking_message.msgIn, -, v_payload) == false) {
                log("*** " & testcasename() & ": INCONC: Failed to verifiy payload ***");
                f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_timeout);
              } else {
                // TODO Check payload
                if (substr(v_payload, 0, 2) == int2oct(2014, 2)) { // TODO Reorganize CAM/DENM/BTP test suites
                  log("*** " & testcasename() & ": INFO: Delta CTL was successfully broadcasted on the correct BTP port ***");
                  f_selfOrClientSyncAndVerdictPreamble(c_prDone, e_success);
                } else {
                  log("*** " & testcasename() & ": INCONC: Delta CTL was not broadcasted on BTP port 114 ***");
                  f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
                }
              }
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdictPreamble(c_tbDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          // Delta CTL boradcasting from another node
          f_broadcast_delta_ctl(cc_ectl_rca_new, v_delta_ctl);
          // Check that the IUT stops broadcasting Delta CTL
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive(
                                         mw_geoNwInd(
                                                     mw_geoNwSecPdu(
                                                                    mw_etsiTs103097Data_signed(
                                                                                               mw_signedData
                                                                                               )))) -> value v_geonetworking_message {
              tc_noac.stop;

              if (f_verify_and_extract_payload(v_geonetworking_message.msgIn, -, v_payload) == false) {
                log("*** " & testcasename() & ": INCONC: Failed to verifiy payload ***");
                f_selfOrClientSyncAndVerdict(c_tbDone, e_timeout);
              } else {
                // TODO Check payload
                if (substr(v_payload, 0, 2) == int2oct(2014, 2)) { // TODO Reorganize CAM/DENM/BTP test suites
                  log("*** " & testcasename() & ": FAIL: The IUT shall not continue Delta CTL broadcasting ***");
                  f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
                } else {
                  tc_noac.start;
                  repeat;
                }
              }
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & ": PASS: IUT stops broadcasting Delta CTL ***");
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          } // End of 'alt' statement

          // Postamble
          f_cfDown_itss();

        } // End of function TC_SECPKI_ITSS_CTLDIST_04_BV_itss

        function f_TC_SECPKI_ITSS_CTLDIST_04_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var HttpMessage v_response;
          var Headers v_headers;

          // Test component configuration
          f_cfHttpUp_tlm();

          // Test adapter configuration

          // Preamble
          tc_ac.start;
          alt {
            [] a_await_cpoc_http_request_from_iut(
                                                  mw_http_request(
                                                                  mw_http_request_get(
                                                                                      PICS_HTTP_GET_URI_TLM
                                                                                      )),
                                                  v_response
                                                  ) {
              var HashedId8 v_rca_hashed_id8; // Used for signature
              var Oct32 v_rca_private_key;
              var EtsiTs103097Certificate v_rca_new; // The CERT_RCA_NEW
              var bitstring v_enc_msg;
              var ToBeSignedData v_tbs;
              var bitstring v_tbs_enc;
              var Oct32 v_tbs_signed;
              var Signature v_signature;
              var Ieee1609Dot2Data v_ieee1609dot2_signed_data;

              tc_ac.stop;

              // Read certificates
              f_getCertificateHash(PICS_IUT_CA_CERTIFICATE_ID, v_rca_hashed_id8);
              f_readSigningKey(PICS_IUT_CA_CERTIFICATE_ID, v_rca_private_key);
              f_readCertificate(cc_ectl_rca_new, v_rca_new);
              // Build the ToBeSignedTlmCtl data structure
              v_enc_msg := encvalue(
                                    valueof(
                                            m_to_be_signed_tlm_full_ctl(
                                                                        f_getCurrentTime() / 1000 + 3600,
                                                                        10,
                                                                        {
                                                                          m_ctrl_command_add(
                                                                                             m_ctl_entry_rca(
                                                                                                             m_root_ca_entry(
                                                                                                                             v_rca_new
                                                                                                                             )))
                                                                          }
                                                                        )));
              v_tbs := valueof(
                               m_toBeSignedData(
                                                m_signedDataPayload(
                                                                    m_etsiTs103097Data_unsecured(bit2oct(v_enc_msg))
                                                                    ),
                                                m_headerInfo_inner_pki_request(-, (f_getCurrentTime() * 1000)/*us*/)
                                                ));
              v_tbs_enc := encvalue(v_tbs);
              // Sign the certificate
              v_tbs_signed := f_signWithEcdsa(bit2oct(v_tbs_enc), v_rca_hashed_id8, v_rca_private_key);
              v_signature := valueof(
                                     m_signature_ecdsaNistP256(
                                                               m_ecdsaP256Signature(
                                                                                    m_eccP256CurvePoint_x_only(
                                                                                                               substr(v_tbs_signed, 0, 32)
                                                                                                               ),
                                                                                    substr(v_tbs_signed, 32, 32)
                                                                                    )));
              log(testcasename() & ": v_signature= ", v_signature);
              v_ieee1609dot2_signed_data := valueof(
                                                    m_etsiTs103097Data_signed(
                                                                              m_signedData(
                                                                                           sha256,
                                                                                           v_tbs,
                                                                                           m_signerIdentifier_digest(v_rca_hashed_id8),
                                                                                           v_signature
                                                                                           )));
              // Send response with CERT_RCA_NEW
              f_init_default_headers_list(-, "tlm_ectl", v_headers);
              f_http_send(
                          v_headers,
                          m_http_response(
                                          m_http_response_ok(
                                                             m_http_message_body_binary(
                                                                                        m_binary_body_ieee1609dot2_data(
                                                                                                                        v_ieee1609dot2_signed_data
                                                                                                                        )),
                                                             v_headers
                                                             )));

              log("*** " & testcasename() & ": INFO: CERT_RCA_NEW was sent to the IUT ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
            }
            [] tc_ac.timeout {
              log("*** " & testcasename() & ": INCONC: Expected message not received ***");
              f_selfOrClientSyncAndVerdict(c_prDone, e_timeout);
            }
          } // End of 'alt' statement

          // Test Body
          f_selfOrClientSyncAndVerdict(c_tbDone, e_success);

          // Postamble
          f_cfHttpDown_tlm();
        } // End of function f_TC_SECPKI_ITSS_CTLDIST_04_BV_pki

      } // End of group f_TC_SECPKI_ITSS_CTLDIST_04_BV

      // TODO

    } // End of group itss_ctl_distribution

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.6  CRL handling
    group itss_crl_handling {
      // TODO To be done after validation of itss_ctl_handling group as CTL and CRL are close
    } // End of group itss_crl_handling

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.7  CRL distribution
    group itss_crl_distribution {
      // TODO To be done after validation of itss_ctl_distribution group as CTL and CRL are close
    } // End of group itss_crl_distribution

  } // End of group itss_behavior

} // End of module ItsPkiItss_TestCases