~ubuntu-branches/ubuntu/raring/m2crypto/raring

« back to all changes in this revision

Viewing changes to demo/ssl/s_client.py

  • Committer: Barry Warsaw
  • Date: 2011-10-25 18:26:01 UTC
  • mfrom: (24.1.4 merge)
  • Revision ID: barry@python.org-20111025182601-uag0ee3eh3ahtofm
Merge from Debian with remaining deltas.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
"""An M2Crypto implementation of OpenSSL's s_client.
4
 
 
5
 
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
6
 
 
7
 
from socket import *
8
 
import getopt
9
 
import string
10
 
import sys
11
 
 
12
 
from M2Crypto import SSL
13
 
 
14
 
# s_server -www
15
 
HOST='127.0.0.1'
16
 
PORT=4433
17
 
REQ='GET / HTTP/1.0\r\n\r\n'
18
 
 
19
 
class Config:
20
 
    pass
21
 
 
22
 
def config(args):
23
 
    options=['connect=', 'verify=', 'cert=', 'key=', 'CApath=', 'CAfile=', \
24
 
        'reconnect', 'pause', 'showcerts', 'debug', 'nbio_test', 'state', \
25
 
        'nbio', 'crlf', 'sslv2', 'sslv3', 'tlsv1', 'no_sslv2', 'no_sslv3', \
26
 
        'no_tlsv1', 'bugs', 'cipher=', 'Verify=']
27
 
    optlist, optarg=getopt.getopt(args, '', options)
28
 
 
29
 
    cfg=Config()
30
 
    for opt in optlist:
31
 
        setattr(cfg, opt[0][2:], opt[1])
32
 
    for x in (('tlsv1','no_tlsv1'),('sslv3','no_sslv3'),('sslv2','no_sslv2')):
33
 
        if hasattr(cfg, x[0]) and hasattr(cfg, x[1]):
34
 
                raise ValueError, 'mutually exclusive: %s and %s' % x
35
 
 
36
 
    if hasattr(cfg, 'connect'):
37
 
        (host, port)=string.split(cfg.connect, ':')
38
 
        cfg.connect=(host, int(port))
39
 
    else:
40
 
        cfg.connect=(HOST, PORT)
41
 
 
42
 
    cfg.protocol=[]
43
 
    # First protocol found will be used.
44
 
    # Permutate the following tuple for preference. 
45
 
    for p in ('tlsv1', 'sslv3', 'sslv2'):
46
 
        if hasattr(cfg, p):
47
 
            cfg.protocol.append(p)
48
 
    cfg.protocol.append('sslv23')
49
 
 
50
 
    return cfg
51
 
 
52
 
def make_context(config):
53
 
    ctx=SSL.Context(config.protocol[0])
54
 
    if hasattr(config, 'cert'):
55
 
        cert=config.cert
56
 
    else:
57
 
        cert='client.pem'
58
 
    if hasattr(config, 'key'):
59
 
        key=config.key
60
 
    else:
61
 
        key='client.pem'
62
 
    #ctx.load_cert(cert, key)
63
 
 
64
 
    if hasattr(config, 'verify'):
65
 
        verify=SSL.verify_peer
66
 
        depth=int(config.verify)
67
 
    elif hasattr(config, 'Verify'):
68
 
        verify=SSL.verify_peer | SSL.verify_fail_if_no_peer_cert
69
 
        depth=int(config.Verify)
70
 
    else:
71
 
        verify=SSL.verify_none
72
 
        depth=10
73
 
    config.verify=verify
74
 
    config.verify_depth=depth
75
 
    ctx.set_verify(verify, depth)
76
 
 
77
 
    if hasattr(config, 'CAfile'):
78
 
        cafile=config.CAfile
79
 
    else:
80
 
        cafile='ca.pem'
81
 
    ctx.load_verify_location(cafile)
82
 
 
83
 
    return ctx
84
 
 
85
 
def s_client(config):
86
 
    ctx=make_context(config)
87
 
    s=SSL.Connection(ctx)
88
 
    s.connect(config.connect)
89
 
    if config.verify != SSL.verify_none and not s.verify_ok():
90
 
        print 'peer verification failed'
91
 
        peer=s.get_peer_cert()
92
 
        if peer is None:
93
 
            print 'unable to get peer certificate'
94
 
        else:
95
 
            print 'peer.as_text()'
96
 
        raise SystemExit
97
 
    s.send(REQ)
98
 
    while 1:
99
 
        data=s.recv()
100
 
        if not data:
101
 
            break
102
 
        print data
103
 
    s.close()
104
 
 
105
 
if __name__=='__main__':
106
 
    cfg=config(sys.argv[1:])
107
 
    s_client(cfg)
108