File: s_server.py

package info (click to toggle)
m2crypto 0.46.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: python: 22,936; makefile: 213; ansic: 94; sh: 17
file content (231 lines) | stat: -rw-r--r-- 5,291 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python

from __future__ import print_function

"""An M2Crypto implementation of OpenSSL's s_server.

Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""

from socket import *
import asyncore
import cStringIO
import getopt
import string
import sys

from M2Crypto import SSL, BIO, DH, Err

# s_server -www
HOST = ""
PORT = 4433


class Config:
    pass


def config(args):
    options = [
        "accept=",
        "context=",
        "verify=",
        "Verify=",
        "cert=",
        "key=",
        "dcert=",
        "dkey=",
        "nocert",
        "crlf",
        "debug",
        "CApath=",
        "CAfile=",
        "quiet",
        "no_tmp_rsa",
        "state",
        "sslv2",
        "sslv3",
        "tlsv1",
        "no_sslv2",
        "no_sslv3",
        "no_tlsv1",
        "bugs",
        "cipher=",
    ]
    optlist, optarg = getopt.getopt(args, "", options)

    cfg = Config()
    for opt in optlist:
        setattr(cfg, opt[0][2:], opt[1])
    for x in (("tlsv1", "no_tlsv1"), ("sslv3", "no_sslv3"), ("sslv2", "no_sslv2")):
        if hasattr(cfg, x[0]) and hasattr(cfg, x[1]):
            raise ValueError("mutually exclusive: %s and %s" % x)

    if hasattr(cfg, "accept"):
        cfg.accept = string.split(cfg.connect, ":")
    else:
        cfg.accept = (HOST, PORT)

    cfg.protocol = []
    # First protocol found will be used.
    # Permutate the following tuple for preference.
    for p in ("tlsv1", "sslv3", "sslv2"):
        if hasattr(cfg, p):
            cfg.protocol.append(p)
    cfg.protocol.append("sslv23")

    return cfg


RESP_HEAD = """\
HTTP/1.0 200 ok
Content-type: text/html

<HTML><BODY BGCOLOR=\"#ffffff\">
<pre>

Emulating s_server -www
Ciphers supported in s_server.py
"""

RESP_TAIL = """\
</pre>
</BODY></HTML>
"""


class channel(SSL.ssl_dispatcher):

    def __init__(self, conn, debug):
        SSL.ssl_dispatcher.__init__(self, conn)
        self.socket.setblocking(0)
        self.buffer = self.fixup_buffer()
        self.debug = debug

    def fixup_buffer(self):
        even = 0
        buffer = cStringIO.StringIO()
        buffer.write(RESP_HEAD)
        for c in self.get_ciphers():
            # This formatting works for around 80 columns.
            buffer.write("%-11s:%-28s" % (c.version(), c.name()))
            if even:
                buffer.write("\r\n")
                even = 1 - even
        buffer.write("\r\n%s" % RESP_TAIL)
        return buffer.getvalue()

    def handle_connect(self):
        pass

    def handle_close(self):
        self.close()

    def handle_error(self, exc_type, exc_value, exc_traceback):
        if self.debug:
            print("handle_error()")
        # print(exc_type, exc_value, exc_traceback)
        print(Err.get_error())
        self.handle_close()

    def writeable(self):
        return len(self.buffer)

    def handle_write(self):
        n = self.send(self.buffer)
        if n == -1:
            pass
        elif n == 0:
            self.handle_close()
        else:
            self.buffer = self.buffer[n:]
        if self.debug:
            print("handle_write():", n)

    def readable(self):
        return 1

    def handle_read(self):
        blob = self.recv()
        if blob is None:
            pass
        elif blob == "":
            self.handle_close()
        else:
            pass
        if self.debug:
            print("handle_read():", blob)


class server(SSL.ssl_dispatcher):

    channel_class = channel

    def __init__(self, addr, port, config, ssl_context):
        asyncore.dispatcher.__init__(self)
        self.create_socket(ssl_context)
        self.set_reuse_addr()
        self.socket.setblocking(0)
        self.bind((addr, port))
        self.listen(5)
        self.config = config
        self.debug = config.debug
        self.ssl_ctx = ssl_context

    def handle_accept(self):
        sock, addr = self.accept()
        print(self.ssl_ctx.get_verify_mode())
        if (self.ssl_ctx.get_verify_mode() is SSL.verify_none) or sock.verify_ok():
            self.channel_class(sock, self.debug)
        else:
            print("client verification failed")
            sock.close()

    def writeable(self):
        return 0


def s_server(config):
    ctx = SSL.Context(config.protocol[0])

    if hasattr(config, "debug"):
        config.debug = 1
    else:
        config.debug = 0

    if hasattr(config, "cert"):
        cert = config.cert
    else:
        cert = "server.pem"
    if hasattr(config, "key"):
        cert = config.key
    else:
        cert = "server.pem"
    ctx.load_cert(cert)

    if hasattr(config, "CAfile"):
        cafile = config.CAfile
    else:
        cafile = "ca.pem"
    ctx.load_verify_location(cafile)

    if hasattr(config, "verify"):
        verify = SSL.verify_peer
        depth = int(config.verify)
    elif hasattr(config, "Verify"):
        verify = SSL.verify_peer | SSL.verify_fail_if_no_peer_cert
        depth = int(config.Verify)
    else:
        verify = SSL.verify_none
        depth = 0
    ctx.set_verify(verify, depth)

    ctx.set_tmp_dh("dh1024.pem")
    # ctx.set_info_callback()

    server(cfg.accept[0], cfg.accept[1], cfg, ctx)
    asyncore.loop()


if __name__ == "__main__":
    cfg = config(sys.argv[1:])
    s_server(cfg)