50530d
From 950fd771fb8908968cce67a38fdde69ef0cd2b80 Mon Sep 17 00:00:00 2001
50530d
From: nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
50530d
Date: Fri, 27 Nov 2015 21:24:30 +0000
50530d
Subject: [PATCH] merge revision(s) 52227,52228: [Backport #11369]
50530d
50530d
	* ext/openssl/ossl_ssl.c (ssl_npn_select_cb): explicitly raise error
50530d
	  in ext/openssl instead of OpenSSL itself because LibreSSL
50530d
	  silently truncate the selected protocol name by casting the length
50530d
	  from int to unsigned char. [Bug #11369]
50530d
	  Patch by Jeremy Evans <merch-redmine@jeremyevans.net>
50530d
50530d
50530d
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@52772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
50530d
---
50530d
 ChangeLog              |  8 ++++++++
50530d
 ext/openssl/ossl_ssl.c | 43 +++++++++++++++++++++++++++++++------------
50530d
 2 files changed, 39 insertions(+), 12 deletions(-)
50530d
50530d
diff --git a/ChangeLog b/ChangeLog
50530d
index 161a4b9..160143c 100644
50530d
--- a/ChangeLog
50530d
+++ b/ChangeLog
50530d
@@ -36,6 +36,14 @@
50530d
 
50530d
 	* ext/dl/handle.c (rb_dlhandle_sym): ditto
50530d
 
50530d
+Sat Nov 28 06:12:32 2015  NARUSE, Yui  <naruse@ruby-lang.org>
50530d
+
50530d
+	* ext/openssl/ossl_ssl.c (ssl_npn_select_cb): explicitly raise error
50530d
+	  in ext/openssl instead of OpenSSL itself because LibreSSL
50530d
+	  silently truncate the selected protocol name by casting the length
50530d
+	  from int to unsigned char. [Bug #11369]
50530d
+	  Patch by Jeremy Evans <merch-redmine@jeremyevans.net>
50530d
+
50530d
 Tue Aug 18 22:00:12 2015  SHIBATA Hiroshi  <hsbt@ruby-lang.org>
50530d
 
50530d
 	* lib/rubygems.rb: bump version to 2.0.14.1. this version fixed
50530d
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
50530d
index 75e26a4..6e777c9 100644
50530d
--- a/ext/openssl/ossl_ssl.c
50530d
+++ b/ext/openssl/ossl_ssl.c
50530d
@@ -601,29 +601,48 @@ ssl_npn_advertise_cb(SSL *ssl, const unsigned char **out, unsigned int *outlen,
50530d
 }
50530d
 
50530d
 static int
50530d
-ssl_npn_select_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
50530d
+ssl_npn_select_cb_common(VALUE cb, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen)
50530d
 {
50530d
-    int i = 0;
50530d
-    VALUE sslctx_obj, cb, protocols, selected;
50530d
-
50530d
-    sslctx_obj = (VALUE) arg;
50530d
-    cb = rb_iv_get(sslctx_obj, "@npn_select_cb");
50530d
-    protocols = rb_ary_new();
50530d
+    VALUE selected;
50530d
+    long len;
50530d
+    unsigned char l;
50530d
+    VALUE protocols = rb_ary_new();
50530d
 
50530d
     /* The format is len_1|proto_1|...|len_n|proto_n\0 */
50530d
-    while (in[i]) {
50530d
-	VALUE protocol = rb_str_new((const char *) &in[i + 1], in[i]);
50530d
+    while (l = *in++) {
50530d
+	VALUE protocol;
50530d
+	if (l > inlen) {
50530d
+	    ossl_raise(eSSLError, "Invalid protocol name list");
50530d
+	}
50530d
+	protocol = rb_str_new((const char *)in, l);
50530d
 	rb_ary_push(protocols, protocol);
50530d
-	i += in[i] + 1;
50530d
+	in += l;
50530d
+	inlen -= l;
50530d
     }
50530d
 
50530d
     selected = rb_funcall(cb, rb_intern("call"), 1, protocols);
50530d
     StringValue(selected);
50530d
-    *out = (unsigned char *) StringValuePtr(selected);
50530d
-    *outlen = RSTRING_LENINT(selected);
50530d
+    len = RSTRING_LEN(selected);
50530d
+    if (len < 1 || len >= 256) {
50530d
+	ossl_raise(eSSLError, "Selected protocol name must have length 1..255");
50530d
+    }
50530d
+    *out = (unsigned char *)RSTRING_PTR(selected);
50530d
+    *outlen = (unsigned char)len;
50530d
 
50530d
     return SSL_TLSEXT_ERR_OK;
50530d
 }
50530d
+
50530d
+static int
50530d
+ssl_npn_select_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
50530d
+{
50530d
+    VALUE sslctx_obj, cb;
50530d
+
50530d
+    sslctx_obj = (VALUE) arg;
50530d
+    cb = rb_iv_get(sslctx_obj, "@npn_select_cb");
50530d
+
50530d
+    return ssl_npn_select_cb_common(cb, (const unsigned char **)out, outlen, in, inlen);
50530d
+}
50530d
+
50530d
 #endif
50530d
 
50530d
 /* This function may serve as the entry point to support further