
                         R    S r SSKJr  SSKJr   " S S\5      r " S S\5      rS rg	)
z1OpenSSL Crypto-related routines for oauth2client.    )crypto)_helpersc                   4    \ rS rSrSrS rS r\S 5       rSr	g)OpenSSLVerifier   z$Verifies the signature on a message.c                     Xl         g)zTConstructor.

Args:
    pubkey: OpenSSL.crypto.PKey, The public key to verify with.
N_pubkey)selfpubkeys     .lib/third_party/oauth2client/_openssl_crypt.py__init__OpenSSLVerifier.__init__   s	         c                     [         R                  " USS9n[         R                  " USS9n [        R                  " U R                  X!S5        g! [        R
                   a     gf = f)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   OpenSSLVerifier.verify    sZ     $$Ww?&&y7C		MM$,,	HE|| 		s   "A A&%A&c                     [         R                  " U 5      n U(       a&  [        R                  " [        R                  U 5      nO%[        R
                  " [        R                  U 5      n[        U5      $ )aK  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_stringOpenSSLVerifier.from_string5   sR     $$W-,,V-@-@'JF++F,?,?IFv&&r   r	   N)
__name__
__module____qualname____firstlineno____doc__r   r   staticmethodr"   __static_attributes__ r   r   r   r      s#    .* ' 'r   r   c                   8    \ rS rSrSrS rS r\SS j5       rSr	g)	OpenSSLSignerL   z"Signs messages with a private key.c                     Xl         g)z\Constructor.

Args:
    pkey: OpenSSL.crypto.PKey (or equiv), The private key to sign with.
N_key)r   pkeys     r   r   OpenSSLSigner.__init__O   s	     	r   c                 p    [         R                  " USS9n[        R                  " U R                  US5      $ )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   signr1   )r   r   s     r   r5   OpenSSLSigner.signW   s-     $$Ww?{{499gx88r   c                 <   [         R                  " U 5      n [         R                  " U 5      nU(       a&  [        R                  " [        R
                  U5      nO9[         R                  " USS9n[        R                  " X5      R                  5       n[        U5      $ )zConstruct 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_keyr2   s       r   r"   OpenSSLSigner.from_stringc   su       %!005))&*=*=~ND))(WEH%%c4CCEDT""r   r0   N)s
   notasecret)
r$   r%   r&   r'   r(   r   r5   r)   r"   r*   r+   r   r   r-   r-   L   s#    ,
9 # #r   r-   c                     [         R                  " U5      n[        R                  " X5      n[        R                  " [        R
                  UR                  5       5      $ )zConvert 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   r9   dump_privatekeyr   r:   )private_key_bytesprivate_key_passwordpkcs12s      r   pkcs12_key_as_pemrD   {   sN     $--.BC 1HF!!&"5"5"("7"7"9; ;r   N)	r(   OpenSSLr   oauth2clientr   objectr   r-   rD   r+   r   r   <module>rH      s/    8  !4'f 4'n,#F ,#^;r   