This change adds a new ENGINE called "dynamic" that allows new ENGINE
[openssl.git] / crypto / engine / hw_ubsec.c
index a05211f27ab4dbf4a4398a43b48bc2ab211b6080..f8f1d4127c299052aa35243f7ae181da47d74072 100644 (file)
 
 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,
@@ -94,19 +98,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",
@@ -123,7 +139,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 =
        {
@@ -138,7 +156,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 =
        {
@@ -151,6 +171,7 @@ static DH_METHOD ubsec_dh =
        0,
        NULL
        };
+#endif
 
 /* Constants used when creating the ENGINE */
 static const char *engine_ubsec_id = "ubsec";
@@ -160,27 +181,40 @@ static const char *engine_ubsec_name = "UBSEC hardware engine support";
  * (indeed - the lock will already be held by our caller!!!) */
 ENGINE *ENGINE_ubsec()
        {
+#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
        ENGINE *ret = ENGINE_new();
        if(!ret)
                return NULL;
        if(!ENGINE_set_id(ret, engine_ubsec_id) ||
                        !ENGINE_set_name(ret, engine_ubsec_name) ||
+#ifndef OPENSSL_NO_RSA
                        !ENGINE_set_RSA(ret, &ubsec_rsa) ||
+#endif
+#ifndef OPENSSL_NO_DSA
                        !ENGINE_set_DSA(ret, &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_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;
                }
 
+#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
@@ -193,13 +227,16 @@ 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
 
        return ret;
        }
@@ -219,13 +256,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;
 
@@ -239,12 +282,18 @@ 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";
 
@@ -255,12 +304,18 @@ 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;
@@ -285,14 +340,20 @@ 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)))
@@ -306,12 +367,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;
 
@@ -333,12 +400,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;
 
@@ -362,17 +435,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)
+                       {
+                       ENGINEerr(ENGINE_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
+                       return 0;
+                       }
+               if(initialised)
+                       {
+                       ENGINEerr(ENGINE_F_UBSEC_CTRL,ENGINE_R_ALREADY_LOADED);
+                       return 0;
+                       }
+               UBSEC_LIBNAME = (const char *)p;
+               return 1;
+       default:
+               break;
+               }
+       ENGINEerr(ENGINE_F_UBSEC_CTRL,ENGINE_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)
        {
@@ -419,6 +523,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;
@@ -448,6 +553,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,
@@ -495,6 +601,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,
@@ -523,26 +630,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,
@@ -550,7 +664,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;
@@ -664,7 +780,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;
@@ -674,6 +792,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)