def extract_pem(pem, private_pem=False):
if private_pem:<EOL><INDENT>pem_header = '<STR_LIT>'<EOL>pem_footer = '<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>pem_header = '<STR_LIT>'<EOL>pem_footer = '<STR_LIT>'<EOL><DEDENT>header_start = <NUM_LIT:0><EOL>footer_start = <NUM_LIT:0><EOL>try:<EOL><INDENT>header_start = pem.index(pem_header)<EOL><DEDENT>except ValueError:<EOL><INDENT>if not private_pem:<EOL><INDENT>raise securesystemslib.exceptions.FormatError('<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem_header) + '<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem))<EOL><DEDENT>else:<EOL><INDENT>raise securesystemslib.exceptions.FormatError('<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem_header) + '<STR_LIT>')<EOL><DEDENT><DEDENT>try:<EOL><INDENT>footer_start = pem.index(pem_footer, header_start + len(pem_header))<EOL><DEDENT>except ValueError:<EOL><INDENT>if not private_pem:<EOL><INDENT>raise securesystemslib.exceptions.FormatError('<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem_footer) + '<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem))<EOL><DEDENT>else:<EOL><INDENT>raise securesystemslib.exceptions.FormatError('<STR_LIT>'<EOL>'<STR_LIT>' + repr(pem_footer) + '<STR_LIT>')<EOL><DEDENT><DEDENT>pem = pem[header_start:footer_start + len(pem_footer)]<EOL>return pem<EOL>
<Purpose>
  Extract only the portion of the pem that includes the header and footer,
  with any leading and trailing characters removed.  The string returned has
  the following form:

  '-----BEGIN PUBLIC KEY----- ... -----END PUBLIC KEY-----'

  or

  '-----BEGIN RSA PRIVATE KEY----- ... -----END RSA PRIVATE KEY-----'

  Note: This function assumes "pem" is a valid pem in the following format:
  pem header + key material + key footer.  Crypto libraries (e.g., pyca's
  cryptography) that parse the pem returned by this function are expected to
  fully validate the pem.

<Arguments>
  pem:
    A string in PEM format.

  private_pem:
    Boolean that indicates whether 'pem' is a private PEM.  Private PEMs
    are not shown in exception messages.

<Exceptions>
  securesystemslib.exceptions.FormatError, if 'pem' is improperly formatted.

<Side Effects>
  Only the public and private portion of the PEM is extracted.  Leading or
  trailing whitespace is not included in the returned PEM string.

<Returns>
  A PEM string (excluding leading and trailing newline characters).
  That is: pem header + key material + pem footer.