DJGPP has some needed header files that other MSDOS/Windows compilers don't have.
[openssl.git] / crypto / engine / hw_aep.c
index cefd3f006a13a00c59ee6b8adc35a5b702b0ff11..8b8380a582b4d1432a76862a70ef6aff952652ce 100644 (file)
@@ -60,7 +60,7 @@
 #include <string.h>
 
 #include <openssl/e_os2.h>
-#ifndef OPENSSL_SYS_MSDOS
+#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
 #include <sys/types.h>
 #include <unistd.h>
 #else
@@ -71,6 +71,7 @@ typedef int pid_t;
 #include <openssl/crypto.h>
 #include <openssl/dso.h>
 #include <openssl/engine.h>
+#include <openssl/buffer.h>
 
 #ifndef OPENSSL_NO_HW
 #ifndef OPENSSL_NO_HW_AEP
@@ -92,6 +93,7 @@ static int aep_destroy(ENGINE *e);
 
 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
+static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
 
 /* BIGNUM stuff */
@@ -136,8 +138,8 @@ static int aep_rand_status(void);
 #endif
 
 /* Bignum conversion stuff */
-static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize);
-static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
+static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize);
+static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
        unsigned char* AEP_BigNum);
 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
        unsigned char* AEP_BigNum);
@@ -362,7 +364,24 @@ static DSO *aep_dso = NULL;
 /* These are the static string constants for the DSO file name and the function
  * symbol names to bind to. 
 */
-static const char *AEP_LIBNAME = "aep";
+static const char *AEP_LIBNAME = NULL;
+static const char *get_AEP_LIBNAME(void)
+       {
+       if(AEP_LIBNAME)
+               return AEP_LIBNAME;
+       return "aep";
+       }
+static void free_AEP_LIBNAME(void)
+       {
+       if(AEP_LIBNAME)
+               OPENSSL_free((void*)AEP_LIBNAME);
+       AEP_LIBNAME = NULL;
+       }
+static long set_AEP_LIBNAME(const char *name)
+       {
+       free_AEP_LIBNAME();
+       return ((AEP_LIBNAME = BUF_strdup(name)) != NULL ? 1 : 0);
+       }
 
 static const char *AEP_F1    = "AEP_ModExp";
 static const char *AEP_F2    = "AEP_ModExpCrt";
@@ -411,7 +430,7 @@ static int aep_init(ENGINE *e)
                }
        /* Attempt to load libaep.so. */
 
-       aep_dso = DSO_load(NULL, AEP_LIBNAME, NULL, 0);
+       aep_dso = DSO_load(NULL, get_AEP_LIBNAME(), NULL, 0);
   
        if(aep_dso == NULL)
                {
@@ -473,6 +492,7 @@ static int aep_init(ENGINE *e)
 /* Destructor (complements the "ENGINE_aep()" constructor) */
 static int aep_destroy(ENGINE *e)
        {
+       free_AEP_LIBNAME();
        ERR_unload_AEPHK_strings();
        return 1;
        }
@@ -548,8 +568,7 @@ static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
                                AEPHK_R_ALREADY_LOADED);
                        return 0;
                        }
-               AEP_LIBNAME = (const char *)p;
-               return 1;
+               return set_AEP_LIBNAME((const char*)p);
        default:
                break;
                }
@@ -649,9 +668,6 @@ static int aep_rand(unsigned char *buf,int len )
        AEP_RV rv = AEP_R_OK;
        AEP_CONNECTION_HNDL hConnection;
 
-       int to_return = 0;
-
-  
        CRYPTO_w_lock(CRYPTO_LOCK_RAND);
 
        /*Can the request be serviced with what's already in the buffer?*/
@@ -950,9 +966,10 @@ static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
        return AEP_R_OK;
        }
 
-static int aep_close_connection(unsigned int hConnection)
+static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
        {
        int count;
+       AEP_RV rv = AEP_R_OK;
 
        CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
 
@@ -961,21 +978,24 @@ static int aep_close_connection(unsigned int hConnection)
                {
                if (aep_app_conn_table[count].conn_hndl == hConnection)
                        {
+                       rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
+                       if (rv != AEP_R_OK)
+                               goto end;
                        aep_app_conn_table[count].conn_state = NotConnected;
-                       close(aep_app_conn_table[count].conn_hndl);
+                       aep_app_conn_table[count].conn_hndl  = 0;
                        break;
                        }
                }
 
+ end:
        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
-
-       return AEP_R_OK;
+       return rv;
        }
 
 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
        {
        int count;
-       AEP_RV rv;
+       AEP_RV rv = AEP_R_OK;
 
        *in_use = 0;
        if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
@@ -986,7 +1006,7 @@ static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
                case Connected:
                        rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
                        if (rv != AEP_R_OK)
-                               return rv;
+                               goto end;
                        aep_app_conn_table[count].conn_state = NotConnected;
                        aep_app_conn_table[count].conn_hndl  = 0;
                        break;
@@ -997,14 +1017,15 @@ static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
                        break;
                        }
                }
+ end:
        if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
-       return AEP_R_OK;
+       return rv;
        }
 
 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
   Note only 32bit Openssl build support*/
 
-static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize)
+static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
        {
        BIGNUM* bn;
 
@@ -1022,7 +1043,7 @@ static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize)
        return AEP_R_OK;
        }
 
-static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
+static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
        unsigned char* AEP_BigNum)
        {
        BIGNUM* bn;
@@ -1044,8 +1065,8 @@ static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
                {
                buf = (unsigned char*)&bn->d[i];
 
-               *((AEP_U32*)AEP_BigNum) =
-                       (AEP_U32) ((unsigned) buf[1] << 8 | buf[0]) |
+               *((AEP_U32*)AEP_BigNum) = (AEP_U32)
+                       ((unsigned) buf[1] << 8 | buf[0]) |
                        ((unsigned) buf[3] << 8 | buf[2])  << 16;
 
                AEP_BigNum += 4;