--- a/src/lib/ASN1ContentHandler.h Sun Jul 04 11:37:27 2021 +0200
+++ b/src/lib/ASN1ContentHandler.h Sun Jul 04 11:51:13 2021 +0200
@@ -91,33 +91,6 @@
uint64_t tag;
};
- enum class StringType : uint64_t {
- UTF8String = 0xC,
- NumericString = 0x12,
- PrintableString = 0x13,
- T61String = 0x14,
- VideotexString = 0x15,
- IA5String = 0x16,
- GraphicString = 0x19,
- VisibleString = 0x1A,
- GeneralString = 0x1B,
- UniversalString = 0x1C,
- CharacterString = 0x1D,
- BMPString = 0x1E,
- };
-
- enum class DateTimeType : uint64_t {
- UTCTime,
- GeneralizedTime,
- Time,
- Date,
- TimeOfDay,
- DateTime,
- Duration,
- // TODO: review date/time types
- };
-
-
// TODO: separate implementation of particular types from the interface, separate SPI from API?
class Integer {
@@ -292,24 +265,24 @@
virtual void writeCollectionStart(const Header& header) = 0;
virtual void writeCollectionEnd() = 0;
- virtual void writeBoolean(bool value) = 0;
- virtual void writeNull() = 0;
- virtual void writeInteger(Integer value) = 0;
+ virtual void writeBoolean(const Header& header, bool value) = 0;
+ virtual void writeNull(const Header& header) = 0;
+ virtual void writeInteger(const Header& header, Integer value) = 0;
/**
* @param type original type in ASN.1
* @param value original text converted to UTF-8
*/
- virtual void writeTextString(StringType type, std::string value) = 0;
+ virtual void writeTextString(const Header& header, std::string value) = 0;
/**
* @param value arbitrary sequence of octets (bytes), usually not a human-readable text
*/
- virtual void writeOctetString(std::string value) = 0;
+ virtual void writeOctetString(const Header& header, std::string value) = 0;
/**
* @param value arbitrary sequence of bits (booleans), usually not a human-readable text
*/
- virtual void writeBitString(std::vector<bool> value) = 0;
- virtual void writeOID(ObjectIdentifier value) = 0;
- virtual void writeDateTime(DateTimeType type, DateTime value) = 0;
+ virtual void writeBitString(const Header& header, std::vector<bool> value) = 0;
+ virtual void writeOID(const Header& header, ObjectIdentifier value) = 0;
+ virtual void writeDateTime(const Header& header, DateTime value) = 0;
// Object descriptor
// virtual void writeReal(float value) = 0;
// Enumerated
@@ -345,36 +318,36 @@
handlers.forward(&ASN1ContentHandler::writeCollectionEnd);
}
- void writeBoolean(bool value) override {
- handlers.forward(&ASN1ContentHandler::writeBoolean, value);
+ void writeBoolean(const Header& header, bool value) override {
+ handlers.forward(&ASN1ContentHandler::writeBoolean, header, value);
}
- void writeNull() override {
- handlers.forward(&ASN1ContentHandler::writeNull);
+ void writeNull(const Header& header) override {
+ handlers.forward(&ASN1ContentHandler::writeNull, header);
}
- void writeInteger(Integer value) override {
- handlers.forward(&ASN1ContentHandler::writeInteger, value);
+ void writeInteger(const Header& header, Integer value) override {
+ handlers.forward(&ASN1ContentHandler::writeInteger, header, value);
}
- void writeTextString(StringType type, std::string value) override {
- handlers.forward(&ASN1ContentHandler::writeTextString, type, value);
+ void writeTextString(const Header& header, std::string value) override {
+ handlers.forward(&ASN1ContentHandler::writeTextString, header, value);
}
- void writeOctetString(std::string value) override {
- handlers.forward(&ASN1ContentHandler::writeOctetString, value);
+ void writeOctetString(const Header& header, std::string value) override {
+ handlers.forward(&ASN1ContentHandler::writeOctetString, header, value);
}
- void writeBitString(std::vector<bool> value) override {
- handlers.forward(&ASN1ContentHandler::writeBitString, value);
+ void writeBitString(const Header& header, std::vector<bool> value) override {
+ handlers.forward(&ASN1ContentHandler::writeBitString, header, value);
}
- void writeOID(ObjectIdentifier value) override {
- handlers.forward(&ASN1ContentHandler::writeOID, value);
+ void writeOID(const Header& header, ObjectIdentifier value) override {
+ handlers.forward(&ASN1ContentHandler::writeOID, header, value);
}
- void writeDateTime(DateTimeType type, DateTime value) override {
- handlers.forward(&ASN1ContentHandler::writeDateTime, type, value);
+ void writeDateTime(const Header& header, DateTime value) override {
+ handlers.forward(&ASN1ContentHandler::writeDateTime, header, value);
}
};
--- a/src/lib/BasicASN1Reader.h Sun Jul 04 11:37:27 2021 +0200
+++ b/src/lib/BasicASN1Reader.h Sun Jul 04 11:51:13 2021 +0200
@@ -139,39 +139,39 @@
level.push_back({typeHeader.definiteLength, typeHeader.length, getBytesRead()}); // TODO: transaction
handlers.writeCollectionStart(typeHeader);
} else if (typeHeader.tag == UniversalType::Null && typeHeader.length == 0) {
- handlers.writeNull();
+ handlers.writeNull(typeHeader);
} else if (typeHeader.tag == UniversalType::Boolean && typeHeader.definiteLength && typeHeader.length == 1) {
bool value;
read((uint8_t*) & value, 1);
- handlers.writeBoolean(value);
+ handlers.writeBoolean(typeHeader, value);
} else if (typeHeader.tag == UniversalType::Integer && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check available bytes before allocating buffer
std::vector<uint8_t> value(typeHeader.length, 0x00);
read(value.data(), typeHeader.length);
- handlers.writeInteger(ASN1ContentHandler::Integer(value));
+ handlers.writeInteger(typeHeader, ASN1ContentHandler::Integer(value));
} else if (typeHeader.tag == UniversalType::ObjectIdentifier && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
std::vector<uint8_t> value(typeHeader.length, 0x00);
read(value.data(), typeHeader.length);
- handlers.writeOID({value});
+ handlers.writeOID(typeHeader,{value});
} else if (typeHeader.tag == UniversalType::UTF8String && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check available bytes before allocating buffer
std::string s;
s.resize(typeHeader.length);
read((uint8_t*) s.data(), typeHeader.length);
- handlers.writeTextString(ASN1ContentHandler::StringType::UTF8String, s);
+ handlers.writeTextString(typeHeader, s);
} else if (typeHeader.tag == UniversalType::PrintableString && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check encoding
// TODO: check available bytes before allocating buffer
std::string s;
s.resize(typeHeader.length);
read((uint8_t*) s.data(), typeHeader.length);
- handlers.writeTextString(ASN1ContentHandler::StringType::PrintableString, s);
+ handlers.writeTextString(typeHeader, s);
} else if (typeHeader.tag == UniversalType::OctetString && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check available bytes before allocating buffer
std::string s;
s.resize(typeHeader.length);
read((uint8_t*) s.data(), typeHeader.length);
- handlers.writeOctetString(s);
+ handlers.writeOctetString(typeHeader, s);
} else if (typeHeader.tag == UniversalType::BitString && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check available bytes before allocating buffer
std::string s;
@@ -184,7 +184,7 @@
for (uint8_t j = padding; j < 8; j++) bits.push_back(s.back() & 1 << j);
for (size_t i = s.size() - 2; i > 0; i--) for (uint8_t j = 0; j < 8; j++) bits.push_back(s[i] & 1 << j);
}
- handlers.writeBitString(bits);
+ handlers.writeBitString(typeHeader, bits);
} else if (typeHeader.tag == UniversalType::UTCTime && typeHeader.tagClass == TagClass::Universal && typeHeader.definiteLength) {
// TODO: check available bytes before allocating buffer
// TODO: check encoding
@@ -216,7 +216,7 @@
dateTime.timezoneHour = std::stoi(match[i++]);
dateTime.timezoneMinute = std::stoi(match[i++]);
}
- handlers.writeDateTime(ASN1ContentHandler::DateTimeType::UTCTime, dateTime);
+ handlers.writeDateTime(typeHeader, dateTime);
} else {
throw std::invalid_argument("Unsupported UTCTime format: " + s); // TODO: better exception
}
@@ -247,7 +247,7 @@
dateTime.timezoneHour = std::stoi(match[i++]);
dateTime.timezoneMinute = std::stoi(match[i++]);
}
- handlers.writeDateTime(ASN1ContentHandler::DateTimeType::GeneralizedTime, dateTime);
+ handlers.writeDateTime(typeHeader, dateTime);
} else {
throw std::invalid_argument("Unsupported GeneralizedTime format: " + s); // TODO: better exception
}
@@ -266,7 +266,7 @@
<< " length = " << typeHeader.length
<< " definite = " << (typeHeader.definiteLength ? "true" : "false");
- handlers.writeTextString(ASN1ContentHandler::StringType::UTF8String, description.str());
+ handlers.writeTextString(typeHeader, description.str());
}
commit();
--- a/src/lib/GenericASN1ContentHandler.h Sun Jul 04 11:37:27 2021 +0200
+++ b/src/lib/GenericASN1ContentHandler.h Sun Jul 04 11:51:13 2021 +0200
@@ -62,30 +62,30 @@
handlers.writeEndElement();
}
- void writeBoolean(bool value) override {
+ void writeBoolean(const Header& header, bool value) override {
handlers.writeStartElement("boolean");
handlers.writeCharacters(value ? "true" : "false");
handlers.writeEndElement();
}
- void writeNull() override {
+ void writeNull(const Header& header) override {
handlers.writeStartElement("null");
handlers.writeEndElement();
}
- void writeInteger(Integer value) override {
+ void writeInteger(const Header& header, Integer value) override {
handlers.writeStartElement("integer",{"hex", value.toHex()});
handlers.writeCharacters(value.toString());
handlers.writeEndElement();
}
- void writeTextString(StringType type, std::string value) override {
- handlers.writeStartElement("text-string",{"type", std::to_string((uint64_t) type)}); // TODO: type name, better attributes
+ void writeTextString(const Header& header, std::string value) override {
+ handlers.writeStartElement("text-string",{"type", std::to_string(header.tag)}); // TODO: type name, better attributes
handlers.writeCharacters(value);
handlers.writeEndElement();
}
- void writeOctetString(std::string value) override {
+ void writeOctetString(const Header& header, std::string value) override {
std::stringstream hex;
hex << std::hex << std::setfill('0');
for (uint8_t b : value) hex << std::setw(2) << (int) b;
@@ -94,7 +94,7 @@
handlers.writeEndElement();
}
- void writeBitString(std::vector<bool> value) override {
+ void writeBitString(const Header& header, std::vector<bool> value) override {
std::stringstream bits;
for (bool b : value) bits << (int) b;
// for (bool b : value) bits << (b ? ':' : '.'); // TODO: configurable true/false symbols?
@@ -103,14 +103,14 @@
handlers.writeEndElement();
}
- void writeOID(ObjectIdentifier value) override {
+ void writeOID(const Header& header, ObjectIdentifier value) override {
// TODO: optionally expand into separate elements with additional metadata
handlers.writeStartElement("oid");
handlers.writeCharacters(value.toString());
handlers.writeEndElement();
}
- void writeDateTime(DateTimeType type, DateTime value) override {
+ void writeDateTime(const Header& header, DateTime value) override {
handlers.writeStartElement("date-time");
handlers.writeCharacters(value.toString());
handlers.writeEndElement();