X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccore%2Fsilcprivate.c;h=26a064297028477ee2dba73f94da60fce0220509;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=c828a8f77facb87c5608fd4643b14710d2a0cf3f;hpb=a242db4691e6b9a6750af4f14b0f2697a7774435;p=silc.git diff --git a/lib/silccore/silcprivate.c b/lib/silccore/silcprivate.c index c828a8f7..26a06429 100644 --- a/lib/silccore/silcprivate.c +++ b/lib/silccore/silcprivate.c @@ -29,11 +29,13 @@ ******************************************************************************/ +#define SILC_PRIVATE_MESSAGE_PAD(__payloadlen) (16 - (__payloadlen) % 16) + /* Private Message Payload structure. Contents of this structure is parsed from SILC packets. */ struct SilcPrivateMessagePayloadStruct { - uint16 flags; - uint16 message_len; + SilcUInt16 flags; + SilcUInt16 message_len; unsigned char *message; }; @@ -41,33 +43,38 @@ struct SilcPrivateMessagePayloadStruct { structure. This also decrypts the message if the `cipher' is provided. */ SilcPrivateMessagePayload -silc_private_message_payload_parse(SilcBuffer buffer, SilcCipher cipher) +silc_private_message_payload_parse(unsigned char *payload, + SilcUInt32 payload_len, + SilcCipher cipher) { + SilcBufferStruct buffer; SilcPrivateMessagePayload new; int ret; SILC_LOG_DEBUG(("Parsing private message payload")); + silc_buffer_set(&buffer, payload, payload_len); + /* Decrypt the payload */ if (cipher) - silc_cipher_decrypt(cipher, buffer->data, buffer->data, - buffer->len, cipher->iv); + silc_cipher_decrypt(cipher, buffer.data, buffer.data, + buffer.len, cipher->iv); new = silc_calloc(1, sizeof(*new)); /* Parse the Private Message Payload. Ignore the padding. */ - ret = silc_buffer_unformat(buffer, + ret = silc_buffer_unformat(&buffer, SILC_STR_UI_SHORT(&new->flags), SILC_STR_UI16_NSTRING_ALLOC(&new->message, &new->message_len), SILC_STR_END); if (ret == -1) { - SILC_LOG_ERROR(("Incorrect private message payload")); + SILC_LOG_DEBUG(("Incorrect private message payload")); goto err; } - if ((new->message_len < 1 || new->message_len > buffer->len)) { - SILC_LOG_ERROR(("Incorrect private message payload in packet, " + if ((new->message_len < 1 || new->message_len > buffer.len)) { + SILC_LOG_DEBUG(("Incorrect private message payload in packet, " "packet dropped")); goto err; } @@ -83,15 +90,15 @@ silc_private_message_payload_parse(SilcBuffer buffer, SilcCipher cipher) the cipher is provided the packet is also encrypted here. It is provided if the private message private keys are used. */ -SilcBuffer silc_private_message_payload_encode(uint16 flags, - uint16 data_len, - unsigned char *data, +SilcBuffer silc_private_message_payload_encode(SilcUInt16 flags, + SilcUInt16 data_len, + const unsigned char *data, SilcCipher cipher) { int i; SilcBuffer buffer; - uint32 len, pad_len = 0; - unsigned char pad[SILC_PACKET_MAX_PADLEN]; + SilcUInt32 len, pad_len = 0; + unsigned char pad[16]; SILC_LOG_DEBUG(("Encoding private message payload")); @@ -99,7 +106,7 @@ SilcBuffer silc_private_message_payload_encode(uint16 flags, if (cipher) { /* Calculate length of padding. */ - pad_len = SILC_PACKET_PADLEN((len + 2)); + pad_len = SILC_PRIVATE_MESSAGE_PAD(len); len += pad_len; /* Generate padding */ @@ -141,7 +148,7 @@ void silc_private_message_payload_free(SilcPrivateMessagePayload payload) /* Return flags */ -uint16 +SilcUInt16 silc_private_message_get_flags(SilcPrivateMessagePayload payload) { return payload->flags; @@ -151,7 +158,7 @@ silc_private_message_get_flags(SilcPrivateMessagePayload payload) unsigned char * silc_private_message_get_message(SilcPrivateMessagePayload payload, - uint32 *message_len) + SilcUInt32 *message_len) { if (message_len) *message_len = payload->message_len;