views:

280

answers:

2

I'm trying to digitally sign an XML document using Java. I've got an implementation working with some references I've found that use various implementations in the javax.xml.crypto.dsig package.

However, my current implementation is like many of the examples I've looked at - it's rather verbose and involves using no less than 23 different API classes from the java.xml.crypto.dsig, javax.xml.transform, and java.security packages, among others. It feels like I've entered factory factory factory land, and it took me several hours just to figure out what was going on.

My question is, is there an easier way to do this? If I've got public/private key files and I want to add a <Signature/> to an XML document, is there a library out there that just lets me call something like:

OutputStream signFile(InputStream xmlFile, File privateKey)

...without all of the XMLSignatureFactory/CanonicalizationMethod/DOMSignContext craziness?

I'm not very well-versed in cryptography, and the Java-provided API seems rather daunting for developers like myself trying to become familiar with digital signing. If all of this is necessary or there's currently no friendlier API out there, that's fine and I'm willing to accept that as an answer. I'd just like to know if I'm unnecessarily taking the hard road here.

+1  A: 

Have look at Apache XML Security. To use the package to generate and verify a signature, checkout the samples in src_samples/org/apache/xml/security/samples/signature/.

Pascal Thivent
+1  A: 

I looked at all of the options for signing XML files and decided to go with a non-standard approach. The standards were all way too verbose. Also, I didn't need compatibility with the standards---I just needed signatures on a block of XML.

Probably the easiest way to "sign" a block of XML is to use GPG with a detached signature.

vy32
Nice idea, probably the simplest one provided so far. It doesn't offer all of the configurability of javax.xml.crypto or Apache's Santuario, but that's really what makes those other ones so complex.
Rob Hruska
Thanks. One system that does this takes this approach takes the XML block, computes a signature using an RSA public key and OpenSSL, and then embeds that signature at the end of the XML block in the text file. You can find the code in my afsign.cpp program that's part of AFFLIB, which can be downloaded from http://afflib.org/
vy32
Accepting this one since it's the simpler solution. Pascal's solution is also reasonable, although it's still requires a fair amount of code.
Rob Hruska