From 72a7754bfee5206288d9211b740dae7b2ca9618b Mon Sep 17 00:00:00 2001 From: Victor Wagner Date: Mon, 15 Dec 2014 15:10:59 +0300 Subject: [PATCH] I've discovered Python's __all__ variable and make use of it in all modles --- ctypescrypto/__init__.py | 3 +++ ctypescrypto/bio.py | 2 ++ ctypescrypto/cipher.py | 2 ++ ctypescrypto/cms.py | 2 +- ctypescrypto/digest.py | 1 + ctypescrypto/ec.py | 2 ++ ctypescrypto/engine.py | 3 +++ ctypescrypto/exception.py | 3 +++ ctypescrypto/oid.py | 3 +++ ctypescrypto/pbkdf2.py | 2 ++ ctypescrypto/pkey.py | 2 ++ ctypescrypto/rand.py | 2 ++ ctypescrypto/x509.py | 32 ++++++++++++++++++++++++++------ 13 files changed, 52 insertions(+), 7 deletions(-) diff --git a/ctypescrypto/__init__.py b/ctypescrypto/__init__.py index 27ad0c5..ebf8c41 100644 --- a/ctypescrypto/__init__.py +++ b/ctypescrypto/__init__.py @@ -3,6 +3,7 @@ """ + from ctypes import CDLL,c_char_p def config(filename=None): @@ -12,6 +13,8 @@ def config(filename=None): """ libcrypto.OPENSSL_config(filename) +__all__ = ['bio','cipher','cms','config','digest','ec','engine','exception','oid','pbkdf2','pkey','rand','x509'] + libcrypto = CDLL("libcrypto.so.1.0.0") libcrypto.OPENSSL_config.argtypes=(c_char_p,) libcrypto.OPENSSL_add_all_algorithms_conf() diff --git a/ctypescrypto/bio.py b/ctypescrypto/bio.py index a333105..ae89aa3 100644 --- a/ctypescrypto/bio.py +++ b/ctypescrypto/bio.py @@ -83,6 +83,8 @@ class Membio: Resets the read-only bio to start and discards all data from writable bio """ libcrypto.BIO_ctrl(self.bio,1,0,None) + +__all__ = ['Membio'] libcrypto.BIO_s_mem.restype=c_void_p libcrypto.BIO_new.restype=c_void_p libcrypto.BIO_new.argtypes=(c_void_p,) diff --git a/ctypescrypto/cipher.py b/ctypescrypto/cipher.py index c2053e9..90fd203 100644 --- a/ctypescrypto/cipher.py +++ b/ctypescrypto/cipher.py @@ -12,6 +12,8 @@ CIPHER_MODES = ("STREAM","ECB","CBC", "CFB", "OFB", "CTR","GCM") # +__all__ = ['CipherError','new','Cipher','CipherType'] + class CipherError(LibCryptoError): pass diff --git a/ctypescrypto/cms.py b/ctypescrypto/cms.py index 2c03898..9213d27 100644 --- a/ctypescrypto/cms.py +++ b/ctypescrypto/cms.py @@ -279,7 +279,7 @@ class EncryptedData(CMSBase): raise CMSError("decrypt data") return str(b) - +__all__=['CMS','CMSError','Flags','SignedData','EnvelopedData','EncryptedData'] libcrypto.CMS_verify.restype=c_int libcrypto.CMS_verify.argtypes=(c_void_p,c_void_p,c_void_p,c_void_p,c_void_p,c_int) diff --git a/ctypescrypto/digest.py b/ctypescrypto/digest.py index 0098ab4..f570103 100644 --- a/ctypescrypto/digest.py +++ b/ctypescrypto/digest.py @@ -19,6 +19,7 @@ from ctypescrypto.exception import LibCryptoError from ctypescrypto.oid import Oid DIGEST_ALGORITHMS = ("MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512") +__all__ = ['DigestError','Digest','DigestType','new'] class DigestError(LibCryptoError): pass diff --git a/ctypescrypto/ec.py b/ctypescrypto/ec.py index f3ea778..047aad9 100644 --- a/ctypescrypto/ec.py +++ b/ctypescrypto/ec.py @@ -5,6 +5,8 @@ from ctypescrypto.pkey import PKey, PKeyError from ctypes import c_void_p,c_char_p,c_int,byref from ctypescrypto import libcrypto +__all__ = [ 'create'] + def create(curve,data): """ Creates EC keypair from the just secret key and curve name diff --git a/ctypescrypto/engine.py b/ctypescrypto/engine.py index dd7c028..898b20d 100644 --- a/ctypescrypto/engine.py +++ b/ctypescrypto/engine.py @@ -4,6 +4,9 @@ engine loading and configuration from ctypes import * from ctypescrypto import libcrypto from ctypescrypto.exception import LibCryptoError + +__all__=['default','set_default'] + default=None def set_default(engine): diff --git a/ctypescrypto/exception.py b/ctypescrypto/exception.py index c4710ec..43ba6a0 100644 --- a/ctypescrypto/exception.py +++ b/ctypescrypto/exception.py @@ -4,6 +4,9 @@ Exception which extracts libcrypto error information from ctypes import * from ctypescrypto import libcrypto strings_loaded=False + +__all__ = ['LibCryptoError','clear_err_stack'] + class LibCryptoError(Exception): """ Exception for libcrypto errors. Adds all the info, which can be diff --git a/ctypescrypto/oid.py b/ctypescrypto/oid.py index 976cd3f..7d4fc21 100644 --- a/ctypescrypto/oid.py +++ b/ctypescrypto/oid.py @@ -9,6 +9,9 @@ """ from ctypescrypto import libcrypto from ctypes import c_char_p, c_void_p, c_int, create_string_buffer + +__all__ = ['Oid','create','cleanup'] + class Oid: """ Represents an OID. It can be consturucted by textual diff --git a/ctypescrypto/pbkdf2.py b/ctypescrypto/pbkdf2.py index 0c2b077..c2e01bb 100644 --- a/ctypescrypto/pbkdf2.py +++ b/ctypescrypto/pbkdf2.py @@ -7,6 +7,8 @@ from ctypes import c_char_p,c_int, c_void_p, create_string_buffer from ctypescrypto import libcrypto from ctypescrypto.digest import DigestType +__all__ = ['pbkdf2'] + def pbkdf2(password,salt,outlen,digesttype="sha1",iterations=2000): """ Interface to PKCS5_PBKDF2_HMAC function diff --git a/ctypescrypto/pkey.py b/ctypescrypto/pkey.py index 10366ee..59a5348 100644 --- a/ctypescrypto/pkey.py +++ b/ctypescrypto/pkey.py @@ -10,6 +10,8 @@ from ctypescrypto import libcrypto from ctypescrypto.exception import LibCryptoError,clear_err_stack from ctypescrypto.bio import Membio import sys + +__all__ = ['PKeyError','password_callback','PKey'] class PKeyError(LibCryptoError): pass diff --git a/ctypescrypto/rand.py b/ctypescrypto/rand.py index 5d51eed..e8d7d95 100644 --- a/ctypescrypto/rand.py +++ b/ctypescrypto/rand.py @@ -6,6 +6,8 @@ from ctypes import create_string_buffer, c_char_p, c_int, c_double from ctypescrypto import libcrypto from ctypescrypto.exception import LibCryptoError +__all__ = ['RandError','bytes','pseudo_bytes','seed','status'] + class RandError(LibCryptoError): pass diff --git a/ctypescrypto/x509.py b/ctypescrypto/x509.py index 5c2a50d..e2c97c6 100644 --- a/ctypescrypto/x509.py +++ b/ctypescrypto/x509.py @@ -1,9 +1,22 @@ +""" +Implements interface to openssl X509 and X509Store structures, +I.e allows to load, analyze and verify certificates. + +X509Store objects are also used to verify other signed documets, +such as CMS, OCSP and timestamps. +""" + + + from ctypes import c_void_p,create_string_buffer,c_long,c_int,POINTER,c_char_p from ctypescrypto.bio import Membio from ctypescrypto.pkey import PKey from ctypescrypto.oid import Oid from ctypescrypto.exception import LibCryptoError from ctypescrypto import libcrypto + +__all__ = ['X509Error','X509Name','X509Store','StackOfX509'] +# X509_extlist is not exported yet, because is not implemented class X509Error(LibCryptoError): """ Exception, generated when some openssl function fail @@ -16,6 +29,10 @@ class X509Name: """ Class which represents X.509 distinguished name - typically a certificate subject name or an issuer name. + + Now used only to represent information, extracted from the + certificate. Potentially can be also used to build DN when creating + certificate signing request """ # XN_FLAG_SEP_COMMA_PLUS & ASN1_STRFLG_UTF8_CONVERT PRINT_FLAG=0x10010 @@ -170,10 +187,10 @@ class X509: @param chain - list of X509 objects to add into verification context.These objects are untrusted, but can be used to build certificate chain up to trusted object in the store - @param key - PKey object - parameters stora and key are mutually exclusive. If neither is specified, attempts to verify + @param key - PKey object with open key to validate signature - itself as self-signed certificate + parameters store and key are mutually exclusive. If neither + is specified, attempts to verify self as self-signed certificate """ if store is not None and key is not None: raise X509Error("key and store cannot be specified simultaneously") @@ -234,11 +251,14 @@ class X509: return libcrypto.X509_check_ca(self.cert)>0 class X509Store: """ - Represents trusted certificate store. Can be used to lookup CA certificates to verify + Represents trusted certificate store. Can be used to lookup CA + certificates to verify - @param file - file with several certificates and crls to load into store + @param file - file with several certificates and crls + to load into store @param dir - hashed directory with certificates and crls - @param default - if true, default verify location (directory) is installed + @param default - if true, default verify location (directory) + is installed """ def __init__(self,file=None,dir=None,default=False): -- 2.39.5