aboutsummaryrefslogtreecommitdiffstats
path: root/misc/python/src/pk.cpp
blob: 1d327c6f5b60f5e7a45f068b73bb7df57eb34b60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
   }