public Stream Sign(Stream inputStream, Encoding encoding, string signatureXPath, IDictionary<string, string> signatureXPathNamespaces) { using (MemoryStream mInputStream = new MemoryStream()) { inputStream.CopyTo(mInputStream); mInputStream.Position = 0; //Load xml in TElXMLDOMDocument using (TElXMLDOMDocument xmlDocument = new TElXMLDOMDocument()) { xmlDocument.LoadFromStream(mInputStream, encoding.HeaderName, true); using (TElXMLKeyInfoX509Data x509KeyData = new TElXMLKeyInfoX509Data(false)) using (TElWinCertStorage winCertStorage = new TElWinCertStorage()) using (TElX509Certificate x509Certificate = GetX509Certificate(winCertStorage)) { if (x509Certificate == null) { throw new Exception("Certificate not found."); } x509KeyData.IncludeKeyValue = true; x509KeyData.Certificate = x509Certificate; using (TElXMLSigner xmlSigner = GetXmlSigner(x509KeyData, GetXmlReference(xmlDocument))) { //Save signature value to definedxml node TElXMLNamespaceMap map = new TElXMLNamespaceMap(); foreach (var ns in signatureXPathNamespaces) { map.AddNamespace(ns.Key, ns.Value); } TElXMLDOMNode signatureNode = xmlDocument.SelectNodes(signatureXPath, map)[0]; xmlSigner.Save(ref signatureNode); MemoryStream outputStream = new MemoryStream(); xmlDocument.SaveToStream(outputStream, SBXMLDefs.Unit.xcmNone, encoding.HeaderName); return outputStream; } } } } }
public void SignInPlace(Stream inputStream) { TElOfficeDocument officeDocument = new TElOfficeDocument(); officeDocument.Open(inputStream, false); using (TElWinCertStorage winCertStorage = new TElWinCertStorage()) { using (TElXMLKeyInfoX509Data x509KeyData = new TElXMLKeyInfoX509Data(false)) { TElX509Certificate x509Certificate = GetX509Certificate(winCertStorage); if (x509Certificate == null) { throw new Exception("Certificate not found."); } x509KeyData.IncludeKeyValue = true; x509KeyData.Certificate = x509Certificate; if (officeDocument.OpenXMLDocument != null) { TElOfficeOpenXMLSignatureHandler openXMLSigHandler = new TElOfficeOpenXMLSignatureHandler(); officeDocument.AddSignature(openXMLSigHandler, true); openXMLSigHandler.AddDocument(); openXMLSigHandler.Sign(x509Certificate); officeDocument.Flush(); } else if (officeDocument.BinaryDocument != null) { TElOfficeBinaryCryptoAPISignatureHandler BinCryptoAPISigHandler = new TElOfficeBinaryCryptoAPISignatureHandler(); officeDocument.AddSignature(BinCryptoAPISigHandler, true); BinCryptoAPISigHandler.ExpireTime = DateTime.UtcNow.AddYears(100); BinCryptoAPISigHandler.Sign(x509Certificate); officeDocument.Flush(); } else { throw new FormatException(); } } } }
private TElX509Certificate GetX509Certificate(TElWinCertStorage winCertStorage) { winCertStorage.StorageType = TSBStorageType.stSystem; winCertStorage.ReadOnly = true; winCertStorage.SystemStores.Text = "MY"; winCertStorage.AccessType = TSBStorageAccessType.atLocalMachine; TElX509Certificate certificate = null; for (int i = 0; i < winCertStorage.Count; i++) { TElX509Certificate cert = winCertStorage.get_Certificates(i); if (SBUtils.Unit.BinaryToString(cert.SerialNumber).ToLower() == this.SerialNumber.ToLower()) { certificate = cert; //Pass pinCode to certificate certificate.KeyMaterial.KeyExchangePIN = this.PinCode; certificate.KeyMaterial.SignaturePIN = this.PinCode; break; } } return certificate; }