Index: openssl-0.9.7g/apps/s_client.c
===================================================================
--- openssl-0.9.7g.orig/apps/s_client.c	2014-11-28 13:28:31.680803820 +0100
+++ openssl-0.9.7g/apps/s_client.c	2014-11-28 13:43:30.315571782 +0100
@@ -212,6 +212,7 @@ static void sc_usage(void)
 	BIO_printf(bio_err," -ssl2         - just use SSLv2\n");
 	BIO_printf(bio_err," -ssl3         - just use SSLv3\n");
 	BIO_printf(bio_err," -tls1         - just use TLSv1\n");
+	BIO_printf(bio_err," -fallback_scsv - send TLS_FALLBACK_SCSV\n");
 	BIO_printf(bio_err," -no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n");
 	BIO_printf(bio_err," -bugs         - Switch on all SSL implementation bug workarounds\n");
 	BIO_printf(bio_err," -serverpref   - Use server's cipher preferences (only SSLv2)\n");
@@ -232,6 +233,7 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
 	{
+	int fallback_scsv = 0;
 	int off=0;
 	SSL *con=NULL,*con2=NULL;
 	X509_STORE *store = NULL;
@@ -375,6 +377,10 @@ int MAIN(int argc, char **argv)
 		else if	(strcmp(*argv,"-tls1") == 0)
 			meth=TLSv1_client_method();
 #endif
+		else if (strcmp(*argv,"-fallback_scsv") == 0)
+			{
+			fallback_scsv = 1;
+			}
 		else if (strcmp(*argv,"-bugs") == 0)
 			bugs=1;
 		else if	(strcmp(*argv,"-key") == 0)
@@ -521,6 +527,9 @@ bad:
 	X509_STORE_set_flags(store, vflags);
 
 	con=SSL_new(ctx);
+
+	if (fallback_scsv)
+		SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);
 #ifndef OPENSSL_NO_KRB5
 	if (con  &&  (con->kssl_ctx = kssl_ctx_new()) != NULL)
                 {
Index: openssl-0.9.7g/crypto/err/openssl.ec
===================================================================
--- openssl-0.9.7g.orig/crypto/err/openssl.ec	2014-11-28 13:28:31.681803831 +0100
+++ openssl-0.9.7g/crypto/err/openssl.ec	2014-11-28 13:28:45.651971264 +0100
@@ -62,6 +62,7 @@ R SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION
 R SSL_R_TLSV1_ALERT_PROTOCOL_VERSION		1070
 R SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY	1071
 R SSL_R_TLSV1_ALERT_INTERNAL_ERROR		1080
+R SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK	1086
 R SSL_R_TLSV1_ALERT_USER_CANCELLED		1090
 R SSL_R_TLSV1_ALERT_NO_RENEGOTIATION		1100
 
Index: openssl-0.9.7g/doc/apps/s_client.pod
===================================================================
--- openssl-0.9.7g.orig/doc/apps/s_client.pod	2014-11-28 13:28:31.681803831 +0100
+++ openssl-0.9.7g/doc/apps/s_client.pod	2014-11-28 13:28:45.651971264 +0100
@@ -31,6 +31,7 @@ B<openssl> B<s_client>
 [B<-no_ssl2>]
 [B<-no_ssl3>]
 [B<-no_tls1>]
+[B<-fallback_scsv>]
 [B<-bugs>]
 [B<-cipher cipherlist>]
 [B<-starttls protocol>]
@@ -147,10 +148,13 @@ these options disable the use of certain
 the initial handshake uses a method which should be compatible with all
 servers and permit them to use SSL v3, SSL v2 or TLS as appropriate.
 
-Unfortunately there are a lot of ancient and broken servers in use which
+Unfortunately there are still ancient and broken servers in use which
 cannot handle this technique and will fail to connect. Some servers only
-work if TLS is turned off with the B<-no_tls> option others will only
-support SSL v2 and may need the B<-ssl2> option.
+work if TLS is turned off.
+
+=item B<-fallback_scsv>
+
+Send TLS_FALLBACK_SCSV in the ClientHello.
 
 =item B<-bugs>
 
Index: openssl-0.9.7g/doc/ssl/SSL_CTX_set_mode.pod
===================================================================
--- openssl-0.9.7g.orig/doc/ssl/SSL_CTX_set_mode.pod	2014-11-28 13:28:31.681803831 +0100
+++ openssl-0.9.7g/doc/ssl/SSL_CTX_set_mode.pod	2014-11-28 13:28:45.651971264 +0100
@@ -61,6 +61,12 @@ deal with read/write operations returnin
 flag SSL_MODE_AUTO_RETRY will cause read/write operations to only
 return after the handshake and successful completion.
 
+=item SSL_MODE_FALLBACK_SCSV
+
+Send TLS_FALLBACK_SCSV in the ClientHello.
+To be set by applications that reconnect with a downgraded protocol
+version; see draft-ietf-tls-downgrade-scsv-00 for details.
+
 =back
 
 =head1 RETURN VALUES
Index: openssl-0.9.7g/ssl/s23_clnt.c
===================================================================
--- openssl-0.9.7g.orig/ssl/s23_clnt.c	2014-11-28 13:28:31.682803843 +0100
+++ openssl-0.9.7g/ssl/s23_clnt.c	2014-11-28 13:28:45.652971276 +0100
@@ -459,6 +459,10 @@ static int ssl23_get_server_hello(SSL *s
 			SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
 			goto err;
 			}
+
+		/* ensure that TLS_MAX_VERSION is up-to-date */
+		OPENSSL_assert(s->version <= TLS_MAX_VERSION);
+
 			
 		s->handshake_func=s->method->ssl_connect;
 		}
Index: openssl-0.9.7g/ssl/s23_srvr.c
===================================================================
--- openssl-0.9.7g.orig/ssl/s23_srvr.c	2014-11-28 13:28:31.682803843 +0100
+++ openssl-0.9.7g/ssl/s23_srvr.c	2014-11-28 13:28:45.652971276 +0100
@@ -413,6 +413,9 @@ int ssl23_get_client_hello(SSL *s)
 		}
 #endif
 
+	/* ensure that TLS_MAX_VERSION is up-to-date */
+	OPENSSL_assert(s->version <= TLS_MAX_VERSION);
+
 	if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
 		{
 		/* we have SSLv3/TLSv1 in an SSLv2 header
Index: openssl-0.9.7g/ssl/s2_lib.c
===================================================================
--- openssl-0.9.7g.orig/ssl/s2_lib.c	2014-11-28 13:28:31.682803843 +0100
+++ openssl-0.9.7g/ssl/s2_lib.c	2014-11-28 13:28:45.652971276 +0100
@@ -343,6 +343,8 @@ long ssl2_ctrl(SSL *s, int cmd, long lar
 	case SSL_CTRL_GET_SESSION_REUSED:
 		ret=s->hit;
 		break;
+	case SSL_CTRL_CHECK_PROTO_VERSION:
+		return ssl3_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, larg, parg);
 	default:
 		break;
 		}
@@ -413,7 +415,7 @@ int ssl2_put_cipher_by_char(const SSL_CI
 	if (p != NULL)
 		{
 		l=c->id;
-		if ((l & 0xff000000) != 0x02000000) return(0);
+		if ((l & 0xff000000) != 0x02000000 && l != SSL3_CK_FALLBACK_SCSV) return(0);
 		p[0]=((unsigned char)(l>>16L))&0xFF;
 		p[1]=((unsigned char)(l>> 8L))&0xFF;
 		p[2]=((unsigned char)(l     ))&0xFF;
Index: openssl-0.9.7g/ssl/s3_enc.c
===================================================================
--- openssl-0.9.7g.orig/ssl/s3_enc.c	2014-11-28 13:28:31.683803855 +0100
+++ openssl-0.9.7g/ssl/s3_enc.c	2014-11-28 13:28:45.653971288 +0100
@@ -730,6 +730,7 @@ int ssl3_alert_code(int code)
 	case SSL_AD_INTERNAL_ERROR:	return(SSL3_AD_HANDSHAKE_FAILURE);
 	case SSL_AD_USER_CANCELLED:	return(SSL3_AD_HANDSHAKE_FAILURE);
 	case SSL_AD_NO_RENEGOTIATION:	return(-1); /* Don't send it :-) */
+	case SSL_AD_INAPPROPRIATE_FALLBACK:return(TLS1_AD_INAPPROPRIATE_FALLBACK);
 	default:			return(-1);
 		}
 	}
Index: openssl-0.9.7g/ssl/s3_lib.c
===================================================================
--- openssl-0.9.7g.orig/ssl/s3_lib.c	2014-11-28 13:28:31.683803855 +0100
+++ openssl-0.9.7g/ssl/s3_lib.c	2014-11-28 13:57:57.301962394 +0100
@@ -1263,6 +1263,29 @@ long ssl3_ctrl(SSL *s, int cmd, long lar
 		}
 		break;
 #endif
+
+	case SSL_CTRL_CHECK_PROTO_VERSION:
+		/* For library-internal use; checks that the current protocol
+		 * is the highest enabled version (according to s->ctx->method,
+		 * as version negotiation may have changed s->method). */
+		if (s->version == s->ctx->method->version)
+			return 1;
+		/* Apparently we're using a version-flexible SSL_METHOD
+		 * (not at its highest protocol version). */
+		if (s->ctx->method->version == SSLv23_method()->version)
+			{
+#if TLS_MAX_VERSION != TLS1_VERSION
+#  error Code needs update for SSLv23_method() support beyond TLS1_VERSION.
+#endif
+			if (!(s->options & SSL_OP_NO_TLSv1))
+				return s->version == TLS1_VERSION;
+			if (!(s->options & SSL_OP_NO_SSLv3))
+				return s->version == SSL3_VERSION;
+			if (!(s->options & SSL_OP_NO_SSLv2))
+				return s->version == SSL2_VERSION;
+			}
+		return 0; /* Unexpected state; fail closed. */
+
 	default:
 		break;
 		}
@@ -1437,6 +1460,7 @@ long ssl3_ctx_callback_ctrl(SSL_CTX *ctx
 		}
 		break;
 #endif
+
 	default:
 		return(0);
 		}
Index: openssl-0.9.7g/ssl/ssl.h
===================================================================
--- openssl-0.9.7g.orig/ssl/ssl.h	2014-11-28 13:28:31.684803867 +0100
+++ openssl-0.9.7g/ssl/ssl.h	2014-11-28 15:14:46.627008548 +0100
@@ -524,6 +524,10 @@ typedef struct ssl_session_st
 #define SSL_MODE_AUTO_RETRY 0x00000004L
 /* Don't attempt to automatically build certificate chain */
 #define SSL_MODE_NO_AUTO_CHAIN 0x00000008L
+/* Send TLS_FALLBACK_SCSV in the ClientHello.
+ * To be set by applications that reconnect with a downgraded protocol
+ * version; see draft-ietf-tls-downgrade-scsv-00 for details. */
+#define SSL_MODE_SEND_FALLBACK_SCSV 0x00000080L
 
 
 /* Note: SSL[_CTX]_set_{options,mode} use |= op on the previous value,
@@ -1078,6 +1082,7 @@ size_t SSL_get_peer_finished(const SSL *
 #define SSL_AD_INTERNAL_ERROR		TLS1_AD_INTERNAL_ERROR	/* fatal */
 #define SSL_AD_USER_CANCELLED		TLS1_AD_USER_CANCELLED
 #define SSL_AD_NO_RENEGOTIATION		TLS1_AD_NO_RENEGOTIATION
+#define SSL_AD_INAPPROPRIATE_FALLBACK	TLS1_AD_INAPPROPRIATE_FALLBACK /* fatal */
 
 #define SSL_ERROR_NONE			0
 #define SSL_ERROR_SSL			1
@@ -1132,6 +1137,8 @@ size_t SSL_get_peer_finished(const SSL *
 #define SSL_CTRL_GET_MAX_CERT_LIST		50
 #define SSL_CTRL_SET_MAX_CERT_LIST		51
 
+#define SSL_CTRL_CHECK_PROTO_VERSION		119
+
 #define SSL_session_reused(ssl) \
 	SSL_ctrl((ssl),SSL_CTRL_GET_SESSION_REUSED,0,NULL)
 #define SSL_num_renegotiations(ssl) \
@@ -1687,6 +1694,7 @@ void ERR_load_SSL_strings(void);
 #define SSL_R_HTTPS_PROXY_REQUEST			 155
 #define SSL_R_HTTP_REQUEST				 156
 #define SSL_R_ILLEGAL_PADDING				 1110
+#define SSL_R_INAPPROPRIATE_FALLBACK			 373
 #define SSL_R_INVALID_CHALLENGE_LENGTH			 158
 #define SSL_R_INVALID_COMMAND				 280
 #define SSL_R_INVALID_PURPOSE				 278
@@ -1770,6 +1778,7 @@ void ERR_load_SSL_strings(void);
 #define SSL_R_REUSE_CERT_LENGTH_NOT_ZERO		 216
 #define SSL_R_REUSE_CERT_TYPE_NOT_ZERO			 217
 #define SSL_R_REUSE_CIPHER_LIST_NOT_ZERO		 218
+#define SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING		 345
 #define SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED		 277
 #define SSL_R_SHORT_READ				 219
 #define SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE	 220
@@ -1806,6 +1815,7 @@ void ERR_load_SSL_strings(void);
 #define SSL_R_TLSV1_ALERT_DECRYPTION_FAILED		 1021
 #define SSL_R_TLSV1_ALERT_DECRYPT_ERROR			 1051
 #define SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION		 1060
+#define SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK	 1086
 #define SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY		 1071
 #define SSL_R_TLSV1_ALERT_INTERNAL_ERROR		 1080
 #define SSL_R_TLSV1_ALERT_NO_RENEGOTIATION		 1100
Index: openssl-0.9.7g/ssl/ssl3.h
===================================================================
--- openssl-0.9.7g.orig/ssl/ssl3.h	2014-11-28 13:28:45.654971300 +0100
+++ openssl-0.9.7g/ssl/ssl3.h	2014-11-28 16:01:11.908636320 +0100
@@ -123,6 +123,10 @@
 extern "C" {
 #endif
 
+/* Signalling cipher suite value from draft-ietf-tls-downgrade-scsv-00
+ * (TLS_FALLBACK_SCSV) */
+#define SSL3_CK_FALLBACK_SCSV			0x03005600
+
 #define SSL3_CK_RSA_NULL_MD5			0x03000001
 #define SSL3_CK_RSA_NULL_SHA			0x03000002
 #define SSL3_CK_RSA_RC4_40_MD5 			0x03000003
Index: openssl-0.9.7g/ssl/ssl_err.c
===================================================================
--- openssl-0.9.7g.orig/ssl/ssl_err.c	2014-11-28 13:28:45.655971313 +0100
+++ openssl-0.9.7g/ssl/ssl_err.c	2014-11-28 14:02:17.177083984 +0100
@@ -278,6 +278,7 @@ static ERR_STRING_DATA SSL_str_reasons[]
 {SSL_R_HTTPS_PROXY_REQUEST               ,"https proxy request"},
 {SSL_R_HTTP_REQUEST                      ,"http request"},
 {SSL_R_ILLEGAL_PADDING                   ,"illegal padding"},
+{SSL_R_INAPPROPRIATE_FALLBACK            ,"inappropriate fallback"},
 {SSL_R_INVALID_CHALLENGE_LENGTH          ,"invalid challenge length"},
 {SSL_R_INVALID_COMMAND                   ,"invalid command"},
 {SSL_R_INVALID_PURPOSE                   ,"invalid purpose"},
@@ -397,6 +398,7 @@ static ERR_STRING_DATA SSL_str_reasons[]
 {SSL_R_TLSV1_ALERT_DECRYPTION_FAILED     ,"tlsv1 alert decryption failed"},
 {SSL_R_TLSV1_ALERT_DECRYPT_ERROR         ,"tlsv1 alert decrypt error"},
 {SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION    ,"tlsv1 alert export restriction"},
+{SSL_R_TLSV1_ALERT_INAPPROPRIATE_FALLBACK,"tlsv1 alert inappropriate fallback"},
 {SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY ,"tlsv1 alert insufficient security"},
 {SSL_R_TLSV1_ALERT_INTERNAL_ERROR        ,"tlsv1 alert internal error"},
 {SSL_R_TLSV1_ALERT_NO_RENEGOTIATION      ,"tlsv1 alert no renegotiation"},
Index: openssl-0.9.7g/ssl/ssl_lib.c
===================================================================
--- openssl-0.9.7g.orig/ssl/ssl_lib.c	2014-11-28 13:28:31.686803891 +0100
+++ openssl-0.9.7g/ssl/ssl_lib.c	2014-11-28 16:06:00.086266239 +0100
@@ -1203,6 +1203,21 @@ int ssl_cipher_list_to_bytes(SSL *s,STAC
 		j=ssl_put_cipher_by_char(s,c,p);
 		p+=j;
 		}
+	/* If p == q, no ciphers; caller indicates an error.
+	 * Otherwise, add applicable SCSVs. */
+	if (p != q)
+		{
+		if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
+			{
+			static SSL_CIPHER scsv =
+				{
+				0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0,
+				};
+			j = ssl_put_cipher_by_char(s,&scsv,p);
+			p+=j;
+			}
+ 		}
+
 	return(p-q);
 	}
 
@@ -1214,7 +1229,7 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_ciphe
 	int i,n;
 
 	n=ssl_put_cipher_by_char(s,NULL,NULL);
-	if ((num%n) != 0)
+	if (n == 0 || (num%n) != 0)
 		{
 		SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
 		return(NULL);
@@ -1229,6 +1244,22 @@ STACK_OF(SSL_CIPHER) *ssl_bytes_to_ciphe
 
 	for (i=0; i<num; i+=n)
 		{
+		/* Check for TLS_FALLBACK_SCSV */
+		if ((n != 3 || !p[0]) &&
+			(p[n-2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
+			(p[n-1] == (SSL3_CK_FALLBACK_SCSV & 0xff)))
+			{
+			/* The SCSV indicates that the client previously tried a higher version.
+			 * Fail if the current version is an unexpected downgrade. */
+			if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL))
+				{
+				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_INAPPROPRIATE_FALLBACK);
+				if (s->s3)
+					ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INAPPROPRIATE_FALLBACK);
+				goto err;
+				}
+			continue;
+			}
 		c=ssl_get_cipher_by_char(s,p);
 		p+=n;
 		if (c != NULL)
Index: openssl-0.9.7g/ssl/t1_enc.c
===================================================================
--- openssl-0.9.7g.orig/ssl/t1_enc.c	2014-11-28 13:28:31.686803891 +0100
+++ openssl-0.9.7g/ssl/t1_enc.c	2014-11-28 14:34:03.267101330 +0100
@@ -821,7 +821,7 @@ int tls1_alert_code(int code)
 	case SSL_AD_INTERNAL_ERROR:	return(TLS1_AD_INTERNAL_ERROR);
 	case SSL_AD_USER_CANCELLED:	return(TLS1_AD_USER_CANCELLED);
 	case SSL_AD_NO_RENEGOTIATION:	return(TLS1_AD_NO_RENEGOTIATION);
+	case SSL_AD_INAPPROPRIATE_FALLBACK:return(TLS1_AD_INAPPROPRIATE_FALLBACK);
 	default:			return(-1);
 		}
 	}
-
Index: openssl-0.9.7g/ssl/tls1.h
===================================================================
--- openssl-0.9.7g.orig/ssl/tls1.h	2014-11-28 13:28:31.687803904 +0100
+++ openssl-0.9.7g/ssl/tls1.h	2014-11-28 13:28:45.656971324 +0100
@@ -68,9 +68,24 @@ extern "C" {
 #define TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES	1
 
 #define TLS1_VERSION			0x0301
+#define TLS1_1_VERSION			0x0302
+#define TLS1_2_VERSION			0x0303
+/* TLS 1.1 and 1.2 are not supported by this version of OpenSSL, so
+ * TLS_MAX_VERSION indicates TLS 1.0 regardless of the above
+ * definitions. (s23_clnt.c and s23_srvr.c have an OPENSSL_assert()
+ * check that would catch the error if TLS_MAX_VERSION was too low.)
+ */
+#define TLS_MAX_VERSION			TLS1_VERSION
+
 #define TLS1_VERSION_MAJOR		0x03
 #define TLS1_VERSION_MINOR		0x01
 
+#define TLS1_1_VERSION_MAJOR		0x03
+#define TLS1_1_VERSION_MINOR		0x02
+
+#define TLS1_2_VERSION_MAJOR		0x03
+#define TLS1_2_VERSION_MINOR		0x03
+
 #define TLS1_AD_DECRYPTION_FAILED	21
 #define TLS1_AD_RECORD_OVERFLOW		22
 #define TLS1_AD_UNKNOWN_CA		48	/* fatal */
@@ -81,6 +96,7 @@ extern "C" {
 #define TLS1_AD_PROTOCOL_VERSION	70	/* fatal */
 #define TLS1_AD_INSUFFICIENT_SECURITY	71	/* fatal */
 #define TLS1_AD_INTERNAL_ERROR		80	/* fatal */
+#define TLS1_AD_INAPPROPRIATE_FALLBACK	86	/* fatal */
 #define TLS1_AD_USER_CANCELLED		90
 #define TLS1_AD_NO_RENEGOTIATION	100
 
