On solaris, the variable name sun clashes, use s_un instead
[openssl.git] / crypto / ec / ec_kmeth.c
index 688940af6f8bbcda9884172b803de79b51ac9191..51992aff433b504cb9db3e6bb20c30093ff1744d 100644 (file)
@@ -1,4 +1,3 @@
-/* crypto/ec/ec_kmeth.c */
 /*
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project.
@@ -73,7 +72,7 @@ static const EC_KEY_METHOD openssl_ec_key_method = {
     ossl_ecdsa_verify_sig
 };
 
-const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
+static const EC_KEY_METHOD *default_ec_key_meth = &openssl_ec_key_method;
 
 const EC_KEY_METHOD *EC_KEY_OpenSSL(void)
 {
@@ -93,6 +92,31 @@ void EC_KEY_set_default_method(const EC_KEY_METHOD *meth)
         default_ec_key_meth = meth;
 }
 
+const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key)
+{
+    return key->meth;
+}
+
+int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth)
+{
+    void (*finish)(EC_KEY *key) = key->meth->finish;
+
+    if (finish != NULL)
+        finish(key);
+
+#ifndef OPENSSL_NO_ENGINE
+    if (key->engine != NULL) {
+        ENGINE_finish(key->engine);
+        key->engine = NULL;
+    }
+#endif
+
+    key->meth = meth;
+    if (meth->init != NULL)
+        return meth->init(key);
+    return 1;
+}
+
 EC_KEY *EC_KEY_new_method(ENGINE *engine)
 {
     EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
@@ -101,9 +125,14 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
+    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
+        OPENSSL_free(ret);
+        return NULL;
+    }
+
     ret->meth = EC_KEY_get_default_method();
 #ifndef OPENSSL_NO_ENGINE
-    if (engine) {
+    if (engine != NULL) {
         if (!ENGINE_init(engine)) {
             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
             OPENSSL_free(ret);
@@ -112,9 +141,9 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
         ret->engine = engine;
     } else
         ret->engine = ENGINE_get_default_EC();
-    if (ret->engine) {
+    if (ret->engine != NULL) {
         ret->meth = ENGINE_get_EC(ret->engine);
-        if (!ret->meth) {
+        if (ret->meth == NULL) {
             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
             ENGINE_finish(ret->engine);
             OPENSSL_free(ret);
@@ -126,7 +155,7 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
     ret->version = 1;
     ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
     ret->references = 1;
-    if (ret->meth->init && ret->meth->init(ret) == 0) {
+    if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
         EC_KEY_free(ret);
         return NULL;
     }
@@ -134,11 +163,11 @@ EC_KEY *EC_KEY_new_method(ENGINE *engine)
 }
 
 int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
-                     EC_KEY *eckey,
+                     const EC_KEY *eckey,
                      void *(*KDF) (const void *in, size_t inlen, void *out,
                                    size_t *outlen))
 {
-    if (eckey->meth->compute_key)
+    if (eckey->meth->compute_key != NULL)
         return eckey->meth->compute_key(out, outlen, pub_key, eckey, KDF);
     ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
     return 0;
@@ -146,11 +175,11 @@ int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
 
 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth)
 {
-    EC_KEY_METHOD *ret;
-    ret = OPENSSL_zalloc(sizeof(*meth));
+    EC_KEY_METHOD *ret = OPENSSL_zalloc(sizeof(*meth));
+
     if (ret == NULL)
         return NULL;
-    if (meth)
+    if (meth != NULL)
         *ret = *meth;
     ret->flags |= EC_KEY_METHOD_DYNAMIC;
     return ret;
@@ -190,7 +219,7 @@ void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
                                    int (*ckey)(void *out,
                                                size_t outlen,
                                                const EC_POINT *pub_key,
-                                               EC_KEY *ecdh,
+                                               const EC_KEY *ecdh,
                                                void *(*KDF) (const void *in,
                                                              size_t inlen,
                                                              void *out,
@@ -268,7 +297,7 @@ void EC_KEY_METHOD_get_compute_key(EC_KEY_METHOD *meth,
                                    int (**pck)(void *out,
                                                size_t outlen,
                                                const EC_POINT *pub_key,
-                                               EC_KEY *ecdh,
+                                               const EC_KEY *ecdh,
                                                void *(*KDF) (const void *in,
                                                              size_t inlen,
                                                              void *out,