tsig_python.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #define PY_SSIZE_T_CLEAN // need for "y#" below
  15. #include <Python.h>
  16. #include <string>
  17. #include <stdexcept>
  18. #include <exceptions/exceptions.h>
  19. #include <util/python/pycppwrapper_util.h>
  20. #include <dns/tsig.h>
  21. #include "pydnspp_common.h"
  22. #include "name_python.h"
  23. #include "tsigkey_python.h"
  24. #include "tsigerror_python.h"
  25. #include "tsigrecord_python.h"
  26. #include "tsig_python.h"
  27. using namespace std;
  28. using namespace isc;
  29. using namespace isc::util::python;
  30. using namespace isc::dns;
  31. using namespace isc::dns::python;
  32. // For each class, we need a struct, a helper functions (init, destroy,
  33. // and static wrappers around the methods we export), a list of methods,
  34. // and a type description
  35. namespace {
  36. // The s_* Class simply covers one instantiation of the object
  37. class s_TSIGContext : public PyObject {
  38. public:
  39. s_TSIGContext() : cppobj(NULL) {};
  40. TSIGContext* cppobj;
  41. };
  42. // Shortcut type which would be convenient for adding class variables safely.
  43. typedef CPPPyObjectContainer<s_TSIGContext, TSIGContext> TSIGContextContainer;
  44. //
  45. // We declare the functions here, the definitions are below
  46. // the type definition of the object, since both can use the other
  47. //
  48. // General creation and destruction
  49. int TSIGContext_init(s_TSIGContext* self, PyObject* args);
  50. void TSIGContext_destroy(s_TSIGContext* self);
  51. // Class specific methods
  52. PyObject* TSIGContext_getState(s_TSIGContext* self);
  53. PyObject* TSIGContext_getError(s_TSIGContext* self);
  54. PyObject* TSIGContext_sign(s_TSIGContext* self, PyObject* args);
  55. PyObject* TSIGContext_verify(s_TSIGContext* self, PyObject* args);
  56. // These are the functions we export
  57. // For a minimal support, we don't need them.
  58. // This list contains the actual set of functions we have in
  59. // python. Each entry has
  60. // 1. Python method name
  61. // 2. Our static function here
  62. // 3. Argument type
  63. // 4. Documentation
  64. PyMethodDef TSIGContext_methods[] = {
  65. { "get_state", reinterpret_cast<PyCFunction>(TSIGContext_getState),
  66. METH_NOARGS,
  67. "Return the current state of the context (mainly for tests)" },
  68. { "get_error", reinterpret_cast<PyCFunction>(TSIGContext_getError),
  69. METH_NOARGS,
  70. "Return the TSIG error as a result of the latest verification" },
  71. { "sign",
  72. reinterpret_cast<PyCFunction>(TSIGContext_sign), METH_VARARGS,
  73. "Sign a DNS message." },
  74. { "verify",
  75. reinterpret_cast<PyCFunction>(TSIGContext_verify), METH_VARARGS,
  76. "Verify a DNS message." },
  77. { NULL, NULL, 0, NULL }
  78. };
  79. int
  80. TSIGContext_init(s_TSIGContext* self, PyObject* args) {
  81. try {
  82. // "From key" constructor
  83. const PyObject* tsigkey_obj;
  84. if (PyArg_ParseTuple(args, "O!", &tsigkey_type, &tsigkey_obj)) {
  85. self->cppobj = new TSIGContext(PyTSIGKey_ToTSIGKey(tsigkey_obj));
  86. return (0);
  87. }
  88. // "From key param + keyring" constructor
  89. PyErr_Clear();
  90. const PyObject* keyname_obj;
  91. const PyObject* algname_obj;
  92. const PyObject* keyring_obj;
  93. if (PyArg_ParseTuple(args, "O!O!O!", &name_type, &keyname_obj,
  94. &name_type, &algname_obj, &tsigkeyring_type,
  95. &keyring_obj)) {
  96. self->cppobj = new TSIGContext(PyName_ToName(keyname_obj),
  97. PyName_ToName(algname_obj),
  98. PyTSIGKeyRing_ToTSIGKeyRing(keyring_obj));
  99. return (0);
  100. }
  101. } catch (const exception& ex) {
  102. const string ex_what = "Failed to construct TSIGContext object: " +
  103. string(ex.what());
  104. PyErr_SetString(po_IscException, ex_what.c_str());
  105. return (-1);
  106. } catch (...) {
  107. PyErr_SetString(po_IscException,
  108. "Unexpected exception in constructing TSIGContext");
  109. return (-1);
  110. }
  111. PyErr_SetString(PyExc_TypeError,
  112. "Invalid arguments to TSIGContext constructor");
  113. return (-1);
  114. }
  115. void
  116. TSIGContext_destroy(s_TSIGContext* const self) {
  117. delete self->cppobj;
  118. self->cppobj = NULL;
  119. Py_TYPE(self)->tp_free(self);
  120. }
  121. PyObject*
  122. TSIGContext_getState(s_TSIGContext* self) {
  123. return (Py_BuildValue("I", self->cppobj->getState()));
  124. }
  125. PyObject*
  126. TSIGContext_getError(s_TSIGContext* self) {
  127. try {
  128. PyObjectContainer container(createTSIGErrorObject(
  129. self->cppobj->getError()));
  130. return (Py_BuildValue("O", container.get()));
  131. } catch (const exception& ex) {
  132. const string ex_what =
  133. "Unexpectedly failed to get TSIGContext error: " +
  134. string(ex.what());
  135. PyErr_SetString(po_IscException, ex_what.c_str());
  136. } catch (...) {
  137. PyErr_SetString(po_IscException,
  138. "Unexpected exception in TSIGContext.get_error");
  139. }
  140. return (NULL);
  141. }
  142. PyObject*
  143. TSIGContext_sign(s_TSIGContext* self, PyObject* args) {
  144. long qid = 0;
  145. const char* mac;
  146. Py_ssize_t mac_size;
  147. if (PyArg_ParseTuple(args, "ly#", &qid, &mac, &mac_size)) {
  148. if (qid < 0 || qid > 0xffff) {
  149. PyErr_SetString(PyExc_ValueError,
  150. "TSIGContext.sign: QID out of range");
  151. return (NULL);
  152. }
  153. try {
  154. ConstTSIGRecordPtr record = self->cppobj->sign(qid, mac, mac_size);
  155. return (createTSIGRecordObject(*record));
  156. } catch (const TSIGContextError& ex) {
  157. PyErr_SetString(po_TSIGContextError, ex.what());
  158. } catch (const exception& ex) {
  159. const string ex_what = "Unexpected failure in TSIG sign: " +
  160. string(ex.what());
  161. PyErr_SetString(po_IscException, ex_what.c_str());
  162. } catch (...) {
  163. PyErr_SetString(PyExc_SystemError,
  164. "Unexpected failure in TSIG sign");
  165. }
  166. } else {
  167. PyErr_SetString(PyExc_TypeError,
  168. "Invalid arguments to TSIGContext.sign");
  169. }
  170. return (NULL);
  171. }
  172. PyObject*
  173. TSIGContext_verify(s_TSIGContext* self, PyObject* args) {
  174. const char* data;
  175. Py_ssize_t data_len;
  176. PyObject* py_record;
  177. PyObject* py_maybe_none;
  178. const TSIGRecord* record;
  179. if (PyArg_ParseTuple(args, "O!y#", &tsigrecord_type, &py_record,
  180. &data, &data_len)) {
  181. record = &PyTSIGRecord_ToTSIGRecord(py_record);
  182. } else if (PyArg_ParseTuple(args, "Oy#", &py_maybe_none, &data,
  183. &data_len)) {
  184. record = NULL;
  185. } else {
  186. PyErr_SetString(PyExc_TypeError,
  187. "Invalid arguments to TSIGContext.verify");
  188. return (NULL);
  189. }
  190. PyErr_Clear();
  191. try {
  192. const TSIGError error = self->cppobj->verify(record, data, data_len);
  193. return (createTSIGErrorObject(error));
  194. } catch (const TSIGContextError& ex) {
  195. PyErr_SetString(po_TSIGContextError, ex.what());
  196. } catch (const InvalidParameter& ex) {
  197. PyErr_SetString(po_InvalidParameter, ex.what());
  198. } catch (const exception& ex) {
  199. const string ex_what = "Unexpected failure in TSIG verify: " +
  200. string(ex.what());
  201. PyErr_SetString(po_IscException, ex_what.c_str());
  202. } catch (...) {
  203. PyErr_SetString(PyExc_SystemError, "Unexpected failure in TSIG verify");
  204. }
  205. return (NULL);
  206. }
  207. } // end of unnamed namespace
  208. namespace isc {
  209. namespace dns {
  210. namespace python {
  211. // Definition of class specific exception(s)
  212. PyObject* po_TSIGContextError;
  213. // This defines the complete type for reflection in python and
  214. // parsing of PyObject* to s_TSIGContext
  215. // Most of the functions are not actually implemented and NULL here.
  216. PyTypeObject tsigcontext_type = {
  217. PyVarObject_HEAD_INIT(NULL, 0)
  218. "pydnspp.TSIGContext",
  219. sizeof(s_TSIGContext), // tp_basicsize
  220. 0, // tp_itemsize
  221. reinterpret_cast<destructor>(TSIGContext_destroy), // tp_dealloc
  222. NULL, // tp_print
  223. NULL, // tp_getattr
  224. NULL, // tp_setattr
  225. NULL, // tp_reserved
  226. NULL, // tp_repr
  227. NULL, // tp_as_number
  228. NULL, // tp_as_sequence
  229. NULL, // tp_as_mapping
  230. NULL, // tp_hash
  231. NULL, // tp_call
  232. NULL, // tp_str
  233. NULL, // tp_getattro
  234. NULL, // tp_setattro
  235. NULL, // tp_as_buffer
  236. // We allow the python version of TSIGContext to act as a base class.
  237. // From pure design point of view, this is wrong because it's not intended
  238. // to be inherited. However, cryptographic operations are generally
  239. // difficult to test, so it would be very advantageous if we can define
  240. // a mock context class.
  241. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, // tp_flags
  242. "The TSIGContext class objects is...(COMPLETE THIS)",
  243. NULL, // tp_traverse
  244. NULL, // tp_clear
  245. NULL, // tp_richcompare
  246. 0, // tp_weaklistoffset
  247. NULL, // tp_iter
  248. NULL, // tp_iternext
  249. TSIGContext_methods, // tp_methods
  250. NULL, // tp_members
  251. NULL, // tp_getset
  252. NULL, // tp_base
  253. NULL, // tp_dict
  254. NULL, // tp_descr_get
  255. NULL, // tp_descr_set
  256. 0, // tp_dictoffset
  257. reinterpret_cast<initproc>(TSIGContext_init), // tp_init
  258. NULL, // tp_alloc
  259. PyType_GenericNew, // tp_new
  260. NULL, // tp_free
  261. NULL, // tp_is_gc
  262. NULL, // tp_bases
  263. NULL, // tp_mro
  264. NULL, // tp_cache
  265. NULL, // tp_subclasses
  266. NULL, // tp_weaklist
  267. NULL, // tp_del
  268. 0 // tp_version_tag
  269. };
  270. bool
  271. PyTSIGContext_Check(PyObject* obj) {
  272. if (obj == NULL) {
  273. isc_throw(PyCPPWrapperException, "obj argument NULL in typecheck");
  274. }
  275. return (PyObject_TypeCheck(obj, &tsigcontext_type));
  276. }
  277. TSIGContext&
  278. PyTSIGContext_ToTSIGContext(PyObject* tsigcontext_obj) {
  279. s_TSIGContext* tsigcontext = static_cast<s_TSIGContext*>(tsigcontext_obj);
  280. return (*tsigcontext->cppobj);
  281. }
  282. } // namespace python
  283. } // namespace dns
  284. } // namespace isc