Botan
1.11.15
|
00001 /* 00002 * BER Decoder 00003 * (C) 1999-2010 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_BER_DECODER_H__ 00009 #define BOTAN_BER_DECODER_H__ 00010 00011 #include <botan/asn1_oid.h> 00012 #include <botan/data_src.h> 00013 00014 namespace Botan { 00015 00016 /** 00017 * BER Decoding Object 00018 */ 00019 class BOTAN_DLL BER_Decoder 00020 { 00021 public: 00022 BER_Object get_next_object(); 00023 00024 std::vector<byte> get_next_octet_string(); 00025 00026 void push_back(const BER_Object& obj); 00027 00028 bool more_items() const; 00029 BER_Decoder& verify_end(); 00030 BER_Decoder& discard_remaining(); 00031 00032 BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag = UNIVERSAL); 00033 BER_Decoder& end_cons(); 00034 00035 BER_Decoder& get_next(BER_Object& ber); 00036 00037 BER_Decoder& raw_bytes(secure_vector<byte>& v); 00038 BER_Decoder& raw_bytes(std::vector<byte>& v); 00039 00040 BER_Decoder& decode_null(); 00041 BER_Decoder& decode(bool& v); 00042 BER_Decoder& decode(size_t& v); 00043 BER_Decoder& decode(class BigInt& v); 00044 BER_Decoder& decode(std::vector<byte>& v, ASN1_Tag type_tag); 00045 BER_Decoder& decode(secure_vector<byte>& v, ASN1_Tag type_tag); 00046 00047 BER_Decoder& decode(bool& v, 00048 ASN1_Tag type_tag, 00049 ASN1_Tag class_tag = CONTEXT_SPECIFIC); 00050 00051 BER_Decoder& decode(size_t& v, 00052 ASN1_Tag type_tag, 00053 ASN1_Tag class_tag = CONTEXT_SPECIFIC); 00054 00055 BER_Decoder& decode(class BigInt& v, 00056 ASN1_Tag type_tag, 00057 ASN1_Tag class_tag = CONTEXT_SPECIFIC); 00058 00059 BER_Decoder& decode(std::vector<byte>& v, 00060 ASN1_Tag real_type, 00061 ASN1_Tag type_tag, 00062 ASN1_Tag class_tag = CONTEXT_SPECIFIC); 00063 00064 BER_Decoder& decode(secure_vector<byte>& v, 00065 ASN1_Tag real_type, 00066 ASN1_Tag type_tag, 00067 ASN1_Tag class_tag = CONTEXT_SPECIFIC); 00068 00069 BER_Decoder& decode(class ASN1_Object& obj, 00070 ASN1_Tag type_tag = NO_OBJECT, 00071 ASN1_Tag class_tag = NO_OBJECT); 00072 00073 BER_Decoder& decode_octet_string_bigint(class BigInt& b); 00074 00075 u64bit decode_constrained_integer(ASN1_Tag type_tag, 00076 ASN1_Tag class_tag, 00077 size_t T_bytes); 00078 00079 template<typename T> BER_Decoder& decode_integer_type(T& out) 00080 { 00081 return decode_integer_type<T>(out, INTEGER, UNIVERSAL); 00082 } 00083 00084 template<typename T> 00085 BER_Decoder& decode_integer_type(T& out, 00086 ASN1_Tag type_tag, 00087 ASN1_Tag class_tag = CONTEXT_SPECIFIC) 00088 { 00089 out = decode_constrained_integer(type_tag, class_tag, sizeof(out)); 00090 return (*this); 00091 } 00092 00093 template<typename T> 00094 BER_Decoder& decode_optional(T& out, 00095 ASN1_Tag type_tag, 00096 ASN1_Tag class_tag, 00097 const T& default_value = T()); 00098 00099 template<typename T> 00100 BER_Decoder& decode_optional_implicit( 00101 T& out, 00102 ASN1_Tag type_tag, 00103 ASN1_Tag class_tag, 00104 ASN1_Tag real_type, 00105 ASN1_Tag real_class, 00106 const T& default_value = T()); 00107 00108 template<typename T> 00109 BER_Decoder& decode_list(std::vector<T>& out, 00110 ASN1_Tag type_tag = SEQUENCE, 00111 ASN1_Tag class_tag = UNIVERSAL); 00112 00113 template<typename T> 00114 BER_Decoder& decode_and_check(const T& expected, 00115 const std::string& error_msg) 00116 { 00117 T actual; 00118 decode(actual); 00119 00120 if(actual != expected) 00121 throw Decoding_Error(error_msg); 00122 00123 return (*this); 00124 } 00125 00126 /* 00127 * Decode an OPTIONAL string type 00128 */ 00129 template<typename Alloc> 00130 BER_Decoder& decode_optional_string(std::vector<byte, Alloc>& out, 00131 ASN1_Tag real_type, 00132 u16bit type_no, 00133 ASN1_Tag class_tag = CONTEXT_SPECIFIC) 00134 { 00135 BER_Object obj = get_next_object(); 00136 00137 ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no); 00138 00139 if(obj.type_tag == type_tag && obj.class_tag == class_tag) 00140 { 00141 if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC)) 00142 BER_Decoder(obj.value).decode(out, real_type).verify_end(); 00143 else 00144 { 00145 push_back(obj); 00146 decode(out, real_type, type_tag, class_tag); 00147 } 00148 } 00149 else 00150 { 00151 out.clear(); 00152 push_back(obj); 00153 } 00154 00155 return (*this); 00156 } 00157 00158 BER_Decoder& operator=(const BER_Decoder&) = delete; 00159 00160 BER_Decoder(DataSource&); 00161 00162 BER_Decoder(const byte[], size_t); 00163 00164 BER_Decoder(const secure_vector<byte>&); 00165 00166 BER_Decoder(const std::vector<byte>& vec); 00167 00168 BER_Decoder(const BER_Decoder&); 00169 ~BER_Decoder(); 00170 private: 00171 BER_Decoder* parent; 00172 DataSource* source; 00173 BER_Object pushed; 00174 mutable bool owns; 00175 }; 00176 00177 /* 00178 * Decode an OPTIONAL or DEFAULT element 00179 */ 00180 template<typename T> 00181 BER_Decoder& BER_Decoder::decode_optional(T& out, 00182 ASN1_Tag type_tag, 00183 ASN1_Tag class_tag, 00184 const T& default_value) 00185 { 00186 BER_Object obj = get_next_object(); 00187 00188 if(obj.type_tag == type_tag && obj.class_tag == class_tag) 00189 { 00190 if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC)) 00191 BER_Decoder(obj.value).decode(out).verify_end(); 00192 else 00193 { 00194 push_back(obj); 00195 decode(out, type_tag, class_tag); 00196 } 00197 } 00198 else 00199 { 00200 out = default_value; 00201 push_back(obj); 00202 } 00203 00204 return (*this); 00205 } 00206 00207 /* 00208 * Decode an OPTIONAL or DEFAULT element 00209 */ 00210 template<typename T> 00211 BER_Decoder& BER_Decoder::decode_optional_implicit( 00212 T& out, 00213 ASN1_Tag type_tag, 00214 ASN1_Tag class_tag, 00215 ASN1_Tag real_type, 00216 ASN1_Tag real_class, 00217 const T& default_value) 00218 { 00219 BER_Object obj = get_next_object(); 00220 00221 if(obj.type_tag == type_tag && obj.class_tag == class_tag) 00222 { 00223 obj.type_tag = real_type; 00224 obj.class_tag = real_class; 00225 push_back(obj); 00226 decode(out, real_type, real_class); 00227 } 00228 else 00229 { 00230 out = default_value; 00231 push_back(obj); 00232 } 00233 00234 return (*this); 00235 } 00236 /* 00237 * Decode a list of homogenously typed values 00238 */ 00239 template<typename T> 00240 BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, 00241 ASN1_Tag type_tag, 00242 ASN1_Tag class_tag) 00243 { 00244 BER_Decoder list = start_cons(type_tag, class_tag); 00245 00246 while(list.more_items()) 00247 { 00248 T value; 00249 list.decode(value); 00250 vec.push_back(value); 00251 } 00252 00253 list.end_cons(); 00254 00255 return (*this); 00256 } 00257 00258 } 00259 00260 #endif