Search in sources :

Example 1 with PaginatedKeyStoreData

use of org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData in project carbon-identity-framework by wso2.

the class KeyStoreAdmin method getPaginatedKeystoreInfo.

/**
 * This method will list 1. Certificate aliases 2. Private key alise 3. Private key value to a
 * given keystore.
 *
 * @param keyStoreName The name of the keystore
 * @param pageNumber   page number
 * @return Instance of KeyStoreData
 * @throws SecurityConfigException will be thrown
 */
public PaginatedKeyStoreData getPaginatedKeystoreInfo(String keyStoreName, int pageNumber) throws SecurityConfigException {
    try {
        if (keyStoreName == null) {
            throw new Exception("keystore name cannot be null");
        }
        KeyStore keyStore;
        String keyStoreType;
        String keyStorePassword = null;
        if (KeyStoreUtil.isPrimaryStore(keyStoreName)) {
            KeyStoreManager keyMan = KeyStoreManager.getInstance(tenantId);
            keyStore = keyMan.getPrimaryKeyStore();
            ServerConfiguration serverConfig = ServerConfiguration.getInstance();
            keyStoreType = serverConfig.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIMARY_KEYSTORE_TYPE);
            keyStorePassword = serverConfig.getFirstProperty(RegistryResources.SecurityManagement.SERVER_PRIVATE_KEY_PASSWORD);
        } else if (isTrustStore(keyStoreName)) {
            KeyStoreManager keyMan = KeyStoreManager.getInstance(tenantId);
            keyStore = getTrustStore();
            ServerConfiguration serverConfig = ServerConfiguration.getInstance();
            keyStoreType = serverConfig.getFirstProperty(SERVER_TRUSTSTORE_TYPE);
            keyStorePassword = serverConfig.getFirstProperty(SERVER_TRUSTSTORE_PASSWORD);
        } else {
            String path = SecurityConstants.KEY_STORES + "/" + keyStoreName;
            if (!registry.resourceExists(path)) {
                throw new SecurityConfigException("Key Store not found");
            }
            Resource resource = registry.get(path);
            KeyStoreManager manager = KeyStoreManager.getInstance(tenantId);
            keyStore = getKeyStore(keyStoreName);
            keyStoreType = resource.getProperty(SecurityConstants.PROP_TYPE);
            String encpass = resource.getProperty(SecurityConstants.PROP_PRIVATE_KEY_PASS);
            if (encpass != null) {
                CryptoUtil util = CryptoUtil.getDefaultCryptoUtil();
                keyStorePassword = new String(util.base64DecodeAndDecrypt(encpass));
            }
        }
        // Fill the information about the certificates
        Enumeration<String> aliases = keyStore.aliases();
        List<org.wso2.carbon.security.keystore.service.CertData> certDataList = new ArrayList<>();
        Format formatter = new SimpleDateFormat("dd/MM/yyyy");
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (keyStore.isCertificateEntry(alias)) {
                X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
                certDataList.add(fillCertData(cert, alias, formatter));
            }
        }
        // Create a cert array
        CertData[] certs = certDataList.toArray(new CertData[certDataList.size()]);
        // Create a KeyStoreData bean, set the name and fill in the cert information
        PaginatedKeyStoreData keyStoreData = new PaginatedKeyStoreData();
        keyStoreData.setKeyStoreName(keyStoreName);
        keyStoreData.setPaginatedCertData(doPaging(pageNumber, certs));
        keyStoreData.setKeyStoreType(keyStoreType);
        List<CertData> keyDataList = new ArrayList<>();
        aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (keyStore.isKeyEntry(alias)) {
                X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
                keyDataList.add(fillCertData(cert, alias, formatter));
            }
        }
        // Create a cert array.
        CertData[] keyCerts = keyDataList.toArray(new CertData[keyDataList.size()]);
        // Create a KeyStoreData bean, set the name and fill in the cert information.
        keyStoreData.setPaginatedKeyData(doPaging(pageNumber, keyCerts));
        return keyStoreData;
    } catch (Exception e) {
        String msg = "Error has encounted while loading the keystore to the given keystore name " + keyStoreName;
        log.error(msg, e);
        throw new SecurityConfigException(msg);
    }
}
Also used : PaginatedCertData(org.wso2.carbon.security.keystore.service.PaginatedCertData) CertData(org.wso2.carbon.security.keystore.service.CertData) ServerConfiguration(org.wso2.carbon.base.ServerConfiguration) Resource(org.wso2.carbon.registry.core.Resource) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) SecurityConfigException(org.wso2.carbon.security.SecurityConfigException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate) KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) SecurityConfigException(org.wso2.carbon.security.SecurityConfigException) CryptoUtil(org.wso2.carbon.core.util.CryptoUtil) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) PaginatedKeyStoreData(org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with PaginatedKeyStoreData

use of org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData in project carbon-identity-framework by wso2.

the class KeyStoreAdminClient method getPaginatedKeystoreInfo.

public PaginatedKeyStoreData getPaginatedKeystoreInfo(String keyStoreName, int pageNumber) throws java.lang.Exception {
    try {
        GetPaginatedKeystoreInfo request = new GetPaginatedKeystoreInfo();
        request.setKeyStoreName(keyStoreName);
        request.setPageNumber(pageNumber);
        GetPaginatedKeystoreInfoResponse response = stub.getPaginatedKeystoreInfo(request);
        return response.get_return();
    } catch (java.lang.Exception e) {
        log.error("Error in getting paginated keystore info.", e);
        throw e;
    }
}
Also used : GetPaginatedKeystoreInfoResponse(org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfoResponse) GetPaginatedKeystoreInfo(org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfo)

Example 3 with PaginatedKeyStoreData

use of org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData in project carbon-identity-framework by wso2.

the class KeyStoreAdminTest method testGetPaginatedKeystoreInfo.

@Test
public void testGetPaginatedKeystoreInfo() throws Exception {
    mockStatic(ServerConfiguration.class);
    when(ServerConfiguration.getInstance()).thenReturn(serverConfiguration);
    mockStatic(KeyStoreManager.class);
    when(KeyStoreManager.getInstance(anyInt())).thenReturn(keyStoreManager);
    when(keyStoreManager.getKeyStore("wso2carbon.jks")).thenReturn(getKeyStoreFromFile("wso2carbon.jks", "wso2carbon"));
    when(serverConfiguration.getFirstProperty(SERVER_TRUSTSTORE_FILE)).thenReturn(createPath("wso2carbon.jks").toString());
    when(serverConfiguration.getFirstProperty(SERVER_TRUSTSTORE_PASSWORD)).thenReturn("wso2carbon");
    mockStatic(KeyStoreUtil.class);
    when(KeyStoreUtil.isPrimaryStore(any())).thenReturn(true);
    mockStatic(KeyStoreManager.class);
    when(KeyStoreManager.getInstance(tenantID)).thenReturn(keyStoreManager);
    when(keyStoreManager.getPrimaryKeyStore()).thenReturn(getKeyStoreFromFile("wso2carbon.jks", "wso2carbon"));
    keyStoreAdmin = new KeyStoreAdmin(tenantID, registry);
    PaginatedKeyStoreData result = keyStoreAdmin.getPaginatedKeystoreInfo("wso2carbon.jks", 10);
    int actualKeysNo = findCertDataSetSize(result.getPaginatedKeyData().getCertDataSet());
    assertEquals(actualKeysNo, 3, "Incorrect key numbers");
}
Also used : PaginatedKeyStoreData(org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with PaginatedKeyStoreData

use of org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData in project product-is by wso2.

the class KeyStoreAdminClient method getPaginatedKeystoreInfo.

public PaginatedKeyStoreData getPaginatedKeystoreInfo(String keyStoreName, int pageNumber) throws Exception {
    try {
        GetPaginatedKeystoreInfo request = new GetPaginatedKeystoreInfo();
        request.setKeyStoreName(keyStoreName);
        request.setPageNumber(pageNumber);
        GetPaginatedKeystoreInfoResponse response = stub.getPaginatedKeystoreInfo(request);
        return response.get_return();
    } catch (Exception e) {
        log.error("Error in getting paginated keystore info.", e);
        throw new Exception("Error in getting paginated keystore info.");
    }
}
Also used : GetPaginatedKeystoreInfoResponse(org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfoResponse) GetPaginatedKeystoreInfo(org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfo) RemoteException(java.rmi.RemoteException)

Aggregations

PaginatedKeyStoreData (org.wso2.carbon.security.keystore.service.PaginatedKeyStoreData)2 GetPaginatedKeystoreInfo (org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfo)2 GetPaginatedKeystoreInfoResponse (org.wso2.carbon.security.mgt.stub.keystore.GetPaginatedKeystoreInfoResponse)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1 ServerConfiguration (org.wso2.carbon.base.ServerConfiguration)1 CryptoUtil (org.wso2.carbon.core.util.CryptoUtil)1 KeyStoreManager (org.wso2.carbon.core.util.KeyStoreManager)1 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)1