Botan  1.11.15
src/lib/cert/cvc/asn1_eac_str.cpp
Go to the documentation of this file.
00001 /*
00002 * Simple ASN.1 String Types
00003 * (C) 2007 FlexSecure GmbH
00004 *     2008-2011 Jack Lloyd
00005 *
00006 * Botan is released under the Simplified BSD License (see license.txt)
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 #include <ios>
00016 
00017 namespace Botan {
00018 
00019 /*
00020 * Create an ASN1_EAC_String
00021 */
00022 ASN1_EAC_String::ASN1_EAC_String(const std::string& str, ASN1_Tag t) : tag(t)
00023    {
00024    iso_8859_str = Charset::transcode(str, LOCAL_CHARSET, LATIN1_CHARSET);
00025 
00026    if(!sanity_check())
00027       throw Invalid_Argument("ASN1_EAC_String contains illegal characters");
00028    }
00029 
00030 /*
00031 * Return this string in ISO 8859-1 encoding
00032 */
00033 std::string ASN1_EAC_String::iso_8859() const
00034    {
00035    return iso_8859_str;
00036    }
00037 
00038 /*
00039 * Return this string in local encoding
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 * Return the type of this string object
00048 */
00049 ASN1_Tag ASN1_EAC_String::tagging() const
00050    {
00051    return tag;
00052    }
00053 
00054 /*
00055 * DER encode an ASN1_EAC_String
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 * Decode a BER encoded ASN1_EAC_String
00065 */
00066 void ASN1_EAC_String::decode_from(BER_Decoder& source)
00067    {
00068    BER_Object obj = source.get_next_object();
00069 
00070    if(obj.type_tag != this->tag)
00071       {
00072       std::stringstream ss;
00073 
00074       ss << "ASN1_EAC_String tag mismatch, tag was "
00075          << std::hex << obj.type_tag
00076          << " expected "
00077          << std::hex << this->tag;
00078 
00079       throw Decoding_Error(ss.str());
00080       }
00081 
00082    Character_Set charset_is;
00083    charset_is = LATIN1_CHARSET;
00084 
00085    try
00086       {
00087       *this = ASN1_EAC_String(
00088          Charset::transcode(ASN1::to_string(obj), charset_is, LOCAL_CHARSET),
00089          obj.type_tag);
00090       }
00091    catch(Invalid_Argument& inv_arg)
00092       {
00093       throw Decoding_Error(std::string("ASN1_EAC_String decoding failed: ") +
00094                            inv_arg.what());
00095       }
00096    }
00097 
00098 // checks for compliance to the alphabet defined in TR-03110 v1.10, 2007-08-20
00099 // p. 43
00100 bool ASN1_EAC_String::sanity_check() const
00101    {
00102    const byte* rep = reinterpret_cast<const byte*>(iso_8859_str.data());
00103    const size_t rep_len = iso_8859_str.size();
00104 
00105    for(size_t i = 0; i != rep_len; ++i)
00106       {
00107       if((rep[i] < 0x20) || ((rep[i] >= 0x7F) && (rep[i] < 0xA0)))
00108          return false;
00109       }
00110 
00111    return true;
00112    }
00113 
00114 bool operator==(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs)
00115    {
00116    return (lhs.iso_8859() == rhs.iso_8859());
00117    }
00118 
00119 ASN1_Car::ASN1_Car(std::string const& str)
00120    : ASN1_EAC_String(str, ASN1_Tag(2))
00121    {}
00122 
00123 ASN1_Chr::ASN1_Chr(std::string const& str)
00124    : ASN1_EAC_String(str, ASN1_Tag(32))
00125    {}
00126 
00127 }