Skip to content

Commit

Permalink
Merge branch 'feature/save-password' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
poderosaproject committed Nov 27, 2023
2 parents a775450 + 5a8a720 commit 307b767
Show file tree
Hide file tree
Showing 20 changed files with 714 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Core/RenderProfileSerialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Type ConcreteType {
}
}

public StructuredText Serialize(object obj) {
public StructuredText Serialize(object obj, SerializationOptions options) {
StructuredText storage = new StructuredText(typeof(RenderProfile).FullName);
RenderProfile prof = (RenderProfile)obj;
storage.Set("font-name", prof.FontName);
Expand Down
8 changes: 4 additions & 4 deletions Core/Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public override void InitializePlugin(IPoderosaWorld poderosa) {
_serviceElements.RegisterExtension(new RenderProfileSerializer());
}

public StructuredText Serialize(object obj) {
return Serialize(obj.GetType(), obj);
public StructuredText Serialize(object obj, SerializationOptions options) {
return Serialize(obj.GetType(), obj, options);
}
public StructuredText Serialize(Type type, object obj) {
public StructuredText Serialize(Type type, object obj, SerializationOptions options) {
ISerializeServiceElement se = FindServiceElement(type.FullName
);
if (se == null)
throw new ArgumentException("ISerializeServiceElement is not found for the class " + obj.GetType().FullName);

StructuredText t = se.Serialize(obj);
StructuredText t = se.Serialize(obj, options);
return t;
}

Expand Down
37 changes: 34 additions & 3 deletions Core/SerializeEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,44 @@
using System.Text;

namespace Poderosa.Serializing {

/// <summary>
/// Type of password serialization
/// </summary>
public enum PasswordSerialization {
/// <summary>Don't serialize passwords</summary>
None,
/// <summary>Serialize passwords as plaintext</summary>
Plaintext,
/// <summary>Serialize passwords using simple encryption</summary>
Encrypted,
}

/// <summary>
/// Serialization options
/// </summary>
public class SerializationOptions {
/// <summary>
/// Constructor
/// </summary>
/// <param name="passwordSerialization">Type of password serialization</param>
public SerializationOptions(PasswordSerialization passwordSerialization = PasswordSerialization.None) {
PasswordSerialization = passwordSerialization;
}

public PasswordSerialization PasswordSerialization {
get;
private set;
}
}

/// <summary>
///
/// </summary>
/// <exclude/>
public interface ISerializeService {
StructuredText Serialize(object obj);
StructuredText Serialize(Type type, object obj); //型を明示
StructuredText Serialize(object obj, SerializationOptions options);
StructuredText Serialize(Type type, object obj, SerializationOptions options);
object Deserialize(StructuredText node);
}

Expand All @@ -41,7 +72,7 @@ public interface ISerializeServiceElement {
Type ConcreteType {
get;
}
StructuredText Serialize(object obj);
StructuredText Serialize(object obj, SerializationOptions options);
object Deserialize(StructuredText node);
}
}
2 changes: 1 addition & 1 deletion Pipe/PipeTerminalParameterSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Type ConcreteType {
}
}

public StructuredText Serialize(object obj) {
public StructuredText Serialize(object obj, SerializationOptions options) {
PipeTerminalParameter tp = obj as PipeTerminalParameter;
Debug.Assert(tp != null);

Expand Down
4 changes: 2 additions & 2 deletions Pipe/PipeTerminalSettingsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public Type ConcreteType {
}
}

public StructuredText Serialize(object obj) {
public StructuredText Serialize(object obj, SerializationOptions options) {
PipeTerminalSettings ts = obj as PipeTerminalSettings;
Debug.Assert(ts != null);

StructuredText node = new StructuredText(this.ConcreteType.FullName);
node.AddChild(PipePlugin.Instance.SerializeService.Serialize(typeof(TerminalSettings), obj));
node.AddChild(PipePlugin.Instance.SerializeService.Serialize(typeof(TerminalSettings), obj, options));

return node;
}
Expand Down
1 change: 1 addition & 0 deletions Protocols/Protocols.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="ProtocolOptions.cs" />
<Compile Include="ProtocolsEx.cs" />
<Compile Include="ProtocolsPlugin.cs" />
<Compile Include="SimpleStringEncrypt.cs" />
<Compile Include="socks.cs" />
<Compile Include="SSH.cs" />
<Compile Include="SSHSocket.cs" />
Expand Down
97 changes: 97 additions & 0 deletions Protocols/SimpleStringEncrypt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2004-2017 The Poderosa Project.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Poderosa;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Poderosa.Protocols {


/// <summary>
/// Simple string encyption with constant key.
/// </summary>
internal class SimpleStringEncrypt {

// Method : DES
// Mode : CBC
// IV : 91 7c 0b f1 7e 3e 7e 33
// Key : 15 b1 84 59 15 60 29 32

private const string CIV = "kXwL8X4+fjM=";
private const string CKEY = "FbGEWRVgKTI=";

private SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();

/// <summary>
/// Decrypt string.
/// </summary>
/// <param name="s">Base64-encoded encrypted data</param>
/// <returns>decrypted text or null if failed</returns>
public string DecryptString(string s) {
if (s == null)
return null;
if (s.Length == 0)
return null;

using (ICryptoTransform transform = this.mCSP.CreateDecryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV))) {
try {
byte[] buffer = Convert.FromBase64String(s);
using (MemoryStream stream = new MemoryStream()) {
using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write)) {
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream2.Close();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
catch (Exception exception) {
RuntimeUtil.SilentReportException(exception);
return null;
}
}
}

/// <summary>
/// Encrypt string.
/// </summary>
/// <param name="text">Text to encrypt</param>
/// <returns>Base64-encoded encrypted data or null if failed</returns>
public string EncryptString(string text) {
if (text == null)
return null;

try {
using (ICryptoTransform transform = this.mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV))) {
byte[] bytes = Encoding.UTF8.GetBytes(text);
using (MemoryStream stream = new MemoryStream()) {
using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write)) {
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
stream2.Close();
return Convert.ToBase64String(stream.ToArray());
}
}
}
}
catch (Exception exception) {
RuntimeUtil.SilentReportException(exception);
return null;
}
}
}
}
Loading

0 comments on commit 307b767

Please sign in to comment.