use a pluto specific credential builder to build pluto cert_t's

This commit is contained in:
Martin Willi
2009-08-26 11:23:49 +02:00
parent c486fa8158
commit 11aa7e7869
6 changed files with 112 additions and 22 deletions
@@ -24,6 +24,7 @@ ENUM(credential_type_names, CRED_PRIVATE_KEY, CRED_CERTIFICATE,
"CRED_PRIVATE_KEY", "CRED_PRIVATE_KEY",
"CRED_PUBLIC_KEY", "CRED_PUBLIC_KEY",
"CRED_CERTIFICATE", "CRED_CERTIFICATE",
"CRED_PLUTO_CERT",
); );
typedef struct private_credential_factory_t private_credential_factory_t; typedef struct private_credential_factory_t private_credential_factory_t;
@@ -36,6 +36,8 @@ enum credential_type_t {
CRED_PUBLIC_KEY, CRED_PUBLIC_KEY,
/** certificates, implemented in certificate_t */ /** certificates, implemented in certificate_t */
CRED_CERTIFICATE, CRED_CERTIFICATE,
/** deprecated pluto style certificates */
CRED_PLUTO_CERT,
}; };
/** /**
@@ -96,9 +98,9 @@ struct credential_factory_t {
builder_constructor_t constructor); builder_constructor_t constructor);
/** /**
* Destroy a credential_factory instance. * Destroy a credential_factory instance.
*/ */
void (*destroy)(credential_factory_t *this); void (*destroy)(credential_factory_t *this);
}; };
/** /**
@@ -561,3 +561,11 @@ builder_t *certificate_pem_builder(certificate_type_t type)
return pem_builder(CRED_CERTIFICATE, type); return pem_builder(CRED_CERTIFICATE, type);
} }
/**
* Pluto specific cert builder.
*/
builder_t *pluto_pem_builder(certificate_type_t type)
{
return pem_builder(CRED_PLUTO_CERT, type);
}
@@ -48,5 +48,13 @@ builder_t *public_key_pem_builder(key_type_t type);
*/ */
builder_t *certificate_pem_builder(certificate_type_t type); builder_t *certificate_pem_builder(certificate_type_t type);
/**
* Builder for PEM encoded pluto certificates of all kind.
*
* @param type type of the key
* @return builder instance
*/
builder_t *pluto_pem_builder(certificate_type_t type);
#endif /** PEM_PRIVATE_KEY_H_ @}*/ #endif /** PEM_PRIVATE_KEY_H_ @}*/
@@ -42,6 +42,8 @@ static void destroy(private_pem_plugin_t *this)
(builder_constructor_t)public_key_pem_builder); (builder_constructor_t)public_key_pem_builder);
lib->creds->remove_builder(lib->creds, lib->creds->remove_builder(lib->creds,
(builder_constructor_t)certificate_pem_builder); (builder_constructor_t)certificate_pem_builder);
lib->creds->remove_builder(lib->creds,
(builder_constructor_t)pluto_pem_builder);
free(this); free(this);
} }
@@ -92,6 +94,14 @@ plugin_t *plugin_create()
lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_PGP, lib->creds->add_builder(lib->creds, CRED_CERTIFICATE, CERT_PGP,
(builder_constructor_t)certificate_pem_builder); (builder_constructor_t)certificate_pem_builder);
/* pluto specific credentials formats */
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, 0,
(builder_constructor_t)pluto_cert_pem_builder);
lib->creds->add_builder(lib->creds, CRED_PLUTO_CRL, 0,
(builder_constructor_t)pluto_crl_pem_builder);
lib->creds->add_builder(lib->creds, CRED_PLUTO_AC, 0,
(builder_constructor_t)pluto_ac_pem_builder);
return &this->public.plugin; return &this->public.plugin;
} }
+79 -18
View File
@@ -215,53 +215,114 @@ private_key_t* load_private_key(char* filename, prompt_pass_t *pass,
} }
/** /**
* Loads a X.509 or OpenPGP certificate * currently building cert_t
*/ */
bool load_cert(char *filename, const char *label, cert_t *cert) static cert_t *cert_builder_cert;
/**
* builder add function
*/
static void add(builder_t *this, builder_part_t part, ...)
{ {
bool pgp = FALSE; chunk_t blob;
chunk_t blob = chunk_empty; va_list args;
/* initialize cert struct */ va_start(args, part);
cert->type = CERT_NONE; blob = va_arg(args, chunk_t);
cert->u.x509 = NULL; va_end(args);
if (load_coded_file(filename, NULL, label, &blob, &pgp)) switch (part)
{ {
if (pgp) case BUILD_BLOB_PGP:
{ {
pgpcert_t *pgpcert = malloc_thing(pgpcert_t); pgpcert_t *pgpcert = malloc_thing(pgpcert_t);
*pgpcert = pgpcert_empty; *pgpcert = pgpcert_empty;
if (parse_pgp(blob, pgpcert)) if (parse_pgp(blob, pgpcert))
{ {
cert->type = CERT_PGP; cert_builder_cert->type = CERT_PGP;
cert->u.pgp = pgpcert; cert_builder_cert->u.pgp = pgpcert;
return TRUE;
} }
else else
{ {
plog(" error in OpenPGP certificate"); plog(" error in OpenPGP certificate");
free_pgpcert(pgpcert); free_pgpcert(pgpcert);
return FALSE;
} }
break;
} }
else case BUILD_BLOB_ASN1_DER:
{ {
x509cert_t *x509cert = malloc_thing(x509cert_t); x509cert_t *x509cert = malloc_thing(x509cert_t);
*x509cert = empty_x509cert; *x509cert = empty_x509cert;
if (parse_x509cert(blob, 0, x509cert)) if (parse_x509cert(blob, 0, x509cert))
{ {
cert->type = CERT_X509_SIGNATURE; cert_builder_cert->type = CERT_X509_SIGNATURE;
cert->u.x509 = x509cert; cert_builder_cert->u.x509 = x509cert;
return TRUE;
} }
else else
{ {
plog(" error in X.509 certificate"); plog(" error in X.509 certificate");
free_x509cert(x509cert); free_x509cert(x509cert);
return FALSE;
} }
break;
} }
default:
builder_cancel(this);
break;
}
}
/**
* builder build function
*/
static void *build(builder_t *this)
{
free(this);
if (cert_builder_cert->type == CERT_NONE)
{
return NULL;
}
return cert_builder_cert;
}
/**
* certificate builder in cert_t format.
*/
static builder_t *cert_builder(credential_type_t type, int subtype)
{
builder_t *this;
if (subtype != 1)
{
return NULL;
}
this = malloc_thing(builder_t);
this->add = add;
this->build = build;
return this;
}
/**
* Loads a X.509 or OpenPGP certificate
*/
bool load_cert(char *filename, const char *label, cert_t *cert)
{
cert_builder_cert = cert;
cert->type = CERT_NONE;
cert->u.x509 = NULL;
cert->u.pgp = NULL;
/* hook in builder functions to build pluto specific certificate format */
lib->creds->add_builder(lib->creds, CRED_PLUTO_CERT, 1,
(builder_constructor_t)cert_builder);
cert = lib->creds->create(lib->creds, CRED_PLUTO_CERT, 1,
BUILD_FROM_FILE, filename, BUILD_END);
lib->creds->remove_builder(lib->creds,
(builder_constructor_t)cert_builder);
if (cert)
{
return TRUE;
} }
return FALSE; return FALSE;
} }