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

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

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

Yann Garcia's avatar
Yann Garcia committed
822
823
                log(">>> f_buildGnSecuredMessage: p_securedMessage=", p_securedMessage);
                log(">>> f_buildGnSecuredMessage: p_payloadField=", p_payloadField);
garciay's avatar
garciay committed
824
                
825
                // Prepare payload to be signed
garciay's avatar
garciay committed
826
                v_toBeSignedData := valueof(p_payloadField);
garciay's avatar
garciay committed
827
                v_secPayload := bit2oct(encvalue(v_toBeSignedData));
828
829
                
                // Signed payload
830
831
832
833
834
                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
835
                    if(not f_readSigningKey(cc_taCert_A, v_privateKey)){
836
837
838
                        return false;
                    }
                }
839
                f_getCertificateHash(p_certificateName, v_certificateIssuer);
840

garciay's avatar
garciay committed
841
                if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaNistP256Signature)) {
Yann Garcia's avatar
Yann Garcia committed
842
843
844
845
846
847
848
849
850
851
852
                  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
853
854
                } else if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaBrainpoolP256r1Signature)) {
                    v_signature := f_signWithEcdsaBrainpoolp256WithSha256(
Yann Garcia's avatar
Yann Garcia committed
855
856
857
858
                                                                          v_secPayload,
                                                                          v_certificateIssuer, 
                                                                          v_privateKey 
                                                                          );
garciay's avatar
garciay committed
859
                    p_securedMessage.content.signedData.signature_ := valueof(m_signature_ecdsaBrainpoolP256r1(
Yann Garcia's avatar
Yann Garcia committed
860
861
862
863
864
                                                                                                               m_ecdsaP256Signature(
                                                                                                                                    m_eccP256CurvePoint_x_only(substr(v_signature, 0, 32)),
                                                                                                                                    substr(v_signature, 32, 32)
                                                                                                                                    )
                                                                                                               ));
865
                } else if (ischosen(p_securedMessage.content.signedData.signature_.ecdsaBrainpoolP384r1Signature)) {
Yann Garcia's avatar
Yann Garcia committed
866
867
868
869
870
871
872
                  v_signature := f_signWithEcdsaBrainpoolp384WithSha384(
                                                                        v_secPayload,
                                                                        v_certificateIssuer, 
                                                                        v_privateKey 
                                                                        );
                  p_securedMessage.content.signedData.signature_ := valueof(m_signature_ecdsaBrainpoolP384r1(
                                                                                                             m_ecdsaP384Signature(
YannGarcia's avatar
YannGarcia committed
873
874
                                                                                                                                  m_eccP384CurvePoint_x_only(substr(v_signature, 0, 48)),
                                                                                                                                  substr(v_signature, 48, 48)
Yann Garcia's avatar
Yann Garcia committed
875
876
                                                                                                                                  )
                                                                                                             ));
garciay's avatar
garciay committed
877
                } // TODO To be continued
878
                
Yann Garcia's avatar
Yann Garcia committed
879
                log("<<< f_buildGnSecuredMessage: p_securedMessage=", p_securedMessage);
garciay's avatar
garciay committed
880
                return true;
881
882
883
884
885
886
887
888
889
890
891
892
893
            } // 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
894
             * @param p_headerInfo        HeaderInfo to be inserted in the message
895
896
897
898
             * @param p_securityProfile     Selected security profile
             * @return true on success, false otherwise
             */
            function f_buildGnSecuredMessage_Bo(
garciay's avatar
garciay committed
899
                                                inout EtsiTs103097Data p_securedMessage,
900
                                                in template (value) charstring p_certificateName,
garciay's avatar
garciay committed
901
                                                in UInt8 p_protocolVersion := c_protocol_version,
902
                                                in integer p_trailerStatus := 0,
garciay's avatar
garciay committed
903
                                                in template (value) ToBeSignedData p_payloadField,
garciay's avatar
garciay committed
904
905
                                                in template (value) HeaderInfo p_mandatoryHeaders,
                                                in template (omit) HeaderInfo p_headerInfo := omit
906
            ) return boolean {
907
908
909
                
                // Local variables
                var octetstring v_secPayload, v_signature;
garciay's avatar
garciay committed
910
                var template (value) ToBeSignedData v_toBeSignedData;
911
                var integer i, j, k, n;
YannGarcia's avatar
YannGarcia committed
912
                var HeaderInfo v_headerFields;
garciay's avatar
garciay committed
913
                var Ieee1609Dot2Content v_toBeSignedPayload;
914
915
916
917
                var Oct32 v_privateKey;
                var UInt8 v_trailerSize;
                
                // Prepare headers
garciay's avatar
garciay committed
918
                if (not(ispresent(p_headerInfo))) {
919
                    v_headerFields := valueof(p_mandatoryHeaders);
garciay's avatar
garciay committed
920
921
                } else {/* FIXME To be reviewed 
                    // Merge p_headerInfo and v_mandatoryHeaders into v_headerFields
922