Skip to content

Commit

Permalink
Merge pull request #4138 from randombit/jack/fix-tn-oss-fuzz
Browse files Browse the repository at this point in the history
Avoid casting TNEntry type code to Type directly
  • Loading branch information
randombit authored Jun 21, 2024
2 parents f82dd53 + ae3b835 commit a55851c
Showing 1 changed file with 39 additions and 40 deletions.
79 changes: 39 additions & 40 deletions src/lib/x509/x509_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,49 +791,48 @@ void TNAuthList::Entry::encode_into(DER_Encoder&) const {
void TNAuthList::Entry::decode_from(class BER_Decoder& ber) {
BER_Object obj = ber.get_next_object();

m_type = static_cast<Type>(obj.type_tag());

switch(m_type) {
case ServiceProviderCode: {
ASN1_String spc_string;
BER_Decoder(obj).decode(spc_string);
m_data = std::move(spc_string);
} break;
case TelephoneNumberRange: {
m_data = RangeContainer();
auto& range_items = std::get<RangeContainer>(m_data);
BER_Decoder list = BER_Decoder(obj).start_sequence();
while(list.more_items()) {
TelephoneNumberRangeData entry;

list.decode(entry.start);
if(!is_valid_telephone_number(entry.start)) {
throw Decoding_Error(fmt("Invalid TelephoneNumberRange start {}", entry.start.value()));
}

list.decode(entry.count);
if(entry.count < 2) {
throw Decoding_Error(fmt("Invalid TelephoneNumberRange count {}", entry.count));
}

range_items.emplace_back(std::move(entry));
const uint32_t type_tag = static_cast<Type>(obj.type_tag());

if(type_tag == ServiceProviderCode) {
m_type = ServiceProviderCode;
ASN1_String spc_string;
BER_Decoder(obj).decode(spc_string);
m_data = std::move(spc_string);
} else if(type_tag == TelephoneNumberRange) {
m_type = TelephoneNumberRange;
m_data = RangeContainer();
auto& range_items = std::get<RangeContainer>(m_data);
BER_Decoder list = BER_Decoder(obj).start_sequence();
while(list.more_items()) {
TelephoneNumberRangeData entry;

list.decode(entry.start);
if(!is_valid_telephone_number(entry.start)) {
throw Decoding_Error(fmt("Invalid TelephoneNumberRange start {}", entry.start.value()));
}
list.end_cons();

if(range_items.empty()) {
throw Decoding_Error("TelephoneNumberRange is empty");
list.decode(entry.count);
if(entry.count < 2) {
throw Decoding_Error(fmt("Invalid TelephoneNumberRange count {}", entry.count));
}
} break;
case TelephoneNumber: {
ASN1_String one_string;
BER_Decoder(obj).decode(one_string);
if(!is_valid_telephone_number(one_string)) {
throw Decoding_Error(fmt("Invalid TelephoneNumber {}", one_string.value()));
}
m_data = std::move(one_string);
} break;
default:
throw Decoding_Error(fmt("Unexpected TNEntry type {}", m_type));

range_items.emplace_back(std::move(entry));
}
list.end_cons();

if(range_items.empty()) {
throw Decoding_Error("TelephoneNumberRange is empty");
}
} else if(type_tag == TelephoneNumber) {
m_type = TelephoneNumber;
ASN1_String one_string;
BER_Decoder(obj).decode(one_string);
if(!is_valid_telephone_number(one_string)) {
throw Decoding_Error(fmt("Invalid TelephoneNumber {}", one_string.value()));
}
m_data = std::move(one_string);
} else {
throw Decoding_Error(fmt("Unexpected TNEntry type code {}", type_tag));
};
}

Expand Down

0 comments on commit a55851c

Please sign in to comment.