
    b	                     Z    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
d Zy)	z!Helpers for parsing certificates.    )absolute_import)division)unicode_literalsN)
exceptions)filesc                 *   t        j                  |       }d|v r|j                  ddd      }|j                  ddd      }d|v r%t        j                  ddj                  |             	 |j                  dd      j                  dd      }t        j                  t        j                  |            }t        j                  |      }||k7  rt        d	      	 |S |S # t        $ r+}t        j                  dd
j                  | |            d}~ww xY w)a#  Read certificate_file and return the certificate in DER encoding.

  Args:
    certificate_file: A file handle to the certificate in PEM or DER format.

  Returns:
    The certificate in DER encoding.

  Raises:
    BadArgumentException: The provided certificate failed to parse as a PEM.
  s   -----BEGIN CERTIFICATE-----       s   -----END CERTIFICATE-----certificate_filez8Cannot place multiple certificates in the same file : {}      
zNon-base64 digit found.z:Recognized {} as a PEM file but failed during parsing : {}N)r   ReadBinaryFileContentsreplacer   BadArgumentExceptionformatbase64	b64decodesixensure_binary	b64encode
ValueError	Exception)r   datacertb64decodedencodedes         +lib/googlecloudsdk/command_lib/kms/certs.pyGetDerCertificater      s!    
	%	%&6	7$#t+ll93BGoo:CCG%0++

D
K
K ! !$s+33E3?g   !2!27!;<g  )g	G	233 
 NK  $++

F
M
M#$ $$s   *A/C 	D'&DD)__doc__
__future__r   r   r   r   googlecloudsdk.callioper   googlecloudsdk.core.utilr   r   r    r	   r   <module>r%      s$    ( &  '  . * 
%r	   