Check the return values where memory allocation failures may happen.
[openssl.git] / crypto / engine / hw_ubsec.c
index f8f1d4127c299052aa35243f7ae181da47d74072..743c06043c72a9ee647379fc34d51d59d45f418a 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
 #include "vendor_defns/hw_ubsec.h"
 #endif
 
+#define UBSEC_LIB_NAME "ubsec engine"
+#include "hw_ubsec_err.c"
+
+#define FAIL_TO_SOFTWARE -15
+
+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)());
@@ -177,9 +183,9 @@ static DH_METHOD ubsec_dh =
 static const char *engine_ubsec_id = "ubsec";
 static const char *engine_ubsec_name = "UBSEC hardware engine support";
 
-/* 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()
+/* 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;
@@ -189,30 +195,23 @@ ENGINE *ENGINE_ubsec()
        const DH_METHOD *meth3;
 #endif /* HAVE_UBSEC_DH */
 #endif
-       ENGINE *ret = ENGINE_new();
-       if(!ret)
-               return NULL;
-       if(!ENGINE_set_id(ret, engine_ubsec_id) ||
-                       !ENGINE_set_name(ret, engine_ubsec_name) ||
+       if(!ENGINE_set_id(e, engine_ubsec_id) ||
+                       !ENGINE_set_name(e, engine_ubsec_name) ||
 #ifndef OPENSSL_NO_RSA
-                       !ENGINE_set_RSA(ret, &ubsec_rsa) ||
+                       !ENGINE_set_RSA(e, &ubsec_rsa) ||
 #endif
 #ifndef OPENSSL_NO_DSA
-                       !ENGINE_set_DSA(ret, &ubsec_dsa) ||
+                       !ENGINE_set_DSA(e, &ubsec_dsa) ||
 #endif
 #ifndef OPENSSL_NO_DH
-                       !ENGINE_set_DH(ret, &ubsec_dh) ||
-#endif
-                       !ENGINE_set_BN_mod_exp(ret, ubsec_mod_exp) ||
-                       !ENGINE_set_BN_mod_exp_crt(ret, ubsec_mod_exp_crt) ||
-                       !ENGINE_set_init_function(ret, ubsec_init) ||
-                       !ENGINE_set_finish_function(ret, ubsec_finish) ||
-                       !ENGINE_set_ctrl_function(ret, ubsec_ctrl) ||
-                       !ENGINE_set_cmd_defns(ret, ubsec_cmd_defns))
-               {
-               ENGINE_free(ret);
-               return NULL;
-               }
+                       !ENGINE_set_DH(e, &ubsec_dh) ||
+#endif
+                       !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
@@ -238,9 +237,34 @@ ENGINE *ENGINE_ubsec()
 #endif /* HAVE_UBSEC_DH */
 #endif
 
+       /* Ensure the ubsec error handling is set up */
+       ERR_load_UBSEC_strings();
+       return 1;
+       }
+
+static ENGINE *engine_ubsec(void)
+       {
+       ENGINE *ret = ENGINE_new();
+       if(!ret)
+               return NULL;
+       if(!bind_helper(ret))
+               {
+               ENGINE_free(ret);
+               return NULL;
+               }
        return ret;
        }
 
+void ENGINE_load_ubsec(void)
+       {
+       /* Copied from eng_[openssl|dyn].c */
+       ENGINE *toadd = engine_ubsec();
+       if(!toadd) return;
+       ENGINE_add(toadd);
+       ENGINE_free(toadd);
+       ERR_clear_error();
+       }
+
 /* This is a process-global DSO handle used for loading and unloading
  * the UBSEC library. NB: This is only set (or unset) during an
  * init() or finish() call (reference counts permitting) and they're
@@ -271,6 +295,9 @@ 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;
+static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
+
+static int max_key_len = 1024;  /* ??? */
 
 /* 
  * These are the static string constants for the DSO file name and the function
@@ -296,6 +323,14 @@ static const char *UBSEC_F10 = "dsa_verify_ioctl";
 #endif
 static const char *UBSEC_F11 = "math_accelerate_ioctl";
 static const char *UBSEC_F12 = "rng_ioctl";
+static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
+
+/* Destructor (complements the "ENGINE_ubsec()" constructor) */
+static int ubsec_destroy(ENGINE *e)
+       {
+       ERR_unload_UBSEC_strings();
+       return 1;
+       }
 
 /* (de)initialisation functions. */
 static int ubsec_init(ENGINE *e)
@@ -318,11 +353,12 @@ static int ubsec_init(ENGINE *e)
 #endif
        t_UBSEC_math_accelerate_ioctl *p11;
        t_UBSEC_rng_ioctl *p12;
+        t_UBSEC_max_key_len_ioctl *p13;
        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;
                }
        /* 
@@ -331,7 +367,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;
                }
 
@@ -356,9 +392,10 @@ static int ubsec_init(ENGINE *e)
 #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)))
+       !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
+        !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
                {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_DSO_FAILURE);
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
                goto err;
                }
 
@@ -381,17 +418,18 @@ static int ubsec_init(ENGINE *e)
 #endif
        p_UBSEC_math_accelerate_ioctl = p11;
        p_UBSEC_rng_ioctl = p12;
+        p_UBSEC_max_key_len_ioctl = p13;
 
        /* Perform an open to see if there's actually any unit running. */
-       if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
-               {
-               p_UBSEC_ubsec_close(fd);
-               return 1;
-               }
+       if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
+       {
+          p_UBSEC_ubsec_close(fd);
+          return 1;
+       }
        else
-               {
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
-               }
+       {
+         UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+       }
 
 err:
        if(ubsec_dso)
@@ -414,6 +452,7 @@ err:
 #endif
        p_UBSEC_math_accelerate_ioctl = NULL;
        p_UBSEC_rng_ioctl = NULL;
+        p_UBSEC_max_key_len_ioctl = NULL;
 
        return 0;
        }
@@ -422,12 +461,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;
@@ -449,6 +488,7 @@ static int ubsec_finish(ENGINE *e)
 #endif
        p_UBSEC_math_accelerate_ioctl = NULL;
        p_UBSEC_rng_ioctl = NULL;
+        p_UBSEC_max_key_len_ioctl = NULL;
        return 1;
        }
 
@@ -460,12 +500,12 @@ static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
        case UBSEC_CMD_SO_PATH:
                if(p == NULL)
                        {
-                       ENGINEerr(ENGINE_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
+                       UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
                        return 0;
                        }
                if(initialised)
                        {
-                       ENGINEerr(ENGINE_F_UBSEC_CTRL,ENGINE_R_ALREADY_LOADED);
+                       UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
                        return 0;
                        }
                UBSEC_LIBNAME = (const char *)p;
@@ -473,7 +513,7 @@ static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
        default:
                break;
                }
-       ENGINEerr(ENGINE_F_UBSEC_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
+       UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
        return 0;
        }
 
@@ -485,36 +525,38 @@ 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);
-               return 0;
+       if (y_len > max_key_len) {
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
+                return BN_mod_exp(r, a, p, m, ctx);
        } 
 
        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);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                return BN_mod_exp(r, a, p, m, ctx);
        }
 
        if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
                (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);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+
+                return BN_mod_exp(r, a, p, m, ctx);
        }
 
        p_UBSEC_ubsec_close(fd);
@@ -534,19 +576,19 @@ 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;
                }
 
-       /*
-        * Do in software if argument is too large for hardware.
-        */
-       if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
-               const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
-               to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
-       } else {
-               to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
-                       rsa->dmq1, rsa->iqmp, ctx);
+       to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
+                   rsa->dmq1, rsa->iqmp, ctx);
+       if (to_return == FAIL_TO_SOFTWARE)
+       {
+         /*
+          * Do in software as hardware failed.
+          */
+          const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
+          to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
        }
 err:
        if(ctx)
@@ -567,20 +609,20 @@ static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
        y_len = BN_num_bits(p) + BN_num_bits(q);
 
        /* 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);
-               return 0;
+       if (y_len > max_key_len) {
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
+               return FAIL_TO_SOFTWARE;
        } 
 
        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);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+               return FAIL_TO_SOFTWARE;
        }
 
        if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
@@ -591,8 +633,9 @@ 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);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+               return FAIL_TO_SOFTWARE;
        }
 
        p_UBSEC_ubsec_close(fd);
@@ -642,7 +685,7 @@ static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
 
 #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) > max_key_len)
                 {
                const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
                ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
@@ -684,19 +727,22 @@ 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) {
+                const DSA_METHOD *meth;
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                meth = DSA_OpenSSL();
+                to_return =  meth->dsa_do_sign(dgst, dlen, dsa);
+               goto err;
        }
 
        if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
@@ -708,7 +754,13 @@ 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);
+                const DSA_METHOD *meth;
+
+               UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+                meth = DSA_OpenSSL();
+                to_return = meth->dsa_do_sign(dgst, dlen, dsa);
+
                goto err;
        }
 
@@ -719,7 +771,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;
        }
 
@@ -746,7 +798,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;
        }
 
@@ -755,9 +807,12 @@ static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
        d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
 
        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
+                const DSA_METHOD *meth;
                fd = 0;
-               ENGINEerr(ENGINE_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
-               return 0;
+               UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                meth = DSA_OpenSSL();
+                to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
+               goto err;
        }
 
        if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
@@ -769,7 +824,13 @@ 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);
+                const DSA_METHOD *meth;
+               UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+
+                meth = DSA_OpenSSL();
+                to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
+
                goto err;
        }
 
@@ -784,21 +845,176 @@ err:
 
 #ifndef OPENSSL_NO_DH
 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
-       {
-       return 0;
-       }
+        {
+        int      ret      = -1,
+                 k_len,
+                 fd;
+
+        k_len = BN_num_bits(dh->p);
+
+        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
+                {
+                const DH_METHOD *meth;
+                ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                meth = DH_OpenSSL();
+                ret = meth->compute_key(key, pub_key, dh);
+                goto err;
+                }
+
+        if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
+                                               (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
+                                               (unsigned char *)pub_key->d, BN_num_bits(pub_key),
+                                               (unsigned char *)dh->p->d, BN_num_bits(dh->p),
+                                               key, &k_len) != 0)
+                {
+                /* Hardware's a no go, failover to software */
+                const DH_METHOD *meth;
+                ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+
+                meth = DH_OpenSSL();
+                ret = meth->compute_key(key, pub_key, dh);
+
+                goto err;
+                }
+
+        p_UBSEC_ubsec_close(fd);
+
+        ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
+err:
+        return ret;
+        }
 
 static int ubsec_dh_generate_key (DH *dh)
-       {
-       return 0;
-       }
+        {
+        int      ret               = 0,
+                 random_bits       = 0,
+                 pub_key_len       = 0,
+                 priv_key_len      = 0,
+                 fd;
+        BIGNUM   *pub_key          = NULL;
+        BIGNUM   *priv_key         = NULL;
+
+        /* 
+         *  How many bits should Random x be? dh_key.c
+         *  sets the range from 0 to num_bits(modulus) ???
+         */
+
+        if (dh->priv_key == NULL)
+                {
+                priv_key = BN_new();
+                if (priv_key == NULL) goto err;
+                priv_key_len = BN_num_bits(dh->p);
+                bn_wexpand(priv_key, dh->p->top);
+                do
+                        if (!BN_rand_range(priv_key, dh->p)) goto err;
+                while (BN_is_zero(priv_key));
+                random_bits = BN_num_bits(priv_key);
+                }
+        else
+                {
+                priv_key = dh->priv_key;
+                }
+
+        if (dh->pub_key == NULL)
+                {
+                pub_key = BN_new();
+                pub_key_len = BN_num_bits(dh->p);
+                bn_wexpand(pub_key, dh->p->top);
+                if(pub_key == NULL) goto err;
+                }
+        else
+                {
+                pub_key = dh->pub_key;
+                }
+
+        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
+                {
+                const DH_METHOD *meth;
+                ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                meth = DH_OpenSSL();
+                ret = meth->generate_key(dh);
+                goto err;
+                }
+
+        if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
+                                                  (unsigned char *)priv_key->d, &priv_key_len,
+                                                  (unsigned char *)pub_key->d,  &pub_key_len,
+                                                  (unsigned char *)dh->g->d, BN_num_bits(dh->g),
+                                                  (unsigned char *)dh->p->d, BN_num_bits(dh->p),
+                                                  0, 0, random_bits) != 0)
+                {
+                /* Hardware's a no go, failover to software */
+                const DH_METHOD *meth;
+
+                ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+
+                meth = DH_OpenSSL();
+                ret = meth->generate_key(dh);
+
+                goto err;
+                }
+
+        p_UBSEC_ubsec_close(fd);
+
+        dh->pub_key = pub_key;
+        dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
+        dh->priv_key = priv_key;
+        dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
+
+        ret = 1;
+err:
+        return ret;
+        }
 #endif
 
-#ifdef NOT_USED
-static int ubsec_rand_bytes(unsigned char *buf, int num)
-       {
-       return 0;
-       }
+#if NOT_USED
+static int ubsec_rand_bytes(unsigned char * buf,
+                            int num)
+        {
+        int      ret      = 0,
+                 fd;
+
+        if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
+                {
+                const RAND_METHOD *meth;
+                ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
+                num = p_UBSEC_ubsec_bits_to_bytes(num);
+                meth = RAND_SSLeay();
+                meth->seed(buf, num);
+                ret = meth->bytes(buf, num);
+                goto err;
+                }
+
+        num *= 8; /* bytes to bits */
+
+        if (p_UBSEC_rng_ioctl(fd,
+                              UBSEC_RNG_DIRECT,
+                              buf,
+                              &num) != 0)
+                {
+                /* Hardware's a no go, failover to software */
+                const RAND_METHOD *meth;
+
+                ENGINEerr(UBSEC_F_UBSEC_RNG_BYTES, UBSEC_R_REQUEST_FAILED);
+                p_UBSEC_ubsec_close(fd);
+
+                num = p_UBSEC_ubsec_bits_to_bytes(num);
+                meth = RAND_SSLeay();
+                meth->seed(buf, num);
+                ret = meth->bytes(buf, num);
+
+                goto err;
+                }
+
+        p_UBSEC_ubsec_close(fd);
+
+        ret = 1;
+err:
+        return(ret);
+        }
+
 
 static int ubsec_rand_status(void)
        {
@@ -806,5 +1022,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 */