@@ -96,61 +96,6 @@ ossl_sslctx_s_alloc(VALUE klass)
96
96
return obj ;
97
97
}
98
98
99
- static int
100
- parse_proto_version (VALUE str )
101
- {
102
- int i ;
103
- static const struct {
104
- const char * name ;
105
- int version ;
106
- } map [] = {
107
- { "SSL2" , SSL2_VERSION },
108
- { "SSL3" , SSL3_VERSION },
109
- { "TLS1" , TLS1_VERSION },
110
- { "TLS1_1" , TLS1_1_VERSION },
111
- { "TLS1_2" , TLS1_2_VERSION },
112
- { "TLS1_3" , TLS1_3_VERSION },
113
- };
114
-
115
- if (NIL_P (str ))
116
- return 0 ;
117
- if (RB_INTEGER_TYPE_P (str ))
118
- return NUM2INT (str );
119
-
120
- if (SYMBOL_P (str ))
121
- str = rb_sym2str (str );
122
- StringValue (str );
123
- for (i = 0 ; i < numberof (map ); i ++ )
124
- if (!strncmp (map [i ].name , RSTRING_PTR (str ), RSTRING_LEN (str )))
125
- return map [i ].version ;
126
- rb_raise (rb_eArgError , "unrecognized version %+" PRIsVALUE , str );
127
- }
128
-
129
- /*
130
- * call-seq:
131
- * ctx.set_minmax_proto_version(min, max) -> nil
132
- *
133
- * Sets the minimum and maximum supported protocol versions. See #min_version=
134
- * and #max_version=.
135
- */
136
- static VALUE
137
- ossl_sslctx_set_minmax_proto_version (VALUE self , VALUE min_v , VALUE max_v )
138
- {
139
- SSL_CTX * ctx ;
140
- int min , max ;
141
-
142
- GetSSLCTX (self , ctx );
143
- min = parse_proto_version (min_v );
144
- max = parse_proto_version (max_v );
145
-
146
- if (!SSL_CTX_set_min_proto_version (ctx , min ))
147
- ossl_raise (eSSLError , "SSL_CTX_set_min_proto_version" );
148
- if (!SSL_CTX_set_max_proto_version (ctx , max ))
149
- ossl_raise (eSSLError , "SSL_CTX_set_max_proto_version" );
150
-
151
- return Qnil ;
152
- }
153
-
154
99
static VALUE
155
100
ossl_call_client_cert_cb (VALUE obj )
156
101
{
@@ -915,6 +860,93 @@ ossl_sslctx_setup(VALUE self)
915
860
return Qtrue ;
916
861
}
917
862
863
+ static int
864
+ parse_proto_version (VALUE str )
865
+ {
866
+ int i ;
867
+ static const struct {
868
+ const char * name ;
869
+ int version ;
870
+ } map [] = {
871
+ { "SSL2" , SSL2_VERSION },
872
+ { "SSL3" , SSL3_VERSION },
873
+ { "TLS1" , TLS1_VERSION },
874
+ { "TLS1_1" , TLS1_1_VERSION },
875
+ { "TLS1_2" , TLS1_2_VERSION },
876
+ { "TLS1_3" , TLS1_3_VERSION },
877
+ };
878
+
879
+ if (NIL_P (str ))
880
+ return 0 ;
881
+ if (RB_INTEGER_TYPE_P (str ))
882
+ return NUM2INT (str );
883
+
884
+ if (SYMBOL_P (str ))
885
+ str = rb_sym2str (str );
886
+ StringValue (str );
887
+ for (i = 0 ; i < numberof (map ); i ++ )
888
+ if (!strncmp (map [i ].name , RSTRING_PTR (str ), RSTRING_LEN (str )))
889
+ return map [i ].version ;
890
+ rb_raise (rb_eArgError , "unrecognized version %+" PRIsVALUE , str );
891
+ }
892
+
893
+ /*
894
+ * call-seq:
895
+ * ctx.min_version = OpenSSL::SSL::TLS1_2_VERSION
896
+ * ctx.min_version = :TLS1_2
897
+ * ctx.min_version = nil
898
+ *
899
+ * Sets the lower bound on the supported SSL/TLS protocol version. The
900
+ * version may be specified by an integer constant named
901
+ * OpenSSL::SSL::*_VERSION, a Symbol, or +nil+ which means "any version".
902
+ *
903
+ * === Example
904
+ * ctx = OpenSSL::SSL::SSLContext.new
905
+ * ctx.min_version = OpenSSL::SSL::TLS1_1_VERSION
906
+ * ctx.max_version = OpenSSL::SSL::TLS1_2_VERSION
907
+ *
908
+ * sock = OpenSSL::SSL::SSLSocket.new(tcp_sock, ctx)
909
+ * sock.connect # Initiates a connection using either TLS 1.1 or TLS 1.2
910
+ */
911
+ static VALUE
912
+ ossl_sslctx_set_min_version (VALUE self , VALUE v )
913
+ {
914
+ SSL_CTX * ctx ;
915
+ int version ;
916
+
917
+ rb_check_frozen (self );
918
+ GetSSLCTX (self , ctx );
919
+ version = parse_proto_version (v );
920
+
921
+ if (!SSL_CTX_set_min_proto_version (ctx , version ))
922
+ ossl_raise (eSSLError , "SSL_CTX_set_min_proto_version" );
923
+ return v ;
924
+ }
925
+
926
+ /*
927
+ * call-seq:
928
+ * ctx.max_version = OpenSSL::SSL::TLS1_2_VERSION
929
+ * ctx.max_version = :TLS1_2
930
+ * ctx.max_version = nil
931
+ *
932
+ * Sets the upper bound of the supported SSL/TLS protocol version. See
933
+ * #min_version= for the possible values.
934
+ */
935
+ static VALUE
936
+ ossl_sslctx_set_max_version (VALUE self , VALUE v )
937
+ {
938
+ SSL_CTX * ctx ;
939
+ int version ;
940
+
941
+ rb_check_frozen (self );
942
+ GetSSLCTX (self , ctx );
943
+ version = parse_proto_version (v );
944
+
945
+ if (!SSL_CTX_set_max_proto_version (ctx , version ))
946
+ ossl_raise (eSSLError , "SSL_CTX_set_max_proto_version" );
947
+ return v ;
948
+ }
949
+
918
950
static VALUE
919
951
ossl_ssl_cipher_to_ary (const SSL_CIPHER * cipher )
920
952
{
@@ -2846,8 +2878,8 @@ Init_ossl_ssl(void)
2846
2878
2847
2879
rb_define_alias (cSSLContext , "ssl_timeout" , "timeout" );
2848
2880
rb_define_alias (cSSLContext , "ssl_timeout=" , "timeout=" );
2849
- rb_define_private_method (cSSLContext , "set_minmax_proto_version" ,
2850
- ossl_sslctx_set_minmax_proto_version , 2 );
2881
+ rb_define_method (cSSLContext , "min_version=" , ossl_sslctx_set_min_version , 1 );
2882
+ rb_define_method ( cSSLContext , "max_version=" , ossl_sslctx_set_max_version , 1 );
2851
2883
rb_define_method (cSSLContext , "ciphers" , ossl_sslctx_get_ciphers , 0 );
2852
2884
rb_define_method (cSSLContext , "ciphers=" , ossl_sslctx_set_ciphers , 1 );
2853
2885
rb_define_method (cSSLContext , "ciphersuites=" , ossl_sslctx_set_ciphersuites , 1 );
0 commit comments