summaryrefslogtreecommitdiffstats
path: root/third_party/tlslite/test/twistedserver.py
blob: 9946e4295c13710e3b09af5f12234ac7855e8d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
from twisted.protocols.policies import WrappingFactory
from twisted.protocols.basic import LineReceiver
from twisted.python import log
from twisted.python.failure import Failure
import sys
from tlslite.api import *

s = open("./serverX509Cert.pem").read()
x509 = X509()
x509.parse(s)
certChain = X509CertChain([x509])

s = open("./serverX509Key.pem").read()
privateKey = parsePEMKey(s, private=True)

verifierDB = VerifierDB("verifierDB")
verifierDB.open()

class Echo(LineReceiver):
  def connectionMade(self):
      self.transport.write("Welcome to the echo server!\r\n")

  def lineReceived(self, line):
      self.transport.write(line + "\r\n")

class Echo1(Echo):
  def connectionMade(self):
      if not self.transport.tlsStarted:
          self.transport.setServerHandshakeOp(certChain=certChain,
                                              privateKey=privateKey,
                                              verifierDB=verifierDB)
      else:
          Echo.connectionMade(self)

  def connectionLost(self, reason):
      pass #Handle any TLS exceptions here

class Echo2(Echo):
  def lineReceived(self, data):
      if data == "STARTTLS":
          self.transport.setServerHandshakeOp(certChain=certChain,
                                              privateKey=privateKey,
                                              verifierDB=verifierDB)
      else:
          Echo.lineReceived(self, data)

  def connectionLost(self, reason):
      pass #Handle any TLS exceptions here

factory = Factory()
factory.protocol = Echo1
#factory.protocol = Echo2

wrappingFactory = WrappingFactory(factory)
wrappingFactory.protocol = TLSTwistedProtocolWrapper

log.startLogging(sys.stdout)
reactor.listenTCP(1079, wrappingFactory)
reactor.run()