@@ -344,9 +344,7 @@ public IRubyObject setup(final ThreadContext context) {
344
344
final List <X509AuxCertificate > clientCert ;
345
345
if ( value != null && ! value .isNil () ) {
346
346
if ( value .respondsTo ("each" ) ) {
347
- final List <X509Cert > cCerts = convertToX509Certs (context , value );
348
- clientCert = new ArrayList <X509AuxCertificate >(cCerts .size ());
349
- for ( X509Cert x : cCerts ) clientCert .add ( x .getAuxCert () );
347
+ clientCert = convertToAuxCerts (context , value );
350
348
} else {
351
349
if ( ! ( value instanceof X509Cert ) ) {
352
350
throw runtime .newTypeError ("OpenSSL::X509::Certificate expected but got @client_ca = " + value .inspect ());
@@ -359,9 +357,7 @@ public IRubyObject setup(final ThreadContext context) {
359
357
value = getInstanceVariable ("@extra_chain_cert" );
360
358
final List <X509AuxCertificate > extraChainCert ;
361
359
if ( value != null && ! value .isNil () ) {
362
- final List <X509Cert > eCerts = convertToX509Certs (context , value );
363
- extraChainCert = new ArrayList <X509AuxCertificate >(eCerts .size ());
364
- for ( X509Cert x : eCerts ) extraChainCert .add ( x .getAuxCert () );
360
+ extraChainCert = convertToAuxCerts (context , value );
365
361
}
366
362
else {
367
363
extraChainCert = null ;
@@ -794,19 +790,30 @@ private long getOptions() {
794
790
return 0 ;
795
791
}
796
792
797
- private List <X509Cert > convertToX509Certs (final ThreadContext context , IRubyObject value ) {
798
- final ArrayList <X509Cert > result = new ArrayList <X509Cert >();
793
+ private static List <X509AuxCertificate > convertToAuxCerts (final ThreadContext context , IRubyObject value ) {
799
794
final RubyModule SSLContext = _SSLContext (context .runtime );
800
795
final RubyModule Certificate = _Certificate (context .runtime );
796
+ if ( value instanceof RubyArray ) {
797
+ final RubyArray val = (RubyArray ) value ;
798
+ final int size = val .size ();
799
+ final ArrayList <X509AuxCertificate > result = new ArrayList <X509AuxCertificate >(size );
800
+ for ( int i =0 ; i <size ; i ++ ) result .add ( assureCertificate (context , Certificate , val .eltInternal (i )).getAuxCert () );
801
+ return result ;
802
+ }
803
+ if ( value instanceof List ) {
804
+ final List <X509Cert > val = (List ) value ;
805
+ final int size = val .size ();
806
+ final ArrayList <X509AuxCertificate > result = new ArrayList <X509AuxCertificate >(size );
807
+ for ( int i =0 ; i <size ; i ++ ) result .add ( assureCertificate (context , Certificate , val .get (i )).getAuxCert () );
808
+ return result ;
809
+ }
810
+ // else :
811
+ final ArrayList <X509AuxCertificate > result = new ArrayList <X509AuxCertificate >();
801
812
Utils .invoke (context , value , "each" ,
802
813
CallBlock .newCallClosure (value , SSLContext , Arity .NO_ARGUMENTS , new BlockCallback () {
803
814
804
815
public IRubyObject call (ThreadContext context , IRubyObject [] args , Block block ) {
805
- final IRubyObject cert = args [0 ];
806
- if ( ! ( Certificate .isInstance (cert ) ) ) {
807
- throw context .runtime .newTypeError ("wrong argument : " + cert .inspect () + " is not a " + Certificate .getName ());
808
- }
809
- result .add ( (X509Cert ) cert );
816
+ result .add ( assureCertificate (context , Certificate , args [0 ]).getAuxCert () );
810
817
return context .nil ;
811
818
}
812
819
@@ -815,6 +822,13 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block)
815
822
return result ;
816
823
}
817
824
825
+ private static X509Cert assureCertificate (final ThreadContext context , final RubyModule Certificate , final IRubyObject cert ) {
826
+ if ( ! ( Certificate .isInstance (cert ) ) ) {
827
+ throw context .runtime .newTypeError ("wrong argument : " + cert .inspect () + " is not a " + Certificate .getName ());
828
+ }
829
+ return (X509Cert ) cert ;
830
+ }
831
+
818
832
static RubyClass _SSLContext (final Ruby runtime ) {
819
833
return (RubyClass ) _SSL (runtime ).getConstantAt ("SSLContext" );
820
834
}
0 commit comments