optional certificate-based peer authentication on TLS server side
[strongswan.git] / src / libtls / tls.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "tls.h"
17
18 #include "tls_protection.h"
19 #include "tls_compression.h"
20 #include "tls_fragmentation.h"
21 #include "tls_crypto.h"
22 #include "tls_server.h"
23 #include "tls_peer.h"
24
25 ENUM_BEGIN(tls_version_names, SSL_2_0, SSL_2_0,
26 "SSLv2");
27 ENUM_NEXT(tls_version_names, SSL_3_0, TLS_1_2, SSL_2_0,
28 "SSLv3",
29 "TLS 1.0",
30 "TLS 1.1",
31 "TLS 1.2");
32 ENUM_END(tls_version_names, TLS_1_2);
33
34 ENUM(tls_content_type_names, TLS_CHANGE_CIPHER_SPEC, TLS_APPLICATION_DATA,
35 "ChangeCipherSpec",
36 "Alert",
37 "Handshake",
38 "ApplicationData",
39 );
40
41 ENUM_BEGIN(tls_handshake_type_names, TLS_HELLO_REQUEST, TLS_SERVER_HELLO,
42 "HelloRequest",
43 "ClientHello",
44 "ServerHello");
45 ENUM_NEXT(tls_handshake_type_names, TLS_CERTIFICATE, TLS_CLIENT_KEY_EXCHANGE, TLS_SERVER_HELLO,
46 "Certificate",
47 "ServerKeyExchange",
48 "CertificateRequest",
49 "ServerHelloDone",
50 "CertificateVerify",
51 "ClientKeyExchange");
52 ENUM_NEXT(tls_handshake_type_names, TLS_FINISHED, TLS_FINISHED, TLS_CLIENT_KEY_EXCHANGE,
53 "Finished");
54 ENUM_END(tls_handshake_type_names, TLS_FINISHED);
55
56
57 typedef struct private_tls_t private_tls_t;
58
59 /**
60 * Private data of an tls_protection_t object.
61 */
62 struct private_tls_t {
63
64 /**
65 * Public tls_t interface.
66 */
67 tls_t public;
68
69 /**
70 * Role this TLS stack acts as.
71 */
72 bool is_server;
73
74 /**
75 * Server identity
76 */
77 identification_t *server;
78
79 /**
80 * Peer identity
81 */
82 identification_t *peer;
83
84 /**
85 * Negotiated TLS version
86 */
87 tls_version_t version;
88
89 /**
90 * TLS record protection layer
91 */
92 tls_protection_t *protection;
93
94 /**
95 * TLS record compression layer
96 */
97 tls_compression_t *compression;
98
99 /**
100 * TLS record fragmentation layer
101 */
102 tls_fragmentation_t *fragmentation;
103
104 /**
105 * TLS crypto helper context
106 */
107 tls_crypto_t *crypto;
108
109 /**
110 * TLS handshake protocol handler
111 */
112 tls_handshake_t *handshake;
113
114 /**
115 * TLS application data handler
116 */
117 tls_application_t *application;
118 };
119
120 METHOD(tls_t, process, status_t,
121 private_tls_t *this, tls_content_type_t type, chunk_t data)
122 {
123 return this->protection->process(this->protection, type, data);
124 }
125
126 METHOD(tls_t, build, status_t,
127 private_tls_t *this, tls_content_type_t *type, chunk_t *data)
128 {
129 return this->protection->build(this->protection, type, data);
130 }
131
132 METHOD(tls_t, is_server, bool,
133 private_tls_t *this)
134 {
135 return this->is_server;
136 }
137
138 METHOD(tls_t, get_version, tls_version_t,
139 private_tls_t *this)
140 {
141 return this->version;
142 }
143
144 METHOD(tls_t, set_version, void,
145 private_tls_t *this, tls_version_t version)
146 {
147 this->version = version;
148 }
149
150 METHOD(tls_t, is_complete, bool,
151 private_tls_t *this)
152 {
153 return this->crypto->get_eap_msk(this->crypto).len != 0;
154 }
155
156 METHOD(tls_t, get_eap_msk, chunk_t,
157 private_tls_t *this)
158 {
159 return this->crypto->get_eap_msk(this->crypto);
160 }
161
162 METHOD(tls_t, destroy, void,
163 private_tls_t *this)
164 {
165 this->protection->destroy(this->protection);
166 this->compression->destroy(this->compression);
167 this->fragmentation->destroy(this->fragmentation);
168 this->crypto->destroy(this->crypto);
169 this->handshake->destroy(this->handshake);
170 this->peer->destroy(this->peer);
171 this->server->destroy(this->server);
172 DESTROY_IF(this->application);
173
174 free(this);
175 }
176
177 /**
178 * See header
179 */
180 tls_t *tls_create(bool is_server, identification_t *server,
181 identification_t *peer, bool request_peer_auth,
182 char *msk_label, tls_application_t *application)
183 {
184 private_tls_t *this;
185
186 INIT(this,
187 .public = {
188 .process = _process,
189 .build = _build,
190 .is_server = _is_server,
191 .get_version = _get_version,
192 .set_version = _set_version,
193 .is_complete = _is_complete,
194 .get_eap_msk = _get_eap_msk,
195 .destroy = _destroy,
196 },
197 .is_server = is_server,
198 .version = TLS_1_2,
199 .server = server->clone(server),
200 .peer = peer->clone(peer),
201 .application = application,
202 );
203
204 this->crypto = tls_crypto_create(&this->public, msk_label);
205 if (is_server)
206 {
207 this->handshake = &tls_server_create(&this->public, this->crypto,
208 this->server, this->peer,
209 request_peer_auth)->handshake;
210 }
211 else
212 {
213 this->handshake = &tls_peer_create(&this->public, this->crypto,
214 this->peer, this->server)->handshake;
215 }
216 this->fragmentation = tls_fragmentation_create(this->handshake,
217 this->application);
218 this->compression = tls_compression_create(this->fragmentation);
219 this->protection = tls_protection_create(&this->public, this->compression);
220 this->crypto->set_protection(this->crypto, this->protection);
221
222 return &this->public;
223 }