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