child-rekey: Don't change state to INSTALLED if it was already REKEYING
[strongswan.git] / src / libstrongswan / credentials / credential_manager.c
index f4cd9b9..95c5cd7 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (C) 2015 Tobias Brunner
  * Copyright (C) 2007 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
@@ -81,6 +82,16 @@ struct private_credential_manager_t {
         * mutex for cache queue
         */
        mutex_t *queue_mutex;
+
+       /**
+        * Registered hook to call on validation errors
+        */
+       credential_hook_t hook;
+
+       /**
+        * Registered data to pass to hook
+        */
+       void *hook_data;
 };
 
 /** data to pass to create_private_enumerator */
@@ -126,6 +137,22 @@ typedef struct {
        enumerator_t *exclusive;
 } sets_enumerator_t;
 
+METHOD(credential_manager_t, set_hook, void,
+       private_credential_manager_t *this, credential_hook_t hook, void *data)
+{
+       this->hook = hook;
+       this->hook_data = data;
+}
+
+METHOD(credential_manager_t, call_hook, void,
+       private_credential_manager_t *this, credential_hook_type_t type,
+       certificate_t *cert)
+{
+       if (this->hook)
+       {
+               this->hook(this->hook_data, type, cert);
+       }
+}
 
 METHOD(enumerator_t, sets_enumerate, bool,
        sets_enumerator_t *this, credential_set_t **set)
@@ -378,8 +405,8 @@ METHOD(credential_manager_t, get_shared, shared_key_t*,
        identification_t *me, identification_t *other)
 {
        shared_key_t *current, *found = NULL;
-       id_match_t *best_me = ID_MATCH_NONE, *best_other = ID_MATCH_NONE;
-       id_match_t *match_me, *match_other;
+       id_match_t best_me = ID_MATCH_NONE, best_other = ID_MATCH_NONE;
+       id_match_t match_me, match_other;
        enumerator_t *enumerator;
 
        enumerator = create_shared_enumerator(this, type, me, other);
@@ -393,6 +420,10 @@ METHOD(credential_manager_t, get_shared, shared_key_t*,
                        best_me = match_me;
                        best_other = match_other;
                }
+               if (best_me == ID_MATCH_PERFECT && best_other == ID_MATCH_PERFECT)
+               {
+                       break;
+               }
        }
        enumerator->destroy(enumerator);
        return found;
@@ -549,15 +580,17 @@ static bool check_lifetime(private_credential_manager_t *this,
                        {
                                DBG1(DBG_CFG, "%s certificate invalid (valid from %T to %T)",
                                         label, &not_before, FALSE, &not_after, FALSE);
-                               return FALSE;
+                               break;
                        }
                        return TRUE;
                case SUCCESS:
                        return TRUE;
                case FAILED:
                default:
-                       return FALSE;
+                       break;
        }
+       call_hook(this, CRED_HOOK_EXPIRED, cert);
+       return FALSE;
 }
 
 /**
@@ -666,6 +699,9 @@ static void get_key_strength(certificate_t *cert, auth_cfg_t *auth)
                        case KEY_ECDSA:
                                auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
                                break;
+                       case KEY_BLISS:
+                               auth->add(auth, AUTH_RULE_BLISS_STRENGTH, strength);
+                               break;
                        default:
                                break;
                }
@@ -718,9 +754,10 @@ static bool verify_trust_chain(private_credential_manager_t *this,
                        {
                                if (current->equals(current, issuer))
                                {
-                                       DBG1(DBG_CFG, "  self-signed certificate \"%Y\" is not trusted",
-                                                current->get_subject(current));
+                                       DBG1(DBG_CFG, "  self-signed certificate \"%Y\" is not "
+                                                "trusted", current->get_subject(current));
                                        issuer->destroy(issuer);
+                                       call_hook(this, CRED_HOOK_UNTRUSTED_ROOT, current);
                                        break;
                                }
                                auth->add(auth, AUTH_RULE_IM_CERT, issuer->get_ref(issuer));
@@ -732,6 +769,7 @@ static bool verify_trust_chain(private_credential_manager_t *this,
                        {
                                DBG1(DBG_CFG, "no issuer certificate found for \"%Y\"",
                                         current->get_subject(current));
+                               call_hook(this, CRED_HOOK_NO_ISSUER, current);
                                break;
                        }
                }
@@ -750,8 +788,8 @@ static bool verify_trust_chain(private_credential_manager_t *this,
                current = issuer;
                if (trusted)
                {
-                       DBG1(DBG_CFG, "  reached self-signed root ca with a path length of %d",
-                                                 pathlen);
+                       DBG1(DBG_CFG, "  reached self-signed root ca with a "
+                                "path length of %d", pathlen);
                        break;
                }
        }
@@ -759,6 +797,7 @@ static bool verify_trust_chain(private_credential_manager_t *this,
        if (pathlen > MAX_TRUST_PATH_LEN)
        {
                DBG1(DBG_CFG, "maximum path length of %d exceeded", MAX_TRUST_PATH_LEN);
+               call_hook(this, CRED_HOOK_EXCEEDED_PATH_LEN, subject);
        }
        if (trusted)
        {
@@ -879,6 +918,8 @@ METHOD(enumerator_t, trusted_destroy, void,
        DESTROY_IF(this->auth);
        DESTROY_IF(this->candidates);
        this->failed->destroy_offset(this->failed, offsetof(certificate_t, destroy));
+       /* check for delayed certificate cache queue */
+       cache_queue(this->this);
        free(this);
 }
 
@@ -947,7 +988,6 @@ METHOD(enumerator_t, public_destroy, void,
                this->wrapper->destroy(this->wrapper);
        }
        this->this->lock->unlock(this->this->lock);
-
        /* check for delayed certificate cache queue */
        cache_queue(this->this);
        free(this);
@@ -955,7 +995,7 @@ METHOD(enumerator_t, public_destroy, void,
 
 METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
        private_credential_manager_t *this, key_type_t type, identification_t *id,
-       auth_cfg_t *auth)
+       auth_cfg_t *auth, bool online)
 {
        public_enumerator_t *enumerator;
 
@@ -964,7 +1004,7 @@ METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
                        .enumerate = (void*)_public_enumerate,
                        .destroy = _public_destroy,
                },
-               .inner = create_trusted_enumerator(this, type, id, TRUE),
+               .inner = create_trusted_enumerator(this, type, id, online),
                .this = this,
        );
        if (auth)
@@ -1244,7 +1284,7 @@ METHOD(credential_manager_t, add_validator, void,
        private_credential_manager_t *this, cert_validator_t *vdtr)
 {
        this->lock->write_lock(this->lock);
-       this->sets->insert_last(this->validators, vdtr);
+       this->validators->insert_last(this->validators, vdtr);
        this->lock->unlock(this->lock);
 }
 
@@ -1301,6 +1341,8 @@ credential_manager_t *credential_manager_create()
                        .remove_local_set = _remove_local_set,
                        .add_validator = _add_validator,
                        .remove_validator = _remove_validator,
+                       .set_hook = _set_hook,
+                       .call_hook = _call_hook,
                        .destroy = _destroy,
                },
                .sets = linked_list_create(),
@@ -1312,7 +1354,7 @@ credential_manager_t *credential_manager_create()
 
        this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
        this->exclusive_local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
-       if (lib->settings->get_bool(lib->settings, "libstrongswan.cert_cache", TRUE))
+       if (lib->settings->get_bool(lib->settings, "%s.cert_cache", TRUE, lib->ns))
        {
                this->cache = cert_cache_create();
                this->sets->insert_first(this->sets, this->cache);