Skip to content

Commit 66b43d5

Browse files
nethsm: Ignore whitespace in base64 data
This patch updates the nethsm dependency to v1.1.0 and uses the new ignore_whitespace option in Base64.from_encoded to ignore whitespace in our base64 input. The main motivation is that the base64 tool per default inserts linebreaks into long data, so previously piping the base64 output into pynitrokey could lead to errors. Fixes: #538
1 parent 8e920eb commit 66b43d5

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

pynitrokey/cli/nethsm.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def make_enum_type(enum_cls: EnumMeta) -> click.Choice:
3737
return click.Choice([variant.value for variant in enum_cls], case_sensitive=False)
3838

3939

40+
def base64_input(s: str) -> Base64:
41+
return Base64.from_encoded(s, ignore_whitespace=True)
42+
43+
4044
API_CERTIFICATE_MIME_TYPE = "application/x-pem-file"
4145
KEY_CERTIFICATE_MIME_TYPES = [
4246
"application/x-pem-file",
@@ -688,9 +692,9 @@ def add_key(
688692
if not public_exponent:
689693
public_exponent = prompt_str("Public exponent")
690694
private_key = nethsm_sdk.RsaPrivateKey(
691-
prime_p=Base64.from_encoded(prime_p),
692-
prime_q=Base64.from_encoded(prime_q),
693-
public_exponent=Base64.from_encoded(public_exponent),
695+
prime_p=base64_input(prime_p),
696+
prime_q=base64_input(prime_q),
697+
public_exponent=base64_input(public_exponent),
694698
)
695699
else:
696700
if prime_p:
@@ -703,7 +707,7 @@ def add_key(
703707
)
704708
if not data:
705709
data = prompt_str("Key data")
706-
private_key = nethsm_sdk.GenericPrivateKey(data=Base64.from_encoded(data))
710+
private_key = nethsm_sdk.GenericPrivateKey(data=base64_input(data))
707711

708712
with connect(ctx) as nethsm:
709713
key_id = nethsm.add_key(
@@ -1543,9 +1547,9 @@ def encrypt(ctx: Context, key_id: str, data: str, mode: str, iv: Optional[str])
15431547
with connect(ctx) as nethsm:
15441548
encrypted = nethsm.encrypt(
15451549
key_id,
1546-
Base64.from_encoded(data),
1550+
base64_input(data),
15471551
nethsm_sdk.EncryptMode.from_string(mode),
1548-
iv=Base64.from_encoded(iv) if iv else None,
1552+
iv=base64_input(iv) if iv else None,
15491553
)
15501554
print(f"Encrypted: {encrypted.encrypted.data}")
15511555
print(f"Initialization vector: {encrypted.iv.data}")
@@ -1586,9 +1590,9 @@ def decrypt(ctx: Context, key_id: str, data: str, mode: str, iv: Optional[str])
15861590
with connect(ctx) as nethsm:
15871591
decrypted = nethsm.decrypt(
15881592
key_id,
1589-
Base64.from_encoded(data),
1593+
base64_input(data),
15901594
nethsm_sdk.DecryptMode.from_string(mode),
1591-
Base64.from_encoded(iv) if iv else None,
1595+
base64_input(iv) if iv else None,
15921596
)
15931597
print(decrypted.data)
15941598

@@ -1620,6 +1624,6 @@ def sign(ctx: Context, key_id: str, data: str, mode: str) -> None:
16201624
This command requires authentication as a user with the Operator role."""
16211625
with connect(ctx) as nethsm:
16221626
signature = nethsm.sign(
1623-
key_id, Base64.from_encoded(data), nethsm_sdk.SignMode.from_string(mode)
1627+
key_id, base64_input(data), nethsm_sdk.SignMode.from_string(mode)
16241628
)
16251629
print(signature.data)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies = [
4040
"protobuf >=3.17.3, < 4.0.0",
4141
"click-aliases",
4242
"semver",
43-
"nethsm >= 1.0.0,<2",
43+
"nethsm >= 1.1.0,<2",
4444
]
4545
dynamic = ["version", "description"]
4646

0 commit comments

Comments
 (0)