From ef45a18ebcb2d1d1f53410defff8b0713aad6598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Cepl?= Date: Thu, 19 Nov 2015 14:42:15 +0100 Subject: Strip trailinig spaces and expand tabs. Just run $ find . -name \*.py -exec sed -r -i -e "s/\t/ /g" '{}' \; $ find . -name \*.py -exec sed -r -i -e "s/[ ]+$//" '{}' \; --- contrib/SimpleX509create.py | 198 ++++++++++++++++++++++---------------------- contrib/isaac.httpslib.py | 38 ++++----- contrib/smimeplus.py | 22 ++--- 3 files changed, 129 insertions(+), 129 deletions(-) (limited to 'contrib') diff --git a/contrib/SimpleX509create.py b/contrib/SimpleX509create.py index 3a7c83c..6c6d007 100644 --- a/contrib/SimpleX509create.py +++ b/contrib/SimpleX509create.py @@ -15,149 +15,149 @@ MBSTRING_BMP = MBSTRING_FLAG | 2 class Cert: - def __init__ ( self ): - self.RsaKey = { 'KeyLength' : 1024, - 'PubExponent' : 0x10001, # -> 65537 - 'keygen_callback' : self.callback - } + def __init__ ( self ): + self.RsaKey = { 'KeyLength' : 1024, + 'PubExponent' : 0x10001, # -> 65537 + 'keygen_callback' : self.callback + } - self.KeyPair = None - self.PKey = None + self.KeyPair = None + self.PKey = None - self.X509Request = None - self.X509Certificate = None + self.X509Request = None + self.X509Certificate = None - def callback ( self, *args ): - return 'p' + def callback ( self, *args ): + return 'p' - def CreatePKey ( self ): - self.KeyPair = M2Crypto.RSA.gen_key( self.RsaKey['KeyLength'], self.RsaKey['PubExponent'], self.RsaKey['keygen_callback'] ) - #PubKey = M2Crypto.RSA.new_pub_key( self.KeyPair.pub () ) + def CreatePKey ( self ): + self.KeyPair = M2Crypto.RSA.gen_key( self.RsaKey['KeyLength'], self.RsaKey['PubExponent'], self.RsaKey['keygen_callback'] ) + #PubKey = M2Crypto.RSA.new_pub_key( self.KeyPair.pub () ) - self.KeyPair.save_key( 'KeyPair.pem', cipher='des_ede3_cbc', callback=self.callback ) - - self.PKey = M2Crypto.EVP.PKey ( md='sha1') - self.PKey.assign_rsa ( self.KeyPair ) + self.KeyPair.save_key( 'KeyPair.pem', cipher='des_ede3_cbc', callback=self.callback ) + self.PKey = M2Crypto.EVP.PKey ( md='sha1') + self.PKey.assign_rsa ( self.KeyPair ) - def CreateX509Request ( self ): - # - # X509 REQUEST - # - self.X509Request = M2Crypto.X509.Request () + def CreateX509Request ( self ): + # + # X509 REQUEST + # - # - # subject - # + self.X509Request = M2Crypto.X509.Request () - X509Name = M2Crypto.X509.X509_Name () + # + # subject + # - X509Name.add_entry_by_txt ( field='C', type=MBSTRING_ASC, entry='austria', len=-1, loc=-1, set=0 ) # country name - X509Name.add_entry_by_txt ( field='SP', type=MBSTRING_ASC, entry='kernten', len=-1, loc=-1, set=0 ) # state of province name - X509Name.add_entry_by_txt ( field='L', type=MBSTRING_ASC, entry='stgallen', len=-1, loc=-1, set=0 ) # locality name - X509Name.add_entry_by_txt ( field='O', type=MBSTRING_ASC, entry='labor', len=-1, loc=-1, set=0 ) # organization name - X509Name.add_entry_by_txt ( field='OU', type=MBSTRING_ASC, entry='it-department', len=-1, loc=-1, set=0 ) # organizational unit name - X509Name.add_entry_by_txt ( field='CN', type=MBSTRING_ASC, entry='Certificate client', len=-1, loc=-1, set=0 ) # common name - X509Name.add_entry_by_txt ( field='Email', type=MBSTRING_ASC, entry='user@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address - X509Name.add_entry_by_txt ( field='emailAddress', type=MBSTRING_ASC, entry='user@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address + X509Name = M2Crypto.X509.X509_Name () - self.X509Request.set_subject_name( X509Name ) + X509Name.add_entry_by_txt ( field='C', type=MBSTRING_ASC, entry='austria', len=-1, loc=-1, set=0 ) # country name + X509Name.add_entry_by_txt ( field='SP', type=MBSTRING_ASC, entry='kernten', len=-1, loc=-1, set=0 ) # state of province name + X509Name.add_entry_by_txt ( field='L', type=MBSTRING_ASC, entry='stgallen', len=-1, loc=-1, set=0 ) # locality name + X509Name.add_entry_by_txt ( field='O', type=MBSTRING_ASC, entry='labor', len=-1, loc=-1, set=0 ) # organization name + X509Name.add_entry_by_txt ( field='OU', type=MBSTRING_ASC, entry='it-department', len=-1, loc=-1, set=0 ) # organizational unit name + X509Name.add_entry_by_txt ( field='CN', type=MBSTRING_ASC, entry='Certificate client', len=-1, loc=-1, set=0 ) # common name + X509Name.add_entry_by_txt ( field='Email', type=MBSTRING_ASC, entry='user@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address + X509Name.add_entry_by_txt ( field='emailAddress', type=MBSTRING_ASC, entry='user@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address - # - # publickey - # + self.X509Request.set_subject_name( X509Name ) - self.X509Request.set_pubkey ( pkey=self.PKey ) - self.X509Request.sign ( pkey=self.PKey, md='sha1' ) - #print(X509Request.as_text ()) + # + # publickey + # + self.X509Request.set_pubkey ( pkey=self.PKey ) + self.X509Request.sign ( pkey=self.PKey, md='sha1' ) + #print(X509Request.as_text ()) - def CreateX509Certificate ( self ): - # - # X509 CERTIFICATE - # - self.X509Certificate = M2Crypto.X509.X509 () + def CreateX509Certificate ( self ): + # + # X509 CERTIFICATE + # - # - # version - # + self.X509Certificate = M2Crypto.X509.X509 () - self.X509Certificate.set_version ( 0 ) + # + # version + # - # - # time notBefore - # + self.X509Certificate.set_version ( 0 ) - ASN1 = M2Crypto.ASN1.ASN1_UTCTIME () - ASN1.set_time ( 500 ) - self.X509Certificate.set_not_before( ASN1 ) + # + # time notBefore + # - # - # time notAfter - # + ASN1 = M2Crypto.ASN1.ASN1_UTCTIME () + ASN1.set_time ( 500 ) + self.X509Certificate.set_not_before( ASN1 ) - ASN1 = M2Crypto.ASN1.ASN1_UTCTIME () - ASN1.set_time ( 500 ) - self.X509Certificate.set_not_after( ASN1 ) + # + # time notAfter + # - # - # public key - # + ASN1 = M2Crypto.ASN1.ASN1_UTCTIME () + ASN1.set_time ( 500 ) + self.X509Certificate.set_not_after( ASN1 ) - self.X509Certificate.set_pubkey ( pkey=self.PKey ) - - # - # subject - # + # + # public key + # - X509Name = self.X509Request.get_subject () + self.X509Certificate.set_pubkey ( pkey=self.PKey ) - #print(X509Name.entry_count ()) - #print(X509Name.as_text ()) + # + # subject + # - self.X509Certificate.set_subject_name( X509Name ) + X509Name = self.X509Request.get_subject () - # - # issuer - # + #print(X509Name.entry_count ()) + #print(X509Name.as_text ()) - X509Name = M2Crypto.X509.X509_Name ( M2Crypto.m2.x509_name_new () ) + self.X509Certificate.set_subject_name( X509Name ) - X509Name.add_entry_by_txt ( field='C', type=MBSTRING_ASC, entry='germany', len=-1, loc=-1, set=0 ) # country name - X509Name.add_entry_by_txt ( field='SP', type=MBSTRING_ASC, entry='bavaria', len=-1, loc=-1, set=0 ) # state of province name - X509Name.add_entry_by_txt ( field='L', type=MBSTRING_ASC, entry='munich', len=-1, loc=-1, set=0 ) # locality name - X509Name.add_entry_by_txt ( field='O', type=MBSTRING_ASC, entry='sbs', len=-1, loc=-1, set=0 ) # organization name - X509Name.add_entry_by_txt ( field='OU', type=MBSTRING_ASC, entry='it-department', len=-1, loc=-1, set=0 ) # organizational unit name - X509Name.add_entry_by_txt ( field='CN', type=MBSTRING_ASC, entry='Certificate Authority', len=-1, loc=-1, set=0 ) # common name - X509Name.add_entry_by_txt ( field='Email', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address - X509Name.add_entry_by_txt ( field='emailAddress', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address + # + # issuer + # - #print(X509Name.entry_count ()) - #print(X509Name.as_text ()) + X509Name = M2Crypto.X509.X509_Name ( M2Crypto.m2.x509_name_new () ) - self.X509Certificate.set_issuer_name( X509Name ) + X509Name.add_entry_by_txt ( field='C', type=MBSTRING_ASC, entry='germany', len=-1, loc=-1, set=0 ) # country name + X509Name.add_entry_by_txt ( field='SP', type=MBSTRING_ASC, entry='bavaria', len=-1, loc=-1, set=0 ) # state of province name + X509Name.add_entry_by_txt ( field='L', type=MBSTRING_ASC, entry='munich', len=-1, loc=-1, set=0 ) # locality name + X509Name.add_entry_by_txt ( field='O', type=MBSTRING_ASC, entry='sbs', len=-1, loc=-1, set=0 ) # organization name + X509Name.add_entry_by_txt ( field='OU', type=MBSTRING_ASC, entry='it-department', len=-1, loc=-1, set=0 ) # organizational unit name + X509Name.add_entry_by_txt ( field='CN', type=MBSTRING_ASC, entry='Certificate Authority', len=-1, loc=-1, set=0 ) # common name + X509Name.add_entry_by_txt ( field='Email', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address + X509Name.add_entry_by_txt ( field='emailAddress', type=MBSTRING_ASC, entry='admin@localhost', len=-1, loc=-1, set=0 ) # pkcs9 email address - # - # signing - # + #print(X509Name.entry_count ()) + #print(X509Name.as_text ()) - self.X509Certificate.sign( pkey=self.PKey, md='sha1' ) - print(self.X509Certificate.as_text ()) + self.X509Certificate.set_issuer_name( X509Name ) + + # + # signing + # + + self.X509Certificate.sign( pkey=self.PKey, md='sha1' ) + print(self.X509Certificate.as_text ()) if __name__ == '__main__': - run = Cert () - run.CreatePKey () - run.CreateX509Request () - run.CreateX509Certificate () + run = Cert () + run.CreatePKey () + run.CreateX509Request () + run.CreateX509Certificate () diff --git a/contrib/isaac.httpslib.py b/contrib/isaac.httpslib.py index a5ea5e2..1415e4f 100644 --- a/contrib/isaac.httpslib.py +++ b/contrib/isaac.httpslib.py @@ -1,6 +1,6 @@ from __future__ import print_function -"""M2Crypto support for Python 1.5.2 and Python 2.x's httplib. +"""M2Crypto support for Python 1.5.2 and Python 2.x's httplib. Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved.""" @@ -9,7 +9,7 @@ from httplib import * import SSL if sys.version[0] == '2': - + if sys.version[:3] in ['2.1', '2.2']: # In 2.1 and above, httplib exports "HTTP" only. from httplib import HTTPConnection, HTTPS_PORT @@ -17,16 +17,16 @@ if sys.version[0] == '2': from httplib import HTTPResponse,FakeSocket class HTTPSConnection(HTTPConnection): - + """ This class allows communication via SSL using M2Crypto. """ - + default_port = HTTPS_PORT - + def __init__(self, host, port=None, **ssl): keys = ssl.keys() - try: + try: keys.remove('key_file') except ValueError: pass @@ -46,11 +46,11 @@ if sys.version[0] == '2': except KeyError: self.ssl_ctx = SSL.Context('sslv23') HTTPConnection.__init__(self, host, port) - + def connect(self): self.sock = SSL.Connection(self.ssl_ctx) self.sock.connect((self.host, self.port)) - + def close(self): # This kludges around line 545 of httplib.py, # which closes the connection in this object; @@ -58,7 +58,7 @@ if sys.version[0] == '2': # object. # # M2Crypto doesn't close-here-keep-open-there, - # so, in effect, we don't close until the whole + # so, in effect, we don't close until the whole # business is over and gc kicks in. # # Long-running callers beware leakage. @@ -70,9 +70,9 @@ if sys.version[0] == '2': class HTTPS(HTTP): - + _connection_class = HTTPSConnection - + def __init__(self, host='', port=None, **ssl): HTTP.__init__(self, host, port) try: @@ -84,7 +84,7 @@ if sys.version[0] == '2': elif sys.version[:3] == '1.5': class HTTPS(HTTP): - + def __init__(self, ssl_context, host='', port=None): assert isinstance(ssl_context, SSL.Context) self.debuglevel=0 @@ -92,7 +92,7 @@ elif sys.version[:3] == '1.5': self.ssl_ctx=ssl_context if host: self.connect(host, port) - + def connect(self, host, port=None): # Cribbed from httplib.HTTP. if not port: @@ -112,7 +112,7 @@ elif sys.version[:3] == '1.5': class HTTPProxyConnection(HTTPConnection): """ This class provides HTTP access through (authenticated) proxies. - + Example: If the HTTP proxy address is proxy.your.org:8080, an authenticated proxy (one which requires a username/password combination in order to serve @@ -163,7 +163,7 @@ class HTTPProxyConnection(HTTPConnection): HTTPConnection.putrequest(self, method, newurl) # Add proxy-specific headers self._add_auth_proxy_header() - + def _add_auth_proxy_header(self): """Adds an HTTP header for authenticated proxies """ @@ -179,7 +179,7 @@ class HTTPProxyConnection(HTTPConnection): class HTTPSProxyResponse(HTTPResponse): """ Replacement class for HTTPResponse - Proxy responses (made through SSL) have to keep the connection open + Proxy responses (made through SSL) have to keep the connection open after the initial request, since the connection is tunneled to the SSL host with the CONNECT method. """ @@ -189,7 +189,7 @@ class HTTPSProxyResponse(HTTPResponse): class HTTPSProxyConnection(HTTPProxyConnection): """This class provides HTTP access through (authenticated) proxies. - + Example: If the HTTP proxy address is proxy.your.org:8080, an authenticated proxy (one which requires a username/password combination in order to serve @@ -219,12 +219,12 @@ class HTTPSProxyConnection(HTTPProxyConnection): HTTPProxyConnection.__init__(self, proxy, host, port, username, password) def connect(self): - """Connect (using SSL) to the host and port specified in __init__ + """Connect (using SSL) to the host and port specified in __init__ (through a proxy).""" import socket # Set the connection with the proxy HTTPProxyConnection.connect(self) - # Use the stock HTTPConnection putrequest + # Use the stock HTTPConnection putrequest host = "%s:%s" % (self._host, self._port) HTTPConnection.putrequest(self, "CONNECT", host) # Add proxy-specific stuff diff --git a/contrib/smimeplus.py b/contrib/smimeplus.py index 57adb50..2cb7fd7 100644 --- a/contrib/smimeplus.py +++ b/contrib/smimeplus.py @@ -17,7 +17,7 @@ class smimeplus(object): self.setcacert(cacert) self.randfile = randfile self.__loadrand() - + def __passcallback(self, v): """private key passphrase callback function""" return self.passphrase @@ -43,7 +43,7 @@ class smimeplus(object): return _data def __pack(self, msg): - """Convert 'msg' to string and put it into an memory buffer for + """Convert 'msg' to string and put it into an memory buffer for openssl operation""" return M2Crypto.BIO.MemoryBuffer(self.__gettext(msg)) @@ -72,7 +72,7 @@ class smimeplus(object): def verify(self, smsg, scert): """Verify to see if 'smsg' was signed by 'scert', and scert was - issued by cacert of this object. Return message signed if success, + issued by cacert of this object. Return message signed if success, None otherwise""" # Load signer's cert. _x509 = M2Crypto.X509.load_cert_bio(self.__pack(scert)) @@ -89,7 +89,7 @@ class smimeplus(object): _sender = M2Crypto.SMIME.SMIME() _sender.set_x509_stack(_stack) _sender.set_x509_store(_store) - + # Load signed message, verify it, and return result _p7, _data = M2Crypto.SMIME.smime_load_pkcs7_bio(self.__pack(smsg)) try: @@ -100,23 +100,23 @@ class smimeplus(object): def encrypt(self, rcert, msg): # Instantiate an SMIME object. _sender = M2Crypto.SMIME.SMIME() - + # Load target cert to encrypt to. _x509 = M2Crypto.X509.load_cert_bio(self.__pack(rcert)) _stack = M2Crypto.X509.X509_Stack() _stack.push(_x509) _sender.set_x509_stack(_stack) - + _sender.set_cipher(M2Crypto.SMIME.Cipher(self.cipher)) - + # Encrypt the buffer. _buf = self.__pack(self.__gettext(msg)) _p7 = _sender.encrypt(_buf) - + # Output p7 in mail-friendly format. _out = self.__pack('') _sender.write(_out, _p7) - + # Save the PRNG's state. self.__saverand() @@ -129,10 +129,10 @@ class smimeplus(object): _sender = M2Crypto.SMIME.SMIME() _sender.load_key_bio(self.__pack(self.key), self.__pack(self.cert), callback=self.__passcallback) - + # Load the encrypted data. _p7, _data = M2Crypto.SMIME.smime_load_pkcs7_bio(self.__pack(emsg)) - + # Decrypt p7. try: return _sender.decrypt(_p7) -- cgit v1.2.1