-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathSecureMessage.cs
184 lines (171 loc) · 9.61 KB
/
SecureMessage.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using PubnubApi.Security.Crypto;
using PubnubApi.Security.Crypto.Cryptors;
namespace PubnubApi
{
internal class SecureMessage
{
private PNConfiguration config;
private IJsonPluggableLibrary jsonLib;
private IPubnubLog pubnubLog;
public static SecureMessage Instance(PNConfiguration pubnubConfig, IJsonPluggableLibrary jsonPluggableLibrary, IPubnubLog log)
{
SecureMessage secureMessage = new SecureMessage();
secureMessage.config = pubnubConfig;
secureMessage.jsonLib = jsonPluggableLibrary;
secureMessage.pubnubLog = log;
return secureMessage;
}
public List<object> HistoryDecodeDecryptLoop<T>(PNOperationType type, List<object> messageList, string[] channels, string[] channelGroups, PNCallback<T> errorCallback)
{
List<object> returnMessage = new List<object>();
if (config.CryptoModule != null || config.CipherKey.Length > 0)
{
config.CryptoModule ??= new CryptoModule(new LegacyCryptor(config.CipherKey, config.UseRandomInitializationVector, pubnubLog), null);
object[] myObjectArray = (from item in messageList
select item as object).ToArray();
object[] enumerable = myObjectArray[0] as object[];
if (enumerable != null)
{
List<object> receivedMsg = new List<object>();
foreach (object element in enumerable)
{
string decryptMessage = "";
try
{
Dictionary<string, object> historyEnv = jsonLib.ConvertToDictionaryObject(element);
if (historyEnv != null && historyEnv.ContainsKey("message"))
{
string dictionaryValue = config.CryptoModule.Decrypt(historyEnv["message"].ToString());
historyEnv["message"] = jsonLib.DeserializeToObject(dictionaryValue);
decryptMessage = jsonLib.SerializeToJsonString(historyEnv);
}
else
{
decryptMessage = config.CryptoModule.Decrypt(element.ToString());
}
}
catch (Exception ex)
{
decryptMessage = "**DECRYPT ERROR**";
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(ex);
PNStatus status = new StatusBuilder(config, jsonLib).CreateStatusResponse<T>(type, category,
null, (int)HttpStatusCode.NotFound, new PNException(ex));
if (channels != null && channels.Length > 0)
{
status.AffectedChannels.AddRange(channels);
}
if (channelGroups != null && channelGroups.Length > 0)
{
status.AffectedChannelGroups.AddRange(channelGroups);
}
LoggingMethod.WriteToLog(pubnubLog, $"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] Failed to decrypt message!\nMessage might be not encrypted, Returning the original content as received ", config.LogVerbosity);
}
object decodedMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? jsonLib.SerializeToJsonString(element) : decryptMessage);
receivedMsg.Add(decodedMessage);
}
returnMessage.Add(receivedMsg);
}
for (int index = 1; index < myObjectArray.Length; index++)
{
returnMessage.Add(myObjectArray[index]);
}
return returnMessage;
}
else
{
var myObjectArray = (from item in messageList
select item as object).ToArray();
IEnumerable enumerable = myObjectArray[0] as IEnumerable;
if (enumerable != null)
{
List<object> receivedMessage = new List<object>();
foreach (object element in enumerable)
{
receivedMessage.Add(element);
}
returnMessage.Add(receivedMessage);
}
for (int index = 1; index < myObjectArray.Length; index++)
{
returnMessage.Add(myObjectArray[index]);
}
return returnMessage;
}
}
public List<object> FetchHistoryDecodeDecryptLoop<T>(PNOperationType type, Dictionary<string, object> messageContainer, string[] channels, string[] channelGroups, PNCallback<T> errorCallback)
{
List<object> returnMessage = new List<object>();
Dictionary<string, List<object>> dicMessage = new Dictionary<string, List<object>>();
foreach (KeyValuePair<string, object> kvp in messageContainer)
{
List<object> currentVal = kvp.Value as List<object>;
if (currentVal != null)
{
object[] currentValArray = jsonLib.ConvertToObjectArray(currentVal);
List<object> decryptList = (currentValArray != null && currentValArray.Length > 0) ? new List<object>() : null;
if (currentValArray != null && decryptList != null)
{
foreach (object currentObj in currentValArray)
{
Dictionary<string, object> dicValue = jsonLib.ConvertToDictionaryObject(currentObj);
if (dicValue != null && dicValue.Count > 0 && dicValue.ContainsKey("message"))
{
Dictionary<string, object> dicDecrypt = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> kvpValue in dicValue)
{
if (kvpValue.Key == "message" && (config.CryptoModule != null || config.CipherKey.Length > 0))
{
config.CryptoModule ??= new CryptoModule(new LegacyCryptor(config.CipherKey, config.UseRandomInitializationVector, pubnubLog), null);
string decryptMessage = "";
try
{
decryptMessage = config.CryptoModule.Decrypt(kvpValue.Value.ToString());
}
catch (Exception ex)
{
#region "Exception"
decryptMessage = "**DECRYPT ERROR**";
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(ex);
PNStatus status = new StatusBuilder(config, jsonLib).CreateStatusResponse<T>(type, category, null, (int)HttpStatusCode.NotFound, new PNException(ex));
if (channels != null && channels.Length > 0)
{
status.AffectedChannels.AddRange(channels);
}
if (channelGroups != null && channelGroups.Length > 0)
{
status.AffectedChannelGroups.AddRange(channelGroups);
}
LoggingMethod.WriteToLog(pubnubLog, $"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] Failed to decrypt message!\nMessage might be not encrypted, returning as is...",config.LogVerbosity);
#endregion
}
object decodedMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? jsonLib.SerializeToJsonString(kvpValue.Value) : decryptMessage);
dicDecrypt.Add(kvpValue.Key, decodedMessage);
}
else
{
dicDecrypt.Add(kvpValue.Key, kvpValue.Value);
}
}
decryptList.Add(dicDecrypt);
}
}
}
dicMessage.Add(kvp.Key, decryptList);
}
}
if (dicMessage.Count > 0)
{
returnMessage.Add(dicMessage);
}
return returnMessage;
}
}
}