LibItsPki_Functions.ttcn 239 KB
Newer Older
3001
3002
3003
3004
3005
      if (PICS_SEC_FIXED_KEYS) {
        p_request_hash := '10ED97A2F2933DD3AC55F47022D125E18F5E1AA024613E616A75BA4979EFE318'O;
      } else {
        p_request_hash := f_hashWithSha256(bit2oct(v_enc_value));
      }
Yann Garcia's avatar
Yann Garcia committed
3006
      log("f_build_pki_secured_request_message_for_authorization: p_request_hash= ", p_request_hash);
3007
      
Yann Garcia's avatar
Yann Garcia committed
3008
      log("<<< f_build_pki_secured_request_message_for_authorization: ", p_ieee1609dot2_signed_and_encrypted_data);
3009
3010
3011
      return true;
    } // End of function f_build_pki_secured_request_message_for_authorization
    
3012
3013
3014
3015
3016
3017
    /**
     * @desc Build a signed and encrypted PKI response message
     * @param p_private_key         Private key for signature
     * @param p_signer_identifier   Signer identifier for signature, could be self or certificate HashedId8
     * @param p_recipientId         Recipient identifier to be inclued in encrypted layer.
     *                              If value is int2oct(0. 8), the recipient id is the HashedId8 of the symmetric key used by the sender to encrypt the message to which the response is built
Yann Garcia's avatar
Yann Garcia committed
3018
3019
     * @param p_public_key_compressed The public compressed key (canonical form) for encryption
     * @param p_compressed_mode      The compressed mode of the public compressed key (canonical form) for encryption
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
     * @param p_pki_message         The PKI message to be secured
     * @param p_ieee1609dot2_signed_and_encrypted_data The secured message
     * @return true on success, false otherwise
     */
    function f_build_pki_secured_response_message(
                                                 in octetstring p_private_key,
                                                 in SignerIdentifier p_signer_identifier,
                                                 in octetstring p_pki_message,
                                                 in Oct16 p_aes_sym_key,
                                                 in Oct12 p_nonce,
                                                 out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data
                                                 ) return boolean {
      // Local variables
      var template (value) ToBeSignedData v_tbs;
      var octetstring v_tbs_signed;
      var template (value) Ieee1609Dot2Data v_ieee1609dot2_signed_data;
      var octetstring v_encoded_inner_ec_response;
      var HashedId8 v_recipientId;
      var octetstring v_encrypted_inner_ec_response;
      
      // Signed the encoded PKI message
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload(
                                                    m_etsiTs103097Data_unsecured(p_pki_message)
                                                   ),
3045
                                m_headerInfo_inner_pki_response(-, (f_getCurrentTime() * 1000)/*us*/)
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
                               );
      if (ischosen(p_signer_identifier.self_)) {
        v_tbs_signed := f_signWithEcdsaNistp256WithSha256(bit2oct(encvalue(v_tbs)), int2oct(0, 32), p_private_key);
      } else {
        var charstring v_certificate_id;
        var octetstring v_hash;
        fx_readCertificateFromDigest(p_signer_identifier.digest, v_certificate_id); // TODO Add a wrapper function
        f_getCertificateHash(v_certificate_id, v_hash);
        v_tbs_signed := f_signWithEcdsaNistp256WithSha256(bit2oct(encvalue(v_tbs)), v_hash, p_private_key);
      }
      // Add the signature and create EtsiTs103097Data-Signed data structure
      v_ieee1609dot2_signed_data := m_etsiTs103097Data_signed(
                                                              m_signedData(
                                                                           sha256,
                                                                           v_tbs,
                                                                           p_signer_identifier,
                                                                           m_signature_ecdsaNistP256(
                                                                                                     m_ecdsaP256Signature(
                                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                                     substr(v_tbs_signed, 0, 32)
                                                                                                                                                     ),
                                                                                                                          substr(v_tbs_signed, 32, 32)
                                                                                                                          )
                                                                                                     )
                                                                           )
                                                      );
      // Encode EtsiTs103097Data-Signed data structure
      v_encoded_inner_ec_response := bit2oct(encvalue(v_ieee1609dot2_signed_data));
      v_encrypted_inner_ec_response := fx_encrypt_aes_128_ccm_test(p_aes_sym_key, p_nonce, v_encoded_inner_ec_response);
      v_recipientId := f_HashedId8FromSha256(f_hashWithSha256(p_aes_sym_key));
      log("v_recipientId= ", v_recipientId);
      // Fill Certificate template with the public compressed keys (canonical form)
      p_ieee1609dot2_signed_and_encrypted_data := valueof(
                                                          m_etsiTs103097Data_encrypted(
                                                                                       m_encryptedData(
                                                                                                       {
                                                                                                        m_recipientInfo_pskRecipInfo(
                                                                                                                                     v_recipientId
                                                                                                                                     )
                                                                                                       },
                                                                                                       m_SymmetricCiphertext_aes128ccm(
                                                                                                                                       m_aesCcmCiphertext(
                                                                                                                                                          p_nonce,
                                                                                                                                                          v_encrypted_inner_ec_response
                                                                                                                                                         )
                                                                                                                                      )
                                                                                                      )
                                                                                       )
                                                         );
      
3096
      log("<<< f_build_pki_secured_response_message: ", p_ieee1609dot2_signed_and_encrypted_data);
3097
3098
      return true;
    } // End of function f_build_pki_secured_response_message
3099
    
Yann Garcia's avatar
Yann Garcia committed
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
    /**
     * @desc Verify the protocol element of the Pki message. 
     *       If p_check_security is set to false, only decryption and decoding of the outer message are verified.
     * @param p_private_enc_key     Private key for decryption
     * @param p_salt
     * @param p_p_ieee1609dot2_encrypted_and_signed_data
     * @param p_check_security      Set to true to verify PKI protocol element such as signatures...
     * @param p_request_hash        The request hash for to be used to build the response
     * @param p_etsi_ts_102941_data The EtsiTs102941Data message
     * @param p_aes_enc_key         The AES 128 encrytion key to be used to encrypt the response
     * @return true on success, false otherwise
     */
    function f_verify_pki_request_message(
                                          in Oct32 p_private_enc_key,
                                          in Oct32 p_salt,
                                          in octetstring p_issuer,
                                          in Ieee1609Dot2Data p_ieee1609dot2_encrypted_and_signed_data,
                                          in boolean p_check_security := true,
                                          out Oct16 p_request_hash,
                                          out EtsiTs102941Data p_etsi_ts_102941_data,
                                          out Oct16 p_aes_enc_key
                                          ) return boolean {
      // Local variables
      var bitstring v_msg_bit;
      var octetstring v_msg;
      var Ieee1609Dot2Data v_ieee1609dot2_signed_data;
      var Certificate v_certificate;

      log(">>> f_verify_pki_request_message: p_private_enc_key= ", p_private_enc_key);
      log(">>> f_verify_pki_request_message: p_salt= ", p_salt);
      log(">>> f_verify_pki_request_message: p_issuer= ", p_issuer);
      
      // 1. Calculate the request Hash
      v_msg := bit2oct(encvalue(p_ieee1609dot2_encrypted_and_signed_data));
      log("f_verify_pki_request_message: Encoded request: ", v_msg);
      p_request_hash := substr(f_hashWithSha256(v_msg), 0, 16);
      log("f_verify_pki_request_message: v_request_hash= ", p_request_hash);
 
      // 2. Decrypt the InnerEcRequest
      log("f_verify_pki_request_message: p_private_enc_key= ", p_private_enc_key);
      if (f_decrypt(p_private_enc_key, p_ieee1609dot2_encrypted_and_signed_data, p_salt, v_ieee1609dot2_signed_data, p_aes_enc_key) == false) {
        log("f_verify_pki_request_message: Failed to decrypt message");
        return false;
      }
      log("f_verify_pki_request_message: v_ieee1609dot2_signed_data= ", v_ieee1609dot2_signed_data);
      log("f_verify_pki_request_message: p_aes_enc_key= ", p_aes_enc_key);

      // 3. Check basics security
      log(
          match(
                v_ieee1609dot2_signed_data,
                mw_etsiTs103097Data_signed(
                                           mw_signedData(
                                                         -,
                                                         mw_toBeSignedData(
                                                                           mw_signedDataPayload,
                                                                           mw_ieee1609Dot2_headerInfo(c_its_aid_SCR)
                                                                           )
                                                         )
                                )));
      if (match(v_ieee1609dot2_signed_data, mw_etsiTs103097Data_signed(mw_signedData(-, mw_toBeSignedData(mw_signedDataPayload)))) == false) {
        log("f_verify_pki_request_message: Failed to check basic security");
        if (p_check_security == true) {
          return false;
        }
      }
      
      // 4. Verifiy signature
      log("f_verify_pki_request_message: v_ieee1609dot2_signed_data.content.signedData.tbsData= ", v_ieee1609dot2_signed_data.content.signedData.tbsData);
      v_msg := bit2oct(encvalue(v_ieee1609dot2_signed_data.content.signedData.tbsData));
Yann Garcia's avatar
Yann Garcia committed
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
      if (p_issuer == ''O) { // ITS-S/OBU
        var PublicVerificationKey v_public_verification_key;
        
        log("f_verify_pki_request_message: Use ITS-S technical keys");
        if (PX_VE_ALG == e_nist_p256) {
          var EccP256CurvePoint v_ecc_p256_curve_point;
          
          if (PICS_ITS_S_SIGN_NISTP256_PUBLIC_KEY[0] == '02'O) {
            v_ecc_p256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(substr(PICS_ITS_S_SIGN_NISTP256_PUBLIC_KEY, 1, 32)));
          } else {
            v_ecc_p256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(substr(PICS_ITS_S_SIGN_NISTP256_PUBLIC_KEY, 1, 32)));
          }
          v_public_verification_key := valueof(
                                               m_publicVerificationKey_ecdsaNistP256(
                                                                                     v_ecc_p256_curve_point
                                                                                     )
                                               );
        } else if (PX_VE_ALG == e_brainpool_p256_r1) {
          var EccP256CurvePoint v_ecc_p256_curve_point;
            
          if (PICS_ITS_S_SIGN_BRAINPOOLP256r1_PUBLIC_KEY[0] == '02'O) {
            v_ecc_p256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(substr(PICS_ITS_S_SIGN_BRAINPOOLP256r1_PUBLIC_KEY, 1, 32)));
          } else {
            v_ecc_p256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(substr(PICS_ITS_S_SIGN_BRAINPOOLP256r1_PUBLIC_KEY, 1, 32)));
          }
          v_public_verification_key := valueof(
                                               m_publicVerificationKey_ecdsaBrainpoolP256r1(
                                                                                            v_ecc_p256_curve_point
                                                                                            )
                                               );
        } else if (PX_VE_ALG == e_brainpool_p384_r1) {
          var EccP384CurvePoint v_ecc_p384_curve_point;
            
          if (PICS_ITS_S_SIGN_BRAINPOOLP384r1_PUBLIC_KEY[0] == '02'O) {
            v_ecc_p384_curve_point := valueof(m_eccP384CurvePoint_compressed_y_0(substr(PICS_ITS_S_SIGN_BRAINPOOLP384r1_PUBLIC_KEY, 1, 48)));
          } else {
            v_ecc_p384_curve_point := valueof(m_eccP384CurvePoint_compressed_y_1(substr(PICS_ITS_S_SIGN_BRAINPOOLP384r1_PUBLIC_KEY, 1, 48)));
          }
          v_public_verification_key := valueof(
                                               m_publicVerificationKey_ecdsaBrainpoolP384r1(
                                                                                            v_ecc_p384_curve_point
                                                                                            )
                                               );
        } else {
Yann Garcia's avatar
Yann Garcia committed
3214
3215
          return false;
        }
Yann Garcia's avatar
Yann Garcia committed
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
        log("f_verify_pki_request_message: v_public_verification_key= ", v_public_verification_key);
        if (f_verifyEcdsa(v_msg, int2oct(0, 32), v_ieee1609dot2_signed_data.content.signedData.signature_, v_public_verification_key) == false) {
          if (p_check_security == true) {
            return false;
          }
        }
      } else {
        if (f_getCertificateFromDigest(f_HashedId8FromSha256(p_issuer), v_certificate) == false) {
          if (p_check_security == true) {
            return false;
          }
        }
        log("f_verify_pki_request_message: v_certificate= ", v_certificate);
        if (f_verifyEcdsa(v_msg, p_issuer, v_ieee1609dot2_signed_data.content.signedData.signature_, v_certificate.toBeSigned.verifyKeyIndicator.verificationKey) == false) {
          if (p_check_security == true) {
            return false;
          }
Yann Garcia's avatar
Yann Garcia committed
3233
3234
3235
3236
3237
3238
3239
        }
      }
      
      // 4. Return the PKI message
      log("f_verify_pki_request_message: v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData= ", v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData);
      v_msg_bit := oct2bit(v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData);
      if (decvalue(v_msg_bit, p_etsi_ts_102941_data) != 0) {
Yann Garcia's avatar
Yann Garcia committed
3240
3241
3242
        if (p_check_security == true) {
          return false;
        }
Yann Garcia's avatar
Yann Garcia committed
3243
3244
      }
      if (p_etsi_ts_102941_data.version != PkiProtocolVersion) {
Yann Garcia's avatar
Yann Garcia committed
3245
3246
3247
        if (p_check_security == true) {
          return false;
        }
Yann Garcia's avatar
Yann Garcia committed
3248
3249
3250
3251
3252
3253
      }
      
      log("<<< f_verify_pki_request_message: true");
      return true;
    } // End of function f_verify_pki_request_message
    
3254
    /**
3255
3256
     * @desc Verify the protocol element of the Pki message. 
     *       If p_check_security is set to false, only decryption and decoding of the outer message are verified.
3257
3258
3259
     * @param p_private_key         Private key for decryption
     * @param p_publicEphemeralCompressedKey
     * @param p_publicEphemeralCompressedKeyMode
3260
     * @param p_issuer              Issuer
3261
3262
     * @param p_certificate         Certificate to use for verification key
     * @param p_ieee1609dot2_encrypted_and_signed_data The received encrypted and signed data
3263
     * @param p_check_security      Set to true to verify PKI protocol element such as signatures...
3264
3265
3266
     * @param p_etsi_ts_102941_data The EtsiTs102941Data message
     * @return true on success, false otherwise
     */
Yann Garcia's avatar
Yann Garcia committed
3267
3268
3269
3270
3271
3272
3273
3274
3275
    function f_verify_pki_response_message(
                                           in octetstring p_private_enc_key,
                                           in Oct16 p_aes_sym_key,
                                           in Oct16 p_authentication_vector, // TODO Tobe removed
                                           in octetstring p_issuer,
                                           in Ieee1609Dot2Data p_ieee1609dot2_encrypted_and_signed_data,
                                           in boolean p_check_security := true,
                                           out EtsiTs102941Data p_etsi_ts_102941_data
                                           ) return boolean {
garciay's avatar
garciay committed
3276
      // Local variables
3277
3278
3279
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var octetstring v_plain_message;
3280
      var Ieee1609Dot2Data v_ieee1609dot2_signed_data;
3281
      var Certificate v_certificate;
3282
3283
3284
      var bitstring v_etsi_ts_102941_data_msg;
      var bitstring v_tbs;
      var boolean v_ret;
3285
      
Yann Garcia's avatar
Yann Garcia committed
3286
3287
3288
3289
      log(">>> f_verify_pki_response_message: p_private_enc_key= ", p_private_enc_key);
      log(">>> f_verify_pki_response_message: p_aes_sym_key= ", p_aes_sym_key);
      log(">>> f_verify_pki_response_message: p_authentication_vector= ", p_authentication_vector);
      log(">>> f_verify_pki_response_message: p_issuer= ", p_issuer);
3290
3291
3292
      
      // TODO Check p_ieee1609dot2_encrypted_and_signed_data.content.encryptedData.recipients[0].pskRecipInfo. See IEEE Std 1609.2-2017 Clause 6.3.34 PreSharedKeyRecipientInfo
      
3293
      // 1. Decrypt the data
Yann Garcia's avatar
Yann Garcia committed
3294
      v_plain_message := fx_decrypt_aes_128_ccm_test(p_aes_sym_key, p_ieee1609dot2_encrypted_and_signed_data.content.encryptedData.ciphertext.aes128ccm.nonce, p_ieee1609dot2_encrypted_and_signed_data.content.encryptedData.ciphertext.aes128ccm.ccmCiphertext);
3295
3296
3297
      if (isbound(v_plain_message) == false) {
        return false;
      }
Yann Garcia's avatar
Yann Garcia committed
3298
      log("f_verify_pki_response_message: v_plain_message= ", v_plain_message);
3299
3300
3301
3302

      // 2. Decode it
      v_tbs := oct2bit(v_plain_message);
      if (decvalue(v_tbs, v_ieee1609dot2_signed_data) != 0) {
3303
        return false;
3304
      }
Yann Garcia's avatar
Yann Garcia committed
3305
      log("f_verify_pki_response_message: v_ieee1609dot2_signed_data= ", v_ieee1609dot2_signed_data);
3306
      
3307
      // 3. Check the signature
Yann Garcia's avatar
Yann Garcia committed
3308
      log("f_verify_pki_response_message: v_ieee1609dot2_signed_data.content.signedData.tbsData= ", v_ieee1609dot2_signed_data.content.signedData.tbsData);
3309
      v_tbs := encvalue(v_ieee1609dot2_signed_data.content.signedData.tbsData);
3310
3311
3312
3313
3314
      if (f_getCertificateFromDigest(v_ieee1609dot2_signed_data.content.signedData.signer.digest, v_certificate) == false) {
        if (p_check_security == true) {
          return false;
        }
      }
Yann Garcia's avatar
Yann Garcia committed
3315
      if (f_verifyEcdsa(bit2oct(v_tbs), p_issuer, v_ieee1609dot2_signed_data.content.signedData.signature_, v_certificate.toBeSigned.verifyKeyIndicator.verificationKey) == false) {
garciay's avatar
garciay committed
3316
3317
3318
        if (p_check_security == true) {
          return false;
        }
3319
      }
3320
      
3321
      // 4. Return the PKI message
Yann Garcia's avatar
Yann Garcia committed
3322
      log("f_verify_pki_response_message: v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData= ", v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData);
3323
3324
3325
3326
3327
      v_etsi_ts_102941_data_msg := oct2bit(v_ieee1609dot2_signed_data.content.signedData.tbsData.payload.data.content.unsecuredData);
      if (decvalue(v_etsi_ts_102941_data_msg, p_etsi_ts_102941_data) != 0) {
        return false;
      }

garciay's avatar
garciay committed
3328
      if (p_etsi_ts_102941_data.version != PkiProtocolVersion) {
Yann Garcia's avatar
Yann Garcia committed
3329
        return false;
3330
      }
garciay's avatar
garciay committed
3331
3332
      
      return true;
Yann Garcia's avatar
Yann Garcia committed
3333
    } // End of function f_verify_pki_response_message
3334
3335
    
    /**
Yann Garcia's avatar
Yann Garcia committed
3336
3337
     * @desc Verify the EC certificate generated by the EA entity
     * @param p_ec_certificate        The new EC certificate
Yann Garcia's avatar
Yann Garcia committed
3338
3339
     * @param p_ea_certificate        The certificate issuer
     * @param p_public_key_compressed The public compressed key (canonical form) generated for the InnerEcRequest
Yann Garcia's avatar
Yann Garcia committed
3340
     * @param p_compressed_mode       The public compressed key mode
3341
3342
     * @return true on success, false otherwise
     */
Yann Garcia's avatar
Yann Garcia committed
3343
3344
    function f_verify_ec_certificate(
                                     in Certificate p_ec_certificate,
Yann Garcia's avatar
Yann Garcia committed
3345
                                     in Certificate p_ea_certificate,
Yann Garcia's avatar
Yann Garcia committed
3346
3347
                                     in octetstring p_public_key_compressed,
                                     in integer p_compressed_mode
3348
                                     ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
3349
      var bitstring v_encoded_cert;
Yann Garcia's avatar
Yann Garcia committed
3350
      var Oct32 v_ec_cert_hash;
Yann Garcia's avatar
Yann Garcia committed
3351
3352
3353
3354
      var HashedId8 v_ec_cert_hashed_id8;
      
      // Calculate the HashedId8 of the whole certificate
      v_encoded_cert := encvalue(p_ec_certificate);
Yann Garcia's avatar
Yann Garcia committed
3355
3356
3357
3358
      v_ec_cert_hash := f_hashWithSha256(bit2oct(v_encoded_cert));
      log("f_verify_ec_certificate: ==> EC certificate Hash: ", v_ec_cert_hash);
      v_ec_cert_hashed_id8 := f_HashedId8FromSha256(v_ec_cert_hash);
      log("f_verify_ec_certificate: ==> EC certificate HashedId8: ", v_ec_cert_hashed_id8);
3359
3360
      
      // Check the signer
Yann Garcia's avatar
Yann Garcia committed
3361
      log("f_verify_ec_certificate: ", match(p_ec_certificate.issuer, mw_issuerIdentifier_sha256AndDigest));
Yann Garcia's avatar
Yann Garcia committed
3362
3363
      if (match(p_ec_certificate.issuer, mw_issuerIdentifier_sha256AndDigest) == false) {
        log("f_verify_ec_certificate: Wrong issuer");
Yann Garcia's avatar
Yann Garcia committed
3364
3365
        return false;
      }
3366
      
Yann Garcia's avatar
Yann Garcia committed
3367
3368
      // Check EC certificate signature
      // TODO Who sign the EC certificate?
Yann Garcia's avatar
Yann Garcia committed
3369
      if (f_verifyCertificateSignatureWithPublicKey(p_ec_certificate, p_ea_certificate.toBeSigned.verifyKeyIndicator.verificationKey) == false) {
Yann Garcia's avatar
Yann Garcia committed
3370
3371
        log("f_verify_ec_certificate: Signature not verified");
        return false;
Yann Garcia's avatar
Yann Garcia committed
3372
      }
Yann Garcia's avatar
Yann Garcia committed
3373
3374
3375

      // TODO Check that requested information are present
      
3376
      
Yann Garcia's avatar
Yann Garcia committed
3377
3378
      return true;
    } // End of function f_verify_ec_certificate
3379
3380
    
    /**
Yann Garcia's avatar
Yann Garcia committed
3381
3382
3383
3384
3385
     * @desc Verify the generated AT certificate 
     * @param p_at_certificate        The new AT certificate
     * @param p_ea_certificate        The certificate issuer
     * @param p_public_key_compressed The public compressed key (canonical form) generated for the InnerAtRequest
     * @param p_compressed_mode       The public compressed key mode
3386
3387
     * @return true on success, false otherwise
     */
Yann Garcia's avatar
Yann Garcia committed
3388
3389
    function f_verify_at_certificate(
                                     in Certificate p_at_certificate,
3390
                                     in Certificate p_aa_certificate,
Yann Garcia's avatar
Yann Garcia committed
3391
3392
                                     in octetstring p_public_key_compressed,
                                     in integer p_compressed_mode
3393
                                     ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
3394
3395
3396
3397
3398
3399
3400
      var bitstring v_encoded_cert;
      var HashedId8 v_at_cert_hashed_id8;
      
      // Calculate the HashedId8 of the whole certificate
      v_encoded_cert := encvalue(p_at_certificate);
      v_at_cert_hashed_id8 := f_HashedId8FromSha256(f_hashWithSha256(bit2oct(v_encoded_cert)));
      log("f_verify_at_certificate: EC certificate HashedId8: ", v_at_cert_hashed_id8);
3401
3402
      
      // Check the signer
Yann Garcia's avatar
Yann Garcia committed
3403
3404
3405
3406
      log("f_verify_at_certificate: ", match(p_at_certificate.issuer, mw_issuerIdentifier_self()));
      if (match(p_at_certificate.issuer, mw_issuerIdentifier_self)) {
        return false;
      }
3407
      
Yann Garcia's avatar
Yann Garcia committed
3408
3409
3410
3411
3412
3413
      // Check EC certificate signature
      // TODO Who sign the EC certificate?
      if (f_verifyCertificateSignatureWithPublicKey(p_at_certificate, p_aa_certificate.toBeSigned.verifyKeyIndicator.verificationKey) == false) {
        log("f_verify_at_certificate: Signature not verified");
        return false;
      }
3414
      
Yann Garcia's avatar
Yann Garcia committed
3415
3416
      return true;
    } // End of function f_verify_at_certificate
3417
    
Yann Garcia's avatar
Yann Garcia committed
3418
  } // End of group inner_ec_xxx
garciay's avatar
garciay committed
3419
3420
3421
3422
3423
3424
3425
3426

  group security_function { // TODO To be moved in LibItsSecurity_Function module

    function f_extract_enc_key(
                               in Certificate p_certificate,
                               out octetstring p_public_enc_key,
                               out integer p_compressed_enc_key_mode
                               ) return boolean {
3427
3428
      log(">>> f_extract_enc_key: ", p_certificate);
      
garciay's avatar
garciay committed
3429
3430
      if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256)) {
        if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_0)) {
3431
3432
          p_public_enc_key := p_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_0;
          p_compressed_enc_key_mode := 0;
garciay's avatar
garciay committed
3433
        } else if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_1)) {
3434
3435
          p_public_enc_key := p_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_1;
          p_compressed_enc_key_mode := 1;
garciay's avatar
garciay committed
3436
        } else {
3437
          log("f_extract_enc_key: Non canonical certificate: ", p_certificate);
garciay's avatar
garciay committed
3438
3439
3440
3441
          return false;
        }
      } else if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesBrainpoolP256r1)) {
        if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesBrainpoolP256r1.compressed_y_0)) {
3442
3443
          p_public_enc_key := p_certificate.toBeSigned.encryptionKey.publicKey.eciesBrainpoolP256r1.compressed_y_0;
          p_compressed_enc_key_mode := 0;
garciay's avatar
garciay committed
3444
3445
3446
3447
        } else if (ischosen(p_certificate.toBeSigned.encryptionKey.publicKey.eciesBrainpoolP256r1.compressed_y_1)) {
          p_public_enc_key := p_certificate.toBeSigned.encryptionKey.publicKey.eciesBrainpoolP256r1.compressed_y_1;
          p_compressed_enc_key_mode := 0;
        } else {
3448
          log("f_extract_enc_key: Non canonical certificate: ", p_certificate);
garciay's avatar
garciay committed
3449
3450
3451
          return false;
        }
      } else {
3452
        log("f_extract_enc_key: Invalid certificate: ", p_certificate);
garciay's avatar
garciay committed
3453
3454
3455
3456
3457
3458
3459
        return false;
      }

      return true;
    } // End of function f_extract_enc_key
    
  } // End of group security_function
Yann Garcia's avatar
Yann Garcia committed
3460
  
Yann Garcia's avatar
Yann Garcia committed
3461
  group altsteps {
3462
3463
    
    altstep a_default_pki_http() runs on ItsPkiHttp {
Yann Garcia's avatar
Yann Garcia committed
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(
                                                      mw_http_response(
                                                                       mw_http_response_ko
                                                                       )) {
        tc_ac.stop;
        
        log("*** " & testcasename() & ": FAIL: Unexpected message received ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(mw_http_request) {
3474
        tc_ac.stop;
Yann Garcia's avatar
Yann Garcia committed
3475
3476
3477
3478
3479
3480
        log("*** a_default: ERROR: Unexpected HTTP Request received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(mw_http_response) {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Response received ***");
3481
3482
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3483
3484
3485
3486
3487
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP message received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3488
3489
3490
3491
      [] a_shutdown() {
        log("*** a_default: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
Yann Garcia's avatar
Yann Garcia committed
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
    } // End of altstep a_default_pki_http
    
    altstep a_default_pki_http_ec() runs on ItsPkiHttp {
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive(
                                                   mw_http_response(
                                                                    mw_http_response_ko
                                                                    )) {
        tc_ac.stop;
        
        log("*** " & testcasename() & ": FAIL: Unexpected message received ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive(mw_http_request) {
3505
3506
3507
3508
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Request received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3509
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive(mw_http_response) {
3510
3511
3512
3513
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Response received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3514
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive {
3515
3516
3517
3518
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP message received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3519
3520
3521
3522
      [] a_shutdown() {
        log("*** a_default: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
Yann Garcia's avatar
Yann Garcia committed
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
    } // End of altstep a_default_pki_http_ec
    
    altstep a_default_pki_http_atv() runs on ItsPkiHttp {
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive(
                                                   mw_http_response(
                                                                    mw_http_response_ko
                                                                    )) {
        tc_ac.stop;
        
        log("*** " & testcasename() & ": FAIL: Unexpected message received ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive(mw_http_request) {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Request received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive(mw_http_response) {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Response received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP message received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3550
3551
3552
3553
      [] a_shutdown() {
        log("*** a_default: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
Yann Garcia's avatar
Yann Garcia committed
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
    } // End of altstep a_default_pki_http_atv
    
    altstep a_default_pki_http_at() runs on ItsPkiHttp {
      [PICS_MULTIPLE_END_POINT] httpAtPort.receive(
                                                   mw_http_response(
                                                                    mw_http_response_ko
                                                                    )) {
        tc_ac.stop;
        
        log("*** " & testcasename() & ": FAIL: Unexpected message received ***");
        f_selfOrClientSyncAndVerdictTestBody(c_tbDone, e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtPort.receive(mw_http_request) {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Request received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtPort.receive(mw_http_response) {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP Response received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      [PICS_MULTIPLE_END_POINT] httpAtPort.receive {
        tc_ac.stop;
        log("*** a_default: ERROR: Unexpected HTTP message received ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
3581
3582
3583
3584
      [] a_shutdown() {
        log("*** a_default: INFO: TEST COMPONENT NOW STOPPING ITSELF! ***");
        stop;
      }
Yann Garcia's avatar
Yann Garcia committed
3585
    } // End of altstep a_default_pki_http_at
Yann Garcia's avatar
Yann Garcia committed
3586

Yann Garcia's avatar
Yann Garcia committed
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
    altstep a_await_ec_http_request_from_iut(
                                              template HttpMessage p_http_message,
                                              out HttpMessage p_response
                                              ) runs on ItsPkiHttp {
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(p_http_message) -> value p_response {
        log("a_await_ec_http_request_from_iut: Received message on httpPort");
      }
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive(p_http_message) -> value p_response {
        log("a_await_ec_http_request_from_iut: Received message on httpEcPort");
      }
    } // End of altstep a_await_ec_http_request_from_iut
    
Yann Garcia's avatar
Yann Garcia committed
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
    altstep a_await_ec_http_response_from_iut(
                                              template HttpMessage p_http_message,
                                              out HttpMessage p_response
                                              ) runs on ItsPkiHttp {
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(p_http_message) -> value p_response {
        log("a_await_ec_http_response_from_iut: Received message on httpPort");
      }
      [PICS_MULTIPLE_END_POINT] httpEcPort.receive(p_http_message) -> value p_response {
        log("a_await_ec_http_response_from_iut: Received message on httpEcPort");
      }
    } // End of altstep a_await_ec_http_response_from_iut
    
    altstep a_await_at_http_response_from_iut(
                                              template HttpMessage p_http_message,
                                              out HttpMessage p_response
                                              ) runs on ItsPkiHttp {
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(p_http_message) -> value p_response {
        log("a_await_at_http_response_from_iut: Received message on httpPort");
      }
      [PICS_MULTIPLE_END_POINT] httpAtPort.receive(p_http_message) -> value p_response {
        log("a_await_at_http_response_from_iut: Received message on httpAtPort");
      }
    } // End of altstep a_await_at_http_response_from_iut
    
Yann Garcia's avatar
Yann Garcia committed
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
    altstep a_await_atv_http_request_from_iut(
                                              template HttpMessage p_http_message,
                                              out HttpMessage p_request
                                              ) runs on ItsPkiHttp {
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(p_http_message) -> value p_request {
        log("a_await_atv_http_request_from_iut: Received message on httpPort");
      }
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive(p_http_message) -> value p_request {
        log("a_await_avt_http_request_from_iut: Received message on httpAtVPort");
      }
    } // End of altstep a_await_atv_http_request_from_iut
    
Yann Garcia's avatar
Yann Garcia committed
3635
    altstep a_await_atv_http_response_from_iut(
Yann Garcia's avatar
Yann Garcia committed
3636
3637
3638
3639
                                              template HttpMessage p_http_message,
                                              out HttpMessage p_response
                                              ) runs on ItsPkiHttp {
      [not(PICS_MULTIPLE_END_POINT)] httpPort.receive(p_http_message) -> value p_response {
Yann Garcia's avatar
Yann Garcia committed
3640
        log("a_await_atv_http_response_from_iut: Received message on httpPort");
Yann Garcia's avatar
Yann Garcia committed
3641
3642
3643
3644
      }
      [PICS_MULTIPLE_END_POINT] httpAtVPort.receive(p_http_message) -> value p_response {
        log("a_await_avt_http_response_from_iut: Received message on httpAtVPort");
      }
Yann Garcia's avatar
Yann Garcia committed
3645
    } // End of altstep a_await_atv_http_response_from_iut
Yann Garcia's avatar
Yann Garcia committed
3646
3647
    
  } // End of group altsteps
3648
  
Yann Garcia's avatar
Yann Garcia committed
3649
} // End of module LibItsPki_Functions 
For faster browsing, not all history is shown. View entire blame