NetworkPkg/HttpDxe: sanity-check the TlsCaCertificate variable before use
[mirror_edk2.git] / NetworkPkg / HttpDxe / HttpsSupport.c
CommitLineData
7618784b
HW
1/** @file\r
2 Miscellaneous routines specific to Https for HttpDxe driver.\r
3\r
7ff68b5e 4Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
7618784b
HW
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "HttpDriver.h"\r
17\r
18/**\r
19 Returns the first occurrence of a Null-terminated ASCII sub-string in a Null-terminated\r
20 ASCII string and ignore case during the search process.\r
21\r
22 This function scans the contents of the ASCII string specified by String\r
23 and returns the first occurrence of SearchString and ignore case during the search process.\r
24 If SearchString is not found in String, then NULL is returned. If the length of SearchString\r
25 is zero, then String is returned.\r
26\r
27 If String is NULL, then ASSERT().\r
28 If SearchString is NULL, then ASSERT().\r
29\r
30 @param[in] String A pointer to a Null-terminated ASCII string.\r
31 @param[in] SearchString A pointer to a Null-terminated ASCII string to search for.\r
32\r
33 @retval NULL If the SearchString does not appear in String.\r
34 @retval others If there is a match return the first occurrence of SearchingString.\r
35 If the length of SearchString is zero,return String.\r
36\r
37**/\r
38CHAR8 *\r
39AsciiStrCaseStr (\r
40 IN CONST CHAR8 *String,\r
41 IN CONST CHAR8 *SearchString\r
42 )\r
43{\r
44 CONST CHAR8 *FirstMatch;\r
45 CONST CHAR8 *SearchStringTmp;\r
46\r
47 CHAR8 Src;\r
48 CHAR8 Dst;\r
49\r
50 //\r
51 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
52 //\r
53 ASSERT (AsciiStrSize (String) != 0);\r
54 ASSERT (AsciiStrSize (SearchString) != 0);\r
55\r
56 if (*SearchString == '\0') {\r
57 return (CHAR8 *) String;\r
58 }\r
59\r
60 while (*String != '\0') {\r
61 SearchStringTmp = SearchString;\r
62 FirstMatch = String;\r
63\r
64 while ((*SearchStringTmp != '\0')\r
65 && (*String != '\0')) {\r
66 Src = *String;\r
67 Dst = *SearchStringTmp;\r
68\r
69 if ((Src >= 'A') && (Src <= 'Z')) {\r
1e4725e5 70 Src += ('a' - 'A');\r
7618784b
HW
71 }\r
72\r
73 if ((Dst >= 'A') && (Dst <= 'Z')) {\r
1e4725e5 74 Dst += ('a' - 'A');\r
7618784b
HW
75 }\r
76\r
77 if (Src != Dst) {\r
78 break;\r
79 }\r
80\r
81 String++;\r
82 SearchStringTmp++;\r
83 }\r
84\r
85 if (*SearchStringTmp == '\0') {\r
86 return (CHAR8 *) FirstMatch;\r
87 }\r
88\r
89 String = FirstMatch + 1;\r
90 }\r
91\r
92 return NULL;\r
93}\r
94\r
95/**\r
96 The callback function to free the net buffer list.\r
97\r
98 @param[in] Arg The opaque parameter.\r
99\r
100**/\r
101VOID\r
102EFIAPI\r
103FreeNbufList (\r
104 IN VOID *Arg\r
105 )\r
106{\r
107 ASSERT (Arg != NULL);\r
108\r
109 NetbufFreeList ((LIST_ENTRY *) Arg);\r
110 FreePool (Arg);\r
111}\r
112\r
113/**\r
114 Check whether the Url is from Https.\r
115\r
116 @param[in] Url The pointer to a HTTP or HTTPS URL string.\r
117\r
118 @retval TRUE The Url is from HTTPS.\r
119 @retval FALSE The Url is from HTTP.\r
120\r
121**/\r
122BOOLEAN\r
123IsHttpsUrl (\r
124 IN CHAR8 *Url\r
125 )\r
126{\r
127 CHAR8 *Tmp;\r
128\r
129 Tmp = NULL;\r
130\r
131 Tmp = AsciiStrCaseStr (Url, HTTPS_FLAG);\r
132 if (Tmp != NULL && Tmp == Url) {\r
133 return TRUE;\r
134 }\r
135\r
136 return FALSE;\r
137}\r
138\r
139/**\r
140 Creates a Tls child handle, open EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL.\r
141\r
142 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
45ea8a0c 143 @param[out] TlsSb Pointer to the TLS SERVICE_BINDING_PROTOCOL.\r
7618784b
HW
144 @param[out] TlsProto Pointer to the EFI_TLS_PROTOCOL instance.\r
145 @param[out] TlsConfiguration Pointer to the EFI_TLS_CONFIGURATION_PROTOCOL instance.\r
146\r
147 @return The child handle with opened EFI_TLS_PROTOCOL and EFI_TLS_CONFIGURATION_PROTOCOL.\r
148\r
149**/\r
150EFI_HANDLE\r
151EFIAPI\r
152TlsCreateChild (\r
153 IN EFI_HANDLE ImageHandle,\r
45ea8a0c 154 OUT EFI_SERVICE_BINDING_PROTOCOL **TlsSb,\r
7618784b
HW
155 OUT EFI_TLS_PROTOCOL **TlsProto,\r
156 OUT EFI_TLS_CONFIGURATION_PROTOCOL **TlsConfiguration\r
157 )\r
158{\r
159 EFI_STATUS Status;\r
7618784b
HW
160 EFI_HANDLE TlsChildHandle;\r
161\r
7618784b
HW
162 TlsChildHandle = 0;\r
163\r
164 //\r
165 // Locate TlsServiceBinding protocol.\r
166 //\r
167 gBS->LocateProtocol (\r
168 &gEfiTlsServiceBindingProtocolGuid,\r
169 NULL,\r
45ea8a0c 170 (VOID **) TlsSb\r
7618784b 171 );\r
45ea8a0c 172 if (*TlsSb == NULL) {\r
7618784b
HW
173 return NULL;\r
174 }\r
175\r
45ea8a0c 176 Status = (*TlsSb)->CreateChild (*TlsSb, &TlsChildHandle);\r
7618784b
HW
177 if (EFI_ERROR (Status)) {\r
178 return NULL;\r
179 }\r
180\r
181 Status = gBS->OpenProtocol (\r
182 TlsChildHandle,\r
183 &gEfiTlsProtocolGuid,\r
184 (VOID **) TlsProto,\r
185 ImageHandle,\r
186 TlsChildHandle,\r
187 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
188 );\r
189 if (EFI_ERROR (Status)) {\r
45ea8a0c 190 (*TlsSb)->DestroyChild (*TlsSb, TlsChildHandle);\r
7618784b
HW
191 return NULL;\r
192 }\r
193\r
194 Status = gBS->OpenProtocol (\r
195 TlsChildHandle,\r
196 &gEfiTlsConfigurationProtocolGuid,\r
197 (VOID **) TlsConfiguration,\r
198 ImageHandle,\r
199 TlsChildHandle,\r
200 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
201 );\r
202 if (EFI_ERROR (Status)) {\r
45ea8a0c 203 (*TlsSb)->DestroyChild (*TlsSb, TlsChildHandle);\r
7618784b
HW
204 return NULL;\r
205 }\r
206\r
207 return TlsChildHandle;\r
208}\r
209\r
210/**\r
211 Create event for the TLS receive and transmit tokens which are used to receive and\r
212 transmit TLS related messages.\r
213\r
214 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
215\r
216 @retval EFI_SUCCESS The events are created successfully.\r
217 @retval others Other error as indicated.\r
218\r
219**/\r
220EFI_STATUS\r
221EFIAPI\r
222TlsCreateTxRxEvent (\r
223 IN OUT HTTP_PROTOCOL *HttpInstance\r
224 )\r
225{\r
226 EFI_STATUS Status;\r
227\r
228 if (!HttpInstance->LocalAddressIsIPv6) {\r
229 //\r
230 // For Tcp4TlsTxToken.\r
231 //\r
232 Status = gBS->CreateEvent (\r
233 EVT_NOTIFY_SIGNAL,\r
234 TPL_NOTIFY,\r
235 HttpCommonNotify,\r
236 &HttpInstance->TlsIsTxDone,\r
237 &HttpInstance->Tcp4TlsTxToken.CompletionToken.Event\r
238 );\r
239 if (EFI_ERROR (Status)) {\r
240 goto ERROR;\r
241 }\r
242\r
243 HttpInstance->Tcp4TlsTxData.Push = TRUE;\r
244 HttpInstance->Tcp4TlsTxData.Urgent = FALSE;\r
245 HttpInstance->Tcp4TlsTxData.DataLength = 0;\r
246 HttpInstance->Tcp4TlsTxData.FragmentCount = 1;\r
247 HttpInstance->Tcp4TlsTxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp4TlsTxData.DataLength;\r
248 HttpInstance->Tcp4TlsTxData.FragmentTable[0].FragmentBuffer = NULL;\r
249 HttpInstance->Tcp4TlsTxToken.Packet.TxData = &HttpInstance->Tcp4TlsTxData;\r
250 HttpInstance->Tcp4TlsTxToken.CompletionToken.Status = EFI_NOT_READY;\r
251\r
252 //\r
253 // For Tcp4TlsRxToken.\r
254 //\r
255 Status = gBS->CreateEvent (\r
256 EVT_NOTIFY_SIGNAL,\r
257 TPL_NOTIFY,\r
258 HttpCommonNotify,\r
259 &HttpInstance->TlsIsRxDone,\r
260 &HttpInstance->Tcp4TlsRxToken.CompletionToken.Event\r
261 );\r
262 if (EFI_ERROR (Status)) {\r
263 goto ERROR;\r
264 }\r
265\r
266 HttpInstance->Tcp4TlsRxData.DataLength = 0;\r
267 HttpInstance->Tcp4TlsRxData.FragmentCount = 1;\r
268 HttpInstance->Tcp4TlsRxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp4TlsRxData.DataLength ;\r
269 HttpInstance->Tcp4TlsRxData.FragmentTable[0].FragmentBuffer = NULL;\r
270 HttpInstance->Tcp4TlsRxToken.Packet.RxData = &HttpInstance->Tcp4TlsRxData;\r
271 HttpInstance->Tcp4TlsRxToken.CompletionToken.Status = EFI_NOT_READY;\r
272 } else {\r
273 //\r
274 // For Tcp6TlsTxToken.\r
275 //\r
276 Status = gBS->CreateEvent (\r
277 EVT_NOTIFY_SIGNAL,\r
278 TPL_NOTIFY,\r
279 HttpCommonNotify,\r
280 &HttpInstance->TlsIsTxDone,\r
281 &HttpInstance->Tcp6TlsTxToken.CompletionToken.Event\r
282 );\r
283 if (EFI_ERROR (Status)) {\r
284 goto ERROR;\r
285 }\r
286\r
287 HttpInstance->Tcp6TlsTxData.Push = TRUE;\r
288 HttpInstance->Tcp6TlsTxData.Urgent = FALSE;\r
289 HttpInstance->Tcp6TlsTxData.DataLength = 0;\r
290 HttpInstance->Tcp6TlsTxData.FragmentCount = 1;\r
291 HttpInstance->Tcp6TlsTxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp6TlsTxData.DataLength;\r
292 HttpInstance->Tcp6TlsTxData.FragmentTable[0].FragmentBuffer = NULL;\r
293 HttpInstance->Tcp6TlsTxToken.Packet.TxData = &HttpInstance->Tcp6TlsTxData;\r
294 HttpInstance->Tcp6TlsTxToken.CompletionToken.Status = EFI_NOT_READY;\r
295\r
296 //\r
297 // For Tcp6TlsRxToken.\r
298 //\r
299 Status = gBS->CreateEvent (\r
300 EVT_NOTIFY_SIGNAL,\r
301 TPL_NOTIFY,\r
302 HttpCommonNotify,\r
303 &HttpInstance->TlsIsRxDone,\r
304 &HttpInstance->Tcp6TlsRxToken.CompletionToken.Event\r
305 );\r
306 if (EFI_ERROR (Status)) {\r
307 goto ERROR;\r
308 }\r
309\r
310 HttpInstance->Tcp6TlsRxData.DataLength = 0;\r
311 HttpInstance->Tcp6TlsRxData.FragmentCount = 1;\r
312 HttpInstance->Tcp6TlsRxData.FragmentTable[0].FragmentLength = HttpInstance->Tcp6TlsRxData.DataLength ;\r
313 HttpInstance->Tcp6TlsRxData.FragmentTable[0].FragmentBuffer = NULL;\r
314 HttpInstance->Tcp6TlsRxToken.Packet.RxData = &HttpInstance->Tcp6TlsRxData;\r
315 HttpInstance->Tcp6TlsRxToken.CompletionToken.Status = EFI_NOT_READY;\r
316 }\r
317\r
318 return Status;\r
319\r
320ERROR:\r
321 //\r
322 // Error handling\r
323 //\r
324 TlsCloseTxRxEvent (HttpInstance);\r
325\r
326 return Status;\r
327}\r
328\r
329/**\r
330 Close events in the TlsTxToken and TlsRxToken.\r
331\r
332 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
333\r
334**/\r
335VOID\r
336EFIAPI\r
337TlsCloseTxRxEvent (\r
338 IN HTTP_PROTOCOL *HttpInstance\r
339 )\r
340{\r
341 ASSERT (HttpInstance != NULL);\r
342 if (!HttpInstance->LocalAddressIsIPv6) {\r
343 if (NULL != HttpInstance->Tcp4TlsTxToken.CompletionToken.Event) {\r
344 gBS->CloseEvent(HttpInstance->Tcp4TlsTxToken.CompletionToken.Event);\r
345 HttpInstance->Tcp4TlsTxToken.CompletionToken.Event = NULL;\r
346 }\r
347\r
348 if (NULL != HttpInstance->Tcp4TlsRxToken.CompletionToken.Event) {\r
349 gBS->CloseEvent (HttpInstance->Tcp4TlsRxToken.CompletionToken.Event);\r
350 HttpInstance->Tcp4TlsRxToken.CompletionToken.Event = NULL;\r
351 }\r
352 } else {\r
353 if (NULL != HttpInstance->Tcp6TlsTxToken.CompletionToken.Event) {\r
354 gBS->CloseEvent(HttpInstance->Tcp6TlsTxToken.CompletionToken.Event);\r
355 HttpInstance->Tcp6TlsTxToken.CompletionToken.Event = NULL;\r
356 }\r
357\r
358 if (NULL != HttpInstance->Tcp6TlsRxToken.CompletionToken.Event) {\r
359 gBS->CloseEvent (HttpInstance->Tcp6TlsRxToken.CompletionToken.Event);\r
360 HttpInstance->Tcp6TlsRxToken.CompletionToken.Event = NULL;\r
361 }\r
362 }\r
363}\r
364\r
365/**\r
366 Read the TlsCaCertificate variable and configure it.\r
367\r
368 @param[in, out] HttpInstance The HTTP instance private data.\r
369\r
370 @retval EFI_SUCCESS TlsCaCertificate is configured.\r
371 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
372 @retval EFI_NOT_FOUND Fail to get 'TlsCaCertificate' variable.\r
373 @retval Others Other error as indicated.\r
374\r
375**/\r
376EFI_STATUS\r
377TlsConfigCertificate (\r
378 IN OUT HTTP_PROTOCOL *HttpInstance\r
379 )\r
380{\r
381 EFI_STATUS Status;\r
382 UINT8 *CACert;\r
383 UINTN CACertSize;\r
384 UINT32 Index;\r
385 EFI_SIGNATURE_LIST *CertList;\r
386 EFI_SIGNATURE_DATA *Cert;\r
0fd13678 387 UINTN CertArraySizeInBytes;\r
7618784b
HW
388 UINTN CertCount;\r
389 UINT32 ItemDataSize;\r
390\r
391 CACert = NULL;\r
392 CACertSize = 0;\r
393\r
394 //\r
395 // Try to read the TlsCaCertificate variable.\r
396 //\r
397 Status = gRT->GetVariable (\r
398 EFI_TLS_CA_CERTIFICATE_VARIABLE,\r
399 &gEfiTlsCaCertificateGuid,\r
400 NULL,\r
401 &CACertSize,\r
402 NULL\r
403 );\r
404\r
405 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
406 return Status;\r
407 }\r
408\r
409 //\r
410 // Allocate buffer and read the config variable.\r
411 //\r
412 CACert = AllocatePool (CACertSize);\r
413 if (CACert == NULL) {\r
414 return EFI_OUT_OF_RESOURCES;\r
415 }\r
416\r
417 Status = gRT->GetVariable (\r
418 EFI_TLS_CA_CERTIFICATE_VARIABLE,\r
419 &gEfiTlsCaCertificateGuid,\r
420 NULL,\r
421 &CACertSize,\r
422 CACert\r
423 );\r
424 if (EFI_ERROR (Status)) {\r
425 //\r
426 // GetVariable still error or the variable is corrupted.\r
7618784b 427 //\r
6f3487a7 428 goto FreeCACert;\r
7618784b
HW
429 }\r
430\r
431 ASSERT (CACert != NULL);\r
432\r
0fd13678
LE
433 //\r
434 // Sanity check\r
435 //\r
436 Status = EFI_INVALID_PARAMETER;\r
437 CertCount = 0;\r
438 ItemDataSize = (UINT32) CACertSize;\r
439 while (ItemDataSize > 0) {\r
440 if (ItemDataSize < sizeof (EFI_SIGNATURE_LIST)) {\r
441 DEBUG ((DEBUG_ERROR, "%a: truncated EFI_SIGNATURE_LIST header\n",\r
442 __FUNCTION__));\r
443 goto FreeCACert;\r
444 }\r
445\r
446 CertList = (EFI_SIGNATURE_LIST *) (CACert + (CACertSize - ItemDataSize));\r
447\r
448 if (CertList->SignatureListSize < sizeof (EFI_SIGNATURE_LIST)) {\r
449 DEBUG ((DEBUG_ERROR,\r
450 "%a: SignatureListSize too small for EFI_SIGNATURE_LIST\n",\r
451 __FUNCTION__));\r
452 goto FreeCACert;\r
453 }\r
454\r
455 if (CertList->SignatureListSize > ItemDataSize) {\r
456 DEBUG ((DEBUG_ERROR, "%a: truncated EFI_SIGNATURE_LIST body\n",\r
457 __FUNCTION__));\r
458 goto FreeCACert;\r
459 }\r
460\r
461 if (!CompareGuid (&CertList->SignatureType, &gEfiCertX509Guid)) {\r
462 DEBUG ((DEBUG_ERROR, "%a: only X509 certificates are supported\n",\r
463 __FUNCTION__));\r
464 Status = EFI_UNSUPPORTED;\r
465 goto FreeCACert;\r
466 }\r
467\r
468 if (CertList->SignatureHeaderSize != 0) {\r
469 DEBUG ((DEBUG_ERROR, "%a: SignatureHeaderSize must be 0 for X509\n",\r
470 __FUNCTION__));\r
471 goto FreeCACert;\r
472 }\r
473\r
474 if (CertList->SignatureSize < sizeof (EFI_SIGNATURE_DATA)) {\r
475 DEBUG ((DEBUG_ERROR,\r
476 "%a: SignatureSize too small for EFI_SIGNATURE_DATA\n", __FUNCTION__));\r
477 goto FreeCACert;\r
478 }\r
479\r
480 CertArraySizeInBytes = (CertList->SignatureListSize -\r
481 sizeof (EFI_SIGNATURE_LIST));\r
482 if (CertArraySizeInBytes % CertList->SignatureSize != 0) {\r
483 DEBUG ((DEBUG_ERROR,\r
484 "%a: EFI_SIGNATURE_DATA array not a multiple of SignatureSize\n",\r
485 __FUNCTION__));\r
486 goto FreeCACert;\r
487 }\r
488\r
489 CertCount += CertArraySizeInBytes / CertList->SignatureSize;\r
490 ItemDataSize -= CertList->SignatureListSize;\r
491 }\r
492 if (CertCount == 0) {\r
493 DEBUG ((DEBUG_ERROR, "%a: no X509 certificates provided\n", __FUNCTION__));\r
494 goto FreeCACert;\r
495 }\r
496\r
7618784b
HW
497 //\r
498 // Enumerate all data and erasing the target item.\r
499 //\r
500 ItemDataSize = (UINT32) CACertSize;\r
501 CertList = (EFI_SIGNATURE_LIST *) CACert;\r
502 while ((ItemDataSize > 0) && (ItemDataSize >= CertList->SignatureListSize)) {\r
503 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize);\r
504 CertCount = (CertList->SignatureListSize - sizeof (EFI_SIGNATURE_LIST) - CertList->SignatureHeaderSize) / CertList->SignatureSize;\r
505 for (Index = 0; Index < CertCount; Index++) {\r
506 //\r
507 // EfiTlsConfigDataTypeCACertificate\r
508 //\r
509 Status = HttpInstance->TlsConfiguration->SetData (\r
510 HttpInstance->TlsConfiguration,\r
511 EfiTlsConfigDataTypeCACertificate,\r
512 Cert->SignatureData,\r
513 CertList->SignatureSize - sizeof (Cert->SignatureOwner)\r
514 );\r
515 if (EFI_ERROR (Status)) {\r
6f3487a7 516 goto FreeCACert;\r
7618784b
HW
517 }\r
518\r
519 Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize);\r
520 }\r
521\r
522 ItemDataSize -= CertList->SignatureListSize;\r
523 CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize);\r
524 }\r
525\r
6f3487a7 526FreeCACert:\r
7618784b
HW
527 FreePool (CACert);\r
528 return Status;\r
529}\r
530\r
7ff68b5e
JW
531/**\r
532 Read the HttpTlsCipherList variable and configure it for HTTPS session.\r
533\r
534 @param[in, out] HttpInstance The HTTP instance private data.\r
535\r
536 @retval EFI_SUCCESS The prefered HTTP TLS CipherList is configured.\r
537 @retval EFI_NOT_FOUND Fail to get 'HttpTlsCipherList' variable.\r
538 @retval EFI_INVALID_PARAMETER The contents of variable are invalid.\r
539 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
540\r
541 @retval Others Other error as indicated.\r
542\r
543**/\r
544EFI_STATUS\r
545TlsConfigCipherList (\r
546 IN OUT HTTP_PROTOCOL *HttpInstance\r
547 )\r
548{\r
549 EFI_STATUS Status;\r
550 UINT8 *CipherList;\r
551 UINTN CipherListSize;\r
552\r
553 CipherList = NULL;\r
554 CipherListSize = 0;\r
555\r
556 //\r
557 // Try to read the HttpTlsCipherList variable.\r
558 //\r
559 Status = gRT->GetVariable (\r
560 EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE,\r
561 &gEdkiiHttpTlsCipherListGuid,\r
562 NULL,\r
563 &CipherListSize,\r
564 NULL\r
565 );\r
566 ASSERT (EFI_ERROR (Status));\r
567 if (Status != EFI_BUFFER_TOO_SMALL) {\r
568 return Status;\r
569 }\r
570\r
571 if (CipherListSize % sizeof (EFI_TLS_CIPHER) != 0) {\r
572 return EFI_INVALID_PARAMETER;\r
573 }\r
574\r
575 //\r
576 // Allocate buffer and read the config variable.\r
577 //\r
578 CipherList = AllocatePool (CipherListSize);\r
579 if (CipherList == NULL) {\r
580 return EFI_OUT_OF_RESOURCES;\r
581 }\r
582\r
583 Status = gRT->GetVariable (\r
584 EDKII_HTTP_TLS_CIPHER_LIST_VARIABLE,\r
585 &gEdkiiHttpTlsCipherListGuid,\r
586 NULL,\r
587 &CipherListSize,\r
588 CipherList\r
589 );\r
590 if (EFI_ERROR (Status)) {\r
591 //\r
592 // GetVariable still error or the variable is corrupted.\r
593 //\r
594 goto ON_EXIT;\r
595 }\r
596\r
597 ASSERT (CipherList != NULL);\r
598\r
599 Status = HttpInstance->Tls->SetSessionData (\r
600 HttpInstance->Tls,\r
601 EfiTlsCipherList,\r
602 CipherList,\r
603 CipherListSize\r
604 );\r
605\r
606ON_EXIT: \r
607 FreePool (CipherList);\r
608 \r
609 return Status;\r
610}\r
611\r
7618784b
HW
612/**\r
613 Configure TLS session data.\r
614\r
615 @param[in, out] HttpInstance The HTTP instance private data.\r
616\r
617 @retval EFI_SUCCESS TLS session data is configured.\r
618 @retval Others Other error as indicated.\r
619\r
620**/\r
621EFI_STATUS\r
622EFIAPI\r
623TlsConfigureSession (\r
624 IN OUT HTTP_PROTOCOL *HttpInstance\r
625 )\r
626{\r
627 EFI_STATUS Status;\r
628\r
629 //\r
630 // TlsConfigData initialization\r
631 //\r
632 HttpInstance->TlsConfigData.ConnectionEnd = EfiTlsClient;\r
633 HttpInstance->TlsConfigData.VerifyMethod = EFI_TLS_VERIFY_PEER;\r
634 HttpInstance->TlsConfigData.SessionState = EfiTlsSessionNotStarted;\r
635\r
636 //\r
637 // EfiTlsConnectionEnd,\r
638 // EfiTlsVerifyMethod\r
639 // EfiTlsSessionState\r
640 //\r
641 Status = HttpInstance->Tls->SetSessionData (\r
642 HttpInstance->Tls,\r
643 EfiTlsConnectionEnd,\r
644 &(HttpInstance->TlsConfigData.ConnectionEnd),\r
645 sizeof (EFI_TLS_CONNECTION_END)\r
646 );\r
647 if (EFI_ERROR (Status)) {\r
648 return Status;\r
649 }\r
650\r
651 Status = HttpInstance->Tls->SetSessionData (\r
652 HttpInstance->Tls,\r
653 EfiTlsVerifyMethod,\r
654 &HttpInstance->TlsConfigData.VerifyMethod,\r
655 sizeof (EFI_TLS_VERIFY)\r
656 );\r
657 if (EFI_ERROR (Status)) {\r
658 return Status;\r
659 }\r
660\r
661 Status = HttpInstance->Tls->SetSessionData (\r
662 HttpInstance->Tls,\r
663 EfiTlsSessionState,\r
664 &(HttpInstance->TlsConfigData.SessionState),\r
665 sizeof (EFI_TLS_SESSION_STATE)\r
666 );\r
667 if (EFI_ERROR (Status)) {\r
668 return Status;\r
669 }\r
670\r
7ff68b5e
JW
671 //\r
672 // Tls Cipher List\r
673 //\r
674 Status = TlsConfigCipherList (HttpInstance);\r
675 if (EFI_ERROR (Status) && Status != EFI_NOT_FOUND) {\r
676 DEBUG ((EFI_D_ERROR, "TlsConfigCipherList: return %r error.\n", Status));\r
677 return Status;\r
678 }\r
679\r
7618784b
HW
680 //\r
681 // Tls Config Certificate\r
682 //\r
683 Status = TlsConfigCertificate (HttpInstance);\r
684 if (EFI_ERROR (Status)) {\r
685 DEBUG ((EFI_D_ERROR, "TLS Certificate Config Error!\n"));\r
686 return Status;\r
687 }\r
688\r
689 //\r
690 // TlsCreateTxRxEvent\r
691 //\r
692 Status = TlsCreateTxRxEvent (HttpInstance);\r
693 if (EFI_ERROR (Status)) {\r
694 goto ERROR;\r
695 }\r
696\r
697 return Status;\r
698\r
699ERROR:\r
700 TlsCloseTxRxEvent (HttpInstance);\r
701\r
702 return Status;\r
703}\r
704\r
705/**\r
706 Transmit the Packet by processing the associated HTTPS token.\r
707\r
708 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
709 @param[in] Packet The packet to transmit.\r
710\r
711 @retval EFI_SUCCESS The packet is transmitted.\r
712 @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL.\r
713 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
714 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.\r
715 @retval Others Other errors as indicated.\r
716\r
717**/\r
718EFI_STATUS\r
719EFIAPI\r
720TlsCommonTransmit (\r
721 IN OUT HTTP_PROTOCOL *HttpInstance,\r
722 IN NET_BUF *Packet\r
723 )\r
724{\r
725 EFI_STATUS Status;\r
726 VOID *Data;\r
727 UINTN Size;\r
728\r
729 if ((HttpInstance == NULL) || (Packet == NULL)) {\r
730 return EFI_INVALID_PARAMETER;\r
731 }\r
732\r
733 if (!HttpInstance->LocalAddressIsIPv6) {\r
734 Size = sizeof (EFI_TCP4_TRANSMIT_DATA) +\r
735 (Packet->BlockOpNum - 1) * sizeof (EFI_TCP4_FRAGMENT_DATA);\r
736 } else {\r
737 Size = sizeof (EFI_TCP6_TRANSMIT_DATA) +\r
738 (Packet->BlockOpNum - 1) * sizeof (EFI_TCP6_FRAGMENT_DATA);\r
739 }\r
740\r
741 Data = AllocatePool (Size);\r
742 if (Data == NULL) {\r
743 return EFI_OUT_OF_RESOURCES;\r
744 }\r
745\r
746 if (!HttpInstance->LocalAddressIsIPv6) {\r
747 ((EFI_TCP4_TRANSMIT_DATA *) Data)->Push = TRUE;\r
748 ((EFI_TCP4_TRANSMIT_DATA *) Data)->Urgent = FALSE;\r
749 ((EFI_TCP4_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize;\r
750\r
751 //\r
752 // Build the fragment table.\r
753 //\r
754 ((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount = Packet->BlockOpNum;\r
755\r
756 NetbufBuildExt (\r
757 Packet,\r
758 (NET_FRAGMENT *) &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentTable[0],\r
759 &((EFI_TCP4_TRANSMIT_DATA *) Data)->FragmentCount\r
760 );\r
761\r
762 HttpInstance->Tcp4TlsTxToken.Packet.TxData = (EFI_TCP4_TRANSMIT_DATA *) Data;\r
763\r
764 Status = EFI_DEVICE_ERROR;\r
765\r
766 //\r
767 // Transmit the packet.\r
768 //\r
769 Status = HttpInstance->Tcp4->Transmit (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsTxToken);\r
770 if (EFI_ERROR (Status)) {\r
771 goto ON_EXIT;\r
772 }\r
773\r
774 while (!HttpInstance->TlsIsTxDone) {\r
775 HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
776 }\r
777\r
778 HttpInstance->TlsIsTxDone = FALSE;\r
779 Status = HttpInstance->Tcp4TlsTxToken.CompletionToken.Status;\r
780 } else {\r
781 ((EFI_TCP6_TRANSMIT_DATA *) Data)->Push = TRUE;\r
782 ((EFI_TCP6_TRANSMIT_DATA *) Data)->Urgent = FALSE;\r
783 ((EFI_TCP6_TRANSMIT_DATA *) Data)->DataLength = Packet->TotalSize;\r
784\r
785 //\r
786 // Build the fragment table.\r
787 //\r
788 ((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentCount = Packet->BlockOpNum;\r
789\r
790 NetbufBuildExt (\r
791 Packet,\r
792 (NET_FRAGMENT *) &((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentTable[0],\r
793 &((EFI_TCP6_TRANSMIT_DATA *) Data)->FragmentCount\r
794 );\r
795\r
796 HttpInstance->Tcp6TlsTxToken.Packet.TxData = (EFI_TCP6_TRANSMIT_DATA *) Data;\r
797\r
798 Status = EFI_DEVICE_ERROR;\r
799\r
800 //\r
801 // Transmit the packet.\r
802 //\r
803 Status = HttpInstance->Tcp6->Transmit (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsTxToken);\r
804 if (EFI_ERROR (Status)) {\r
805 goto ON_EXIT;\r
806 }\r
807\r
808 while (!HttpInstance->TlsIsTxDone) {\r
809 HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
810 }\r
811\r
812 HttpInstance->TlsIsTxDone = FALSE;\r
813 Status = HttpInstance->Tcp6TlsTxToken.CompletionToken.Status;\r
814 }\r
815\r
816ON_EXIT:\r
817 FreePool (Data);\r
818\r
819 return Status;\r
820}\r
821\r
822/**\r
823 Receive the Packet by processing the associated HTTPS token.\r
824\r
825 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
826 @param[in] Packet The packet to transmit.\r
827 @param[in] Timeout The time to wait for connection done.\r
828\r
829 @retval EFI_SUCCESS The Packet is received.\r
830 @retval EFI_INVALID_PARAMETER HttpInstance is NULL or Packet is NULL.\r
831 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
832 @retval EFI_TIMEOUT The operation is time out.\r
833 @retval Others Other error as indicated.\r
834\r
835**/\r
836EFI_STATUS\r
837EFIAPI\r
838TlsCommonReceive (\r
839 IN OUT HTTP_PROTOCOL *HttpInstance,\r
840 IN NET_BUF *Packet,\r
841 IN EFI_EVENT Timeout\r
842 )\r
843{\r
844 EFI_TCP4_RECEIVE_DATA *Tcp4RxData;\r
845 EFI_TCP6_RECEIVE_DATA *Tcp6RxData;\r
846 EFI_STATUS Status;\r
847 NET_FRAGMENT *Fragment;\r
848 UINT32 FragmentCount;\r
849 UINT32 CurrentFragment;\r
850\r
851 Tcp4RxData = NULL;\r
852 Tcp6RxData = NULL;\r
853\r
854 if ((HttpInstance == NULL) || (Packet == NULL)) {\r
855 return EFI_INVALID_PARAMETER;\r
856 }\r
857\r
858 FragmentCount = Packet->BlockOpNum;\r
859 Fragment = AllocatePool (FragmentCount * sizeof (NET_FRAGMENT));\r
860 if (Fragment == NULL) {\r
861 Status = EFI_OUT_OF_RESOURCES;\r
862 goto ON_EXIT;\r
863 }\r
864\r
865 //\r
866 // Build the fragment table.\r
867 //\r
868 NetbufBuildExt (Packet, Fragment, &FragmentCount);\r
869\r
870 if (!HttpInstance->LocalAddressIsIPv6) {\r
871 Tcp4RxData = HttpInstance->Tcp4TlsRxToken.Packet.RxData;\r
872 if (Tcp4RxData == NULL) {\r
873 return EFI_INVALID_PARAMETER;\r
874 }\r
875 Tcp4RxData->FragmentCount = 1;\r
876 } else {\r
877 Tcp6RxData = HttpInstance->Tcp6TlsRxToken.Packet.RxData;\r
878 if (Tcp6RxData == NULL) {\r
879 return EFI_INVALID_PARAMETER;\r
880 }\r
881 Tcp6RxData->FragmentCount = 1;\r
882 }\r
883\r
884 CurrentFragment = 0;\r
885 Status = EFI_SUCCESS;\r
886\r
887 while (CurrentFragment < FragmentCount) {\r
888 if (!HttpInstance->LocalAddressIsIPv6) {\r
889 Tcp4RxData->DataLength = Fragment[CurrentFragment].Len;\r
890 Tcp4RxData->FragmentTable[0].FragmentLength = Fragment[CurrentFragment].Len;\r
891 Tcp4RxData->FragmentTable[0].FragmentBuffer = Fragment[CurrentFragment].Bulk;\r
892 Status = HttpInstance->Tcp4->Receive (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken);\r
893 } else {\r
894 Tcp6RxData->DataLength = Fragment[CurrentFragment].Len;\r
895 Tcp6RxData->FragmentTable[0].FragmentLength = Fragment[CurrentFragment].Len;\r
896 Tcp6RxData->FragmentTable[0].FragmentBuffer = Fragment[CurrentFragment].Bulk;\r
897 Status = HttpInstance->Tcp6->Receive (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken);\r
898 }\r
899 if (EFI_ERROR (Status)) {\r
900 goto ON_EXIT;\r
901 }\r
902\r
903 while (!HttpInstance->TlsIsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) {\r
904 //\r
905 // Poll until some data is received or an error occurs.\r
906 //\r
907 if (!HttpInstance->LocalAddressIsIPv6) {\r
908 HttpInstance->Tcp4->Poll (HttpInstance->Tcp4);\r
909 } else {\r
910 HttpInstance->Tcp6->Poll (HttpInstance->Tcp6);\r
911 }\r
912 }\r
913\r
914 if (!HttpInstance->TlsIsRxDone) {\r
915 //\r
916 // Timeout occurs, cancel the receive request.\r
917 //\r
918 if (!HttpInstance->LocalAddressIsIPv6) {\r
919 HttpInstance->Tcp4->Cancel (HttpInstance->Tcp4, &HttpInstance->Tcp4TlsRxToken.CompletionToken);\r
920 } else {\r
921 HttpInstance->Tcp6->Cancel (HttpInstance->Tcp6, &HttpInstance->Tcp6TlsRxToken.CompletionToken);\r
922 }\r
923\r
924 Status = EFI_TIMEOUT;\r
925 goto ON_EXIT;\r
926 } else {\r
927 HttpInstance->TlsIsRxDone = FALSE;\r
928 }\r
929\r
930 if (!HttpInstance->LocalAddressIsIPv6) {\r
931 Status = HttpInstance->Tcp4TlsRxToken.CompletionToken.Status;\r
932 if (EFI_ERROR (Status)) {\r
933 goto ON_EXIT;\r
934 }\r
935\r
936 Fragment[CurrentFragment].Len -= Tcp4RxData->FragmentTable[0].FragmentLength;\r
937 if (Fragment[CurrentFragment].Len == 0) {\r
938 CurrentFragment++;\r
939 } else {\r
940 Fragment[CurrentFragment].Bulk += Tcp4RxData->FragmentTable[0].FragmentLength;\r
941 }\r
942 } else {\r
943 Status = HttpInstance->Tcp6TlsRxToken.CompletionToken.Status;\r
944 if (EFI_ERROR (Status)) {\r
945 goto ON_EXIT;\r
946 }\r
947\r
948 Fragment[CurrentFragment].Len -= Tcp6RxData->FragmentTable[0].FragmentLength;\r
949 if (Fragment[CurrentFragment].Len == 0) {\r
950 CurrentFragment++;\r
951 } else {\r
952 Fragment[CurrentFragment].Bulk += Tcp6RxData->FragmentTable[0].FragmentLength;\r
953 }\r
954 }\r
955 }\r
956\r
957ON_EXIT:\r
958\r
959 if (Fragment != NULL) {\r
960 FreePool (Fragment);\r
961 }\r
962\r
963 return Status;\r
964}\r
965\r
966/**\r
967 Receive one TLS PDU. An TLS PDU contains an TLS record header and it's\r
968 corresponding record data. These two parts will be put into two blocks of buffers in the\r
969 net buffer.\r
970\r
971 @param[in, out] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
972 @param[out] Pdu The received TLS PDU.\r
973 @param[in] Timeout The time to wait for connection done.\r
974\r
975 @retval EFI_SUCCESS An TLS PDU is received.\r
976 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
977 @retval EFI_PROTOCOL_ERROR An unexpected TLS packet was received.\r
978 @retval Others Other errors as indicated.\r
979\r
980**/\r
981EFI_STATUS\r
982EFIAPI\r
983TlsReceiveOnePdu (\r
984 IN OUT HTTP_PROTOCOL *HttpInstance,\r
985 OUT NET_BUF **Pdu,\r
986 IN EFI_EVENT Timeout\r
987 )\r
988{\r
989 EFI_STATUS Status;\r
990\r
991 LIST_ENTRY *NbufList;\r
992\r
993 UINT32 Len;\r
994\r
995 NET_BUF *PduHdr;\r
996 UINT8 *Header;\r
997 TLS_RECORD_HEADER RecordHeader;\r
998\r
999 NET_BUF *DataSeg;\r
1000\r
1001 NbufList = NULL;\r
1002 PduHdr = NULL;\r
1003 Header = NULL;\r
1004 DataSeg = NULL;\r
1005\r
1006 NbufList = AllocatePool (sizeof (LIST_ENTRY));\r
1007 if (NbufList == NULL) {\r
1008 return EFI_OUT_OF_RESOURCES;\r
1009 }\r
1010\r
1011 InitializeListHead (NbufList);\r
1012\r
1013 //\r
1014 // Allocate buffer to receive one TLS header.\r
1015 //\r
07bd82d4 1016 Len = TLS_RECORD_HEADER_LENGTH;\r
7618784b
HW
1017 PduHdr = NetbufAlloc (Len);\r
1018 if (PduHdr == NULL) {\r
1019 Status = EFI_OUT_OF_RESOURCES;\r
1020 goto ON_EXIT;\r
1021 }\r
1022\r
1023 Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL);\r
1024 if (Header == NULL) {\r
1025 Status = EFI_OUT_OF_RESOURCES;\r
1026 goto ON_EXIT;\r
1027 }\r
1028\r
1029 //\r
1030 // First step, receive one TLS header.\r
1031 //\r
1032 Status = TlsCommonReceive (HttpInstance, PduHdr, Timeout);\r
1033 if (EFI_ERROR (Status)) {\r
1034 goto ON_EXIT;\r
1035 }\r
1036\r
1037 RecordHeader = *(TLS_RECORD_HEADER *) Header;\r
1038 if ((RecordHeader.ContentType == TlsContentTypeHandshake ||\r
1039 RecordHeader.ContentType == TlsContentTypeAlert ||\r
1040 RecordHeader.ContentType == TlsContentTypeChangeCipherSpec ||\r
1041 RecordHeader.ContentType == TlsContentTypeApplicationData) &&\r
1042 (RecordHeader.Version.Major == 0x03) && /// Major versions are same.\r
1043 (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR ||\r
1044 RecordHeader.Version.Minor ==TLS11_PROTOCOL_VERSION_MINOR ||\r
1045 RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR)\r
1046 ) {\r
1047 InsertTailList (NbufList, &PduHdr->List);\r
1048 } else {\r
1049 Status = EFI_PROTOCOL_ERROR;\r
1050 goto ON_EXIT;\r
1051 }\r
1052\r
1053 Len = SwapBytes16(RecordHeader.Length);\r
1054 if (Len == 0) {\r
1055 //\r
1056 // No TLS payload.\r
1057 //\r
1058 goto FORM_PDU;\r
1059 }\r
1060\r
1061 //\r
1062 // Allocate buffer to receive one TLS payload.\r
1063 //\r
1064 DataSeg = NetbufAlloc (Len);\r
1065 if (DataSeg == NULL) {\r
1066 Status = EFI_OUT_OF_RESOURCES;\r
1067 goto ON_EXIT;\r
1068 }\r
1069\r
1070 NetbufAllocSpace (DataSeg, Len, NET_BUF_TAIL);\r
1071\r
1072 //\r
1073 // Second step, receive one TLS payload.\r
1074 //\r
1075 Status = TlsCommonReceive (HttpInstance, DataSeg, Timeout);\r
1076 if (EFI_ERROR (Status)) {\r
1077 goto ON_EXIT;\r
1078 }\r
1079\r
1080 InsertTailList (NbufList, &DataSeg->List);\r
1081\r
1082FORM_PDU:\r
1083 //\r
1084 // Form the PDU from a list of PDU.\r
1085 //\r
1086 *Pdu = NetbufFromBufList (NbufList, 0, 0, FreeNbufList, NbufList);\r
1087 if (*Pdu == NULL) {\r
1088 Status = EFI_OUT_OF_RESOURCES;\r
1089 }\r
1090\r
1091ON_EXIT:\r
1092\r
1093 if (EFI_ERROR (Status)) {\r
1094 //\r
1095 // Free the Nbufs in this NbufList and the NbufList itself.\r
1096 //\r
1097 FreeNbufList (NbufList);\r
1098 }\r
1099\r
1100 return Status;\r
1101}\r
1102\r
1103/**\r
1104 Connect one TLS session by finishing the TLS handshake process.\r
1105\r
1106 @param[in] HttpInstance The HTTP instance private data.\r
1107 @param[in] Timeout The time to wait for connection done.\r
1108\r
1109 @retval EFI_SUCCESS The TLS session is established.\r
1110 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
1111 @retval EFI_ABORTED TLS session state is incorrect.\r
1112 @retval Others Other error as indicated.\r
1113\r
1114**/\r
1115EFI_STATUS\r
1116EFIAPI\r
1117TlsConnectSession (\r
1118 IN HTTP_PROTOCOL *HttpInstance,\r
1119 IN EFI_EVENT Timeout\r
1120 )\r
1121{\r
1122 EFI_STATUS Status;\r
1123 UINT8 *BufferOut;\r
1124 UINTN BufferOutSize;\r
1125 NET_BUF *PacketOut;\r
1126 UINT8 *DataOut;\r
1127 NET_BUF *Pdu;\r
1128 UINT8 *BufferIn;\r
1129 UINTN BufferInSize;\r
1130 UINT8 *GetSessionDataBuffer;\r
1131 UINTN GetSessionDataBufferSize;\r
1132\r
1133 BufferOut = NULL;\r
1134 PacketOut = NULL;\r
1135 DataOut = NULL;\r
1136 Pdu = NULL;\r
1137 BufferIn = NULL;\r
1138\r
1139 //\r
1140 // Initialize TLS state.\r
1141 //\r
1142 HttpInstance->TlsSessionState = EfiTlsSessionNotStarted;\r
1143 Status = HttpInstance->Tls->SetSessionData (\r
1144 HttpInstance->Tls,\r
1145 EfiTlsSessionState,\r
1146 &(HttpInstance->TlsSessionState),\r
1147 sizeof (EFI_TLS_SESSION_STATE)\r
1148 );\r
1149 if (EFI_ERROR (Status)) {\r
1150 return Status;\r
1151 }\r
1152\r
1153 //\r
1154 // Create ClientHello\r
1155 //\r
1156 BufferOutSize = DEF_BUF_LEN;\r
1157 BufferOut = AllocateZeroPool (BufferOutSize);\r
1158 if (BufferOut == NULL) {\r
1159 Status = EFI_OUT_OF_RESOURCES;\r
1160 return Status;\r
1161 }\r
1162\r
1163 Status = HttpInstance->Tls->BuildResponsePacket (\r
1164 HttpInstance->Tls,\r
1165 NULL,\r
1166 0,\r
1167 BufferOut,\r
1168 &BufferOutSize\r
1169 );\r
1170 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1171 FreePool (BufferOut);\r
1172 BufferOut = AllocateZeroPool (BufferOutSize);\r
1173 if (BufferOut == NULL) {\r
1174 Status = EFI_OUT_OF_RESOURCES;\r
1175 return Status;\r
1176 }\r
1177\r
1178 Status = HttpInstance->Tls->BuildResponsePacket (\r
1179 HttpInstance->Tls,\r
1180 NULL,\r
1181 0,\r
1182 BufferOut,\r
1183 &BufferOutSize\r
1184 );\r
1185 }\r
1186 if (EFI_ERROR (Status)) {\r
1187 FreePool (BufferOut);\r
1188 return Status;\r
1189 }\r
1190\r
1191 //\r
1192 // Transmit ClientHello\r
1193 //\r
1194 PacketOut = NetbufAlloc ((UINT32) BufferOutSize);\r
1195 DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL);\r
1196 if (DataOut == NULL) {\r
1197 FreePool (BufferOut);\r
1198 return EFI_OUT_OF_RESOURCES;\r
1199 }\r
1200\r
1201 CopyMem (DataOut, BufferOut, BufferOutSize);\r
1202 Status = TlsCommonTransmit (HttpInstance, PacketOut);\r
1203\r
1204 FreePool (BufferOut);\r
1205 NetbufFree (PacketOut);\r
1206\r
1207 if (EFI_ERROR (Status)) {\r
1208 return Status;\r
1209 }\r
1210\r
1211 while(HttpInstance->TlsSessionState != EfiTlsSessionDataTransferring && \\r
1212 ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) {\r
1213 //\r
1214 // Receive one TLS record.\r
1215 //\r
1216 Status = TlsReceiveOnePdu (HttpInstance, &Pdu, Timeout);\r
1217 if (EFI_ERROR (Status)) {\r
1218 return Status;\r
1219 }\r
1220\r
1221 BufferInSize = Pdu->TotalSize;\r
1222 BufferIn = AllocateZeroPool (BufferInSize);\r
1223 if (BufferIn == NULL) {\r
1224 NetbufFree (Pdu);\r
1225 Status = EFI_OUT_OF_RESOURCES;\r
1226 return Status;\r
1227 }\r
1228\r
1229 NetbufCopy (Pdu, 0, (UINT32)BufferInSize, BufferIn);\r
1230\r
1231 NetbufFree (Pdu);\r
1232\r
1233 //\r
1234 // Handle Receive data.\r
1235 //\r
1236 BufferOutSize = DEF_BUF_LEN;\r
1237 BufferOut = AllocateZeroPool (BufferOutSize);\r
1238 if (BufferOut == NULL) {\r
1239 Status = EFI_OUT_OF_RESOURCES;\r
1240 return Status;\r
1241 }\r
1242\r
1243 Status = HttpInstance->Tls->BuildResponsePacket (\r
1244 HttpInstance->Tls,\r
1245 BufferIn,\r
1246 BufferInSize,\r
1247 BufferOut,\r
1248 &BufferOutSize\r
1249 );\r
1250 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1251 FreePool (BufferOut);\r
1252 BufferOut = AllocateZeroPool (BufferOutSize);\r
1253 if (BufferOut == NULL) {\r
1254 FreePool (BufferIn);\r
1255 Status = EFI_OUT_OF_RESOURCES;\r
1256 return Status;\r
1257 }\r
1258\r
1259 Status = HttpInstance->Tls->BuildResponsePacket (\r
1260 HttpInstance->Tls,\r
1261 BufferIn,\r
1262 BufferInSize,\r
1263 BufferOut,\r
1264 &BufferOutSize\r
1265 );\r
1266 }\r
1267\r
1268 FreePool (BufferIn);\r
1269\r
1270 if (EFI_ERROR (Status)) {\r
1271 FreePool (BufferOut);\r
1272 return Status;\r
1273 }\r
1274\r
1275 if (BufferOutSize != 0) {\r
1276 //\r
1277 // Transmit the response packet.\r
1278 //\r
1279 PacketOut = NetbufAlloc ((UINT32) BufferOutSize);\r
1280 DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL);\r
1281 if (DataOut == NULL) {\r
1282 FreePool (BufferOut);\r
1283 return EFI_OUT_OF_RESOURCES;\r
1284 }\r
1285\r
1286 CopyMem (DataOut, BufferOut, BufferOutSize);\r
1287\r
1288 Status = TlsCommonTransmit (HttpInstance, PacketOut);\r
1289\r
1290 NetbufFree (PacketOut);\r
1291\r
1292 if (EFI_ERROR (Status)) {\r
1293 FreePool (BufferOut);\r
1294 return Status;\r
1295 }\r
1296 }\r
1297\r
1298 FreePool (BufferOut);\r
1299\r
1300 //\r
1301 // Get the session state, then decide whether need to continue handle received packet.\r
1302 //\r
1303 GetSessionDataBufferSize = DEF_BUF_LEN;\r
1304 GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize);\r
1305 if (GetSessionDataBuffer == NULL) {\r
1306 Status = EFI_OUT_OF_RESOURCES;\r
1307 return Status;\r
1308 }\r
1309\r
1310 Status = HttpInstance->Tls->GetSessionData (\r
1311 HttpInstance->Tls,\r
1312 EfiTlsSessionState,\r
1313 GetSessionDataBuffer,\r
1314 &GetSessionDataBufferSize\r
1315 );\r
1316 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1317 FreePool (GetSessionDataBuffer);\r
1318 GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize);\r
1319 if (GetSessionDataBuffer == NULL) {\r
1320 Status = EFI_OUT_OF_RESOURCES;\r
1321 return Status;\r
1322 }\r
1323\r
1324 Status = HttpInstance->Tls->GetSessionData (\r
1325 HttpInstance->Tls,\r
1326 EfiTlsSessionState,\r
1327 GetSessionDataBuffer,\r
1328 &GetSessionDataBufferSize\r
1329 );\r
1330 }\r
1331 if (EFI_ERROR (Status)) {\r
1332 FreePool(GetSessionDataBuffer);\r
1333 return Status;\r
1334 }\r
1335\r
1336 ASSERT(GetSessionDataBufferSize == sizeof (EFI_TLS_SESSION_STATE));\r
1337 HttpInstance->TlsSessionState = *(EFI_TLS_SESSION_STATE *) GetSessionDataBuffer;\r
1338\r
1339 FreePool (GetSessionDataBuffer);\r
1340\r
1341 if(HttpInstance->TlsSessionState == EfiTlsSessionError) {\r
1342 return EFI_ABORTED;\r
1343 }\r
1344 }\r
1345\r
1346 if (HttpInstance->TlsSessionState != EfiTlsSessionDataTransferring) {\r
1347 Status = EFI_ABORTED;\r
1348 }\r
1349\r
1350 return Status;\r
1351}\r
1352\r
1353/**\r
1354 Close the TLS session and send out the close notification message.\r
1355\r
1356 @param[in] HttpInstance The HTTP instance private data.\r
1357\r
1358 @retval EFI_SUCCESS The TLS session is closed.\r
1359 @retval EFI_INVALID_PARAMETER HttpInstance is NULL.\r
1360 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
1361 @retval Others Other error as indicated.\r
1362\r
1363**/\r
1364EFI_STATUS\r
1365EFIAPI\r
1366TlsCloseSession (\r
1367 IN HTTP_PROTOCOL *HttpInstance\r
1368 )\r
1369{\r
1370 EFI_STATUS Status;\r
1371\r
1372 UINT8 *BufferOut;\r
1373 UINTN BufferOutSize;\r
1374\r
1375 NET_BUF *PacketOut;\r
1376 UINT8 *DataOut;\r
1377\r
1378 Status = EFI_SUCCESS;\r
1379 BufferOut = NULL;\r
1380 PacketOut = NULL;\r
1381 DataOut = NULL;\r
1382\r
1383 if (HttpInstance == NULL) {\r
1384 return EFI_INVALID_PARAMETER;\r
1385 }\r
1386\r
1387 HttpInstance->TlsSessionState = EfiTlsSessionClosing;\r
1388\r
1389 Status = HttpInstance->Tls->SetSessionData (\r
1390 HttpInstance->Tls,\r
1391 EfiTlsSessionState,\r
1392 &(HttpInstance->TlsSessionState),\r
1393 sizeof (EFI_TLS_SESSION_STATE)\r
1394 );\r
1395 if (EFI_ERROR (Status)) {\r
1396 return Status;\r
1397 }\r
1398\r
1399 BufferOutSize = DEF_BUF_LEN;\r
1400 BufferOut = AllocateZeroPool (BufferOutSize);\r
1401 if (BufferOut == NULL) {\r
1402 Status = EFI_OUT_OF_RESOURCES;\r
1403 return Status;\r
1404 }\r
1405\r
1406 Status = HttpInstance->Tls->BuildResponsePacket (\r
1407 HttpInstance->Tls,\r
1408 NULL,\r
1409 0,\r
1410 BufferOut,\r
1411 &BufferOutSize\r
1412 );\r
1413 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1414 FreePool (BufferOut);\r
1415 BufferOut = AllocateZeroPool (BufferOutSize);\r
1416 if (BufferOut == NULL) {\r
1417 Status = EFI_OUT_OF_RESOURCES;\r
1418 return Status;\r
1419 }\r
1420\r
1421 Status = HttpInstance->Tls->BuildResponsePacket (\r
1422 HttpInstance->Tls,\r
1423 NULL,\r
1424 0,\r
1425 BufferOut,\r
1426 &BufferOutSize\r
1427 );\r
1428 }\r
1429\r
1430 if (EFI_ERROR (Status)) {\r
1431 FreePool (BufferOut);\r
1432 return Status;\r
1433 }\r
1434\r
1435 PacketOut = NetbufAlloc ((UINT32) BufferOutSize);\r
1436 DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL);\r
1437 if (DataOut == NULL) {\r
1438 FreePool (BufferOut);\r
1439 return EFI_OUT_OF_RESOURCES;\r
1440 }\r
1441\r
1442 CopyMem (DataOut, BufferOut, BufferOutSize);\r
1443\r
1444 Status = TlsCommonTransmit (HttpInstance, PacketOut);\r
1445\r
1446 FreePool (BufferOut);\r
1447 NetbufFree (PacketOut);\r
1448\r
1449 return Status;\r
1450}\r
1451\r
1452/**\r
1453 Process one message according to the CryptMode.\r
1454\r
1455 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
07bd82d4
JW
1456 @param[in] Message Pointer to the message buffer needed to processed. \r
1457 If ProcessMode is EfiTlsEncrypt, the message contain the TLS\r
1458 header and plain text TLS APP payload.\r
1459 If ProcessMode is EfiTlsDecrypt, the message contain the TLS \r
1460 header and cipher text TLS APP payload.\r
7618784b
HW
1461 @param[in] MessageSize Pointer to the message buffer size.\r
1462 @param[in] ProcessMode Process mode.\r
1463 @param[in, out] Fragment Only one Fragment returned after the Message is\r
1464 processed successfully.\r
07bd82d4
JW
1465 If ProcessMode is EfiTlsEncrypt, the fragment contain the TLS \r
1466 header and cipher text TLS APP payload.\r
1467 If ProcessMode is EfiTlsDecrypt, the fragment contain the TLS \r
1468 header and plain text TLS APP payload.\r
7618784b
HW
1469\r
1470 @retval EFI_SUCCESS Message is processed successfully.\r
1471 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
1472 @retval Others Other errors as indicated.\r
1473\r
1474**/\r
1475EFI_STATUS\r
1476EFIAPI\r
1477TlsProcessMessage (\r
1478 IN HTTP_PROTOCOL *HttpInstance,\r
1479 IN UINT8 *Message,\r
1480 IN UINTN MessageSize,\r
1481 IN EFI_TLS_CRYPT_MODE ProcessMode,\r
1482 IN OUT NET_FRAGMENT *Fragment\r
1483 )\r
1484{\r
1485 EFI_STATUS Status;\r
1486 UINT8 *Buffer;\r
1487 UINT32 BufferSize;\r
1488 UINT32 BytesCopied;\r
1489 EFI_TLS_FRAGMENT_DATA *FragmentTable;\r
1490 UINT32 FragmentCount;\r
1491 EFI_TLS_FRAGMENT_DATA *OriginalFragmentTable;\r
1492 UINTN Index;\r
1493\r
1494 Status = EFI_SUCCESS;\r
1495 Buffer = NULL;\r
1496 BufferSize = 0;\r
1497 BytesCopied = 0;\r
1498 FragmentTable = NULL;\r
1499 OriginalFragmentTable = NULL;\r
1500\r
1501 //\r
1502 // Rebuild fragment table from BufferIn.\r
1503 //\r
1504 FragmentCount = 1;\r
1505 FragmentTable = AllocateZeroPool (FragmentCount * sizeof (EFI_TLS_FRAGMENT_DATA));\r
1506 if (FragmentTable == NULL) {\r
1507 Status = EFI_OUT_OF_RESOURCES;\r
1508 goto ON_EXIT;\r
1509 }\r
1510\r
1511 FragmentTable->FragmentLength = (UINT32) MessageSize;\r
1512 FragmentTable->FragmentBuffer = Message;\r
1513\r
1514 //\r
1515 // Record the original FragmentTable.\r
1516 //\r
1517 OriginalFragmentTable = FragmentTable;\r
1518\r
1519 //\r
1520 // Process the Message.\r
1521 //\r
1522 Status = HttpInstance->Tls->ProcessPacket (\r
1523 HttpInstance->Tls,\r
1524 &FragmentTable,\r
1525 &FragmentCount,\r
1526 ProcessMode\r
1527 );\r
1528 if (EFI_ERROR (Status)) {\r
1529 goto ON_EXIT;\r
1530 }\r
1531\r
1532 //\r
1533 // Calculate the size according to FragmentTable.\r
1534 //\r
1535 for (Index = 0; Index < FragmentCount; Index++) {\r
1536 BufferSize += FragmentTable[Index].FragmentLength;\r
1537 }\r
1538\r
1539 //\r
1540 // Allocate buffer for processed data.\r
1541 //\r
1542 Buffer = AllocateZeroPool (BufferSize);\r
1543 if (Buffer == NULL) {\r
1544 Status = EFI_OUT_OF_RESOURCES;\r
1545 goto ON_EXIT;\r
1546 }\r
1547\r
1548 //\r
1549 // Copy the new FragmentTable buffer into Buffer.\r
1550 //\r
1551 for (Index = 0; Index < FragmentCount; Index++) {\r
1552 CopyMem (\r
1553 (Buffer + BytesCopied),\r
1554 FragmentTable[Index].FragmentBuffer,\r
1555 FragmentTable[Index].FragmentLength\r
1556 );\r
1557 BytesCopied += FragmentTable[Index].FragmentLength;\r
1558\r
1559 //\r
1560 // Free the FragmentBuffer since it has been copied.\r
1561 //\r
1562 FreePool (FragmentTable[Index].FragmentBuffer);\r
1563 }\r
1564\r
1565 Fragment->Len = BufferSize;\r
1566 Fragment->Bulk = Buffer;\r
1567\r
1568ON_EXIT:\r
1569\r
1570 if (OriginalFragmentTable != NULL) {\r
07bd82d4
JW
1571 if( FragmentTable == OriginalFragmentTable) {\r
1572 FragmentTable = NULL;\r
1573 }\r
7618784b
HW
1574 FreePool (OriginalFragmentTable);\r
1575 OriginalFragmentTable = NULL;\r
1576 }\r
1577\r
1578 //\r
1579 // Caller has the responsibility to free the FragmentTable.\r
1580 //\r
1581 if (FragmentTable != NULL) {\r
1582 FreePool (FragmentTable);\r
1583 FragmentTable = NULL;\r
1584 }\r
1585\r
1586 return Status;\r
1587}\r
1588\r
1589/**\r
1590 Receive one fragment decrypted from one TLS record.\r
1591\r
1592 @param[in] HttpInstance Pointer to HTTP_PROTOCOL structure.\r
1593 @param[in, out] Fragment The received Fragment.\r
1594 @param[in] Timeout The time to wait for connection done.\r
1595\r
1596 @retval EFI_SUCCESS One fragment is received.\r
1597 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
1598 @retval EFI_ABORTED Something wrong decryption the message.\r
1599 @retval Others Other errors as indicated.\r
1600\r
1601**/\r
1602EFI_STATUS\r
1603EFIAPI\r
1604HttpsReceive (\r
1605 IN HTTP_PROTOCOL *HttpInstance,\r
1606 IN OUT NET_FRAGMENT *Fragment,\r
1607 IN EFI_EVENT Timeout\r
1608 )\r
1609{\r
1610 EFI_STATUS Status;\r
1611 NET_BUF *Pdu;\r
1612 TLS_RECORD_HEADER RecordHeader;\r
1613 UINT8 *BufferIn;\r
1614 UINTN BufferInSize;\r
1615 NET_FRAGMENT TempFragment;\r
1616 UINT8 *BufferOut;\r
1617 UINTN BufferOutSize;\r
1618 NET_BUF *PacketOut;\r
1619 UINT8 *DataOut;\r
1620 UINT8 *GetSessionDataBuffer;\r
1621 UINTN GetSessionDataBufferSize;\r
1622\r
1623 Status = EFI_SUCCESS;\r
1624 Pdu = NULL;\r
1625 BufferIn = NULL;\r
1626 BufferInSize = 0;\r
1627 BufferOut = NULL;\r
1628 BufferOutSize = 0;\r
1629 PacketOut = NULL;\r
1630 DataOut = NULL;\r
1631 GetSessionDataBuffer = NULL;\r
1632 GetSessionDataBufferSize = 0;\r
1633\r
1634 //\r
1635 // Receive only one TLS record\r
1636 //\r
1637 Status = TlsReceiveOnePdu (HttpInstance, &Pdu, Timeout);\r
1638 if (EFI_ERROR (Status)) {\r
1639 return Status;\r
1640 }\r
1641\r
1642 BufferInSize = Pdu->TotalSize;\r
1643 BufferIn = AllocateZeroPool (BufferInSize);\r
1644 if (BufferIn == NULL) {\r
1645 Status = EFI_OUT_OF_RESOURCES;\r
1646 NetbufFree (Pdu);\r
1647 return Status;\r
1648 }\r
1649\r
1650 NetbufCopy (Pdu, 0, (UINT32) BufferInSize, BufferIn);\r
1651\r
1652 NetbufFree (Pdu);\r
1653\r
1654 //\r
1655 // Handle Receive data.\r
1656 //\r
1657 RecordHeader = *(TLS_RECORD_HEADER *) BufferIn;\r
1658\r
1659 if ((RecordHeader.ContentType == TlsContentTypeApplicationData) &&\r
1660 (RecordHeader.Version.Major == 0x03) &&\r
1661 (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR ||\r
1662 RecordHeader.Version.Minor == TLS11_PROTOCOL_VERSION_MINOR ||\r
1663 RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR)\r
1664 ) {\r
1665 //\r
1666 // Decrypt Packet.\r
1667 //\r
1668 Status = TlsProcessMessage (\r
1669 HttpInstance,\r
1670 BufferIn,\r
1671 BufferInSize,\r
1672 EfiTlsDecrypt,\r
1673 &TempFragment\r
1674 );\r
1675\r
1676 FreePool (BufferIn);\r
1677\r
1678 if (EFI_ERROR (Status)) {\r
1679 if (Status == EFI_ABORTED) {\r
1680 //\r
1681 // Something wrong decryption the message.\r
1682 // BuildResponsePacket() will be called to generate Error Alert message and send it out.\r
1683 //\r
1684 BufferOutSize = DEF_BUF_LEN;\r
1685 BufferOut = AllocateZeroPool (BufferOutSize);\r
1686 if (BufferOut == NULL) {\r
1687 Status = EFI_OUT_OF_RESOURCES;\r
1688 return Status;\r
1689 }\r
1690\r
1691 Status = HttpInstance->Tls->BuildResponsePacket (\r
1692 HttpInstance->Tls,\r
1693 NULL,\r
1694 0,\r
1695 BufferOut,\r
1696 &BufferOutSize\r
1697 );\r
1698 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1699 FreePool (BufferOut);\r
1700 BufferOut = AllocateZeroPool (BufferOutSize);\r
1701 if (BufferOut == NULL) {\r
1702 Status = EFI_OUT_OF_RESOURCES;\r
1703 return Status;\r
1704 }\r
1705\r
1706 Status = HttpInstance->Tls->BuildResponsePacket (\r
1707 HttpInstance->Tls,\r
1708 NULL,\r
1709 0,\r
1710 BufferOut,\r
1711 &BufferOutSize\r
1712 );\r
1713 }\r
1714 if (EFI_ERROR (Status)) {\r
1715 FreePool(BufferOut);\r
1716 return Status;\r
1717 }\r
1718\r
1719 if (BufferOutSize != 0) {\r
1720 PacketOut = NetbufAlloc ((UINT32)BufferOutSize);\r
1721 DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL);\r
1722 if (DataOut == NULL) {\r
1723 FreePool (BufferOut);\r
1724 return EFI_OUT_OF_RESOURCES;\r
1725 }\r
1726\r
1727 CopyMem (DataOut, BufferOut, BufferOutSize);\r
1728\r
1729 Status = TlsCommonTransmit (HttpInstance, PacketOut);\r
1730\r
1731 NetbufFree (PacketOut);\r
1732 }\r
1733\r
1734 FreePool(BufferOut);\r
1735\r
1736 if (EFI_ERROR (Status)) {\r
1737 return Status;\r
1738 }\r
1739\r
1740 return EFI_ABORTED;\r
1741 }\r
1742\r
1743 return Status;\r
1744 }\r
1745\r
1746 //\r
1747 // Parsing buffer.\r
1748 //\r
1749 ASSERT (((TLS_RECORD_HEADER *) (TempFragment.Bulk))->ContentType == TlsContentTypeApplicationData);\r
1750\r
1751 BufferInSize = ((TLS_RECORD_HEADER *) (TempFragment.Bulk))->Length;\r
1752 BufferIn = AllocateZeroPool (BufferInSize);\r
1753 if (BufferIn == NULL) {\r
1754 Status = EFI_OUT_OF_RESOURCES;\r
1755 return Status;\r
1756 }\r
1757\r
07bd82d4 1758 CopyMem (BufferIn, TempFragment.Bulk + TLS_RECORD_HEADER_LENGTH, BufferInSize);\r
7618784b
HW
1759\r
1760 //\r
1761 // Free the buffer in TempFragment.\r
1762 //\r
1763 FreePool (TempFragment.Bulk);\r
1764\r
1765 } else if ((RecordHeader.ContentType == TlsContentTypeAlert) &&\r
1766 (RecordHeader.Version.Major == 0x03) &&\r
1767 (RecordHeader.Version.Minor == TLS10_PROTOCOL_VERSION_MINOR ||\r
1768 RecordHeader.Version.Minor == TLS11_PROTOCOL_VERSION_MINOR ||\r
1769 RecordHeader.Version.Minor == TLS12_PROTOCOL_VERSION_MINOR)\r
1770 ) {\r
1771 BufferOutSize = DEF_BUF_LEN;\r
1772 BufferOut = AllocateZeroPool (BufferOutSize);\r
1773 if (BufferOut == NULL) {\r
1774 FreePool (BufferIn);\r
1775 Status = EFI_OUT_OF_RESOURCES;\r
1776 return Status;\r
1777 }\r
1778\r
1779 Status = HttpInstance->Tls->BuildResponsePacket (\r
1780 HttpInstance->Tls,\r
1781 BufferIn,\r
1782 BufferInSize,\r
1783 BufferOut,\r
1784 &BufferOutSize\r
1785 );\r
1786 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1787 FreePool (BufferOut);\r
1788 BufferOut = AllocateZeroPool (BufferOutSize);\r
1789 if (BufferOut == NULL) {\r
1790 FreePool (BufferIn);\r
1791 Status = EFI_OUT_OF_RESOURCES;\r
1792 return Status;\r
1793 }\r
1794\r
1795 Status = HttpInstance->Tls->BuildResponsePacket (\r
1796 HttpInstance->Tls,\r
1797 BufferIn,\r
1798 BufferInSize,\r
1799 BufferOut,\r
1800 &BufferOutSize\r
1801 );\r
1802 }\r
1803\r
1804 FreePool (BufferIn);\r
1805\r
1806 if (EFI_ERROR (Status)) {\r
1807 FreePool (BufferOut);\r
1808 return Status;\r
1809 }\r
1810\r
1811 if (BufferOutSize != 0) {\r
1812 PacketOut = NetbufAlloc ((UINT32) BufferOutSize);\r
1813 DataOut = NetbufAllocSpace (PacketOut, (UINT32) BufferOutSize, NET_BUF_TAIL);\r
1814 if (DataOut == NULL) {\r
1815 FreePool (BufferOut);\r
1816 return EFI_OUT_OF_RESOURCES;\r
1817 }\r
1818\r
1819 CopyMem (DataOut, BufferOut, BufferOutSize);\r
1820\r
1821 Status = TlsCommonTransmit (HttpInstance, PacketOut);\r
1822\r
1823 NetbufFree (PacketOut);\r
1824 }\r
1825\r
1826 FreePool (BufferOut);\r
1827\r
1828 //\r
1829 // Get the session state.\r
1830 //\r
1831 GetSessionDataBufferSize = DEF_BUF_LEN;\r
1832 GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize);\r
1833 if (GetSessionDataBuffer == NULL) {\r
1834 Status = EFI_OUT_OF_RESOURCES;\r
1835 return Status;\r
1836 }\r
1837\r
1838 Status = HttpInstance->Tls->GetSessionData (\r
1839 HttpInstance->Tls,\r
1840 EfiTlsSessionState,\r
1841 GetSessionDataBuffer,\r
1842 &GetSessionDataBufferSize\r
1843 );\r
1844 if (Status == EFI_BUFFER_TOO_SMALL) {\r
1845 FreePool (GetSessionDataBuffer);\r
1846 GetSessionDataBuffer = AllocateZeroPool (GetSessionDataBufferSize);\r
1847 if (GetSessionDataBuffer == NULL) {\r
1848 Status = EFI_OUT_OF_RESOURCES;\r
1849 return Status;\r
1850 }\r
1851\r
1852 Status = HttpInstance->Tls->GetSessionData (\r
1853 HttpInstance->Tls,\r
1854 EfiTlsSessionState,\r
1855 GetSessionDataBuffer,\r
1856 &GetSessionDataBufferSize\r
1857 );\r
1858 }\r
1859 if (EFI_ERROR (Status)) {\r
1860 FreePool (GetSessionDataBuffer);\r
1861 return Status;\r
1862 }\r
1863\r
1864 ASSERT(GetSessionDataBufferSize == sizeof (EFI_TLS_SESSION_STATE));\r
1865 HttpInstance->TlsSessionState = *(EFI_TLS_SESSION_STATE *) GetSessionDataBuffer;\r
1866\r
1867 FreePool (GetSessionDataBuffer);\r
1868\r
1869 if(HttpInstance->TlsSessionState == EfiTlsSessionError) {\r
1870 DEBUG ((EFI_D_ERROR, "TLS Session State Error!\n"));\r
1871 return EFI_ABORTED;\r
1872 }\r
1873\r
1874 BufferIn = NULL;\r
1875 BufferInSize = 0;\r
1876 }\r
1877\r
1878 Fragment->Bulk = BufferIn;\r
1879 Fragment->Len = (UINT32) BufferInSize;\r
1880\r
1881 return Status;\r
1882}\r
1883\r