diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libstrongswan/credentials/keys/key_encoding.c | 26 | ||||
-rw-r--r-- | src/libstrongswan/credentials/keys/key_encoding.h | 15 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/libstrongswan/credentials/keys/key_encoding.c b/src/libstrongswan/credentials/keys/key_encoding.c index 1ffb4241b..1e6201603 100644 --- a/src/libstrongswan/credentials/keys/key_encoding.c +++ b/src/libstrongswan/credentials/keys/key_encoding.c @@ -186,6 +186,31 @@ static bool encode(private_key_encoding_t *this, key_encoding_type_t type, } /** + * Implementation of key_encoding_t.cache + */ +static void cache(private_key_encoding_t *this, key_encoding_type_t type, + void *cache, chunk_t encoding) +{ + chunk_t *chunk; + + if (type >= KEY_ENCODING_MAX || type < 0) + { + return free(encoding.ptr); + } + chunk = malloc_thing(chunk_t); + *chunk = encoding; + this->lock->write_lock(this->lock); + chunk = this->cache[type]->put(this->cache[type], cache, chunk); + this->lock->unlock(this->lock); + /* free an encoding already associated to the cache */ + if (chunk) + { + free(chunk->ptr); + free(chunk); + } +} + +/** * Implementation of key_encoding_t.clear_cache */ static void clear_cache(private_key_encoding_t *this, void *cache) @@ -262,6 +287,7 @@ key_encoding_t *key_encoding_create() 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; diff --git a/src/libstrongswan/credentials/keys/key_encoding.h b/src/libstrongswan/credentials/keys/key_encoding.h index a338f07ad..0f70186c2 100644 --- a/src/libstrongswan/credentials/keys/key_encoding.h +++ b/src/libstrongswan/credentials/keys/key_encoding.h @@ -154,13 +154,26 @@ struct key_encoding_t { * * @param type format of the key encoding * @param cache key to use for caching, as given to encode() - * @encoding encoding result, internal data + * @param encoding encoding result, internal data * @return TRUE if cache entry found */ bool (*get_cache)(key_encoding_t *this, key_encoding_type_t type, void *cache, chunk_t *encoding); /** + * Cache a key encoding created externally. + * + * After calling cache(), the passed encoding is owned by the key encoding + * facility. + * + * @param type format of the key 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, + chunk_t encoding); + + /** * Register a key encoder function. * * @param encoder key encoder function to add |