-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a support for Cicada HSM (from a CryptaLabs).
- Loading branch information
Showing
5 changed files
with
180 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import abc | ||
|
||
|
||
class HSM(abc.ABC): | ||
|
||
def load(self): | ||
''' | ||
Load from persistent storage. | ||
Return True if OK (private key exists) or False if privake key doesn't exists | ||
''' | ||
return False | ||
|
||
|
||
def store(self): | ||
''' | ||
Store to persistent storage. Can be NOOP. | ||
''' | ||
pass | ||
|
||
|
||
@abc.abstractmethod | ||
def generate_private_key(self): | ||
''' | ||
Generate a private key. | ||
''' | ||
pass | ||
|
||
|
||
@abc.abstractmethod | ||
def get_public_key(self): | ||
''' | ||
Get a public key for a private key | ||
''' | ||
pass | ||
|
||
|
||
@abc.abstractmethod | ||
def sign(self, payload): | ||
''' | ||
Sign a payload, return 'r' and 's'. | ||
''' | ||
pass |
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,59 @@ | ||
import cryptography.hazmat.backends | ||
|
||
from .hsm_abc import HSM | ||
|
||
class CicadaHSM(HSM): | ||
|
||
def __init__(self): | ||
self._PrivateKey = None | ||
self.P11URI = None | ||
|
||
backend = cryptography.hazmat.backends.default_backend() | ||
backend._lib.ENGINE_load_dynamic() | ||
engine = backend._lib.ENGINE_by_id(b"dynamic"); | ||
backend.openssl_assert(engine != backend._ffi.NULL) | ||
engine = backend._ffi.gc(engine, backend._lib.ENGINE_free) | ||
|
||
backend._lib.ENGINE_ctrl_cmd_string(engine, b"SO_PATH", b"/usr/lib/arm-linux-gnueabihf/engines-1.1/libpkcs11.so", 0) | ||
backend._lib.ENGINE_ctrl_cmd_string(engine, b"ID", b"pkcs11", 0) | ||
backend._lib.ENGINE_ctrl_cmd_string(engine, b"LOAD", backend._ffi.NULL, 0) | ||
backend._lib.ENGINE_ctrl_cmd_string(engine, b"MODULE_PATH", b"/usr/lib/cicada-pkcs11.so", 0) | ||
res = backend._lib.ENGINE_init(engine) | ||
backend.openssl_assert(res > 0) | ||
|
||
self.Engine = engine | ||
|
||
|
||
def generate_private_key(self): | ||
print("Not implemented yet!") | ||
|
||
|
||
def load(self): | ||
p11uri = "pkcs11:object=test-key;type=private" | ||
backend = cryptography.hazmat.backends.default_backend() | ||
pkey = backend._lib.ENGINE_load_private_key( | ||
self.Engine, | ||
p11uri.encode("utf-8"), | ||
backend._ffi.NULL, | ||
backend._ffi.NULL | ||
) | ||
|
||
backend.openssl_assert(pkey != backend._ffi.NULL) | ||
pkey = backend._ffi.gc(pkey, backend._lib.EVP_PKEY_free) | ||
self.P11URI = p11uri | ||
self._PrivateKey = backend._evp_pkey_to_private_key(pkey) | ||
|
||
|
||
def get_public_key(self): | ||
return self._PrivateKey.public_key() | ||
|
||
|
||
def sign(self, payload): | ||
signature_RFC3279 = self._PrivateKey.sign( | ||
payload, | ||
cryptography.hazmat.primitives.asymmetric.ec.ECDSA( | ||
cryptography.hazmat.primitives.hashes.SHA256() | ||
) | ||
) | ||
r, s = cryptography.hazmat.primitives.asymmetric.utils.decode_dss_signature(signature_RFC3279) | ||
return r, s |
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,56 @@ | ||
import os.path | ||
|
||
import cryptography.hazmat.primitives.asymmetric.ec | ||
import cryptography.hazmat.primitives.serialization | ||
|
||
from .hsm_abc import HSM | ||
|
||
|
||
class EmulatedHSM(HSM): | ||
|
||
def __init__(self, directory): | ||
self.Directory = directory | ||
self._PrivateKey = None | ||
|
||
|
||
def load(self): | ||
try: | ||
self._PrivateKey = cryptography.hazmat.primitives.serialization.load_der_private_key( | ||
open(os.path.join(self.Directory, 'itss.key'),'rb').read(), | ||
password=b'strong-and-secret :-)', | ||
backend=cryptography.hazmat.backends.default_backend() | ||
) | ||
except: | ||
return False | ||
return True | ||
|
||
|
||
def store(self): | ||
x = self._PrivateKey.private_bytes( | ||
encoding=cryptography.hazmat.primitives.serialization.Encoding.DER, | ||
format=cryptography.hazmat.primitives.serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm=cryptography.hazmat.primitives.serialization.BestAvailableEncryption(b'strong-and-secret :-)') | ||
) | ||
open(os.path.join(self.Directory, 'itss.key'),'wb').write(x) | ||
|
||
|
||
def generate_private_key(self): | ||
self._PrivateKey = cryptography.hazmat.primitives.asymmetric.ec.generate_private_key( | ||
cryptography.hazmat.primitives.asymmetric.ec.SECP256R1(), | ||
cryptography.hazmat.backends.default_backend() | ||
) | ||
|
||
|
||
def get_public_key(self): | ||
return self._PrivateKey.public_key() | ||
|
||
|
||
def sign(self, payload): | ||
signature_RFC3279 = self._PrivateKey.sign( | ||
payload, | ||
cryptography.hazmat.primitives.asymmetric.ec.ECDSA( | ||
cryptography.hazmat.primitives.hashes.SHA256() | ||
) | ||
) | ||
r, s = cryptography.hazmat.primitives.asymmetric.utils.decode_dss_signature(signature_RFC3279) | ||
return r, s |
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