public class Encrypter extends Object
Various overloaded method variants are supplied for encrypting XMLObjects and their contents (with or without encryption of the associated data encryption key), as well as for encrypting keys separately.
The parameters for data encryption are specified with an instance of DataEncryptionParameters
. The parameters
for key encryption are specified with one or more instances of KeyEncryptionParameters
.
The data encryption credential supplied by DataEncryptionParameters.getEncryptionCredential()
is mandatory
unless key encryption is also being performed and all associated key encryption parameters contain a valid key
encryption credential containing a valid key encryption key. In this case the data encryption key will be randomly
generated based on the algorithm URI supplied by DataEncryptionParameters.getAlgorithm()
.
If encryption of the data encryption key is being performed using the overloaded methods for elements or content, the resulting EncryptedKey(s) will be placed inline within the KeyInfo of the resulting EncryptedData. If this is not the desired behavior, the XMLObject and the data encryption key should be encrypted separately, and the placement of EncryptedKey(s) handled by the caller. Specialized subclasses of this class maybe also handle key placement in an application-specific manner.
Modifier and Type | Field and Description |
---|---|
private org.opensaml.core.xml.io.Unmarshaller |
encryptedDataUnmarshaller
Unmarshaller used to create EncryptedData objects from DOM element.
|
private org.opensaml.core.xml.io.Unmarshaller |
encryptedKeyUnmarshaller
Unmarshaller used to create EncryptedData objects from DOM element.
|
private String |
jcaProviderName
The name of the JCA security provider to use.
|
private XMLSignatureBuilder<KeyInfo> |
keyInfoBuilder
Builder instance for building KeyInfo objects.
|
private org.slf4j.Logger |
log
Class logger.
|
Constructor and Description |
---|
Encrypter()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected XMLCipher |
buildXMLCipher(Key encryptionKey,
String encryptionAlgorithmURI,
RSAOAEPParameters rsaOAEPParams)
Construct and return an instance of
XMLCipher based on the given inputs. |
protected void |
checkAndMarshall(org.opensaml.core.xml.XMLObject xmlObject)
Ensure that the XMLObject is marshalled.
|
protected void |
checkParams(DataEncryptionParameters encParams)
Check data encryption parameters for consistency and required values.
|
protected void |
checkParams(DataEncryptionParameters encParams,
List<KeyEncryptionParameters> kekParamsList)
Check the encryption parameters and key encryption parameters for valid combinations of options.
|
protected void |
checkParams(KeyEncryptionParameters kekParams,
boolean allowEmpty)
Check key encryption parameters for consistency and required values.
|
protected void |
checkParams(List<KeyEncryptionParameters> kekParamsList,
boolean allowEmpty)
Check a list of key encryption parameters for consistency and required values.
|
protected byte[] |
decodeOAEPParams(String base64Params)
Safely decode and normalize base64-encoded OAEPParams data.
|
EncryptedData |
encryptElement(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams)
Encrypts the DOM representation of the XMLObject.
|
EncryptedData |
encryptElement(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams,
KeyEncryptionParameters kekParams)
Encrypts the DOM representation of the XMLObject, encrypts the encryption key using the specified key encryption
parameters and places the resulting EncryptedKey within the EncryptedData's KeyInfo.
|
EncryptedData |
encryptElement(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams,
List<KeyEncryptionParameters> kekParamsList)
Encrypts the DOM representation of the XMLObject, encrypts the encryption key using the specified key encryption
parameters and places the resulting EncryptedKey(s) within the EncryptedData's KeyInfo.
|
private EncryptedData |
encryptElement(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams,
List<KeyEncryptionParameters> kekParamsList,
boolean encryptContentMode)
Encrypts the given XMLObject using the specified encryption key, algorithm URI and content mode flag.
|
protected EncryptedData |
encryptElement(org.opensaml.core.xml.XMLObject xmlObject,
Key encryptionKey,
String encryptionAlgorithmURI,
boolean encryptContentMode)
Encrypts the given XMLObject using the specified encryption key, algorithm URI and content mode flag.
|
EncryptedData |
encryptElementContent(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams)
Encrypts the DOM representation of the content of an XMLObject.
|
EncryptedData |
encryptElementContent(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams,
KeyEncryptionParameters kekParams)
Encrypts the DOM representation of the content of an XMLObject, encrypts the encryption key using the specified
key encryption parameters and places the resulting EncryptedKey within the EncryptedData's KeyInfo..
|
EncryptedData |
encryptElementContent(org.opensaml.core.xml.XMLObject xmlObject,
DataEncryptionParameters encParams,
List<KeyEncryptionParameters> kekParamsList)
Encrypts the DOM representation of the content of an XMLObject, encrypts the encryption key using the specified
key encryption parameters and places the resulting EncryptedKey(s) within the EncryptedData's KeyInfo..
|
EncryptedKey |
encryptKey(Key key,
KeyEncryptionParameters kekParams,
Document containingDocument)
Encrypts a key.
|
protected EncryptedKey |
encryptKey(Key targetKey,
Key encryptionKey,
String encryptionAlgorithmURI,
RSAOAEPParameters rsaOAEPParams,
Document containingDocument)
Encrypts a key using the specified encryption key and algorithm URI.
|
List<EncryptedKey> |
encryptKey(Key key,
List<KeyEncryptionParameters> kekParamsList,
Document containingDocument)
Encrypts a key once for each key encryption parameters set that is supplied.
|
protected SecretKey |
generateEncryptionKey(String encryptionAlgorithmURI)
Generate a random symmetric encryption key.
|
protected String |
getEffectiveMGF(String encryptionAlgorithmURI,
RSAOAEPParameters rsaOAEPParams)
Get the effective RSA OAEP mask generation function (MGF) to use.
|
String |
getJCAProviderName()
Get the Java Cryptography Architecture (JCA) security provider name that should be used to provide the encryption
support.
|
protected void |
postProcessApacheEncryptedKey(EncryptedKey apacheEncryptedKey,
Key targetKey,
Key encryptionKey,
String encryptionAlgorithmURI,
Document containingDocument)
Post-process the Apache EncryptedKey, prior to marshalling to DOM and unmarshalling into an XMLObject.
|
void |
setJCAProviderName(String providerName)
Set the Java Cryptography Architecture (JCA) security provider name that should be used to provide the encryption
support.
|
private final org.slf4j.Logger log
private final org.opensaml.core.xml.io.Unmarshaller encryptedDataUnmarshaller
private final org.opensaml.core.xml.io.Unmarshaller encryptedKeyUnmarshaller
private final XMLSignatureBuilder<KeyInfo> keyInfoBuilder
private String jcaProviderName
@Nullable public String getJCAProviderName()
null
, which means that the first registered provider which supports the requested
encryption algorithm URI will be used.public void setJCAProviderName(@Nullable String providerName)
null
, which means that the first registered provider which supports the requested
encryption algorithm URI will be used.providerName
- the JCA provider name to use@Nonnull public EncryptedData encryptElement(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the dataEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedData encryptElement(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams, @Nonnull KeyEncryptionParameters kekParams) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the datakekParams
- parameters for encrypting the encryption keyEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedData encryptElement(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams, @Nonnull List<KeyEncryptionParameters> kekParamsList) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the datakekParamsList
- parameters for encrypting the encryption keyEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedData encryptElementContent(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the dataEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedData encryptElementContent(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams, @Nonnull KeyEncryptionParameters kekParams) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the datakekParams
- parameters for encrypting the encryption keyEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedData encryptElementContent(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams, @Nonnull List<KeyEncryptionParameters> kekParamsList) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- parameters for encrypting the datakekParamsList
- parameters for encrypting the encryption keyEncryptionException
- exception thrown on encryption errors@Nonnull public List<EncryptedKey> encryptKey(@Nonnull Key key, @Nonnull List<KeyEncryptionParameters> kekParamsList, @Nonnull Document containingDocument) throws EncryptionException
key
- the key to encryptkekParamsList
- a list parameters for encrypting the keycontainingDocument
- the document that will own the DOM element underlying the resulting EncryptedKey
objectsEncryptionException
- exception thrown on encryption errors@Nonnull public EncryptedKey encryptKey(@Nonnull Key key, @Nonnull KeyEncryptionParameters kekParams, @Nonnull Document containingDocument) throws EncryptionException
key
- the key to encryptkekParams
- parameters for encrypting the keycontainingDocument
- the document that will own the DOM element underlying the resulting EncryptedKey objectEncryptionException
- exception thrown on encryption errors@Nonnull protected EncryptedKey encryptKey(@Nonnull Key targetKey, @Nonnull Key encryptionKey, @Nonnull String encryptionAlgorithmURI, @Nullable RSAOAEPParameters rsaOAEPParams, @Nonnull Document containingDocument) throws EncryptionException
targetKey
- the key to encryptencryptionKey
- the key with which to encrypt the target keyencryptionAlgorithmURI
- the XML Encryption algorithm URI corresponding to the encryption keyrsaOAEPParams
- the RSA-OAEP params instance (may be null)containingDocument
- the document that will own the resulting elementEncryptionException
- exception thrown on encryption errors@Nonnull protected XMLCipher buildXMLCipher(@Nonnull Key encryptionKey, @Nonnull String encryptionAlgorithmURI, @Nullable RSAOAEPParameters rsaOAEPParams) throws XMLEncryptionException
XMLCipher
based on the given inputs.encryptionKey
- the key transport encryption key with which to initialize the XMLCipherencryptionAlgorithmURI
- the key transport encryption algorithm URIrsaOAEPParams
- the optional RSA OAEP parameters instanceXMLEncryptionException
- if there is a problem constructing the XMLCipher instance@Nullable protected String getEffectiveMGF(@Nonnull String encryptionAlgorithmURI, @Nullable RSAOAEPParameters rsaOAEPParams)
encryptionAlgorithmURI
- the key transport encryption algorithm URIrsaOAEPParams
- the optional RSA OAEP params instance@Nullable protected byte[] decodeOAEPParams(@Nullable String base64Params) throws EncryptionException
base64Params
- the base64-encoded parametersEncryptionException
- if there is a problem base64-decoding the OAEPParams dataprotected void postProcessApacheEncryptedKey(@Nonnull EncryptedKey apacheEncryptedKey, @Nonnull Key targetKey, @Nonnull Key encryptionKey, @Nonnull String encryptionAlgorithmURI, @Nonnull Document containingDocument) throws EncryptionException
apacheEncryptedKey
- the Apache EncryptedKeyObject to post-processtargetKey
- the key to encryptencryptionKey
- the key with which to encrypt the target keyencryptionAlgorithmURI
- the XML Encryption algorithm URI corresponding to the encryption keycontainingDocument
- the document that will own the resulting elementEncryptionException
- exception thrown on encryption errors@Nonnull protected EncryptedData encryptElement(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull Key encryptionKey, @Nonnull String encryptionAlgorithmURI, boolean encryptContentMode) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencryptionKey
- the key with which to encrypt the XMLObjectencryptionAlgorithmURI
- the XML Encryption algorithm URI corresponding to the encryption keyencryptContentMode
- whether just the content of the XMLObject should be encryptedEncryptionException
- exception thrown on encryption errors@Nonnull private EncryptedData encryptElement(@Nonnull org.opensaml.core.xml.XMLObject xmlObject, @Nonnull DataEncryptionParameters encParams, @Nonnull List<KeyEncryptionParameters> kekParamsList, boolean encryptContentMode) throws EncryptionException
xmlObject
- the XMLObject to be encryptedencParams
- the encryption parameters to usekekParamsList
- the key encryption parameters to useencryptContentMode
- whether just the content of the XMLObject should be encryptedEncryptionException
- exception thrown on encryption errorsprotected void checkAndMarshall(@Nonnull org.opensaml.core.xml.XMLObject xmlObject) throws EncryptionException
xmlObject
- the object to check and marshallEncryptionException
- thrown if there is an error when marshalling the XMLObjectprotected void checkParams(@Nonnull DataEncryptionParameters encParams) throws EncryptionException
encParams
- the data encryption parameters to checkEncryptionException
- thrown if any parameters are missing or have invalid valuesprotected void checkParams(@Nullable KeyEncryptionParameters kekParams, boolean allowEmpty) throws EncryptionException
kekParams
- the key encryption parameters to checkallowEmpty
- if false, a null parameter is treated as an errorEncryptionException
- thrown if any parameters are missing or have invalid valuesprotected void checkParams(@Nullable List<KeyEncryptionParameters> kekParamsList, boolean allowEmpty) throws EncryptionException
kekParamsList
- the key encryption parameters list to checkallowEmpty
- if false, a null or empty list is treated as an errorEncryptionException
- thrown if any parameters are missing or have invalid valuesprotected void checkParams(@Nonnull DataEncryptionParameters encParams, @Nullable List<KeyEncryptionParameters> kekParamsList) throws EncryptionException
encParams
- the encryption parameters to usekekParamsList
- the key encryption parameters to useEncryptionException
- exception thrown on encryption errors@Nonnull protected SecretKey generateEncryptionKey(@Nonnull String encryptionAlgorithmURI) throws EncryptionException
encryptionAlgorithmURI
- the encryption algorithm URIEncryptionException
- thrown if the key cannot be generated based on the specified algorithm URICopyright © 1999–2019 Shibboleth Consortium. All rights reserved.