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");
}
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);
}
}
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);
}
}
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);
}
}
Aggregations