/* Buffer for handshake packets. Keeps the packets in order * for finished messages to use them. Used in HMAC calculation * and finished messages. */ int _gnutls_handshake_buffer_put (gnutls_session_t session, opaque * data, size_t length) { if (length == 0) return 0; if ((session->internals.max_handshake_data_buffer_size > 0) && ((length + session->internals.handshake_hash_buffer.length) > session->internals.max_handshake_data_buffer_size)) { gnutls_assert (); return GNUTLS_E_MEMORY_ERROR; } _gnutls_buffers_log ("BUF[HSK]: Inserted %d bytes of Data\n", length); if (_gnutls_buffer_append (&session->internals.handshake_hash_buffer, data, length) < 0) { gnutls_assert (); return GNUTLS_E_MEMORY_ERROR; } return 0; }
/* Buffers received packets of type APPLICATION DATA and * HANDSHAKE DATA. */ int _gnutls_record_buffer_put (content_type_t type, gnutls_session_t session, opaque * data, size_t length) { gnutls_buffer *buf; if (length == 0) return 0; switch (type) { case GNUTLS_APPLICATION_DATA: buf = &session->internals.application_data_buffer; _gnutls_buffers_log ("BUF[REC]: Inserted %d bytes of Data(%d)\n", length, type); break; case GNUTLS_HANDSHAKE: buf = &session->internals.handshake_data_buffer; _gnutls_buffers_log ("BUF[HSK]: Inserted %d bytes of Data(%d)\n", length, type); break; case GNUTLS_INNER_APPLICATION: buf = &session->internals.ia_data_buffer; _gnutls_buffers_log ("BUF[IA]: Inserted %d bytes of Data(%d)\n", length, type); break; default: gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } if (_gnutls_buffer_append (buf, data, length) < 0) { gnutls_assert (); return GNUTLS_E_MEMORY_ERROR; } return 0; }
int _gnutls_handshake_buffer_clear (gnutls_session_t session) { _gnutls_buffers_log ("BUF[HSK]: Cleared Data from buffer\n"); _gnutls_buffer_clear (&session->internals.handshake_hash_buffer); return 0; }
/* Does not free the buffer */ int _gnutls_handshake_buffer_empty (gnutls_session_t session) { _gnutls_buffers_log ("BUF[HSK]: Emptied buffer\n"); session->internals.handshake_hash_buffer.length = 0; return 0; }
int _gnutls_record_buffer_get (content_type_t type, gnutls_session_t session, opaque * data, size_t length) { if (length == 0 || data == NULL) { gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } switch (type) { case GNUTLS_APPLICATION_DATA: _gnutls_buffer_get_data( &session->internals.application_data_buffer, data, &length); _gnutls_buffers_log ("BUFFER[REC][AD]: Read %d bytes of Data(%d)\n", length, type); break; case GNUTLS_HANDSHAKE: _gnutls_buffer_get_data( &session->internals.handshake_data_buffer, data, &length); _gnutls_buffers_log ("BUF[REC][HD]: Read %d bytes of Data(%d)\n", length, type); break; case GNUTLS_INNER_APPLICATION: _gnutls_buffer_get_data( &session->internals.ia_data_buffer, data, &length); _gnutls_buffers_log ("BUF[REC][IA]: Read %d bytes of Data(%d)\n", length, type); break; default: gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } return length; }
/* this function does not touch the buffer * and returns data from it (peek mode!) */ int _gnutls_handshake_buffer_get_ptr (gnutls_session_t session, opaque ** data_ptr, size_t * length) { if (length != NULL) *length = session->internals.handshake_hash_buffer.length; _gnutls_buffers_log ("BUF[HSK]: Peeked %d bytes of Data\n", *length); if (data_ptr != NULL) *data_ptr = session->internals.handshake_hash_buffer.data; return 0; }
/* this function does not touch the buffer * and returns data from it (peek mode!) */ int _gnutls_handshake_buffer_peek (gnutls_session_t session, opaque * data, size_t length) { if (length > session->internals.handshake_hash_buffer.length) { length = session->internals.handshake_hash_buffer.length; } _gnutls_buffers_log ("BUF[HSK]: Peeked %d bytes of Data\n", length); memcpy (data, session->internals.handshake_hash_buffer.data, length); return length; }
/* Buffers received packets of type APPLICATION DATA and * HANDSHAKE DATA. */ int _gnutls_record_buffer_put (gnutls_session_t session, content_type_t type, uint64* seq, mbuffer_st* bufel) { bufel->type = type; memcpy(&bufel->record_sequence, seq, sizeof(*seq)); _mbuffer_enqueue(&session->internals.record_buffer, bufel); _gnutls_buffers_log ("BUF[REC]: Inserted %d bytes of Data(%d)\n", (int) bufel->msg.size, (int) type); return 0; }
int _gnutls_record_buffer_get (content_type_t type, gnutls_session_t session, opaque * data, size_t length) { if (length == 0 || data == NULL) { gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } switch (type) { case GNUTLS_APPLICATION_DATA: if (length > session->internals.application_data_buffer.length) { length = session->internals.application_data_buffer.length; } _gnutls_buffers_log ("BUFFER[REC][AD]: Read %d bytes of Data(%d)\n", length, type); session->internals.application_data_buffer.length -= length; memcpy (data, session->internals.application_data_buffer.data, length); /* overwrite buffer */ memmove (session->internals.application_data_buffer.data, &session->internals.application_data_buffer.data[length], session->internals.application_data_buffer.length); /* we do no longer realloc the application_data_buffer.data, * since it serves no practical reason. It also decreases * performance. */ break; case GNUTLS_HANDSHAKE: if (length > session->internals.handshake_data_buffer.length) { length = session->internals.handshake_data_buffer.length; } _gnutls_buffers_log ("BUF[REC][HD]: Read %d bytes of Data(%d)\n", length, type); session->internals.handshake_data_buffer.length -= length; memcpy (data, session->internals.handshake_data_buffer.data, length); /* overwrite buffer */ memmove (session->internals.handshake_data_buffer.data, &session->internals.handshake_data_buffer.data[length], session->internals.handshake_data_buffer.length); break; case GNUTLS_INNER_APPLICATION: if (length > session->internals.ia_data_buffer.length) length = session->internals.ia_data_buffer.length; _gnutls_buffers_log ("BUF[REC][IA]: Read %d bytes of Data(%d)\n", length, type); session->internals.ia_data_buffer.length -= length; memcpy (data, session->internals.ia_data_buffer.data, length); /* overwrite buffer */ memmove (session->internals.ia_data_buffer.data, &session->internals.ia_data_buffer.data[length], session->internals.ia_data_buffer.length); break; default: gnutls_assert (); return GNUTLS_E_INVALID_REQUEST; } return length; }