From ec1fde9dc1e03cf83678f194b29c4c6b437a0e84 Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 20 Nov 2023 14:36:44 +0200 Subject: [PATCH] BAEL-6014: Check certificate name and alias in keystore file --- .../KeystoreCertificateNameAliasUnitTest.java | 50 ++++++++++++++++++ .../baeldung/keystorealias/my-keystore.jks | Bin 0 -> 2764 bytes 2 files changed, 50 insertions(+) create mode 100644 core-java-modules/core-java-security-4/src/test/java/com/baeldung/keystorealias/KeystoreCertificateNameAliasUnitTest.java create mode 100644 core-java-modules/core-java-security-4/src/test/resources/com/baeldung/keystorealias/my-keystore.jks diff --git a/core-java-modules/core-java-security-4/src/test/java/com/baeldung/keystorealias/KeystoreCertificateNameAliasUnitTest.java b/core-java-modules/core-java-security-4/src/test/java/com/baeldung/keystorealias/KeystoreCertificateNameAliasUnitTest.java new file mode 100644 index 0000000000..47bc4c3425 --- /dev/null +++ b/core-java-modules/core-java-security-4/src/test/java/com/baeldung/keystorealias/KeystoreCertificateNameAliasUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.keystorealias; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.security.KeyStore; +import java.security.cert.X509Certificate; + +import org.junit.jupiter.api.Test; + +public class KeystoreCertificateNameAliasUnitTest { + private static final String KEYSTORE_FILE = "my-keystore.jks"; + private static final String KEYSTORE_PWD = "storepw@1"; + private static final String KEYSTORE_ALIAS = "baeldung"; + + private KeyStore readKeyStore() throws Exception { + KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); + keystore.load(getClass().getResourceAsStream(KEYSTORE_FILE), KEYSTORE_PWD.toCharArray()); + return keystore; + } + + @Test + void whenCheckingAliasAndName_thenMatchIsFound() throws Exception { + KeyStore keystore = readKeyStore(); + + assertThat(keystore.containsAlias(KEYSTORE_ALIAS)).isTrue(); + + X509Certificate x509Certificate = (X509Certificate) keystore.getCertificate(KEYSTORE_ALIAS); + String owner = x509Certificate.getSubjectX500Principal().getName(); + assertThat(owner.contains("my-cn.localhost")).isTrue(); + } + + @Test + void whenCheckingAliasAndName_thenNameIsNotFound() throws Exception { + KeyStore keystore = readKeyStore(); + + assertThat(keystore.containsAlias(KEYSTORE_ALIAS)).isTrue(); + + X509Certificate x509Certificate = (X509Certificate) keystore.getCertificate(KEYSTORE_ALIAS); + String owner = x509Certificate.getSubjectX500Principal() + .getName(); + assertThat(owner.contains("commonName1")).isFalse(); + } + + @Test + void whenCheckingAliasAndName_thenAliasIsNotFound() throws Exception { + KeyStore keystore = readKeyStore(); + + assertThat(keystore.containsAlias("alias1")).isFalse(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-security-4/src/test/resources/com/baeldung/keystorealias/my-keystore.jks b/core-java-modules/core-java-security-4/src/test/resources/com/baeldung/keystorealias/my-keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..8f7709ee9656d443f21a53170449736898f0325b GIT binary patch literal 2764 zcma);S5y-W5`~kHMsGnRg0xViXoB?K1q?+g(wh*9G%3{r zC<2DAfb^zdp(#OBSoiE6|FciK4|C?soqOl;JA=kE4S;|QXgpIelu?RcM4+<*VL%d| z={gwCXlBRO&d=MMuWX=da_eb-q(?9bFV!6WW*!0{X_kG9svM) z?xUJzn&1b&{VUdNM9G;;Q(?tiGM+n+TxiEM{FlaCH94FY;BeXEqBcuh;X`KI!E!a~ z`dNv*29>23L_{zT3m*oW4slqpoS#DtCD;#d%*@x!M>@+HvQBJyIL$J^15#c#aER3lr{W{9H%Ms|L|B zIn-x~`K0MlRqK;7&MmoP__V$)XVZlJU_9N~StOx0e!SStk6c<#zYW=Y@uTscD{^bY zWVs$vq-hep-{`QoI(@gwBntI~W>j4DI(4waP>N8W5c8d8UgS)``u2qk`J7fzoZI!Cu!PMEqcZd#$zVwm0H-c^yXWAUpQlL4n9<8lfcFtbeJKe zVGl}wd~SRWOG2-EXeK|Dx$XmB`szEuf1ScQ?5;m(NUbvQ2(+8abHgULF+5^ZOEvdU zD3ml{^YDBq$e29yk%&5*l zpqpNA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