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