Package org.wso2.carbon.crypto.impl
Class DefaultCryptoService
java.lang.Object
org.wso2.carbon.crypto.impl.DefaultCryptoService
- All Implemented Interfaces:
org.wso2.carbon.crypto.api.CryptoService,org.wso2.carbon.crypto.api.PrivateKeyRetriever
public class DefaultCryptoService
extends Object
implements org.wso2.carbon.crypto.api.CryptoService, org.wso2.carbon.crypto.api.PrivateKeyRetriever
The default implementation of
CryptoService.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if there is at least oneExternalCryptoProviderregistered with the service, false otherwise.booleanReturns true if there is at least oneInternalCryptoProviderregistered with the service, false otherwise.byte[]customDecrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, Object... params) byte[]customEncrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText, Object... params) byte[]byte[]decrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, String internalCryptoProviderType) byte[]decrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) byte[]byte[]encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText) Computes and returns the ciphertext of the given cleartext.byte[]encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText, String internalCryptoProviderType) byte[]encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) getCertificate(org.wso2.carbon.crypto.api.CryptoContext cryptoContext) org.wso2.carbon.crypto.api.ExternalCryptoProviderReturns the most suitableExternalCryptoProviderfor the operations, among the registered providers.org.wso2.carbon.crypto.api.InternalCryptoProviderReturns the most suitableInternalCryptoProviderfor the operations, among the registered providers.getPrivateKey(org.wso2.carbon.crypto.api.CryptoContext cryptoContext) byte[]hybridDecrypt(org.wso2.carbon.crypto.api.HybridEncryptionOutput hybridEncryptionOutput, String symmetricAlgorithm, String asymmetricAlgorithm, String javaSecurityProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) org.wso2.carbon.crypto.api.HybridEncryptionOutputhybridEncrypt(org.wso2.carbon.crypto.api.HybridEncryptionInput hybridEncryptionInput, String symmetricAlgorithm, String asymmetricAlgorithm, String javaSecurityProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) voidoverrideKeyResolverPriority(String keyResolverClassName, int keyResolverPriority) Overrides the priority of the given key resolver, with the given new priority.voidregisterExternalCryptoProvider(org.wso2.carbon.crypto.api.ExternalCryptoProvider provider) Registers aExternalCryptoProviderwith this service.voidregisterInternalCryptoProvider(org.wso2.carbon.crypto.api.InternalCryptoProvider internalCryptoProvider) Registers aInternalCryptoProviderwith this service.voidregisterKeyResolver(org.wso2.carbon.crypto.api.KeyResolver keyResolver) Registers a new key resolver.voidsetExternalCryptoProviderClassName(String externalCryptoProviderClassName) Sets the preferred external provider class name, which was read from a configuration.voidsetInternalCryptoProviderClassName(String internalCryptoProviderClassName) Sets the preferred internal provider class name, which was read from a configuration.byte[]sign(byte[] data, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) voidUnregisters all the registered external crypto providers.voidUnregisters all the registered providers.voidunregisterExternalCryptoProvider(org.wso2.carbon.crypto.api.ExternalCryptoProvider externalCryptoProvider) Unregisters the given external crypto provider.voidunregisterInternalCryptoProvider(org.wso2.carbon.crypto.api.InternalCryptoProvider internalCryptoProvider) Unregisters the given internal crypto provider.voidunregisterKeyResolver(org.wso2.carbon.crypto.api.KeyResolver keyResolver) Unregisters the given key resolver.booleanverifySignature(byte[] data, byte[] signature, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext)
-
Constructor Details
-
DefaultCryptoService
public DefaultCryptoService()
-
-
Method Details
-
encrypt
public byte[] encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
encryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
cleartext- Cleartext to be encrypted.algorithm- The encryption / decryption algorithmjavaSecurityAPIProvider- The Java Security API provider.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
decrypt
public byte[] decrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
decryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
ciphertext- The ciphertext to be decrypted.algorithm- The signature + hashing algorithm to be used in signing.javaSecurityAPIProvider- The Java Security API provider.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
sign
public byte[] sign(byte[] data, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
signin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
data- The content which the signature should be generated against.algorithm- The signature + hashing algorithm to be used in signing.javaSecurityAPIProvider- The Java Security API provider.cryptoContext- The context information which is needed to discover the private key.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
decrypt
public byte[] decrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
decryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
ciphertext- The content which the signature should be generated against.algorithm- The signature + hashing algorithm to be used in signing.javaSecurityAPIProvider- The Java Security API provider.cryptoContext- The context information which is needed to discover the private key.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
encrypt
public byte[] encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
encryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
cleartext- The cleartext to be encrypted.algorithm- The signature + hashing algorithm to be used in signing.javaSecurityAPIProvider- The Java Security API provider.cryptoContext- The context information which is needed to discover the public key of the external entity.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
verifySignature
public boolean verifySignature(byte[] data, byte[] signature, String algorithm, String javaSecurityAPIProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
verifySignaturein interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
data- The data which was the signature generated on.signature- The signature bytes of data.algorithm- The signature + hashing algorithm to be used in signing.javaSecurityAPIProvider- The Java Security API provider.cryptoContext- The context information which is needed to discover the public key of the external entity.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
getCertificate
public Certificate getCertificate(org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
getCertificatein interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
cryptoContext- The context information which is used to discover the public key of the external entity.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
hybridEncrypt
public org.wso2.carbon.crypto.api.HybridEncryptionOutput hybridEncrypt(org.wso2.carbon.crypto.api.HybridEncryptionInput hybridEncryptionInput, String symmetricAlgorithm, String asymmetricAlgorithm, String javaSecurityProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
hybridEncryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
hybridEncryptionInput- Input data for hybrid encryption.symmetricAlgorithm- The symmetric encryption/decryption algorithm.asymmetricAlgorithm- The asymmetric encryption/decryption algorithm.javaSecurityProvider- The Java Security API provider.cryptoContext- The context information which is used to discover the public key of the external entity.- Returns:
HybridEncryptionOutputdata related to hybrid encryption.- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
hybridDecrypt
public byte[] hybridDecrypt(org.wso2.carbon.crypto.api.HybridEncryptionOutput hybridEncryptionOutput, String symmetricAlgorithm, String asymmetricAlgorithm, String javaSecurityProvider, org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
hybridDecryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
hybridEncryptionOutput-HybridEncryptionOutputciphered data with parameters.symmetricAlgorithm- The symmetric encryption/decryption algorithm.asymmetricAlgorithm- The asymmetric encryption/decryption algorithm.javaSecurityProvider- The Java Security API provider.cryptoContext- The context information which is used to discover the public key of the external entity.- Returns:
- plain data
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
getPrivateKey
public PrivateKey getPrivateKey(org.wso2.carbon.crypto.api.CryptoContext cryptoContext) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
getPrivateKeyin interfaceorg.wso2.carbon.crypto.api.PrivateKeyRetriever- Parameters:
cryptoContext-- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
encrypt
public byte[] encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText) throws org.wso2.carbon.crypto.api.CryptoException Computes and returns the ciphertext of the given cleartext.- Specified by:
encryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Parameters:
cleartext- The cleartext to be encrypted.algorithm- The encryption / decryption algorithmjavaSecurityAPIProvider- The Java Security API provider.returnSelfContainedCipherText- Whether cipher text need to be self contained.- Returns:
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
encrypt
public byte[] encrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText, String internalCryptoProviderType) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
encryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
decrypt
public byte[] decrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, String internalCryptoProviderType) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
decryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
customDecrypt
public byte[] customDecrypt(byte[] ciphertext, String algorithm, String javaSecurityAPIProvider, Object... params) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
customDecryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
customEncrypt
public byte[] customEncrypt(byte[] cleartext, String algorithm, String javaSecurityAPIProvider, boolean returnSelfContainedCipherText, Object... params) throws org.wso2.carbon.crypto.api.CryptoException - Specified by:
customEncryptin interfaceorg.wso2.carbon.crypto.api.CryptoService- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
registerKeyResolver
public void registerKeyResolver(org.wso2.carbon.crypto.api.KeyResolver keyResolver) Registers a new key resolver.- Parameters:
keyResolver- The key resolver to be registered.
-
unregisterKeyResolver
public void unregisterKeyResolver(org.wso2.carbon.crypto.api.KeyResolver keyResolver) Unregisters the given key resolver.- Parameters:
keyResolver- The key resolver to be unregistered.
-
registerInternalCryptoProvider
public void registerInternalCryptoProvider(org.wso2.carbon.crypto.api.InternalCryptoProvider internalCryptoProvider) Registers aInternalCryptoProviderwith this service. These providers are used for internal crypto needs.- Parameters:
internalCryptoProvider-
-
unregisterAllInternalCryptoProviders
public void unregisterAllInternalCryptoProviders()Unregisters all the registered providers. -
unregisterInternalCryptoProvider
public void unregisterInternalCryptoProvider(org.wso2.carbon.crypto.api.InternalCryptoProvider internalCryptoProvider) Unregisters the given internal crypto provider.- Parameters:
internalCryptoProvider-
-
areInternalCryptoProvidersAvailable
public boolean areInternalCryptoProvidersAvailable()Returns true if there is at least oneInternalCryptoProviderregistered with the service, false otherwise.- Returns:
-
getMostSuitableInternalProvider
public org.wso2.carbon.crypto.api.InternalCryptoProvider getMostSuitableInternalProvider() throws org.wso2.carbon.crypto.api.CryptoExceptionReturns the most suitableInternalCryptoProviderfor the operations, among the registered providers.- Returns:
- The most suitable provider.
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
registerExternalCryptoProvider
public void registerExternalCryptoProvider(org.wso2.carbon.crypto.api.ExternalCryptoProvider provider) Registers aExternalCryptoProviderwith this service. These providers are used for crypto needs which involves an external entity (e.g. Signing messages for an external application).- Parameters:
provider-
-
unregisterExternalCryptoProvider
public void unregisterExternalCryptoProvider(org.wso2.carbon.crypto.api.ExternalCryptoProvider externalCryptoProvider) Unregisters the given external crypto provider.- Parameters:
externalCryptoProvider-
-
unregisterAllExternalCryptoProviders
public void unregisterAllExternalCryptoProviders()Unregisters all the registered external crypto providers. -
areExternalCryptoProvidersAvailable
public boolean areExternalCryptoProvidersAvailable()Returns true if there is at least oneExternalCryptoProviderregistered with the service, false otherwise.- Returns:
-
getMostSuitableExternalProvider
public org.wso2.carbon.crypto.api.ExternalCryptoProvider getMostSuitableExternalProvider() throws org.wso2.carbon.crypto.api.CryptoExceptionReturns the most suitableExternalCryptoProviderfor the operations, among the registered providers.- Returns:
- The most suitable provider.
- Throws:
org.wso2.carbon.crypto.api.CryptoException
-
setInternalCryptoProviderClassName
Sets the preferred internal provider class name, which was read from a configuration.- Parameters:
internalCryptoProviderClassName- The preferred internal crypto provider class name.
-
setExternalCryptoProviderClassName
Sets the preferred external provider class name, which was read from a configuration.- Parameters:
externalCryptoProviderClassName- The preferred external crypto provider class name.
-
overrideKeyResolverPriority
Overrides the priority of the given key resolver, with the given new priority.- Parameters:
keyResolverClassName- The class name of the key resolver whose priority should be overridden.keyResolverPriority- The new priority.
-