From 9775f78ab2cf6cc348bad8728caf76709729a42a Mon Sep 17 00:00:00 2001 From: Axel Eppe Date: Sun, 23 Aug 2015 17:38:21 +0100 Subject: pki: Add Add new pki_import_cert_buffer function Signed-off-by: Axel Eppe Reviewed-by: Andreas Schneider --- src/pki.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'src/pki.c') diff --git a/src/pki.c b/src/pki.c index 8fa5a12f..c02e5dbe 100644 --- a/src/pki.c +++ b/src/pki.c @@ -827,6 +827,57 @@ fail: return SSH_ERROR; } +static int pki_import_cert_buffer(ssh_buffer buffer, + enum ssh_keytypes_e type, + ssh_key *pkey) { + ssh_buffer cert; + ssh_string type_s; + ssh_key key; + int rc; + + key = ssh_key_new(); + if (key == NULL) { + return SSH_ERROR; + } + cert = ssh_buffer_new(); + if (cert == NULL) { + ssh_key_free(key); + return SSH_ERROR; + } + + key->type = type; + key->type_c = ssh_key_type_to_char(type); + key->flags = SSH_KEY_FLAG_PUBLIC; + + /* + * The cert blob starts with the key type as an ssh_string, but this + * string has been read out of the buffer to identify the key type. + * Simply add it again as first element before copying the rest. + */ + type_s = ssh_string_from_char(key->type_c); + if (type_s == NULL) { + goto fail; + } + rc = buffer_add_ssh_string(cert, type_s); + if (rc != 0) { + goto fail; + } + + rc = buffer_add_buffer(cert, buffer); + if (rc != 0) { + goto fail; + } + key->cert = (void*) cert; + + *pkey = key; + return SSH_OK; + +fail: + ssh_key_free(key); + ssh_buffer_free(cert); + return SSH_ERROR; +} + /** * @brief Import a base64 formated public key from a memory c-string. * -- cgit v1.2.3