-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfile.pas
203 lines (189 loc) · 5.13 KB
/
Profile.pas
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
200
201
202
203
UNIT Profile;
INTERFACE
USES ObjectModel,SysUtils;
const cMagic:packed array [1..7] of char='BNProf'#26;
const cFormatVer=6;
type tProfID=tKey20;
type tProfileRead=object
Valid: Boolean;
ProfID: tKey20;
format: byte;
nick: string[12];
expires: Int64;
master_key, sign_key, encr_key, chat_key: tKey32;
cOldKeys: byte;
OldKeys: ^tKey32;
Fullname: AnsiString;
master_sig: tKey64;
updated: Int64;
cHosts,cBackups:byte;
Hosts,Backups: ^tKey20;
Textnote: AnsiString;
cert_sig: tKey64;
signature: tKey64;
constructor ReadFrom(var src:tCommonStream; parseLevel:byte);
constructor InitEmpty;
procedure DoMasterSign(const priv:tKey64);
procedure WriteTo(var dst:tCommonStream; const priv:tKey64);
destructor Done;
{0=readAll+Verify}
end;
IMPLEMENTATION
uses Crypto,ed25519;
type tHashingStream = object (tCommonStream)
driver: ^tCommonStream;
ctx: tSha512Context;
procedure Read(out buf; cnt:Word); virtual;
procedure Write(const buf; cnt:word); virtual;
constructor Init(var idriver:tCommonStream);
end;
procedure tHashingStream.Read(out buf; cnt:Word);
begin
driver^.Read(buf,cnt);
sha512_update(ctx,buf,cnt);
end;
procedure tHashingStream.Write(const buf; cnt:Word);
begin
driver^.Write(buf,cnt);
sha512_update(ctx,buf,cnt);
end;
constructor tHashingStream.Init(var idriver:tCommonStream);
begin
inherited Init;
driver:=@idriver;
sha512_init(ctx);
end;
constructor tProfileRead.InitEmpty;
begin
TextNote:=''; FullName:='';
hosts:=nil;
valid:=false;
cHosts:=0;cBackups:=0;cOldKeys:=0;
FillChar(nick,sizeof(nick),0);
end;
function ReadLength1Value(var m:tCommonStream; out count:byte; size:longword):pointer;
begin
count:=m.ReadByte;
size:=count*size;
if count>0 then begin
result:=GetMem(size);
m.Read(result^,size);
end else result:=nil;
end;
constructor tProfileRead.ReadFrom(var src:tCommonStream; parseLevel:byte);
var Magic:packed array [1..8] of char;
var i:longword;
var m,m2:tHashingStream;
var now2:Int64;
begin
InitEmpty;
m.Init(src);
m.Read(Magic,8);
if CompareByte(Magic,cMagic,7)<>0
then raise eFormatError.create('Invalid magic sequence');
format:=byte(Magic[8]);
{$IFDEF profLoadOldFormat}
if Format=5 then begin
{load into tMemoryStream for better parformace}
version:=src.ReadWord4;
Updated:=src.ReadWord4+40179;
src.Read(nick[1],8);SetLength(nick,8);
for i:=1 to 8 do if nick[i]=#0 then break;SetLength(nick,i-1);
src.Read(key,sizeof(key));
sigexpire:=tDateTime(dword(key.expire))+40179;
src.Read(enckey,32);
fullname:=src.ReadShortString;
if parselevel<9 then begin
hostsCount:=src.ReadByte;
backupCount:=src.ReadByte;
hosts:=GetMem((backupCount+hostsCount)*20);
src.Read(hosts^,(backupCount+hostsCount)*20);
textnote:=src.ReadShortString;
//filerefs:array[b1] of 2,20 TODO
SHA512Buffer(key.cert, 32, ProfID, 20 );
end;
if parseLevel=0 then begin
//TODO: check signature
{$warning SECURITY VIOLATION, signature check not implemented}
end;
end else{$ENDIF}
if Format=6 then begin
valid:=true;
m.Read(nick[1],12);
i:=1; while (i<=12)and(nick[i]<>#0) do inc(i); SetLength(nick,i-1);
updated:=m.ReadWord6;
m2.Init(m);
expires:=m2.ReadWord6;
m2.Read(master_key,32);
m2.Read(sign_key,32);
m.Read(master_sig,64);
now2:=UnixNow;
valid:=valid and (updated<now2) and (expires>now2);
valid:=valid
and ed25519.Verify2(m2.ctx, master_sig, master_key);
SHA256_Buffer( ProfID, 20, master_key, 32);
m.Read(encr_key,32);
m.Read(chat_key,32);
OldKeys:=ReadLength1Value(m,cOldKeys,32);
Fullname:=m.ReadShortString;
Hosts:=ReadLength1Value(m,cHosts,20);
Backups:=ReadLength1Value(m,cBackups,32);
Textnote:=m.ReadShortString;
src.Read(signature,64);
valid:=valid
and ed25519.Verify2(m.ctx, signature, sign_key);
end else raise eFormatError.create('Unknown format version');
end;
destructor tProfileRead.Done;
begin
FreeMem(OldKeys);
FreeMem(Hosts);
FreeMem(Backups);
SetLength(TextNote,0);
end;
procedure tProfileRead.WriteTo(var dst:tCommonStream; const priv:tPrivKey);
var s:tMemoryStream;
begin
s.Init(9999);//FIXME
try
with s do begin
Write(cMagic,7);
WriteByte(6);
Write(nick[1],12);
WriteWord6(updated);
WriteWord6(expires);
Write(master_key,32);
Write(sign_key,32);
Write(master_sig,64);
Write(encr_key,32);
Write(chat_key,32);
WriteByte(cOldKeys);
Write(OldKeys^,cOldKeys*32);
WriteShortString(Fullname);
WriteByte(cHosts);
Write(Hosts^,cHosts*20);
WriteByte(cBackups);
Write(Backups^,cBackups*20);
WriteShortString(Textnote);
end;
s.Seek(0);
dst.Write(s.RdBuf^, s.RdBufLen);
Sign(signature, s.RdBuf^, s.RdBufLen, sign_key, priv);
dst.Write(signature,64);
finally s.Free; end;
end;
procedure tProfileRead.DoMasterSign(const priv:tPrivKey);
var s:tMemoryStream;
begin
s.Init(70);
try
with s do begin
WriteWord6(expires);
Write(master_key,32);
Write(sign_key,32);
end;
s.Seek(0);
Sign(master_sig, s.RdBuf^, s.RdBufLen, master_key, priv);
finally s.Free; end;
end;
END.