Skip to content

Commit f9dbf02

Browse files
committed
fix: incorrect str->bytes->str conversion
1 parent 173c91e commit f9dbf02

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pyeudiw/x509/verify.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ def to_DER_cert(cert: str | bytes) -> bytes:
144144
if _BASE64_RE.fullmatch(cert_s):
145145
return B64DER_cert_to_DER_cert(cert_s)
146146

147-
raise ValueError("unable to recognize input [cert] as a ccertifficate")
147+
raise ValueError("unable to recognize input [cert] as a certificate")
148148

149149

150-
def to_PEM_cert(cer: str | bytes) -> str:
150+
def to_PEM_cert(cert: str | bytes) -> str:
151151
"""
152152
This function takes in a certificate with unknown representation
153153
(allegedly, PEM, DER or Base64 encoded DER) and applies some
@@ -157,22 +157,28 @@ def to_PEM_cert(cer: str | bytes) -> str:
157157
use it unless you do NOT hany prior way to know the actual representation
158158
format of a certificate
159159
"""
160-
cert_s = b""
160+
cert_b = b""
161161

162-
if isinstance(cer, str):
163-
if is_pem_format(cer):
164-
return cer
165-
cert_s = cer.encode()
162+
if isinstance(cert, str):
163+
if is_pem_format(cert):
164+
return cert
165+
if _BASE64_RE.fullmatch(cert):
166+
return B64DER_cert_to_DER_cert(cert)
167+
cert_b = cert.encode()
166168
else:
167-
cert_s = cer
169+
cert_b = cert
168170

169-
if cert_s.startswith(b"-----BEGIN CERTIFICATE-----"):
170-
return str(cert_s)
171+
if cert_b.startswith(b"-----BEGIN CERTIFICATE-----"):
172+
return cert_b.decode()
171173

172-
if _BASE64_RE.fullmatch(str(cert_s)):
173-
return B64DER_cert_to_PEM_cert(cert_s)
174-
else:
175-
return DER_cert_to_PEM_cert(cert_s)
174+
try:
175+
cert_s = cert_b.decode()
176+
if _BASE64_RE.fullmatch(cert_s):
177+
return B64DER_cert_to_PEM_cert(cert_s)
178+
except UnicodeError:
179+
return DER_cert_to_PEM_cert(cert_b)
180+
181+
raise ValueError("unable to recognize input [cert] as a certificate")
176182

177183
def pem_to_pems_list(cert: str) -> list[str]:
178184
"""

0 commit comments

Comments
 (0)