Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::ASN1_String Class Referencefinal

#include <asn1_str.h>

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

Public Member Functions

 ASN1_String (const std::string &utf8="")
 
 ASN1_String (const std::string &utf8, ASN1_Tag tag)
 
void decode_from (class BER_Decoder &) override
 
void encode_into (class DER_Encoder &) const override
 
std::string iso_8859 () const
 
ASN1_Tag tagging () const
 
const std::string & value () const
 

Static Public Member Functions

static bool is_string_type (ASN1_Tag tag)
 

Detailed Description

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

Definition at line 19 of file asn1_str.h.

Constructor & Destructor Documentation

◆ ASN1_String() [1/2]

Botan::ASN1_String::ASN1_String ( const std::string &  utf8 = "")
explicit

Definition at line 97 of file asn1_str.cpp.

97  :
98  m_utf8_str(str),
99  m_tag(choose_encoding(m_utf8_str))
100  {}

◆ ASN1_String() [2/2]

Botan::ASN1_String::ASN1_String ( const std::string &  utf8,
ASN1_Tag  tag 
)

Definition at line 84 of file asn1_str.cpp.

References Botan::DIRECTORY_STRING.

84  : m_utf8_str(str), m_tag(t)
85  {
86  if(m_tag == DIRECTORY_STRING)
87  {
88  m_tag = choose_encoding(m_utf8_str);
89  }
90 
91  assert_is_string_type(m_tag);
92  }

Member Function Documentation

◆ decode_from()

void Botan::ASN1_String::decode_from ( class 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 129 of file asn1_str.cpp.

References Botan::BMP_STRING, Botan::BER_Decoder::get_next_object(), Botan::ASN1::to_string(), Botan::BER_Object::type_tag, Botan::ucs2_to_utf8(), Botan::ucs4_to_utf8(), Botan::UNIVERSAL_STRING, and Botan::BER_Object::value.

130  {
131  BER_Object obj = source.get_next_object();
132 
133  assert_is_string_type(obj.type_tag);
134 
135  m_tag = obj.type_tag;
136  m_data.assign(obj.value.begin(), obj.value.end());
137 
138  if(m_tag == BMP_STRING)
139  {
140  m_utf8_str = ucs2_to_utf8(m_data.data(), m_data.size());
141  }
142  else if(m_tag == UNIVERSAL_STRING)
143  {
144  m_utf8_str = ucs4_to_utf8(m_data.data(), m_data.size());
145  }
146  else
147  {
148  // All other supported string types are UTF-8 or some subset thereof
149  m_utf8_str = ASN1::to_string(obj);
150  }
151  }
std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len)
Definition: charset.cpp:78
std::string ucs2_to_utf8(const uint8_t ucs2[], size_t len)
Definition: charset.cpp:61
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108

◆ encode_into()

void Botan::ASN1_String::encode_into ( class 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 113 of file asn1_str.cpp.

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

114  {
115  if(m_data.empty())
116  {
117  encoder.add_object(tagging(), UNIVERSAL, m_utf8_str);
118  }
119  else
120  {
121  // If this string was decoded, reserialize using original encoding
122  encoder.add_object(tagging(), UNIVERSAL, m_data.data(), m_data.size());
123  }
124  }
ASN1_Tag tagging() const
Definition: asn1_str.h:25

◆ is_string_type()

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

Return true iff this is a tag for a known string type we can handle. This ignores string types that are not supported, eg teletexString

Definition at line 68 of file asn1_str.cpp.

References Botan::BMP_STRING, Botan::IA5_STRING, Botan::NUMERIC_STRING, Botan::PRINTABLE_STRING, Botan::T61_STRING, Botan::UNIVERSAL_STRING, Botan::UTF8_STRING, and Botan::VISIBLE_STRING.

Referenced by Botan::AlternativeName::decode_from(), and Botan::ASN1_Formatter::print_to_stream().

69  {
70  return (tag == NUMERIC_STRING ||
71  tag == PRINTABLE_STRING ||
72  tag == VISIBLE_STRING ||
73  tag == T61_STRING ||
74  tag == IA5_STRING ||
75  tag == UTF8_STRING ||
76  tag == BMP_STRING ||
77  tag == UNIVERSAL_STRING);
78  }

◆ iso_8859()

std::string Botan::ASN1_String::iso_8859 ( ) const

Definition at line 105 of file asn1_str.cpp.

References Botan::utf8_to_latin1().

106  {
107  return utf8_to_latin1(m_utf8_str);
108  }
std::string utf8_to_latin1(const std::string &utf8)
Definition: charset.cpp:98

◆ tagging()

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

Definition at line 25 of file asn1_str.h.

Referenced by encode_into().

25 { return m_tag; }

◆ value()

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

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