tlsv1_client.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "sha1.h"
00020 #include "tls.h"
00021 #include "tlsv1_common.h"
00022 #include "tlsv1_record.h"
00023 #include "tlsv1_client.h"
00024 #include "tlsv1_client_i.h"
00025 
00026 /* TODO:
00027  * Support for a message fragmented across several records (RFC 2246, 6.2.1)
00028  */
00029 
00030 
00031 void tls_alert(struct tlsv1_client *conn, u8 level, u8 description)
00032 {
00033         conn->alert_level = level;
00034         conn->alert_description = description;
00035 }
00036 
00037 
00038 void tlsv1_client_free_dh(struct tlsv1_client *conn)
00039 {
00040         os_free(conn->dh_p);
00041         os_free(conn->dh_g);
00042         os_free(conn->dh_ys);
00043         conn->dh_p = conn->dh_g = conn->dh_ys = NULL;
00044 }
00045 
00046 
00047 int tls_derive_pre_master_secret(u8 *pre_master_secret)
00048 {
00049         WPA_PUT_BE16(pre_master_secret, TLS_VERSION);
00050         if (os_get_random(pre_master_secret + 2,
00051                           TLS_PRE_MASTER_SECRET_LEN - 2))
00052                 return -1;
00053         return 0;
00054 }
00055 
00056 
00057 int tls_derive_keys(struct tlsv1_client *conn,
00058                     const u8 *pre_master_secret, size_t pre_master_secret_len)
00059 {
00060         u8 seed[2 * TLS_RANDOM_LEN];
00061         u8 key_block[TLS_MAX_KEY_BLOCK_LEN];
00062         u8 *pos;
00063         size_t key_block_len;
00064 
00065         if (pre_master_secret) {
00066                 wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: pre_master_secret",
00067                                 pre_master_secret, pre_master_secret_len);
00068                 os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
00069                 os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
00070                           TLS_RANDOM_LEN);
00071                 if (tls_prf(pre_master_secret, pre_master_secret_len,
00072                             "master secret", seed, 2 * TLS_RANDOM_LEN,
00073                             conn->master_secret, TLS_MASTER_SECRET_LEN)) {
00074                         wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive "
00075                                    "master_secret");
00076                         return -1;
00077                 }
00078                 wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: master_secret",
00079                                 conn->master_secret, TLS_MASTER_SECRET_LEN);
00080         }
00081 
00082         os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
00083         os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random, TLS_RANDOM_LEN);
00084         key_block_len = 2 * (conn->rl.hash_size + conn->rl.key_material_len +
00085                              conn->rl.iv_size);
00086         if (tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
00087                     "key expansion", seed, 2 * TLS_RANDOM_LEN,
00088                     key_block, key_block_len)) {
00089                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to derive key_block");
00090                 return -1;
00091         }
00092         wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: key_block",
00093                         key_block, key_block_len);
00094 
00095         pos = key_block;
00096 
00097         /* client_write_MAC_secret */
00098         os_memcpy(conn->rl.write_mac_secret, pos, conn->rl.hash_size);
00099         pos += conn->rl.hash_size;
00100         /* server_write_MAC_secret */
00101         os_memcpy(conn->rl.read_mac_secret, pos, conn->rl.hash_size);
00102         pos += conn->rl.hash_size;
00103 
00104         /* client_write_key */
00105         os_memcpy(conn->rl.write_key, pos, conn->rl.key_material_len);
00106         pos += conn->rl.key_material_len;
00107         /* server_write_key */
00108         os_memcpy(conn->rl.read_key, pos, conn->rl.key_material_len);
00109         pos += conn->rl.key_material_len;
00110 
00111         /* client_write_IV */
00112         os_memcpy(conn->rl.write_iv, pos, conn->rl.iv_size);
00113         pos += conn->rl.iv_size;
00114         /* server_write_IV */
00115         os_memcpy(conn->rl.read_iv, pos, conn->rl.iv_size);
00116         pos += conn->rl.iv_size;
00117 
00118         return 0;
00119 }
00120 
00121 
00133 u8 * tlsv1_client_handshake(struct tlsv1_client *conn,
00134                             const u8 *in_data, size_t in_len,
00135                             size_t *out_len, u8 **appl_data,
00136                             size_t *appl_data_len)
00137 {
00138         const u8 *pos, *end;
00139         u8 *msg = NULL, *in_msg, *in_pos, *in_end, alert, ct;
00140         size_t in_msg_len;
00141         int no_appl_data;
00142 
00143         if (conn->state == CLIENT_HELLO) {
00144                 if (in_len)
00145                         return NULL;
00146                 return tls_send_client_hello(conn, out_len);
00147         }
00148 
00149         if (in_data == NULL || in_len == 0)
00150                 return NULL;
00151 
00152         pos = in_data;
00153         end = in_data + in_len;
00154         in_msg = os_malloc(in_len);
00155         if (in_msg == NULL)
00156                 return NULL;
00157 
00158         /* Each received packet may include multiple records */
00159         while (pos < end) {
00160                 in_msg_len = in_len;
00161                 if (tlsv1_record_receive(&conn->rl, pos, end - pos,
00162                                          in_msg, &in_msg_len, &alert)) {
00163                         wpa_printf(MSG_DEBUG, "TLSv1: Processing received "
00164                                    "record failed");
00165                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
00166                         goto failed;
00167                 }
00168                 ct = pos[0];
00169 
00170                 in_pos = in_msg;
00171                 in_end = in_msg + in_msg_len;
00172 
00173                 /* Each received record may include multiple messages of the
00174                  * same ContentType. */
00175                 while (in_pos < in_end) {
00176                         in_msg_len = in_end - in_pos;
00177                         if (tlsv1_client_process_handshake(conn, ct, in_pos,
00178                                                            &in_msg_len,
00179                                                            appl_data,
00180                                                            appl_data_len) < 0)
00181                                 goto failed;
00182                         in_pos += in_msg_len;
00183                 }
00184 
00185                 pos += TLS_RECORD_HEADER_LEN + WPA_GET_BE16(pos + 3);
00186         }
00187 
00188         os_free(in_msg);
00189         in_msg = NULL;
00190 
00191         no_appl_data = appl_data == NULL || *appl_data == NULL;
00192         msg = tlsv1_client_handshake_write(conn, out_len, no_appl_data);
00193 
00194 failed:
00195         os_free(in_msg);
00196         if (conn->alert_level) {
00197                 conn->state = FAILED;
00198                 os_free(msg);
00199                 msg = tlsv1_client_send_alert(conn, conn->alert_level,
00200                                               conn->alert_description,
00201                                               out_len);
00202         } else if (msg == NULL) {
00203                 msg = os_zalloc(1);
00204                 *out_len = 0;
00205         }
00206 
00207         return msg;
00208 }
00209 
00210 
00224 int tlsv1_client_encrypt(struct tlsv1_client *conn,
00225                          const u8 *in_data, size_t in_len,
00226                          u8 *out_data, size_t out_len)
00227 {
00228         size_t rlen;
00229 
00230         wpa_hexdump_key(MSG_MSGDUMP, "TLSv1: Plaintext AppData",
00231                         in_data, in_len);
00232 
00233         os_memcpy(out_data + TLS_RECORD_HEADER_LEN, in_data, in_len);
00234 
00235         if (tlsv1_record_send(&conn->rl, TLS_CONTENT_TYPE_APPLICATION_DATA,
00236                               out_data, out_len, in_len, &rlen) < 0) {
00237                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to create a record");
00238                 tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
00239                           TLS_ALERT_INTERNAL_ERROR);
00240                 return -1;
00241         }
00242 
00243         return rlen;
00244 }
00245 
00246 
00260 int tlsv1_client_decrypt(struct tlsv1_client *conn,
00261                          const u8 *in_data, size_t in_len,
00262                          u8 *out_data, size_t out_len)
00263 {
00264         const u8 *in_end, *pos;
00265         int res;
00266         u8 alert, *out_end, *out_pos;
00267         size_t olen;
00268 
00269         pos = in_data;
00270         in_end = in_data + in_len;
00271         out_pos = out_data;
00272         out_end = out_data + out_len;
00273 
00274         while (pos < in_end) {
00275                 if (pos[0] != TLS_CONTENT_TYPE_APPLICATION_DATA) {
00276                         wpa_printf(MSG_DEBUG, "TLSv1: Unexpected content type "
00277                                    "0x%x", pos[0]);
00278                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
00279                                   TLS_ALERT_UNEXPECTED_MESSAGE);
00280                         return -1;
00281                 }
00282 
00283                 olen = out_end - out_pos;
00284                 res = tlsv1_record_receive(&conn->rl, pos, in_end - pos,
00285                                            out_pos, &olen, &alert);
00286                 if (res < 0) {
00287                         wpa_printf(MSG_DEBUG, "TLSv1: Record layer processing "
00288                                    "failed");
00289                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL, alert);
00290                         return -1;
00291                 }
00292                 out_pos += olen;
00293                 if (out_pos > out_end) {
00294                         wpa_printf(MSG_DEBUG, "TLSv1: Buffer not large enough "
00295                                    "for processing the received record");
00296                         tls_alert(conn, TLS_ALERT_LEVEL_FATAL,
00297                                   TLS_ALERT_INTERNAL_ERROR);
00298                         return -1;
00299                 }
00300 
00301                 pos += TLS_RECORD_HEADER_LEN + WPA_GET_BE16(pos + 3);
00302         }
00303 
00304         return out_pos - out_data;
00305 }
00306 
00307 
00315 int tlsv1_client_global_init(void)
00316 {
00317         return crypto_global_init();
00318 }
00319 
00320 
00329 void tlsv1_client_global_deinit(void)
00330 {
00331         crypto_global_deinit();
00332 }
00333 
00334 
00340 struct tlsv1_client * tlsv1_client_init(void)
00341 {
00342         struct tlsv1_client *conn;
00343         size_t count;
00344         u16 *suites;
00345 
00346         conn = os_zalloc(sizeof(*conn));
00347         if (conn == NULL)
00348                 return NULL;
00349 
00350         conn->state = CLIENT_HELLO;
00351 
00352         if (tls_verify_hash_init(&conn->verify) < 0) {
00353                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to initialize verify "
00354                            "hash");
00355                 os_free(conn);
00356                 return NULL;
00357         }
00358 
00359         count = 0;
00360         suites = conn->cipher_suites;
00361 #ifndef CONFIG_CRYPTO_INTERNAL
00362         suites[count++] = TLS_RSA_WITH_AES_256_CBC_SHA;
00363 #endif /* CONFIG_CRYPTO_INTERNAL */
00364         suites[count++] = TLS_RSA_WITH_AES_128_CBC_SHA;
00365         suites[count++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA;
00366         suites[count++] = TLS_RSA_WITH_RC4_128_SHA;
00367         suites[count++] = TLS_RSA_WITH_RC4_128_MD5;
00368         conn->num_cipher_suites = count;
00369 
00370         return conn;
00371 }
00372 
00373 
00379 void tlsv1_client_deinit(struct tlsv1_client *conn)
00380 {
00381         crypto_public_key_free(conn->server_rsa_key);
00382         tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
00383         tlsv1_record_change_write_cipher(&conn->rl);
00384         tlsv1_record_change_read_cipher(&conn->rl);
00385         tls_verify_hash_free(&conn->verify);
00386         os_free(conn->client_hello_ext);
00387         tlsv1_client_free_dh(conn);
00388         tlsv1_cred_free(conn->cred);
00389         os_free(conn);
00390 }
00391 
00392 
00399 int tlsv1_client_established(struct tlsv1_client *conn)
00400 {
00401         return conn->state == ESTABLISHED;
00402 }
00403 
00404 
00416 int tlsv1_client_prf(struct tlsv1_client *conn, const char *label,
00417                      int server_random_first, u8 *out, size_t out_len)
00418 {
00419         u8 seed[2 * TLS_RANDOM_LEN];
00420 
00421         if (conn->state != ESTABLISHED)
00422                 return -1;
00423 
00424         if (server_random_first) {
00425                 os_memcpy(seed, conn->server_random, TLS_RANDOM_LEN);
00426                 os_memcpy(seed + TLS_RANDOM_LEN, conn->client_random,
00427                           TLS_RANDOM_LEN);
00428         } else {
00429                 os_memcpy(seed, conn->client_random, TLS_RANDOM_LEN);
00430                 os_memcpy(seed + TLS_RANDOM_LEN, conn->server_random,
00431                           TLS_RANDOM_LEN);
00432         }
00433 
00434         return tls_prf(conn->master_secret, TLS_MASTER_SECRET_LEN,
00435                        label, seed, 2 * TLS_RANDOM_LEN, out, out_len);
00436 }
00437 
00438 
00449 int tlsv1_client_get_cipher(struct tlsv1_client *conn, char *buf,
00450                             size_t buflen)
00451 {
00452         char *cipher;
00453 
00454         switch (conn->rl.cipher_suite) {
00455         case TLS_RSA_WITH_RC4_128_MD5:
00456                 cipher = "RC4-MD5";
00457                 break;
00458         case TLS_RSA_WITH_RC4_128_SHA:
00459                 cipher = "RC4-SHA";
00460                 break;
00461         case TLS_RSA_WITH_DES_CBC_SHA:
00462                 cipher = "DES-CBC-SHA";
00463                 break;
00464         case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
00465                 cipher = "DES-CBC3-SHA";
00466                 break;
00467         case TLS_DH_anon_WITH_AES_128_CBC_SHA:
00468                 cipher = "ADH-AES-128-SHA";
00469                 break;
00470         case TLS_RSA_WITH_AES_256_CBC_SHA:
00471                 cipher = "AES-256-SHA";
00472                 break;
00473         case TLS_RSA_WITH_AES_128_CBC_SHA:
00474                 cipher = "AES-128-SHA";
00475                 break;
00476         default:
00477                 return -1;
00478         }
00479 
00480         if (os_strlcpy(buf, cipher, buflen) >= buflen)
00481                 return -1;
00482         return 0;
00483 }
00484 
00485 
00492 int tlsv1_client_shutdown(struct tlsv1_client *conn)
00493 {
00494         conn->state = CLIENT_HELLO;
00495 
00496         if (tls_verify_hash_init(&conn->verify) < 0) {
00497                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to re-initialize verify "
00498                            "hash");
00499                 return -1;
00500         }
00501 
00502         tlsv1_record_set_cipher_suite(&conn->rl, TLS_NULL_WITH_NULL_NULL);
00503         tlsv1_record_change_write_cipher(&conn->rl);
00504         tlsv1_record_change_read_cipher(&conn->rl);
00505 
00506         conn->certificate_requested = 0;
00507         crypto_public_key_free(conn->server_rsa_key);
00508         conn->server_rsa_key = NULL;
00509         conn->session_resumed = 0;
00510 
00511         return 0;
00512 }
00513 
00514 
00521 int tlsv1_client_resumed(struct tlsv1_client *conn)
00522 {
00523         return !!conn->session_resumed;
00524 }
00525 
00526 
00536 int tlsv1_client_hello_ext(struct tlsv1_client *conn, int ext_type,
00537                            const u8 *data, size_t data_len)
00538 {
00539         u8 *pos;
00540 
00541         conn->session_ticket_included = 0;
00542         os_free(conn->client_hello_ext);
00543         conn->client_hello_ext = NULL;
00544         conn->client_hello_ext_len = 0;
00545 
00546         if (data == NULL || data_len == 0)
00547                 return 0;
00548 
00549         pos = conn->client_hello_ext = os_malloc(6 + data_len);
00550         if (pos == NULL)
00551                 return -1;
00552 
00553         WPA_PUT_BE16(pos, 4 + data_len);
00554         pos += 2;
00555         WPA_PUT_BE16(pos, ext_type);
00556         pos += 2;
00557         WPA_PUT_BE16(pos, data_len);
00558         pos += 2;
00559         os_memcpy(pos, data, data_len);
00560         conn->client_hello_ext_len = 6 + data_len;
00561 
00562         if (ext_type == TLS_EXT_PAC_OPAQUE) {
00563                 conn->session_ticket_included = 1;
00564                 wpa_printf(MSG_DEBUG, "TLSv1: Using session ticket");
00565         }
00566 
00567         return 0;
00568 }
00569 
00570 
00578 int tlsv1_client_get_keys(struct tlsv1_client *conn, struct tls_keys *keys)
00579 {
00580         os_memset(keys, 0, sizeof(*keys));
00581         if (conn->state == CLIENT_HELLO)
00582                 return -1;
00583 
00584         keys->client_random = conn->client_random;
00585         keys->client_random_len = TLS_RANDOM_LEN;
00586 
00587         if (conn->state != SERVER_HELLO) {
00588                 keys->server_random = conn->server_random;
00589                 keys->server_random_len = TLS_RANDOM_LEN;
00590                 keys->master_key = conn->master_secret;
00591                 keys->master_key_len = TLS_MASTER_SECRET_LEN;
00592         }
00593 
00594         return 0;
00595 }
00596 
00597 
00605 int tlsv1_client_get_keyblock_size(struct tlsv1_client *conn)
00606 {
00607         if (conn->state == CLIENT_HELLO || conn->state == SERVER_HELLO)
00608                 return -1;
00609 
00610         return 2 * (conn->rl.hash_size + conn->rl.key_material_len +
00611                     conn->rl.iv_size);
00612 }
00613 
00614 
00623 int tlsv1_client_set_cipher_list(struct tlsv1_client *conn, u8 *ciphers)
00624 {
00625 #ifdef EAP_FAST
00626         size_t count;
00627         u16 *suites;
00628 
00629         /* TODO: implement proper configuration of cipher suites */
00630         if (ciphers[0] == TLS_CIPHER_ANON_DH_AES128_SHA) {
00631                 count = 0;
00632                 suites = conn->cipher_suites;
00633 #ifndef CONFIG_CRYPTO_INTERNAL
00634                 suites[count++] = TLS_DH_anon_WITH_AES_256_CBC_SHA;
00635 #endif /* CONFIG_CRYPTO_INTERNAL */
00636                 suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
00637                 suites[count++] = TLS_DH_anon_WITH_3DES_EDE_CBC_SHA;
00638                 suites[count++] = TLS_DH_anon_WITH_RC4_128_MD5;
00639                 suites[count++] = TLS_DH_anon_WITH_DES_CBC_SHA;
00640 
00641                 /*
00642                  * Cisco AP (at least 350 and 1200 series) local authentication
00643                  * server does not know how to search cipher suites from the
00644                  * list and seem to require that the last entry in the list is
00645                  * the one that it wants to use. However, TLS specification
00646                  * requires the list to be in the client preference order. As a
00647                  * workaround, add anon-DH AES-128-SHA1 again at the end of the
00648                  * list to allow the Cisco code to find it.
00649                  */
00650                 suites[count++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
00651                 conn->num_cipher_suites = count;
00652         }
00653 
00654         return 0;
00655 #else /* EAP_FAST */
00656         return -1;
00657 #endif /* EAP_FAST */
00658 }
00659 
00660 
00672 int tlsv1_client_set_cred(struct tlsv1_client *conn,
00673                           struct tlsv1_credentials *cred)
00674 {
00675         tlsv1_cred_free(conn->cred);
00676         conn->cred = cred;
00677         return 0;
00678 }
00679 
00680 
00681 void tlsv1_client_set_session_ticket_cb(struct tlsv1_client *conn,
00682                                         tlsv1_client_session_ticket_cb cb,
00683                                         void *ctx)
00684 {
00685         wpa_printf(MSG_DEBUG, "TLSv1: SessionTicket callback set %p (ctx %p)",
00686                    cb, ctx);
00687         conn->session_ticket_cb = cb;
00688         conn->session_ticket_cb_ctx = ctx;
00689 }
00690 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:53 2009 for hostapd by  doxygen 1.6.1