Add initial state machine rewrite code
[openssl.git] / ssl / s3_both.c
index 32193c3460d5b220ad4e1ef841af3ec2e2c6b99b..d8a2da1362f37632664d471685186b09bcc241f5 100644 (file)
@@ -240,12 +240,23 @@ int ssl3_get_change_cipher_spec(SSL *s, int a, int b)
 
     /*
      * 'Change Cipher Spec' is just a single byte, which should already have
-     * been consumed by ssl_get_message() so there should be no bytes left
+     * been consumed by ssl_get_message() so there should be no bytes left,
+     * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
      */
-    if (n != 0) {
-        al = SSL_AD_ILLEGAL_PARAMETER;
-        SSLerr(SSL_F_SSL3_GET_CHANGE_CIPHER_SPEC, SSL_R_BAD_CHANGE_CIPHER_SPEC);
-        goto f_err;
+    if (SSL_IS_DTLS(s)) {
+        if ((s->version == DTLS1_BAD_VER && n != DTLS1_CCS_HEADER_LENGTH + 1)
+                    || (s->version != DTLS1_BAD_VER
+                        && n != DTLS1_CCS_HEADER_LENGTH - 1)) {
+                al = SSL_AD_ILLEGAL_PARAMETER;
+                SSLerr(SSL_F_SSL3_GET_CHANGE_CIPHER_SPEC, SSL_R_BAD_CHANGE_CIPHER_SPEC);
+                goto f_err;
+        }
+    } else {
+        if (n != 0) {
+            al = SSL_AD_ILLEGAL_PARAMETER;
+            SSLerr(SSL_F_SSL3_GET_CHANGE_CIPHER_SPEC, SSL_R_BAD_CHANGE_CIPHER_SPEC);
+            goto f_err;
+        }
     }
 
     /* Check we have a cipher to change to */
@@ -262,6 +273,22 @@ int ssl3_get_change_cipher_spec(SSL *s, int a, int b)
         goto f_err;
     }
 
+    if (SSL_IS_DTLS(s)) {
+        dtls1_reset_seq_numbers(s, SSL3_CC_READ);
+
+        if (s->version == DTLS1_BAD_VER)
+            s->d1->handshake_read_seq++;
+
+#ifndef OPENSSL_NO_SCTP
+        /*
+         * Remember that a CCS has been received, so that an old key of
+         * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
+         * SCTP is used
+         */
+        BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
+#endif
+    }
+
     return 1;
  f_err:
     ssl3_send_alert(s, SSL3_AL_FATAL, al);
@@ -384,9 +411,8 @@ unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)
 long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
 {
     unsigned char *p;
-    unsigned long l;
     long n;
-    int i, al, recvd_type;
+    int al, mtin;
 
     if (s->s3->tmp.reuse_message) {
         s->s3->tmp.reuse_message = 0;
@@ -405,122 +431,169 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
     p = (unsigned char *)s->init_buf->data;
 
     if (s->state == st1) {
-        /* s->init_num < SSL3_HM_HEADER_LENGTH */
-        int skip_message;
-
-        do {
-            while (s->init_num < SSL3_HM_HEADER_LENGTH) {
-                i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
-                    &p[s->init_num], SSL3_HM_HEADER_LENGTH - s->init_num, 0);
-                if (i <= 0) {
-                    s->rwstate = SSL_READING;
-                    *ok = 0;
-                    return i;
-                }
-                if (s->init_num == 0
-                        && recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC
-                        && (mt < 0 || mt == SSL3_MT_CHANGE_CIPHER_SPEC)) {
-                    if (*p != SSL3_MT_CCS) {
-                        al = SSL_AD_UNEXPECTED_MESSAGE;
-                        SSLerr(SSL_F_SSL3_GET_MESSAGE,
-                               SSL_R_UNEXPECTED_MESSAGE);
-                        goto f_err;
-                    }
-                    s->init_num = i - 1;
-                    s->init_msg = p + 1;
-                    s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
-                    s->s3->tmp.message_size = i - 1;
-                    s->state = stn;
-                    *ok = 1;
-                    if (s->msg_callback)
-                        s->msg_callback(0, s->version,
-                                        SSL3_RT_CHANGE_CIPHER_SPEC, p, 1, s,
-                                        s->msg_callback_arg);
-                    return i - 1;
-                } else if (recvd_type != SSL3_RT_HANDSHAKE) {
-                    al = SSL_AD_UNEXPECTED_MESSAGE;
-                    SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
-                    goto f_err;
-                }
-                s->init_num += i;
+        if (tls_get_message_header(s, &mtin) == 0) {
+            /* Could be NBIO */
+            *ok = 0;
+            return -1;
+        }
+        s->state = stn;
+        if (s->init_num == 0
+                && mtin == SSL3_MT_CHANGE_CIPHER_SPEC
+                && (mt < 0 || mt == SSL3_MT_CHANGE_CIPHER_SPEC)) {
+            if (*p != SSL3_MT_CCS) {
+                al = SSL_AD_UNEXPECTED_MESSAGE;
+                SSLerr(SSL_F_SSL3_GET_MESSAGE,
+                       SSL_R_UNEXPECTED_MESSAGE);
+                goto f_err;
             }
-
-            skip_message = 0;
-            if (!s->server)
-                if (p[0] == SSL3_MT_HELLO_REQUEST)
-                    /*
-                     * The server may always send 'Hello Request' messages --
-                     * we are doing a handshake anyway now, so ignore them if
-                     * their format is correct. Does not count for 'Finished'
-                     * MAC.
-                     */
-                    if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
-                        s->init_num = 0;
-                        skip_message = 1;
-
-                        if (s->msg_callback)
-                            s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
-                                            p, SSL3_HM_HEADER_LENGTH, s,
-                                            s->msg_callback_arg);
-                    }
-        } while (skip_message);
-        /* s->init_num == SSL3_HM_HEADER_LENGTH */
-
-        if ((mt >= 0) && (*p != mt)) {
+            s->init_msg = p + 1;
+            s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
+            s->s3->tmp.message_size = s->init_num;
+            *ok = 1;
+            if (s->msg_callback)
+                s->msg_callback(0, s->version,
+                                SSL3_RT_CHANGE_CIPHER_SPEC, p, 1, s,
+                                s->msg_callback_arg);
+            return s->init_num;
+        }
+        if (s->s3->tmp.message_size > (unsigned long)max) {
+            al = SSL_AD_ILLEGAL_PARAMETER;
+            SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
+            goto f_err;
+        }
+        if ((mt >= 0) && (mtin != mt)) {
             al = SSL_AD_UNEXPECTED_MESSAGE;
             SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
             goto f_err;
         }
+    }
 
-        s->s3->tmp.message_type = *(p++);
-
-        if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
-            /*
-             * Only happens with SSLv3+ in an SSLv2 backward compatible
-             * ClientHello
-             */
-             /*
-              * Total message size is the remaining record bytes to read
-              * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
-              */
-            l = RECORD_LAYER_get_rrec_length(&s->rlayer)
-                + SSL3_HM_HEADER_LENGTH;
-            if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l)) {
-                SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB);
-                goto err;
-            }
-            s->s3->tmp.message_size = l;
-            s->state = stn;
+    /* next state (stn) */
+    if (tls_get_message_body(s, (unsigned long *)&n) == 0) {
+        *ok = 0;
+        return n;
+    }
 
-            s->init_msg = s->init_buf->data;
-            s->init_num = SSL3_HM_HEADER_LENGTH;
-        } else {
-            n2l3(p, l);
-            if (l > (unsigned long)max) {
-                al = SSL_AD_ILLEGAL_PARAMETER;
-                SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
-                goto f_err;
+    *ok = 1;
+    return n;
+ f_err:
+    ssl3_send_alert(s, SSL3_AL_FATAL, al);
+    statem_set_error(s);
+    *ok = 0;
+    return 0;
+}
+
+int tls_get_message_header(SSL *s, int *mt)
+{
+    /* s->init_num < SSL3_HM_HEADER_LENGTH */
+    int skip_message, i, recvd_type, al;
+    unsigned char *p;
+    unsigned long l;
+
+    p = (unsigned char *)s->init_buf->data;
+
+    do {
+        while (s->init_num < SSL3_HM_HEADER_LENGTH) {
+            i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
+                &p[s->init_num], SSL3_HM_HEADER_LENGTH - s->init_num, 0);
+            if (i <= 0) {
+                s->rwstate = SSL_READING;
+                return 0;
             }
-            /* BUF_MEM_grow takes an 'int' parameter */
-            if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
-                al = SSL_AD_ILLEGAL_PARAMETER;
-                SSLerr(SSL_F_SSL3_GET_MESSAGE, SSL_R_EXCESSIVE_MESSAGE_SIZE);
+            if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
+                s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
+                s->init_num = i - 1;
+                s->s3->tmp.message_size = i;
+                return 1;
+            } else if (recvd_type != SSL3_RT_HANDSHAKE) {
+                al = SSL_AD_UNEXPECTED_MESSAGE;
+                SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
                 goto f_err;
             }
-            if (l && !BUF_MEM_grow_clean(s->init_buf,
-                                        (int)l + SSL3_HM_HEADER_LENGTH)) {
-                SSLerr(SSL_F_SSL3_GET_MESSAGE, ERR_R_BUF_LIB);
-                goto err;
-            }
-            s->s3->tmp.message_size = l;
-            s->state = stn;
+            s->init_num += i;
+        }
+
+        skip_message = 0;
+        if (!s->server)
+            if (p[0] == SSL3_MT_HELLO_REQUEST)
+                /*
+                 * The server may always send 'Hello Request' messages --
+                 * we are doing a handshake anyway now, so ignore them if
+                 * their format is correct. Does not count for 'Finished'
+                 * MAC.
+                 */
+                if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
+                    s->init_num = 0;
+                    skip_message = 1;
+
+                    if (s->msg_callback)
+                        s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
+                                        p, SSL3_HM_HEADER_LENGTH, s,
+                                        s->msg_callback_arg);
+                }
+    } while (skip_message);
+    /* s->init_num == SSL3_HM_HEADER_LENGTH */
+
+    *mt = *p;
+    s->s3->tmp.message_type = *(p++);
+
+    if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
+        /*
+         * Only happens with SSLv3+ in an SSLv2 backward compatible
+         * ClientHello
+         */
+         /*
+          * Total message size is the remaining record bytes to read
+          * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
+          */
+        l = RECORD_LAYER_get_rrec_length(&s->rlayer)
+            + SSL3_HM_HEADER_LENGTH;
+        if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l)) {
+            SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
+            goto err;
+        }
+        s->s3->tmp.message_size = l;
 
-            s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
-            s->init_num = 0;
+        s->init_msg = s->init_buf->data;
+        s->init_num = SSL3_HM_HEADER_LENGTH;
+    } else {
+        n2l3(p, l);
+        /* BUF_MEM_grow takes an 'int' parameter */
+        if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
+            al = SSL_AD_ILLEGAL_PARAMETER;
+            SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
+            goto f_err;
+        }
+        if (l && !BUF_MEM_grow_clean(s->init_buf,
+                                    (int)l + SSL3_HM_HEADER_LENGTH)) {
+            SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
+            goto err;
         }
+        s->s3->tmp.message_size = l;
+
+        s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
+        s->init_num = 0;
+    }
+
+    return 1;
+ f_err:
+    ssl3_send_alert(s, SSL3_AL_FATAL, al);
+ err:
+    return 0;
+}
+
+int tls_get_message_body(SSL *s, unsigned long *len)
+{
+    long n;
+    unsigned char *p;
+    int i;
+
+    if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
+        /* We've already read everything in */
+        *len = (unsigned long)s->init_num;
+        return 1;
     }
 
-    /* next state (stn) */
     p = s->init_msg;
     n = s->s3->tmp.message_size - s->init_num;
     while (n > 0) {
@@ -528,8 +601,8 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                                       &p[s->init_num], n, 0);
         if (i <= 0) {
             s->rwstate = SSL_READING;
-            *ok = 0;
-            return i;
+            *len = 0;
+            return 0;
         }
         s->init_num += i;
         n -= i;
@@ -559,13 +632,18 @@ long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)
                             s->msg_callback_arg);
     }
 
-    *ok = 1;
-    return s->init_num;
- f_err:
-    ssl3_send_alert(s, SSL3_AL_FATAL, al);
- err:
-    *ok = 0;
-    return (-1);
+    /*
+     * init_num should never be negative...should probably be declared
+     * unsigned
+     */
+    if (s->init_num < 0) {
+        SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_INTERNAL_ERROR);
+        ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
+        *len = 0;
+        return 0;
+    }
+    *len = (unsigned long)s->init_num;
+    return 1;
 }
 
 int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
@@ -591,9 +669,7 @@ int ssl_cert_type(X509 *x, EVP_PKEY *pkey)
         ret = SSL_PKEY_ECC;
     }
 #endif
-    else if (i == NID_id_GostR3410_94 || i == NID_id_GostR3410_94_cc) {
-        ret = SSL_PKEY_GOST94;
-    } else if (i == NID_id_GostR3410_2001 || i == NID_id_GostR3410_2001_cc) {
+    else if (i == NID_id_GostR3410_2001) {
         ret = SSL_PKEY_GOST01;
     } else if (x && (i == EVP_PKEY_DH || i == EVP_PKEY_DHX)) {
         /*