monotone

monotone Mtn Source Tree

Root/botan/asn1_str.cpp

1/*************************************************
2* Simple ASN.1 String Types Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/asn1_obj.h>
7#include <botan/der_enc.h>
8#include <botan/ber_dec.h>
9#include <botan/charset.h>
10#include <botan/parsing.h>
11#include <botan/config.h>
12
13namespace Botan {
14
15namespace {
16
17/*************************************************
18* Choose an encoding for the string *
19*************************************************/
20ASN1_Tag choose_encoding(const std::string& str)
21 {
22 static const byte IS_PRINTABLE[256] = {
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,
27 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
28 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
29 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
30 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
32 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
33 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 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 };
45
46 for(u32bit j = 0; j != str.size(); ++j)
47 if(!IS_PRINTABLE[static_cast<byte>(str[j])])
48 {
49 const std::string type = global_config().option("x509/ca/str_type");
50
51 if(type == "utf8") return UTF8_STRING;
52 if(type == "latin1") return T61_STRING;
53 throw Invalid_Argument("Bad setting for x509/ca/str_type: " + type);
54 }
55 return PRINTABLE_STRING;
56 }
57
58}
59
60/*************************************************
61* Check if type is a known ASN.1 string type *
62*************************************************/
63bool is_string_type(ASN1_Tag tag)
64 {
65 if(tag == NUMERIC_STRING || tag == PRINTABLE_STRING ||
66 tag == VISIBLE_STRING || tag == T61_STRING || tag == IA5_STRING ||
67 tag == UTF8_STRING || tag == BMP_STRING)
68 return true;
69 return false;
70 }
71
72/*************************************************
73* Create an ASN1_String *
74*************************************************/
75ASN1_String::ASN1_String(const std::string& str, ASN1_Tag t) : tag(t)
76 {
77 iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
78
79 if(tag == DIRECTORY_STRING)
80 tag = choose_encoding(iso_8859_str);
81
82 if(tag != NUMERIC_STRING &&
83 tag != PRINTABLE_STRING &&
84 tag != VISIBLE_STRING &&
85 tag != T61_STRING &&
86 tag != IA5_STRING &&
87 tag != UTF8_STRING &&
88 tag != BMP_STRING)
89 throw Invalid_Argument("ASN1_String: Unknown string type " +
90 to_string(tag));
91 }
92
93/*************************************************
94* Create an ASN1_String *
95*************************************************/
96ASN1_String::ASN1_String(const std::string& str)
97 {
98 iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
99 tag = choose_encoding(iso_8859_str);
100 }
101
102/*************************************************
103* Return this string in ISO 8859-1 encoding *
104*************************************************/
105std::string ASN1_String::iso_8859() const
106 {
107 return iso_8859_str;
108 }
109
110/*************************************************
111* Return this string in local encoding *
112*************************************************/
113std::string ASN1_String::value() const
114 {
115 return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET);
116 }
117
118/*************************************************
119* Return the type of this string object *
120*************************************************/
121ASN1_Tag ASN1_String::tagging() const
122 {
123 return tag;
124 }
125
126/*************************************************
127* DER encode an ASN1_String *
128*************************************************/
129void ASN1_String::encode_into(DER_Encoder& encoder) const
130 {
131 std::string value = iso_8859();
132 if(tagging() == UTF8_STRING)
133 value = Charset::transcode(value, LATIN1_CHARSET, UTF8_CHARSET);
134 encoder.add_object(tagging(), UNIVERSAL, value);
135 }
136
137/*************************************************
138* Decode a BER encoded ASN1_String *
139*************************************************/
140void ASN1_String::decode_from(BER_Decoder& source)
141 {
142 BER_Object obj = source.get_next_object();
143
144 Character_Set charset_is;
145
146 if(obj.type_tag == BMP_STRING)
147 charset_is = UCS2_CHARSET;
148 else if(obj.type_tag == UTF8_STRING)
149 charset_is = UTF8_CHARSET;
150 else
151 charset_is = LATIN1_CHARSET;
152
153 *this = ASN1_String(
154 Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET),
155 obj.type_tag);
156 }
157
158}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status