Welcome to OpenXML Developer Sign in | Join | Help

Digital Signatures using System.IO.Packaging Classes

Last post 09-09-2006, 11:10 PM by Krishna. 4 replies.
Sort Posts: Previous Next
  •  07-22-2006, 8:41 PM 393

    Digital Signatures using System.IO.Packaging Classes

    Hi,

    I used the following code from the Windows SDK to digitally sign Word Documents but When I open the Signed documents in Word, Word is complaining it can't verify the Signatures and I notices that the Package folder and relatinonship entries are different when using the Word UI to digitally sign a document compared to using the Packaging API, is there anything different that needs to be done compared to the code below from the June CTP of the Windows SDK ?

    private void Sign(Package package)

    {

    string _digitalSignatureUri =

    "/package/services/digital-signature/_rels/origin.psdsor.rels";

    if (package == null)

    throw new ArgumentNullException("SignAllParts(package)");

    // Create the DigitalSignature Manager

    PackageDigitalSignatureManager dsm =

    new PackageDigitalSignatureManager(package);

    dsm.CertificateOption =

    CertificateEmbeddingOption.InSignaturePart;

    // Create a list of all the part URIs in the package to sign

    // (GetParts() also includes PackageRelationship parts).

    System.Collections.Generic.List<Uri> toSign =

    new System.Collections.Generic.List<Uri>();

    foreach (PackagePart packagePart in package.GetParts())

    {

    // Add all package parts to the list for signing.

    toSign.Add(packagePart.Uri);

    }

    // Add the URI for SignatureOrigin PackageRelationship part.

    // The SignatureOrigin relationship is created when Sign() is called.

    // Signing the SignatureOrigin relationship disables counter-signatures.

    Uri uriPartSignatureOriginRelationship = PackUriHelper.CreatePartUri(

    new Uri(_digitalSignatureUri, UriKind.Relative));

    toSign.Add(uriPartSignatureOriginRelationship);

    // Sign() will prompt the user to select a Certificate to sign with.

    try

    {

    dsm.Sign(toSign);

    }

    // If there are no certificates or the SmartCard manager is

    // not running, catch the exception and show an error message.

    catch (CryptographicException ex)

    {

    MessageBox.Show(

    "Cannot Sign\n" + ex.Message,

    "No Digital Certificates Available",

    MessageBoxButton.OK,

    MessageBoxImage.Exclamation);

    }

    }

     

    Thanks,

    Krishna

  •  08-01-2006, 2:51 AM 419 in reply to 393

    Re: Digital Signatures using System.IO.Packaging Classes

    Hi Krishna,

    I've just tried your code and it works for me. I didn't add your _digitalSignatureUri to the list of parts to sign. It is different from how Word signs your document though, the signature created using the packaging API freezes the Word UI because the signed file shouldn't be modified.

    I'll try and find out more details on this.

     


    Wouter van Vugt
  •  08-01-2006, 11:52 PM 437 in reply to 419

    Re: Digital Signatures using System.IO.Packaging Classes

    Thanks !, I think the relationships that Word creates is different from what the Packaging Classes does. For me when I sign using the above code (excluding the _digitalSignatureUri) and open in Word, Word sees the Signature but says it can't verify the signature. Also I noticed the amount of details Captured by Word during Signing (Time, Desktop Resolution, etc.,) are not there when using the Packaging API. Let me know if you find something.
  •  08-25-2006, 12:29 AM 563 in reply to 437

    Re: Digital Signatures using System.IO.Packaging Classes

    Sorry for replying so late to this.   This was a bug in the Packaging API code.  It's been fixed for the RTM build.  Office is looking for the time detail Krishna mentioned in his reply. 

    - Kevin

  •  09-09-2006, 11:10 PM 635 in reply to 563

    Re: Digital Signatures using System.IO.Packaging Classes

    It was based on July CTP build of Vista (5472.5 / 4306), I will try the Code in the RTM build and get back on the result.

    -Krishna

View as RSS news feed in XML