-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added password changing capabilities to the SGX powHSM implementation - Added main SGX manager script - Added build script for the SGX manager - Added password change and password retries getter commands to the HSM2DongleSGX - Updated user options to make names more generic - Added unit tests - Updated middleware docker do script to expose the host to the container (allowing for running tests against e.g. an SGX powHSM simulator)
- Loading branch information
1 parent
ecc626d
commit 38abda6
Showing
9 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
source $(dirname $0)/bld-docker manager_sgx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2021 RSK Labs Ltd | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
# of the Software, and to permit persons to whom the Software is furnished to do | ||
# so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
import os | ||
from sgx.hsm2dongle import HSM2DongleSGX | ||
from mgr.runner import ManagerRunner | ||
from ledger.pin import FileBasedPin | ||
from user.options import UserOptionParser | ||
|
||
|
||
def load_pin(user_options): | ||
env_pin = os.environ.get("PIN", None) | ||
if env_pin is not None: | ||
env_pin = env_pin.encode() | ||
pin = FileBasedPin( | ||
user_options.pin_file, | ||
default_pin=env_pin, | ||
force_change=user_options.force_pin_change, | ||
) | ||
return pin | ||
|
||
|
||
def configure_protocol_messages(protocol): | ||
protocol.MESSAGES = { | ||
"restart": "restart the SGX powHSM", | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
user_options = UserOptionParser("Start the powHSM manager for SGX", | ||
with_pin=True, | ||
with_tcpconn=True, | ||
host_name="SGX", | ||
default_tcpconn_port=7777).parse() | ||
|
||
runner = ManagerRunner("powHSM manager for SGX", | ||
lambda options: HSM2DongleSGX(options.tcpconn_host, | ||
options.tcpconn_port, | ||
options.io_debug), | ||
load_pin, configure_protocol_messages) | ||
|
||
runner.run(user_options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2021 RSK Labs Ltd | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
# of the Software, and to permit persons to whom the Software is furnished to do | ||
# so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from unittest import TestCase | ||
from unittest.mock import Mock, patch | ||
from sgx.hsm2dongle import HSM2DongleSGX | ||
from ledger.hsm2dongle import HSM2DongleError | ||
|
||
import logging | ||
|
||
logging.disable(logging.CRITICAL) | ||
|
||
|
||
class TestHSM2DongleSGX(TestCase): | ||
EXPECTED_DONGLE_TIMEOUT = 10 | ||
|
||
@patch("ledger.hsm2dongle_tcp.getDongle") | ||
def setUp(self, getDongleMock): | ||
self.dongle = Mock() | ||
self.getDongleMock = getDongleMock | ||
self.getDongleMock.return_value = self.dongle | ||
self.hsm2dongle = HSM2DongleSGX("a-host", 1234, "a-debug-value") | ||
|
||
self.getDongleMock.assert_not_called() | ||
self.hsm2dongle.connect() | ||
self.getDongleMock.assert_called_with("a-host", 1234, "a-debug-value") | ||
self.assertEqual(self.hsm2dongle.dongle, self.dongle) | ||
|
||
def assert_exchange_called(self, bs): | ||
self.dongle.exchange.assert_called_with(bs, timeout=self.EXPECTED_DONGLE_TIMEOUT) | ||
|
||
def test_echo_ok(self): | ||
self.dongle.exchange.return_value = bytes([0x80, 0xA4, 0x41, 0x42, 0x43]) | ||
self.assertTrue(self.hsm2dongle.echo()) | ||
self.assert_exchange_called(bytes([0x80, 0xA4, 0x41, 0x42, 0x43])) | ||
|
||
def test_echo_response_differs(self): | ||
self.dongle.exchange.return_value = bytes([1, 2, 3]) | ||
self.assertFalse(self.hsm2dongle.echo()) | ||
self.assert_exchange_called(bytes([0x80, 0xA4, 0x41, 0x42, 0x43])) | ||
|
||
def test_echo_error_triggered(self): | ||
self.dongle.exchange.side_effect = RuntimeError("SomethingWentWrong") | ||
with self.assertRaises(HSM2DongleError) as cm: | ||
self.hsm2dongle.echo() | ||
self.assert_exchange_called(bytes([0x80, 0xA4, 0x41, 0x42, 0x43])) | ||
|
||
def test_unlock_ok(self): | ||
self.dongle.exchange.return_value = bytes([0xAA, 0xBB, 0xCC, 0xDD]) | ||
self.assertTrue(self.hsm2dongle.unlock(b'a-password')) | ||
self.assert_exchange_called(bytes([0x80, 0xA3, 0x00]) + b'a-password') | ||
|
||
def test_unlock_wrong_pass(self): | ||
self.dongle.exchange.return_value = bytes([0xAA, 0xBB, 0x00, 0xDD]) | ||
self.assertFalse(self.hsm2dongle.unlock(b'wrong-pass')) | ||
self.assert_exchange_called(bytes([0x80, 0xA3, 0x00]) + b'wrong-pass') | ||
|
||
def test_newpin_ok(self): | ||
self.dongle.exchange.return_value = bytes([0xAA, 0xBB, 0x01, 0xDD]) | ||
self.assertTrue(self.hsm2dongle.new_pin(b'new-password')) | ||
self.assert_exchange_called(bytes([0x80, 0xA5, 0x00]) + b'new-password') | ||
|
||
def test_newpin_error(self): | ||
self.dongle.exchange.return_value = bytes([0xAA, 0xBB, 0x55, 0xDD]) | ||
self.assertFalse(self.hsm2dongle.new_pin(b'new-password')) | ||
self.assert_exchange_called(bytes([0x80, 0xA5, 0x00]) + b'new-password') | ||
|
||
def test_get_retries(self): | ||
self.dongle.exchange.return_value = bytes([0xAA, 0xBB, 0x05, 0xDD]) | ||
self.assertEqual(5, self.hsm2dongle.get_retries()) | ||
self.assert_exchange_called(bytes([0x80, 0xA2])) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters