This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.c
424 lines (366 loc) · 12 KB
/
Main.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/PrintLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseCryptLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <sstd.h>
#include <Socket.h>
#include <Mtftp.h>
#include <Main.h>
//
// Define the Exchange Mode of Pre-Msater Key (RSA or DH)
//
#define RSA
//
// FTP/MTFTP Server IPv4 Address
//
CONST UINT32 ServerAddr = IPV4(192,168,3,99);
//
// The nounce required for session key
//
UINT32 Nounce1, Nounce3;
UINTN Nounce2, Nounce;
//
// Rsa Ctx from the X509 Cert
//
VOID *RsaCtx;
//
// Retrieve PubKey from Der-Coded X509 Cert
//
EFI_STATUS
EFIAPI
RetrieveCertPk(
IN INT32 MtftpId
)
{
EFI_STATUS Status;
CHAR8 *Cert = (CHAR8*)"crypt_cert.der";
CHAR16 *CertPath = (CHAR16*)L"crypt_cert.der";
VOID extern *RsaCtx;
VOID *Data;
UINTN CertSize = 0;
UINTN BlockSize = 512;
BOOLEAN Result = FALSE;
//
// Get File Info of DER-coded X509 Cert from server
//
Status = GetFileSize(MtftpId, Cert, &CertSize);
if(EFI_ERROR(Status)) CertSize = 588;
//
// DownLoad Cert File To Buffer and Dump into File specified by CertPath
//
Status = DownloadFile (MtftpId, Cert, CertSize, BlockSize, &Data);
DumpData(Data, CertPath, &CertSize);
//
// Read Cert Content and Retrieve Pk
//
RsaCtx = NULL;
Result = RsaGetPublicKeyFromX509((UINT8*)Data, CertSize, &RsaCtx);
if (Result) {
Print(L"[Success] RSA Public Key Retrieved Successfully\n");
//Status = X509VerifyCert ((UINT8*)Data, CertSize, TestCACert, sizeof (TestCACert));
//if (EFI_ERROR(Status)) Print(L"[Warning] Cert issued from unrecognized CA\n");
} else {
Print(L"[Fail] RSA PK Retrieved Failed\n");
}
return Status;
}
//
// Process Receiced Msg and response to server
//
EFI_STATUS
EFIAPI
RecvMsgProcessing(
IN INT32 SocketId,
IN INT32 MtftpId,
IN CHAR8 *RecvBuffer
)
{
EFI_STATUS Status = EFI_SUCCESS;
UINT8 DecryptData[1024];
UINTN extern Nounce;
CHAR16 PrintBuffer[1024];
RandomSeed (NULL, 0);
//
// Case 1: server response from client hello msg
//
if (AsciiStrStr(RecvBuffer, "Invitation") != NULL){
AsciiPrint("[Info] Recved Auth Invitation\n");
//
// Extract the Nounce from Server
//
UINTN extern Nounce2;
UINT32 extern Nounce1, Nounce3;
UINT8 Record[128];
CHAR8 HelloMsg[128];
CHAR8 *MtftpBuf = (CHAR8*) malloc(4096);
UINT8 *Path = (UINT8*)"cert.pem";
CHAR8 *Pointer = AsciiStrStr(RecvBuffer, ":") + 2;
Nounce2 = AsciiStrDecimalToUintn(Pointer);
Print(L"[Info] The Second Nounce from Server: %d\n", Nounce2);
Status = RetrieveCertPk(MtftpId);
if(EFI_ERROR(Status)){
Print(L"[Fail] Retrieve Pem Cert Pk Failure Code: %d\n", Status);
return Status;
}
//
// Get and Encrypt Pre Master Key after authticating server identification
//
Status = GetRandom(&Nounce3);
AsciiSPrint(HelloMsg, sizeof(HelloMsg), "Pre Master: %d", Nounce3);
//Status = Send(SocketId, HelloMsg, sizeof(HelloMsg)+3);
#ifdef RSA
//
// Test: Hash the data and encrypt the diegst with Rsa PubKey for ServerSide Auth
//
BOOLEAN Result;
VOID extern *RsaCtx;
UINT8 Encode[512];
CHAR8 MasterKey[64];
CHAR8 RsaMsg[512];
CHAR16 PrintBuffer[128];
ZeroMem(Encode, sizeof(Encode));
ZeroMem(MasterKey, sizeof(MasterKey));
ZeroMem(PrintBuffer, sizeof(PrintBuffer));
ZeroMem(RsaMsg, sizeof(RsaMsg));
AsciiSPrint(MasterKey, 128, "MasterKey: %d", Nounce3);
Result = RsaEncrypt(RsaCtx, MasterKey, AsciiStrLen(MasterKey), Encode);
if (!Result) {
Print(L"[Fail] RSA Encryption Failed\n");
} else {
Print(L"\n[Debug] RSA Encryption Result:\n");
for (int i = 0; i < sizeof(Encode); i++) {
Print(L"%02x ", Encode[i]);
}
Print(L"\n");
AsciiSPrint(RsaMsg, 512, "==rsa==");
UintToCharSize(Encode, 512, RsaMsg);
AsciiToUnicodeSize(RsaMsg, 128, PrintBuffer);
Print(L"[Denug] The RsaMsg: %s\n", PrintBuffer);
Status = Send(SocketId, RsaMsg, sizeof(RsaMsg)+2);
}
#else
//
// Parameter for DH Pre-Matser Key Exchange
//
VOID *DhCtx;
UINT8 Prime[64];
UINT8 PublicKey1[64];
UINT8 PublicKey2[64];
UINTN PublicKey1Length;
UINTN PublicKey2Length;
UINT8 Key1[64];
UINTN Key1Length;
CHAR8 DhMsg[64];
BOOLEAN Result;
//
// Diffile-Hellman Key Exchange Configuration with g = 7 and bit(p) = 64
//
PublicKey1Length = sizeof(PublicKey1);
PublicKey2Length = sizeof(PublicKey2);
Key1Length = sizeof(Key1);
ZeroMem(DhMsg, sizeof(DhMsg));
DhCtx = DhNew();
if (DhCtx == NULL) {
Print(L"[Fail] Dh Ctx Init Failed\n");
return EFI_ABORTED;
}
Result = DhGenerateParameter (DhCtx, 7, 64, Prime);
if (!Result) {
Print(L"[Fail] Dh Set Parameter Failed\n");
return EFI_ABORTED;
}
Result = DhGenerateKey (DhCtx, PublicKey1, &PublicKey1Length);
if (!Result) {
Print(L"[Fail] Dh Generate Key Failed\n");
return EFI_ABORTED;
}
Print(L"[debug] The Public Key 1: %d\n", PublicKey1);
//
// One more loop to exchange Dh publickey and generate pre-master key
//
AsciiSPrint(DhMsg, 128, "Dh Pubkey1: %d", PublickKey1);
Status = Send(SocketId, DhMsg, AsciiStrLen(DhMsg)+2);
ZeroMem(RecvBuffer, sizeof(RecvBuffer));
Status = Recv(WebSocket, RecvBuffer, 1024);
CHAR8 Clean[64];
CHAR8 *LenEnd = AsciiStrStr(RecvBuffer, (CHAR8*)"+");
UINTN Len = LenEnd - RecvBuffer;
Print(L"[Debug] The Read-in Len is : %d\n", Len);
AsciiStrnCpyS(Clean, 128, RecvBuffer, Len);
//
// Compute Dh Key (x)^Y mod p with the publickey2 received
//
Status = AsciiStrDecimalToUintnS(Clean, NULL, PublickKey2);
if (!Status) {
Print(L"[Fail] Fail to Receive PublicKey2 from Server\n");
return EFI_ABORTED;
}
Status = DhComputeKey (DhCtx, PublicKey2, PublicKey2Length, Key1, &Key1Length);
if (!Status) {
Print(L"[Fail] Fail to Compute Dh Key\n");
return EFI_ABORTED;
}
Print(L"[Debug] The Dh Key generated: %d\n" Key1);
#endif
//
// Generate Session Key using Nounce[0:3]
//
Nounce = (UINTN)Nounce1 + Nounce2 + (UINTN)Nounce3;
Print(L"[Debug] The Nounces: %d, %d, %d\n", Nounce1, Nounce2, Nounce3);
Print(L"[Debug] The Primal Key Material: %d\n", Nounce);
//
// Aes-128 Encryption Test: The Aes key is generated from nounce
//
ZeroMem(Record, sizeof(Record));
ZeroMem(HelloMsg, sizeof(HelloMsg));
AesCryptoData(Nounce, HelloMsg, Record, sizeof(Record));
UintToCharSize(Record, 256, HelloMsg);
Status = Send(SocketId, HelloMsg, sizeof(HelloMsg)+2);
//
// Read Data from Mtftp and Store to local file
//
Status = Read(MtftpId, Path, MtftpBuf, 4096);
AsciiToUnicodeSize(MtftpBuf, 1280, PrintBuffer);
Print(L"[Debug] The Recved MTFTP Msg: %s\n", PrintBuffer);
UINTN MsgSize = StrLen(PrintBuffer) * 2;
Status = DumpData(PrintBuffer, (CHAR16*)L"TheRecvedMsg.log", &MsgSize);
}
//
// Case2: decrypt data using Aes-128 with session key and take actions
//
else if (!EFI_ERROR(Status = AesDecryptoData(Nounce, RecvBuffer, DecryptData))){
AsciiToUnicodeSize(RecvBuffer, 1024, PrintBuffer);
Print(L"\n[INFO] Communicating in Encrypted Mode...\n");
Print(L"[Debug] Encrypted Data from server: %s\n\n", PrintBuffer);
//
// Prepare to send the Pcr Digests
//
UINT8 Record[4096];
CHAR16 Buffer[4096];
CHAR8 TcpMsg[128];
UINT8 MsgRecord[128];
CHAR8 EncryptoData[8192];
CHAR8 ConvertData[8192];
ZeroMem(Record, sizeof(Record));
ZeroMem(Buffer, sizeof(Buffer));
ZeroMem(TcpMsg, sizeof(TcpMsg));
ZeroMem(MsgRecord, sizeof(MsgRecord));
ZeroMem(EncryptoData, sizeof(EncryptoData));
ZeroMem(ConvertData, sizeof(ConvertData));
Status = ExtractPcrValue(Buffer);
if (EFI_ERROR(Status)) {
Print(L"[Debug] The Extraction Process Aborted\n");
} else {
Print(L"[Debug] Extract Success...\n");
Print(L"[Debug] Return Result:\n %s", Buffer);
}
Status = Write(MtftpId, (UINT8*)"PcrValue.log", Buffer, 4096);
//
// Inform server with Encrypted Msg using TCP tunnel
//
AsciiSPrint(TcpMsg, 128, "[INFO] PCR Sent.");
AesCryptoData(Nounce, TcpMsg, MsgRecord, sizeof(MsgRecord));
ZeroMem(TcpMsg, sizeof(TcpMsg));
AsciiSPrint(TcpMsg, 128, "=====");
UintToCharSize(MsgRecord, 256, TcpMsg);
Status = Send(SocketId, TcpMsg, sizeof(TcpMsg)+2);
//
// Encrypt the data using Aes-128 algorithm and transmit with TFTP
//
UnicodeStrToAsciiStrS(Buffer, EncryptoData, sizeof(EncryptoData));
AesCryptoData(Nounce, EncryptoData, Record, sizeof(Record));
UintToCharSize(Record, 8192, ConvertData);
Status = Write(MtftpId, (UINT8*)"EncryptPcr.log", ConvertData, 8192);
//
// To send the Event Log
//
CHAR16 TextBuffer[40960];
ZeroMem(TextBuffer, sizeof(TextBuffer));
Status = GetEventLog(TextBuffer);
Print(L"[Debug] The Event Log has been extracted...\n");
Print(L"[Debug] %s", TextBuffer);
Status = Write(MtftpId, (UINT8*)"Event.log", TextBuffer, 40960);
//
// Inform server with
//
AsciiSPrint(TcpMsg, 128, "[INFO] Event Sent.");
ZeroMem(MsgRecord, sizeof(MsgRecord));
AesCryptoData(Nounce, TcpMsg, MsgRecord, sizeof(MsgRecord));
ZeroMem(TcpMsg, sizeof(TcpMsg));
AsciiSPrint(TcpMsg, 128, "=====");
UintToCharSize(MsgRecord, 256, TcpMsg);
Status = Send(SocketId, TcpMsg, sizeof(TcpMsg)+2);
Status = EFI_ABORTED;
}
return Status;
}
EFI_STATUS
EFIAPI
ShellAppMain (
IN UINTN Argc,
IN CHAR16 **Argv
)
{
EFI_STATUS Status;
//
// Test: Read Data from File into Buf and Dump Buffer into data.log
//
CHAR16 Buf[20480];
UINTN BufferSize = 20479;
CHAR16* File = (CHAR16*)L"Event.log";
Status = ReadFileToMem(Buf, &BufferSize, File);
//
// Config and Connect to the TCP/MTFTP Server
//
int WebSocket = Socket();
int WebMtftpClient = MtftpClient();
UINT32 extern Nounce1, Nounce3;
CHAR8 HelloMsg[128];
CHAR8 *RecvBuffer = (CHAR8*) malloc(1024);
Status = Connect(WebSocket, ServerAddr, 8000);
if(EFI_ERROR(Status)){
Print(L"[Fail] TCP Connect Failure Code: %d\n", Status);
return Status;
}
//
// Connect to the Mtftp Server
//
Status = MtftpConnect(WebMtftpClient, ServerAddr, 0);
if(EFI_ERROR(Status)){
Print(L"[Fail] MTFTP Connect Failure Code: %d\n", Status);
return Status;
}
//
// Send Hello Msg and Nouce to Server
//
Status = GetRandom(&Nounce1);
AsciiSPrint(HelloMsg, sizeof(HelloMsg), "HelloMsg: %d", Nounce1);
Status = Send(WebSocket, HelloMsg, sizeof(HelloMsg)+3);
//
// Staring the while loop
//
while(1) {
ZeroMem(RecvBuffer, sizeof(RecvBuffer));
Status = Recv(WebSocket, RecvBuffer, 1024);
if(EFI_ERROR(Status)){
Print(L"[Fail] Recv Failure Code: %d\n", Status);
return Status;
}
//
// Recved Msg Processing
//
Status = RecvMsgProcessing(WebSocket, WebMtftpClient, RecvBuffer);
if(!(Status == EFI_SUCCESS)){
Print(L"[Stop] Recving Loop Stopped : %d\n", Status);
break;
}
}
//Status = MtftpClose(WebMtftpClient);
//if(EFI_ERROR(Status)){
// Print(L" FTP Close Failure Code: %d\n", Status);
// return Status;
//}
return Status;
}