Updated pubkey plugin to the new builder API

This commit is contained in:
Martin Willi
2009-09-10 16:20:20 +02:00
parent 872176d350
commit 91ef5c66ab
3 changed files with 31 additions and 74 deletions
+23 -68
View File
@@ -242,89 +242,44 @@ static pubkey_cert_t *pubkey_cert_create(public_key_t *key)
return &this->public; return &this->public;
} }
typedef struct private_builder_t private_builder_t;
/** /**
* Builder implementation for key loading * See header.
*/ */
struct private_builder_t { pubkey_cert_t *pubkey_cert_wrap(certificate_type_t type, va_list args)
/** implements the builder interface */
builder_t public;
/** loaded public key */
pubkey_cert_t *key;
};
/**
* Implementation of builder_t.build
*/
static pubkey_cert_t *build(private_builder_t *this)
{ {
pubkey_cert_t *key = this->key; public_key_t *key = NULL;
chunk_t blob = chunk_empty;
free(this); while (TRUE)
return key;
}
/**
* Implementation of builder_t.add
*/
static void add(private_builder_t *this, builder_part_t part, ...)
{
if (!this->key)
{ {
public_key_t *key; switch (va_arg(args, builder_part_t))
va_list args;
switch (part)
{ {
case BUILD_BLOB_ASN1_DER: case BUILD_BLOB_ASN1_DER:
{ blob = va_arg(args, chunk_t);
va_start(args, part); continue;
key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
va_arg(args, chunk_t));
if (key)
{
this->key = pubkey_cert_create(key);
}
va_end(args);
return;
}
case BUILD_PUBLIC_KEY: case BUILD_PUBLIC_KEY:
{
va_start(args, part);
key = va_arg(args, public_key_t*); key = va_arg(args, public_key_t*);
pubkey_cert_create(key->get_ref(key)); continue;
va_end(args); case BUILD_END:
return;
}
default:
break; break;
default:
return NULL;
} }
break;
} }
if (this->key) if (key)
{ {
destroy((private_pubkey_cert_t*)this->key); key->get_ref(key);
} }
builder_cancel(&this->public); else if (blob.ptr)
}
/**
* Builder construction function
*/
builder_t *pubkey_cert_builder(certificate_type_t type)
{
private_builder_t *this;
if (type != CERT_TRUSTED_PUBKEY)
{ {
return NULL; key = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
BUILD_BLOB_ASN1_DER, blob, BUILD_END);
} }
if (key)
this = malloc_thing(private_builder_t); {
return pubkey_cert_create(key);
this->key = NULL; }
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))add; return NULL;
this->public.build = (void*(*)(builder_t *this))build;
return &this->public;
} }
@@ -21,6 +21,7 @@
#ifndef PUBKEY_CERT_H_ #ifndef PUBKEY_CERT_H_
#define PUBKEY_CERT_H_ #define PUBKEY_CERT_H_
#include <credentials/builder.h>
#include <credentials/certificates/certificate.h> #include <credentials/certificates/certificate.h>
typedef struct pubkey_cert_t pubkey_cert_t; typedef struct pubkey_cert_t pubkey_cert_t;
@@ -37,13 +38,14 @@ struct pubkey_cert_t {
}; };
/** /**
* Create the builder for a trusted public key. * Create a trusted public key cert using a public key.
* *
* The builders add() function takes BUILD_PUBLIC_KEY to enwrap. * The build accepts a BUILD_PUBLIC_KEY or a BUILD_BLOB_ASN1_DER part.
* *
* @param type type of the certificate, must be CERT_pubkey_cert * @param type type of the certificate, must be CERT_pubkey_cert
* @return builder instance * @param args builder_part_t argument list
* @return pubkey_cert_t, NULL on failure
*/ */
builder_t *pubkey_cert_builder(certificate_type_t type); pubkey_cert_t *pubkey_cert_wrap(certificate_type_t type, va_list args);
#endif /** PUBKEY_CERT_H_ @}*/ #endif /** PUBKEY_CERT_H_ @}*/
@@ -37,7 +37,7 @@ struct private_pubkey_plugin_t {
static void destroy(private_pubkey_plugin_t *this) static void destroy(private_pubkey_plugin_t *this)
{ {
lib->creds->remove_builder(lib->creds, lib->creds->remove_builder(lib->creds,
(builder_constructor_t)pubkey_cert_builder); (builder_function_t)pubkey_cert_wrap);
free(this); free(this);
} }
@@ -51,7 +51,7 @@ plugin_t *plugin_create()
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY, lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
(builder_constructor_t)pubkey_cert_builder); (builder_function_t)pubkey_cert_wrap);
return &this->public.plugin; return &this->public.plugin;
} }