1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
using System;
using System.Security.Cryptography;
using System.Text;
namespace PKISharp.WACS.Services.Serialization
{
/// <summary>
/// Wrapper to handle string encryption and encoding
/// Strings can be in three forms:
/// - Clear, prefixed by ClearPrefix
/// - Base64 encoded, without any prefix
/// - Base64 encoded *with* encryption, prefixed by EncryptedPrefix
/// </summary>
public class ProtectedString
{
/// <summary>
/// Indicates encryption
/// </summary>
internal const string EncryptedPrefix = "enc-";
/// <summary>
/// Indicates clear text
/// </summary>
internal const string ClearPrefix = "clear-";
/// <summary>
/// Logging service, used only by the JsonConverter
/// </summary>
private readonly ILogService _log;
/// <summary>
/// Indicates if there was an error decoding or decrypting the string
/// </summary>
public bool Error { get; private set; } = false;
/// <summary>
/// Clear value, should be used for operations
/// </summary>
public string Value { get; private set; }
/// <summary>
/// Value to save to disk, based on the setting
/// </summary>
public string DiskValue
{
get
{
return Properties.Settings.Default.EncryptConfig? ProtectedValue : EncodedValue;
}
}
/// <summary>
/// Constructor for user input, always starting with clear text
/// </summary>
/// <param name="clearValue"></param>
public ProtectedString(string clearValue)
{
Value = clearValue;
}
/// <summary>
/// Constructor for deserialisation, may be any format
/// </summary>
/// <param name="rawValue"></param>
/// <param name="log"></param>
public ProtectedString(string rawValue, ILogService log)
{
_log = log;
Value = rawValue;
if (!string.IsNullOrEmpty(rawValue))
{
if (rawValue.StartsWith(EncryptedPrefix))
{
// Sure to be encrypted
try
{
Value = Unprotect(rawValue.Substring(EncryptedPrefix.Length));
}
catch
{
_log.Error("Unable to decrypt configuration value, may have been written by a different machine.");
Error = true;
}
}
else if (rawValue.StartsWith(ClearPrefix))
{
// Sure to be clear/unencoded
Value = rawValue.Substring(ClearPrefix.Length);
}
else
{
// Should be Base64
try
{
var clearBytes = Convert.FromBase64String(rawValue);
Value = Encoding.UTF8.GetString(clearBytes);
}
catch
{
_log.Error("Unable to decode configuration value, use the prefix {prefix} to input clear text", ClearPrefix);
Error = true;
}
}
}
}
/// <summary>
/// Encrypted value should be used when the "EncryptConfig" setting is true
/// </summary>
internal string ProtectedValue
{
get
{
if (string.IsNullOrEmpty(Value) || Error)
{
return Value;
}
else
{
return EncryptedPrefix + Protect(Value);
}
}
}
/// <summary>
/// Encoded value should be used when the "EncryptConfig" setting is false
/// </summary>
internal string EncodedValue
{
get
{
if (string.IsNullOrEmpty(Value) || Error)
{
return Value;
}
else
{
return Encode(Value);
}
}
}
/// <summary>
/// Base64 encode a string
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
private string Encode(string clearText)
{
byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
return Convert.ToBase64String(clearBytes);
}
/// <summary>
/// Encrypt and Base64-encode a string
/// </summary>
/// <param name="clearText"></param>
/// <param name="optionalEntropy"></param>
/// <param name="scope"></param>
/// <returns></returns>
private string Protect(string clearText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.LocalMachine)
{
byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
? null
: Encoding.UTF8.GetBytes(optionalEntropy);
byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, scope);
return Convert.ToBase64String(encryptedBytes);
}
/// <summary>
/// Base64-decode and decrypt a string
/// </summary>
/// <param name="clearText"></param>
/// <param name="optionalEntropy"></param>
/// <param name="scope"></param>
/// <returns></returns>
private string Unprotect(string encryptedText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.LocalMachine)
{
if (encryptedText == null)
{
return null;
}
byte[] clearBytes = null;
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy)
? null
: Encoding.UTF8.GetBytes(optionalEntropy);
clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, scope);
return Encoding.UTF8.GetString(clearBytes);
}
}
}
|