]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c
CryptoPkg BaseCryptLib: Avoid passing NULL ptr to function BN_bn2bin()
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptDh.c
index b7e164cf43770dc8a38bf4aca58bd25148ee0db8..5e0447b488a3396adaab31076af078fb63f86bae 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Diffie-Hellman Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 **/\r
 \r
 #include "InternalCryptLib.h"\r
+#include <openssl/bn.h>\r
 #include <openssl/dh.h>\r
 \r
 \r
@@ -32,13 +33,13 @@ DhNew (
   //\r
   // Allocates & Initializes DH Context by OpenSSL DH_new()\r
   //\r
-  return (VOID *)DH_new ();\r
+  return (VOID *) DH_new ();\r
 }\r
 \r
 /**\r
   Release the specified DH context.\r
 \r
-  If DhContext is NULL, then ASSERT().\r
+  If DhContext is NULL, then return FALSE.\r
 \r
   @param[in]  DhContext  Pointer to the DH context to be released.\r
 \r
@@ -52,7 +53,7 @@ DhFree (
   //\r
   // Free OpenSSL DH Context\r
   //\r
-  DH_free ((DH *)DhContext);\r
+  DH_free ((DH *) DhContext);\r
 }\r
 \r
 /**\r
@@ -64,8 +65,8 @@ DhFree (
   Before this function can be invoked, pseudorandom number generator must be correctly\r
   initialized by RandomSeed().\r
 \r
-  If DhContext is NULL, then ASSERT().\r
-  If Prime is NULL, then ASSERT().\r
+  If DhContext is NULL, then return FALSE.\r
+  If Prime is NULL, then return FALSE.\r
 \r
   @param[in, out]  DhContext    Pointer to the DH context.\r
   @param[in]       Generator    Value of generator.\r
@@ -88,6 +89,13 @@ DhGenerateParameter (
 {\r
   BOOLEAN RetVal;\r
 \r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+\r
   if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
     return FALSE;\r
   }\r
@@ -108,8 +116,8 @@ DhGenerateParameter (
   Given generator g, and prime number p, this function and sets DH\r
   context accordingly.\r
 \r
-  If DhContext is NULL, then ASSERT().\r
-  If Prime is NULL, then ASSERT().\r
+  If DhContext is NULL, then return FALSE.\r
+  If Prime is NULL, then return FALSE.\r
 \r
   @param[in, out]  DhContext    Pointer to the DH context.\r
   @param[in]       Generator    Value of generator.\r
@@ -132,20 +140,60 @@ DhSetParameter (
   IN      CONST UINT8  *Prime\r
   )\r
 {\r
-  DH  *Dh;\r
+  DH      *Dh;\r
+  BIGNUM  *Bn;\r
 \r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+  \r
   if (Generator != DH_GENERATOR_2 && Generator != DH_GENERATOR_5) {\r
     return FALSE;\r
   }\r
 \r
+  Bn = NULL;\r
+\r
   Dh = (DH *) DhContext;\r
-  Dh->p = BN_new();\r
-  Dh->g = BN_new();\r
+  Dh->g = NULL;\r
+  Dh->p = BN_new ();\r
+  if (Dh->p == NULL) {\r
+    goto Error;\r
+  }\r
+  \r
+  Dh->g = BN_new ();\r
+  if (Dh->g == NULL) {\r
+    goto Error;\r
+  }\r
 \r
-  BN_bin2bn (Prime, (UINT32) (PrimeLength / 8), Dh->p);\r
-  BN_set_word (Dh->g, (UINT32) Generator);\r
+  Bn = BN_bin2bn (Prime, (UINT32) (PrimeLength / 8), Dh->p);\r
+  if (Bn == NULL) {\r
+    goto Error;\r
+  }\r
+\r
+  if (BN_set_word (Dh->g, (UINT32) Generator) == 0) {\r
+    goto Error;\r
+  }\r
 \r
   return TRUE;\r
+\r
+Error:\r
+\r
+  if (Dh->p != NULL) {\r
+    BN_free (Dh->p);\r
+  }\r
+\r
+  if (Dh->g != NULL) {\r
+    BN_free (Dh->g);\r
+  }\r
+\r
+  if (Bn != NULL) {\r
+    BN_free (Bn);\r
+  }\r
+  \r
+  return FALSE;\r
 }\r
 \r
 /**\r
@@ -156,9 +204,9 @@ DhSetParameter (
   If the PublicKey buffer is too small to hold the public key, FALSE is returned and\r
   PublicKeySize is set to the required buffer size to obtain the public key.\r
 \r
-  If DhContext is NULL, then ASSERT().\r
-  If PublicKeySize is NULL, then ASSERT().\r
-  If PublicKeySize is large enough but PublicKey is NULL, then ASSERT().\r
+  If DhContext is NULL, then return FALSE.\r
+  If PublicKeySize is NULL, then return FALSE.\r
+  If PublicKeySize is large enough but PublicKey is NULL, then return FALSE.\r
 \r
   @param[in, out]  DhContext      Pointer to the DH context.\r
   @param[out]      PublicKey      Pointer to the buffer to receive generated public key.\r
@@ -180,14 +228,35 @@ DhGenerateKey (
 {\r
   BOOLEAN RetVal;\r
   DH      *Dh;\r
+  INTN    Size;\r
 \r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (DhContext == NULL || PublicKeySize == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (PublicKey == NULL && *PublicKeySize != 0) {\r
+    return FALSE;\r
+  }\r
+  \r
   Dh = (DH *) DhContext;\r
-  *PublicKeySize = 0;\r
 \r
   RetVal = (BOOLEAN) DH_generate_key (DhContext);\r
   if (RetVal) {\r
+    Size = BN_num_bytes (Dh->pub_key);\r
+    if (Size <= 0) {\r
+      *PublicKeySize = 0;\r
+      return FALSE;\r
+    }\r
+    if (*PublicKeySize < (UINTN) Size) {\r
+      *PublicKeySize = Size;\r
+      return FALSE;\r
+    }\r
+    \r
     BN_bn2bin (Dh->pub_key, PublicKey);\r
-    *PublicKeySize  = BN_num_bytes (Dh->pub_key);\r
+    *PublicKeySize = Size;\r
   }\r
 \r
   return RetVal;\r
@@ -199,10 +268,11 @@ DhGenerateKey (
   Given peer's public key, this function computes the exchanged common key, based on its own\r
   context including value of prime modulus and random secret exponent. \r
 \r
-  If DhContext is NULL, then ASSERT().\r
-  If PeerPublicKey is NULL, then ASSERT().\r
-  If KeySize is NULL, then ASSERT().\r
-  If KeySize is large enough but Key is NULL, then ASSERT().\r
+  If DhContext is NULL, then return FALSE.\r
+  If PeerPublicKey is NULL, then return FALSE.\r
+  If KeySize is NULL, then return FALSE.\r
+  If Key is NULL, then return FALSE.\r
+  If KeySize is not large enough, then return FALSE.\r
 \r
   @param[in, out]  DhContext          Pointer to the DH context.\r
   @param[in]       PeerPublicKey      Pointer to the peer's public key.\r
@@ -227,12 +297,37 @@ DhComputeKey (
   )\r
 {\r
   BIGNUM  *Bn;\r
+  INTN    Size;\r
 \r
+  //\r
+  // Check input parameters.\r
+  //\r
+  if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL || Key == NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  if (PeerPublicKeySize > INT_MAX) {\r
+    return FALSE;\r
+  }\r
+  \r
   Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL);\r
+  if (Bn == NULL) {\r
+    return FALSE;\r
+  }\r
 \r
-  *KeySize = (BOOLEAN) DH_compute_key (Key, Bn, DhContext);\r
+  Size = DH_compute_key (Key, Bn, DhContext);\r
+  if (Size < 0) {\r
+    BN_free (Bn);\r
+    return FALSE;\r
+  }\r
 \r
-  BN_free (Bn);\r
+  if (*KeySize < (UINTN) Size) {\r
+    *KeySize = Size;\r
+    BN_free (Bn);\r
+    return FALSE;\r
+  }\r
 \r
+  *KeySize = Size;\r
+  BN_free (Bn);\r
   return TRUE;\r
 }\r