Renamed key_encod{ing,der}_t and constants, prepare for generic credential encoding

This commit is contained in:
Martin Willi
2010-07-13 11:29:35 +02:00
parent 24d327ab4d
commit da9724e6d0
55 changed files with 358 additions and 359 deletions
+48 -48
View File
@@ -21,25 +21,25 @@
#include <utils/hashtable.h>
#include <threading/rwlock.h>
typedef struct private_key_encoding_t private_key_encoding_t;
typedef struct private_cred_encoding_t private_cred_encoding_t;
/**
* Private data of an key_encoding_t object.
* Private data of an cred_encoding_t object.
*/
struct private_key_encoding_t {
struct private_cred_encoding_t {
/**
* Public key_encoding_t interface.
* Public cred_encoding_t interface.
*/
key_encoding_t public;
cred_encoding_t public;
/**
* cached encodings, a table for each encoding_type_t, containing chunk_t*
*/
hashtable_t *cache[KEY_ENCODING_MAX];
hashtable_t *cache[CRED_ENCODING_MAX];
/**
* Registered encoding fuctions, key_encoder_t
* Registered encoding fuctions, cred_encoder_t
*/
linked_list_t *encoders;
@@ -52,7 +52,7 @@ struct private_key_encoding_t {
/**
* See header.
*/
bool key_encoding_args(va_list args, ...)
bool cred_encoding_args(va_list args, ...)
{
va_list parts, copy;
bool failed = FALSE;
@@ -61,12 +61,12 @@ bool key_encoding_args(va_list args, ...)
while (!failed)
{
key_encoding_part_t current, target;
cred_encoding_part_t current, target;
chunk_t *out, data;
/* get the part we are looking for */
target = va_arg(parts, key_encoding_part_t);
if (target == KEY_PART_END)
target = va_arg(parts, cred_encoding_part_t);
if (target == CRED_PART_END)
{
break;
}
@@ -75,8 +75,8 @@ bool key_encoding_args(va_list args, ...)
va_copy(copy, args);
while (!failed)
{
current = va_arg(copy, key_encoding_part_t);
if (current == KEY_PART_END)
current = va_arg(copy, cred_encoding_part_t);
if (current == CRED_PART_END)
{
failed = TRUE;
break;
@@ -111,14 +111,14 @@ static bool equals(void *key1, void *key2)
}
/**
* Implementation of key_encoding_t.get_cache
* Implementation of cred_encoding_t.get_cache
*/
static bool get_cache(private_key_encoding_t *this, key_encoding_type_t type,
static bool get_cache(private_cred_encoding_t *this, cred_encoding_type_t type,
void *cache, chunk_t *encoding)
{
chunk_t *chunk;
if (type >= KEY_ENCODING_MAX || type < 0)
if (type >= CRED_ENCODING_MAX || type < 0)
{
return FALSE;
}
@@ -133,18 +133,18 @@ static bool get_cache(private_key_encoding_t *this, key_encoding_type_t type,
}
/**
* Implementation of key_encoding_t.encode
* Implementation of cred_encoding_t.encode
*/
static bool encode(private_key_encoding_t *this, key_encoding_type_t type,
static bool encode(private_cred_encoding_t *this, cred_encoding_type_t type,
void *cache, chunk_t *encoding, ...)
{
enumerator_t *enumerator;
va_list args, copy;
key_encoder_t encode;
cred_encoder_t encode;
bool success = FALSE;
chunk_t *chunk;
if (type >= KEY_ENCODING_MAX || type < 0)
if (type >= CRED_ENCODING_MAX || type < 0)
{
return FALSE;
}
@@ -187,14 +187,14 @@ static bool encode(private_key_encoding_t *this, key_encoding_type_t type,
}
/**
* Implementation of key_encoding_t.cache
* Implementation of cred_encoding_t.cache
*/
static void cache(private_key_encoding_t *this, key_encoding_type_t type,
static void cache(private_cred_encoding_t *this, cred_encoding_type_t type,
void *cache, chunk_t encoding)
{
chunk_t *chunk;
if (type >= KEY_ENCODING_MAX || type < 0)
if (type >= CRED_ENCODING_MAX || type < 0)
{
return free(encoding.ptr);
}
@@ -212,15 +212,15 @@ static void cache(private_key_encoding_t *this, key_encoding_type_t type,
}
/**
* Implementation of key_encoding_t.clear_cache
* Implementation of cred_encoding_t.clear_cache
*/
static void clear_cache(private_key_encoding_t *this, void *cache)
static void clear_cache(private_cred_encoding_t *this, void *cache)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t *chunk;
this->lock->write_lock(this->lock);
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
chunk = this->cache[type]->remove(this->cache[type], cache);
if (chunk)
@@ -233,9 +233,9 @@ static void clear_cache(private_key_encoding_t *this, void *cache)
}
/**
* Implementation of key_encoding_t.add_encoder
* Implementation of cred_encoding_t.add_encoder
*/
static void add_encoder(private_key_encoding_t *this, key_encoder_t encoder)
static void add_encoder(private_cred_encoding_t *this, cred_encoder_t encoder)
{
this->lock->write_lock(this->lock);
this->encoders->insert_last(this->encoders, encoder);
@@ -243,9 +243,9 @@ static void add_encoder(private_key_encoding_t *this, key_encoder_t encoder)
}
/**
* Implementation of key_encoding_t.remove_encoder
* Implementation of cred_encoding_t.remove_encoder
*/
static void remove_encoder(private_key_encoding_t *this, key_encoder_t encoder)
static void remove_encoder(private_cred_encoding_t *this, cred_encoder_t encoder)
{
this->lock->write_lock(this->lock);
this->encoders->remove(this->encoders, encoder, NULL);
@@ -253,18 +253,18 @@ static void remove_encoder(private_key_encoding_t *this, key_encoder_t encoder)
}
/**
* Implementation of key_encoder_t.destroy.
* Implementation of cred_encoder_t.destroy.
*/
static void destroy(private_key_encoding_t *this)
static void destroy(private_cred_encoding_t *this)
{
key_encoding_type_t type;
cred_encoding_type_t type;
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
/* We explicitly do not free remaining encodings. All keys should
/* We explicitly do not free remaining encodings. All creds should
* have gone now, and they are responsible for cleaning out their
* cache entries. Not flushing here allows the leak detective to
* complain if a key did not flush cached encodings. */
* complain if a credential did not flush cached encodings. */
this->cache[type]->destroy(this->cache[type]);
}
this->encoders->destroy(this->encoders);
@@ -275,20 +275,20 @@ static void destroy(private_key_encoding_t *this)
/**
* See header
*/
key_encoding_t *key_encoding_create()
cred_encoding_t *cred_encoding_create()
{
private_key_encoding_t *this = malloc_thing(private_key_encoding_t);
key_encoding_type_t type;
private_cred_encoding_t *this = malloc_thing(private_cred_encoding_t);
cred_encoding_type_t type;
this->public.encode = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding, ...))encode;
this->public.get_cache = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding))get_cache;
this->public.cache = (void(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t encoding))cache;
this->public.clear_cache = (void(*)(key_encoding_t*, void *cache))clear_cache;
this->public.add_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))add_encoder;
this->public.remove_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))remove_encoder;
this->public.destroy = (void(*)(key_encoding_t*))destroy;
this->public.encode = (bool(*)(cred_encoding_t*, cred_encoding_type_t type, void *cache, chunk_t *encoding, ...))encode;
this->public.get_cache = (bool(*)(cred_encoding_t*, cred_encoding_type_t type, void *cache, chunk_t *encoding))get_cache;
this->public.cache = (void(*)(cred_encoding_t*, cred_encoding_type_t type, void *cache, chunk_t encoding))cache;
this->public.clear_cache = (void(*)(cred_encoding_t*, void *cache))clear_cache;
this->public.add_encoder = (void(*)(cred_encoding_t*, cred_encoder_t encoder))add_encoder;
this->public.remove_encoder = (void(*)(cred_encoding_t*, cred_encoder_t encoder))remove_encoder;
this->public.destroy = (void(*)(cred_encoding_t*))destroy;
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
this->cache[type] = hashtable_create(hash, equals, 8);
}
+80 -80
View File
@@ -14,134 +14,134 @@
*/
/**
* @defgroup key_encoding key_encoding
* @{ @ingroup keys
* @defgroup cred_encoding cred_encoding
* @{ @ingroup credentials
*/
#ifndef KEY_ENCODING_H_
#define KEY_ENCODING_H_
#ifndef CRED_ENCODING_H_
#define CRED_ENCODING_H_
typedef struct key_encoding_t key_encoding_t;
typedef enum key_encoding_type_t key_encoding_type_t;
typedef enum key_encoding_part_t key_encoding_part_t;
typedef struct cred_encoding_t cred_encoding_t;
typedef enum cred_encoding_type_t cred_encoding_type_t;
typedef enum cred_encoding_part_t cred_encoding_part_t;
#include <library.h>
/**
* Key encoder function implementing encoding/fingerprinting.
* Credential encoder function implementing encoding/fingerprinting.
*
* The variable argument list takes key_encoding_part_t, followed by part
* The variable argument list takes cred_encoding_part_t, followed by part
* specific arguments, terminated by KEY_PART_END.
*
* @param type format to encode the key to
* @param args list of (key_encoding_part_t, data)
* @param type format to encode the credential to
* @param args list of (cred_encoding_part_t, data)
* @param encoding encoding result, allocated
* @return TRUE if encoding successful
*/
typedef bool (*key_encoder_t)(key_encoding_type_t type, chunk_t *encoding,
va_list args);
typedef bool (*cred_encoder_t)(cred_encoding_type_t type, chunk_t *encoding,
va_list args);
/**
* Helper function for key_encoder_t implementations to parse argument list.
* Helper function for cred_encoder_t implementations to parse argument list.
*
* Key encoder functions get a variable argument list to parse. To simplify
* the job, this function reads the arguments and returns chunks for each
* part.
* The argument list of this function takes a key_encoding_part_t, followed
* by a data pointer receiving the value, terminated by KEY_PART_END.
* Credential encoder functions get a variable argument list to parse. To
* simplify the job, this function reads the arguments and returns chunks for
* each part.
* The argument list of this function takes a cred_encoding_part_t, followed
* by a data pointer receiving the value, terminated by CRED_PART_END.
*
* @param args argument list passed to key encoder function
* @param ... list of (key_encoding_part_t, data*)
* @param args argument list passed to credential encoder function
* @param ... list of (cred_encoding_part_t, data*)
* @return TRUE if all parts found, FALSE otherwise
*/
bool key_encoding_args(va_list args, ...);
bool cred_encoding_args(va_list args, ...);
/**
* Encoding type of a fingerprint/private-/public-key.
* Encoding type of a fingerprint/credential.
*
* Fingerprints have have the KEY_ID_*, public keys the KEY_PUB_* and
* private keys the KEY_PRIV_* prefix.
* Fingerprints have have the KEYID_*, public keys the PUBKEY_* and
* private keys the PRIVKEY_* prefix.
*/
enum key_encoding_type_t {
enum cred_encoding_type_t {
/** SHA1 fingerprint over subjectPublicKeyInfo */
KEY_ID_PUBKEY_INFO_SHA1 = 0,
KEYID_PUBKEY_INFO_SHA1 = 0,
/** SHA1 fingerprint over subjectPublicKey */
KEY_ID_PUBKEY_SHA1,
KEYID_PUBKEY_SHA1,
/** PGPv3 fingerprint */
KEY_ID_PGPV3,
KEYID_PGPV3,
/** PGPv4 fingerprint */
KEY_ID_PGPV4,
KEYID_PGPV4,
KEY_ID_MAX,
KEYID_MAX,
/** PKCS#1 and similar ASN.1 key encoding */
KEY_PUB_ASN1_DER,
KEY_PRIV_ASN1_DER,
PUBKEY_ASN1_DER,
PRIVKEY_ASN1_DER,
/** subjectPublicKeyInfo encoding */
KEY_PUB_SPKI_ASN1_DER,
PUBKEY_SPKI_ASN1_DER,
/** PEM encoded PKCS#1 key */
KEY_PUB_PEM,
KEY_PRIV_PEM,
PUBKEY_PEM,
PRIVKEY_PEM,
/** PGP key encoding */
KEY_PUB_PGP,
KEY_PRIV_PGP,
PUBKEY_PGP,
PRIVKEY_PGP,
KEY_ENCODING_MAX,
CRED_ENCODING_MAX,
};
/**
* Parts of a key to encode.
* Parts of a credential to encode.
*/
enum key_encoding_part_t {
enum cred_encoding_part_t {
/** modulus of a RSA key, n */
KEY_PART_RSA_MODULUS,
CRED_PART_RSA_MODULUS,
/** public exponent of a RSA key, e */
KEY_PART_RSA_PUB_EXP,
CRED_PART_RSA_PUB_EXP,
/** private exponent of a RSA key, d */
KEY_PART_RSA_PRIV_EXP,
CRED_PART_RSA_PRIV_EXP,
/** prime1 a RSA key, p */
KEY_PART_RSA_PRIME1,
CRED_PART_RSA_PRIME1,
/** prime2 a RSA key, q */
KEY_PART_RSA_PRIME2,
CRED_PART_RSA_PRIME2,
/** exponent1 a RSA key, exp1 */
KEY_PART_RSA_EXP1,
CRED_PART_RSA_EXP1,
/** exponent1 a RSA key, exp2 */
KEY_PART_RSA_EXP2,
CRED_PART_RSA_EXP2,
/** coefficient of RSA key, coeff */
KEY_PART_RSA_COEFF,
CRED_PART_RSA_COEFF,
/** a DER encoded RSA public key */
KEY_PART_RSA_PUB_ASN1_DER,
CRED_PART_RSA_PUB_ASN1_DER,
/** a DER encoded RSA private key */
KEY_PART_RSA_PRIV_ASN1_DER,
CRED_PART_RSA_PRIV_ASN1_DER,
/** a DER encoded ECDSA public key */
KEY_PART_ECDSA_PUB_ASN1_DER,
CRED_PART_ECDSA_PUB_ASN1_DER,
/** a DER encoded ECDSA private key */
KEY_PART_ECDSA_PRIV_ASN1_DER,
CRED_PART_ECDSA_PRIV_ASN1_DER,
KEY_PART_END,
CRED_PART_END,
};
/**
* Private/Public key encoding and fingerprinting facility.
* Credential encoding and fingerprinting facility.
*/
struct key_encoding_t {
struct cred_encoding_t {
/**
* Encode a key into a format using several key parts, optional caching.
* Encode a credential in a format using several parts, optional caching.
*
* The variable argument list takes key_encoding_part_t, followed by part
* specific arguments, terminated by KEY_PART_END.
* The variable argument list takes cred_encoding_part_t, followed by part
* specific arguments, terminated by CRED_PART_END.
* If a cache key is given, the returned encoding points to internal data:
* do not free or modify. If no cache key is given, the encoding is
* allocated and must be freed by the caller.
*
* @param type format the key should be encoded to
* @param type format the credential should be encoded to
* @param cache key to use for caching, NULL to not cache
* @param encoding encoding result, allocated if caching disabled
* @param ... list of (key_encoding_part_t, data)
* @param ... list of (cred_encoding_part_t, data)
* @return TRUE if encoding successful
*/
bool (*encode)(key_encoding_t *this, key_encoding_type_t type, void *cache,
bool (*encode)(cred_encoding_t *this, cred_encoding_type_t type, void *cache,
chunk_t *encoding, ...);
/**
@@ -149,55 +149,55 @@ struct key_encoding_t {
*
* @param cache key used in encode() for caching
*/
void (*clear_cache)(key_encoding_t *this, void *cache);
void (*clear_cache)(cred_encoding_t *this, void *cache);
/**
* Check for a cached encoding.
*
* @param type format of the key encoding
* @param type format of the credential encoding
* @param cache key to use for caching, as given to encode()
* @param encoding encoding result, internal data
* @return TRUE if cache entry found
*/
bool (*get_cache)(key_encoding_t *this, key_encoding_type_t type,
bool (*get_cache)(cred_encoding_t *this, cred_encoding_type_t type,
void *cache, chunk_t *encoding);
/**
* Cache a key encoding created externally.
* Cache a credential encoding created externally.
*
* After calling cache(), the passed encoding is owned by the key encoding
* After calling cache(), the passed encoding is owned by the cred encoding
* facility.
*
* @param type format of the key encoding
* @param type format of the credential encoding
* @param cache key to use for caching, as given to encode()
* @param encoding encoding to cache, gets owned by this
*/
void (*cache)(key_encoding_t *this, key_encoding_type_t type, void *cache,
void (*cache)(cred_encoding_t *this, cred_encoding_type_t type, void *cache,
chunk_t encoding);
/**
* Register a key encoder function.
* Register a credential encoder function.
*
* @param encoder key encoder function to add
* @param encoder credential encoder function to add
*/
void (*add_encoder)(key_encoding_t *this, key_encoder_t encoder);
void (*add_encoder)(cred_encoding_t *this, cred_encoder_t encoder);
/**
* Unregister a previously registered key encoder function.
* Unregister a previously registered credential encoder function.
*
* @param encoder key encoder function to remove
* @param encoder credential encoder function to remove
*/
void (*remove_encoder)(key_encoding_t *this, key_encoder_t encoder);
void (*remove_encoder)(cred_encoding_t *this, cred_encoder_t encoder);
/**
* Destroy a key_encoding_t.
* Destroy a cred_encoding_t.
*/
void (*destroy)(key_encoding_t *this);
void (*destroy)(cred_encoding_t *this);
};
/**
* Create a key_encoding instance.
* Create a cred_encoding instance.
*/
key_encoding_t *key_encoding_create();
cred_encoding_t *cred_encoding_create();
#endif /** KEY_ENCODING_H_ @}*/
#endif /** CRED_ENCODING_H_ @}*/
@@ -909,7 +909,7 @@ static private_key_t *get_private_by_cert(private_credential_manager_t *this,
public = cert->get_public_key(cert);
if (public)
{
if (public->get_fingerprint(public, KEY_ID_PUBKEY_SHA1, &chunk))
if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &chunk))
{
keyid = identification_create_from_encoding(ID_KEY_ID, chunk);
private = get_private_by_keyid(this, type, keyid);
@@ -20,7 +20,7 @@
*/
bool private_key_equals(private_key_t *this, private_key_t *other)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t a, b;
if (this == other)
@@ -28,7 +28,7 @@ bool private_key_equals(private_key_t *this, private_key_t *other)
return TRUE;
}
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
if (this->get_fingerprint(this, type, &a) &&
other->get_fingerprint(other, type, &b))
@@ -44,10 +44,10 @@ bool private_key_equals(private_key_t *this, private_key_t *other)
*/
bool private_key_belongs_to(private_key_t *private, public_key_t *public)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t a, b;
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
if (private->get_fingerprint(private, type, &a) &&
public->get_fingerprint(public, type, &b))
@@ -63,10 +63,10 @@ bool private_key_belongs_to(private_key_t *private, public_key_t *public)
*/
bool private_key_has_fingerprint(private_key_t *private, chunk_t fingerprint)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t current;
for (type = 0; type < KEY_ID_MAX; type++)
for (type = 0; type < KEYID_MAX; type++)
{
if (private->get_fingerprint(private, type, &current) &&
chunk_equals(current, fingerprint))
@@ -90,11 +90,11 @@ struct private_key_t {
/**
* Get the fingerprint of the key.
*
* @param type type of fingerprint, one of KEY_ID_*
* @param type type of fingerprint, one of KEYID_*
* @param fp fingerprint, points to internal data
* @return TRUE if fingerprint type supported
*/
bool (*get_fingerprint)(private_key_t *this, key_encoding_type_t type,
bool (*get_fingerprint)(private_key_t *this, cred_encoding_type_t type,
chunk_t *fp);
/**
@@ -108,11 +108,11 @@ struct private_key_t {
/**
* Get the key in an encoded form as a chunk.
*
* @param type type of the encoding, one of KEY_PRIV_*
* @param type type of the encoding, one of PRIVKEY_*
* @param encoding encoding of the key, allocated
* @return TRUE if encoding supported
*/
bool (*get_encoding)(private_key_t *this, key_encoding_type_t type,
bool (*get_encoding)(private_key_t *this, cred_encoding_type_t type,
chunk_t *encoding);
/**
@@ -47,7 +47,7 @@ ENUM(signature_scheme_names, SIGN_UNKNOWN, SIGN_ECDSA_521,
*/
bool public_key_equals(public_key_t *this, public_key_t *other)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t a, b;
if (this == other)
@@ -55,7 +55,7 @@ bool public_key_equals(public_key_t *this, public_key_t *other)
return TRUE;
}
for (type = 0; type < KEY_ENCODING_MAX; type++)
for (type = 0; type < CRED_ENCODING_MAX; type++)
{
if (this->get_fingerprint(this, type, &a) &&
other->get_fingerprint(other, type, &b))
@@ -71,10 +71,10 @@ bool public_key_equals(public_key_t *this, public_key_t *other)
*/
bool public_key_has_fingerprint(public_key_t *public, chunk_t fingerprint)
{
key_encoding_type_t type;
cred_encoding_type_t type;
chunk_t current;
for (type = 0; type < KEY_ID_MAX; type++)
for (type = 0; type < KEYID_MAX; type++)
{
if (public->get_fingerprint(public, type, &current) &&
chunk_equals(current, fingerprint))
@@ -23,7 +23,6 @@
typedef struct public_key_t public_key_t;
typedef enum key_type_t key_type_t;
typedef enum key_id_type_t key_id_type_t;
typedef enum signature_scheme_t signature_scheme_t;
#include <library.h>
@@ -147,11 +146,11 @@ struct public_key_t {
/**
* Get the fingerprint of the key.
*
* @param type type of fingerprint, one of KEY_ID_*
* @param type type of fingerprint, one of KEYID_*
* @param fp fingerprint, points to internal data
* @return TRUE if fingerprint type supported
*/
bool (*get_fingerprint)(public_key_t *this, key_encoding_type_t type,
bool (*get_fingerprint)(public_key_t *this, cred_encoding_type_t type,
chunk_t *fp);
/**
@@ -165,11 +164,11 @@ struct public_key_t {
/**
* Get the key in an encoded form as a chunk.
*
* @param type type of the encoding, one of KEY_PRIV_*
* @param type type of the encoding, one of PRIVKEY_*
* @param encoding encoding of the key, allocated
* @return TRUE if encoding supported
*/
bool (*get_encoding)(public_key_t *this, key_encoding_type_t type,
bool (*get_encoding)(public_key_t *this, cred_encoding_type_t type,
chunk_t *encoding);
/**