Jean-Paul Calderone | 3de9f62 | 2008-03-12 14:12:19 -0400 | [diff] [blame] | 1 | # -*- coding: latin-1 -*- |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2001 Martin Sjögren and AB Strakt, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame^] | 4 | # Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
| 5 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | """ |
| 7 | Simple SSL client, using blocking I/O |
| 8 | """ |
| 9 | |
| 10 | from OpenSSL import SSL |
| 11 | import sys, os, select, socket |
| 12 | |
| 13 | def 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 | |
| 18 | if len(sys.argv) < 3: |
| 19 | print 'Usage: python[2] client.py HOST PORT' |
| 20 | sys.exit(1) |
| 21 | |
| 22 | dir = os.path.dirname(sys.argv[0]) |
| 23 | if dir == '': |
| 24 | dir = os.curdir |
| 25 | |
| 26 | # Initialize context |
| 27 | ctx = SSL.Context(SSL.SSLv23_METHOD) |
| 28 | ctx.set_verify(SSL.VERIFY_PEER, verify_cb) # Demand a certificate |
| 29 | ctx.use_privatekey_file (os.path.join(dir, 'client.pkey')) |
| 30 | ctx.use_certificate_file(os.path.join(dir, 'client.cert')) |
| 31 | ctx.load_verify_locations(os.path.join(dir, 'CA.cert')) |
| 32 | |
| 33 | # Set up client |
| 34 | sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM)) |
| 35 | sock.connect((sys.argv[1], int(sys.argv[2]))) |
| 36 | |
| 37 | while 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 | |
| 50 | sock.shutdown() |
| 51 | sock.close() |