LibItsSecurity_Functions.ttcn 195 KB
Newer Older
garciay's avatar
garciay committed
1
/**
Yann Garcia's avatar
Yann Garcia committed
2
 *  @author   ETSI / STF481 / STF507 / STF517 / STF538 / STF545
garciay's avatar
garciay committed
3
4
5
 *  @version  $URL$
 *            $Id$
 *  @desc     Module containing functions for Security Protocol
garciay's avatar
garciay committed
6
7
8
9
 *  @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.
garciay's avatar
garciay committed
10
11
12
 *
 */
module LibItsSecurity_Functions {
13
    
14
    // Libcommon
15
    import from LibCommon_BasicTypesAndValues all;
16
    import from LibCommon_DataStrings all;
17

garciay's avatar
garciay committed
18
19
20
21
22
    // LibIts
    import from IEEE1609dot2BaseTypes language "ASN.1:1997" all;
    import from IEEE1609dot2 language "ASN.1:1997" all;
    import from EtsiTs103097Module language "ASN.1:1997" all;
    
23
    // LibItsCommon
filatov's avatar
filatov committed
24
25
    //import from LibItsCommon_Functions all;
    //import from LibItsCommon_TypesAndValues all;
26
27
    
    // LibItsSecurity
28
    import from LibItsSecurity_TypesAndValues all;
29
30
    import from LibItsSecurity_Templates all;
    import from LibItsSecurity_Pixits all;
garciay's avatar
garciay committed
31
    import from LibItsSecurity_Pics all;
32
    import from LibItsSecurity_TestSystem all;
garciay's avatar
garciay committed
33
34
    
    group helpersFunctions {
35
36
        
        /**
garciay's avatar
garciay committed
37
38
39
40
         * @desc    Produces a 256-bit (32-byte) hash value
         * @param   p_toBeHashedData Data to be used to calculate the hash value
         * @return  The hash value
         */
41
        function f_hashWithSha256(
garciay's avatar
garciay committed
42
                                  in octetstring p_toBeHashedData
43
        ) return Oct32 {
44
45
            return fx_hashWithSha256(p_toBeHashedData);
        } // End of function f_hashWithSha256
46
47
        
        /**
garciay's avatar
garciay committed
48
49
50
51
52
53
54
55
56
57
58
         * @desc    Produces a 384-bit (48-byte) hash value
         * @param   p_toBeHashedData Data to be used to calculate the hash value
         * @return  The hash value
         */
        function f_hashWithSha384(
                                  in octetstring p_toBeHashedData
        ) return Oct48 {
            return fx_hashWithSha384(p_toBeHashedData);
        } // End of function f_hashWithSha256
        
        /**
Yann Garcia's avatar
Yann Garcia committed
59
         * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
60
         * @param   p_toBeSignedSecuredMessage    The data to be signed
61
         * @param   p_certificateIssuer           The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
62
         * @param   p_privateKey                  The private key for signature
garciay's avatar
garciay committed
63
64
         * @return  The signature value
         */
65
        function f_signWithEcdsaNistp256WithSha256(
66
                                                   in octetstring p_toBeSignedSecuredMessage,
67
                                                   in Oct32 p_certificateIssuer,
68
                                                   in Oct32 p_privateKey
69
        ) return octetstring {
70
            return fx_signWithEcdsaNistp256WithSha256(
71
                p_toBeSignedSecuredMessage,
72
                p_certificateIssuer,
73
                p_privateKey
74
            );
75
            
76
        } // End of function f_signWithEcdsaNistp256WithSha256
77
        
garciay's avatar
garciay committed
78
        /**
Yann Garcia's avatar
Yann Garcia committed
79
         * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
80
81
         * @param   p_toBeSignedSecuredMessage    The data to be signed
         * @param   p_certificateIssuer           The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
82
         * @param   p_privateKey                  The private key for signature
garciay's avatar
garciay committed
83
84
85
86
         * @return  The signature value
         */
        function f_signWithEcdsaBrainpoolp256WithSha256(
                                                        in octetstring p_toBeSignedSecuredMessage,
87
                                                        in Oct32 p_certificateIssuer,
garciay's avatar
garciay committed
88
                                                        in Oct32 p_privateKey
89
        ) return octetstring {
garciay's avatar
garciay committed
90
91
            return fx_signWithEcdsaBrainpoolp256WithSha256(
                p_toBeSignedSecuredMessage,
92
                p_certificateIssuer,
garciay's avatar
garciay committed
93
94
95
96
97
                p_privateKey
            );
            
        } // End of function f_signWithEcdsaBrainpoolp256WithSha256
        
garciay's avatar
garciay committed
98
        /**
Yann Garcia's avatar
Yann Garcia committed
99
         * @desc    Produces a Elliptic Curve Digital Signature Algorithm (ECDSA) signature based on standard IEEE 1609.2
100
         * @param   p_toBeSignedSecuredMessage    The data to be signed
101
         * @param   p_certificateIssuer           The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
102
         * @param   p_privateKey                  The private key for signature
garciay's avatar
garciay committed
103
104
105
106
         * @return  The signature value
         */
        function f_signWithEcdsaBrainpoolp384WithSha384(
                                                        in octetstring p_toBeSignedSecuredMessage,
107
                                                        in Oct48 p_certificateIssuer,
garciay's avatar
garciay committed
108
                                                        in Oct48 p_privateKey
109
        ) return octetstring {
garciay's avatar
garciay committed
110
111
            return fx_signWithEcdsaBrainpoolp384WithSha384(
                p_toBeSignedSecuredMessage,
112
                p_certificateIssuer,
garciay's avatar
garciay committed
113
114
115
116
117
                p_privateKey
            );
            
        } // End of function f_signWithEcdsaBrainpoolp384WithSha384
        
118
        function f_decrypt(
119
                           in octetstring         p_encryptPrivateKey,
120
                           in EtsiTs103097Data    p_encrypedSecuredMessage,
121
                           in octetstring         p_salt,
Yann Garcia's avatar
Yann Garcia committed
122
123
                           out EtsiTs103097Data   p_decrypedSecuredMessage,
                           out octetstring        p_aes_sym_enc_key
124
        ) return boolean {
125
            if (ischosen(p_encrypedSecuredMessage.content.encryptedData)) {
126
127
                var PKRecipientInfo v_pKRecipientInfo;
                var RecipientInfo v_recipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0];
128
            var octetstring v_decryptedSecuredMessage;
129
                
130
131
132
133
134
135
                // Check the private encryption key
                if (not(isbound(p_encryptPrivateKey))) {
                  log("*** " & testcasename() & ":ERROR: Failed to load encryption private key ***");
                  return false;
                }
                
136
                if (ischosen(v_recipientInfo.certRecipInfo)) {
137
138
                  v_pKRecipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0].certRecipInfo;
                  // Read the certificate based on the recipientId
139
                } else if (ischosen(v_recipientInfo.signedDataRecipInfo)) {
140
141
                v_pKRecipientInfo := p_encrypedSecuredMessage.content.encryptedData.recipients[0].signedDataRecipInfo;
                  // Read the certificate based on the recipientId
142
                } else {
143
144
                  log("*** " & testcasename() & ":ERROR: Unsupported RecipientInfo variant ***");
                  return false;
145
146
                }
                
147
            if (isbound(v_pKRecipientInfo)) {
148
149
                if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256)) {
                  var SymmetricCiphertext v_ciphertext := p_encrypedSecuredMessage.content.encryptedData.ciphertext;
150
151
152
153
154
155
156
157
                  if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_0)) {
                      v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                                                                                        v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                        p_encryptPrivateKey,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_0,
                                                                                        0,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.c,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.t,
158
                                                                                        v_ciphertext.aes128ccm.nonce,
Yann Garcia's avatar
Yann Garcia committed
159
160
                                                                                        p_salt,
                                                                                        p_aes_sym_enc_key
161
162
163
164
165
166
167
168
169
                                                                                        );
                  } else if (ischosen(v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_1)) {
                      v_decryptedSecuredMessage := f_decryptWithEciesNistp256WithSha256(
                                                                                        v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                        p_encryptPrivateKey,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.v.compressed_y_1,
                                                                                        1,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.c,
                                                                                        v_pKRecipientInfo.encKey.eciesNistP256.t,
170
                                                                                        v_ciphertext.aes128ccm.nonce,
Yann Garcia's avatar
Yann Garcia committed
171
172
                                                                                        p_salt,
                                                                                        p_aes_sym_enc_key
173
174
175
176
177
                                                                                        );
                  } else {
                    log("*** " & testcasename() & ":ERROR: Non canonical ephemeral encryption keys ***");
                    return false;
                  }
178
179
180
181
                  if (isbound(v_decryptedSecuredMessage)) {
                    var bitstring v_decode := oct2bit(v_decryptedSecuredMessage);
                    if (decvalue(v_decode, p_decrypedSecuredMessage) == 0) {
                      return true;
182
183
                    } else {
                      log("*** " & testcasename() & ":ERROR: Faild to decode secured message ***");
184
                    }
185
                  }
186
                } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1)) {
187
                  var SymmetricCiphertext v_ciphertext := p_encrypedSecuredMessage.content.encryptedData.ciphertext;
188
189
190
191
192
193
194
195
                  if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_0)) {
                    v_decryptedSecuredMessage := f_decryptWithEciesBrainpoolp256WithSha256(
                                                                                           v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                           p_encryptPrivateKey,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_0,
                                                                                           0,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.c,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.t,
Yann Garcia's avatar
Yann Garcia committed
196
197
                                                                                           v_ciphertext.aes128ccm.nonce,
                                                                                           p_salt,                                                                                                                                                     p_aes_sym_enc_key
198
199
200
201
202
203
204
205
206
                                                                                      );
                  } else if (ischosen(v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_1)) {
                    v_decryptedSecuredMessage := f_decryptWithEciesBrainpoolp256WithSha256(
                                                                                           v_ciphertext.aes128ccm.ccmCiphertext,
                                                                                           p_encryptPrivateKey,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.v.compressed_y_1,
                                                                                           1,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.c,
                                                                                           v_pKRecipientInfo.encKey.eciesBrainpoolP256r1.t,
Yann Garcia's avatar
Yann Garcia committed
207
208
209
                                                                                           v_ciphertext.aes128ccm.nonce,
                                                                                           p_salt,
                                                                                           p_aes_sym_enc_key
210
211
212
213
214
                                                                                      );
                  } else {
                    log("*** " & testcasename() & ":ERROR: Non canonical ephemeral encryption keys ***");
                    return false;
                  }
215
216
217
218
219
220
221
222
223
              } else {
                log("*** " & testcasename() & ":ERROR: Non canonical ephemeral encryption keys ***");
                return false;
              }
              // TODO else, other variants shall be processed here if
            } else {
              log("*** " & testcasename() & ":ERROR: Invalid recipient info ***");
              return false;
            }
224
225
226
227
                  if (isbound(v_decryptedSecuredMessage)) {
                    var bitstring v_decode := oct2bit(v_decryptedSecuredMessage);
                    if (decvalue(v_decode, p_decrypedSecuredMessage) == 0) {
                      return true;
228
229
                    } else {
                      log("*** " & testcasename() & ":ERROR: Faild to decode secured message ***");
230
231
                    }
                  }
232
233
            } else {
              log("*** " & testcasename() & ":ERROR: Message not encrypted ***");
234
235
236
            }
            
            return false;
237
        } // End of function f_decrypt
garciay's avatar
garciay committed
238

239
        /**
240
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Nist-P256 algorithm
241
         * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
242
         * @param   p_recipientsPublicKeyCompressed  The Recipient's compressed public key
Yann Garcia's avatar
Yann Garcia committed
243
         * @param   p_compressed_mode                 The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
244
245
         * @param   p_publicEphemeralKeyCompressed   The generated ephemeral compressed key
         * @param   p_ephemeralKeyModeCompressed     The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
246
247
248
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
249
250
251
         * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
         * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
         * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
252
         * @return  The encrypted message
253
254
255
         */
        function f_encryptWithEciesNistp256WithSha256(
                                                      in octetstring p_toBeEncryptedSecuredMessage,
256
                                                      in Oct32 p_recipientsPublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
257
                                                      in integer p_compressed_mode,
258
                                                      in octetstring p_salt,
259
260
                                                      out Oct32 p_publicEphemeralKeyCompressed,
                                                      out integer p_ephemeralKeyModeCompressed,
261
                                                      out Oct16 p_aes_sym_key,
garciay's avatar
garciay committed
262
                                                      out Oct16 p_encrypted_sym_key,
garciay's avatar
garciay committed
263
                                                      out Oct16 p_authentication_vector,
264
265
                                                      out Oct12 p_nonce,
                                                      in boolean p_use_hardcoded_values := false
266
        ) return octetstring {
267
          return fx_encryptWithEciesNistp256WithSha256(
268
                                                       p_toBeEncryptedSecuredMessage,
269
                                                       p_recipientsPublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
270
                                                       p_compressed_mode,
271
                                                       p_salt,
272
273
                                                       p_publicEphemeralKeyCompressed,
                                                       p_ephemeralKeyModeCompressed,
274
                                                       p_aes_sym_key,
275
276
                                                       p_encrypted_sym_key,
                                                       p_authentication_vector,
277
278
                                                       p_nonce,
                                                       p_use_hardcoded_values
279
                                                       );
280
281
282
        } // End of function f_encryptWithEciesNistp256WithSha256
        
        /**
283
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Nist-P256 algorithm
284
         * @param   p_encryptedSecuredMessage    The data to be decrypted
285
286
         * @param   p_publicEphemeralKeyCompressed   The generated ephemeral compressed key
         * @param   p_ephemeralKeyModeCompressed     The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
287
288
289
290
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The decrypted message
291
292
293
         * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
         * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
         * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
294
295
296
         */
        function f_decryptWithEciesNistp256WithSha256(
                                                      in octetstring p_encryptedSecuredMessage,
297
                                                      in Oct32 p_privateEncKey,
298
299
                                                      in Oct32 p_publicEphemeralKeyCompressed,
                                                      in integer p_ephemeralKeyModeCompressed,
300
301
                                                      in Oct16 p_encrypted_sym_key,
                                                      in Oct16 p_authentication_vector,
302
                                                      in Oct12 p_nonce,
Yann Garcia's avatar
Yann Garcia committed
303
304
                                                      in Oct32 p_salt,
                                                      out Oct16 p_aes_sym_enc_key
305
        ) return octetstring {
306
307
308
          return fx_decryptWithEciesNistp256WithSha256(
                                                       p_encryptedSecuredMessage,
                                                       p_privateEncKey,
309
310
                                                       p_publicEphemeralKeyCompressed, 
                                                       p_ephemeralKeyModeCompressed,
311
312
                                                       p_encrypted_sym_key,
                                                       p_authentication_vector,
313
                                                       p_nonce,
Yann Garcia's avatar
Yann Garcia committed
314
315
                                                       p_salt,
                                                       p_aes_sym_enc_key
316
                                                       );
317
318
        } // End of function f_decryptWithEcdsaNistp256WithSha256
        
319
320
321
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) encryption using Brainpool-P256 algorithm
         * @param   p_toBeEncryptedSecuredMessage    The data to be encrypted
322
         * @param   p_recipientsPublicKeyCompressed  The Recipient's compressed public key
Yann Garcia's avatar
Yann Garcia committed
323
         * @param   p_compressed_mode                 The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
324
325
         * @param   p_publicEphemeralKeyCompressed   The generated ephemeral compressed key
         * @param   p_ephemeralKeyModeCompressed     The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
326
327
328
329
330
331
332
333
334
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The encrypted message
         * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
         * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
         * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
         */
        function f_encryptWithEciesBrainpoolp256WithSha256(
335
                                                           in octetstring p_toBeEncryptedSecuredMessage,
336
                                                           in Oct32 p_recipientsPublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
337
                                                           in integer p_compressed_mode,
338
                                                           in octetstring p_salt,
339
340
                                                           out Oct32 p_publicEphemeralKeyCompressed,
                                                           out integer p_ephemeralKeyModeCompressed,
341
                                                           out Oct16 p_aes_sym_key,
342
343
                                                           out Oct16 p_encrypted_sym_key,
                                                           out Oct16 p_authentication_vector,
344
345
                                                           out Oct12 p_nonce,
                                                           in boolean p_use_hardcoded_values := false
346
347
        ) return octetstring {
          return fx_encryptWithEciesBrainpoolp256WithSha256(
348
                                                            p_toBeEncryptedSecuredMessage,
349
                                                            p_recipientsPublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
350
                                                            p_compressed_mode,
351
                                                            p_salt,
352
353
                                                            p_publicEphemeralKeyCompressed,
                                                            p_ephemeralKeyModeCompressed,
354
                                                            p_aes_sym_key,
355
356
                                                            p_encrypted_sym_key,
                                                            p_authentication_vector,
357
358
                                                            p_nonce,
                                                            p_use_hardcoded_values
359
                                                            );
360
361
362
363
364
        } // End of function f_encryptWithEciesBrainpoolp256WithSha256
        
        /**
         * @desc    Produces a Elliptic Curve Digital Encrytion Algorithm (ECIES) decryption using Brainpool-P256 algorithm
         * @param   p_encryptedSecuredMessage    The data to be decrypted
365
366
         * @param   p_publicEphemeralKeyCompressed   The generated ephemeral compressed key
         * @param   p_ephemeralKeyModeCompressed     The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
367
368
369
370
371
372
373
374
375
         * @param   p_encrypted_sym_key              The encrypted AES 128 CCM symmetric key
         * @param   p_authentication_vector          The tag of the AES 128 CCM symmetric key encryption
         * @param   p_nonce                          The nonce vector of the AES 128 CCM symmetric key encryption
         * @return  The decrypted message
         * @see IEEE Std 1609.2-2017 Clause 5.3.5 Public key encryption algorithms: ECIES
         * @see https://www.nominet.uk/researchblog/how-elliptic-curve-cryptography-encryption-works/
         * @see http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf
         */
        function f_decryptWithEciesBrainpoolp256WithSha256(
376
377
378
379
380
381
                                                           in octetstring p_encryptedSecuredMessage,
                                                           in Oct32 p_privateEncKey,
                                                           in Oct32 p_publicEphemeralKeyCompressed, 
                                                           in integer p_ephemeralKeyModeCompressed,
                                                           in Oct16 p_encrypted_sym_key,
                                                           in Oct16 p_authentication_vector,
Yann Garcia's avatar
Yann Garcia committed
382
383
384
                                                           in Oct12 p_nonce,
                                                           in Oct32 p_salt,
                                                           out Oct16 p_aes_sym_enc_key
385
386
        ) return octetstring {
          return fx_decryptWithEciesBrainpoolp256WithSha256(
387
388
389
390
391
392
                                                            p_encryptedSecuredMessage,
                                                            p_privateEncKey,
                                                            p_publicEphemeralKeyCompressed, 
                                                            p_ephemeralKeyModeCompressed,
                                                            p_encrypted_sym_key,
                                                            p_authentication_vector,
Yann Garcia's avatar
Yann Garcia committed
393
394
395
                                                            p_nonce,
                                                            p_salt,
                                                            p_aes_sym_enc_key
396
                                                            );
397
398
        } // End of function f_decryptWithEcdsaBrainpoolp256WithSha256
        
399
        /**
garciay's avatar
garciay committed
400
401
402
403
404
         * @desc Compute the HashedId8 value from the hash value
         * @param p_hash The hash value
         * @return The HashedId8 value
         * @verdict
         */
405
        function f_HashedId8FromSha256( 
garciay's avatar
garciay committed
406
                                       in Oct32 p_hash
407
408
        ) return HashedId8 {
            return substr(p_hash, lengthof(p_hash) - 8, 8);
berge's avatar
berge committed
409
        } // End of function f_HashedId8FromSha256
410
        
411
412
413
414
415
416
417
418
419
420
421
422
        /**
         * @desc Compute the HashedId8 value from the hash value
         * @param p_hash The hash value
         * @return The HashedId8 value
         * @verdict
         */
        function f_HashedId8FromSha384( 
                                       in Oct48 p_hash
        ) return HashedId8 {
            return substr(p_hash, lengthof(p_hash) - 8, 8);
        } // End of function f_HashedId8FromSha384
        
423
        /**
garciay's avatar
garciay committed
424
425
426
427
428
         * @desc Compute the HashedId3 value from the HashedId8 value
         * @param p_hashp_hashedId8 The HashedId8 value
         * @return The HashedId3 value
         * @verdict Unchanged
         */
garciay's avatar
garciay committed
429
430
        function f_HashedId3FromHashedId8(
                                          in HashedId8 p_hashedId8
431
432
        ) return HashedId3 {
            return substr(p_hashedId8, lengthof(p_hashedId8) - 3, 3);
433
        }  // End of function f_HashedId3FromHashedId8
434
435
        
        /**
garciay's avatar
garciay committed
436
437
         * @desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
438
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
garciay's avatar
garciay committed
439
         * @param   p_signature                 The signature
440
         * @param   p_ecdsaNistp256PublicKeyCompressed   The compressed public key
Yann Garcia's avatar
Yann Garcia committed
441
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
442
443
         * @return  true on success, false otherwise
         */
444
        function f_verifyWithEcdsaNistp256WithSha256(
garciay's avatar
garciay committed
445
                                                     in octetstring p_toBeVerifiedData,
446
447
                                                     in Oct32 p_certificateIssuer,
                                                     in Oct64 p_signature,
448
                                                     in Oct32 p_ecdsaNistp256PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
449
                                                     in integer p_compressed_mode
450
451
452
453
454
455
456
        ) return boolean {
//            log("f_verifyWithEcdsaNistp256WithSha256: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaNistp256WithSha256: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaNistp256WithSha256: signature", p_signature);
//            log("f_verifyWithEcdsaNistp256WithSha256: ecdsaNistp256PublicKeyCompressed", p_ecdsaNistp256PublicKeyCompressed);
            return fx_verifyWithEcdsaNistp256WithSha256(
                                                        p_toBeVerifiedData,
457
                                                        p_certificateIssuer,
458
459
                                                        p_signature,
                                                        p_ecdsaNistp256PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
460
                                                        p_compressed_mode
461
462
463
464
465
466
                                                        );
        } // End of function f_verifyWithEcdsaNistp256WithSha256
        
        /**
         * @desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
467
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
468
469
470
471
472
473
474
         * @param   p_signature                 The signature
         * @param   p_ecdsaNistp256PublicKeyX   The public key (x coordinate)
         * @param   p_ecdsaNistp256PublicKeyY   The public key (y coordinate)
         * @return  true on success, false otherwise
         */
        function f_verifyWithEcdsaNistp256WithSha256_1( // TODO To be removed
                                                       in octetstring p_toBeVerifiedData,
475
476
                                                       in Oct32 p_certificateIssuer,
                                                       in Oct64 p_signature,
477
478
                                                       in Oct32 p_ecdsaNistp256PublicKeyX,
                                                       in Oct32 p_ecdsaNistp256PublicKeyY
479
        ) return boolean {
480
481
482
483
484
//            log("f_verifyWithEcdsaNistp256WithSha256: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaNistp256WithSha256: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaNistp256WithSha256: signature", p_signature);
//            log("f_verifyWithEcdsaNistp256WithSha256: ecdsaNistp256PublicKeyX", p_ecdsaNistp256PublicKeyX);
//            log("f_verifyWithEcdsaNistp256WithSha256: ecdsaNistp256PublicKeyY", p_ecdsaNistp256PublicKeyY);
485
            return fx_verifyWithEcdsaNistp256WithSha256_1(
berge's avatar
berge committed
486
                p_toBeVerifiedData,
487
                p_certificateIssuer,
berge's avatar
berge committed
488
489
                p_signature,
                p_ecdsaNistp256PublicKeyX,
490
                p_ecdsaNistp256PublicKeyY);
491
        } // End of function f_verifyWithEcdsaNistp256WithSha256_1
492
        
garciay's avatar
garciay committed
493
494
495
        /**
         * @Desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
496
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
garciay's avatar
garciay committed
497
         * @param   p_signature                 The signature
498
         * @param   p_ecdsaBrainpoolp256PublicKeyCompressed   The compressed public key
Yann Garcia's avatar
Yann Garcia committed
499
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
500
501
502
503
         * @return  true on success, false otherwise
         */
        function f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                          in octetstring p_toBeVerifiedData,
504
505
                                                          in Oct32 p_certificateIssuer,
                                                          in Oct64 p_signature,
506
                                                          in Oct32 p_ecdsaBrainpoolp256PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
507
                                                          in integer p_compressed_mode
508
509
510
511
512
513
514
        ) return boolean {
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: signature", p_signature);
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: ecdsaBrainpoolp256PublicKeyCompressed", p_ecdsaBrainpoolp256PublicKeyCompressed);
            return fx_verifyWithEcdsaBrainpoolp256WithSha256(
                                                             p_toBeVerifiedData,
515
                                                             p_certificateIssuer,
516
517
                                                             p_signature,
                                                             p_ecdsaBrainpoolp256PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
518
                                                             p_compressed_mode
519
520
521
522
523
524
                                                             );
        } // End of function f_verifyWithEcdsaBrainpoolp256WithSha256
        
        /**
         * @Desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
525
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
526
527
528
529
530
531
532
         * @param   p_signature                 The signature
         * @param   p_ecdsaBrainpoolp256PublicKeyX   The public key (x coordinate)
         * @param   p_ecdsaBrainpoolp256PublicKeyY   The public key (y coordinate)
         * @return  true on success, false otherwise
         */
        function f_verifyWithEcdsaBrainpoolp256WithSha256_1( // TODO To be removed
                                                            in octetstring p_toBeVerifiedData,
533
534
                                                            in Oct32 p_certificateIssuer,
                                                            in Oct64 p_signature,
535
536
                                                            in Oct32 p_ecdsaBrainpoolp256PublicKeyX,
                                                            in Oct32 p_ecdsaBrainpoolp256PublicKeyY
garciay's avatar
garciay committed
537
538
539
540
541
542
        ) return boolean {
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: signature", p_signature);
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: ecdsaBrainpoolp256PublicKeyX", p_ecdsaBrainpoolp256PublicKeyX);
//            log("f_verifyWithEcdsaBrainpoolp256WithSha256: ecdsaBrainpoolp256PublicKeyY", p_ecdsaBrainpoolp256PublicKeyY);
543
            return fx_verifyWithEcdsaBrainpoolp256WithSha256_1(
garciay's avatar
garciay committed
544
                p_toBeVerifiedData,
545
                p_certificateIssuer,
garciay's avatar
garciay committed
546
547
548
                p_signature,
                p_ecdsaBrainpoolp256PublicKeyX,
                p_ecdsaBrainpoolp256PublicKeyY);
549
        } // End of function f_verifyWithEcdsaBrainpoolp256WithSha256_1
garciay's avatar
garciay committed
550
551
552
553
        
        /**
         * @Desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
554
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
garciay's avatar
garciay committed
555
556
557
         * @param   p_signature                 The signature
         * @param   p_ecdsaBrainpoolp384PublicKeyX   The public key (x coordinate)
         * @param   p_ecdsaBrainpoolp384PublicKeyY   The public key (y coordinate)
Yann Garcia's avatar
Yann Garcia committed
558
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
559
560
561
562
         * @return  true on success, false otherwise
         */
        function f_verifyWithEcdsaBrainpoolp384WithSha384(
                                                          in octetstring p_toBeVerifiedData,
563
564
                                                          in Oct48 p_certificateIssuer,
                                                          in Oct96 p_signature,
565
                                                          in Oct48 p_ecdsaBrainpoolp384PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
566
                                                          in integer p_compressed_mode
567
568
569
570
571
572
573
        ) return boolean {
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: signature", p_signature);
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: ecdsaBrainpoolp384PublicKeyCompressed", p_ecdsaBrainpoolp384PublicKeyCompressed);
            return fx_verifyWithEcdsaBrainpoolp384WithSha384(
                                                             p_toBeVerifiedData,
574
                                                             p_certificateIssuer,
575
576
                                                             p_signature,
                                                             p_ecdsaBrainpoolp384PublicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
577
                                                             p_compressed_mode
578
579
580
581
582
583
                                                             );
        } // End of function f_verifyWithEcdsaBrainpoolp384WithSha384
        
        /**
         * @Desc    Verify the signature of the specified data
         * @param   p_toBeVerifiedData          The data to be verified
584
         * @param   p_certificateIssuer         The whole-hash issuer certificate or int2oct(0, 32) in case of self signed certificate
585
586
587
588
589
590
591
         * @param   p_signature                 The signature
         * @param   p_ecdsaBrainpoolp384PublicKeyX   The public key (x coordinate)
         * @param   p_ecdsaBrainpoolp384PublicKeyY   The public key (y coordinate)
         * @return  true on success, false otherwise
         */
        function f_verifyWithEcdsaBrainpoolp384WithSha384_1( // TODO To be removed
                                                            in octetstring p_toBeVerifiedData,
592
593
                                                            in Oct48 p_certificateIssuer,
                                                            in Oct96 p_signature,
594
595
                                                            in Oct48 p_ecdsaBrainpoolp384PublicKeyX,
                                                            in Oct48 p_ecdsaBrainpoolp384PublicKeyY
garciay's avatar
garciay committed
596
597
598
599
600
601
        ) return boolean {
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: toBeVerifiedData", p_toBeVerifiedData);
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: toBeVerifiedData length", lengthof(p_toBeVerifiedData));
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: signature", p_signature);
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: ecdsaBrainpoolp384PublicKeyX", p_ecdsaBrainpoolp384PublicKeyX);
//            log("f_verifyWithEcdsaBrainpoolp384WithSha384: ecdsaBrainpoolp384PublicKeyY", p_ecdsaBrainpoolp384PublicKeyY);
602
            return fx_verifyWithEcdsaBrainpoolp384WithSha384_1(
garciay's avatar
garciay committed
603
                p_toBeVerifiedData,
604
                p_certificateIssuer,
garciay's avatar
garciay committed
605
606
607
                p_signature,
                p_ecdsaBrainpoolp384PublicKeyX,
                p_ecdsaBrainpoolp384PublicKeyY);
608
        } // End of function f_verifyWithEcdsaBrainpoolp384WithSha384_1
garciay's avatar
garciay committed
609
        
garciay's avatar
garciay committed
610
611
612
613
614
615
        /**
         * @desc    Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
         *          This function should not be used by the ATS
         * @param   p_privateKey    The new private key value
         * @param   p_publicKeyX    The new public key value (x coordinate)
         * @param   p_publicKeyX    The new public key value (y coordinate)
616
         * @param   p_publicKeyCompressed The compressed public keys
Yann Garcia's avatar
Yann Garcia committed
617
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
618
619
         * @return  true on success, false otherwise
         */
garciay's avatar
garciay committed
620
        function f_generate_key_pair_nistp256(
garciay's avatar
garciay committed
621
622
                                              out Oct32 p_privateKey,
                                              out Oct32 p_publicKeyX,
623
624
                                              out Oct32 p_publicKeyY,
                                              out Oct32 p_publicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
625
                                              out integer p_compressed_mode
garciay's avatar
garciay committed
626
                                             ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
627
628
          log(">>> f_generate_key_pair_nistp256");
          
629
630
631
632
633
          if (PICS_SEC_FIXED_KEYS) { // Debug mode: Use fixed values
           p_privateKey := '43481BC44C073C1432DB6EC4F0EF57062BEA08E4C19F811567325AD1FD1C6577'O;
           p_publicKeyX := '0B5D74B033531C51D17B4F218DD4E39289AE4BF2EE3D7BAB7C07DAF0C14F0317'O;
           p_publicKeyY := '5D49B139A9237832FDE24D77555878CE65D6C2284A1BDA4CE08ABDD4071E0255'O;
           p_publicKeyCompressed := '0B5D74B033531C51D17B4F218DD4E39289AE4BF2EE3D7BAB7C07DAF0C14F0317'O;
Yann Garcia's avatar
Yann Garcia committed
634
           p_compressed_mode := 1;
garciay's avatar
garciay committed
635
636
637

           return true;
         }
Yann Garcia's avatar
Yann Garcia committed
638
          return fx_generateKeyPair_nistp256(p_privateKey, p_publicKeyX, p_publicKeyY, p_publicKeyCompressed, p_compressed_mode);
garciay's avatar
garciay committed
639
640
641
642
643
644
645
646
        }
        
        /**
         * @desc    Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
         *          This function should not be used by the ATS
         * @param   p_privateKey    The new private key value
         * @param   p_publicKeyX    The new public key value (x coordinate)
         * @param   p_publicKeyX    The new public key value (y coordinate)
647
         * @param   p_publicKeyCompressed The compressed public keys
Yann Garcia's avatar
Yann Garcia committed
648
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
649
650
651
         * @return  true on success, false otherwise
         */
        function f_generate_key_pair_brainpoolp256(
garciay's avatar
garciay committed
652
653
                                                   out Oct32 p_privateKey,
                                                   out Oct32 p_publicKeyX,
654
655
                                                   out Oct32 p_publicKeyY,
                                                   out Oct32 p_publicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
656
                                                   out integer p_compressed_mode
garciay's avatar
garciay committed
657
                                                  ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
658
          log(">>> f_generate_key_pair_brainpoolp256");
Yann Garcia's avatar
Yann Garcia committed
659
          return fx_generateKeyPair_brainpoolp256(p_privateKey, p_publicKeyX, p_publicKeyY, p_publicKeyCompressed, p_compressed_mode);
garciay's avatar
garciay committed
660
661
662
663
664
665
666
667
        }
        
        /**
         * @desc    Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
         *          This function should not be used by the ATS
         * @param   p_privateKey    The new private key value
         * @param   p_publicKeyX    The new public key value (x coordinate)
         * @param   p_publicKeyX    The new public key value (y coordinate)
668
         * @param   p_publicKeyCompressed The compressed public keys
Yann Garcia's avatar
Yann Garcia committed
669
         * @param   p_compressed_mode The compressed mode, 0 if the latest bit of Y-coordinate is 0, 1 otherwise
garciay's avatar
garciay committed
670
671
672
         * @return  true on success, false otherwise
         */
        function f_generate_key_pair_brainpoolp384(
garciay's avatar
garciay committed
673
674
                                                   out Oct48 p_privateKey,
                                                   out Oct48 p_publicKeyX,
675
676
                                                   out Oct48 p_publicKeyY,
                                                   out Oct48 p_publicKeyCompressed,
Yann Garcia's avatar
Yann Garcia committed
677
                                                   out integer p_compressed_mode
garciay's avatar
garciay committed
678
                                                  ) return boolean {
Yann Garcia's avatar
Yann Garcia committed
679
          return fx_generateKeyPair_brainpoolp384(p_privateKey, p_publicKeyX, p_publicKeyY, p_publicKeyCompressed, p_compressed_mode);
garciay's avatar
garciay committed
680
681
        }
        
garciay's avatar
garciay committed
682
683
684
685
686
687
688
689
690
691
692
        /**
        * @desc    Calculate digest over the certificate
        * @param   p_cert The certificate
        * @return  the HashedId8 value
        * @see Draft ETSI TS 103 097 V1.1.14 Clause 4.2.13   HashedId8
        */
        function f_calculateDigestFromCertificate(
                                                  in Certificate p_cert
        ) return HashedId8 {
            var octetstring v_hash;
            
693
            if (PICS_SEC_SHA256) {
garciay's avatar
garciay committed
694
                v_hash := f_calculateDigestSha256FromCertificate(p_cert);
695
            } if (PICS_SEC_SHA384) {
garciay's avatar
garciay committed
696
697
698
699
700
701
                v_hash := f_calculateDigestSha384FromCertificate(p_cert);
            } 
            
            return substr(v_hash, lengthof(v_hash) - 8, 8);
        } // End of function f_calculateDigestFromCertificate
        
702
        /**
berge's avatar
berge committed
703
704
        * @desc    Calculate digest over the certificate
        * @param   p_cert The certificate
705
        * @return  the HashedId8 value
706
        * @see Draft ETSI TS 103 097 V1.1.14 Clause 4.2.13   HashedId8
berge's avatar
berge committed
707
        */
garciay's avatar
garciay committed
708
709
        function f_calculateDigestSha256FromCertificate(
                                                        in EtsiTs103097Certificate p_cert
710
        ) return HashedId8 {
711
712
            var octetstring v_toBeHashedData;
            var octetstring v_hash;
713
                        
714
715
716
            v_toBeHashedData := bit2oct(encvalue(p_cert));
            v_hash := f_hashWithSha256(v_toBeHashedData);
            return substr(v_hash, lengthof(v_hash) - 8, 8);
garciay's avatar
garciay committed
717
718
719
720
721
722
723
724
725
726
727
728
        } // End of function f_calculateDigestSha256FromCertificate
        
        function f_calculateDigestSha384FromCertificate(
                                                        in EtsiTs103097Certificate p_cert
        ) return HashedId8 {
            var octetstring v_toBeHashedData;
            var octetstring v_hash;
                        
            v_toBeHashedData := bit2oct(encvalue(p_cert));
            v_hash := f_hashWithSha384(v_toBeHashedData);
            return substr(v_hash, lengthof(v_hash) - 8, 8);
        } // End of function f_calculateDigestSha384FromCertificate
729
        
garciay's avatar
garciay committed
730
731
        function f_duration2time(
                                 in Duration p_duration
garciay's avatar
garciay committed
732
733
734
735
736
737
738
739
740
741
742
        ) return UInt16 {
            if (ischosen(p_duration.seconds)) {
                return p_duration.seconds;
            } else if (ischosen(p_duration.minutes)) { 
                return p_duration.minutes;
            } else if (ischosen(p_duration.hours)) { 
                return p_duration.hours;
            } else if (ischosen(p_duration.sixtyHours)) {
                return p_duration.sixtyHours;
            } else if (ischosen(p_duration.years)) {
                return p_duration.years;
garciay's avatar
garciay committed
743
744
            }
            
garciay's avatar
garciay committed
745
            return 0; // Unrechable code
garciay's avatar
garciay committed
746
747
        } // End of function f_duration2time
        
748
        group hostSignatureHelpers {
749
            
750
751
752
753
754
755
            /**
            * @desc    Initialize [out] certificates according to the specified certificate name
            * @param   p_certificateName The certificate name to be used
            * @param   p_aaCertificate The AA certificate [out]
            * @param   p_atCertificate The AT certificate [out]
            * @return  true on succes, false otherwise
756
            * @see Draft ETSI TS 103 097 V1.1.14 Clause 4.2.13   HashedId8
757
            */
garciay's avatar
garciay committed
758
759
            function f_prepareCertificates(
                                           in template (omit) charstring p_certificateName,
garciay's avatar
garciay committed
760
761
                                           out EtsiTs103097Certificate p_aaCertificate,
                                           out EtsiTs103097Certificate p_atCertificate
garciay's avatar
garciay committed
762
            ) runs on ItsSecurityBaseComponent return boolean {
garciay's avatar
garciay committed
763
                //log(">>> f_prepareCertificates: ", p_certificateName);
garciay's avatar
garciay committed
764
765
                
                // Load certificates if required
garciay's avatar
garciay committed
766
767
768
769
                if ((lengthof(p_certificateName) > 0) and (valueof(p_certificateName) != cc_taCert_A)) {
                    var HashedId8 v_digest;
                    var charstring v_cert;
                    
770
                    if (f_readCertificate(valueof(p_certificateName), p_atCertificate) == false){
garciay's avatar
garciay committed
771
772
773
774
775
776
777
778
779
                        log("f_prepareCertificates: Failed to read certificate ", p_certificateName);
                        return false;
                    }
                    if (ischosen(p_atCertificate.issuer.sha256AndDigest)) {
                        v_digest := p_atCertificate.issuer.sha256AndDigest;
                    } else if (ischosen(p_atCertificate.issuer.sha384AndDigest)) {
                        v_digest := p_atCertificate.issuer.sha384AndDigest;
                    } else {
                        log("f_prepareCertificates: Invalid certificate issuer ", p_atCertificate.issuer);
garciay's avatar
garciay committed
780
781
                        return false;
                    }
garciay's avatar
garciay committed
782
783
                    if (f_getCertificateFromDigest(v_digest, p_aaCertificate) == false) {
                        log("f_prepareCertificates: Failed to read certificate issuer ", v_digest);
garciay's avatar
garciay committed
784
785
786
787
                        return false;
                    }
                } else {
                    p_atCertificate := vc_atCertificate;
788
                    p_aaCertificate := vc_aaCertificate;
garciay's avatar
garciay committed
789
                }
Yann Garcia's avatar
Yann Garcia committed
790
                // Store the certificate to build this message
garciay's avatar
garciay committed
791
792
793
794
                vc_lastAtCertificateUsed := p_atCertificate;
                
                return true;
            } // End of function f_prepareCertificates
795
            
796
            /**
berge's avatar
berge committed
797
798
             * @desc  This function build and sign the SecureMessage part covered by the signature process
             * @param p_securedMessage      The signed  SecureMessage part
799
             * @param p_payloadField       Payloads to be included in the message
berge's avatar
berge committed
800
             * @param p_mandatoryHeaders    Mandatory headers for the selected profile 
garciay's avatar
garciay committed
801
             * @param p_headerInfo        HeaderInfo to be inserted in the message
berge's avatar
berge committed
802
803
804
805
             * @param p_securityProfile     Selected security profile
             * @return true on success, false otherwise
             */
            function f_buildGnSecuredMessage(
garciay's avatar
garciay committed
806
                                             inout EtsiTs103097Data p_securedMessage,
garciay's avatar
garciay committed
807
808
                                             in charstring p_certificateName,
                                             in ToBeSignedData p_payloadField
809
            ) runs on ItsSecurityBaseComponent return boolean {
garciay's avatar
garciay committed
810
                
811
                // Local variables
812
                var octetstring v_secPayload, v_signature;
garciay's avatar
garciay committed
813
                var ToBeSignedData v_toBeSignedData;
814
                var octetstring v_certificateIssuer;
815
                var octetstring v_privateKey;
garciay's avatar
garciay committed
816

Yann Garcia's avatar
Yann Garcia committed
817
818
                log(">>> f_buildGnSecuredMessage: p_securedMessage=", p_securedMessage);
                log(">>> f_buildGnSecuredMessage: p_payloadField=", p_payloadField);
garciay's avatar
garciay committed
819
                
820
                // Prepare payload to be signed
garciay's avatar
garciay committed
821
                v_toBeSignedData := valueof(p_payloadField);
garciay's avatar
garciay committed
822
                v_secPayload := bit2oct(encvalue(v_toBeSignedData));
823
824
                
                // Signed payload
825
826
827
828
829
                if (ispresent(p_certificateName) and (valueof(p_certificateName) != cc_taCert_A)) {
                    if(not f_readSigningKey(valueof(p_certificateName), v_privateKey)){
                        return false;
                    }
                } else {
filatov's avatar
filatov committed
830
                    if(not f_readSigningKey(cc_taCert_A, v_privateKey)){
831
832
833
                        return false;
                    }
                }
834
                f_getCertificateHash(p_certificateName, v_certificateIssuer);
835

garciay's avatar
garciay committed
836
                if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaNistP256Signature)) {
Yann Garcia's avatar
Yann Garcia committed
837
838
839
840
841
842
843
844
845
846
847
                  v_signature := f_signWithEcdsaNistp256WithSha256(
                                                                   v_secPayload,
                                                                   v_certificateIssuer,
                                                                   v_privateKey 
                                                                   );
                  p_securedMessage.content.signedData.signature_ := valueof(m_signature_ecdsaNistP256(
                                                                                                      m_ecdsaP256Signature(
                                                                                                                           m_eccP256CurvePoint_x_only(substr(v_signature, 0, 32)),
                                                                                                                           substr(v_signature, 32, 32)
                                                                                                                           )
                                                                                                      ));
garciay's avatar
garciay committed
848
849
                } else if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaBrainpoolP256r1Signature)) {
                    v_signature := f_signWithEcdsaBrainpoolp256WithSha256(
Yann Garcia's avatar
Yann Garcia committed
850
851
852
853
                                                                          v_secPayload,
                                                                          v_certificateIssuer, 
                                                                          v_privateKey 
                                                                          );
garciay's avatar
garciay committed
854
                    p_securedMessage.content.signedData.signature_ := valueof(m_signature_ecdsaBrainpoolP256r1(
Yann Garcia's avatar
Yann Garcia committed
855
856
857
858
859
                                                                                                               m_ecdsaP256Signature(
                                                                                                                                    m_eccP256CurvePoint_x_only(substr(v_signature, 0, 32)),
                                                                                                                                    substr(v_signature, 32, 32)
                                                                                                                                    )
                                                                                                               ));
860
                } else if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaBrainpoolP384r1Signature)) {
Yann Garcia's avatar
Yann Garcia committed
861
862
863
864
865
866
867
868
869
870
871
                  v_signature := f_signWithEcdsaBrainpoolp384WithSha384(
                                                                        v_secPayload,
                                                                        v_certificateIssuer, 
                                                                        v_privateKey 
                                                                        );
                  p_securedMessage.content.signedData.signature_ := valueof(m_signature_ecdsaBrainpoolP384r1(
                                                                                                             m_ecdsaP384Signature(
                                                                                                                                  m_eccP384CurvePoint_x_only(substr(v_signature, 0, 32)),
                                                                                                                                  substr(v_signature, 32, 32)
                                                                                                                                  )
                                                                                                             ));
garciay's avatar
garciay committed
872
                } // TODO To be continued
873
                
Yann Garcia's avatar
Yann Garcia committed
874
                log("<<< f_buildGnSecuredMessage: p_securedMessage=", p_securedMessage);
garciay's avatar
garciay committed
875
                return true;
876
877
878
879
880
881
882
883
884
885
886
887
888
            } // End of function f_buildGnSecuredMessage
            
            /**
             * @desc  This function build and sign the SecureMessage part covered by the signature process including wrong elements of protocols. It is used for BO test cases
             * @param p_securedMessage      The signed  SecureMessage part
             * @param p_certificateName     The certificate name
             * @param  p_protocolVersion    The protocol version to be set. Default: 2
             * @param  p_trailerStatus      The Traile behaviour:
             *                              <li>0 for no trailer</li>
             *                              <li>1 for invalid trailer</li>
             *                              <li>2 for duplicated trailer</li>
             * @param p_payloadField        Payloads to be included in the message
             * @param p_mandatoryHeaders    Mandatory headers for the selected profile 
garciay's avatar
garciay committed
889
             * @param p_headerInfo        HeaderInfo to be inserted in the message
890
891
892
893
             * @param p_securityProfile     Selected security profile
             * @return true on success, false otherwise
             */
            function f_buildGnSecuredMessage_Bo(
garciay's avatar
garciay committed
894
                                                inout EtsiTs103097Data p_securedMessage,
895
                                                in template (value) charstring p_certificateName,
garciay's avatar
garciay committed
896
                                                in UInt8 p_protocolVersion := c_protocol_version,
897
                                                in integer p_trailerStatus := 0,
garciay's avatar
garciay committed
898
                                                in template (value) ToBeSignedData p_payloadField,
garciay's avatar
garciay committed
899
900
                                                in template (value) HeaderInfo p_mandatoryHeaders,
                                                in template (omit) HeaderInfo p_headerInfo := omit
901
            ) return boolean {
902
903
904
                
                // Local variables
                var octetstring v_secPayload, v_signature;
garciay's avatar
garciay committed
905
                var template (value) ToBeSignedData v_toBeSignedData;
906
                var integer i, j, k, n;
garciay's avatar
garciay committed
907
908
                var HeaderInfo v_headerFields := {};
                var Ieee1609Dot2Content v_toBeSignedPayload;
909
910
911
912
                var Oct32 v_privateKey;
                var UInt8 v_trailerSize;
                
                // Prepare headers
garciay's avatar
garciay committed
913
                if (not(ispresent(p_headerInfo))) {
914
                    v_headerFields := valueof(p_mandatoryHeaders);
garciay's avatar
garciay committed
915
916
                } else {/* FIXME To be reviewed 
                    // Merge p_headerInfo and v_mandatoryHeaders into v_headerFields
917
                        
garciay's avatar
garciay committed
918
                    i := 0; // index for p_headerInfo
919
920
921
922
                    j := 0; // index for v_mandatoryHeaders
                    k := 0; // index for v_headerFields
                        
                    // Special processing for signer_info
garciay's avatar
garciay committed
923
924
                    if (lengthof(valueof(p_headerInfo)) > 0 and valueof(p_headerInfo[i].type_) == e_signer_info) {
                        v_headerFields[k] := valueof(p_headerInfo[i]);
925
926
927
928
929
930
                        k := k + 1;
                        i := i + 1;
                    }
                    
                    for (j := j; j < lengthof(p_mandatoryHeaders); j := j + 1) {
                        // Search for mandatory header in p_HeaderFields
garciay's avatar
garciay committed
931
932
                        for (n := 0; n < lengthof(p_headerInfo); n := n + 1) {
                            if (valueof(p_headerInfo[n].type_) == valueof(p_mandatoryHeaders[j].type_)) {