ItsPki_TestCases.ttcn 1.06 MB
Newer Older
ASN.1 Documenter's avatar
ASN.1 Documenter committed
/**
ASN.1 Documenter's avatar
ASN.1 Documenter committed
 *  @version  $Url$
 *            $Id$
 *  @desc     Testcases  file for Security 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 ItsPki_TestCases {
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  import from CAM_PDU_Descriptions language "ASN.1:1997" all;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // LibItsCommon
  import from LibItsCommon_TypesAndValues all;
  import from LibItsCommon_Functions all;
  import from LibItsCommon_TypesAndValues all;
  import from LibItsCommon_ASN1_NamedNumbers all;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  import from LibItsGeoNetworking_TestSystem all;

  // LibItsCam
  import from LibItsCam_TypesAndValues all;
  import from LibItsCam_Templates all;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // 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;

ASN.1 Documenter's avatar
ASN.1 Documenter committed
  // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed

  // ItsPki
  import from ItsPki_Pixits all;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
  /**
ASN.1 Documenter's avatar
ASN.1 Documenter committed
   */
  group itss_behavior {
    group itss_states {
      const charstring c_stInitial := "initial";
      const charstring c_stEnrolled := "enrolled";
      const charstring c_stAuthorized := "authorized";
    }

ASN.1 Documenter's avatar
ASN.1 Documenter committed
    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

ASN.1 Documenter's avatar
ASN.1 Documenter committed
      /**
       * @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,
ASN.1 Documenter's avatar
ASN.1 Documenter committed
                                                      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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        var EtsiTs102941Data v_etsi_ts_102941_data;
        var Oct16 v_request_hash;
        var HashedId8 v_bfk_hashed_id8;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        log(">>> f_verify_http_ec_request_from_iut_itss: ", p_request);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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))))) {
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // 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);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
            } 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;
              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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
            }
          } else {
            log("*** " & testcasename() & ": FAIL: Not implemented yet ***");
            f_selfOrClientSyncAndVerdict(c_prDone, e_error);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          }
          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);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        }
        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));
ASN.1 Documenter's avatar
ASN.1 Documenter committed

        // Set verdict
        p_result := 0;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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,
ASN.1 Documenter's avatar
ASN.1 Documenter committed
                                                      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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        log(">>> f_verify_http_at_request_from_iut_itss:", p_request);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // 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);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        } 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;
        }

ASN.1 Documenter's avatar
ASN.1 Documenter committed
            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)));
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              // 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)));
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              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)));
ASN.1 Documenter's avatar
ASN.1 Documenter committed
                // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          }
        }
        
        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;
        }
ASN.1 Documenter's avatar
ASN.1 Documenter committed

        // 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)));
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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: 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 {
            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));
      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
ASN.1 Documenter's avatar
ASN.1 Documenter committed

      /**
       * @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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
    } // End of group itss_helpers

    // ETSI TS 103 525-2 V2.0.2 (2023-07) Clause 5.2.2.1  Enrollment request
ASN.1 Documenter's avatar
ASN.1 Documenter committed
    group itss_enrolment_request {
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      /**
       * @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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
       * @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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // 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;
        }
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Test component configuration
        f_cfMtcUp01(v_itss, v_ea);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Start components
        v_itss.start(f_TC_SECPKI_ITSS_ENR_01_BV_itss());
        v_ea.start(f_TC_SECPKI_ITSS_ENR_01_BV_pki());
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      } // End of testcase TC_SECPKI_ITSS_ENR_01_BV
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      group f_TC_SECPKI_ITSS_ENR_01_BV {
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Test component configuration
          vc_hashedId8ToBeUsed := ""; // No certificates //PX_IUT_DEFAULT_CERTIFICATE
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          f_cfUp_itss();
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Test adapter configuration
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Preamble
          // Initial state: No CAM shall be emitted
          geoNetworkingPort.clear;
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "itss: FAIL: Unexpected GeoNet message received ***");
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              f_selfOrClientSyncAndVerdict(c_prDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "itss: INFO: No GeoNet message received. Continue ***");
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              f_selfOrClientSyncAndVerdict(c_prDone, e_success);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          } // End of 'alt' statement
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // 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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          tc_noac.start;
          alt {
            [] geoNetworkingPort.receive {
              log("*** " & testcasename() & "itss: FAIL: Unexpected GeoNet message received ***");
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              f_selfOrClientSyncAndVerdict(c_tbDone, e_error);
            }
            [] tc_noac.timeout {
              log("*** " & testcasename() & "itss: PASS: Enrolment trigger sent succesfully ***");
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              f_selfOrClientSyncAndVerdict(c_tbDone, e_success);
            }
          } // End of 'alt' statement
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Postamble
          f_cfDown_itss();
        } // End of function f_TC_SECPKI_ITSS_ENR_01_BV_itss
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        function f_TC_SECPKI_ITSS_ENR_01_BV_pki() runs on ItsPkiHttp system ItsPkiItssSystem {
          // Local variable
          var Headers v_headers;
          var HttpMessage v_request;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Test component configuration
          f_cfHttpUp(PICS_TS_EA_CERTIFICATE_ID, PICS_TS_AA_CERTIFICATE_ID);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Test adapter configuration
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Preamble
          f_init_default_headers_list(-, "inner_ec_response", v_headers);
          f_selfOrClientSyncAndVerdict(c_prDone, e_success);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // 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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              var InnerEcRequest v_inner_ec_request;
              var InnerEcResponse v_inner_ec_response;
              var Ieee1609Dot2Data v_response_message;
              var EtsiTs102941Data v_pki_request;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
              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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
                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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
          // Postamble
          f_cfHttpDown();
        } // End of function f_TC_SECPKI_ITSS_ENR_01_BV_pki
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      } // End of group f_TC_SECPKI_ITSS_ENR_01_BV
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      /**
       * @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
ASN.1 Documenter's avatar
ASN.1 Documenter committed
       * @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;
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // 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;
        }
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // 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());
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Synchronization
        f_serverSync2ClientsAndStop({c_prDone, c_tbDone});
ASN.1 Documenter's avatar
ASN.1 Documenter committed
        // Cleanup
        f_cfMtcDown01(v_itss, v_ea);
ASN.1 Documenter's avatar
ASN.1 Documenter committed
      } // 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);