@@ -344,9 +344,7 @@ public IRubyObject setup(final ThreadContext context) {
344344 final List <X509AuxCertificate > clientCert ;
345345 if ( value != null && ! value .isNil () ) {
346346 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 );
350348 } else {
351349 if ( ! ( value instanceof X509Cert ) ) {
352350 throw runtime .newTypeError ("OpenSSL::X509::Certificate expected but got @client_ca = " + value .inspect ());
@@ -359,9 +357,7 @@ public IRubyObject setup(final ThreadContext context) {
359357 value = getInstanceVariable ("@extra_chain_cert" );
360358 final List <X509AuxCertificate > extraChainCert ;
361359 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 );
365361 }
366362 else {
367363 extraChainCert = null ;
@@ -794,19 +790,30 @@ private long getOptions() {
794790 return 0 ;
795791 }
796792
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 ) {
799794 final RubyModule SSLContext = _SSLContext (context .runtime );
800795 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 >();
801812 Utils .invoke (context , value , "each" ,
802813 CallBlock .newCallClosure (value , SSLContext , Arity .NO_ARGUMENTS , new BlockCallback () {
803814
804815 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 () );
810817 return context .nil ;
811818 }
812819
@@ -815,6 +822,13 @@ public IRubyObject call(ThreadContext context, IRubyObject[] args, Block block)
815822 return result ;
816823 }
817824
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+
818832 static RubyClass _SSLContext (final Ruby runtime ) {
819833 return (RubyClass ) _SSL (runtime ).getConstantAt ("SSLContext" );
820834 }
0 commit comments