-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMEncrypt.cs
199 lines (191 loc) · 7.75 KB
/
PMEncrypt.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Text;
using AdiIRCAPI;
using Chaos.NaCl;
namespace AdiIRC_Encrypt {
public class PMEncrypt : IDisposable {
private PMKeyContainer keys;
private List<string> hasPluginSent;
private IPluginHost host;
private ITools tools;
private readonly byte[] msgPluginTag = new byte[]{ 0x02, 0x1d, 0x02, 0x1d }; //bibi
private const string msgPluginTagStr = "\x02\x1d\x02\x1d"; //bibi
private const byte msgKeyEnquire = 0x05;
private const byte msgKeyAcknowledge = 0x06;
private const byte msgEncrypted = 0x07;
private readonly byte[] CRLF = new byte[]{0x0D, 0x0A};
private byte[] msgEncryptedIndicator = Encoding.ASCII.GetBytes("{X}");
public PMEncrypt(IPluginHost Host, ITools Tools) {
keys = new PMKeyContainer();
hasPluginSent = new List<string>();
host = Host;
tools = Tools;
host.OnRawData += OnRawData;
host.OnSendData += OnSendData;
}
private void SendMyKey(IServer server, string prefix, byte responseType, bool generateNew = false) {
if (prefix.Contains("!")) {
prefix = prefix.Substring(0, prefix.IndexOf('!'));
}
if (generateNew) {
keys.DeleteKey(prefix);
}
string message = string.Format("PRIVMSG {0} :", prefix);
server.SendRawData(server.Encoding.GetBytes(message).Concatenate(new byte[]{responseType}, keys[prefix].MyPublicKey.IrcEscape(), CRLF));
host.NotifyUser(server.FindUser(prefix), string.Format("Sending public key."));
}
private void OnKeyEnquire(IServer server, string prefix, byte[] trailing) {
// Recived PUB key, the other end still needs ours.
if (prefix.Contains("!")) {
prefix = prefix.Substring(0, prefix.IndexOf('!'));
}
keys[prefix].SetUsersPublicKey(trailing.SubArray(1).IrcUnescape());
SendMyKey(server, prefix, msgKeyAcknowledge);
host.NotifyUser(server.FindUser(prefix), string.Format("Received public key."));
}
private void OnKeyAcknowledge(IServer server, string prefix, byte[] trailing) {
// Received PUB key, the other end already has ours.
if (prefix.Contains("!")) {
prefix = prefix.Substring(0, prefix.IndexOf('!'));
}
keys[prefix].SetUsersPublicKey(trailing.SubArray(1).IrcUnescape());
host.NotifyUser(server.FindUser(prefix), string.Format("Received public key."));
}
private byte[] EncryptMessage(string prefix, byte[] message) {
if (prefix.Contains("!")) {
prefix = prefix.Substring(0, prefix.IndexOf('!'));
}
byte[] nonce = RNG.GetBytes(24);
byte[] key = keys[prefix].SharedKey;
byte[] encMsg = XSalsa20Poly1305.Encrypt(message, key, nonce);
return nonce.Concatenate(encMsg);
}
private byte[] DecryptMessage(string prefix, byte[] message, int startIdx = 0) {
if (prefix.Contains("!")) {
prefix = prefix.Substring(0, prefix.IndexOf('!'));
}
byte[] result = null;
if (keys.HasKey(prefix)) {
byte[] nonce = message.SubArray(startIdx, 24);
byte[] key = keys[prefix].SharedKey;
byte[] encMsg = message.SubArray(startIdx + 24);
result = XSalsa20Poly1305.TryDecrypt(encMsg, key, nonce);
if (result != null) {
result = msgEncryptedIndicator.Concatenate(result);
}
}
return result;
}
private void OnRawData(object sender, RawDataArgs e) {
// Raw Data from server.
if (e.Bytes[0] != ':')
return; //Ignore anything that doesn't have a prefix because PRIVMSGs sent to us will/should be prefixed
int offset = 1;
int spaceIdx = Array.IndexOf<byte>(e.Bytes, 0x20, offset);
if (spaceIdx <= offset) {
host.NotifyUser("AdiIRC Encrypt: couldn't find space after prefix");
return;
}
string prefix = e.Server.Encoding.GetString(e.Bytes, offset, spaceIdx - offset);
offset = spaceIdx + 1;
spaceIdx = Array.IndexOf<byte>(e.Bytes, 0x20, offset);
if (spaceIdx <= offset) {
//couldn't find space after command, so it's not a PRIVMSG
return;
}
string command = e.Server.Encoding.GetString(e.Bytes, offset, spaceIdx - offset);
if (command.ToUpper() != "PRIVMSG") {
return; //Don't care about anything other than PRIVMSGs.
}
List<string> argsList = new List<string>();
byte[] trailing = null;
string arg;
while (true) {
offset = spaceIdx + 1;
if (e.Bytes[offset] == ':') { //the trailing part!
offset++;
trailing = e.Bytes.SubArray(offset);
break;
}
spaceIdx = Array.IndexOf<byte>(e.Bytes, 0x20, offset);
if (spaceIdx <= offset) {
break;
}
arg = e.Server.Encoding.GetString(e.Bytes, offset, spaceIdx - offset);
argsList.Add(arg);
}
string[] args = argsList.ToArray();
// phew, now we should have a privmsg, the sender is prefix and target is args[0] and the message is in trailing
if (args[0] != e.Server.Nick) {
return; //Don't care about channels here.
}
//It's a PRIVMSG to ME!
//Do we need to mess with it?
if ((trailing != null) && (trailing.Length <= 4)) {
// host.NotifyUser("AdiIRC Encrypt: trailing was null or too small."); //Will interfear with small messages from non AdiIRC Encrypt clients.
return;
}
switch (trailing[0]) {
case msgKeyEnquire:
// Recived PUB key, the other end still needs ours.
OnKeyEnquire(e.Server, prefix, trailing);
e.Bytes = new byte[0]; //Eat it, TODO: replace with null when the API is updated.
break;
case msgKeyAcknowledge:
// Received PUB key, the other end already has ours.
OnKeyAcknowledge(e.Server, prefix, trailing);
e.Bytes = new byte[0]; //Eat it, TODO: replace with null when the API is updated.
break;
case msgEncrypted:
//Encrypted message, decrypt it and pass it on.
//host.NotifyUser("Received Data: " + trailing.SubArray(1).ToBase64String());
byte[] decMsg = DecryptMessage(prefix, trailing.IrcUnescape(), 1);
e.Bytes = e.Bytes.SubArray(0, offset); //Everything upto and including the :
e.Bytes = e.Bytes.Concatenate(decMsg); //tack on the message.
break;
}
if (trailing.StartsWith(msgPluginTag)) {
//A plain text message from someone that has the AdiIRC Encrypt, send them a public key
SendMyKey(e.Server, prefix, msgKeyEnquire, true);
//Let the message pass as is, msgPluginTag is invisible
}
}
private void OnSendData(IServer server, string data, out EatData result) {
result = EatData.EatNone;
string[] args = data.Split(new char[]{ ' ' }, 3);
if (args[0].ToUpper() != "PRIVMSG") {
return; //We only care about PRIVMSGs
}
char targetPrefix = args[1][0];
foreach (char c in server.ChannelPrefix + server.UserPrefix) {
if (targetPrefix == c) {
return; //We don't care about channels either.
}
}
if (args[2][1] == 0x01) {
return; // don't mess with CTCP
}
//Now we have a privmsg being sent to a user. target is args[1] and the message is args[2] including the ':' prefix
if (keys.HasKey(args[1])) {
//We have a key for the user, Encrypt the message.
string msgStart = string.Format("PRIVMSG {0} :", args[1]);
byte[] msgData = EncryptMessage(args[1], server.Encoding.GetBytes(args[2].Substring(1))).IrcEscape();
//host.NotifyUser("Sending Data: " + msgData.ToBase64String());
server.SendRawData(server.Encoding.GetBytes(msgStart).Concatenate(new byte[]{ msgEncrypted }, msgData, CRLF));
result = EatData.EatAll;
return;
}
if (!hasPluginSent.Contains(args[1])) {
//We have not sent the "has plugin" tag yet.
hasPluginSent.Add(args[1]);
string message = string.Format("PRIVMSG {0} :{1}{2}", args[1], msgPluginTagStr, args[2].Substring(1));
server.SendRawData(server.Encoding.GetBytes(message).Concatenate(CRLF));
result = EatData.EatAll;
return;
}
}
public void Dispose() {
}
}
}