]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c
CryptoPkg/BaseCryptLib: Add missing OpenSSL includes
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptDh.c
index 30792d27ca08f6b30eceff3d185448f8d468825f..9cbf182c70f5b292a39ab2b67107208fa0173e26 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Diffie-Hellman Wrapper Implementation over OpenSSL.\r
 \r
 /** @file\r
   Diffie-Hellman Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2015, 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
 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
 **/\r
 \r
 #include "InternalCryptLib.h"\r
+#include <openssl/bn.h>\r
 #include <openssl/dh.h>\r
 \r
 \r
 #include <openssl/dh.h>\r
 \r
 \r
@@ -91,7 +92,7 @@ DhGenerateParameter (
   //\r
   // Check input parameters.\r
   //\r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || Prime == NULL) {\r
+  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
@@ -139,12 +140,13 @@ DhSetParameter (
   IN      CONST UINT8  *Prime\r
   )\r
 {\r
   IN      CONST UINT8  *Prime\r
   )\r
 {\r
-  DH  *Dh;\r
+  DH      *Dh;\r
+  BIGNUM  *Bn;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || Prime == NULL) {\r
+  if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
     return FALSE;\r
   }\r
   \r
     return FALSE;\r
   }\r
   \r
@@ -152,14 +154,46 @@ DhSetParameter (
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
+  Bn = NULL;\r
+\r
   Dh = (DH *) DhContext;\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
 \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
   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
 }\r
 \r
 /**\r
@@ -194,6 +228,7 @@ DhGenerateKey (
 {\r
   BOOLEAN RetVal;\r
   DH      *Dh;\r
 {\r
   BOOLEAN RetVal;\r
   DH      *Dh;\r
+  INTN    Size;\r
 \r
   //\r
   // Check input parameters.\r
 \r
   //\r
   // Check input parameters.\r
@@ -207,12 +242,17 @@ DhGenerateKey (
   }\r
   \r
   Dh = (DH *) DhContext;\r
   }\r
   \r
   Dh = (DH *) DhContext;\r
-  *PublicKeySize = 0;\r
 \r
   RetVal = (BOOLEAN) DH_generate_key (DhContext);\r
   if (RetVal) {\r
 \r
   RetVal = (BOOLEAN) DH_generate_key (DhContext);\r
   if (RetVal) {\r
+    Size = BN_num_bytes (Dh->pub_key);\r
+    if ((Size > 0) && (*PublicKeySize < (UINTN) Size)) {\r
+      *PublicKeySize = Size;\r
+      return FALSE;\r
+    }\r
+    \r
     BN_bn2bin (Dh->pub_key, PublicKey);\r
     BN_bn2bin (Dh->pub_key, PublicKey);\r
-    *PublicKeySize  = BN_num_bytes (Dh->pub_key);\r
+    *PublicKeySize = Size;\r
   }\r
 \r
   return RetVal;\r
   }\r
 \r
   return RetVal;\r
@@ -227,7 +267,8 @@ DhGenerateKey (
   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 DhContext is NULL, then return FALSE.\r
   If PeerPublicKey is NULL, then return FALSE.\r
   If KeySize is NULL, then return FALSE.\r
-  If KeySize is large enough but Key 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
 \r
   @param[in, out]  DhContext          Pointer to the DH context.\r
   @param[in]       PeerPublicKey      Pointer to the peer's public key.\r
@@ -252,23 +293,37 @@ DhComputeKey (
   )\r
 {\r
   BIGNUM  *Bn;\r
   )\r
 {\r
   BIGNUM  *Bn;\r
+  INTN    Size;\r
 \r
   //\r
   // Check input parameters.\r
   //\r
 \r
   //\r
   // Check input parameters.\r
   //\r
-  if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL) {\r
+  if (DhContext == NULL || PeerPublicKey == NULL || KeySize == NULL || Key == NULL) {\r
     return FALSE;\r
   }\r
 \r
     return FALSE;\r
   }\r
 \r
-  if (Key == NULL && *KeySize != 0) {\r
+  if (PeerPublicKeySize > INT_MAX) {\r
     return FALSE;\r
   }\r
   \r
   Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL);\r
     return FALSE;\r
   }\r
   \r
   Bn = BN_bin2bn (PeerPublicKey, (UINT32) PeerPublicKeySize, NULL);\r
+  if (Bn == NULL) {\r
+    return FALSE;\r
+  }\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
 \r
-  BN_free (Bn);\r
+  if (*KeySize < (UINTN) Size) {\r
+    *KeySize = Size;\r
+    BN_free (Bn);\r
+    return FALSE;\r
+  }\r
 \r
 \r
+  *KeySize = Size;\r
+  BN_free (Bn);\r
   return TRUE;\r
 }\r
   return TRUE;\r
 }\r