Ad

C Python.h API ImportError With OpenSSL

- 1 answer

I am trying out the Python API (python3) for C by making a hashing example using OpenSSL and MD5 but I am getting an error when attempting to import the module in python3. This is my C source code:

  • hashmodule.c
#include <stdlib.h>
#include <string.h>
#include <Python.h>
#include <openssl/md5.h>


unsigned char * __c_md5hash(const /*unsigned*/ char * string) {
    unsigned char * raw_hash = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
    memset(raw_hash, '\0', MD5_DIGEST_LENGTH);

    MD5_CTX ctx;
    MD5_Init(&ctx);
    MD5_Update(&ctx, string, strlen(string));
    MD5_Final(raw_hash, &ctx);

    return raw_hash;
}

static PyObject * md5hash(PyObject * self, PyObject * args) {;
    const /*unsigned*/ char * string;

    if (!PyArg_ParseTuple(args, "s", &string))
        return NULL;

    return Py_BuildValue("s", __c_md5hash(string));
}

static PyMethodDef hashmethods[] = {
    {"md5hash", md5hash, METH_VARARGS, NULL},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef hashmodule = {
    PyModuleDef_HEAD_INIT,
    "hashmodule",
    "a simple test hashing module for python3",
    -1,
    hashmethods
};

PyMODINIT_FUNC PyInit_testmodule(void) {
    return PyModule_Create(&hashmodule);
}

This is my python3 setup script

  • setup.py
from distutils.core import setup, Extension
import sysconfig

setup(
    name="hashmodule",
    version="1.0",
    ext_modules=[
        Extension(
            "hashmodule",
            sources=["hashmodule.c"],
            extra_compile_args=["-lcrypto", "-lssl"]
        )
    ]
)

As shown in the setup.py file, i have added the -lssl and -lcrypto flags for openssl and the build & install compilation is fine when i run python3 setup.py build and python3 setup.py install. Here is the output:

  • python3 setup.py build
running build                                                                                                       
running build_ext                                                                                                   
building 'hashmodule' extension                                                                                     
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-s
trong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -
Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.8 -c hashmodule.c -o build/temp.linux-x86_64-3.8/hashmod
ule.o -lcrypto -lssl                                      
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-z,relro -g -
fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.l
inux-x86_64-3.8/hashmodule.o -o build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so
  • python3 setup.py install
running install                                           
running build                                                                                                       
running build_ext                                                                                                   
running install_lib                                                                                                 
copying build/lib.linux-x86_64-3.8/hashmodule.cpython-38-x86_64-linux-gnu.so -> /usr/local/lib/python3.8/dist-packag
es                                                                                                                  
running install_egg_info                                  
Writing /usr/local/lib/python3.8/dist-packages/hashmodule-1.0.egg-info

As shown in the build, it include the correct crypto and ssl flags required for normal compilations with openssl. However when i then go into python3 and execute the following, this error appears:

  • python3
>>> import hashmodule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so: undefined symbol: MD5_Final
>>>

I have ran nm to find if the MD5 functions were included in the module which shows the following:

  • nm -S /usr/local/lib/python3.8/dist-packages/hashmodule.cpython-38-x86_64-linux-gnu.so | grep MD5
                 U MD5_Final
                 U MD5_Init
                 U MD5_Update

Any help would be really appreciated and if there is anything i have missed, please let me know.

Many thanks!

Ad

Answer

After a few days i managed to figure out a work around/alternative way to build the module without the use of setup.py and still use it in python3

gcc -fPIC -shared hashmodule.c -o hashmodule.so -I /usr/include/python3.8 -lcrypto

and the module works fine.

I'm leaving this answer here for other people in-case they have the same issue.

Edit:

Also just found the setup.py Extension arg called "extra_link_args"

add it to your distutils.core.Extension() parameters and compile as usual with python3 setup.py build & python3 setup.py install

Ad
source: stackoverflow.com
Ad