Commit [a50f98] Maximize Restore History

New structure - second step done

Setup of new structure done. Compiles, build library and demo tests work as before.
No installation tests done.

Werner Dittmann Werner Dittmann 2012-07-09

1 2 > >> (Page 1 of 2)
added clients/ccrtp/ZrtpQueue.h
added clients/ccrtp/zrtpccrtp.h
added clients/ccrtp/ZrtpQueue.cpp
changed clients/ccrtp/CMakeLists.txt
changed cmake/Modules/GeneratePackage.cmake
changed demo/CMakeLists.txt
changed demo/zrtptest.cpp
changed demo/zrtptestMulti.cpp
changed zrtp/crypto/gcrypt/gcryptAesCFB.cpp
changed zrtp/crypto/gcrypt/gcryptZrtpDH.cpp
changed zrtp/crypto/gcrypt/gcrypthmac256.cpp
changed zrtp/crypto/gcrypt/gcrypthmac384.cpp
changed zrtp/crypto/gcrypt/gcryptsha256.cpp
changed zrtp/crypto/gcrypt/gcryptsha384.cpp
changed zrtp/crypto/openssl/hmac256.cpp
changed zrtp/crypto/openssl/hmac384.cpp
changed zrtp/crypto/openssl/sha256.cpp
changed zrtp/crypto/openssl/sha384.cpp
changed zrtp/crypto/twoCFB.cpp
copied zrtp/crypto/openssl/AesCFB.cpp -> zrtp/crypto/openssl/aesCFB.cpp
copied zrtp/crypto/openssl/ZrtpDH.cpp -> zrtp/crypto/openssl/zrtpDH.cpp
copied zrtp/crypto/zrtpDh.h -> clients/ccrtp/TimeoutProvider.h
clients/ccrtp/ZrtpQueue.h Diff Switch to side-by-side view
Loading...
clients/ccrtp/zrtpccrtp.h Diff Switch to side-by-side view
Loading...
clients/ccrtp/ZrtpQueue.cpp Diff Switch to side-by-side view
Loading...
clients/ccrtp/CMakeLists.txt Diff Switch to side-by-side view
Loading...
cmake/Modules/GeneratePackage.cmake Diff Switch to side-by-side view
Loading...
demo/CMakeLists.txt Diff Switch to side-by-side view
Loading...
demo/zrtptest.cpp Diff Switch to side-by-side view
Loading...
demo/zrtptestMulti.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcryptAesCFB.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcryptZrtpDH.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcrypthmac256.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcrypthmac384.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcryptsha256.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/gcrypt/gcryptsha384.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/openssl/hmac256.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/openssl/hmac384.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/openssl/sha256.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/openssl/sha384.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/twoCFB.cpp Diff Switch to side-by-side view
Loading...
zrtp/crypto/openssl/AesCFB.cpp to zrtp/crypto/openssl/aesCFB.cpp
--- a/zrtp/crypto/openssl/AesCFB.cpp
+++ b/zrtp/crypto/openssl/aesCFB.cpp
@@ -38,7 +38,7 @@
 #include <openssl/aes.h>
 #include <string.h>
 
-#include <libzrtpcpp/crypto/aesCFB.h>
+#include <crypto/aesCFB.h>
 
 // extern void initializeOpenSSL();
 
zrtp/crypto/openssl/ZrtpDH.cpp to zrtp/crypto/openssl/zrtpDH.cpp
--- a/zrtp/crypto/openssl/ZrtpDH.cpp
+++ b/zrtp/crypto/openssl/zrtpDH.cpp
@@ -46,7 +46,7 @@
 #include <openssl/ec.h>
 #include <openssl/ecdh.h>
 
-#include <libzrtpcpp/crypto/ZrtpDH.h>
+#include <crypto/zrtpDH.h>
 #include <libzrtpcpp/ZrtpTextData.h>
 
 // extern void initializeOpenSSL();
zrtp/crypto/zrtpDh.h to clients/ccrtp/TimeoutProvider.h
--- a/zrtp/crypto/zrtpDh.h
+++ b/clients/ccrtp/TimeoutProvider.h
@@ -1,163 +1,298 @@
 /*
-  Copyright (C) 2006-2009 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 3 of the License, or
-  (at your option) any later version.
-
-  This program is distributed in the hope that it will be useful,
+  Copyright (C) 2006, 2005, 2004 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 General Public License for more details.
-
-  You should have received a copy of the GNU General Public License
-  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  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
 */
 
-/*
- * Authors: Werner Dittmann <Werner.Dittmann@t-online.de>
- */
-
-#ifndef _ZRTPDH_H__
-#define _ZRTPDH_H__
-
-#include <stdint.h>
-#include <libzrtpcpp/ZrtpConfigure.h>
+
+#ifndef TIMEOUTPROVIDER_H
+#define TIMEOUTPROVIDER_H
 
 /**
- * @file ZrtpDH.h
- * @brief Class that implemets Diffie-Helman key agreement for ZRTP
- * 
- * @ingroup GNU_ZRTP
- * @{
- */
-
-/**
- * Generates a number of random bytes.
- *
- * @param buf
- *    Pointer to a buffer that receives the random data. Must have a size
- *    of at least <code>length</code> bytes.
- *
- * @param length
- *    Number of random bytes to produce.
- */
-void randomZRTP(uint8_t *buf, int32_t length);
-
-const int32_t DH2K = 0;
-const int32_t DH3K = 1;
-const int32_t EC25 = 2;
-const int32_t EC38 = 3;
-
-
-/**
- * Implementation of Diffie-Helman for ZRTP
- *
- * This class defines functions to generate and compute the
- * Diffie-Helman public and secret data and the shared secret. According to
- * the ZRTP specification we use the MODP groups as defined by RFC 3526 for
- * length 3072 and 4096.
- *
+ * Provides a way to request timeouts after a number of milli seconds.
+ *
+ * A command is associated to each timeout.
+ *
+ * Modified to use the common c++ library functions and the STL
+ * list by Werner Dittmann.
+ *
+ * @author Erik Eliasson, eliasson@it.kth.se, 2003
  * @author Werner Dittmann <Werner.Dittmann@t-online.de>
  */
 
-class ZrtpDH {
+#include <list>
+#include <sys/time.h>
+
+#include <commoncpp/config.h>
+#include <commoncpp/thread.h>
+
+/**
+ * Represents a request of a "timeout" (delivery of a command to a
+ * "timeout receiver" after at least a specified time period).
+ *
+ * Slightly modified to use gettimeofday directly.
+ *
+ * NOTE: This class is only used internaly.
+ * @author Erik Eliasson
+ * @author Werner Dittmann
+ */
+template <class TOCommand, class TOSubscriber>
+class TPRequest
+{
+
+public:
+
+    TPRequest( TOSubscriber tsi, int timeoutMs, const TOCommand &command):
+    subscriber(tsi)
+    {
+        struct timeval tv;
+        gettimeofday(&tv, NULL );
+
+        when_ms = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
+    when_ms += timeoutMs;
+    this->command = command;
+    }
+
+    /**
+     * @param t  ms since Epoch
+     */
+    bool happensBefore(uint64 t)
+    {
+    if (when_ms < t) {
+        return true;
+    }
+    if (when_ms > t) {
+        return false;
+    }
+    return false; // if equal it does not "happens_before"
+
+    }
+
+    bool happensBefore(const TPRequest *req){
+    return happensBefore(req->when_ms);
+    }
+
+    /**
+     * Number of milli seconds until timeout from when this method is
+     * called
+     */
+    int getMsToTimeout ()
+    {
+        struct timeval tv;
+        gettimeofday(&tv, NULL );
+
+        uint64 now = ((uint64)tv.tv_sec) * (uint64)1000 + ((uint64)tv.tv_usec) / (uint64)1000;
+
+    if (happensBefore(now)) {
+        return 0;
+    }
+    else {
+        return (int)(when_ms - now);
+    }
+    }
+
+    TOCommand getCommand()
+    {
+    return command;
+    }
+
+    TOSubscriber getSubscriber()
+    {
+    return subscriber;
+    }
+
+    /**
+     * Two timeout requests are considered equeal if they have
+     * the same subscriber AND command AND time when they
+     * occur. If one of the time is zero then this is a
+     * wildcard and matches always.
+     */
+    bool operator==(const TPRequest<TOCommand, TOSubscriber> &req)
+    {
+    if (req.subscriber == subscriber &&
+               req.command == command &&
+               req.when_ms == when_ms) {
+            return true;
+        }
+        return false;
+    }
 
 private:
-    void* ctx;      ///< Context the DH
-    int pkType;     ///< Which type of DH to use
+    TOSubscriber subscriber;
+    uint64 when_ms;     // Time since Epoch in ms when the timeout
+                // will happen
+
+    TOCommand command;      // Command that will be delivered to the
+                // receiver (subscriber) of the timeout.
+};
+
+/**
+ * Class to generate objects giving timeout functionality.
+ *
+ * @author Erik Eliasson
+ * @author Werner Dittmann
+ */
+template<class TOCommand, class TOSubscriber>
+        class TimeoutProvider : public ost::Thread, ost::Event {
 
 public:
-    /**
-     * Create a Diffie-Helman key agreement algorithm
-     * 
-     * @param type
-     *     Name of the DH algorithm to use
-     */
-    ZrtpDH(const char* type);
-    
-    ~ZrtpDH();
-
-    /**
-     * Generates a public key based on the DH parameters and a random
-     * private key.
+
+    /**
+     * Timeout Provide Constructor
+     */
+    TimeoutProvider(): requests(), synchLock(), stop(false)  { }
+
+    /**
+     * Destructor also terminates the Timeout thread.
+     */
+    ~TimeoutProvider() {
+    terminate();
+    }
+
+    /**
+     * Terminates the Timeout provider thread.
+     */
+    void stopThread(){
+    stop = true;
+    signal();       // signal event to waiting thread
+    }
+
+    /**
+     * Request a timeout trigger.
      *
-     * @return 1 on success, 0 on failure
-     */
-    int32_t generatePublicKey();
-
-    /**
-     * Returns the size in bytes of the DH parameter p.
+     * @param time_ms   Number of milli-seconds until the timeout is
+     *          wanted. Note that a small additional period of time is
+     *          added that depends on execution speed.
+     * @param subscriber The receiver of the callback when the command has timed
+     *          out. This argument must not be NULL.
+     * @param command   Specifies the String command to be passed back in the
+     *          callback.
+     */
+    void requestTimeout(int32_t time_ms, TOSubscriber subscriber, const TOCommand &command)
+    {
+        TPRequest<TOCommand, TOSubscriber>* request =
+                new TPRequest<TOCommand, TOSubscriber>(subscriber, time_ms, command);
+
+        synchLock.enter();
+
+    if (requests.size()==0) {
+        requests.push_front(request);
+        signal();
+        synchLock.leave();
+        return;
+    }
+        if (request->happensBefore(requests.front())) {
+        requests.push_front(request);
+        signal();
+            synchLock.leave();
+        return;
+    }
+        if (requests.back()->happensBefore(request)){
+        requests.push_back(request);
+        signal();
+            synchLock.leave();
+        return;
+    }
+
+        typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
+        for(i = requests.begin(); i != requests.end(); i++ ) {
+            if( request->happensBefore(*i)) {
+                requests.insert(i, request);
+                break;
+            }
+        }
+    signal();
+    synchLock.leave();
+    }
+
+    /**
+     * Removes timeout requests that belong to a subscriber and command.
      *
-     * @return Size in bytes.
-     */
-    int32_t getDhSize() const;
-
-    /**
-     * Returns the size in bytes of computed public key.
-     *
-     * @return Size in bytes.
-     */
-    int32_t getPubKeySize() const;
-
-    /**
-     * Returns the bytes of computed secret key.
-     *
-     * Returns the bytes of the public key in network (big endian) order.#
-     *
-     * @param buf
-     *    Pointer to a buffer of at least <code>getPubKeySize()</code> bytes.
-     *
-     * @return Size in bytes.
-     */
-    int32_t getPubKeyBytes(uint8_t *buf) const;
-
-    /**
-     * Compute the secret key and returns it to caller.
-     *
-     * This method computes the secret key based on the DH parameters, the
-     * private key and the peer's public key.
-     *
-     * @param pubKeyBytes
-     *    Pointer to the peer's public key bytes. Must be in big endian order.
-     *
-     * @param secret
-     *    Pointer to a buffer that receives the secret key. This buffer must
-     *    have a length of at least <code>getSecretSize()</code> bytes.
-     *
-     * @return the size of the shared secret on success, -1 on error.
-     */
-    int32_t computeSecretKey(uint8_t *pubKeyBytes, uint8_t *secret);
-
-    /**
-     * Check and validate the public key received from peer.
-     *
-     * Check if this is a correct Diffie-Helman public key. If the public
-     * key value is either one or (P-1) then this is a wrong public key
-     * value.
-     *
-     * @param pubKeyBytes
-     *     Pointer to the peer's public key bytes. Must be in big endian order.
-     *
-     * @return 0 if check faild, 1 if public key value is ok.
-     */
-    int32_t checkPubKey(uint8_t* pubKeyBytes) const;
-
-    /**
-     * Get type of DH algorithm.
-     * 
-     * @return
-     *     Pointer to DH algorithm name
-     */
-    const char* getDHtype();
+     * @see requestTimeout
+     */
+    void cancelRequest(TOSubscriber subscriber, const TOCommand &command)
+    {
+        synchLock.enter();
+        typename std::list<TPRequest<TOCommand, TOSubscriber>* >::iterator i;
+        for(i = requests.begin(); i != requests.end(); ) {
+            if( (*i)->getCommand() == command &&
+                (*i)->getSubscriber() == subscriber) {
+                i = requests.erase(i);
+                continue;
+            }
+            i++;
+        }
+    synchLock.leave();
+    }
+
+protected:
+
+    void run()
+    {
+    do {
+        synchLock.enter();
+        int32_t time = 3600000;
+        int32_t size = 0;
+        if ((size = requests.size()) > 0) {
+                time = requests.front()->getMsToTimeout();
+            }
+        if (time == 0 && size > 0) {
+        if (stop){  // This must be checked so that we will
+                // stop even if we have timeouts to deliver.
+                    synchLock.leave();
+            return;
+        }
+                TPRequest<TOCommand, TOSubscriber>* req = requests.front();
+        TOSubscriber subs = req->getSubscriber();
+        TOCommand command = req->getCommand();
+
+                requests.pop_front();
+
+        synchLock.leave(); // call the command with free Mutex
+        subs->handleTimeout(command);
+        continue;
+        }
+        synchLock.leave();
+        if (stop) {     // If we were told to stop while delivering
+                                // a timeout we will exit here
+        return;
+        }
+        reset();        // ready to receive triggers again
+            wait(time);
+        if (stop) {     // If we are told to exit while waiting we
+                                // will exit
+        return;
+        }
+    } while(true);
+    }
+
+private:
+
+    // The timeouts are ordered in the order of which they
+    // will expire. Nearest in future is first in list.
+    std::list<TPRequest<TOCommand, TOSubscriber> *> requests;
+
+    ost::Mutex synchLock;   // Protects the internal data structures
+
+    bool stop;      // Flag to tell the worker thread
+                        // to terminate. Set to true and
+                        // wake the worker thread to
+                        // terminate it.
 };
 
-#endif // ZRTPDH_H
-
-/**
- * @}
- */
+#endif
 
 /** EMACS **
  * Local variables:
1 2 > >> (Page 1 of 2)