Botan::PEM_Code Namespace Reference


Functions

SecureVector< bytedecode (DataSource &source, std::string &label)
SecureVector< bytedecode_check_label (DataSource &source, const std::string &label_want)
std::string encode (const MemoryRegion< byte > &data, const std::string &label, u32bit width)
std::string encode (const byte der[], u32bit length, const std::string &label, u32bit width)
bool matches (DataSource &source, const std::string &extra, u32bit search_range)

Function Documentation

BOTAN_DLL SecureVector< byte > Botan::PEM_Code::decode ( DataSource &  source,
std::string &  label 
)

Definition at line 56 of file pem.cpp.

References Botan::Pipe::end_msg(), Botan::Pipe::read_all(), Botan::DataSource::read_byte(), Botan::Pipe::start_msg(), and Botan::Pipe::write().

Referenced by decode_check_label(), and Botan::DL_Group::PEM_decode().

00057    {
00058    const u32bit RANDOM_CHAR_LIMIT = 8;
00059 
00060    const std::string PEM_HEADER1 = "-----BEGIN ";
00061    const std::string PEM_HEADER2 = "-----";
00062    u32bit position = 0;
00063 
00064    while(position != PEM_HEADER1.length())
00065       {
00066       byte b;
00067       if(!source.read_byte(b))
00068          throw Decoding_Error("PEM: No PEM header found");
00069       if(b == PEM_HEADER1[position])
00070          ++position;
00071       else if(position >= RANDOM_CHAR_LIMIT)
00072          throw Decoding_Error("PEM: Malformed PEM header");
00073       else
00074          position = 0;
00075       }
00076    position = 0;
00077    while(position != PEM_HEADER2.length())
00078       {
00079       byte b;
00080       if(!source.read_byte(b))
00081          throw Decoding_Error("PEM: No PEM header found");
00082       if(b == PEM_HEADER2[position])
00083          ++position;
00084       else if(position)
00085          throw Decoding_Error("PEM: Malformed PEM header");
00086 
00087       if(position == 0)
00088          label += static_cast<char>(b);
00089       }
00090 
00091    Pipe base64(new Base64_Decoder);
00092    base64.start_msg();
00093 
00094    const std::string PEM_TRAILER = "-----END " + label + "-----";
00095    position = 0;
00096    while(position != PEM_TRAILER.length())
00097       {
00098       byte b;
00099       if(!source.read_byte(b))
00100          throw Decoding_Error("PEM: No PEM trailer found");
00101       if(b == PEM_TRAILER[position])
00102          ++position;
00103       else if(position)
00104          throw Decoding_Error("PEM: Malformed PEM trailer");
00105 
00106       if(position == 0)
00107          base64.write(b);
00108       }
00109    base64.end_msg();
00110    return base64.read_all();
00111    }

BOTAN_DLL SecureVector< byte > Botan::PEM_Code::decode_check_label ( DataSource &  source,
const std::string &  label_want 
)

Definition at line 42 of file pem.cpp.

References decode().

Referenced by Botan::CMS_Decoder::CMS_Decoder(), Botan::cryptobox_decrypt(), Botan::EC_Domain_Params::EC_Domain_Params(), Botan::X509::load_key(), and Botan::PKCS10_Request::raw_public_key().

00044    {
00045    std::string label_got;
00046    SecureVector<byte> ber = decode(source, label_got);
00047    if(label_got != label_want)
00048       throw Decoding_Error("PEM: Label mismatch, wanted " + label_want +
00049                            ", got " + label_got);
00050    return ber;
00051    }

BOTAN_DLL std::string Botan::PEM_Code::encode ( const MemoryRegion< byte > &  data,
const std::string &  label,
u32bit  width 
)

Definition at line 33 of file pem.cpp.

References encode(), and Botan::MemoryRegion< T >::size().

00035    {
00036    return encode(data, data.size(), label, width);
00037    }

BOTAN_DLL std::string Botan::PEM_Code::encode ( const byte  der[],
u32bit  length,
const std::string &  label,
u32bit  width 
)

Definition at line 19 of file pem.cpp.

References Botan::Pipe::process_msg(), and Botan::Pipe::read_all_as_string().

Referenced by Botan::cryptobox_encrypt(), and encode().

00021    {
00022    const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n";
00023    const std::string PEM_TRAILER = "-----END " + label + "-----\n";
00024 
00025    Pipe pipe(new Base64_Encoder(true, width));
00026    pipe.process_msg(der, length);
00027    return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER);
00028    }

BOTAN_DLL bool Botan::PEM_Code::matches ( DataSource &  source,
const std::string &  extra,
u32bit  search_range 
)

Definition at line 116 of file pem.cpp.

References Botan::DataSource::peek(), and Botan::MemoryRegion< T >::size().

Referenced by Botan::CMS_Decoder::CMS_Decoder(), Botan::create_alt_name(), and Botan::X509::load_key().

00118    {
00119    const std::string PEM_HEADER = "-----BEGIN " + extra;
00120 
00121    SecureVector<byte> search_buf(search_range);
00122    u32bit got = source.peek(search_buf, search_buf.size(), 0);
00123 
00124    if(got < PEM_HEADER.length())
00125       return false;
00126 
00127    u32bit index = 0;
00128 
00129    for(u32bit j = 0; j != got; ++j)
00130       {
00131       if(search_buf[j] == PEM_HEADER[index])
00132          ++index;
00133       else
00134          index = 0;
00135       if(index == PEM_HEADER.size())
00136          return true;
00137       }
00138    return false;
00139    }


Generated on Tue Jun 29 08:56:47 2010 for Botan by  doxygen 1.5.8