1414HAS_OPENSSL = shutil .which ("openssl" ) is not None
1515
1616
17- def _check_cert_signature (cert_path , digest ):
17+ def _check_cert_signature (cert_path , digest , inform = "PEM" ):
1818 """Use OpenSSL to verify the signature on a self-signed certificate.
1919
2020 Returns True on success, raises AssertionError on failure.
@@ -24,10 +24,13 @@ def _check_cert_signature(cert_path, digest):
2424 raise unittest .SkipTest ("openssl not available" )
2525
2626 stripped = cert_path + ".stripped.pem"
27+ sig_bin = cert_path + ".sig.bin"
28+ body_bin = cert_path + ".body.bin"
29+ pub_pem = cert_path + ".pub.pem"
2730 try :
2831 subprocess .run (
29- ["openssl" , "x509" , "-in " , cert_path , "-out " , stripped ,
30- "-outform" , "PEM" ],
32+ ["openssl" , "x509" , "-inform " , inform , "-in " , cert_path ,
33+ "-out" , stripped , "- outform" , "PEM" ],
3134 check = True , capture_output = True , timeout = 60 )
3235
3336 # Extract signature hex
@@ -48,17 +51,14 @@ def _check_cert_signature(cert_path, digest):
4851 lines .append (stripped_line )
4952 sig_hex = "" .join (lines )
5053
51- sig_bin = cert_path + ".sig.bin"
5254 with open (sig_bin , "wb" ) as f :
5355 f .write (bytes .fromhex (sig_hex ))
5456
55- body_bin = cert_path + ".body.bin"
5657 subprocess .run (
5758 ["openssl" , "asn1parse" , "-in" , stripped , "-strparse" , "4" ,
5859 "-out" , body_bin , "-noout" ],
5960 check = True , capture_output = True , timeout = 60 )
6061
61- pub_pem = cert_path + ".pub.pem"
6262 with open (pub_pem , "w" ) as pub_f :
6363 subprocess .run (
6464 ["openssl" , "x509" , "-in" , stripped , "-noout" , "-pubkey" ],
@@ -150,7 +150,7 @@ def test_1c_pem_to_der_signature(self):
150150 "-in" , os .path .join (CERTS_DIR , "ca-cert.pem" ),
151151 "-out" , out )
152152 self .assertEqual (r .returncode , 0 , r .stderr )
153- _check_cert_signature (out , "sha256" )
153+ _check_cert_signature (out , "sha256" , inform = "DER" )
154154
155155 def test_1d_der_to_pem_stdout (self ):
156156 """DER -> PEM to stdout succeeds."""
@@ -175,7 +175,7 @@ def test_1e_der_to_der_signature(self):
175175 "-in" , os .path .join (CERTS_DIR , "ca-cert.der" ),
176176 "-out" , out )
177177 self .assertEqual (r .returncode , 0 , r .stderr )
178- _check_cert_signature (out , "sha256" )
178+ _check_cert_signature (out , "sha256" , inform = "DER" )
179179
180180 def test_1f_der_text_noout (self ):
181181 """DER text/noout succeeds."""
0 commit comments