From 613fa5de2f443c2375287bb23217c73f6b64ed22 Mon Sep 17 00:00:00 2001 From: Scott Petersen Date: Mon, 21 Oct 2013 16:42:25 -0700 Subject: [PATCH] Can now use key/cert from memory in addition to from a file path. --- pyad2usb/devices.py | 22 ++++++++++++++++++---- test.py | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pyad2usb/devices.py b/pyad2usb/devices.py index a5ca05b..b93c380 100644 --- a/pyad2usb/devices.py +++ b/pyad2usb/devices.py @@ -11,7 +11,7 @@ import threading import serial import serial.tools.list_ports import socket -from OpenSSL import SSL +from OpenSSL import SSL, crypto from pyftdi.pyftdi.ftdi import * from pyftdi.pyftdi.usbtools import * from . import util @@ -742,9 +742,23 @@ class SocketDevice(Device): if self._use_ssl: try: ctx = SSL.Context(SSL.TLSv1_METHOD) - ctx.use_privatekey_file(self.ssl_key) - ctx.use_certificate_file(self.ssl_certificate) - ctx.load_verify_locations(self.ssl_ca, None) + + if isinstance(self.ssl_key, crypto.PKey): + ctx.use_privatekey(self.ssl_key) + else: + ctx.use_privatekey_file(self.ssl_key) + + if isinstance(self.ssl_certificate, crypto.X509): + ctx.use_certificate(self.ssl_certificate) + else: + ctx.use_certificate_file(self.ssl_certificate) + + if isinstance(self.ssl_ca, crypto.X509): + store = ctx.get_cert_store() + store.add_cert(self.ssl_ca) + else: + ctx.load_verify_locations(self.ssl_ca, None) + ctx.set_verify(SSL.VERIFY_PEER, self._verify_ssl_callback) self._device = SSL.Connection(ctx, self._device) diff --git a/test.py b/test.py index 724ab96..42814b2 100755 --- a/test.py +++ b/test.py @@ -374,14 +374,14 @@ try: #test_serial() #upload_serial() - #test_usb() + test_usb() #test_usb_serial() #test_factory() #test_factory_watcher() #upload_usb() #upload_usb_serial() - test_socket() + #test_socket() #upload_socket() #test_no_read_thread()