| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.gsi.bc; |
| 16 |
|
| 17 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 18 |
|
| 19 |
import java.io.InputStream; |
| 20 |
import org.globus.gsi.proxy.ProxyPathValidatorTest; |
| 21 |
import java.io.ByteArrayInputStream; |
| 22 |
import java.security.cert.X509Certificate; |
| 23 |
import java.security.cert.CertificateException; |
| 24 |
import org.globus.gsi.GSIConstants; |
| 25 |
import org.globus.gsi.TrustedCertificates; |
| 26 |
import org.globus.gsi.bc.BouncyCastleUtil; |
| 27 |
import junit.framework.TestCase; |
| 28 |
|
| 29 |
public class BouncyCastleUtilTest extends TestCase { |
| 30 |
|
| 31 |
static String [] badCerts = ProxyPathValidatorTest.badCerts; |
| 32 |
|
| 33 |
private X509Certificate getCertificate(int i) throws Exception { |
| 34 |
ClassLoader loader = ProxyPathValidatorTest.class.getClassLoader(); |
| 35 |
String name = ProxyPathValidatorTest.BASE + ProxyPathValidatorTest.certs[i][1]; |
| 36 |
InputStream in = loader.getResourceAsStream(name); |
| 37 |
if (in == null) { |
| 38 |
throw new Exception("Unable to load: " + name); |
| 39 |
} |
| 40 |
return CertificateLoadUtil.loadCertificate(in); |
| 41 |
} |
| 42 |
|
| 43 |
public void testGetCertificateType() throws Exception { |
| 44 |
for (int i=0;i<ProxyPathValidatorTest.certs.length;i++) { |
| 45 |
X509Certificate cert = getCertificate(i); |
| 46 |
String type = ProxyPathValidatorTest.certs[i][0]; |
| 47 |
assertEquals(type, BouncyCastleUtil.getCertificateType(cert).name()); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public void testGetCertificateType2() throws Exception { |
| 52 |
for (int i=0;i<badCerts.length;i++) { |
| 53 |
X509Certificate cert = CertificateLoadUtil.loadCertificate(new ByteArrayInputStream(badCerts[i].getBytes())); |
| 54 |
try { |
| 55 |
BouncyCastleUtil.getCertificateType(cert); |
| 56 |
fail("proxy verification did not fail as expected"); |
| 57 |
} catch (CertificateException e) { |
| 58 |
// ignore |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public void testGetCertificateType3() throws Exception { |
| 64 |
X509Certificate cert = getCertificate(1); |
| 65 |
assertEquals(GSIConstants.CertificateType.EEC, BouncyCastleUtil.getCertificateType(cert)); |
| 66 |
|
| 67 |
TrustedCertificates trustedCerts = |
| 68 |
new TrustedCertificates(new X509Certificate[] {cert}); |
| 69 |
assertEquals(GSIConstants.CertificateType.CA, BouncyCastleUtil.getCertificateType(cert, trustedCerts)); |
| 70 |
} |
| 71 |
|
| 72 |
public void testGetGsi2IdentityCertificate() throws Exception { |
| 73 |
X509Certificate [] goodCertsArr = ProxyPathValidatorTest.initCerts(); |
| 74 |
|
| 75 |
X509Certificate [] chain = null; |
| 76 |
|
| 77 |
// EEC, CA |
| 78 |
chain = new X509Certificate[] {goodCertsArr[1], goodCertsArr[0]}; |
| 79 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 80 |
|
| 81 |
// proxy, EEC, CA |
| 82 |
chain = new X509Certificate[] {goodCertsArr[2], goodCertsArr[1], goodCertsArr[0]}; |
| 83 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 84 |
|
| 85 |
// limited proxy, EEC, CA |
| 86 |
chain = new X509Certificate[] {goodCertsArr[3], goodCertsArr[1], goodCertsArr[0]}; |
| 87 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 88 |
|
| 89 |
// double limited proxy, limited proxy, EEC, CA |
| 90 |
chain = new X509Certificate[] {goodCertsArr[4], goodCertsArr[3], |
| 91 |
goodCertsArr[1], goodCertsArr[0]}; |
| 92 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 93 |
} |
| 94 |
|
| 95 |
public void testValidateGsi3PathGood() throws Exception { |
| 96 |
X509Certificate [] goodCertsArr = ProxyPathValidatorTest.initCerts(); |
| 97 |
|
| 98 |
X509Certificate [] chain = null; |
| 99 |
|
| 100 |
// GSI 3 PC impersonation, EEC, CA |
| 101 |
chain = new X509Certificate[] {goodCertsArr[5], goodCertsArr[1], goodCertsArr[0]}; |
| 102 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 103 |
|
| 104 |
// GSI 3 PC independent, EEC, CA |
| 105 |
chain = new X509Certificate[] {goodCertsArr[6], goodCertsArr[1], goodCertsArr[0]}; |
| 106 |
assertEquals(goodCertsArr[6], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 107 |
|
| 108 |
// GSI 3 PC imperson limited, EEC, CA |
| 109 |
chain = new X509Certificate[] {goodCertsArr[7], goodCertsArr[1], goodCertsArr[0]}; |
| 110 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 111 |
|
| 112 |
// GSI 3 PC restricted, EEC, CA |
| 113 |
chain = new X509Certificate[] {goodCertsArr[8], goodCertsArr[1], goodCertsArr[0]}; |
| 114 |
assertEquals(goodCertsArr[8], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 115 |
|
| 116 |
// GSI 3 PC impersonation, GSI 3 PC limited impersonation, EEC, CA |
| 117 |
chain = new X509Certificate[] {goodCertsArr[9], goodCertsArr[7], |
| 118 |
goodCertsArr[1], goodCertsArr[0]}; |
| 119 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 120 |
|
| 121 |
// GSI 3 PC impersonation, GSI 3 PC impersonation, EEC, CA |
| 122 |
chain = new X509Certificate[] {goodCertsArr[9], goodCertsArr[5], |
| 123 |
goodCertsArr[1], goodCertsArr[0]}; |
| 124 |
assertEquals(goodCertsArr[1], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 125 |
|
| 126 |
// GSI 3 PC indepedent, GSI 3 PC independent, EEC, CA |
| 127 |
chain = new X509Certificate[] {goodCertsArr[10], goodCertsArr[6], |
| 128 |
goodCertsArr[1], goodCertsArr[0]}; |
| 129 |
assertEquals(goodCertsArr[10], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 130 |
|
| 131 |
// GSI 3 PC impersonation, GSI 3 PC independent, EEC, CA |
| 132 |
chain = new X509Certificate[] {goodCertsArr[9], goodCertsArr[6], |
| 133 |
goodCertsArr[1], goodCertsArr[0]}; |
| 134 |
assertEquals(goodCertsArr[6], BouncyCastleUtil.getIdentityCertificate(chain)); |
| 135 |
} |
| 136 |
} |
| 137 |
|