tlsv1_common.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "x509v3.h"
00020 #include "tlsv1_common.h"
00021 
00022 
00023 /*
00024  * TODO:
00025  * RFC 2246 Section 9: Mandatory to implement TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA
00026  * Add support for commonly used cipher suites; don't bother with exportable
00027  * suites.
00028  */ 
00029 
00030 static const struct tls_cipher_suite tls_cipher_suites[] = {
00031         { TLS_NULL_WITH_NULL_NULL, TLS_KEY_X_NULL, TLS_CIPHER_NULL,
00032           TLS_HASH_NULL },
00033         { TLS_RSA_WITH_RC4_128_MD5, TLS_KEY_X_RSA, TLS_CIPHER_RC4_128,
00034           TLS_HASH_MD5 },
00035         { TLS_RSA_WITH_RC4_128_SHA, TLS_KEY_X_RSA, TLS_CIPHER_RC4_128,
00036           TLS_HASH_SHA },
00037         { TLS_RSA_WITH_DES_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_DES_CBC,
00038           TLS_HASH_SHA },
00039         { TLS_RSA_WITH_3DES_EDE_CBC_SHA, TLS_KEY_X_RSA,
00040           TLS_CIPHER_3DES_EDE_CBC, TLS_HASH_SHA },
00041         { TLS_DH_anon_WITH_RC4_128_MD5, TLS_KEY_X_DH_anon,
00042           TLS_CIPHER_RC4_128, TLS_HASH_MD5 },
00043         { TLS_DH_anon_WITH_DES_CBC_SHA, TLS_KEY_X_DH_anon,
00044           TLS_CIPHER_DES_CBC, TLS_HASH_SHA },
00045         { TLS_DH_anon_WITH_3DES_EDE_CBC_SHA, TLS_KEY_X_DH_anon,
00046           TLS_CIPHER_3DES_EDE_CBC, TLS_HASH_SHA },
00047         { TLS_RSA_WITH_AES_128_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_AES_128_CBC,
00048           TLS_HASH_SHA },
00049         { TLS_DH_anon_WITH_AES_128_CBC_SHA, TLS_KEY_X_DH_anon,
00050           TLS_CIPHER_AES_128_CBC, TLS_HASH_SHA },
00051         { TLS_RSA_WITH_AES_256_CBC_SHA, TLS_KEY_X_RSA, TLS_CIPHER_AES_256_CBC,
00052           TLS_HASH_SHA },
00053         { TLS_DH_anon_WITH_AES_256_CBC_SHA, TLS_KEY_X_DH_anon,
00054           TLS_CIPHER_AES_256_CBC, TLS_HASH_SHA }
00055 };
00056 
00057 #define NUM_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
00058 #define NUM_TLS_CIPHER_SUITES NUM_ELEMS(tls_cipher_suites)
00059 
00060 
00061 static const struct tls_cipher_data tls_ciphers[] = {
00062         { TLS_CIPHER_NULL,         TLS_CIPHER_STREAM,  0,  0,  0,
00063           CRYPTO_CIPHER_NULL },
00064         { TLS_CIPHER_IDEA_CBC,     TLS_CIPHER_BLOCK,  16, 16,  8,
00065           CRYPTO_CIPHER_NULL },
00066         { TLS_CIPHER_RC2_CBC_40,   TLS_CIPHER_BLOCK,   5, 16,  0,
00067           CRYPTO_CIPHER_ALG_RC2 },
00068         { TLS_CIPHER_RC4_40,       TLS_CIPHER_STREAM,  5, 16,  0,
00069           CRYPTO_CIPHER_ALG_RC4 },
00070         { TLS_CIPHER_RC4_128,      TLS_CIPHER_STREAM, 16, 16,  0,
00071           CRYPTO_CIPHER_ALG_RC4 },
00072         { TLS_CIPHER_DES40_CBC,    TLS_CIPHER_BLOCK,   5,  8,  8,
00073           CRYPTO_CIPHER_ALG_DES },
00074         { TLS_CIPHER_DES_CBC,      TLS_CIPHER_BLOCK,   8,  8,  8,
00075           CRYPTO_CIPHER_ALG_DES },
00076         { TLS_CIPHER_3DES_EDE_CBC, TLS_CIPHER_BLOCK,  24, 24,  8,
00077           CRYPTO_CIPHER_ALG_3DES },
00078         { TLS_CIPHER_AES_128_CBC,  TLS_CIPHER_BLOCK,  16, 16, 16,
00079           CRYPTO_CIPHER_ALG_AES },
00080         { TLS_CIPHER_AES_256_CBC,  TLS_CIPHER_BLOCK,  32, 32, 16,
00081           CRYPTO_CIPHER_ALG_AES }
00082 };
00083 
00084 #define NUM_TLS_CIPHER_DATA NUM_ELEMS(tls_ciphers)
00085 
00086 
00093 const struct tls_cipher_suite * tls_get_cipher_suite(u16 suite)
00094 {
00095         size_t i;
00096         for (i = 0; i < NUM_TLS_CIPHER_SUITES; i++)
00097                 if (tls_cipher_suites[i].suite == suite)
00098                         return &tls_cipher_suites[i];
00099         return NULL;
00100 }
00101 
00102 
00103 const struct tls_cipher_data * tls_get_cipher_data(tls_cipher cipher)
00104 {
00105         size_t i;
00106         for (i = 0; i < NUM_TLS_CIPHER_DATA; i++)
00107                 if (tls_ciphers[i].cipher == cipher)
00108                         return &tls_ciphers[i];
00109         return NULL;
00110 }
00111 
00112 
00113 int tls_server_key_exchange_allowed(tls_cipher cipher)
00114 {
00115         const struct tls_cipher_suite *suite;
00116 
00117         /* RFC 2246, Section 7.4.3 */
00118         suite = tls_get_cipher_suite(cipher);
00119         if (suite == NULL)
00120                 return 0;
00121 
00122         switch (suite->key_exchange) {
00123         case TLS_KEY_X_DHE_DSS:
00124         case TLS_KEY_X_DHE_DSS_EXPORT:
00125         case TLS_KEY_X_DHE_RSA:
00126         case TLS_KEY_X_DHE_RSA_EXPORT:
00127         case TLS_KEY_X_DH_anon_EXPORT:
00128         case TLS_KEY_X_DH_anon:
00129                 return 1;
00130         case TLS_KEY_X_RSA_EXPORT:
00131                 return 1 /* FIX: public key len > 512 bits */;
00132         default:
00133                 return 0;
00134         }
00135 }
00136 
00137 
00150 int tls_parse_cert(const u8 *buf, size_t len, struct crypto_public_key **pk)
00151 {
00152         struct x509_certificate *cert;
00153 
00154         wpa_hexdump(MSG_MSGDUMP, "TLSv1: Parse ASN.1 DER certificate",
00155                     buf, len);
00156 
00157         *pk = crypto_public_key_from_cert(buf, len);
00158         if (*pk)
00159                 return 0;
00160 
00161         cert = x509_certificate_parse(buf, len);
00162         if (cert == NULL) {
00163                 wpa_printf(MSG_DEBUG, "TLSv1: Failed to parse X.509 "
00164                            "certificate");
00165                 return -1;
00166         }
00167 
00168         /* TODO
00169          * verify key usage (must allow encryption)
00170          *
00171          * All certificate profiles, key and cryptographic formats are
00172          * defined by the IETF PKIX working group [PKIX]. When a key
00173          * usage extension is present, the digitalSignature bit must be
00174          * set for the key to be eligible for signing, as described
00175          * above, and the keyEncipherment bit must be present to allow
00176          * encryption, as described above. The keyAgreement bit must be
00177          * set on Diffie-Hellman certificates. (PKIX: RFC 3280)
00178          */
00179 
00180         *pk = crypto_public_key_import(cert->public_key, cert->public_key_len);
00181         x509_certificate_free(cert);
00182 
00183         if (*pk == NULL) {
00184                 wpa_printf(MSG_ERROR, "TLSv1: Failed to import "
00185                            "server public key");
00186                 return -1;
00187         }
00188 
00189         return 0;
00190 }
00191 
00192 
00193 int tls_verify_hash_init(struct tls_verify_hash *verify)
00194 {
00195         tls_verify_hash_free(verify);
00196         verify->md5_client = crypto_hash_init(CRYPTO_HASH_ALG_MD5, NULL, 0);
00197         verify->md5_server = crypto_hash_init(CRYPTO_HASH_ALG_MD5, NULL, 0);
00198         verify->md5_cert = crypto_hash_init(CRYPTO_HASH_ALG_MD5, NULL, 0);
00199         verify->sha1_client = crypto_hash_init(CRYPTO_HASH_ALG_SHA1, NULL, 0);
00200         verify->sha1_server = crypto_hash_init(CRYPTO_HASH_ALG_SHA1, NULL, 0);
00201         verify->sha1_cert = crypto_hash_init(CRYPTO_HASH_ALG_SHA1, NULL, 0);
00202         if (verify->md5_client == NULL || verify->md5_server == NULL ||
00203             verify->md5_cert == NULL || verify->sha1_client == NULL ||
00204             verify->sha1_server == NULL || verify->sha1_cert == NULL) {
00205                 tls_verify_hash_free(verify);
00206                 return -1;
00207         }
00208         return 0;
00209 }
00210 
00211 
00212 void tls_verify_hash_add(struct tls_verify_hash *verify, const u8 *buf,
00213                          size_t len)
00214 {
00215         if (verify->md5_client && verify->sha1_client) {
00216                 crypto_hash_update(verify->md5_client, buf, len);
00217                 crypto_hash_update(verify->sha1_client, buf, len);
00218         }
00219         if (verify->md5_server && verify->sha1_server) {
00220                 crypto_hash_update(verify->md5_server, buf, len);
00221                 crypto_hash_update(verify->sha1_server, buf, len);
00222         }
00223         if (verify->md5_cert && verify->sha1_cert) {
00224                 crypto_hash_update(verify->md5_cert, buf, len);
00225                 crypto_hash_update(verify->sha1_cert, buf, len);
00226         }
00227 }
00228 
00229 
00230 void tls_verify_hash_free(struct tls_verify_hash *verify)
00231 {
00232         crypto_hash_finish(verify->md5_client, NULL, NULL);
00233         crypto_hash_finish(verify->md5_server, NULL, NULL);
00234         crypto_hash_finish(verify->md5_cert, NULL, NULL);
00235         crypto_hash_finish(verify->sha1_client, NULL, NULL);
00236         crypto_hash_finish(verify->sha1_server, NULL, NULL);
00237         crypto_hash_finish(verify->sha1_cert, NULL, NULL);
00238         verify->md5_client = NULL;
00239         verify->md5_server = NULL;
00240         verify->md5_cert = NULL;
00241         verify->sha1_client = NULL;
00242         verify->sha1_server = NULL;
00243         verify->sha1_cert = NULL;
00244 }
00245 
 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