42
42
import java .security .cert .TrustAnchor ;
43
43
import java .security .cert .X509Certificate ;
44
44
import java .util .ArrayList ;
45
- import java .util .Collection ;
46
- import java .util .Iterator ;
45
+ import java .util .Arrays ;
46
+ import java .util .LinkedHashMap ;
47
+ import java .util .Map ;
47
48
48
49
import org .jruby .Ruby ;
49
50
import org .jruby .ext .openssl .OpenSSL ;
@@ -333,8 +334,8 @@ public int loadDefaultJavaCACertsFile(String certsFile) throws IOException, Gene
333
334
final FileInputStream fin = new FileInputStream (certsFile );
334
335
int count = 0 ;
335
336
try {
336
- // hardcode the keystore type, as we expect cacerts to be a java
337
- // keystore - especially needed for jdk9
337
+ // hardcode the keystore type, as we expect cacerts to be a java keystore
338
+ // especially needed since Java 9 (getDefaultType on 11/13 is "pkcs12")
338
339
KeyStore keystore = SecurityHelper .getKeyStore ("jks" );
339
340
// null password - as the cacerts file isn't password protected
340
341
keystore .load (fin , null );
@@ -466,13 +467,13 @@ public int call(final Lookup ctx, final Integer cmd, final String argp, final Nu
466
467
case X509_L_FILE_LOAD :
467
468
if (arglInt == X509_FILETYPE_DEFAULT ) {
468
469
try {
469
- file = ctx .envEntry ( getDefaultCertificateFileEnvironment () ); // ENV['SSL_CERT_FILE']
470
+ file = ctx .envEntry (X509_CERT_FILE_EVP ); // ENV['SSL_CERT_FILE']
470
471
}
471
472
catch (RuntimeException e ) {
472
- OpenSSL .debug (ctx .runtime , "failed to read SSL_CERT_FILE" , e );
473
+ OpenSSL .debug (ctx .runtime , "failed to read env " + X509_CERT_FILE_EVP , e );
473
474
}
474
475
if (file == null ) {
475
- file = X509Utils . X509_CERT_FILE .replace ('/' , File .separatorChar );
476
+ file = X509_CERT_FILE .replace ('/' , File .separatorChar );
476
477
}
477
478
if (file .matches (".*\\ .(crt|cer|pem)$" )) {
478
479
ok = ctx .loadCertificateOrCRLFile (file , X509_FILETYPE_PEM ) != 0 ? 1 : 0 ;
@@ -498,19 +499,16 @@ public int call(final Lookup ctx, final Integer cmd, final String argp, final Nu
498
499
* c: BY_DIR, lookup_dir_st
499
500
*/
500
501
private static class LookupDir {
501
- Collection < String > dirs ;
502
- Collection < Integer > dirsType ;
502
+ String [] dirs ;
503
+ int [] dirsType ;
503
504
}
504
505
505
506
/**
506
507
* c: new_dir
507
508
*/
508
509
private static class NewLookupDir implements LookupMethod .NewItemFunction {
509
510
public int call (final Lookup lookup ) {
510
- final LookupDir lookupDir = new LookupDir ();
511
- lookupDir .dirs = new ArrayList <String >();
512
- lookupDir .dirsType = new ArrayList <Integer >();
513
- lookup .methodData = lookupDir ;
511
+ lookup .methodData = new LookupDir ();
514
512
return 1 ;
515
513
}
516
514
}
@@ -535,7 +533,7 @@ private static class LookupDirControl implements LookupMethod.ControlFunction {
535
533
536
534
public int call (final Lookup ctx , final Integer cmd , String argp , Number argl , String [] retp ) {
537
535
int ret = 0 ;
538
- final LookupDir lookupData = (LookupDir ) ctx .methodData ;
536
+ final LookupDir lookupDir = (LookupDir ) ctx .methodData ;
539
537
switch ( cmd ) {
540
538
case X509_L_ADD_DIR :
541
539
if ( argl .intValue () == X509_FILETYPE_DEFAULT ) {
@@ -546,16 +544,16 @@ public int call(final Lookup ctx, final Integer cmd, String argp, Number argl, S
546
544
catch (RuntimeException e ) { }
547
545
548
546
if ( certDir != null ) {
549
- ret = addCertificateDirectory (lookupData , certDir , X509_FILETYPE_PEM );
547
+ ret = addCertificateDirectory (lookupDir , certDir , X509_FILETYPE_PEM );
550
548
} else {
551
- ret = addCertificateDirectory (lookupData , X509_CERT_DIR , X509_FILETYPE_PEM );
549
+ ret = addCertificateDirectory (lookupDir , X509_CERT_DIR , X509_FILETYPE_PEM );
552
550
}
553
551
if ( ret == 0 ) {
554
552
X509Error .addError (X509_R_LOADING_CERT_DIR );
555
553
}
556
554
}
557
555
else {
558
- ret = addCertificateDirectory (lookupData , argp , argl .intValue ());
556
+ ret = addCertificateDirectory (lookupDir , argp , argl .intValue ());
559
557
}
560
558
break ;
561
559
}
@@ -575,19 +573,20 @@ private int addCertificateDirectory(final LookupDir ctx, final String dir, final
575
573
return 0 ;
576
574
}
577
575
578
- String [] dirs = dir .split (File .pathSeparator );
576
+ final String [] dirs = dir .split (File .pathSeparator );
579
577
580
- for ( int i =0 ; i <dirs .length ; i ++ ) {
581
- if ( dirs [i ].length () == 0 ) {
582
- continue ;
583
- }
584
- if ( ctx .dirs .contains (dirs [i ]) ) {
585
- continue ;
586
- }
587
- ctx .dirsType .add (type );
588
- ctx .dirs .add (dirs [i ]);
578
+ final Map <String , ?> ctxDirs = new LinkedHashMap <String , Object >(dirs .length );
579
+ if ( ctx .dirs != null ) for (String d : ctx .dirs ) ctxDirs .put (d , null );
580
+
581
+ for ( int i = 0 ; i < dirs .length ; i ++ ) {
582
+ if ( dirs [i ].isEmpty () ) continue ;
583
+ ctxDirs .put (dirs [i ], null ); // if key exists no-op
589
584
}
590
585
586
+ final int len = ctxDirs .size ();
587
+ ctx .dirs = ctxDirs .keySet ().toArray (new String [len ]);
588
+ Arrays .fill (ctx .dirsType = new int [len ], type );
589
+
591
590
return 1 ;
592
591
}
593
592
}
@@ -618,10 +617,9 @@ else if ( type == X509_LU_CRL ) {
618
617
final String hash = String .format ("%08x" , name .hash ());
619
618
final StringBuilder buffer = new StringBuilder (48 );
620
619
621
- final Iterator <Integer > iter = context .dirsType .iterator ();
622
-
623
- for ( final String dir : context .dirs ) {
624
- final int dirType = iter .next ();
620
+ for ( int i = 0 ; i < context .dirs .length ; i ++ ) {
621
+ final String dir = context .dirs [i ];
622
+ final int dirType = context .dirsType [i ];
625
623
for ( int k = 0 ; ; k ++ ) {
626
624
buffer .setLength (0 ); // reset - clear buffer
627
625
buffer .append (dir ).append (File .separatorChar );
0 commit comments