TLS server: OCSP stapling
[mech_eap.git] / src / tls / tlsv1_client.c
1 /*
2  * TLS v1.0/v1.1/v1.2 client (RFC 2246, RFC 4346, RFC 5246)
3  * Copyright (c) 2006-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "crypto/sha1.h"
13 #include "crypto/tls.h"
14 #include "x509v3.h"
15 #include "tlsv1_common.h"
16 #include "tlsv1_record.h"
17 #include "tlsv1_client.h"
18 #include "tlsv1_client_i.h"
19
20 /* TODO:
21  * Support for a message fragmented across several records (RFC 2246, 6.2.1)
22  */
23
24
25 void tls_alert(struct tlsv1_client *conn, u8 level, u8 description)
26 {
27         conn->alert_level = level;
28         conn->alert_description = description;
29 }
30
31
32 void tlsv1_client_free_dh(struct tlsv1_client *conn)
33 {
34         os_free(conn->dh_p);
35         os_free(conn->dh_g);
36         os_free(conn->dh_ys);
37         conn->dh_p = conn->dh_g = conn->dh_ys = NULL;
38 }
39
40
41 int tls_derive_pre_master_secret(u8 *pre_master_secret)
42 {
43         WPA_PUT_BE16(pre_master_secret, TLS_VERSION);
44         if (os_get_random(pre_master_secret + 2,
45                           TLS_PRE_MASTER_SECRET_LEN - 2))
46                 return -1;
47         return 0;
48 }
49
50
51 int tls_derive_keys(struct tlsv1_client *conn,
52                     const u8 *pre_master_secret, size_t pre_master_secret_len)
53 {
54         u8 seed[2 * TLS_RANDOM_LEN];
55         u8 key_block[TLS_MAX_KEY_BLOCK_LEN];
56         u8 *pos;
57         size_t key_block_len;
58
59         if (pre_master_secret) {
60                 wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: pre_master_secret",
61                                 pre_master_secret, pre_master_secret_len);
62                 os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
63                 os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
64                           TLS_RANDOM_LEN);
65                 if (tls_prf(conn->rl.tls_version,
66                             pre_master_secret, pre_master_secret_len,
67                             "master secret", seed, 2 * TLS_RANDOM_LEN,
68                             conn->master_secret, TLS_MASTER_SECRET_LEN)) {
69                         wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive "
70                                    "master_secret");
71                         return -1;
72                 }
73                 wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: master_secret",
74                                 conn->master_secret, TLS_MASTER_SECRET_LEN);
75         }
76
77         os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
78         os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random, TLS_RANDOM_LEN);
79         key_block_len = 2 * (conn->rl.hash_size + conn->rl.key_material_len);
80         if (conn->rl.tls_version == TLS_VERSION_1)
81                 key_block_len += 2 * conn->rl.iv_size;
82         if (tls_prf(conn->rl.tls_version,
83                     conn->master_secret, TLS_MASTER_SECRET_LEN,
84                     "key expansion", seed, 2 * TLS_RANDOM_LEN,
85                     key_block, key_block_len)) {
86                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive key_block");
87                 return -1;
88         }
89         wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: key_block",
90                         key_block, key_block_len);
91
92         pos = key_block;
93
94         /* client_write_MAC_secret */
95         os_memcpy(conn->rl.write_mac_secret, pos, conn->rl.hash_size);
96         pos += conn->rl.hash_size;
97         /* server_write_MAC_secret */
98         os_memcpy(conn->rl.read_mac_secret, pos, conn->rl.hash_size);
99         pos += conn->rl.hash_size;
100
101         /* client_write_key */
102         os_memcpy(conn->rl.write_key, pos, conn->rl.key_material_len);
103         pos += conn->rl.key_material_len;
104         /* server_write_key */
105         os_memcpy(conn->rl.read_key, pos, conn->rl.key_material_len);
106         pos += conn->rl.key_material_len;
107
108         if (conn->rl.tls_version == TLS_VERSION_1) {
109                 /* client_write_IV */
110                 os_memcpy(conn->rl.write_iv, pos, conn->rl.iv_size);
111                 pos += conn->rl.iv_size;
112                 /* server_write_IV */
113                 os_memcpy(conn->rl.read_iv, pos, conn->rl.iv_size);
114                 pos += conn->rl.iv_size;
115         } else {
116                 /*
117                  * Use IV field to set the mask value for TLS v1.1. A fixed
118                  * mask of zero is used per the RFC 4346, 6.2.3.2 CBC Block
119                  * Cipher option 2a.
120                  */
121                 os_memset(conn->rl.write_iv, 0, conn->rl.iv_size);
122         }
123
124         return 0;
125 }
126
127
128 /**
129  * tlsv1_client_handshake - Process TLS handshake
130  * @conn: TLSv1 client connection data from tlsv1_client_init()
131  * @in_data: Input data from TLS peer
132  * @in_len: Input data length
133  * @out_len: Length of the output buffer.
134  * @appl_data: Pointer to application data pointer, or %NULL if dropped
135  * @appl_data_len: Pointer to variable that is set to appl_data length
136  * @need_more_data: Set to 1 if more data would be needed to complete
137  *      processing
138  * Returns: Pointer to output data, %NULL on failure
139  */
140 u8 * tlsv1_client_handshake(struct tlsv1_client *conn,
141                             const u8 *in_data, size_t in_len,
142                             size_t *out_len, u8 **appl_data,
143                             size_t *appl_data_len, int *need_more_data)
144 {
145         const u8 *pos, *end;
146         u8 *msg = NULL, *in_msg = NULL, *in_pos, *in_end, alert, ct;
147         size_t in_msg_len;
148         int no_appl_data;
149         int used;
150
151         if (need_more_data)
152                 *need_more_data = 0;
153
154         if (conn->state == CLIENT_HELLO) {
155                 if (in_len)
156                         return NULL;
157                 return tls_send_client_hello(conn, out_len);
158         }
159
160         if (conn->partial_input) {
161                 if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
162                         wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
163                                    "memory for pending record");
164                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
165                                   TLS_ALERT_INTERNAL_ERROR);
166                         goto failed;
167                 }
168                 wpabuf_put_data(conn->partial_input, in_data, in_len);
169                 in_data = wpabuf_head(conn->partial_input);
170                 in_len = wpabuf_len(conn->partial_input);
171         }
172
173         if (in_data == NULL || in_len == 0)
174                 return NULL;
175
176         pos = in_data;
177         end = in_data + in_len;
178         in_msg = os_malloc(in_len);
179         if (in_msg == NULL)
180                 return NULL;
181
182         /* Each received packet may include multiple records */
183         while (pos < end) {
184                 in_msg_len = in_len;
185                 used = tlsv1_record_receive(&conn->rl, pos, end - pos,
186                                             in_msg, &in_msg_len, &alert);
187                 if (used < 0) {
188                         wpa_printf(MSG_DEBUG, "TLSv1: Processing received "
189                                    "record failed");
190                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
191                         goto failed;
192                 }
193                 if (used == 0) {
194                         struct wpabuf *partial;
195                         wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
196                         partial = wpabuf_alloc_copy(pos, end - pos);
197                         wpabuf_free(conn->partial_input);
198                         conn->partial_input = partial;
199                         if (conn->partial_input == NULL) {
200                                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
201                                            "allocate memory for pending "
202                                            "record");
203                                 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
204                                           TLS_ALERT_INTERNAL_ERROR);
205                                 goto failed;
206                         }
207                         os_free(in_msg);
208                         if (need_more_data)
209                                 *need_more_data = 1;
210                         return NULL;
211                 }
212                 ct = pos[0];
213
214                 in_pos = in_msg;
215                 in_end = in_msg + in_msg_len;
216
217                 /* Each received record may include multiple messages of the
218                  * same ContentType. */
219                 while (in_pos < in_end) {
220                         in_msg_len = in_end - in_pos;
221                         if (tlsv1_client_process_handshake(conn, ct, in_pos,
222                                                            &in_msg_len,
223                                                            appl_data,
224                                                            appl_data_len) < 0)
225                                 goto failed;
226                         in_pos += in_msg_len;
227                 }
228
229                 pos += used;
230         }
231
232         os_free(in_msg);
233         in_msg = NULL;
234
235         no_appl_data = appl_data == NULL || *appl_data == NULL;
236         msg = tlsv1_client_handshake_write(conn, out_len, no_appl_data);
237
238 failed:
239         os_free(in_msg);
240         if (conn->alert_level) {
241                 wpabuf_free(conn->partial_input);
242                 conn->partial_input = NULL;
243                 conn->state = FAILED;
244                 os_free(msg);
245                 msg = tlsv1_client_send_alert(conn, conn->alert_level,
246                                               conn->alert_description,
247                                               out_len);
248         } else if (msg == NULL) {
249                 msg = os_zalloc(1);
250                 *out_len = 0;
251         }
252
253         if (need_more_data == NULL || !(*need_more_data)) {
254                 wpabuf_free(conn->partial_input);
255                 conn->partial_input = NULL;
256         }
257
258         return msg;
259 }
260
261
262 /**
263  * tlsv1_client_encrypt - Encrypt data into TLS tunnel
264  * @conn: TLSv1 client connection data from tlsv1_client_init()
265  * @in_data: Pointer to plaintext data to be encrypted
266  * @in_len: Input buffer length
267  * @out_data: Pointer to output buffer (encrypted TLS data)
268  * @out_len: Maximum out_data length 
269  * Returns: Number of bytes written to out_data, -1 on failure
270  *
271  * This function is used after TLS handshake has been completed successfully to
272  * send data in the encrypted tunnel.
273  */
274 int tlsv1_client_encrypt(struct tlsv1_client *conn,
275                          const u8 *in_data, size_t in_len,
276                          u8 *out_data, size_t out_len)
277 {
278         size_t rlen;
279
280         wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Plaintext AppData",
281                         in_data, in_len);
282
283         if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_APPLICATION_DATA,
284                               out_data, out_len, in_data, in_len, &rlen) < 0) {
285                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
286                 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
287                           TLS_ALERT_INTERNAL_ERROR);
288                 return -1;
289         }
290
291         return rlen;
292 }
293
294
295 /**
296  * tlsv1_client_decrypt - Decrypt data from TLS tunnel
297  * @conn: TLSv1 client connection data from tlsv1_client_init()
298  * @in_data: Pointer to input buffer (encrypted TLS data)
299  * @in_len: Input buffer length
300  * @need_more_data: Set to 1 if more data would be needed to complete
301  *      processing
302  * Returns: Decrypted data or %NULL on failure
303  *
304  * This function is used after TLS handshake has been completed successfully to
305  * receive data from the encrypted tunnel.
306  */
307 struct wpabuf * tlsv1_client_decrypt(struct tlsv1_client *conn,
308                                      const u8 *in_data, size_t in_len,
309                                      int *need_more_data)
310 {
311         const u8 *in_end, *pos;
312         int used;
313         u8 alert, *out_pos, ct;
314         size_t olen;
315         struct wpabuf *buf = NULL;
316
317         if (need_more_data)
318                 *need_more_data = 0;
319
320         if (conn->partial_input) {
321                 if (wpabuf_resize(&conn->partial_input, in_len) < 0) {
322                         wpa_printf(MSG_DEBUG, "TLSv1: Failed to allocate "
323                                    "memory for pending record");
324                         alert = TLS_ALERT_INTERNAL_ERROR;
325                         goto fail;
326                 }
327                 wpabuf_put_data(conn->partial_input, in_data, in_len);
328                 in_data = wpabuf_head(conn->partial_input);
329                 in_len = wpabuf_len(conn->partial_input);
330         }
331
332         pos = in_data;
333         in_end = in_data + in_len;
334
335         while (pos < in_end) {
336                 ct = pos[0];
337                 if (wpabuf_resize(&buf, in_end - pos) < 0) {
338                         alert = TLS_ALERT_INTERNAL_ERROR;
339                         goto fail;
340                 }
341                 out_pos = wpabuf_put(buf, 0);
342                 olen = wpabuf_tailroom(buf);
343                 used = tlsv1_record_receive(&conn->rl, pos, in_end - pos,
344                                             out_pos, &olen, &alert);
345                 if (used < 0) {
346                         wpa_printf(MSG_DEBUG, "TLSv1: Record layer processing "
347                                    "failed");
348                         goto fail;
349                 }
350                 if (used == 0) {
351                         struct wpabuf *partial;
352                         wpa_printf(MSG_DEBUG, "TLSv1: Need more data");
353                         partial = wpabuf_alloc_copy(pos, in_end - pos);
354                         wpabuf_free(conn->partial_input);
355                         conn->partial_input = partial;
356                         if (conn->partial_input == NULL) {
357                                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to "
358                                            "allocate memory for pending "
359                                            "record");
360                                 alert = TLS_ALERT_INTERNAL_ERROR;
361                                 goto fail;
362                         }
363                         if (need_more_data)
364                                 *need_more_data = 1;
365                         return buf;
366                 }
367
368                 if (ct == TLS_CONTENT_TYPE_ALERT) {
369                         if (olen < 2) {
370                                 wpa_printf(MSG_DEBUG, "TLSv1: Alert "
371                                            "underflow");
372                                 alert = TLS_ALERT_DECODE_ERROR;
373                                 goto fail;
374                         }
375                         wpa_printf(MSG_DEBUG, "TLSv1: Received alert %d:%d",
376                                    out_pos[0], out_pos[1]);
377                         if (out_pos[0] == TLS_ALERT_LEVEL_WARNING) {
378                                 /* Continue processing */
379                                 pos += used;
380                                 continue;
381                         }
382
383                         alert = out_pos[1];
384                         goto fail;
385                 }
386
387                 if (ct != TLS_CONTENT_TYPE_APPLICATION_DATA) {
388                         wpa_printf(MSG_DEBUG, "TLSv1: Unexpected content type "
389                                    "0x%x when decrypting application data",
390                                    pos[0]);
391                         alert = TLS_ALERT_UNEXPECTED_MESSAGE;
392                         goto fail;
393                 }
394
395                 wpabuf_put(buf, olen);
396
397                 pos += used;
398         }
399
400         wpabuf_free(conn->partial_input);
401         conn->partial_input = NULL;
402         return buf;
403
404 fail:
405         wpabuf_free(buf);
406         wpabuf_free(conn->partial_input);
407         conn->partial_input = NULL;
408         tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
409         return NULL;
410 }
411
412
413 /**
414  * tlsv1_client_global_init - Initialize TLSv1 client
415  * Returns: 0 on success, -1 on failure
416  *
417  * This function must be called before using any other TLSv1 client functions.
418  */
419 int tlsv1_client_global_init(void)
420 {
421         return crypto_global_init();
422 }
423
424
425 /**
426  * tlsv1_client_global_deinit - Deinitialize TLSv1 client
427  *
428  * This function can be used to deinitialize the TLSv1 client that was
429  * initialized by calling tlsv1_client_global_init(). No TLSv1 client functions
430  * can be called after this before calling tlsv1_client_global_init() again.
431  */
432 void tlsv1_client_global_deinit(void)
433 {
434         crypto_global_deinit();
435 }
436
437
438 /**
439  * tlsv1_client_init - Initialize TLSv1 client connection
440  * Returns: Pointer to TLSv1 client connection data or %NULL on failure
441  */
442 struct tlsv1_client * tlsv1_client_init(void)
443 {
444         struct tlsv1_client *conn;
445         size_t count;
446         u16 *suites;
447
448         conn = os_zalloc(sizeof(*conn));
449         if (conn == NULL)
450                 return NULL;
451
452         conn->state = CLIENT_HELLO;
453
454         if (tls_verify_hash_init(&conn->verify) < 0) {
455                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize verify "
456                            "hash");
457                 os_free(conn);
458                 return NULL;
459         }
460
461         count = 0;
462         suites = conn->cipher_suites;
463         suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA256;
464         suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
465         suites[count++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
466         suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
467         suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA256;
468         suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
469         suites[count++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
470         suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
471         suites[count++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
472         suites[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
473         suites[count++] = TLS_RSA_WITH_RC4_128_SHA;
474         suites[count++] = TLS_RSA_WITH_RC4_128_MD5;
475         conn->num_cipher_suites = count;
476
477         conn->rl.tls_version = TLS_VERSION;
478
479         return conn;
480 }
481
482
483 /**
484  * tlsv1_client_deinit - Deinitialize TLSv1 client connection
485  * @conn: TLSv1 client connection data from tlsv1_client_init()
486  */
487 void tlsv1_client_deinit(struct tlsv1_client *conn)
488 {
489         crypto_public_key_free(conn->server_rsa_key);
490         tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
491         tlsv1_record_change_write_cipher(&conn->rl);
492         tlsv1_record_change_read_cipher(&conn->rl);
493         tls_verify_hash_free(&conn->verify);
494         os_free(conn->client_hello_ext);
495         tlsv1_client_free_dh(conn);
496         tlsv1_cred_free(conn->cred);
497         wpabuf_free(conn->partial_input);
498         x509_certificate_chain_free(conn->server_cert);
499         os_free(conn);
500 }
501
502
503 /**
504  * tlsv1_client_established - Check whether connection has been established
505  * @conn: TLSv1 client connection data from tlsv1_client_init()
506  * Returns: 1 if connection is established, 0 if not
507  */
508 int tlsv1_client_established(struct tlsv1_client *conn)
509 {
510         return conn->state == ESTABLISHED;
511 }
512
513
514 /**
515  * tlsv1_client_prf - Use TLS-PRF to derive keying material
516  * @conn: TLSv1 client connection data from tlsv1_client_init()
517  * @label: Label (e.g., description of the key) for PRF
518  * @server_random_first: seed is 0 = client_random|server_random,
519  * 1 = server_random|client_random
520  * @out: Buffer for output data from TLS-PRF
521  * @out_len: Length of the output buffer
522  * Returns: 0 on success, -1 on failure
523  */
524 int tlsv1_client_prf(struct tlsv1_client *conn, const char *label,
525                      int server_random_first, u8 *out, size_t out_len)
526 {
527         u8 seed[2 * TLS_RANDOM_LEN];
528
529         if (conn->state != ESTABLISHED)
530                 return -1;
531
532         if (server_random_first) {
533                 os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
534                 os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random,
535                           TLS_RANDOM_LEN);
536         } else {
537                 os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
538                 os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
539                           TLS_RANDOM_LEN);
540         }
541
542         return tls_prf(conn->rl.tls_version,
543                        conn->master_secret, TLS_MASTER_SECRET_LEN,
544                        label, seed, 2 * TLS_RANDOM_LEN, out, out_len);
545 }
546
547
548 /**
549  * tlsv1_client_get_cipher - Get current cipher name
550  * @conn: TLSv1 client connection data from tlsv1_client_init()
551  * @buf: Buffer for the cipher name
552  * @buflen: buf size
553  * Returns: 0 on success, -1 on failure
554  *
555  * Get the name of the currently used cipher.
556  */
557 int tlsv1_client_get_cipher(struct tlsv1_client *conn, char *buf,
558                             size_t buflen)
559 {
560         char *cipher;
561
562         switch (conn->rl.cipher_suite) {
563         case TLS_RSA_WITH_RC4_128_MD5:
564                 cipher = "RC4-MD5";
565                 break;
566         case TLS_RSA_WITH_RC4_128_SHA:
567                 cipher = "RC4-SHA";
568                 break;
569         case TLS_RSA_WITH_DES_CBC_SHA:
570                 cipher = "DES-CBC-SHA";
571                 break;
572         case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
573                 cipher = "DES-CBC3-SHA";
574                 break;
575         case TLS_DHE_RSA_WITH_DES_CBC_SHA:
576                 cipher = "DHE-RSA-DES-CBC-SHA";
577                 break;
578         case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
579                 cipher = "DHE-RSA-DES-CBC3-SHA";
580                 break;
581         case TLS_DH_anon_WITH_RC4_128_MD5:
582                 cipher = "ADH-RC4-MD5";
583                 break;
584         case TLS_DH_anon_WITH_DES_CBC_SHA:
585                 cipher = "ADH-DES-SHA";
586                 break;
587         case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
588                 cipher = "ADH-DES-CBC3-SHA";
589                 break;
590         case TLS_RSA_WITH_AES_128_CBC_SHA:
591                 cipher = "AES-128-SHA";
592                 break;
593         case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
594                 cipher = "DHE-RSA-AES-128-SHA";
595                 break;
596         case TLS_DH_anon_WITH_AES_128_CBC_SHA:
597                 cipher = "ADH-AES-128-SHA";
598                 break;
599         case TLS_RSA_WITH_AES_256_CBC_SHA:
600                 cipher = "AES-256-SHA";
601                 break;
602         case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
603                 cipher = "DHE-RSA-AES-256-SHA";
604                 break;
605         case TLS_DH_anon_WITH_AES_256_CBC_SHA:
606                 cipher = "ADH-AES-256-SHA";
607                 break;
608         case TLS_RSA_WITH_AES_128_CBC_SHA256:
609                 cipher = "AES-128-SHA256";
610                 break;
611         case TLS_RSA_WITH_AES_256_CBC_SHA256:
612                 cipher = "AES-256-SHA256";
613                 break;
614         case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
615                 cipher = "DHE-RSA-AES-128-SHA256";
616                 break;
617         case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
618                 cipher = "DHE-RSA-AES-256-SHA256";
619                 break;
620         case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
621                 cipher = "ADH-AES-128-SHA256";
622                 break;
623         case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
624                 cipher = "ADH-AES-256-SHA256";
625                 break;
626         default:
627                 return -1;
628         }
629
630         if (os_strlcpy(buf, cipher, buflen) >= buflen)
631                 return -1;
632         return 0;
633 }
634
635
636 /**
637  * tlsv1_client_shutdown - Shutdown TLS connection
638  * @conn: TLSv1 client connection data from tlsv1_client_init()
639  * Returns: 0 on success, -1 on failure
640  */
641 int tlsv1_client_shutdown(struct tlsv1_client *conn)
642 {
643         conn->state = CLIENT_HELLO;
644
645         if (tls_verify_hash_init(&conn->verify) < 0) {
646                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to re-initialize verify "
647                            "hash");
648                 return -1;
649         }
650
651         tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
652         tlsv1_record_change_write_cipher(&conn->rl);
653         tlsv1_record_change_read_cipher(&conn->rl);
654
655         conn->certificate_requested = 0;
656         crypto_public_key_free(conn->server_rsa_key);
657         conn->server_rsa_key = NULL;
658         conn->session_resumed = 0;
659
660         return 0;
661 }
662
663
664 /**
665  * tlsv1_client_resumed - Was session resumption used
666  * @conn: TLSv1 client connection data from tlsv1_client_init()
667  * Returns: 1 if current session used session resumption, 0 if not
668  */
669 int tlsv1_client_resumed(struct tlsv1_client *conn)
670 {
671         return !!conn->session_resumed;
672 }
673
674
675 /**
676  * tlsv1_client_hello_ext - Set TLS extension for ClientHello
677  * @conn: TLSv1 client connection data from tlsv1_client_init()
678  * @ext_type: Extension type
679  * @data: Extension payload (%NULL to remove extension)
680  * @data_len: Extension payload length
681  * Returns: 0 on success, -1 on failure
682  */
683 int tlsv1_client_hello_ext(struct tlsv1_client *conn, int ext_type,
684                            const u8 *data, size_t data_len)
685 {
686         u8 *pos;
687
688         conn->session_ticket_included = 0;
689         os_free(conn->client_hello_ext);
690         conn->client_hello_ext = NULL;
691         conn->client_hello_ext_len = 0;
692
693         if (data == NULL || data_len == 0)
694                 return 0;
695
696         pos = conn->client_hello_ext = os_malloc(4 + data_len);
697         if (pos == NULL)
698                 return -1;
699
700         WPA_PUT_BE16(pos, ext_type);
701         pos += 2;
702         WPA_PUT_BE16(pos, data_len);
703         pos += 2;
704         os_memcpy(pos, data, data_len);
705         conn->client_hello_ext_len = 4 + data_len;
706
707         if (ext_type == TLS_EXT_PAC_OPAQUE) {
708                 conn->session_ticket_included = 1;
709                 wpa_printf(MSG_DEBUG, "TLSv1: Using session ticket");
710         }
711
712         return 0;
713 }
714
715
716 /**
717  * tlsv1_client_get_random - Get random data from TLS connection
718  * @conn: TLSv1 client connection data from tlsv1_client_init()
719  * @keys: Structure of random data (filled on success)
720  * Returns: 0 on success, -1 on failure
721  */
722 int tlsv1_client_get_random(struct tlsv1_client *conn, struct tls_random *keys)
723 {
724         os_memset(keys, 0, sizeof(*keys));
725         if (conn->state == CLIENT_HELLO)
726                 return -1;
727
728         keys->client_random = conn->client_random;
729         keys->client_random_len = TLS_RANDOM_LEN;
730
731         if (conn->state != SERVER_HELLO) {
732                 keys->server_random = conn->server_random;
733                 keys->server_random_len = TLS_RANDOM_LEN;
734         }
735
736         return 0;
737 }
738
739
740 /**
741  * tlsv1_client_get_keyblock_size - Get TLS key_block size
742  * @conn: TLSv1 client connection data from tlsv1_client_init()
743  * Returns: Size of the key_block for the negotiated cipher suite or -1 on
744  * failure
745  */
746 int tlsv1_client_get_keyblock_size(struct tlsv1_client *conn)
747 {
748         if (conn->state == CLIENT_HELLO || conn->state == SERVER_HELLO)
749                 return -1;
750
751         return 2 * (conn->rl.hash_size + conn->rl.key_material_len +
752                     conn->rl.iv_size);
753 }
754
755
756 /**
757  * tlsv1_client_set_cipher_list - Configure acceptable cipher suites
758  * @conn: TLSv1 client connection data from tlsv1_client_init()
759  * @ciphers: Zero (TLS_CIPHER_NONE) terminated list of allowed ciphers
760  * (TLS_CIPHER_*).
761  * Returns: 0 on success, -1 on failure
762  */
763 int tlsv1_client_set_cipher_list(struct tlsv1_client *conn, u8 *ciphers)
764 {
765         size_t count;
766         u16 *suites;
767
768         /* TODO: implement proper configuration of cipher suites */
769         if (ciphers[0] == TLS_CIPHER_ANON_DH_AES128_SHA) {
770                 count = 0;
771                 suites = conn->cipher_suites;
772                 suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA256;
773                 suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA;
774                 suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA256;
775                 suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
776                 suites[count++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA;
777                 suites[count++] = TLS_DH_anon_WITH_RC4_128_MD5;
778                 suites[count++] = TLS_DH_anon_WITH_DES_CBC_SHA;
779
780                 /*
781                  * Cisco AP (at least 350 and 1200 series) local authentication
782                  * server does not know how to search cipher suites from the
783                  * list and seem to require that the last entry in the list is
784                  * the one that it wants to use. However, TLS specification
785                  * requires the list to be in the client preference order. As a
786                  * workaround, add anon-DH AES-128-SHA1 again at the end of the
787                  * list to allow the Cisco code to find it.
788                  */
789                 suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
790                 conn->num_cipher_suites = count;
791         }
792
793         return 0;
794 }
795
796
797 /**
798  * tlsv1_client_set_cred - Set client credentials
799  * @conn: TLSv1 client connection data from tlsv1_client_init()
800  * @cred: Credentials from tlsv1_cred_alloc()
801  * Returns: 0 on success, -1 on failure
802  *
803  * On success, the client takes ownership of the credentials block and caller
804  * must not free it. On failure, caller is responsible for freeing the
805  * credential block.
806  */
807 int tlsv1_client_set_cred(struct tlsv1_client *conn,
808                           struct tlsv1_credentials *cred)
809 {
810         tlsv1_cred_free(conn->cred);
811         conn->cred = cred;
812         return 0;
813 }
814
815
816 /**
817  * tlsv1_client_set_flags - Set connection flags
818  * @conn: TLSv1 client connection data from tlsv1_client_init()
819  * @flags: TLS_CONN_* bitfield
820  */
821 void tlsv1_client_set_flags(struct tlsv1_client *conn, unsigned int flags)
822 {
823         conn->flags = flags;
824 }
825
826
827 void tlsv1_client_set_session_ticket_cb(struct tlsv1_client *conn,
828                                         tlsv1_client_session_ticket_cb cb,
829                                         void *ctx)
830 {
831         wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback set %p (ctx %p)",
832                    cb, ctx);
833         conn->session_ticket_cb = cb;
834         conn->session_ticket_cb_ctx = ctx;
835 }
836
837
838 void tlsv1_client_set_cb(struct tlsv1_client *conn,
839                          void (*event_cb)(void *ctx, enum tls_event ev,
840                                           union tls_event_data *data),
841                          void *cb_ctx,
842                          int cert_in_cb)
843 {
844         conn->event_cb = event_cb;
845         conn->cb_ctx = cb_ctx;
846         conn->cert_in_cb = !!cert_in_cb;
847 }
848
849
850 int tlsv1_client_get_version(struct tlsv1_client *conn, char *buf,
851                              size_t buflen)
852 {
853         if (!conn)
854                 return -1;
855         switch (conn->rl.tls_version) {
856         case TLS_VERSION_1:
857                 os_strlcpy(buf, "TLSv1", buflen);
858                 break;
859         case TLS_VERSION_1_1:
860                 os_strlcpy(buf, "TLSv1.1", buflen);
861                 break;
862         case TLS_VERSION_1_2:
863                 os_strlcpy(buf, "TLSv1.2", buflen);
864                 break;
865         default:
866                 return -1;
867         }
868
869         return 0;
870 }