This patch is used to refine the code by removing ASSERT and
using error handling in IpSecDxe driver.

Cc: Ye Ting <[email protected]>
Cc: Fu Siyuan <[email protected]>
Cc: Zhang Lubo <[email protected]>
Cc: Yao Jiewen <[email protected]>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jiaxin Wu <[email protected]>
---
 NetworkPkg/IpSecDxe/Ikev2/Info.c      |   6 +-
 NetworkPkg/IpSecDxe/Ikev2/Payload.c   | 203 +++++++++++++++++++++++++++-------
 NetworkPkg/IpSecDxe/Ikev2/Sa.c        |  76 ++++++++++---
 NetworkPkg/IpSecDxe/Ikev2/Utility.c   | 151 ++++++++++++++++++++-----
 NetworkPkg/IpSecDxe/Ikev2/Utility.h   |   7 +-
 NetworkPkg/IpSecDxe/IpSecConfigImpl.c |   5 +-
 NetworkPkg/IpSecDxe/IpSecCryptIo.c    |   7 +-
 NetworkPkg/IpSecDxe/IpSecImpl.c       |  21 ++--
 8 files changed, 379 insertions(+), 97 deletions(-)

diff --git a/NetworkPkg/IpSecDxe/Ikev2/Info.c b/NetworkPkg/IpSecDxe/Ikev2/Info.c
index fe75d6c..23e47ce 100644
--- a/NetworkPkg/IpSecDxe/Ikev2/Info.c
+++ b/NetworkPkg/IpSecDxe/Ikev2/Info.c
@@ -1,10 +1,10 @@
 /** @file
   The Implementations for Information Exchange.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -45,11 +45,13 @@ Ikev2InfoGenerator (
   IKEV2_INFO_EXCHANGE_CONTEXT *InfoContext;
 
   InfoContext  = NULL;
   IkeSaSession = (IKEV2_SA_SESSION *) SaSession;
   IkePacket    = IkePacketAlloc ();
-  ASSERT (IkePacket != NULL);
+  if (IkePacket == NULL) {
+    return NULL;
+  }
 
   //
   // Fill IkePacket Header.
   //
   IkePacket->Header->ExchangeType    = IKEV2_EXCHANGE_TYPE_INFO;
diff --git a/NetworkPkg/IpSecDxe/Ikev2/Payload.c 
b/NetworkPkg/IpSecDxe/Ikev2/Payload.c
index b92ae3a..d5fe1ab 100644
--- a/NetworkPkg/IpSecDxe/Ikev2/Payload.c
+++ b/NetworkPkg/IpSecDxe/Ikev2/Payload.c
@@ -1,10 +1,10 @@
 /** @file
   The implementation of Payloads Creation.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -50,11 +50,14 @@ Ikev2GenerateSaPayload (
   IKE_PAYLOAD   *SaPayload;
   IKEV2_SA_DATA *SaData;
   UINTN         SaDataSize;
 
   SaPayload = IkePayloadAlloc ();
-  ASSERT (SaPayload != NULL);
+  if (SaPayload == NULL) {
+    return NULL;
+  }
+  
   //
   // TODO: Get the Proposal Number and Transform Number from IPsec Config,
   // after the Ipsecconfig Application is support it.
   //
 
@@ -68,11 +71,14 @@ Ikev2GenerateSaPayload (
                  sizeof (IKEV2_TRANSFORM_DATA) * SessionSaData->NumProposals * 
3;
 
   }
 
   SaData = AllocateZeroPool (SaDataSize);
-  ASSERT (SaData != NULL);
+  if (SaData == NULL) {
+    IkePayloadFree (SaPayload);
+    return NULL;
+  }
 
   CopyMem (SaData, SessionSaData, SaDataSize);
   SaData->SaHeader.Header.NextPayload = NextPayload;
   SaPayload->PayloadType              = IKEV2_PAYLOAD_TYPE_SA;
   SaPayload->PayloadBuf               = (UINT8 *) SaData;
@@ -116,18 +122,24 @@ Ikev2GenerateNoncePayload (
   //
   Size        = sizeof (IKEV2_NONCE) + NonceSize;
   NonceBlock  = NonceBuf;
 
   Nonce       = AllocateZeroPool (Size);
-  ASSERT (Nonce != NULL);
+  if (Nonce == NULL) {
+    return NULL;
+  }
+  
   CopyMem (Nonce + 1, NonceBlock, Size - sizeof (IKEV2_NONCE));
 
   Nonce->Header.NextPayload   = NextPayload;
   Nonce->Header.PayloadLength = (UINT16) Size;
   NoncePayload                = IkePayloadAlloc ();
-
-  ASSERT (NoncePayload != NULL);
+  if (NoncePayload == NULL) {
+    FreePool (Nonce);
+    return NULL;
+  }
+  
   NoncePayload->PayloadType = IKEV2_PAYLOAD_TYPE_NONCE;
   NoncePayload->PayloadBuf  = (UINT8 *) Nonce;
   NoncePayload->PayloadSize = Size;
 
   return NoncePayload;
@@ -178,11 +190,13 @@ Ikev2GenerateKePayload (
 
   //
   // Allocate buffer for Key Exchange
   //
   Ke = AllocateZeroPool (KeSize);
-  ASSERT (Ke != NULL);
+  if (Ke == NULL) {
+    return NULL;
+  }
 
   Ke->Header.NextPayload    = NextPayload;
   Ke->Header.PayloadLength  = (UINT16) KeSize;
   Ke->DhGroup               = IkeSaSession->SessionCommon.PreferDhGroup;
 
@@ -190,11 +204,14 @@ Ikev2GenerateKePayload (
 
   //
   // Create IKE_PAYLOAD to point to Key Exchange payload
   //
   KePayload = IkePayloadAlloc ();
-  ASSERT (KePayload != NULL);
+  if (KePayload == NULL) {
+    FreePool (Ke);
+    return NULL;
+  }
 
   KePayload->PayloadType = IKEV2_PAYLOAD_TYPE_KE;
   KePayload->PayloadBuf  = (UINT8 *) Ke;
   KePayload->PayloadSize = KeSize;
   return KePayload;
@@ -239,14 +256,19 @@ Ikev2GenerateIdPayload (
   IpVersion = CommonSession->UdpService->IpVersion;
   AddrSize  = (UINT8) ((IpVersion == IP_VERSION_4) ? sizeof(EFI_IPv4_ADDRESS) 
: sizeof(EFI_IPv6_ADDRESS));
   IdSize    = sizeof (IKEV2_ID) + AddrSize;
 
   Id = (IKEV2_ID *) AllocateZeroPool (IdSize);
-  ASSERT (Id != NULL);
+  if (Id == NULL) {
+    return NULL;
+  }
 
   IdPayload = IkePayloadAlloc ();
-  ASSERT (IdPayload != NULL);
+  if (IdPayload == NULL) {
+    FreePool (Id);
+    return NULL;
+  }
 
   IdPayload->PayloadType  = (UINT8) ((CommonSession->IsInitiator) ? 
IKEV2_PAYLOAD_TYPE_ID_INIT : IKEV2_PAYLOAD_TYPE_ID_RSP);
   IdPayload->PayloadBuf   = (UINT8 *) Id;
   IdPayload->PayloadSize  = IdSize;
 
@@ -315,14 +337,19 @@ Ikev2GenerateCertIdPayload (
   }
 
   IdSize = sizeof (IKEV2_ID) + SubjectSize;
 
   Id = (IKEV2_ID *) AllocateZeroPool (IdSize);
-  ASSERT (Id != NULL);
+  if (Id == NULL) {
+    return NULL;
+  }
 
   IdPayload = IkePayloadAlloc ();
-  ASSERT (IdPayload != NULL);
+  if (IdPayload == NULL) {
+    FreePool (Id);
+    return NULL;
+  }
 
   IdPayload->PayloadType  = (UINT8) ((CommonSession->IsInitiator) ? 
IKEV2_PAYLOAD_TYPE_ID_INIT : IKEV2_PAYLOAD_TYPE_ID_RSP);
   IdPayload->PayloadBuf   = (UINT8 *) Id;
   IdPayload->PayloadSize  = IdSize;
 
@@ -396,17 +423,18 @@ Ikev2PskGenerateAuthPayload (
   AuthPayload = NULL;
   Digest      = NULL;
 
   DigestSize = IpSecGetHmacDigestLength 
((UINT8)IkeSaSession->SessionCommon.SaParams->Prf);
   Digest     = AllocateZeroPool (DigestSize);
-
   if (Digest == NULL) {
     return NULL;
   }
+  
   if (IdPayload == NULL) {
     return NULL;
   }
+  
   //
   // Calcualte Prf(Seceret, "Key Pad for IKEv2");
   //
   Fragments[0].Data     = (UINT8 *) mConstantKey;
   Fragments[0].DataSize = CONSTANT_KEY_SIZE;
@@ -426,11 +454,15 @@ Ikev2PskGenerateAuthPayload (
 
   //
   // Store the AuthKey into KeyBuf
   //
   KeyBuf = AllocateZeroPool (DigestSize);
-  ASSERT (KeyBuf != NULL);
+  if (KeyBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   CopyMem (KeyBuf, Digest, DigestSize);
   KeySize = DigestSize;
 
   //
   // Calculate Prf(SK_Pi/r, IDi/r)
@@ -484,10 +516,15 @@ Ikev2PskGenerateAuthPayload (
 
   //
   // Copy the result of Prf(SK_Pr, IDi/r) to Fragments[2].
   //
   Fragments[2].Data     = AllocateZeroPool (DigestSize);
+  if (Fragments[2].Data == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   Fragments[2].DataSize = DigestSize;
   CopyMem (Fragments[2].Data, Digest, DigestSize);
 
   //
   // Calculate Prf(Key,IKE_SA_INIi/r|Ni/r|Prf(SK_Pr, IDi/r))
@@ -507,15 +544,22 @@ Ikev2PskGenerateAuthPayload (
 
   //
   // Allocate buffer for Auth Payload
   //
   AuthPayload               = IkePayloadAlloc ();
-  ASSERT (AuthPayload != NULL);
+  if (AuthPayload == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
 
   AuthPayload->PayloadSize  = sizeof (IKEV2_AUTH) + DigestSize;
   PayloadBuf                = (IKEV2_AUTH *) AllocateZeroPool 
(AuthPayload->PayloadSize);
-  ASSERT (PayloadBuf != NULL);
+  if (PayloadBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   //
   // Fill in Auth payload.
   //
   PayloadBuf->Header.NextPayload   = NextPayload;
   PayloadBuf->Header.PayloadLength = (UINT16) (AuthPayload->PayloadSize);
@@ -647,21 +691,23 @@ Ikev2CertGenerateAuthPayload (
   if (IdPayload == NULL) {
     return NULL;
   }
   DigestSize = IpSecGetHmacDigestLength 
((UINT8)IkeSaSession->SessionCommon.SaParams->Prf);
   Digest     = AllocateZeroPool (DigestSize);
-
   if (Digest == NULL) {
     return NULL;
   }
 
   //
   // Store the AuthKey into KeyBuf
   //
   KeyBuf  = AllocateZeroPool (DigestSize);
-  ASSERT (KeyBuf != NULL);
-
+  if (KeyBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   CopyMem (KeyBuf, Digest, DigestSize);
 
   //
   // Calculate Prf(SK_Pi/r, IDi/r)
   //
@@ -722,10 +768,15 @@ Ikev2CertGenerateAuthPayload (
 
   //
   // Copy the result of Prf(SK_Pr, IDi/r) to Fragments[2].
   //
   Fragments[2].Data     = AllocateZeroPool (DigestSize);
+  if (Fragments[2].Data == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   Fragments[2].DataSize = DigestSize;
   CopyMem (Fragments[2].Data, Digest, DigestSize);
 
   //
   // Calculate Prf(Key,IKE_SA_INIi/r|Ni/r|Prf(SK_Pr, IDi/r))
@@ -764,20 +815,27 @@ Ikev2CertGenerateAuthPayload (
 
   //
   // Allocate buffer for Auth Payload
   //
   AuthPayload = IkePayloadAlloc ();
-  ASSERT (AuthPayload != NULL);
+  if (AuthPayload == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
 
   if (!IsVerify) {
     AuthPayload->PayloadSize  = sizeof (IKEV2_AUTH) + SigSize;
   } else {
     AuthPayload->PayloadSize  = sizeof (IKEV2_AUTH) + DigestSize;
   }
 
   PayloadBuf = (IKEV2_AUTH *) AllocateZeroPool (AuthPayload->PayloadSize);
-  ASSERT (PayloadBuf != NULL);
+  if (PayloadBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
+  
   //
   // Fill in Auth payload.
   //
   PayloadBuf->Header.NextPayload   = NextPayload;
   PayloadBuf->Header.PayloadLength = (UINT16) (AuthPayload->PayloadSize);
@@ -877,22 +935,26 @@ Ikev2GenerateTsPayload (
   //    !                                                               !
   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   //
 
   TsPayload    = IkePayloadAlloc();
-  ASSERT (TsPayload != NULL);
+  if (TsPayload == NULL) {
+    return NULL;
+  }
 
   IpVersion    = ChildSa->SessionCommon.UdpService->IpVersion;
   //
   // The Starting Address and Ending Address is variable length depends on
   // is IPv4 or IPv6
   //
   AddrSize      = (UINT8)((IpVersion == IP_VERSION_4) ? sizeof 
(EFI_IPv4_ADDRESS) : sizeof (EFI_IPv6_ADDRESS));
   SelectorSize  = sizeof (TRAFFIC_SELECTOR) + 2 * AddrSize;
   TsPayloadSize = sizeof (IKEV2_TS) + SelectorSize;
   TsPayloadBuf = AllocateZeroPool (TsPayloadSize);
-  ASSERT (TsPayloadBuf != NULL);
+  if (TsPayloadBuf == NULL) {
+    goto ON_ERROR;
+  }
 
   TsPayload->PayloadBuf = (UINT8 *) TsPayloadBuf;
   TsSelector            = (TRAFFIC_SELECTOR*)(TsPayloadBuf + 1);
 
   TsSelector->TSType = (UINT8)((IpVersion == IP_VERSION_4) ? 
IKEV2_TS_TYPE_IPV4_ADDR_RANGE : IKEV2_TS_TYPS_IPV6_ADDR_RANGE);
@@ -1144,11 +1206,13 @@ Ikev2GenerateNotifyPayload (
   //  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   //
   //
   NotifyPayloadLen  = (UINT16) (sizeof (IKEV2_NOTIFY) + NotifyDataSize + 
SpiSize);
   Notify            = (IKEV2_NOTIFY *) AllocateZeroPool (NotifyPayloadLen);
-  ASSERT (Notify != NULL);
+  if (Notify == NULL) {
+    return NULL;
+  }
 
   //
   // Set Delete Payload's Generic Header
   //
   Notify->Header.NextPayload    = NextPayload;
@@ -1175,11 +1239,15 @@ Ikev2GenerateNotifyPayload (
 
   //
   // Create Payload for and set type as IKEV2_PAYLOAD_TYPE_NOTIFY
   //
   NotifyPayload = IkePayloadAlloc ();
-  ASSERT (NotifyPayload != NULL);
+  if (NotifyPayload == NULL) {
+    FreePool (Notify);
+    return NULL;
+  }
+  
   NotifyPayload->PayloadType  = IKEV2_PAYLOAD_TYPE_NOTIFY;
   NotifyPayload->PayloadBuf   = (UINT8 *) Notify;
   NotifyPayload->PayloadSize  = NotifyPayloadLen;
   return NotifyPayload;
 }
@@ -1236,11 +1304,13 @@ Ikev2GenerateDeletePayload (
   }
 
   DelPayloadLen = (UINT16) (sizeof (IKEV2_DELETE) + SpiBufSize);
 
   Del           = AllocateZeroPool (DelPayloadLen);
-  ASSERT (Del != NULL);
+  if (Del == NULL) {
+    return NULL;
+  }
 
   //
   // Set Delete Payload's Generic Header
   //
   Del->Header.NextPayload   = NextPayload;
@@ -1260,11 +1330,15 @@ Ikev2GenerateDeletePayload (
   //
   // Set Del Payload's Idntification Data
   //
   CopyMem (Del + 1, SpiBuf, SpiBufSize);
   DelPayload = IkePayloadAlloc ();
-  ASSERT (DelPayload != NULL);
+  if (DelPayload == NULL) {
+    FreePool (Del);
+    return NULL;
+  }
+  
   DelPayload->PayloadType = IKEV2_PAYLOAD_TYPE_DELETE;
   DelPayload->PayloadBuf  = (UINT8 *) Del;
   DelPayload->PayloadSize = DelPayloadLen;
   return DelPayload;
 }
@@ -1624,11 +1698,14 @@ Ikev2EncodeSa (
            TotalTransforms * (sizeof (IKEV2_TRANSFORM) + MAX_SA_ATTRS_SIZE);
   //
   // Allocate buffer for IKE_SA.
   //
   Sa = AllocateZeroPool (SaSize);
-  ASSERT (Sa != NULL);
+  if (Sa == NULL) {
+    return NULL;
+  }
+  
   CopyMem (Sa, SaData, sizeof (IKEV2_SA));
   Sa->Header.PayloadLength  = (UINT16) sizeof (IKEV2_SA);
   ProposalsSize             = 0;
   Proposal                  = (IKEV2_PROPOSAL *) (Sa + 1);
 
@@ -1817,11 +1894,15 @@ Ikev2DecodeSa (
   SaData = (IKEV2_SA_DATA *) AllocateZeroPool (
                                sizeof (IKEV2_SA_DATA) +
                                TotalProposals * sizeof (IKEV2_PROPOSAL_DATA) +
                                TotalTransforms * sizeof (IKEV2_TRANSFORM_DATA)
                                );
-  ASSERT (SaData != NULL);
+  if (SaData == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Exit;
+  }
+  
   CopyMem (SaData, Sa, sizeof (IKEV2_SA));
   SaData->NumProposals        = TotalProposals;
   ProposalData                = (IKEV2_PROPOSAL_DATA *) (SaData + 1);
 
   //
@@ -1850,11 +1931,15 @@ Ikev2DecodeSa (
     } else {
       //
       // SpiSize == 4
       //
       Spi = AllocateZeroPool (Proposal->SpiSize);
-      ASSERT (Spi != NULL);
+      if (Spi == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        goto Exit;
+      }
+      
       CopyMem (Spi, (UINT32 *) (Proposal + 1), Proposal->SpiSize);
       *((UINT32*) Spi) = NTOHL (*((UINT32*) Spi));
       ProposalData->Spi = Spi;
     }
 
@@ -2282,11 +2367,15 @@ Ikev2DecodePacket (
   // IkeSa->InitiPacket or IkeSa->RespPacket for following Auth Payload
   // calculate.
   //
   if (IkePacket->Header->ExchangeType == IKEV2_EXCHANGE_TYPE_INIT) {
     IkeHeader = AllocateZeroPool (sizeof (IKE_HEADER));
-    ASSERT (IkeHeader != NULL);
+    if (IkeHeader == NULL) {
+      Status = EFI_OUT_OF_RESOURCES;
+      goto Exit;
+    }
+    
     CopyMem (IkeHeader, IkePacket->Header, sizeof (IKE_HEADER));
 
     //
     // Before store the whole packet, roll back the host order to network 
order,
     // since the header order was changed in the IkePacketFromNetbuf.
@@ -2356,11 +2445,14 @@ Ikev2DecodePacket (
 
     //
     // Initial IkePayload
     //
     IkePayload = IkePayloadAlloc ();
-    ASSERT (IkePayload != NULL);
+    if (IkePayload == NULL) {
+      Status = EFI_OUT_OF_RESOURCES;
+      goto Exit;
+    }
 
     IkePayload->PayloadType     = PayloadType;
     IkePayload->PayloadBuf      = (UINT8 *) PayloadHdr;
     IkePayload->PayloadSize     = PayloadSize;
     IkePayload->IsPayloadBufExt = TRUE;
@@ -2481,11 +2573,14 @@ Ikev2EncodePacket (
   if (IkePacket->Header->ExchangeType == IKEV2_EXCHANGE_TYPE_INIT) {
     IkeSaSession =  IKEV2_SA_SESSION_FROM_COMMON (SessionCommon);
     if (SessionCommon->IsInitiator) {
       IkeSaSession->InitPacketSize = IkePacket->PayloadTotalSize + sizeof 
(IKE_HEADER);
       IkeSaSession->InitPacket     = AllocateZeroPool 
(IkeSaSession->InitPacketSize);
-      ASSERT (IkeSaSession->InitPacket != NULL);
+      if (IkeSaSession->InitPacket == NULL) {
+        return EFI_OUT_OF_RESOURCES;
+      }
+      
       CopyMem (IkeSaSession->InitPacket, IkePacket->Header, sizeof 
(IKE_HEADER));
       PayloadTotalSize = 0;
       for (Entry = IkePacket->PayloadList.ForwardLink; Entry != 
&(IkePacket->PayloadList);) {
         IkePayload  = IKE_PAYLOAD_BY_PACKET (Entry);
         Entry       = Entry->ForwardLink;
@@ -2497,11 +2592,14 @@ Ikev2EncodePacket (
         PayloadTotalSize = PayloadTotalSize + IkePayload->PayloadSize;
       }
     } else {
       IkeSaSession->RespPacketSize = IkePacket->PayloadTotalSize + 
sizeof(IKE_HEADER);
       IkeSaSession->RespPacket     = AllocateZeroPool 
(IkeSaSession->RespPacketSize);
-      ASSERT (IkeSaSession->RespPacket != NULL);
+      if (IkeSaSession->RespPacket == NULL) {
+        return EFI_OUT_OF_RESOURCES;
+      }
+      
       CopyMem (IkeSaSession->RespPacket, IkePacket->Header, sizeof 
(IKE_HEADER));
       PayloadTotalSize = 0;
       for (Entry = IkePacket->PayloadList.ForwardLink; Entry != 
&(IkePacket->PayloadList);) {
         IkePayload  = IKE_PAYLOAD_BY_PACKET (Entry);
         Entry       = Entry->ForwardLink;
@@ -2594,18 +2692,25 @@ Ikev2DecryptPacket (
     //
     return EFI_INVALID_PARAMETER;
   }
 
   CheckSumData = AllocateZeroPool (CheckSumSize);
-  ASSERT (CheckSumData != NULL);
+  if (CheckSumData == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
 
   //
   // Fill in the Integrity buffer
   //
   IntegritySize   = IkePacket->PayloadTotalSize + sizeof (IKE_HEADER);
   IntegrityBuffer = AllocateZeroPool (IntegritySize);
-  ASSERT (IntegrityBuffer != NULL);
+  if (IntegrityBuffer == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
+  
   CopyMem (IntegrityBuffer, IkePacket->Header, sizeof(IKE_HEADER));
   CopyMem (IntegrityBuffer + sizeof (IKE_HEADER), IkePacket->PayloadsBuf, 
IkePacket->PayloadTotalSize);
 
   //
   // Change Host order to Network order, since the header order was changed
@@ -2662,11 +2767,14 @@ Ikev2DecryptPacket (
   //
   // Decrypt the payload with the key.
   //
   DecryptedSize = IkePacket->PayloadTotalSize - sizeof 
(IKEV2_COMMON_PAYLOAD_HEADER) - IvSize - CheckSumSize;
   DecryptedBuf  = AllocateZeroPool (DecryptedSize);
-  ASSERT (DecryptedBuf != NULL);
+  if (DecryptedBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
 
   CopyMem (
     DecryptedBuf,
     IkePacket->PayloadsBuf + sizeof (IKEV2_COMMON_PAYLOAD_HEADER) + IvSize,
     DecryptedSize
@@ -2809,12 +2917,15 @@ Ikev2EncryptPacket (
   // Calcualte the EncryptPayloadSize and the PAD length
   //
   CryptBlockSizeMask  = (UINT8) (CryptBlockSize - 1);
   EncryptedSize       = (IkePacket->PayloadTotalSize + sizeof (IKEV2_PAD_LEN) 
+ CryptBlockSizeMask) & ~CryptBlockSizeMask;
   EncryptedBuf        = (UINT8 *) AllocateZeroPool (EncryptedSize);
-  ASSERT (EncryptedBuf != NULL);
-
+  if (EncryptedBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
+  
   //
   // Copy all payload into EncryptedIkePayload
   //
   Index = 0;
   NET_LIST_FOR_EACH (Entry, &(IkePacket)->PayloadList) {
@@ -2876,11 +2987,14 @@ Ikev2EncryptPacket (
   //
   // Allocate the buffer for the whole IKE payload (Encrypted Payload).
   //
   EncryptPayloadSize = sizeof(IKEV2_ENCRYPTED) + IvSize + EncryptedSize + 
CheckSumSize;
   EncryptPayloadBuf  = AllocateZeroPool (EncryptPayloadSize);
-  ASSERT (EncryptPayloadBuf != NULL);
+  if (EncryptPayloadBuf == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
 
   //
   // Fill in Header of  Encrypted Payload
   //
   ((IKEV2_ENCRYPTED *) EncryptPayloadBuf)->Header.NextPayload   = 
IkePacket->Header->NextPayload;
@@ -2963,11 +3077,14 @@ Ikev2EncryptPacket (
 
   //
   // Create Encrypted Payload and add into IkePacket->PayloadList
   //
   EncryptPayload = IkePayloadAlloc ();
-  ASSERT (EncryptPayload != NULL);
+  if (EncryptPayload == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
 
   //
   // Fill the encrypted payload into the IKE_PAYLOAD structure.
   //
   EncryptPayload->PayloadBuf  = EncryptPayloadBuf;
@@ -3209,11 +3326,13 @@ Ikev2SendIkePacket (
 
   //
   // Transform IkePacke to NetBuf
   //
   IkePacketNetbuf = IkeNetbufFromPacket ((UINT8 *) SessionCommon, IkePacket, 
IkeType);
-  ASSERT (IkePacketNetbuf != NULL);
+  if (IkePacketNetbuf == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
 
   ZeroMem (&EndPoint, sizeof (UDP_END_POINT));
   EndPoint.RemotePort = IKE_DEFAULT_PORT;
   CopyMem (&IkePacket->RemotePeerIp, &Common->RemotePeerIp, sizeof 
(EFI_IP_ADDRESS));
   CopyMem (&EndPoint.RemoteAddr, &Common->RemotePeerIp, sizeof 
(EFI_IP_ADDRESS));
diff --git a/NetworkPkg/IpSecDxe/Ikev2/Sa.c b/NetworkPkg/IpSecDxe/Ikev2/Sa.c
index 9967e1a..c83d456 100644
--- a/NetworkPkg/IpSecDxe/Ikev2/Sa.c
+++ b/NetworkPkg/IpSecDxe/Ikev2/Sa.c
@@ -1,10 +1,10 @@
 /** @file
   The operations for IKEv2 SA.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -100,11 +100,13 @@ Ikev2InitPskGenerator (
 
   //
   // 1. Allocate IKE packet
   //
   IkePacket = IkePacketAlloc ();
-  ASSERT (IkePacket != NULL);
+  if (IkePacket == NULL) {
+    goto CheckError;
+  }
 
   //
   // 1.a Fill the IkePacket->Hdr
   //
   IkePacket->Header->ExchangeType    = IKEV2_EXCHANGE_TYPE_INIT;
@@ -174,11 +176,13 @@ Ikev2InitPskGenerator (
   //    the Nonce Payload.
   //
   if ((IkeSaSession->SessionCommon.IsInitiator) && (IkeSaSession->NCookie == 
NULL)) {
     IkeSaSession->NiBlkSize = IKE_NONCE_SIZE;
     IkeSaSession->NiBlock   = IkeGenerateNonce (IKE_NONCE_SIZE);
-    ASSERT (IkeSaSession->NiBlock != NULL);
+    if (IkeSaSession->NiBlock == NULL) {
+      goto CheckError;
+    }
   }
 
   if (IkeSaSession->SessionCommon.IsInitiator) {
     NoncePayload = Ikev2GenerateNoncePayload (
                      IkeSaSession->NiBlock,
@@ -296,11 +300,15 @@ Ikev2InitPskParser (
   //
   // Store NoncePayload for SKEYID computing.
   //
   NonceSize   = NoncePayload->PayloadSize - sizeof 
(IKEV2_COMMON_PAYLOAD_HEADER);
   NonceBuffer = (UINT8 *) AllocatePool (NonceSize);
-  ASSERT (NonceBuffer != NULL);
+  if (NonceBuffer == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto CheckError;
+  }
+  
   CopyMem (
     NonceBuffer,
     NoncePayload->PayloadBuf + sizeof (IKEV2_COMMON_PAYLOAD_HEADER),
     NonceSize
     );
@@ -442,11 +450,13 @@ Ikev2AuthPskGenerator (
   
   //
   // 1. Allocate IKE Packet
   //
   IkePacket= IkePacketAlloc ();
-  ASSERT (IkePacket != NULL);
+  if (IkePacket == NULL) {
+    return NULL;
+  }
 
   //
   // 1.a Fill the IkePacket Header.
   //
   IkePacket->Header->ExchangeType    = IKEV2_EXCHANGE_TYPE_AUTH;
@@ -743,11 +753,14 @@ Ikev2AuthPskParser (
     //
     // Associate the IkeSaSession's SPD to the first ChildSaSession's SPD.
     //
     if (ChildSaSession->IkeSaSession->Spd == NULL) {
       ChildSaSession->IkeSaSession->Spd = ChildSaSession->Spd;
-      Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession);
+      Status = Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession);
+      if (EFI_ERROR (Status)) {
+        return Status;
+      }
     }
   } else {
     //
     //TODO:check the Port range.
     //
@@ -928,11 +941,13 @@ Ikev2AuthCertGenerator (
 
   //
   // 1. Allocate IKE Packet
   //
   IkePacket= IkePacketAlloc ();
-  ASSERT (IkePacket != NULL);
+  if (IkePacket == NULL) {
+    return NULL;
+  }
 
   //
   // 1.a Fill the IkePacket Header.
   //
   IkePacket->Header->ExchangeType    = IKEV2_EXCHANGE_TYPE_AUTH;
@@ -1278,11 +1293,14 @@ Ikev2AuthCertParser (
     //
     // Associate the IkeSaSession's SPD to the first ChildSaSession's SPD.
     //
     if (ChildSaSession->IkeSaSession->Spd == NULL) {
       ChildSaSession->IkeSaSession->Spd = ChildSaSession->Spd;
-      Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession);
+      Status = Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession);
+      if (EFI_ERROR (Status)) {
+        goto Exit;
+      }
     }
   } else {
     //
     // Todo:check the Port range.
     //
@@ -1358,21 +1376,31 @@ Ikev2GenerateSaDhPublicKey (
 {
   EFI_STATUS         Status;
   IKEV2_SESSION_KEYS *IkeKeys;
 
   IkeSaSession->IkeKeys = AllocateZeroPool (sizeof (IKEV2_SESSION_KEYS));
-  ASSERT (IkeSaSession->IkeKeys != NULL);
+  if (IkeSaSession->IkeKeys == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+  
   IkeKeys = IkeSaSession->IkeKeys;
   IkeKeys->DhBuffer = AllocateZeroPool (sizeof (IKEV2_DH_BUFFER));
-  ASSERT (IkeKeys->DhBuffer != NULL);
+  if (IkeKeys->DhBuffer == NULL) {
+    FreePool (IkeSaSession->IkeKeys);
+    return EFI_OUT_OF_RESOURCES;
+  }
 
   //
   // Init DH with the certain DH Group Description.
   //
   IkeKeys->DhBuffer->GxSize   = 
OakleyModpGroup[(UINT8)IkeSaSession->SessionCommon.PreferDhGroup].Size >> 3;
   IkeKeys->DhBuffer->GxBuffer = AllocateZeroPool (IkeKeys->DhBuffer->GxSize);
-  ASSERT (IkeKeys->DhBuffer->GxBuffer != NULL);
+  if (IkeKeys->DhBuffer->GxBuffer == NULL) {
+    FreePool (IkeKeys->DhBuffer);
+    FreePool (IkeSaSession->IkeKeys);
+    return EFI_OUT_OF_RESOURCES;
+  }
 
   //
   // Get X PublicKey
   //
   Status = IpSecCryptoIoDhGetPublicKey (
@@ -1383,10 +1411,17 @@ Ikev2GenerateSaDhPublicKey (
              IkeKeys->DhBuffer->GxBuffer,
              &IkeKeys->DhBuffer->GxSize
              );
   if (EFI_ERROR (Status)) {
     DEBUG ((DEBUG_ERROR, "Error CPLKeyManGetKeyParam X public key error Status 
= %r\n", Status));
+    
+    FreePool (IkeKeys->DhBuffer->GxBuffer);
+    
+    FreePool (IkeKeys->DhBuffer);
+    
+    FreePool (IkeSaSession->IkeKeys);
+    
     return Status;
   }
 
   IPSEC_DUMP_BUF ("DH Public Key (g^x) Dump", IkeKeys->DhBuffer->GxBuffer, 
IkeKeys->DhBuffer->GxSize);
 
@@ -1420,11 +1455,13 @@ Ikev2GenerateSaDhComputeKey (
   Ke                  = (IKEV2_KEY_EXCHANGE *) KePayload->PayloadBuf;
   PubKey              = (UINT8 *) (Ke + 1);
   PubKeySize          = KePayload->PayloadSize - sizeof (IKEV2_KEY_EXCHANGE);
   DhBuffer->GxySize   = DhBuffer->GxSize;
   DhBuffer->GxyBuffer = AllocateZeroPool (DhBuffer->GxySize);
-  ASSERT (DhBuffer->GxyBuffer != NULL);
+  if (DhBuffer->GxyBuffer == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
 
   //
   // Get GxyBuf
   //
   Status = IpSecCryptoIoDhComputeKey (
@@ -1434,19 +1471,27 @@ Ikev2GenerateSaDhComputeKey (
              DhBuffer->GxyBuffer,
              &DhBuffer->GxySize
              );
   if (EFI_ERROR (Status)) {
     DEBUG ((DEBUG_ERROR, "Error CPLKeyManGetKeyParam Y session key error 
Status = %r\n", Status));
+
+    FreePool (DhBuffer->GxyBuffer);
+    
     return Status;
   }
 
   //
   // Create GxyBuf.
   //
   DhBuffer->GySize   = PubKeySize;
   DhBuffer->GyBuffer = AllocateZeroPool (DhBuffer->GySize);
-  ASSERT (DhBuffer->GyBuffer != NULL);
+  if (DhBuffer->GyBuffer == NULL) {
+    FreePool (DhBuffer->GxyBuffer);
+    
+    return Status;
+  }
+  
   CopyMem (DhBuffer->GyBuffer, PubKey, DhBuffer->GySize);
 
   IPSEC_DUMP_BUF ("DH Public Key (g^y) Dump", DhBuffer->GyBuffer, 
DhBuffer->GySize);
   IPSEC_DUMP_BUF ("DH Shared Key (g^xy) Dump", DhBuffer->GxyBuffer, 
DhBuffer->GxySize);
 
@@ -1522,11 +1567,14 @@ Ikev2GenerateSaKeys (
   //
   // Compute SKEYSEED = prf(Ni | Nr, g^ir)
   //
   KeyBufferSize = IkeSaSession->NiBlkSize + IkeSaSession->NrBlkSize;
   KeyBuffer     = AllocateZeroPool (KeyBufferSize);
-  ASSERT (KeyBuffer != NULL);
+  if (KeyBuffer == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Exit;
+  }
 
   CopyMem (KeyBuffer, IkeSaSession->NiBlock, IkeSaSession->NiBlkSize);
   CopyMem (KeyBuffer + IkeSaSession->NiBlkSize, IkeSaSession->NrBlock, 
IkeSaSession->NrBlkSize);
 
   Fragments[0].Data     = IkeSaSession->IkeKeys->DhBuffer->GxyBuffer;
diff --git a/NetworkPkg/IpSecDxe/Ikev2/Utility.c 
b/NetworkPkg/IpSecDxe/Ikev2/Utility.c
index aa3e440..5b26ba1 100644
--- a/NetworkPkg/IpSecDxe/Ikev2/Utility.c
+++ b/NetworkPkg/IpSecDxe/Ikev2/Utility.c
@@ -1,10 +1,10 @@
 /** @file
   The Common operations used by IKE Exchange Process.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -55,11 +55,13 @@ Ikev2SaSessionAlloc (
   EFI_STATUS            Status;
   IKEV2_SESSION_COMMON  *SessionCommon;
   IKEV2_SA_SESSION      *IkeSaSession;
 
   IkeSaSession = AllocateZeroPool (sizeof (IKEV2_SA_SESSION));
-  ASSERT (IkeSaSession != NULL);
+  if (IkeSaSession == NULL) {
+    return NULL;
+  }
 
   //
   // Initialize the fields of IkeSaSession and its SessionCommon.
   //
   IkeSaSession->NCookie              = NULL;
@@ -906,13 +908,13 @@ Ikev2ChildSaSilentDelete (
   LocalSpi  = ChildSession->LocalPeerSpi;
   RemoteSpi = ChildSession->RemotePeerSpi;
   
   SelectorSize  = sizeof (EFI_IPSEC_CONFIG_SELECTOR);
   Selector      = AllocateZeroPool (SelectorSize);
-  ASSERT (Selector != NULL);
-
-  
+  if (Selector == NULL) {
+    return EFI_OUT_OF_RESOURCES;
+  }
 
   while (1) {
     Status = EfiIpSecConfigGetNextSelector (
                &Private->IpSecConfig,
                IPsecConfigDataTypeSad,
@@ -921,11 +923,15 @@ Ikev2ChildSaSilentDelete (
                );
     if (Status == EFI_BUFFER_TOO_SMALL) {
       FreePool (Selector);
 
       Selector = AllocateZeroPool (SelectorSize);
-      ASSERT (Selector != NULL);
+      if (Selector == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        break;
+      }
+      
       Status   = EfiIpSecConfigGetNextSelector (
                    &Private->IpSecConfig,
                    IPsecConfigDataTypeSad,
                    &SelectorSize,
                    Selector
@@ -941,22 +947,30 @@ Ikev2ChildSaSilentDelete (
       // SPI is unique. There is only one SAD whose SPI is
       // same with RemoteSpi.
       //
       IsRemoteFound   = TRUE;
       RemoteSelector  = AllocateZeroPool (SelectorSize);
-      ASSERT (RemoteSelector != NULL);
+      if (RemoteSelector == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        break;
+      }
+      
       CopyMem (RemoteSelector, Selector, SelectorSize);
     }
 
     if (Selector->SaId.Spi == LocalSpi) {
       //
       // SPI is unique. There is only one SAD whose SPI is
       // same with LocalSpi.
       //
       IsLocalFound  = TRUE;
       LocalSelector = AllocateZeroPool (SelectorSize);
-      ASSERT (LocalSelector != NULL);
+      if (LocalSelector == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        break;
+      }
+      
       CopyMem (LocalSelector, Selector, SelectorSize);
     }
   }
   //
   // Delete SA from the Variable.
@@ -1268,11 +1282,15 @@ Ikev2InitializeSaData (
     ProposalData->ProtocolId    = IPSEC_PROTO_ISAKMP;
   } else {
     ChildSaSession              = IKEV2_CHILD_SA_SESSION_FROM_COMMON 
(SessionCommon);
     ProposalData->ProtocolId    = IPSEC_PROTO_IPSEC_ESP;
     ProposalData->Spi           = AllocateZeroPool (sizeof 
(ChildSaSession->LocalPeerSpi));
-    ASSERT (ProposalData->Spi != NULL);
+    if (ProposalData->Spi == NULL) {
+      FreePool (SaData);
+      return NULL;
+    }
+    
     CopyMem (
       ProposalData->Spi,
       &ChildSaSession->LocalPeerSpi,
       sizeof(ChildSaSession->LocalPeerSpi)
     );
@@ -1336,11 +1354,16 @@ Ikev2InitializeSaData (
 
     ChildSaSession              = IKEV2_CHILD_SA_SESSION_FROM_COMMON 
(SessionCommon);
     ProposalData->ProtocolId    = IPSEC_PROTO_IPSEC_ESP;
     ProposalData->NumTransforms = 3;
     ProposalData->Spi           = AllocateZeroPool (sizeof 
(ChildSaSession->LocalPeerSpi));
-    ASSERT (ProposalData->Spi != NULL);
+    if (ProposalData->Spi == NULL) {
+      FreePool (((IKEV2_PROPOSAL_DATA *) (SaData + 1))->Spi);
+      FreePool (SaData);
+      return NULL;
+    }
+    
     CopyMem (
       ProposalData->Spi,
       &ChildSaSession->LocalPeerSpi,
       sizeof(ChildSaSession->LocalPeerSpi)
     );
@@ -1729,21 +1752,31 @@ Ikev2ResendNotify (
   ChildSaSession->SpdSelector stores the real Spdselector for its SA. Sometime,
   the SpdSelector in ChildSaSession is more accurated or the scope is smaller 
   than the one in ChildSaSession->Spd, especially for the tunnel mode.
     
   @param[in, out]  ChildSaSession  Pointer to IKEV2_CHILD_SA_SESSION related 
to.
+
+  @retval EFI_SUCCESS            The operation complete successfully.
+  @retval EFI_OUT_OF_RESOURCES   If the required resource can't be allocated.
   
 **/
-VOID
+EFI_STATUS
 Ikev2ChildSaSessionSpdSelectorCreate (
   IN OUT IKEV2_CHILD_SA_SESSION *ChildSaSession
   ) 
 {
+  EFI_STATUS          Status;
+
+  Status = EFI_SUCCESS;
+
   if (ChildSaSession->Spd != NULL && ChildSaSession->Spd->Selector != NULL) {
     if (ChildSaSession->SpdSelector == NULL) {
       ChildSaSession->SpdSelector = AllocateZeroPool (sizeof 
(EFI_IPSEC_SPD_SELECTOR));
-      ASSERT (ChildSaSession->SpdSelector != NULL);
+      if (ChildSaSession->SpdSelector == NULL) {
+        Status = EFI_OUT_OF_RESOURCES;
+        return Status;
+      }
     }
     CopyMem (
       ChildSaSession->SpdSelector, 
       ChildSaSession->Spd->Selector, 
       sizeof (EFI_IPSEC_SPD_SELECTOR)
@@ -1751,22 +1784,38 @@ Ikev2ChildSaSessionSpdSelectorCreate (
     ChildSaSession->SpdSelector->RemoteAddress = AllocateCopyPool (
                                                    
ChildSaSession->Spd->Selector->RemoteAddressCount * 
                                                    sizeof 
(EFI_IP_ADDRESS_INFO), 
                                                    
ChildSaSession->Spd->Selector->RemoteAddress
                                                    );
+    if (ChildSaSession->SpdSelector->RemoteAddress == NULL) {
+      Status = EFI_OUT_OF_RESOURCES;
+
+      FreePool (ChildSaSession->SpdSelector);
+      
+      return Status;
+    }
+    
     ChildSaSession->SpdSelector->LocalAddress = AllocateCopyPool (
                                                   
ChildSaSession->Spd->Selector->LocalAddressCount * 
                                                   sizeof 
(EFI_IP_ADDRESS_INFO), 
                                                   
ChildSaSession->Spd->Selector->LocalAddress
                                                   );
+    if (ChildSaSession->SpdSelector->LocalAddress == NULL) {
+      Status = EFI_OUT_OF_RESOURCES;
+
+      FreePool (ChildSaSession->SpdSelector->RemoteAddress);
 
-    ASSERT (ChildSaSession->SpdSelector->LocalAddress != NULL);
-    ASSERT (ChildSaSession->SpdSelector->RemoteAddress != NULL);
+      FreePool (ChildSaSession->SpdSelector);
+      
+      return Status;
+    }
 
     ChildSaSession->SpdSelector->RemoteAddressCount = 
ChildSaSession->Spd->Selector->RemoteAddressCount;
     ChildSaSession->SpdSelector->LocalAddressCount = 
ChildSaSession->Spd->Selector->LocalAddressCount; 
   }
+
+  return Status;
 }
 
 /**
   Generate a ChildSa Session and insert it into related IkeSaSession.
 
@@ -1787,11 +1836,13 @@ Ikev2ChildSaSessionCreate (
 
   //
   // Create a new ChildSaSession.Insert it into processing list and initiate 
the common parameters.
   //
   ChildSaSession = Ikev2ChildSaSessionAlloc (UdpService, IkeSaSession);
-  ASSERT (ChildSaSession != NULL);
+  if (ChildSaSession == NULL) {
+    return NULL;
+  }
 
   //
   // Set the specific parameters.
   // 
   ChildSaSession->Spd        = IkeSaSession->Spd;
@@ -1808,22 +1859,33 @@ Ikev2ChildSaSessionCreate (
   //
   // If SPD->Selector is not NULL, copy it to the ChildSaSession->SpdSelector.
   // The ChildSaSession->SpdSelector might be changed after the traffic 
selector
   // negoniation and it will be copied into the SAData after ChildSA 
established.
   //
-  Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession);
+  if (EFI_ERROR (Ikev2ChildSaSessionSpdSelectorCreate (ChildSaSession))) {
+    Ikev2ChildSaSessionFree (ChildSaSession);
+    return NULL;
+  }
 
   //
   // Copy first NiBlock and NrBlock to ChildSa Session
   //
   ChildSaSession->NiBlock   = AllocateZeroPool (IkeSaSession->NiBlkSize);
-  ASSERT (ChildSaSession->NiBlock != NULL);
+  if (ChildSaSession->NiBlock == NULL) {
+    Ikev2ChildSaSessionFree (ChildSaSession);
+    return NULL;
+  }
+  
   ChildSaSession->NiBlkSize = IkeSaSession->NiBlkSize;
   CopyMem (ChildSaSession->NiBlock, IkeSaSession->NiBlock, 
IkeSaSession->NiBlkSize);
 
   ChildSaSession->NrBlock   = AllocateZeroPool (IkeSaSession->NrBlkSize);
-  ASSERT (ChildSaSession->NrBlock != NULL);
+  if (ChildSaSession->NrBlock == NULL) {
+    Ikev2ChildSaSessionFree (ChildSaSession);
+    return NULL;
+  }
+  
   ChildSaSession->NrBlkSize = IkeSaSession->NrBlkSize;
   CopyMem (ChildSaSession->NrBlock, IkeSaSession->NrBlock, 
IkeSaSession->NrBlkSize);
 
   //
   //  Only if the Create Child SA is called for the IKE_INIT Exchange and 
@@ -2192,11 +2254,14 @@ Ikev2SaParseSaPayload (
               ) {
             //
             // Find the matched one. 
             //
             IkeSaSession->SessionCommon.SaParams = AllocateZeroPool (sizeof 
(IKEV2_SA_PARAMS));
-            ASSERT (IkeSaSession->SessionCommon.SaParams != NULL);
+            if (IkeSaSession->SessionCommon.SaParams == NULL) {
+              return FALSE;
+            }
+            
             IkeSaSession->SessionCommon.SaParams->EncAlgId   = 
PreferEncryptAlgorithm;
             IkeSaSession->SessionCommon.SaParams->EnckeyLen  = 
PreferEncryptKeylength;
             IkeSaSession->SessionCommon.SaParams->DhGroup    = PreferDhGroup;
             IkeSaSession->SessionCommon.SaParams->Prf        = 
PreferPrfAlgorithm;
             IkeSaSession->SessionCommon.SaParams->IntegAlgId = 
PreferIntegrityAlgorithm;
@@ -2207,11 +2272,14 @@ Ikev2SaParseSaPayload (
             //
             SaDataSize           = sizeof (IKEV2_SA_DATA) +
                                    sizeof (IKEV2_PROPOSAL_DATA) +
                                    sizeof (IKEV2_TRANSFORM_DATA) * 4;
             IkeSaSession->SaData = AllocateZeroPool (SaDataSize);
-            ASSERT (IkeSaSession->SaData != NULL);
+            if (IkeSaSession->SaData == NULL) {
+              FreePool (IkeSaSession->SessionCommon.SaParams);
+              return FALSE;
+            }
 
             IkeSaSession->SaData->NumProposals  = 1;
 
             //
             // BUGBUG: Suppose the matched proposal only has 4 transforms. If
@@ -2223,10 +2291,11 @@ Ikev2SaParseSaPayload (
                ProposalData, 
                SaDataSize - sizeof (IKEV2_SA_DATA)
               );
 
             ((IKEV2_PROPOSAL_DATA *) (IkeSaSession->SaData + 
1))->ProposalIndex = 1;
+            
             return TRUE;
           } else {
             PreferEncryptAlgorithm   = 0;
             PreferIntegrityAlgorithm = 0;
             PreferPrfAlgorithm       = 0;
@@ -2298,21 +2367,25 @@ Ikev2SaParseSaPayload (
                      ProposalData->NumTransforms * sizeof 
(IKEV2_TRANSFORM_DATA));    
     }
 
     if (IsMatch) {
         IkeSaSession->SessionCommon.SaParams = AllocateZeroPool (sizeof 
(IKEV2_SA_PARAMS));
-        ASSERT (IkeSaSession->SessionCommon.SaParams != NULL);
+        if (IkeSaSession->SessionCommon.SaParams == NULL) {
+          return FALSE;
+        }
+        
         IkeSaSession->SessionCommon.SaParams->EncAlgId   = 
PreferEncryptAlgorithm;
         IkeSaSession->SessionCommon.SaParams->EnckeyLen  = 
PreferEncryptKeylength;
         IkeSaSession->SessionCommon.SaParams->DhGroup    = PreferDhGroup;
         IkeSaSession->SessionCommon.SaParams->Prf        = PreferPrfAlgorithm;
         IkeSaSession->SessionCommon.SaParams->IntegAlgId = 
PreferIntegrityAlgorithm;
         IkeSaSession->SessionCommon.PreferDhGroup        = PreferDhGroup;
       
         return TRUE;
     }
   }
+  
   return FALSE;
 }
 
 /**
   Parse the received Authentication Exchange Packet.
@@ -2389,11 +2462,14 @@ Ikev2ChildSaParseSaPayload (
             ) {
           //
           // Find the matched one. 
           //
           ChildSaSession->SessionCommon.SaParams = AllocateZeroPool (sizeof 
(IKEV2_SA_PARAMS));
-          ASSERT (ChildSaSession->SessionCommon.SaParams != NULL);
+          if (ChildSaSession->SessionCommon.SaParams == NULL) {
+            return FALSE;
+          }
+          
           ChildSaSession->SessionCommon.SaParams->EncAlgId   = 
PreferEncryptAlgorithm;
           ChildSaSession->SessionCommon.SaParams->EnckeyLen  = 
PreferEncryptKeylength;
           ChildSaSession->SessionCommon.SaParams->IntegAlgId = 
PreferIntegrityAlgorithm;
           CopyMem (&ChildSaSession->RemotePeerSpi, ProposalData->Spi, sizeof 
(ChildSaSession->RemotePeerSpi));
 
@@ -2403,11 +2479,14 @@ Ikev2ChildSaParseSaPayload (
           SaDataSize           = sizeof (IKEV2_SA_DATA) +
                                  sizeof (IKEV2_PROPOSAL_DATA) +
                                  sizeof (IKEV2_TRANSFORM_DATA) * 4;
 
           ChildSaSession->SaData = AllocateZeroPool (SaDataSize);
-          ASSERT (ChildSaSession->SaData != NULL);
+          if (ChildSaSession->SaData == NULL) {
+            FreePool (ChildSaSession->SessionCommon.SaParams);
+            return FALSE;
+          }
 
           ChildSaSession->SaData->NumProposals  = 1;
 
           //
           // BUGBUG: Suppose there are 4 transforms in the matched proposal. If
@@ -2424,11 +2503,18 @@ Ikev2ChildSaParseSaPayload (
 
           ((IKEV2_PROPOSAL_DATA *) (ChildSaSession->SaData + 1))->Spi = 
AllocateCopyPool (
                                                                           
sizeof (ChildSaSession->LocalPeerSpi), 
                                                                           
&ChildSaSession->LocalPeerSpi
                                                                           );
-          ASSERT (((IKEV2_PROPOSAL_DATA *) (ChildSaSession->SaData + 1))->Spi 
!= NULL);
+          if (((IKEV2_PROPOSAL_DATA *) (ChildSaSession->SaData + 1))->Spi == 
NULL) {
+            FreePool (ChildSaSession->SessionCommon.SaParams);
+
+            FreePool (ChildSaSession->SaData );
+            
+            return FALSE;
+          }
+          
           return TRUE;
 
         } else {
           PreferEncryptAlgorithm   = 0;
           PreferIntegrityAlgorithm = 0;
@@ -2494,11 +2580,14 @@ Ikev2ChildSaParseSaPayload (
     }
   
     ProposalData  = (IKEV2_PROPOSAL_DATA *)((IKEV2_SA_DATA 
*)SaPayload->PayloadBuf + 1);
     if (IsMatch) {
         ChildSaSession->SessionCommon.SaParams = AllocateZeroPool (sizeof 
(IKEV2_SA_PARAMS));
-        ASSERT (ChildSaSession->SessionCommon.SaParams != NULL);
+        if (ChildSaSession->SessionCommon.SaParams == NULL) {
+          return FALSE;
+        }
+        
         ChildSaSession->SessionCommon.SaParams->EncAlgId   = 
PreferEncryptAlgorithm;
         ChildSaSession->SessionCommon.SaParams->EnckeyLen  = 
PreferEncryptKeylength;
         ChildSaSession->SessionCommon.SaParams->IntegAlgId = 
PreferIntegrityAlgorithm;
         CopyMem (&ChildSaSession->RemotePeerSpi, ProposalData->Spi, sizeof 
(ChildSaSession->RemotePeerSpi));
 
@@ -2603,11 +2692,15 @@ Ikev2SaGenerateKey (
   for (Index = 0; Index < NumFragments; Index++) {
     FragmentsSize = FragmentsSize + Fragments[Index].DataSize;
   }
 
   LocalFragments[1].Data     = AllocateZeroPool (FragmentsSize);
-  ASSERT (LocalFragments[1].Data != NULL);
+  if (LocalFragments[1].Data == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Exit;
+  }
+  
   LocalFragments[1].DataSize = FragmentsSize;
 
   //
   // Copy all input fragments into LocalFragments[1];
   //
@@ -2629,11 +2722,15 @@ Ikev2SaGenerateKey (
   LocalFragments[2].DataSize = sizeof (TailData);
   //
   // Allocate buffer for the first fragment
   //
   LocalFragments[0].Data     = AllocateZeroPool (AuthKeyLength);
-  ASSERT (LocalFragments[0].Data != NULL);
+  if (LocalFragments[0].Data == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto Exit;
+  }
+  
   LocalFragments[0].DataSize = AuthKeyLength;
 
   Round = (OutputKeyLength - 1) / AuthKeyLength + 1;
   for (Index = 0; Index < Round; Index++) {
     Status = IpSecCryptoIoHmac (
diff --git a/NetworkPkg/IpSecDxe/Ikev2/Utility.h 
b/NetworkPkg/IpSecDxe/Ikev2/Utility.h
index c018f42..319b6cb 100644
--- a/NetworkPkg/IpSecDxe/Ikev2/Utility.h
+++ b/NetworkPkg/IpSecDxe/Ikev2/Utility.h
@@ -1,10 +1,10 @@
 /** @file
   The interfaces of IKE/Child session operations and payload related 
operations 
   used by IKE Exchange Process.
 
-  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -1117,13 +1117,16 @@ Ikev2SaGenerateKey (
   ChildSaSession->SpdSelector stores the real Spdselector for its SA. Sometime,
   the SpdSelector in ChildSaSession is more accurated or the scope is smaller 
   than the one in ChildSaSession->Spd, especially for the tunnel mode.
     
   @param[in, out]  ChildSaSession  Pointer to IKEV2_CHILD_SA_SESSION related 
to.
+
+  @retval EFI_SUCCESS            The operation complete successfully.
+  @retval EFI_OUT_OF_RESOURCES   If the required resource can't be allocated.
   
 **/
-VOID
+EFI_STATUS
 Ikev2ChildSaSessionSpdSelectorCreate (
   IN OUT IKEV2_CHILD_SA_SESSION *ChildSaSession
   );
 
 extern IKE_ALG_GUID_INFO mIPsecEncrAlgInfo[];
diff --git a/NetworkPkg/IpSecDxe/IpSecConfigImpl.c 
b/NetworkPkg/IpSecDxe/IpSecConfigImpl.c
index e1b24e4..cfee978 100644
--- a/NetworkPkg/IpSecDxe/IpSecConfigImpl.c
+++ b/NetworkPkg/IpSecDxe/IpSecConfigImpl.c
@@ -2173,11 +2173,14 @@ IpSecGetVariable (
   // "VariableNameNULL".
   //
   VariableNameLength  = StrLen (VariableName);
   VariableNameISize   = (VariableNameLength + 5) * sizeof (CHAR16);
   VariableNameI       = AllocateZeroPool (VariableNameISize);
-  ASSERT (VariableNameI != NULL);
+  if (VariableNameI == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_EXIT;
+  }
   
   //
   // Construct the varible name of ipsecconfig meta data.
   //
   UnicodeSPrint (VariableNameI, VariableNameISize, L"%s%s", VariableName, 
L"Info");
diff --git a/NetworkPkg/IpSecDxe/IpSecCryptIo.c 
b/NetworkPkg/IpSecDxe/IpSecCryptIo.c
index 8396c59..dca4423 100644
--- a/NetworkPkg/IpSecDxe/IpSecCryptIo.c
+++ b/NetworkPkg/IpSecDxe/IpSecCryptIo.c
@@ -1,9 +1,9 @@
 /** @file
   Common interfaces to call Security library.
 
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -966,11 +966,14 @@ IpSecCryptoIoGetPublicKeyFromCert (
   *PublicKeyLen = 0;
  
   RsaGetKey (RsaContext, RsaKeyN, NULL, PublicKeyLen);
  
   *PublicKey = AllocateZeroPool (*PublicKeyLen);
-  ASSERT (*PublicKey != NULL);
+  if (*PublicKey == NULL) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto EXIT;
+  }
 
   if (!RsaGetKey (RsaContext, RsaKeyN, *PublicKey, PublicKeyLen)) {
     Status = EFI_INVALID_PARAMETER;
   }
 
diff --git a/NetworkPkg/IpSecDxe/IpSecImpl.c b/NetworkPkg/IpSecDxe/IpSecImpl.c
index 854a9a5..625f154 100644
--- a/NetworkPkg/IpSecDxe/IpSecImpl.c
+++ b/NetworkPkg/IpSecDxe/IpSecImpl.c
@@ -1,10 +1,10 @@
 /** @file
   The implementation of IPsec.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
 
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
   http://opensource.org/licenses/bsd-license.php.
@@ -1188,13 +1188,10 @@ IpSecTunnelInboundPacket (
   @param[in, out] FragmentTable      Pointer to a list of fragments to be 
protected by
                                      IPsec on input, and with IPsec protected
                                      on return.
   @param[in]      FragmentCount      The number of fragments.
 
-  @retval EFI_SUCCESS              The operation was successful.
-  @retval EFI_OUT_OF_RESOURCES     The required system resources can't be 
allocated.
-
 **/
 UINT8 *
 IpSecTunnelOutboundPacket (
   IN OUT UINT8                   *IpHead,
   IN     UINT8                   IpVersion,
@@ -1218,11 +1215,14 @@ IpSecTunnelOutboundPacket (
     return NULL;
   }
 
   if (IpVersion == IP_VERSION_4) {
     InnerHead = AllocateZeroPool (sizeof (IP4_HEAD) + *OptionsLength);
-    ASSERT (InnerHead != NULL);
+    if (InnerHead == NULL) {
+      return NULL;
+    }
+    
     CopyMem (
       InnerHead,
       IpHead,
       sizeof (IP4_HEAD)
       );
@@ -1231,11 +1231,14 @@ IpSecTunnelOutboundPacket (
       *OptionsBuffer,
       *OptionsLength
       );
   } else {
     InnerHead = AllocateZeroPool (sizeof (EFI_IP6_HEADER) + *OptionsLength);
-    ASSERT (InnerHead != NULL);
+    if (InnerHead == NULL) {
+      return NULL;
+    }
+    
     CopyMem (
       InnerHead,
       IpHead,
       sizeof (EFI_IP6_HEADER)
       );
@@ -1262,11 +1265,15 @@ IpSecTunnelOutboundPacket (
              0,
              0,
              IpSecOnRecyclePacket,
              NULL
              );
-  ASSERT (Packet != NULL);
+  if (Packet == NULL) {
+    FreePool (InnerHead);
+    return NULL;
+  }
+  
   //
   // 3. Check the Last Header, if it is TCP, UDP or ICMP recalcualate its 
pesudo
   //    CheckSum.
   //
   switch (*LastHead) {
-- 
1.9.5.msysgit.1

_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to