* Encrypt the Key to be transported @@ -151,7 +88,7 @@ public class XMLEncryptionUtil { */ public static EncryptedKey encryptKey(Document document, SecretKey keyToBeEncrypted, PublicKey keyUsedToEncryptSecretKey, int keySize) throws ProcessingException { - XMLCipher keyCipher = null; + XMLCipher keyCipher; String pubKeyAlg = keyUsedToEncryptSecretKey.getAlgorithm(); try { @@ -170,14 +107,13 @@ public class XMLEncryptionUtil { * data * * @param elementQName QName of the element that we like to encrypt + * @param document * @param publicKey * @param secretKey * @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 ProcessingException */ public static void encryptElement(QName elementQName, Document document, PublicKey publicKey, SecretKey secretKey, @@ -187,7 +123,7 @@ public class XMLEncryptionUtil { if (document == null) throw logger.nullArgumentError("document"); String wrappingElementPrefix = wrappingElementQName.getPrefix(); - if (wrappingElementPrefix == null || wrappingElementPrefix == "") + if (wrappingElementPrefix == null || "".equals(wrappingElementPrefix)) throw logger.wrongTypeError("Wrapping element prefix invalid"); Element documentElement = DocumentUtil.getElement(document, elementQName); @@ -217,18 +153,22 @@ public class XMLEncryptionUtil { // The EncryptedKey element is added Element encryptedKeyElement = cipher.martial(document, encryptedKey); - String wrappingElementName = wrappingElementPrefix + ":" + wrappingElementQName.getLocalPart(); - - // Create the wrapping element and set its attribute NS - Element wrappingElement = encryptedDoc.createElementNS(wrappingElementQName.getNamespaceURI(), wrappingElementName); + final String wrappingElementName; if (StringUtil.isNullOrEmpty(wrappingElementPrefix)) { wrappingElementName = wrappingElementQName.getLocalPart(); + } else { + wrappingElementName = wrappingElementPrefix + ":" + wrappingElementQName.getLocalPart(); + } + // Create the wrapping element and set its attribute NS + Element wrappingElement = encryptedDoc.createElementNS(wrappingElementQName.getNamespaceURI(), wrappingElementName); + + if (! StringUtil.isNullOrEmpty(wrappingElementPrefix)) { + wrappingElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + wrappingElementPrefix, wrappingElementQName.getNamespaceURI()); } - wrappingElement.setAttributeNS(XMLNS, "xmlns:" + wrappingElementPrefix, wrappingElementQName.getNamespaceURI()); // Get Hold of the Cipher Data - NodeList cipherElements = encryptedDoc.getElementsByTagNameNS(XMLENC_NS, "EncryptedData"); + NodeList cipherElements = encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA); if (cipherElements == null || cipherElements.getLength() == 0) throw logger.domMissingElementError("xenc:EncryptedData"); Element encryptedDataElement = (Element) cipherElements.item(0); @@ -240,12 +180,12 @@ public class XMLEncryptionUtil { if (addEncryptedKeyInKeyInfo) { // Outer ds:KeyInfo Element to hold the EncryptionKey - Element sigElement = encryptedDoc.createElementNS(XMLSIG_NS, DS_KEY_INFO); - sigElement.setAttributeNS(XMLNS, "xmlns:ds", XMLSIG_NS); + Element sigElement = encryptedDoc.createElementNS(XMLSignature.XMLNS, DS_KEY_INFO); + sigElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ds", XMLSignature.XMLNS); sigElement.appendChild(encryptedKeyElement); // Insert the Encrypted key before the CipherData element - NodeList nodeList = encryptedDoc.getElementsByTagNameNS(XMLENC_NS, CIPHER_DATA_LOCALNAME); + NodeList nodeList = encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_CIPHERDATA); if (nodeList == null || nodeList.getLength() == 0) throw logger.domMissingElementError("xenc:CipherData"); Element cipherDataElement = (Element) nodeList.item(0); @@ -328,12 +268,12 @@ public class XMLEncryptionUtil { Element encryptedKeyElement = cipher.martial(document, encryptedKey); // Outer ds:KeyInfo Element to hold the EncryptionKey - Element sigElement = encryptedDoc.createElementNS(XMLSIG_NS, DS_KEY_INFO); - sigElement.setAttributeNS(XMLNS, "xmlns:ds", XMLSIG_NS); + Element sigElement = encryptedDoc.createElementNS(XMLSignature.XMLNS, DS_KEY_INFO); + sigElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ds", XMLSignature.XMLNS); sigElement.appendChild(encryptedKeyElement); // Insert the Encrypted key before the CipherData element - NodeList nodeList = encryptedDoc.getElementsByTagNameNS(XMLENC_NS, CIPHER_DATA_LOCALNAME); + NodeList nodeList = encryptedDoc.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_CIPHERDATA); if (nodeList == null || nodeList.getLength() == 0) throw logger.domMissingElementError("xenc:CipherData"); Element cipherDataElement = (Element) nodeList.item(0); @@ -342,7 +282,7 @@ public class XMLEncryptionUtil { } /** - * Encrypt the root document element inside a Document. NOTE:> The document root element will be replaced by + * Encrypt the root document element inside a Document. NOTE: The document root element will be replaced by * the * wrapping element. * @@ -361,7 +301,7 @@ public class XMLEncryptionUtil { public static Element encryptElementInDocument(Document document, PublicKey publicKey, SecretKey secretKey, int keySize, QName wrappingElementQName, boolean addEncryptedKeyInKeyInfo) throws ProcessingException, ConfigurationException { String wrappingElementPrefix = wrappingElementQName.getPrefix(); - if (wrappingElementPrefix == null || wrappingElementPrefix == "") + if (wrappingElementPrefix == null || "".equals(wrappingElementPrefix)) throw logger.wrongTypeError("Wrapping element prefix invalid"); XMLCipher cipher = null; @@ -386,15 +326,19 @@ public class XMLEncryptionUtil { // The EncryptedKey element is added Element encryptedKeyElement = cipher.martial(document, encryptedKey); - String wrappingElementName = wrappingElementPrefix + ":" + wrappingElementQName.getLocalPart(); - - // Create the wrapping element and set its attribute NS - Element wrappingElement = encryptedDoc.createElementNS(wrappingElementQName.getNamespaceURI(), wrappingElementName); + final String wrappingElementName; if (StringUtil.isNullOrEmpty(wrappingElementPrefix)) { wrappingElementName = wrappingElementQName.getLocalPart(); + } else { + wrappingElementName = wrappingElementPrefix + ":" + wrappingElementQName.getLocalPart(); + } + // Create the wrapping element and set its attribute NS + Element wrappingElement = encryptedDoc.createElementNS(wrappingElementQName.getNamespaceURI(), wrappingElementName); + + if (! StringUtil.isNullOrEmpty(wrappingElementPrefix)) { + wrappingElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + wrappingElementPrefix, wrappingElementQName.getNamespaceURI()); } - wrappingElement.setAttributeNS(XMLNS, "xmlns:" + wrappingElementPrefix, wrappingElementQName.getNamespaceURI()); Element encryptedDocRootElement = encryptedDoc.getDocumentElement(); // Bring in the encrypted wrapping element to wrap the root node @@ -404,12 +348,12 @@ public class XMLEncryptionUtil { if (addEncryptedKeyInKeyInfo) { // Outer ds:KeyInfo Element to hold the EncryptionKey - Element sigElement = encryptedDoc.createElementNS(XMLSIG_NS, DS_KEY_INFO); - sigElement.setAttributeNS(XMLNS, "xmlns:ds", XMLSIG_NS); + Element sigElement = encryptedDoc.createElementNS(XMLSignature.XMLNS, DS_KEY_INFO); + sigElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ds", XMLSignature.XMLNS); sigElement.appendChild(encryptedKeyElement); // Insert the Encrypted key before the CipherData element - NodeList nodeList = encryptedDocRootElement.getElementsByTagNameNS(XMLENC_NS, CIPHER_DATA_LOCALNAME); + NodeList nodeList = encryptedDocRootElement.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_CIPHERDATA); if (nodeList == null || nodeList.getLength() == 0) throw logger.domMissingElementError("xenc:CipherData"); @@ -430,9 +374,6 @@ public class XMLEncryptionUtil { * @param privateKey key need to unwrap the encryption key * * @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 ProcessingException { @@ -449,7 +390,7 @@ public class XMLEncryptionUtil { Element encKeyElement = getNextElementNode(encDataElement.getNextSibling()); if (encKeyElement == null) { // Search the enc data element for enc key - NodeList nodeList = encDataElement.getElementsByTagNameNS(XMLENC_NS, ENCRYPTED_KEY_LOCALNAME); + NodeList nodeList = encDataElement.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDKEY); if (nodeList == null || nodeList.getLength() == 0) throw logger.nullValueError("Encrypted Key not found in the enc data"); @@ -522,8 +463,6 @@ public class XMLEncryptionUtil { } if (publicKeyAlgo.contains("RSA")) return RSA_ENCRYPTION_SCHEME; - if (publicKeyAlgo.contains("DES")) - return XMLCipher.TRIPLEDES_KeyWrap; throw logger.unsupportedType("unsupported publicKey Algo:" + publicKeyAlgo); } @@ -548,8 +487,6 @@ public class XMLEncryptionUtil { } if (algo.contains("RSA")) return XMLCipher.RSA_v1dot5; - if (algo.contains("DES")) - return XMLCipher.TRIPLEDES_KeyWrap; throw logger.unsupportedType("Secret Key with unsupported algo:" + algo); }