Skip to content

Commit affc285

Browse files
committedNov 2, 2017
Replace incoming certs with newer versions from trust store.
Fixes jruby/jruby#4802
1 parent ec7b8e7 commit affc285

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

Diff for: ‎src/main/java/org/jruby/ext/openssl/x509store/StoreContext.java

+20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package org.jruby.ext.openssl.x509store;
2929

3030
import java.security.GeneralSecurityException;
31+
import java.security.Principal;
3132
import java.security.PublicKey;
3233
import java.security.cert.X509CRL;
3334
import java.security.cert.X509Certificate;
@@ -38,13 +39,16 @@
3839
import java.util.Date;
3940
import java.util.HashSet;
4041
import java.util.List;
42+
import java.util.ListIterator;
4143
import java.util.Set;
4244

4345
import org.bouncycastle.asn1.ASN1InputStream;
4446
import org.bouncycastle.asn1.ASN1Integer;
4547
import org.bouncycastle.asn1.ASN1Sequence;
4648
import org.jruby.ext.openssl.SecurityHelper;
4749

50+
import javax.security.auth.x500.X500Principal;
51+
4852
/**
4953
* c: X509_STORE_CTX
5054
*
@@ -661,6 +665,22 @@ public int verifyCertificate() throws Exception {
661665
List<X509AuxCertificate> sktmp = null;
662666
if ( untrusted != null ) {
663667
sktmp = new ArrayList<X509AuxCertificate>(untrusted);
668+
669+
// replace certs in untrusted with trusted versions if found
670+
X509Object[] objTmp = {null};
671+
for (ListIterator<X509AuxCertificate> iter = sktmp.listIterator(); iter.hasNext();) {
672+
X509AuxCertificate skCert = iter.next();
673+
X500Principal principal = skCert.cert.getSubjectX500Principal();
674+
int ok = getBySubject(X509Utils.X509_LU_X509, new Name(principal), objTmp);
675+
if (ok == X509Utils.X509_LU_X509) {
676+
// replace old with new and clear rest of untrusted
677+
iter.set(((Certificate) objTmp[0]).x509);
678+
while (iter.hasNext()) {
679+
iter.next();
680+
iter.remove();
681+
}
682+
}
683+
}
664684
}
665685
num = chain.size();
666686
x = chain.get(num - 1);

0 commit comments

Comments
 (0)
Please sign in to comment.