Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add simulator device #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions agents/simulator/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
from setuptools import setup

setup(
name='simulator_agent',
version='0.0.1',
description='Using a simulation of a hardware device as SSH/GPG/age agent',
author='Roman Zeyde',
author_email='dev@romanzey.de',
url='http://github.com/romanz/trezor-agent',
scripts=['simulator_agent.py'],
install_requires=[
'libagent>=0.14.0'
],
platforms=['POSIX', 'win32'],
classifiers=[
'Environment :: Console',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: Information Technology',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',
'Operating System :: POSIX',
'Operating System :: Microsoft :: Windows',
'Programming Language :: Python :: 3 :: Only',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: System :: Networking',
'Topic :: Communications',
'Topic :: Security',
'Topic :: Utilities',
],
entry_points={'console_scripts': [
'simulator-agent = simulator_agent:ssh_agent',
'simulator-gpg = simulator_agent:gpg_tool',
'simulator-gpg-agent = simulator_agent:gpg_agent',
'simulator-signify = simulator_agent:signify_tool',
'age-plugin-simulator = simulator_agent:age_tool',
]},
)
8 changes: 8 additions & 0 deletions agents/simulator/simulator_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from libagent import age, signify, gpg, ssh
from libagent.device.simulator import Simulator as DeviceType

age_tool = lambda: age.main(DeviceType)
ssh_agent = lambda: ssh.main(DeviceType)
gpg_tool = lambda: gpg.main(DeviceType)
gpg_agent = lambda: gpg.run_agent(DeviceType)
signify_tool = lambda: signify.main(DeviceType)
5 changes: 3 additions & 2 deletions libagent/age/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run_pubkey(device_type, args):
'so please note that the API and features may '
'change without backwards compatibility!')

c = client.Client(device=device_type())
c = client.Client(device=device_type(args))
pubkey = c.pubkey(identity=client.create_identity(args.identity), ecdh=True)
recipient = bech32_encode(prefix="age", data=pubkey)
print(f"# recipient: {recipient}")
Expand Down Expand Up @@ -89,7 +89,7 @@ def decrypt(key, encrypted):
def run_decrypt(device_type, args):
"""Unlock hardware device (for future interaction)."""
# pylint: disable=too-many-locals
c = client.Client(device=device_type())
c = client.Client(device=device_type(args))

lines = (line.strip() for line in sys.stdin) # strip whitespace
lines = (line for line in lines if line) # skip empty lines
Expand Down Expand Up @@ -148,6 +148,7 @@ def _handle_single_file(file_index, stanzas, identities, c):
def main(device_type):
"""Parse command-line arguments."""
p = argparse.ArgumentParser()
device_type.setup_arg_parser(p)

agent_package = device_type.package_name()
resources_map = {r.key: r for r in pkg_resources.require(agent_package)}
Expand Down
4 changes: 4 additions & 0 deletions libagent/device/fake_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def package_name(cls):
"""Python package name."""
return 'fake-device-agent'

@classmethod
def setup_arg_parser(cls, parser):
"""Add device-specific parameters to the argument parser."""

def connect(self):
"""Return "dummy" connection."""
log.critical('NEVER USE THIS CODE FOR REAL-LIFE USE-CASES!!!')
Expand Down
15 changes: 14 additions & 1 deletion libagent/device/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def get_curve_name(self, ecdh=False):
class Device:
"""Abstract cryptographic hardware device interface."""

def __init__(self):
def __init__(self, args):
"""C-tor."""
self.conn = None
self.args = args

def connect(self):
"""Connect to device, otherwise raise NotFoundError."""
Expand Down Expand Up @@ -142,10 +143,22 @@ def sign(self, identity, blob):
"""Sign given blob and return the signature (as bytes)."""
raise NotImplementedError()

def sign_with_pubkey(self, identity, blob):
"""Sign given blob and return the signature (as bytes)."""
return (self.sign(identity, blob),
formats.compress_pubkey(self.pubkey(identity, ecdh=False),
identity.get_curve_name(False)))

def ecdh(self, identity, pubkey):
"""Get shared session key using Elliptic Curve Diffie-Hellman."""
raise NotImplementedError()

def ecdh_with_pubkey(self, identity, pubkey):
"""Get shared session key using Elliptic Curve Diffie-Hellman."""
return (self.ecdh(identity, pubkey),
formats.compress_pubkey(self.pubkey(identity, ecdh=True),
identity.get_curve_name(True))[1:])

def __str__(self):
"""Human-readable representation."""
return '{}'.format(self.__class__.__name__)
4 changes: 4 additions & 0 deletions libagent/device/jade.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def package_name(cls):
"""Python package name (at PyPI)."""
return 'jade-agent'

@classmethod
def setup_arg_parser(cls, parser):
"""Add device-specific parameters to the argument parser."""

def connect(self):
"""Connect to the first matching device."""
# pylint: disable=import-error
Expand Down
4 changes: 4 additions & 0 deletions libagent/device/keepkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def package_name(cls):
"""Python package name (at PyPI)."""
return 'keepkey-agent'

@classmethod
def setup_arg_parser(cls, parser):
"""Add device-specific parameters to the argument parser."""

@property
def _defs(self):
from . import keepkey_defs
Expand Down
4 changes: 4 additions & 0 deletions libagent/device/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def package_name(cls):
"""Python package name (at PyPI)."""
return 'ledger-agent'

@classmethod
def setup_arg_parser(cls, parser):
"""Add device-specific parameters to the argument parser."""

def connect(self):
"""Enumerate and connect to the first USB HID interface."""
try:
Expand Down
4 changes: 4 additions & 0 deletions libagent/device/onlykey.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def package_name(cls):
"""Python package name (at PyPI)."""
return 'onlykey-agent'

@classmethod
def setup_arg_parser(cls, parser):
"""Add device-specific parameters to the argument parser."""

@property
def _defs(self):
from . import onlykey_defs
Expand Down
Loading