選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

X509UtilsTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.security.PrivateKey;
  20. import java.security.cert.X509Certificate;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.zip.ZipEntry;
  24. import java.util.zip.ZipInputStream;
  25. import org.eclipse.jgit.util.FileUtils;
  26. import org.junit.After;
  27. import org.junit.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import com.gitblit.models.UserModel;
  31. import com.gitblit.utils.HttpUtils;
  32. import com.gitblit.utils.X509Utils;
  33. import com.gitblit.utils.X509Utils.RevocationReason;
  34. import com.gitblit.utils.X509Utils.X509Log;
  35. import com.gitblit.utils.X509Utils.X509Metadata;
  36. /**
  37. * Unit tests for X509 certificate generation.
  38. *
  39. * @author James Moger
  40. *
  41. */
  42. public class X509UtilsTest extends Assert {
  43. // passwords are case-sensitive and may be length-limited
  44. // based on the JCE policy files
  45. String caPassword = "aBcDeFg";
  46. File folder = new File(System.getProperty("user.dir"), "x509test");
  47. X509Log log = new X509Log() {
  48. public void log(String message) {
  49. System.out.println(message);
  50. }
  51. };
  52. @Before
  53. public void prepare() throws Exception {
  54. cleanUp();
  55. X509Metadata goMetadata = new X509Metadata("localhost", caPassword);
  56. X509Utils.prepareX509Infrastructure(goMetadata, folder, log);
  57. }
  58. @After
  59. public void cleanUp() throws Exception {
  60. if (folder.exists()) {
  61. FileUtils.delete(folder, FileUtils.RECURSIVE);
  62. }
  63. }
  64. @Test
  65. public void testNewCA() throws Exception {
  66. File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
  67. X509Utils.getPrivateKey(X509Utils.CA_ALIAS, storeFile, caPassword);
  68. X509Certificate cert = X509Utils.getCertificate(X509Utils.CA_ALIAS, storeFile, caPassword);
  69. assertEquals("O=Gitblit,OU=Gitblit,CN=Gitblit Certificate Authority", cert.getIssuerDN().getName());
  70. }
  71. @Test
  72. public void testCertificateUserMapping() throws Exception {
  73. File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
  74. PrivateKey caPrivateKey = X509Utils.getPrivateKey(X509Utils.CA_ALIAS, storeFile, caPassword);
  75. X509Certificate caCert = X509Utils.getCertificate(X509Utils.CA_ALIAS, storeFile, caPassword);
  76. X509Metadata userMetadata = new X509Metadata("james", "james");
  77. userMetadata.serverHostname = "www.myserver.com";
  78. userMetadata.userDisplayname = "James Moger";
  79. userMetadata.passwordHint = "your name";
  80. userMetadata.oids.put("C", "US");
  81. X509Certificate cert1 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
  82. UserModel userModel1 = HttpUtils.getUserModelFromCertificate(cert1);
  83. assertEquals(userMetadata.commonName, userModel1.username);
  84. assertEquals(userMetadata.emailAddress, userModel1.emailAddress);
  85. assertEquals("C=US,O=Gitblit,OU=Gitblit,CN=james", cert1.getSubjectDN().getName());
  86. X509Certificate cert2 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
  87. UserModel userModel2 = HttpUtils.getUserModelFromCertificate(cert2);
  88. assertEquals(userMetadata.commonName, userModel2.username);
  89. assertEquals(userMetadata.emailAddress, userModel2.emailAddress);
  90. assertEquals("C=US,O=Gitblit,OU=Gitblit,CN=james", cert2.getSubjectDN().getName());
  91. assertNotSame("Serial numbers are the same!", cert1.getSerialNumber().longValue(), cert2.getSerialNumber().longValue());
  92. }
  93. @Test
  94. public void testUserBundle() throws Exception {
  95. File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
  96. X509Metadata userMetadata = new X509Metadata("james", "james");
  97. userMetadata.serverHostname = "www.myserver.com";
  98. userMetadata.userDisplayname = "James Moger";
  99. userMetadata.passwordHint = "your name";
  100. File zip = X509Utils.newClientBundle(userMetadata, storeFile, caPassword, log);
  101. assertTrue(zip.exists());
  102. List<String> expected = Arrays.asList(
  103. userMetadata.commonName + ".pem",
  104. userMetadata.commonName + ".p12",
  105. userMetadata.commonName + ".cer",
  106. "ca.cer",
  107. "README.TXT");
  108. ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
  109. ZipEntry entry = null;
  110. while ((entry = zis.getNextEntry()) != null) {
  111. assertTrue("Unexpected file: " + entry.getName(), expected.contains(entry.getName()));
  112. }
  113. zis.close();
  114. }
  115. @Test
  116. public void testCertificateRevocation() throws Exception {
  117. File storeFile = new File(folder, X509Utils.CA_KEY_STORE);
  118. PrivateKey caPrivateKey = X509Utils.getPrivateKey(X509Utils.CA_ALIAS, storeFile, caPassword);
  119. X509Certificate caCert = X509Utils.getCertificate(X509Utils.CA_ALIAS, storeFile, caPassword);
  120. X509Metadata userMetadata = new X509Metadata("james", "james");
  121. userMetadata.serverHostname = "www.myserver.com";
  122. userMetadata.userDisplayname = "James Moger";
  123. userMetadata.passwordHint = "your name";
  124. // generate a new client certificate
  125. X509Certificate cert1 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
  126. // confirm this certificate IS NOT revoked
  127. File caRevocationList = new File(folder, X509Utils.CA_REVOCATION_LIST);
  128. assertFalse(X509Utils.isRevoked(cert1, caRevocationList));
  129. // revoke certificate and then confirm it IS revoked
  130. X509Utils.revoke(cert1, RevocationReason.ACompromise, caRevocationList, storeFile, caPassword, log);
  131. assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
  132. // generate a second certificate
  133. X509Certificate cert2 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
  134. // confirm second certificate IS NOT revoked
  135. assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
  136. assertFalse(X509Utils.isRevoked(cert2, caRevocationList));
  137. // revoke second certificate and then confirm it IS revoked
  138. X509Utils.revoke(cert2, RevocationReason.ACompromise, caRevocationList, caPrivateKey, log);
  139. assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
  140. assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
  141. // generate a third certificate
  142. X509Certificate cert3 = X509Utils.newClientCertificate(userMetadata, caPrivateKey, caCert, storeFile.getParentFile());
  143. // confirm third certificate IS NOT revoked
  144. assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
  145. assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
  146. assertFalse(X509Utils.isRevoked(cert3, caRevocationList));
  147. // revoke third certificate and then confirm it IS revoked
  148. X509Utils.revoke(cert3, RevocationReason.ACompromise, caRevocationList, caPrivateKey, log);
  149. assertTrue(X509Utils.isRevoked(cert1, caRevocationList));
  150. assertTrue(X509Utils.isRevoked(cert2, caRevocationList));
  151. assertTrue(X509Utils.isRevoked(cert3, caRevocationList));
  152. }
  153. }