@@ -97,44 +97,35 @@ public class SSLContext extends RubyObject {
97
97
98
98
// Mapping table for OpenSSL's SSL_METHOD -> JSSE's SSLContext algorithm.
99
99
private static final HashMap <String , String > SSL_VERSION_OSSL2JSSE ;
100
- // Inverse mapping (incomplete, does not map 1-1 with SSL_VERSION_OSSL2JSSE)
101
- private static final HashMap <String , String > SSL_VERSION_OSSL2JSSE_INV ;
102
100
// Mapping table for JSEE's enabled protocols for the algorithm.
103
101
private static final Map <String , String []> ENABLED_PROTOCOLS ;
104
102
// Mapping table from CRuby parse_proto_version(VALUE str)
105
103
private static final Map <String , Integer > PROTO_VERSION_MAP ;
106
- // same as METHODS_MAP from ssl.rb
107
- private static final Map <String , Integer > METHODS_MAP ;
108
- private static final Map <Integer , String > METHODS_MAP_INV ;
104
+
105
+ private static final Map <String , Integer > JSSE_TO_VERSION ;
109
106
110
107
static {
111
108
SSL_VERSION_OSSL2JSSE = new LinkedHashMap <String , String >(20 , 1 );
112
109
ENABLED_PROTOCOLS = new HashMap <String , String []>(8 , 1 );
113
110
114
- SSL_VERSION_OSSL2JSSE_INV = new HashMap <String , String >();
115
-
116
111
SSL_VERSION_OSSL2JSSE .put ("TLSv1" , "TLSv1" );
117
112
SSL_VERSION_OSSL2JSSE .put ("TLSv1_server" , "TLSv1" );
118
113
SSL_VERSION_OSSL2JSSE .put ("TLSv1_client" , "TLSv1" );
119
114
ENABLED_PROTOCOLS .put ("TLSv1" , new String [] { "TLSv1" });
120
- SSL_VERSION_OSSL2JSSE_INV .put ("TLSv1" , "TLSv1_1" );
121
115
122
116
SSL_VERSION_OSSL2JSSE .put ("SSLv2" , "SSLv2" );
123
117
SSL_VERSION_OSSL2JSSE .put ("SSLv2_server" , "SSLv2" );
124
118
SSL_VERSION_OSSL2JSSE .put ("SSLv2_client" , "SSLv2" );
125
119
ENABLED_PROTOCOLS .put ("SSLv2" , new String [] { "SSLv2" });
126
- SSL_VERSION_OSSL2JSSE_INV .put ("SSLv2" , "SSLv2" );
127
120
128
121
SSL_VERSION_OSSL2JSSE .put ("SSLv3" , "SSLv3" );
129
122
SSL_VERSION_OSSL2JSSE .put ("SSLv3_server" , "SSLv3" );
130
123
SSL_VERSION_OSSL2JSSE .put ("SSLv3_client" , "SSLv3" );
131
124
ENABLED_PROTOCOLS .put ("SSLv3" , new String [] { "SSLv3" });
132
- SSL_VERSION_OSSL2JSSE_INV .put ("SSLv3" , "SSLv3" );
133
125
134
126
SSL_VERSION_OSSL2JSSE .put ("SSLv23" , "SSL" );
135
127
SSL_VERSION_OSSL2JSSE .put ("SSLv23_server" , "SSL" );
136
128
SSL_VERSION_OSSL2JSSE .put ("SSLv23_client" , "SSL" );
137
- SSL_VERSION_OSSL2JSSE_INV .put ("SSL" , "SSLv23" );
138
129
139
130
ENABLED_PROTOCOLS .put ("SSL" , new String [] { "SSLv2" , "SSLv3" , "TLSv1" , "TLSv1.1" , "TLSv1.2" });
140
131
@@ -152,8 +143,6 @@ public class SSLContext extends RubyObject {
152
143
SSL_VERSION_OSSL2JSSE .put ("TLSv1_1" , "TLSv1.1" ); // supported on MRI 2.x
153
144
SSL_VERSION_OSSL2JSSE .put ("TLSv1_2" , "TLSv1.2" ); // supported on MRI 2.x
154
145
ENABLED_PROTOCOLS .put ("TLSv1.2" , new String [] { "TLSv1.2" });
155
- SSL_VERSION_OSSL2JSSE_INV .put ("TLSv1.1" , "TLSv1_1" );
156
- SSL_VERSION_OSSL2JSSE_INV .put ("TLSv1.2" , "TLSv1_2" );
157
146
158
147
SSL_VERSION_OSSL2JSSE .put ("TLSv1.2" , "TLSv1.2" ); // just for completeness
159
148
SSL_VERSION_OSSL2JSSE .put ("TLSv1_2_server" , "TLSv1.2" );
@@ -167,19 +156,13 @@ public class SSLContext extends RubyObject {
167
156
PROTO_VERSION_MAP .put ("TLS1_2" , SSL .TLS1_2_VERSION );
168
157
PROTO_VERSION_MAP .put ("TLS1_3" , SSL .TLS1_3_VERSION );
169
158
170
- METHODS_MAP = new HashMap <String , Integer >();
171
- METHODS_MAP .put ("SSLv23" , 0 );
172
- METHODS_MAP .put ("SSLv2" , SSL .SSL2_VERSION );
173
- METHODS_MAP .put ("SSLv3" , SSL .SSL3_VERSION );
174
- METHODS_MAP .put ("TLSv1" , SSL .TLS1_VERSION );
175
- METHODS_MAP .put ("TLSv1_1" , SSL .TLS1_1_VERSION );
176
- METHODS_MAP .put ("TLSv1_2" , SSL .TLS1_2_VERSION );
177
- METHODS_MAP .put ("TLSv1_3" , SSL .TLS1_3_VERSION );
178
-
179
- METHODS_MAP_INV = new HashMap <Integer , String >();
180
- for (Map .Entry <String , Integer > e : METHODS_MAP .entrySet ()) {
181
- METHODS_MAP_INV .put (e .getValue (), e .getKey ());
182
- }
159
+ JSSE_TO_VERSION = new HashMap <String , Integer >();
160
+ JSSE_TO_VERSION .put ("SSLv2" , SSL .SSL2_VERSION );
161
+ JSSE_TO_VERSION .put ("SSLv3" , SSL .SSL3_VERSION );
162
+ JSSE_TO_VERSION .put ("TLSv1" , SSL .TLS1_VERSION );
163
+ JSSE_TO_VERSION .put ("TLSv1.1" , SSL .TLS1_1_VERSION );
164
+ JSSE_TO_VERSION .put ("TLSv1.2" , SSL .TLS1_2_VERSION );
165
+ JSSE_TO_VERSION .put ("TLSv1.3" , SSL .TLS1_3_VERSION );
183
166
}
184
167
185
168
private static ObjectAllocator SSLCONTEXT_ALLOCATOR = new ObjectAllocator () {
@@ -305,10 +288,7 @@ public SSLContext(Ruby runtime, RubyClass type) {
305
288
}
306
289
307
290
private String ciphers = CipherStrings .SSL_DEFAULT_CIPHER_LIST ;
308
- private static final String DEFAULT_PROTOCOL = "SSL" ; // SSLv23 in OpenSSL by default
309
- private static final int DEFAULT_PROTOCOL_VERSION = 0 ;
310
- private String protocol = null ;
311
- private Integer protocolVersion = null ;
291
+ private String protocol = "SSL" ; // SSLv23 in OpenSSL by default
312
292
private boolean protocolForServer = true ;
313
293
private boolean protocolForClient = true ;
314
294
private int minProtocolVersion = 0 ;
@@ -489,8 +469,6 @@ public IRubyObject setup(final ThreadContext context) {
489
469
}
490
470
*/
491
471
492
- setupProtocolVersion (context );
493
-
494
472
try {
495
473
internalContext = createInternalContext (context , cert , key , store , clientCert , extraChainCert , verifyMode , timeout );
496
474
}
@@ -501,37 +479,6 @@ public IRubyObject setup(final ThreadContext context) {
501
479
return runtime .getTrue ();
502
480
}
503
481
504
- private void setupProtocolVersion (final ThreadContext context ) {
505
- if (protocolVersion == null ) {
506
- String ssl_version = null ;
507
- if (maxProtocolVersion != 0 ) {
508
- ssl_version = METHODS_MAP_INV .get (maxProtocolVersion );
509
- set_ssl_version (ssl_version );
510
- } else {
511
- ssl_version = getMaxSupportedProtocolVersion ();
512
- set_ssl_version (ssl_version );
513
- }
514
- }
515
-
516
- if (minProtocolVersion !=0 || maxProtocolVersion !=0 ) {
517
- if (minProtocolVersion != 0 && protocolVersion < minProtocolVersion ) {
518
- throw newSSLError (context .runtime , "no protocols available" );
519
- }
520
- if (maxProtocolVersion != 0 && protocolVersion > maxProtocolVersion ) {
521
- throw newSSLError (context .runtime , "no protocols available" );
522
- }
523
- }
524
- }
525
-
526
- private String getMaxSupportedProtocolVersion () {
527
- //TODO: probably needs to be computed from
528
- //javax.net.ssl.SSLContext.getSupportedSSLParameters().getProtocols()
529
- //some changes prob. needed in dummySSLEngine and SecurityHelper.getSSLContext
530
- //Hardcoding now.
531
- //Nonetheless, TLS1.3 needs more changes in jruby-openssl
532
- return "TLSv1_2" ;
533
- }
534
-
535
482
@ JRubyMethod
536
483
public RubyArray ciphers (final ThreadContext context ) {
537
484
return matchedCiphers (context );
@@ -540,7 +487,6 @@ public RubyArray ciphers(final ThreadContext context) {
540
487
private RubyArray matchedCiphers (final ThreadContext context ) {
541
488
final Ruby runtime = context .runtime ;
542
489
try {
543
- setupProtocolVersion (context );
544
490
final String [] supported = getSupportedCipherSuites (protocol );
545
491
final Collection <CipherStrings .Def > cipherDefs =
546
492
CipherStrings .matchingCiphers (this .ciphers , supported , false );
@@ -594,19 +540,14 @@ public IRubyObject set_ssl_version(IRubyObject version) {
594
540
} else {
595
541
versionStr = version .convertToString ().toString ();
596
542
}
597
- set_ssl_version (versionStr );
598
- return version ;
599
- }
600
-
601
- private void set_ssl_version (final String versionStr ) {
602
543
final String protocol = SSL_VERSION_OSSL2JSSE .get (versionStr );
603
544
if ( protocol == null ) {
604
545
throw getRuntime ().newArgumentError ("unknown SSL method `" + versionStr +"'" );
605
546
}
606
547
this .protocol = protocol ;
607
- this .protocolVersion = METHODS_MAP .get (versionStr );
608
548
protocolForServer = ! versionStr .endsWith ("_client" );
609
549
protocolForClient = ! versionStr .endsWith ("_server" );
550
+ return version ;
610
551
}
611
552
612
553
@ JRubyMethod (name = "set_minmax_proto_version" )
@@ -621,7 +562,7 @@ private int parseProtoVersion(IRubyObject version) {
621
562
if (version .isNil ())
622
563
return 0 ;
623
564
if (version instanceof RubyFixnum ) {
624
- return ( int ) (( RubyFixnum ) version ). getLongValue ( );
565
+ return RubyFixnum . fix2int ( version );
625
566
}
626
567
627
568
String string = version .asString ().asJavaString ();
@@ -634,7 +575,7 @@ private int parseProtoVersion(IRubyObject version) {
634
575
return sslVersion ;
635
576
}
636
577
637
- final String getProtocol () { return this .protocol ; }
578
+ final String getProtocol () { return this .protocol ; }
638
579
639
580
@ JRubyMethod (name = "session_cache_mode" )
640
581
public IRubyObject session_cache_mode () {
@@ -758,6 +699,10 @@ private String[] getEnabledProtocols(final SSLEngine engine) {
758
699
final String [] engineProtocols = engine .getEnabledProtocols ();
759
700
final List <String > protocols = new ArrayList <String >(enabledProtocols .length );
760
701
for ( final String enabled : enabledProtocols ) {
702
+ int protocolVersion = JSSE_TO_VERSION .get (enabled );
703
+ if (minProtocolVersion != 0 && protocolVersion < minProtocolVersion ) continue ;
704
+ if (maxProtocolVersion != 0 && protocolVersion > maxProtocolVersion ) continue ;
705
+
761
706
if (((options & OP_NO_SSLv2 ) != 0 ) && enabled .equals ("SSLv2" )) continue ;
762
707
if (((options & OP_NO_SSLv3 ) != 0 ) && enabled .equals ("SSLv3" )) continue ;
763
708
if (((options & OP_NO_TLSv1 ) != 0 ) && enabled .equals ("TLSv1" )) continue ;
0 commit comments