Package org.opensaml.xmlsec.algorithm
Class AlgorithmSupport
java.lang.Object
org.opensaml.xmlsec.algorithm.AlgorithmSupport
Helper methods for working with XML security algorithm URI's.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkKeyAlgorithmAndLength
(Key key, AlgorithmDescriptor algorithm) Check that the supplied key is consistent with the supplied algorithm's specified key algorithm and key length, where applicable.static boolean
credentialSupportsAlgorithmForEncryption
(Credential credential, AlgorithmDescriptor algorithm) Check whether the supplied credential may be used with the supplied algorithm for the purpose of encryption.static boolean
credentialSupportsAlgorithmForSigning
(Credential credential, AlgorithmDescriptor algorithm) Check whether the supplied credential may be used with the supplied algorithm for the purpose of signing.static KeyPair
generateKeyPair
(String algoURI, int keyLength) Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI.static KeyPair
generateKeyPair
(String algoURI, AlgorithmParameterSpec paramSpec) Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI.static Credential
generateKeyPairAndCredential
(String algorithmURI, int keyLength, boolean includePrivate) Generate a random asymmetric key pair and return in a BasicCredential.static Credential
generateKeyPairAndCredential
(String algorithmURI, AlgorithmParameterSpec paramSpec, boolean includePrivate) Generate a random asymmetric key pair and return in a BasicCredential.static SecretKey
generateSymmetricKey
(String algoURI) Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI.static Credential
generateSymmetricKeyAndCredential
(String algorithmURI) Generate a random symmetric key and return in a BasicCredential.static String
getAlgorithmID
(String algorithmURI) Get the Java security JCA/JCE algorithm identifier associated with an algorithm URI.static AlgorithmRegistry
Get the globalAlgorithmRegistry
instance.static String
getKeyAlgorithm
(String algorithmURI) Get the Java security JCA/JCE key algorithm specifier associated with an algorithm URI.static Integer
getKeyLength
(String algorithmURI) Get the length of the key indicated by the algorithm URI, if applicable and available.static boolean
isBlockEncryption
(String algorithm) Check whether the algorithm URI indicates block encryption.static boolean
isDataEncryptionAlgorithm
(AlgorithmDescriptor algorithm) Check whether the supplied descriptor represents an algorithm that my be used for data encryption, i.e.static boolean
Check whether the signature method algorithm URI indicates HMAC.static boolean
isKeyEncryptionAlgorithm
(AlgorithmDescriptor algorithm) Check whether the supplied descriptor represents an algorithm that my be used for key encryption, i.e.static boolean
Check whether the key transport encryption algorithm URI indicates RSA-OAEP.static boolean
isSymmetricKeyWrap
(String algorithm) Check whether the algorithm URI indicates symmetric key wrap.static boolean
validateAlgorithmURI
(String algorithmURI, Collection<String> includedAlgorithmURIs, Collection<String> excludedAlgorithmURIs) Validate the supplied algorithm URI against the specified includes and excludes.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOGLogger.
-
-
Constructor Details
-
AlgorithmSupport
private AlgorithmSupport()Constructor.
-
-
Method Details
-
getGlobalAlgorithmRegistry
Get the globalAlgorithmRegistry
instance.- Returns:
- the global algorithm registry, or null if nothing registered
-
isKeyEncryptionAlgorithm
Check whether the supplied descriptor represents an algorithm that my be used for key encryption, i.e. a key transport or symmetric key wrap algorithm.- Parameters:
algorithm
- the algorithm descriptor to evaluate- Returns:
- true if the algorithm may be used for key encryption, false otherwise
-
isDataEncryptionAlgorithm
Check whether the supplied descriptor represents an algorithm that my be used for data encryption, i.e. a block encryption algorithm.- Parameters:
algorithm
- the algorithm descriptor to evaluate- Returns:
- true if the algorithm may be used for key encryption, false otherwise
-
credentialSupportsAlgorithmForSigning
public static boolean credentialSupportsAlgorithmForSigning(@Nullable Credential credential, @Nullable AlgorithmDescriptor algorithm) Check whether the supplied credential may be used with the supplied algorithm for the purpose of signing.This checks the consistency of the type of credential signing key and the algorithm type, as well as the key algorithm and length where applicable.
- Parameters:
credential
- the candidate signing credential to evaluatealgorithm
- the candidate signing algorithm to evaluate- Returns:
- true if the credential may be used with the algorithm for signing, false otherwise
-
credentialSupportsAlgorithmForEncryption
public static boolean credentialSupportsAlgorithmForEncryption(@Nullable Credential credential, @Nullable AlgorithmDescriptor algorithm) Check whether the supplied credential may be used with the supplied algorithm for the purpose of encryption.This checks the consistency of the extracted credential encryption key and the algorithm type, as well as the key algorithm and length where applicable.
- Parameters:
credential
- the candidate encryption credential to evaluatealgorithm
- the candidate encryption algorithm to evaluate- Returns:
- true if the credential may be used with the algorithm for encryption, false otherwise
-
checkKeyAlgorithmAndLength
public static boolean checkKeyAlgorithmAndLength(@Nonnull Key key, @Nonnull AlgorithmDescriptor algorithm) Check that the supplied key is consistent with the supplied algorithm's specified key algorithm and key length, where applicable.- Parameters:
key
- the key to evaluatealgorithm
- the algorithm to evaluate- Returns:
- true if the key is consistent with key algorithm and length specified by the algorithm (if any) false otherwise
-
getAlgorithmID
Get the Java security JCA/JCE algorithm identifier associated with an algorithm URI.- Parameters:
algorithmURI
- the algorithm URI to evaluate- Returns:
- the Java algorithm identifier, or null if the mapping is unavailable or indeterminable from the URI
-
isRSAOAEP
Check whether the key transport encryption algorithm URI indicates RSA-OAEP.- Parameters:
keyTransportAlgorithm
- the key transport encryption algorithm URI- Returns:
- true if URI indicates RSA-OAEP, false otherwise
-
isBlockEncryption
Check whether the algorithm URI indicates block encryption.- Parameters:
algorithm
- the algorithm URI- Returns:
- true if URI indicates symmetric key wrap, false otherwise
-
isSymmetricKeyWrap
Check whether the algorithm URI indicates symmetric key wrap.- Parameters:
algorithm
- the algorithm URI- Returns:
- true if URI indicates symmetric key wrap, false otherwise
-
isHMAC
Check whether the signature method algorithm URI indicates HMAC.- Parameters:
signatureAlgorithm
- the signature method algorithm URI- Returns:
- true if URI indicates HMAC, false otherwise
-
getKeyAlgorithm
Get the Java security JCA/JCE key algorithm specifier associated with an algorithm URI.- Parameters:
algorithmURI
- the algorithm URI to evaluate- Returns:
- the Java key algorithm specifier, or null if the mapping is unavailable or indeterminable from the URI
-
getKeyLength
Get the length of the key indicated by the algorithm URI, if applicable and available.- Parameters:
algorithmURI
- the algorithm URI to evaluate- Returns:
- the length of the key indicated by the algorithm URI, or null if the length is either unavailable or indeterminable from the URI
-
generateSymmetricKey
@Nonnull public static SecretKey generateSymmetricKey(@Nonnull String algoURI) throws NoSuchAlgorithmException, KeyException Generates a random Java JCE symmetric Key object from the specified XML Encryption algorithm URI.- Parameters:
algoURI
- The XML Encryption algorithm URI- Returns:
- a randomly-generated symmetric Key
- Throws:
NoSuchAlgorithmException
- thrown if the specified algorithm is invalidKeyException
- thrown if the length of the key to generate could not be determined
-
generateKeyPair
@Nonnull public static KeyPair generateKeyPair(@Nonnull String algoURI, int keyLength) throws NoSuchAlgorithmException, NoSuchProviderException Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI.- Parameters:
algoURI
- The XML Encryption algorithm URIkeyLength
- the length of key to generate- Returns:
- a randomly-generated KeyPair
- Throws:
NoSuchProviderException
- provider not foundNoSuchAlgorithmException
- algorithm not found
-
generateKeyPair
@Nonnull public static KeyPair generateKeyPair(@Nonnull String algoURI, @Nonnull AlgorithmParameterSpec paramSpec) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException Randomly generates a Java JCE KeyPair object from the specified XML Encryption algorithm URI.- Parameters:
algoURI
- The XML Encryption algorithm URIparamSpec
- the algorithm parameter specification- Returns:
- a randomly-generated KeyPair
- Throws:
NoSuchProviderException
- provider not foundNoSuchAlgorithmException
- algorithm not foundInvalidAlgorithmParameterException
- algorithm parameter spec is unsupported
-
generateSymmetricKeyAndCredential
@Nonnull public static Credential generateSymmetricKeyAndCredential(@Nonnull String algorithmURI) throws NoSuchAlgorithmException, KeyException Generate a random symmetric key and return in a BasicCredential.- Parameters:
algorithmURI
- The XML Encryption algorithm URI- Returns:
- a basic credential containing a randomly generated symmetric key
- Throws:
KeyException
- thrown if the length of key to generate could not be determinedNoSuchAlgorithmException
- algorithm not found
-
generateKeyPairAndCredential
@Nonnull public static Credential generateKeyPairAndCredential(@Nonnull String algorithmURI, int keyLength, boolean includePrivate) throws NoSuchAlgorithmException, NoSuchProviderException Generate a random asymmetric key pair and return in a BasicCredential.- Parameters:
algorithmURI
- The XML Encryption algorithm URIkeyLength
- key lengthincludePrivate
- if true, the private key will be included as well- Returns:
- a basic credential containing a randomly generated asymmetric key pair
- Throws:
NoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not found
-
generateKeyPairAndCredential
@Nonnull public static Credential generateKeyPairAndCredential(@Nonnull String algorithmURI, @Nonnull AlgorithmParameterSpec paramSpec, boolean includePrivate) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException Generate a random asymmetric key pair and return in a BasicCredential.- Parameters:
algorithmURI
- The XML Encryption algorithm URIparamSpec
- the algorithm parameter specificationincludePrivate
- if true, the private key will be included as well- Returns:
- a basic credential containing a randomly generated asymmetric key pair
- Throws:
NoSuchAlgorithmException
- algorithm not foundNoSuchProviderException
- provider not foundInvalidAlgorithmParameterException
- algorithm parameter spec is unsupported
-
validateAlgorithmURI
public static boolean validateAlgorithmURI(@Nonnull String algorithmURI, @Nullable Collection<String> includedAlgorithmURIs, @Nullable Collection<String> excludedAlgorithmURIs) Validate the supplied algorithm URI against the specified includes and excludes.- Parameters:
algorithmURI
- the algorithm URI to evaluateincludedAlgorithmURIs
- the algorithm include listexcludedAlgorithmURIs
- the algorithm exclude list- Returns:
- true if algorithm URI satisfies the specified includes and excludes, otherwise false
-