2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "tls_protection.h"
18 #include <utils/debug.h>
20 typedef struct private_tls_protection_t private_tls_protection_t
;
23 * Private data of an tls_protection_t object.
25 struct private_tls_protection_t
{
28 * Public tls_protection_t interface.
30 tls_protection_t
public;
33 * negotiated TLS version
35 tls_version_t version
;
38 * Upper layer, TLS record compression
40 tls_compression_t
*compression
;
48 * Sequence number of incoming records
53 * Sequence number for outgoing records
58 * AEAD transform for inbound traffic
63 * AEAD transform for outbound traffic
68 METHOD(tls_protection_t
, process
, status_t
,
69 private_tls_protection_t
*this, tls_content_type_t type
, chunk_t data
)
71 if (this->alert
->fatal(this->alert
))
72 { /* don't accept more input, fatal error occurred */
78 if (!this->aead_in
->decrypt(this->aead_in
, this->version
,
79 type
, this->seq_in
, &data
))
81 DBG1(DBG_TLS
, "TLS record decryption failed");
82 this->alert
->add(this->alert
, TLS_FATAL
, TLS_BAD_RECORD_MAC
);
87 if (type
== TLS_CHANGE_CIPHER_SPEC
)
95 return this->compression
->process(this->compression
, type
, data
);
98 METHOD(tls_protection_t
, build
, status_t
,
99 private_tls_protection_t
*this, tls_content_type_t
*type
, chunk_t
*data
)
103 status
= this->compression
->build(this->compression
, type
, data
);
104 if (*type
== TLS_CHANGE_CIPHER_SPEC
)
110 if (status
== NEED_MORE
)
114 if (!this->aead_out
->encrypt(this->aead_out
, this->version
,
115 *type
, this->seq_out
, data
))
117 DBG1(DBG_TLS
, "TLS record encryption failed");
127 METHOD(tls_protection_t
, set_cipher
, void,
128 private_tls_protection_t
*this, bool inbound
, tls_aead_t
*aead
)
132 this->aead_in
= aead
;
136 this->aead_out
= aead
;
140 METHOD(tls_protection_t
, set_version
, void,
141 private_tls_protection_t
*this, tls_version_t version
)
143 this->version
= version
;
146 METHOD(tls_protection_t
, destroy
, void,
147 private_tls_protection_t
*this)
155 tls_protection_t
*tls_protection_create(tls_compression_t
*compression
,
158 private_tls_protection_t
*this;
164 .set_cipher
= _set_cipher
,
165 .set_version
= _set_version
,
169 .compression
= compression
,
172 return &this->public;