newbie question - how to sign binary files

A

AK

Hello,

I'm trying to create an XML digital signature over some assets listed
in the XML file. It works fine until I add a pdf file in the list of
assets, which throws an exception saying that "Data at the root level
is invalid". I'm sure it must be possible to sign binary files as
well, but I've been searching for hours and can't find any examples.
Hope someone can help me with this.

Here's the code I'm using:

// Create a SignedXml object
SignedXml signedXml = new SignedXml(Doc);

// Add the key
signedXml.SigningKey = Key;

// Get urls to assets with signed = true
assetUris = getAssetUris();

foreach (string assetUri in assetUris)
{
// Create a reference to be signed
Reference reference = new Reference();
reference.Uri = assetUri;

// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

// Add the reference to the SignedXml object
signedXml.AddReference(reference);
}

// Hash the entire doc as well so that the asset description will be
hashed
Reference reference2 = new Reference();
reference2.Uri = "";
signedXml.AddReference(reference2);

// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env2 = new
XmlDsigEnvelopedSignatureTransform();
reference2.AddTransform(env2);

// Add the KeyInfo object holding the certificate into the SignedXml
object
signedXml.KeyInfo = getKeyInfo();

// Compute the signature
signedXml.ComputeSignature();

// Get the XML representation of the signature and save it to an
XmlElement object
XmlElement xmlDigitalSignature = signedXml.GetXml();

// Append the element to the XML document
Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature,
true));


Many thanks,

AK
 
A

AK

And as usual, I'll answer myself in case anyone else might have the
same problem in the future. With a little help from a friend, I
realised that I was trying to apply the transform to the binary files,
which obviously fails.

So, for the binary files, don't do the following:

// Add an enveloped transformation to the reference
XmlDsigEnvelopedSignatureTransform env = new
XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);

Cheers,

AK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top