Skip to content

Commit ba0ccb6

Browse files
committed
make sure X509Object list is synchronized properly
Sponsored by Lookout Inc.
1 parent 753ce6d commit ba0ccb6

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/main/java/org/jruby/ext/openssl/x509store/Lookup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ else if ( type == X509_LU_CRL ) {
596596
}
597597
X509Object tmp = null;
598598
synchronized ( CRYPTO_LOCK_X509_STORE ) {
599-
for ( X509Object obj : lookup.store.objects ) {
599+
for ( X509Object obj : lookup.store.getObjects() ) {
600600
if ( obj.type() == type && obj.isName(name) ) {
601601
tmp = obj; break;
602602
}

src/main/java/org/jruby/ext/openssl/x509store/Store.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public int call(StoreContext context) {
114114

115115
@Deprecated int cache = 1; // not-used
116116

117-
final List<X509Object> objects;
117+
private final List<X509Object> objects;
118118
private final List<Lookup> certificateMethods;
119119

120120
public final VerifyParameter verifyParameter;
@@ -375,13 +375,15 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) {
375375

376376
@Override
377377
public X509Certificate[] getAcceptedIssuers() {
378-
ArrayList<X509Certificate> issuers = new ArrayList<X509Certificate>(objects.size());
379-
for ( X509Object object : objects ) {
380-
if ( object instanceof Certificate ) {
381-
issuers.add( ( (Certificate) object ).x509 );
378+
synchronized(CRYPTO_LOCK_X509_STORE) {
379+
ArrayList<X509Certificate> issuers = new ArrayList<X509Certificate>(objects.size());
380+
for ( X509Object object : objects ) {
381+
if ( object instanceof Certificate ) {
382+
issuers.add( ( (Certificate) object ).x509 );
383+
}
382384
}
385+
return issuers.toArray( new X509Certificate[ issuers.size() ] );
383386
}
384-
return issuers.toArray( new X509Certificate[ issuers.size() ] );
385387
}
386388

387389
}// X509_STORE

src/main/java/org/jruby/ext/openssl/x509store/StoreContext.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,24 @@ else if ( ok != X509Utils.X509_LU_FAIL ) {
150150
return 1;
151151
}
152152

153-
int idx = X509Object.indexBySubject(store.objects, X509Utils.X509_LU_X509, xn);
154-
if ( idx == -1 ) return 0;
155-
156-
/* Look through all matching certificates for a suitable issuer */
157-
for ( int i = idx; i < store.objects.size(); i++ ) {
158-
final X509Object pobj = store.objects.get(i);
159-
if ( pobj.type() != X509Utils.X509_LU_X509 ) {
160-
return 0;
161-
}
162-
final X509AuxCertificate x509 = ((Certificate) pobj).x509;
163-
if ( ! xn.equalTo( x509.getSubjectX500Principal() ) ) {
164-
return 0;
165-
}
166-
if ( checkIssued.call(this, x, x509) != 0 ) {
167-
issuers[0] = x509;
168-
return 1;
153+
synchronized(X509Utils.CRYPTO_LOCK_X509_STORE) {
154+
int idx = X509Object.indexBySubject(store.getObjects(), X509Utils.X509_LU_X509, xn);
155+
if ( idx == -1 ) return 0;
156+
157+
/* Look through all matching certificates for a suitable issuer */
158+
for ( int i = idx; i < store.getObjects().size(); i++ ) {
159+
final X509Object pobj = store.getObjects().get(i);
160+
if ( pobj.type() != X509Utils.X509_LU_X509 ) {
161+
return 0;
162+
}
163+
final X509AuxCertificate x509 = ((Certificate) pobj).x509;
164+
if ( ! xn.equalTo( x509.getSubjectX500Principal() ) ) {
165+
return 0;
166+
}
167+
if ( checkIssued.call(this, x, x509) != 0 ) {
168+
issuers[0] = x509;
169+
return 1;
170+
}
169171
}
170172
}
171173
return 0;
@@ -605,9 +607,9 @@ public int setDefault(String name) {
605607
public int getBySubject(int type,Name name,X509Object[] ret) throws Exception {
606608
Store c = store;
607609

608-
X509Object tmp = X509Object.retrieveBySubject(c.objects,type,name);
609-
if ( tmp == null ) {
610-
synchronized(X509Utils.CRYPTO_LOCK_X509_STORE) {
610+
synchronized(X509Utils.CRYPTO_LOCK_X509_STORE) {
611+
X509Object tmp = X509Object.retrieveBySubject(c.getObjects(),type,name);
612+
if ( tmp == null ) {
611613
for(int i=currentMethod; i<c.getCertificateMethods().size(); i++) {
612614
Lookup lu = c.getCertificateMethods().get(i);
613615
X509Object[] stmp = new X509Object[1];
@@ -625,8 +627,8 @@ else if( j > 0 ) {
625627
currentMethod = 0;
626628

627629
if ( tmp == null ) return 0;
630+
ret[0] = tmp;
628631
}
629-
ret[0] = tmp;
630632
return 1;
631633
}
632634

0 commit comments

Comments
 (0)