You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@santuario.apache.org by mujahedsyed <ms...@gmail.com> on 2014/07/23 12:22:57 UTC
newbie - decryption stream using stax
Hi,
I have followed sample test cases and observed that I am able to encrypt my
xml successfully but when I try to decrypt and use the decryptUsingStAX
method in EncryptionUtils it returns void unlike the encryptionUsingStax
that returned ByteArrayOutputStream.
So, for encryption I was easily able to write the encrypted file to the xml
file. But for decryption I am unable to figure out how I can proceed I have
been trying various things; like I changed the return type of
decryptionusingStax method to document and at the end of the method I had
Document document = StAX2DOM.readDoc(
XMLUtils.createDocumentBuilder(false), securityStreamReader);
return document
But this is returning empty xml file.
I have also followed advise to review this class
http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java?revision=1589370&view=co
but unfortunately because I am using Stax I am unable to work around this.
The problem for me is that I am new both to Stax concepts and xml security
and really finding it difficult to return byteoutputarryastream for
decryptionusingstax method and would really appreciate if someone can throw
some light.
Thanks very much in advance,
Regards
Mujahed
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by mujahedsyed <ms...@gmail.com>.
Hi Colm,
Apologies for delay in responding, I was away. Thanks for the response.
Yes I tried mvn clean install following is the output:
Running org.apache.coheigea.santuario.xmlencryption.EncryptionStAXTest
result:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec
Also, the document that is returned by the readDoc method in StAX2DOM class
is null, I have verified that by repositioning debug point on the return
statement of the method.
Thanks.
Regards,
Mujahed Syed
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41307.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by Colm O hEigeartaigh <co...@apache.org>.
If you run the patched project with "mvn clean install", do you not see the
output on the console window? Your breakpoint appears to be before the
stream is read into the document, and so you wouldn't expect any content to
be in the document at that point.
Colm.
On Sat, Jul 26, 2014 at 12:28 AM, mujahedsyed <ms...@gmail.com> wrote:
> Hi Colm,
>
> Thank you very much for the patch.
>
> I have applied the patch to the code available on github here
>
> https://github.com/coheigea/testcases/tree/master/apache/santuario/santuario-xml-encryption
>
> after applying the patch successfully I ran the test and the result is
> still
> the same I dont get the xml back, I have debug the junit test and it looks
> like the document object that is being returned by stax2dom.java class is
> being returned as null. I am not sure what I am doing wrong, I have
> attached
> the screenshot, would appreciate if you can advise.
>
> Thanks.
> <http://apache-xml-project.6118.n7.nabble.com/file/n41297/screenshot.png>
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41297.html
> Sent from the Apache XML - Security - Dev mailing list archive at
> Nabble.com.
>
--
Colm O hEigeartaigh
Talend Community Coder
http://coders.talend.com
Re: newbie - decryption stream using stax
Posted by mujahedsyed <ms...@gmail.com>.
Hi Colm,
Thank you very much for the patch.
I have applied the patch to the code available on github here
https://github.com/coheigea/testcases/tree/master/apache/santuario/santuario-xml-encryption
after applying the patch successfully I ran the test and the result is still
the same I dont get the xml back, I have debug the junit test and it looks
like the document object that is being returned by stax2dom.java class is
being returned as null. I am not sure what I am doing wrong, I have attached
the screenshot, would appreciate if you can advise.
Thanks.
<http://apache-xml-project.6118.n7.nabble.com/file/n41297/screenshot.png>
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41297.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by Colm O hEigeartaigh <co...@apache.org>.
The following patch for the encryption testcase on github works for me...
Colm.
On Wed, Jul 23, 2014 at 5:02 PM, mujahedsyed <ms...@gmail.com> wrote:
> Hi Colm,
>
> Thank you for the reply.
>
> As per decryptUsingStAX method in EncryptionUtils class I tried to code
> something similar but it returns a document object rather void:
>
> public static Document decryptUsingStAX(InputStream inputStream,
> List<QName> namesToEncrypt, Key privateKey) throws
> Exception {
> // Set up the Configuration
> XMLSecurityProperties properties = new
> XMLSecurityProperties();
> List<XMLSecurityConstants.Action> actions = new
> ArrayList<XMLSecurityConstants.Action>();
> actions.add(XMLSecurityConstants.ENCRYPT);
> properties.setActions(actions);
>
> properties.setDecryptionKey(privateKey);
>
> InboundXMLSec inboundXMLSec =
> XMLSec.getInboundWSSec(properties);
>
> XMLInputFactory xmlInputFactory =
> XMLInputFactory.newInstance();
> final XMLStreamReader xmlStreamReader = xmlInputFactory
> .createXMLStreamReader(inputStream);
>
> TestSecurityEventListener eventListener = new
> TestSecurityEventListener();
> XMLStreamReader securityStreamReader =
> inboundXMLSec.processInMessage(
> xmlStreamReader, null, eventListener);
>
> while (securityStreamReader.hasNext()) {
> securityStreamReader.next();
> }
>
> xmlStreamReader.close();
> inputStream.close();
>
> // Check that what we were expecting to be encrypted was
> actually
> // encrypted
> List<EncryptedElementSecurityEvent> encryptedElementEvents
> = eventListener
>
> .getSecurityEvents(SecurityEventConstants.EncryptedElement);
>
> for (int i = 0; i < encryptedElementEvents.size(); i++) {
> System.out.println(i + ": " +
> encryptedElementEvents.get(i));
> }
>
> for (QName nameToEncrypt : namesToEncrypt) {
> boolean found = false;
> for (EncryptedElementSecurityEvent
> encryptedElement :
> encryptedElementEvents) {
> if (encryptedElement.isEncrypted()
> && nameToEncrypt
>
> .equals(getEncryptedQName(encryptedElement
>
> .getElementPath()))) {
> found = true;
> System.out.println("nameToEncrypt
> " + nameToEncrypt);
> break;
> }
> if (!found) {
> throw new Exception("Element not
> found");
> }
> }
> }
>
> Document document = StAX2DOM.readDoc(
> XMLUtils.createDocumentBuilder(false),
> securityStreamReader);
> return document;
> }
>
> Now, in my main method I have instructions like:
> // Decrypt
> Key privateKey = keyStore
> .getKey("myservicekey",
> "skpass".toCharArray());
> ByteArrayInputStream bais = new
> ByteArrayInputStream(baos.toByteArray());
>
> Document result = EncryptionUtils.decryptUsingStAX(bais,
> namesToEncrypt, privateKey);
> // check decrypt done successfully
> String resultString = getStringFromDocument(result);
> System.out.println("result:\n" + resultString.toString());
>
> The method getStringFromDocument is a helper method
>
> public String getStringFromDocument(Document doc) {
> try {
> DOMSource domSource = new DOMSource(doc);
> StringWriter writer = new StringWriter();
> StreamResult result = new StreamResult(writer);
> TransformerFactory tf =
> TransformerFactory.newInstance();
> Transformer transformer = tf.newTransformer();
> transformer.transform(domSource, result);
> return writer.toString();
> } catch (TransformerException ex) {
> ex.printStackTrace();
> return null;
> }
> }
>
> The output is:
> result:
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
>
>
> Apologies if the question looks stupid but I am really struggling to figure
> out the way to achieve the task, I would appreciate your assistance.
> Thanks very much in advance,
> Regards
> Mujahed
>
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41286.html
> Sent from the Apache XML - Security - Dev mailing list archive at
> Nabble.com.
>
--
Colm O hEigeartaigh
Talend Community Coder
http://coders.talend.com
Re: newbie - decryption stream using stax
Posted by mujahedsyed <ms...@gmail.com>.
Hi Marc, Colm,
you are absolutely right , it works fine once i comment the while loop. I
definitely need some more time to spend on the stax, streams concepts.
Thank you very much both of you for the help.
Cheers!
Regards
Mujahed
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41310.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by Marc Giger <gi...@apache.org>.
Hi Mujahed,
On Tue, 29 Jul 2014 03:04:29 -0700 (PDT)
mujahedsyed <ms...@gmail.com> wrote:
> Hi Marc,
>
> Thanks for looking into this ticket. I appreciate your hints but I
> would like to let you know that I am new to both StAX and encryption
> concepts.
>
> From the hints you provided I am thinking that you wish to say while
> loop reads everything so nothing more is left for the other method to
> read.
Exactly! and therefore the empty document is expected.
>
> As advised by Colm my understanding is also that the while loop is
> reading the stream, adding to this there is a listener pattern so it
> is collecting security events.
The security events have nothing todo with the document structure
itself. They are generated on the fly when processing a document
to allow the "user" to see what exactly was encrypted and/or signed and how.
As said, these events are generated on the fly when you "leech" on
the (security-)streamReader.
>
> The expected output from below statement is that a document object is
> returned which has decrypted xml in it.
Yes, if nothing else uses the security-streamReader beforehand as the
while loop did. So simply replace the while loop with the call
StAX2DOM.readDoc() and world is fine again:-) :
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
TestSecurityEventListener eventListener = new TestSecurityEventListener();
XMLStreamReader securityStreamReader =
inboundXMLSec.processInMessage(xmlStreamReader, null, eventListener);
final Document doc = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);
// Check that what we were expecting to be encrypted was actually encrypted
List<EncryptedElementSecurityEvent> encryptedElementEvents =
eventListener.getSecurityEvents(SecurityEventConstants.EncryptedElement);
Assert.assertNotNull(encryptedElementEvents);
...
return doc;
I hope its clear now so far.
Thanks,
Marc
Re: newbie - decryption stream using stax
Posted by mujahedsyed <ms...@gmail.com>.
Hi Marc,
Thanks for looking into this ticket. I appreciate your hints but I would
like to let you know that I am new to both StAX and encryption concepts.
>From the hints you provided I am thinking that you wish to say while loop
reads everything so nothing more is left for the other method to read.
As advised by Colm my understanding is also that the while loop is reading
the stream, adding to this there is a listener pattern so it is collecting
security events.
The expected output from below statement is that a document object is
returned which has decrypted xml in it.
Document document = StAX2DOM.readDoc(
> > > XMLUtils.createDocumentBuilder(false),
> > > securityStreamReader); return document;
Please correct me if you think I am wrong, appreciate your assistance and
time.
Thanks
Regards
Mujahed Syed
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41308.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by Marc Giger <gi...@apache.org>.
On Mon, 28 Jul 2014 19:49:59 +0100
Colm O hEigeartaigh <co...@apache.org> wrote:
> > Question: What does the while loop above exactly do?
>
> Is that question addressed to me? It reads from the InputStream...
>
> https://github.com/coheigea/testcases/blob/master/apache/santuario/santuario-xml-encryption/src/test/java/org/apache/coheigea/santuario/xmlencryption/EncryptionUtils.java
>
> Colm.
Sorry, it was primarily addressed to Mujahed, but since it's also wrong
in the patch you provided to Mujahed the question may also be
something for you:-)
Hehe, yup, it reads from the inputStream...and how long? And moreover
what is the expected outcome from
Document document = StAX2DOM.readDoc(
> > > XMLUtils.createDocumentBuilder(false),
> > > securityStreamReader); return document;
after the loop?
Mujahed?
Marc
>
>
> On Mon, Jul 28, 2014 at 6:47 PM, Marc Giger <gi...@apache.org> wrote:
>
> > Hi,
> >
> > > XMLStreamReader securityStreamReader =
> > > inboundXMLSec.processInMessage(
> > > xmlStreamReader, null,
> > > eventListener);
> > >
> >
> > ...
> >
> > > while (securityStreamReader.hasNext()) {
> > > securityStreamReader.next();
> > > }
> > >
> >
> > Question: What does the while loop above exactly do?
> >
> > >
> > > Document document = StAX2DOM.readDoc(
> > > XMLUtils.createDocumentBuilder(false),
> > > securityStreamReader); return document;
> > > }
> > >
> >
> > ...
> >
> > > The output is:
> > > result:
> > > <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> >
> > When you have found the answer you will see that this is the
> > expected output from the above code. Also it will be
> > very easy for you to change the code so that it works
> > as desired/expected.
> >
> > Please share the solution or come back if my tip didn't help you.
> >
> > Thanks,
> >
> > Marc
> >
>
>
>
> --
> Colm O hEigeartaigh
>
> Talend Community Coder
> http://coders.talend.com
Re: newbie - decryption stream using stax
Posted by Colm O hEigeartaigh <co...@apache.org>.
> Question: What does the while loop above exactly do?
Is that question addressed to me? It reads from the InputStream...
https://github.com/coheigea/testcases/blob/master/apache/santuario/santuario-xml-encryption/src/test/java/org/apache/coheigea/santuario/xmlencryption/EncryptionUtils.java
Colm.
On Mon, Jul 28, 2014 at 6:47 PM, Marc Giger <gi...@apache.org> wrote:
> Hi,
>
> > XMLStreamReader securityStreamReader =
> > inboundXMLSec.processInMessage(
> > xmlStreamReader, null, eventListener);
> >
>
> ...
>
> > while (securityStreamReader.hasNext()) {
> > securityStreamReader.next();
> > }
> >
>
> Question: What does the while loop above exactly do?
>
> >
> > Document document = StAX2DOM.readDoc(
> > XMLUtils.createDocumentBuilder(false),
> > securityStreamReader); return document;
> > }
> >
>
> ...
>
> > The output is:
> > result:
> > <?xml version="1.0" encoding="UTF-8" standalone="no"?>
>
> When you have found the answer you will see that this is the
> expected output from the above code. Also it will be
> very easy for you to change the code so that it works
> as desired/expected.
>
> Please share the solution or come back if my tip didn't help you.
>
> Thanks,
>
> Marc
>
--
Colm O hEigeartaigh
Talend Community Coder
http://coders.talend.com
Re: newbie - decryption stream using stax
Posted by Marc Giger <gi...@apache.org>.
Hi,
> XMLStreamReader securityStreamReader =
> inboundXMLSec.processInMessage(
> xmlStreamReader, null, eventListener);
>
...
> while (securityStreamReader.hasNext()) {
> securityStreamReader.next();
> }
>
Question: What does the while loop above exactly do?
>
> Document document = StAX2DOM.readDoc(
> XMLUtils.createDocumentBuilder(false),
> securityStreamReader); return document;
> }
>
...
> The output is:
> result:
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
When you have found the answer you will see that this is the
expected output from the above code. Also it will be
very easy for you to change the code so that it works
as desired/expected.
Please share the solution or come back if my tip didn't help you.
Thanks,
Marc
Re: newbie - decryption stream using stax
Posted by mujahedsyed <ms...@gmail.com>.
Hi Colm,
Thank you for the reply.
As per decryptUsingStAX method in EncryptionUtils class I tried to code
something similar but it returns a document object rather void:
public static Document decryptUsingStAX(InputStream inputStream,
List<QName> namesToEncrypt, Key privateKey) throws Exception {
// Set up the Configuration
XMLSecurityProperties properties = new XMLSecurityProperties();
List<XMLSecurityConstants.Action> actions = new
ArrayList<XMLSecurityConstants.Action>();
actions.add(XMLSecurityConstants.ENCRYPT);
properties.setActions(actions);
properties.setDecryptionKey(privateKey);
InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLStreamReader xmlStreamReader = xmlInputFactory
.createXMLStreamReader(inputStream);
TestSecurityEventListener eventListener = new TestSecurityEventListener();
XMLStreamReader securityStreamReader = inboundXMLSec.processInMessage(
xmlStreamReader, null, eventListener);
while (securityStreamReader.hasNext()) {
securityStreamReader.next();
}
xmlStreamReader.close();
inputStream.close();
// Check that what we were expecting to be encrypted was actually
// encrypted
List<EncryptedElementSecurityEvent> encryptedElementEvents = eventListener
.getSecurityEvents(SecurityEventConstants.EncryptedElement);
for (int i = 0; i < encryptedElementEvents.size(); i++) {
System.out.println(i + ": " + encryptedElementEvents.get(i));
}
for (QName nameToEncrypt : namesToEncrypt) {
boolean found = false;
for (EncryptedElementSecurityEvent encryptedElement :
encryptedElementEvents) {
if (encryptedElement.isEncrypted()
&& nameToEncrypt
.equals(getEncryptedQName(encryptedElement
.getElementPath()))) {
found = true;
System.out.println("nameToEncrypt " + nameToEncrypt);
break;
}
if (!found) {
throw new Exception("Element not found");
}
}
}
Document document = StAX2DOM.readDoc(
XMLUtils.createDocumentBuilder(false), securityStreamReader);
return document;
}
Now, in my main method I have instructions like:
// Decrypt
Key privateKey = keyStore
.getKey("myservicekey", "skpass".toCharArray());
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Document result = EncryptionUtils.decryptUsingStAX(bais,
namesToEncrypt, privateKey);
// check decrypt done successfully
String resultString = getStringFromDocument(result);
System.out.println("result:\n" + resultString.toString());
The method getStringFromDocument is a helper method
public String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException ex) {
ex.printStackTrace();
return null;
}
}
The output is:
result:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Apologies if the question looks stupid but I am really struggling to figure
out the way to achieve the task, I would appreciate your assistance.
Thanks very much in advance,
Regards
Mujahed
--
View this message in context: http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41286.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.
Re: newbie - decryption stream using stax
Posted by Colm O hEigeartaigh <co...@apache.org>.
Why don't you post the code you are using to decrypt the stream + we can
take a look?
Colm.
On Wed, Jul 23, 2014 at 11:22 AM, mujahedsyed <ms...@gmail.com> wrote:
> Hi,
>
> I have followed sample test cases and observed that I am able to encrypt my
> xml successfully but when I try to decrypt and use the decryptUsingStAX
> method in EncryptionUtils it returns void unlike the encryptionUsingStax
> that returned ByteArrayOutputStream.
>
> So, for encryption I was easily able to write the encrypted file to the xml
> file. But for decryption I am unable to figure out how I can proceed I have
> been trying various things; like I changed the return type of
> decryptionusingStax method to document and at the end of the method I had
>
>
> Document document = StAX2DOM.readDoc(
> XMLUtils.createDocumentBuilder(false),
> securityStreamReader);
> return document
>
> But this is returning empty xml file.
>
> I have also followed advise to review this class
>
> http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/encryption/DecryptionTest.java?revision=1589370&view=co
> but unfortunately because I am using Stax I am unable to work around this.
>
> The problem for me is that I am new both to Stax concepts and xml security
> and really finding it difficult to return byteoutputarryastream for
> decryptionusingstax method and would really appreciate if someone can throw
> some light.
>
> Thanks very much in advance,
> Regards
> Mujahed
>
>
>
> --
> View this message in context:
> http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280.html
> Sent from the Apache XML - Security - Dev mailing list archive at
> Nabble.com.
>
--
Colm O hEigeartaigh
Talend Community Coder
http://coders.talend.com