yubihsm.core
Core classes for YubiHSM communication.
Attributes
Classes
Data class holding various information about the YubiHSM. |
|
YubiHSM log entry. |
|
Data class holding response data from a GET_LOGS command. |
|
An unauthenticated connection to a YubiHSM. |
|
A negotiation of an authenticated Session with a YubiHSM. |
|
A negotiation of an authenticated Session with a YubiHSM. |
|
An authenticated secure session with a YubiHSM. |
Module Contents
- yubihsm.core.KEY_ENC = 4
- yubihsm.core.KEY_MAC = 6
- yubihsm.core.KEY_RMAC = 7
- yubihsm.core.CARD_CRYPTOGRAM = 0
- yubihsm.core.HOST_CRYPTOGRAM = 1
- class yubihsm.core.DeviceInfo[source]
Data class holding various information about the YubiHSM.
- Variables:
version – YubiHSM version tuple.
serial – YubiHSM serial number.
log_size – Log entry storage capacity.
log_used – Log entries currently stored.
supported_algorithms – List of supported algorithms.
part_number – Chip designator.
- version: yubihsm.defs.Version
- supported_algorithms: Set[yubihsm.defs.ALGORITHM]
- class yubihsm.core.LogEntry[source]
YubiHSM log entry.
- Parameters:
number (int) – The sequence number of the entry.
command (int) – The COMMAND executed.
length (int) – The length of the command.
session_key (int) – The ID of the Authentication Key for the session.
target_key (int) – The ID of the key used by the command.
second_key (int) – The ID of the secondary key used by the command, if applicable.
result (int) – The result byte of the response.
tick (int) – The YubiHSM system tick value when the command was run.
digest (bytes) – A truncated hash of the entry and previous digest.
- command: yubihsm.defs.COMMAND
- property data: bytes
Get log entry binary data.
- Returns:
The binary LogEntry data, excluding the digest.
- Return type:
- class yubihsm.core.LogData[source]
Bases:
NamedTuple
Data class holding response data from a GET_LOGS command.
- Parameters:
n_boot – Number of unlogged boot events.
n_auth – Number of unlogged authentication events.
entries – List of LogEntry items.
- class yubihsm.core.YubiHsm(backend)[source]
An unauthenticated connection to a YubiHSM.
- Parameters:
backend (yubihsm.backends.YhsmBackend)
- send_cmd(cmd, data=b'')[source]
Encode and send a command byte and its associated data.
- Parameters:
cmd (yubihsm.defs.COMMAND) – The command to send.
data (bytes) – The command payload to send.
- Returns:
The response data from the YubiHSM.
- Return type:
- get_device_info()[source]
Get general device information from the YubiHSM.
- Returns:
Device information.
- Return type:
- get_device_public_key()[source]
Retrieve the device’s public key.
- Returns:
The device public key.
- Return type:
cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
- init_session(auth_key_id)[source]
Initiate the symmetric authentication process for establishing an authenticated session with the YubiHSM.
- Parameters:
auth_key_id (int) – The ID of the Authentication key used to authenticate the session.
- Returns:
A negotiation of an authenticated Session with a YubiHSM.
- Return type:
- init_session_asymmetric(auth_key_id, epk_oce)[source]
Initiate the asymmetric authentication process for establishing an authenticated session with the YubiHSM.
- Parameters:
- Return type:
- create_session(auth_key_id, key_enc, key_mac)[source]
Create an authenticated session with the YubiHSM.
See also create_session_derived, which derives K-ENC and K-MAC from a password.
- Parameters:
- Returns:
An authenticated session.
- Return type:
- create_session_derived(auth_key_id, password)[source]
Create an authenticated session with the YubiHSM.
Uses a supplied password to derive the keys K-ENC and K-MAC.
- Parameters:
- Returns:
An authenticated session.
- Return type:
- create_session_asymmetric(auth_key_id, private_key, public_key=None)[source]
Create an authenticated session with the YubiHSM.
- Parameters:
auth_key_id (int) – The ID of the Authentication key used to authenticate the session.
private_key (cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey) – Private key corresponding to the public authentication key object.
public_key (Optional[cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey]) – The device’s public key. If omitted, the public key is fetched from the YubiHSM.
- Returns:
An authenticated session.
- Return type:
- class yubihsm.core.SymmetricAuth(hsm, sid, context, card_crypto)[source]
A negotiation of an authenticated Session with a YubiHSM.
This class is used to begin the mutual authentication process for establishing an authenticated session with the YubiHSM, using symmetric authentication. Typically you get an instance of this class by calling
init_session()
.- classmethod init_session(hsm, auth_key_id)[source]
Initiate the mutual symmetric session authentication process.
- Parameters:
- Return type:
- classmethod create_session(hsm, auth_key_id, key_enc, key_mac)[source]
Construct an authenticated session.
- Parameters:
- Return type:
- class yubihsm.core.AsymmetricAuth(hsm, sid, context, receipt)[source]
A negotiation of an authenticated Session with a YubiHSM.
This class is used to begin the mutual authentication process for establishing an authenticated session with the YubiHSM, using asymmetric authentication. Typically you get an instance of this class by calling
init_session_asymmetric()
.- classmethod init_session(hsm, auth_key_id, epk_oce)[source]
Initiate the mutual asymmetric session authentication process.
- Parameters:
- Return type:
- classmethod create_session(hsm, auth_key_id, private_key, public_key)[source]
Construct an authenticated session.
- Parameters:
hsm (YubiHsm) – The YubiHSM connection.
auth_key_id (int) – The ID of the Authentication key used to authenticate the session.
private_key (cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePrivateKey) – Private key corresponding to the public authentication key object.
public_key (cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey) – The device’s public key.
- Return type:
- class yubihsm.core.AuthSession(hsm, sid, key_enc, key_mac, key_rmac, mac_chain)[source]
An authenticated secure session with a YubiHSM.
Typically you get an instance of this class by calling
create_session()
,create_session_derived()
, orcreate_session_asymmetric()
.- Parameters:
- close()[source]
Close this session with the YubiHSM.
Once closed, this session object can no longer be used, unless re-connected.
- Return type:
None
- send_secure_cmd(cmd, data=b'')[source]
Send a command over the encrypted session.
- Parameters:
cmd (yubihsm.defs.COMMAND) – The command to send.
data (bytes) – The command payload to send.
- Returns:
The decrypted response data from the YubiHSM.
- Return type:
- list_objects(object_id=None, object_type=None, domains=None, capabilities=None, algorithm=None, label=None)[source]
List objects from the YubiHSM.
This returns a list of all objects currently stored on the YubiHSM, which are accessible by this session. The arguments to this method can be used to filter the results returned.
- Parameters:
object_id (Optional[int]) – Return only objects with this ID.
object_type (Optional[yubihsm.defs.OBJECT]) – Return only objects of this type.
domains (Optional[int]) – Return only objects belonging to one or more of these domains.
capabilities (Optional[int]) – Return only objects with one or more of these capabilities.
algorithm (Optional[yubihsm.defs.ALGORITHM]) – Return only objects with this algorithm.
label (Optional[str]) – Return only objects with this label.
- Returns:
A list of matched objects.
- Return type:
Sequence[yubihsm.objects.YhsmObject]
- get_object(object_id, object_type)[source]
Get a reference to a YhsmObject with the given id and type.
The object returned will be a subclass of YhsmObject corresponding to the given object_type.
- Parameters:
object_id (int) – The ID of the object to retrieve.
object_type (yubihsm.defs.OBJECT) – The type of the object to retrieve.
- Returns:
An object reference.
- Return type:
- reset_device()[source]
Perform a factory reset of the YubiHSM.
Resets and reboots the YubiHSM, deletes all Objects and restores the default Authkey.
- Return type:
None
- get_log_entries(previous_entry=None)[source]
Get logs from the YubiHSM.
This returns a tuple of the number of unlogged boot events, the number of unlogged authentication events, and the log entries from the YubiHSM. The chain of entry digests will be validated, starting from the first entry returned, or the one supplied as previous_entry.
- set_log_index(index)[source]
Clear logs to free up space for use with forced audit.
- Parameters:
index (int) – The log entry index to clear up to (inclusive).
- Return type:
None
- put_option(option, value)[source]
Set the raw value of a YubiHSM device option.
- Parameters:
option (yubihsm.defs.OPTION) – The OPTION to set.
value (bytes) – The value to set the OPTION to.
- Return type:
None
- get_option(option)[source]
Get the raw value of a YubiHSM device option.
- Parameters:
option (yubihsm.defs.OPTION) – The OPTION to get.
- Returns:
The currently set value for the given OPTION
- Return type:
- set_force_audit(audit)[source]
Set the FORCE_AUDIT mode of the YubiHSM.
- Parameters:
audit (yubihsm.defs.AUDIT) – The AUDIT mode to set.
- Return type:
None
- get_force_audit()[source]
Get the current setting for forced audit mode.
- Returns:
The AUDIT setting for FORCE_AUDIT.
- Return type:
- set_command_audit(commands)[source]
Set audit mode of commands.
Takes a dict of COMMAND -> AUDIT pairs and updates the audit settings for the commands given.
- Parameters:
commands (Mapping[yubihsm.defs.COMMAND, yubihsm.defs.AUDIT]) – Settings to update.
- Example:
- Return type:
None
>>> session.set_comment_audit({ ... COMMAND.ECHO: AUDIT.OFF, ... COMMAND.LIST_OBJECTS: AUDIT.ON ... })
- get_command_audit()[source]
Get a mapping of all available commands and their audit settings.
- Returns:
Dictionary of COMMAND -> AUDIT pairs.
- Return type:
Mapping[yubihsm.defs.COMMAND, yubihsm.defs.AUDIT]
- set_enabled_algorithms(algorithms)[source]
Set audit mode of commands.
New in YubiHSM 2.2.0.
Algorithms can only be toggled on a “fresh” device (after reset, before adding objects).
Takes a dict of ALGORITHM -> bool pairs and updates the enabled algorithm settings for the algorithms given.
- Parameters:
algorithms (Mapping[yubihsm.defs.ALGORITHM, bool]) – The algorithms to update.
- Example:
- Return type:
None
>>> session.set_enabled_algorithms({ ... ALGORITHM.RSA_2048: False, ... ALGORITHM.RSA_OAEP_SHA256_: True, ... })
- get_enabled_algorithms()[source]
Get the algorithms available, and whether or not they are enabled.
- Returns:
A mapping of algorithms, to whether or not they are enabled.
- Return type:
Mapping[yubihsm.defs.ALGORITHM, bool]
- set_fips_mode(mode)[source]
Set the FIPS mode of the YubiHSM.
YubiHSM2 FIPS only.
This can only be toggled on a “fresh” device (after reset, before adding objects).
- Parameters:
mode (bool) – Whether to be in FIPS compliant mode or not.
- Return type:
None