aboutsummaryrefslogtreecommitdiff
path: root/cryptech/libhal.py
diff options
context:
space:
mode:
Diffstat (limited to 'cryptech/libhal.py')
-rw-r--r--cryptech/libhal.py29
1 files changed, 17 insertions, 12 deletions
diff --git a/cryptech/libhal.py b/cryptech/libhal.py
index 0c6b3f6..273a8a0 100644
--- a/cryptech/libhal.py
+++ b/cryptech/libhal.py
@@ -43,7 +43,6 @@ import uuid
import xdrlib
import socket
import logging
-import contextlib
logger = logging.getLogger(__name__)
@@ -117,10 +116,7 @@ HALError.define(HAL_ERROR_KEYSTORE_LOST_DATA = "Keystore appears to have
HALError.define(HAL_ERROR_BAD_ATTRIBUTE_LENGTH = "Bad attribute length")
HALError.define(HAL_ERROR_ATTRIBUTE_NOT_FOUND = "Attribute not found")
HALError.define(HAL_ERROR_NO_KEY_INDEX_SLOTS = "No key index slots available")
-HALError.define(HAL_ERROR_KSI_INDEX_UUID_MISORDERED = "Key index UUID misordered")
-HALError.define(HAL_ERROR_KSI_INDEX_CHUNK_ORPHANED = "Key index chunk orphaned")
-HALError.define(HAL_ERROR_KSI_INDEX_CHUNK_MISSING = "Key index chunk missing")
-HALError.define(HAL_ERROR_KSI_INDEX_CHUNK_OVERLAPS = "Key index chunk overlaps")
+HALError.define(HAL_ERROR_KS_INDEX_UUID_MISORDERED = "Key index UUID misordered")
HALError.define(HAL_ERROR_KEYSTORE_WRONG_BLOCK_TYPE = "Wrong block type in keystore")
HALError.define(HAL_ERROR_RPC_PROTOCOL_ERROR = "RPC protocol error")
HALError.define(HAL_ERROR_NOT_IMPLEMENTED = "Not implemented")
@@ -406,11 +402,20 @@ class PKey(Handle):
return result
def export_pkey(self, pkey):
- return self.hsm.pkey_export(pkey = pkey, kekek = self, pkcs8_max = 2560, kek_max = 512)
+ return self.hsm.pkey_export(pkey = pkey, kekek = self, pkcs8_max = 5480, kek_max = 512)
def import_pkey(self, pkcs8, kek, flags = 0):
return self.hsm.pkey_import(kekek = self, pkcs8 = pkcs8, kek = kek, flags = flags)
+class ContextManagedUnpacker(xdrlib.Unpacker):
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.done()
+
+
class HSM(object):
mixed_mode = False
@@ -432,7 +437,7 @@ class HSM(object):
logger.debug("send: %s", ":".join("{:02x}".format(ord(c)) for c in msg))
self.socket.sendall(msg)
- def _recv(self, code): # Returns an xdrlib.Unpacker
+ def _recv(self, code): # Returns a ContextManagedUnpacker
closed = False
while True:
msg = [self.sockfile.read(1)]
@@ -445,7 +450,7 @@ class HSM(object):
msg = slip_decode("".join(msg))
if not msg:
continue
- msg = xdrlib.Unpacker("".join(msg))
+ msg = ContextManagedUnpacker("".join(msg))
if msg.unpack_uint() != code:
continue
return msg
@@ -483,7 +488,6 @@ class HSM(object):
self._pack_arg(packer, name)
self._pack_arg(packer, HAL_PKEY_ATTRIBUTE_NIL if value is None else value)
- @contextlib.contextmanager
def rpc(self, code, *args, **kwargs):
client = kwargs.get("client", 0)
packer = xdrlib.Packer()
@@ -494,8 +498,7 @@ class HSM(object):
unpacker = self._recv(code)
client = unpacker.unpack_uint()
self._raise_if_error(unpacker.unpack_uint())
- yield unpacker
- unpacker.done()
+ return unpacker
def get_version(self):
with self.rpc(RPC_FUNC_GET_VERSION) as r:
@@ -537,7 +540,9 @@ class HSM(object):
with self.rpc(RPC_FUNC_HASH_GET_ALGORITHM, handle) as r:
return HALDigestAlgorithm.index[r.unpack_uint()]
- def hash_initialize(self, alg, key = "", client = 0, session = 0, mixed_mode = None):
+ def hash_initialize(self, alg, key = None, client = 0, session = 0, mixed_mode = None):
+ if key is None:
+ key = ""
if mixed_mode is None:
mixed_mode = self.mixed_mode
if mixed_mode: