Botan  1.11.15
src/lib/asn1/der_enc.h
Go to the documentation of this file.
00001 /*
00002 * DER Encoder
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_DER_ENCODER_H__
00009 #define BOTAN_DER_ENCODER_H__
00010 
00011 #include <botan/asn1_obj.h>
00012 #include <vector>
00013 
00014 namespace Botan {
00015 
00016 class BigInt;
00017 class ASN1_Object;
00018 
00019 /**
00020 * General DER Encoding Object
00021 */
00022 class BOTAN_DLL DER_Encoder
00023    {
00024    public:
00025       secure_vector<byte> get_contents();
00026 
00027       std::vector<byte> get_contents_unlocked()
00028          { return unlock(get_contents()); }
00029 
00030       DER_Encoder& start_cons(ASN1_Tag type_tag,
00031                               ASN1_Tag class_tag = UNIVERSAL);
00032       DER_Encoder& end_cons();
00033 
00034       DER_Encoder& start_explicit(u16bit type_tag);
00035       DER_Encoder& end_explicit();
00036 
00037       DER_Encoder& raw_bytes(const byte val[], size_t len);
00038       DER_Encoder& raw_bytes(const secure_vector<byte>& val);
00039       DER_Encoder& raw_bytes(const std::vector<byte>& val);
00040 
00041       DER_Encoder& encode_null();
00042       DER_Encoder& encode(bool b);
00043       DER_Encoder& encode(size_t s);
00044       DER_Encoder& encode(const BigInt& n);
00045       DER_Encoder& encode(const secure_vector<byte>& v, ASN1_Tag real_type);
00046       DER_Encoder& encode(const std::vector<byte>& v, ASN1_Tag real_type);
00047       DER_Encoder& encode(const byte val[], size_t len, ASN1_Tag real_type);
00048 
00049       DER_Encoder& encode(bool b,
00050                           ASN1_Tag type_tag,
00051                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00052 
00053       DER_Encoder& encode(size_t s,
00054                           ASN1_Tag type_tag,
00055                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00056 
00057       DER_Encoder& encode(const BigInt& n,
00058                           ASN1_Tag type_tag,
00059                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00060 
00061       DER_Encoder& encode(const std::vector<byte>& v,
00062                           ASN1_Tag real_type,
00063                           ASN1_Tag type_tag,
00064                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00065 
00066       DER_Encoder& encode(const secure_vector<byte>& v,
00067                           ASN1_Tag real_type,
00068                           ASN1_Tag type_tag,
00069                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00070 
00071       DER_Encoder& encode(const byte v[], size_t len,
00072                           ASN1_Tag real_type,
00073                           ASN1_Tag type_tag,
00074                           ASN1_Tag class_tag = CONTEXT_SPECIFIC);
00075 
00076       template<typename T>
00077       DER_Encoder& encode_optional(const T& value, const T& default_value)
00078          {
00079          if(value != default_value)
00080             encode(value);
00081          return (*this);
00082          }
00083 
00084       template<typename T>
00085       DER_Encoder& encode_list(const std::vector<T>& values)
00086          {
00087          for(size_t i = 0; i != values.size(); ++i)
00088             encode(values[i]);
00089          return (*this);
00090          }
00091 
00092       DER_Encoder& encode(const ASN1_Object& obj);
00093       DER_Encoder& encode_if(bool pred, DER_Encoder& enc);
00094       DER_Encoder& encode_if(bool pred, const ASN1_Object& obj);
00095 
00096       DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
00097                               const byte rep[], size_t length);
00098 
00099       DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
00100                               const std::vector<byte>& rep)
00101          {
00102          return add_object(type_tag, class_tag, &rep[0], rep.size());
00103          }
00104 
00105       DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
00106                               const secure_vector<byte>& rep)
00107          {
00108          return add_object(type_tag, class_tag, &rep[0], rep.size());
00109          }
00110 
00111       DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
00112                               const std::string& str);
00113 
00114       DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
00115                               byte val);
00116 
00117    private:
00118       class DER_Sequence
00119          {
00120          public:
00121             ASN1_Tag tag_of() const;
00122             secure_vector<byte> get_contents();
00123             void add_bytes(const byte[], size_t);
00124             DER_Sequence(ASN1_Tag, ASN1_Tag);
00125          private:
00126             ASN1_Tag type_tag, class_tag;
00127             secure_vector<byte> contents;
00128             std::vector< secure_vector<byte> > set_contents;
00129          };
00130 
00131       secure_vector<byte> contents;
00132       std::vector<DER_Sequence> subsequences;
00133    };
00134 
00135 }
00136 
00137 #endif