Skip to content

Commit

Permalink
Update keystore extension to PKCS
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 17, 2024
1 parent 013656a commit b25ded6
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 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 @@ -177,7 +177,7 @@ private void createMocks() throws Exception {
when(KeyStoreUtil.isPrimaryStore(anyString())).thenReturn(true);

KeyStore keyStore = TestUtils.
loadKeyStoreFromFileSystem(TestUtils.getFilePath("wso2carbon.jks"), "wso2carbon", "JKS");
loadKeyStoreFromFileSystem(TestUtils.getFilePath("wso2carbon.p12"), "wso2carbon", "PKCS12");

SAMLSSOUtil.setRegistryService(registryService);
when(registryService.getGovernanceSystemRegistry()).thenReturn(registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class TestConstants {
public static final String SESSION_ID = "sessionId4567890";
public static final String SAMPLE_SERVER_URL = "https://localhost:9443/server";
public static final String WSO2_CARBON = "wso2carbon";
public static final String KEY_STORE_NAME = WSO2_CARBON + ".jks";
public static final String KEY_STORE_NAME = WSO2_CARBON + ".p12";
public static final String RSA_SHA1_SIG_ALGO = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
public static final String SHA1_DIGEST_ALGO = "http://www.w3.org/2000/09/xmldsig#sha1";
public static final String GENERAL_STRING = "WSO2 is an open source technology provider that increases the " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static void prepareCredentials(X509Credential x509Credential) throws KeyS
UnrecoverableKeyException, NoSuchAlgorithmException {

KeyStore keyStore = TestUtils.loadKeyStoreFromFileSystem(TestUtils
.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "JKS");
.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "PKCS12");
X509Certificate[] issuerCerts = null;
Certificate[] certificates;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ private void prepareForAssertionEncryption() throws Exception {
mockStatic(KeyStoreManager.class);
when(KeyStoreManager.getInstance(anyInt())).thenReturn(keyStoreManager);
when(keyStoreManager.getKeyStore(anyString())).thenReturn(TestUtils.loadKeyStoreFromFileSystem(
TestUtils.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "JKS"));
TestUtils.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "PKCS12"));
when(keyStoreManager.getPrimaryKeyStore()).thenReturn(TestUtils.loadKeyStoreFromFileSystem(
TestUtils.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "JKS"));
TestUtils.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "PKCS12"));
SAMLSSOUtil.setRealmService(realmService);

mockStatic(IdentityUtil.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public void testGetX509CredentialImplForSuperTenant() throws Exception {
mockStatic(KeyStoreManager.class);
when(KeyStoreManager.getInstance(eq(-1234))).thenReturn(keyStoreManager);
when(keyStoreManager.getPrimaryKeyStore()).thenReturn(TestUtils.loadKeyStoreFromFileSystem(TestUtils
.getFilePath("wso2carbon.jks"), "wso2carbon", "JKS"));
.getFilePath("wso2carbon.p12"), "wso2carbon", "PKCS12"));
X509CredentialImpl x509Credential = SAMLSSOUtil.getX509CredentialImplForTenant("carbon.super", "wso2carbon");
assertNotNull(x509Credential.getPublicKey(), "public key is missing");
}
Expand All @@ -420,7 +420,7 @@ public void testGetX509CredentialImplForTenant() throws Exception {
when(KeyStoreManager.getInstance(eq(1))).thenReturn(keyStoreManager);
when(keyStoreManager.getKeyStore(eq(SAMLSSOUtil.generateKSNameFromDomainName(TestConstants.WSO2_TENANT_DOMAIN)))).thenReturn
(TestUtils.loadKeyStoreFromFileSystem(TestUtils
.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "JKS"));
.getFilePath(TestConstants.KEY_STORE_NAME), TestConstants.WSO2_CARBON, "PKCS12"));
X509CredentialImpl x509Credential = SAMLSSOUtil.getX509CredentialImplForTenant(TestConstants
.WSO2_TENANT_DOMAIN, TestConstants.WSO2_CARBON);
assertNotNull(x509Credential.getPublicKey(), "public key is missing for tenant");
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.22-SNAPSHOT</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 b25ded6

Please sign in to comment.