Beispiel #1
0
 internal static void CheckLicense(string librarySerialNumberLicense)
 {
     try
     {
         if (librarySerialNumberLicense != null)
         {
             int num = 0;
             while (num <= 5000)
             {
                 if (librarySerialNumberLicense.Trim().ToLower() != Licensing.GetLicProc(string.Concat(num.ToString(), "KryptoSigner".ToLower())).Substring(0, 20).ToLower())
                 {
                     num++;
                 }
                 else
                 {
                     Licensing.demoVersion = false;
                     return;
                 }
             }
         }
     }
     catch (Exception exception)
     {
         throw new ArgumentException(string.Concat("Licensing error: ", exception.Message));
     }
 }
Beispiel #2
0
        /// <summary>
        /// Verifies a digital signature attached to the document.
        /// </summary>
        /// <param name="inputFile">Path to the document.</param>
        /// <param name="certificate">The signer certificate.</param>
        /// <returns>Returns true if the signature is valid.</returns>
        public bool VerifyDigitalSignature(string inputFile, X509Certificate2 certificate)
        {
            bool flag;

            try
            {
                Licensing.ShowDemoMessage();
                if (!File.Exists(inputFile))
                {
                    throw new FileNotFoundException();
                }
                XmlDocument xmlDocument = new XmlDocument()
                {
                    PreserveWhitespace = this.RemoveWhitespaces
                };
                xmlDocument.Load(inputFile);
                SignedXml   signedXml         = new SignedXml(xmlDocument);
                XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("Signature");
                if (elementsByTagName.Count <= 0)
                {
                    throw new CryptographicException(CustomExceptions.XMLSignatureNoDigitalSignatures);
                }
                if (elementsByTagName.Count >= 2)
                {
                    throw new CryptographicException(CustomExceptions.XMLSignatureMoreThanOneSignature);
                }
                signedXml.LoadXml((XmlElement)elementsByTagName[0]);
                flag = signedXml.CheckSignature(certificate, true);
            }
            catch
            {
                throw;
            }
            return(flag);
        }
Beispiel #3
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;
     }
 }
 private void SignOpenOfficeXML(Package package)
 {
     try
     {
         Licensing.ShowDemoMessage();
         DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate);
         if (package == null)
         {
             throw new ArgumentNullException();
         }
         List <Uri> uris = new List <Uri>();
         List <PackageRelationshipSelector> packageRelationshipSelectors = new List <PackageRelationshipSelector>();
         foreach (PackageRelationship relationshipsByType in package.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"))
         {
             this.CreateListOfSignableItems(relationshipsByType, uris, packageRelationshipSelectors);
         }
         PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager(package)
         {
             CertificateOption = CertificateEmbeddingOption.InSignaturePart
         };
         packageDigitalSignatureManager.Sign(uris, this.DigitalSignatureCertificate, packageRelationshipSelectors);
     }
     catch
     {
         throw;
     }
 }
 private void SignXml(string inputFile, string outputFile)
 {
     try
     {
         Licensing.ShowDemoMessage();
         DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate);
         XmlDocument xmlDocument = new XmlDocument()
         {
             PreserveWhitespace = this.RemoveWhitespaces
         };
         xmlDocument.Load(inputFile);
         SignedXml signedXml = new SignedXml(xmlDocument);
         RSACryptoServiceProvider rSACryptoServiceProvider = null;
         try
         {
             string xmlString = this.DigitalSignatureCertificate.PrivateKey.ToXmlString(true);
             rSACryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters(24))
             {
                 PersistKeyInCsp = false
             };
             rSACryptoServiceProvider.FromXmlString(xmlString);
             signedXml.SigningKey = rSACryptoServiceProvider;
         }
         catch
         {
             rSACryptoServiceProvider = this.DigitalSignatureCertificate.PrivateKey as RSACryptoServiceProvider;
             signedXml.SigningKey     = rSACryptoServiceProvider;
         }
         signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
         if (this.IncludeKeyInfo || this.IncludeSignatureCertificate)
         {
             KeyInfo keyInfos = new KeyInfo();
             if (this.IncludeKeyInfo)
             {
                 keyInfos.AddClause(new RSAKeyValue(rSACryptoServiceProvider));
             }
             if (this.IncludeSignatureCertificate)
             {
                 keyInfos.AddClause(new KeyInfoX509Data(this.DigitalSignatureCertificate.GetRawCertData()));
             }
             signedXml.KeyInfo = keyInfos;
         }
         Reference reference = new Reference();
         reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
         reference.AddTransform(new XmlDsigExcC14NTransform());
         reference.Uri          = "";
         reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
         signedXml.AddReference(reference);
         signedXml.ComputeSignature();
         SignatureDescription signatureDescription = new SignatureDescription();
         XmlElement           xml = signedXml.GetXml();
         xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xml, true));
         xmlDocument.Save(outputFile);
     }
     catch
     {
         throw;
     }
 }
 /// <summary>
 /// Initializes a new instance of OfficeSign class.
 /// </summary>
 /// <param name="librarySerialNumberLicense">The serial number provided to register the library.</param>
 public OfficeSignature(string librarySerialNumberLicense)
 {
     try
     {
         Licensing.CheckLicense(librarySerialNumberLicense);
     }
     catch (Exception exception)
     {
         throw new ArgumentException(string.Concat("Licensing error: ", exception.Message));
     }
 }
Beispiel #7
0
 /// <summary>
 /// Initializes a new instance of XPSSign class.
 /// </summary>
 /// <param name="librarySerialNumberLicense">The serial number provided to register the library.</param>
 public XpsSignature(string librarySerialNumberLicense)
 {
     try
     {
         Licensing.CheckLicense(librarySerialNumberLicense);
     }
     catch (Exception exception)
     {
         throw new ArgumentException(string.Concat("Licensing error: ", exception.Message));
     }
     this.AllowMultipleSignatures = false;
 }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of XMLSign class.
 /// </summary>
 /// <param name="librarySerialNumberLicense">The serial number provided to register the library.</param>
 public XmlSignature(string librarySerialNumberLicense)
 {
     try
     {
         Licensing.CheckLicense(librarySerialNumberLicense);
     }
     catch (Exception exception)
     {
         throw new ArgumentException(string.Concat("Licensing error: ", exception.Message));
     }
     this.IncludeKeyInfo = true;
     this.IncludeSignatureCertificate = true;
     this.RemoveWhitespaces           = true;
 }
 /// <summary>
 /// Initializes a new instance of XMLSign class.
 /// </summary>
 /// <param name="librarySerialNumberLicense">The serial number provided to register the library.</param>
 public XmlSignatureSha256(string librarySerialNumberLicense)
 {
     try
     {
         Licensing.CheckLicense(librarySerialNumberLicense);
     }
     catch (Exception exception)
     {
         throw new ArgumentException(string.Concat("Licensing error: ", exception.Message));
     }
     try
     {
         CryptoConfig.AddAlgorithm(typeof(RSAPKCS1SHA256SignatureDescription), new string[] { "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" });
     }
     catch
     {
     }
     this.IncludeKeyInfo = true;
     this.IncludeSignatureCertificate = true;
     this.RemoveWhitespaces           = true;
 }
Beispiel #10
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;
     }
 }
Beispiel #11
0
        /// <summary>
        /// Verifies a digital signature attached to the document.
        /// </summary>
        /// <param name="inputFile">Path to the document.</param>
        /// <param name="signatureNumber">Signature number.</param>
        /// <returns></returns>
        public string VerifyDigitalSignature(string inputFile, int signatureNumber)
        {
            string str;

            try
            {
                Licensing.ShowDemoMessage();
                if (!File.Exists(inputFile))
                {
                    throw new FileNotFoundException();
                }
                XpsDocument xpsDocument = new XpsDocument(inputFile, FileAccess.Read);
                string      str1        = xpsDocument.Signatures[signatureNumber - 1].Verify().ToString();
                xpsDocument.Close();
                str = str1;
            }
            catch
            {
                throw;
            }
            return(str);
        }
Beispiel #12
0
        /// <summary>
        /// Verifies a digital signature attached to the document.
        /// </summary>
        /// <param name="inputFile">Path to the document.</param>
        /// <param name="signatureNumber">Signature number.</param>
        /// <returns></returns>
        public string VerifyDigitalSignature(string inputFile, int signatureNumber)
        {
            string str;

            try
            {
                Licensing.ShowDemoMessage();
                if (!File.Exists(inputFile))
                {
                    throw new FileNotFoundException();
                }
                Package package = Package.Open(inputFile);
                PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager(package);
                string str1 = packageDigitalSignatureManager.Signatures[signatureNumber - 1].Verify().ToString();
                package.Close();
                str = str1;
            }
            catch
            {
                throw;
            }
            return(str);
        }
Beispiel #13
0
        /// <summary>
        /// Get the signing certificate used to perform the digital signature.
        /// </summary>
        /// <param name="inputFile">Path to the document.</param>
        /// <param name="signatureNumber">Signature number.</param>
        /// <returns></returns>
        public X509Certificate2 GetDigitalSignatureCertificate(string inputFile, int signatureNumber)
        {
            X509Certificate2 x509Certificate2;

            try
            {
                Licensing.ShowDemoMessage();
                if (!File.Exists(inputFile))
                {
                    throw new FileNotFoundException();
                }
                Package package = Package.Open(inputFile);
                PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager(package);
                X509Certificate signer = packageDigitalSignatureManager.Signatures[signatureNumber - 1].Signer;
                package.Close();
                x509Certificate2 = new X509Certificate2(signer);
            }
            catch
            {
                throw;
            }
            return(x509Certificate2);
        }
Beispiel #14
0
 private void SignXml(string inputFile, string outputFile)
 {
     try
     {
         Licensing.ShowDemoMessage();
         DigitalCertificate.LogOnEToken(this.DigitalSignatureCertificate);
         DSACryptoServiceProvider dSACryptoServiceProvider = new DSACryptoServiceProvider();
         RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
         string friendlyName = (new Oid(this.DigitalSignatureCertificate.GetKeyAlgorithm())).FriendlyName;
         if (this.DigitalSignatureCertificate.HasPrivateKey)
         {
             if (friendlyName == "DSA")
             {
                 dSACryptoServiceProvider = this.DigitalSignatureCertificate.PrivateKey as DSACryptoServiceProvider;
             }
             if (friendlyName == "RSA")
             {
                 rSACryptoServiceProvider = this.DigitalSignatureCertificate.PrivateKey as RSACryptoServiceProvider;
             }
             if (friendlyName != "DSA" && friendlyName != "RSA")
             {
                 throw new ArgumentException(CustomExceptions.XMLSigningAlgorithmNotSupported);
             }
         }
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.Load(inputFile);
         xmlDocument.PreserveWhitespace = this.RemoveWhitespaces;
         SignedXml signedXml = new SignedXml(xmlDocument);
         if (friendlyName == "DSA")
         {
             signedXml.SigningKey = dSACryptoServiceProvider;
         }
         if (friendlyName == "RSA")
         {
             signedXml.SigningKey = rSACryptoServiceProvider;
         }
         if (this.IncludeKeyInfo || this.IncludeSignatureCertificate)
         {
             KeyInfo keyInfos = new KeyInfo();
             if (this.IncludeKeyInfo)
             {
                 if (friendlyName == "DSA")
                 {
                     keyInfos.AddClause(new DSAKeyValue(dSACryptoServiceProvider));
                 }
                 if (friendlyName == "RSA")
                 {
                     keyInfos.AddClause(new RSAKeyValue(rSACryptoServiceProvider));
                 }
             }
             if (this.IncludeSignatureCertificate)
             {
                 keyInfos.AddClause(new KeyInfoX509Data(this.DigitalSignatureCertificate.GetRawCertData()));
             }
             signedXml.KeyInfo = keyInfos;
         }
         Reference reference = new Reference();
         reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
         reference.AddTransform(new XmlDsigExcC14NTransform());
         reference.Uri = "";
         signedXml.AddReference(reference);
         signedXml.ComputeSignature();
         XmlElement xml = signedXml.GetXml();
         xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xml, true));
         xmlDocument.Save(outputFile);
     }
     catch
     {
         throw;
     }
 }