Botan  1.10.9
Functions
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

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 Botan::BER_Decoder::decode_and_check(), decode_check_label(), Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(), and Botan::DL_Group::PEM_decode().

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  }
unsigned char byte
Definition: types.h:22
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_Group::EC_Group(), Botan::X509::load_key(), and Botan::PKCS10_Request::raw_public_key().

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
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.

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

Referenced by Botan::CMS_Encoder::compress(), Botan::X942_PRF::derive(), encode(), Botan::DER_Encoder::encode_list(), Botan::DER_Encoder::encode_optional(), Botan::CryptoBox::encrypt(), Botan::CMS_Encoder::get_contents(), Botan::X509_CA::make_cert(), Botan::CMS_Encoder::PEM_contents(), Botan::X509::PEM_encode(), Botan::DL_Group::PEM_encode(), Botan::EC_Group::PEM_encode(), Botan::X509_Object::PEM_encode(), and Botan::PKCS8::PEM_encode().

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  }
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.

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

35  {
36  return encode(&data[0], data.size(), label, width);
37  }
std::string encode(const MemoryRegion< byte > &data, const std::string &label, size_t width)
Definition: pem.cpp:33
size_t size() const
Definition: secmem.h:29
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.

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().

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  }