Botan 1.10.17
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 byte der[], size_t length, const std::string &label, size_t width)
std::string encode (const MemoryRegion< byte > &data, const std::string &label, size_t width)
bool matches (DataSource &source, const std::string &extra, size_t search_range)

Function Documentation

◆ decode()

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

Definition at line 56 of file pem.cpp.

57 {
58 const size_t RANDOM_CHAR_LIMIT = 8;
59
60 const std::string PEM_HEADER1 = "-----BEGIN ";
61 const std::string PEM_HEADER2 = "-----";
62 size_t position = 0;
63
64 while(position != PEM_HEADER1.length())
65 {
66 byte b;
67 if(!source.read_byte(b))
68 throw Decoding_Error("PEM: No PEM header found");
69 if(b == PEM_HEADER1[position])
70 ++position;
71 else if(position >= RANDOM_CHAR_LIMIT)
72 throw Decoding_Error("PEM: Malformed PEM header");
73 else
74 position = 0;
75 }
76 position = 0;
77 while(position != PEM_HEADER2.length())
78 {
79 byte b;
80 if(!source.read_byte(b))
81 throw Decoding_Error("PEM: No PEM header found");
82 if(b == PEM_HEADER2[position])
83 ++position;
84 else if(position)
85 throw Decoding_Error("PEM: Malformed PEM header");
86
87 if(position == 0)
88 label += static_cast<char>(b);
89 }
90
91 Pipe base64(new Base64_Decoder);
92 base64.start_msg();
93
94 const std::string PEM_TRAILER = "-----END " + label + "-----";
95 position = 0;
96 while(position != PEM_TRAILER.length())
97 {
98 byte b;
99 if(!source.read_byte(b))
100 throw Decoding_Error("PEM: No PEM trailer found");
101 if(b == PEM_TRAILER[position])
102 ++position;
103 else if(position)
104 throw Decoding_Error("PEM: Malformed PEM trailer");
105
106 if(position == 0)
107 base64.write(b);
108 }
109 base64.end_msg();
110 return base64.read_all();
111 }
size_t read_byte(byte &out)
Definition data_src.cpp:19
Decoding_Error(const std::string &name)
Definition exceptn.h:140

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

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

◆ decode_check_label()

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.

44 {
45 std::string label_got;
46 SecureVector<byte> ber = decode(source, label_got);
47 if(label_got != label_want)
48 throw Decoding_Error("PEM: Label mismatch, wanted " + label_want +
49 ", got " + label_got);
50 return ber;
51 }
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition pem.cpp:56

References decode(), decode_check_label(), and Botan::Decoding_Error::Decoding_Error().

Referenced by Botan::CMS_Decoder::CMS_Decoder(), decode_check_label(), Botan::CryptoBox::decrypt(), Botan::EC_Group::EC_Group(), Botan::X509::load_key(), and Botan::PKCS10_Request::raw_public_key().

◆ encode() [1/2]

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

Definition at line 19 of file pem.cpp.

21 {
22 const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n";
23 const std::string PEM_TRAILER = "-----END " + label + "-----\n";
24
25 Pipe pipe(new Base64_Encoder(true, width));
26 pipe.process_msg(der, length);
27 return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER);
28 }
Base64_Encoder(bool breaks=false, size_t length=72, bool t_n=false)
Definition b64_filt.cpp:19

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

Referenced by encode(), encode(), Botan::CryptoBox::encrypt(), Botan::CMS_Encoder::PEM_contents(), Botan::DL_Group::PEM_encode(), Botan::EC_Group::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::PKCS8::PEM_encode(), Botan::X509::PEM_encode(), and Botan::X509_Object::PEM_encode().

◆ encode() [2/2]

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

Definition at line 33 of file pem.cpp.

35 {
36 return encode(&data[0], data.size(), label, width);
37 }
size_t size() const
Definition secmem.h:29
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19

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

◆ matches()

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

Definition at line 116 of file pem.cpp.

118 {
119 const std::string PEM_HEADER = "-----BEGIN " + extra;
120
121 SecureVector<byte> search_buf(search_range);
122 size_t got = source.peek(&search_buf[0], search_buf.size(), 0);
123
124 if(got < PEM_HEADER.length())
125 return false;
126
127 size_t index = 0;
128
129 for(size_t j = 0; j != got; ++j)
130 {
131 if(search_buf[j] == PEM_HEADER[index])
132 ++index;
133 else
134 index = 0;
135 if(index == PEM_HEADER.size())
136 return true;
137 }
138 return false;
139 }
virtual size_t peek(byte out[], size_t length, size_t peek_offset) const =0

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

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