From: Pekka Riikonen Date: Sun, 1 Jan 2006 10:43:02 +0000 (+0000) Subject: Added. X-Git-Tag: silc.client.1.1.beta1~321 X-Git-Url: http://git.silcnet.org/gitweb/?a=commitdiff_plain;h=1341374e663e9099e25c0d2d78c213c0340ed1b6;p=silc.git Added. --- diff --git a/lib/silccore/silcpubkey.c b/lib/silccore/silcpubkey.c new file mode 100644 index 00000000..9583f397 --- /dev/null +++ b/lib/silccore/silcpubkey.c @@ -0,0 +1,93 @@ +/* + + silcpubkey.c + + Author: Pekka Riikonen + + Copyright (C) 2005 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +#include "silc.h" + +/* Encodes Public Key Payload for transmitting public keys and certificates. */ + +SilcBuffer silc_public_key_payload_encode(SilcPublicKey public_key) +{ + SilcBuffer buffer; + unsigned char *pk; + SilcUInt32 pk_len; + SilcPKCSType type; + + if (!public_key) + return NULL; + + type = silc_pkcs_get_type(public_key); + pk = silc_pkcs_public_key_encode(public_key, &pk_len); + if (!pk) + return NULL; + + buffer = silc_buffer_alloc_size(4 + pk_len); + if (!buffer) { + silc_free(pk); + return NULL; + } + + if (silc_buffer_format(buffer, + SILC_STR_UI_SHORT(pk_len), + SILC_STR_UI_SHORT(type), + SILC_STR_UI_XNSTRING(pk, pk_len), + SILC_STR_END) < 0) { + silc_buffer_free(buffer); + silc_free(pk); + return NULL; + } + + silc_free(pk); + return buffer; +} + +/* Decodes public key payload and returns allocated public key */ + +SilcBool silc_public_key_payload_decode(unsigned char *data, + SilcUInt32 data_len, + SilcPublicKey *public_key) +{ + SilcBufferStruct buf; + SilcUInt16 pk_len, pk_type; + unsigned char *pk; + int ret; + + if (!public_key) + return FALSE; + + silc_buffer_set(&buf, data, data_len); + ret = silc_buffer_unformat(&buf, + SILC_STR_UI_SHORT(&pk_len), + SILC_STR_UI_SHORT(&pk_type), + SILC_STR_END); + if (ret < 0 || pk_len > data_len - 4) + return FALSE; + + if (pk_type < SILC_PKCS_SILC || pk_type > SILC_PKCS_SPKI) + return FALSE; + + silc_buffer_pull(&buf, 4); + ret = silc_buffer_unformat(&buf, + SILC_STR_UI_XNSTRING(&pk, pk_len), + SILC_STR_END); + if (ret < 0) + return FALSE; + + return silc_pkcs_public_key_alloc((SilcPKCSType)pk_type, + pk, pk_len, public_key); +} diff --git a/lib/silccore/silcpubkey.h b/lib/silccore/silcpubkey.h new file mode 100644 index 00000000..4bf004b9 --- /dev/null +++ b/lib/silccore/silcpubkey.h @@ -0,0 +1,67 @@ +/* + + silcpubkey.h + + Author: Pekka Riikonen + + Copyright (C) 2005 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +/****h* silccore/SILC Public Key Payload + * + * DESCRIPTION + * + * Implementation of the Public Key Payload. Public Key Payload is used to + * deliver different types of public keys and certificates in the SILC + * protocol. + * + ***/ + +#ifndef SILCPUBKEY_H +#define SILCPUBKEY_H + +/****f* silccore/SilcPubKeyAPI/silc_public_key_payload_encode + * + * SYNOPSIS + * + * SilcBool silc_public_key_payload_encode(SilcPublicKey public_key); + * + * DESCRIPTION + * + * Encodes the Public Key Payload from the public key indicated by + * `public_key'. Returns the allocated and encoded payload buffer, + * or NULL on error. + * + ***/ +SilcBuffer silc_public_key_payload_encode(SilcPublicKey public_key); + +/****f* silccore/SilcPubKeyAPI/silc_public_key_payload_decode + * + * SYNOPSIS + * + * SilcBool silc_public_key_payload_decode(unsigned char *data, + * SilcUInt32 data_len, + * SilcPublicKey *public_key); + * + * DESCRIPTION + * + * Decodes Public Key Payload from `data' of `data_len' bytes in length + * data buffer into `public_key' pointer. Returns FALSE if the payload + * cannot be decoded. + * + ***/ +SilcBool silc_public_key_payload_decode(unsigned char *data, + SilcUInt32 data_len, + SilcPublicKey *public_key); + +#endif /* SILCPUBKEY_H */