Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit 487be76

Browse files
committed
Some keys/x509 refactoring
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1877774 13f79535-47bb-0310-9956-ffa450edef68
1 parent 911cc6d commit 487be76

6 files changed

Lines changed: 128 additions & 16 deletions

File tree

src/main/java/org/apache/xml/security/keys/content/x509/XMLX509Certificate.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,7 @@ public X509Certificate getX509Certificate() throws XMLSecurityException {
100100
try (InputStream is = new ByteArrayInputStream(certbytes)) {
101101
CertificateFactory certFact =
102102
CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
103-
X509Certificate cert =
104-
(X509Certificate) certFact.generateCertificate(is);
105-
106-
if (cert != null) {
107-
return cert;
108-
}
109-
110-
return null;
103+
return (X509Certificate) certFact.generateCertificate(is);
111104
} catch (CertificateException | IOException ex) {
112105
throw new XMLSecurityException(ex);
113106
}

src/main/java/org/apache/xml/security/keys/content/x509/XMLX509Digest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ public byte[] getDigestBytes() throws XMLSecurityException {
113113
public static byte[] getDigestBytesFromCert(X509Certificate cert, String algorithmURI) throws XMLSecurityException {
114114
String jcaDigestAlgorithm = JCEMapper.translateURItoJCEID(algorithmURI);
115115
if (jcaDigestAlgorithm == null) {
116-
Object[] exArgs = { algorithmURI };
117-
throw new XMLSecurityException("XMLX509Digest.UnknownDigestAlgorithm", exArgs);
116+
Object[] exArgs = {algorithmURI};
117+
throw new XMLSecurityException("XMLX509Digest.UnknownDigestAlgorithm", exArgs);
118118
}
119119

120120
try {
121-
MessageDigest md = MessageDigest.getInstance(jcaDigestAlgorithm);
122-
return md.digest(cert.getEncoded());
123-
} catch (Exception e) {
124-
Object[] exArgs = { jcaDigestAlgorithm };
125-
throw new XMLSecurityException("XMLX509Digest.FailedDigest", exArgs);
126-
}
121+
MessageDigest md = MessageDigest.getInstance(jcaDigestAlgorithm);
122+
return md.digest(cert.getEncoded());
123+
} catch (Exception e) {
124+
Object[] exArgs = {jcaDigestAlgorithm};
125+
throw new XMLSecurityException("XMLX509Digest.FailedDigest", exArgs);
126+
}
127127

128128
}
129129

src/test/java/org/apache/xml/security/test/dom/keys/content/x509/XMLX509CertificateTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020

2121
import java.io.File;
2222
import java.io.FileInputStream;
23+
import java.security.cert.CertificateFactory;
24+
import java.security.cert.X509Certificate;
2325

26+
import org.apache.xml.security.test.dom.TestUtils;
2427
import org.w3c.dom.Document;
2528
import org.w3c.dom.Element;
2629
import org.w3c.dom.NodeList;
2730
import org.apache.xml.security.keys.content.x509.XMLX509Certificate;
2831
import org.apache.xml.security.utils.Constants;
2932
import org.apache.xml.security.utils.XMLUtils;
3033

34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
3136
/**
3237
* Certificate parsing test.
3338
*
@@ -54,4 +59,27 @@ public void testGetX509Certificate() throws Exception {
5459
// System.out.println(cert);
5560
}
5661

62+
@org.junit.jupiter.api.Test
63+
public void testEqualsAndHashCode() throws Exception {
64+
File f = null;
65+
if (BASEDIR != null && !"".equals(BASEDIR)) {
66+
f = new File(BASEDIR + SEP +
67+
"src/test/resources/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/certs/lugh.crt");
68+
} else {
69+
f = new File(
70+
"src/test/resources/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/certs/lugh.crt");
71+
}
72+
73+
FileInputStream fis = new FileInputStream(f);
74+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
75+
X509Certificate cert = (X509Certificate) cf.generateCertificate(fis);
76+
77+
XMLX509Certificate x509Cert1 = new XMLX509Certificate(TestUtils.newDocument(), cert);
78+
XMLX509Certificate x509Cert2 = new XMLX509Certificate(TestUtils.newDocument(), cert);
79+
80+
assertEquals(x509Cert1, x509Cert2);
81+
assertEquals(x509Cert1.hashCode(), x509Cert2.hashCode());
82+
}
83+
84+
5785
}

src/test/java/org/apache/xml/security/test/dom/keys/content/x509/XMLX509IssuerSerialTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,16 @@ public void testGetIssuerName() throws Exception {
5050
assertEquals("CN=\\#abc123", is.getIssuerName());
5151
// System.out.println(is.getIssuerName());
5252
}
53+
54+
@org.junit.jupiter.api.Test
55+
public void testEqualsHashcode() throws Exception {
56+
XMLX509IssuerSerial is1 = new XMLX509IssuerSerial(doc, "1234", 0);
57+
assertEquals("1234", is1.getIssuerName());
58+
59+
XMLX509IssuerSerial is2 = new XMLX509IssuerSerial(doc, "1234", 0);
60+
assertEquals("1234", is2.getIssuerName());
61+
62+
assertEquals(is1, is2);
63+
assertEquals(is1.hashCode(), is2.hashCode());
64+
}
5365
}

src/test/java/org/apache/xml/security/test/dom/keys/content/x509/XMLX509SKITest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import java.util.Collections;
3131

3232
import org.apache.xml.security.keys.content.x509.XMLX509SKI;
33+
import org.apache.xml.security.test.dom.TestUtils;
3334

35+
import static org.junit.jupiter.api.Assertions.assertEquals;
3436
import static org.junit.jupiter.api.Assertions.assertFalse;
37+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3538

3639
/**
3740
* Test bugfix 41892: XML Security 1.4.0 does not build with IBM's JDK
@@ -79,5 +82,15 @@ public void testGetSKIBytesFromCert() throws Exception {
7982

8083
Collection<?> certs = cs.getCertificates(xcs);
8184
assertFalse(certs.isEmpty());
85+
86+
XMLX509SKI xmlx509SKI = new XMLX509SKI(TestUtils.newDocument(), skid);
87+
assertNotNull(xmlx509SKI.getSKIBytes());
88+
89+
XMLX509SKI xmlx509SKI2 = new XMLX509SKI(TestUtils.newDocument(), cert);
90+
assertNotNull(xmlx509SKI2.getSKIBytes());
91+
92+
assertEquals(xmlx509SKI, xmlx509SKI2);
93+
assertEquals(xmlx509SKI.hashCode(), xmlx509SKI2.hashCode());
94+
8295
}
8396
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.xml.security.test.dom.keys.content.x509;
20+
21+
import java.io.File;
22+
import java.io.FileInputStream;
23+
import java.security.cert.CertificateFactory;
24+
import java.security.cert.X509Certificate;
25+
26+
import org.apache.xml.security.keys.content.x509.XMLX509SubjectName;
27+
import org.apache.xml.security.test.dom.TestUtils;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
31+
32+
/**
33+
* Certificate parsing test.
34+
*
35+
*/
36+
public class XMLX509SubjectNameTest {
37+
38+
private static final String BASEDIR =
39+
System.getProperty("basedir") == null ? "./": System.getProperty("basedir");
40+
private static final String SEP = System.getProperty("file.separator");
41+
42+
@org.junit.jupiter.api.Test
43+
public void testEqualsAndHashCode() throws Exception {
44+
File f = null;
45+
if (BASEDIR != null && !"".equals(BASEDIR)) {
46+
f = new File(BASEDIR + SEP +
47+
"src/test/resources/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/certs/lugh.crt");
48+
} else {
49+
f = new File(
50+
"src/test/resources/ie/baltimore/merlin-examples/merlin-xmldsig-twenty-three/certs/lugh.crt");
51+
}
52+
53+
FileInputStream fis = new FileInputStream(f);
54+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
55+
X509Certificate cert = (X509Certificate) cf.generateCertificate(fis);
56+
57+
XMLX509SubjectName x509SubjectName1 = new XMLX509SubjectName(TestUtils.newDocument(), cert);
58+
assertNotNull(x509SubjectName1.getSubjectName());
59+
XMLX509SubjectName x509SubjectName2 = new XMLX509SubjectName(TestUtils.newDocument(), cert);
60+
61+
assertEquals(x509SubjectName1, x509SubjectName2);
62+
assertEquals(x509SubjectName1.hashCode(), x509SubjectName2.hashCode());
63+
}
64+
65+
66+
}

0 commit comments

Comments
 (0)