LibItsPki_Functions.ttcn 276 KB
Newer Older
Yann Garcia's avatar
Yann Garcia committed
1
/**
Yann Garcia's avatar
Yann Garcia committed
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;
YannGarcia's avatar
YannGarcia committed
29
  import from EtsiTs102941TrustLists language "ASN.1:1997" all;
Yann Garcia's avatar
Yann Garcia committed
30
31
32
33
34
  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
35
  import from LibItsCommon_TypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
36
37
38
  import from LibItsCommon_Functions all;
  import from LibItsCommon_ASN1_NamedNumbers all;
  import from LibItsCommon_Pixits all;
garciay's avatar
garciay committed
39
  
40
  // LibItsGeoNetworking
Yann Garcia's avatar
Yann Garcia committed
41
42
  import from LibItsGeoNetworking_TypesAndValues all;
  import from LibItsGeoNetworking_TestSystem all;
43
44
  import from LibItsGeoNetworking_Pixits all;
  
Yann Garcia's avatar
Yann Garcia committed
45
46
47
48
  // LibItsSecurity
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Functions all;
49
  import from LibItsSecurity_Pics all;
Yann Garcia's avatar
Yann Garcia committed
50
  import from LibItsSecurity_Pixits all;
51
  import from LibItsSecurity_TestSystem all;
garciay's avatar
garciay committed
52
  
Yann Garcia's avatar
Yann Garcia committed
53
54
  // LibItsHttp
  import from LibItsHttp_TypesAndValues all;
55
  import from LibItsHttp_Templates all;
Yann Garcia's avatar
Yann Garcia committed
56
57
  import from LibItsHttp_BinaryTemplates all;
  import from LibItsHttp_Functions all;
Yann Garcia's avatar
Yann Garcia committed
58
  import from LibItsHttp_TestSystem all;
garciay's avatar
garciay committed
59
  
Yann Garcia's avatar
Yann Garcia committed
60
  // LibItsPki
garciay's avatar
garciay committed
61
  import from LibItsPki_TypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
62
  import from LibItsPki_Templates all;
garciay's avatar
garciay committed
63
  import from LibItsPki_Pics all;
garciay's avatar
garciay committed
64
  import from LibItsPki_Pixits all;
Yann Garcia's avatar
Yann Garcia committed
65
  import from LibItsPki_TestSystem all;
garciay's avatar
garciay committed
66
67
  
  group pkiConfigurationFunctions {
Yann Garcia's avatar
Yann Garcia committed
68

69
70
71
72
    function f_cfMtcUp01(
                         out ItsPkiItss p_itss,
                         out ItsPkiHttp p_pki
                         ) runs on ItsMtc {
Yann Garcia's avatar
Yann Garcia committed
73
74
75
76
77
78
      p_itss := ItsPkiItss.create("IUT") alive;
      p_pki := ItsPkiHttp.create("TS") alive;
      
      connect(self:syncPort, mtc:syncPort);
      connect(p_itss:syncPort, self:syncPort);
      connect(p_pki:syncPort, self:syncPort);
79
80
81
82
83
84
85
86
87
88
89
90
91
    } // End of function f_cfMtcUp01
    
    function f_cfMtcUp02(
                         out ItsPkiHttp p_itss,
                         out ItsPkiHttp p_ea
                         ) runs on ServerSyncComp {
      p_itss := ItsPkiItss.create("ITS-S") alive;
      p_ea := ItsPkiHttp.create("EA") alive;
      
      connect(self:syncPort, mtc:syncPort);
      connect(p_itss:syncPort, self:syncPort);
      connect(p_ea:syncPort, self:syncPort);
    } // End of function f_cfMtcUp02
Yann Garcia's avatar
Yann Garcia committed
92
93
    
    /**
94
     * @desc    Setups default configuration
Yann Garcia's avatar
Yann Garcia committed
95
     * @param   p_certificate_id The certificate identifier the TA shall use in case of secured IUT
Yann Garcia's avatar
Yann Garcia committed
96
97
     */
    function f_cfHttpUp(
garciay's avatar
garciay committed
98
                        in charstring p_ea_certificate_id := "CERT_TS_A_EA", // TODO Use a constant
99
                        in charstring p_aa_certificate_id := "CERT_TS_A_AA"
garciay's avatar
garciay committed
100
                        ) runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
Yann Garcia's avatar
Yann Garcia committed
101

Yann Garcia's avatar
Yann Garcia committed
102
      if (PICS_MULTIPLE_END_POINT == false) {
Yann Garcia's avatar
Yann Garcia committed
103
104
105
106
107
108
        map(self:httpPort, system:httpPort);
      } else {
        map(self:httpEcPort, system:httpEcPort);
        map(self:httpAtVPort, system:httpAtVPort);
        map(self:httpAtPort, system:httpAtPort);
      }
Yann Garcia's avatar
Yann Garcia committed
109
      f_connect4SelfOrClientSync();
110

YannGarcia's avatar
YannGarcia committed
111
      f_initialiseSecuredMode(p_ea_certificate_id, p_aa_certificate_id);
112
113

      // Setup EA certificate shared with PKI EA entity
garciay's avatar
garciay committed
114
      f_readCertificate(p_ea_certificate_id, vc_eaCertificate);
Yann Garcia's avatar
Yann Garcia committed
115
116
      f_readSigningKey(p_ea_certificate_id, vc_eaPrivateKey); // TODO To be removed
      f_readEncryptingKey(p_ea_certificate_id, vc_eaPrivateEncKey);
garciay's avatar
garciay committed
117
      f_getCertificateDigest(p_ea_certificate_id, vc_eaHashedId8);
118
      f_getCertificateHash(p_ea_certificate_id, vc_eaWholeHash);
YannGarcia's avatar
YannGarcia committed
119
      f_getCertificateHash256(p_ea_certificate_id, vc_eaWholeHash256);
Yann Garcia's avatar
Yann Garcia committed
120
      log("f_cfHttpUp: vc_eaPrivateKey= ", vc_eaPrivateKey);
121
      log("f_cfHttpUp: vc_eaPrivateEncKey= ", vc_eaPrivateEncKey);
Yann Garcia's avatar
Yann Garcia committed
122
123
      log("f_cfHttpUp: vc_eaHashedId8= ", vc_eaHashedId8);
      log("f_cfHttpUp: vc_eaWholeHash= ", vc_eaWholeHash);
YannGarcia's avatar
YannGarcia committed
124
      log("f_cfHttpUp: vc_eaWholeHash256= ", vc_eaWholeHash256);
125
126
127
      
      // Setup AA certificate shared with PKI AA entity
      f_readCertificate(p_aa_certificate_id, vc_aaCertificate);
128
129
      f_readSigningKey(p_aa_certificate_id, vc_aaPrivateKey); // Required for AuthorizationValidation request
      f_readEncryptingKey(p_aa_certificate_id, vc_aaPrivateEncKey);
130
131
      f_getCertificateDigest(p_aa_certificate_id, vc_aaHashedId8);
      f_getCertificateHash(p_aa_certificate_id, vc_aaWholeHash);
YannGarcia's avatar
YannGarcia committed
132
      f_getCertificateHash256(p_aa_certificate_id, vc_aaWholeHash256);
Yann Garcia's avatar
Yann Garcia committed
133
134
      log("f_cfHttpUp: vc_aaPrivateKey= ", vc_aaPrivateKey);
      log("f_cfHttpUp: vc_aaPrivateEncKey= ", vc_aaPrivateEncKey);
Yann Garcia's avatar
Yann Garcia committed
135
136
      log("f_cfHttpUp: vc_aaHashedId8= ", vc_aaHashedId8);
      log("f_cfHttpUp: vc_aaWholeHash= ", vc_aaWholeHash);
YannGarcia's avatar
YannGarcia committed
137
      log("f_cfHttpUp: vc_aaWholeHash256= ", vc_aaWholeHash256);
Yann Garcia's avatar
Yann Garcia committed
138
139
140
141
142
143
144
145

      if (PICS_MULTIPLE_END_POINT == false) {
        activate(a_default_pki_http());
      } else {
        activate(a_default_pki_http_ec());
        activate(a_default_pki_http_atv());
        activate(a_default_pki_http_at());
      }
Yann Garcia's avatar
Yann Garcia committed
146
    } // End of function f_cfHttpUp
147
    
YannGarcia's avatar
YannGarcia committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    /**
     * @desc    Setups default configuration
     */
    function f_cfHttpUp_ca() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {

      if (PICS_MULTIPLE_END_POINT == false) {
        map(self:httpPort, system:httpPort);
      } else {
        map(self:httpCaPort, system:httpCaPort);
      }
      f_connect4SelfOrClientSync();

      f_initialiseSecuredMode();

      if (PICS_MULTIPLE_END_POINT == false) {
        activate(a_default_pki_http());
      } else {
        activate(a_default_pki_http_ca());
      }
    } // End of function f_cfHttpUp_ca
    
169
170
171
172
173
174
175
176
177
178
179
180
    /**
     * @desc    Setups default configuration
     * @param   p_certificate_id The certificate identifier the TA shall use in case of secured IUT
     */
    function f_cfHttpUp_itss(
                             in charstring p_ea_certificate_id := "CERT_TS_A_EA", // TODO Use a constant
                             in charstring p_aa_certificate_id := "CERT_TS_A_AA"
                             ) runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {

      if (PICS_MULTIPLE_END_POINT == false) {
        map(self:httpPort, system:httpPort);
      } else {
YannGarcia's avatar
YannGarcia committed
181
        map(self:httpAtPort, system:httpAtPort);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      }

      f_initialiseSecuredMode(p_ea_certificate_id, p_aa_certificate_id); // TODO To be removed???

      // Setup EA certificate shared with PKI EA entity
      f_readCertificate(p_ea_certificate_id, vc_eaCertificate);
      f_readSigningKey(p_ea_certificate_id, vc_eaPrivateKey); // TODO To be removed
      f_readEncryptingKey(p_ea_certificate_id, vc_eaPrivateEncKey);
      f_getCertificateDigest(p_ea_certificate_id, vc_eaHashedId8);
      f_getCertificateHash(p_ea_certificate_id, vc_eaWholeHash);
      log("f_cfHttpUp_itss: vc_eaPrivateKey= ", vc_eaPrivateKey);
      log("f_cfHttpUp_itss: vc_eaPrivateEncKey= ", vc_eaPrivateEncKey);
      log("f_cfHttpUp_itss: vc_eaHashedId8= ", vc_eaHashedId8);
      log("f_cfHttpUp_itss: vc_eaWholeHash= ", vc_eaWholeHash);
      
      // Setup AA certificate shared with PKI AA entity
      f_readCertificate(p_aa_certificate_id, vc_aaCertificate);
      f_readSigningKey(p_aa_certificate_id, vc_aaPrivateKey); // Required for AuthorizationValidation request
      f_readEncryptingKey(p_aa_certificate_id, vc_aaPrivateEncKey);
      f_getCertificateDigest(p_aa_certificate_id, vc_aaHashedId8);
      f_getCertificateHash(p_aa_certificate_id, vc_aaWholeHash);
      log("f_cfHttpUp_itss: vc_aaPrivateKey= ", vc_aaPrivateKey);
      log("f_cfHttpUp_itss: vc_aaPrivateEncKey= ", vc_aaPrivateEncKey);
      log("f_cfHttpUp_itss: vc_aaHashedId8= ", vc_aaHashedId8);
      log("f_cfHttpUp_itss: vc_aaWholeHash= ", vc_aaWholeHash);

      if (PICS_MULTIPLE_END_POINT == false) {
        activate(a_default_pki_http());
      } else {
YannGarcia's avatar
YannGarcia committed
211
        activate(a_default_pki_http_at());
212
213
214
215
216
217
218
219
220
221
222
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
249
250
251
252
253
254
255
256
257
258
259
260
261
      }
    } // End of function f_cfHttpUp_itss
    
    /**
     * @desc    Setups default configuration
     * @param   p_certificate_id The certificate identifier the TA shall use in case of secured IUT
     */
    function f_cfHttpUp_ea(
                             in charstring p_ea_certificate_id := "CERT_TS_A_EA", // TODO Use a constant
                             in charstring p_aa_certificate_id := "CERT_TS_A_AA"
                             ) runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {

      if (PICS_MULTIPLE_END_POINT == false) {
        map(self:httpPort, system:httpPort);
      } else {
        map(self:httpAtVPort, system:httpAtVPort);
      }

      f_initialiseSecuredMode(p_ea_certificate_id, p_aa_certificate_id); // TODO To be removed???

      // Setup EA certificate shared with PKI EA entity
      f_readCertificate(p_ea_certificate_id, vc_eaCertificate);
      f_readSigningKey(p_ea_certificate_id, vc_eaPrivateKey); // TODO To be removed
      f_readEncryptingKey(p_ea_certificate_id, vc_eaPrivateEncKey);
      f_getCertificateDigest(p_ea_certificate_id, vc_eaHashedId8);
      f_getCertificateHash(p_ea_certificate_id, vc_eaWholeHash);
      log("f_cfHttpUp_ea: vc_eaPrivateKey= ", vc_eaPrivateKey);
      log("f_cfHttpUp_ea: vc_eaPrivateEncKey= ", vc_eaPrivateEncKey);
      log("f_cfHttpUp_ea: vc_eaHashedId8= ", vc_eaHashedId8);
      log("f_cfHttpUp_ea: vc_eaWholeHash= ", vc_eaWholeHash);
      
      // Setup AA certificate shared with PKI AA entity
      f_readCertificate(p_aa_certificate_id, vc_aaCertificate);
      f_readSigningKey(p_aa_certificate_id, vc_aaPrivateKey); // Required for AuthorizationValidation request
      f_readEncryptingKey(p_aa_certificate_id, vc_aaPrivateEncKey);
      f_getCertificateDigest(p_aa_certificate_id, vc_aaHashedId8);
      f_getCertificateHash(p_aa_certificate_id, vc_aaWholeHash);
      log("f_cfHttpUp_ea: vc_aaPrivateKey= ", vc_aaPrivateKey);
      log("f_cfHttpUp_ea: vc_aaPrivateEncKey= ", vc_aaPrivateEncKey);
      log("f_cfHttpUp_ea: vc_aaHashedId8= ", vc_aaHashedId8);
      log("f_cfHttpUp_ea: vc_aaWholeHash= ", vc_aaWholeHash);

      if (PICS_MULTIPLE_END_POINT == false) {
        activate(a_default_pki_http());
      } else {
        activate(a_default_pki_http_atv());
        activate(a_default_pki_http_at());
      }
    } // End of function f_cfHttpUp_ea
    
262
    function f_cfUp_itss(
Yann Garcia's avatar
Yann Garcia committed
263
                         in charstring p_certificate_id := "CERT_TS_A_EA" // TODO Use a constant
264
265
266
                         ) runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      
      map(self:geoNetworkingPort, system:geoNetworkingPort);
267
      map(self:utPort, system:utPort);
Yann Garcia's avatar
Yann Garcia committed
268
      //map(self:acPort, system:acPort);
269
      
270
271
      f_initializeState();
      
Yann Garcia's avatar
Yann Garcia committed
272
      // activate(a_default_pki()); TOTO Defualt from geoNet
273
    } // End of function f_cfUp_itss
274

Yann Garcia's avatar
Yann Garcia committed
275
276
277
    /**
     * @desc    Deletes default configuration 
     */
278
279
280
281
    function f_cfMtcDown01(
                           inout ItsPkiItss p_itss,
                           inout ItsPkiHttp p_pki
                           ) runs on ItsMtc {
Yann Garcia's avatar
Yann Garcia committed
282
283
284
285
286
287
288
289
      disconnect(self:syncPort, mtc:syncPort);
      disconnect(p_itss:syncPort, self:syncPort);
      disconnect(p_pki:syncPort, self:syncPort);
      
      p_itss.done;
      p_pki.done;
    }
    
290
291
292
293
294
295
296
297
298
299
300
301
    function f_cfMtcDown02(
                           inout ItsPkiHttp p_itss,
                           inout ItsPkiHttp p_ea
                           ) runs on ServerSyncComp {
      disconnect(self:syncPort, mtc:syncPort);
      disconnect(p_itss:syncPort, self:syncPort);
      disconnect(p_ea:syncPort, self:syncPort);
      
      p_itss.done;
      p_ea.done;
    }
    
Yann Garcia's avatar
Yann Garcia committed
302
303
304
305
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
Yann Garcia's avatar
Yann Garcia committed
306
307
308
309
310
311
312
      if (PICS_MULTIPLE_END_POINT == false) {
        unmap(self:httpPort, system:httpPort);
      } else {
        unmap(self:httpEcPort, system:httpEcPort);
        unmap(self:httpAtVPort, system:httpAtVPort);
        unmap(self:httpAtPort, system:httpAtPort);
      }
Yann Garcia's avatar
Yann Garcia committed
313
      f_disconnect4SelfOrClientSync();
314
      f_uninitialiseSecuredMode();
Yann Garcia's avatar
Yann Garcia committed
315
316
    } // End of function f_cfHttpDown
    
YannGarcia's avatar
YannGarcia committed
317
318
319
320
321
322
323
324
325
326
327
328
329
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown_ca() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
      if (PICS_MULTIPLE_END_POINT == false) {
        unmap(self:httpPort, system:httpPort);
      } else {
        unmap(self:httpCaPort, system:httpCaPort);
      }
      f_disconnect4SelfOrClientSync();
      f_uninitialiseSecuredMode();
    } // End of function f_cfHttpDown_ca
    
330
331
332
333
334
335
336
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown_itss() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
      if (PICS_MULTIPLE_END_POINT == false) {
        unmap(self:httpPort, system:httpPort);
      } else {
YannGarcia's avatar
YannGarcia committed
337
        unmap(self:httpAtPort, system:httpAtPort);
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
      }
      f_disconnect4SelfOrClientSync();
      f_uninitialiseSecuredMode();
    } // End of function f_cfHttpDown_itss
    
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown_ea() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
      if (PICS_MULTIPLE_END_POINT == false) {
        unmap(self:httpPort, system:httpPort);
      } else {
        unmap(self:httpAtVPort, system:httpAtVPort);
      }
      f_disconnect4SelfOrClientSync();
      f_uninitialiseSecuredMode();
    } // End of function f_cfHttpDown_ea
    
356
357
358
359
360
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfDown_itss() runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      unmap(self:geoNetworkingPort, system:geoNetworkingPort);
361
      unmap(self:utPort, system:utPort);
Yann Garcia's avatar
Yann Garcia committed
362
      //unmap(self:acPort, system:acPort);
363
364
365
      
    } // End of function f_cfDown
    
Yann Garcia's avatar
Yann Garcia committed
366
367
368
    /**
     * @desc Initialise secure mode if required
     */
369
    function f_initialiseSecuredMode(
Yann Garcia's avatar
Yann Garcia committed
370
                                     in charstring p_certificate_id  := "CERT_TS_A_EA", // TODO Use a constant
371
372
                                     in charstring p_peerCertificateId := "CERT_IUT_A_EA"
                                     ) runs on ItsSecurityBaseComponent {
Yann Garcia's avatar
Yann Garcia committed
373
374
375
      // Local variables
      
      // Load certificates
376
      if(not(f_loadCertificates(PX_IUT_SEC_CONFIG_NAME))) {
Yann Garcia's avatar
Yann Garcia committed
377
        log("*** INFO: TEST CASE NOW STOPPING ITSELF! ***");
378
        setverdict(inconc);
Yann Garcia's avatar
Yann Garcia committed
379
380
381
        stop;
      }
      
Yann Garcia's avatar
Yann Garcia committed
382
      //      f_prepareCertificates(p_certificate_id, vc_aaCertificate, vc_atCertificate);
Yann Garcia's avatar
Yann Garcia committed
383
    } // End of function f_initialiseSecuredMode()
384
385
    
    function f_uninitialiseSecuredMode() runs on ItsSecurityBaseComponent {
Yann Garcia's avatar
Yann Garcia committed
386
387
      f_unloadCertificates();
    } // End of function f_uninitialiseSecuredMode()
388
389
    
    function f_initializeState() runs on ItsPkiItss {
Yann Garcia's avatar
Yann Garcia committed
390
391
392
393
394
      var Oct8 v_hashedId8ToBeUsed;

      log(">>> f_initializeState: vc_hashedId8ToBeUsed= ", vc_hashedId8ToBeUsed);
      v_hashedId8ToBeUsed := f_setupIutCertificate(vc_hashedId8ToBeUsed);
      log("f_initializeState: v_hashedId8ToBeUsed= ", v_hashedId8ToBeUsed);
395
396
397
398
399
400
401
402
403

      f_utInitializeIut(UtPkiInitialize: { v_hashedId8ToBeUsed } );

      f_sleep(PX_NEIGHBOUR_DISCOVERY_DELAY);
      
      //      f_acLoadScenario(p_scenario);
      //      f_acStartScenario();
    }
    
Yann Garcia's avatar
Yann Garcia committed
404
  } // End of pkiConfigurationFunctions
garciay's avatar
garciay committed
405

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  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
    
Yann Garcia's avatar
Yann Garcia committed
429
430
431
432
433
434
435
    function f_sendUtTriggerEnrolmentRequestPrimitive(
                                                      in octetstring p_canonical_id := ''O,
                                                      in Oct1 p_enc_algorithm := '00'O,
                                                      in octetstring p_private_key := ''O,
                                                      in octetstring p_public_key_compressed := ''O,
                                                      in integer p_compressed_mode := 0
                                                      ) runs on ItsPkiItss {
436
437
438
      var TriggerEnrolmentRequest v_ut_trigger_enrolment_request;
      var octetstring v_compressed_public_key;

Yann Garcia's avatar
Yann Garcia committed
439
440
      if (p_compressed_mode == 2) { // TODO v_compressed_public_key := int2oct(p_compressed_mode, 1) & p_public_key_compressed?
        v_compressed_public_key := '02'O & p_public_key_compressed;
441
      } else {
Yann Garcia's avatar
Yann Garcia committed
442
        v_compressed_public_key := '03'O & p_public_key_compressed;
443
444
      }
      
Yann Garcia's avatar
Yann Garcia committed
445
      v_ut_trigger_enrolment_request := { p_canonical_id, p_enc_algorithm, p_private_key, v_compressed_public_key };
446
447
448
449
450
451
452
453
      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;
Yann Garcia's avatar
Yann Garcia committed
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
          log("*** f_sendUtTriggerEnrolmentRequestPrimitive: 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_sendUtTriggerEnrolmentRequestPrimitive
    
    function f_sendUtTriggerAuthorizationRequestPrimitive(
                                                          in octetstring p_canonical_id := ''O,
                                                          in Oct1 p_enc_algorithm := '00'O,
                                                          in octetstring p_private_key := ''O,
                                                          in octetstring p_public_key_compressed := ''O,
                                                          in integer p_compressed_mode := 0
                                                          ) runs on ItsPkiItss {
      var TriggerAuthorizationRequest v_ut_trigger_enrolment_request;
      var octetstring v_compressed_public_key;

      if (p_compressed_mode == 2) { // TODO v_compressed_public_key := int2oct(p_compressed_mode, 1) & p_public_key_compressed?
        v_compressed_public_key := '02'O & p_public_key_compressed;
      } else {
        v_compressed_public_key := '03'O & p_public_key_compressed;
      }
      
      v_ut_trigger_enrolment_request := { p_canonical_id, p_enc_algorithm, p_private_key, v_compressed_public_key };
      utPort.send(UtPkiTrigger: { triggerAuthorizationRequest := 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_sendUtTriggerAuthorizationRequestPrimitive: ERROR: Received unexpected message ***");
490
491
492
493
494
495
496
          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
Yann Garcia's avatar
Yann Garcia committed
497
    } // End of function f_sendUtTriggerAuthorizationRequestPrimitive
498
499
500
    
  } // End of group ut_port

501
502
503
504
505
506
507
508
509
  group helpers {

    function f_generate_key_pair(
                                 out octetstring p_private_key,
                                 out octetstring p_public_key_x,
                                 out octetstring p_public_key_y,
                                 out octetstring p_public_key_compressed,
                                 out integer p_compressed_mode
                                 ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
      if (PX_VE_ALG == e_nist_p256) {
        f_generate_key_pair_nistp256(p_private_key, p_public_key_x, p_public_key_y, p_public_key_compressed, p_compressed_mode);
      } else if (PX_VE_ALG == e_brainpool_p256_r1) {
        f_generate_key_pair_brainpoolp256(p_private_key, p_public_key_x, p_public_key_y, p_public_key_compressed, p_compressed_mode);
      } else if (PX_VE_ALG == e_brainpool_p384_r1) {
        f_generate_key_pair_brainpoolp384(p_private_key, p_public_key_x, p_public_key_y, p_public_key_compressed, p_compressed_mode);
      } else {
        // error
        return false;
      }

      return true;
    }

    function f_generate_key_pair_for_encryption(
                                                in SecurityAlg p_algorithm,
                                                out octetstring p_private_key,
                                                out octetstring p_public_key_x,
                                                out octetstring p_public_key_y,
                                                out octetstring p_public_key_compressed,
                                                out integer p_compressed_mode
                                                ) return boolean {
      if (p_algorithm == e_nist_p256) {
533
        f_generate_key_pair_nistp256(p_private_key, p_public_key_x, p_public_key_y, p_public_key_compressed, p_compressed_mode);
Yann Garcia's avatar
Yann Garcia committed
534
      } else if (p_algorithm == e_brainpool_p256_r1) {
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
        f_generate_key_pair_brainpoolp256(p_private_key, p_public_key_x, p_public_key_y, p_public_key_compressed, p_compressed_mode);
      } else {
        // error
        return false;
      }

      return true;
    }

    function f_signWithEcdsa(
                             in octetstring p_toBeSignedSecuredMessage,
                             in octetstring p_certificateIssuer,
                             in octetstring p_privateKey
                             ) return octetstring {
      if (PX_VE_ALG == e_nist_p256) {
        return f_signWithEcdsaNistp256WithSha256(p_toBeSignedSecuredMessage, p_certificateIssuer, p_privateKey);
      } else if (PX_VE_ALG == e_brainpool_p256_r1) {
        return f_signWithEcdsaBrainpoolp256WithSha256(p_toBeSignedSecuredMessage, p_certificateIssuer, p_privateKey);
      } else if (PX_VE_ALG == e_brainpool_p384_r1) {
YannGarcia's avatar
YannGarcia committed
554
555
556
557
558
        if (p_certificateIssuer == int2oct(0, 32)) {
          return f_signWithEcdsaBrainpoolp384WithSha384(p_toBeSignedSecuredMessage, int2oct(0, 48), p_privateKey);
        } else {
          return f_signWithEcdsaBrainpoolp384WithSha384(p_toBeSignedSecuredMessage, p_certificateIssuer, p_privateKey);
        }
559
560
561
      }
      
      return ''O;
Yann Garcia's avatar
Yann Garcia committed
562
563
564
565
566
567
568
569
570
571
    } // End of function f_signWithEcdsa

    function f_verifyEcdsa(
                           in octetstring p_tbs,
                           in octetstring p_issuer,
                           in Signature p_signature_,
                           in PublicVerificationKey p_verification_key
                           ) return boolean {
      var boolean v_ret := false;
      
572
573
574
575
      log(">>> f_verifyEcdsa: p_tbs= ", p_tbs);
      log(">>> f_verifyEcdsa: p_issuer= ", p_issuer);
      log(">>> f_verifyEcdsa: p_signature_= ", p_signature_);
      log(">>> f_verifyEcdsa: p_verification_key= ", p_verification_key);
Yann Garcia's avatar
Yann Garcia committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
      
      if (ischosen(p_verification_key.ecdsaNistP256)) {
        if (ischosen(p_verification_key.ecdsaNistP256.compressed_y_0)) {
          v_ret := f_verifyWithEcdsaNistp256WithSha256(
                                                       p_tbs,
                                                       p_issuer,
                                                       p_signature_.ecdsaNistP256Signature.rSig.x_only & p_signature_.ecdsaNistP256Signature.sSig,
                                                       p_verification_key.ecdsaNistP256.compressed_y_0,
                                                       0);
        } else {
          v_ret := f_verifyWithEcdsaNistp256WithSha256(
                                                       p_tbs,
                                                       p_issuer,
                                                       p_signature_.ecdsaNistP256Signature.rSig.x_only & p_signature_.ecdsaNistP256Signature.sSig,
                                                       p_verification_key.ecdsaNistP256.compressed_y_1,
                                                       1);
        }
      } else if (ischosen(p_verification_key.ecdsaBrainpoolP256r1)) {
        if (ischosen(p_verification_key.ecdsaBrainpoolP256r1.compressed_y_0)) {
          v_ret := f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                            p_tbs,
                                                            p_issuer,
                                                            p_signature_.ecdsaBrainpoolP256r1Signature.rSig.x_only & p_signature_.ecdsaBrainpoolP256r1Signature.sSig,
                                                            p_verification_key.ecdsaBrainpoolP256r1.compressed_y_0,
                                                            0);
        } else {
          v_ret := f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                            p_tbs,
                                                            p_issuer,
                                                            p_signature_.ecdsaBrainpoolP256r1Signature.rSig.x_only & p_signature_.ecdsaBrainpoolP256r1Signature.sSig,
                                                            p_verification_key.ecdsaBrainpoolP256r1.compressed_y_1,
                                                            1);
        }
      } else if (ischosen(p_verification_key.ecdsaBrainpoolP384r1)) {
        if (ischosen(p_verification_key.ecdsaBrainpoolP384r1.compressed_y_0)) {
          v_ret := f_verifyWithEcdsaBrainpoolp384WithSha384(
                                                            p_tbs,
                                                            p_issuer,
                                                            p_signature_.ecdsaBrainpoolP384r1Signature.rSig.x_only & p_signature_.ecdsaBrainpoolP384r1Signature.sSig,
                                                            p_verification_key.ecdsaBrainpoolP384r1.compressed_y_0,
                                                            0);
        } else {
          v_ret := f_verifyWithEcdsaBrainpoolp384WithSha384(
                                                            p_tbs,
                                                            p_issuer,
                                                            p_signature_.ecdsaBrainpoolP384r1Signature.rSig.x_only & p_signature_.ecdsaBrainpoolP384r1Signature.sSig,
                                                            p_verification_key.ecdsaBrainpoolP384r1.compressed_y_1,
                                                            1);
        }
      }
      
      return v_ret;
    } // End of function f_verifyEcdsa
629
    
Yann Garcia's avatar
Yann Garcia committed
630
631
632
633
    function f_http_send(
                         in HeaderLines p_headers,
                         in template (value) HttpMessage p_http_message
                         ) runs on ItsPkiHttp {
YannGarcia's avatar
YannGarcia committed
634
635
      log(">>> f_http_send: ", p_http_message);
      
Yann Garcia's avatar
Yann Garcia committed
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
      if (not(PICS_MULTIPLE_END_POINT)) {
        httpPort.send(p_http_message);
      } else {
        var charstring_list v_content_text;
        
        f_get_header(p_headers, c_header_content_text, v_content_text);
        
        if (not(isvalue(v_content_text))) {
          log("f_http_send: Failed to send message: header not found: ", c_header_content_text);
          return;
        }
        if (v_content_text == { "inner_ec_request" }) {
          log("f_http_send: Send on EC end point");
          f_set_headers_list({ c_header_host }, { PICS_HEADER_HOST_EC }, p_headers);
          if (ischosen(p_http_message.request)) {
            p_http_message.request.header := p_headers;
          } else {
            p_http_message.response.header := p_headers;
          }
          httpEcPort.send(p_http_message);
        } else if (v_content_text == { "inner_atv_request" }) {
          log("f_http_send: Send on ATV end point");
          f_set_headers_list({ c_header_host }, { PICS_HEADER_HOST_ATV }, p_headers);
          if (ischosen(p_http_message.request)) {
            p_http_message.request.header := p_headers;
          } else {
            p_http_message.response.header := p_headers;
          }
          httpAtVPort.send(p_http_message);
        } else if (v_content_text == { "inner_at_request" }) {
          log("f_http_send: Send on AT end point");
          f_set_headers_list({ c_header_host }, { PICS_HEADER_HOST_AT }, p_headers);
          if (ischosen(p_http_message.request)) {
            p_http_message.request.header := p_headers;
          } else {
            p_http_message.response.header := p_headers;
          }
          httpAtPort.send(p_http_message);
YannGarcia's avatar
YannGarcia committed
674
675
676
677
678
679
680
681
682
        } else if (v_content_text == { "ca_request" }) {
          log("f_http_send: Send on CA end point");
          f_set_headers_list({ c_header_host }, { PICS_HEADER_HOST_CA }, p_headers);
          if (ischosen(p_http_message.request)) {
            p_http_message.request.header := p_headers;
          } else {
            p_http_message.response.header := p_headers;
          }
          httpCaPort.send(p_http_message);
Yann Garcia's avatar
Yann Garcia committed
683
684
685
686
687
        } else {
          log("f_http_send: Invalid header value: ", v_content_text);
        }
      }
    } // End of function f_http_send
Yann Garcia's avatar
Yann Garcia committed
688
689
690
691
692
693
694
695
696
697
698

    function f_generate_key_tag(
                                in octetstring p_public_key_compressed,
                                in integer p_compressed_key_mode,
                                in octetstring p_public_compressed_enc_key,
                                in integer p_compressed_enc_key_mode,
                                out octetstring p_encoded_tag
                                ) return boolean {
      // Local variables
      var PublicVerificationKey v_verification_tag;
      var PublicEncryptionKey v_encryption_tag;
YannGarcia's avatar
YannGarcia committed
699
700
701
702

      log(">>> f_generate_key_tag: p_public_key_compressed=", p_public_key_compressed);
      log(">>> f_generate_key_tag: p_public_compressed_enc_key=", p_public_compressed_enc_key);
      
Yann Garcia's avatar
Yann Garcia committed
703
704
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
      
      if (PX_VE_ALG == e_nist_p256) {
        if (p_compressed_key_mode == 0) {
          v_verification_tag.ecdsaNistP256.compressed_y_0 := p_public_key_compressed;
        } else {
          v_verification_tag.ecdsaNistP256.compressed_y_1 := p_public_key_compressed;
        }
      } else if (PX_VE_ALG == e_brainpool_p256_r1) {
        if (p_compressed_key_mode == 0) {
          v_verification_tag.ecdsaBrainpoolP256r1.compressed_y_0 := p_public_key_compressed;
        } else {
          v_verification_tag.ecdsaBrainpoolP256r1.compressed_y_1 := p_public_key_compressed;
        }
      } else if (PX_VE_ALG == e_brainpool_p384_r1) {
        if (p_compressed_key_mode == 0) {
          v_verification_tag.ecdsaBrainpoolP384r1.compressed_y_0 := p_public_key_compressed;
        } else {
          v_verification_tag.ecdsaBrainpoolP384r1.compressed_y_1 := p_public_key_compressed;
        }
      } else {
        log("f_generate_key_tag: Failed to generate HMAC tag");
        return false;
      }
      log("f_generate_key_tag: v_verification_tag= ", v_verification_tag);
      p_encoded_tag := bit2oct(encvalue(v_verification_tag));
      if (PX_INCLUDE_ENCRYPTION_KEYS) {
        v_encryption_tag.supportedSymmAlg := aes128Ccm;
YannGarcia's avatar
YannGarcia committed
730
        if (PX_EC_ALG_FOR_EC_SIGN == e_nist_p256) {
Yann Garcia's avatar
Yann Garcia committed
731
732
733
734
735
          if (p_compressed_enc_key_mode == 0) {
            v_encryption_tag.publicKey.eciesNistP256.compressed_y_0 := p_public_compressed_enc_key;
          } else {
            v_encryption_tag.publicKey.eciesNistP256.compressed_y_1 := p_public_compressed_enc_key;
          }
YannGarcia's avatar
YannGarcia committed
736
        } else if (PX_EC_ALG_FOR_EC_SIGN == e_brainpool_p256_r1) {
Yann Garcia's avatar
Yann Garcia committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
          if (p_compressed_enc_key_mode == 0) {
            v_encryption_tag.publicKey.eciesBrainpoolP256r1.compressed_y_0 := p_public_compressed_enc_key;
          } else {
            v_encryption_tag.publicKey.eciesBrainpoolP256r1.compressed_y_1 := p_public_compressed_enc_key;
          }
        } else {
          log("f_generate_key_tag: Failed to generate HMAC tag (enc)");
          return false;
        }
        log("f_generate_key_tag: v_encryption_tag= ", v_encryption_tag);
        p_encoded_tag := p_encoded_tag & bit2oct(encvalue(v_encryption_tag));
      }

      return true;
    } // End of function f_generate_key_tag
Yann Garcia's avatar
Yann Garcia committed
752
    
753
754
  } // End of group helpers
  
Yann Garcia's avatar
Yann Garcia committed
755
  group http { // TODO Split into EnnerEc, Authorization & AuthorizationValidation
756

757
    function f_http_build_inner_ec_request( // TODO Cleanup parameters
garciay's avatar
garciay committed
758
                                           out octetstring p_private_key,
Yann Garcia's avatar
Yann Garcia committed
759
760
                                           out octetstring p_public_key_compressed,
                                           out integer p_compressed_mode,
761
762
763
764
765
                                           out Oct16 p_aes_sym_key,
                                           out Oct16 p_encrypted_sym_key,
                                           out Oct16 p_authentication_vector,
                                           out Oct12 p_nonce,
                                           out octetstring p_salt,
garciay's avatar
garciay committed
766
                                           out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
767
                                           out Oct32 p_request_hash
garciay's avatar
garciay committed
768
                                           ) runs on ItsPkiHttp {
769
770
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
garciay's avatar
garciay committed
771
772
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
Yann Garcia's avatar
Yann Garcia committed
773
      var boolean v_result;
Yann Garcia's avatar
Yann Garcia committed
774
775

      log(">>> f_http_build_inner_ec_request");
Yann Garcia's avatar
Yann Garcia committed
776
      if (f_generate_inner_ec_request(p_private_key, p_public_key_compressed, p_compressed_mode, v_inner_ec_request) == false) {
Yann Garcia's avatar
Yann Garcia committed
777
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequest ***");
778
779
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
780
781
782
      log ("f_http_build_enrolment_request: ==> EC verification private key: ", p_private_key);
      log ("f_http_build_enrolment_request: ==> EC verification public compressed key: ", p_public_key_compressed);
      log ("f_http_build_enrolment_request: ==> EC verification public compressed mode: ", p_compressed_mode);
783
784
      // 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
785
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
786
787
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
788
      log("f_http_build_enrolment_request: v_inner_ec_request_signed_for_pop= ", v_inner_ec_request_signed_for_pop);
789
      
790
      // Secure InnerEcRequestSignedForPoP message
garciay's avatar
garciay committed
791
792
793
794
      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);
      }
795
796
797
798
      if (PICS_SEC_FIXED_KEYS) { // Debug mode: Use fixed values
        v_public_enc_key := 'DD4F6B0DF57C6E3BD0E32B565CACA1D858CEB08A5C2BBAB2C23E203C6DE697FF'O;
        v_compressed_enc_key_mode := 0;
      }
Yann Garcia's avatar
Yann Garcia committed
799
800
      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);
YannGarcia's avatar
YannGarcia committed
801
      p_salt := vc_eaWholeHash256; // IEEE 1609.2: If the encryption key was obtained from a certificate c, P1 is SHA-256 (c), where c is the COER encoding of the certificate, canonicalized per 6.4.3.
802
      if (PICS_SECPKI_REENROLMENT == false) { // This is the first enrolment, we used Factory keys
803
      v_result := f_build_pki_secured_request_message_signed_with_pop(PICS_ITS_S_SIGN_NITSP256_PRIVATE_KEY, valueof(m_signerIdentifier_self), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
garciay's avatar
garciay committed
804
      } else { // We use last valid EC certificate
Yann Garcia's avatar
Yann Garcia committed
805
806
807
        // TODO Retrieve EC certificate from the first enrolment instead of PX
        log("f_http_build_inner_ec_request: v_ec_private_key: ", PX_EC_PRIVATE_KEY);
        log("f_http_build_inner_ec_request: v_ec_hashed_id8: ", PX_EC_HASHED_ID8);
808
      v_result := f_build_pki_secured_request_message_signed_with_pop(PX_EC_PRIVATE_KEY, valueof(m_signerIdentifier_digest(PX_EC_HASHED_ID8)), PX_EC_HASHED_ID8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
garciay's avatar
garciay committed
809
      }
Yann Garcia's avatar
Yann Garcia committed
810
      if (v_result == false) {
Yann Garcia's avatar
Yann Garcia committed
811
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
812
        f_selfOrClientSyncAndVerdict("error", e_error);
Yann Garcia's avatar
Yann Garcia committed
813
814
815
      } else {
        log("f_http_build_inner_ec_request: p_ieee1609dot2_signed_and_encrypted_data= ", p_ieee1609dot2_signed_and_encrypted_data);
        log("f_http_build_inner_ec_request: p_request_hash= ", p_request_hash);
816
      }
Yann Garcia's avatar
Yann Garcia committed
817
    } // End of function f_http_build_inner_ec_request
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
    function f_http_build_inner_ec_request_with_wrong_parameters( // TODO Cleanup parameters
                                                                 in SequenceOfPsidSsp p_appPermissions,
                                                                 in octetstring p_canonical_id := PICS_ITS_S_CANONICAL_ID,
                                                                 in Time32 p_start,
                                                                 in Duration p_duration,
                                                                 in boolean p_alter_private_key := false,
                                                                 out octetstring p_private_key,
                                                                 out octetstring p_public_key_compressed,
                                                                 out integer p_compressed_mode,
                                                                 out Oct16 p_aes_sym_key,
                                                                 out Oct16 p_encrypted_sym_key,
                                                                 out Oct16 p_authentication_vector,
                                                                 out Oct12 p_nonce,
                                                                 out octetstring p_salt,
                                                                 out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
                                                                 out Oct32 p_request_hash
                                                                 ) runs on ItsPkiHttp {
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_result;

      log(">>> f_http_build_inner_ec_request_with_wrong_parameters");
      if (f_generate_inner_ec_request_with_wrong_parameters(p_appPermissions, p_canonical_id, p_start, p_duration, p_alter_private_key, p_private_key, p_public_key_compressed, p_compressed_mode, v_inner_ec_request) == false) {
        log("*** f_http_build_inner_ec_request_with_wrong_parameters: ERROR: Failed to generate InnerEcRequest ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      log ("f_http_build_enrolment_request: ==> EC verification private key: ", p_private_key);
      log ("f_http_build_enrolment_request: ==> EC verification public compressed key: ", p_public_key_compressed);
      log ("f_http_build_enrolment_request: ==> EC verification public compressed mode: ", p_compressed_mode);
      // 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_inner_ec_request_with_wrong_parameters: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      log("f_http_build_enrolment_request: v_inner_ec_request_signed_for_pop= ", v_inner_ec_request_signed_for_pop);
      
      // Secure InnerEcRequestSignedForPoP 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_with_wrong_parameters: ERROR: Non canonical EA certificate ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      if (PICS_SEC_FIXED_KEYS) { // Debug mode: Use fixed values
        v_public_enc_key := 'DD4F6B0DF57C6E3BD0E32B565CACA1D858CEB08A5C2BBAB2C23E203C6DE697FF'O;
        v_compressed_enc_key_mode := 0;
      }
      log("f_http_build_inner_ec_request_with_wrong_parameters: Public encryption key: ", v_public_enc_key);
      log("f_http_build_inner_ec_request_with_wrong_parameters: Public encryption key comp: ", v_compressed_enc_key_mode);
YannGarcia's avatar
YannGarcia committed
868
      p_salt := vc_eaWholeHash256; // IEEE 1609.2: If the encryption key was obtained from a certificate c, P1 is SHA-256 (c), where c is the COER encoding of the certificate, canonicalized per 6.4.3.
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
      if (PICS_SECPKI_REENROLMENT == false) { // This is the first enrolment, we used Factory keys
      v_result := f_build_pki_secured_request_message_signed_with_pop(PICS_ITS_S_SIGN_NITSP256_PRIVATE_KEY, valueof(m_signerIdentifier_self), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
      } else { // We use last valid EC certificate
        // TODO Retrieve EC certificate from the first enrolment instead of PX
        log("f_http_build_inner_ec_request_with_wrong_parameters: v_ec_private_key: ", PX_EC_PRIVATE_KEY);
        log("f_http_build_inner_ec_request_with_wrong_parameters: v_ec_hashed_id8: ", PX_EC_HASHED_ID8);
      v_result := f_build_pki_secured_request_message_signed_with_pop(PX_EC_PRIVATE_KEY, valueof(m_signerIdentifier_digest(PX_EC_HASHED_ID8)), PX_EC_HASHED_ID8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
      }
      if (v_result == false) {
        log("*** f_http_build_inner_ec_request_with_wrong_parameters: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      } else {
        log("f_http_build_inner_ec_request_with_wrong_parameters: p_ieee1609dot2_signed_and_encrypted_data= ", p_ieee1609dot2_signed_and_encrypted_data);
        log("f_http_build_inner_ec_request_with_wrong_parameters: p_request_hash= ", p_request_hash);
      }
    } // End of function f_http_build_inner_ec_request_with_wrong_parameters

886
887
    function f_http_build_invalid_enrolment_request(
                                                    out octetstring p_private_key,
Yann Garcia's avatar
Yann Garcia committed
888
889
                                                    out octetstring p_public_key_compressed,
                                                    out integer p_compressed_mode,
890
891
892
893
894
                                                    out Oct16 p_aes_sym_key,
                                                    out Oct16 p_encrypted_sym_key,
                                                    out Oct16 p_authentication_vector,
                                                    out Oct12 p_nonce,
                                                    out octetstring p_salt,
895
                                                    out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
896
                                                    out Oct32 p_request_hash
897
898
899
                                                    ) runs on ItsPkiHttp {
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
garciay's avatar
garciay committed
900
901
902
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
903
      
Yann Garcia's avatar
Yann Garcia committed
904
      if (f_generate_inner_ec_request(p_private_key, p_public_key_compressed, p_compressed_mode, v_inner_ec_request) == false) {
905
906
907
        log("*** f_http_build_invalid_enrolment_request: ERROR: Failed to generate InnerEcRequest ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
908
909
910
      log ("f_http_build_invalid_enrolment_request: EC verification private key: ", p_private_key);
      log ("f_http_build_invalid_enrolment_request: EC verification public compressed key: ", p_public_key_compressed);
      log ("f_http_build_invalid_enrolment_request: EC verification public compressed mode: ", p_compressed_mode);
911
912
913
914
915
      // 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);
      }
Yann Garcia's avatar
Yann Garcia committed
916
      log("f_http_build_invalid_enrolment_request: v_inner_ec_request_signed_for_pop= ", v_inner_ec_request_signed_for_pop);
917
918
919
      // Modify signature to get an error
      if (ischosen(v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaNistP256Signature)) {
        v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaNistP256Signature.sSig[1] := bit2oct('10101010'B xor4b oct2bit(v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaNistP256Signature.sSig[1]));
920
      } else if (ischosen(v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaBrainpoolP256r1Signature)) {
921
        v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaBrainpoolP256r1Signature.sSig[1] := bit2oct('10101010'B xor4b oct2bit(v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaBrainpoolP256r1Signature.sSig[1]));
922
923
      } else {
        v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaBrainpoolP384r1Signature.sSig[1] := bit2oct('10101010'B xor4b oct2bit(v_inner_ec_request_signed_for_pop.content.signedData.signature_.ecdsaBrainpoolP384r1Signature.sSig[1]));
924
      }
925
      
926
      // Secure InnerEcRequestSignedForPoP message
garciay's avatar
garciay committed
927
      if (f_extract_enc_key(vc_eaCertificate, v_public_enc_key, v_compressed_enc_key_mode) == false) {
Yann Garcia's avatar
Yann Garcia committed
928
        log("*** f_http_build_invalid_enrolment_request: ERROR: Non canonical EA certificate ***");
garciay's avatar
garciay committed
929
930
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
931
932
      log("*** f_http_build_invalid_enrolment_request: Public encryption key: ", v_public_enc_key);
      log("*** f_http_build_invalid_enrolment_request: Public encryption key comp: ", v_compressed_enc_key_mode);
YannGarcia's avatar
YannGarcia committed
933
      p_salt := vc_eaWholeHash256; // IEEE 1609.2: If the encryption key was obtained from a certificate c, P1 is SHA-256 (c), where c is the COER encoding of the certificate, canonicalized per 6.4.3.
934
      if (PICS_SECPKI_REENROLMENT == false) { // This is the first enrolment, we used Factory keys
935
      v_ret_code := f_build_pki_secured_request_message_signed_with_pop(PICS_ITS_S_SIGN_NITSP256_PRIVATE_KEY, valueof(m_signerIdentifier_self), vc_eaHashedId8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
garciay's avatar
garciay committed
936
      } else { // We use last valid EC certificate
Yann Garcia's avatar
Yann Garcia committed
937
938
939
        // TODO Retrieve EC certificate from the first enrolment instead of PX
        log("f_http_build_inner_ec_request: v_ec_private_key: ", PX_EC_PRIVATE_KEY);
        log("f_http_build_inner_ec_request: v_ec_hashed_id8: ", PX_EC_HASHED_ID8);
940
      v_ret_code := f_build_pki_secured_request_message_signed_with_pop(PX_EC_PRIVATE_KEY, valueof(m_signerIdentifier_digest(PX_EC_HASHED_ID8)), PX_EC_HASHED_ID8/*recipientId*/, v_public_enc_key, v_compressed_enc_key_mode, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), PX_EC_ALG_FOR_EC, -, p_ieee1609dot2_signed_and_encrypted_data, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, p_request_hash);
garciay's avatar
garciay committed
941
942
      }
      if (v_ret_code == false) {
943
944
945
946
        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);
947
    } // End of function f_http_build_invalid_enrolment_request
garciay's avatar
garciay committed
948

Yann Garcia's avatar
Yann Garcia committed
949
950
951
952
    function f_http_build_inner_ec_response(
                                            in InnerEcRequest p_inner_ec_request,
                                            in EnrolmentResponseCode p_responseCode := ok,
                                            in Oct16 p_request_hash,
953
954
                                            in octetstring p_private_key := ''O,
                                            in octetstring p_digest := ''O,
Yann Garcia's avatar
Yann Garcia committed
955
                                            in Oct16 p_aes_sym_key,
Yann Garcia's avatar
Yann Garcia committed
956
                                            out InnerEcResponse p_inner_ec_response,
Yann Garcia's avatar
Yann Garcia committed
957
958
959
960
961
962
963
964
965
966
                                            out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data
                                            ) return boolean {
      // Local variables
      var bitstring v_msg_bit;
      var octetstring v_msg;
      var Oct12 v_nonce;
      var Ieee1609Dot2Data v_ieee1609dot2_signed_data;
      var EtsiTs103097Certificate v_ec_certificate;
      var boolean p_result := false;

967
968
969
970
971
972
      log(">>> f_http_build_inner_ec_response: p_inner_ec_request= ", p_inner_ec_request);
      log(">>> f_http_build_inner_ec_response: p_responseCode= ", p_responseCode);
      log(">>> f_http_build_inner_ec_response: p_request_hash= ", p_request_hash);
      log(">>> f_http_build_inner_ec_response: p_private_key= ", p_private_key);
      log(">>> f_http_build_inner_ec_response: p_digest= ", p_digest);
      log(">>> f_http_build_inner_ec_response: p_aes_sym_key= ", p_aes_sym_key);
Yann Garcia's avatar
Yann Garcia committed
973
974
975
      
      // Check expectred response
      if (p_responseCode != ok) {
Yann Garcia's avatar
Yann Garcia committed
976
        p_inner_ec_response := valueof(
Yann Garcia's avatar
Yann Garcia committed
977
978
979
980
981
982
983
984
                                       m_innerEcResponse_ko(
                                                            p_request_hash,
                                                            p_responseCode
                                                            )
                                       );
        p_result := true;
      } else {
        // Generate the certificate
985
        if (f_generate_ec_certificate_for_inner_ec_response(p_inner_ec_request, p_private_key, p_digest, v_ec_certificate) == false) {
Yann Garcia's avatar
Yann Garcia committed
986
          log("f_http_build_inner_ec_response: Failed to generate the certificate");
Yann Garcia's avatar
Yann Garcia committed
987
          p_inner_ec_response := valueof(
Yann Garcia's avatar
Yann Garcia committed
988
989
990
991
992
993
                                         m_innerEcResponse_ko(
                                                              p_request_hash,
                                                              incompleterequest
                                                              )
                                         );
        } else {
Yann Garcia's avatar
Yann Garcia committed
994
          p_inner_ec_response := valueof(
Yann Garcia's avatar
Yann Garcia committed
995
996
997
998
999
1000
                                         m_innerEcResponse_ok(
                                                              p_request_hash,
                                                              v_ec_certificate
                                                              )
                                         );
        }
For faster browsing, not all history is shown. View entire blame