some minimal pkcs7 and pkcs12 tests, and minor adjustments to their part of OpenSSL.crypto module initialization; change NetscapeSPKI to be a type that can be instantiated, and add a very minimal amount of testing for it
diff --git a/src/crypto/crypto.c b/src/crypto/crypto.c
index 49e0b16..89091cc 100644
--- a/src/crypto/crypto.c
+++ b/src/crypto/crypto.c
@@ -515,37 +515,6 @@
}
-static char crypto_NetscapeSPKI_doc[] = "\n\
-The factory function inserted in the module dictionary to create NetscapeSPKI\n\
-objects\n\
-\n\
-@param enc: Base64 encoded NetscapeSPKI object.\n\
-@type enc: C{str}\n\
-@return: The NetscapeSPKI object\n\
-";
-
-static PyObject *
-crypto_NetscapeSPKI(PyObject *spam, PyObject *args)
-{
- char *enc = NULL;
- int enc_len = -1;
- NETSCAPE_SPKI *spki;
-
- if (!PyArg_ParseTuple(args, "|s#:NetscapeSPKI", &enc, &enc_len))
- return NULL;
-
- if (enc_len >= 0)
- spki = NETSCAPE_SPKI_b64_decode(enc, enc_len);
- else
- spki = NETSCAPE_SPKI_new();
- if (spki == NULL)
- {
- exception_from_error_queue();
- return NULL;
- }
- return (PyObject *)crypto_NetscapeSPKI_New(spki, 1);
-}
-
static char crypto_X509_verify_cert_error_string_doc[] = "\n\
Get X509 verify certificate error string.\n\
\n\
@@ -577,8 +546,6 @@
{ "dump_certificate_request", (PyCFunction)crypto_dump_certificate_request, METH_VARARGS, crypto_dump_certificate_request_doc },
{ "load_pkcs7_data", (PyCFunction)crypto_load_pkcs7_data, METH_VARARGS, crypto_load_pkcs7_data_doc },
{ "load_pkcs12", (PyCFunction)crypto_load_pkcs12, METH_VARARGS, crypto_load_pkcs12_doc },
- /* Factory functions */
- { "NetscapeSPKI", (PyCFunction)crypto_NetscapeSPKI, METH_VARARGS, crypto_NetscapeSPKI_doc },
{ "X509_verify_cert_error_string", (PyCFunction)crypto_X509_verify_cert_error_string, METH_VARARGS, crypto_X509_verify_cert_error_string_doc },
{ NULL, NULL }
};
@@ -712,11 +679,11 @@
goto error;
if (!init_crypto_x509extension(module))
goto error;
- if (!init_crypto_pkcs7(dict))
+ if (!init_crypto_pkcs7(module))
goto error;
- if (!init_crypto_pkcs12(dict))
+ if (!init_crypto_pkcs12(module))
goto error;
- if (!init_crypto_netscape_spki(dict))
+ if (!init_crypto_netscape_spki(module))
goto error;
error:
diff --git a/src/crypto/netscape_spki.c b/src/crypto/netscape_spki.c
index a0796cb..d4255f0 100644
--- a/src/crypto/netscape_spki.c
+++ b/src/crypto/netscape_spki.c
@@ -33,6 +33,37 @@
return self;
}
+
+static char crypto_NetscapeSPKI_doc[] = "\n\
+NetscapeSPKI([enc]) -> NetscapeSPKI instance\n\
+\n\
+@param enc: Base64 encoded NetscapeSPKI object.\n\
+@type enc: C{str}\n\
+@return: The NetscapeSPKI object\n\
+";
+
+static PyObject *
+crypto_NetscapeSPKI_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
+ char *enc = NULL;
+ int enc_len = -1;
+ NETSCAPE_SPKI *spki;
+
+ if (!PyArg_ParseTuple(args, "|s#:NetscapeSPKI", &enc, &enc_len))
+ return NULL;
+
+ if (enc_len >= 0)
+ spki = NETSCAPE_SPKI_b64_decode(enc, enc_len);
+ else
+ spki = NETSCAPE_SPKI_new();
+ if (spki == NULL)
+ {
+ exception_from_error_queue();
+ return NULL;
+ }
+ return (PyObject *)crypto_NetscapeSPKI_New(spki, 1);
+}
+
+
/*
* Deallocate the memory used by the NetscapeSPKI object
*
@@ -224,21 +255,53 @@
NULL, /* as_number */
NULL, /* as_sequence */
NULL, /* as_mapping */
- NULL /* hash */
+ NULL, /* hash */
+ NULL, /* call */
+ NULL, /* str */
+ NULL, /* getattro */
+ NULL, /* setattro */
+ NULL, /* as_buffer */
+ Py_TPFLAGS_DEFAULT,
+ crypto_NetscapeSPKI_doc, /* doc */
+ NULL, /* traverse */
+ NULL, /* clear */
+ NULL, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ NULL, /* tp_iter */
+ NULL, /* tp_iternext */
+ crypto_NetscapeSPKI_methods, /* tp_methods */
+ NULL, /* tp_members */
+ NULL, /* tp_getset */
+ NULL, /* tp_base */
+ NULL, /* tp_dict */
+ NULL, /* tp_descr_get */
+ NULL, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ NULL, /* tp_init */
+ NULL, /* tp_alloc */
+ crypto_NetscapeSPKI_new, /* tp_new */
};
/*
* Initialize the X509Name part of the crypto module
*
- * Arguments: dict - The crypto module dictionary
+ * Arguments: module - The crypto module
* Returns: None
*/
int
-init_crypto_netscape_spki(PyObject *dict)
-{
- crypto_NetscapeSPKI_Type.ob_type = &PyType_Type;
- Py_INCREF(&crypto_NetscapeSPKI_Type);
- PyDict_SetItemString(dict, "NetscapeSPKIType", (PyObject *)&crypto_NetscapeSPKI_Type);
+init_crypto_netscape_spki(PyObject *module) {
+ if (PyType_Ready(&crypto_NetscapeSPKI_Type) < 0) {
+ return 0;
+ }
+
+ if (PyModule_AddObject(module, "NetscapeSPKI", (PyObject *)&crypto_NetscapeSPKI_Type) != 0) {
+ return 0;
+ }
+
+ if (PyModule_AddObject(module, "NetscapeSPKIType", (PyObject *)&crypto_NetscapeSPKI_Type) != 0) {
+ return 0;
+ }
+
return 1;
}
diff --git a/src/crypto/pkcs12.c b/src/crypto/pkcs12.c
index 3fa39aa..71ebcdd 100644
--- a/src/crypto/pkcs12.c
+++ b/src/crypto/pkcs12.c
@@ -253,15 +253,19 @@
/*
* Initialize the PKCS12 part of the crypto sub module
*
- * Arguments: dict - The crypto module dictionary
+ * Arguments: module - The crypto module
* Returns: None
*/
int
-init_crypto_pkcs12(PyObject *dict)
-{
- crypto_PKCS12_Type.ob_type = &PyType_Type;
- Py_INCREF(&crypto_PKCS12_Type);
- PyDict_SetItemString(dict, "PKCS12Type", (PyObject *)&crypto_PKCS12_Type);
+init_crypto_pkcs12(PyObject *module) {
+ if (PyType_Ready(&crypto_PKCS12_Type) < 0) {
+ return 0;
+ }
+
+ if (PyModule_AddObject(module, "PKCS12Type", (PyObject *)&crypto_PKCS12_Type) != 0) {
+ return 0;
+ }
+
return 1;
}
diff --git a/src/crypto/pkcs7.c b/src/crypto/pkcs7.c
index 4ff0b6b..1cb0813 100644
--- a/src/crypto/pkcs7.c
+++ b/src/crypto/pkcs7.c
@@ -197,15 +197,19 @@
/*
* Initialize the PKCS7 part of the crypto sub module
*
- * Arguments: dict - The crypto module dictionary
+ * Arguments: module - The crypto module
* Returns: None
*/
int
-init_crypto_pkcs7(PyObject *dict)
-{
- crypto_PKCS7_Type.ob_type = &PyType_Type;
- Py_INCREF(&crypto_PKCS7_Type);
- PyDict_SetItemString(dict, "PKCS7Type", (PyObject *)&crypto_PKCS7_Type);
+init_crypto_pkcs7(PyObject *module) {
+ if (PyType_Ready(&crypto_PKCS7_Type) < 0) {
+ return 0;
+ }
+
+ if (PyModule_AddObject(module, "PKCS7Type", (PyObject *)&crypto_PKCS7_Type) != 0) {
+ return 0;
+ }
+
return 1;
}