[Twisted-Python] Strange SSL behaviour.

Voznesensky Vladimir vovic at nfi.kiae.ru
Mon Jun 18 08:49:26 MDT 2007


Dear all!

I have a bug demonstration code.
Blocking SSL works where Twisted one does not.
It is below.
How should I handle this case?
Can anybody tell me what to do with this bug?
I can dig into twisted sources, but I'm not a
twisted specialist, so it could take weeks to
boil them to get the dirt.
Help me, please.

Thank you.
VV

---8<---
from twisted.python.util import println

from twisted.protocols.basic import LineReceiver
from twisted.internet import protocol, reactor, defer, ssl
from twisted.python.failure import Failure
from OpenSSL import crypto, SSL

_CMD="""VERSION=MYPROXYv2
COMMAND=%d
USERNAME=%s
PASSPHRASE=%s
LIFETIME=%d\0""" % (0, "LOGIN", 'PASSPHRASE', 100)
WHERETO = ("myproxy.cern.ch",7512)

## Myproxy client protocol.
class MyproxyClient(protocol.Protocol):

   def connectionMade(self):
     self.transport.write('0') # GT compat. stuff.
     self.transport.write(_CMD)

   def dataReceived(self, data):
     self.transport.loseConnection()

   def connectionLost(self, 
reason=protocol.connectionDone):
     d = self.factory.deferred
     if reason != protocol.connectionDone:
       d.errback(reason)
     else:
       d.callback(data)

## Myproxy client factory.
class MyproxyClientFactory(protocol.ClientFactory):
   protocol = MyproxyClient

   def __init__(self):
     self.deferred = defer.Deferred()

   def clientConnectionFailed(self, connector, reason):
     self.deferred.errback(reason)

## Context factory suitable for local needs.
class CF:
   def getContext(self):
     ctx = SSL.Context(SSL.SSLv3_METHOD)
     # disable for compatibility with myproxy server (er, 
globus)
     # globus doesn't handle this case, apparently, and 
instead
     # chokes in proxy delegation code
     ctx.set_options(0x00000800L)

     return ctx

ctx = CF().getContext()
import socket
conn = SSL.Connection(ctx,socket.socket())
conn.connect(WHERETO)
conn.write('0')
conn.write(_CMD)
dat = conn.recv(8192)
print 'data received by blocking call\n', dat
conn.close()
del ctx

f = MyproxyClientFactory()
contextFactory = CF()
reactor.connectSSL(WHERETO[0], WHERETO[1], f, 
contextFactory)
f.deferred.addCallbacks(
       callback=lambda data:(println("data received", 
data),reactor.stop()),
       errback=lambda error:(println("an error occurred", 
error),reactor.stop()))
reactor.run()
---8<---

Yours,
VV




More information about the Twisted-Python mailing list