LibItsPki_Functions.ttcn 77 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
      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);
105
      f_getCertificateHash(p_ea_certificate_id, vc_eaWholeHash);
garciay's avatar
garciay committed
106
      f_getCertificateDigest(p_ec_certificate_id, vc_ecHashedId8);
107
      f_readCertificate(p_peerCertificateId, vc_peerEaCertificate);
108
      f_getCertificateHash(p_peerCertificateId, vc_eaPeerWholeHash);
109
110
      
      activate(a_default_pki_http());
Yann Garcia's avatar
Yann Garcia committed
111
    } // End of function f_cfHttpUp
112
    
113
114
115
116
117
118
119
    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);
120
121
      map(self:utPort, system:utPort);
      map(self:acPort, system:acPort);
122
      
123
124
      f_initializeState();
      
125
126
      activate(a_default_pki());
    } // End of function f_cfUp_itss
127

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

  group ac_port {

    function f_sendAcPkiPrimitive(
Yann Garcia's avatar
Yann Garcia committed
197
                                  in charstring p_certificate,
garciay's avatar
garciay committed
198
199
200
201
202
203
                                  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
204
      v_ac_set_security_data := { p_certificate, PICS_IUT_CERTIFICATE_ID, p_private_key, p_publicKeyCompressed, p_compressedMode };
garciay's avatar
garciay committed
205
      acPort.send(AcPkiPrimitive: { acSetSecurityData := v_ac_set_security_data });
garciay's avatar
garciay committed
206
207
      tc_ac.start;
      alt {
garciay's avatar
garciay committed
208
        [] acPort.receive(AcPkiResponse: { result := true }) {
garciay's avatar
garciay committed
209
210
          tc_ac.stop;
        }
garciay's avatar
garciay committed
211
        [] acPort.receive(AcPkiResponse: { result := false }) {
garciay's avatar
garciay committed
212
213
214
215
216
217
218
219
220
          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
221
    } // End of function f_sendAcPkiPrimitive
garciay's avatar
garciay committed
222
223
    
  } // End of group ac_port
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
  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(
250
251
                                      in charstring p_cannonicalId,
                                      in Oct1 p_encAlgorithm,
252
253
254
255
256
257
258
259
260
261
262
263
264
                                      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;
      }
      
265
      v_ut_trigger_enrolment_request := { p_cannonicalId, p_encAlgorithm, p_private_key, v_compressed_public_key };
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      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

286
287
  group http {

Yann Garcia's avatar
Yann Garcia committed
288
    function f_http_build_inner_ec_request(
garciay's avatar
garciay committed
289
290
291
292
293
294
                                           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
295
296
      var octetstring v_publicKeyX;
      var octetstring v_publicKeyY
297
298
299
      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
300
301
302
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
303
      
Yann Garcia's avatar
Yann Garcia committed
304
305
      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 ***");
306
307
308
309
        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
310
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
311
312
313
314
        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
315
      // Prepare for getting the 16 most bytes of SHA256 of InnerEcRequestSignedForPop
316
      p_hash_inner_ec_request_signed_for_pop := f_hashWithSha256(bit2oct(v_inner_ec_request_signed_for_pop_msg));
garciay's avatar
garciay committed
317
318
319
320
321
322
323
324
325
      // 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
326
        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, vc_eaWholeHash, bit2oct(v_inner_ec_request_signed_for_pop_msg), p_ieee1609dot2_signed_and_encrypted_data);
garciay's avatar
garciay committed
327
      } else { // We use last valid EC certificate
328
        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, vc_eaWholeHash, bit2oct(v_inner_ec_request_signed_for_pop_msg), p_ieee1609dot2_signed_and_encrypted_data);
garciay's avatar
garciay committed
329
330
      }
      if (v_ret_code == false) {
Yann Garcia's avatar
Yann Garcia committed
331
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
332
333
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
334
335
336
337
      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
338

339
340
341
342
343
344
345
346
347
    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
348
349
350
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
351
      
Yann Garcia's avatar
Yann Garcia committed
352
      if (f_generate_inner_ec_request(p_private_key, p_publicKeyCompressed, p_compressedMode, v_inner_ec_request) == false) {
353
354
355
356
357
358
359
360
361
362
        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
363
      p_hash_inner_ec_request_signed_for_pop := f_hashWithSha256(int2oct(12345, 32)); // Invalid InnseEcRequestSignedForPop
garciay's avatar
garciay committed
364
365
366
367
368
369
370
371
      // 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
372
        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, vc_eaWholeHash, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), p_ieee1609dot2_signed_and_encrypted_data);
garciay's avatar
garciay committed
373
      } else { // We use last valid EC certificate
374
        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, vc_eaWholeHash, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), p_ieee1609dot2_signed_and_encrypted_data);
garciay's avatar
garciay committed
375
376
      }
      if (v_ret_code == false) {
377
378
379
380
        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);
381
    } // End of function f_http_build_invalid_enrolment_request
garciay's avatar
garciay committed
382
383
384
385
386
387
388
389
390
391
392

    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
393
    
Yann Garcia's avatar
Yann Garcia committed
394
    function f_http_build_authorization_validation_request(
garciay's avatar
garciay committed
395
396
397
398
399
400
401
                                                           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;
402
      var bitstring v_authorization_validation_request_msg;
garciay's avatar
garciay committed
403
404
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
garciay's avatar
garciay committed
405
406
407
408
409
      
      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
410
411
412
      // 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
413
414
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
garciay's avatar
garciay committed
415
416
417
418
419
420
421
422
423
424
      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);
425
      /* 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, vc_eaWholeHash, bit2oct(encvalue(m_etsiTs102941Data_authorization_validation_request(v_authorization_validation_request))), p_ieee1609dot2_signed_and_encrypted_data) == false) { */
garciay's avatar
garciay committed
426
427
428
      /*   log("*** f_http_build_authorization_validation_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***"); */
      /*   f_selfOrClientSyncAndVerdict("error", e_error); */
      /* } */
garciay's avatar
garciay committed
429
430
431
      
      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
432
    
433
  } // End of group http
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
541

  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
542
543
544
  
  group inner_ec_xxx {
    
garciay's avatar
garciay committed
545
    function f_generate_inner_ec_request(
546
547
                                         out octetstring p_private_key,
                                         out octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
548
549
550
                                         out integer p_compressedMode,
                                         out InnerEcRequest p_inner_ec_request
                                         ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
551
      // Local variables
Yann Garcia's avatar
Yann Garcia committed
552
553
      var Oct32 v_publicKeyX;
      var Oct32 v_publicKeyY;
554
555
556
      var EccP256CurvePoint v_eccP256_curve_point;

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

667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
  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
682
683
      var octetstring public_enc_key_x;
      var octetstring public_enc_key_y;
684
685
686
687
688
689
690
691
692
693
694
      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
695
      if (f_generate_key_pair_nistp256(p_private_enc_key, public_enc_key_x, public_enc_key_y, p_publicEncKeyCompressed, p_compressedMode) == false) {
696
697
698
699
700
701
702
703
704
        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
705
      v_hmac_key := f_hashWithSha256(int2oct(f_getCurrentTime(), 12));
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
753
      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(
754
755
756
                                                                                 m_certificate_subject_attributes( // FIXME Review subjectPermissions
                                                                                                                  p_ec_certificate.toBeSigned.appPermissions,
                                                                                                                  { { subjectPermissions := { all_ := NULL }, minChainLength := 1, chainLengthRange   := 0, eeType := '00000000'B } },
757
758
759
                                                                                                                  p_ec_certificate.toBeSigned.id,
                                                                                                                  p_ec_certificate.toBeSigned.validityPeriod,
                                                                                                                  p_ec_certificate.toBeSigned.region,
760
                                                                                                                  p_ec_certificate.toBeSigned.assuranceLevel
761
762
763
764
765
766
767
768
769
770
                                                                                                                  ))));
      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
771
      log("v_tbs=", v_tbs);
772
773
      // Signed ToBeSigned payload
      v_whole_certificate_hash := f_hashWithSha256(bit2oct(encvalue(p_ec_certificate)));
garciay's avatar
garciay committed
774
      log("v_whole_certificate_hash=", v_whole_certificate_hash);
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
808
      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
809
810
811
812
813
814
815
816
817
        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;
        }
818
        v_enc_signed_ec_signature := f_encryptWithEciesNistp256WithSha256(bit2oct(encvalue(v_signed_ec_signature)), v_public_enc_key, v_compressed_mode, ''O, v_publicEphemeralKeyCompressed, v_ephemeralKeyModeCompressed, v_encrypted_sym_key, v_authentication_vector, v_nonce);
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
880
        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
881
882
883
884
885
                                                         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,
886
887
                                                         out AuthorizationValidationRequest p_authorization_validation_request
                                                         ) return boolean {
garciay's avatar
garciay committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
      // 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
911
912
      
      return true;
913
914
915
    } // End of function f_generate_authorization_validation_request
    
    function f_generate_authorization_validation_response(
916
917
                                                          in octetstring p_authorization_validation_request_hash,
                                                          in CertificateSubjectAttributes p_Certificate_subject_attributes,
Yann Garcia's avatar
Yann Garcia committed
918
                                                          out AuthorizationValidationResponse p_authorization_validation_response
919
                                                          ) return boolean {
920
921
922
923
924
925
926
927
928
929
930
      // 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;
931
932
933
934
    } // End of function f_generate_authorization_validation_response
    
  } // End of group authorization_validation_xxx
  
garciay's avatar
garciay committed
935
  group pki_functions {
936
    
937
938
939
940
941
942
943
944
945
946
947
948
    /**
     * @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
949
    function f_build_pki_secured_message(
950
                                         in octetstring p_private_key,
951
952
                                         in SignerIdentifier p_signer_identifier,
                                         in HashedId8 p_recipientId,
953
                                         in octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
954
                                         in integer p_compressedMode,
955
                                         in octetstring p_salt,
956
                                         in octetstring p_pki_message,
garciay's avatar
garciay committed
957
958
959
960
961
                                         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;
962
      var octetstring v_tbs_signed;
Yann Garcia's avatar
Yann Garcia committed
963
      var template (value) Ieee1609Dot2Data v_ieee1609dot2_signed_data;
garciay's avatar
garciay committed
964
      var octetstring v_encoded_inner_ec_request;
Yann Garcia's avatar
Yann Garcia committed
965
966
967
      var Oct12 v_nonce;
      var Oct16 v_authentication_vector;
      var Oct16 v_encrypted_sym_key;
968
      var HashedId8 v_recipientId;
969
      var octetstring v_publicEphemeralKeyCompressed;
Yann Garcia's avatar
Yann Garcia committed
970
971
      var integer v_ephemeralKeyModeCompressed;
      var octetstring v_encrypted_inner_ec_request;
972
      
973
      // Signed the encoded PKI message
Yann Garcia's avatar
Yann Garcia committed
974
975
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload(
garciay's avatar
garciay committed
976
                                                    m_etsiTs103097Data_unsecured(p_pki_message)
Yann Garcia's avatar
Yann Garcia committed
977
                                                   ),
978
                                m_headerInfo_inner_ec_request(c_its_aid_SCR, f_getCurrentTimeUtc())
Yann Garcia's avatar
Yann Garcia committed
979
                               );
980
981
982
983
984
985
986
987
988
989
      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
990
991
992
993
      v_ieee1609dot2_signed_data := m_etsiTs103097Data_signed(
                                                              m_signedData(
                                                                           sha256,
                                                                           v_tbs,
994
                                                                           p_signer_identifier,
Yann Garcia's avatar
Yann Garcia committed
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