update main-with-bazel from master branch

This commit is contained in:
BoringSSL Robot
2023-05-24 18:12:11 +00:00
7 changed files with 916 additions and 760 deletions
+553 -551
View File
File diff suppressed because it is too large Load Diff
+28 -185
View File
@@ -68,21 +68,10 @@
#include "conf_def.h"
#include "internal.h"
#include "../internal.h"
#include "../lhash/internal.h"
DEFINE_LHASH_OF(CONF_VALUE)
struct conf_st {
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
static uint32_t conf_value_hash(const CONF_VALUE *v) {
const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
@@ -139,24 +128,23 @@ CONF_VALUE *CONF_VALUE_new(void) {
}
static void value_free_contents(CONF_VALUE *value) {
if (value->section) {
OPENSSL_free(value->section);
}
OPENSSL_free(value->section);
if (value->name) {
OPENSSL_free(value->name);
if (value->value) {
OPENSSL_free(value->value);
}
OPENSSL_free(value->value);
} else {
if (value->value) {
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
}
// TODO(davidben): When |value->name| is NULL, |CONF_VALUE| is actually an
// entirely different structure. This is fragile and confusing. Make a
// proper |CONF_SECTION| type that doesn't require this.
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE) *)value->value);
}
}
static void value_free(CONF_VALUE *value) {
value_free_contents(value);
OPENSSL_free(value);
if (value != NULL) {
value_free_contents(value);
OPENSSL_free(value);
}
}
static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
@@ -192,28 +180,21 @@ static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
goto err;
}
if (old_value) {
value_free(old_value);
}
value_free(old_value);
ok = 1;
err:
if (!ok) {
if (sk != NULL) {
sk_CONF_VALUE_free(sk);
}
if (v != NULL) {
OPENSSL_free(v);
}
sk_CONF_VALUE_free(sk);
OPENSSL_free(v);
v = NULL;
}
return v;
}
static int str_copy(CONF *conf, char *section, char **pto, char *from) {
int q, r, rr = 0, to = 0, len = 0;
char *s, *e, *rp, *rrp, *np, *cp, v;
const char *p;
int q, to = 0, len = 0;
char v;
BUF_MEM *buf;
buf = BUF_MEM_new();
@@ -242,22 +223,6 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
if (*from == q) {
from++;
}
} else if (IS_DQUOTE(conf, *from)) {
q = *from;
from++;
while (!IS_EOF(conf, *from)) {
if (*from == q) {
if (*(from + 1) == q) {
from++;
} else {
break;
}
}
buf->data[to++] = *(from++);
}
if (*from == q) {
from++;
}
} else if (IS_ESC(conf, *from)) {
from++;
v = *(from++);
@@ -276,102 +241,23 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
} else if (IS_EOF(conf, *from)) {
break;
} else if (*from == '$') {
// try to expand it
rrp = NULL;
s = &(from[1]);
if (*s == '{') {
q = '}';
} else if (*s == '(') {
q = ')';
} else {
q = 0;
}
if (q) {
s++;
}
cp = section;
e = np = s;
while (IS_ALPHA_NUMERIC(conf, *e)) {
e++;
}
if (e[0] == ':' && e[1] == ':') {
cp = np;
rrp = e;
rr = *e;
*rrp = '\0';
e += 2;
np = e;
while (IS_ALPHA_NUMERIC(conf, *e)) {
e++;
}
}
r = *e;
*e = '\0';
rp = e;
if (q) {
if (r != q) {
OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
goto err;
}
e++;
}
// So at this point we have
// np which is the start of the name string which is
// '\0' terminated.
// cp which is the start of the section string which is
// '\0' terminated.
// e is the 'next point after'.
// r and rr are the chars replaced by the '\0'
// rp and rrp is where 'r' and 'rr' came from.
p = NCONF_get_string(conf, cp, np);
if (rrp != NULL) {
*rrp = rr;
}
*rp = r;
if (p == NULL) {
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
goto err;
}
size_t newsize = strlen(p) + buf->length - (e - from);
if (newsize > MAX_CONF_VALUE_LENGTH) {
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
goto err;
}
if (!BUF_MEM_grow_clean(buf, newsize)) {
goto err;
}
while (*p) {
buf->data[to++] = *(p++);
}
/* Since we change the pointer 'from', we also have
to change the perceived length of the string it
points at. /RL */
len -= e - from;
from = e;
/* In case there were no braces or parenthesis around
the variable reference, we have to put back the
character that was replaced with a '\0'. /RL */
*rp = r;
// Historically, $foo would expand to a previously-parsed value. This
// feature has been removed as it was unused and is a DoS vector.
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED);
goto err;
} else {
buf->data[to++] = *(from++);
}
}
buf->data[to] = '\0';
if (*pto != NULL) {
OPENSSL_free(*pto);
}
OPENSSL_free(*pto);
*pto = buf->data;
OPENSSL_free(buf);
return 1;
err:
if (buf != NULL) {
BUF_MEM_free(buf);
}
BUF_MEM_free(buf);
return 0;
}
@@ -472,33 +358,8 @@ static char *scan_quote(CONF *conf, char *p) {
return p;
}
static char *scan_dquote(CONF *conf, char *p) {
int q = *p;
p++;
while (!(IS_EOF(conf, *p))) {
if (*p == q) {
if (*(p + 1) == q) {
p++;
} else {
break;
}
}
p++;
}
if (*p == q) {
p++;
}
return p;
}
static void clear_comments(CONF *conf, char *p) {
for (;;) {
if (IS_FCOMMENT(conf, *p)) {
*p = '\0';
return;
}
if (!IS_WS(conf, *p)) {
break;
}
@@ -510,10 +371,6 @@ static void clear_comments(CONF *conf, char *p) {
*p = '\0';
return;
}
if (IS_DQUOTE(conf, *p)) {
p = scan_dquote(conf, p);
continue;
}
if (IS_QUOTE(conf, *p)) {
p = scan_quote(conf, p);
continue;
@@ -707,21 +564,13 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
v = NULL;
}
}
if (buff != NULL) {
BUF_MEM_free(buff);
}
if (section != NULL) {
OPENSSL_free(section);
}
BUF_MEM_free(buff);
OPENSSL_free(section);
return 1;
err:
if (buff != NULL) {
BUF_MEM_free(buff);
}
if (section != NULL) {
OPENSSL_free(section);
}
BUF_MEM_free(buff);
OPENSSL_free(section);
if (out_error_line != NULL) {
*out_error_line = eline;
}
@@ -729,15 +578,9 @@ err:
ERR_add_error_data(2, "line ", btmp);
if (v != NULL) {
if (v->name != NULL) {
OPENSSL_free(v->name);
}
if (v->value != NULL) {
OPENSSL_free(v->value);
}
if (v != NULL) {
OPENSSL_free(v);
}
OPENSSL_free(v->name);
OPENSSL_free(v->value);
OPENSSL_free(v);
}
return 0;
}
+3 -7
View File
@@ -55,8 +55,9 @@
* [including the GNU Public Licence.]
*/
/* THIS FILE WAS AUTOMAGICALLY GENERATED!
Please modify and use keysets.pl to regenerate it. */
// This file was historically generated by keysets.pl in OpenSSL.
//
// TODO(davidben): Replace it with something more readable.
#define CONF_NUMBER 1
#define CONF_UPPER 2
@@ -66,9 +67,7 @@
#define CONF_WS 16
#define CONF_ESC 32
#define CONF_QUOTE 64
#define CONF_DQUOTE 1024
#define CONF_COMMENT 128
#define CONF_FCOMMENT 2048
#define CONF_EOF 8
#define CONF_HIGHBIT 4096
#define CONF_ALPHA (CONF_UPPER|CONF_LOWER)
@@ -78,7 +77,6 @@
#define KEYTYPES(c) CONF_type_default
#define IS_COMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_COMMENT)
#define IS_FCOMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_FCOMMENT)
#define IS_EOF(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_EOF)
#define IS_ESC(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_ESC)
#define IS_NUMBER(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_NUMBER)
@@ -87,8 +85,6 @@
#define IS_ALPHA_NUMERIC_PUNCT(c,a) \
(KEYTYPES(c)[(a)&0xff]&CONF_ALPHA_NUMERIC_PUNCT)
#define IS_QUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_QUOTE)
#define IS_DQUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_DQUOTE)
#define IS_HIGHBIT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_HIGHBIT)
static const unsigned short CONF_type_default[256]={
0x0008,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
+318 -16
View File
@@ -12,8 +12,10 @@
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <openssl/bio.h>
#include <openssl/conf.h>
@@ -23,29 +25,329 @@
#include "internal.h"
TEST(ConfTest, Parse) {
// Check that basic parsing works. (We strongly recommend that people don't
// use the [N]CONF functions.)
// A |CONF| is an unordered list of sections, where each section contains an
// ordered list of (name, value) pairs.
using ConfModel =
std::map<std::string, std::vector<std::pair<std::string, std::string>>>;
static const char kConf[] = R"(
# Comment
static void ExpectConfEquals(const CONF *conf, const ConfModel &model) {
// There is always a default section, even if empty. This is an easy mistake
// to make in test data, so test for it.
EXPECT_NE(model.find("default"), model.end())
<< "Model does not have a default section";
size_t total_values = 0;
for (const auto &pair : model) {
const std::string &section = pair.first;
SCOPED_TRACE(section);
const STACK_OF(CONF_VALUE) *values =
NCONF_get_section(conf, section.c_str());
ASSERT_TRUE(values);
total_values += pair.second.size();
EXPECT_EQ(sk_CONF_VALUE_num(values), pair.second.size());
// If the lengths do not match, still compare up to the smaller of the two,
// to aid debugging.
size_t min_len = std::min(sk_CONF_VALUE_num(values), pair.second.size());
for (size_t i = 0; i < min_len; i++) {
SCOPED_TRACE(i);
const std::string &name = pair.second[i].first;
const std::string &value = pair.second[i].second;
const CONF_VALUE *v = sk_CONF_VALUE_value(values, i);
EXPECT_EQ(v->section, section);
EXPECT_EQ(v->name, name);
EXPECT_EQ(v->value, value);
const char *str = NCONF_get_string(conf, section.c_str(), name.c_str());
ASSERT_NE(str, nullptr);
EXPECT_EQ(str, value);
if (section == "default") {
// nullptr is interpreted as the default section.
str = NCONF_get_string(conf, nullptr, name.c_str());
ASSERT_NE(str, nullptr);
EXPECT_EQ(str, value);
}
}
}
// Unrecognized sections must return nullptr.
EXPECT_EQ(NCONF_get_section(conf, "must_not_appear_in_tests"), nullptr);
EXPECT_EQ(NCONF_get_string(conf, "must_not_appear_in_tests",
"must_not_appear_in_tests"),
nullptr);
if (!model.empty()) {
// Valid section, invalid name.
EXPECT_EQ(NCONF_get_string(conf, model.begin()->first.c_str(),
"must_not_appear_in_tests"),
nullptr);
if (!model.begin()->second.empty()) {
// Invalid section, valid name.
EXPECT_EQ(NCONF_get_string(conf, "must_not_appear_in_tests",
model.begin()->second.front().first.c_str()),
nullptr);
}
}
// There should not be any other values in |conf|. |conf| currently stores
// both sections and values in the same map.
EXPECT_EQ(lh_CONF_VALUE_num_items(conf->data), total_values + model.size());
}
TEST(ConfTest, Parse) {
const struct {
std::string in;
ConfModel model;
} kTests[] = {
// Test basic parsing.
{
R"(# Comment
key=value
[section_name]
key=value2
)";
)",
{
{"default", {{"key", "value"}}},
{"section_name", {{"key", "value2"}}},
},
},
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kConf, sizeof(kConf) - 1));
ASSERT_TRUE(bio);
bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
ASSERT_TRUE(conf);
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"));
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);
// If a section is listed multiple times, keys add to the existing one.
{
R"(key1 = value1
[section1]
key2 = value2
[section2]
key3 = value3
[default]
key4 = value4
[section1]
key5 = value5
)",
{
{"default", {{"key1", "value1"}, {"key4", "value4"}}},
{"section1", {{"key2", "value2"}, {"key5", "value5"}}},
{"section2", {{"key3", "value3"}}},
},
},
// Although the CONF parser internally uses a buffer size of 512 bytes to
// read one line, it detects truncation and is able to parse long lines.
{
std::string(1000, 'a') + " = " + std::string(1000, 'b') + "\n",
{
{"default", {{std::string(1000, 'a'), std::string(1000, 'b')}}},
},
},
// Trailing backslashes are line continations.
{
"key=\\\nvalue\nkey2=foo\\\nbar=baz",
{
{"default", {{"key", "value"}, {"key2", "foobar=baz"}}},
},
},
// To be a line continuation, it must be at the end of the line.
{
"key=\\\nvalue\nkey2=foo\\ \nbar=baz",
{
{"default", {{"key", "value"}, {"key2", "foo"}, {"bar", "baz"}}},
},
},
// A line continuation without any following line is ignored.
{
"key=value\\",
{
{"default", {{"key", "value"}}},
},
},
// Values may have embedded whitespace, but leading and trailing
// whitespace is dropped.
{
"key = \t foo \t\t\tbar \t ",
{
{"default", {{"key", "foo \t\t\tbar"}}},
},
},
// Empty sections still end up in the file.
{
"[section1]\n[section2]\n[section3]\n",
{
{"default", {}},
{"section1", {}},
{"section2", {}},
{"section3", {}},
},
},
// Section names can contain spaces and punctuation.
{
"[This! Is. A? Section;]\nkey = value",
{
{"default", {}},
{"This! Is. A? Section;", {{"key", "value"}}},
},
},
// Trailing data after a section line is ignored.
{
"[section] key = value\nkey2 = value2\n",
{
{"default", {}},
{"section", {{"key2", "value2"}}},
},
},
// Comments may appear within a line. Escapes and quotes, however,
// suppress the comment character.
{
R"(
key1 = # comment
key2 = "# not a comment"
key3 = '# not a comment'
key4 = `# not a comment`
key5 = \# not a comment
)",
{
{"default",
{
{"key1", ""},
{"key2", "# not a comment"},
{"key3", "# not a comment"},
{"key4", "# not a comment"},
{"key5", "# not a comment"},
}},
},
},
// Quotes may appear in the middle of a string. Inside quotes, escape
// sequences like \n are not evaluated. \X always evaluates to X.
{
R"(
key1 = mix "of" 'different' `quotes`
key2 = "`'"
key3 = "\r\n\b\t\""
key4 = '\r\n\b\t\''
key5 = `\r\n\b\t\``
)",
{
{"default",
{
{"key1", "mix of different quotes"},
{"key2", "`'"},
{"key3", "rnbt\""},
{"key4", "rnbt'"},
{"key5", "rnbt`"},
}},
},
},
// Outside quotes, escape sequences like \n are evaluated. Unknown escapes
// turn into the character.
{
R"(
key = \r\n\b\t\"\'\`\z
)",
{
{"default",
{
{"key", "\r\n\b\t\"'`z"},
}},
},
},
// Escapes (but not quoting) work inside section names.
{
"[section\\ name]\nkey = value\n",
{
{"default", {}},
{"section name", {{"key", "value"}}},
},
},
// Escapes (but not quoting) are skipped over in key names, but they are
// left unevaluated. This is probably a bug.
{
"key\\ name = value\n",
{
{"default", {{"key\\ name", "value"}}},
},
},
// Keys can specify sections explicitly with ::.
{
R"(
[section1]
default::key1 = value1
section1::key2 = value2
section2::key3 = value3
section1::key4 = value4
section2::key5 = value5
default::key6 = value6
key7 = value7 # section1
)",
{
{"default", {{"key1", "value1"}, {"key6", "value6"}}},
{"section1",
{{"key2", "value2"}, {"key4", "value4"}, {"key7", "value7"}}},
{"section2", {{"key3", "value3"}, {"key5", "value5"}}},
},
},
// Punctuation is allowed in key names.
{
"key.1 = value\n",
{
{"default", {{"key.1", "value"}}},
},
},
};
for (const auto &t : kTests) {
SCOPED_TRACE(t.in);
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.in.data(), t.in.size()));
ASSERT_TRUE(bio);
bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
ASSERT_TRUE(conf);
ASSERT_TRUE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
ExpectConfEquals(conf.get(), t.model);
}
const char *kInvalidTests[] = {
// Missing equals sign.
"key",
// Unterminated section heading.
"[section",
// Section names can only contain alphanumeric characters, punctuation,
// and escapes. Quotes are not punctuation.
"[\"section\"]",
// Keys can only contain alphanumeric characters, punctuaion, and escapes.
"key name = value",
"\"key\" = value",
// Variable references have been removed.
"key1 = value1\nkey2 = $key1",
};
for (const auto &t : kInvalidTests) {
SCOPED_TRACE(t);
bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t, strlen(t)));
ASSERT_TRUE(bio);
bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
ASSERT_TRUE(conf);
EXPECT_FALSE(NCONF_load_bio(conf.get(), bio.get(), nullptr));
}
}
TEST(ConfTest, ParseList) {
+8
View File
@@ -17,11 +17,19 @@
#include <openssl/base.h>
#include "../lhash/internal.h"
#if defined(__cplusplus)
extern "C" {
#endif
DEFINE_LHASH_OF(CONF_VALUE)
struct conf_st {
LHASH_OF(CONF_VALUE) *data;
};
// CONF_VALUE_new returns a freshly allocated and zeroed |CONF_VALUE|.
CONF_VALUE *CONF_VALUE_new(void);
+1
View File
@@ -3,5 +3,6 @@ CONF,101,MISSING_CLOSE_SQUARE_BRACKET
CONF,102,MISSING_EQUAL_SIGN
CONF,103,NO_CLOSE_BRACE
CONF,104,UNABLE_TO_CREATE_NEW_SECTION
CONF,107,VARIABLE_EXPANSION_NOT_SUPPORTED
CONF,106,VARIABLE_EXPANSION_TOO_LONG
CONF,105,VARIABLE_HAS_NO_VALUE
+5 -1
View File
@@ -77,7 +77,10 @@ extern "C" {
// [section_name]
// key2=value2
//
// Config files are represented by a |CONF|.
// Config files are represented by a |CONF|. Use of this module is strongly
// discouraged. It is a remnant of the OpenSSL command-line tool. Parsing an
// untrusted input as a config file risks string injection and denial of service
// vulnerabilities.
struct conf_value_st {
char *section;
@@ -166,5 +169,6 @@ BSSL_NAMESPACE_END
#define CONF_R_UNABLE_TO_CREATE_NEW_SECTION 104
#define CONF_R_VARIABLE_HAS_NO_VALUE 105
#define CONF_R_VARIABLE_EXPANSION_TOO_LONG 106
#define CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED 107
#endif // OPENSSL_HEADER_THREAD_H