Skip to content

Commit 5206edc

Browse files
committed
Resolved issue 313
replaced the Popen commands in check_cert_key to use OpenSSL
1 parent 6646436 commit 5206edc

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

ssm/crypto.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,37 @@ def check_cert_key(certpath, keypath):
6464
if cert == key:
6565
return False
6666

67-
p1 = Popen(['openssl', 'x509', '-pubkey', '-noout'],
68-
stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
69-
pubkey1, error = p1.communicate(cert)
67+
try:
68+
certificate = OpenSSL.crypto.load_certificate(
69+
OpenSSL.crypto.FILETYPE_PEM, cert
70+
)
71+
crypto_public_key = certificate.get_pubkey()
72+
public_key_bytes = OpenSSL.crypto.dump_publickey(
73+
OpenSSL.crypto.FILETYPE_PEM, crypto_public_key
74+
)
75+
76+
certificate_public_key = public_key_bytes.decode("utf-8")
7077

71-
if error != '':
78+
except Exception as error:
7279
log.error(error)
7380
return False
81+
82+
try:
83+
private_key = OpenSSL.crypto.load_privatekey(
84+
OpenSSL.crypto.FILETYPE_PEM, key
85+
)
86+
public_key_bytes = OpenSSL.crypto.dump_publickey(
87+
OpenSSL.crypto.FILETYPE_PEM, private_key
88+
)
89+
90+
private_public_key = public_key_bytes.decode("utf-8")
7491

75-
p2 = Popen(['openssl', 'pkey', '-pubout'],
76-
stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
77-
pubkey2, error = p2.communicate(key)
78-
79-
if error != '':
92+
except Exception as error:
8093
log.error(error)
8194
return False
82-
83-
return pubkey1.strip() == pubkey2.strip()
95+
96+
97+
return certificate_public_key.strip() == private_public_key.strip()
8498

8599
def sign(text, certpath, keypath):
86100
"""Sign the message using the certificate and key in the files specified.

0 commit comments

Comments
 (0)