From 9af610a45b9f1e79e2cbefcb4aac7ac204c30e2a Mon Sep 17 00:00:00 2001 From: David Sparer Date: Tue, 11 Oct 2016 16:09:42 -0600 Subject: [PATCH] Rewrote the full file encryption code for the xml serializer. It now conforms to the expected placement of encrypted data --- .../Config/Connections/ConnectionsSaver.cs | 3 +- .../Serializers/XmlConnectionsSerializer.cs | 48 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/mRemoteV1/Config/Connections/ConnectionsSaver.cs b/mRemoteV1/Config/Connections/ConnectionsSaver.cs index b294e152..2cf9f506 100644 --- a/mRemoteV1/Config/Connections/ConnectionsSaver.cs +++ b/mRemoteV1/Config/Connections/ConnectionsSaver.cs @@ -243,7 +243,8 @@ namespace mRemoteNG.Config.Connections var xmlConnectionsSerializer = new XmlConnectionsSerializer() { Export = Export, - SaveSecurity = SaveSecurity + SaveSecurity = SaveSecurity, + UseFullEncryption = mRemoteNG.Settings.Default.EncryptCompleteConnectionsFile }; var xml = xmlConnectionsSerializer.Serialize(ConnectionTreeModel); diff --git a/mRemoteV1/Config/Serializers/XmlConnectionsSerializer.cs b/mRemoteV1/Config/Serializers/XmlConnectionsSerializer.cs index 3c20c44b..b7f84fb2 100644 --- a/mRemoteV1/Config/Serializers/XmlConnectionsSerializer.cs +++ b/mRemoteV1/Config/Serializers/XmlConnectionsSerializer.cs @@ -24,6 +24,7 @@ namespace mRemoteNG.Config.Serializers public bool Export { get; set; } public Save SaveSecurity { get; set; } = new Save(); + public bool UseFullEncryption { get; set; } public string Serialize(ConnectionTreeModel connectionTreeModel) @@ -49,13 +50,17 @@ namespace mRemoteNG.Config.Serializers { SetXmlTextWriterSettings(); _xmlTextWriter.WriteStartDocument(); + SerializeRootNodeInfo(GetRootNodeFromConnectionInfo(serializationTarget)); SaveNodesRecursive(serializationTarget); + _xmlTextWriter.WriteEndElement(); _xmlTextWriter.WriteEndDocument(); _xmlTextWriter.Flush(); var streamReader = new StreamReader(memoryStream, Encoding.UTF8, true); memoryStream.Seek(0, SeekOrigin.Begin); xml = streamReader.ReadToEnd(); + if (UseFullEncryption) + xml = EncyrptFullFile(xml); } } catch (Exception ex) @@ -65,12 +70,48 @@ namespace mRemoteNG.Config.Serializers return xml; } + private RootNodeInfo GetRootNodeFromConnectionInfo(ConnectionInfo connectionInfo) + { + while (true) + { + var connectionInfoAsRootNode = connectionInfo as RootNodeInfo; + if (connectionInfoAsRootNode != null) return connectionInfoAsRootNode; + connectionInfo = connectionInfo.Parent; + } + } + private void SetXmlTextWriterSettings() { _xmlTextWriter.Formatting = Formatting.Indented; _xmlTextWriter.Indentation = 4; } + private string EncyrptFullFile(string xml) + { + var xmldoc = new XmlDocument(); + xmldoc.LoadXml(xml); + if (xmldoc.DocumentElement == null) return xml; + var plainTextContent = xmldoc.DocumentElement.InnerXml; + var encryptedContent = _cryptographyProvider.Encrypt(plainTextContent, _password); + xmldoc.DocumentElement.InnerXml = encryptedContent; + var xmlString = WriteXmlToString(xmldoc); + return xmlString; + } + + private string WriteXmlToString(XmlDocument xmlDocument) + { + var xmlString = ""; + var xmlWriterSettings = new XmlWriterSettings {Indent = true, IndentChars = " ", Encoding = Encoding.UTF8}; + using (var stringWriter = new StringWriter()) + using (var xmlTextWriter = XmlWriter.Create(stringWriter, xmlWriterSettings)) + { + xmlDocument.WriteTo(xmlTextWriter); + xmlTextWriter.Flush(); + xmlString = stringWriter.GetStringBuilder().ToString(); + } + return xmlString; + } + private void SaveNodesRecursive(ConnectionInfo node) { try @@ -79,7 +120,6 @@ namespace mRemoteNG.Config.Serializers var nodeAsContainer = node as ContainerInfo; if (nodeAsRoot != null) { - SerializeRootNodeInfo(nodeAsRoot); foreach (var child in nodeAsRoot.Children) SaveNodesRecursive(child); } @@ -93,7 +133,9 @@ namespace mRemoteNG.Config.Serializers { SerializeConnectionInfo(node); } - _xmlTextWriter.WriteEndElement(); + + if (nodeAsRoot == null) + _xmlTextWriter.WriteEndElement(); } catch (Exception ex) { @@ -108,7 +150,7 @@ namespace mRemoteNG.Config.Serializers _xmlTextWriter.WriteAttributeString("Export", "", Convert.ToString(Export)); _xmlTextWriter.WriteAttributeString("EncryptionEngine", "", Enum.GetName(typeof(BlockCipherEngines), mRemoteNG.Settings.Default.EncryptionEngine)); _xmlTextWriter.WriteAttributeString("BlockCipherMode", "", Enum.GetName(typeof(BlockCipherModes), mRemoteNG.Settings.Default.EncryptionBlockCipherMode)); - _xmlTextWriter.WriteAttributeString("FullFileEncryption", "", mRemoteNG.Settings.Default.EncryptCompleteConnectionsFile.ToString()); + _xmlTextWriter.WriteAttributeString("FullFileEncryption", "", UseFullEncryption.ToString()); if (Export) {