Some of the ENGINE file names were changed for 8.3 filename uniqueness
[openssl.git] / crypto / engine / hw_ubsec.c
index 143efbdeea4e588d9414a964af3089d3e4cd1866..0ed7f9b54183a97ded7fc3cb342e6a136956ef33 100644 (file)
@@ -5,7 +5,7 @@
  * Cloned shamelessly by Joe Tardo. 
  */
 /* ====================================================================
- * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -62,7 +62,6 @@
 #include <openssl/crypto.h>
 #include "cryptlib.h"
 #include <openssl/dso.h>
-#include "engine_int.h"
 #include <openssl/engine.h>
 
 #ifndef OPENSSL_NO_HW
 #include "vendor_defns/hw_ubsec.h"
 #endif
 
+static int ubsec_destroy(ENGINE *e);
 static int ubsec_init(ENGINE *e);
 static int ubsec_finish(ENGINE *e);
+static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx);
 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *q, const BIGNUM *dp,
                        const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
+#ifndef OPENSSL_NO_RSA
 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
+#endif
 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
+#ifndef OPENSSL_NO_DSA
 #if NOT_USED
 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
                BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
@@ -95,19 +99,31 @@ static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
                                 DSA_SIG *sig, DSA *dsa);
+#endif
+#ifndef OPENSSL_NO_DH
 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
                const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
                BN_MONT_CTX *m_ctx);
 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
 static int ubsec_dh_generate_key(DH *dh);
+#endif
 
 #if NOT_USED
 static int ubsec_rand_bytes(unsigned char *buf, int num);
 static int ubsec_rand_status(void);
 #endif
-/* Our internal RSA_METHOD that we provide pointers to */
 
+#define UBSEC_CMD_SO_PATH              ENGINE_CMD_BASE
+static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
+       {UBSEC_CMD_SO_PATH,
+               "SO_PATH",
+               "Specifies the path to the 'ubsec' shared library",
+               ENGINE_CMD_FLAG_STRING},
+       {0, NULL, NULL, 0}
+       };
+
+#ifndef OPENSSL_NO_RSA
+/* Our internal RSA_METHOD that we provide pointers to */
 static RSA_METHOD ubsec_rsa =
        {
        "UBSEC RSA method",
@@ -124,7 +140,9 @@ static RSA_METHOD ubsec_rsa =
        NULL,
        NULL
        };
+#endif
 
+#ifndef OPENSSL_NO_DSA
 /* Our internal DSA_METHOD that we provide pointers to */
 static DSA_METHOD ubsec_dsa =
        {
@@ -139,7 +157,9 @@ static DSA_METHOD ubsec_dsa =
        0, /* flags */
        NULL /* app_data */
        };
+#endif
 
+#ifndef OPENSSL_NO_DH
 /* Our internal DH_METHOD that we provide pointers to */
 static DH_METHOD ubsec_dh =
        {
@@ -152,37 +172,116 @@ static DH_METHOD ubsec_dh =
        0,
        NULL
        };
+#endif
 
-/* Our ENGINE structure. */
-static ENGINE engine_ubsec =
-        {
-       "ubsec",
-       "UBSEC hardware engine support",
-       &ubsec_rsa,
-       &ubsec_dsa,
-       &ubsec_dh,
-       NULL,
-       ubsec_mod_exp,
-       ubsec_mod_exp_crt,
-       ubsec_init,
-       ubsec_finish,
-       NULL, /* no ctrl() */
-       NULL, /* no load_privkey() */
-       NULL, /* no load_pubkey() */
-       0, /* no flags */
-       0, 0, /* no references */
-       NULL, NULL /* unlinked */
-        };
+#ifndef OPENSSL_NO_ERR
+/* Error function codes for use in ubsec operation */
+#define UBSEC_F_UBSEC_INIT                     100
+#define UBSEC_F_UBSEC_FINISH                   101
+#define UBSEC_F_UBSEC_CTRL                     102
+#define UBSEC_F_UBSEC_MOD_EXP                  103
+#define UBSEC_F_UBSEC_RSA_MOD_EXP              104
+#define UBSEC_F_UBSEC_RSA_MOD_EXP_CRT          105
+#define UBSEC_F_UBSEC_DSA_SIGN                 106
+#define UBSEC_F_UBSEC_DSA_VERIFY               107
+/* Error reason codes */
+#define UBSEC_R_ALREADY_LOADED                 108
+#define UBSEC_R_DSO_FAILURE                    109
+#define UBSEC_R_UNIT_FAILURE                   110
+#define UBSEC_R_NOT_LOADED                     111
+#define UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED   112
+#define UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL    113
+#define UBSEC_R_BN_EXPAND_FAIL                 114
+#define UBSEC_R_REQUEST_FAILED                 115
+#define UBSEC_R_MISSING_KEY_COMPONENTS         116
+static ERR_STRING_DATA ubsec_str_functs[] =
+       {
+       /* This first element is changed to match the dynamic 'lib' number */
+{ERR_PACK(0,0,0),                              "ubsec engine code"},
+{ERR_PACK(0,UBSEC_F_UBSEC_INIT,0),             "ubsec_init"},
+{ERR_PACK(0,UBSEC_F_UBSEC_FINISH,0),           "ubsec_finish"},
+{ERR_PACK(0,UBSEC_F_UBSEC_CTRL,0),             "ubsec_ctrl"},
+{ERR_PACK(0,UBSEC_F_UBSEC_MOD_EXP,0),          "ubsec_mod_exp"},
+{ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP,0),      "ubsec_rsa_mod_exp"},
+{ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP_CRT,0),  "ubsec_rsa_mod_exp_crt"},
+{ERR_PACK(0,UBSEC_F_UBSEC_DSA_SIGN,0),         "ubsec_dsa_sign"},
+{ERR_PACK(0,UBSEC_F_UBSEC_DSA_VERIFY,0),       "ubsec_dsa_verify"},
+/* Error reason codes */
+{UBSEC_R_ALREADY_LOADED                        ,"already loaded"},
+{UBSEC_R_DSO_FAILURE                   ,"DSO failure"},
+{UBSEC_R_UNIT_FAILURE                  ,"unit failure"},
+{UBSEC_R_NOT_LOADED                    ,"not loaded"},
+{UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED  ,"ctrl command not implemented"},
+{UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL   ,"size too large or too small"},
+{UBSEC_R_BN_EXPAND_FAIL                        ,"bn_expand fail"},
+{UBSEC_R_REQUEST_FAILED                        ,"request failed"},
+{UBSEC_R_MISSING_KEY_COMPONENTS                ,"missing key components"},
+{0,NULL}
+       };
+/* The library number we obtain dynamically from the ERR code */
+static int ubsec_err_lib = -1;
+#define UBSECerr(f,r) ERR_PUT_error(ubsec_err_lib,(f),(r),__FILE__,__LINE__)
+static void ubsec_load_error_strings(void)
+       {
+       if(ubsec_err_lib < 0)
+               {
+               if((ubsec_err_lib = ERR_get_next_error_library()) <= 0)
+                       return;
+               ubsec_str_functs[0].error = ERR_PACK(ubsec_err_lib,0,0);
+               ERR_load_strings(ubsec_err_lib, ubsec_str_functs);
+               }
+       }
+static void ubsec_unload_error_strings(void)
+       {
+       if(ubsec_err_lib >= 0)
+               {
+               ERR_unload_strings(ubsec_err_lib, ubsec_str_functs);
+               ubsec_err_lib = -1;
+               }
+       }
+#else
+#define UBSECerr(f,r)                                  /* NOP */
+static void ubsec_load_error_strings(void) { }         /* NOP */
+static void ubsec_unload_error_strings(void) { }       /* NOP */
+#endif
 
-/* As this is only ever called once, there's no need for locking
- * (indeed - the lock will already be held by our caller!!!) */
-ENGINE *ENGINE_ubsec()
+/* Constants used when creating the ENGINE */
+static const char *engine_ubsec_id = "ubsec";
+static const char *engine_ubsec_name = "UBSEC hardware engine support";
+
+/* This internal function is used by ENGINE_ubsec() and possibly by the
+ * "dynamic" ENGINE support too */
+static int bind_helper(ENGINE *e)
        {
+#ifndef OPENSSL_NO_RSA
        const RSA_METHOD *meth1;
+#endif
+#ifndef OPENSSL_NO_DH
 #ifndef HAVE_UBSEC_DH
        const DH_METHOD *meth3;
 #endif /* HAVE_UBSEC_DH */
+#endif
+       if(!ENGINE_set_id(e, engine_ubsec_id) ||
+                       !ENGINE_set_name(e, engine_ubsec_name) ||
+#ifndef OPENSSL_NO_RSA
+                       !ENGINE_set_RSA(e, &ubsec_rsa) ||
+#endif
+#ifndef OPENSSL_NO_DSA
+                       !ENGINE_set_DSA(e, &ubsec_dsa) ||
+#endif
+#ifndef OPENSSL_NO_DH
+                       !ENGINE_set_DH(e, &ubsec_dh) ||
+#endif
+                       !ENGINE_set_BN_mod_exp(e, ubsec_mod_exp) ||
+                       !ENGINE_set_BN_mod_exp_crt(e, ubsec_mod_exp_crt) ||
+                       !ENGINE_set_destroy_function(e, ubsec_destroy) ||
+                       !ENGINE_set_init_function(e, ubsec_init) ||
+                       !ENGINE_set_finish_function(e, ubsec_finish) ||
+                       !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
+                       !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
+               return 0;
 
+#ifndef OPENSSL_NO_RSA
        /* We know that the "PKCS1_SSLeay()" functions hook properly
         * to the Broadcom-specific mod_exp and mod_exp_crt so we use
         * those functions. NB: We don't use ENGINE_openssl() or
@@ -195,15 +294,35 @@ ENGINE *ENGINE_ubsec()
        ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
        ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
        ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
+#endif
 
+#ifndef OPENSSL_NO_DH
 #ifndef HAVE_UBSEC_DH
        /* Much the same for Diffie-Hellman */
        meth3 = DH_OpenSSL();
        ubsec_dh.generate_key = meth3->generate_key;
        ubsec_dh.compute_key = meth3->compute_key;
 #endif /* HAVE_UBSEC_DH */
+#endif
+
+       /* Ensure the ubsec error handling is set up */
+       ubsec_load_error_strings();
+       return 1;
+       }
 
-       return &engine_ubsec;
+/* As this is only ever called once, there's no need for locking
+ * (indeed - the lock will already be held by our caller!!!) */
+ENGINE *ENGINE_ubsec(void)
+       {
+       ENGINE *ret = ENGINE_new();
+       if(!ret)
+               return NULL;
+       if(!bind_helper(ret))
+               {
+               ENGINE_free(ret);
+               return NULL;
+               }
+       return ret;
        }
 
 /* This is a process-global DSO handle used for loading and unloading
@@ -221,13 +340,19 @@ static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
+#ifndef OPENSSL_NO_DH
 static t_UBSEC_diffie_hellman_generate_ioctl 
        *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
+#endif
+/* #ifndef OPENSSL_NO_RSA */
 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
+/* #endif */
+#ifndef OPENSSL_NO_DSA
 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
+#endif
 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
 
@@ -241,15 +366,28 @@ static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
 static const char *UBSEC_F3 = "ubsec_open";
 static const char *UBSEC_F4 = "ubsec_close";
+#ifndef OPENSSL_NO_DH
 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
+#endif
+/* #ifndef OPENSSL_NO_RSA */
 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
+/* #endif */
+#ifndef OPENSSL_NO_DSA
 static const char *UBSEC_F9 = "dsa_sign_ioctl";
 static const char *UBSEC_F10 = "dsa_verify_ioctl";
+#endif
 static const char *UBSEC_F11 = "math_accelerate_ioctl";
 static const char *UBSEC_F12 = "rng_ioctl";
 
+/* Destructor (complements the "ENGINE_ubsec()" constructor) */
+static int ubsec_destroy(ENGINE *e)
+       {
+       ubsec_unload_error_strings();
+       return 1;
+       }
+
 /* (de)initialisation functions. */
 static int ubsec_init(ENGINE *e)
        {
@@ -257,19 +395,25 @@ static int ubsec_init(ENGINE *e)
        t_UBSEC_ubsec_bits_to_bytes *p2;
        t_UBSEC_ubsec_open *p3;
        t_UBSEC_ubsec_close *p4;
+#ifndef OPENSSL_NO_DH
        t_UBSEC_diffie_hellman_generate_ioctl *p5;
        t_UBSEC_diffie_hellman_agree_ioctl *p6;
+#endif
+/* #ifndef OPENSSL_NO_RSA */
        t_UBSEC_rsa_mod_exp_ioctl *p7;
        t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
+/* #endif */
+#ifndef OPENSSL_NO_DSA
        t_UBSEC_dsa_sign_ioctl *p9;
        t_UBSEC_dsa_verify_ioctl *p10;
+#endif
        t_UBSEC_math_accelerate_ioctl *p11;
        t_UBSEC_rng_ioctl *p12;
        int fd = 0;
 
        if(ubsec_dso != NULL)
                {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_ALREADY_LOADED);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
                goto err;
                }
        /* 
@@ -278,7 +422,7 @@ static int ubsec_init(ENGINE *e)
        ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
        if(ubsec_dso == NULL)
                {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_DSO_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
                goto err;
                }
 
@@ -287,19 +431,25 @@ static int ubsec_init(ENGINE *e)
        !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
        !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
        !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
+#ifndef OPENSSL_NO_DH
        !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) 
                                DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
        !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) 
                                DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
+#endif
+/* #ifndef OPENSSL_NO_RSA */
        !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
        !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
+/* #endif */
+#ifndef OPENSSL_NO_DSA
        !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
        !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
+#endif
        !(p11 = (t_UBSEC_math_accelerate_ioctl *) 
                                DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
        !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
                {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_DSO_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
                goto err;
                }
 
@@ -308,12 +458,18 @@ static int ubsec_init(ENGINE *e)
        p_UBSEC_ubsec_bits_to_bytes = p2;
        p_UBSEC_ubsec_open = p3;
        p_UBSEC_ubsec_close = p4;
+#ifndef OPENSSL_NO_DH
        p_UBSEC_diffie_hellman_generate_ioctl = p5;
        p_UBSEC_diffie_hellman_agree_ioctl = p6;
+#endif
+#ifndef OPENSSL_NO_RSA
        p_UBSEC_rsa_mod_exp_ioctl = p7;
        p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
+#endif
+#ifndef OPENSSL_NO_DSA
        p_UBSEC_dsa_sign_ioctl = p9;
        p_UBSEC_dsa_verify_ioctl = p10;
+#endif
        p_UBSEC_math_accelerate_ioctl = p11;
        p_UBSEC_rng_ioctl = p12;
 
@@ -325,7 +481,7 @@ static int ubsec_init(ENGINE *e)
                }
        else
                {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
                }
 
 err:
@@ -335,12 +491,18 @@ err:
        p_UBSEC_ubsec_bits_to_bytes = NULL;
        p_UBSEC_ubsec_open = NULL;
        p_UBSEC_ubsec_close = NULL;
+#ifndef OPENSSL_NO_DH
        p_UBSEC_diffie_hellman_generate_ioctl = NULL;
        p_UBSEC_diffie_hellman_agree_ioctl = NULL;
+#endif
+#ifndef OPENSSL_NO_RSA
        p_UBSEC_rsa_mod_exp_ioctl = NULL;
        p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
+#endif
+#ifndef OPENSSL_NO_DSA
        p_UBSEC_dsa_sign_ioctl = NULL;
        p_UBSEC_dsa_verify_ioctl = NULL;
+#endif
        p_UBSEC_math_accelerate_ioctl = NULL;
        p_UBSEC_rng_ioctl = NULL;
 
@@ -351,12 +513,12 @@ static int ubsec_finish(ENGINE *e)
        {
        if(ubsec_dso == NULL)
                {
-               ENGINEerr(ENGINE_F_UBSEC_FINISH, ENGINE_R_NOT_LOADED);
+               UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
                return 0;
                }
        if(!DSO_free(ubsec_dso))
                {
-               ENGINEerr(ENGINE_F_UBSEC_FINISH, ENGINE_R_DSO_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
                return 0;
                }
        ubsec_dso = NULL;
@@ -364,17 +526,48 @@ static int ubsec_finish(ENGINE *e)
        p_UBSEC_ubsec_bits_to_bytes = NULL;
        p_UBSEC_ubsec_open = NULL;
        p_UBSEC_ubsec_close = NULL;
+#ifndef OPENSSL_NO_DH
        p_UBSEC_diffie_hellman_generate_ioctl = NULL;
        p_UBSEC_diffie_hellman_agree_ioctl = NULL;
+#endif
+#ifndef OPENSSL_NO_RSA
        p_UBSEC_rsa_mod_exp_ioctl = NULL;
        p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
+#endif
+#ifndef OPENSSL_NO_DSA
        p_UBSEC_dsa_sign_ioctl = NULL;
        p_UBSEC_dsa_verify_ioctl = NULL;
+#endif
        p_UBSEC_math_accelerate_ioctl = NULL;
        p_UBSEC_rng_ioctl = NULL;
        return 1;
        }
 
+static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
+       {
+       int initialised = ((ubsec_dso == NULL) ? 0 : 1);
+       switch(cmd)
+               {
+       case UBSEC_CMD_SO_PATH:
+               if(p == NULL)
+                       {
+                       UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
+                       return 0;
+                       }
+               if(initialised)
+                       {
+                       UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
+                       return 0;
+                       }
+               UBSEC_LIBNAME = (const char *)p;
+               return 1;
+       default:
+               break;
+               }
+       UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
+       return 0;
+       }
+
 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx)
        {
@@ -383,27 +576,27 @@ static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 
        if(ubsec_dso == NULL)
        {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_NOT_LOADED);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
                return 0;
        }
 
        /* Check if hardware can't handle this argument. */
        y_len = BN_num_bits(m);
        if (y_len > 1024) {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
                return 0;
        } 
 
        if(!bn_wexpand(r, m->top))
        {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
                return 0;
        }
        memset(r->d, 0, BN_num_bytes(m));
 
        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
                return 0;
        }
 
@@ -411,7 +604,7 @@ static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d, 
                BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
        {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_REQUEST_FAILED);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
                return 0;
        }
 
@@ -421,6 +614,7 @@ static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        return 1;
        }
 
+#ifndef OPENSSL_NO_RSA
 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
        {
        BN_CTX *ctx;
@@ -431,7 +625,7 @@ static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
 
        if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
                {
-               ENGINEerr(ENGINE_F_UBSEC_RSA_MOD_EXP, ENGINE_R_MISSING_KEY_COMPONENTS);
+               UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
                goto err;
                }
 
@@ -450,6 +644,7 @@ err:
                BN_CTX_free(ctx);
        return to_return;
        }
+#endif
 
 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                        const BIGNUM *q, const BIGNUM *dp,
@@ -464,18 +659,18 @@ static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 
        /* Check if hardware can't handle this argument. */
        if (y_len > 1024) {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
                return 0;
        } 
 
        if (!bn_wexpand(r, p->top + q->top + 1)) {
-               ENGINEerr(ENGINE_F_UBSEC_RSA_MOD_EXP_CRT, ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
                return 0;
        }
 
        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
                return 0;
        }
 
@@ -487,7 +682,7 @@ static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                (unsigned char *)dq->d, BN_num_bits(dq),
                (unsigned char *)q->d, BN_num_bits(q),
                (unsigned char *)r->d,  &y_len) != 0) {
-               ENGINEerr(ENGINE_F_UBSEC_MOD_EXP, ENGINE_R_REQUEST_FAILED);
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
                return 0;
        }
 
@@ -497,6 +692,7 @@ static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        return 1;
 }
 
+#ifndef OPENSSL_NO_DSA
 #if NOT_USED
 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
                BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
@@ -525,26 +721,33 @@ static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
        return ubsec_mod_exp(r, a, p, m, ctx);
        }
 #endif
+#endif
 
 /*
  * This function is aliased to mod_exp (with the mont stuff dropped).
  */
 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
-{
+        {
        int ret = 0;
 
+#ifndef OPENSSL_NO_RSA
        /* Do in software if the key is too large for the hardware. */
-       if (BN_num_bits(m) > 1024) {
+       if (BN_num_bits(m) > 1024)
+                {
                const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
                ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
-       } else {
+                }
+        else
+#endif
+                {
                ret = ubsec_mod_exp(r, a, p, m, ctx);
-       }
+                }
        
        return ret;
-}
+        }
 
+#ifndef OPENSSL_NO_DH
 /* This function is aliased to mod_exp (with the dh and mont dropped). */
 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
                const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
@@ -552,7 +755,9 @@ static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
        {
        return ubsec_mod_exp(r, a, p, m, ctx);
        }
+#endif
 
+#ifndef OPENSSL_NO_DSA
 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
        {
        DSA_SIG *to_return = NULL;
@@ -570,18 +775,18 @@ static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
 
         if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
                   (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
                goto err;
        }
 
        if (BN_bin2bn(dgst,dlen,&m) == NULL) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
                goto err;
        } 
 
        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
                return 0;
        }
 
@@ -594,7 +799,7 @@ static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
                (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
                (unsigned char *)r->d, &r_len,
                (unsigned char *)s->d, &s_len ) != 0) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_REQUEST_FAILED);
+               UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
                goto err;
        }
 
@@ -605,7 +810,7 @@ static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
 
        to_return = DSA_SIG_new();
        if(to_return == NULL) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_SIGN, ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
                goto err;
        }
 
@@ -632,7 +837,7 @@ static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
        BN_init(&v);
 
        if(!bn_wexpand(&v, dsa->p->top)) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_VERIFY ,ENGINE_R_BN_EXPAND_FAIL);
+               UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
                goto err;
        }
 
@@ -642,7 +847,7 @@ static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
 
        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
                return 0;
        }
 
@@ -655,7 +860,7 @@ static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
                (unsigned char *)sig->r->d, BN_num_bits(sig->r),
                (unsigned char *)sig->s->d, BN_num_bits(sig->s),
                (unsigned char *)v.d, &v_len) != 0) {
-               ENGINEerr(ENGINE_F_UBSEC_DSA_VERIFY , ENGINE_R_REQUEST_FAILED);
+               UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
                goto err;
        }
 
@@ -666,7 +871,9 @@ err:
        BN_clear_free(&v);
        return to_return;
        }
+#endif
 
+#ifndef OPENSSL_NO_DH
 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
        {
        return 0;
@@ -676,6 +883,7 @@ static int ubsec_dh_generate_key (DH *dh)
        {
        return 0;
        }
+#endif
 
 #ifdef NOT_USED
 static int ubsec_rand_bytes(unsigned char *buf, int num)
@@ -689,5 +897,20 @@ static int ubsec_rand_status(void)
        }
 #endif
 
+/* This stuff is needed if this ENGINE is being compiled into a self-contained
+ * shared-library. */
+#ifdef ENGINE_DYNAMIC_SUPPORT
+static int bind_fn(ENGINE *e, const char *id)
+       {
+       if(id && (strcmp(id, engine_ubsec_id) != 0))
+               return 0;
+       if(!bind_helper(e))
+               return 0;
+       return 1;
+       }
+IMPLEMENT_DYNAMIC_CHECK_FN()
+IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
+#endif /* ENGINE_DYNAMIC_SUPPORT */
+
 #endif /* !OPENSSL_NO_HW_UBSEC */
 #endif /* !OPENSSL_NO_HW */