The current XML Encryption Processing Rules (section 4) state that
If the requirement for replacement is not intentional, we should fix the text. If the requirement is intentional, I propose that it may be too limiting.
Note 1: I am not suggesting that XML Encryption specify an API design, absolutely NOT! However, I don’t want XML Encryption to unnecessarily restrict API designs either.
Note 2: Slides with detailed code are included for completeness; they are not essential for understanding this topic.
Original/Decrypted | Encrypted |
---|---|
<?xml version="1.0" encoding="UTF-8"?> <Customers> <Customer> <Name>Jose Aznar</Name> <CreditCard> <Number>1000 1234 5678 0001</Number> <ExpiryDate>2003 June 30</ExpiryDate> </CreditCard> </Customer> . . . </Customers> |
<?xml version="1.0" encoding="UTF-8"?> <Customers> <Customer> <Name><EncryptedData…></Name> <CreditCard> <Number><EncryptedData…></Number> <ExpiryDate>2003 June 30</ExpiryDate> </Customer> . . . </Customers> |
Encrypting
// Encrypt the content of the/ elements NodeIterator ni2 = XPathAPI.selectNodeIterator(doc,"//CreditCard/Number"); // Encrypt the nodes (only element content is encrypted) while ((node = ni2.nextNode())!= null) { System.out.print("."); xmlencEncryptor.encryptAndReplace((Element)node, true, getEncryptedDataTemplate(desKey, true), desKey);
Decrypting
// Get the nodes to be decrypted NodeList nl2 = DOMUtil.getElementsByTagNameNS( doc, XEncryption.XMLENC_NS, "EncryptedData"); // Decrypt for (int i = 0; i < nl2.getLength(); i++) { System.out.print("."); Element el = (Element)nl2.item(i); xmlencDecryptor.decryptAndReplace(el);
Scenario A: The XML source has no encrypted parts and is protected through authorization instead. However, there is an authorized app which selects certain credit card info for processing. It wants to query Scenario B: The XML source has encrypted elements and content accessible by a number of applications. When one of these applications queries an encrypted element, that app needs to decrypt the element but MUST NOT modify the source.
Encrypting
Note: The preceding code works (uses IBM’s XSS4J) but, according to the spec, its illegal because the XML source is not being replaced.
Decrypting
Don’t want to use decryptAndReplace() because I don’t want to modify the XML source.
But XML Encryption doesn’t allow Toolkits to give me an alternative so I have to use low-level security APIs instead!
Rather, XML Encryption should allow Toolkits to return the decrypted XML element or content without requiring replacement in the source.
Question: Why not create a dummy document before and/or after encrypting?
Answer: Yes, one could create a dummy document and copy in the relevant elements before encrypting or decrypting and still conform to the XML Encryption spec as it currently stands.However, this would be inefficient and often inelegant.
Question: The example code you showed doesn’t deal with more complex context situations such as inherited namespaces, default attributes, etc.. How will those artifacts affect the no-replacement processing of <EncryptedData> elements?
Answer: I think this question will only be answered through more coding and application experience. There could be some issues that arise.
Scenario A: SOAP msg w/ encrypted data
Scenario A: SOAP message
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://www.w3.org/2001/06/soap-envelope">
<Body>
<VerifyCreditCardRequest xmlns="http://…/actions">
<EncryptedData Type="NodeList“ xmlns="http://…/xmlenc">
<EncryptionMethod Algorithm="urn:nist-gov:tripledes…">
<IV>adCwS3wowQ8=</IV>
</EncryptionMethod>
…<CipherData>Ynj…M1f</CipherData>…
</EncryptedData>
</VerifyCreditCardRequest>
</Body>
</Envelope>
Scenario A code
// Encrypt the content of the 2nd
Scenario A code…
Scenario B: Encrypted customer DB
Scenario B code
// Get the nodes to be decrypted
Element elemEncryptedDataToDecrypt = (Element)
DOMUtil.getElementsByTagNameNS(doc, XEncryption.XMLENC_NS,
"EncryptedData").item(5);
Element elemIV = (Element)
elemEncryptedDataToDecrypt.getElementsByTagName("IV").item(0);
String strIV = elemIV.getFirstChild().getNodeValue();
Element elemCipherData = (Element)
elemEncryptedDataToDecrypt.getElementsByTagName("CipherText").item(0);
String strCipherData = elemCipherData.getFirstChild().getNodeValue();
javax.crypto.spec.IvParameterSpec ivparmspec = new
javax.crypto.spec.IvParameterSpec(com.ibm.xml.dsig.Base64.decode(strIV));
Cipher desCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
desCipher.init(Cipher.DECRYPT_MODE, desKey, ivparmspec);
byte[] bytesPlainData = desCipher.doFinal(com.ibm.xml.dsig.Base64.decode(strCipherData));
String strCreditCardNumber = new String(bytesPlainData);
Scenario B code…
QAQ (Quietly Anticipated Questions)