]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLib/Pk/CryptDh.c
CryptoPkg/BaseCryptLib: Add NULL pointer checks in DH and P7Verify
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptDh.c
index a5d6e49b8fa6f8b6ef1569b45a405162217a3259..391efd5c14550f16bd9466ad076d2313a3b3bb83 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Diffie-Hellman Wrapper Implementation over OpenSSL.\r
 \r
-Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2017, 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
@@ -16,7 +16,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <openssl/bn.h>\r
 #include <openssl/dh.h>\r
 \r
-\r
 /**\r
   Allocates and Initializes one Diffie-Hellman Context for subsequent use.\r
 \r
@@ -88,6 +87,7 @@ DhGenerateParameter (
   )\r
 {\r
   BOOLEAN RetVal;\r
+  BIGNUM  *BnP;\r
 \r
   //\r
   // Check input parameters.\r
@@ -105,7 +105,8 @@ DhGenerateParameter (
     return FALSE;\r
   }\r
 \r
-  BN_bn2bin (((DH *) DhContext)->p, Prime);\r
+  DH_get0_pqg (DhContext, (const BIGNUM **)&BnP, NULL, NULL);\r
+  BN_bn2bin (BnP, Prime);\r
 \r
   return TRUE;\r
 }\r
@@ -141,7 +142,8 @@ DhSetParameter (
   )\r
 {\r
   DH      *Dh;\r
-  BIGNUM  *Bn;\r
+  BIGNUM  *BnP;\r
+  BIGNUM  *BnG;\r
 \r
   //\r
   // Check input parameters.\r
@@ -149,50 +151,27 @@ DhSetParameter (
   if (DhContext == NULL || Prime == NULL || PrimeLength > INT_MAX) {\r
     return FALSE;\r
   }\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->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 = 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
+  //\r
+  // Set the generator and prime parameters for DH object.\r
+  //\r
+  Dh  = (DH *)DhContext;\r
+  BnP = BN_bin2bn ((const unsigned char *)Prime, (int)(PrimeLength / 8), NULL);\r
+  BnG = BN_bin2bn ((const unsigned char *)&Generator, 1, NULL);\r
+  if ((BnP == NULL) || (BnG == NULL) || !DH_set0_pqg (Dh, BnP, NULL, BnG)) {\r
     goto Error;\r
   }\r
 \r
   return TRUE;\r
 \r
 Error:\r
+  BN_free (BnP);\r
+  BN_free (BnG);\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
@@ -228,6 +207,7 @@ DhGenerateKey (
 {\r
   BOOLEAN RetVal;\r
   DH      *Dh;\r
+  BIGNUM  *DhPubKey;\r
   INTN    Size;\r
 \r
   //\r
@@ -240,22 +220,21 @@ DhGenerateKey (
   if (PublicKey == NULL && *PublicKeySize != 0) {\r
     return FALSE;\r
   }\r
-  \r
+\r
   Dh = (DH *) DhContext;\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
+    DH_get0_key (Dh, (const BIGNUM **)&DhPubKey, NULL);\r
+    Size = BN_num_bytes (DhPubKey);\r
+    if ((Size > 0) && (*PublicKeySize < (UINTN) Size)) {\r
       *PublicKeySize = Size;\r
       return FALSE;\r
     }\r
-    \r
-    BN_bn2bin (Dh->pub_key, PublicKey);\r
+\r
+    if (PublicKey != NULL) {\r
+      BN_bn2bin (DhPubKey, PublicKey);\r
+    }\r
     *PublicKeySize = Size;\r
   }\r
 \r