aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/misc/pem
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-10-14 16:32:17 -0400
committerJack Lloyd <[email protected]>2015-10-14 16:32:17 -0400
commit39052451400dd9beb12c350d87c2d48ff476210e (patch)
tree0b0a87bcaf47c70e48df39808cd70c549d71668f /src/lib/misc/pem
parentaad256035d4ecb9c4e87a7698f74f6f3178da0e2 (diff)
Move DataSource to utils and rewrite PEM encoding to avoid filters
Removes filters as as an internal dependency pretty much entirely (outside of some dusty corners in misc).
Diffstat (limited to 'src/lib/misc/pem')
-rw-r--r--src/lib/misc/pem/info.txt2
-rw-r--r--src/lib/misc/pem/pem.cpp41
2 files changed, 31 insertions, 12 deletions
diff --git a/src/lib/misc/pem/info.txt b/src/lib/misc/pem/info.txt
index c614b5ca7..9340a7cef 100644
--- a/src/lib/misc/pem/info.txt
+++ b/src/lib/misc/pem/info.txt
@@ -1,5 +1,5 @@
define PEM_CODEC 20131128
<requires>
-codec_filt
+base64
</requires>
diff --git a/src/lib/misc/pem/pem.cpp b/src/lib/misc/pem/pem.cpp
index f33016c70..1279b665e 100644
--- a/src/lib/misc/pem/pem.cpp
+++ b/src/lib/misc/pem/pem.cpp
@@ -6,25 +6,45 @@
*/
#include <botan/pem.h>
-#include <botan/filters.h>
+#include <botan/base64.h>
#include <botan/parsing.h>
namespace Botan {
namespace PEM_Code {
+namespace {
+
+std::string linewrap(size_t width, const std::string& in)
+ {
+ std::string out;
+ for(size_t i = 0; i != in.size(); ++i)
+ {
+ if(i > 0 && i % width == 0)
+ {
+ out.push_back('\n');
+ }
+ out.push_back(in[i]);
+ }
+ if(out.size() > 0 && out[out.size()-1] != '\n')
+ {
+ out.push_back('\n');
+ }
+
+ return out;
+ }
+
+}
+
/*
* PEM encode BER/DER-encoded objects
*/
-std::string encode(const byte der[], size_t length, const std::string& label,
- size_t width)
+std::string encode(const byte der[], size_t length, const std::string& label, size_t width)
{
const std::string PEM_HEADER = "-----BEGIN " + label + "-----\n";
const std::string PEM_TRAILER = "-----END " + label + "-----\n";
- Pipe pipe(new Base64_Encoder(true, width));
- pipe.process_msg(der, length);
- return (PEM_HEADER + pipe.read_all_as_string() + PEM_TRAILER);
+ return (PEM_HEADER + linewrap(width, base64_encode(der, length)) + PEM_TRAILER);
}
/*
@@ -79,8 +99,7 @@ secure_vector<byte> decode(DataSource& source, std::string& label)
label += static_cast<char>(b);
}
- Pipe base64(new Base64_Decoder);
- base64.start_msg();
+ std::vector<char> b64;
const std::string PEM_TRAILER = "-----END " + label + "-----";
position = 0;
@@ -95,10 +114,10 @@ secure_vector<byte> decode(DataSource& source, std::string& label)
throw Decoding_Error("PEM: Malformed PEM trailer");
if(position == 0)
- base64.write(b);
+ b64.push_back(b);
}
- base64.end_msg();
- return base64.read_all();
+
+ return base64_decode(b64.data(), b64.size());
}
secure_vector<byte> decode_check_label(const std::string& pem,