Commit 1966f90e authored by garciay's avatar garciay
Browse files

Major security bugs fixed for signature. Encryption to do.

parent 69b69ba3
Loading
Loading
Loading
Loading
+42 −12
Changes for ttcn/AtsGenCert/ItsGenCert_Functions.ttcn: 42 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -34,9 +34,9 @@ module ItsGenCert_Functions {
    
    if (p_certificate_params.encryption_key == true) {
      if (p_certificate_params.encryption_curve == e_nist_p256) {
        f_generate_key_pair_nistp256(p_certificate_details.private_enc_key, p_certificate_details.public_enc_key_x, p_certificate_details.public_enc_key_y, p_certificate_details.public_key_compressed, p_certificate_details.public_key_compressed_mode);
        f_generate_key_pair_nistp256(p_certificate_details.private_enc_key, p_certificate_details.public_enc_key_x, p_certificate_details.public_enc_key_y, p_certificate_details.public_enc_key_compressed, p_certificate_details.public_enc_key_compressed_mode);
      } else if (p_certificate_params.encryption_curve == e_brainpool_p256) {
        f_generate_key_pair_brainpoolp256(p_certificate_details.private_enc_key, p_certificate_details.public_enc_key_x, p_certificate_details.public_enc_key_y, p_certificate_details.public_key_compressed, p_certificate_details.public_key_compressed_mode);
        f_generate_key_pair_brainpoolp256(p_certificate_details.private_enc_key, p_certificate_details.public_enc_key_x, p_certificate_details.public_enc_key_y, p_certificate_details.public_enc_key_compressed, p_certificate_details.public_enc_key_compressed_mode);
      } else {
        log("f_generate_signing_keys: Unsupported encryption curve");
        return -1;
@@ -115,25 +115,43 @@ module ItsGenCert_Functions {
                                               );
    if (p_certificate_params.encryption_key == true) {
      if (p_certificate_params.encryption_curve == e_nist_p256) {
        if (p_certificate_details.public_enc_key_compressed_mode == 0) {
          p_certificate_details.certificate.toBeSigned.encryptionKey := valueof(m_encryptionKey(
                                                                                                aes128Ccm, 
                                                                                                m_publicEncryptionKey_ecdsaNistP256(
                                                                                                                                  m_eccP256CurvePoint_uncompressed(
                                                                                                                                                                   p_certificate_details.public_enc_key_x,
                                                                                                                                                                   p_certificate_details.public_enc_key_y
                                                                                                                                    m_eccP256CurvePoint_compressed_y_0(
                                                                                                                                                                       p_certificate_details.public_enc_key_compressed
                                                                                                                                                                       )))
                                                                               );
        } else {
          p_certificate_details.certificate.toBeSigned.encryptionKey := valueof(m_encryptionKey(
                                                                                                aes128Ccm, 
                                                                                                m_publicEncryptionKey_ecdsaNistP256(
                                                                                                                                    m_eccP256CurvePoint_compressed_y_1(
                                                                                                                                                                       p_certificate_details.public_enc_key_compressed
                                                                                                                                                                       )))
                                                                             );
        }
      } else if (p_certificate_params.encryption_curve == e_brainpool_p256) {
        if (p_certificate_details.public_enc_key_compressed_mode == 0) {
          p_certificate_details.certificate.toBeSigned.encryptionKey := valueof(m_encryptionKey(
                                                                                                aes128Ccm, 
                                                                                                m_publicEncryptionKey_eciesBrainpoolP256r1(
                                                                                                                                         m_eccP256CurvePoint_uncompressed(
                                                                                                                                                                          p_certificate_details.public_enc_key_x,
                                                                                                                                                                          p_certificate_details.public_enc_key_y
                                                                                                                                           m_eccP256CurvePoint_compressed_y_0(
                                                                                                                                                                              p_certificate_details.public_enc_key_compressed
                                                                                                                                                                              )))
                                                                               );
        } else {
          p_certificate_details.certificate.toBeSigned.encryptionKey := valueof(m_encryptionKey(
                                                                                                aes128Ccm, 
                                                                                                m_publicEncryptionKey_eciesBrainpoolP256r1(
                                                                                                                                           m_eccP256CurvePoint_compressed_y_1(
                                                                                                                                                                              p_certificate_details.public_enc_key_compressed
                                                                                                                                                                              )))
                                                                               );
        }
      }
    }
    
    return 0;
  } // End of function f_fill_certificate
@@ -144,17 +162,19 @@ module ItsGenCert_Functions {
                                  inout certificate_details p_certificate_details
                                  ) return integer {
    var bitstring v_enc_msg;
    var HashedId8 v_issuer := '0000000000000000'O;
    var octetstring v_issuer;
    var octetstring v_signature;
        
    // Encode it ==> Get octetstring
    v_enc_msg := encvalue(p_certificate_details.certificate.toBeSigned);
    if (p_issuer_certificate_details.issuer != p_issuer_certificate_details.hashid8) { // This is not a CA certificate
      v_issuer := p_issuer_certificate_details.issuer;
    }
    
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    if (p_certificate_params.curve == e_nist_p256) {
      if (p_issuer_certificate_details.issuer != p_issuer_certificate_details.hashid8) { // This is not a CA certificate
        v_issuer := f_hashWithSha256(p_issuer_certificate_details.enc_cert);
      } else {
          v_issuer := int2oct(0, 32);
      }
      v_signature := f_signWithEcdsaNistp256WithSha256(bit2oct(v_enc_msg), v_issuer, p_issuer_certificate_details.private_key);
      if (lengthof(v_signature) != 64) {
        setverdict(fail, "Wrong signature size, shall be 64 instead of ", lengthof(v_signature));
@@ -170,6 +190,11 @@ module ItsGenCert_Functions {
                                                                                                             )
                                                                                        ));
    } else if (p_certificate_params.curve == e_brainpool_p256) {
      if (p_issuer_certificate_details.issuer != p_issuer_certificate_details.hashid8) { // This is not a CA certificate
        v_issuer := f_hashWithSha256(p_issuer_certificate_details.enc_cert);
      } else {
          v_issuer := int2oct(0, 32);
      }
      v_signature := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_enc_msg), v_issuer, p_issuer_certificate_details.private_key);
      if (lengthof(v_signature) != 64) {
        setverdict(fail, "Wrong signature size, shall be 64 instead of ", lengthof(v_signature));
@@ -185,6 +210,11 @@ module ItsGenCert_Functions {
                                                                                                                    )
                                                                                               ));
    } else if (p_certificate_params.curve == e_brainpool_p384) {
      if (p_issuer_certificate_details.issuer != p_issuer_certificate_details.hashid8) { // This is not a CA certificate
        v_issuer := f_hashWithSha384(p_issuer_certificate_details.enc_cert);
      } else {
          v_issuer := int2oct(0, 48);
      }
      v_signature := f_signWithEcdsaBrainpoolp384WithSha384(bit2oct(v_enc_msg), v_issuer, p_issuer_certificate_details.private_key);
      if (lengthof(v_signature) != 96) {
        setverdict(fail, "Wrong signature size, shall be 96 instead of ", lengthof(v_signature));
+1 −1
Changes for ttcn/AtsGenCert/ItsGenCert_TestCases.ttcn: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ module ItsGencert_TestCases {
      
      // Setup
      v_certificate_params := PICS_CERTFICATES[v_counter];
      v_details := { v_certificate_params.certificate_id, {}, ''O, ''O, ''O, ''O, ''O, 0, '0000000000000000'O, '0000000000000000'O, omit, omit, omit };
      v_details := { v_certificate_params.certificate_id, {}, ''O, ''O, ''O, ''O, ''O, 0, '0000000000000000'O, '0000000000000000'O, omit, omit, omit, omit, omit };
      
      // Generate Private/Public signing and encryption keys for the certificate
      if (f_generate_signing_keys(v_certificate_params, v_details) == -1) {
+3 −1
Changes for ttcn/AtsGenCert/ItsGenCert_TypeAndValues.ttcn: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -101,7 +101,9 @@ module ItsGenCert_TypeAndValues {
      HashedId8                 issuer,
      octetstring               private_enc_key optional,
      octetstring               public_enc_key_x optional,
      octetstring               public_enc_key_y optional
      octetstring               public_enc_key_y optional,
      octetstring               public_enc_key_compressed optional,
      integer                   public_enc_key_compressed_mode optional
    } // End of type certificate_details
    type record of certificate_details certificate_details_list;
    
+14 −14
Changes for ttcn/TestCodec/TestCodec_Certificates.ttcn: 14 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ module TestCodec_Certificates {
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    // IEEE Std 1609.2-20XX Clause 5.3.1 ii) If the verification type is self-signed, signer identifier input shall be the empty string, i.e., a string of length 0.
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -122,7 +122,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaNistp256WithSha256(
                                            bit2oct(v_encMsg), 
                                            '0000000000000000'O,
                                            int2oct(0, 32),
                                            v_cert_dec.signature_.ecdsaNistP256Signature.rSig.x_only & v_cert_dec.signature_.ecdsaNistP256Signature.sSig,
                                            v_publicKeyCompressed,
                                            v_compressedMode
@@ -193,7 +193,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert.toBeSigned));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -224,7 +224,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                 bit2oct(v_encMsg), 
                                                 '0000000000000000'O,
                                                 int2oct(0, 32),
                                                 v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.rSig.x_only & v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.sSig,
                                                 v_publicKeyCompressed,
                                                 v_compressedMode
@@ -311,7 +311,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert.toBeSigned));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -342,7 +342,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                 bit2oct(v_encMsg), 
                                                 '0000000000000000'O,
                                                 int2oct(0, 32),
                                                 v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.rSig.x_only & v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.sSig,
                                                 v_publicKeyCompressed,
                                                 v_compressedMode
@@ -413,7 +413,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert.toBeSigned));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate
    v_sig := f_signWithEcdsaBrainpoolp384WithSha384(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaBrainpoolp384WithSha384(bit2oct(v_encMsg), int2oct(0, 48), v_private_key);
    if (lengthof(v_sig) != 96) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -444,7 +444,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaBrainpoolp384WithSha384(
                                                 bit2oct(v_encMsg), 
                                                 '0000000000000000'O,
                                                 int2oct(0, 48),
                                                 v_cert_dec.signature_.ecdsaBrainpoolP384r1Signature.rSig.x_only & v_cert_dec.signature_.ecdsaBrainpoolP384r1Signature.sSig,
                                                 v_publicKeyCompressed,
                                                 v_compressedMode
@@ -511,7 +511,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -543,7 +543,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaNistp256WithSha256(
                                            bit2oct(v_encMsg), 
                                            '0000000000000000'O,
                                            int2oct(0, 32),
                                            v_cert_dec.signature_.ecdsaNistP256Signature.rSig.x_only & v_cert_dec.signature_.ecdsaNistP256Signature.sSig,
                                            v_publicKeyCompressed,
                                            v_compressedMode
@@ -609,7 +609,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert.toBeSigned));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (NIST p-256)
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaNistp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -639,7 +639,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaNistp256WithSha256(
                                            bit2oct(v_encMsg), 
                                            '0000000000000000'O,
                                            int2oct(0, 32),
                                            v_cert_dec.signature_.ecdsaNistP256Signature.rSig.x_only & v_cert_dec.signature_.ecdsaNistP256Signature.sSig,
                                            v_publicKeyCompressed,
                                            v_compressedMode
@@ -702,7 +702,7 @@ module TestCodec_Certificates {
    log("Encode template ", valueof(v_cert.toBeSigned));
    v_encMsg := encvalue(v_cert.toBeSigned);
    // Sign the certificate using ECDSA/SHA-256 (BP p-256)
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), '0000000000000000'O, v_private_key);
    v_sig := f_signWithEcdsaBrainpoolp256WithSha256(bit2oct(v_encMsg), int2oct(0, 32), v_private_key);
    if (lengthof(v_sig) != 64) {
      log("Invalid signature: ", v_sig);
      setverdict(fail);
@@ -732,7 +732,7 @@ module TestCodec_Certificates {
    }
    if (f_verifyWithEcdsaBrainpoolp256WithSha256(
                                                 bit2oct(v_encMsg), 
                                                 '0000000000000000'O,
                                                 int2oct(0, 32),
                                                 v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.rSig.x_only & v_cert_dec.signature_.ecdsaBrainpoolP256r1Signature.sSig,
                                                 v_publicKeyCompressed,
                                                 v_compressedMode
+16 −17

File changed.

Preview size limit exceeded, changes collapsed.

Loading