implemented NetworkManager certificate/private key authentication using ssh-agent
This commit is contained in:
@@ -49,12 +49,54 @@ struct private_nm_creds_t {
|
||||
*/
|
||||
char *pass;
|
||||
|
||||
/**
|
||||
* users certificate
|
||||
*/
|
||||
certificate_t *usercert;
|
||||
|
||||
/**
|
||||
* users private key
|
||||
*/
|
||||
private_key_t *key;
|
||||
|
||||
/**
|
||||
* read/write lock
|
||||
*/
|
||||
pthread_rwlock_t lock;
|
||||
};
|
||||
|
||||
/**
|
||||
* Enumerator for user certificate
|
||||
*/
|
||||
static enumerator_t *create_usercert_enumerator(private_nm_creds_t *this,
|
||||
certificate_type_t cert, key_type_t key)
|
||||
{
|
||||
public_key_t *public;
|
||||
|
||||
if (cert != CERT_ANY && cert != this->usercert->get_type(this->usercert))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (key != KEY_ANY)
|
||||
{
|
||||
public = this->usercert->get_public_key(this->usercert);
|
||||
if (!public)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (public->get_type(public) != key)
|
||||
{
|
||||
public->destroy(public);
|
||||
return NULL;
|
||||
}
|
||||
public->destroy(public);
|
||||
}
|
||||
pthread_rwlock_rdlock(&this->lock);
|
||||
return enumerator_create_cleaner(
|
||||
enumerator_create_single(this->usercert, NULL),
|
||||
(void*)pthread_rwlock_unlock, &this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements credential_set_t.create_cert_enumerator
|
||||
*/
|
||||
@@ -62,6 +104,12 @@ static enumerator_t* create_cert_enumerator(private_nm_creds_t *this,
|
||||
certificate_type_t cert, key_type_t key,
|
||||
identification_t *id, bool trusted)
|
||||
{
|
||||
if (id && this->usercert &&
|
||||
id->equals(id, this->usercert->get_subject(this->usercert)))
|
||||
{
|
||||
return create_usercert_enumerator(this, cert, key);
|
||||
}
|
||||
|
||||
if (!this->cert)
|
||||
{
|
||||
return NULL;
|
||||
@@ -95,6 +143,35 @@ static enumerator_t* create_cert_enumerator(private_nm_creds_t *this,
|
||||
(void*)pthread_rwlock_unlock, &this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements credential_set_t.create_cert_enumerator
|
||||
*/
|
||||
static enumerator_t* create_private_enumerator(private_nm_creds_t *this,
|
||||
key_type_t type, identification_t *id)
|
||||
{
|
||||
if (this->key == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (type != KEY_ANY && type != this->key->get_type(this->key))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (id && id->get_type(id) != ID_ANY)
|
||||
{
|
||||
identification_t *keyid;
|
||||
|
||||
keyid = this->key->get_id(this->key, id->get_type(id));
|
||||
if (!keyid || !keyid->equals(keyid, id))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
pthread_rwlock_rdlock(&this->lock);
|
||||
return enumerator_create_cleaner(enumerator_create_single(this->key, NULL),
|
||||
(void*)pthread_rwlock_unlock, &this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* shared key enumerator implementation
|
||||
*/
|
||||
@@ -179,7 +256,7 @@ static void set_certificate(private_nm_creds_t *this, certificate_t *cert)
|
||||
/**
|
||||
* Implementation of nm_creds_t.set_password
|
||||
*/
|
||||
static void set_password(private_nm_creds_t *this, identification_t *id,
|
||||
static void set_username_password(private_nm_creds_t *this, identification_t *id,
|
||||
char *password)
|
||||
{
|
||||
pthread_rwlock_wrlock(&this->lock);
|
||||
@@ -188,18 +265,47 @@ static void set_password(private_nm_creds_t *this, identification_t *id,
|
||||
this->user = identification_create_from_encoding(ID_EAP,
|
||||
id->get_encoding(id));
|
||||
free(this->pass);
|
||||
this->pass = strdup(password);
|
||||
this->pass = password ? strdup(password) : NULL;
|
||||
pthread_rwlock_unlock(&this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of nm_creds_t.set_cert_and_key
|
||||
*/
|
||||
static void set_cert_and_key(private_nm_creds_t *this, certificate_t *cert,
|
||||
private_key_t *key)
|
||||
{
|
||||
pthread_rwlock_wrlock(&this->lock);
|
||||
DESTROY_IF(this->key);
|
||||
DESTROY_IF(this->usercert);
|
||||
this->key = key;
|
||||
this->usercert = cert;
|
||||
pthread_rwlock_unlock(&this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of nm_creds_t.clear
|
||||
*/
|
||||
static void clear(private_nm_creds_t *this)
|
||||
{
|
||||
DESTROY_IF(this->cert);
|
||||
DESTROY_IF(this->user);
|
||||
free(this->pass);
|
||||
DESTROY_IF(this->usercert);
|
||||
DESTROY_IF(this->key);
|
||||
this->key = NULL;
|
||||
this->usercert = NULL;
|
||||
this->pass = NULL;
|
||||
this->cert = NULL;
|
||||
this->user = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of nm_creds_t.destroy
|
||||
*/
|
||||
static void destroy(private_nm_creds_t *this)
|
||||
{
|
||||
DESTROY_IF(this->cert);
|
||||
DESTROY_IF(this->user);
|
||||
free(this->pass);
|
||||
clear(this);
|
||||
pthread_rwlock_destroy(&this->lock);
|
||||
free(this);
|
||||
}
|
||||
@@ -211,13 +317,15 @@ nm_creds_t *nm_creds_create()
|
||||
{
|
||||
private_nm_creds_t *this = malloc_thing(private_nm_creds_t);
|
||||
|
||||
this->public.set.create_private_enumerator = (void*)return_null;
|
||||
this->public.set.create_private_enumerator = (void*)create_private_enumerator;
|
||||
this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
|
||||
this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
|
||||
this->public.set.create_cdp_enumerator = (void*)return_null;
|
||||
this->public.set.cache_cert = (void*)nop;
|
||||
this->public.set_certificate = (void(*)(nm_creds_t*, certificate_t *cert))set_certificate;
|
||||
this->public.set_password = (void(*)(nm_creds_t*, identification_t *id, char *password))set_password;
|
||||
this->public.set_username_password = (void(*)(nm_creds_t*, identification_t *id, char *password))set_username_password;
|
||||
this->public.set_cert_and_key = (void(*)(nm_creds_t*, certificate_t *cert, private_key_t *key))set_cert_and_key;
|
||||
this->public.clear = (void(*)(nm_creds_t*))clear;
|
||||
this->public.destroy = (void(*)(nm_creds_t*))destroy;
|
||||
|
||||
pthread_rwlock_init(&this->lock, NULL);
|
||||
@@ -225,6 +333,8 @@ nm_creds_t *nm_creds_create()
|
||||
this->cert = NULL;
|
||||
this->user = NULL;
|
||||
this->pass = NULL;
|
||||
this->usercert = NULL;
|
||||
this->key = NULL;
|
||||
|
||||
return &this->public;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user