LibItsPki_Functions.ttcn 76.7 KB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 *  @author   ETSI / STF544
 *  @version  $URL$
 *            $Id$
 *  @desc     Module containing functions for ITS PKI ATS
 *  @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 LibItsPki_Functions {
    
  // LibCommon
  import from LibCommon_Time all;
  import from LibCommon_VerdictControl all;
  import from LibCommon_Sync all;
  import from LibCommon_BasicTypesAndValues all;
  import from LibCommon_DataStrings all;
  
  // LibIts
  import from IEEE1609dot2BaseTypes language "ASN.1:1997" all;
  import from IEEE1609dot2 language "ASN.1:1997" all;
  import from EtsiTs102941BaseTypes language "ASN.1:1997" all;
  import from EtsiTs102941TypesEnrolment language "ASN.1:1997" all;
26
27
  import from EtsiTs102941TypesAuthorization language "ASN.1:1997" all;
  import from EtsiTs102941TypesAuthorizationValidation language "ASN.1:1997" all;
28
  import from EtsiTs102941MessagesCa language "ASN.1:1997" all;
Yann Garcia's avatar
Yann Garcia committed
29
30
31
32
33
  import from EtsiTs103097Module language "ASN.1:1997" all;
  import from ITS_Container language "ASN.1:1997" all;
  import from CAM_PDU_Descriptions language "ASN.1:1997" all;
  
  // LibItsCommon
garciay's avatar
garciay committed
34
  import from LibItsCommon_TypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
35
36
37
  import from LibItsCommon_Functions all;
  import from LibItsCommon_ASN1_NamedNumbers all;
  import from LibItsCommon_Pixits all;
garciay's avatar
garciay committed
38
  
39
40
41
  // LibItsGeoNetworking
  import from LibItsGeoNetworking_Pixits all;
  
Yann Garcia's avatar
Yann Garcia committed
42
43
44
45
46
  // LibItsSecurity
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Functions all;
  import from LibItsSecurity_Pixits all;
47
  import from LibItsSecurity_TestSystem all;
garciay's avatar
garciay committed
48
  
Yann Garcia's avatar
Yann Garcia committed
49
50
  // LibItsHttp
  import from LibItsHttp_TypesAndValues all;
51
  import from LibItsHttp_Templates all;
Yann Garcia's avatar
Yann Garcia committed
52
  import from LibItsHttp_TestSystem all;
garciay's avatar
garciay committed
53
  
Yann Garcia's avatar
Yann Garcia committed
54
  // LibItsPki
garciay's avatar
garciay committed
55
  import from LibItsPki_TypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
56
  import from LibItsPki_Templates all;
garciay's avatar
garciay committed
57
  import from LibItsPki_Pics all;
garciay's avatar
garciay committed
58
  import from LibItsPki_Pixits all;
Yann Garcia's avatar
Yann Garcia committed
59
  import from LibItsPki_TestSystem all;
garciay's avatar
garciay committed
60
61
  
  group pkiConfigurationFunctions {
Yann Garcia's avatar
Yann Garcia committed
62
63
64
65
66
67
     
    /**
     * @desc    Setups default configuration   
     * @param   p_certificateId The certificate identifier the TA shall use in case of secured IUT
     */
    function f_cfUp(
68
69
                    in charstring p_certificateId := "CERT_TS_A_EA", // TODO Use a constant
                    in charstring p_peerCertificateId := "CERT_IUT_A_EA"
Yann Garcia's avatar
Yann Garcia committed
70
                   ) runs on ItsPki /* TITAN TODO: system ItsPkiSystem */ {
71
      
Yann Garcia's avatar
Yann Garcia committed
72
      map(self:pkiPort, system:pkiPort);
garciay's avatar
garciay committed
73
      map(self:acPort, system:acPort);
Yann Garcia's avatar
Yann Garcia committed
74
75
      f_connect4SelfOrClientSync();
      
76
      f_initialiseSecuredMode(p_certificateId, p_peerCertificateId);
Yann Garcia's avatar
Yann Garcia committed
77
78
      
      f_readCertificate(p_certificateId, vc_eaCertificate);
79
      f_getCertificateDigest(p_certificateId, vc_eaHashedId8);
80
81
      
      activate(a_default_pki());
Yann Garcia's avatar
Yann Garcia committed
82
83
84
85
86
87
88
    } // End of function f_cfUp
    
    /**
     * @desc    Setups default configuration   
     * @param   p_certificateId The certificate identifier the TA shall use in case of secured IUT
     */
    function f_cfHttpUp(
garciay's avatar
garciay committed
89
90
                        in charstring p_ea_certificate_id := "CERT_TS_A_EA", // TODO Use a constant
                        in charstring p_ec_certificate_id := "CERT_TS_A_EC",
91
                        in charstring p_peerCertificateId := "CERT_IUT_A_EA"
garciay's avatar
garciay committed
92
                        ) runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
Yann Garcia's avatar
Yann Garcia committed
93
94
95
      
      map(self:httpPort, system:httpPort);
      f_connect4SelfOrClientSync();
96

garciay's avatar
garciay committed
97
      f_initialiseSecuredMode(p_ea_certificate_id, p_peerCertificateId);
Yann Garcia's avatar
Yann Garcia committed
98
      
garciay's avatar
garciay committed
99
100
101
102
103
104
105
      f_readCertificate(p_ea_certificate_id, vc_eaCertificate);
      f_readSigningKey(p_ea_certificate_id, vc_eaPrivateKey);
      f_readCertificate(p_ec_certificate_id, vc_ecCertificate);
      f_readSigningKey(p_ec_certificate_id, vc_ecPrivateKey);
      f_readEncryptingKey(p_ea_certificate_id, vc_eaPrivateEncKey);
      f_getCertificateDigest(p_ea_certificate_id, vc_eaHashedId8);
      f_getCertificateDigest(p_ec_certificate_id, vc_ecHashedId8);
106
      f_readCertificate(p_peerCertificateId, vc_peerEaCertificate);
107
      f_getCertificateHash(p_peerCertificateId, vc_eaPeerWholeHash);
108
109
      
      activate(a_default_pki_http());
Yann Garcia's avatar
Yann Garcia committed
110
    } // End of function f_cfHttpUp
111
    
112
113
114
115
116
117
118
    function f_cfUp_itss(
                         in charstring p_certificateId := "CERT_TS_A_EA" // TODO Use a constant
                         ) runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      
      f_cfUp();
      
      map(self:geoNetworkingPort, system:geoNetworkingPort);
119
120
      map(self:utPort, system:utPort);
      map(self:acPort, system:acPort);
121
      
122
123
      f_initializeState();
      
124
125
      activate(a_default_pki());
    } // End of function f_cfUp_itss
126

Yann Garcia's avatar
Yann Garcia committed
127
128
129
130
131
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfDown() runs on ItsPki /* TITAN TODO: system ItsPkiSystem */ {
      unmap(self:pkiPort, system:pkiPort);
garciay's avatar
garciay committed
132
      unmap(self:acPort, system:acPort);
Yann Garcia's avatar
Yann Garcia committed
133
      f_disconnect4SelfOrClientSync();
134
      f_uninitialiseSecuredMode();
Yann Garcia's avatar
Yann Garcia committed
135
136
137
138
139
140
141
142
    } // End of function f_cfDown
    
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
      unmap(self:httpPort, system:httpPort);
      f_disconnect4SelfOrClientSync();
143
      f_uninitialiseSecuredMode();
Yann Garcia's avatar
Yann Garcia committed
144
145
    } // End of function f_cfHttpDown
    
146
147
148
149
150
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfDown_itss() runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      unmap(self:geoNetworkingPort, system:geoNetworkingPort);
151
152
      unmap(self:utPort, system:utPort);
      unmap(self:acPort, system:acPort);
153
154
155
156
      
      f_cfDown();
    } // End of function f_cfDown
    
Yann Garcia's avatar
Yann Garcia committed
157
158
159
    /**
     * @desc Initialise secure mode if required
     */
160
161
162
163
    function f_initialiseSecuredMode(
                                     in charstring p_certificateId  := "CERT_TS_A_EA", // TODO Use a constant
                                     in charstring p_peerCertificateId := "CERT_IUT_A_EA"
                                     ) runs on ItsSecurityBaseComponent {
Yann Garcia's avatar
Yann Garcia committed
164
165
166
167
168
      // Local variables
      
      // Load certificates
      if( not f_loadCertificates(PX_IUT_SEC_CONFIG_NAME) ) {
        log("*** INFO: TEST CASE NOW STOPPING ITSELF! ***");
169
        setverdict(inconc);
Yann Garcia's avatar
Yann Garcia committed
170
171
172
        stop;
      }
      
173
      f_prepareCertificates(p_certificateId, vc_aaCertificate, vc_atCertificate);
Yann Garcia's avatar
Yann Garcia committed
174
    } // End of function f_initialiseSecuredMode()
175
176
    
    function f_uninitialiseSecuredMode() runs on ItsSecurityBaseComponent {
Yann Garcia's avatar
Yann Garcia committed
177
178
      f_unloadCertificates();
    } // End of function f_uninitialiseSecuredMode()
179
180
181
182
183
184
185
186
187
188
189
190
    
    function f_initializeState() runs on ItsPkiItss {
      var Oct8 v_hashedId8ToBeUsed := f_setupIutCertificate(vc_hashedId8ToBeUsed);

      f_utInitializeIut(UtPkiInitialize: { v_hashedId8ToBeUsed } );

      f_sleep(PX_NEIGHBOUR_DISCOVERY_DELAY);
      
      //      f_acLoadScenario(p_scenario);
      //      f_acStartScenario();
    }
    
Yann Garcia's avatar
Yann Garcia committed
191
  } // End of pkiConfigurationFunctions
garciay's avatar
garciay committed
192
193
194
195

  group ac_port {

    function f_sendAcPkiPrimitive(
Yann Garcia's avatar
Yann Garcia committed
196
                                  in charstring p_certificate,
garciay's avatar
garciay committed
197
198
199
200
201
202
                                  in octetstring p_private_key,
                                  in octetstring p_publicKeyCompressed,
                                  in integer p_compressedMode
                                  ) runs on ItsPki {
      var AcSetSecurityData v_ac_set_security_data;
      
Yann Garcia's avatar
Yann Garcia committed
203
      v_ac_set_security_data := { p_certificate, PICS_IUT_CERTIFICATE_ID, p_private_key, p_publicKeyCompressed, p_compressedMode };
garciay's avatar
garciay committed
204
      acPort.send(AcPkiPrimitive: { acSetSecurityData := v_ac_set_security_data });
garciay's avatar
garciay committed
205
206
      tc_ac.start;
      alt {
garciay's avatar
garciay committed
207
        [] acPort.receive(AcPkiResponse: { result := true }) {
garciay's avatar
garciay committed
208
209
          tc_ac.stop;
        }
garciay's avatar
garciay committed
210
        [] acPort.receive(AcPkiResponse: { result := false }) {
garciay's avatar
garciay committed
211
212
213
214
215
216
217
218
219
          tc_ac.stop;
          log("*** f_sendAcPkiPrimitive: ERROR: Received unexpected message ***");
          f_selfOrClientSyncAndVerdict("error", e_error);
        }
        [] tc_ac.timeout {
          log("*** f_sendAcPkiPrimitive: ERROR: Timeout while waiting for adapter control event result ***");
          f_selfOrClientSyncAndVerdict("error", e_timeout);
        }
      } // End of 'alt' statement
220
    } // End of function f_sendAcPkiPrimitive
garciay's avatar
garciay committed
221
222
    
  } // End of group ac_port
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  group ut_port {

    function f_utInitializeIut(template (value) UtPkiInitialize p_init) runs on ItsPkiItss {
      
      utPort.send(p_init);
      tc_wait.start;
      alt {
        [] utPort.receive(UtPkiResults: { utPkiInitializeResult := true }) {
          tc_wait.stop;
          log("*** f_utInitializeIut: INFO: IUT initialized ***");
        }
        [] utPort.receive {
          tc_wait.stop;
          log("*** f_utInitializeIut: INFO: IUT could not be initialized ***");
          f_selfOrClientSyncAndVerdict("error", e_error);
        }
        [] tc_wait.timeout {
          log("*** f_utInitializeIut: INFO: IUT could not be initialized in time ***");
          f_selfOrClientSyncAndVerdict("error", e_timeout);
        }
      }
      
    } // End of function f_utInitializeIut
    
    function f_sendUtTriggerPrimitive(
249
250
                                      in charstring p_cannonicalId,
                                      in Oct1 p_encAlgorithm,
251
252
253
254
255
256
257
258
259
260
261
262
263
                                      in octetstring p_private_key,
                                      in octetstring p_publicKeyCompressed,
                                      in integer p_compressedMode
                                      ) runs on ItsPkiItss {
      var TriggerEnrolmentRequest v_ut_trigger_enrolment_request;
      var octetstring v_compressed_public_key;

      if (p_compressedMode == 2) { // TODO v_compressed_public_key := int2oct(p_compressedMode, 1) & p_publicKeyCompressed?
        v_compressed_public_key := '02'O & p_publicKeyCompressed;
      } else {
        v_compressed_public_key := '03'O & p_publicKeyCompressed;
      }
      
264
      v_ut_trigger_enrolment_request := { p_cannonicalId, p_encAlgorithm, p_private_key, v_compressed_public_key };
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
      utPort.send(UtPkiTrigger: { triggerEnrolmentRequest := v_ut_trigger_enrolment_request });
      tc_ac.start;
      alt {
        [] utPort.receive(UtPkiResults: { utPkiTriggerResult := true }) {
          tc_ac.stop;
        }
        [] utPort.receive(UtPkiResults: { utPkiTriggerResult := false }) {
          tc_ac.stop;
          log("*** f_sendUtTriggerPrimitive: ERROR: Received unexpected message ***");
          f_selfOrClientSyncAndVerdict("error", e_error);
        }
        [] tc_ac.timeout {
          log("*** f_sendAcPkiPrimitive: ERROR: Timeout while waiting for adapter control event result ***");
          f_selfOrClientSyncAndVerdict("error", e_timeout);
        }
      } // End of 'alt' statement
    } // End of function f_sendUtTriggerPrimitive
    
  } // End of group ut_port

285
286
  group http {

Yann Garcia's avatar
Yann Garcia committed
287
    function f_http_build_inner_ec_request(
garciay's avatar
garciay committed
288
289
290
291
292
293
                                           out octetstring p_private_key,
                                           out octetstring p_publicKeyCompressed,
                                           out integer p_compressedMode,
                                           out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
                                           out Oct32 p_hash_inner_ec_request_signed_for_pop
                                           ) runs on ItsPkiHttp {
Yann Garcia's avatar
Yann Garcia committed
294
295
      var octetstring v_publicKeyX;
      var octetstring v_publicKeyY
296
297
298
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
      var bitstring v_inner_ec_request_signed_for_pop_msg;
garciay's avatar
garciay committed
299
300
301
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
302
      
Yann Garcia's avatar
Yann Garcia committed
303
304
      if (f_generate_inner_ec_request(p_private_key, p_publicKeyCompressed, p_compressedMode, v_inner_ec_request) == false) {
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequest ***");
305
306
307
308
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      // Generate InnerEcRequestSignedForPoP
      if (f_generate_inner_ec_request_signed_for_pop(p_private_key, v_inner_ec_request, v_inner_ec_request_signed_for_pop) == false) {
Yann Garcia's avatar
Yann Garcia committed
309
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
310
311
312
313
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      // Secure InnerEcRequestSignedForPoP message
      v_inner_ec_request_signed_for_pop_msg := encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop));
garciay's avatar
garciay committed
314
      // Prepare for getting the 16 most bytes of SHA256 of InnerEcRequestSignedForPop
315
      p_hash_inner_ec_request_signed_for_pop := f_hashWithSha256(bit2oct(v_inner_ec_request_signed_for_pop_msg));
garciay's avatar
garciay committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
      // Secure the Pki message
      if (f_extract_enc_key(vc_eaCertificate, v_public_enc_key, v_compressed_enc_key_mode) == false) {
        log("*** f_http_build_inner_ec_request: ERROR: Non canonical EA certificate ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      log("*** f_http_build_inner_ec_request: Public encryption key: ", v_public_enc_key);
      log("*** f_http_build_inner_ec_request: Public encryption key comp: ", v_compressed_enc_key_mode);
      log("*** f_http_build_inner_ec_request: First enrolment: ", PX_FIRST_ENROLMENT);
      if (PX_FIRST_ENROLMENT == true) { // This is the first enrolment, we used Factory keys
        v_ret_code := f_build_pki_secured_message(PICS_ITS_S_SIGN_NITSP256_PRIVATE_KEY, valueof(m_signerIdentifier_self), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, bit2oct(v_inner_ec_request_signed_for_pop_msg), p_ieee1609dot2_signed_and_encrypted_data);
      } else { // We use last valid EC certificate
        v_ret_code := f_build_pki_secured_message(vc_ecPrivateKey, valueof(m_signerIdentifier_digest(vc_ecHashedId8)), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, bit2oct(v_inner_ec_request_signed_for_pop_msg), p_ieee1609dot2_signed_and_encrypted_data);
      }
      if (v_ret_code == false) {
Yann Garcia's avatar
Yann Garcia committed
330
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
331
332
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
333
334
335
336
      log("*** f_http_build_inner_ec_request: DEBUG: v_inner_ec_request_signed_for_pop_msg= ", v_inner_ec_request_signed_for_pop_msg);
      log("*** f_http_build_inner_ec_request: DEBUG: p_ieee1609dot2_signed_and_encrypted_data= ", p_ieee1609dot2_signed_and_encrypted_data);
      log("*** f_http_build_inner_ec_request: DEBUG: p_hash_inner_ec_request_signed_for_pop= ", p_hash_inner_ec_request_signed_for_pop);
    } // End of function f_http_build_inner_ec_request
337

338
339
340
341
342
343
344
345
346
    function f_http_build_invalid_enrolment_request(
                                                    out octetstring p_private_key,
                                                    out octetstring p_publicKeyCompressed,
                                                    out integer p_compressedMode,
                                                    out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
                                                    out Oct32 p_hash_inner_ec_request_signed_for_pop
                                                    ) runs on ItsPkiHttp {
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
garciay's avatar
garciay committed
347
348
349
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
350
      
Yann Garcia's avatar
Yann Garcia committed
351
      if (f_generate_inner_ec_request(p_private_key, p_publicKeyCompressed, p_compressedMode, v_inner_ec_request) == false) {
352
353
354
355
356
357
358
359
360
361
        log("*** f_http_build_invalid_enrolment_request: ERROR: Failed to generate InnerEcRequest ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      // Generate InnerEcRequestSignedForPoP
      if (f_generate_inner_ec_request_signed_for_pop(p_private_key, v_inner_ec_request, v_inner_ec_request_signed_for_pop) == false) {
        log("*** f_http_build_invalid_enrolment_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      // Secure InnerEcRequestSignedForPoP message
      // Get 16 last byte of SHA256 of InnerEcRequestSignedForPop
362
      p_hash_inner_ec_request_signed_for_pop := f_hashWithSha256(int2oct(12345, 32)); // Invalid InnseEcRequestSignedForPop
garciay's avatar
garciay committed
363
364
365
366
367
368
369
370
371
372
373
374
375
      // Secure the Pki message
      if (f_extract_enc_key(vc_eaCertificate, v_public_enc_key, v_compressed_enc_key_mode) == false) {
        log("*** f_http_build_inner_ec_request: ERROR: Non canonical EA certificate ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      log("*** f_http_build_inner_ec_request: Public encryption key: ", v_public_enc_key);
      log("*** f_http_build_inner_ec_request: Public encryption key comp: ", v_compressed_enc_key_mode);
      if (PX_FIRST_ENROLMENT == true) { // This is the first enrolment, we used Factory keys
        v_ret_code := f_build_pki_secured_message(PICS_ITS_S_SIGN_NITSP256_PRIVATE_KEY, valueof(m_signerIdentifier_self), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), p_ieee1609dot2_signed_and_encrypted_data);
      } else { // We use last valid EC certificate
        v_ret_code := f_build_pki_secured_message(vc_ecPrivateKey, valueof(m_signerIdentifier_digest(vc_ecHashedId8)), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), p_ieee1609dot2_signed_and_encrypted_data);
      }
      if (v_ret_code == false) {
376
377
378
379
        log("*** f_http_build_invalid_enrolment_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      log("*** f_http_build_invalid_enrolment_request: DEBUG: p_ieee1609dot2_signed_and_encrypted_data = ", p_ieee1609dot2_signed_and_encrypted_data);
380
    } // End of function f_http_build_invalid_enrolment_request
garciay's avatar
garciay committed
381
382
383
384
385
386
387
388
389
390
391

    function f_http_build_inner_at_request(
                                           out octetstring p_private_key,
                                           out octetstring p_publicKeyCompressed,
                                           out integer p_compressedMode,
                                           out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
                                           out HashedId8 p_hash_inner_at_request
                                           ) runs on ItsPkiHttp {
      // Local variables
      
    } // End of function f_http_build_inner_at_request
392
    
Yann Garcia's avatar
Yann Garcia committed
393
    function f_http_build_authorization_validation_request(
garciay's avatar
garciay committed
394
395
396
397
398
399
400
                                                           out octetstring p_private_key,
                                                           out octetstring p_publicKeyCompressed,
                                                           out integer p_compressedMode,
                                                           out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data
                                                           ) runs on ItsPkiHttp {
      // Local variables
      var AuthorizationValidationRequest v_authorization_validation_request;
401
      var bitstring v_authorization_validation_request_msg;
garciay's avatar
garciay committed
402
403
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
garciay's avatar
garciay committed
404
405
406
407
408
      
      if (f_generate_authorization_validation_request(vc_eaCertificate, vc_eaHashedId8, p_private_key, p_publicKeyCompressed, p_compressedMode, v_authorization_validation_request) == false) {
        log("*** f_http_build_authorization_validation_request: ERROR: Failed to generate AuthorizationValidationRequest ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
garciay's avatar
garciay committed
409
410
411
      // Secure the Pki message
      if (f_extract_enc_key(vc_eaCertificate, v_public_enc_key, v_compressed_enc_key_mode) == false) {
        log("*** f_http_build_inner_ec_request: ERROR: Non canonical EA certificate ***");
garciay's avatar
garciay committed
412
413
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
garciay's avatar
garciay committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
      log("*** f_http_build_authorization_validation_request: Public encryption key: ", v_public_enc_key);
      log("*** f_http_build_authorization_validation_request: Public encryption key comp: ", v_compressed_enc_key_mode);


      /**
         TODO: Load certificate according to the IUT role
         ==> a complete fucntion which set up the TestSustem certificate,keyy... according to the IUT role
       **/
      log("*** f_http_build_inner_ec_request: ERROR: Need to add TestSystem variable vc_aa ***");
      f_selfOrClientSyncAndVerdict("error", e_error);
      /* if (f_build_pki_secured_message(vc_aaPrivateKey, valueof(m_signerIdentifier_digest(vc_aaHashedId8)), vc_eaHashedId8/\*recipientId*\/, v_public_enc_key, v_compressed_enc_key_mode, bit2oct(encvalue(m_etsiTs102941Data_authorization_validation_request(v_authorization_validation_request))), p_ieee1609dot2_signed_and_encrypted_data) == false) { */
      /*   log("*** f_http_build_authorization_validation_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***"); */
      /*   f_selfOrClientSyncAndVerdict("error", e_error); */
      /* } */
garciay's avatar
garciay committed
428
429
430
      
      log("*** f_http_build_authorization_validation_request: DEBUG: p_ieee1609dot2_signed_and_encrypted_data = ", p_ieee1609dot2_signed_and_encrypted_data);
    } // End of function f_http_build_authorization_validation_request
Yann Garcia's avatar
Yann Garcia committed
431
    
432
  } // End of group http
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540

  group generate_certificates {

    function f_generate_ec_certificate(
                                       in octetstring p_private_key,
                                       in InnerEcRequest p_inner_ec_request,
                                       out Certificate p_ec_certificate
                                       ) return boolean {
      var SequenceOfPsidSsp v_appPermissions := { // ETSI TS 102 965 Table A.1: ETSI ITS standardized ITS-AIDs
        valueof(m_appPermissions(36, { bitmapSsp := '830001'O })), 
        valueof(m_appPermissions(37, { bitmapSsp := '830001'O })) 
      };
      var template (value) EtsiTs103097Certificate v_cert;
      var bitstring v_tbs;
      var Oct32 v_sig;
      var bitstring v_enc_msg;
      
      v_cert := m_etsiTs103097Certificate(
                                          m_issuerIdentifier_sha256AndDigest(f_HashedId8FromSha256(f_hashWithSha256('616263'O))),
                                          m_toBeSignedCertificate_at(
                                                                     v_appPermissions,
                                                                     m_verificationKeyIndicator_verificationKey(
                                                                                                                m_publicVerificationKey_ecdsaNistP256(
                                                                                                                                                      p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256
                                                                                                                                                      )),
                                                                     m_validityPeriod(
                                                                                      17469212,
                                                                                      m_duration_years(10)
                                                                                      ),
                                                                     m_geographicRegion_identifiedRegion(
                                                                                                         {
                                                                                                           m_identifiedRegion_country_only(12), 
                                                                                                           m_identifiedRegion_country_only(34)
                                                                                                           }
                                                                                                         )
                                                                     )
                                          );
      // Encode it ==> Get octetstring
      v_tbs := encvalue(v_cert.toBeSigned);
      // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
      v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_tbs), int2oct(11, 32), p_private_key);
      v_cert.signature_ := m_signature_ecdsaNistP256(
                                                     m_ecdsaP256Signature(
                                                                          m_eccP256CurvePoint_x_only(
                                                                                                     substr(v_sig, 0, 32)
                                                                                                     ),
                                                                          substr(v_sig, 32, 32)
                                                                          )
                                                     );
      log("v_cert= ", v_cert);
      
      p_ec_certificate := valueof(v_cert);
      return true;
    } // End of function f_generate_ec_certificate
    
    function f_generate_at_certificate(
                                       in octetstring p_private_key,
                                       in InnerEcRequest p_inner_ec_request,
                                       out Certificate p_at_certificate
                                       ) return boolean {
      var SequenceOfPsidSsp v_appPermissions := { // ETSI TS 102 965 Table A.1: ETSI ITS standardized ITS-AIDs
        valueof(m_appPermissions(36, { bitmapSsp := '830001'O })), 
        valueof(m_appPermissions(37, { bitmapSsp := '830001'O })) 
      };
      var template (value) EtsiTs103097Certificate v_cert;
      var bitstring v_tbs;
      var Oct32 v_sig;
      var bitstring v_enc_msg;
      
      v_cert := m_etsiTs103097Certificate(
                                          m_issuerIdentifier_sha256AndDigest(f_HashedId8FromSha256(f_hashWithSha256('616263'O))),
                                          m_toBeSignedCertificate_at(
                                                                     v_appPermissions,
                                                                     m_verificationKeyIndicator_verificationKey(
                                                                                                                m_publicVerificationKey_ecdsaNistP256(
                                                                                                                                                      p_inner_ec_request.publicKeys.verificationKey.ecdsaNistP256
                                                                                                                                                      )),
                                                                     m_validityPeriod(
                                                                                      17469212,
                                                                                      m_duration_years(10)
                                                                                      ),
                                                                     m_geographicRegion_identifiedRegion(
                                                                                                         {
                                                                                                           m_identifiedRegion_country_only(12), 
                                                                                                           m_identifiedRegion_country_only(34)
                                                                                                           }
                                                                                                         )
                                                                     )
                                          );
      // Encode it ==> Get octetstring
      v_tbs := encvalue(v_cert.toBeSigned);
      // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
      v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_tbs), int2oct(11, 32), p_private_key);
      v_cert.signature_ := m_signature_ecdsaNistP256(
                                                     m_ecdsaP256Signature(
                                                                          m_eccP256CurvePoint_x_only(
                                                                                                     substr(v_sig, 0, 32)
                                                                                                     ),
                                                                          substr(v_sig, 32, 32)
                                                                          )
                                                     );
      log("v_cert= ", v_cert);
      
      p_at_certificate := valueof(v_cert);
      return true;
    } // End of function f_generate_at_certificate
    
  } // End of group generate_certificates 
Yann Garcia's avatar
Yann Garcia committed
541
542
543
  
  group inner_ec_xxx {
    
garciay's avatar
garciay committed
544
    function f_generate_inner_ec_request(
545
546
                                         out octetstring p_private_key,
                                         out octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
547
548
549
                                         out integer p_compressedMode,
                                         out InnerEcRequest p_inner_ec_request
                                         ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
550
      // Local variables
Yann Garcia's avatar
Yann Garcia committed
551
552
      var Oct32 v_publicKeyX;
      var Oct32 v_publicKeyY;
553
554
555
      var EccP256CurvePoint v_eccP256_curve_point;

      log (">>> f_generate_inner_ec_request");
Yann Garcia's avatar
Yann Garcia committed
556
      // Generate keys for the certificate to be requested
Yann Garcia's avatar
Yann Garcia committed
557
      if (f_generate_key_pair_nistp256(p_private_key, v_publicKeyX, v_publicKeyY, p_publicKeyCompressed, p_compressedMode) == false) {
558
        log ("f_generate_inner_ec_request: failed to generate keys");
Yann Garcia's avatar
Yann Garcia committed
559
560
561
        return false;
      }
      if (p_compressedMode == 0) {
562
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
563
      } else {
564
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
565
      }
566
      log ("f_generate_inner_ec_request: ", v_eccP256_curve_point);
garciay's avatar
garciay committed
567
      // Build the Proof of Possession InnerEcRequest
Yann Garcia's avatar
Yann Garcia committed
568
569
      p_inner_ec_request := valueof(
                                    m_innerEcRequest(
570
                                                     PICS_ITS_S_CANONICAL_ID,
Yann Garcia's avatar
Yann Garcia committed
571
                                                     m_publicKeys(
572
                                                                  m_publicVerificationKey_ecdsaNistP256(v_eccP256_curve_point)
Yann Garcia's avatar
Yann Garcia committed
573
574
575
                                                                  ),
                                                     m_certificateSubjectAttributes(
                                                                                    { // ETSI TS 102 965 Table A.1: ETSI ITS standardized ITS-AIDs
garciay's avatar
garciay committed
576
577
                                                                                      valueof(m_appPermissions(36, { bitmapSsp := '830001'O })), // TODO Use PIXIT
                                                                                      valueof(m_appPermissions(37, { bitmapSsp := '830001'O })) // TODO Use PIXIT
Yann Garcia's avatar
Yann Garcia committed
578
579
                                                                                    },
                                                                                    m_validityPeriod(
580
581
582
                                                                                                     17469212,
                                                                                                     m_duration_years(10) // TODO Use PIXIT
                                                                                                     ),
Yann Garcia's avatar
Yann Garcia committed
583
                                                                                    m_geographicRegion_identifiedRegion(
584
585
586
587
588
                                                                                                                        {
                                                                                                                          m_identifiedRegion_country_only(12), // TODO Use PIXIT
                                                                                                                          m_identifiedRegion_country_only(34) // TODO Use PIXIT
                                                                                                                          }
                                                                                                                        ),
garciay's avatar
garciay committed
589
                                                                                    'C0'O // TODO Use PIXIT
590
591
                                                                                    )
                                                     )
garciay's avatar
garciay committed
592
                                    );
593
      log ("f_generate_inner_ec_request: ", p_inner_ec_request);
Yann Garcia's avatar
Yann Garcia committed
594
595
      
      return true;
garciay's avatar
garciay committed
596
    } // End of function f_generate_inner_ec_request
Yann Garcia's avatar
Yann Garcia committed
597
    
garciay's avatar
garciay committed
598
    function f_generate_inner_ec_request_signed_for_pop(
599
                                                        in octetstring p_private_key,
garciay's avatar
garciay committed
600
601
                                                        in InnerEcRequest p_inner_ec_request,
                                                        out Ieee1609Dot2Data p_inner_ec_request_signed_for_pop
Yann Garcia's avatar
Yann Garcia committed
602
603
604
605
606
    ) return boolean {
      // Local variables
      var template (value) EccP256CurvePoint v_eccP256_curve_point;
      var octetstring v_encoded_inner_ec_request;
      var template (value) ToBeSignedData v_tbs;
607
      var octetstring v_tbs_signed;
garciay's avatar
garciay committed
608
609
610
611
612
613
614
615
616
617
618
619
      
      // Encode it
      v_encoded_inner_ec_request := bit2oct(encvalue(p_inner_ec_request));
      // Signed the encoded InnerEcRequestSignedForPop
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload(
                                                    m_etsiTs103097Data_unsecured(
                                                                                 v_encoded_inner_ec_request
                                                                                 )
                                                   ),
                                m_headerInfo_inner_ec_request(
                                                              c_its_aid_SCR,
620
621
                                                              f_getCurrentTimeUtc()
                                                              )
garciay's avatar
garciay committed
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
                               );
      // Signed the encoded InnerEcRequestSignedForPop
      v_tbs_signed := f_signWithEcdsaNistp256WithSha256(bit2oct(encvalue(v_tbs)), int2oct(0, 32), p_private_key);
      // Finalyse signed InnerEcRequestSignedForPop
      p_inner_ec_request_signed_for_pop := valueof(
                                                   m_etsiTs103097Data_signed(
                                                                             m_signedData(
                                                                                          sha256,
                                                                                          v_tbs,
                                                                                          m_signerIdentifier_self,
                                                                                          m_signature_ecdsaNistP256(
                                                                                                                    m_ecdsaP256Signature(
                                                                                                                                         m_eccP256CurvePoint_x_only(
                                                                                                                                                                    substr(v_tbs_signed, 0, 32)
                                                                                                                                                                    ),
                                                                                                                                         substr(v_tbs_signed, 32, 32)
                                                                                                                                         )
                                                                                                                    )
                                                                                          )
                                                                             )
                                                   );
      return true;
    } // End of function f_generate_inner_ec_request_signed_for_pop
    
646
    function f_generate_inner_ec_response(
647
                                          in octetstring p_inner_ec_request_hash,
648
649
650
651
652
653
654
655
                                          in EtsiTs103097Certificate p_certificate,
                                          out InnerEcResponse p_inner_ec_response
                                         ) return boolean {
      // Local variables
      
      // Build the Proof of Possession InnerEcResponse
      p_inner_ec_response := valueof(
                                     m_innerEcResponse_ok(
656
                                                          substr(p_inner_ec_request_hash, 0, 16),
657
658
659
660
661
662
663
                                                          p_certificate
                                                          )
                                     );
      
      return true;
    } // End of function f_generate_inner_ec_response
    
garciay's avatar
garciay committed
664
665
  } // End of group inner_ec_xxx

666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
  group inner_at_xxx {

    function f_generate_inner_at_request(
                                         in Certificate p_ec_certificate,
                                         in octetstring p_private_key,
                                         in Certificate p_ea_certificate,
                                         in HashedId8 p_eaHashedId8,
                                         in boolean p_itss_privacy,
                                         out octetstring p_private_enc_key,
                                         out octetstring p_publicEncKeyCompressed,
                                         out integer p_compressedMode,
                                         out InnerAtRequest p_inner_at_request,
                                         out Ieee1609Dot2Data p_inner_at_request_data
                                         ) return boolean {
      // Local variables
Yann Garcia's avatar
Yann Garcia committed
681
682
      var octetstring public_enc_key_x;
      var octetstring public_enc_key_y;
683
684
685
686
687
688
689
690
691
692
693
      var template (value) EccP256CurvePoint v_eccP256_curve_point;
      var Oct32 v_hmac_key;
      var octetstring v_message_to_tag;
      var Oct16 v_key_tag;
      var octetstring v_whole_certificate_hash;
      var octetstring v_hash_shared_at_request;
      var template (value) ToBeSignedData v_tbs;
      var octetstring v_tbs_signed;
      var Ieee1609Dot2Data v_signed_ec_signature;
      
      // Generate encryption keys for the certificate to be requested
Yann Garcia's avatar
Yann Garcia committed
694
      if (f_generate_key_pair_nistp256(p_private_enc_key, public_enc_key_x, public_enc_key_y, p_publicEncKeyCompressed, p_compressedMode) == false) {
695
696
697
698
699
700
701
702
703
        return false;
      }
      if (p_compressedMode == 0) {
        v_eccP256_curve_point := m_eccP256CurvePoint_compressed_y_0(p_publicEncKeyCompressed);
      } else {
        v_eccP256_curve_point := m_eccP256CurvePoint_compressed_y_1(p_publicEncKeyCompressed);
      }
      
      // Generate 32 octets length secret key
garciay's avatar
garciay committed
704
      v_hmac_key := f_hashWithSha256(int2oct(f_getCurrentTime(), 12));
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
      log("v_hmac_key=", v_hmac_key);

      // Generate tag based on the concatenation of ???
      if (ischosen(p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey)) {
        if (ischosen(p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaNistP256)) {
          if (ischosen(p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaNistP256.compressed_y_0)) {
          v_message_to_tag := '02'O & p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaNistP256.compressed_y_0;
          } else {
          v_message_to_tag := '03'O & p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaNistP256.compressed_y_1;
          }
        } else {
          // TODO
          /* if (ischosen(p_ec_certificate.verifyKeyIndicator.ecdsaBrainpoolP256.compressed_y_0)) { */
          /*   v_message_to_tag := '02'O & p_ec_certificate.verifyKeyIndicator.ecdsaBrainpoolP256.compressed_y_0; */
          /* } else { */
          /*   v_message_to_tag := '03'O & p_ec_certificate.verifyKeyIndicator.ecdsaBrainpoolP256.compressed_y_1; */
          /* } */
        }
      } else {
        return false;
      }
      if (p_compressedMode == 0) {
        v_message_to_tag := v_message_to_tag & '02'O & p_publicEncKeyCompressed;
      } else {
        v_message_to_tag := v_message_to_tag & '03'O & p_publicEncKeyCompressed;
      }
      log("v_message_to_tag=", v_message_to_tag);
      v_key_tag := fx_test_hmac_sha256(v_hmac_key, v_message_to_tag); // TODO Rename and use a wrapper function
      log("v_key_tag=", v_key_tag);

      // Build the SharedAtRequest
      p_inner_at_request.publicKeys := valueof(
                                               m_publicKeys(
                                                            m_publicVerificationKey_ecdsaNistP256(
                                                                                                  p_ec_certificate.toBeSigned.verifyKeyIndicator.verificationKey.ecdsaNistP256
                                                                                                  ),
                                                            m_encryptionKey(
                                                                            -,
                                                                            m_publicEncryptionKey_ecdsaNistP256(v_eccP256_curve_point)
                                                                            )
                                                            )
                                               );
      p_inner_at_request.hmacKey := v_hmac_key;
      p_inner_at_request.sharedAtRequest  := valueof(
                                                     m_shared_at_request(
                                                                         p_eaHashedId8,
                                                                         v_key_tag,
                                                                         valueof(
753
754
755
                                                                                 m_certificate_subject_attributes( // FIXME Review subjectPermissions
                                                                                                                  p_ec_certificate.toBeSigned.appPermissions,
                                                                                                                  { { subjectPermissions := { all_ := NULL }, minChainLength := 1, chainLengthRange   := 0, eeType := '00000000'B } },
756
757
758
                                                                                                                  p_ec_certificate.toBeSigned.id,
                                                                                                                  p_ec_certificate.toBeSigned.validityPeriod,
                                                                                                                  p_ec_certificate.toBeSigned.region,
759
                                                                                                                  p_ec_certificate.toBeSigned.assuranceLevel
760
761
762
763
764
765
766
767
768
769
                                                                                                                  ))));
      v_hash_shared_at_request := f_hashWithSha256(bit2oct(encvalue(p_inner_at_request.sharedAtRequest)));
      log("v_hash_shared_at_request=", v_hash_shared_at_request);
      // Build the ToBeSigned payload
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload_ext(v_hash_shared_at_request),
                                m_headerInfo_inner_ec_request(
                                                              c_its_aid_SCR,
                                                              f_getCurrentTimeUtc())
                               );
garciay's avatar
garciay committed
770
      log("v_tbs=", v_tbs);
771
772
      // Signed ToBeSigned payload
      v_whole_certificate_hash := f_hashWithSha256(bit2oct(encvalue(p_ec_certificate)));
garciay's avatar
garciay committed
773
      log("v_whole_certificate_hash=", v_whole_certificate_hash);
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
      v_tbs_signed := f_signWithEcdsaNistp256WithSha256(bit2oct(encvalue(v_tbs)), v_whole_certificate_hash, p_private_key);
      v_signed_ec_signature := valueof(
                                       m_etsiTs103097Data_signed(
                                                                 m_signedData(
                                                                              sha256,
                                                                              v_tbs,
                                                                              m_signerIdentifier_digest(
                                                                                                        f_HashedId8FromSha256(
                                                                                                                              v_whole_certificate_hash
                                                                                                                              )
                                                                                                        ),
                                                                              m_signature_ecdsaNistP256(
                                                                                                        m_ecdsaP256Signature(
                                                                                                                             m_eccP256CurvePoint_x_only(
                                                                                                                                                        substr(v_tbs_signed, 0, 32)
                                                                                                                                                        ),
                                                                                                                             substr(v_tbs_signed, 32, 32)
                                                                                                                             )
                                                                                                        )
                                                                              )
                                                                 )
                                       );
      // Encrypt EcSignature with EA certificate
      if (p_itss_privacy) {
        var octetstring v_public_enc_key;
        var integer v_compressed_mode;
        var Oct12 v_nonce;
        var Oct16 v_authentication_vector;
        var Oct16 v_encrypted_sym_key;
        var HashedId8 v_recipientId;
        var octetstring v_publicEphemeralKeyCompressed;
        var integer v_ephemeralKeyModeCompressed;
        var octetstring v_enc_signed_ec_signature;
        
garciay's avatar
garciay committed
808
809
810
811
812
813
814
815
816
        if (ischosen(p_ea_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_0)) {
          v_public_enc_key := p_ea_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_0;
          v_compressed_mode := 0;
        } else if (ischosen(p_ea_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_1)) {
          v_public_enc_key := p_ea_certificate.toBeSigned.encryptionKey.publicKey.eciesNistP256.compressed_y_1;
          v_compressed_mode := 1;
        } else {
          return false;
        }
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
        v_enc_signed_ec_signature := f_encryptWithEciesNistp256WithSha256(bit2oct(encvalue(v_signed_ec_signature)), v_public_enc_key, v_compressed_mode, v_publicEphemeralKeyCompressed, v_ephemeralKeyModeCompressed, v_encrypted_sym_key, v_authentication_vector, v_nonce);
        v_recipientId := p_eaHashedId8;
        log("v_recipientId=", v_recipientId);
        // Fill Certificate template with the public compressed keys (canonical form)
        if (v_ephemeralKeyModeCompressed == 0) {
          v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(v_publicEphemeralKeyCompressed));
        } else {
          v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(v_publicEphemeralKeyCompressed));
        }
        p_inner_at_request.ecSignature := valueof(
                                                  m_ec_signature(
                                                                 m_etsiTs103097Data_encrypted(
                                                                                              m_encryptedData(
                                                                                                              {
                                                                                                                m_recipientInfo_signedDataRecipInfo(
                                                                                                                                                    m_pKRecipientInfo(
                                                                                                                                                                      v_recipientId,
                                                                                                                                                                      m_encryptedDataEncryptionKey_eciesNistP256(
                                                                                                                                                                                                                 m_evciesP256EncryptedKey(
                                                                                                                                                                                                                                          v_eccP256_curve_point,
                                                                                                                                                                                                                                          v_encrypted_sym_key, 
                                                                                                                                                                                                                                          v_authentication_vector
                                                                                                                                                                                                                                          ))))
                                                                                                                },
                                                                                                              m_SymmetricCiphertext_aes128ccm(
                                                                                                                                              m_aesCcmCiphertext(
                                                                                                                                                                 v_nonce, 
                                                                                                                                                                 v_enc_signed_ec_signature
                                                                                                                                                                 )
                                                                                                                                              )
                                                                                                              )
                                                                               )
                                                                 )
                                                  );
      } else {
        p_inner_at_request.ecSignature := valueof(m_ec_signature_ext_payload(v_signed_ec_signature));
      }
      log("p_inner_at_request=", p_inner_at_request);
      
      return true;
    } // End of function f_generate_inner_at_request
    
    function f_generate_inner_at_response(
                                          in Certificate p_ec_certificate,
                                          in octetstring p_private_key,
                                          in HashedId8 p_eaHashedId8,
                                          out octetstring p_private_enc_key,
                                          out octetstring p_publicEncKeyX,
                                          out octetstring p_publicEncKeyY,
                                          out octetstring p_publicEncKeyCompressed,
                                          out integer p_compressedMode,
                                          out InnerAtRequest p_inner_at_request,
                                          out Ieee1609Dot2Data p_inner_at_request_data
                                          ) return boolean {
      // Local variables
      return false;
    } // End of function f_generate_inner_at_response
    
  } // End of group inner_at_xxx
  
  group authorization_validation_xxx {
    
    function f_generate_authorization_validation_request(
garciay's avatar
garciay committed
880
881
882
883
884
                                                         in Certificate p_ea_certificate,
                                                         in HashedId8 p_ea_certificate_hashed_id8,
                                                         out octetstring p_private_key,
                                                         out octetstring p_publicKeyCompressed,
                                                         out integer p_compressedMode,
885
886
                                                         out AuthorizationValidationRequest p_authorization_validation_request
                                                         ) return boolean {
garciay's avatar
garciay committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
      // Local variables
      var octetstring v_private_enc_key;
      var Oct32 v_publicEncKeyCompressed;
      var integer v_compressedMode;
      var InnerEcRequest v_inner_ec_request;
      var Certificate v_ec_certificate;
      var InnerAtRequest v_inner_at_request;
      var Ieee1609Dot2Data v_inner_at_request_data;
      
      if (f_generate_inner_ec_request(p_private_key, p_publicKeyCompressed, p_compressedMode, v_inner_ec_request) == false) {
        return false;
      }
      if (f_generate_ec_certificate(p_private_key, v_inner_ec_request, v_ec_certificate) == false) {
        return false;
      }
      if (f_generate_inner_at_request(v_ec_certificate, p_private_key, p_ea_certificate, p_ea_certificate_hashed_id8, true, v_private_enc_key, v_publicEncKeyCompressed, v_compressedMode, v_inner_at_request, v_inner_at_request_data) == false) {
        return false;
      }
      
      p_authorization_validation_request.sharedAtRequest := v_inner_at_request.sharedAtRequest;
      p_authorization_validation_request.ecSignature := v_inner_at_request.ecSignature;
      
      log("f_generate_authorization_validation_request: ", p_authorization_validation_request);
Yann Garcia's avatar
Yann Garcia committed
910
911
      
      return true;
912
913
914
    } // End of function f_generate_authorization_validation_request
    
    function f_generate_authorization_validation_response(
915
916
                                                          in octetstring p_authorization_validation_request_hash,
                                                          in CertificateSubjectAttributes p_Certificate_subject_attributes,
Yann Garcia's avatar
Yann Garcia committed
917
                                                          out AuthorizationValidationResponse p_authorization_validation_response
918
                                                          ) return boolean {
919
920
921
922
923
924
925
926
927
928
929
      // Local variables
      
      // Build the Proof of Possession InnerEcResponse
      p_authorization_validation_response := valueof(
                                                     m_authorizationValidationResponse_ok(
                                                                                          substr(p_authorization_validation_request_hash, 0, 16),
                                                                                          p_Certificate_subject_attributes
                                                                                          )
                                                     );
      
      return true;
930
931
932
933
    } // End of function f_generate_authorization_validation_response
    
  } // End of group authorization_validation_xxx
  
garciay's avatar
garciay committed
934
  group pki_functions {
935
    
936
937
938
939
940
941
942
943
944
945
946
947
    /**
     * @desc Build a signed and encrypted PKI 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
     * @param p_publicKeyCompressed The public compressed key (canonical form) for encryption
     * @param p_compressedMode      The compressed mode of the public compressed key (canonical form) for encryption
     * @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
     */
garciay's avatar
garciay committed
948
    function f_build_pki_secured_message(
949
                                         in octetstring p_private_key,
950
951
                                         in SignerIdentifier p_signer_identifier,
                                         in HashedId8 p_recipientId,
952
                                         in octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
953
                                         in integer p_compressedMode,
954
                                         in octetstring p_pki_message,
garciay's avatar
garciay committed
955
956
957
958
959
                                         out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data
                                         ) return boolean {
      // Local variables
      var template (value) EccP256CurvePoint v_eccP256_curve_point;
      var template (value) ToBeSignedData v_tbs;
960
      var octetstring v_tbs_signed;
Yann Garcia's avatar
Yann Garcia committed
961
      var template (value) Ieee1609Dot2Data v_ieee1609dot2_signed_data;
garciay's avatar
garciay committed
962
      var octetstring v_encoded_inner_ec_request;
Yann Garcia's avatar
Yann Garcia committed
963
964
965
      var Oct12 v_nonce;
      var Oct16 v_authentication_vector;
      var Oct16 v_encrypted_sym_key;
966
      var HashedId8 v_recipientId;
967
      var octetstring v_publicEphemeralKeyCompressed;
Yann Garcia's avatar
Yann Garcia committed
968
969
      var integer v_ephemeralKeyModeCompressed;
      var octetstring v_encrypted_inner_ec_request;
970
      
971
      // Signed the encoded PKI message
Yann Garcia's avatar
Yann Garcia committed
972
973
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload(
garciay's avatar
garciay committed
974
                                                    m_etsiTs103097Data_unsecured(p_pki_message)
Yann Garcia's avatar
Yann Garcia committed
975
                                                   ),
976
                                m_headerInfo_inner_ec_request(c_its_aid_SCR, f_getCurrentTimeUtc())
Yann Garcia's avatar
Yann Garcia committed
977
                               );
978
979
980
981
982
983
984
985
986
987
      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
Yann Garcia's avatar
Yann Garcia committed
988
989
990
991
      v_ieee1609dot2_signed_data := m_etsiTs103097Data_signed(
                                                              m_signedData(
                                                                           sha256,
                                                                           v_tbs,
992
                                                                           p_signer_identifier,
Yann Garcia's avatar
Yann Garcia committed
993
994
995
996
997
998
999
1000
                                                                           m_signature_ecdsaNistP256(
                                                                                                     m_ecdsaP256Signature(
                                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                                     substr(v_tbs_signed, 0, 32)
                                                                                                                                                     ),
                                                                                                                          substr(v_tbs_signed, 32, 32)
                                                                                                                          )
                                                                                                     )
For faster browsing, not all history is shown. View entire blame