Make EVP_PKEY_dup_ref work for nullptr

It now returns nullptr rather than crashing.

Change-Id: Iab79f4c10799024a42bd0e25045aa83c6a6a6964
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/102507
Presubmit-BoringSSL-Verified: boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com <boringssl-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Benjamin <davidben@google.com>
Auto-Submit: Lily Chen <chlily@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/evp/evp.cc b/crypto/evp/evp.cc
index 5a716b0..f99ee15 100644
--- a/crypto/evp/evp.cc
+++ b/crypto/evp/evp.cc
@@ -60,6 +60,9 @@
 }
 
 EVP_PKEY *EVP_PKEY_dup_ref(const EVP_PKEY *pkey) {
+  if (pkey == nullptr) {
+    return nullptr;
+  }
   auto pkey_ref = const_cast<EVP_PKEY *>(pkey);
   // We know that this call always returns one.
   EVP_PKEY_up_ref(pkey_ref);
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
index 8f3234f..8a767ca 100644
--- a/include/openssl/evp.h
+++ b/include/openssl/evp.h
@@ -56,8 +56,9 @@
 // concurrently.
 OPENSSL_EXPORT int EVP_PKEY_up_ref(EVP_PKEY *pkey);
 
-// EVP_PKEY_dup_ref increments the reference count of `pkey` and returns `pkey`.
-// The caller must call `EVP_PKEY_free` on the result to release the reference.
+// EVP_PKEY_dup_ref increments the reference count of `pkey`, if non-null, and
+// returns `pkey`. The caller must call `EVP_PKEY_free` on the result to
+// release the reference.
 //
 // WARNING: Although the result is non-const for use with `EVP_PKEY_free`, it is
 // still shared with other parts of the application that share the same object.