From e741b91727e774f2c03a160b29a938bf5b00580f Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Tue, 5 Oct 2010 17:14:08 -0400
Subject: [PATCH] [#4243] Init init local crypto context in when initializing
 audiortp

---
 .../src/audio/audiortp/AudioRtpFactory.cpp    |  9 ++++
 .../src/audio/audiortp/AudioRtpFactory.h      |  2 +
 .../src/audio/audiortp/AudioSrtpSession.cpp   | 12 ++---
 .../src/audio/audiortp/AudioSrtpSession.h     |  4 +-
 sflphone-common/src/managerimpl.cpp           |  2 +-
 sflphone-common/src/sip/SdesNegotiator.cpp    |  5 +-
 sflphone-common/src/sip/SdesNegotiator.h      | 12 +++++
 sflphone-common/test/sdesnegotiatortest.cpp   | 52 ++++++++++++++++---
 sflphone-common/test/sdesnegotiatortest.h     |  5 +-
 9 files changed, 85 insertions(+), 18 deletions(-)

diff --git a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
index eabf054b16..71ed7952af 100644
--- a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
@@ -136,6 +136,8 @@ void AudioRtpFactory::initAudioRtpSession (SIPCall * ca)
                 _rtpSession = new AudioSrtpSession (&Manager::instance(), ca);
                 _rtpSessionType = Sdes;
 
+                static_cast<AudioSrtpSession *> (_rtpSession)->initLocalCryptoInfo ();
+
                 ca->getLocalSDP()->set_srtp_crypto (static_cast<AudioSrtpSession *> (_rtpSession)->getLocalCryptoInfo());
                 break;
 
@@ -260,6 +262,13 @@ sfl::AudioZrtpSession * AudioRtpFactory::getAudioZrtpSession()
     }
 }
 
+void sfl::AudioRtpFactory::initLocalCryptoInfo ()
+{
+    if (_rtpSession && _rtpSessionType && (_rtpSessionType == Sdes)) {
+        static_cast<AudioSrtpSession *> (_rtpSession)->initLocalCryptoInfo ();
+    }
+}
+
 void AudioRtpFactory::setRemoteCryptoInfo (sfl::SdesNegotiator& nego)
 {
     if (_rtpSession && _rtpSessionType && (_rtpSessionType == Sdes)) {
diff --git a/sflphone-common/src/audio/audiortp/AudioRtpFactory.h b/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
index b870333fb0..263e9fee10 100644
--- a/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
+++ b/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
@@ -166,6 +166,8 @@ class AudioRtpFactory
          */
         sfl::AudioZrtpSession * getAudioZrtpSession();
 
+        void initLocalCryptoInfo (void);
+
         /**
          * Set remote cryptographic info. Should be called after negotiation in SDP
          * offer/answer session.
diff --git a/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp b/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
index 522164ab34..67000fc550 100644
--- a/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
@@ -51,8 +51,8 @@ namespace sfl
 AudioSrtpSession::AudioSrtpSession (ManagerImpl * manager, SIPCall * sipcall) :
         ost::SymmetricRTPSession (ost::InetHostAddress (sipcall->getLocalIp().c_str()), sipcall->getLocalAudioPort()),
         AudioRtpSession<AudioSrtpSession> (manager, sipcall),
-        _localCryptoSuite (1),
-        _remoteCryptoSuite (1),
+        _localCryptoSuite (0),
+        _remoteCryptoSuite (0),
         _localMasterKeyLength (0),
         _localMasterSaltLength (0),
         _remoteMasterKeyLength (0),
@@ -120,8 +120,8 @@ void AudioSrtpSession::setRemoteCryptoInfo (sfl::SdesNegotiator& nego)
 
     // Use second crypto suite if key length is 32 bit, default is 80;
 
-    if (nego.getMkiLength() == "32") {
-        _debug ("AudioSrtp: Using %s byte key length", nego.getMkiLength().c_str());
+    if (nego.getAuthTagLength() == "32") {
+        _debug ("AudioSrtp: Using %s byte authentication tag length", nego.getAuthTagLength().c_str());
         _localCryptoSuite = 1;
         _remoteCryptoSuite = 1;
     }
@@ -251,7 +251,7 @@ void AudioSrtpSession::initializeRemoteCryptoContext (void)
             _remoteMasterSaltLength,
             crypto.encryptionKeyLength / 8,
             crypto.srtpAuthKeyLength / 8,
-            112 / 8,                         // session salt len
+            crypto.masterSaltLength / 8,                         // session salt len
             crypto.srtpAuthTagLength / 8);
 
 }
@@ -273,7 +273,7 @@ void AudioSrtpSession::initializeLocalCryptoContext (void)
             _localMasterSaltLength,
             crypto.encryptionKeyLength / 8,
             crypto.srtpAuthKeyLength / 8,
-            112 / 8,                         // session salt len
+            crypto.masterSaltLength / 8,                         // session salt len
             crypto.srtpAuthTagLength / 8);
 
 }
diff --git a/sflphone-common/src/audio/audiortp/AudioSrtpSession.h b/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
index b461a5a132..34e680f343 100644
--- a/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
+++ b/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
@@ -84,10 +84,10 @@ class AudioSrtpSession : public ost::SymmetricRTPSession, public AudioRtpSession
 
         void setRemoteCryptoInfo (sfl::SdesNegotiator& nego);
 
-    private:
-
         void initLocalCryptoInfo (void);
 
+    private:
+
         void initializeLocalMasterKey (void);
 
         void initializeLocalMasterSalt (void);
diff --git a/sflphone-common/src/managerimpl.cpp b/sflphone-common/src/managerimpl.cpp
index b453b4866d..8c165cdb47 100644
--- a/sflphone-common/src/managerimpl.cpp
+++ b/sflphone-common/src/managerimpl.cpp
@@ -3942,7 +3942,7 @@ ManagerImpl::getAccount (const AccountID& accountID)
         return iter->second;
     }
 
-    _debug ("Manager: Did not found account %s, returning IP2IP account");
+    _debug ("Manager: Did not found account %s, returning IP2IP account", accountID.c_str());
     return _directIpAccount;
 }
 
diff --git a/sflphone-common/src/sip/SdesNegotiator.cpp b/sflphone-common/src/sip/SdesNegotiator.cpp
index cce934716c..9e85492b49 100644
--- a/sflphone-common/src/sip/SdesNegotiator.cpp
+++ b/sflphone-common/src/sip/SdesNegotiator.cpp
@@ -248,7 +248,7 @@ bool SdesNegotiator::negotiate (void)
                     // _mkiValue = (*iter_offer)->getMkiValue();
                     // _mkiLength = (*iter_offer)->getMkiLength();
 
-                    _mkiLength = _cryptoSuite.substr (_cryptoSuite.size()-2, 2);
+                    _authTagLength = _cryptoSuite.substr (_cryptoSuite.size()-2, 2);
 
                     std::cout << "Negotiate tag: " + (*iter_offer)->getTag() << std::endl;
                     std::cout << "Crypto Suite: " + _cryptoSuite << std::endl;
@@ -256,7 +256,8 @@ bool SdesNegotiator::negotiate (void)
                     std::cout << "SRTP Key Info: " + _srtpKeyInfo << std::endl;
                     // std::cout << "Lifetime: " + _lifetime << std::endl;
                     // std::cout << "MKI Value: " + _mkiValue << std::endl;
-                    std::cout << "MKI Length: " + _mkiLength << std::endl;
+                    // std::cout << "MKI Length: " + _mkiLength << std::endl;
+                    std::cout << "Auth tag length: " + _authTagLength << std::endl;
                 }
 
                 iter_local++;
diff --git a/sflphone-common/src/sip/SdesNegotiator.h b/sflphone-common/src/sip/SdesNegotiator.h
index dc78fa1132..f62a597e9d 100644
--- a/sflphone-common/src/sip/SdesNegotiator.h
+++ b/sflphone-common/src/sip/SdesNegotiator.h
@@ -202,6 +202,13 @@ class SdesNegotiator
             return _mkiLength;
         }
 
+        /**
+        * Authentication tag lenth
+        */
+        std::string getAuthTagLength (void) {
+            return _authTagLength;
+        }
+
 
     private:
         /**
@@ -243,6 +250,11 @@ class SdesNegotiator
          */
         std::string _mkiLength;
 
+        /**
+         * Authenticvation tag length in byte
+         */
+        std::string _authTagLength;
+
         std::vector<CryptoAttribute *> parse (void);
 };
 }
diff --git a/sflphone-common/test/sdesnegotiatortest.cpp b/sflphone-common/test/sdesnegotiatortest.cpp
index fd1f630fe7..df6b3675d2 100644
--- a/sflphone-common/test/sdesnegotiatortest.cpp
+++ b/sflphone-common/test/sdesnegotiatortest.cpp
@@ -222,12 +222,14 @@ void SdesNegotiatorTest::testMostSimpleCase()
 
     CPPUNIT_ASSERT (negotiator->negotiate() == true);
 
-    CPPUNIT_ASSERT (negotiator->getCryptoSuite().compare ("AES_CM_128_HMAC_SHA1_80") == 0);
-    CPPUNIT_ASSERT (negotiator->getKeyMethod().compare ("inline") == 0);
-    CPPUNIT_ASSERT (negotiator->getKeyInfo().compare ("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd") == 0);
-    CPPUNIT_ASSERT (negotiator->getLifeTime().compare ("") == 0);
-    CPPUNIT_ASSERT (negotiator->getMkiValue().compare ("") == 0);
-    CPPUNIT_ASSERT (negotiator->getMkiLength().compare ("") == 0);
+    CPPUNIT_ASSERT (negotiator->getCryptoSuite() == "AES_CM_128_HMAC_SHA1_80");
+    CPPUNIT_ASSERT (negotiator->getKeyMethod() == "inline");
+    CPPUNIT_ASSERT (negotiator->getKeyInfo() == "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd");
+    CPPUNIT_ASSERT (negotiator->getLifeTime() == "");
+    CPPUNIT_ASSERT (negotiator->getMkiValue() == "");
+    CPPUNIT_ASSERT (negotiator->getMkiLength() == "");
+    CPPUNIT_ASSERT (negotiator->getAuthTagLength() == "80");
+
 
     delete capabilities;
     capabilities = NULL;
@@ -236,3 +238,41 @@ void SdesNegotiatorTest::testMostSimpleCase()
     delete negotiator;
     negotiator = NULL;
 }
+
+
+void SdesNegotiatorTest::test32ByteKeyLength()
+{
+    _debug ("-------------------- SdesNegotiatorTest::test32ByteKeyLength --------------------\n");
+
+    // Register the local capabilities.
+    std::vector<sfl::CryptoSuiteDefinition> * capabilities = new std::vector<sfl::CryptoSuiteDefinition>();
+
+    //Support all the CryptoSuites
+    for (int i = 0; i < 3; i++) {
+        capabilities->push_back (sfl::CryptoSuites[i]);
+    }
+
+    std::string cryptoLine ("a=crypto:1 AES_CM_128_HMAC_SHA1_32 inline:AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd");
+    std::vector<std::string> * cryptoOffer = new std::vector<std::string>();
+    cryptoOffer->push_back (cryptoLine);
+
+    sfl::SdesNegotiator * negotiator = new sfl::SdesNegotiator (*capabilities, *cryptoOffer);
+
+    CPPUNIT_ASSERT (negotiator->negotiate() == true);
+
+    CPPUNIT_ASSERT (negotiator->getCryptoSuite() == "AES_CM_128_HMAC_SHA1_32");
+    CPPUNIT_ASSERT (negotiator->getKeyMethod() == "inline");
+    CPPUNIT_ASSERT (negotiator->getKeyInfo() == "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwd");
+    CPPUNIT_ASSERT (negotiator->getLifeTime() == "");
+    CPPUNIT_ASSERT (negotiator->getMkiValue() == "");
+    CPPUNIT_ASSERT (negotiator->getMkiLength() == "");
+    CPPUNIT_ASSERT (negotiator->getAuthTagLength() == "32");
+
+    delete capabilities;
+    capabilities = NULL;
+    delete cryptoOffer;
+    cryptoOffer = NULL;
+    delete negotiator;
+    negotiator = NULL;
+}
+
diff --git a/sflphone-common/test/sdesnegotiatortest.h b/sflphone-common/test/sdesnegotiatortest.h
index ee5cb33964..eee9de5b35 100644
--- a/sflphone-common/test/sdesnegotiatortest.h
+++ b/sflphone-common/test/sdesnegotiatortest.h
@@ -76,6 +76,7 @@ class SdesNegotiatorTest : public CppUnit::TestCase {
     CPPUNIT_TEST( testKeyParamsPatternWithoutMKI );
     CPPUNIT_TEST( testNegotiation );
     CPPUNIT_TEST( testMostSimpleCase );
+    CPPUNIT_TEST( test32ByteKeyLength );
     CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -90,7 +91,7 @@ class SdesNegotiatorTest : public CppUnit::TestCase {
 
 		void testKeyParamsPattern();
 		
-        void testKeyParamsPatternCiscoStyle();
+                void testKeyParamsPatternCiscoStyle();
 
 		void testKeyParamsPatternWithoutMKI();
 
@@ -100,6 +101,8 @@ class SdesNegotiatorTest : public CppUnit::TestCase {
 
 		void testMostSimpleCase();
 
+                void test32ByteKeyLength();
+
     private:
 
 		sfl::Pattern *pattern;
-- 
GitLab