|
| 1 | +package org.cloudfoundry.credhub.integration; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.security.cert.CertificateFactory; |
| 5 | +import java.security.cert.X509Certificate; |
| 6 | +import java.time.temporal.ChronoUnit; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Calendar; |
| 9 | + |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.test.context.ActiveProfiles; |
| 13 | +import org.springframework.test.context.TestPropertySource; |
| 14 | +import org.springframework.test.context.junit4.SpringRunner; |
| 15 | +import org.springframework.test.web.servlet.MockMvc; |
| 16 | +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; |
| 17 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 18 | +import org.springframework.transaction.annotation.Transactional; |
| 19 | +import org.springframework.web.context.WebApplicationContext; |
| 20 | + |
| 21 | +import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier; |
| 22 | +import org.bouncycastle.asn1.x509.Extension; |
| 23 | +import org.bouncycastle.asn1.x509.KeyUsage; |
| 24 | +import org.bouncycastle.asn1.x509.SubjectKeyIdentifier; |
| 25 | +import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; |
| 26 | +import org.cloudfoundry.credhub.CredhubTestApp; |
| 27 | +import org.cloudfoundry.credhub.utils.BouncyCastleFipsConfigurer; |
| 28 | +import org.cloudfoundry.credhub.utils.DatabaseProfileResolver; |
| 29 | +import org.json.JSONObject; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.rules.Timeout; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | + |
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 38 | +import static org.bouncycastle.asn1.x509.KeyUsage.cRLSign; |
| 39 | +import static org.bouncycastle.asn1.x509.KeyUsage.keyCertSign; |
| 40 | +import static org.cloudfoundry.credhub.utils.AuthConstants.ALL_PERMISSIONS_TOKEN; |
| 41 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 42 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 43 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 44 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 45 | +import static org.springframework.http.MediaType.APPLICATION_JSON; |
| 46 | +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; |
| 47 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 48 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 49 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 50 | + |
| 51 | +@RunWith(SpringRunner.class) |
| 52 | +@ActiveProfiles( |
| 53 | + value = { |
| 54 | + "unit-test", |
| 55 | + "unit-test-permissions", |
| 56 | + }, |
| 57 | + resolver = DatabaseProfileResolver.class |
| 58 | +) |
| 59 | +@SpringBootTest(classes = CredhubTestApp.class) |
| 60 | +@TestPropertySource(properties = "certificates.enable_default_ca_key_usages=true") |
| 61 | +@Transactional |
| 62 | +public class CertificateGenerateWitDefaultKeyUsagesTest { |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private WebApplicationContext webApplicationContext; |
| 66 | + private MockMvc mockMvc; |
| 67 | + |
| 68 | + @Rule |
| 69 | + public Timeout globalTimeout = Timeout.seconds(60); |
| 70 | + |
| 71 | + @BeforeClass |
| 72 | + public static void beforeAll() { |
| 73 | + BouncyCastleFipsConfigurer.configure(); |
| 74 | + } |
| 75 | + |
| 76 | + @Before |
| 77 | + public void beforeEach() throws Exception { |
| 78 | + mockMvc = MockMvcBuilders |
| 79 | + .webAppContextSetup(webApplicationContext) |
| 80 | + .apply(springSecurity()) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void certificateGeneration_shouldGenerateCorrectCertificate() throws Exception { |
| 86 | + final MockHttpServletRequestBuilder caPost = post("/api/v1/data") |
| 87 | + .header("Authorization", "Bearer " + ALL_PERMISSIONS_TOKEN) |
| 88 | + .accept(APPLICATION_JSON) |
| 89 | + .contentType(APPLICATION_JSON) |
| 90 | + //language=JSON |
| 91 | + .content("{\n" |
| 92 | + + " \"name\" : \"picard\",\n" |
| 93 | + + " \"type\" : \"certificate\",\n" |
| 94 | + + " \"parameters\" : {\n" |
| 95 | + + " \"common_name\" : \"federation\",\n" |
| 96 | + + " \"is_ca\" : true,\n" |
| 97 | + + " \"duration\" : 1 \n" |
| 98 | + + " }\n" |
| 99 | + + "}"); |
| 100 | + |
| 101 | + final String caResult = mockMvc.perform(caPost) |
| 102 | + .andDo(print()) |
| 103 | + .andExpect(status().isOk()) |
| 104 | + .andReturn().getResponse().getContentAsString(); |
| 105 | + |
| 106 | + JSONObject result = new JSONObject(caResult); |
| 107 | + final String picardCert = result.getJSONObject("value").getString("certificate"); |
| 108 | + final String picardCA = result.getJSONObject("value").getString("ca"); |
| 109 | + assertThat(picardCert, equalTo(picardCA)); |
| 110 | + |
| 111 | + final String expiryDate = result.getString("expiry_date"); |
| 112 | + final String truncatedExpiryDate = expiryDate.substring(0, expiryDate.indexOf('T')); |
| 113 | + |
| 114 | + final Calendar calendar = Calendar.getInstance(); |
| 115 | + calendar.add(Calendar.DATE, 1); |
| 116 | + final String expectedTime = calendar.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toString(); |
| 117 | + final String truncatedExpected = expectedTime.substring(0, expectedTime.indexOf('T')); |
| 118 | + |
| 119 | + |
| 120 | + assertThat(truncatedExpiryDate, equalTo(truncatedExpected)); |
| 121 | + assertThat(result.getBoolean("certificate_authority"), equalTo(true)); |
| 122 | + assertThat(result.getBoolean("self_signed"), equalTo(true)); |
| 123 | + assertThat(result.getBoolean("generated"), equalTo(true)); |
| 124 | + assertThat(result.getBoolean("duration_overridden"), equalTo(false)); |
| 125 | + assertThat(result.getInt("duration_used"), equalTo(1)); |
| 126 | + |
| 127 | + assertThat(picardCert, notNullValue()); |
| 128 | + |
| 129 | + final MockHttpServletRequestBuilder certPost = post("/api/v1/data") |
| 130 | + .header("Authorization", "Bearer " + ALL_PERMISSIONS_TOKEN) |
| 131 | + .accept(APPLICATION_JSON) |
| 132 | + .contentType(APPLICATION_JSON) |
| 133 | + //language=JSON |
| 134 | + .content("{\n" |
| 135 | + + " \"name\" : \"riker\",\n" |
| 136 | + + " \"type\" : \"certificate\",\n" |
| 137 | + + " \"parameters\" : {\n" |
| 138 | + + " \"common_name\" : \"federation\",\n" |
| 139 | + + " \"ca\" : \"picard\"\n" |
| 140 | + + " }\n" |
| 141 | + + "}"); |
| 142 | + |
| 143 | + final String certResult = mockMvc.perform(certPost) |
| 144 | + .andDo(print()) |
| 145 | + .andExpect(status().isOk()) |
| 146 | + .andReturn().getResponse().getContentAsString(); |
| 147 | + |
| 148 | + final String certCa = (new JSONObject(certResult)).getJSONObject("value").getString("ca"); |
| 149 | + final String cert = (new JSONObject(certResult)).getJSONObject("value").getString("certificate"); |
| 150 | + |
| 151 | + assertThat(certCa, equalTo(picardCert)); |
| 152 | + |
| 153 | + |
| 154 | + final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| 155 | + final X509Certificate caPem = (X509Certificate) certificateFactory |
| 156 | + .generateCertificate(new ByteArrayInputStream(picardCert.getBytes(UTF_8))); |
| 157 | + |
| 158 | + final X509Certificate certPem = (X509Certificate) certificateFactory |
| 159 | + .generateCertificate(new ByteArrayInputStream(cert.getBytes(UTF_8))); |
| 160 | + |
| 161 | + final byte[] subjectKeyIdDer = caPem.getExtensionValue(Extension.subjectKeyIdentifier.getId()); |
| 162 | + final SubjectKeyIdentifier subjectKeyIdentifier = SubjectKeyIdentifier.getInstance(JcaX509ExtensionUtils.parseExtensionValue(subjectKeyIdDer)); |
| 163 | + final byte[] subjectKeyId = subjectKeyIdentifier.getKeyIdentifier(); |
| 164 | + |
| 165 | + final byte[] authorityKeyIdDer = certPem.getExtensionValue(Extension.authorityKeyIdentifier.getId()); |
| 166 | + final AuthorityKeyIdentifier authorityKeyIdentifier = AuthorityKeyIdentifier.getInstance(JcaX509ExtensionUtils.parseExtensionValue(authorityKeyIdDer)); |
| 167 | + final byte[] authKeyId = authorityKeyIdentifier.getKeyIdentifier(); |
| 168 | + |
| 169 | + assertThat(subjectKeyId, equalTo(authKeyId)); |
| 170 | + |
| 171 | + final byte[] generatedKeyUsageCA = caPem.getExtensionValue(Extension.keyUsage.getId()); |
| 172 | + assertThat(generatedKeyUsageCA, notNullValue()); |
| 173 | + assertThat(Arrays.copyOfRange(generatedKeyUsageCA, 5, generatedKeyUsageCA.length), equalTo(new KeyUsage(keyCertSign | cRLSign).getBytes())); |
| 174 | + |
| 175 | + final byte[] generatedKeyUsageCert = certPem.getExtensionValue(Extension.keyUsage.getId()); |
| 176 | + assertThat(generatedKeyUsageCert, nullValue()); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments