Skip to content

Commit 3dc2047

Browse files
committed
[fix] ignore aux in X509AuxCertificate equality
1 parent b7b2009 commit 3dc2047

3 files changed

Lines changed: 52 additions & 22 deletions

File tree

‎src/main/java/org/jruby/ext/openssl/x509store/Certificate.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public boolean isName(final Name name) {
5454
public boolean matches(final X509Object other) {
5555
if (other instanceof Certificate) {
5656
final Certificate that = (Certificate) other;
57-
return this.cert.cert.hashCode() == that.cert.cert.hashCode()
58-
&& this.cert.cert.equals(that.cert.cert);
57+
return this.cert.hashCode() == that.cert.hashCode()
58+
&& this.cert.equals(that.cert);
5959
}
6060
return false;
6161
}

‎src/main/java/org/jruby/ext/openssl/x509store/X509AuxCertificate.java‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.bouncycastle.asn1.x509.Certificate;
6565
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
6666
import org.jruby.ext.openssl.SecurityHelper;
67+
import org.jruby.ext.openssl.util.ExceptionUtil;
6768

6869
/**
6970
* Since regular X509Certificate doesn't represent the Aux part of a
@@ -73,14 +74,12 @@
7374
* @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
7475
*/
7576
public class X509AuxCertificate extends X509Certificate implements Cloneable {
76-
private static final long serialVersionUID = -909543379295427515L;
7777

7878
final X509Certificate cert;
79+
final transient X509Aux aux;
7980

80-
final X509Aux aux;
81-
82-
boolean verified = false; // opt: avoids doing internal_verify twice
83-
private int ex_flags = -1;
81+
transient boolean verified; // opt: avoids doing internal_verify twice
82+
private transient int ex_flags = -1;
8483

8584
public X509AuxCertificate(Certificate wrap) throws IOException, CertificateException {
8685
super();
@@ -100,12 +99,13 @@ public X509AuxCertificate(X509Certificate wrap) {
10099
this.aux = aux;
101100
}
102101

103-
public final X509AuxCertificate clone() {
102+
@Override
103+
protected final X509AuxCertificate clone() {
104104
try {
105105
return (X509AuxCertificate) super.clone();
106106
}
107107
catch (CloneNotSupportedException ex) {
108-
throw new IllegalStateException(ex);
108+
ExceptionUtil.throwException(ex); return null;
109109
}
110110
}
111111

@@ -117,13 +117,7 @@ final X509AuxCertificate cloneForCache() {
117117
}
118118

119119
public int getExFlags() throws IOException {
120-
if (ex_flags == -1) {
121-
//try {
122-
ex_flags = computeExFlags();
123-
//} catch (IOException e) {
124-
// throw new IllegalStateException(e);
125-
//}
126-
}
120+
if (ex_flags == -1) ex_flags = computeExFlags();
127121
return ex_flags;
128122
}
129123

@@ -290,17 +284,14 @@ public byte[] getEncoded() throws CertificateEncodingException {
290284
public boolean equals(Object other) {
291285
if ( this == other ) return true;
292286
if ( other instanceof X509AuxCertificate ) {
293-
X509AuxCertificate o = (X509AuxCertificate) other;
294-
return this.cert.equals(o.cert) && ((this.aux == null) ? o.aux == null : this.aux.equals(o.aux));
287+
return this.cert.equals(((X509AuxCertificate) other).cert);
295288
}
296-
return false;
289+
return this.cert.equals(other);
297290
}
298291

299292
@Override
300293
public int hashCode() {
301-
int ret = cert.hashCode();
302-
ret += 3 * (aux == null ? 1 : aux.hashCode());
303-
return ret;
294+
return 11 * cert.hashCode();
304295
}
305296

306297
@Override

‎src/test/java/org/jruby/ext/openssl/x509store/StoreContextTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
package org.jruby.ext.openssl.x509store;
33

4+
import java.io.ByteArrayInputStream;
5+
import java.nio.charset.StandardCharsets;
6+
import java.security.cert.CertificateFactory;
7+
import java.security.cert.X509Certificate;
8+
import java.util.Collections;
49
import org.junit.jupiter.api.Test;
510

611
import static org.junit.jupiter.api.Assertions.*;
@@ -22,4 +27,38 @@ public void getExtraDataRoundTrips() {
2227
assertEquals("app", ctx.getExtraData(0));
2328
assertNull(ctx.getExtraData(3)); // beyond populated range
2429
}
30+
31+
@Test
32+
public void auxCertificateEqualityIgnoresAux() throws Exception {
33+
final String pem = "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n2htJ1srqDpUsncFVZ/ecP19GkeOZ6BmIhppcHhE5WyLBcPX\n" +
49+
"It5q1BW0PiAzT9LlEGoaiW0nw39so0Pr1whJDfc1t4fjdk+kSiMIzRHbTDvHWfpV\n" +
50+
"nTA=\n" +
51+
"-----END CERTIFICATE-----\n";
52+
final X509Certificate x509 = (X509Certificate) CertificateFactory.getInstance("X.509")
53+
.generateCertificate(new ByteArrayInputStream(pem.getBytes(StandardCharsets.US_ASCII)));
54+
final X509AuxCertificate plain = new X509AuxCertificate(x509);
55+
final X509Aux aux = new X509Aux("alias", null, Collections.singletonList("1.3.6.1.5.5.7.3.1"), null, null);
56+
final X509AuxCertificate withAux = new X509AuxCertificate(x509, aux);
57+
58+
assertEquals(plain, withAux);
59+
assertEquals(withAux, plain);
60+
assertEquals(plain.hashCode(), withAux.hashCode());
61+
assertEquals(plain, plain.cert);
62+
assertTrue(new Certificate(plain).matches(new Certificate(withAux)));
63+
}
2564
}

0 commit comments

Comments
 (0)