Botan 3.13.0
Crypto and TLS for C&
Botan::ASN1_String Class Referencefinal

#include <asn1_obj.h>

Inheritance diagram for Botan::ASN1_String:
Botan::ASN1_Object

Public Member Functions

 ASN1_String (std::string_view utf8, ASN1_Type tag)
 ASN1_String (std::string_view utf8="")
std::vector< uint8_t > BER_encode () const
void decode_from (BER_Decoder &from) override
bool empty () const
void encode_into (DER_Encoder &to) const override
bool operator== (const ASN1_String &other) const
size_t size () const
ASN1_Type tagging () const
const std::string & value () const

Static Public Member Functions

static bool is_string_type (ASN1_Type tag)

Friends

bool operator< (const ASN1_String &a, const ASN1_String &b)

Detailed Description

ASN.1 string type This class normalizes all inputs to a UTF-8 std::string

Definition at line 577 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_String() [1/2]

Botan::ASN1_String::ASN1_String ( std::string_view utf8 = "")
explicit

Create a string, tagged as PrintableString if possible and otherwise as Utf8String

Parameters
utf8the value of the string, encoded as UTF-8

Definition at line 145 of file asn1_str.cpp.

145: ASN1_String(str, choose_encoding(str)) {}
ASN1_String(std::string_view utf8="")
Definition asn1_str.cpp:145

References ASN1_String().

Referenced by ASN1_String(), operator<, and operator==().

◆ ASN1_String() [2/2]

Botan::ASN1_String::ASN1_String ( std::string_view utf8,
ASN1_Type tag )

Create a string with a specific type tag

Parameters
utf8the value of the string, encoded as UTF-8
tagthe string type tag to encode this value with

Throws Invalid_Argument if tag is not a string type that is a subset of UTF-8, or if utf8 is not a valid value for that type.

Definition at line 135 of file asn1_str.cpp.

135 : m_utf8_str(str), m_tag(t) {
136 if(!is_utf8_subset_string_type(m_tag)) {
137 throw Invalid_Argument("ASN1_String only supports encoding to UTF-8 or a UTF-8 subset");
138 }
139
140 if(!is_valid_asn1_string_content(m_utf8_str, m_tag)) {
141 throw Invalid_Argument(fmt("ASN1_String: Invalid {} encoding", asn1_tag_to_string(m_tag)));
142 }
143}
std::string asn1_tag_to_string(ASN1_Type type)
Definition asn1_obj.cpp:129
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::asn1_tag_to_string(), and Botan::fmt().

Member Function Documentation

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 21 of file asn1_obj.cpp.

21 {
22 std::vector<uint8_t> output;
23 DER_Encoder der(output);
24 this->encode_into(der);
25 return output;
26}
virtual void encode_into(DER_Encoder &to) const =0

References encode_into().

Referenced by decode_from(), Botan::PKCS12::export_to(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), and Botan::PSS_Params::PSS_Params().

◆ decode_from()

void Botan::ASN1_String::decode_from ( BER_Decoder & from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 162 of file asn1_str.cpp.

162 {
163 const BER_Object obj = source.get_next_object();
164
165 if(obj.get_class() != ASN1_Class::Universal || !is_asn1_string_type(obj.type())) {
166 auto typ = static_cast<uint32_t>(obj.type());
167 auto cls = static_cast<uint32_t>(obj.get_class());
168 throw Decoding_Error(fmt("ASN1_String: Unknown string type {}/{}", typ, cls));
169 }
170
171 m_tag = obj.type();
172 m_data.assign(obj.bits(), obj.bits() + obj.length());
173
174 if(m_tag == ASN1_Type::BmpString) {
175 m_utf8_str = ucs2_to_utf8(m_data);
176 } else if(m_tag == ASN1_Type::UniversalString) {
177 m_utf8_str = ucs4_to_utf8(m_data);
178 } else if(m_tag == ASN1_Type::TeletexString) {
179 /*
180 TeletexString is nominally ITU T.61 not ISO-8859-1 but it seems
181 the majority of implementations actually used that charset here.
182 */
183 m_utf8_str = latin1_to_utf8(m_data);
184 } else {
185 // All other supported string types are UTF-8 or some subset thereof
186 m_utf8_str = ASN1::to_string(obj);
187
188 if(!is_valid_asn1_string_content(m_utf8_str, m_tag)) {
189 throw Decoding_Error(fmt("ASN1_String: Invalid {} encoding", asn1_tag_to_string(m_tag)));
190 }
191 }
192}
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:224
std::string latin1_to_utf8(std::span< const uint8_t > chars)
Definition charset.cpp:190
std::string ucs2_to_utf8(std::span< const uint8_t > ucs2)
Definition charset.cpp:121
std::string ucs4_to_utf8(std::span< const uint8_t > ucs4)
Definition charset.cpp:155

References Botan::asn1_tag_to_string(), Botan::BER_Object::bits(), Botan::BmpString, Botan::fmt(), Botan::BER_Object::get_class(), Botan::BER_Decoder::get_next_object(), Botan::latin1_to_utf8(), Botan::BER_Object::length(), Botan::TeletexString, Botan::ASN1::to_string(), Botan::BER_Object::type(), Botan::ucs2_to_utf8(), Botan::ucs4_to_utf8(), Botan::Universal, and Botan::UniversalString.

◆ empty()

bool Botan::ASN1_String::empty ( ) const
inline

Return true if this string is empty

Definition at line 600 of file asn1_obj.h.

600{ return m_utf8_str.empty(); }

Referenced by Botan::X509_DN::add_attribute().

◆ encode_into()

void Botan::ASN1_String::encode_into ( DER_Encoder & to) const
overridevirtual

Encode whatever this object is into to

Parameters
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 150 of file asn1_str.cpp.

150 {
151 if(is_utf8_subset_string_type(tagging())) {
152 encoder.add_object(tagging(), ASN1_Class::Universal, m_utf8_str);
153 } else {
154 // BMP/Universal/Teletex: m_utf8_str is the UTF-8 conversion, m_data is the wire form
155 encoder.add_object(tagging(), ASN1_Class::Universal, m_data.data(), m_data.size());
156 }
157}
ASN1_Type tagging() const
Definition asn1_obj.h:585

References Botan::DER_Encoder::add_object(), tagging(), and Botan::Universal.

◆ is_string_type()

bool Botan::ASN1_String::is_string_type ( ASN1_Type tag)
static

Return true iff this is a tag for a known string type we can handle.

Definition at line 131 of file asn1_str.cpp.

131 {
132 return is_asn1_string_type(tag);
133}

Referenced by Botan::AlternativeName::decode_from().

◆ operator==()

bool Botan::ASN1_String::operator== ( const ASN1_String & other) const
inline

Compare two strings, ignoring the type tag they were encoded with

Definition at line 610 of file asn1_obj.h.

610{ return value() == other.value(); }
const std::string & value() const
Definition asn1_obj.h:590

References ASN1_String(), and value().

◆ size()

size_t Botan::ASN1_String::size ( ) const
inline

Return the length in bytes of the UTF-8 representation of this string

Definition at line 595 of file asn1_obj.h.

595{ return value().size(); }

References value().

◆ tagging()

ASN1_Type Botan::ASN1_String::tagging ( ) const
inline

Return the string type tag this value was encoded with

Definition at line 585 of file asn1_obj.h.

585{ return m_tag; }

Referenced by encode_into().

◆ value()

const std::string & Botan::ASN1_String::value ( ) const
inline

◆ operator<

bool operator< ( const ASN1_String & a,
const ASN1_String & b )
friend

Definition at line 612 of file asn1_obj.h.

612{ return a.value() < b.value(); }

References ASN1_String(), and value().


The documentation for this class was generated from the following files: