2 * TLSv1 server (RFC 2246)
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "tlsv1_common.h"
21 #include "tlsv1_record.h"
22 #include "tlsv1_server.h"
23 #include "tlsv1_server_i.h"
26 * Support for a message fragmented across several records (RFC 2246, 6.2.1)
30 void tlsv1_server_alert(struct tlsv1_server
*conn
, u8 level
, u8 description
)
32 conn
->alert_level
= level
;
33 conn
->alert_description
= description
;
37 int tlsv1_server_derive_keys(struct tlsv1_server
*conn
,
38 const u8
*pre_master_secret
,
39 size_t pre_master_secret_len
)
41 u8 seed
[2 * TLS_RANDOM_LEN
];
42 u8 key_block
[TLS_MAX_KEY_BLOCK_LEN
];
46 if (pre_master_secret
) {
47 wpa_hexdump_key(MSG_MSGDUMP
, "TLSv1: pre_master_secret",
48 pre_master_secret
, pre_master_secret_len
);
49 os_memcpy(seed
, conn
->client_random
, TLS_RANDOM_LEN
);
50 os_memcpy(seed
+ TLS_RANDOM_LEN
, conn
->server_random
,
52 if (tls_prf(pre_master_secret
, pre_master_secret_len
,
53 "master secret", seed
, 2 * TLS_RANDOM_LEN
,
54 conn
->master_secret
, TLS_MASTER_SECRET_LEN
)) {
55 wpa_printf(MSG_DEBUG
, "TLSv1: Failed to derive "
59 wpa_hexdump_key(MSG_MSGDUMP
, "TLSv1: master_secret",
60 conn
->master_secret
, TLS_MASTER_SECRET_LEN
);
63 os_memcpy(seed
, conn
->server_random
, TLS_RANDOM_LEN
);
64 os_memcpy(seed
+ TLS_RANDOM_LEN
, conn
->client_random
, TLS_RANDOM_LEN
);
65 key_block_len
= 2 * (conn
->rl
.hash_size
+ conn
->rl
.key_material_len
+
67 if (tls_prf(conn
->master_secret
, TLS_MASTER_SECRET_LEN
,
68 "key expansion", seed
, 2 * TLS_RANDOM_LEN
,
69 key_block
, key_block_len
)) {
70 wpa_printf(MSG_DEBUG
, "TLSv1: Failed to derive key_block");
73 wpa_hexdump_key(MSG_MSGDUMP
, "TLSv1: key_block",
74 key_block
, key_block_len
);
78 /* client_write_MAC_secret */
79 os_memcpy(conn
->rl
.read_mac_secret
, pos
, conn
->rl
.hash_size
);
80 pos
+= conn
->rl
.hash_size
;
81 /* server_write_MAC_secret */
82 os_memcpy(conn
->rl
.write_mac_secret
, pos
, conn
->rl
.hash_size
);
83 pos
+= conn
->rl
.hash_size
;
85 /* client_write_key */
86 os_memcpy(conn
->rl
.read_key
, pos
, conn
->rl
.key_material_len
);
87 pos
+= conn
->rl
.key_material_len
;
88 /* server_write_key */
89 os_memcpy(conn
->rl
.write_key
, pos
, conn
->rl
.key_material_len
);
90 pos
+= conn
->rl
.key_material_len
;
93 os_memcpy(conn
->rl
.read_iv
, pos
, conn
->rl
.iv_size
);
94 pos
+= conn
->rl
.iv_size
;
96 os_memcpy(conn
->rl
.write_iv
, pos
, conn
->rl
.iv_size
);
97 pos
+= conn
->rl
.iv_size
;
104 * tlsv1_server_handshake - Process TLS handshake
105 * @conn: TLSv1 server connection data from tlsv1_server_init()
106 * @in_data: Input data from TLS peer
107 * @in_len: Input data length
108 * @out_len: Length of the output buffer.
109 * Returns: Pointer to output data, %NULL on failure
111 u8
* tlsv1_server_handshake(struct tlsv1_server
*conn
,
112 const u8
*in_data
, size_t in_len
,
116 u8
*msg
= NULL
, *in_msg
, *in_pos
, *in_end
, alert
, ct
;
119 if (in_data
== NULL
|| in_len
== 0) {
120 wpa_printf(MSG_DEBUG
, "TLSv1: No input data to server");
125 end
= in_data
+ in_len
;
126 in_msg
= os_malloc(in_len
);
130 /* Each received packet may include multiple records */
133 if (tlsv1_record_receive(&conn
->rl
, pos
, end
- pos
,
134 in_msg
, &in_msg_len
, &alert
)) {
135 wpa_printf(MSG_DEBUG
, "TLSv1: Processing received "
137 tlsv1_server_alert(conn
, TLS_ALERT_LEVEL_FATAL
, alert
);
143 in_end
= in_msg
+ in_msg_len
;
145 /* Each received record may include multiple messages of the
146 * same ContentType. */
147 while (in_pos
< in_end
) {
148 in_msg_len
= in_end
- in_pos
;
149 if (tlsv1_server_process_handshake(conn
, ct
, in_pos
,
152 in_pos
+= in_msg_len
;
155 pos
+= TLS_RECORD_HEADER_LEN
+ WPA_GET_BE16(pos
+ 3);
161 msg
= tlsv1_server_handshake_write(conn
, out_len
);
165 if (conn
->alert_level
) {
166 if (conn
->state
== FAILED
) {
167 /* Avoid alert loops */
168 wpa_printf(MSG_DEBUG
, "TLSv1: Drop alert loop");
172 conn
->state
= FAILED
;
174 msg
= tlsv1_server_send_alert(conn
, conn
->alert_level
,
175 conn
->alert_description
,
184 * tlsv1_server_encrypt - Encrypt data into TLS tunnel
185 * @conn: TLSv1 server connection data from tlsv1_server_init()
186 * @in_data: Pointer to plaintext data to be encrypted
187 * @in_len: Input buffer length
188 * @out_data: Pointer to output buffer (encrypted TLS data)
189 * @out_len: Maximum out_data length
190 * Returns: Number of bytes written to out_data, -1 on failure
192 * This function is used after TLS handshake has been completed successfully to
193 * send data in the encrypted tunnel.
195 int tlsv1_server_encrypt(struct tlsv1_server
*conn
,
196 const u8
*in_data
, size_t in_len
,
197 u8
*out_data
, size_t out_len
)
201 wpa_hexdump_key(MSG_MSGDUMP
, "TLSv1: Plaintext AppData",
204 os_memcpy(out_data
+ TLS_RECORD_HEADER_LEN
, in_data
, in_len
);
206 if (tlsv1_record_send(&conn
->rl
, TLS_CONTENT_TYPE_APPLICATION_DATA
,
207 out_data
, out_len
, in_len
, &rlen
) < 0) {
208 wpa_printf(MSG_DEBUG
, "TLSv1: Failed to create a record");
209 tlsv1_server_alert(conn
, TLS_ALERT_LEVEL_FATAL
,
210 TLS_ALERT_INTERNAL_ERROR
);
219 * tlsv1_server_decrypt - Decrypt data from TLS tunnel
220 * @conn: TLSv1 server connection data from tlsv1_server_init()
221 * @in_data: Pointer to input buffer (encrypted TLS data)
222 * @in_len: Input buffer length
223 * @out_data: Pointer to output buffer (decrypted data from TLS tunnel)
224 * @out_len: Maximum out_data length
225 * Returns: Number of bytes written to out_data, -1 on failure
227 * This function is used after TLS handshake has been completed successfully to
228 * receive data from the encrypted tunnel.
230 int tlsv1_server_decrypt(struct tlsv1_server
*conn
,
231 const u8
*in_data
, size_t in_len
,
232 u8
*out_data
, size_t out_len
)
234 const u8
*in_end
, *pos
;
236 u8 alert
, *out_end
, *out_pos
;
240 in_end
= in_data
+ in_len
;
242 out_end
= out_data
+ out_len
;
244 while (pos
< in_end
) {
245 if (pos
[0] != TLS_CONTENT_TYPE_APPLICATION_DATA
) {
246 wpa_printf(MSG_DEBUG
, "TLSv1: Unexpected content type "
248 tlsv1_server_alert(conn
, TLS_ALERT_LEVEL_FATAL
,
249 TLS_ALERT_UNEXPECTED_MESSAGE
);
253 olen
= out_end
- out_pos
;
254 res
= tlsv1_record_receive(&conn
->rl
, pos
, in_end
- pos
,
255 out_pos
, &olen
, &alert
);
257 wpa_printf(MSG_DEBUG
, "TLSv1: Record layer processing "
259 tlsv1_server_alert(conn
, TLS_ALERT_LEVEL_FATAL
, alert
);
263 if (out_pos
> out_end
) {
264 wpa_printf(MSG_DEBUG
, "TLSv1: Buffer not large enough "
265 "for processing the received record");
266 tlsv1_server_alert(conn
, TLS_ALERT_LEVEL_FATAL
,
267 TLS_ALERT_INTERNAL_ERROR
);
271 pos
+= TLS_RECORD_HEADER_LEN
+ WPA_GET_BE16(pos
+ 3);
274 return out_pos
- out_data
;
279 * tlsv1_server_global_init - Initialize TLSv1 server
280 * Returns: 0 on success, -1 on failure
282 * This function must be called before using any other TLSv1 server functions.
284 int tlsv1_server_global_init(void)
286 return crypto_global_init();
291 * tlsv1_server_global_deinit - Deinitialize TLSv1 server
293 * This function can be used to deinitialize the TLSv1 server that was
294 * initialized by calling tlsv1_server_global_init(). No TLSv1 server functions
295 * can be called after this before calling tlsv1_server_global_init() again.
297 void tlsv1_server_global_deinit(void)
299 crypto_global_deinit();
304 * tlsv1_server_init - Initialize TLSv1 server connection
305 * @cred: Pointer to server credentials from tlsv1_server_cred_alloc()
306 * Returns: Pointer to TLSv1 server connection data or %NULL on failure
308 struct tlsv1_server
* tlsv1_server_init(struct tlsv1_credentials
*cred
)
310 struct tlsv1_server
*conn
;
314 conn
= os_zalloc(sizeof(*conn
));
320 conn
->state
= CLIENT_HELLO
;
322 if (tls_verify_hash_init(&conn
->verify
) < 0) {
323 wpa_printf(MSG_DEBUG
, "TLSv1: Failed to initialize verify "
330 suites
= conn
->cipher_suites
;
331 #ifndef CONFIG_CRYPTO_INTERNAL
332 suites
[count
++] = TLS_RSA_WITH_AES_256_CBC_SHA
;
333 #endif /* CONFIG_CRYPTO_INTERNAL */
334 suites
[count
++] = TLS_RSA_WITH_AES_128_CBC_SHA
;
335 suites
[count
++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA
;
336 suites
[count
++] = TLS_RSA_WITH_RC4_128_SHA
;
337 suites
[count
++] = TLS_RSA_WITH_RC4_128_MD5
;
338 conn
->num_cipher_suites
= count
;
344 static void tlsv1_server_clear_data(struct tlsv1_server
*conn
)
346 tlsv1_record_set_cipher_suite(&conn
->rl
, TLS_NULL_WITH_NULL_NULL
);
347 tlsv1_record_change_write_cipher(&conn
->rl
);
348 tlsv1_record_change_read_cipher(&conn
->rl
);
349 tls_verify_hash_free(&conn
->verify
);
351 crypto_public_key_free(conn
->client_rsa_key
);
352 conn
->client_rsa_key
= NULL
;
354 os_free(conn
->session_ticket
);
355 conn
->session_ticket
= NULL
;
356 conn
->session_ticket_len
= 0;
357 conn
->use_session_ticket
= 0;
359 os_free(conn
->dh_secret
);
360 conn
->dh_secret
= NULL
;
361 conn
->dh_secret_len
= 0;
366 * tlsv1_server_deinit - Deinitialize TLSv1 server connection
367 * @conn: TLSv1 server connection data from tlsv1_server_init()
369 void tlsv1_server_deinit(struct tlsv1_server
*conn
)
371 tlsv1_server_clear_data(conn
);
377 * tlsv1_server_established - Check whether connection has been established
378 * @conn: TLSv1 server connection data from tlsv1_server_init()
379 * Returns: 1 if connection is established, 0 if not
381 int tlsv1_server_established(struct tlsv1_server
*conn
)
383 return conn
->state
== ESTABLISHED
;
388 * tlsv1_server_prf - Use TLS-PRF to derive keying material
389 * @conn: TLSv1 server connection data from tlsv1_server_init()
390 * @label: Label (e.g., description of the key) for PRF
391 * @server_random_first: seed is 0 = client_random|server_random,
392 * 1 = server_random|client_random
393 * @out: Buffer for output data from TLS-PRF
394 * @out_len: Length of the output buffer
395 * Returns: 0 on success, -1 on failure
397 int tlsv1_server_prf(struct tlsv1_server
*conn
, const char *label
,
398 int server_random_first
, u8
*out
, size_t out_len
)
400 u8 seed
[2 * TLS_RANDOM_LEN
];
402 if (conn
->state
!= ESTABLISHED
)
405 if (server_random_first
) {
406 os_memcpy(seed
, conn
->server_random
, TLS_RANDOM_LEN
);
407 os_memcpy(seed
+ TLS_RANDOM_LEN
, conn
->client_random
,
410 os_memcpy(seed
, conn
->client_random
, TLS_RANDOM_LEN
);
411 os_memcpy(seed
+ TLS_RANDOM_LEN
, conn
->server_random
,
415 return tls_prf(conn
->master_secret
, TLS_MASTER_SECRET_LEN
,
416 label
, seed
, 2 * TLS_RANDOM_LEN
, out
, out_len
);
421 * tlsv1_server_get_cipher - Get current cipher name
422 * @conn: TLSv1 server connection data from tlsv1_server_init()
423 * @buf: Buffer for the cipher name
425 * Returns: 0 on success, -1 on failure
427 * Get the name of the currently used cipher.
429 int tlsv1_server_get_cipher(struct tlsv1_server
*conn
, char *buf
,
434 switch (conn
->rl
.cipher_suite
) {
435 case TLS_RSA_WITH_RC4_128_MD5
:
438 case TLS_RSA_WITH_RC4_128_SHA
:
441 case TLS_RSA_WITH_DES_CBC_SHA
:
442 cipher
= "DES-CBC-SHA";
444 case TLS_RSA_WITH_3DES_EDE_CBC_SHA
:
445 cipher
= "DES-CBC3-SHA";
447 case TLS_DH_anon_WITH_AES_128_CBC_SHA
:
448 cipher
= "ADH-AES-128-SHA";
450 case TLS_RSA_WITH_AES_256_CBC_SHA
:
451 cipher
= "AES-256-SHA";
453 case TLS_RSA_WITH_AES_128_CBC_SHA
:
454 cipher
= "AES-128-SHA";
460 if (os_strlcpy(buf
, cipher
, buflen
) >= buflen
)
467 * tlsv1_server_shutdown - Shutdown TLS connection
468 * @conn: TLSv1 server connection data from tlsv1_server_init()
469 * Returns: 0 on success, -1 on failure
471 int tlsv1_server_shutdown(struct tlsv1_server
*conn
)
473 conn
->state
= CLIENT_HELLO
;
475 if (tls_verify_hash_init(&conn
->verify
) < 0) {
476 wpa_printf(MSG_DEBUG
, "TLSv1: Failed to re-initialize verify "
481 tlsv1_server_clear_data(conn
);
488 * tlsv1_server_resumed - Was session resumption used
489 * @conn: TLSv1 server connection data from tlsv1_server_init()
490 * Returns: 1 if current session used session resumption, 0 if not
492 int tlsv1_server_resumed(struct tlsv1_server
*conn
)
499 * tlsv1_server_get_keys - Get master key and random data from TLS connection
500 * @conn: TLSv1 server connection data from tlsv1_server_init()
501 * @keys: Structure of key/random data (filled on success)
502 * Returns: 0 on success, -1 on failure
504 int tlsv1_server_get_keys(struct tlsv1_server
*conn
, struct tls_keys
*keys
)
506 os_memset(keys
, 0, sizeof(*keys
));
507 if (conn
->state
== CLIENT_HELLO
)
510 keys
->client_random
= conn
->client_random
;
511 keys
->client_random_len
= TLS_RANDOM_LEN
;
513 if (conn
->state
!= SERVER_HELLO
) {
514 keys
->server_random
= conn
->server_random
;
515 keys
->server_random_len
= TLS_RANDOM_LEN
;
516 keys
->master_key
= conn
->master_secret
;
517 keys
->master_key_len
= TLS_MASTER_SECRET_LEN
;
525 * tlsv1_server_get_keyblock_size - Get TLS key_block size
526 * @conn: TLSv1 server connection data from tlsv1_server_init()
527 * Returns: Size of the key_block for the negotiated cipher suite or -1 on
530 int tlsv1_server_get_keyblock_size(struct tlsv1_server
*conn
)
532 if (conn
->state
== CLIENT_HELLO
|| conn
->state
== SERVER_HELLO
)
535 return 2 * (conn
->rl
.hash_size
+ conn
->rl
.key_material_len
+
541 * tlsv1_server_set_cipher_list - Configure acceptable cipher suites
542 * @conn: TLSv1 server connection data from tlsv1_server_init()
543 * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
545 * Returns: 0 on success, -1 on failure
547 int tlsv1_server_set_cipher_list(struct tlsv1_server
*conn
, u8
*ciphers
)
553 /* TODO: implement proper configuration of cipher suites */
554 if (ciphers
[0] == TLS_CIPHER_ANON_DH_AES128_SHA
) {
556 suites
= conn
->cipher_suites
;
557 #ifndef CONFIG_CRYPTO_INTERNAL
558 suites
[count
++] = TLS_RSA_WITH_AES_256_CBC_SHA
;
559 #endif /* CONFIG_CRYPTO_INTERNAL */
560 suites
[count
++] = TLS_RSA_WITH_AES_128_CBC_SHA
;
561 suites
[count
++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA
;
562 suites
[count
++] = TLS_RSA_WITH_RC4_128_SHA
;
563 suites
[count
++] = TLS_RSA_WITH_RC4_128_MD5
;
564 #ifndef CONFIG_CRYPTO_INTERNAL
565 suites
[count
++] = TLS_DH_anon_WITH_AES_256_CBC_SHA
;
566 #endif /* CONFIG_CRYPTO_INTERNAL */
567 suites
[count
++] = TLS_DH_anon_WITH_AES_128_CBC_SHA
;
568 suites
[count
++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA
;
569 suites
[count
++] = TLS_DH_anon_WITH_RC4_128_MD5
;
570 suites
[count
++] = TLS_DH_anon_WITH_DES_CBC_SHA
;
571 conn
->num_cipher_suites
= count
;
577 #endif /* EAP_FAST */
581 int tlsv1_server_set_verify(struct tlsv1_server
*conn
, int verify_peer
)
583 conn
->verify_peer
= verify_peer
;
588 void tlsv1_server_set_session_ticket_cb(struct tlsv1_server
*conn
,
589 tlsv1_server_session_ticket_cb cb
,
592 wpa_printf(MSG_DEBUG
, "TLSv1: SessionTicket callback set %p (ctx %p)",
594 conn
->session_ticket_cb
= cb
;
595 conn
->session_ticket_cb_ctx
= ctx
;