Search in sources :

Example 11 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class DefaultApiKeyGenerator method buildSignature.

protected byte[] buildSignature(String assertion) throws APIManagementException {
    PrivateKey privateKey = null;
    // get super tenant's key store manager
    KeyStoreManager tenantKSM = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
    try {
        ServerConfigurationService config = tenantKSM.getServerConfigService();
        String apiKeySignKeyStoreName = APIUtil.getApiKeySignKeyStoreName();
        String keyStorePassword = config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_PRIVATE_KEY_PASSWORD.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName));
        String apiKeySignAlias = config.getFirstProperty(APIConstants.KeyStoreManagement.SERVER_APIKEYSIGN_KEYSTORE_KEY_ALIAS.replaceFirst(APIConstants.KeyStoreManagement.KeyStoreName, apiKeySignKeyStoreName));
        KeyStore apiKeySignKeyStore = getApiKeySignKeyStore(tenantKSM);
        if (apiKeySignKeyStore != null) {
            privateKey = (PrivateKey) apiKeySignKeyStore.getKey(apiKeySignAlias, keyStorePassword.toCharArray());
        }
    } catch (Exception e) {
        throw new APIManagementException("Error while signing Api Key", e);
    }
    return APIUtil.signJwt(assertion, privateKey, "SHA256withRSA");
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) PrivateKey(java.security.PrivateKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ServerConfigurationService(org.wso2.carbon.base.api.ServerConfigurationService) KeyStore(java.security.KeyStore) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 12 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class ServiceReferenceHolder method setPublicCert.

public void setPublicCert() {
    try {
        KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(MultitenantConstants.SUPER_TENANT_ID);
        this.publicCert = keyStoreManager.getDefaultPrimaryCertificate();
    } catch (Exception e) {
        String error = "Error in obtaining keystore";
        log.debug(error, e);
    }
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager)

Example 13 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class SigningUtil method getPublicCertificate.

/**
 * Util method to get public certificate.
 *
 * @param tenantId Tenant domain
 * @return public cert
 * @throws APIManagementException If an error occurs
 */
public static Certificate getPublicCertificate(int tenantId) throws APIManagementException {
    // get tenant domain of the key to add the certificate from
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    try {
        Certificate publicCert;
        if (!(publicCerts.containsKey(tenantId))) {
            // get tenant's key store manager
            APIUtil.loadTenantRegistry(tenantId);
            KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(tenantId);
            KeyStore keyStore;
            if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
                // derive key store name
                String ksName = tenantDomain.trim().replace('.', '-');
                String jksName = ksName + APIConstants.KeyStoreManagement.KEY_STORE_EXTENSION_JKS;
                keyStore = keyStoreManager.getKeyStore(jksName);
                publicCert = keyStore.getCertificate(tenantDomain);
            } else {
                publicCert = keyStoreManager.getDefaultPrimaryCertificate();
            }
            if (publicCert != null) {
                publicCerts.put(tenantId, publicCert);
            }
        } else {
            publicCert = publicCerts.get(tenantId);
        }
        if (publicCert == null) {
            throw new APIManagementException("Error while obtaining public certificate from keystore for tenant: " + tenantDomain);
        } else {
            return publicCert;
        }
    } catch (RegistryException e) {
        throw new APIManagementException("Error while loading tenant registry for " + tenantDomain, e);
    } catch (Exception e) {
        throw new APIManagementException("Error while obtaining public certificate from keystore for tenant: " + tenantDomain, e);
    }
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyStore(java.security.KeyStore) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Certificate(java.security.cert.Certificate)

Example 14 with KeyStoreManager

use of org.wso2.carbon.core.util.KeyStoreManager in project carbon-apimgt by wso2.

the class SigningUtil method getSigningKey.

/**
 * Util method to get signing key for the tenant.
 *
 * @param tenantId Tenant Id
 * @return Private key to sign
 * @throws APIManagementException If an error occurs
 */
public static PrivateKey getSigningKey(int tenantId) throws APIManagementException {
    // get tenant domain of the key to sign from
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    Key privateKey;
    try {
        if (!(privateKeys.containsKey(tenantId))) {
            APIUtil.loadTenantRegistry(tenantId);
            // get tenant's key store manager
            KeyStoreManager tenantKeyStoreManager = KeyStoreManager.getInstance(tenantId);
            if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
                // derive key store name
                String ksName = tenantDomain.trim().replace('.', '-');
                String jksName = ksName + APIConstants.KeyStoreManagement.KEY_STORE_EXTENSION_JKS;
                // obtain private key
                privateKey = tenantKeyStoreManager.getPrivateKey(jksName, tenantDomain);
            } else {
                privateKey = tenantKeyStoreManager.getDefaultPrivateKey();
            }
            if (privateKey != null) {
                privateKeys.put(tenantId, privateKey);
            }
        } else {
            privateKey = privateKeys.get(tenantId);
        }
        if (privateKey == null) {
            throw new APIManagementException("Error while obtaining private key for tenant: " + tenantDomain);
        }
        return (PrivateKey) privateKey;
    } catch (RegistryException e) {
        throw new APIManagementException("Error while loading tenant registry for " + tenantDomain, e);
    } catch (Exception e) {
        throw new APIManagementException("Error while obtaining private key for tenant: " + tenantDomain, e);
    }
}
Also used : KeyStoreManager(org.wso2.carbon.core.util.KeyStoreManager) PrivateKey(java.security.PrivateKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Key(java.security.Key) PrivateKey(java.security.PrivateKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

KeyStoreManager (org.wso2.carbon.core.util.KeyStoreManager)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 PrivateKey (java.security.PrivateKey)5 KeyStore (java.security.KeyStore)3 Certificate (java.security.cert.Certificate)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 JWSSigner (com.nimbusds.jose.JWSSigner)2 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)2 IOException (java.io.IOException)2 ServerConfigurationService (org.wso2.carbon.base.api.ServerConfigurationService)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 UserStoreException (org.wso2.carbon.user.api.UserStoreException)2 JWSHeader (com.nimbusds.jose.JWSHeader)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Key (java.security.Key)1 KeyPairGenerator (java.security.KeyPairGenerator)1