From b8d6524d43dd0667184aebc79fb77a9b8a46939a Mon Sep 17 00:00:00 2001 From: Martin Basti Date: Thu, 19 Jan 2017 15:28:15 +0100 Subject: py3: strip_header: support both bytes and unicode Various method passed various bytes or unicode as parameter https://fedorahosted.org/freeipa/ticket/4985 Reviewed-By: Christian Heimes --- ipalib/x509.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'ipalib') diff --git a/ipalib/x509.py b/ipalib/x509.py index 13327c175..d883ac4e1 100644 --- a/ipalib/x509.py +++ b/ipalib/x509.py @@ -85,12 +85,16 @@ def strip_header(pem): """ Remove the header and footer from a certificate. """ - s = pem.find("-----BEGIN CERTIFICATE-----") - if s >= 0: - e = pem.find("-----END CERTIFICATE-----") - pem = pem[s+27:e] - - return pem + regexp = ( + u"^-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----" + ) + if isinstance(pem, bytes): + regexp = regexp.encode('ascii') + s = re.search(regexp, pem, re.MULTILINE | re.DOTALL) + if s is not None: + return s.group(1) + else: + return pem def load_certificate(data, datatype=PEM): -- cgit