def read_cert_from_file(cert_file, cert_type):
 if (not cert_file):
  return ''
 if (cert_type == 'pem'):
  line = open(cert_file).read().replace('\r\n', '\n').split('\n')
  if (line[0] == '-----BEGIN CERTIFICATE-----'):
   line = line[1:]
  elif (line[0] == '-----BEGIN PUBLIC KEY-----'):
   line = line[1:]
  else:
   raise CertificateError('Strange beginning of PEM file')
  while (line[(-1)] == ''):
   line = line[:(-1)]
  if (line[(-1)] == '-----END CERTIFICATE-----'):
   line = line[:(-1)]
  elif (line[(-1)] == '-----END PUBLIC KEY-----'):
   line = line[:(-1)]
  else:
   raise CertificateError('Strange end of PEM file')
  return ''.join(line)
 if (cert_type in ['der', 'cer', 'crt']):
  data = read_file(cert_file)
  return base64.b64encode(str(data))

def read_cert_from_file ( cert_file , cert_type ) : if ( not cert_file ) : return '' if ( cert_type == 'pem' ) : line = open ( cert_file ) . read ( ) . replace ( '\r\n' , '\n' ) . split ( '\n' ) if ( line [ 0 ] == '-----BEGIN CERTIFICATE-----' ) : line = line [ 1 : ] elif ( line [ 0 ] == '-----BEGIN PUBLIC KEY-----' ) : line = line [ 1 : ] else : raise CertificateError ( 'Strange beginning of PEM file' ) while ( line [ ( - 1 ) ] == '' ) : line = line [ : ( - 1 ) ] if ( line [ ( - 1 ) ] == '-----END CERTIFICATE-----' ) : line = line [ : ( - 1 ) ] elif ( line [ ( - 1 ) ] == '-----END PUBLIC KEY-----' ) : line = line [ : ( - 1 ) ] else : raise CertificateError ( 'Strange end of PEM file' ) return '' . join ( line ) if ( cert_type in [ 'der' , 'cer' , 'crt' ] ) : data = read_file ( cert_file ) return base64 . b64encode ( str ( data ) )
reads a certificate from a file .