00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <botan/eac_asn_obj.h>
00010 #include <botan/der_enc.h>
00011 #include <botan/ber_dec.h>
00012 #include <botan/charset.h>
00013 #include <botan/parsing.h>
00014 #include <sstream>
00015
00016 namespace Botan {
00017
00018
00019
00020
00021 ASN1_EAC_String::ASN1_EAC_String(const std::string& str, ASN1_Tag t) : tag(t)
00022 {
00023 iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
00024 if (!sanity_check())
00025 {
00026 throw Invalid_Argument("attempted to construct ASN1_EAC_String with illegal characters");
00027 }
00028 }
00029
00030
00031
00032
00033 std::string ASN1_EAC_String::iso_8859() const
00034 {
00035 return iso_8859_str;
00036 }
00037
00038
00039
00040
00041 std::string ASN1_EAC_String::value() const
00042 {
00043 return Charset::transcode(iso_8859_str, LATIN1_CHARSET, LOCAL_CHARSET);
00044 }
00045
00046
00047
00048
00049 ASN1_Tag ASN1_EAC_String::tagging() const
00050 {
00051 return tag;
00052 }
00053
00054
00055
00056
00057 void ASN1_EAC_String::encode_into(DER_Encoder& encoder) const
00058 {
00059 std::string value = iso_8859();
00060 encoder.add_object(tagging(), APPLICATION, value);
00061 }
00062
00063
00064
00065
00066 void ASN1_EAC_String::decode_from(BER_Decoder& source)
00067 {
00068 BER_Object obj = source.get_next_object();
00069 if (obj.type_tag != this->tag)
00070 {
00071
00072 std::string message("decoding type mismatch for ASN1_EAC_String, tag is ");
00073 std::stringstream ss;
00074 std::string str_is;
00075 ss << std::hex << obj.type_tag;
00076 ss >> str_is;
00077 message.append(str_is);
00078 message.append(", while it should be ");
00079 std::stringstream ss2;
00080 std::string str_should;
00081 ss2 << std::hex << this->tag;
00082 ss2 >> str_should;
00083 message.append(str_should);
00084 throw Decoding_Error(message);
00085 }
00086 Character_Set charset_is;
00087 charset_is = LATIN1_CHARSET;
00088
00089 try
00090 {
00091 *this = ASN1_EAC_String(
00092 Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET),
00093 obj.type_tag);
00094 }
00095 catch (Invalid_Argument inv_arg)
00096 {
00097 throw Decoding_Error(std::string("error while decoding ASN1_EAC_String: ") + std::string(inv_arg.what()));
00098 }
00099 }
00100
00101
00102
00103 bool ASN1_EAC_String::sanity_check() const
00104 {
00105 const byte* rep = reinterpret_cast<const byte*>(iso_8859_str.data());
00106 const u32bit rep_len = iso_8859_str.size();
00107 for (u32bit i=0; i<rep_len; i++)
00108 {
00109 if ((rep[i] < 0x20) || ((rep[i] >= 0x7F) && (rep[i] < 0xA0)))
00110 {
00111 return false;
00112 }
00113 }
00114 return true;
00115 }
00116
00117 bool operator==(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs)
00118 {
00119 return (lhs.iso_8859() == rhs.iso_8859());
00120 }
00121
00122 ASN1_Car::ASN1_Car(std::string const& str)
00123 : ASN1_EAC_String(str, ASN1_Tag(2))
00124 {}
00125
00126 ASN1_Chr::ASN1_Chr(std::string const& str)
00127 : ASN1_EAC_String(str, ASN1_Tag(32))
00128 {}
00129
00130 }