Skip to content

Commit a0a1273

Browse files
Openssl sign verify (#3852)
* Added openSSL tool and testsuites to lay ground work for encryption and decryption tests * Fixing tuple type error in actions * made helper method private and added more to test description * Removed duplicated line * fixed comment
1 parent fab456b commit a0a1273

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

lisa/tools/openssl.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT license.
33

44
import shlex
5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, Tuple
66

77
from lisa.executable import Tool
88

@@ -37,7 +37,7 @@ def encrypt(
3737
return self._run_with_piped_input(
3838
plaintext,
3939
f"enc -{algorithm} -K '{hex_key}' -iv '{hex_iv}' -base64 -A",
40-
expected_exit_code_failure_message="Failed to encrypt data with OpenSSL.",
40+
expected_exit_code_failure_message=("Failed to encrypt data with OpenSSL."),
4141
)
4242

4343
def decrypt(
@@ -56,7 +56,69 @@ def decrypt(
5656
return self._run_with_piped_input(
5757
ciphertext,
5858
f"enc -d -{algorithm} -K '{hex_key}' -iv '{hex_iv}' -base64 -A",
59-
expected_exit_code_failure_message="Failed to decrypt data with OpenSSL.",
59+
expected_exit_code_failure_message=("Failed to decrypt data with OpenSSL."),
60+
)
61+
62+
def create_key_pair(self, algorithm: str = "RSA") -> Tuple[str, str]:
63+
"""
64+
Generate a key pair using the specified algorithm.
65+
Returns the private key and public key as strings.
66+
67+
This key generation is for testing generation of keys
68+
with OpenSSL on the remote.
69+
"""
70+
private_key_result = self.run(
71+
f"genpkey -algorithm {algorithm} -outform PEM",
72+
expected_exit_code=0,
73+
expected_exit_code_failure_message=(
74+
"Failed to generate private key with OpenSSL."
75+
),
76+
)
77+
private_key_pem = private_key_result.stdout.strip()
78+
public_key = self._run_with_piped_input(
79+
private_key_pem,
80+
"pkey -in /dev/stdin -pubout -outform PEM",
81+
expected_exit_code_failure_message=(
82+
"Failed to generate public key with OpenSSL."
83+
),
84+
)
85+
return private_key_pem, public_key
86+
87+
def sign(
88+
self,
89+
data: str,
90+
private_key: str,
91+
algorithm: str = "sha256",
92+
) -> str:
93+
"""
94+
Sign the data using the specified private key and algorithm.
95+
Returns the base64 encoded signature.
96+
"""
97+
return self._run_with_piped_input(
98+
data,
99+
f"dgst -{algorithm} -sign <(echo '{private_key}') | openssl base64 -A",
100+
expected_exit_code_failure_message="Failed to sign data with OpenSSL.",
101+
)
102+
103+
def verify(
104+
self,
105+
data: str,
106+
public_key: str,
107+
signature_base64: str,
108+
algorithm: str = "sha256",
109+
) -> None:
110+
"""
111+
Verify the signature of the data using the specified
112+
public key and algorithm.
113+
"""
114+
self._run_with_piped_input(
115+
data,
116+
f"dgst -{algorithm} -verify <(echo '{public_key}') "
117+
f"-signature <(echo '{signature_base64}' | "
118+
"openssl base64 -A -d)",
119+
expected_exit_code_failure_message=(
120+
"Failed to verify signature with OpenSSL."
121+
),
60122
)
61123

62124
def _run_with_piped_input(

microsoft/testsuites/security/openssl.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Tests the functionality of OpenSSL, including encryption and decryption
1717
operations. Validates that OpenSSL can successfully encrypt plaintext data
1818
and decrypt it back to its original form using generated keys and IVs.
19+
Validates that OpenSSL signs and verifies signatures correctly.
1920
""",
2021
)
2122
class OpenSSLTestSuite(TestSuite):
@@ -32,12 +33,16 @@ class OpenSSLTestSuite(TestSuite):
3233
priority=2,
3334
)
3435
def verify_openssl_basic(self, log: Logger, node: Node) -> None:
36+
"""This function tests the basic functionality of
37+
OpenSSL by calling helper functions"""
3538
self._openssl_test_encrypt_decrypt(log, node)
39+
self._openssl_test_sign_verify(log, node)
3640

3741
def _openssl_test_encrypt_decrypt(self, log: Logger, node: Node) -> None:
3842
"""
3943
Tests OpenSSL encryption and decryption functionality.
4044
This function generates a random key and IV, encrypts various types of
45+
plaintext, and then decrypts them to verify the functionality.
4146
"""
4247

4348
# Key and IV for encryption and decryption.
@@ -66,3 +71,18 @@ def _openssl_test_encrypt_decrypt(self, log: Logger, node: Node) -> None:
6671
assert_that(plaintext).described_as(
6772
"Plaintext and decrypted data do not match"
6873
).is_equal_to(decrypted_data)
74+
75+
def _openssl_test_sign_verify(self, log: Logger, node: Node) -> None:
76+
"""
77+
Tests OpenSSL signing and verification functionality.
78+
This function generates a key pair, signs a message,
79+
and verifies the signature.
80+
"""
81+
openssl = node.tools[OpenSSL]
82+
private_key, public_key = openssl.create_key_pair()
83+
84+
plaintext = "cool"
85+
signature = openssl.sign(plaintext, private_key)
86+
openssl.verify(plaintext, public_key, signature)
87+
88+
log.debug("Successfully signed and verified a file.")

0 commit comments

Comments
 (0)