Skip to content

Commit

Permalink
Add RFC2253 formatting of RDNSequence
Browse files Browse the repository at this point in the history
BUG=

Review URL: https://codereview.chromium.org/1720203002

Cr-Commit-Position: refs/heads/master@{#379090}
  • Loading branch information
svaldez authored and Commit bot committed Mar 3, 2016
1 parent bc45d7d commit 983b88f
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 26 deletions.
130 changes: 124 additions & 6 deletions net/cert/internal/parse_name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "net/cert/internal/parse_name.h"

#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -62,6 +63,39 @@ bool ConvertUniversalStringValue(const der::Input& in, std::string* out) {
return true;
}

std::string OidToString(const uint8_t* data, size_t len) {
std::string out;
size_t index = 0;
while (index < len) {
uint64_t value = 0;
while ((data[index] & 0x80) == 0x80 && index < len) {
value = value << 7 | (data[index] & 0x7F);
index += 1;
}
if (index >= len)
return std::string();
value = value << 7 | (data[index] & 0x7F);
index += 1;

if (out.empty()) {
uint8_t first = 0;
if (value < 40) {
first = 0;
} else if (value < 80) {
first = 1;
value -= 40;
} else {
first = 2;
value -= 80;
}
out = base::UintToString(first);
}
out += "." + base::UintToString(value);
}

return out;
}

} // namespace

der::Input TypeCommonNameOid() {
Expand Down Expand Up @@ -160,7 +194,71 @@ bool X509NameAttribute::ValueAsStringUnsafe(std::string* out) const {
}
}

bool ReadRdn(der::Parser* parser, std::vector<X509NameAttribute>* out) {
bool X509NameAttribute::AsRFC2253String(std::string* out) const {
std::string type_string;
std::string value_string;
if (type == TypeCommonNameOid()) {
type_string = "CN";
} else if (type == TypeSurnameOid()) {
type_string = "SN";
} else if (type == TypeCountryNameOid()) {
type_string = "C";
} else if (type == TypeLocalityNameOid()) {
type_string = "L";
} else if (type == TypeStateOrProvinceNameOid()) {
type_string = "ST";
} else if (type == TypeOrganizationNameOid()) {
type_string = "O";
} else if (type == TypeOrganizationUnitNameOid()) {
type_string = "OU";
} else if (type == TypeGivenNameOid()) {
type_string = "GN";
} else {
type_string = OidToString(type.UnsafeData(), type.Length());
if (type_string.empty())
return false;
value_string = "#" + base::HexEncode(value.UnsafeData(), value.Length());
}

if (value_string.empty()) {
std::string unescaped;
if (!ValueAsStringUnsafe(&unescaped))
return false;

bool nonprintable = false;
for (unsigned int i = 0; i < unescaped.length(); ++i) {
unsigned char c = static_cast<unsigned char>(unescaped[i]);
if (i == 0 && c == '#') {
value_string += "\\#";
} else if (i == 0 && c == ' ') {
value_string += "\\ ";
} else if (i == unescaped.length() - 1 && c == ' ') {
value_string += "\\ ";
} else if (c == ',' || c == '+' || c == '"' || c == '\\' || c == '<' ||
c == '>' || c == ';') {
value_string += "\\";
value_string += c;
} else if (c < 32 || c > 126) {
nonprintable = true;
std::string h;
h += c;
value_string += "\\" + base::HexEncode(h.data(), h.length());
} else {
value_string += c;
}
}

// If we have non-printable characters in a TeletexString, we hex encode
// since we don't handle Teletex control codes.
if (nonprintable && value_tag == der::kTeletexString)
value_string = "#" + base::HexEncode(value.UnsafeData(), value.Length());
}

*out = type_string + "=" + value_string;
return true;
}

bool ReadRdn(der::Parser* parser, RelativeDistinguishedName* out) {
while (parser->HasMore()) {
der::Parser attr_type_and_value;
if (!parser->ReadSequence(&attr_type_and_value))
Expand Down Expand Up @@ -189,8 +287,7 @@ bool ReadRdn(der::Parser* parser, std::vector<X509NameAttribute>* out) {
return out->size() != 0;
}

bool ParseName(const der::Input& name_tlv,
std::vector<X509NameAttribute>* out) {
bool ParseName(const der::Input& name_tlv, RDNSequence* out) {
der::Parser name_parser(name_tlv);
der::Parser rdn_sequence_parser;
if (!name_parser.ReadSequence(&rdn_sequence_parser))
Expand All @@ -200,14 +297,35 @@ bool ParseName(const der::Input& name_tlv,
der::Parser rdn_parser;
if (!rdn_sequence_parser.ReadConstructed(der::kSet, &rdn_parser))
return false;
std::vector<X509NameAttribute> type_and_values;
RelativeDistinguishedName type_and_values;
if (!ReadRdn(&rdn_parser, &type_and_values))
return false;
for (const auto& type_and_value : type_and_values) {
out->push_back(type_and_value);
out->push_back(type_and_values);
}

return true;
}

bool ConvertToRFC2253(const RDNSequence& rdn_sequence, std::string* out) {
std::string rdns_string;
size_t size = rdn_sequence.size();
for (size_t i = 0; i < size; ++i) {
RelativeDistinguishedName rdn = rdn_sequence[size - i - 1];
std::string rdn_string;
for (const auto& atv : rdn) {
if (!rdn_string.empty())
rdn_string += "+";
std::string atv_string;
if (!atv.AsRFC2253String(&atv_string))
return false;
rdn_string += atv_string;
}
if (!rdns_string.empty())
rdns_string += ",";
rdns_string += rdn_string;
}

*out = rdns_string;
return true;
}

Expand Down
17 changes: 14 additions & 3 deletions net/cert/internal/parse_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ struct NET_EXPORT X509NameAttribute {
// ASN.1 definition of the value type.
bool ValueAsStringUnsafe(std::string* out) const WARN_UNUSED_RESULT;

// Formats the NameAttribute per RFC2253 into an ASCII string and stores
// the result in |out|, returning whether the conversion was successful.
bool AsRFC2253String(std::string* out) const WARN_UNUSED_RESULT;

der::Input type;
der::Tag value_tag;
der::Input value;
};

typedef std::vector<X509NameAttribute> RelativeDistinguishedName;
typedef std::vector<RelativeDistinguishedName> RDNSequence;

// Parses all the ASN.1 AttributeTypeAndValue elements in |parser| and stores
// each as an AttributeTypeAndValue object in |out|.
//
Expand All @@ -78,14 +85,18 @@ struct NET_EXPORT X509NameAttribute {
// The type of the component AttributeValue is determined by the AttributeType;
// in general it will be a DirectoryString.
NET_EXPORT bool ReadRdn(der::Parser* parser,
std::vector<X509NameAttribute>* out) WARN_UNUSED_RESULT;
RelativeDistinguishedName* out) WARN_UNUSED_RESULT;

// Parses a DER-encoded "Name" as specified by 5280. Returns true on success
// and sets the results in |out|.
NET_EXPORT bool ParseName(const der::Input& name_tlv,
std::vector<X509NameAttribute>* out)
WARN_UNUSED_RESULT;
RDNSequence* out) WARN_UNUSED_RESULT;

// Formats a RDNSequence |rdn_sequence| per RFC2253 as an ASCII string and
// stores the result into |out|, and returns whether the conversion was
// successful.
NET_EXPORT bool ConvertToRFC2253(const RDNSequence& rdn_sequence,
std::string* out) WARN_UNUSED_RESULT;
} // namespace net

#endif // NET_CERT_INTERNAL_PARSE_NAME_H_
133 changes: 119 additions & 14 deletions net/cert/internal/parse_name_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST(ParseNameTest, ConvertInvalidUniversalString) {
TEST(ParseNameTest, EmptyName) {
const uint8_t der[] = {0x30, 0x00};
der::Input rdn(der);
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_TRUE(ParseName(rdn, &atv));
ASSERT_EQ(0u, atv.size());
}
Expand All @@ -83,61 +83,166 @@ TEST(ParseNameTest, ValidName) {
0x03, 0x13, 0x0e, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x43, 0x41};
der::Input rdn(der);
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_TRUE(ParseName(rdn, &atv));
ASSERT_EQ(3u, atv.size());
ASSERT_TRUE(atv[0].type == TypeCountryNameOid());
ASSERT_EQ("US", atv[0].value.AsString());
ASSERT_TRUE(atv[1].type == TypeOrganizationNameOid());
ASSERT_EQ("Google Inc.", atv[1].value.AsString());
ASSERT_TRUE(atv[2].type == TypeCommonNameOid());
ASSERT_EQ("Google Test CA", atv[2].value.AsString());
ASSERT_EQ(1u, atv[0].size());
ASSERT_EQ(TypeCountryNameOid(), atv[0][0].type);
ASSERT_EQ("US", atv[0][0].value.AsString());
ASSERT_EQ(1u, atv[1].size());
ASSERT_EQ(TypeOrganizationNameOid(), atv[1][0].type);
ASSERT_EQ("Google Inc.", atv[1][0].value.AsString());
ASSERT_EQ(1u, atv[2].size());
ASSERT_EQ(TypeCommonNameOid(), atv[2][0].type);
ASSERT_EQ("Google Test CA", atv[2][0].value.AsString());
}

TEST(ParseNameTest, InvalidNameExtraData) {
std::string invalid;
ASSERT_TRUE(
LoadTestData("invalid", "AttributeTypeAndValue", "extradata", &invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, InvalidNameEmpty) {
std::string invalid;
ASSERT_TRUE(
LoadTestData("invalid", "AttributeTypeAndValue", "empty", &invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, InvalidNameBadType) {
std::string invalid;
ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue",
"badAttributeType", &invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, InvalidNameNotSequence) {
std::string invalid;
ASSERT_TRUE(LoadTestData("invalid", "AttributeTypeAndValue", "setNotSequence",
&invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, InvalidNameNotSet) {
std::string invalid;
ASSERT_TRUE(LoadTestData("invalid", "RDN", "sequenceInsteadOfSet", &invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, InvalidNameEmptyRdn) {
std::string invalid;
ASSERT_TRUE(LoadTestData("invalid", "RDN", "empty", &invalid));
std::vector<X509NameAttribute> atv;
RDNSequence atv;
ASSERT_FALSE(ParseName(SequenceValueFromString(&invalid), &atv));
}

TEST(ParseNameTest, RFC2253FormatBasic) {
const uint8_t der[] = {0x30, 0x3b, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
0x04, 0x06, 0x13, 0x02, 0x47, 0x42, 0x31, 0x16, 0x30,
0x14, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0d, 0x49,
0x73, 0x6f, 0x64, 0x65, 0x20, 0x4c, 0x69, 0x6d, 0x69,
0x74, 0x65, 0x64, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
0x55, 0x04, 0x03, 0x13, 0x0b, 0x53, 0x74, 0x65, 0x76,
0x65, 0x20, 0x4b, 0x69, 0x6c, 0x6c, 0x65};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("CN=Steve Kille,O=Isode Limited,C=GB", output);
}

TEST(ParseNameTest, RFC2253FormatMultiRDN) {
const uint8_t der[] = {
0x30, 0x44, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x55, 0x53, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a,
0x13, 0x0b, 0x57, 0x69, 0x64, 0x67, 0x65, 0x74, 0x20, 0x49, 0x6e, 0x63,
0x2e, 0x31, 0x1f, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x05,
0x53, 0x61, 0x6c, 0x65, 0x73, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x03,
0x13, 0x08, 0x4a, 0x2e, 0x20, 0x53, 0x6d, 0x69, 0x74, 0x68};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("OU=Sales+CN=J. Smith,O=Widget Inc.,C=US", output);
}

TEST(ParseNameTest, RFC2253FormatQuoted) {
const uint8_t der[] = {
0x30, 0x40, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06,
0x13, 0x02, 0x47, 0x42, 0x31, 0x1e, 0x30, 0x1c, 0x06, 0x03, 0x55,
0x04, 0x0a, 0x13, 0x15, 0x53, 0x75, 0x65, 0x2c, 0x20, 0x47, 0x72,
0x61, 0x62, 0x62, 0x69, 0x74, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x52,
0x75, 0x6e, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04,
0x03, 0x13, 0x08, 0x4c, 0x2e, 0x20, 0x45, 0x61, 0x67, 0x6c, 0x65};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB", output);
}

TEST(ParseNameTest, RFC2253FormatNonPrintable) {
const uint8_t der[] = {0x30, 0x33, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
0x04, 0x06, 0x13, 0x02, 0x47, 0x42, 0x31, 0x0d, 0x30,
0x0b, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x04, 0x54,
0x65, 0x73, 0x74, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03,
0x55, 0x04, 0x03, 0x13, 0x0c, 0x42, 0x65, 0x66, 0x6f,
0x72, 0x65, 0x0d, 0x41, 0x66, 0x74, 0x65, 0x72};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("CN=Before\\0DAfter,O=Test,C=GB", output);
}

TEST(ParseNameTest, RFC2253FormatUnknownOid) {
const uint8_t der[] = {0x30, 0x30, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
0x04, 0x06, 0x13, 0x02, 0x47, 0x42, 0x31, 0x0d, 0x30,
0x0b, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x04, 0x54,
0x65, 0x73, 0x74, 0x31, 0x12, 0x30, 0x10, 0x06, 0x08,
0x2b, 0x06, 0x01, 0x04, 0x01, 0x8b, 0x3a, 0x00, 0x13,
0x04, 0x04, 0x02, 0x48, 0x69};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB", output);
}

TEST(ParseNameTest, RFC2253FormatLargeOid) {
const uint8_t der[] = {0x30, 0x16, 0x31, 0x14, 0x30, 0x12, 0x06, 0x0a,
0x81, 0x0d, 0x06, 0x01, 0x99, 0x21, 0x01, 0x8b,
0x3a, 0x00, 0x13, 0x04, 0x74, 0x65, 0x73, 0x74};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("2.61.6.1.3233.1.1466.0=#74657374", output);
}

TEST(ParseNameTest, RFC2253FormatUTF8) {
const uint8_t der[] = {0x30, 0x12, 0x31, 0x10, 0x30, 0x0e, 0x06,
0x03, 0x55, 0x04, 0x04, 0x13, 0x07, 0x4c,
0x75, 0xc4, 0x8d, 0x69, 0xc4, 0x87};
der::Input rdn_input(der);
RDNSequence rdn;
ASSERT_TRUE(ParseName(rdn_input, &rdn));
std::string output;
ASSERT_TRUE(ConvertToRFC2253(rdn, &output));
ASSERT_EQ("SN=Lu\\C4\\8Di\\C4\\87", output);
}

} // namespace net
Loading

0 comments on commit 983b88f

Please sign in to comment.