unified pluto builder implementations
This commit is contained in:
+83
-124
@@ -33,15 +33,24 @@
|
|||||||
#include "ac.h"
|
#include "ac.h"
|
||||||
#include "crl.h"
|
#include "crl.h"
|
||||||
|
|
||||||
/**
|
typedef struct private_builder_t private_builder_t;
|
||||||
* currently building cert_t
|
|
||||||
*/
|
struct private_builder_t {
|
||||||
static cert_t *cert;
|
/** implements builder interface */
|
||||||
|
builder_t public;
|
||||||
|
/** built credential */
|
||||||
|
union {
|
||||||
|
void *cred;
|
||||||
|
cert_t *cert;
|
||||||
|
x509crl_t *crl;
|
||||||
|
x509acert_t *ac;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* builder add function
|
* builder add function for certificates
|
||||||
*/
|
*/
|
||||||
static void cert_add(builder_t *this, builder_part_t part, ...)
|
static void cert_add(private_builder_t *this, builder_part_t part, ...)
|
||||||
{
|
{
|
||||||
chunk_t blob;
|
chunk_t blob;
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -58,8 +67,9 @@ static void cert_add(builder_t *this, builder_part_t part, ...)
|
|||||||
*pgpcert = pgpcert_empty;
|
*pgpcert = pgpcert_empty;
|
||||||
if (parse_pgp(blob, pgpcert))
|
if (parse_pgp(blob, pgpcert))
|
||||||
{
|
{
|
||||||
cert->type = CERT_PGP;
|
this->cert = malloc_thing(cert_t);
|
||||||
cert->u.pgp = pgpcert;
|
this->cert->type = CERT_PGP;
|
||||||
|
this->cert->u.pgp = pgpcert;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -74,8 +84,9 @@ static void cert_add(builder_t *this, builder_part_t part, ...)
|
|||||||
*x509cert = empty_x509cert;
|
*x509cert = empty_x509cert;
|
||||||
if (parse_x509cert(blob, 0, x509cert))
|
if (parse_x509cert(blob, 0, x509cert))
|
||||||
{
|
{
|
||||||
cert->type = CERT_X509_SIGNATURE;
|
this->cert = malloc_thing(cert_t);
|
||||||
cert->u.x509 = x509cert;
|
this->cert->type = CERT_X509_SIGNATURE;
|
||||||
|
this->cert->u.x509 = x509cert;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -85,55 +96,30 @@ static void cert_add(builder_t *this, builder_part_t part, ...)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
builder_cancel(this);
|
if (this->cert)
|
||||||
|
{
|
||||||
|
switch (this->cert->type)
|
||||||
|
{
|
||||||
|
case CERT_X509_SIGNATURE:
|
||||||
|
free_x509cert(this->cert->u.x509);
|
||||||
|
break;
|
||||||
|
case CERT_PGP:
|
||||||
|
free_pgpcert(this->cert->u.pgp);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
free(this->cert);
|
||||||
|
}
|
||||||
|
builder_cancel(&this->public);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* builder build function
|
* builder add function for attribute certificates
|
||||||
*/
|
*/
|
||||||
static void *cert_build(builder_t *this)
|
static void ac_add(private_builder_t *this, builder_part_t part, ...)
|
||||||
{
|
|
||||||
free(this);
|
|
||||||
if (cert->type == CERT_NONE)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return cert;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* certificate builder in cert_t format.
|
|
||||||
*/
|
|
||||||
static builder_t *cert_builder(credential_type_t type, int subtype)
|
|
||||||
{
|
|
||||||
builder_t *this;
|
|
||||||
|
|
||||||
if (subtype != CRED_TYPE_CERTIFICATE)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
this = malloc_thing(builder_t);
|
|
||||||
this->add = cert_add;
|
|
||||||
this->build = cert_build;
|
|
||||||
|
|
||||||
cert->type = CERT_NONE;
|
|
||||||
cert->u.x509 = NULL;
|
|
||||||
cert->u.pgp = NULL;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* currently building x509ac_t
|
|
||||||
*/
|
|
||||||
static x509acert_t *ac;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* builder add function
|
|
||||||
*/
|
|
||||||
static void ac_add(builder_t *this, builder_part_t part, ...)
|
|
||||||
{
|
{
|
||||||
chunk_t blob;
|
chunk_t blob;
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -146,61 +132,28 @@ static void ac_add(builder_t *this, builder_part_t part, ...)
|
|||||||
blob = va_arg(args, chunk_t);
|
blob = va_arg(args, chunk_t);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
ac = malloc_thing(x509acert_t);
|
this->ac = malloc_thing(x509acert_t);
|
||||||
|
|
||||||
*ac = empty_ac;
|
*this->ac = empty_ac;
|
||||||
|
|
||||||
if (!parse_ac(blob, ac) && !verify_x509acert(ac, FALSE))
|
if (!parse_ac(blob, this->ac) && !verify_x509acert(this->ac, FALSE))
|
||||||
{
|
{
|
||||||
free_acert(ac);
|
free_acert(this->ac);
|
||||||
ac = NULL;
|
this->ac = NULL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
builder_cancel(this);
|
free_acert(this->ac);
|
||||||
|
builder_cancel(&this->public);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* builder build function
|
* builder add function for crls
|
||||||
*/
|
*/
|
||||||
static void *ac_build(builder_t *this)
|
static void crl_add(private_builder_t *this, builder_part_t part, ...)
|
||||||
{
|
|
||||||
free(this);
|
|
||||||
return ac;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* certificate builder in x509ac_t format.
|
|
||||||
*/
|
|
||||||
static builder_t *ac_builder(credential_type_t type, int subtype)
|
|
||||||
{
|
|
||||||
builder_t *this;
|
|
||||||
|
|
||||||
if (subtype != CRED_TYPE_AC)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
this = malloc_thing(builder_t);
|
|
||||||
this->add = ac_add;
|
|
||||||
this->build = ac_build;
|
|
||||||
|
|
||||||
ac = NULL;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* currently building x509crl_t
|
|
||||||
*/
|
|
||||||
static x509crl_t *crl;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* builder add function
|
|
||||||
*/
|
|
||||||
static void crl_add(builder_t *this, builder_part_t part, ...)
|
|
||||||
{
|
{
|
||||||
chunk_t blob;
|
chunk_t blob;
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -213,19 +166,20 @@ static void crl_add(builder_t *this, builder_part_t part, ...)
|
|||||||
blob = va_arg(args, chunk_t);
|
blob = va_arg(args, chunk_t);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
crl = malloc_thing(x509crl_t);
|
this->crl = malloc_thing(x509crl_t);
|
||||||
*crl = empty_x509crl;
|
*this->crl = empty_x509crl;
|
||||||
|
|
||||||
if (!parse_x509crl(blob, 0, crl))
|
if (!parse_x509crl(blob, 0, this->crl))
|
||||||
{
|
{
|
||||||
plog(" error in X.509 crl");
|
plog(" error in X.509 crl");
|
||||||
free_crl(crl);
|
free_crl(this->crl);
|
||||||
crl = NULL;
|
this->crl = NULL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
builder_cancel(this);
|
free_crl(this->crl);
|
||||||
|
builder_cancel(&this->public);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,47 +187,52 @@ static void crl_add(builder_t *this, builder_part_t part, ...)
|
|||||||
/**
|
/**
|
||||||
* builder build function
|
* builder build function
|
||||||
*/
|
*/
|
||||||
static void *crl_build(builder_t *this)
|
static void *build(private_builder_t *this)
|
||||||
{
|
{
|
||||||
|
void *cred;
|
||||||
|
|
||||||
|
cred = this->cred;
|
||||||
free(this);
|
free(this);
|
||||||
return crl;
|
|
||||||
|
return cred;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CRL builder in x509crl_t format.
|
* builder for pluto credentials
|
||||||
*/
|
*/
|
||||||
static builder_t *crl_builder(credential_type_t type, int subtype)
|
static builder_t *builder(credential_type_t type, int subtype)
|
||||||
{
|
{
|
||||||
builder_t *this;
|
private_builder_t *this = malloc_thing(private_builder_t);
|
||||||
|
|
||||||
if (subtype != CRED_TYPE_CRL)
|
switch (subtype)
|
||||||
{
|
{
|
||||||
return NULL;
|
case CRED_TYPE_CERTIFICATE:
|
||||||
|
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))cert_add;
|
||||||
|
break;
|
||||||
|
case CRED_TYPE_AC:
|
||||||
|
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))ac_add;
|
||||||
|
break;
|
||||||
|
case CRED_TYPE_CRL:
|
||||||
|
this->public.add = (void(*)(builder_t *this, builder_part_t part, ...))crl_add;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
free(this);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
this = malloc_thing(builder_t);
|
this->public.build = (void*(*)(builder_t*))build;
|
||||||
this->add = crl_add;
|
this->cred = NULL;
|
||||||
this->build = crl_build;
|
|
||||||
|
|
||||||
crl = NULL;
|
return &this->public;
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void init_builder(void)
|
void init_builder(void)
|
||||||
{
|
{
|
||||||
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, CRED_TYPE_CERTIFICATE,
|
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, 0,
|
||||||
(builder_constructor_t)cert_builder);
|
(builder_constructor_t)builder);
|
||||||
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, CRED_TYPE_AC,
|
|
||||||
(builder_constructor_t)ac_builder);
|
|
||||||
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, CRED_TYPE_CRL,
|
|
||||||
(builder_constructor_t)crl_builder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_builder(void)
|
void free_builder(void)
|
||||||
{
|
{
|
||||||
lib->creds->remove_builder(lib->creds, (builder_constructor_t)cert_builder);
|
lib->creds->remove_builder(lib->creds, (builder_constructor_t)builder);
|
||||||
lib->creds->remove_builder(lib->creds, (builder_constructor_t)ac_builder);
|
|
||||||
lib->creds->remove_builder(lib->creds, (builder_constructor_t)crl_builder);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -159,9 +159,9 @@ bool load_cert(char *filename, const char *label, cert_t *out)
|
|||||||
BUILD_FROM_FILE, filename, BUILD_END);
|
BUILD_FROM_FILE, filename, BUILD_END);
|
||||||
if (cert)
|
if (cert)
|
||||||
{
|
{
|
||||||
/* As the API passes an empty cert_t, the CRED_TYPE_CERTIFICATE
|
/* the API passes an empty cert_t, we move over and free the built one */
|
||||||
* returns a statically allocated cert to copy. */
|
|
||||||
*out = *cert;
|
*out = *cert;
|
||||||
|
free(cert);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|||||||
Reference in New Issue
Block a user