LibItsPki_Functions.ttcn 82.3 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
                                           ) runs on ItsPkiHttp {
      // Local variables
vagrant's avatar
vagrant committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      // Local variables
      var InnerAtRequest v_authorization_request;
      var bitstring v_authorization_request_msg;
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
      
      if (f_generate_authorization_request(vc_eaCertificate, vc_eaHashedId8, p_private_key, p_publicKeyCompressed, p_compressedMode, v_authorization_request) == false) {
        log("*** f_http_build_authorization_request: ERROR: Failed to generate AuthorizationValidationRequest ***");
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
      // 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_authorization_request: Public encryption key: ", v_public_enc_key);
      log("*** f_http_build_authorization_request: Public encryption key comp: ", v_compressed_enc_key_mode);


      /**
         TODO: Load certificate according to the IUT role
         ==> a complete fucntion which set up the TestSustem certificate,keyy... according to the IUT role
       **/
      log("*** f_http_build_inner_ec_request: ERROR: Need to add TestSystem variable vc_aa ***");
      f_selfOrClientSyncAndVerdict("error", e_error);
      /* if (f_build_pki_secured_message(vc_aaPrivateKey, valueof(m_signerIdentifier_digest(vc_aaHashedId8)), vc_eaHashedId8/\*recipientId*\/, v_public_enc_key, v_compressed_enc_key_mode, vc_eaWholeHash, bit2oct(encvalue(m_etsiTs102941Data_authorization_request(v_authorization_request))), p_ieee1609dot2_signed_and_encrypted_data, p_request_hash) == false) { */
      /*   log("*** f_http_build_authorization_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***"); */
      /*   f_selfOrClientSyncAndVerdict("error", e_error); */
      /* } */
garciay's avatar
garciay committed
363
      
vagrant's avatar
vagrant committed
364
      log("*** f_http_build_authorization_request: DEBUG: p_ieee1609dot2_signed_and_encrypted_data = ", p_ieee1609dot2_signed_and_encrypted_data);
garciay's avatar
garciay committed
365
    } // End of function f_http_build_inner_at_request
366
    
Yann Garcia's avatar
Yann Garcia committed
367
    function f_http_build_authorization_validation_request(
garciay's avatar
garciay committed
368
369
370
371
372
373
374
                                                           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;
375
      var bitstring v_authorization_validation_request_msg;
garciay's avatar
garciay committed
376
377
      var octetstring v_public_enc_key;
      var integer v_compressed_enc_key_mode;
garciay's avatar
garciay committed
378
379
380
381
382
      
      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
383
384
385
      // 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
386
387
        f_selfOrClientSyncAndVerdict("error", e_error);
      }
garciay's avatar
garciay committed
388
389
390
391
392
393
394
395
396
397
      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);
398
      /* 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
399
400
401
      /*   log("*** f_http_build_authorization_validation_request: ERROR: Failed to generate InnerEcRequestSignedForPop ***"); */
      /*   f_selfOrClientSyncAndVerdict("error", e_error); */
      /* } */
garciay's avatar
garciay committed
402
403
404
      
      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
405
    
406
  } // End of group http
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
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

  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
515
516
517
  
  group inner_ec_xxx {
    
garciay's avatar
garciay committed
518
    function f_generate_inner_ec_request(
519
520
                                         out octetstring p_private_key,
                                         out octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
521
522
523
                                         out integer p_compressedMode,
                                         out InnerEcRequest p_inner_ec_request
                                         ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
524
      // Local variables
Yann Garcia's avatar
Yann Garcia committed
525
526
      var Oct32 v_publicKeyX;
      var Oct32 v_publicKeyY;
527
528
529
      var EccP256CurvePoint v_eccP256_curve_point;

      log (">>> f_generate_inner_ec_request");
Yann Garcia's avatar
Yann Garcia committed
530
      // Generate keys for the certificate to be requested
Yann Garcia's avatar
Yann Garcia committed
531
      if (f_generate_key_pair_nistp256(p_private_key, v_publicKeyX, v_publicKeyY, p_publicKeyCompressed, p_compressedMode) == false) {
532
        log ("f_generate_inner_ec_request: failed to generate keys");
Yann Garcia's avatar
Yann Garcia committed
533
534
535
        return false;
      }
      if (p_compressedMode == 0) {
536
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_0(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
537
      } else {
538
        v_eccP256_curve_point := valueof(m_eccP256CurvePoint_compressed_y_1(p_publicKeyCompressed));
Yann Garcia's avatar
Yann Garcia committed
539
      }
540
      log ("f_generate_inner_ec_request: ", v_eccP256_curve_point);
garciay's avatar
garciay committed
541
      // Build the Proof of Possession InnerEcRequest
Yann Garcia's avatar
Yann Garcia committed
542
543
      p_inner_ec_request := valueof(
                                    m_innerEcRequest(
544
                                                     PICS_ITS_S_CANONICAL_ID,
Yann Garcia's avatar
Yann Garcia committed
545
                                                     m_publicKeys(
546
                                                                  m_publicVerificationKey_ecdsaNistP256(v_eccP256_curve_point)
Yann Garcia's avatar
Yann Garcia committed
547
548
549
                                                                  ),
                                                     m_certificateSubjectAttributes(
                                                                                    { // ETSI TS 102 965 Table A.1: ETSI ITS standardized ITS-AIDs
vagrant's avatar
vagrant committed
550
                                                                                      valueof(m_appPermissions(623, { bitmapSsp := '00C0'O }))
Yann Garcia's avatar
Yann Garcia committed
551
552
                                                                                    },
                                                                                    m_validityPeriod(
553
554
                                                                                                     f_getCurrentTime() / 1000,
                                                                                                     m_duration_years(1) // TODO Use PIXIT
555
                                                                                                     ),
Yann Garcia's avatar
Yann Garcia committed
556
                                                                                    m_geographicRegion_identifiedRegion(
557
558
559
560
561
                                                                                                                        {
                                                                                                                          m_identifiedRegion_country_only(12), // TODO Use PIXIT
                                                                                                                          m_identifiedRegion_country_only(34) // TODO Use PIXIT
                                                                                                                          }
                                                                                                                        ),
garciay's avatar
garciay committed
562
                                                                                    'C0'O // TODO Use PIXIT
563
564
                                                                                    )
                                                     )
garciay's avatar
garciay committed
565
                                    );
566
      log ("f_generate_inner_ec_request: ", p_inner_ec_request);
Yann Garcia's avatar
Yann Garcia committed
567
568
      
      return true;
garciay's avatar
garciay committed
569
    } // End of function f_generate_inner_ec_request
Yann Garcia's avatar
Yann Garcia committed
570
    
garciay's avatar
garciay committed
571
    function f_generate_inner_ec_request_signed_for_pop(
572
                                                        in octetstring p_private_key,
garciay's avatar
garciay committed
573
574
                                                        in InnerEcRequest p_inner_ec_request,
                                                        out Ieee1609Dot2Data p_inner_ec_request_signed_for_pop
Yann Garcia's avatar
Yann Garcia committed
575
576
577
578
579
    ) 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;
580
      var octetstring v_tbs_signed;
garciay's avatar
garciay committed
581
582
583
584
585
586
587
588
589
590
591
592
      
      // 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,
593
594
                                                              f_getCurrentTimeUtc()
                                                              )
garciay's avatar
garciay committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
                               );
      // 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
    
619
    function f_generate_inner_ec_response(
620
                                          in octetstring p_inner_ec_request_hash,
621
622
623
624
625
626
627
628
                                          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(
629
                                                          substr(p_inner_ec_request_hash, 0, 16),
630
631
632
633
634
635
636
                                                          p_certificate
                                                          )
                                     );
      
      return true;
    } // End of function f_generate_inner_ec_response
    
garciay's avatar
garciay committed
637
638
  } // End of group inner_ec_xxx

639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
  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
654
655
      var octetstring public_enc_key_x;
      var octetstring public_enc_key_y;
656
657
658
659
660
661
662
663
664
665
666
      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
667
      if (f_generate_key_pair_nistp256(p_private_enc_key, public_enc_key_x, public_enc_key_y, p_publicEncKeyCompressed, p_compressedMode) == false) {
668
669
670
671
672
673
674
675
676
        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
677
      v_hmac_key := f_hashWithSha256(int2oct(f_getCurrentTime(), 12));
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
      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
705
      v_key_tag := fx_hmac_sha256(v_hmac_key, v_message_to_tag); // TODO Rename and use a wrapper function
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
      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(
726
727
728
                                                                                 m_certificate_subject_attributes( // FIXME Review subjectPermissions
                                                                                                                  p_ec_certificate.toBeSigned.appPermissions,
                                                                                                                  { { subjectPermissions := { all_ := NULL }, minChainLength := 1, chainLengthRange   := 0, eeType := '00000000'B } },
729
730
731
                                                                                                                  p_ec_certificate.toBeSigned.id,
                                                                                                                  p_ec_certificate.toBeSigned.validityPeriod,
                                                                                                                  p_ec_certificate.toBeSigned.region,
732
                                                                                                                  p_ec_certificate.toBeSigned.assuranceLevel
733
734
735
736
737
738
739
740
741
742
                                                                                                                  ))));
      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
743
      log("v_tbs=", v_tbs);
744
745
      // Signed ToBeSigned payload
      v_whole_certificate_hash := f_hashWithSha256(bit2oct(encvalue(p_ec_certificate)));
garciay's avatar
garciay committed
746
      log("v_whole_certificate_hash=", v_whole_certificate_hash);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
      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;
775
        var Oct16 v_aes_sym_key;
776
777
778
779
780
781
        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
782
783
784
785
786
787
788
789
790
        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;
        }
791
        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);
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
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
        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
  
vagrant's avatar
vagrant committed
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
  group authorization_xxx {
    
    function f_generate_authorization_request(
                                              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,
                                              out InnerAtRequest p_authorization_request
                                              ) return boolean {
      // 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_request.sharedAtRequest := v_inner_at_request.sharedAtRequest;
      p_authorization_request.ecSignature := v_inner_at_request.ecSignature;
      
      log("f_generate_authorization_request: ", p_authorization_request);
      
      return true;
    } // End of function f_generate_authorization_request
    
    function f_generate_authorization_response(
                                               in octetstring p_authorization_request_hash,
                                               in CertificateSubjectAttributes p_Certificate_subject_attributes,
                                               out InnerAtResponse p_authorization_response
                                               ) return boolean {
      // Local variables
      
      // Build the Proof of Possession InnerAtResponse
      /*p_authorization_response := valueof(
                                          m_innerAtResponse_ok(
                                                               p_authorization_request_hash,
                                                               p_Certificate_subject_attributes
                                                               )
                                                               );*/
      
      return true;
    } // End of function f_generate_authorization_response
    
  } // End of group authorization_xxx
  
908
909
910
  group authorization_validation_xxx {
    
    function f_generate_authorization_validation_request(
garciay's avatar
garciay committed
911
912
913
914
915
                                                         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,
916
917
                                                         out AuthorizationValidationRequest p_authorization_validation_request
                                                         ) return boolean {
garciay's avatar
garciay committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
      // 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
941
942
      
      return true;
943
944
945
    } // End of function f_generate_authorization_validation_request
    
    function f_generate_authorization_validation_response(
946
947
                                                          in octetstring p_authorization_validation_request_hash,
                                                          in CertificateSubjectAttributes p_Certificate_subject_attributes,
Yann Garcia's avatar
Yann Garcia committed
948
                                                          out AuthorizationValidationResponse p_authorization_validation_response
949
                                                          ) return boolean {
950
951
952
953
954
955
956
957
958
959
960
      // 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;
961
962
963
964
    } // End of function f_generate_authorization_validation_response
    
  } // End of group authorization_validation_xxx
  
garciay's avatar
garciay committed
965
  group pki_functions {
966
    
967
968
969
970
971
972
973
974
975
976
977
978
    /**
     * @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
979
    function f_build_pki_secured_message(
980
                                         in octetstring p_private_key,
981
982
                                         in SignerIdentifier p_signer_identifier,
                                         in HashedId8 p_recipientId,
983
                                         in octetstring p_publicKeyCompressed,
garciay's avatar
garciay committed
984
                                         in integer p_compressedMode,
985
                                         in octetstring p_salt,
986
                                         in octetstring p_pki_message,
987
988
989
990
                                         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,
991
992
                                         out Oct12 p_nonce,
                                         out Oct32 p_request_hash
garciay's avatar
garciay committed
993
994
995
996
                                         ) return boolean {
      // Local variables
      var template (value) EccP256CurvePoint v_eccP256_curve_point;
      var template (value) ToBeSignedData v_tbs;
997
      var octetstring v_tbs_signed;
Yann Garcia's avatar
Yann Garcia committed
998
      var template (value) Ieee1609Dot2Data v_ieee1609dot2_signed_data;
garciay's avatar
garciay committed
999
      var octetstring v_encoded_inner_ec_request;
1000
      var HashedId8 v_recipientId;
For faster browsing, not all history is shown. View entire blame