Skip to content

Commit

Permalink
Merge pull request openlawlibrary#444 from Rana-KV/update-yubikey
Browse files Browse the repository at this point in the history
Support Yubikey Manager 5.1.x
  • Loading branch information
renatav authored Jun 11, 2024
2 parents d3effa0 + 115fd40 commit 2dcee04
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning][semver].

### Added


- Support for Yubikey Manager 5.1.x ([444])
- Support for Python 3.11 and 3.12 ([440])
- Fix add_target_repo when signing role is the top-level targets role ([431])
- New git hook that validates repo before push ([423])
Expand All @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning][semver].

### Changed

- Dropped support for Yubikey Manager 4.x [444]
- Only load the latest mirrors.jon ([441])
- Fix generation of keys when they should be printed to the command line ([435])
- Made Updater faster through parallelization ([434])
Expand All @@ -32,6 +33,7 @@ and this project adheres to [Semantic Versioning][semver].
- Fixes repeating error messages in taf repo create and manual entry of keys-description ([432])
- When checking if branch is synced, find first remote that works, instead of only trying the last remote url ([419])

[444]: https://github.com/openlawlibrary/taf/pull/444
[440]: https://github.com/openlawlibrary/taf/pull/440
[435]: https://github.com/openlawlibrary/taf/pull/435
[434]: https://github.com/openlawlibrary/taf/pull/434
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def finalize_options(self):
"jsonschema==3.2.0",
]

yubikey_require = ["yubikey-manager==4.0.*"]
yubikey_require = ["yubikey-manager==5.1.*"]

# Determine the appropriate version of pygit2 based on the Python version
if sys.version_info > (3, 10):
Expand Down
51 changes: 27 additions & 24 deletions taf/yubikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from tuf.repository_tool import import_rsakey_from_pem
from ykman.device import list_all_devices, connect_to_device
from ykman.device import list_all_devices
from yubikit.core.smartcard import SmartCardConnection
from ykman.piv import (
KEY_TYPE,
Expand Down Expand Up @@ -110,31 +110,34 @@ def _yk_piv_ctrl(serial=None, pub_key_pem=None):
# If pub_key_pem is given, iterate all devices, read x509 certs and try to match
# public keys.
if pub_key_pem is not None:
for _, info in list_all_devices():
connection, _, device = connect_to_device(
info.serial, [SmartCardConnection]
)
session = PivSession(connection)
device_pub_key_pem = (
session.get_certificate(SLOT.SIGNATURE)
.public_key()
.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
for dev, info in list_all_devices():
# Connect to a YubiKey over a SmartCardConnection, which is needed for PIV.
with dev.open_connection(SmartCardConnection) as connection:
session = PivSession(connection)
device_pub_key_pem = (
session.get_certificate(SLOT.SIGNATURE)
.public_key()
.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
.decode("utf-8")
)
.decode("utf-8")
)
# Tries to match without last newline char
if (
device_pub_key_pem == pub_key_pem
or device_pub_key_pem[:-1] == pub_key_pem
):
break
yield session, device.serial
# Tries to match without last newline char
if (
device_pub_key_pem == pub_key_pem
or device_pub_key_pem[:-1] == pub_key_pem
):
break
yield session, info.serial
else:
connection, _, device = connect_to_device(serial, [SmartCardConnection])
session = PivSession(connection)
yield session, device.serial
for dev, info in list_all_devices():
if serial is None or info.serial == serial:
with dev.open_connection(SmartCardConnection) as connection:
session = PivSession(connection)
yield session, info.serial
else:
pass


def is_inserted():
Expand Down

0 comments on commit 2dcee04

Please sign in to comment.