| 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.util; |
| 16 |
|
| 17 |
import static org.hamcrest.CoreMatchers.is; |
| 18 |
import static org.junit.Assert.assertThat; |
| 19 |
import static org.junit.Assert.assertTrue; |
| 20 |
|
| 21 |
import org.globus.gsi.testutils.FileSetupUtil; |
| 22 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 23 |
|
| 24 |
import java.io.BufferedReader; |
| 25 |
import java.io.ByteArrayInputStream; |
| 26 |
import java.io.StringReader; |
| 27 |
import java.security.GeneralSecurityException; |
| 28 |
import java.security.cert.X509CRL; |
| 29 |
import java.security.cert.X509Certificate; |
| 30 |
|
| 31 |
import org.junit.After; |
| 32 |
import org.junit.Before; |
| 33 |
import org.junit.Test; |
| 34 |
|
| 35 |
import javax.security.auth.x500.X500Principal; |
| 36 |
|
| 37 |
/** |
| 38 |
* FILL ME |
| 39 |
* |
| 40 |
* @author [email protected] |
| 41 |
*/ |
| 42 |
public class CertificateUtilTest { |
| 43 |
|
| 44 |
String validCert1 = |
| 45 |
"-----BEGIN CERTIFICATE-----\n" + |
| 46 |
"MIID+DCCAuCgAwIBAgIBKTANBgkqhkiG9w0BAQUFADB1MRMwEQYKCZImiZPyLGQB\n" + |
| 47 |
"GRYDbmV0MRIwEAYKCZImiZPyLGQBGRYCRVMxDjAMBgNVBAoTBUVTbmV0MSAwHgYD\n" + |
| 48 |
"VQ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1U+bf3ZRyFcZ4FGseebaCSEiSvjw6roSY0ZX39rpd9u\n" + |
| 79 |
"mVBb8lZu09U9aRqL\n" + |
| 80 |
"-----END CERTIFICATE-----"; |
| 81 |
|
| 82 |
String invalidCert2 = |
| 83 |
"-----BEGIN CERTIFICATE-----\n" + |
| 84 |
"MIID+DCCAuCgAwIBAgIBKTANBgkqhkiG9w0BAQUFADB1MRMwEQYKCZImiZPyLGQB\n" + |
| 85 |
"GRYDbmV0MRIwEAYKCZImiZPyLGQBGRYCRVMxDjAMBgNVBAoTBUVTbmV0MSAwHgYD\n" + |
| 86 |
"VQQLExdDZXJ0aWZpY2F0ZSBBdXRob3JpdGllczEYMBYGA1UEAxMPRVNuZXQgUm9v\n" + |
| 87 |
"dCBDQSAxMB4XDTAyMTIwNTA4MDAwMFoXDTEzMDEyNTA4MDAwMFowaTETMBEGCgmS\n" + |
| 88 |
"JomT8ixkARkWA29yZzEYMBYGCgmSJomT8ixkARkWCERPRUdyaWRzMSAwHgYDVQQL\n" + |
| 89 |
"ExdDZXJ0aWZpY2F0ZSBBdXRob3JpdGllczEWMBQGA1UEAxMNRE9FR3JpZHMgQ0Eg\n" + |
| 90 |
"MTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALT11iNho9sIPma1uJBv\n" + |
| 91 |
"sprfLWoCbRlyooIVyJZx97wrBy7L22Me4iwt/1ki12QNbjHLyy5r2cmXHcqXCO26\n" + |
| 92 |
"ZMy062DfkpkKSdR3wozhUZNIV0tUb0Bs1rJ5/vpxpUIYzX6PIXQurTeRq4Y49Nw1\n"; |
| 93 |
|
| 94 |
String invalidCrl1 = |
| 95 |
"-----BEGIN X509 CRL-----\n" + |
| 96 |
"MIIBLDCBljANBgkqhkiG9w0BAQQFADA9MREwDwYDVQQKEwh0ZXN0IENBMjESMBAG\n" + |
| 97 |
"A1UECxMJc2ltcGxlIGNhMRQwEgYDVQQDEwtHbG9idXMgVGVzdBcNMDYwNTIzMDEy\n" + |
| 98 |
"NjEwWhcNMDcwNTIzMDEyNjEwWjAoMBICAQIXDTA2MDUyMzAxMTM1MFowEgIBAxcN"; |
| 99 |
|
| 100 |
FileSetupUtil testCert1; |
| 101 |
FileSetupUtil testCert2; |
| 102 |
FileSetupUtil testCrl1; |
| 103 |
|
| 104 |
@Before |
| 105 |
public void setup() throws Exception { |
| 106 |
|
| 107 |
this.testCert1 = new FileSetupUtil("certificateUtilTest/1c3f2ca8.0"); |
| 108 |
|
| 109 |
this.testCert2 = |
| 110 |
new FileSetupUtil("certificateUtilTest/b38b4d8c-invalid.0"); |
| 111 |
|
| 112 |
this.testCrl1 = new FileSetupUtil("certificateUtilTest/validCrl.r0"); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void testReadCertificate() throws Exception { |
| 117 |
|
| 118 |
BufferedReader reader = |
| 119 |
new BufferedReader(new StringReader(this.validCert1)); |
| 120 |
X509Certificate cert = |
| 121 |
CertificateLoadUtil.readCertificate(reader); |
| 122 |
assert (cert != null); |
| 123 |
|
| 124 |
reader = new BufferedReader(new StringReader(this.invalidCert1)); |
| 125 |
|
| 126 |
boolean expected = false; |
| 127 |
try { |
| 128 |
cert = CertificateLoadUtil.readCertificate(reader); |
| 129 |
} catch (GeneralSecurityException e) { |
| 130 |
|
| 131 |
if ((e.getMessage().indexOf( |
| 132 |
"Certificate needs to start with BEGIN CERTIFICATE")) != -1) { |
| 133 |
expected = true; |
| 134 |
} |
| 135 |
} |
| 136 |
assertTrue(expected); |
| 137 |
|
| 138 |
reader = new BufferedReader(new StringReader(this.invalidCert1)); |
| 139 |
|
| 140 |
expected = false; |
| 141 |
try { |
| 142 |
cert = CertificateLoadUtil.readCertificate(reader); |
| 143 |
} catch (GeneralSecurityException e) { |
| 144 |
|
| 145 |
if ((e.getMessage().indexOf( |
| 146 |
"Certificate needs to start with BEGIN CERTIFICATE")) != -1) { |
| 147 |
expected = true; |
| 148 |
} |
| 149 |
} |
| 150 |
assertTrue(expected); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void testLoadCertificate() throws Exception { |
| 155 |
{ |
| 156 |
|
| 157 |
this.testCert1.copyFileToTemp(); |
| 158 |
|
| 159 |
X509Certificate cert = |
| 160 |
CertificateLoadUtil |
| 161 |
.loadCertificate(testCert1.getAbsoluteFilename()); |
| 162 |
|
| 163 |
assert (cert != null); |
| 164 |
|
| 165 |
this.testCert2.copyFileToTemp(); |
| 166 |
|
| 167 |
boolean worked = false; |
| 168 |
try { |
| 169 |
cert = CertificateLoadUtil |
| 170 |
.loadCertificate(testCert2.getAbsoluteFilename()); |
| 171 |
} catch (GeneralSecurityException e) { |
| 172 |
String err = e.getMessage(); |
| 173 |
if (err != null && |
| 174 |
err.indexOf("BEGIN CERTIFICATE") != -1) { |
| 175 |
worked = true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
assertTrue(worked); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void testLoadCrl() throws Exception { |
| 185 |
|
| 186 |
this.testCrl1.copyFileToTemp(); |
| 187 |
|
| 188 |
X509CRL crl = |
| 189 |
CertificateLoadUtil.loadCrl(testCrl1.getAbsoluteFilename()); |
| 190 |
|
| 191 |
assert (crl != null); |
| 192 |
|
| 193 |
ByteArrayInputStream in = |
| 194 |
new ByteArrayInputStream(this.invalidCrl1.getBytes()); |
| 195 |
|
| 196 |
boolean worked = false; |
| 197 |
try { |
| 198 |
crl = CertificateLoadUtil.loadCrl(in); |
| 199 |
} catch (GeneralSecurityException e) { |
| 200 |
worked = true; |
| 201 |
} |
| 202 |
|
| 203 |
assertTrue(worked); |
| 204 |
} |
| 205 |
|
| 206 |
@Test |
| 207 |
public void testToGlobusIdForString() |
| 208 |
{ |
| 209 |
String dn = |
| 210 |
CertificateUtil.toGlobusID("DC=org, DC=DOEGrids, OU=Certificate Authorities, CN=DOEGrids CA 1", true); |
| 211 |
assertThat(dn, is("/DC=org/DC=DOEGrids/OU=Certificate Authorities/CN=DOEGrids CA 1")); |
| 212 |
} |
| 213 |
|
| 214 |
@Test |
| 215 |
public void testToGlobusIdForReverseString() |
| 216 |
{ |
| 217 |
String dn = |
| 218 |
CertificateUtil.toGlobusID("CN=DOEGrids CA 1, OU=Certificate Authorities, DC=DOEGrids, DC=org", false); |
| 219 |
assertThat(dn, is("/DC=org/DC=DOEGrids/OU=Certificate Authorities/CN=DOEGrids CA 1")); |
| 220 |
} |
| 221 |
|
| 222 |
@Test |
| 223 |
public void testToGlobusIdForX500Principal() |
| 224 |
{ |
| 225 |
String dn = CertificateUtil.toGlobusID( |
| 226 |
new X500Principal("CN=DOEGrids CA 1, OU=Certificate Authorities, DC=DOEGrids, DC=org")); |
| 227 |
assertThat(dn, is("/DC=org/DC=DOEGrids/OU=Certificate Authorities/CN=DOEGrids CA 1")); |
| 228 |
} |
| 229 |
|
| 230 |
@Test |
| 231 |
public void testToPrincipal() |
| 232 |
{ |
| 233 |
X500Principal principal = |
| 234 |
CertificateUtil.toPrincipal("/DC=org/DC=DOEGrids/OU=Certificate Authorities/CN=DOEGrids CA 1"); |
| 235 |
assertThat(principal, is(new X500Principal( |
| 236 |
"CN=DOEGrids CA 1, OU=Certificate Authorities, DC=DOEGrids, DC=org"))); |
| 237 |
} |
| 238 |
|
| 239 |
@Test |
| 240 |
public void testToPrincipalWithSlashInAttribute() |
| 241 |
{ |
| 242 |
X500Principal principal = |
| 243 |
CertificateUtil.toPrincipal("/DC=org/DC=DOEGrids/OU=Certificate / Authorities/CN=DOEGrids CA 1"); |
| 244 |
assertThat(principal, is(new X500Principal( |
| 245 |
"CN=DOEGrids CA 1, OU=Certificate / Authorities, DC=DOEGrids, DC=org"))); |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void testToPrincipalWithEmptyAttribute() |
| 250 |
{ |
| 251 |
X500Principal principal = |
| 252 |
CertificateUtil.toPrincipal("/DC=org/DC=DOEGrids//CN=DOEGrids CA 1"); |
| 253 |
assertThat(principal, is(new X500Principal( |
| 254 |
"CN=DOEGrids CA 1, DC=DOEGrids, DC=org"))); |
| 255 |
} |
| 256 |
|
| 257 |
@Test |
| 258 |
public void testToPrincipalWithEmptyString() |
| 259 |
{ |
| 260 |
X500Principal principal = |
| 261 |
CertificateUtil.toPrincipal(""); |
| 262 |
assertThat(principal, is(new X500Principal(""))); |
| 263 |
} |
| 264 |
|
| 265 |
@Test |
| 266 |
public void testToPrincipalWithWhiteSpace() |
| 267 |
{ |
| 268 |
X500Principal principal = |
| 269 |
CertificateUtil.toPrincipal(" /DC=org/ DC=DOEGrids/OU=Certificate Authorities / CN=DOEGrids CA 1 "); |
| 270 |
assertThat(principal, is(new X500Principal( |
| 271 |
"CN=DOEGrids CA 1, OU=Certificate Authorities, DC=DOEGrids, DC=org"))); |
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void testToPrincipalWithRdnUnknownToJre() |
| 276 |
{ |
| 277 |
String dn = "/DC=org/DC=terena/DC=tcs/C=FI/PostalCode=02101/ST=Uusimaa/L=Espoo/STREET=P.O. Box " + |
| 278 |
"405/O=CSC/OU=satellite.csc.fi/CN=liuske.csc.fi"; |
| 279 |
X500Principal principal = CertificateUtil.toPrincipal(dn); |
| 280 |
String newDn = CertificateUtil.toGlobusID(principal); |
| 281 |
assertThat(newDn, is(dn)); |
| 282 |
} |
| 283 |
|
| 284 |
@Test |
| 285 |
public void testToPrincipalWithUrl() { |
| 286 |
String dn = "/C=US/ST=UT/L=Salt Lake City/O=The USERTRUST Network" |
| 287 |
+ "/OU=http://www.usertrust.com/CN=UTN-USERFirst-Client Authentication and Email"; |
| 288 |
X500Principal principal = CertificateUtil.toPrincipal(dn); |
| 289 |
String newDn = CertificateUtil.toGlobusID(principal); |
| 290 |
assertThat(newDn, is(dn)); |
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void testToPrincipalWithComma() { |
| 295 |
String dn = "/C=DE/ST=Hamburg/O=dCache.ORG/CN=Gena, Crocodile"; |
| 296 |
X500Principal principal = CertificateUtil.toPrincipal(dn); |
| 297 |
String newDn = CertificateUtil.toGlobusID(principal); |
| 298 |
assertThat(newDn, is(dn)); |
| 299 |
} |
| 300 |
|
| 301 |
@After |
| 302 |
public void tearDown() { |
| 303 |
|
| 304 |
this.testCert1.deleteFile(); |
| 305 |
this.testCert2.deleteFile(); |
| 306 |
this.testCrl1.deleteFile(); |
| 307 |
} |
| 308 |
} |