conf: fix getting keys from the default section.

The comments say that this should work, but it didn't. OpenSSL doesn't
have any documentation about this but from looking at the code it works
there. (Along with things like magic sections called “ENV” to get
environment variables, sigh.)

Change-Id: I538fbad57e6af37eee739de6d2643f554bfc5c79
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48386
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
diff --git a/crypto/conf/conf.c b/crypto/conf/conf.c
index 9e7dfe5..c1e4e96 100644
--- a/crypto/conf/conf.c
+++ b/crypto/conf/conf.c
@@ -77,6 +77,8 @@
   LHASH_OF(CONF_VALUE) *data;
 };
 
+static const char kDefaultSectionName[] = "default";
+
 // The maximum length we can grow a value to after variable expansion. 64k
 // should be more than enough for all reasonable uses.
 #define MAX_CONF_VALUE_LENGTH 65536
@@ -395,6 +397,10 @@
                              const char *name) {
   CONF_VALUE template, *value;
 
+  if (section == NULL) {
+    section = kDefaultSectionName;
+  }
+
   OPENSSL_memset(&template, 0, sizeof(template));
   template.section = (char *) section;
   template.name = (char *) name;
@@ -543,7 +549,7 @@
     goto err;
   }
 
-  section = OPENSSL_strdup("default");
+  section = OPENSSL_strdup(kDefaultSectionName);
   if (section == NULL) {
     OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
     goto err;
diff --git a/crypto/conf/conf_test.cc b/crypto/conf/conf_test.cc
index 25f4b4e..65938e1 100644
--- a/crypto/conf/conf_test.cc
+++ b/crypto/conf/conf_test.cc
@@ -38,8 +38,7 @@
   ASSERT_TRUE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
   EXPECT_TRUE(NCONF_get_section(conf.get(), "section_name"));
   EXPECT_FALSE(NCONF_get_section(conf.get(), "other_section"));
-  // Doesn't work but should.
-  // EXPECT_STREQ(NCONF_get_string(conf.get(), nullptr, "key"), "value");
+  EXPECT_STREQ(NCONF_get_string(conf.get(), nullptr, "key"), "value");
   EXPECT_STREQ(NCONF_get_string(conf.get(), "section_name", "key"), "value2");
   EXPECT_STREQ(NCONF_get_string(conf.get(), "other_section", "key"), nullptr);
 }