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 */
76 if (this->version
< TLS_1_3
||
77 type
== TLS_APPLICATION_DATA
)
81 if (!this->aead_in
->decrypt(this->aead_in
, this->version
,
82 &type
, this->seq_in
, &data
))
84 DBG1(DBG_TLS
, "TLS record decryption failed");
85 this->alert
->add(this->alert
, TLS_FATAL
, TLS_BAD_RECORD_MAC
);
91 return this->compression
->process(this->compression
, type
, data
);
94 METHOD(tls_protection_t
, build
, status_t
,
95 private_tls_protection_t
*this, tls_content_type_t
*type
, chunk_t
*data
)
99 status
= this->compression
->build(this->compression
, type
, data
);
100 if (status
== NEED_MORE
)
102 if (*type
== TLS_CHANGE_CIPHER_SPEC
&& this->version
< TLS_1_3
)
108 if (!this->aead_out
->encrypt(this->aead_out
, this->version
,
109 type
, this->seq_out
, data
))
111 DBG1(DBG_TLS
, "TLS record encryption failed");
121 METHOD(tls_protection_t
, set_cipher
, void,
122 private_tls_protection_t
*this, bool inbound
, tls_aead_t
*aead
)
126 this->aead_in
= aead
;
131 this->aead_out
= aead
;
136 METHOD(tls_protection_t
, set_version
, void,
137 private_tls_protection_t
*this, tls_version_t version
)
139 this->version
= version
;
142 METHOD(tls_protection_t
, destroy
, void,
143 private_tls_protection_t
*this)
151 tls_protection_t
*tls_protection_create(tls_compression_t
*compression
,
154 private_tls_protection_t
*this;
160 .set_cipher
= _set_cipher
,
161 .set_version
= _set_version
,
165 .compression
= compression
,
168 return &this->public;