Blame SOURCES/openssl-1.0.1e-fallback-scsv.patch

170643
diff -up openssl-1.0.1e/apps/s_client.c.fallback-scsv openssl-1.0.1e/apps/s_client.c
170643
--- openssl-1.0.1e/apps/s_client.c.fallback-scsv	2014-10-15 17:06:01.000000000 +0200
170643
+++ openssl-1.0.1e/apps/s_client.c	2014-10-15 17:07:36.392502320 +0200
170643
@@ -336,6 +336,7 @@ static void sc_usage(void)
170643
 	BIO_printf(bio_err," -tls1_1       - just use TLSv1.1\n");
170643
 	BIO_printf(bio_err," -tls1         - just use TLSv1\n");
170643
 	BIO_printf(bio_err," -dtls1        - just use DTLSv1\n");    
170643
+	BIO_printf(bio_err," -fallback_scsv - send TLS_FALLBACK_SCSV\n");
170643
 	BIO_printf(bio_err," -mtu          - set the link layer MTU\n");
170643
 	BIO_printf(bio_err," -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
170643
 	BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
170643
@@ -616,6 +617,7 @@ int MAIN(int argc, char **argv)
170643
 	char *sess_out = NULL;
170643
 	struct sockaddr peer;
170643
 	int peerlen = sizeof(peer);
170643
+	int fallback_scsv = 0;
170643
 	int enable_timeouts = 0 ;
170643
 	long socket_mtu = 0;
170643
 #ifndef OPENSSL_NO_JPAKE
170643
@@ -829,6 +831,10 @@ int MAIN(int argc, char **argv)
170643
 			socket_mtu = atol(*(++argv));
170643
 			}
170643
 #endif
170643
+		else if (strcmp(*argv,"-fallback_scsv") == 0)
170643
+			{
170643
+			fallback_scsv = 1;
170643
+			}
170643
 		else if (strcmp(*argv,"-bugs") == 0)
170643
 			bugs=1;
170643
 		else if	(strcmp(*argv,"-keyform") == 0)
170643
@@ -1240,6 +1246,10 @@ bad:
170643
 		SSL_set_session(con, sess);
170643
 		SSL_SESSION_free(sess);
170643
 		}
170643
+
170643
+	if (fallback_scsv)
170643
+		SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
170643
+
170643
 #ifndef OPENSSL_NO_TLSEXT
170643
 	if (servername != NULL)
170643
 		{
170643
diff -up openssl-1.0.1e/doc/apps/s_client.pod.fallback-scsv openssl-1.0.1e/doc/apps/s_client.pod
170643
--- openssl-1.0.1e/doc/apps/s_client.pod.fallback-scsv	2014-10-15 17:06:01.000000000 +0200
170643
+++ openssl-1.0.1e/doc/apps/s_client.pod	2014-10-15 17:08:17.354427053 +0200
170643
@@ -41,6 +41,7 @@ B<openssl> B<s_client>
170643
 [B<-no_tls1>]
170643
 [B<-no_tls1_1>]
170643
 [B<-no_tls1_2>]
170643
+[B<-fallback_scsv>]
170643
 [B<-bugs>]
170643
 [B<-cipher cipherlist>]
170643
 [B<-starttls protocol>]
170643
@@ -200,6 +201,10 @@ cannot handle this technique and will fa
170643
 work if TLS is turned off with the B<-no_tls> option others will only
170643
 support SSL v2 and may need the B<-ssl2> option.
170643
 
170643
+=item B<-fallback_scsv>
170643
+
170643
+Send TLS_FALLBACK_SCSV in the ClientHello.
170643
+
170643
 =item B<-bugs>
170643
 
170643
 there are several known bug in SSL and TLS implementations. Adding this
170643
diff -up openssl-1.0.1e/doc/ssl/SSL_CTX_set_mode.pod.fallback-scsv openssl-1.0.1e/doc/ssl/SSL_CTX_set_mode.pod
170643
--- openssl-1.0.1e/doc/ssl/SSL_CTX_set_mode.pod.fallback-scsv	2013-02-11 16:26:04.000000000 +0100
170643
+++ openssl-1.0.1e/doc/ssl/SSL_CTX_set_mode.pod	2014-10-15 17:09:57.577689637 +0200
170643
@@ -71,6 +71,12 @@ SSL_CTX->freelist_max_len, which default
170643
 save around 34k per idle SSL connection.
170643
 This flag has no effect on SSL v2 connections, or on DTLS connections.
170643
 
170643
+=item SSL_MODE_SEND_FALLBACK_SCSV
170643
+
170643
+Send TLS_FALLBACK_SCSV in the ClientHello.
170643
+To be set by applications that reconnect with a downgraded protocol
170643
+version; see draft-ietf-tls-downgrade-scsv-00 for details.
170643
+
170643
 =back
170643
 
170643
 =head1 RETURN VALUES
170643
diff -up openssl-1.0.1e/ssl/dtls1.h.fallback-scsv openssl-1.0.1e/ssl/dtls1.h
83c29f
--- openssl-1.0.1e/ssl/dtls1.h.fallback-scsv	2014-10-15 14:45:25.492913542 +0200
83c29f
+++ openssl-1.0.1e/ssl/dtls1.h	2014-10-15 14:45:25.596915890 +0200
170643
@@ -84,6 +84,8 @@ extern "C" {
170643
 #endif
170643
 
170643
 #define DTLS1_VERSION			0xFEFF
170643
+#define DTLS_MAX_VERSION		DTLS1_VERSION
170643
+
170643
 #define DTLS1_BAD_VER			0x0100
170643
 
170643
 #if 0
170643
@@ -284,4 +286,3 @@ typedef struct dtls1_record_data_st
170643
 }
170643
 #endif
170643
 #endif
170643
-
170643
diff -up openssl-1.0.1e/ssl/d1_lib.c.fallback-scsv openssl-1.0.1e/ssl/d1_lib.c
83c29f
--- openssl-1.0.1e/ssl/d1_lib.c.fallback-scsv	2014-10-15 14:45:25.539914603 +0200
83c29f
+++ openssl-1.0.1e/ssl/d1_lib.c	2014-10-15 14:45:25.596915890 +0200
170643
@@ -263,6 +263,16 @@ long dtls1_ctrl(SSL *s, int cmd, long la
170643
 	case DTLS_CTRL_LISTEN:
170643
 		ret = dtls1_listen(s, parg);
170643
 		break;
170643
+	case SSL_CTRL_CHECK_PROTO_VERSION:
170643
+		/* For library-internal use; checks that the current protocol
170643
+		 * is the highest enabled version (according to s->ctx->method,
170643
+		 * as version negotiation may have changed s->method). */
170643
+#if DTLS_MAX_VERSION != DTLS1_VERSION
170643
+#  error Code needs update for DTLS_method() support beyond DTLS1_VERSION.
170643
+#endif
170643
+		/* Just one protocol version is supported so far;
170643
+		 * fail closed if the version is not as expected. */
170643
+		return s->version == DTLS_MAX_VERSION;
170643
 
170643
 	default:
170643
 		ret = ssl3_ctrl(s, cmd, larg, parg);
170643
diff -up openssl-1.0.1e/ssl/ssl_err.c.fallback-scsv openssl-1.0.1e/ssl/ssl_err.c
170643
--- openssl-1.0.1e/ssl/ssl_err.c.fallback-scsv	2013-02-11 16:26:04.000000000 +0100
83c29f
+++ openssl-1.0.1e/ssl/ssl_err.c	2014-10-15 14:45:25.596915890 +0200
170643
@@ -382,6 +382,7 @@ static ERR_STRING_DATA SSL_str_reasons[]
170643
 {ERR_REASON(SSL_R_HTTPS_PROXY_REQUEST)   ,"https proxy request"},
170643
 {ERR_REASON(SSL_R_HTTP_REQUEST)          ,"http request"},
170643
 {ERR_REASON(SSL_R_ILLEGAL_PADDING)       ,"illegal padding"},
170643
+{ERR_REASON(SSL_R_INAPPROPRIATE_FALLBACK),"inappropriate fallback"},
170643
 {ERR_REASON(SSL_R_INCONSISTENT_COMPRESSION),"inconsistent compression"},
170643
 {ERR_REASON(SSL_R_INVALID_CHALLENGE_LENGTH),"invalid challenge length"},
170643
 {ERR_REASON(SSL_R_INVALID_COMMAND)       ,"invalid command"},
170643
@@ -528,6 +529,7 @@ static ERR_STRING_DATA SSL_str_reasons[]
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_DECRYPTION_FAILED),"tlsv1 alert decryption failed"},
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_DECRYPT_ERROR),"tlsv1 alert decrypt error"},
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION),"tlsv1 alert export restriction"},
170643
+{ERR_REASON(SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK),"tlsv1 alert inappropriate fallback"},
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY),"tlsv1 alert insufficient security"},
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_INTERNAL_ERROR),"tlsv1 alert internal error"},
170643
 {ERR_REASON(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION),"tlsv1 alert no renegotiation"},
170643
diff -up openssl-1.0.1e/ssl/ssl.h.fallback-scsv openssl-1.0.1e/ssl/ssl.h
83c29f
--- openssl-1.0.1e/ssl/ssl.h.fallback-scsv	2014-10-15 14:45:25.588915709 +0200
83c29f
+++ openssl-1.0.1e/ssl/ssl.h	2014-10-15 14:47:04.423146935 +0200
170643
@@ -638,6 +638,10 @@ struct ssl_session_st
170643
  * TLS only.)  "Released" buffers are put onto a free-list in the context
170643
  * or just freed (depending on the context's setting for freelist_max_len). */
170643
 #define SSL_MODE_RELEASE_BUFFERS 0x00000010L
170643
+/* Send TLS_FALLBACK_SCSV in the ClientHello.
170643
+ * To be set by applications that reconnect with a downgraded protocol
170643
+ * version; see draft-ietf-tls-downgrade-scsv-00 for details. */
170643
+#define SSL_MODE_SEND_FALLBACK_SCSV 0x00000080L
170643
 
170643
 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value,
170643
  * they cannot be used to clear bits. */
170643
@@ -1453,6 +1457,7 @@ DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION)
170643
 #define SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE
170643
 #define SSL_AD_BAD_CERTIFICATE_HASH_VALUE TLS1_AD_BAD_CERTIFICATE_HASH_VALUE
170643
 #define SSL_AD_UNKNOWN_PSK_IDENTITY     TLS1_AD_UNKNOWN_PSK_IDENTITY /* fatal */
170643
+#define SSL_AD_INAPPROPRIATE_FALLBACK	TLS1_AD_INAPPROPRIATE_FALLBACK /* fatal */
170643
 
170643
 #define SSL_ERROR_NONE			0
170643
 #define SSL_ERROR_SSL			1
83c29f
@@ -1566,6 +1571,8 @@ DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION)
83c29f
 #define SSL_CTRL_SET_ECDH_AUTO			94
170643
 #define SSL_CTRL_GET_SERVER_TMP_KEY		109
170643
 
170643
+#define SSL_CTRL_CHECK_PROTO_VERSION		119
170643
+
170643
 #define DTLSv1_get_timeout(ssl, arg) \
170643
 	SSL_ctrl(ssl,DTLS_CTRL_GET_TIMEOUT,0, (void *)arg)
170643
 #define DTLSv1_handle_timeout(ssl) \
83c29f
@@ -2304,6 +2311,7 @@ void ERR_load_SSL_strings(void);
170643
 #define SSL_R_HTTPS_PROXY_REQUEST			 155
170643
 #define SSL_R_HTTP_REQUEST				 156
170643
 #define SSL_R_ILLEGAL_PADDING				 283
170643
+#define SSL_R_INAPPROPRIATE_FALLBACK			 373
170643
 #define SSL_R_INCONSISTENT_COMPRESSION			 340
170643
 #define SSL_R_INVALID_CHALLENGE_LENGTH			 158
170643
 #define SSL_R_INVALID_COMMAND				 280
83c29f
@@ -2450,6 +2458,7 @@ void ERR_load_SSL_strings(void);
170643
 #define SSL_R_TLSV1_ALERT_DECRYPTION_FAILED		 1021
170643
 #define SSL_R_TLSV1_ALERT_DECRYPT_ERROR			 1051
170643
 #define SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION		 1060
170643
+#define SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK	 1086
170643
 #define SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY		 1071
170643
 #define SSL_R_TLSV1_ALERT_INTERNAL_ERROR		 1080
170643
 #define SSL_R_TLSV1_ALERT_NO_RENEGOTIATION		 1100
170643
diff -up openssl-1.0.1e/ssl/ssl_lib.c.fallback-scsv openssl-1.0.1e/ssl/ssl_lib.c
83c29f
--- openssl-1.0.1e/ssl/ssl_lib.c.fallback-scsv	2014-10-15 14:45:25.589915731 +0200
83c29f
+++ openssl-1.0.1e/ssl/ssl_lib.c	2014-10-15 14:45:25.597915912 +0200
170643
@@ -1383,6 +1383,8 @@ int ssl_cipher_list_to_bytes(SSL *s,STAC
170643
 
170643
 	if (sk == NULL) return(0);
170643
 	q=p;
170643
+	if (put_cb == NULL)
170643
+		put_cb = s->method->put_cipher_by_char;
170643
 
170643
 	for (i=0; i
170643
 		{
170643
@@ -1402,24 +1404,36 @@ int ssl_cipher_list_to_bytes(SSL *s,STAC
170643
 		    s->psk_client_callback == NULL)
170643
 			continue;
170643
 #endif /* OPENSSL_NO_PSK */
170643
-		j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
170643
+		j = put_cb(c,p);
170643
 		p+=j;
170643
 		}
170643
-	/* If p == q, no ciphers and caller indicates an error. Otherwise
170643
-	 * add SCSV if not renegotiating.
170643
-	 */
170643
-	if (p != q && !s->renegotiate)
170643
+	/* If p == q, no ciphers; caller indicates an error.
170643
+	 * Otherwise, add applicable SCSVs. */
170643
+	if (p != q)
170643
 		{
170643
-		static SSL_CIPHER scsv =
170643
+		if (!s->renegotiate)
170643
 			{
170643
-			0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
170643
-			};
170643
-		j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
170643
-		p+=j;
170643
+			static SSL_CIPHER scsv =
170643
+				{
170643
+				0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
170643
+				};
170643
+			j = put_cb(&scsv,p);
170643
+			p+=j;
170643
 #ifdef OPENSSL_RI_DEBUG
170643
-		fprintf(stderr, "SCSV sent by client\n");
170643
+			fprintf(stderr, "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
170643
 #endif
170643
-		}
170643
+			}
170643
+
170643
+		if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
170643
+			{
170643
+			static SSL_CIPHER scsv =
170643
+				{
170643
+				0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
170643
+				};
170643
+			j = put_cb(&scsv,p);
170643
+			p+=j;
170643
+			}
170643
+ 		}
170643
 
170643
 	return(p-q);
170643
 	}
170643
@@ -1430,11 +1444,12 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_ciphe
170643
 	const SSL_CIPHER *c;
170643
 	STACK_OF(SSL_CIPHER) *sk;
170643
 	int i,n;
170643
+
170643
 	if (s->s3)
170643
 		s->s3->send_connection_binding = 0;
170643
 
170643
 	n=ssl_put_cipher_by_char(s,NULL,NULL);
170643
-	if ((num%n) != 0)
170643
+	if (n == 0 || (num%n) != 0)
170643
 		{
170643
 		SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
170643
 		return(NULL);
170643
@@ -1449,7 +1464,7 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_ciphe
170643
 
170643
 	for (i=0; i
170643
 		{
170643
-		/* Check for SCSV */
170643
+		/* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
170643
 		if (s->s3 && (n != 3 || !p[0]) &&
170643
 			(p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
170643
 			(p[n-1] == (SSL3_CK_SCSV & 0xff)))
170643
@@ -1469,6 +1484,23 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_ciphe
170643
 			continue;
170643
 			}
170643
 
170643
+		/* Check for TLS_FALLBACK_SCSV */
170643
+		if ((n != 3 || !p[0]) &&
170643
+			(p[n-2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
170643
+			(p[n-1] == (SSL3_CK_FALLBACK_SCSV & 0xff)))
170643
+			{
170643
+			/* The SCSV indicates that the client previously tried a higher version.
170643
+			 * Fail if the current version is an unexpected downgrade. */
170643
+			if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL))
170643
+				{
170643
+				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_INAPPROPRIATE_FALLBACK);
170643
+				if (s->s3)
170643
+					ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INAPPROPRIATE_FALLBACK);
170643
+				goto err;
170643
+				}
170643
+			continue;
170643
+			}
170643
+
170643
 		c=ssl_get_cipher_by_char(s,p);
170643
 		p+=n;
170643
 		if (c != NULL)
170643
diff -up openssl-1.0.1e/ssl/ssl3.h.fallback-scsv openssl-1.0.1e/ssl/ssl3.h
83c29f
--- openssl-1.0.1e/ssl/ssl3.h.fallback-scsv	2014-10-15 14:45:25.570915303 +0200
83c29f
+++ openssl-1.0.1e/ssl/ssl3.h	2014-10-15 14:45:25.598915935 +0200
170643
@@ -128,9 +128,14 @@
170643
 extern "C" {
170643
 #endif
170643
 
170643
-/* Signalling cipher suite value: from draft-ietf-tls-renegotiation-03.txt */
170643
+/* Signalling cipher suite value from RFC 5746
170643
+ * (TLS_EMPTY_RENEGOTIATION_INFO_SCSV) */
170643
 #define SSL3_CK_SCSV				0x030000FF
170643
 
170643
+/* Signalling cipher suite value from draft-ietf-tls-downgrade-scsv-00
170643
+ * (TLS_FALLBACK_SCSV) */
170643
+#define SSL3_CK_FALLBACK_SCSV			0x03005600
170643
+
170643
 #define SSL3_CK_RSA_NULL_MD5			0x03000001
170643
 #define SSL3_CK_RSA_NULL_SHA			0x03000002
170643
 #define SSL3_CK_RSA_RC4_40_MD5 			0x03000003
170643
diff -up openssl-1.0.1e/ssl/s2_lib.c.fallback-scsv openssl-1.0.1e/ssl/s2_lib.c
83c29f
--- openssl-1.0.1e/ssl/s2_lib.c.fallback-scsv	2014-10-15 14:45:25.526914309 +0200
83c29f
+++ openssl-1.0.1e/ssl/s2_lib.c	2014-10-15 14:45:25.598915935 +0200
170643
@@ -391,6 +391,8 @@ long ssl2_ctrl(SSL *s, int cmd, long lar
170643
 	case SSL_CTRL_GET_SESSION_REUSED:
170643
 		ret=s->hit;
170643
 		break;
170643
+	case SSL_CTRL_CHECK_PROTO_VERSION:
170643
+		return ssl3_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, larg, parg);
170643
 	default:
170643
 		break;
170643
 		}
170643
@@ -437,7 +439,7 @@ int ssl2_put_cipher_by_char(const SSL_CI
170643
 	if (p != NULL)
170643
 		{
170643
 		l=c->id;
170643
-		if ((l & 0xff000000) != 0x02000000) return(0);
170643
+		if ((l & 0xff000000) != 0x02000000 && l != SSL3_CK_FALLBACK_SCSV) return(0);
170643
 		p[0]=((unsigned char)(l>>16L))&0xFF;
170643
 		p[1]=((unsigned char)(l>> 8L))&0xFF;
170643
 		p[2]=((unsigned char)(l     ))&0xFF;
170643
diff -up openssl-1.0.1e/ssl/s23_clnt.c.fallback-scsv openssl-1.0.1e/ssl/s23_clnt.c
170643
--- openssl-1.0.1e/ssl/s23_clnt.c.fallback-scsv	2013-02-11 16:26:04.000000000 +0100
83c29f
+++ openssl-1.0.1e/ssl/s23_clnt.c	2014-10-15 14:45:25.598915935 +0200
170643
@@ -715,6 +715,9 @@ static int ssl23_get_server_hello(SSL *s
170643
 			goto err;
170643
 			}
170643
 
170643
+		/* ensure that TLS_MAX_VERSION is up-to-date */
170643
+		OPENSSL_assert(s->version <= TLS_MAX_VERSION);
170643
+
170643
 		if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING)
170643
 			{
170643
 			/* fatal alert */
170643
diff -up openssl-1.0.1e/ssl/s23_srvr.c.fallback-scsv openssl-1.0.1e/ssl/s23_srvr.c
83c29f
--- openssl-1.0.1e/ssl/s23_srvr.c.fallback-scsv	2014-10-15 14:45:25.584915619 +0200
83c29f
+++ openssl-1.0.1e/ssl/s23_srvr.c	2014-10-15 14:45:25.598915935 +0200
170643
@@ -421,6 +421,9 @@ int ssl23_get_client_hello(SSL *s)
170643
 			}
170643
 		}
170643
 
170643
+	/* ensure that TLS_MAX_VERSION is up-to-date */
170643
+	OPENSSL_assert(s->version <= TLS_MAX_VERSION);
170643
+
170643
 #ifdef OPENSSL_FIPS
170643
 	if (FIPS_mode() && (s->version < TLS1_VERSION))
170643
 		{
170643
diff -up openssl-1.0.1e/ssl/s3_enc.c.fallback-scsv openssl-1.0.1e/ssl/s3_enc.c
170643
--- openssl-1.0.1e/ssl/s3_enc.c.fallback-scsv	2013-02-11 16:26:04.000000000 +0100
83c29f
+++ openssl-1.0.1e/ssl/s3_enc.c	2014-10-15 14:45:25.598915935 +0200
170643
@@ -892,7 +892,7 @@ int ssl3_alert_code(int code)
170643
 	case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(SSL3_AD_HANDSHAKE_FAILURE);
170643
 	case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(SSL3_AD_HANDSHAKE_FAILURE);
170643
 	case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY);
170643
+	case SSL_AD_INAPPROPRIATE_FALLBACK:return(TLS1_AD_INAPPROPRIATE_FALLBACK);
170643
 	default:			return(-1);
170643
 		}
170643
 	}
170643
-
170643
diff -up openssl-1.0.1e/ssl/s3_lib.c.fallback-scsv openssl-1.0.1e/ssl/s3_lib.c
83c29f
--- openssl-1.0.1e/ssl/s3_lib.c.fallback-scsv	2014-10-15 14:45:25.590915754 +0200
83c29f
+++ openssl-1.0.1e/ssl/s3_lib.c	2014-10-15 14:45:25.599915957 +0200
83c29f
@@ -3394,6 +3394,33 @@ long ssl3_ctrl(SSL *s, int cmd, long lar
170643
 			EVP_PKEY_free(ptmp);
170643
 			return 0;
170643
 			}
170643
+
170643
+	case SSL_CTRL_CHECK_PROTO_VERSION:
170643
+		/* For library-internal use; checks that the current protocol
170643
+		 * is the highest enabled version (according to s->ctx->method,
170643
+		 * as version negotiation may have changed s->method). */
170643
+		if (s->version == s->ctx->method->version)
170643
+			return 1;
170643
+		/* Apparently we're using a version-flexible SSL_METHOD
170643
+		 * (not at its highest protocol version). */
170643
+		if (s->ctx->method->version == SSLv23_method()->version)
170643
+			{
170643
+#if TLS_MAX_VERSION != TLS1_2_VERSION
170643
+#  error Code needs update for SSLv23_method() support beyond TLS1_2_VERSION.
170643
+#endif
170643
+			if (!(s->options & SSL_OP_NO_TLSv1_2))
170643
+				return s->version == TLS1_2_VERSION;
170643
+			if (!(s->options & SSL_OP_NO_TLSv1_1))
170643
+				return s->version == TLS1_1_VERSION;
170643
+			if (!(s->options & SSL_OP_NO_TLSv1))
170643
+				return s->version == TLS1_VERSION;
170643
+			if (!(s->options & SSL_OP_NO_SSLv3))
170643
+				return s->version == SSL3_VERSION;
170643
+			if (!(s->options & SSL_OP_NO_SSLv2))
170643
+				return s->version == SSL2_VERSION;
170643
+			}
170643
+		return 0; /* Unexpected state; fail closed. */
170643
+
170643
 	default:
170643
 		break;
170643
 		}
83c29f
@@ -3759,6 +3786,7 @@ long ssl3_ctx_callback_ctrl(SSL_CTX *ctx
170643
 		break;
170643
 #endif
170643
 #endif
170643
+
170643
 	default:
170643
 		return(0);
170643
 		}
83c29f
@@ -4337,4 +4365,3 @@ long ssl_get_algorithm2(SSL *s)
170643
 		return SSL_HANDSHAKE_MAC_SHA256 | TLS1_PRF_SHA256;
170643
 	return alg2;
170643
 	}
170643
-		
170643
diff -up openssl-1.0.1e/ssl/tls1.h.fallback-scsv openssl-1.0.1e/ssl/tls1.h
83c29f
--- openssl-1.0.1e/ssl/tls1.h.fallback-scsv	2014-10-15 14:45:25.382911058 +0200
83c29f
+++ openssl-1.0.1e/ssl/tls1.h	2014-10-15 14:45:25.599915957 +0200
170643
@@ -159,17 +159,19 @@ extern "C" {
170643
 
170643
 #define TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES	0
170643
 
170643
+#define TLS1_VERSION			0x0301
170643
+#define TLS1_1_VERSION			0x0302
170643
 #define TLS1_2_VERSION			0x0303
170643
-#define TLS1_2_VERSION_MAJOR		0x03
170643
-#define TLS1_2_VERSION_MINOR		0x03
170643
+#define TLS_MAX_VERSION			TLS1_2_VERSION
170643
+
170643
+#define TLS1_VERSION_MAJOR		0x03
170643
+#define TLS1_VERSION_MINOR		0x01
170643
 
170643
-#define TLS1_1_VERSION			0x0302
170643
 #define TLS1_1_VERSION_MAJOR		0x03
170643
 #define TLS1_1_VERSION_MINOR		0x02
170643
 
170643
-#define TLS1_VERSION			0x0301
170643
-#define TLS1_VERSION_MAJOR		0x03
170643
-#define TLS1_VERSION_MINOR		0x01
170643
+#define TLS1_2_VERSION_MAJOR		0x03
170643
+#define TLS1_2_VERSION_MINOR		0x03
170643
 
170643
 #define TLS1_get_version(s) \
170643
 		((s->version >> 8) == TLS1_VERSION_MAJOR ? s->version : 0)
170643
@@ -187,6 +189,7 @@ extern "C" {
170643
 #define TLS1_AD_PROTOCOL_VERSION	70	/* fatal */
170643
 #define TLS1_AD_INSUFFICIENT_SECURITY	71	/* fatal */
170643
 #define TLS1_AD_INTERNAL_ERROR		80	/* fatal */
170643
+#define TLS1_AD_INAPPROPRIATE_FALLBACK	86	/* fatal */
170643
 #define TLS1_AD_USER_CANCELLED		90
170643
 #define TLS1_AD_NO_RENEGOTIATION	100
170643
 /* codes 110-114 are from RFC3546 */
170643
diff -up openssl-1.0.1e/ssl/t1_enc.c.fallback-scsv openssl-1.0.1e/ssl/t1_enc.c
83c29f
--- openssl-1.0.1e/ssl/t1_enc.c.fallback-scsv	2014-10-15 14:45:25.557915009 +0200
83c29f
+++ openssl-1.0.1e/ssl/t1_enc.c	2014-10-15 14:45:25.599915957 +0200
170643
@@ -1265,6 +1265,7 @@ int tls1_alert_code(int code)
170643
 	case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE: return(TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE);
170643
 	case SSL_AD_BAD_CERTIFICATE_HASH_VALUE: return(TLS1_AD_BAD_CERTIFICATE_HASH_VALUE);
170643
 	case SSL_AD_UNKNOWN_PSK_IDENTITY:return(TLS1_AD_UNKNOWN_PSK_IDENTITY);
170643
+	case SSL_AD_INAPPROPRIATE_FALLBACK:return(TLS1_AD_INAPPROPRIATE_FALLBACK);
170643
 #if 0 /* not appropriate for TLS, not used for DTLS */
170643
 	case DTLS1_AD_MISSING_HANDSHAKE_MESSAGE: return 
170643
 					  (DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);