From 97e0eadad2d76a4ce09397a25cd274f69150b07b Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandresavard@alexandresavard-desktop.(none)>
Date: Mon, 11 Jan 2010 09:47:25 -0500
Subject: [PATCH] [#1722] added base64 encode/decode methods for Srtp master
 key

---
 .../src/audio/audiortp/AudioRtpFactory.cpp    |  2 +
 .../src/audio/audiortp/AudioSrtpSession.cpp   | 61 ++++++++++++++++++-
 .../src/audio/audiortp/AudioSrtpSession.h     | 13 +++-
 sflphone-common/src/sip/sdp.cpp               |  3 +
 sflphone-common/src/sip/sdp.h                 |  9 ++-
 5 files changed, 83 insertions(+), 5 deletions(-)

diff --git a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
index 69eeac0428..17e9242152 100644
--- a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
@@ -110,6 +110,8 @@ void AudioRtpFactory::initAudioRtpSession (SIPCall * ca)
 	        _rtpSession = new AudioSrtpSession (&Manager::instance(), ca);
                 _rtpSessionType = Sdes;
 
+		// ca->getLocalSDP()->set_srtp_master_key (static_cast<AudioSrtpSession *> (_rtpSession)->getMasterKey());
+
 		break;
 
             default:
diff --git a/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp b/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
index d3d1d83cdb..67a07d1311 100644
--- a/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioSrtpSession.cpp
@@ -21,6 +21,10 @@
 
 #include "sip/sipcall.h"
 
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+
+
 #include <cstdio>
 #include <cstring>
 #include <cerrno>
@@ -31,6 +35,13 @@ static uint8 mk[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 static uint8 ms[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
 	 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d };
 
+
+// static std::string crypto_suite = "AES_CM_128_HMAC_SHA1_32";
+// static std::string application = "srtp";
+// static std::string srtp_key = "inline:16/14/NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj/2^20/1:32";
+
+
+
 namespace sfl
 {
 
@@ -50,6 +61,14 @@ AudioSrtpSession::AudioSrtpSession (ManagerImpl * manager, SIPCall * sipcall) :
     setOutQueueCryptoContext(outputCryptoCtx);
 }
 
+  /*
+std::string AudioSrtpSession::getCryptoInfo() {
+
+
+    return ;
+}
+  */
+
 void AudioSrtpSession::initializeMasterKey(void)
 {
 
@@ -88,7 +107,7 @@ void AudioSrtpSession::initializeInputCryptoContext(void)
 					  112 / 8,                     // session salt len
 					  80 / 8);                     // authentication tag len
 
-    _debug("********************* Crypto Context IN with SSRC %i ******************", IncomingDataQueue::getLocalSSRCNetwork());
+    
 }
 
 void AudioSrtpSession::initializeOutputCryptoContext(void)
@@ -110,6 +129,44 @@ void AudioSrtpSession::initializeOutputCryptoContext(void)
 					     112 / 8,                     // session salt len
 					     80 / 8);                     // authentication tag len
 
-    _debug("********************* Crypto Context OUT with SSRC %i ******************", OutgoingDataQueue::getLocalSSRC());
+
+}
+
+
+char* AudioSrtpSession::encodeBase64(unsigned char *input, int length)
+{
+    BIO *b64, *bmem;
+
+    char *buffer = (char *)malloc(length);
+    memset(buffer, 0, length);
+
+    b64 = BIO_new(BIO_f_base64());
+    bmem = BIO_new_mem_buf(input, length);
+    bmem = BIO_push(bmem, b64);
+
+    BIO_read(bmem, buffer, length);
+
+    BIO_free_all(bmem);
+
+    return buffer;
+}
+
+char* AudioSrtpSession::decodeBase64(unsigned char *input, int length)
+{
+    BIO *b64, *bmem;
+
+    char *buffer = (char *)malloc(length);
+    memset(buffer, 0, length);
+  
+    b64 = BIO_new(BIO_f_base64());
+    bmem = BIO_new_mem_buf(input, length);
+    bmem = BIO_push(b64, bmem);
+
+    BIO_read(bmem, buffer, length);
+
+    BIO_free_all(bmem);
+
+    return buffer;
 }
+
 }
diff --git a/sflphone-common/src/audio/audiortp/AudioSrtpSession.h b/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
index c37a898a1f..89bc2450ed 100644
--- a/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
+++ b/sflphone-common/src/audio/audiortp/AudioSrtpSession.h
@@ -39,8 +39,13 @@ namespace sfl {
     class AudioSrtpSession : public ost::SymmetricRTPSession, public AudioRtpSession<AudioSrtpSession> 
     {
         public:
-        AudioSrtpSession(ManagerImpl * manager, SIPCall * sipcall);          
-            
+
+            AudioSrtpSession(ManagerImpl * manager, SIPCall * sipcall);
+
+	    // std::string getCryptoInfo(void);
+
+	    uint8* getMasterKey(void){ return _masterKey; }
+
         private:
 
             void initializeMasterKey(void);
@@ -51,6 +56,10 @@ namespace sfl {
 
 	    void initializeOutputCryptoContext(void);
 
+	    char* encodeBase64(unsigned char *input, int length);
+
+	    char* decodeBase64(unsigned char *input, int length);
+
             uint8 _masterKey[16];
 
 	    uint8 _masterSalt[14];
diff --git a/sflphone-common/src/sip/sdp.cpp b/sflphone-common/src/sip/sdp.cpp
index 545601e96e..524d6f9fc1 100644
--- a/sflphone-common/src/sip/sdp.cpp
+++ b/sflphone-common/src/sip/sdp.cpp
@@ -158,7 +158,10 @@ int Sdp::create_local_offer ()
     //sdp_addAttributes( _pool );
     sdp_add_media_description();
 
+    // if(!_srtp_master_key.empty()) {
+
     sdp_add_sdes_attribute();
+    // }
 
     //toString ();
 
diff --git a/sflphone-common/src/sip/sdp.h b/sflphone-common/src/sip/sdp.h
index 68eaa771d8..9f5fc9ea44 100644
--- a/sflphone-common/src/sip/sdp.h
+++ b/sflphone-common/src/sip/sdp.h
@@ -101,6 +101,11 @@ class Sdp {
          * @param hash The hello hash of a rtp session. (Only audio at the moment)
          */
         inline void set_zrtp_hash(const std::string& hash) { _zrtp_hello_hash = hash; _debug("Zrtp hash set with %s\n", hash.c_str()); }
+
+	/* Set the srtp _master_key
+         * @param mk The Master Key of a srtp session.
+         */
+        inline void set_srtp_master_key(const std::string& mk) { _srtp_master_key = mk; }
         
         /*
          * On building an invite outside a dialog, build the local offer and create the
@@ -246,7 +251,9 @@ class Sdp {
         /** Remote's audio port */
         unsigned int _remote_audio_port;
 
-        std::string _zrtp_hello_hash; 
+        std::string _zrtp_hello_hash;
+
+	std::string _srtp_master_key;
         
         Sdp(const Sdp&); //No Copy Constructor
         Sdp& operator=(const Sdp&); //No Assignment Operator
-- 
GitLab