LibItsPki_Functions.ttcn 77.6 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
  // LibItsSecurity
  import from LibItsSecurity_TypesAndValues all;
  import from LibItsSecurity_Templates all;
  import from LibItsSecurity_Functions all;
46
  import from LibItsSecurity_Pics all;
Yann Garcia's avatar
Yann Garcia committed
47
  import from LibItsSecurity_Pixits all;
48
  import from LibItsSecurity_TestSystem all;
garciay's avatar
garciay committed
49
  
Yann Garcia's avatar
Yann Garcia committed
50
51
  // LibItsHttp
  import from LibItsHttp_TypesAndValues all;
52
  import from LibItsHttp_Templates all;
Yann Garcia's avatar
Yann Garcia committed
53
  import from LibItsHttp_TestSystem all;
garciay's avatar
garciay committed
54
  
Yann Garcia's avatar
Yann Garcia committed
55
  // LibItsPki
garciay's avatar
garciay committed
56
  import from LibItsPki_TypesAndValues all;
Yann Garcia's avatar
Yann Garcia committed
57
  import from LibItsPki_Templates all;
garciay's avatar
garciay committed
58
  import from LibItsPki_Pics all;
garciay's avatar
garciay committed
59
  import from LibItsPki_Pixits all;
Yann Garcia's avatar
Yann Garcia committed
60
  import from LibItsPki_TestSystem all;
garciay's avatar
garciay committed
61
62
  
  group pkiConfigurationFunctions {
Yann Garcia's avatar
Yann Garcia committed
63
     
Yann Garcia's avatar
Yann Garcia committed
64
     /**
Yann Garcia's avatar
Yann Garcia committed
65
66
67
68
     * @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
69
70
                        in charstring p_ea_certificate_id := "CERT_TS_A_EA", // TODO Use a constant
                        in charstring p_ec_certificate_id := "CERT_TS_A_EC",
71
                        in charstring p_peerCertificateId := "CERT_IUT_A_EA"
garciay's avatar
garciay committed
72
                        ) runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
Yann Garcia's avatar
Yann Garcia committed
73
74
75
      
      map(self:httpPort, system:httpPort);
      f_connect4SelfOrClientSync();
76

garciay's avatar
garciay committed
77
      f_initialiseSecuredMode(p_ea_certificate_id, p_peerCertificateId);
Yann Garcia's avatar
Yann Garcia committed
78
      
garciay's avatar
garciay committed
79
80
81
82
83
84
      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);
85
      f_getCertificateHash(p_ea_certificate_id, vc_eaWholeHash);
garciay's avatar
garciay committed
86
      f_getCertificateDigest(p_ec_certificate_id, vc_ecHashedId8);
87
      f_readCertificate(p_peerCertificateId, vc_peerEaCertificate);
88
      f_getCertificateHash(p_peerCertificateId, vc_eaPeerWholeHash);
89
90
      
      activate(a_default_pki_http());
Yann Garcia's avatar
Yann Garcia committed
91
    } // End of function f_cfHttpUp
92
    
93
94
95
96
97
    function f_cfUp_itss(
                         in charstring p_certificateId := "CERT_TS_A_EA" // TODO Use a constant
                         ) runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      
      map(self:geoNetworkingPort, system:geoNetworkingPort);
98
      map(self:utPort, system:utPort);
Yann Garcia's avatar
Yann Garcia committed
99
      //map(self:acPort, system:acPort);
100
      
101
102
      f_initializeState();
      
Yann Garcia's avatar
Yann Garcia committed
103
      // activate(a_default_pki()); TOTO Defualt from geoNet
104
    } // End of function f_cfUp_itss
105

Yann Garcia's avatar
Yann Garcia committed
106
107
108
109
110
111
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfHttpDown() runs on ItsPkiHttp /* TITAN TODO: system ItsPkiHttpSystem */ {
      unmap(self:httpPort, system:httpPort);
      f_disconnect4SelfOrClientSync();
112
      f_uninitialiseSecuredMode();
Yann Garcia's avatar
Yann Garcia committed
113
114
    } // End of function f_cfHttpDown
    
115
116
117
118
119
    /**
     * @desc    Deletes default configuration 
     */
    function f_cfDown_itss() runs on ItsPkiItss /* TITAN TODO: system ItsPkiItssSystem */ {
      unmap(self:geoNetworkingPort, system:geoNetworkingPort);
120
      unmap(self:utPort, system:utPort);
Yann Garcia's avatar
Yann Garcia committed
121
      //unmap(self:acPort, system:acPort);
122
123
124
      
    } // End of function f_cfDown
    
Yann Garcia's avatar
Yann Garcia committed
125
126
127
    /**
     * @desc Initialise secure mode if required
     */
128
129
130
131
    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
132
133
134
135
136
      // Local variables
      
      // Load certificates
      if( not f_loadCertificates(PX_IUT_SEC_CONFIG_NAME) ) {
        log("*** INFO: TEST CASE NOW STOPPING ITSELF! ***");
137
        setverdict(inconc);
Yann Garcia's avatar
Yann Garcia committed
138
139
140
        stop;
      }
      
141
      f_prepareCertificates(p_certificateId, vc_aaCertificate, vc_atCertificate);
Yann Garcia's avatar
Yann Garcia committed
142
    } // End of function f_initialiseSecuredMode()
143
144
    
    function f_uninitialiseSecuredMode() runs on ItsSecurityBaseComponent {
Yann Garcia's avatar
Yann Garcia committed
145
146
      f_unloadCertificates();
    } // End of function f_uninitialiseSecuredMode()
147
148
149
150
151
152
153
154
155
156
157
158
    
    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
159
  } // End of pkiConfigurationFunctions
garciay's avatar
garciay committed
160

161
162
163
  group ut_port {

    function f_utInitializeIut(template (value) UtPkiInitialize p_init) runs on ItsPkiItss {
Yann Garcia's avatar
Yann Garcia committed
164
      timer tc_wait := PX_TAC;
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
      
      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(
187
188
                                      in charstring p_cannonicalId,
                                      in Oct1 p_encAlgorithm,
189
190
191
192
193
194
195
196
197
198
199
200
201
                                      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;
      }
      
202
      v_ut_trigger_enrolment_request := { p_cannonicalId, p_encAlgorithm, p_private_key, v_compressed_public_key };
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
      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

223
224
  group http {

225
    function f_http_build_inner_ec_request( // TODO Cleanup parameters
garciay's avatar
garciay committed
226
227
228
                                           out octetstring p_private_key,
                                           out octetstring p_publicKeyCompressed,
                                           out integer p_compressedMode,
229
230
231
232
233
                                           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
234
                                           out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
235
                                           out Oct32 p_request_hash
garciay's avatar
garciay committed
236
                                           ) runs on ItsPkiHttp {
237
238
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
garciay's avatar
garciay committed
239
240
241
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
242
      
Yann Garcia's avatar
Yann Garcia committed
243
244
      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 ***");
245
246
247
248
        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
249
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
250
251
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
252
253
      log("v_inner_ec_request_signed_for_pop= ", v_inner_ec_request_signed_for_pop);
      
254
      // Secure InnerEcRequestSignedForPoP message
garciay's avatar
garciay committed
255
256
257
258
259
260
261
      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);
262
      p_salt := vc_eaWholeHash;
garciay's avatar
garciay committed
263
      if (PX_FIRST_ENROLMENT == true) { // This is the first enrolment, we used Factory keys
264
        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, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), 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
265
      } else { // We use last valid EC certificate
266
      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, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), 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
267
268
      }
      if (v_ret_code == false) {
Yann Garcia's avatar
Yann Garcia committed
269
        log("*** f_http_build_inner_ec_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***");
270
271
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
Yann Garcia's avatar
Yann Garcia committed
272
      log("*** f_http_build_inner_ec_request: DEBUG: p_ieee1609dot2_signed_and_encrypted_data= ", p_ieee1609dot2_signed_and_encrypted_data);
273
      log("*** f_http_build_inner_ec_request: DEBUG: p_request_hash= ", p_request_hash);
Yann Garcia's avatar
Yann Garcia committed
274
    } // End of function f_http_build_inner_ec_request
275

276
277
278
279
    function f_http_build_invalid_enrolment_request(
                                                    out octetstring p_private_key,
                                                    out octetstring p_publicKeyCompressed,
                                                    out integer p_compressedMode,
280
281
282
283
284
                                                    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,
285
                                                    out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
286
                                                    out Oct32 p_request_hash
287
288
289
                                                    ) runs on ItsPkiHttp {
      var InnerEcRequest v_inner_ec_request;
      var Ieee1609Dot2Data v_inner_ec_request_signed_for_pop;
garciay's avatar
garciay committed
290
291
292
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      var boolean v_ret_code;
293
      
Yann Garcia's avatar
Yann Garcia committed
294
      if (f_generate_inner_ec_request(p_private_key, p_publicKeyCompressed, p_compressedMode, v_inner_ec_request) == false) {
295
296
297
298
299
300
301
302
        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);
      }
303
304
305
      
      log("v_inner_ec_request_signed_for_pop= ", v_inner_ec_request_signed_for_pop);
      
306
      // Secure InnerEcRequestSignedForPoP message
garciay's avatar
garciay committed
307
308
309
310
311
312
      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);
313
      p_salt := vc_eaWholeHash;
garciay's avatar
garciay committed
314
      if (PX_FIRST_ENROLMENT == true) { // This is the first enrolment, we used Factory keys
315
        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, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), 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
316
      } else { // We use last valid EC certificate
317
        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, p_salt, bit2oct(encvalue(m_etsiTs102941Data_inner_ec_request_signed_for_pop(v_inner_ec_request_signed_for_pop))), 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
318
319
      }
      if (v_ret_code == false) {
320
321
322
323
        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);
324
    } // End of function f_http_build_invalid_enrolment_request
garciay's avatar
garciay committed
325
326
327
328
329
330

    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,
331
                                           out Oct32 p_request_hash
garciay's avatar
garciay committed
332
333
334
335
                                           ) runs on ItsPkiHttp {
      // Local variables
      
    } // End of function f_http_build_inner_at_request
336
    
Yann Garcia's avatar
Yann Garcia committed
337
    function f_http_build_authorization_validation_request(
garciay's avatar
garciay committed
338
339
340
341
342
343
344
                                                           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;
345
      var bitstring v_authorization_validation_request_msg;
garciay's avatar
garciay committed
346
347
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
garciay's avatar
garciay committed
348
349
350
351
352
      
      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
353
354
355
      // 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
356
357
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
garciay's avatar
garciay committed
358
359
360
361
362
363
364
365
366
367
      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);
368
      /* 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, p_request_hash) == false) { */
garciay's avatar
garciay committed
369
370
371
      /*   log("*** f_http_build_authorization_validation_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***"); */
      /*   f_selfOrClientSyncAndVerdict("error", e_error); */
      /* } */
garciay's avatar
garciay committed
372
373
374
      
      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
375
    
376
  } // End of group http
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

  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
485
486
487
  
  group inner_ec_xxx {
    
garciay's avatar
garciay committed
488
    function f_generate_inner_ec_request(
489
490
                                         out octetstring p_private_key,
                                         out octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
491
492
493
                                         out integer p_compressedMode,
                                         out InnerEcRequest p_inner_ec_request
                                         ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
494
      // Local variables
Yann Garcia's avatar
Yann Garcia committed
495
496
      var Oct32 v_publicKeyX;
      var Oct32 v_publicKeyY;
497
498
499
      var EccP256CurvePoint v_eccP256_curve_point;

      log (">>> f_generate_inner_ec_request");
Yann Garcia's avatar
Yann Garcia committed
500
      // Generate keys for the certificate to be requested
Yann Garcia's avatar
Yann Garcia committed
501
      if (f_generate_key_pair_nistp256(p_private_key, v_publicKeyX, v_publicKeyY, p_publicKeyCompressed, p_compressedMode) == false) {
502
        log ("f_generate_inner_ec_request: failed to generate keys");
Yann Garcia's avatar
Yann Garcia committed
503
504
505
        return false;
      }
      if (p_compressedMode == 0) {
506
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
507
      } else {
508
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
509
      }
510
      log ("f_generate_inner_ec_request: ", v_eccP256_curve_point);
garciay's avatar
garciay committed
511
      // Build the Proof of Possession InnerEcRequest
Yann Garcia's avatar
Yann Garcia committed
512
513
      p_inner_ec_request := valueof(
                                    m_innerEcRequest(
514
                                                     PICS_ITS_S_CANONICAL_ID,
Yann Garcia's avatar
Yann Garcia committed
515
                                                     m_publicKeys(
516
                                                                  m_publicVerificationKey_ecdsaNistP256(v_eccP256_curve_point)
Yann Garcia's avatar
Yann Garcia committed
517
518
519
                                                                  ),
                                                     m_certificateSubjectAttributes(
                                                                                    { // ETSI TS 102 965 Table A.1: ETSI ITS standardized ITS-AIDs
garciay's avatar
garciay committed
520
521
                                                                                      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
522
523
                                                                                    },
                                                                                    m_validityPeriod(
524
525
                                                                                                     f_getCurrentTime() / 1000,
                                                                                                     m_duration_years(1) // TODO Use PIXIT
526
                                                                                                     ),
Yann Garcia's avatar
Yann Garcia committed
527
                                                                                    m_geographicRegion_identifiedRegion(
528
529
530
531
532
                                                                                                                        {
                                                                                                                          m_identifiedRegion_country_only(12), // TODO Use PIXIT
                                                                                                                          m_identifiedRegion_country_only(34) // TODO Use PIXIT
                                                                                                                          }
                                                                                                                        ),
garciay's avatar
garciay committed
533
                                                                                    'C0'O // TODO Use PIXIT
534
535
                                                                                    )
                                                     )
garciay's avatar
garciay committed
536
                                    );
537
      log ("f_generate_inner_ec_request: ", p_inner_ec_request);
Yann Garcia's avatar
Yann Garcia committed
538
539
      
      return true;
garciay's avatar
garciay committed
540
    } // End of function f_generate_inner_ec_request
Yann Garcia's avatar
Yann Garcia committed
541
    
garciay's avatar
garciay committed
542
    function f_generate_inner_ec_request_signed_for_pop(
543
                                                        in octetstring p_private_key,
garciay's avatar
garciay committed
544
545
                                                        in InnerEcRequest p_inner_ec_request,
                                                        out Ieee1609Dot2Data p_inner_ec_request_signed_for_pop
Yann Garcia's avatar
Yann Garcia committed
546
547
548
549
550
    ) 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;
551
      var octetstring v_tbs_signed;
garciay's avatar
garciay committed
552
553
554
555
556
557
558
559
560
561
562
563
      
      // 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,
564
565
                                                              f_getCurrentTimeUtc()
                                                              )
garciay's avatar
garciay committed
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
                               );
      // 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
    
590
    function f_generate_inner_ec_response(
591
                                          in octetstring p_inner_ec_request_hash,
592
593
594
595
596
597
598
599
                                          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(
600
                                                          substr(p_inner_ec_request_hash, 0, 16),
601
602
603
604
605
606
607
                                                          p_certificate
                                                          )
                                     );
      
      return true;
    } // End of function f_generate_inner_ec_response
    
garciay's avatar
garciay committed
608
609
  } // End of group inner_ec_xxx

610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  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
625
626
      var octetstring public_enc_key_x;
      var octetstring public_enc_key_y;
627
628
629
630
631
632
633
634
635
636
637
      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
638
      if (f_generate_key_pair_nistp256(p_private_enc_key, public_enc_key_x, public_enc_key_y, p_publicEncKeyCompressed, p_compressedMode) == false) {
639
640
641
642
643
644
645
646
647
        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
648
      v_hmac_key := f_hashWithSha256(int2oct(f_getCurrentTime(), 12));
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
674
675
      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);
Yann Garcia's avatar
Yann Garcia committed
676
      v_key_tag := fx_hmac_sha256(v_hmac_key, v_message_to_tag); // TODO Rename and use a wrapper function
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
      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(
697
698
699
                                                                                 m_certificate_subject_attributes( // FIXME Review subjectPermissions
                                                                                                                  p_ec_certificate.toBeSigned.appPermissions,
                                                                                                                  { { subjectPermissions := { all_ := NULL }, minChainLength := 1, chainLengthRange   := 0, eeType := '00000000'B } },
700
701
702
                                                                                                                  p_ec_certificate.toBeSigned.id,
                                                                                                                  p_ec_certificate.toBeSigned.validityPeriod,
                                                                                                                  p_ec_certificate.toBeSigned.region,
703
                                                                                                                  p_ec_certificate.toBeSigned.assuranceLevel
704
705
706
707
708
709
710
711
712
713
                                                                                                                  ))));
      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
714
      log("v_tbs=", v_tbs);
715
716
      // Signed ToBeSigned payload
      v_whole_certificate_hash := f_hashWithSha256(bit2oct(encvalue(p_ec_certificate)));
garciay's avatar
garciay committed
717
      log("v_whole_certificate_hash=", v_whole_certificate_hash);
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
      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;
746
        var Oct16 v_aes_sym_key;
747
748
749
750
751
752
        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
753
754
755
756
757
758
759
760
761
        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;
        }
762
        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_aes_sym_key, v_encrypted_sym_key, v_authentication_vector, v_nonce);
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
        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
825
826
827
828
829
                                                         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,
830
831
                                                         out AuthorizationValidationRequest p_authorization_validation_request
                                                         ) return boolean {
garciay's avatar
garciay committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
      // 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
855
856
      
      return true;
857
858
859
    } // End of function f_generate_authorization_validation_request
    
    function f_generate_authorization_validation_response(
860
861
                                                          in octetstring p_authorization_validation_request_hash,
                                                          in CertificateSubjectAttributes p_Certificate_subject_attributes,
Yann Garcia's avatar
Yann Garcia committed
862
                                                          out AuthorizationValidationResponse p_authorization_validation_response
863
                                                          ) return boolean {
864
865
866
867
868
869
870
871
872
873
874
      // 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;
875
876
877
878
    } // End of function f_generate_authorization_validation_response
    
  } // End of group authorization_validation_xxx
  
garciay's avatar
garciay committed
879
  group pki_functions {
880
    
881
882
883
884
885
886
887
888
889
890
891
892
    /**
     * @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
893
    function f_build_pki_secured_message(
894
                                         in octetstring p_private_key,
895
896
                                         in SignerIdentifier p_signer_identifier,
                                         in HashedId8 p_recipientId,
897
                                         in octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
898
                                         in integer p_compressedMode,
899
                                         in octetstring p_salt,
900
                                         in octetstring p_pki_message,
901
902
903
904
                                         out Ieee1609Dot2Data p_ieee1609dot2_signed_and_encrypted_data,
                                         out Oct16 p_aes_sym_key,
                                         out Oct16 p_encrypted_sym_key,
                                         out Oct16 p_authentication_vector,
905
906
                                         out Oct12 p_nonce,
                                         out Oct32 p_request_hash
garciay's avatar
garciay committed
907
908
909
910
                                         ) return boolean {
      // Local variables
      var template (value) EccP256CurvePoint v_eccP256_curve_point;
      var template (value) ToBeSignedData v_tbs;
911
      var octetstring v_tbs_signed;
Yann Garcia's avatar
Yann Garcia committed
912
      var template (value) Ieee1609Dot2Data v_ieee1609dot2_signed_data;
garciay's avatar
garciay committed
913
      var octetstring v_encoded_inner_ec_request;
914
      var HashedId8 v_recipientId;
915
      var octetstring v_publicEphemeralKeyCompressed;
Yann Garcia's avatar
Yann Garcia committed
916
917
      var integer v_ephemeralKeyModeCompressed;
      var octetstring v_encrypted_inner_ec_request;
918
      
919
      // Signed the encoded PKI message
Yann Garcia's avatar
Yann Garcia committed
920
921
      v_tbs := m_toBeSignedData(
                                m_signedDataPayload(
garciay's avatar
garciay committed
922
                                                    m_etsiTs103097Data_unsecured(p_pki_message)
Yann Garcia's avatar
Yann Garcia committed
923
                                                   ),
924
                                m_headerInfo_inner_ec_request(c_its_aid_SCR, f_getCurrentTimeUtc())
Yann Garcia's avatar
Yann Garcia committed
925
                               );
926
927
928
929
930
931
932
933
934
935
      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
936
937
938
939
      v_ieee1609dot2_signed_data := m_etsiTs103097Data_signed(
                                                              m_signedData(
                                                                           sha256,
                                                                           v_tbs,
940
                                                                           p_signer_identifier,
Yann Garcia's avatar
Yann Garcia committed
941
942
943
944
945
946
947
948
949
950
                                                                           m_signature_ecdsaNistP256(
                                                                                                     m_ecdsaP256Signature(
                                                                                                                          m_eccP256CurvePoint_x_only(
                                                                                                                                                     substr(v_tbs_signed, 0, 32)
                                                                                                                                                     ),
                                                                                                                          substr(v_tbs_signed, 32, 32)
                                                                                                                          )
                                                                                                     )
                                                                           )
                                                      );
951
      // Encode EtsiTs103097Data-Signed data structure
Yann Garcia's avatar
Yann Garcia committed
952
      v_encoded_inner_ec_request := bit2oct(encvalue(v_ieee1609dot2_signed_data));
953
954
955
      // Calculate the SHA256 of v_encoded_inner_ec_request
      p_request_hash := f_hashWithSha256(v_encoded_inner_ec_request);
      log("p_request_hash= ", p_request_hash);
956
      // Encrypt encode EtsiTs103097Data-Signed data structure
957
958
959
960
961
962
963
964
965
966
      if (PICS_SEC_FIXED_KEYS) {
        p_publicKeyCompressed := '8C5E20FE31935F6FA682A1F6D46E4468534FFEA1A698B14B0B12513EED8DEB11'O;
        p_compressedMode := 0;
        p_salt := '9169155B08B07674CBADF75FB46A7B0D'O;
      }
      v_encrypted_inner_ec_request := f_encryptWithEciesNistp256WithSha256(v_encoded_inner_ec_request, p_publicKeyCompressed, p_compressedMode, p_salt, v_publicEphemeralKeyCompressed, v_ephemeralKeyModeCompressed, p_aes_sym_key, p_encrypted_sym_key, p_authentication_vector, p_nonce, PICS_SEC_FIXED_KEYS);
      log("p_aes_sym_key=", p_aes_sym_key);
      log("p_encrypted_sym_key=", p_encrypted_sym_key);
      log("p_authentication_vector=", p_authentication_vector);
      log("p_nonce=", p_nonce);
967
968
      log("p_recipientId=", p_recipientId);
      if (p_recipientId == int2oct(0, 8)) {
969
970
        log("f_hashWithSha256(v_encrypted_sym_key)=", f_hashWithSha256(p_encrypted_sym_key));
        v_recipientId := f_HashedId8FromSha256(f_hashWithSha256(p_encrypted_sym_key));
971
972
973
974
      } else {
        v_recipientId := p_recipientId;
      }
      log("v_recipientId=", v_recipientId);
Yann Garcia's avatar
Yann Garcia committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
      // 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_ieee1609dot2_signed_and_encrypted_data := valueof(
                                                          m_etsiTs103097Data_encrypted(
                                                                                       m_encryptedData(
                                                                                                       {
                                                                                                        m_recipientInfo_signedDataRecipInfo(
                                                                                                                                            m_pKRecipientInfo(
                                                                                                                                                              v_recipientId,
                                                                                                                                                              m_encryptedDataEncryptionKey_eciesNistP256(
                                                                                                                                                                                                      m_evciesP256EncryptedKey(
                                                                                                                                                                                                                               v_eccP256_curve_point,
991
992
                                                                                                                                                                                                                               p_encrypted_sym_key, 
                                                                                                                                                                                                                               p_authentication_vector
Yann Garcia's avatar
Yann Garcia committed
993
994
995
996
                                                                                                        ))))
                                                                                                       },
                                                                                                       m_SymmetricCiphertext_aes128ccm(
                                                                                                                                       m_aesCcmCiphertext(
997
                                                                                                                                                          p_nonce,
Yann Garcia's avatar
Yann Garcia committed
998
999
1000
                                                                                                                                                          v_encrypted_inner_ec_request
                                                                                                                                                         )
                                                                                                                                      )
For faster browsing, not all history is shown. View entire blame