Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 61 additions & 25 deletions core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ constexpr int NestedAsn1Error = 58;
constexpr int MallocFailure = 65;
constexpr int BadObjectHeader = 102;
constexpr int InvalidTimeFormat = 132;
constexpr int MissingEoc = 137;
constexpr int StringTooShort = 152;
constexpr int SequenceLengthMismatch = 148;
constexpr int SequenceNotConstructed = 149;
constexpr int TooLong = 155;
constexpr int UnexpectedEoc = 159;
constexpr int WrongTag = 168;
constexpr int NestedAsn1String = 197;
constexpr int InvalidObjectEncoding = 216;
constexpr int InvalidBitStringBitsLeft = 220;
constexpr int TypeNotPrimitive = 231;
Expand All @@ -50,6 +53,7 @@ constexpr std::int64_t BitsLeftFlag = 0x08;
constexpr int ObjectDynamic = 0x01;
constexpr int ObjectDynamicData = 0x08;
constexpr std::uint8_t ContextClass = 2;
constexpr int MaxStringNesting = 5;

enum class Decoder {
Unsupported,
Expand Down Expand Up @@ -174,7 +178,51 @@ bool ReadLength(std::span<const unsigned char>& input, Header& header) {
return true;
}

std::optional<std::span<const unsigned char>> PrimitiveContent(std::span<const unsigned char> input, std::uint32_t expectedTag) {
bool ReadCheckedLength(std::span<const unsigned char>& input, Header& header) {
if (!ReadLength(input, header) || (header.indefinite && !header.constructed)) {
PutHeaderError(BadObjectHeader);
return false;
}
if ((!header.indefinite && header.length > input.size()) ||
header.tag > static_cast<std::uint32_t>(std::numeric_limits<int>::max())) {
PutHeaderError(TooLong);
return false;
}
return true;
}

bool Collect(std::span<const unsigned char>& input, bool indefinite, int depth, std::vector<unsigned char>& collected) {
if (depth > MaxStringNesting) {
PutError(NestedAsn1String);
return false;
}
while (!input.empty()) {
if (input.size() >= 2 && input[0] == 0 && input[1] == 0) {
input = input.subspan(2);
if (indefinite) return true;
PutError(UnexpectedEoc);
return false;
}
Header header;
if (!ReadIdentifier(input, header)) {
PutHeaderError(BadObjectHeader);
return false;
}
if (!ReadCheckedLength(input, header)) return false;
if (header.constructed) {
if (!Collect(input, header.indefinite, depth + 1, collected)) return false;
continue;
}
collected.insert(collected.end(), input.begin(), input.begin() + static_cast<std::ptrdiff_t>(header.length));
input = input.subspan(header.length);
}
if (!indefinite) return true;
PutError(MissingEoc);
return false;
}

std::optional<std::span<const unsigned char>> Content(std::span<const unsigned char> input, std::uint32_t expectedTag,
std::vector<unsigned char>* collected = nullptr) {
std::span<const unsigned char> remaining = input;
Header header;
if (!ReadIdentifier(remaining, header)) {
Expand All @@ -185,27 +233,14 @@ std::optional<std::span<const unsigned char>> PrimitiveContent(std::span<const u
PutHeaderError(WrongTag);
return std::nullopt;
}
if (!ReadLength(remaining, header)) {
PutHeaderError(BadObjectHeader);
return std::nullopt;
}
if (header.indefinite && !header.constructed) {
PutHeaderError(BadObjectHeader);
return std::nullopt;
}
if (!header.indefinite && header.length > remaining.size()) {
PutHeaderError(TooLong);
return std::nullopt;
}
if (header.tag > static_cast<std::uint32_t>(std::numeric_limits<int>::max())) {
PutHeaderError(TooLong);
return std::nullopt;
}
if (header.constructed) {
if (!ReadCheckedLength(remaining, header)) return std::nullopt;
if (!header.constructed) return remaining.first(header.length);
if (!collected) {
PutError(TypeNotPrimitive);
return std::nullopt;
}
return remaining.first(header.length);
if (!Collect(remaining, header.indefinite, 0, *collected)) return std::nullopt;
return std::span<const unsigned char>(*collected);
}

Asn1String* NewString(std::int32_t type, std::span<const unsigned char> content) {
Expand Down Expand Up @@ -457,33 +492,34 @@ void* Decode(const LibreSsl3X509Extension& extension, Decoder decoder) {
if (encoded->length > 0 && !encoded->data)
throw std::invalid_argument(std::string(__func__) + ": null extension value data");
const std::span<const unsigned char> value(encoded->data, static_cast<std::size_t>(encoded->length));
std::vector<unsigned char> collected;
switch (decoder) {
case Decoder::OcspNonce:
return NewString(OctetStringType, value);
case Decoder::BitString: {
const auto content = PrimitiveContent(value, BitStringTag);
const auto content = Content(value, BitStringTag, &collected);
return content ? NewBitString(*content) : nullptr;
}
case Decoder::OctetString: {
const auto content = PrimitiveContent(value, OctetStringTag);
const auto content = Content(value, OctetStringTag, &collected);
return content ? NewString(OctetStringType, *content) : nullptr;
}
case Decoder::Integer:
return NewInteger(value);
case Decoder::Enumerated: {
const auto content = PrimitiveContent(value, EnumeratedTag);
const auto content = Content(value, EnumeratedTag);
return content ? NewEnumerated(*content) : nullptr;
}
case Decoder::Ia5String: {
const auto content = PrimitiveContent(value, Ia5StringTag);
const auto content = Content(value, Ia5StringTag, &collected);
return content ? NewString(Ia5StringType, *content) : nullptr;
}
case Decoder::GeneralizedTime: {
const auto content = PrimitiveContent(value, GeneralizedTimeTag);
const auto content = Content(value, GeneralizedTimeTag, &collected);
return content ? NewGeneralizedTime(*content) : nullptr;
}
case Decoder::Object: {
const auto content = PrimitiveContent(value, ObjectTag);
const auto content = Content(value, ObjectTag);
return content ? NewObject(*content) : nullptr;
}
case Decoder::AuthorityKeyId:
Expand Down
83 changes: 82 additions & 1 deletion core/libs/prx/libSceLibreSsl3/tests/X509V3ExtD2i.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,85 @@ void TestOctetStrings() {
FreeOctetString(value);
}

void CheckString(int nid, std::initializer_list<unsigned char> bytes, std::int32_t type, std::vector<unsigned char> expected,
std::source_location location = std::source_location::current()) {
Fixture fixture(nid, bytes);
auto* value = static_cast<Asn1String*>(X509V3_EXT_d2i_nid_postfix(&fixture.extension));
Require(value != nullptr, location);
Require(value->type == type && value->flags == 0 && value->length == static_cast<std::int32_t>(expected.size()), location);
Require(value->data != nullptr && value->data[expected.size()] == 0, location);
Require(expected.empty() || std::memcmp(value->data, expected.data(), expected.size()) == 0, location);
FreeString(value);
RequireQueue({});
}

void CheckRejected(int nid, std::initializer_list<unsigned char> bytes, std::initializer_list<int> queue,
std::source_location location = std::source_location::current()) {
Fixture fixture(nid, bytes);
Require(X509V3_EXT_d2i_nid_postfix(&fixture.extension) == nullptr, location);
Require(allocations.empty(), location);
for (const int reason : queue) Require(ERR_get_error_nid_postfix() == Error(reason), location);
Require(ERR_get_error_nid_postfix() == 0, location);
}

void TestConstructedStrings() {
CheckString(82, {0x24, 0x06, 0x04, 0x01, 0x41, 0x04, 0x01, 0x42}, 4, {0x41, 0x42});
CheckString(82, {0x24, 0x80, 0x04, 0x01, 0x41, 0x00, 0x00}, 4, {0x41});
CheckString(82, {0x24, 0x00}, 4, {});
CheckString(82, {0x24, 0x80, 0x00, 0x00}, 4, {});
CheckString(82, {0x24, 0x80, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00}, 4, {});
CheckString(82, {0x24, 0x08, 0x24, 0x06, 0x04, 0x01, 0x41, 0x04, 0x01, 0x42}, 4, {0x41, 0x42});
CheckString(82, {0x24, 0x0a, 0x24, 0x80, 0x04, 0x01, 0x41, 0x00, 0x00, 0x04, 0x01, 0x42}, 4, {0x41, 0x42});
CheckString(82, {0x24, 0x80, 0x04, 0x01, 0x41, 0x00, 0x00, 0x04, 0x01, 0x42}, 4, {0x41});
CheckString(82, {0x24, 0x05, 0x16, 0x03, 'a', 'b', 'c'}, 4, {'a', 'b', 'c'});
CheckString(82, {0x24, 0x03, 0x80, 0x01, 0x41}, 4, {0x41});
CheckString(82, {0x24, 0x03, 0x00, 0x01, 0x41}, 4, {0x41});
CheckString(82, {0x24, 0x05, 0x1f, 0x81, 0x01, 0x01, 0x41}, 4, {0x41});
CheckString(72, {0x36, 0x05, 0x16, 0x03, 'a', 'b', 'c'}, 22, {'a', 'b', 'c'});
CheckString(78, {0x36, 0x0a, 0x16, 0x03, 'a', 'b', 'c', 0x36, 0x03, 0x16, 0x01, 'd'}, 22, {'a', 'b', 'c', 'd'});
CheckString(72, {0x36, 0x80, 0x16, 0x01, 'a', 0x04, 0x01, 'b', 0x00, 0x00}, 22, {'a', 'b'});
CheckString(142, {0x38, 0x13, 0x04, 0x08, '2', '0', '2', '4', '0', '1', '0', '2', 0x04, 0x07, '0', '3', '0', '4', '0', '5', 'Z'},
24, {'2', '0', '2', '4', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', 'Z'});
CheckString(82, {0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x04, 0x01, 0x41,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
4, {0x41});
CheckRejected(82, {0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x24, 0x80, 0x04, 0x01, 0x41,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{197});
CheckRejected(82, {0x24, 0x0e, 0x24, 0x0c, 0x24, 0x0a, 0x24, 0x08, 0x24, 0x06, 0x24, 0x04, 0x24, 0x02, 0x04, 0x00}, {197});
CheckRejected(82, {0x24, 0x80}, {137});
CheckRejected(82, {0x24, 0x80, 0x04, 0x01, 0x41}, {137});
CheckRejected(82, {0x24, 0x05, 0x24, 0x80, 0x04, 0x01, 0x41}, {137});
CheckRejected(82, {0x24, 0x02, 0x00, 0x00}, {159});
CheckRejected(82, {0x24, 0x04, 0x24, 0x02, 0x00, 0x00}, {159});
CheckRejected(83, {0x23, 0x00}, {152});
CheckRejected(83, {0x23, 0x04, 0x03, 0x02, 0x08, 0x80}, {220});
CheckRejected(141, {0x2a, 0x03, 0x0a, 0x01, 0x01}, {231});
CheckRejected(430, {0x26, 0x03, 0x06, 0x01, 0x2a}, {231});
Fixture bits(83, {0x23, 0x80, 0x03, 0x02, 0x07, 0x80, 0x00, 0x00});
CheckKeyUsage(bits, 1, 0x0f, 0x80);
Fixture joinedBits(83, {0x23, 0x08, 0x03, 0x02, 0x00, 0x80, 0x03, 0x02, 0x00, 0x40});
auto* value = static_cast<Asn1String*>(X509V3_EXT_d2i_nid_postfix(&joinedBits.extension));
Require(value != nullptr && value->type == 3 && value->length == 3 && value->flags == 0x08);
Require(value->data[0] == 0x80 && value->data[1] == 0x00 && value->data[2] == 0x40);
FreeString(value);
}

void TestConstructedLibreSsl4Rules() {
CheckString(82, {0x24, 0x03, 0x04, 0x01, 0x41, 0x04, 0x01, 0x42}, 4, {0x41, 0x42});
CheckString(82, {0x24, 0x00, 0x04, 0x01, 0x41}, 4, {0x41});
CheckString(82, {0x24, 0x03, 0x04, 0x05, 'A', 'B', 'C', 'D', 'E'}, 4, {'A', 'B', 'C', 'D', 'E'});
CheckString(82, {0x24, 0x05, 0x24, 0x80, 0x04, 0x01, 0x41, 0x00, 0x00}, 4, {0x41});
CheckRejected(82, {0x24, 0x80, 0x24, 0x03, 0x04, 0x01, 0x41, 0x00, 0x00}, {159});
CheckRejected(82, {0x24, 0x03, 0x04, 0x01, 0x41, 0x00, 0x00}, {159});
CheckRejected(82, {0x24, 0x03, 0x04, 0x01, 0x41, 0xff}, {102, 58});
CheckRejected(82, {0x24, 0x02, 0x04, 0x05}, {155, 58});
CheckRejected(82, {0x24, 0x07, 0x1f, 0x88, 0x80, 0x80, 0x80, 0x00, 0x00}, {155, 58});
CheckRejected(82, {0x24, 0x04, 0x04, 0x80, 0x00, 0x00}, {102, 58});
CheckRejected(82, {0x24, 0x80, 0x04, 0x01, 0x41, 0x00}, {102, 58});
CheckRejected(142, {0x38, 0x11, 0x04, 0x0f, '2', '0', '2', '4', '1', '3', '0', '2', '0', '3', '0', '4', '0', '5', 'Z'}, {132});
}

void TestIntegers() {
Fixture crlNumber(88, {0x02, 0x01, 0x2a});
auto* value = static_cast<Asn1String*>(X509V3_EXT_d2i_nid_postfix(&crlNumber.extension));
Expand Down Expand Up @@ -424,7 +503,7 @@ void TestMalformed() {
RequireQueue({155, 58});
Fixture constructed(83, {0x23, 0x03, 0x00, 0x00, 0x00});
Require(X509V3_EXT_d2i_nid_postfix(&constructed.extension) == nullptr);
RequireQueue({231});
RequireQueue({159});
Fixture indefinite(82, {0x04, 0x80, 0x00, 0x00});
Require(X509V3_EXT_d2i_nid_postfix(&indefinite.extension) == nullptr);
RequireQueue({102, 58});
Expand Down Expand Up @@ -495,6 +574,8 @@ int main() {
DrainErrors();
TestBitStrings();
TestOctetStrings();
TestConstructedStrings();
TestConstructedLibreSsl4Rules();
TestIntegers();
TestEnumerated();
TestGeneralizedTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libSceLibreSsl3

## add Functional

- [X509V3_EXT_d2i constructed strings](../../../core/libs/prx/libSceLibreSsl3/src/X509V3ExtD2i.cpp) (libSceLibreSsl3) - not compared with the console. A length field of more than 8 octets is still rejected, where LibreSSL accepts it; this is the only known difference from 4.0.0. The rest follows LibreSSL 4.0.0 [`asn1_collect`](https://github.com/libressl/openbsd/blob/ce488b78e0bd994780569df718596b9776ff681a/src/lib/libcrypto/asn1/tasn_dec.c#L180-L231), read but not run; 3.7.3 and the LibreSSL main branch read the same on this path. It differs from the measured 3.4.1 submodule: a definite length does not bound the fragments read, constructed ENUMERATED and OBJECT values give `TYPE_NOT_PRIMITIVE` (231) where 3.4.1 accepts them, and constructed GeneralizedTime content is validated where 3.4.1 validates no GeneralizedTime. Closes when: compared with the console.
Loading