5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 /* Encodes Public Key Payload for transmitting public keys and certificates. */
24 SilcBuffer silc_public_key_payload_encode(SilcStack stack,
25 SilcPublicKey public_key)
35 type = silc_pkcs_get_type(public_key);
36 pk = silc_pkcs_public_key_encode(stack, public_key, &pk_len);
40 buffer = silc_buffer_salloc_size(stack, 4 + pk_len);
42 silc_sfree(stack, pk);
46 if (silc_buffer_sformat(stack, buffer,
47 SILC_STR_UI_SHORT(pk_len),
48 SILC_STR_UI_SHORT(type),
49 SILC_STR_DATA(pk, pk_len),
51 silc_buffer_sfree(stack, buffer);
52 silc_sfree(stack, pk);
56 silc_sfree(stack, pk);
60 /* Decodes public key payload and returns allocated public key */
62 SilcBool silc_public_key_payload_decode(unsigned char *data,
64 SilcPublicKey *public_key)
67 SilcUInt16 pk_len, pk_type;
74 silc_buffer_set(&buf, data, data_len);
75 ret = silc_buffer_unformat(&buf,
77 SILC_STR_UI_SHORT(&pk_len),
78 SILC_STR_UI_SHORT(&pk_type),
80 if (ret < 0 || pk_len > data_len - 4)
83 if (pk_type < SILC_PKCS_SILC || pk_type > SILC_PKCS_SPKI)
86 ret = silc_buffer_unformat(&buf,
87 SILC_STR_DATA(&pk, pk_len),
92 return silc_pkcs_public_key_alloc((SilcPKCSType)pk_type,
93 pk, pk_len, public_key);