#13795: Initial commit for sflphone-android

includes: libexpat libyaml libdbus-c++ commoncpp ccrtp
          libdbus (from android-4.0.4 sources)

TODO:
- git ignores "/jni/sflphone", sflphone repo should be cloned.
- sflphone-android only needs daemon directory. Ideally it should be possible
to clone it without cloning the whole sflphone project.
into sfl-android (commit 6a0fa7a "#13961: Fix cipher handling" has been used here)
- add pjsip-android project as a git submodule
- sflphone-android needs pjsip android project. Ideally daemon git repository
should not embed pjsip. Instead pjsip should be clone from official repositories.

Considering this, structure should have three distincts git repos:

sflphone-android/.git
sflphone-android/jni/ccrtp-1.8.0-android
sflphone-android/jni/commoncpp2-1.8.1-android
sflphone-android/jni/dbus
sflphone-android/jni/libdbus-c++-0.9.0-android
sflphone-android/jni/libexpat
sflphone-android/jni/libyaml

sflphone-android/jni/sflphone-daemon/.git
sflphone-android/jni/sflphone-daemon/src/audio
sflphone-android/jni/sflphone-daemon/src/config
sflphone-android/jni/sflphone-daemon/src/dbus
sflphone-android/jni/sflphone-daemon/src/history
sflphone-android/jni/sflphone-daemon/src/hooks
sflphone-android/jni/sflphone-daemon/src/iax
sflphone-android/jni/sflphone-daemon/src/sip
sflphone-android/jni/sflphone-daemon/src/video

sflphone-android/jni/pjsip-android/.git

Signed-off-by: Emeric Vigier <emeric.vigier@savoirfairelinux.com>
diff --git a/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/AesSrtp.cxx b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/AesSrtp.cxx
new file mode 100644
index 0000000..eb669f8
--- /dev/null
+++ b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/AesSrtp.cxx
@@ -0,0 +1,274 @@
+/*
+  Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+  * In addition, as a special exception, the copyright holders give
+  * permission to link the code of portions of this program with the
+  * OpenSSL library under certain conditions as described in each
+  * individual source file, and distribute linked combinations
+  * including the two.
+  * You must obey the GNU General Public License in all respects
+  * for all of the code used other than OpenSSL.  If you modify
+  * file(s) with this exception, you may extend this exception to your
+  * version of the file(s), but you are not obligated to do so.  If you
+  * do not wish to do so, delete this exception statement from your
+  * version.  If you delete this exception statement from all source
+  * files in the program, then also delete it here.
+  */
+
+/**
+ * @author Erik Eliasson <eliasson@it.kth.se>
+ * @author Johan Bilien <jobi@via.ecp.fr>
+ * @author Werner Dittmann <Werner.Dittmann@t-online.de>
+ */
+
+extern void initializeOpenSSL();
+
+#include <stdlib.h>
+#include <openssl/aes.h>                // the include of openSSL
+#include <ccrtp/crypto/AesSrtp.h>
+#include <string.h>
+#include <stdio.h>
+
+AesSrtp::AesSrtp(int algo):key(NULL), algorithm(algo) {
+    void initializeOpenSSL();
+}
+
+AesSrtp::AesSrtp( uint8* k, int32 keyLength, int algo ):
+    key(NULL), algorithm(algo) {
+
+    void initializeOpenSSL();
+    setNewKey(k, keyLength);
+}
+
+AesSrtp::~AesSrtp() {
+    if (key != NULL)
+	delete[] (uint8*)key;
+}
+
+bool AesSrtp::setNewKey(const uint8* k, int32 keyLength) {
+    // release an existing key before setting a new one
+    if (key != NULL)
+	delete[] (uint8*)key;
+
+    if (!(keyLength == 16 || keyLength == 32)) {
+	return false;
+    }
+    key = new uint8[sizeof( AES_KEY )];
+    memset(key, 0, sizeof(AES_KEY) );
+    AES_set_encrypt_key(k, keyLength*8, (AES_KEY *)key );
+    return true;
+}
+
+
+void AesSrtp::encrypt( const uint8* input, uint8* output ){
+    AES_encrypt(input, output, (AES_KEY *)key);
+}
+
+void AesSrtp::get_ctr_cipher_stream( uint8* output, uint32 length,
+				     uint8* iv ){
+    uint16 ctr;
+    uint16 input;
+
+    unsigned char aes_input[SRTP_BLOCK_SIZE];
+    unsigned char temp[SRTP_BLOCK_SIZE];
+
+    memcpy(aes_input, iv, 14 );
+    iv += 14;
+
+    for( ctr = 0; ctr < length/SRTP_BLOCK_SIZE; ctr++ ){
+	input = ctr;
+	//compute the cipher stream
+	aes_input[14] = (uint8)((input & 0xFF00) >>  8);
+	aes_input[15] = (uint8)((input & 0x00FF));
+
+	AES_encrypt(aes_input, &output[ctr*SRTP_BLOCK_SIZE], (AES_KEY *)key );
+    }
+    if ((length % SRTP_BLOCK_SIZE) > 0) {
+        // Treat the last bytes:
+        input = ctr;
+        aes_input[14] = (uint8)((input & 0xFF00) >>  8);
+        aes_input[15] = (uint8)((input & 0x00FF));
+
+        AES_encrypt(aes_input, temp, (AES_KEY *)key );
+        memcpy( &output[ctr*SRTP_BLOCK_SIZE], temp, length % SRTP_BLOCK_SIZE );
+    }
+}
+
+
+void AesSrtp::ctr_encrypt( const uint8* input, uint32 input_length,
+			   uint8* output, uint8* iv ) {
+
+    if (key == NULL)
+	return;
+
+    uint8* cipher_stream = new uint8[input_length];
+
+    get_ctr_cipher_stream( cipher_stream, input_length, iv );
+
+    for( unsigned int i = 0; i < input_length; i++ ){
+	output[i] = cipher_stream[i] ^ input[i];
+    }
+    delete []cipher_stream;
+}
+
+void AesSrtp::ctr_encrypt( uint8* data, uint32 data_length, uint8* iv ) {
+
+    if (key == NULL)
+	return;
+
+    //unsigned char cipher_stream[data_length];
+    uint8* cipher_stream = new uint8[data_length];
+
+    get_ctr_cipher_stream( cipher_stream, data_length, iv );
+
+    for( uint32 i = 0; i < data_length; i++ ){
+	data[i] ^= cipher_stream[i];
+    }
+    delete[] cipher_stream;
+}
+
+void AesSrtp::f8_encrypt(const uint8* data, uint32 data_length,
+			 uint8* iv, uint8* origKey, int32 keyLen,
+			 uint8* salt, int32 saltLen, AesSrtp* f8Cipher ) {
+
+    f8_encrypt(data, data_length, const_cast<uint8*>(data), iv, origKey, keyLen, salt, saltLen, f8Cipher);
+}
+
+#define MAX_KEYLEN 32
+
+void AesSrtp::f8_encrypt(const uint8* in, uint32 in_length, uint8* out,
+			 uint8* iv, uint8* origKey, int32 keyLen,
+			 uint8* salt, int32 saltLen, AesSrtp* f8Cipher ) {
+
+
+    unsigned char *cp_in, *cp_in1, *cp_out;
+    int i;
+    int offset = 0;
+
+    unsigned char ivAccent[SRTP_BLOCK_SIZE];
+    unsigned char maskedKey[MAX_KEYLEN];
+    unsigned char saltMask[MAX_KEYLEN];
+    unsigned char S[SRTP_BLOCK_SIZE];
+
+    F8_CIPHER_CTX f8ctx;
+
+    if (key == NULL)
+	return;
+
+    if (keyLen > MAX_KEYLEN)
+	return;
+
+    if (saltLen > keyLen)
+	return;
+
+    /*
+     * Get memory for the derived IV (IV')
+     */
+    f8ctx.ivAccent = ivAccent;
+
+    /*
+     * First copy the salt into the mask field, then fill with 0x55 to
+     * get a full key.
+     */
+    memcpy(saltMask, salt, saltLen);
+    memset(saltMask+saltLen, 0x55, keyLen-saltLen);
+
+    /*
+     * XOR the original key with the above created mask to
+     * get the special key.
+     */
+    cp_out = maskedKey;
+    cp_in = origKey;
+    cp_in1 = saltMask;
+    for (i = 0; i < keyLen; i++) {
+        *cp_out++ = *cp_in++ ^ *cp_in1++;
+    }
+    /*
+     * Prepare the a new AES cipher with the special key to compute IV'
+     */
+    f8Cipher->setNewKey(maskedKey, keyLen);
+
+    /*
+     * Use the masked key to encrypt the original IV to produce IV'.
+     *
+     * After computing the IV' we don't need this cipher context anymore, free it.
+     */
+    f8Cipher->encrypt(iv, f8ctx.ivAccent);
+
+    f8ctx.J = 0;                       // initialize the counter
+    f8ctx.S = S;		       // get the key stream buffer
+
+    memset(f8ctx.S, 0, SRTP_BLOCK_SIZE); // initial value for key stream
+
+    while (in_length >= SRTP_BLOCK_SIZE) {
+        processBlock(&f8ctx, in+offset, SRTP_BLOCK_SIZE, out+offset);
+        in_length -= SRTP_BLOCK_SIZE;
+        offset += SRTP_BLOCK_SIZE;
+    }
+    if (in_length > 0) {
+        processBlock(&f8ctx, in+offset, in_length, out+offset);
+    }
+}
+
+int AesSrtp::processBlock(F8_CIPHER_CTX *f8ctx, const uint8* in, int32 length, uint8* out) {
+
+    int i;
+    const uint8 *cp_in;
+    uint8* cp_in1, *cp_out;
+    uint32_t *ui32p;
+
+    /*
+     * XOR the previous key stream with IV'
+     * ( S(-1) xor IV' )
+     */
+    cp_in = f8ctx->ivAccent;
+    cp_out = f8ctx->S;
+    for (i = 0; i < SRTP_BLOCK_SIZE; i++) {
+        *cp_out++ ^= *cp_in++;
+    }
+    /*
+     * Now XOR (S(n-1) xor IV') with the current counter, then increment the counter
+     */
+    ui32p = (uint32_t *)f8ctx->S;
+    ui32p[3] ^= htonl(f8ctx->J);
+    f8ctx->J++;
+    /*
+     * Now compute the new key stream using AES encrypt
+     */
+    AES_encrypt(f8ctx->S, f8ctx->S, (AES_KEY *)key);
+    /*
+     * as the last step XOR the plain text with the key stream to produce
+     * the ciphertext.
+     */
+    cp_out = out;
+    cp_in = in;
+    cp_in1 = f8ctx->S;
+    for (i = 0; i < length; i++) {
+        *cp_out++ = *cp_in++ ^ *cp_in1++;
+    }
+    return length;
+}
+
+
+/** EMACS **
+ * Local variables:
+ * mode: c++
+ * c-default-style: ellemtel
+ * c-basic-offset: 4
+ * End:
+ */
+
diff --git a/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/InitializeOpenSSL.cxx b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/InitializeOpenSSL.cxx
new file mode 100755
index 0000000..f0b3258
--- /dev/null
+++ b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/InitializeOpenSSL.cxx
@@ -0,0 +1,236 @@
+/*
+  Copyright (C) 2006 Werner Dittmann
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2, or (at your option)
+  any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Boston, MA 02111.
+*/
+
+#include <stdio.h>
+#include <openssl/evp.h>
+
+#include <private.h>
+
+#ifdef OPENSSL_SYS_WIN32
+#include <windows.h>
+#endif
+#if defined SOLARIS && !defined HAVE_PTHREAD_H
+#include <synch.h>
+#include <thread.h>
+#endif
+#if defined HAVE_PTHREAD_H && !defined SOLARIS
+#include <pthread.h>
+#endif
+
+static void threadLockSetup(void);
+static void threadLockCleanup(void);
+static void myLockingCallback(int, int, const char *, int);
+
+/**
+ * Implement the locking callback functions for openSSL.
+ *
+ * Unfortunatly we can't use the Commonc++ Mutex here because the
+ * Mutex may use (for some cases) the Commonc++ Thread class. OpenSSL
+ * does not use this Thread class.
+ */
+
+static int initialized = 0;
+
+int initializeOpenSSL ()
+{
+
+    if (initialized) {
+	return 1;
+    }
+    initialized = 1;
+    threadLockSetup();
+    return 1;
+}
+
+int finalizeOpenSSL ()
+{
+	if(!initialized)
+		return 1;
+
+	initialized = 0;
+	threadLockCleanup();
+	return 1;
+}
+
+#ifdef OPENSSL_SYS_WIN32
+
+static HANDLE *lock_cs;
+
+static void threadLockSetup(void) {
+    int i;
+
+    lock_cs=OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	lock_cs[i] = CreateMutex(NULL,FALSE,NULL);
+    }
+
+    CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))myLockingCallback);
+    /* id callback defined */
+}
+
+static void threadLockCleanup(void) {
+    int i;
+
+    CRYPTO_set_locking_callback(NULL);
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	CloseHandle(lock_cs[i]);
+    }
+    OPENSSL_free(lock_cs);
+}
+
+static void myLockingCallback(int mode, int type, const char *file, int line) {
+    if (mode & CRYPTO_LOCK) {
+	WaitForSingleObject(lock_cs[type], INFINITE);
+    }
+    else {
+	ReleaseMutex(lock_cs[type]);
+    }
+}
+
+#endif /* OPENSSL_SYS_WIN32 */
+
+
+#if defined SOLARIS && !defined HAVE_PTHREAD_H
+
+static mutex_t *lock_cs;
+static long *lock_count;
+
+static void threadLockSetup(void) {
+    int i;
+
+    lock_cs = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(mutex_t));
+    lock_count = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	lock_count[i] = 0;
+	/* rwlock_init(&(lock_cs[i]),USYNC_THREAD,NULL); */
+	mutex_init(&(lock_cs[i]), USYNC_THREAD, NULL);
+    }
+    CRYPTO_set_locking_callback((void (*)(int, int ,const char *, int))myLockingCallback);
+}
+
+static void threadLockCleanup(void) {
+    int i;
+
+    CRYPTO_set_locking_callback(NULL);
+
+    fprintf(stderr,"cleanup\n");
+
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	/* rwlock_destroy(&(lock_cs[i])); */
+	mutex_destroy(&(lock_cs[i]));
+	fprintf(stderr,"%8ld:%s\n",lock_count[i],CRYPTO_get_lock_name(i));
+    }
+    OPENSSL_free(lock_cs);
+    OPENSSL_free(lock_count);
+}
+
+static void myLockingCallback(int mode, int type, const char *file, int line)
+{
+#ifdef undef
+    fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
+	    CRYPTO_thread_id(),
+	    (mode&CRYPTO_LOCK)?"l":"u",
+	    (type&CRYPTO_READ)?"r":"w",file,line);
+#endif
+
+    /*
+      if (CRYPTO_LOCK_SSL_CERT == type)
+      fprintf(stderr,"(t,m,f,l) %ld %d %s %d\n",
+      CRYPTO_thread_id(),
+      mode,file,line);
+    */
+    if (mode & CRYPTO_LOCK) {
+	mutex_lock(&(lock_cs[type]));
+	lock_count[type]++;
+    }
+    else {
+	mutex_unlock(&(lock_cs[type]));
+    }
+}
+
+static unsigned long solaris_thread_id(void) {
+    unsigned long ret;
+
+    ret=(unsigned long)thr_self();
+    return(ret);
+}
+#endif /* SOLARIS */
+
+
+#if defined HAVE_PTHREAD_H && !defined SOLARIS
+
+static pthread_mutex_t* lock_cs;
+static long* lock_count;
+
+static void threadLockSetup(void) {
+    int i;
+
+    lock_cs = (pthread_mutex_t*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
+    lock_count = (long*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(long));
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	lock_count[i] = 0;
+	pthread_mutex_init(&(lock_cs[i]),NULL);
+    }
+
+    // CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
+    CRYPTO_set_locking_callback((void (*)(int, int ,const char *, int))myLockingCallback);
+}
+
+static void threadLockCleanup(void)
+{
+    int i;
+
+    CRYPTO_set_locking_callback(NULL);
+    fprintf(stderr,"cleanup\n");
+    for (i = 0; i < CRYPTO_num_locks(); i++) {
+	pthread_mutex_destroy(&(lock_cs[i]));
+	fprintf(stderr,"%8ld:%s\n",lock_count[i],
+		CRYPTO_get_lock_name(i));
+    }
+    OPENSSL_free(lock_cs);
+    OPENSSL_free(lock_count);
+}
+
+static void myLockingCallback(int mode, int type, const char *file,
+			      int line) {
+#ifdef undef
+    fprintf(stderr,"thread=%4d mode=%s lock=%s %s:%d\n",
+	    CRYPTO_thread_id(),
+	    (mode&CRYPTO_LOCK)?"l":"u",
+	    (type&CRYPTO_READ)?"r":"w",file,line);
+#endif
+    if (mode & CRYPTO_LOCK) {
+	pthread_mutex_lock(&(lock_cs[type]));
+	lock_count[type]++;
+    }
+    else {
+	pthread_mutex_unlock(&(lock_cs[type]));
+    }
+}
+
+/*
+static unsigned long pthreads_thread_id(void)
+{
+    unsigned long ret;
+
+    ret = (unsigned long)pthread_self();
+    return(ret);
+}
+*/
+
+#endif /* LIBPTHREAD && !SOLARIS */
diff --git a/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/hmac.cpp b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/hmac.cpp
new file mode 100644
index 0000000..4edf375
--- /dev/null
+++ b/jni/ccrtp-1.8.0-android/src/ccrtp/crypto/openssl/hmac.cpp
@@ -0,0 +1,106 @@
+/*
+  Copyright (C) 2005, 2004, 2010, Erik Eliasson, Johan Bilien, Werner Dittmann
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+
+/*
+ * Authors: Erik Eliasson <eliasson@it.kth.se>
+ *          Johan Bilien <jobi@via.ecp.fr>
+ *          Werner Dittmann
+ */
+
+#include <stdint.h>
+#include <openssl/hmac.h>
+#include <ccrtp/crypto/hmac.h>
+
+void hmac_sha1(uint8_t * key, int32_t key_length,
+               const uint8_t* data, uint32_t data_length,
+               uint8_t* mac, int32_t* mac_length )
+{
+    HMAC(EVP_sha1(), key, key_length,
+         data, data_length, mac,
+         reinterpret_cast<uint32_t*>(mac_length) );
+}
+
+void hmac_sha1( uint8_t* key, int32_t key_length,
+                const uint8_t* data_chunks[],
+                uint32_t data_chunck_length[],
+                uint8_t* mac, int32_t* mac_length ) {
+    HMAC_CTX ctx;
+    HMAC_CTX_init( &ctx );
+    HMAC_Init_ex( &ctx, key, key_length, EVP_sha1(), NULL );
+    while ( *data_chunks ) {
+        HMAC_Update( &ctx, *data_chunks, *data_chunck_length );
+        data_chunks ++;
+        data_chunck_length ++;
+    }
+    HMAC_Final( &ctx, mac, reinterpret_cast<uint32_t*>(mac_length) );
+    HMAC_CTX_cleanup( &ctx );
+}
+
+void* createSha1HmacContext(uint8_t* key, int32_t key_length)
+{
+    HMAC_CTX* ctx = (HMAC_CTX*)malloc(sizeof(HMAC_CTX));
+    
+    HMAC_CTX_init(ctx);
+    HMAC_Init_ex(ctx, key, key_length, EVP_sha1(), NULL);
+    return ctx;
+}
+
+void hmacSha1Ctx(void* ctx, const uint8_t* data, uint32_t data_length,
+                uint8_t* mac, int32_t* mac_length)
+{
+    HMAC_CTX* pctx = (HMAC_CTX*)ctx;
+    
+    HMAC_Init_ex(pctx, NULL, 0, NULL, NULL );
+    HMAC_Update(pctx, data, data_length );
+    HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) );
+}
+
+void hmacSha1Ctx(void* ctx, const uint8_t* data[], uint32_t data_length[],
+                uint8_t* mac, int32_t* mac_length )
+{
+    HMAC_CTX* pctx = (HMAC_CTX*)ctx;
+    
+    HMAC_Init_ex(pctx, NULL, 0, NULL, NULL );
+    while (*data) {
+        HMAC_Update(pctx, *data, *data_length);
+        data++;
+        data_length++;
+    }
+    HMAC_Final(pctx, mac, reinterpret_cast<uint32_t*>(mac_length) );
+}
+
+void freeSha1HmacContext(void* ctx)
+{
+    if (ctx) {
+        HMAC_CTX_cleanup((HMAC_CTX*)ctx);
+        free(ctx);
+    }
+}
\ No newline at end of file