Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
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 &) override
 
bool empty () const
 
void encode_into (DER_Encoder &) 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)
 

Detailed Description

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

Definition at line 428 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ ASN1_String() [1/2]

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

Definition at line 82 of file asn1_str.cpp.

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

◆ ASN1_String() [2/2]

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

Definition at line 74 of file asn1_str.cpp.

74 : m_utf8_str(str), m_tag(t)
75 {
76 if(!is_utf8_subset_string_type(m_tag))
77 {
78 throw Invalid_Argument("ASN1_String only supports encoding to UTF-8 or a UTF-8 subset");
79 }
80 }

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 17 of file asn1_obj.cpp.

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

References Botan::ASN1_Object::encode_into().

Referenced by Botan::PSS_Params::decode_from(), 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::Certificate_Store_In_SQL::revoke_cert().

◆ 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 106 of file asn1_str.cpp.

107 {
108 BER_Object obj = source.get_next_object();
109
110 if(!is_asn1_string_type(obj.type()))
111 {
112 auto typ = static_cast<uint32_t>(obj.type());
113 throw Decoding_Error(fmt("ASN1_String: Unknown string type {}", typ));
114 }
115
116 m_tag = obj.type();
117 m_data.assign(obj.bits(), obj.bits() + obj.length());
118
119 if(m_tag == ASN1_Type::BmpString)
120 {
121 m_utf8_str = ucs2_to_utf8(m_data.data(), m_data.size());
122 }
123 else if(m_tag == ASN1_Type::UniversalString)
124 {
125 m_utf8_str = ucs4_to_utf8(m_data.data(), m_data.size());
126 }
127 else if(m_tag == ASN1_Type::TeletexString)
128 {
129 /*
130 TeletexString is nominally ITU T.61 not ISO-8859-1 but it seems
131 the majority of implementations actually used that charset here.
132 */
133 m_utf8_str = latin1_to_utf8(m_data.data(), m_data.size());
134 }
135 else
136 {
137 // All other supported string types are UTF-8 or some subset thereof
138 m_utf8_str = ASN1::to_string(obj);
139 }
140 }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len)
Definition: charset.cpp:61
std::string latin1_to_utf8(const uint8_t chars[], size_t len)
Definition: charset.cpp:98
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len)
Definition: charset.cpp:78

References Botan::BER_Object::bits(), Botan::BmpString, Botan::fmt(), 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(), and Botan::UniversalString.

◆ empty()

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

Definition at line 440 of file asn1_obj.h.

440{ 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 89 of file asn1_str.cpp.

90 {
91 if(m_data.empty())
92 {
93 BOTAN_ASSERT_NOMSG(is_utf8_subset_string_type(tagging()));
94 encoder.add_object(tagging(), ASN1_Class::Universal, m_utf8_str);
95 }
96 else
97 {
98 // If this string was decoded, reserialize using original encoding
99 encoder.add_object(tagging(), ASN1_Class::Universal, m_data.data(), m_data.size());
100 }
101 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:67
ASN1_Type tagging() const
Definition: asn1_obj.h:434

References Botan::DER_Encoder::add_object(), BOTAN_ASSERT_NOMSG, 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 69 of file asn1_str.cpp.

70 {
71 return is_asn1_string_type(tag);
72 }

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

◆ operator==()

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

Definition at line 447 of file asn1_obj.h.

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

References value().

◆ size()

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

Definition at line 438 of file asn1_obj.h.

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

◆ tagging()

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

Definition at line 434 of file asn1_obj.h.

434{ return m_tag; }

Referenced by encode_into().

◆ value()

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

Definition at line 436 of file asn1_obj.h.

436{ return m_utf8_str; }

Referenced by Botan::X509_DN::get_first_attribute(), operator==(), and Botan::TLS::Session::Session().


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