Skip to content

Commit

Permalink
Merge pull request #415 from Thisara-Welmilla/change-keystore-extensions
Browse files Browse the repository at this point in the history
Update keystore and truststore extension to PKCS
  • Loading branch information
Thisara-Welmilla authored Jan 20, 2024
2 parents 013656a + 596ec0b commit 16c854b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.AuditLog;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.security.KeyStore;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -334,8 +335,7 @@ private void saveCertificateToKeyStore(SAMLSSOServiceProviderDO serviceProviderD
*/
private String getKeyStoreName(int tenantId) {

String ksName = IdentityTenantUtil.getTenantDomain(tenantId).replace(".", "-");
return (ksName + ".jks");
return KeystoreUtils.getKeyStoreFileLocation(IdentityTenantUtil.getTenantDomain(tenantId));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -205,9 +206,7 @@ private void initCredentialForTenant(String tenantDomain, KeyStoreManager keySto

try {
// Derive key store name.
String ksName = tenantDomain.trim().replace(".", "-");
// Derive JKS name.
String jksName = ksName + ".jks";
String jksName = KeystoreUtils.getKeyStoreFileLocation(tenantDomain);
privateKey = (PrivateKey) keyStoreManager.getPrivateKey(jksName, tenantDomain);
signingCert = (X509Certificate) keyStoreManager.getKeyStore(jksName).getCertificate(tenantDomain);
// This Exception is thrown from the KeyStoreManager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -924,8 +925,8 @@ public static String createID() {
* @return key store file name
*/
public static String generateKSNameFromDomainName(String tenantDomain) {
String ksName = tenantDomain.trim().replace(".", "-");
return ksName + ".jks";

return KeystoreUtils.getKeyStoreFileLocation(tenantDomain);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.utils.security.KeystoreUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -84,7 +85,8 @@
*/
@PrepareForTest({IdentityProviderManager.class, IdentityUtil.class, IdentityApplicationManagementUtil.class,
KeyStoreManager.class, IdentitySAMLSSOServiceComponentHolder.class, SSOServiceProviderConfigManager.class,
IdentityTenantUtil.class, ServiceURLBuilder.class, IdentityConstants.class, FrameworkServiceComponent.class})
IdentityTenantUtil.class, ServiceURLBuilder.class, IdentityConstants.class, FrameworkServiceComponent.class,
KeystoreUtils.class})
@PowerMockIgnore({"javax.xml.*", "org.xml.*", "org.w3c.dom.*", "org.apache.xerces.*"})
public class SAMLSSOUtilTest extends PowerMockTestCase {

Expand Down Expand Up @@ -135,6 +137,12 @@ public void setUp() throws Exception {
TestUtils.startTenantFlow(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
}

private void prepareForGetKeyStorePath() throws Exception {
mockStatic(KeystoreUtils.class);
when(KeystoreUtils.getKeyStoreFileLocation(TestConstants.WSO2_TENANT_DOMAIN)).thenReturn(TestConstants
.WSO2_TENANT_DOMAIN.replace(".", "-") + TestUtils.getFilePath(TestConstants.KEY_STORE_NAME));
}

private void prepareForGetIssuer() throws Exception {

mockStatic(IdentityTenantUtil.class);
Expand Down Expand Up @@ -412,6 +420,7 @@ public void testGetX509CredentialImplForSuperTenant() throws Exception {
public void testGetX509CredentialImplForTenant() throws Exception {

prepareForGetIssuer();
prepareForGetKeyStorePath();
mockStatic(FrameworkServiceComponent.class);
when(FrameworkServiceComponent.getRealmService()).thenReturn(realmService);
when(realmService.getTenantManager()).thenReturn(tenantManager);
Expand All @@ -430,6 +439,7 @@ public void testGetX509CredentialImplForTenant() throws Exception {
public void testGetX509CredentialImplException() throws Exception {

prepareForGetIssuer();
prepareForGetKeyStorePath();
when(tenantManager.getTenantId(anyString())).thenReturn(1);
mockStatic(KeyStoreManager.class);
when(KeyStoreManager.getInstance(eq(1))).thenReturn(keyStoreManager);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@
</modules>

<properties>
<carbon.kernel.version>4.9.10</carbon.kernel.version>
<carbon.kernel.version>4.9.23</carbon.kernel.version>
<carbon.kernel.feature.version>4.9.0</carbon.kernel.feature.version>
<carbon.identity.framework.version>5.25.507</carbon.identity.framework.version>
<carbon.identity.framework.imp.pkg.version.range>[5.25.260, 7.0.0)
Expand Down

0 comments on commit 16c854b

Please sign in to comment.