2008-03-20 14:31:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2008-07-30 11:38:44 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2008-03-20 14:31:36 +00:00
|
|
|
#include "cert_cache.h"
|
|
|
|
|
|
|
|
#include <daemon.h>
|
|
|
|
#include <utils/linked_list.h>
|
|
|
|
|
2008-03-28 08:38:51 +00:00
|
|
|
#define CACHE_SIZE 30
|
2008-03-20 14:31:36 +00:00
|
|
|
|
|
|
|
typedef struct private_cert_cache_t private_cert_cache_t;
|
|
|
|
typedef struct relation_t relation_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private data of cert_cache
|
|
|
|
*/
|
|
|
|
struct private_cert_cache_t {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* public functions
|
|
|
|
*/
|
|
|
|
cert_cache_t public;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list of trusted subject-issuer relations, as relation_t
|
|
|
|
*/
|
|
|
|
linked_list_t *relations;
|
2008-03-28 08:38:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* do we have an active enumerator
|
|
|
|
*/
|
2008-07-30 11:38:44 +00:00
|
|
|
refcount_t enumerating;
|
2008-03-28 08:38:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* have we increased the cache without a check_cache?
|
|
|
|
*/
|
|
|
|
bool check_required;
|
2008-04-16 08:38:15 +00:00
|
|
|
|
|
|
|
/**
|
2008-07-30 11:38:44 +00:00
|
|
|
* read-write lock to sets list
|
2008-04-16 08:38:15 +00:00
|
|
|
*/
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_t lock;
|
2008-03-20 14:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A trusted relation between subject and issuer
|
|
|
|
*/
|
|
|
|
struct relation_t {
|
|
|
|
/** subject of this relation */
|
|
|
|
certificate_t *subject;
|
|
|
|
/** issuer of this relation */
|
|
|
|
certificate_t *issuer;
|
|
|
|
/** time of last use */
|
|
|
|
time_t last_use;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* destroy a relation_t structure
|
|
|
|
*/
|
|
|
|
static void relation_destroy(relation_t *this)
|
|
|
|
{
|
|
|
|
this->subject->destroy(this->subject);
|
|
|
|
this->issuer->destroy(this->issuer);
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2008-03-28 08:38:51 +00:00
|
|
|
/**
|
|
|
|
* check the cache for oversize
|
|
|
|
*/
|
|
|
|
static void check_cache(private_cert_cache_t *this)
|
|
|
|
{
|
|
|
|
if (this->enumerating)
|
|
|
|
{
|
|
|
|
this->check_required = TRUE;
|
|
|
|
}
|
2008-07-30 11:38:44 +00:00
|
|
|
else if (pthread_rwlock_trywrlock(&this->lock) == 0)
|
|
|
|
{ /* never blocks, only done if lock is available */
|
2008-03-28 08:38:51 +00:00
|
|
|
while (this->relations->get_count(this->relations) > CACHE_SIZE)
|
|
|
|
{
|
|
|
|
relation_t *oldest = NULL, *current;
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
|
|
|
|
enumerator = this->relations->create_enumerator(this->relations);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
|
|
|
if (oldest == NULL || oldest->last_use <= current->last_use)
|
|
|
|
{
|
|
|
|
oldest = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
|
|
|
this->relations->remove(this->relations, oldest, NULL);
|
|
|
|
relation_destroy(oldest);
|
|
|
|
}
|
|
|
|
this->check_required = FALSE;
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_unlock(&this->lock);
|
2008-03-28 08:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-20 14:31:36 +00:00
|
|
|
/**
|
|
|
|
* Implementation of cert_cache_t.issued_by.
|
|
|
|
*/
|
|
|
|
static bool issued_by(private_cert_cache_t *this,
|
|
|
|
certificate_t *subject, certificate_t *issuer)
|
|
|
|
{
|
2008-03-28 08:38:51 +00:00
|
|
|
relation_t *found = NULL, *current;
|
2008-03-20 14:31:36 +00:00
|
|
|
enumerator_t *enumerator;
|
|
|
|
|
|
|
|
/* lookup cache */
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_rdlock(&this->lock);
|
2008-03-20 14:31:36 +00:00
|
|
|
enumerator = this->relations->create_enumerator(this->relations);
|
|
|
|
while (enumerator->enumerate(enumerator, ¤t))
|
|
|
|
{
|
2008-04-07 08:44:43 +00:00
|
|
|
bool match = FALSE;
|
|
|
|
|
|
|
|
/* check for equal certificates */
|
|
|
|
if (subject->equals(subject, current->subject))
|
|
|
|
{
|
|
|
|
match = TRUE;
|
|
|
|
subject = current->subject;
|
|
|
|
}
|
|
|
|
if (issuer->equals(issuer, current->issuer))
|
2008-03-20 14:31:36 +00:00
|
|
|
{
|
2008-04-07 08:44:43 +00:00
|
|
|
issuer = current->issuer;
|
|
|
|
/* if both certs match, we already have a relation */
|
|
|
|
if (match)
|
|
|
|
{
|
|
|
|
current->last_use = time(NULL);
|
|
|
|
found = current;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-20 14:31:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_unlock(&this->lock);
|
2008-03-20 14:31:36 +00:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
/* no cache hit, check signature */
|
2008-03-26 15:21:50 +00:00
|
|
|
if (!subject->issued_by(subject, issuer))
|
2008-03-20 14:31:36 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* cache if good, respect cache limit */
|
|
|
|
found = malloc_thing(relation_t);
|
|
|
|
found->subject = subject->get_ref(subject);
|
|
|
|
found->issuer = issuer->get_ref(issuer);
|
|
|
|
found->last_use = time(NULL);
|
2008-07-30 11:38:44 +00:00
|
|
|
/* insert should be ok without lock */
|
2008-03-20 14:31:36 +00:00
|
|
|
this->relations->insert_last(this->relations, found);
|
2008-03-28 08:38:51 +00:00
|
|
|
check_cache(this);
|
2008-03-20 14:31:36 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data associated to a cert enumeration
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** type of requested certificate */
|
|
|
|
certificate_type_t cert;
|
|
|
|
/** type of requested key */
|
|
|
|
key_type_t key;
|
|
|
|
/** ID to get a cert from */
|
|
|
|
identification_t *id;
|
2008-03-28 08:38:51 +00:00
|
|
|
/** reverse pointer to cache */
|
|
|
|
private_cert_cache_t *this;
|
2008-03-20 14:31:36 +00:00
|
|
|
} cert_data_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* filter function for certs enumerator
|
|
|
|
*/
|
|
|
|
static bool certs_filter(cert_data_t *data, relation_t **in, certificate_t **out)
|
|
|
|
{
|
|
|
|
public_key_t *public;
|
|
|
|
certificate_t *cert;
|
|
|
|
|
|
|
|
cert = (*in)->subject;
|
2008-03-28 08:14:47 +00:00
|
|
|
if (data->key == KEY_ANY && data->id &&
|
|
|
|
(data->cert == CERT_ANY || data->cert == CERT_X509_CRL) &&
|
|
|
|
cert->get_type(cert) == CERT_X509_CRL)
|
|
|
|
{ /* CRL lookup is done using issuer/authkeyidentifier */
|
|
|
|
if (cert->has_issuer(cert, data->id))
|
|
|
|
{
|
|
|
|
*out = cert;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-20 14:31:36 +00:00
|
|
|
if ((data->cert == CERT_ANY || cert->get_type(cert) == data->cert) &&
|
2008-03-26 15:21:50 +00:00
|
|
|
(!data->id || cert->has_subject(cert, data->id)))
|
2008-03-20 14:31:36 +00:00
|
|
|
{
|
|
|
|
if (data->key == KEY_ANY)
|
|
|
|
{
|
|
|
|
*out = cert;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
public = cert->get_public_key(cert);
|
|
|
|
if (public)
|
|
|
|
{
|
|
|
|
if (public->get_type(public) == data->key)
|
|
|
|
{
|
|
|
|
public->destroy(public);
|
|
|
|
*out = cert;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
public->destroy(public);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-03-28 08:38:51 +00:00
|
|
|
/**
|
|
|
|
* clean up enumeration data
|
|
|
|
*/
|
|
|
|
static void certs_destroy(cert_data_t *data)
|
|
|
|
{
|
2008-07-30 11:38:44 +00:00
|
|
|
ref_put(&data->this->enumerating);
|
|
|
|
pthread_rwlock_unlock(&data->this->lock);
|
2008-03-28 08:38:51 +00:00
|
|
|
if (data->this->check_required)
|
|
|
|
{
|
|
|
|
check_cache(data->this);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
2008-03-20 14:31:36 +00:00
|
|
|
/**
|
|
|
|
* implementation of credential_set_t.create_cert_enumerator
|
|
|
|
*/
|
|
|
|
static enumerator_t *create_enumerator(private_cert_cache_t *this,
|
|
|
|
certificate_type_t cert, key_type_t key,
|
|
|
|
identification_t *id, bool trusted)
|
|
|
|
{
|
|
|
|
cert_data_t *data;
|
|
|
|
|
|
|
|
if (trusted)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data = malloc_thing(cert_data_t);
|
|
|
|
data->cert = cert;
|
|
|
|
data->key = key;
|
|
|
|
data->id = id;
|
2008-03-28 08:38:51 +00:00
|
|
|
data->this = this;
|
2008-03-20 14:31:36 +00:00
|
|
|
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_rdlock(&this->lock);
|
|
|
|
ref_get(&this->enumerating);
|
2008-03-20 14:31:36 +00:00
|
|
|
return enumerator_create_filter(
|
|
|
|
this->relations->create_enumerator(this->relations),
|
2008-03-28 08:38:51 +00:00
|
|
|
(void*)certs_filter, data, (void*)certs_destroy);
|
2008-03-20 14:31:36 +00:00
|
|
|
}
|
|
|
|
|
2008-03-27 06:37:29 +00:00
|
|
|
/**
|
|
|
|
* Implementation of cert_cache_t.flush.
|
|
|
|
*/
|
|
|
|
static void flush(private_cert_cache_t *this, certificate_type_t type)
|
|
|
|
{
|
|
|
|
enumerator_t *enumerator;
|
|
|
|
relation_t *relation;
|
|
|
|
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_wrlock(&this->lock);
|
2008-03-27 06:37:29 +00:00
|
|
|
enumerator = this->relations->create_enumerator(this->relations);
|
|
|
|
while (enumerator->enumerate(enumerator, &relation))
|
|
|
|
{
|
|
|
|
if (type == CERT_ANY ||
|
|
|
|
type == relation->subject->get_type(relation->subject))
|
|
|
|
{
|
|
|
|
this->relations->remove_at(this->relations, enumerator);
|
|
|
|
relation_destroy(relation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->destroy(enumerator);
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_unlock(&this->lock);
|
2008-03-27 06:37:29 +00:00
|
|
|
}
|
|
|
|
|
2008-03-20 14:31:36 +00:00
|
|
|
/**
|
|
|
|
* Implementation of cert_cache_t.destroy
|
|
|
|
*/
|
|
|
|
static void destroy(private_cert_cache_t *this)
|
|
|
|
{
|
|
|
|
this->relations->destroy_function(this->relations, (void*)relation_destroy);
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_destroy(&this->lock);
|
2008-03-20 14:31:36 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* see header file
|
|
|
|
*/
|
|
|
|
cert_cache_t *cert_cache_create()
|
|
|
|
{
|
|
|
|
private_cert_cache_t *this = malloc_thing(private_cert_cache_t);
|
|
|
|
|
|
|
|
this->public.set.create_private_enumerator = (void*)return_null;
|
|
|
|
this->public.set.create_cert_enumerator = (void*)create_enumerator;
|
|
|
|
this->public.set.create_shared_enumerator = (void*)return_null;
|
|
|
|
this->public.set.create_cdp_enumerator = (void*)return_null;
|
2008-07-30 11:38:44 +00:00
|
|
|
this->public.set.cache_cert = (void*)nop;
|
2008-03-20 14:31:36 +00:00
|
|
|
this->public.issued_by = (bool(*)(cert_cache_t*, certificate_t *subject, certificate_t *issuer))issued_by;
|
2008-03-27 06:37:29 +00:00
|
|
|
this->public.flush = (void(*)(cert_cache_t*, certificate_type_t type))flush;
|
2008-03-20 14:31:36 +00:00
|
|
|
this->public.destroy = (void(*)(cert_cache_t*))destroy;
|
|
|
|
|
|
|
|
this->relations = linked_list_create();
|
2008-07-30 11:38:44 +00:00
|
|
|
this->enumerating = 0;
|
2008-03-28 08:38:51 +00:00
|
|
|
this->check_required = FALSE;
|
2008-07-30 11:38:44 +00:00
|
|
|
pthread_rwlock_init(&this->lock, NULL);
|
2008-03-20 14:31:36 +00:00
|
|
|
|
|
|
|
return &this->public;
|
|
|
|
}
|
|
|
|
|