blob: dcad3ba00ced9575e47f45aac85f482634a45ea3 [file] [log] [blame]
Jean-Paul Calderone3de9f622008-03-12 14:12:19 -04001# -*- coding: latin-1 -*-
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05002#
3# Copyright (C) 2001 Martin Sjögren and AB Strakt, All rights reserved
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04004# Copyright (C) Jean-Paul Calderone 2008, All rights reserved
5
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006"""
7Simple SSL client, using blocking I/O
8"""
9
10from OpenSSL import SSL
11import sys, os, select, socket
12
13def verify_cb(conn, cert, errnum, depth, ok):
14 # This obviously has to be updated
15 print 'Got certificate: %s' % cert.get_subject()
16 return ok
17
18if len(sys.argv) < 3:
19 print 'Usage: python[2] client.py HOST PORT'
20 sys.exit(1)
21
22dir = os.path.dirname(sys.argv[0])
23if dir == '':
24 dir = os.curdir
25
26# Initialize context
27ctx = SSL.Context(SSL.SSLv23_METHOD)
28ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate
29ctx.use_privatekey_file (os.path.join(dir, 'client.pkey'))
30ctx.use_certificate_file(os.path.join(dir, 'client.cert'))
31ctx.load_verify_locations(os.path.join(dir, 'CA.cert'))
32
33# Set up client
34sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
35sock.connect((sys.argv[1], int(sys.argv[2])))
36
37while 1:
38 line = sys.stdin.readline()
39 if line == '':
40 break
41 try:
42 sock.send(line)
43 sys.stdout.write(sock.recv(1024))
44 sys.stdout.flush()
45 except SSL.Error:
46 print 'Connection died unexpectedly'
47 break
48
49
50sock.shutdown()
51sock.close()