Author: anil.saldhana(a)jboss.com
Date: 2009-05-29 16:58:38 -0400 (Fri, 29 May 2009)
New Revision: 546
Modified:
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/util/XMLEncryptionUtil.java
Log:
use checked exceptions
Modified:
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/util/XMLEncryptionUtil.java
===================================================================
---
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/util/XMLEncryptionUtil.java 2009-05-29
20:58:27 UTC (rev 545)
+++
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/util/XMLEncryptionUtil.java 2009-05-29
20:58:38 UTC (rev 546)
@@ -32,7 +32,9 @@
import org.apache.xml.security.encryption.EncryptedData;
import org.apache.xml.security.encryption.EncryptedKey;
import org.apache.xml.security.encryption.XMLCipher;
-import org.apache.xml.security.exceptions.XMLSecurityException;
+import org.apache.xml.security.encryption.XMLEncryptionException;
+import org.jboss.identity.federation.core.exceptions.ConfigurationException;
+import org.jboss.identity.federation.core.exceptions.ProcessingException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -136,20 +138,27 @@
* @param keyUsedToEncryptSecretKey Asymmetric Key (Public Key)
* @param keySize Length of the key
* @return
- * @throws XMLSecurityException
+ * @throws ProcessingException
*/
public static EncryptedKey encryptKey(Document document,
SecretKey keyToBeEncrypted, PublicKey keyUsedToEncryptSecretKey,
- int keySize) throws XMLSecurityException
+ int keySize) throws ProcessingException
{
XMLCipher keyCipher = null;
String pubKeyAlg = keyUsedToEncryptSecretKey.getAlgorithm();
- String keyWrapAlgo = getXMLEncryptionURLForKeyUnwrap(pubKeyAlg, keySize);
- keyCipher = XMLCipher.getInstance(keyWrapAlgo);
-
- keyCipher.init(XMLCipher.WRAP_MODE, keyUsedToEncryptSecretKey);
- return keyCipher.encryptKey(document, keyToBeEncrypted);
+ try
+ {
+ String keyWrapAlgo = getXMLEncryptionURLForKeyUnwrap(pubKeyAlg, keySize);
+ keyCipher = XMLCipher.getInstance(keyWrapAlgo);
+
+ keyCipher.init(XMLCipher.WRAP_MODE, keyUsedToEncryptSecretKey);
+ return keyCipher.encryptKey(document, keyToBeEncrypted);
+ }
+ catch (XMLEncryptionException e)
+ {
+ throw new ProcessingException(e);
+ }
}
/**
@@ -161,14 +170,14 @@
* @param keySize
* @param wrappingElementQName A QName of an element that will wrap the encrypted
element
* @param addEncryptedKeyInKeyInfo Need for the EncryptedKey to be placed in
ds:KeyInfo
- * @return
- * @throws Exception
+ * @return
+ * @throws ProcessingException
*/
public static void encryptElement(QName elementQName,
Document document,
PublicKey publicKey,
SecretKey secretKey, int keySize, QName wrappingElementQName,
- boolean addEncryptedKeyInKeyInfo) throws Exception
+ boolean addEncryptedKeyInKeyInfo) throws ProcessingException
{
if(elementQName == null)
throw new IllegalArgumentException("elementQName is null");
@@ -191,10 +200,25 @@
String encryptionAlgorithm = getXMLEncryptionURL(secretKey.getAlgorithm(),
keySize);
//Encrypt the Document
- cipher = XMLCipher.getInstance(encryptionAlgorithm);
- cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
+ try
+ {
+ cipher = XMLCipher.getInstance(encryptionAlgorithm);
+ cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
+ }
+ catch (XMLEncryptionException e1)
+ {
+ throw new ProcessingException(e1);
+ }
- Document encryptedDoc = cipher.doFinal(document, documentElement);
+ Document encryptedDoc;
+ try
+ {
+ encryptedDoc = cipher.doFinal(document, documentElement);
+ }
+ catch (Exception e)
+ {
+ throw new ProcessingException(e);
+ }
// The EncryptedKey element is added
Element encryptedKeyElement = cipher.martial(document, encryptedKey);
@@ -259,13 +283,14 @@
* the cipher data.
* @param addEncryptedKeyInKeyInfo Should the encrypted key be inside a KeyInfo
* or added as a peer of Cipher Data
- * @return An element that has the wrappingElementQName
- * @throws Exception
+ * @return An element that has the wrappingElementQName
+ * @throws ProcessingException
+ * @throws ConfigurationException
*/
public static Element encryptElementInDocument(Document document,
PublicKey publicKey,
SecretKey secretKey, int keySize, QName wrappingElementQName,
- boolean addEncryptedKeyInKeyInfo) throws Exception
+ boolean addEncryptedKeyInKeyInfo) throws ProcessingException,
ConfigurationException
{
String wrappingElementPrefix = wrappingElementQName.getPrefix();
if(wrappingElementPrefix == null || wrappingElementPrefix == "")
@@ -276,10 +301,25 @@
String encryptionAlgorithm = getXMLEncryptionURL(secretKey.getAlgorithm(),
keySize);
//Encrypt the Document
- cipher = XMLCipher.getInstance(encryptionAlgorithm);
- cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
+ try
+ {
+ cipher = XMLCipher.getInstance(encryptionAlgorithm);
+ cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);
+ }
+ catch (XMLEncryptionException e1)
+ {
+ throw new ConfigurationException(e1);
+ }
- Document encryptedDoc = cipher.doFinal(document, document.getDocumentElement());
+ Document encryptedDoc;
+ try
+ {
+ encryptedDoc = cipher.doFinal(document, document.getDocumentElement());
+ }
+ catch (Exception e)
+ {
+ throw new ProcessingException(e);
+ }
// The EncryptedKey element is added
Element encryptedKeyElement = cipher.martial(document, encryptedKey);
@@ -331,11 +371,12 @@
* Decrypt an encrypted element inside a document
* @param documentWithEncryptedElement
* @param privateKey key need to unwrap the encryption key
- * @return the document with the encrypted element replaced by the data element
- * @throws Exception
+ * @return the document with the encrypted element replaced by the data element
+ * @throws XMLEncryptionException
+ * @throws ProcessingException
*/
public static Element decryptElementInDocument(Document documentWithEncryptedElement,
- PrivateKey privateKey) throws Exception
+ PrivateKey privateKey) throws ProcessingException
{
if(documentWithEncryptedElement == null)
throw new IllegalArgumentException("Input document is null");
@@ -359,22 +400,40 @@
encKeyElement = (Element) nodeList.item(0);
}
- XMLCipher cipher = XMLCipher.getInstance();
- cipher.init(XMLCipher.DECRYPT_MODE, null);
- EncryptedData encryptedData =
cipher.loadEncryptedData(documentWithEncryptedElement, encDataElement);
- EncryptedKey encryptedKey = cipher.loadEncryptedKey(documentWithEncryptedElement,
encKeyElement);
+ XMLCipher cipher;
+ EncryptedData encryptedData;
+ EncryptedKey encryptedKey;
+ try
+ {
+ cipher = XMLCipher.getInstance();
+ cipher.init(XMLCipher.DECRYPT_MODE, null);
+ encryptedData = cipher.loadEncryptedData(documentWithEncryptedElement,
encDataElement);
+ encryptedKey = cipher.loadEncryptedKey(documentWithEncryptedElement,
encKeyElement);
+ }
+ catch (XMLEncryptionException e1)
+ {
+ throw new ProcessingException(e1);
+ }
Document decryptedDoc = null;
if (encryptedData != null && encryptedKey != null)
{
- String encAlgoURL = encryptedData.getEncryptionMethod().getAlgorithm();
- XMLCipher keyCipher = XMLCipher.getInstance();
- keyCipher.init(XMLCipher.UNWRAP_MODE, privateKey);
- Key encryptionKey = keyCipher.decryptKey( encryptedKey, encAlgoURL );
- cipher = XMLCipher.getInstance();
- cipher.init(XMLCipher.DECRYPT_MODE, encryptionKey);
- decryptedDoc = cipher.doFinal(documentWithEncryptedElement, encDataElement);
+ try
+ {
+ String encAlgoURL = encryptedData.getEncryptionMethod().getAlgorithm();
+ XMLCipher keyCipher = XMLCipher.getInstance();
+ keyCipher.init(XMLCipher.UNWRAP_MODE, privateKey);
+ Key encryptionKey = keyCipher.decryptKey( encryptedKey, encAlgoURL );
+ cipher = XMLCipher.getInstance();
+ cipher.init(XMLCipher.DECRYPT_MODE, encryptionKey);
+
+ decryptedDoc = cipher.doFinal(documentWithEncryptedElement, encDataElement);
+ }
+ catch (Exception e)
+ {
+ throw new ProcessingException(e);
+ }
}
Element decryptedRoot = decryptedDoc.getDocumentElement();