Ejemplo n.º 1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="overrideCurrentSignature"></param>
        /// <from>http://msdn.microsoft.com/en-us/library/system.io.packaging.packagedigitalsignaturemanager.sign(v=vs.100).aspx</from>
        public void Sign(string path, bool overrideCurrentSignature)
        {
            {
                _log.Debug("We're going to try signing {0}, override current signature {1}".format(path,
                                                                                                   overrideCurrentSignature));
                var package = Package.Open(path);

                _log.Debug("Opened {0}".format(path));
                var signatureManager = new PackageDigitalSignatureManager(package)
                    {
                        CertificateOption = CertificateEmbeddingOption.InSignaturePart
                    };

                if (signatureManager.IsSigned)
                {
                    if (overrideCurrentSignature)
                    {
                        _log.Debug("{0} is signed we'll try to remove signatures".format(path));
                        //TODO: make smarter so we only remove signatures for the relevant parts
                        signatureManager.RemoveAllSignatures();
                        package.Flush();
                    }
                    else
                    {
                        _log.Debug("{0} is signed, we're going to throw".format(path));
                        throw new AlreadySignedException();
                    }
                }

                var toSign = package.GetParts().Select(packagePart => packagePart.Uri).ToList();

                toSign.Add(PackUriHelper.GetRelationshipPartUri(signatureManager.SignatureOrigin));
                toSign.Add(signatureManager.SignatureOrigin);
                toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));

                _log.Debug("About to start signing {0}".format(path));
                signatureManager.Sign(toSign, Certificate);
                _log.Debug("signed {0}, going to close".format(path));
                package.Close();

                _log.Debug("closed {0}".format(path));
            }
            GC.Collect();
        }
 /// <summary>
 /// RemoveAll
 /// </summary>
 public static OpenXmlPowerToolsDocument RemoveAll(OpenXmlPowerToolsDocument doc)
 {
     using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc))
     {
         using (Package package = streamDoc.GetPackage())
         {
             // Creates the PackageDigitalSignatureManager
             PackageDigitalSignatureManager digitalSignatureManager = new PackageDigitalSignatureManager(package);
             digitalSignatureManager.RemoveAllSignatures();
         }
         return streamDoc.GetModifiedDocument();
     }
 }