Blame SOURCES/nss-3.44-handle-malformed-ecdh-gtests.patch

57c594
diff -up ./gtests/pk11_gtest/manifest.mn.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/manifest.mn
57c594
--- ./gtests/pk11_gtest/manifest.mn.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.827832606 -0800
57c594
+++ ./gtests/pk11_gtest/manifest.mn	2019-12-05 10:59:53.802966671 -0800
57c594
@@ -10,16 +10,18 @@ CPPSRCS = \
57c594
       pk11_aeskeywrap_unittest.cc \
57c594
       pk11_chacha20poly1305_unittest.cc \
57c594
       pk11_curve25519_unittest.cc \
57c594
+      pk11_der_private_key_import_unittest.cc \
57c594
       pk11_ecdsa_unittest.cc \
57c594
       pk11_encrypt_derive_unittest.cc \
57c594
       pk11_export_unittest.cc \
57c594
       pk11_import_unittest.cc \
57c594
+      pk11_keygen.cc \
57c594
+      pk11_key_unittest.cc \
57c594
       pk11_pbkdf2_unittest.cc \
57c594
       pk11_prf_unittest.cc \
57c594
       pk11_prng_unittest.cc \
57c594
       pk11_rsapkcs1_unittest.cc \
57c594
       pk11_rsapss_unittest.cc \
57c594
-      pk11_der_private_key_import_unittest.cc \
57c594
       $(NULL)
57c594
 
57c594
 INCLUDES += -I$(CORE_DEPTH)/gtests/google_test/gtest/include \
57c594
diff -up ./gtests/pk11_gtest/pk11_gtest.gyp.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_gtest.gyp
57c594
--- ./gtests/pk11_gtest/pk11_gtest.gyp.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.828832617 -0800
57c594
+++ ./gtests/pk11_gtest/pk11_gtest.gyp	2019-12-05 11:01:38.874134681 -0800
57c594
@@ -11,20 +11,22 @@
57c594
       'target_name': 'pk11_gtest',
57c594
       'type': 'executable',
57c594
       'sources': [
57c594
-        'pk11_aeskeywrap_unittest.cc',
57c594
         'pk11_aes_gcm_unittest.cc',
57c594
+        'pk11_aeskeywrap_unittest.cc',
57c594
         'pk11_chacha20poly1305_unittest.cc',
57c594
         'pk11_cipherop_unittest.cc',
57c594
         'pk11_curve25519_unittest.cc',
57c594
+        'pk11_der_private_key_import_unittest.cc',
57c594
         'pk11_ecdsa_unittest.cc',
57c594
         'pk11_encrypt_derive_unittest.cc',
57c594
         'pk11_import_unittest.cc',
57c594
+        'pk11_keygen.cc',
57c594
+        'pk11_key_unittest.cc',
57c594
         'pk11_pbkdf2_unittest.cc',
57c594
         'pk11_prf_unittest.cc',
57c594
         'pk11_prng_unittest.cc',
57c594
         'pk11_rsapkcs1_unittest.cc',
57c594
         'pk11_rsapss_unittest.cc',
57c594
-        'pk11_der_private_key_import_unittest.cc',
57c594
         '<(DEPTH)/gtests/common/gtests.cc'
57c594
       ],
57c594
       'dependencies': [
57c594
diff -up ./gtests/pk11_gtest/pk11_import_unittest.cc.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_import_unittest.cc
57c594
--- ./gtests/pk11_gtest/pk11_import_unittest.cc.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.821832539 -0800
57c594
+++ ./gtests/pk11_gtest/pk11_import_unittest.cc	2019-12-05 11:08:42.394842692 -0800
57c594
@@ -15,6 +15,7 @@
57c594
 #include "nss_scoped_ptrs.h"
57c594
 #include "gtest/gtest.h"
57c594
 #include "databuffer.h"
57c594
+#include "pk11_keygen.h"
57c594
 
57c594
 namespace nss_test {
57c594
 
57c594
@@ -30,7 +31,7 @@ struct PK11GenericObjectsDeleter {
57c594
 
57c594
 class Pk11KeyImportTestBase : public ::testing::Test {
57c594
  public:
57c594
-  Pk11KeyImportTestBase(CK_MECHANISM_TYPE mech) : mech_(mech) {}
57c594
+  Pk11KeyImportTestBase() = default;
57c594
   virtual ~Pk11KeyImportTestBase() = default;
57c594
 
57c594
   void SetUp() override {
57c594
@@ -42,12 +43,18 @@ class Pk11KeyImportTestBase : public ::t
57c594
     password_.reset(SECITEM_DupItem(&pwItem));
57c594
   }
57c594
 
57c594
-  void Test() {
57c594
+  void Test(const Pkcs11KeyPairGenerator& generator) {
57c594
     // Generate a key and export it.
57c594
-    KeyType key_type;
57c594
+    KeyType key_type = nullKey;
57c594
     ScopedSECKEYEncryptedPrivateKeyInfo key_info;
57c594
     ScopedSECItem public_value;
57c594
-    GenerateAndExport(&key_type, &key_info, &public_value);
57c594
+    GenerateAndExport(generator, &key_type, &key_info, &public_value);
57c594
+
57c594
+    // Note: NSS is currently unable export wrapped DH keys, so this doesn't
57c594
+    // test those beyond generate and verify.
57c594
+    if (key_type == dhKey) {
57c594
+      return;
57c594
+    }
57c594
     ASSERT_NE(nullptr, key_info);
57c594
     ASSERT_NE(nullptr, public_value);
57c594
 
57c594
@@ -66,17 +73,6 @@ class Pk11KeyImportTestBase : public ::t
57c594
     CheckForPublicKey(priv_key, public_value.get());
57c594
   }
57c594
 
57c594
- protected:
57c594
-  class ParamHolder {
57c594
-   public:
57c594
-    virtual ~ParamHolder() = default;
57c594
-    virtual void* get() = 0;
57c594
-  };
57c594
-
57c594
-  virtual std::unique_ptr<ParamHolder> MakeParams() = 0;
57c594
-
57c594
-  CK_MECHANISM_TYPE mech_;
57c594
-
57c594
  private:
57c594
   SECItem GetPublicComponent(ScopedSECKEYPublicKey& pub_key) {
57c594
     SECItem null = { siBuffer, NULL, 0};
57c594
@@ -196,20 +192,14 @@ class Pk11KeyImportTestBase : public ::t
57c594
     }
57c594
   }
57c594
 
57c594
-  void GenerateAndExport(KeyType* key_type,
57c594
+  void GenerateAndExport(const Pkcs11KeyPairGenerator& generator,
57c594
+                         KeyType* key_type,
57c594
                          ScopedSECKEYEncryptedPrivateKeyInfo* key_info,
57c594
                          ScopedSECItem* public_value) {
57c594
-    auto params = MakeParams();
57c594
-    ASSERT_NE(nullptr, params);
57c594
-
57c594
-    SECKEYPublicKey* pub_tmp;
57c594
-    ScopedSECKEYPrivateKey priv_key(
57c594
-        PK11_GenerateKeyPair(slot_.get(), mech_, params->get(), &pub_tmp,
57c594
-                             PR_FALSE, PR_TRUE, nullptr));
57c594
-    ASSERT_NE(nullptr, priv_key) << "PK11_GenerateKeyPair failed: "
57c594
-                                 << PORT_ErrorToName(PORT_GetError());
57c594
-    ScopedSECKEYPublicKey pub_key(pub_tmp);
57c594
-    ASSERT_NE(nullptr, pub_key);
57c594
+    ScopedSECKEYPrivateKey priv_key;
57c594
+    ScopedSECKEYPublicKey pub_key;
57c594
+    generator.GenerateKey(&priv_key, &pub_key);
57c594
+    ASSERT_TRUE(priv_key);
57c594
 
57c594
     // Wrap and export the key.
57c594
     ScopedSECKEYEncryptedPrivateKeyInfo epki(PK11_ExportEncryptedPrivKeyInfo(
57c594
@@ -239,6 +229,11 @@ class Pk11KeyImportTestBase : public ::t
57c594
     }
57c594
 
57c594
     CheckForPublicKey(priv_key, pub_val);
57c594
+    // Note: NSS is currently unable export wrapped DH keys, so this doesn't
57c594
+    // test those beyond generate and verify.
57c594
+    if (t == dhKey) {
57c594
+      return;
57c594
+    }
57c594
 
57c594
     *key_type = t;
57c594
     key_info->swap(epki);
57c594
@@ -253,82 +248,13 @@ class Pk11KeyImportTest
57c594
     : public Pk11KeyImportTestBase,
57c594
       public ::testing::WithParamInterface<CK_MECHANISM_TYPE> {
57c594
  public:
57c594
-  Pk11KeyImportTest() : Pk11KeyImportTestBase(GetParam()) {}
57c594
+  Pk11KeyImportTest() = default;
57c594
   virtual ~Pk11KeyImportTest() = default;
57c594
-
57c594
- protected:
57c594
-  std::unique_ptr<ParamHolder> MakeParams() override {
57c594
-    switch (mech_) {
57c594
-      case CKM_RSA_PKCS_KEY_PAIR_GEN:
57c594
-        return std::unique_ptr<ParamHolder>(new RsaParamHolder());
57c594
-
57c594
-      case CKM_DSA_KEY_PAIR_GEN:
57c594
-      case CKM_DH_PKCS_KEY_PAIR_GEN: {
57c594
-        PQGParams* pqg_params = nullptr;
57c594
-        PQGVerify* pqg_verify = nullptr;
57c594
-        const unsigned int key_size = 1024;
57c594
-        SECStatus rv = PK11_PQG_ParamGenV2(key_size, 0, key_size / 16,
57c594
-                                           &pqg_params, &pqg_verify);
57c594
-        if (rv != SECSuccess) {
57c594
-          ADD_FAILURE() << "PK11_PQG_ParamGenV2 failed";
57c594
-          return nullptr;
57c594
-        }
57c594
-        EXPECT_NE(nullptr, pqg_verify);
57c594
-        EXPECT_NE(nullptr, pqg_params);
57c594
-        PK11_PQG_DestroyVerify(pqg_verify);
57c594
-        if (mech_ == CKM_DSA_KEY_PAIR_GEN) {
57c594
-          return std::unique_ptr<ParamHolder>(new PqgParamHolder(pqg_params));
57c594
-        }
57c594
-        return std::unique_ptr<ParamHolder>(new DhParamHolder(pqg_params));
57c594
-      }
57c594
-
57c594
-      default:
57c594
-        ADD_FAILURE() << "unknown OID " << mech_;
57c594
-    }
57c594
-    return nullptr;
57c594
-  }
57c594
-
57c594
- private:
57c594
-  class RsaParamHolder : public ParamHolder {
57c594
-   public:
57c594
-    RsaParamHolder()
57c594
-        : params_({/*.keySizeInBits = */ 1024, /*.pe = */ 0x010001}) {}
57c594
-    ~RsaParamHolder() = default;
57c594
-
57c594
-    void* get() override { return &params_; }
57c594
-
57c594
-   private:
57c594
-    PK11RSAGenParams params_;
57c594
-  };
57c594
-
57c594
-  class PqgParamHolder : public ParamHolder {
57c594
-   public:
57c594
-    PqgParamHolder(PQGParams* params) : params_(params) {}
57c594
-    ~PqgParamHolder() = default;
57c594
-
57c594
-    void* get() override { return params_.get(); }
57c594
-
57c594
-   private:
57c594
-    ScopedPQGParams params_;
57c594
-  };
57c594
-
57c594
-  class DhParamHolder : public PqgParamHolder {
57c594
-   public:
57c594
-    DhParamHolder(PQGParams* params)
57c594
-        : PqgParamHolder(params),
57c594
-          params_({/*.arena = */ nullptr,
57c594
-                   /*.prime = */ params->prime,
57c594
-                   /*.base = */ params->base}) {}
57c594
-    ~DhParamHolder() = default;
57c594
-
57c594
-    void* get() override { return &params_; }
57c594
-
57c594
-   private:
57c594
-    SECKEYDHParams params_;
57c594
-  };
57c594
 };
57c594
 
57c594
-TEST_P(Pk11KeyImportTest, GenerateExportImport) { Test(); }
57c594
+TEST_P(Pk11KeyImportTest, GenerateExportImport) {
57c594
+  Test(Pkcs11KeyPairGenerator(GetParam()));
57c594
+}
57c594
 
57c594
 INSTANTIATE_TEST_CASE_P(Pk11KeyImportTest, Pk11KeyImportTest,
57c594
                         ::testing::Values(CKM_RSA_PKCS_KEY_PAIR_GEN,
57c594
@@ -339,42 +265,13 @@ INSTANTIATE_TEST_CASE_P(Pk11KeyImportTes
57c594
 class Pk11KeyImportTestEC : public Pk11KeyImportTestBase,
57c594
                             public ::testing::WithParamInterface<SECOidTag> {
57c594
  public:
57c594
-  Pk11KeyImportTestEC() : Pk11KeyImportTestBase(CKM_EC_KEY_PAIR_GEN) {}
57c594
+  Pk11KeyImportTestEC() = default;
57c594
   virtual ~Pk11KeyImportTestEC() = default;
57c594
-
57c594
- protected:
57c594
-  std::unique_ptr<ParamHolder> MakeParams() override {
57c594
-    return std::unique_ptr<ParamHolder>(new EcParamHolder(GetParam()));
57c594
-  }
57c594
-
57c594
- private:
57c594
-  class EcParamHolder : public ParamHolder {
57c594
-   public:
57c594
-    EcParamHolder(SECOidTag curve_oid) {
57c594
-      SECOidData* curve = SECOID_FindOIDByTag(curve_oid);
57c594
-      EXPECT_NE(nullptr, curve);
57c594
-
57c594
-      size_t plen = curve->oid.len + 2;
57c594
-      extra_.reset(new uint8_t[plen]);
57c594
-      extra_[0] = SEC_ASN1_OBJECT_ID;
57c594
-      extra_[1] = static_cast<uint8_t>(curve->oid.len);
57c594
-      memcpy(&extra_[2], curve->oid.data, curve->oid.len);
57c594
-
57c594
-      ec_params_ = {/*.type = */ siBuffer,
57c594
-                    /*.data = */ extra_.get(),
57c594
-                    /*.len = */ static_cast<unsigned int>(plen)};
57c594
-    }
57c594
-    ~EcParamHolder() = default;
57c594
-
57c594
-    void* get() override { return &ec_params_; }
57c594
-
57c594
-   private:
57c594
-    SECKEYECParams ec_params_;
57c594
-    std::unique_ptr<uint8_t[]> extra_;
57c594
-  };
57c594
 };
57c594
 
57c594
-TEST_P(Pk11KeyImportTestEC, GenerateExportImport) { Test(); }
57c594
+TEST_P(Pk11KeyImportTestEC, GenerateExportImport) {
57c594
+  Test(Pkcs11KeyPairGenerator(CKM_EC_KEY_PAIR_GEN, GetParam()));
57c594
+}
57c594
 
57c594
 INSTANTIATE_TEST_CASE_P(Pk11KeyImportTestEC, Pk11KeyImportTestEC,
57c594
                         ::testing::Values(SEC_OID_SECG_EC_SECP256R1,
57c594
diff -up ./gtests/pk11_gtest/pk11_keygen.cc.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_keygen.cc
57c594
--- ./gtests/pk11_gtest/pk11_keygen.cc.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.829832628 -0800
57c594
+++ ./gtests/pk11_gtest/pk11_keygen.cc	2019-12-05 10:56:41.829832628 -0800
57c594
@@ -0,0 +1,143 @@
57c594
+/* This Source Code Form is subject to the terms of the Mozilla Public
57c594
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
57c594
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
57c594
+
57c594
+#include "pk11_keygen.h"
57c594
+
57c594
+#include "pk11pub.h"
57c594
+#include "pk11pqg.h"
57c594
+#include "prerror.h"
57c594
+
57c594
+#include "gtest/gtest.h"
57c594
+
57c594
+namespace nss_test {
57c594
+
57c594
+class ParamHolder {
57c594
+ public:
57c594
+  virtual void* get() = 0;
57c594
+  virtual ~ParamHolder() = default;
57c594
+
57c594
+ protected:
57c594
+  ParamHolder() = default;
57c594
+};
57c594
+
57c594
+void Pkcs11KeyPairGenerator::GenerateKey(ScopedSECKEYPrivateKey* priv_key,
57c594
+                                         ScopedSECKEYPublicKey* pub_key) const {
57c594
+  // This function returns if an assertion fails, so don't leak anything.
57c594
+  priv_key->reset(nullptr);
57c594
+  pub_key->reset(nullptr);
57c594
+
57c594
+  auto params = MakeParams();
57c594
+  ASSERT_NE(nullptr, params);
57c594
+
57c594
+  ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
57c594
+  ASSERT_TRUE(slot);
57c594
+
57c594
+  SECKEYPublicKey* pub_tmp;
57c594
+  ScopedSECKEYPrivateKey priv_tmp(PK11_GenerateKeyPair(
57c594
+      slot.get(), mech_, params->get(), &pub_tmp, PR_FALSE, PR_TRUE, nullptr));
57c594
+  ASSERT_NE(nullptr, priv_tmp) << "PK11_GenerateKeyPair failed: "
57c594
+                               << PORT_ErrorToName(PORT_GetError());
57c594
+  ASSERT_NE(nullptr, pub_tmp);
57c594
+
57c594
+  priv_key->swap(priv_tmp);
57c594
+  pub_key->reset(pub_tmp);
57c594
+}
57c594
+
57c594
+class RsaParamHolder : public ParamHolder {
57c594
+ public:
57c594
+  RsaParamHolder() : params_({1024, 0x010001}) {}
57c594
+  ~RsaParamHolder() = default;
57c594
+
57c594
+  void* get() override { return &params_; }
57c594
+
57c594
+ private:
57c594
+  PK11RSAGenParams params_;
57c594
+};
57c594
+
57c594
+class PqgParamHolder : public ParamHolder {
57c594
+ public:
57c594
+  PqgParamHolder(PQGParams* params) : params_(params) {}
57c594
+  ~PqgParamHolder() = default;
57c594
+
57c594
+  void* get() override { return params_.get(); }
57c594
+
57c594
+ private:
57c594
+  ScopedPQGParams params_;
57c594
+};
57c594
+
57c594
+class DhParamHolder : public PqgParamHolder {
57c594
+ public:
57c594
+  DhParamHolder(PQGParams* params)
57c594
+      : PqgParamHolder(params),
57c594
+        params_({nullptr, params->prime, params->base}) {}
57c594
+  ~DhParamHolder() = default;
57c594
+
57c594
+  void* get() override { return &params_; }
57c594
+
57c594
+ private:
57c594
+  SECKEYDHParams params_;
57c594
+};
57c594
+
57c594
+class EcParamHolder : public ParamHolder {
57c594
+ public:
57c594
+  EcParamHolder(SECOidTag curve_oid) {
57c594
+    SECOidData* curve = SECOID_FindOIDByTag(curve_oid);
57c594
+    EXPECT_NE(nullptr, curve);
57c594
+
57c594
+    size_t plen = curve->oid.len + 2;
57c594
+    extra_.reset(new uint8_t[plen]);
57c594
+    extra_[0] = SEC_ASN1_OBJECT_ID;
57c594
+    extra_[1] = static_cast<uint8_t>(curve->oid.len);
57c594
+    memcpy(&extra_[2], curve->oid.data, curve->oid.len);
57c594
+
57c594
+    ec_params_ = {siBuffer, extra_.get(), static_cast<unsigned int>(plen)};
57c594
+  }
57c594
+  ~EcParamHolder() = default;
57c594
+
57c594
+  void* get() override { return &ec_params_; }
57c594
+
57c594
+ private:
57c594
+  SECKEYECParams ec_params_;
57c594
+  std::unique_ptr<uint8_t[]> extra_;
57c594
+};
57c594
+
57c594
+std::unique_ptr<ParamHolder> Pkcs11KeyPairGenerator::MakeParams() const {
57c594
+  switch (mech_) {
57c594
+    case CKM_RSA_PKCS_KEY_PAIR_GEN:
57c594
+      std::cerr << "Generate RSA pair" << std::endl;
57c594
+      return std::unique_ptr<ParamHolder>(new RsaParamHolder());
57c594
+
57c594
+    case CKM_DSA_KEY_PAIR_GEN:
57c594
+    case CKM_DH_PKCS_KEY_PAIR_GEN: {
57c594
+      PQGParams* pqg_params = nullptr;
57c594
+      PQGVerify* pqg_verify = nullptr;
57c594
+      const unsigned int key_size = 1024;
57c594
+      SECStatus rv = PK11_PQG_ParamGenV2(key_size, 0, key_size / 16,
57c594
+                                         &pqg_params, &pqg_verify);
57c594
+      if (rv != SECSuccess) {
57c594
+        ADD_FAILURE() << "PK11_PQG_ParamGenV2 failed";
57c594
+        return nullptr;
57c594
+      }
57c594
+      EXPECT_NE(nullptr, pqg_verify);
57c594
+      EXPECT_NE(nullptr, pqg_params);
57c594
+      PK11_PQG_DestroyVerify(pqg_verify);
57c594
+      if (mech_ == CKM_DSA_KEY_PAIR_GEN) {
57c594
+        std::cerr << "Generate DSA pair" << std::endl;
57c594
+        return std::unique_ptr<ParamHolder>(new PqgParamHolder(pqg_params));
57c594
+      }
57c594
+      std::cerr << "Generate DH pair" << std::endl;
57c594
+      return std::unique_ptr<ParamHolder>(new DhParamHolder(pqg_params));
57c594
+    }
57c594
+
57c594
+    case CKM_EC_KEY_PAIR_GEN:
57c594
+      std::cerr << "Generate EC pair on " << curve_ << std::endl;
57c594
+      return std::unique_ptr<ParamHolder>(new EcParamHolder(curve_));
57c594
+
57c594
+    default:
57c594
+      ADD_FAILURE() << "unknown OID " << mech_;
57c594
+  }
57c594
+  return nullptr;
57c594
+}
57c594
+
57c594
+}  // namespace nss_test
57c594
diff -up ./gtests/pk11_gtest/pk11_keygen.h.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_keygen.h
57c594
--- ./gtests/pk11_gtest/pk11_keygen.h.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.828832617 -0800
57c594
+++ ./gtests/pk11_gtest/pk11_keygen.h	2019-12-05 10:56:41.828832617 -0800
57c594
@@ -0,0 +1,34 @@
57c594
+/* This Source Code Form is subject to the terms of the Mozilla Public
57c594
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
57c594
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
57c594
+
57c594
+#include "nss.h"
57c594
+#include "secoid.h"
57c594
+
57c594
+#include "nss_scoped_ptrs.h"
57c594
+
57c594
+namespace nss_test {
57c594
+
57c594
+class ParamHolder;
57c594
+
57c594
+class Pkcs11KeyPairGenerator {
57c594
+ public:
57c594
+  Pkcs11KeyPairGenerator(CK_MECHANISM_TYPE mech, SECOidTag curve_oid)
57c594
+      : mech_(mech), curve_(curve_oid) {}
57c594
+  Pkcs11KeyPairGenerator(CK_MECHANISM_TYPE mech)
57c594
+      : Pkcs11KeyPairGenerator(mech, SEC_OID_UNKNOWN) {}
57c594
+
57c594
+  CK_MECHANISM_TYPE mechanism() const { return mech_; }
57c594
+  SECOidTag curve() const { return curve_; }
57c594
+
57c594
+  void GenerateKey(ScopedSECKEYPrivateKey* priv_key,
57c594
+                   ScopedSECKEYPublicKey* pub_key) const;
57c594
+
57c594
+ private:
57c594
+  std::unique_ptr<ParamHolder> MakeParams() const;
57c594
+
57c594
+  CK_MECHANISM_TYPE mech_;
57c594
+  SECOidTag curve_;
57c594
+};
57c594
+
57c594
+}  // namespace nss_test
57c594
diff -up ./gtests/pk11_gtest/pk11_key_unittest.cc.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_key_unittest.cc
57c594
--- ./gtests/pk11_gtest/pk11_key_unittest.cc.handle-malformed-ecdh-gtests	2019-12-05 10:56:41.828832617 -0800
57c594
+++ ./gtests/pk11_gtest/pk11_key_unittest.cc	2019-12-05 10:56:41.828832617 -0800
57c594
@@ -0,0 +1,80 @@
57c594
+/* This Source Code Form is subject to the terms of the Mozilla Public
57c594
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
57c594
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
57c594
+
57c594
+#include <memory>
57c594
+#include "nss.h"
57c594
+#include "pk11pub.h"
57c594
+#include "pk11pqg.h"
57c594
+#include "prerror.h"
57c594
+#include "secoid.h"
57c594
+
57c594
+#include "gtest/gtest.h"
57c594
+#include "nss_scoped_ptrs.h"
57c594
+#include "pk11_keygen.h"
57c594
+
57c594
+namespace nss_test {
57c594
+
57c594
+class Pkcs11NullKeyTestBase : public ::testing::Test {
57c594
+ protected:
57c594
+  // This constructs a key pair, then erases the public value from the public
57c594
+  // key.  NSS should reject this.
57c594
+  void Test(const Pkcs11KeyPairGenerator& generator,
57c594
+            CK_MECHANISM_TYPE dh_mech) {
57c594
+    ScopedSECKEYPrivateKey priv;
57c594
+    ScopedSECKEYPublicKey pub;
57c594
+    generator.GenerateKey(&priv, &pub;;
57c594
+    ASSERT_TRUE(priv);
57c594
+
57c594
+    // These don't leak because they are allocated to the arena associated with
57c594
+    // the public key.
57c594
+    SECItem* pub_val = nullptr;
57c594
+    switch (SECKEY_GetPublicKeyType(pub.get())) {
57c594
+      case rsaKey:
57c594
+        pub_val = &pub->u.rsa.modulus;
57c594
+        break;
57c594
+
57c594
+      case dsaKey:
57c594
+        pub_val = &pub->u.dsa.publicValue;
57c594
+        break;
57c594
+
57c594
+      case dhKey:
57c594
+        pub_val = &pub->u.dh.publicValue;
57c594
+        break;
57c594
+
57c594
+      case ecKey:
57c594
+        pub_val = &pub->u.ec.publicValue;
57c594
+        break;
57c594
+
57c594
+      default:
57c594
+        FAIL() << "Unknown key type " << SECKEY_GetPublicKeyType(pub.get());
57c594
+    }
57c594
+    pub_val->data = nullptr;
57c594
+    pub_val->len = 0;
57c594
+
57c594
+    ScopedPK11SymKey symKey(PK11_PubDeriveWithKDF(
57c594
+        priv.get(), pub.get(), false, nullptr, nullptr, dh_mech,
57c594
+        CKM_SHA512_HMAC, CKA_DERIVE, 0, CKD_NULL, nullptr, nullptr));
57c594
+    ASSERT_FALSE(symKey);
57c594
+  }
57c594
+};
57c594
+
57c594
+class Pkcs11DhNullKeyTest : public Pkcs11NullKeyTestBase {};
57c594
+TEST_F(Pkcs11DhNullKeyTest, UseNullPublicValue) {
57c594
+  Test(Pkcs11KeyPairGenerator(CKM_DH_PKCS_KEY_PAIR_GEN), CKM_DH_PKCS_DERIVE);
57c594
+}
57c594
+
57c594
+class Pkcs11EcdhNullKeyTest : public Pkcs11NullKeyTestBase,
57c594
+                              public ::testing::WithParamInterface<SECOidTag> {
57c594
+};
57c594
+TEST_P(Pkcs11EcdhNullKeyTest, UseNullPublicValue) {
57c594
+  Test(Pkcs11KeyPairGenerator(CKM_EC_KEY_PAIR_GEN, GetParam()),
57c594
+       CKM_ECDH1_DERIVE);
57c594
+}
57c594
+INSTANTIATE_TEST_CASE_P(Pkcs11EcdhNullKeyTest, Pkcs11EcdhNullKeyTest,
57c594
+                        ::testing::Values(SEC_OID_SECG_EC_SECP256R1,
57c594
+                                          SEC_OID_SECG_EC_SECP384R1,
57c594
+                                          SEC_OID_SECG_EC_SECP521R1,
57c594
+                                          SEC_OID_CURVE25519));
57c594
+
57c594
+}  // namespace nss_test
57c594
diff --git ./gtests/pk11_gtest/pk11_curve25519_unittest.cc.handle-malformed-ecdh-gtests ./gtests/pk11_gtest/pk11_curve25519_unittest.cc
57c594
--- ./gtests/pk11_gtest/pk11_curve25519_unittest.cc.handle-malformed-ecdh-gtests
57c594
+++ ./gtests/pk11_gtest/pk11_curve25519_unittest.cc
57c594
@@ -40,6 +40,9 @@
57c594
 
57c594
     ScopedCERTSubjectPublicKeyInfo certSpki(
57c594
         SECKEY_DecodeDERSubjectPublicKeyInfo(&spkiItem));
57c594
+    if (!expect_success && !certSpki) {
57c594
+      return;
57c594
+    }
57c594
     ASSERT_TRUE(certSpki);
57c594
 
57c594
     ScopedSECKEYPublicKey pubKey(SECKEY_ExtractPublicKey(certSpki.get()));
57c594
57c594
diff -up ./gtests/ssl_gtest/tls_connect.cc.addtime ./gtests/ssl_gtest/tls_connect.cc
57c594
--- ./gtests/ssl_gtest/tls_connect.cc.addtime	2019-12-06 09:02:39.006583359 -0800
57c594
+++ ./gtests/ssl_gtest/tls_connect.cc	2019-12-06 09:02:54.120745545 -0800
57c594
@@ -292,7 +292,7 @@ void TlsConnectTestBase::Handshake() {
57c594
 
57c594
   ASSERT_TRUE_WAIT((client_->state() != TlsAgent::STATE_CONNECTING) &&
57c594
                        (server_->state() != TlsAgent::STATE_CONNECTING),
57c594
-                   5000);
57c594
+                   10000);
57c594
 }
57c594
 
57c594
 void TlsConnectTestBase::EnableExtendedMasterSecret() {
57c594