Botan: src/asn1/asn1_str.cpp Source File

Botan 1.10.16

asn1_str.cpp
Go to the documentation of this file.
1 /*
2 * Simple ASN.1 String Types
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/asn1_str.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/charset.h>
12 #include <botan/parsing.h>
13
14 namespace Botan {
15
16 namespace {
17
18 /*
19 * Choose an encoding for the string
20 */
21 ASN1_Tag choose_encoding(const std::string& str,
22 const std::string& type)
23 {
24 static const byte IS_PRINTABLE[256] = {
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
28 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
29 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
30 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
31 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
32 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
34 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
35 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00 };
47
48 for(size_t i = 0; i != str.size(); ++i)
49 {
50 if(!IS_PRINTABLE[static_cast<byte>(str[i])])
51 {
52 if(type == "utf8") return UTF8_STRING;
53 if(type == "latin1") return T61_STRING;
54 throw Invalid_Argument("choose_encoding: Bad string type " + type);
55 }
56 }
57 return PRINTABLE_STRING;
58 }
59
60 }
61
62 /*
63 * Create an ASN1_String
64 */
65 ASN1_String::ASN1_String(const std::string& str, ASN1_Tag t) : tag(t)
66 {
67 iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
68
69 if(tag == DIRECTORY_STRING)
70 tag = choose_encoding(iso_8859_str, "latin1");
71
72 if(tag != NUMERIC_STRING &&
73 tag != PRINTABLE_STRING &&
74 tag != VISIBLE_STRING &&
75 tag != T61_STRING &&
76 tag != IA5_STRING &&
77 tag != UTF8_STRING &&
78 tag != BMP_STRING)
79 throw Invalid_Argument("ASN1_String: Unknown string type " +
80 to_string(tag));
81 }
82
83 /*
84 * Create an ASN1_String
85 */
86 ASN1_String::ASN1_String(const std::string& str)
87 {
88 iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
89 tag = choose_encoding(iso_8859_str, "latin1");
90 }
91
92 /*
93 * Return this string in ISO 8859-1 encoding
94 */
95 std::string ASN1_String::iso_8859() const
96 {
97 return iso_8859_str;
98 }
99
100 /*
101 * Return this string in local encoding
102 */
103 std::string ASN1_String::value() const
104 {
105 return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET);
106 }
107
108 /*
109 * Return the type of this string object
110 */
111 ASN1_Tag ASN1_String::tagging() const
112 {
113 return tag;
114 }
115
116 /*
117 * DER encode an ASN1_String
118 */
119 void ASN1_String::encode_into(DER_Encoder& encoder) const
120 {
121 std::string value = iso_8859();
122 if(tagging() == UTF8_STRING)
123 value = Charset::transcode(value, LATIN1_CHARSET, UTF8_CHARSET);
124 encoder.add_object(tagging(), UNIVERSAL, value);
125 }
126
127 /*
128 * Decode a BER encoded ASN1_String
129 */
130 void ASN1_String::decode_from(BER_Decoder& source)
131 {
132 BER_Object obj = source.get_next_object();
133
134 Character_Set charset_is;
135
136 if(obj.type_tag == BMP_STRING)
137 charset_is = UCS2_CHARSET;
138 else if(obj.type_tag == UTF8_STRING)
139 charset_is = UTF8_CHARSET;
140 else
141 charset_is = LATIN1_CHARSET;
142
143 *this = ASN1_String(
144 Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET),
145 obj.type_tag);
146 }
147
148 }
Botan::ASN1_String::iso_8859
std::string iso_8859() const
Definition: asn1_str.cpp:95
Botan::BMP_STRING
Definition: asn1_int.h:44
Botan::ASN1_String::encode_into
void encode_into(class DER_Encoder &) const
Definition: asn1_str.cpp:119
Botan::UTF8_CHARSET
Definition: charset.h:22
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_int.cpp:46
Botan::PRINTABLE_STRING
Definition: asn1_int.h:40
Botan::Character_Set
Character_Set
Definition: charset.h:19
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::Charset::transcode
std::string transcode(const std::string &str, Character_Set to, Character_Set from)
Definition: charset.cpp:103
Botan::ASN1_String::value
std::string value() const
Definition: asn1_str.cpp:103
Botan::T61_STRING
Definition: asn1_int.h:41
Botan::ASN1_String::ASN1_String
ASN1_String(const std::string &="")
Definition: asn1_str.cpp:86
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_int.h:19
Botan::LATIN1_CHARSET
Definition: charset.h:23
Botan
Definition: algo_base.h:14
Botan::DIRECTORY_STRING
Definition: asn1_int.h:50
Botan::ASN1_String::decode_from
void decode_from(class BER_Decoder &)
Definition: asn1_str.cpp:130
Botan::BER_Decoder::get_next_object
BER_Object get_next_object()
Definition: ber_dec.cpp:196
Botan::VISIBLE_STRING
Definition: asn1_int.h:43
Botan::LOCAL_CHARSET
Definition: charset.h:20
Botan::BER_Object::type_tag
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::NUMERIC_STRING
Definition: asn1_int.h:39
Botan::UTF8_STRING
Definition: asn1_int.h:38
Botan::ASN1_String::tagging
ASN1_Tag tagging() const
Definition: asn1_str.cpp:111
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::BER_Object
Definition: asn1_int.h:77
Botan::IA5_STRING
Definition: asn1_int.h:42
Botan::UCS2_CHARSET
Definition: charset.h:21
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346