Migrated remaining classes in openssl plugin to INIT/METHOD macros
authorMartin Willi <martin@revosec.ch>
Tue, 10 Aug 2010 12:22:10 +0000 (14:22 +0200)
committerMartin Willi <martin@revosec.ch>
Tue, 10 Aug 2010 16:46:30 +0000 (18:46 +0200)
15 files changed:
src/libstrongswan/plugins/openssl/openssl_crypter.c
src/libstrongswan/plugins/openssl/openssl_crypter.h
src/libstrongswan/plugins/openssl/openssl_diffie_hellman.c
src/libstrongswan/plugins/openssl/openssl_ec_diffie_hellman.c
src/libstrongswan/plugins/openssl/openssl_ec_private_key.c
src/libstrongswan/plugins/openssl/openssl_ec_private_key.h
src/libstrongswan/plugins/openssl/openssl_ec_public_key.c
src/libstrongswan/plugins/openssl/openssl_ec_public_key.h
src/libstrongswan/plugins/openssl/openssl_hasher.c
src/libstrongswan/plugins/openssl/openssl_hasher.h
src/libstrongswan/plugins/openssl/openssl_plugin.c
src/libstrongswan/plugins/openssl/openssl_rsa_private_key.c
src/libstrongswan/plugins/openssl/openssl_rsa_private_key.h
src/libstrongswan/plugins/openssl/openssl_rsa_public_key.c
src/libstrongswan/plugins/openssl/openssl_rsa_public_key.h

index a8923ab..520b232 100644 (file)
@@ -118,8 +118,11 @@ static char* lookup_algorithm(openssl_algorithm_t *openssl_algo,
        return NULL;
 }
 
-static void crypt(private_openssl_crypter_t *this, chunk_t data,
-                                       chunk_t iv, chunk_t *dst, int enc)
+/**
+ * Do the actual en/decryption in an EVP context
+ */
+static void crypt(private_openssl_crypter_t *this, chunk_t data, chunk_t iv,
+                                 chunk_t *dst, int enc)
 {
        int len;
        u_char *out;
@@ -141,53 +144,38 @@ static void crypt(private_openssl_crypter_t *this, chunk_t data,
        EVP_CIPHER_CTX_cleanup(&ctx);
 }
 
-/**
- * Implementation of crypter_t.decrypt.
- */
-static void decrypt(private_openssl_crypter_t *this, chunk_t data,
-                                               chunk_t iv, chunk_t *dst)
+METHOD(crypter_t, decrypt, void,
+       private_openssl_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
 {
        crypt(this, data, iv, dst, 0);
 }
 
-
-/**
- * Implementation of crypter_t.encrypt.
- */
-static void encrypt (private_openssl_crypter_t *this, chunk_t data,
-                                                       chunk_t iv, chunk_t *dst)
+METHOD(crypter_t, encrypt, void,
+       private_openssl_crypter_t *this, chunk_t data, chunk_t iv, chunk_t *dst)
 {
        crypt(this, data, iv, dst, 1);
 }
 
-/**
- * Implementation of crypter_t.get_block_size.
- */
-static size_t get_block_size(private_openssl_crypter_t *this)
+METHOD(crypter_t, get_block_size, size_t,
+       private_openssl_crypter_t *this)
 {
        return this->cipher->block_size;
 }
 
-/**
- * Implementation of crypter_t.get_key_size.
- */
-static size_t get_key_size(private_openssl_crypter_t *this)
+METHOD(crypter_t, get_key_size, size_t,
+       private_openssl_crypter_t *this)
 {
        return this->key.len;
 }
 
-/**
- * Implementation of crypter_t.set_key.
- */
-static void set_key(private_openssl_crypter_t *this, chunk_t key)
+METHOD(crypter_t, set_key, void,
+       private_openssl_crypter_t *this, chunk_t key)
 {
        memcpy(this->key.ptr, key.ptr, min(key.len, this->key.len));
 }
 
-/**
- * Implementation of crypter_t.destroy.
- */
-static void destroy (private_openssl_crypter_t *this)
+METHOD(crypter_t, destroy, void,
+       private_openssl_crypter_t *this)
 {
        free(this->key.ptr);
        free(this);
@@ -201,7 +189,16 @@ openssl_crypter_t *openssl_crypter_create(encryption_algorithm_t algo,
 {
        private_openssl_crypter_t *this;
 
-       this = malloc_thing(private_openssl_crypter_t);
+       INIT(this,
+               .public.crypter = {
+                       .encrypt = _encrypt,
+                       .decrypt = _decrypt,
+                       .get_block_size = _get_block_size,
+                       .get_key_size = _get_key_size,
+                       .set_key = _set_key,
+                       .destroy = _destroy,
+               },
+       );
 
        switch (algo)
        {
@@ -268,12 +265,5 @@ openssl_crypter_t *openssl_crypter_create(encryption_algorithm_t algo,
 
        this->key = chunk_alloc(key_size);
 
-       this->public.crypter_interface.encrypt = (void (*) (crypter_t *, chunk_t,chunk_t, chunk_t *)) encrypt;
-       this->public.crypter_interface.decrypt = (void (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt;
-       this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *)) get_block_size;
-       this->public.crypter_interface.get_key_size = (size_t (*) (crypter_t *)) get_key_size;
-       this->public.crypter_interface.set_key = (void (*) (crypter_t *,chunk_t)) set_key;
-       this->public.crypter_interface.destroy = (void (*) (crypter_t *)) destroy;
-
        return &this->public;
 }
index 7e30ae0..b12e7a6 100644 (file)
@@ -31,9 +31,9 @@ typedef struct openssl_crypter_t openssl_crypter_t;
 struct openssl_crypter_t {
 
        /**
-        * The crypter_t interface.
+        * Implements crypter_t interface.
         */
-       crypter_t crypter_interface;
+       crypter_t crypter;
 };
 
 /**
index 9a032c5..ff49a2d 100644 (file)
@@ -57,11 +57,8 @@ struct private_openssl_diffie_hellman_t {
        bool computed;
 };
 
-/**
- * Implementation of openssl_diffie_hellman_t.get_my_public_value.
- */
-static void get_my_public_value(private_openssl_diffie_hellman_t *this,
-                                                               chunk_t *value)
+METHOD(diffie_hellman_t, get_my_public_value, void,
+       private_openssl_diffie_hellman_t *this, chunk_t *value)
 {
        *value = chunk_alloc(DH_size(this->dh));
        memset(value->ptr, 0, value->len);
@@ -69,11 +66,8 @@ static void get_my_public_value(private_openssl_diffie_hellman_t *this,
                          value->ptr + value->len - BN_num_bytes(this->dh->pub_key));
 }
 
-/**
- * Implementation of openssl_diffie_hellman_t.get_shared_secret.
- */
-static status_t get_shared_secret(private_openssl_diffie_hellman_t *this,
-                                                                 chunk_t *secret)
+METHOD(diffie_hellman_t, get_shared_secret, status_t,
+       private_openssl_diffie_hellman_t *this, chunk_t *secret)
 {
        if (!this->computed)
        {
@@ -88,11 +82,8 @@ static status_t get_shared_secret(private_openssl_diffie_hellman_t *this,
 }
 
 
-/**
- * Implementation of openssl_diffie_hellman_t.set_other_public_value.
- */
-static void set_other_public_value(private_openssl_diffie_hellman_t *this,
-                                                                  chunk_t value)
+METHOD(diffie_hellman_t, set_other_public_value, void,
+       private_openssl_diffie_hellman_t *this, chunk_t value)
 {
        int len;
 
@@ -110,10 +101,8 @@ static void set_other_public_value(private_openssl_diffie_hellman_t *this,
        this->computed = TRUE;
 }
 
-/**
- * Implementation of openssl_diffie_hellman_t.get_dh_group.
- */
-static diffie_hellman_group_t get_dh_group(private_openssl_diffie_hellman_t *this)
+METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
+       private_openssl_diffie_hellman_t *this)
 {
        return this->group;
 }
@@ -137,10 +126,8 @@ static status_t set_modulus(private_openssl_diffie_hellman_t *this)
        return SUCCESS;
 }
 
-/**
- * Implementation of openssl_diffie_hellman_t.destroy.
- */
-static void destroy(private_openssl_diffie_hellman_t *this)
+METHOD(diffie_hellman_t, destroy, void,
+       private_openssl_diffie_hellman_t *this)
 {
        BN_clear_free(this->pub_key);
        DH_free(this->dh);
@@ -153,13 +140,17 @@ static void destroy(private_openssl_diffie_hellman_t *this)
  */
 openssl_diffie_hellman_t *openssl_diffie_hellman_create(diffie_hellman_group_t group)
 {
-       private_openssl_diffie_hellman_t *this = malloc_thing(private_openssl_diffie_hellman_t);
-
-       this->public.dh.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;
-       this->public.dh.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;
-       this->public.dh.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;
-       this->public.dh.get_dh_group = (diffie_hellman_group_t (*)(diffie_hellman_t *)) get_dh_group;
-       this->public.dh.destroy = (void (*)(diffie_hellman_t *)) destroy;
+       private_openssl_diffie_hellman_t *this;
+
+       INIT(this,
+               .public.dh = {
+                       .get_shared_secret = _get_shared_secret,
+                       .set_other_public_value = _set_other_public_value,
+                       .get_my_public_value = _get_my_public_value,
+                       .get_dh_group = _get_dh_group,
+                       .destroy = _destroy,
+               },
+       );
 
        this->dh = DH_new();
        if (!this->dh)
index a53e8ae..2a856ce 100644 (file)
@@ -165,7 +165,8 @@ error:
  *   of the Diffie-Hellman shared secret value is the same as that of the
  *   Diffie-Hellman public value."
  */
-static bool compute_shared_key(private_openssl_ec_diffie_hellman_t *this, chunk_t *shared_secret)
+static bool compute_shared_key(private_openssl_ec_diffie_hellman_t *this,
+                                                          chunk_t *shared_secret)
 {
        const BIGNUM *priv_key;
        EC_POINT *secret = NULL;
@@ -209,10 +210,8 @@ error:
        return ret;
 }
 
-/**
- * Implementation of openssl_ec_diffie_hellman_t.set_other_public_value.
- */
-static void set_other_public_value(private_openssl_ec_diffie_hellman_t *this, chunk_t value)
+METHOD(diffie_hellman_t, set_other_public_value, void,
+       private_openssl_ec_diffie_hellman_t *this, chunk_t value)
 {
        if (!chunk2ecp(this->ec_group, value, this->pub_key))
        {
@@ -230,18 +229,14 @@ static void set_other_public_value(private_openssl_ec_diffie_hellman_t *this, ch
        this->computed = TRUE;
 }
 
-/**
- * Implementation of openssl_ec_diffie_hellman_t.get_my_public_value.
- */
-static void get_my_public_value(private_openssl_ec_diffie_hellman_t *this,chunk_t *value)
+METHOD(diffie_hellman_t, get_my_public_value, void,
+       private_openssl_ec_diffie_hellman_t *this,chunk_t *value)
 {
        ecp2chunk(this->ec_group, EC_KEY_get0_public_key(this->key), value, FALSE);
 }
 
-/**
- * Implementation of openssl_ec_diffie_hellman_t.get_shared_secret.
- */
-static status_t get_shared_secret(private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
+METHOD(diffie_hellman_t, get_shared_secret, status_t,
+       private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
 {
        if (!this->computed)
        {
@@ -251,18 +246,14 @@ static status_t get_shared_secret(private_openssl_ec_diffie_hellman_t *this, chu
        return SUCCESS;
 }
 
-/**
- * Implementation of openssl_ec_diffie_hellman_t.get_dh_group.
- */
-static diffie_hellman_group_t get_dh_group(private_openssl_ec_diffie_hellman_t *this)
+METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
+       private_openssl_ec_diffie_hellman_t *this)
 {
        return this->group;
 }
 
-/**
- * Implementation of openssl_ec_diffie_hellman_t.destroy.
- */
-static void destroy(private_openssl_ec_diffie_hellman_t *this)
+METHOD(diffie_hellman_t, destroy, void,
+       private_openssl_ec_diffie_hellman_t *this)
 {
        EC_POINT_clear_free(this->pub_key);
        EC_KEY_free(this->key);
@@ -275,13 +266,18 @@ static void destroy(private_openssl_ec_diffie_hellman_t *this)
  */
 openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
 {
-       private_openssl_ec_diffie_hellman_t *this = malloc_thing(private_openssl_ec_diffie_hellman_t);
-
-       this->public.dh.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;
-       this->public.dh.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;
-       this->public.dh.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;
-       this->public.dh.get_dh_group = (diffie_hellman_group_t (*)(diffie_hellman_t *)) get_dh_group;
-       this->public.dh.destroy = (void (*)(diffie_hellman_t *)) destroy;
+       private_openssl_ec_diffie_hellman_t *this;
+
+       INIT(this,
+               .public.dh = {
+                       .get_shared_secret = _get_shared_secret,
+                       .set_other_public_value = _set_other_public_value,
+                       .get_my_public_value = _get_my_public_value,
+                       .get_dh_group = _get_dh_group,
+                       .destroy = _destroy,
+               },
+               .group = group,
+       );
 
        switch (group)
        {
@@ -328,11 +324,6 @@ openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_gro
                return NULL;
        }
 
-       this->group = group;
-       this->computed = FALSE;
-
-       this->shared_secret = chunk_empty;
-
        return &this->public;
 }
 #endif /* OPENSSL_NO_EC */
index 2811559..1b5a24f 100644 (file)
@@ -138,11 +138,9 @@ static bool build_der_signature(private_openssl_ec_private_key_t *this,
        return built;
 }
 
-/**
- * Implementation of private_key_t.sign.
- */
-static bool sign(private_openssl_ec_private_key_t *this,
-                                signature_scheme_t scheme, chunk_t data, chunk_t *signature)
+METHOD(private_key_t, sign, bool,
+       private_openssl_ec_private_key_t *this, signature_scheme_t scheme,
+       chunk_t data, chunk_t *signature)
 {
        switch (scheme)
        {
@@ -172,36 +170,27 @@ static bool sign(private_openssl_ec_private_key_t *this,
        }
 }
 
-/**
- * Implementation of private_key_t.destroy.
- */
-static bool decrypt(private_openssl_ec_private_key_t *this,
-                                       chunk_t crypto, chunk_t *plain)
+METHOD(private_key_t, decrypt, bool,
+       private_openssl_ec_private_key_t *this, chunk_t crypto, chunk_t *plain)
 {
        DBG1(DBG_LIB, "EC private key decryption not implemented");
        return FALSE;
 }
 
-/**
- * Implementation of private_key_t.get_keysize.
- */
-static size_t get_keysize(private_openssl_ec_private_key_t *this)
+METHOD(private_key_t, get_keysize, size_t,
+       private_openssl_ec_private_key_t *this)
 {
        return EC_FIELD_ELEMENT_LEN(EC_KEY_get0_group(this->ec));
 }
 
-/**
- * Implementation of private_key_t.get_type.
- */
-static key_type_t get_type(private_openssl_ec_private_key_t *this)
+METHOD(private_key_t, get_type, key_type_t,
+       private_openssl_ec_private_key_t *this)
 {
        return KEY_ECDSA;
 }
 
-/**
- * Implementation of private_key_t.get_public_key.
- */
-static public_key_t* get_public_key(private_openssl_ec_private_key_t *this)
+METHOD(private_key_t, get_public_key, public_key_t*,
+       private_openssl_ec_private_key_t *this)
 {
        public_key_t *public;
        chunk_t key;
@@ -217,20 +206,16 @@ static public_key_t* get_public_key(private_openssl_ec_private_key_t *this)
        return public;
 }
 
-/**
- * Implementation of private_key_t.get_fingerprint.
- */
-static bool get_fingerprint(private_openssl_ec_private_key_t *this,
-                                                       cred_encoding_type_t type, chunk_t *fingerprint)
+METHOD(private_key_t, get_fingerprint, bool,
+       private_openssl_ec_private_key_t *this, cred_encoding_type_t type,
+       chunk_t *fingerprint)
 {
        return openssl_ec_fingerprint(this->ec, type, fingerprint);
 }
 
-/**
- * Implementation of private_key_t.get_encoding.
- */
-static bool get_encoding(private_openssl_ec_private_key_t *this,
-                                                cred_encoding_type_t type, chunk_t *encoding)
+METHOD(private_key_t, get_encoding, bool,
+       private_openssl_ec_private_key_t *this, cred_encoding_type_t type,
+       chunk_t *encoding)
 {
        u_char *p;
 
@@ -261,19 +246,15 @@ static bool get_encoding(private_openssl_ec_private_key_t *this,
        }
 }
 
-/**
- * Implementation of private_key_t.get_ref.
- */
-static private_key_t* get_ref(private_openssl_ec_private_key_t *this)
+METHOD(private_key_t, get_ref, private_key_t*,
+       private_openssl_ec_private_key_t *this)
 {
        ref_get(&this->ref);
-       return &this->public.interface;
+       return &this->public.key;
 }
 
-/**
- * Implementation of private_key_t.destroy.
- */
-static void destroy(private_openssl_ec_private_key_t *this)
+METHOD(private_key_t, destroy, void,
+       private_openssl_ec_private_key_t *this)
 {
        if (ref_put(&this->ref))
        {
@@ -291,23 +272,25 @@ static void destroy(private_openssl_ec_private_key_t *this)
  */
 static private_openssl_ec_private_key_t *create_empty(void)
 {
-       private_openssl_ec_private_key_t *this = malloc_thing(private_openssl_ec_private_key_t);
-
-       this->public.interface.get_type = (key_type_t (*)(private_key_t *this))get_type;
-       this->public.interface.sign = (bool (*)(private_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t *signature))sign;
-       this->public.interface.decrypt = (bool (*)(private_key_t *this, chunk_t crypto, chunk_t *plain))decrypt;
-       this->public.interface.get_keysize = (size_t (*) (private_key_t *this))get_keysize;
-       this->public.interface.get_public_key = (public_key_t* (*)(private_key_t *this))get_public_key;
-       this->public.interface.equals = private_key_equals;
-       this->public.interface.belongs_to = private_key_belongs_to;
-       this->public.interface.get_fingerprint = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint;
-       this->public.interface.has_fingerprint = (bool(*)(private_key_t*, chunk_t fp))private_key_has_fingerprint;
-       this->public.interface.get_encoding = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding;
-       this->public.interface.get_ref = (private_key_t* (*)(private_key_t *this))get_ref;
-       this->public.interface.destroy = (void (*)(private_key_t *this))destroy;
-
-       this->ec = NULL;
-       this->ref = 1;
+       private_openssl_ec_private_key_t *this;
+
+       INIT(this,
+               .public.key = {
+                       .get_type = _get_type,
+                       .sign = _sign,
+                       .decrypt = _decrypt,
+                       .get_keysize = _get_keysize,
+                       .get_public_key = _get_public_key,
+                       .equals = private_key_equals,
+                       .belongs_to = private_key_belongs_to,
+                       .get_fingerprint = _get_fingerprint,
+                       .has_fingerprint = private_key_has_fingerprint,
+                       .get_encoding = _get_encoding,
+                       .get_ref = _get_ref,
+                       .destroy = _destroy,
+               },
+               .ref = 1,
+       );
 
        return this;
 }
index 720c63f..f56c95a 100644 (file)
@@ -34,7 +34,7 @@ struct openssl_ec_private_key_t {
        /**
         * Implements private_key_t interface
         */
-       private_key_t interface;
+       private_key_t key;
 };
 
 /**
index def36c9..814e774 100644 (file)
@@ -130,19 +130,15 @@ static bool verify_der_signature(private_openssl_ec_public_key_t *this,
        return valid;
 }
 
-/**
- * Implementation of public_key_t.get_type.
- */
-static key_type_t get_type(private_openssl_ec_public_key_t *this)
+METHOD(public_key_t, get_type, key_type_t,
+       private_openssl_ec_public_key_t *this)
 {
        return KEY_ECDSA;
 }
 
-/**
- * Implementation of public_key_t.verify.
- */
-static bool verify(private_openssl_ec_public_key_t *this,
-                                  signature_scheme_t scheme, chunk_t data, chunk_t signature)
+METHOD(public_key_t, verify, bool,
+       private_openssl_ec_public_key_t *this, signature_scheme_t scheme,
+       chunk_t data, chunk_t signature)
 {
        switch (scheme)
        {
@@ -172,20 +168,15 @@ static bool verify(private_openssl_ec_public_key_t *this,
        }
 }
 
-/**
- * Implementation of public_key_t.get_keysize.
- */
-static bool encrypt_(private_openssl_ec_public_key_t *this,
-                                        chunk_t crypto, chunk_t *plain)
+METHOD(public_key_t, encrypt, bool,
+       private_openssl_ec_public_key_t *this, chunk_t crypto, chunk_t *plain)
 {
        DBG1(DBG_LIB, "EC public key encryption not implemented");
        return FALSE;
 }
 
-/**
- * Implementation of public_key_t.get_keysize.
- */
-static size_t get_keysize(private_openssl_ec_public_key_t *this)
+METHOD(public_key_t, get_keysize, size_t,
+       private_openssl_ec_public_key_t *this)
 {
        return EC_FIELD_ELEMENT_LEN(EC_KEY_get0_group(this->ec));
 }
@@ -232,20 +223,16 @@ bool openssl_ec_fingerprint(EC_KEY *ec, cred_encoding_type_t type, chunk_t *fp)
        return TRUE;
 }
 
-/**
- * Implementation of private_key_t.get_fingerprint.
- */
-static bool get_fingerprint(private_openssl_ec_public_key_t *this,
-                                                       cred_encoding_type_t type, chunk_t *fingerprint)
+METHOD(public_key_t, get_fingerprint, bool,
+       private_openssl_ec_public_key_t *this, cred_encoding_type_t type,
+       chunk_t *fingerprint)
 {
        return openssl_ec_fingerprint(this->ec, type, fingerprint);
 }
 
-/**
- * Implementation of private_key_t.get_encoding.
- */
-static bool get_encoding(private_openssl_ec_public_key_t *this,
-                                                cred_encoding_type_t type, chunk_t *encoding)
+METHOD(public_key_t, get_encoding, bool,
+       private_openssl_ec_public_key_t *this, cred_encoding_type_t type,
+       chunk_t *encoding)
 {
        u_char *p;
 
@@ -276,19 +263,15 @@ static bool get_encoding(private_openssl_ec_public_key_t *this,
        }
 }
 
-/**
- * Implementation of public_key_t.get_ref.
- */
-static public_key_t* get_ref(private_openssl_ec_public_key_t *this)
+METHOD(public_key_t, get_ref, public_key_t*,
+       private_openssl_ec_public_key_t *this)
 {
        ref_get(&this->ref);
-       return &this->public.interface;
+       return &this->public.key;
 }
 
-/**
- * Implementation of openssl_ec_public_key.destroy.
- */
-static void destroy(private_openssl_ec_public_key_t *this)
+METHOD(public_key_t, destroy, void,
+       private_openssl_ec_public_key_t *this)
 {
        if (ref_put(&this->ref))
        {
@@ -306,21 +289,23 @@ static void destroy(private_openssl_ec_public_key_t *this)
  */
 static private_openssl_ec_public_key_t *create_empty()
 {
-       private_openssl_ec_public_key_t *this = malloc_thing(private_openssl_ec_public_key_t);
-
-       this->public.interface.get_type = (key_type_t (*)(public_key_t *this))get_type;
-       this->public.interface.verify = (bool (*)(public_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t signature))verify;
-       this->public.interface.encrypt = (bool (*)(public_key_t *this, chunk_t crypto, chunk_t *plain))encrypt_;
-       this->public.interface.get_keysize = (size_t (*) (public_key_t *this))get_keysize;
-       this->public.interface.equals = public_key_equals;
-       this->public.interface.get_fingerprint = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint;
-       this->public.interface.has_fingerprint = (bool(*)(public_key_t*, chunk_t fp))public_key_has_fingerprint;
-       this->public.interface.get_encoding = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding;
-       this->public.interface.get_ref = (public_key_t* (*)(public_key_t *this))get_ref;
-       this->public.interface.destroy = (void (*)(public_key_t *this))destroy;
-
-       this->ec = NULL;
-       this->ref = 1;
+       private_openssl_ec_public_key_t *this;
+
+       INIT(this,
+               .public.key = {
+                       .get_type = _get_type,
+                       .verify = _verify,
+                       .encrypt = _encrypt,
+                       .get_keysize = _get_keysize,
+                       .equals = public_key_equals,
+                       .get_fingerprint = _get_fingerprint,
+                       .has_fingerprint = public_key_has_fingerprint,
+                       .get_encoding = _get_encoding,
+                       .get_ref = _get_ref,
+                       .destroy = _destroy,
+               },
+               .ref = 1,
+       );
 
        return this;
 }
index 29d607d..8094083 100644 (file)
@@ -34,7 +34,7 @@ struct openssl_ec_public_key_t {
        /**
         * Implements the public_key_t interface
         */
-       public_key_t interface;
+       public_key_t key;
 };
 
 /**
index 7556bc5..8904ae9 100644 (file)
@@ -90,27 +90,20 @@ static char* lookup_algorithm(openssl_algorithm_t *openssl_algo,
        return NULL;
 }
 
-/**
- * Implementation of hasher_t.get_hash_size.
- */
-static size_t get_hash_size(private_openssl_hasher_t *this)
+METHOD(hasher_t, get_hash_size, size_t,
+       private_openssl_hasher_t *this)
 {
        return this->hasher->md_size;
 }
 
-/**
- * Implementation of hasher_t.reset.
- */
-static void reset(private_openssl_hasher_t *this)
+METHOD(hasher_t, reset, void,
+       private_openssl_hasher_t *this)
 {
        EVP_DigestInit_ex(this->ctx, this->hasher, NULL);
 }
 
-/**
- * Implementation of hasher_t.get_hash.
- */
-static void get_hash(private_openssl_hasher_t *this, chunk_t chunk,
-                                        u_int8_t *hash)
+METHOD(hasher_t, get_hash, void,
+       private_openssl_hasher_t *this, chunk_t chunk, u_int8_t *hash)
 {
        EVP_DigestUpdate(this->ctx, chunk.ptr, chunk.len);
        if (hash)
@@ -120,11 +113,8 @@ static void get_hash(private_openssl_hasher_t *this, chunk_t chunk,
        }
 }
 
-/**
- * Implementation of hasher_t.allocate_hash.
- */
-static void allocate_hash(private_openssl_hasher_t *this, chunk_t chunk,
-                                                 chunk_t *hash)
+METHOD(hasher_t, allocate_hash, void,
+       private_openssl_hasher_t *this, chunk_t chunk, chunk_t *hash)
 {
        if (hash)
        {
@@ -137,10 +127,8 @@ static void allocate_hash(private_openssl_hasher_t *this, chunk_t chunk,
        }
 }
 
-/**
- * Implementation of hasher_t.destroy.
- */
-static void destroy (private_openssl_hasher_t *this)
+METHOD(hasher_t, destroy, void,
+       private_openssl_hasher_t *this)
 {
        EVP_MD_CTX_destroy(this->ctx);
        free(this);
@@ -160,7 +148,15 @@ openssl_hasher_t *openssl_hasher_create(hash_algorithm_t algo)
                return NULL;
        }
 
-       this = malloc_thing(private_openssl_hasher_t);
+       INIT(this,
+               .public.hasher = {
+                       .get_hash = _get_hash,
+                       .allocate_hash = _allocate_hash,
+                       .get_hash_size = _get_hash_size,
+                       .reset = _reset,
+                       .destroy = _destroy,
+               },
+       );
 
        this->hasher = EVP_get_digestbyname(name);
        if (!this->hasher)
@@ -170,12 +166,6 @@ openssl_hasher_t *openssl_hasher_create(hash_algorithm_t algo)
                return NULL;
        }
 
-       this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
-       this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
-       this->public.hasher_interface.get_hash_size = (size_t (*) (hasher_t*))get_hash_size;
-       this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
-       this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
-
        this->ctx = EVP_MD_CTX_create();
 
        /* initialization */
index fd7a043..b03f689 100644 (file)
@@ -31,9 +31,9 @@ typedef struct openssl_hasher_t openssl_hasher_t;
 struct openssl_hasher_t {
 
        /**
-        * The hasher_t interface.
+        * Implements hasher_t interface.
         */
-       hasher_t hasher_interface;
+       hasher_t hasher;
 };
 
 /**
index ab08a20..f0a16ea 100644 (file)
@@ -166,10 +166,8 @@ static void threading_cleanup()
        mutex = NULL;
 }
 
-/**
- * Implementation of openssl_plugin_t.destroy
- */
-static void destroy(private_openssl_plugin_t *this)
+METHOD(plugin_t, destroy, void,
+       private_openssl_plugin_t *this)
 {
        lib->crypto->remove_crypter(lib->crypto,
                                        (crypter_constructor_t)openssl_crypter_create);
@@ -218,9 +216,11 @@ static void destroy(private_openssl_plugin_t *this)
  */
 plugin_t *openssl_plugin_create()
 {
-       private_openssl_plugin_t *this = malloc_thing(private_openssl_plugin_t);
+       private_openssl_plugin_t *this;
 
-       this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
+       INIT(this,
+               .public.plugin.destroy = _destroy,
+       );
 
        threading_init();
 
index e50656e..14bb25c 100644 (file)
@@ -131,19 +131,16 @@ error:
        return success;
 }
 
-/**
- * Implementation of openssl_rsa_private_key.get_type.
- */
-static key_type_t get_type(private_openssl_rsa_private_key_t *this)
+
+METHOD(private_key_t, get_type, key_type_t,
+       private_openssl_rsa_private_key_t *this)
 {
        return KEY_RSA;
 }
 
-/**
- * Implementation of openssl_rsa_private_key.sign.
- */
-static bool sign(private_openssl_rsa_private_key_t *this, signature_scheme_t scheme,
-                                chunk_t data, chunk_t *signature)
+METHOD(private_key_t, sign, bool,
+       private_openssl_rsa_private_key_t *this, signature_scheme_t scheme,
+       chunk_t data, chunk_t *signature)
 {
        switch (scheme)
        {
@@ -168,28 +165,21 @@ static bool sign(private_openssl_rsa_private_key_t *this, signature_scheme_t sch
        }
 }
 
-/**
- * Implementation of openssl_rsa_private_key.decrypt.
- */
-static bool decrypt(private_openssl_rsa_private_key_t *this,
-                                       chunk_t crypto, chunk_t *plain)
+METHOD(private_key_t, decrypt, bool,
+       private_openssl_rsa_private_key_t *this, chunk_t crypto, chunk_t *plain)
 {
        DBG1(DBG_LIB, "RSA private key decryption not implemented");
        return FALSE;
 }
 
-/**
- * Implementation of openssl_rsa_private_key.get_keysize.
- */
-static size_t get_keysize(private_openssl_rsa_private_key_t *this)
+METHOD(private_key_t, get_keysize, size_t,
+       private_openssl_rsa_private_key_t *this)
 {
        return RSA_size(this->rsa);
 }
 
-/**
- * Implementation of openssl_rsa_private_key.get_public_key.
- */
-static public_key_t* get_public_key(private_openssl_rsa_private_key_t *this)
+METHOD(private_key_t, get_public_key, public_key_t*,
+       private_openssl_rsa_private_key_t *this)
 {
        chunk_t enc;
        public_key_t *key;
@@ -204,20 +194,16 @@ static public_key_t* get_public_key(private_openssl_rsa_private_key_t *this)
        return key;
 }
 
-/**
- * Implementation of public_key_t.get_fingerprint.
- */
-static bool get_fingerprint(private_openssl_rsa_private_key_t *this,
-                                                       cred_encoding_type_t type, chunk_t *fingerprint)
+METHOD(private_key_t, get_fingerprint, bool,
+       private_openssl_rsa_private_key_t *this, cred_encoding_type_t type,
+       chunk_t *fingerprint)
 {
        return openssl_rsa_fingerprint(this->rsa, type, fingerprint);
 }
 
-/*
- * Implementation of public_key_t.get_encoding.
- */
-static bool get_encoding(private_openssl_rsa_private_key_t *this,
-                                                cred_encoding_type_t type, chunk_t *encoding)
+METHOD(private_key_t, get_encoding, bool,
+       private_openssl_rsa_private_key_t *this, cred_encoding_type_t type,
+       chunk_t *encoding)
 {
        u_char *p;
 
@@ -252,19 +238,15 @@ static bool get_encoding(private_openssl_rsa_private_key_t *this,
        }
 }
 
-/**
- * Implementation of openssl_rsa_private_key.get_ref.
- */
-static private_openssl_rsa_private_key_t* get_ref(private_openssl_rsa_private_key_t *this)
+METHOD(private_key_t, get_ref, private_key_t*,
+       private_openssl_rsa_private_key_t *this)
 {
        ref_get(&this->ref);
-       return this;
+       return &this->public.key;
 }
 
-/**
- * Implementation of openssl_rsa_private_key.destroy.
- */
-static void destroy(private_openssl_rsa_private_key_t *this)
+METHOD(private_key_t, destroy, void,
+       private_openssl_rsa_private_key_t *this)
 {
        if (ref_put(&this->ref))
        {
@@ -280,25 +262,27 @@ static void destroy(private_openssl_rsa_private_key_t *this)
 /**
  * Internal generic constructor
  */
-static private_openssl_rsa_private_key_t *create_empty(void)
+static private_openssl_rsa_private_key_t *create_empty()
 {
-       private_openssl_rsa_private_key_t *this = malloc_thing(private_openssl_rsa_private_key_t);
-
-       this->public.interface.get_type = (key_type_t (*) (private_key_t*))get_type;
-       this->public.interface.sign = (bool (*) (private_key_t*, signature_scheme_t, chunk_t, chunk_t*))sign;
-       this->public.interface.decrypt = (bool (*) (private_key_t*, chunk_t, chunk_t*))decrypt;
-       this->public.interface.get_keysize = (size_t (*) (private_key_t*))get_keysize;
-       this->public.interface.get_public_key = (public_key_t* (*) (private_key_t*))get_public_key;
-       this->public.interface.equals = private_key_equals;
-       this->public.interface.belongs_to = private_key_belongs_to;
-       this->public.interface.get_fingerprint = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint;
-       this->public.interface.has_fingerprint = (bool(*)(private_key_t*, chunk_t fp))private_key_has_fingerprint;
-       this->public.interface.get_encoding = (bool(*)(private_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding;
-       this->public.interface.get_ref = (private_key_t* (*) (private_key_t*))get_ref;
-       this->public.interface.destroy = (void (*) (private_key_t*))destroy;
-
-       this->engine = FALSE;
-       this->ref = 1;
+       private_openssl_rsa_private_key_t *this;
+
+       INIT(this,
+               .public.key = {
+                       .get_type = _get_type,
+                       .sign = _sign,
+                       .decrypt = _decrypt,
+                       .get_keysize = _get_keysize,
+                       .get_public_key = _get_public_key,
+                       .equals = private_key_equals,
+                       .belongs_to = private_key_belongs_to,
+                       .get_fingerprint = _get_fingerprint,
+                       .has_fingerprint = private_key_has_fingerprint,
+                       .get_encoding = _get_encoding,
+                       .get_ref = _get_ref,
+                       .destroy = _destroy,
+               },
+               .ref = 1,
+       );
 
        return this;
 }
index 079dfa4..60889d6 100644 (file)
@@ -34,7 +34,7 @@ struct openssl_rsa_private_key_t {
        /**
         * Implements private_key_t interface
         */
-       private_key_t interface;
+       private_key_t key;
 };
 
 /**
index 6ac61a6..54701da 100644 (file)
@@ -114,19 +114,15 @@ error:
        return valid;
 }
 
-/**
- * Implementation of public_key_t.get_type.
- */
-static key_type_t get_type(private_openssl_rsa_public_key_t *this)
+METHOD(public_key_t, get_type, key_type_t,
+       private_openssl_rsa_public_key_t *this)
 {
        return KEY_RSA;
 }
 
-/**
- * Implementation of public_key_t.verify.
- */
-static bool verify(private_openssl_rsa_public_key_t *this, signature_scheme_t scheme,
-                                  chunk_t data, chunk_t signature)
+METHOD(public_key_t, verify, bool,
+       private_openssl_rsa_public_key_t *this, signature_scheme_t scheme,
+       chunk_t data, chunk_t signature)
 {
        switch (scheme)
        {
@@ -151,20 +147,15 @@ static bool verify(private_openssl_rsa_public_key_t *this, signature_scheme_t sc
        }
 }
 
-/**
- * Implementation of public_key_t.get_keysize.
- */
-static bool encrypt_(private_openssl_rsa_public_key_t *this,
-                                        chunk_t crypto, chunk_t *plain)
+METHOD(public_key_t, encrypt, bool,
+       private_openssl_rsa_public_key_t *this, chunk_t crypto, chunk_t *plain)
 {
        DBG1(DBG_LIB, "RSA public key encryption not implemented");
        return FALSE;
 }
 
-/**
- * Implementation of public_key_t.get_keysize.
- */
-static size_t get_keysize(private_openssl_rsa_public_key_t *this)
+METHOD(public_key_t, get_keysize, size_t,
+       private_openssl_rsa_public_key_t *this)
 {
        return RSA_size(this->rsa);
 }
@@ -211,20 +202,16 @@ bool openssl_rsa_fingerprint(RSA *rsa, cred_encoding_type_t type, chunk_t *fp)
        return TRUE;
 }
 
-/**
- * Implementation of public_key_t.get_fingerprint.
- */
-static bool get_fingerprint(private_openssl_rsa_public_key_t *this,
-                                                       cred_encoding_type_t type, chunk_t *fingerprint)
+METHOD(public_key_t, get_fingerprint, bool,
+       private_openssl_rsa_public_key_t *this, cred_encoding_type_t type,
+       chunk_t *fingerprint)
 {
        return openssl_rsa_fingerprint(this->rsa, type, fingerprint);
 }
 
-/*
- * Implementation of public_key_t.get_encoding.
- */
-static bool get_encoding(private_openssl_rsa_public_key_t *this,
-                                                cred_encoding_type_t type, chunk_t *encoding)
+METHOD(public_key_t, get_encoding, bool,
+       private_openssl_rsa_public_key_t *this, cred_encoding_type_t type,
+       chunk_t *encoding)
 {
        u_char *p;
 
@@ -262,19 +249,15 @@ static bool get_encoding(private_openssl_rsa_public_key_t *this,
        }
 }
 
-/**
- * Implementation of public_key_t.get_ref.
- */
-static public_key_t* get_ref(private_openssl_rsa_public_key_t *this)
+METHOD(public_key_t, get_ref, public_key_t*,
+       private_openssl_rsa_public_key_t *this)
 {
        ref_get(&this->ref);
-       return &this->public.interface;
+       return &this->public.key;
 }
 
-/**
- * Implementation of openssl_rsa_public_key.destroy.
- */
-static void destroy(private_openssl_rsa_public_key_t *this)
+METHOD(public_key_t, destroy, void,
+       private_openssl_rsa_public_key_t *this)
 {
        if (ref_put(&this->ref))
        {
@@ -292,21 +275,23 @@ static void destroy(private_openssl_rsa_public_key_t *this)
  */
 static private_openssl_rsa_public_key_t *create_empty()
 {
-       private_openssl_rsa_public_key_t *this = malloc_thing(private_openssl_rsa_public_key_t);
-
-       this->public.interface.get_type = (key_type_t (*)(public_key_t *this))get_type;
-       this->public.interface.verify = (bool (*)(public_key_t *this, signature_scheme_t scheme, chunk_t data, chunk_t signature))verify;
-       this->public.interface.encrypt = (bool (*)(public_key_t *this, chunk_t crypto, chunk_t *plain))encrypt_;
-       this->public.interface.equals = public_key_equals;
-       this->public.interface.get_keysize = (size_t (*) (public_key_t *this))get_keysize;
-       this->public.interface.get_fingerprint = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *fp))get_fingerprint;
-       this->public.interface.has_fingerprint = (bool(*)(public_key_t*, chunk_t fp))public_key_has_fingerprint;
-       this->public.interface.get_encoding = (bool(*)(public_key_t*, cred_encoding_type_t type, chunk_t *encoding))get_encoding;
-       this->public.interface.get_ref = (public_key_t* (*)(public_key_t *this))get_ref;
-       this->public.interface.destroy = (void (*)(public_key_t *this))destroy;
-
-       this->rsa = NULL;
-       this->ref = 1;
+       private_openssl_rsa_public_key_t *this;
+
+       INIT(this,
+               .public.key = {
+                       .get_type = _get_type,
+                       .verify = _verify,
+                       .encrypt = _encrypt,
+                       .equals = public_key_equals,
+                       .get_keysize = _get_keysize,
+                       .get_fingerprint = _get_fingerprint,
+                       .has_fingerprint = public_key_has_fingerprint,
+                       .get_encoding = _get_encoding,
+                       .get_ref = _get_ref,
+                       .destroy = _destroy,
+               },
+               .ref = 1,
+       );
 
        return this;
 }
index 620aa51..021257d 100644 (file)
@@ -33,7 +33,7 @@ struct openssl_rsa_public_key_t {
        /**
         * Implements the public_key_t interface
         */
-       public_key_t interface;
+       public_key_t key;
 };
 
 /**