Key strength checking stores all key sizes in auth_cfg, verifies all in complies()
[strongswan.git] / src / libstrongswan / credentials / credential_manager.c
index 466d86f..f437bbf 100644 (file)
 #include <threading/thread_value.h>
 #include <threading/mutex.h>
 #include <threading/rwlock.h>
-#include <selectors/traffic_selector.h>
 #include <utils/linked_list.h>
 #include <credentials/sets/cert_cache.h>
 #include <credentials/sets/auth_cfg_wrapper.h>
-#include <credentials/sets/ocsp_response_wrapper.h>
 #include <credentials/certificates/x509.h>
-#include <credentials/certificates/crl.h>
-#include <credentials/certificates/ocsp_request.h>
-#include <credentials/certificates/ocsp_response.h>
 
 /**
  * Maximum length of a certificate trust chain
@@ -68,6 +63,11 @@ struct private_credential_manager_t {
        linked_list_t *cache_queue;
 
        /**
+        * list of certificate validators, cert_validator_t
+        */
+       linked_list_t *validators;
+
+       /**
         * read-write lock to sets list
         */
        rwlock_t *lock;
@@ -157,8 +157,10 @@ static enumerator_t *create_sets_enumerator(private_credential_manager_t *this)
        linked_list_t *local;
 
        INIT(enumerator,
-               .public.enumerate = (void*)_sets_enumerate,
-               .public.destroy = _sets_destroy,
+               .public = {
+                       .enumerate = (void*)_sets_enumerate,
+                       .destroy = _sets_destroy,
+               },
                .global = this->sets->create_enumerator(this->sets),
        );
        local = this->local_sets->get(this->local_sets);
@@ -370,11 +372,8 @@ METHOD(credential_manager_t, get_shared, shared_key_t*,
        return found;
 }
 
-/**
- * add a credential set to the thread local list
- */
-static void add_local_set(private_credential_manager_t *this,
-                                                 credential_set_t *set)
+METHOD(credential_manager_t, add_local_set, void,
+       private_credential_manager_t *this, credential_set_t *set)
 {
        linked_list_t *sets;
 
@@ -387,11 +386,8 @@ static void add_local_set(private_credential_manager_t *this,
        sets->insert_last(sets, set);
 }
 
-/**
- * remove a credential set from the thread local list
- */
-static void remove_local_set(private_credential_manager_t *this,
-                                                        credential_set_t *set)
+METHOD(credential_manager_t, remove_local_set, void,
+       private_credential_manager_t *this, credential_set_t *set)
 {
        linked_list_t *sets;
 
@@ -453,546 +449,6 @@ static void cache_queue(private_credential_manager_t *this)
 }
 
 /**
- * forward declaration
- */
-static enumerator_t *create_trusted_enumerator(private_credential_manager_t *this,
-                                       key_type_t type, identification_t *id, bool online);
-
-/**
- * Do an OCSP request
- */
-static certificate_t *fetch_ocsp(private_credential_manager_t *this, char *url,
-                                                                certificate_t *subject, certificate_t *issuer)
-{
-       certificate_t *request, *response;
-       chunk_t send, receive;
-
-       /* TODO: requestor name, signature */
-       request = lib->creds->create(lib->creds,
-                                               CRED_CERTIFICATE, CERT_X509_OCSP_REQUEST,
-                                               BUILD_CA_CERT, issuer,
-                                               BUILD_CERT, subject, BUILD_END);
-       if (!request)
-       {
-               DBG1(DBG_CFG, "generating ocsp request failed");
-               return NULL;
-       }
-
-       send = request->get_encoding(request);
-       request->destroy(request);
-
-       DBG1(DBG_CFG, "  requesting ocsp status from '%s' ...", url);
-       if (lib->fetcher->fetch(lib->fetcher, url, &receive,
-                                                       FETCH_REQUEST_DATA, send,
-                                                       FETCH_REQUEST_TYPE, "application/ocsp-request",
-                                                       FETCH_END) != SUCCESS)
-       {
-               DBG1(DBG_CFG, "ocsp request to %s failed", url);
-               chunk_free(&send);
-               return NULL;
-       }
-       chunk_free(&send);
-
-       response = lib->creds->create(lib->creds,
-                                                                 CRED_CERTIFICATE, CERT_X509_OCSP_RESPONSE,
-                                                                 BUILD_BLOB_ASN1_DER, receive, BUILD_END);
-       chunk_free(&receive);
-       if (!response)
-       {
-               DBG1(DBG_CFG, "parsing ocsp response failed");
-               return NULL;
-       }
-       return response;
-}
-
-/**
- * check the signature of an OCSP response
- */
-static bool verify_ocsp(private_credential_manager_t *this,
-                                               ocsp_response_t *response)
-{
-       certificate_t *issuer, *subject;
-       identification_t *responder;
-       ocsp_response_wrapper_t *wrapper;
-       enumerator_t *enumerator;
-       bool verified = FALSE;
-
-       wrapper = ocsp_response_wrapper_create((ocsp_response_t*)response);
-       add_local_set(this, &wrapper->set);
-
-       subject = &response->certificate;
-       responder = subject->get_issuer(subject);
-       enumerator = create_trusted_enumerator(this, KEY_ANY, responder, FALSE);
-       while (enumerator->enumerate(enumerator, &issuer, NULL))
-       {
-               if (this->cache->issued_by(this->cache, subject, issuer))
-               {
-                       DBG1(DBG_CFG, "  ocsp response correctly signed by \"%Y\"",
-                                                        issuer->get_subject(issuer));
-                       verified = TRUE;
-                       break;
-               }
-       }
-       enumerator->destroy(enumerator);
-
-       remove_local_set(this, &wrapper->set);
-       wrapper->destroy(wrapper);
-       return verified;
-}
-
-/**
- * Get the better of two OCSP responses, and check for usable OCSP info
- */
-static certificate_t *get_better_ocsp(private_credential_manager_t *this,
-                                                                         certificate_t *cand, certificate_t *best,
-                                                                         x509_t *subject, x509_t *issuer,
-                                                                         cert_validation_t *valid, bool cache)
-{
-       ocsp_response_t *response;
-       time_t revocation, this_update, next_update, valid_until;
-       crl_reason_t reason;
-       bool revoked = FALSE;
-
-       response = (ocsp_response_t*)cand;
-
-       /* check ocsp signature */
-       if (!verify_ocsp(this, response))
-       {
-               DBG1(DBG_CFG, "ocsp response verification failed");
-               cand->destroy(cand);
-               return best;
-       }
-       /* check if response contains our certificate */
-       switch (response->get_status(response, subject, issuer, &revocation, &reason,
-                                                                &this_update, &next_update))
-       {
-               case VALIDATION_REVOKED:
-                       /* subject has been revoked by a valid OCSP response */
-                       DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
-                                                 &revocation, TRUE, crl_reason_names, reason);
-                       revoked = TRUE;
-                       break;
-               case VALIDATION_GOOD:
-                       /* results in either good or stale */
-                       break;
-               default:
-               case VALIDATION_FAILED:
-                       /* candidate unusable, does not contain our cert */
-                       DBG1(DBG_CFG, "  ocsp response contains no status on our certificate");
-                       cand->destroy(cand);
-                       return best;
-       }
-
-       /* select the better of the two responses */
-       if (best == NULL || certificate_is_newer(cand, best))
-       {
-               DESTROY_IF(best);
-               best = cand;
-               if (best->get_validity(best, NULL, NULL, &valid_until))
-               {
-                       DBG1(DBG_CFG, "  ocsp response is valid: until %T",
-                                                        &valid_until, FALSE);
-                       *valid = VALIDATION_GOOD;
-                       if (cache)
-                       {       /* cache non-stale only, stale certs get refetched */
-                               cache_cert(this, best);
-                       }
-               }
-               else
-               {
-                       DBG1(DBG_CFG, "  ocsp response is stale: since %T",
-                                                        &valid_until, FALSE);
-                       *valid = VALIDATION_STALE;
-               }
-       }
-       else
-       {
-               *valid = VALIDATION_STALE;
-               cand->destroy(cand);
-       }
-       if (revoked)
-       {       /* revoked always counts, even if stale */
-               *valid = VALIDATION_REVOKED;
-       }
-       return best;
-}
-
-/**
- * validate a x509 certificate using OCSP
- */
-static cert_validation_t check_ocsp(private_credential_manager_t *this,
-                                                                       x509_t *subject, x509_t *issuer,
-                                                                       auth_cfg_t *auth)
-{
-       enumerator_t *enumerator;
-       cert_validation_t valid = VALIDATION_SKIPPED;
-       certificate_t *best = NULL, *current;
-       identification_t *keyid = NULL;
-       public_key_t *public;
-       chunk_t chunk;
-       char *uri = NULL;
-
-       /** lookup cache for valid OCSP responses */
-       enumerator = create_cert_enumerator(this, CERT_X509_OCSP_RESPONSE,
-                                                                               KEY_ANY, NULL, FALSE);
-       while (enumerator->enumerate(enumerator, &current))
-       {
-               current->get_ref(current);
-               best = get_better_ocsp(this, current, best, subject, issuer,
-                                                          &valid, FALSE);
-               if (best && valid != VALIDATION_STALE)
-               {
-                       DBG1(DBG_CFG, "  using cached ocsp response");
-                       break;
-               }
-       }
-       enumerator->destroy(enumerator);
-
-       /* derive the authorityKeyIdentifier from the issuer's public key */
-       current = &issuer->interface;
-       public = current->get_public_key(current);
-       if (public && public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &chunk))
-       {
-               keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
-       }
-       /** fetch from configured OCSP responder URLs */
-       if (keyid && valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
-       {
-               enumerator = create_cdp_enumerator(this, CERT_X509_OCSP_RESPONSE, keyid);
-               while (enumerator->enumerate(enumerator, &uri))
-               {
-                       current = fetch_ocsp(this, uri, &subject->interface,
-                                                                &issuer->interface);
-                       if (current)
-                       {
-                               best = get_better_ocsp(this, current, best, subject, issuer,
-                                                                          &valid, TRUE);
-                               if (best && valid != VALIDATION_STALE)
-                               {
-                                       break;
-                               }
-                       }
-               }
-               enumerator->destroy(enumerator);
-       }
-       DESTROY_IF(public);
-       DESTROY_IF(keyid);
-
-       /* fallback to URL fetching from subject certificate's URIs */
-       if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
-       {
-               enumerator = subject->create_ocsp_uri_enumerator(subject);
-               while (enumerator->enumerate(enumerator, &uri))
-               {
-                       current = fetch_ocsp(this, uri, &subject->interface,
-                                                                &issuer->interface);
-                       if (current)
-                       {
-                               best = get_better_ocsp(this, current, best, subject, issuer,
-                                                                          &valid, TRUE);
-                               if (best && valid != VALIDATION_STALE)
-                               {
-                                       break;
-                               }
-                       }
-               }
-               enumerator->destroy(enumerator);
-       }
-       /* an uri was found, but no result. switch validation state to failed */
-       if (valid == VALIDATION_SKIPPED && uri)
-       {
-               valid = VALIDATION_FAILED;
-       }
-       if (auth)
-       {
-               auth->add(auth, AUTH_RULE_OCSP_VALIDATION, valid);
-               if (valid == VALIDATION_GOOD)
-               {       /* successful OCSP check fulfills also CRL constraint */
-                       auth->add(auth, AUTH_RULE_CRL_VALIDATION, VALIDATION_GOOD);
-               }
-       }
-       DESTROY_IF(best);
-       return valid;
-}
-
-/**
- * fetch a CRL from an URL
- */
-static certificate_t* fetch_crl(private_credential_manager_t *this, char *url)
-{
-       certificate_t *crl;
-       chunk_t chunk;
-
-       DBG1(DBG_CFG, "  fetching crl from '%s' ...", url);
-       if (lib->fetcher->fetch(lib->fetcher, url, &chunk, FETCH_END) != SUCCESS)
-       {
-               DBG1(DBG_CFG, "crl fetching failed");
-               return NULL;
-       }
-       crl = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_CRL,
-                                                        BUILD_BLOB_ASN1_DER, chunk, BUILD_END);
-       chunk_free(&chunk);
-       if (!crl)
-       {
-               DBG1(DBG_CFG, "crl fetched successfully but parsing failed");
-               return NULL;
-       }
-       return crl;
-}
-
-/**
- * check the signature of an CRL
- */
-static bool verify_crl(private_credential_manager_t *this, certificate_t *crl)
-{
-       certificate_t *issuer;
-       enumerator_t *enumerator;
-       bool verified = FALSE;
-
-       enumerator = create_trusted_enumerator(this, KEY_ANY, crl->get_issuer(crl),
-                                                                                  FALSE);
-       while (enumerator->enumerate(enumerator, &issuer, NULL))
-       {
-               if (this->cache->issued_by(this->cache, crl, issuer))
-               {
-                       DBG1(DBG_CFG, "  crl correctly signed by \"%Y\"",
-                                                  issuer->get_subject(issuer));
-                       verified = TRUE;
-                       break;
-               }
-       }
-       enumerator->destroy(enumerator);
-
-       return verified;
-}
-
-/**
- * Get the better of two CRLs, and check for usable CRL info
- */
-static certificate_t *get_better_crl(private_credential_manager_t *this,
-                                                                        certificate_t *cand, certificate_t *best,
-                                                                        x509_t *subject, x509_t *issuer,
-                                                                        cert_validation_t *valid, bool cache)
-{
-       enumerator_t *enumerator;
-       time_t revocation, valid_until;
-       crl_reason_t reason;
-       chunk_t serial;
-       crl_t *crl;
-
-       /* check CRL signature */
-       if (!verify_crl(this, cand))
-       {
-               DBG1(DBG_CFG, "crl response verification failed");
-               cand->destroy(cand);
-               return best;
-       }
-
-       crl = (crl_t*)cand;
-       enumerator = crl->create_enumerator(crl);
-       while (enumerator->enumerate(enumerator, &serial, &revocation, &reason))
-       {
-               if (chunk_equals(serial, subject->get_serial(subject)))
-               {
-                       DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
-                                &revocation, TRUE, crl_reason_names, reason);
-                       *valid = VALIDATION_REVOKED;
-                       enumerator->destroy(enumerator);
-                       DESTROY_IF(best);
-                       return cand;
-               }
-       }
-       enumerator->destroy(enumerator);
-
-       /* select the better of the two CRLs */
-       if (best == NULL || crl_is_newer(crl, (crl_t*)best))
-       {
-               DESTROY_IF(best);
-               best = cand;
-               if (best->get_validity(best, NULL, NULL, &valid_until))
-               {
-                       DBG1(DBG_CFG, "  crl is valid: until %T", &valid_until, FALSE);
-                       *valid = VALIDATION_GOOD;
-                       if (cache)
-                       {       /* we cache non-stale crls only, as a stale crls are refetched */
-                               cache_cert(this, best);
-                       }
-               }
-               else
-               {
-                       DBG1(DBG_CFG, "  crl is stale: since %T", &valid_until, FALSE);
-                       *valid = VALIDATION_STALE;
-               }
-       }
-       else
-       {
-               *valid = VALIDATION_STALE;
-               cand->destroy(cand);
-       }
-       return best;
-}
-
-/**
- * validate a x509 certificate using CRL
- */
-static cert_validation_t check_crl(private_credential_manager_t *this,
-                                                                  x509_t *subject, x509_t *issuer,
-                                                                  auth_cfg_t *auth)
-{
-       cert_validation_t valid = VALIDATION_SKIPPED;
-       identification_t *keyid = NULL;
-       certificate_t *best = NULL;
-       certificate_t *current;
-       public_key_t *public;
-       enumerator_t *enumerator;
-       chunk_t chunk;
-       char *uri = NULL;
-
-       /* derive the authorityKeyIdentifier from the issuer's public key */
-       current = &issuer->interface;
-       public = current->get_public_key(current);
-       if (public && public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &chunk))
-       {
-               keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
-
-               /* find a cached crl by authorityKeyIdentifier */
-               enumerator = create_cert_enumerator(this, CERT_X509_CRL, KEY_ANY,
-                                                                                       keyid, FALSE);
-               while (enumerator->enumerate(enumerator, &current))
-               {
-                       current->get_ref(current);
-                       best = get_better_crl(this, current, best, subject, issuer,
-                                                                 &valid, FALSE);
-                       if (best && valid != VALIDATION_STALE)
-                       {
-                               DBG1(DBG_CFG, "  using cached crl");
-                               break;
-                       }
-               }
-               enumerator->destroy(enumerator);
-
-               /* fallback to fetching crls from credential sets cdps */
-               if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
-               {
-                       enumerator = create_cdp_enumerator(this, CERT_X509_CRL, keyid);
-
-                       while (enumerator->enumerate(enumerator, &uri))
-                       {
-                               current = fetch_crl(this, uri);
-                               if (current)
-                               {
-                                       best = get_better_crl(this, current, best, subject, issuer,
-                                                                                 &valid, TRUE);
-                                       if (best && valid != VALIDATION_STALE)
-                                       {
-                                               break;
-                                       }
-                               }
-                       }
-                       enumerator->destroy(enumerator);
-               }
-               keyid->destroy(keyid);
-       }
-       DESTROY_IF(public);
-
-       /* fallback to fetching crls from cdps from subject's certificate */
-       if (valid != VALIDATION_GOOD && valid != VALIDATION_REVOKED)
-       {
-               enumerator = subject->create_crl_uri_enumerator(subject);
-
-               while (enumerator->enumerate(enumerator, &uri))
-               {
-                       current = fetch_crl(this, uri);
-                       if (current)
-                       {
-                               best = get_better_crl(this, current, best, subject, issuer,
-                                                                         &valid, TRUE);
-                               if (best && valid != VALIDATION_STALE)
-                               {
-                                       break;
-                               }
-                       }
-               }
-               enumerator->destroy(enumerator);
-       }
-
-       /* an uri was found, but no result. switch validation state to failed */
-       if (valid == VALIDATION_SKIPPED && uri)
-       {
-               valid = VALIDATION_FAILED;
-       }
-       if (auth)
-       {
-               if (valid == VALIDATION_SKIPPED)
-               {       /* if we skipped CRL validation, we use the result of OCSP for
-                        * constraint checking */
-                       auth->add(auth, AUTH_RULE_CRL_VALIDATION,
-                                         auth->get(auth, AUTH_RULE_OCSP_VALIDATION));
-               }
-               else
-               {
-                       auth->add(auth, AUTH_RULE_CRL_VALIDATION, valid);
-               }
-       }
-       DESTROY_IF(best);
-       return valid;
-}
-
-/**
- * check a certificate for optional IP address block constraints
- */
-static bool check_ip_addr_block_constraints(x509_t *subject, x509_t *issuer)
-{
-       bool subject_constraint = subject->get_flags(subject) & X509_IP_ADDR_BLOCKS;
-       bool issuer_constraint = issuer->get_flags(issuer) & X509_IP_ADDR_BLOCKS;
-       bool contained = TRUE;
-
-       enumerator_t *subject_enumerator, *issuer_enumerator;
-       traffic_selector_t *subject_ts, *issuer_ts;
-
-       if (!subject_constraint && !issuer_constraint)
-       {
-               return TRUE;
-       }
-       if (!subject_constraint)
-       {
-               DBG1(DBG_CFG, "subject certficate lacks ipAddrBlocks extension");
-               return FALSE;
-       }
-       if (!issuer_constraint)
-       {
-               DBG1(DBG_CFG, "issuer certficate lacks ipAddrBlocks extension");
-               return FALSE;
-       }
-       subject_enumerator = subject->create_ipAddrBlock_enumerator(subject);
-       while (subject_enumerator->enumerate(subject_enumerator, &subject_ts))
-       {
-               contained = FALSE;
-
-               issuer_enumerator = issuer->create_ipAddrBlock_enumerator(issuer);
-               while (issuer_enumerator->enumerate(issuer_enumerator, &issuer_ts))
-               {
-                       if (subject_ts->is_contained_in(subject_ts, issuer_ts))
-                       {
-                               DBG2(DBG_CFG, "  subject address block %R is contained in "
-                                                         "issuer address block %R", subject_ts, issuer_ts);
-                               contained = TRUE;
-                               break;
-                       }
-               }
-               issuer_enumerator->destroy(issuer_enumerator);
-               if (!contained)
-               {
-                       DBG1(DBG_CFG, "subject address block %R is not contained in any "
-                                                 "issuer address block", subject_ts);
-                       break;
-               }
-       }
-       subject_enumerator->destroy(subject_enumerator);
-       return contained;
-}
-
-/**
  * check a certificate for its lifetime
  */
 static bool check_certificate(private_credential_manager_t *this,
@@ -1000,6 +456,8 @@ static bool check_certificate(private_credential_manager_t *this,
                                                          bool online, int pathlen, auth_cfg_t *auth)
 {
        time_t not_before, not_after;
+       cert_validator_t *validator;
+       enumerator_t *enumerator;
 
        if (!subject->get_validity(subject, NULL, &not_before, &not_after))
        {
@@ -1019,11 +477,6 @@ static bool check_certificate(private_credential_manager_t *this,
                int pathlen_constraint;
                x509_t *x509;
 
-               if (!check_ip_addr_block_constraints((x509_t*)subject, (x509_t*)issuer))
-               {
-                       return FALSE;
-               }
-
                /* check path length constraint */
                x509 = (x509_t*)issuer;
                pathlen_constraint = x509->get_pathLenConstraint(x509);
@@ -1034,47 +487,19 @@ static bool check_certificate(private_credential_manager_t *this,
                                 pathlen, pathlen_constraint);
                        return FALSE;
                }
+       }
 
-               if (online)
+       enumerator = this->validators->create_enumerator(this->validators);
+       while (enumerator->enumerate(enumerator, &validator))
+       {
+               if (!validator->validate(validator, subject, issuer,
+                                                                online, pathlen, auth))
                {
-                       DBG1(DBG_CFG, "checking certificate status of \"%Y\"",
-                                                  subject->get_subject(subject));
-                       switch (check_ocsp(this, (x509_t*)subject, (x509_t*)issuer, auth))
-                       {
-                               case VALIDATION_GOOD:
-                                       DBG1(DBG_CFG, "certificate status is good");
-                                       return TRUE;
-                               case VALIDATION_REVOKED:
-                                       /* has already been logged */
-                                       return FALSE;
-                               case VALIDATION_SKIPPED:
-                                       DBG2(DBG_CFG, "ocsp check skipped, no ocsp found");
-                                       break;
-                               case VALIDATION_STALE:
-                                       DBG1(DBG_CFG, "ocsp information stale, fallback to crl");
-                                       break;
-                               case VALIDATION_FAILED:
-                                       DBG1(DBG_CFG, "ocsp check failed, fallback to crl");
-                                       break;
-                       }
-                       switch (check_crl(this, (x509_t*)subject, (x509_t*)issuer, auth))
-                       {
-                               case VALIDATION_GOOD:
-                                       DBG1(DBG_CFG, "certificate status is good");
-                                       return TRUE;
-                               case VALIDATION_REVOKED:
-                                       /* has already been logged */
-                                       return FALSE;
-                               case VALIDATION_FAILED:
-                               case VALIDATION_SKIPPED:
-                                       DBG1(DBG_CFG, "certificate status is not available");
-                                       break;
-                               case VALIDATION_STALE:
-                                       DBG1(DBG_CFG, "certificate status is unknown, crl is stale");
-                                       break;
-                       }
+                       enumerator->destroy(enumerator);
+                       return FALSE;
                }
        }
+       enumerator->destroy(enumerator);
        return TRUE;
 }
 
@@ -1126,6 +551,37 @@ static certificate_t *get_issuer_cert(private_credential_manager_t *this,
 }
 
 /**
+ * Get the strength of certificate, add it to auth
+ */
+static void get_key_strength(certificate_t *cert, auth_cfg_t *auth)
+{
+       uintptr_t strength;
+       public_key_t *key;
+       key_type_t type;
+
+       key = cert->get_public_key(cert);
+       if (key)
+       {
+               type = key->get_type(key);
+               strength = key->get_keysize(key);
+               DBG2(DBG_CFG, "  certificate \"%Y\" key: %d bit %N",
+                        cert->get_subject(cert), strength, key_type_names, type);
+               switch (type)
+               {
+                       case KEY_RSA:
+                               auth->add(auth, AUTH_RULE_RSA_STRENGTH, strength);
+                               break;
+                       case KEY_ECDSA:
+                               auth->add(auth, AUTH_RULE_ECDSA_STRENGTH, strength);
+                               break;
+                       default:
+                               break;
+               }
+               key->destroy(key);
+       }
+}
+
+/**
  * try to verify the trust chain of subject, return TRUE if trusted
  */
 static bool verify_trust_chain(private_credential_manager_t *this,
@@ -1137,6 +593,7 @@ static bool verify_trust_chain(private_credential_manager_t *this,
        int pathlen;
 
        auth = auth_cfg_create();
+       get_key_strength(subject, auth);
        current = subject->get_ref(subject);
 
        for (pathlen = 0; pathlen <= MAX_TRUST_PATH_LEN; pathlen++)
@@ -1189,6 +646,10 @@ static bool verify_trust_chain(private_credential_manager_t *this,
                        issuer->destroy(issuer);
                        break;
                }
+               if (issuer)
+               {
+                       get_key_strength(issuer, auth);
+               }
                current->destroy(current);
                current = issuer;
                if (trusted)
@@ -1285,6 +746,8 @@ METHOD(enumerator_t, trusted_enumerate, bool,
                if (verify_trust_chain(this->this, current, this->auth, FALSE,
                                                           this->online))
                {
+                       this->auth->add(this->auth, AUTH_RULE_SUBJECT_CERT,
+                                                       current->get_ref(current));
                        *cert = current;
                        if (auth)
                        {
@@ -1305,11 +768,9 @@ METHOD(enumerator_t, trusted_destroy, void,
        free(this);
 }
 
-/**
- * create an enumerator over trusted certificates and their trustchain
- */
-static enumerator_t *create_trusted_enumerator(private_credential_manager_t *this,
-                                       key_type_t type, identification_t *id, bool online)
+METHOD(credential_manager_t, create_trusted_enumerator, enumerator_t*,
+       private_credential_manager_t *this, key_type_t type,
+       identification_t *id, bool online)
 {
        trusted_enumerator_t *enumerator;
 
@@ -1401,7 +862,7 @@ METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
 }
 
 /**
- * Check if a certificate's keyid is contained in the auth helper
+ * Check if an helper contains a certificate as trust anchor
  */
 static bool auth_contains_cacert(auth_cfg_t *auth, certificate_t *cert)
 {
@@ -1433,17 +894,10 @@ static auth_cfg_t *build_trustchain(private_credential_manager_t *this,
        certificate_t *issuer, *current;
        auth_cfg_t *trustchain;
        int pathlen = 0;
+       bool has_anchor;
 
        trustchain = auth_cfg_create();
-
-       current = auth->get(auth, AUTH_RULE_CA_CERT);
-       if (!current)
-       {
-               /* no trust anchor specified, return this cert only */
-               trustchain->add(trustchain, AUTH_RULE_SUBJECT_CERT,
-                                               subject->get_ref(subject));
-               return trustchain;
-       }
+       has_anchor = auth->get(auth, AUTH_RULE_CA_CERT) != NULL;
        current = subject->get_ref(subject);
        while (TRUE)
        {
@@ -1458,17 +912,33 @@ static auth_cfg_t *build_trustchain(private_credential_manager_t *this,
                }
                else
                {
+                       if (!has_anchor &&
+                               this->cache->issued_by(this->cache, current, current))
+                       {       /* If no trust anchor specified, accept any CA */
+                               trustchain->add(trustchain, AUTH_RULE_CA_CERT, current);
+                               return trustchain;
+                       }
                        trustchain->add(trustchain, AUTH_RULE_IM_CERT, current);
                }
+               if (pathlen++ > MAX_TRUST_PATH_LEN)
+               {
+                       break;
+               }
                issuer = get_issuer_cert(this, current, FALSE);
-               if (!issuer || issuer->equals(issuer, current) ||
-                       pathlen > MAX_TRUST_PATH_LEN)
+               if (!issuer)
+               {
+                       if (!has_anchor)
+                       {       /* If no trust anchor specified, accept incomplete chains */
+                               return trustchain;
+                       }
+                       break;
+               }
+               if (has_anchor && issuer->equals(issuer, current))
                {
-                       DESTROY_IF(issuer);
+                       issuer->destroy(issuer);
                        break;
                }
                current = issuer;
-               pathlen++;
        }
        trustchain->destroy(trustchain);
        return NULL;
@@ -1488,7 +958,7 @@ static private_key_t *get_private_by_cert(private_credential_manager_t *this,
        public = cert->get_public_key(cert);
        if (public)
        {
-               if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &chunk))
+               if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
                {
                        keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
                        private = get_private_by_keyid(this, type, keyid);
@@ -1579,6 +1049,13 @@ METHOD(credential_manager_t, flush_cache, void,
        this->cache->flush(this->cache, type);
 }
 
+METHOD(credential_manager_t, issued_by, bool,
+       private_credential_manager_t *this, certificate_t *subject,
+       certificate_t *issuer)
+{
+       return this->cache->issued_by(this->cache, subject, issuer);
+}
+
 METHOD(credential_manager_t, add_set, void,
        private_credential_manager_t *this, credential_set_t *set)
 {
@@ -1595,6 +1072,22 @@ METHOD(credential_manager_t, remove_set, void,
        this->lock->unlock(this->lock);
 }
 
+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->lock->unlock(this->lock);
+}
+
+METHOD(credential_manager_t, remove_validator, void,
+       private_credential_manager_t *this, cert_validator_t *vdtr)
+{
+       this->lock->write_lock(this->lock);
+       this->validators->remove(this->validators, vdtr, NULL);
+       this->lock->unlock(this->lock);
+}
+
 METHOD(credential_manager_t, destroy, void,
        private_credential_manager_t *this)
 {
@@ -1604,6 +1097,7 @@ METHOD(credential_manager_t, destroy, void,
        this->sets->destroy(this->sets);
        this->local_sets->destroy(this->local_sets);
        this->cache->destroy(this->cache);
+       this->validators->destroy(this->validators);
        this->lock->destroy(this->lock);
        this->queue_mutex->destroy(this->queue_mutex);
        free(this);
@@ -1624,14 +1118,21 @@ credential_manager_t *credential_manager_create()
                        .get_cert = _get_cert,
                        .get_shared = _get_shared,
                        .get_private = _get_private,
+                       .create_trusted_enumerator = _create_trusted_enumerator,
                        .create_public_enumerator = _create_public_enumerator,
                        .flush_cache = _flush_cache,
                        .cache_cert = _cache_cert,
+                       .issued_by = _issued_by,
                        .add_set = _add_set,
                        .remove_set = _remove_set,
+                       .add_local_set = _add_local_set,
+                       .remove_local_set = _remove_local_set,
+                       .add_validator = _add_validator,
+                       .remove_validator = _remove_validator,
                        .destroy = _destroy,
                },
                .sets = linked_list_create(),
+               .validators = linked_list_create(),
                .cache = cert_cache_create(),
                .cache_queue = linked_list_create(),
                .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),