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
|
# -*- coding: latin-1 -*-
# client.py
#
# Copyright (C) 2001 Martin Sjögren and AB Strakt, All rights reserved
#
# $Id: client.py,v 1.7 2002/08/15 12:20:46 martin Exp $
#
"""
Simple SSL client, using blocking I/O
"""
from OpenSSL import SSL
import sys, os, select, socket
def verify_cb(conn, cert, errnum, depth, ok):
# This obviously has to be updated
print 'Got certificate: %s' % cert.get_subject()
return ok
if len(sys.argv) < 3:
print 'Usage: python[2] client.py HOST PORT'
sys.exit(1)
dir = os.path.dirname(sys.argv[0])
if dir == '':
dir = os.curdir
# Initialize context
ctx = SSL.Context(SSL.SSLv23_METHOD)
ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate
ctx.use_privatekey_file (os.path.join(dir, 'client.pkey'))
ctx.use_certificate_file(os.path.join(dir, 'client.cert'))
ctx.load_verify_locations(os.path.join(dir, 'CA.cert'))
# Set up client
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
sock.connect((sys.argv[1], int(sys.argv[2])))
while 1:
line = sys.stdin.readline()
if line == '':
break
try:
sock.send(line)
sys.stdout.write(sock.recv(1024))
sys.stdout.flush()
except SSL.Error:
print 'Connection died unexpectedly'
break
sock.shutdown()
sock.close()
|