Ejemplo n.º 1
0
 private void SignXPS(string inputFile)
 {
     try
     {
         Licensing.ShowDemoMessage();
         DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate);
         XpsDocument xpsDocument = new XpsDocument(inputFile, FileAccess.ReadWrite);
         if (!this.AllowMultipleSignatures)
         {
             XpsSignatureDefinition xpsSignatureDefinition = new XpsSignatureDefinition()
             {
                 Intent        = this.SigningIntent,
                 SigningLocale = this.SigningLocation,
                 SpotId        = new Guid?(Guid.NewGuid())
             };
             IXpsFixedDocumentReader item = xpsDocument.FixedDocumentSequenceReader.FixedDocuments[0];
             item.AddSignatureDefinition(xpsSignatureDefinition);
             item.CommitSignatureDefinition();
             X509Certificate2 digitalSignatureCertificate = this.DigitalSignatureCertificate;
             Guid?            spotId = xpsSignatureDefinition.SpotId;
             xpsDocument.SignDigitally(digitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.SignatureOrigin, spotId.Value);
         }
         else
         {
             xpsDocument.SignDigitally(this.DigitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.None);
         }
         xpsDocument.Close();
     }
     catch
     {
         throw;
     }
 }
Ejemplo n.º 2
0
        }     // end:IterateSignatures()

        // ------------------------------ SignXps -----------------------------
        /// <summary>
        ///   Signs an XPS document with a given X509 certificate, and if one
        ///   exists, associates the signature with a given SignatureDefintion
        ///   spotId GUID.</summary>
        /// <param name="xpsDocument">
        ///   The XPS document to sign.</param>
        /// <param name="cert">
        ///   The X509 certificate to use for signing.</param>
        /// <param name="spotId">
        ///   The nullable spotId GUID of the signature definition.</param>
        public void SignXps(
            XpsDocument xpsDocument, X509Certificate cert, Guid?spotId)
        {
            // If there's a SignatureDefinition spotId,
            // associate it with the signature.
            if (spotId != null)
            {
                xpsDocument.SignDigitally(
                    cert, true, XpsDigSigPartAlteringRestrictions.None,
                    spotId.Value);
            }
            else
            {
                xpsDocument.SignDigitally(
                    cert, true, XpsDigSigPartAlteringRestrictions.None);
            }
        } // end:SignXps()
Ejemplo n.º 3
0
        /// <summary>
        /// See IDigitalSignatureProvider
        /// </summary>
        void IDigitalSignatureProvider.SignDocument(DigitalSignature digitalSignature)
        {
            AssertIsSignable();

            XpsDigSigPartAlteringRestrictions reachRestrictions = XpsDigSigPartAlteringRestrictions.None;


            if (digitalSignature.IsDocumentPropertiesRestricted)
            {
                reachRestrictions |= XpsDigSigPartAlteringRestrictions.CoreMetadata;
            }

            // If additional signatures should invalidate this signature, we
            // need to sign the signature origin part
            if (digitalSignature.IsAddingSignaturesRestricted)
            {
                reachRestrictions |= XpsDigSigPartAlteringRestrictions.SignatureOrigin;
            }

            // a null guid means there was no associated spot, so create a guid
            if (digitalSignature.GuidID == null)
            {
                digitalSignature.GuidID = Guid.NewGuid();
            }

            XpsDigitalSignature xpsDigitalSignature =
                XpsDocument.SignDigitally(
                    digitalSignature.Certificate,
                    true,
                    reachRestrictions,
                    (Guid)digitalSignature.GuidID,
                    false  /* don't re-verify IsSignable, we've already done it */
                    );


            if (xpsDigitalSignature != null)
            {
                // Fill in relevant fields from the XPS signature
                digitalSignature.XpsDigitalSignature = xpsDigitalSignature;
                digitalSignature.SignatureState      = SignatureStatus.Valid;
                digitalSignature.SignedOn            = xpsDigitalSignature.SigningTime;

                // Save the simple name from the certificate as the subject name
                // in the signature
                digitalSignature.SubjectName =
                    digitalSignature.Certificate.GetNameInfo(
                        X509NameType.SimpleName,
                        false /* don't include issuer name */);

                // Add the new signature to the list (if it isn't already there).
                // That is a possibility since the first signature in a document
                // is always added as a signature definition and a signature.
                if (!DigitalSignatureList.Contains(digitalSignature))
                {
                    DigitalSignatureList.Add(digitalSignature);
                }
            }
        }
Ejemplo n.º 4
0
 private void SignXPSStream(Stream inputStream)
 {
     try
     {
         Licensing.ShowDemoMessage();
         DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate);
         using (Package package = Package.Open(inputStream, FileMode.Open, FileAccess.ReadWrite))
         {
             string str = "memorystream://myXps.xps";
             Uri    uri = new Uri(str);
             PackageStore.AddPackage(uri, package);
             XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Maximum, str);
             if (!this.AllowMultipleSignatures)
             {
                 XpsSignatureDefinition xpsSignatureDefinition = new XpsSignatureDefinition()
                 {
                     Intent        = this.SigningIntent,
                     SigningLocale = this.SigningLocation,
                     SpotId        = new Guid?(Guid.NewGuid())
                 };
                 IXpsFixedDocumentReader item = xpsDocument.FixedDocumentSequenceReader.FixedDocuments[0];
                 item.AddSignatureDefinition(xpsSignatureDefinition);
                 item.CommitSignatureDefinition();
                 X509Certificate2 digitalSignatureCertificate = this.DigitalSignatureCertificate;
                 Guid?            spotId = xpsSignatureDefinition.SpotId;
                 xpsDocument.SignDigitally(digitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.SignatureOrigin, spotId.Value);
             }
             else
             {
                 xpsDocument.SignDigitally(this.DigitalSignatureCertificate, true, XpsDigSigPartAlteringRestrictions.None);
             }
             PackageStore.RemovePackage(uri);
             xpsDocument.Close();
         }
     }
     catch
     {
         throw;
     }
 }