Skip to content

Commit

Permalink
Merge pull request #6179 from sadilchamishka/add-utils-for-contents-s…
Browse files Browse the repository at this point in the history
…ign-and-signature-validation

Throw signature exception from signing and signature validation utils
  • Loading branch information
sadilchamishka authored Dec 5, 2024
2 parents ca6bc45 + 3b6ada1 commit 18a8f0c
Showing 1 changed file with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1976,23 +1976,20 @@ public static boolean isSCIM2UserMaxItemsPerPageEnabled() {
* @param signature The signature to be verified.
* @param tenantDomain The tenant domain to which the data belongs.
* @return true if the signature is valid, false otherwise.
* @throws IdentityKeyStoreResolverException If an error occurs during the signature validation process.
* @throws SignatureException If an error occurs during the signature validation process.
*/
public static boolean validateSignatureFromTenant(String data, byte[] signature, String tenantDomain)
throws IdentityKeyStoreResolverException {
throws SignatureException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
try {
IdentityTenantUtil.initializeRegistry(tenantId);
PublicKey publicKey = IdentityKeyStoreResolver.getInstance().getCertificate(tenantDomain, null)
.getPublicKey();
return SignatureUtil.validateSignature(data, signature, publicKey);
} catch (IdentityException e) {
throw new IdentityKeyStoreResolverException(
IdentityKeyStoreResolverConstants.ErrorMessages
.ERROR_CODE_ERROR_RETRIEVING_TENANT_PRIVATE_KEY.getCode(),
"Error while loading the private key", e);
throw new SignatureException("Error while validating the signature from tenant: " + tenantDomain, e);
}
PublicKey publicKey = IdentityKeyStoreResolver.getInstance().getCertificate(tenantDomain, null)
.getPublicKey();
return SignatureUtil.validateSignature(data, signature, publicKey);
}

/**
Expand All @@ -2001,9 +1998,9 @@ public static boolean validateSignatureFromTenant(String data, byte[] signature,
* @param data The data to be signed.
* @param tenantDomain The tenant domain to which the data belongs.
* @return The signature of the data.
* @throws IdentityKeyStoreResolverException If an error occurs during the signature generation process.
* @throws SignatureException If an error occurs during the signature generation process.
*/
public static byte[] signWithTenantKey(String data, String tenantDomain) throws IdentityKeyStoreResolverException {
public static byte[] signWithTenantKey(String data, String tenantDomain) throws SignatureException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(tenantId);
Expand All @@ -2013,23 +2010,18 @@ public static byte[] signWithTenantKey(String data, String tenantDomain) throws
try {
privateKey = keyStoreManager.getDefaultPrivateKey();
} catch (Exception e) {
throw new IdentityKeyStoreResolverException(IdentityKeyStoreResolverConstants.ErrorMessages
.ERROR_CODE_ERROR_RETRIEVING_TENANT_PRIVATE_KEY.getCode(),
String.format(IdentityKeyStoreResolverConstants.ErrorMessages
throw new SignatureException(String.format(IdentityKeyStoreResolverConstants.ErrorMessages
.ERROR_CODE_ERROR_RETRIEVING_TENANT_PRIVATE_KEY.getDescription(), tenantDomain),
e);
}
} else {
String tenantKeyStoreName = IdentityKeyStoreResolverUtil.buildTenantKeyStoreName(tenantDomain);
try {
String tenantKeyStoreName = IdentityKeyStoreResolverUtil.buildTenantKeyStoreName(tenantDomain);
IdentityTenantUtil.initializeRegistry(tenantId);
privateKey = (PrivateKey) keyStoreManager.getPrivateKey(tenantKeyStoreName, tenantDomain);
} catch (IdentityException e) {
throw new IdentityKeyStoreResolverException(
IdentityKeyStoreResolverConstants.ErrorMessages
.ERROR_CODE_ERROR_RETRIEVING_TENANT_PRIVATE_KEY.getCode(),
"Error while loading the private key", e);
throw new SignatureException("Error while signing from private key of tenant: " + tenantDomain, e);
}
privateKey = (PrivateKey) keyStoreManager.getPrivateKey(tenantKeyStoreName, tenantDomain);
}
return SignatureUtil.doSignature(data, privateKey);
}
Expand Down

0 comments on commit 18a8f0c

Please sign in to comment.