ó
ùR‹_c           @   sÿ   d  Z  d d l Z d d l Z d d l m Z d d l Z d d l Z d d l Z d d l m	 Z	 d Z
 d Z e j d e j ƒ Z e j d e j ƒ Z e j e ƒ Z e j d	 e j ƒ Z d
 „  Z d „  Z e d „ Z e d „ Z d d „ Z d „  Z d S(   s/   Helper functions for getting mTLS cert and key.iÿÿÿÿN(   t   path(   t
   exceptionss,   ~/.secureConnect/context_aware_metadata.jsont   cert_provider_commands:   -----BEGIN CERTIFICATE-----.+-----END CERTIFICATE-----?
?sH   -----BEGIN [A-Z ]*PRIVATE KEY-----.+-----END [A-Z ]*PRIVATE KEY-----?
?s6   -----BEGIN PASSPHRASE-----(.+)-----END PASSPHRASE-----c         C   s6   t  j |  ƒ }  t  j |  ƒ s2 t j d |  ƒ d S|  S(   sø   Checks for context aware metadata. If it exists, returns the absolute path;
    otherwise returns None.

    Args:
        metadata_path (str): context aware metadata path.

    Returns:
        str: absolute path if exists and None otherwise.
    s0   %s is not found, skip client SSL authentication.N(   R    t
   expandusert   existst   _LOGGERt   debugt   None(   t   metadata_path(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   _check_dca_metadata_path3   s
    
c         C   sa   y( t  |  ƒ  } t j | ƒ } Wd QXWn2 t k
 r\ } t j | ƒ } t j | | ƒ n X| S(   s  Loads context aware metadata from the given path.

    Args:
        metadata_path (str): context aware metadata path.

    Returns:
        Dict[str, str]: The metadata.

    Raises:
        google.auth.exceptions.ClientCertError: If failed to parse metadata as JSON.
    N(   t   opent   jsont   loadt
   ValueErrorR   t   ClientCertErrort   sixt
   raise_from(   R   t   ft   metadatat
   caught_exct   new_exc(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   _read_dca_metadata_fileD   s    c   
      C   sØ  y7 t  j |  d t  j d t  j ƒ} | j ƒ  \ } } Wn2 t k
 rk } t j | ƒ } t j | | ƒ n X| j	 d k r” t j d | j	 ƒ ‚ n  t
 j t | ƒ } t | ƒ d k rÊ t j d ƒ ‚ n  t
 j t | ƒ } t | ƒ d k r t j d ƒ ‚ n  t
 j t | ƒ }	 | r}t |	 ƒ d k r<t j d ƒ ‚ n  d	 | d k r^t j d
 ƒ ‚ n  | d | d |	 d j ƒ  f Sd	 | d k rŸt j d ƒ ‚ n  t |	 ƒ d k rÃt j d ƒ ‚ n  | d | d d f S(   s!  Run the provided command, and return client side mTLS cert, key and
    passphrase.

    Args:
        command (List[str]): cert provider command.
        expect_encrypted_key (bool): If encrypted private key is expected.

    Returns:
        Tuple[bytes, bytes, bytes]: client certificate bytes in PEM format, key
            bytes in PEM format and passphrase bytes.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when running
            the cert provider command or generating cert, key and passphrase.
    t   stdoutt   stderri    s5   Cert provider command returns non-zero status code %si   s,   Client SSL certificate is missing or invalids$   Client SSL key is missing or invalids    Passphrase is missing or invalidt	   ENCRYPTEDs!   Encrypted private key is expecteds%   Encrypted private key is not expecteds   Passphrase is not expectedN(   t
   subprocesst   Popent   PIPEt   communicatet   OSErrorR   R   R   R   t
   returncodet   ret   findallt   _CERT_REGEXt   lent
   _KEY_REGEXt   _PASSPHRASE_REGEXt   stripR   (
   t   commandt   expect_encrypted_keyt   processR   R   R   R   t
   cert_matcht	   key_matcht   passphrase_match(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   _run_cert_provider_commandZ   s8    c         C   s£   t  t ƒ } | r“ t | ƒ } t | k r< t j d ƒ ‚ n  | t } |  rh d | k rh | j d ƒ n  t | d |  ƒ\ } } } t | | | f St	 d d d f S(   s†  Returns the client side certificate, private key and passphrase.

    Args:
        generate_encrypted_key (bool): If set to True, encrypted private key
            and passphrase will be generated; otherwise, unencrypted private key
            will be generated and passphrase will be None.

    Returns:
        Tuple[bool, bytes, bytes, bytes]:
            A boolean indicating if cert, key and passphrase are obtained, the
            cert bytes and key bytes both in PEM format, and passphrase bytes.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert, key and passphrase.
    s"   Cert provider command is not founds   --with_passphraseR'   N(   R	   t   CONTEXT_AWARE_METADATA_PATHR   t   _CERT_PROVIDER_COMMANDR   R   t   appendR,   t   Truet   FalseR   (   t   generate_encrypted_keyR   t   metadata_jsonR&   t   certt   keyt
   passphrase(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   get_client_ssl_credentials   s    
c         C   sJ   |  r" |  ƒ  \ } } t  | | f St d t ƒ \ } } } } | | | f S(   s  Returns the client side certificate and private key. The function first
    tries to get certificate and key from client_cert_callback; if the callback
    is None or doesn't provide certificate and key, the function tries application
    default SSL credentials.

    Args:
        client_cert_callback (Optional[Callable[[], (bytes, bytes)]]): An
            optional callback which returns client certificate bytes and private
            key bytes both in PEM format.

    Returns:
        Tuple[bool, bytes, bytes]:
            A boolean indicating if cert and key are obtained, the cert bytes
            and key bytes both in PEM format.

    Raises:
        google.auth.exceptions.ClientCertError: if problems occurs when getting
            the cert and key.
    R2   (   R0   R7   R1   (   t   client_cert_callbackR4   R5   t   has_certt   _(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   get_client_cert_and_key·   s
    c         C   s>   d d l  m } | j | j |  d | ƒ} | j | j | ƒ S(   s#  A helper function to decrypt the private key with the given passphrase.
    google-auth library doesn't support passphrase protected private key for
    mutual TLS channel. This helper function can be used to decrypt the
    passphrase protected private key in order to estalish mutual TLS channel.

    For example, if you have a function which produces client cert, passphrase
    protected private key and passphrase, you can convert it to a client cert
    callback function accepted by google-auth::

        from google.auth.transport import _mtls_helper

        def your_client_cert_function():
            return cert, encrypted_key, passphrase

        # callback accepted by google-auth for mutual TLS channel.
        def client_cert_callback():
            cert, encrypted_key, passphrase = your_client_cert_function()
            decrypted_key = _mtls_helper.decrypt_private_key(encrypted_key,
                passphrase)
            return cert, decrypted_key

    Args:
        key (bytes): The private key bytes in PEM format.
        passphrase (bytes): The passphrase bytes.

    Returns:
        bytes: The decrypted private key in PEM format.

    Raises:
        ImportError: If pyOpenSSL is not installed.
        OpenSSL.crypto.Error: If there is any problem decrypting the private key.
    iÿÿÿÿ(   t   cryptoR6   (   t   OpenSSLR<   t   load_privatekeyt   FILETYPE_PEMt   dump_privatekey(   R5   R6   R<   t   pkey(    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   decrypt_private_keyÓ   s    !(   t   __doc__R   t   loggingt   osR    R   R   R   t   google.authR   R-   R.   t   compilet   DOTALLR!   R#   t	   getLoggert   __name__R   R$   R	   R   R1   R,   R7   R   R;   RB   (    (    (    s[   /var/www/syncserver/local/lib/python2.7/site-packages/google/auth/transport/_mtls_helper.pyt   <module>   s.   		6'