Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2008 Pekka Riikonen
+ Copyright (C) 1997 - 2009 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
/* Initialize packet procesors list */
ps->process = silc_dlist_init();
if (!ps->process) {
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
ps->sc = silc_calloc(1, sizeof(*ps->sc));
if (!ps->sc) {
silc_mutex_unlock(engine->lock);
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
silc_free(ps->sc);
ps->sc = NULL;
silc_mutex_unlock(engine->lock);
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
silc_free(ps->sc);
ps->sc = NULL;
silc_mutex_unlock(engine->lock);
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
silc_free(ps->sc);
ps->sc = NULL;
silc_mutex_unlock(engine->lock);
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
if (!silc_stream_set_notifier(ps->stream, schedule,
silc_packet_stream_io, ps)) {
SILC_LOG_DEBUG(("Cannot set stream notifier for packet stream"));
+ ps->stream = NULL;
silc_packet_stream_destroy(ps);
return NULL;
}
silc_mutex_lock(stream->lock);
- if (stream->process) {
- silc_dlist_start(stream->process);
- while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END)
- if (p->callbacks == callbacks &&
- p->callback_context == callback_context) {
- silc_dlist_del(stream->process, p);
- silc_free(p->types);
- silc_free(p);
- break;
- }
-
- if (!silc_dlist_count(stream->process)) {
- silc_dlist_uninit(stream->process);
- stream->process = NULL;
+ silc_dlist_start(stream->process);
+ while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END)
+ if (p->callbacks == callbacks &&
+ p->callback_context == callback_context) {
+ silc_dlist_del(stream->process, p);
+ silc_free(p->types);
+ silc_free(p);
+ break;
}
+
+ if (!silc_dlist_count(stream->process)) {
+ silc_dlist_uninit(stream->process);
+ stream->process = NULL;
}
silc_mutex_unlock(stream->lock);
SILC_LOG_HEXDUMP(("Counter Block"), iv, 16);
}
+/* Return special packet's encrypted length */
+
+static inline int silc_packet_special_len(unsigned char *data)
+{
+ return (((SilcUInt8)data[4] + (SilcUInt8)data[6] +
+ (SilcUInt8)data[7] + SILC_PACKET_HEADER_LEN));
+}
+
/* Decrypts SILC packet. Handles both normal and special packet decryption.
Return 0 when packet is normal and 1 when it it special, -1 on error. */
/* Padding length + src id len + dst id len + header length - 16
bytes already decrypted, gives the rest of the encrypted packet */
silc_buffer_push(buffer, block_len);
- len = (((SilcUInt8)buffer->data[4] + (SilcUInt8)buffer->data[6] +
- (SilcUInt8)buffer->data[7] + SILC_PACKET_HEADER_LEN) -
- block_len);
+ len = silc_packet_special_len(buffer->data) - block_len;
silc_buffer_pull(buffer, block_len);
if (silc_unlikely(len > silc_buffer_len(buffer))) {
SilcCipher cipher;
SilcHmac hmac;
SilcPacket packet;
- SilcUInt8 sid;
+ SilcUInt8 sid, flags, type;
SilcUInt16 packetlen;
SilcUInt32 paddedlen, mac_len, block_len, ivlen, psnlen;
unsigned char tmp[SILC_PACKET_MIN_HEADER_LEN], *header;
/* Get packet length and full packet length with padding */
SILC_PACKET_LENGTH(header, packetlen, paddedlen);
- /* Sanity checks */
- if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
- if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
- SILC_LOG_ERROR(("Received too short packet"));
+ /* Parse packet header */
+ flags = (SilcPacketFlags)header[2];
+ type = (SilcPacketType)header[3];
+
+ if (stream->sc->engine->local_is_router) {
+ if (type == SILC_PACKET_PRIVATE_MESSAGE &&
+ (flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
+ normal = FALSE;
+ else if (type != SILC_PACKET_CHANNEL_MESSAGE ||
+ (type == SILC_PACKET_CHANNEL_MESSAGE &&
+ stream->is_router == TRUE))
+ normal = TRUE;
+ } else {
+ if (type == SILC_PACKET_PRIVATE_MESSAGE &&
+ (flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
+ normal = FALSE;
+ else if (type != SILC_PACKET_CHANNEL_MESSAGE)
+ normal = TRUE;
+ }
+
+ /* Padding sanity checks */
+ if (cipher && silc_cipher_get_mode(cipher) != SILC_CIPHER_MODE_CTR &&
+ ((normal && paddedlen % block_len != 0) ||
+ (!normal && silc_packet_special_len(header) % block_len != 0))) {
+ SILC_LOG_DEBUG(("Packet length %d not multiple by cipher block length",
+ paddedlen));
silc_mutex_unlock(stream->lock);
SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
silc_mutex_lock(stream->lock);
goto out;
}
+ /* Sanity checks */
+ if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
+ if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
+ SILC_LOG_ERROR(("Received too short packet"));
+ silc_mutex_unlock(stream->lock);
+ SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
+ silc_mutex_lock(stream->lock);
+ memset(tmp, 0, sizeof(tmp));
+ goto out;
+ }
+
/* Get packet */
packet = silc_packet_alloc(stream->sc->engine);
if (silc_unlikely(!packet)) {
goto out;
}
packet->stream = stream;
+ packet->flags = flags;
+ packet->type = type;
/* Allocate more space to packet buffer, if needed */
if (silc_unlikely(silc_buffer_truelen(&packet->buffer) < paddedlen)) {
}
}
- /* Parse packet header */
- packet->flags = (SilcPacketFlags)header[2];
- packet->type = (SilcPacketType)header[3];
-
- if (stream->sc->engine->local_is_router) {
- if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
- (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
- normal = FALSE;
- else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE ||
- (packet->type == SILC_PACKET_CHANNEL_MESSAGE &&
- stream->is_router == TRUE))
- normal = TRUE;
- } else {
- if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
- (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
- normal = FALSE;
- else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE)
- normal = TRUE;
- }
-
SILC_LOG_HEXDUMP(("Incoming packet (%d) len %d",
stream->receive_psn, paddedlen + ivlen + mac_len),
inbuf->data, paddedlen + ivlen + mac_len);