added generic implementation helpers for private_key_t.equals/belongs_to, public_key_...
authorMartin Willi <martin@strongswan.org>
Mon, 24 Aug 2009 12:00:43 +0000 (14:00 +0200)
committerMartin Willi <martin@strongswan.org>
Wed, 26 Aug 2009 09:23:52 +0000 (11:23 +0200)
src/libstrongswan/credentials/keys/private_key.c
src/libstrongswan/credentials/keys/private_key.h
src/libstrongswan/credentials/keys/public_key.c
src/libstrongswan/credentials/keys/public_key.h

index 0a01d03..a334370 100644 (file)
 
 #include "private_key.h"
 
+/**
+ * See header.
+ */
+bool private_key_equals(private_key_t *this, private_key_t *other)
+{
+       key_encoding_type_t type;
+       chunk_t a, b;
+       
+       if (this == other)
+       {
+               return TRUE;
+       }
+       
+       for (type = 0; type < KEY_ENCODING_MAX; type++)
+       {
+               if (this->get_fingerprint(this, type, &a) &&
+                       other->get_fingerprint(other, type, &b))
+               {
+                       return chunk_equals(a, b);
+               }
+       }
+       return FALSE;
+}
+
+/**
+ * See header.
+ */
+bool private_key_belongs_to(private_key_t *private, public_key_t *public)
+{
+       key_encoding_type_t type;
+       chunk_t a, b;
+       
+       for (type = 0; type < KEY_ENCODING_MAX; type++)
+       {
+               if (private->get_fingerprint(private, type, &a) &&
+                       public->get_fingerprint(public, type, &b))
+               {
+                       return chunk_equals(a, b);
+               }
+       }
+       return FALSE;
+}
+
index f38af8f..bbc77a8 100644 (file)
@@ -112,9 +112,27 @@ struct private_key_t {
        private_key_t* (*get_ref)(private_key_t *this);
                
        /**
-     * Decrease refcount, destroy private_key if no more references.
-     */
-    void (*destroy)(private_key_t *this);
+        * Decrease refcount, destroy private_key if no more references.
+        */
+       void (*destroy)(private_key_t *this);
 };
 
+/**
+ * Generic private key equals() implementation, usable by implementors.
+ *
+ * @param this                 first key to compare
+ * @param other                        second key to compare
+ * @return                             TRUE if this is equal to other
+ */
+bool private_key_equals(private_key_t *this, private_key_t *other);
+
+/**
+ * Generic private key belongs_to() implementation, usable by implementors.
+ *
+ * @param this                 first key to compare
+ * @param other                        second key to compare
+ * @return                             TRUE if this is equal to other
+ */
+bool private_key_belongs_to(private_key_t *private, public_key_t *public);
+
 #endif /** PRIVATE_KEY_H_ @}*/
index a5f5470..4abaf40 100644 (file)
@@ -39,6 +39,30 @@ ENUM(signature_scheme_names, SIGN_UNKNOWN, SIGN_ECDSA_521,
        "ECDSA-521",
 );
 
+/**
+ * See header.
+ */
+bool public_key_equals(public_key_t *this, public_key_t *other)
+{
+       key_encoding_type_t type;
+       chunk_t a, b;
+       
+       if (this == other)
+       {
+               return TRUE;
+       }
+       
+       for (type = 0; type < KEY_ENCODING_MAX; type++)
+       {
+               if (this->get_fingerprint(this, type, &a) &&
+                       other->get_fingerprint(other, type, &b))
+               {
+                       return chunk_equals(a, b);
+               }
+       }
+       return FALSE;
+}
+
 /*
  * Defined in header.
  */
index be5f3bd..6cab37d 100644 (file)
@@ -169,6 +169,15 @@ struct public_key_t {
 };
 
 /**
+ * Generic public key equals() implementation, usable by implementors.
+ *
+ * @param this                 first key to compare
+ * @param other                        second key to compare
+ * @return                             TRUE if this is equal to other
+ */
+bool public_key_equals(public_key_t *this, public_key_t *other);
+
+/**
  * Conversion of ASN.1 signature or hash OID to signature scheme.
  * 
  * @param oid                  ASN.1 OID