aboutsummaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-09-10 05:22:51 +0000
committerlloyd <[email protected]>2006-09-10 05:22:51 +0000
commit7cc3dc61427c1f67e881afb3947c062604a86856 (patch)
treea4053c050e8433a6782e777b956cba508d0d5633 /misc
parent0e28c141d24fe14dfacbfd42072292f348f0673c (diff)
Check in some very preliminary code for wrapping public keys
Some fixes to the Filter code, though it is still not entirely functional.
Diffstat (limited to 'misc')
-rw-r--r--misc/python/src/filter.cpp31
-rw-r--r--misc/python/src/pk.cpp49
2 files changed, 68 insertions, 12 deletions
diff --git a/misc/python/src/filter.cpp b/misc/python/src/filter.cpp
index edd282048..552b43853 100644
--- a/misc/python/src/filter.cpp
+++ b/misc/python/src/filter.cpp
@@ -10,10 +10,11 @@ using namespace boost::python;
#include <botan/lookup.h>
using namespace Botan;
-class StringFilter : public Filter
+class Py_Filter : public Filter
{
public:
virtual void write_str(const std::string&) = 0;
+
void write(const byte data[], u32bit length)
{
write_str(std::string((const char*)data, length));
@@ -21,21 +22,24 @@ class StringFilter : public Filter
void send_str(const std::string& str)
{
+ printf("Py_Filter::send_str\n");
send((const byte*)str.data(), str.length());
}
};
-class FilterWrapper : public StringFilter, public wrapper<StringFilter>
+class FilterWrapper : public Py_Filter, public wrapper<Py_Filter>
{
public:
void start_msg()
{
+ printf("wrapper start_msg\n");
if(override start_msg = this->get_override("start_msg"))
start_msg();
}
void end_msg()
{
+ printf("wrapper end_msg\n");
if(override end_msg = this->get_override("end_msg"))
end_msg();
}
@@ -45,9 +49,9 @@ class FilterWrapper : public StringFilter, public wrapper<StringFilter>
virtual void write_str(const std::string& str)
{
+ printf("wrapper write\n");
this->get_override("write")(str);
}
-
};
Filter* return_or_raise(Filter* filter, const std::string& name)
@@ -105,16 +109,16 @@ void append_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
filter.release();
}
-void append_pyfilter(Pipe& pipe, std::auto_ptr<FilterWrapper> filter)
+void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
{
- pipe.append(filter.get());
+ pipe.prepend(filter.get());
filter.release();
}
-void prepend_filter(Pipe& pipe, std::auto_ptr<Filter> filter)
+void do_send(std::auto_ptr<FilterWrapper> filter, const std::string& data)
{
- pipe.prepend(filter.get());
- filter.release();
+ printf("Sending %s to %p\n", data.c_str(), filter.get());
+ filter->send_str(data);
}
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(rallas_ovls, read_all_as_string, 0, 1)
@@ -135,13 +139,17 @@ void export_filters()
// This might not work - Pipe will delete the filter, but Python
// might have allocated the space with malloc() or who-knows-what -> bad
- class_<FilterWrapper, std::auto_ptr<FilterWrapper>, boost::noncopyable>
+ class_<FilterWrapper, std::auto_ptr<FilterWrapper>,
+ bases<Filter>, boost::noncopyable>
("FilterObj")
- .def("write", pure_virtual(&StringFilter::write_str))
- .def("send_str", &StringFilter::send_str)
+ .def("write", pure_virtual(&Py_Filter::write_str))
+ .def("send", &do_send)
.def("start_msg", &Filter::start_msg, &FilterWrapper::default_start_msg)
.def("end_msg", &Filter::end_msg, &FilterWrapper::default_end_msg);
+ implicitly_convertible<std::auto_ptr<FilterWrapper>,
+ std::auto_ptr<Filter> >();
+
void (Pipe::*pipe_write_str)(const std::string&) = &Pipe::write;
void (Pipe::*pipe_process_str)(const std::string&) = &Pipe::process_msg;
@@ -152,7 +160,6 @@ void export_filters()
.add_property("default_msg", &Pipe::default_msg, &Pipe::set_default_msg)
.add_property("msg_count", &Pipe::message_count)
.def("append", append_filter)
- .def("append", append_pyfilter)
.def("prepend", prepend_filter)
.def("reset", &Pipe::reset)
.def("pop", &Pipe::pop)
diff --git a/misc/python/src/pk.cpp b/misc/python/src/pk.cpp
new file mode 100644
index 000000000..1d327c6f5
--- /dev/null
+++ b/misc/python/src/pk.cpp
@@ -0,0 +1,49 @@
+/*************************************************
+* Boost.Python module definition *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/rsa.h>
+#include <botan/dsa.h>
+#include <botan/dh.h>
+#include <botan/look_pk.h>
+using namespace Botan;
+
+#include <boost/python.hpp>
+namespace python = boost::python;
+
+std::string DER_encode_str(const X509_PublicKey* key)
+ {
+ Pipe pipe;
+ X509::encode(*key, pipe, RAW_BER);
+ return pipe.read_all_as_string();
+ }
+
+std::string get_oid_str(const X509_PublicKey* key)
+ {
+ try
+ {
+ return key->get_oid().as_string();
+ }
+ catch(Lookup_Error)
+ {
+ return "";
+ }
+ }
+
+X509_PublicKey* load_key_str(const std::string& file)
+ {
+ return X509::load_key(file);
+ }
+
+void export_pk()
+ {
+ python::class_<X509_PublicKey, boost::noncopyable>
+ ("X509_PublicKey", python::no_init)
+ .def("__init__", python::make_constructor(load_key_str))
+ .add_property("algo", &X509_PublicKey::algo_name)
+ .add_property("max_input_bits", &X509_PublicKey::max_input_bits)
+ .add_property("oid", &get_oid_str)
+ .def("__str__", &X509::PEM_encode)
+ .def("der_encode", &DER_encode_str);
+ }