Botan 3.4.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 408 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 70 of file asn1_str.cpp.

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

◆ ASN1_String() [2/2]

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

Definition at line 64 of file asn1_str.cpp.

64 : m_utf8_str(str), m_tag(t) {
65 if(!is_utf8_subset_string_type(m_tag)) {
66 throw Invalid_Argument("ASN1_String only supports encoding to UTF-8 or a UTF-8 subset");
67 }
68}

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

19 {
20 std::vector<uint8_t> output;
21 DER_Encoder der(output);
22 this->encode_into(der);
23 return output;
24}
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 88 of file asn1_str.cpp.

88 {
89 BER_Object obj = source.get_next_object();
90
91 if(!is_asn1_string_type(obj.type())) {
92 auto typ = static_cast<uint32_t>(obj.type());
93 throw Decoding_Error(fmt("ASN1_String: Unknown string type {}", typ));
94 }
95
96 m_tag = obj.type();
97 m_data.assign(obj.bits(), obj.bits() + obj.length());
98
99 if(m_tag == ASN1_Type::BmpString) {
100 m_utf8_str = ucs2_to_utf8(m_data.data(), m_data.size());
101 } else if(m_tag == ASN1_Type::UniversalString) {
102 m_utf8_str = ucs4_to_utf8(m_data.data(), m_data.size());
103 } else if(m_tag == ASN1_Type::TeletexString) {
104 /*
105 TeletexString is nominally ITU T.61 not ISO-8859-1 but it seems
106 the majority of implementations actually used that charset here.
107 */
108 m_utf8_str = latin1_to_utf8(m_data.data(), m_data.size());
109 } else {
110 // All other supported string types are UTF-8 or some subset thereof
111 m_utf8_str = ASN1::to_string(obj);
112 }
113}
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:185
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len)
Definition charset.cpp:54
std::string latin1_to_utf8(const uint8_t chars[], size_t len)
Definition charset.cpp:89
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len)
Definition charset.cpp:70

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 419 of file asn1_obj.h.

419{ 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 75 of file asn1_str.cpp.

75 {
76 if(m_data.empty()) {
77 BOTAN_ASSERT_NOMSG(is_utf8_subset_string_type(tagging()));
78 encoder.add_object(tagging(), ASN1_Class::Universal, m_utf8_str);
79 } else {
80 // If this string was decoded, reserialize using original encoding
81 encoder.add_object(tagging(), ASN1_Class::Universal, m_data.data(), m_data.size());
82 }
83}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
ASN1_Type tagging() const
Definition asn1_obj.h:413

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

60 {
61 return is_asn1_string_type(tag);
62}

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

◆ operator==()

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

Definition at line 426 of file asn1_obj.h.

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

References value().

◆ size()

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

Definition at line 417 of file asn1_obj.h.

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

◆ tagging()

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

Definition at line 413 of file asn1_obj.h.

413{ return m_tag; }

Referenced by encode_into().

◆ value()

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

Definition at line 415 of file asn1_obj.h.

415{ 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: