
                         R    d Z ddlmZ ddlmZ  G d de      Z G d de      Zd Zy	)
z1OpenSSL Crypto-related routines for oauth2client.    )crypto)_helpersc                   ,    e Zd ZdZd Zd Zed        Zy)OpenSSLVerifierz$Verifies the signature on a message.c                     || _         y)zlConstructor.

        Args:
            pubkey: OpenSSL.crypto.PKey, The public key to verify with.
        N)_pubkey)selfpubkeys     .lib/third_party/oauth2client/_openssl_crypt.py__init__zOpenSSLVerifier.__init__   s         c                     t        j                  |d      }t        j                  |d      }	 t        j                  | j                  ||d       y# t        j
                  $ r Y yw xY w)a  Verifies a message against a signature.

        Args:
        message: string or bytes, The message to verify. If string, will be
                 encoded to bytes as utf-8.
        signature: string or bytes, The signature on the message. If string,
                   will be encoded to bytes as utf-8.

        Returns:
            True if message was signed by the private key associated with the
            public key that this object was constructed with.
        utf-8encodingsha256TF)r   	_to_bytesr   verifyr   Error)r	   message	signatures      r   r   zOpenSSLVerifier.verify    s\     $$Ww?&&y7C		MM$,,	7HE|| 		s   "A A)(A)c                     t        j                  |       } |r/t        j                  t        j                  |       }t        |      S t        j
                  t        j                  |       }t        |      S )a  Construct a Verified instance from a string.

        Args:
            key_pem: string, public key in PEM format.
            is_x509_cert: bool, True if key_pem is an X509 cert, otherwise it
                          is expected to be an RSA key in PEM format.

        Returns:
            Verifier instance.

        Raises:
            OpenSSL.crypto.Error: if the key_pem can't be parsed.
        )r   r   r   load_certificateFILETYPE_PEMload_privatekeyr   )key_pemis_x509_certr
   s      r   from_stringzOpenSSLVerifier.from_string5   sa     $$W-,,V-@-@'JF v&& ++F,?,?IFv&&r   N)__name__
__module____qualname____doc__r   r   staticmethodr    r   r   r   r      s#    .* ' 'r   r   c                   .    e Zd ZdZd Zd Zedd       Zy)OpenSSLSignerz"Signs messages with a private key.c                     || _         y)ztConstructor.

        Args:
            pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
        N)_key)r	   pkeys     r   r   zOpenSSLSigner.__init__O   s     	r   c                 r    t        j                  |d      }t        j                  | j                  |d      S )zSigns a message.

        Args:
            message: bytes, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        r   r   r   )r   r   r   signr(   )r	   r   s     r   r+   zOpenSSLSigner.signW   s-     $$Ww?{{499gx88r   c                 D   t        j                  |       } t        j                  |       }|r/t        j                  t        j
                  |      }t        |      S t        j                  |d      }t        j                  | |      j                         }t        |      S )a>  Construct a Signer instance from a string.

        Args:
            key: string, private key in PKCS12 or PEM format.
            password: string, password for the private key file.

        Returns:
            Signer instance.

        Raises:
            OpenSSL.crypto.Error if the key can't be parsed.
        r   r   )	r   r   _parse_pem_keyr   r   r   load_pkcs12get_privatekeyr&   )keypasswordparsed_pem_keyr)   s       r   r   zOpenSSLSigner.from_stringc   s       %!005))&*=*=~ND T""  ))(WEH%%c84CCEDT""r   N)s
   notasecret)r   r    r!   r"   r   r+   r#   r   r$   r   r   r&   r&   L   s#    ,
9 # #r   r&   c                     t        j                  |      }t        j                  | |      }t        j                  t        j
                  |j                               S )a  Convert the contents of a PKCS#12 key to PEM using pyOpenSSL.

    Args:
        private_key_bytes: Bytes. PKCS#12 key in DER format.
        private_key_password: String. Password for PKCS#12 key.

    Returns:
        String. PEM contents of ``private_key_bytes``.
    )r   r   r   r.   dump_privatekeyr   r/   )private_key_bytesprivate_key_passwordpkcs12s      r   pkcs12_key_as_pemr8   {   sQ     $--.BC 13GHF!!&"5"5"("7"7"9; ;r   N)	r"   OpenSSLr   oauth2clientr   objectr   r&   r8   r$   r   r   <module>r<      s/    8  !4'f 4'n,#F ,#^;r   