我在Python中为OpenSLL libeay32.dll
编写小包装器。对于大多数功能,可以按如下方式导入它们:
self.Function_Name = self._dll.Function_Name
self.Function_Name.restype = ctypes.c_int #for example
self.Function_Name.argtypes = [list of ctypes arguments]
不幸的是,我无法以任何方式导入任何宏:
X509_get_notAfter
,X509_get_notBefore
等。
任何想法,如何使用ctypes
?
答案 0 :(得分:2)
您无法导入宏。您导入的是DLL中的函数。宏不会从DLL导出,因此无需导入任何内容。
幸运的是,大多数宏都很简单,所以你可以在Python中重新实现它们。
或者,或者,在C中创建一个包装器DLL,为每个宏定义一个函数,编译并链接它,然后用ctypes
导入包装器函数。
或者您可能希望使用Cython
或其他技术代替ctypes
。它们通常的工作方式是生成和编译包装C库并导出Python类型和函数的C代码,并且通常像导出C函数一样将C宏导出为Python函数。
或者,最简单的是...... PyOpenSSL已经包装了你需要的一切吗?
答案 1 :(得分:0)
经过一番研究后,我决定回答我自己的问题。 X509_get_notAfter的宏看起来像这样:
#define X509_get_notAfter(x) ((x)->cert_info->validity->notAfter)
x - 是X509结构,它包含X509_CINF结构,其中包含X509_VAL结构,其中包含指向notAfter的指针:)
所以我的计划是在python代码中实现整个X509,X509_CINF和X509_VAL结构。
在C中它看起来像这样
struct x509_st
{
X509_CINF *cert_info;
X509_ALGOR *sig_alg;
ASN1_BIT_STRING *signature;
int valid;
int references;
char *name;
CRYPTO_EX_DATA ex_data;
/* These contain copies of various extension values */
long ex_pathlen;
long ex_pcpathlen;
unsigned long ex_flags;
unsigned long ex_kusage;
unsigned long ex_xkusage;
unsigned long ex_nscert;
ASN1_OCTET_STRING *skid;
AUTHORITY_KEYID *akid;
X509_POLICY_CACHE *policy_cache;
STACK_OF(DIST_POINT) *crldp;
STACK_OF(GENERAL_NAME) *altname;
NAME_CONSTRAINTS *nc;
#ifndef OPENSSL_NO_RFC3779
STACK_OF(IPAddressFamily) *rfc3779_addr;
struct ASIdentifiers_st *rfc3779_asid;
#endif
#ifndef OPENSSL_NO_SHA
unsigned char sha1_hash[SHA_DIGEST_LENGTH];
#endif
X509_CERT_AUX *aux;
} /* X509 */;
X509_CINF看起来像这样:
typedef struct x509_cinf_st
{
ASN1_INTEGER *version; /* [ 0 ] default of v1 */
ASN1_INTEGER *serialNumber;
X509_ALGOR *signature;
X509_NAME *issuer;
X509_VAL *validity;
X509_NAME *subject;
X509_PUBKEY *key;
ASN1_BIT_STRING *issuerUID; /* [ 1 ] optional in v2 */
ASN1_BIT_STRING *subjectUID; /* [ 2 ] optional in v2 */
STACK_OF(X509_EXTENSION) *extensions; /* [ 3 ] optional in v3 */
ASN1_ENCODING enc;
} X509_CINF;
这是X509_VAL:
typedef struct X509_val_st
{
ASN1_TIME *notBefore;
ASN1_TIME *notAfter;
} X509_VAL;
为了使整个任务更容易,我决定用ctypes.c_void_p替换所有我不想访问的结构的指针
所以我的python代码现在看起来像这样:
class X509_val_st(ctypes.Structure):
_fields_ = [('notBefore', ctypes.c_void_p),
('notAfter', ctypes.c_void_p)]
class X509_cinf_st(ctypes.Structure):
_fields_ = [('version', ctypes.c_void_p),
('serialNumber', ctypes.c_void_p),
('signature', ctypes.c_void_p),
('issuer', ctypes.c_void_p),
('validity', X509_val_st),
('subject', ctypes.c_void_p),
('key', ctypes.c_void_p),
('issuerUID', ctypes.c_void_p),
('subjectUID', ctypes.c_void_p),
('extensions', ctypes.c_void_p),
('enc', ctypes.c_uint)]
class X509_st(ctypes.Structure):
_fields_ = [('cert_info', X509_cinf_st),
('sig_alg', ctypes.c_void_p),
('signature', ctypes.c_void_p),
('valid', ctypes.c_int),
('references', ctypes.c_int),
('name', ctypes.c_void_p),
('ex_data', ctypes.c_int),
('ex_pathlen', ctypes.c_long),
('ex_pcpathlen', ctypes.c_long),
('ex_flags', ctypes.c_ulong),
('ex_kusage', ctypes.c_ulong),
('ex_xkusage', ctypes.c_ulong),
('ex_nscert', ctypes.c_ulong),
('skid', ctypes.c_void_p),
('akid', ctypes.c_void_p),
('policy_cache', ctypes.c_void_p),
('crldp', ctypes.c_void_p),
('altname', ctypes.c_void_p),
('nc', ctypes.c_void_p),
('rfc3779_addr', ctypes.c_void_p),
('rfc3779_asid', ctypes.c_void_p),
('sha1_hash', ctypes.c_char),
('aux', ctypes.c_void_p)]
最后一步:将结构分配给从函数X509_new()接收的指针:
self.X509_new = self._lib.X509_new
self.X509_new.restype = ctypes.POINTER(X509_st)
self.X509_new.argtypes = []
因此,OpenSSL宏X509_get_notBefore的python函数将如下所示:
def X509_get_notBefore(self):
return self.X509[0].cert_info.validity.notBefore