chacha20poly1305
[openssl.git] / ssl / s3_lib.c
index c026c7b..25b652c 100644 (file)
@@ -161,6 +161,11 @@ const char ssl3_version_str[]="SSLv3" OPENSSL_VERSION_PTEXT;
 
 #define SSL3_NUM_CIPHERS       (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
 
+/* FIXED_NONCE_LEN is a macro that results in the correct value to set the
+ * fixed nonce length in SSL_CIPHER.algorithms2. It's the inverse of
+ * SSL_CIPHER_AEAD_FIXED_NONCE_LEN. */
+#define FIXED_NONCE_LEN(x) ((x/2)<<24)
+
 /* list of available SSLv3 ciphers (sorted by id) */
 OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
 
@@ -1831,7 +1836,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -1847,7 +1854,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -1863,7 +1872,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -1879,7 +1890,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -1895,7 +1908,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -1911,7 +1926,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -1927,7 +1944,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -1943,7 +1962,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -1959,7 +1980,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -1975,7 +1998,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -1991,7 +2016,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -2007,7 +2034,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -2720,7 +2749,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -2736,7 +2767,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -2752,7 +2785,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -2768,7 +2803,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -2784,7 +2821,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -2800,7 +2839,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -2816,7 +2857,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        128,
        128,
        },
@@ -2832,7 +2875,9 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        SSL_AEAD,
        SSL_TLSV1_2,
        SSL_NOT_EXP|SSL_HIGH|SSL_FIPS,
-       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384,
+       SSL_HANDSHAKE_MAC_SHA384|TLS1_PRF_SHA384|SSL_CIPHER_ALGORITHM2_AEAD|
+               FIXED_NONCE_LEN(4)|
+               SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_INCLUDED_IN_RECORD,
        256,
        256,
        },
@@ -2900,6 +2945,53 @@ OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
        },
 #endif
 
+#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
+       {
+       1,
+       TLS1_TXT_ECDHE_RSA_WITH_CHACHA20_POLY1305,
+       TLS1_CK_ECDHE_RSA_CHACHA20_POLY1305,
+       SSL_kEECDH,
+       SSL_aRSA,
+       SSL_CHACHA20POLY1305,
+       SSL_AEAD,
+       SSL_TLSV1_2,
+       SSL_NOT_EXP|SSL_HIGH,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|FIXED_NONCE_LEN(0),
+       256,
+       0,
+       },
+
+       {
+       1,
+       TLS1_TXT_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
+       TLS1_CK_ECDHE_ECDSA_CHACHA20_POLY1305,
+       SSL_kEECDH,
+       SSL_aECDSA,
+       SSL_CHACHA20POLY1305,
+       SSL_AEAD,
+       SSL_TLSV1_2,
+       SSL_NOT_EXP|SSL_HIGH,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|FIXED_NONCE_LEN(0),
+       256,
+       0,
+       },
+
+       {
+       1,
+       TLS1_TXT_DHE_RSA_WITH_CHACHA20_POLY1305,
+       TLS1_CK_DHE_RSA_CHACHA20_POLY1305,
+       SSL_kEDH,
+       SSL_aRSA,
+       SSL_CHACHA20POLY1305,
+       SSL_AEAD,
+       SSL_TLSV1_2,
+       SSL_NOT_EXP|SSL_HIGH,
+       SSL_HANDSHAKE_MAC_SHA256|TLS1_PRF_SHA256|SSL_CIPHER_ALGORITHM2_AEAD|FIXED_NONCE_LEN(0),
+       256,
+       0,
+       },
+#endif
+
 /* end of list */
        };
 
@@ -3020,6 +3112,11 @@ void ssl3_free(SSL *s)
                BIO_free(s->s3->handshake_buffer);
        }
        if (s->s3->handshake_dgst) ssl3_free_digest_list(s);
+#ifndef OPENSSL_NO_TLSEXT
+       if (s->s3->alpn_selected)
+               OPENSSL_free(s->s3->alpn_selected);
+#endif
+
 #ifndef OPENSSL_NO_SRP
        SSL_SRP_CTX_free(s);
 #endif
@@ -3101,6 +3198,14 @@ void ssl3_clear(SSL *s)
        if (s->s3->handshake_dgst) {
                ssl3_free_digest_list(s);
        }       
+
+#if !defined(OPENSSL_NO_TLSEXT)
+       if (s->s3->alpn_selected)
+               {
+               free(s->s3->alpn_selected);
+               s->s3->alpn_selected = NULL;
+               }
+#endif
        memset(s->s3,0,sizeof *s->s3);
        s->s3->rbuf.buf = rp;
        s->s3->wbuf.buf = wp;
@@ -3383,7 +3488,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
 
 #ifndef OPENSSL_NO_HEARTBEATS
        case SSL_CTRL_TLS_EXT_SEND_HEARTBEAT:
-               if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
+               if (SSL_IS_DTLS(s))
                        ret = dtls1_heartbeat(s);
                else
                        ret = tls1_heartbeat(s);
@@ -3505,7 +3610,7 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
                return ssl_cert_set_cert_store(s->cert, parg, 1, larg);
 
        case SSL_CTRL_GET_PEER_SIGNATURE_NID:
-               if (TLS1_get_version(s) >= TLS1_2_VERSION)
+               if (SSL_USE_SIGALGS(s))
                        {
                        if (s->session && s->session->sess_cert)
                                {
@@ -4085,9 +4190,9 @@ SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *clnt,
                {
                c=sk_SSL_CIPHER_value(prio,i);
 
-               /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
+               /* Skip TLS v1.2 only ciphersuites if not supported */
                if ((c->algorithm_ssl & SSL_TLSV1_2) && 
-                       (TLS1_get_version(s) < TLS1_2_VERSION))
+                       !SSL_USE_TLS1_2_CIPHERS(s))
                        continue;
 
                ssl_set_cert_masks(cert,c);
@@ -4474,14 +4579,14 @@ need to go to SSL_ST_ACCEPT.
                }
        return(ret);
        }
-/* If we are using TLS v1.2 or later and default SHA1+MD5 algorithms switch
- * to new SHA256 PRF and handshake macs
+/* If we are using default SHA1+MD5 algorithms switch to new SHA256 PRF
+ * and handshake macs if required.
  */
 long ssl_get_algorithm2(SSL *s)
        {
        long alg2 = s->s3->tmp.new_cipher->algorithm2;
-       if (TLS1_get_version(s) >= TLS1_2_VERSION &&
-           alg2 == (SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF))
+       if (s->method->ssl3_enc->enc_flags & SSL_ENC_FLAG_SHA256_PRF
+           && alg2 == (SSL_HANDSHAKE_MAC_DEFAULT|TLS1_PRF))
                return SSL_HANDSHAKE_MAC_SHA256 | TLS1_PRF_SHA256;
        return alg2;
        }