
                         p    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
  G d de      Z G d	 d
e      Zy)z2pyCrypto Crypto-related routines for oauth2client.    )SHA256)RSA)
PKCS1_v1_5)DerSequence)_helpersc                   ,    e Zd ZdZd Zd Zed        Zy)PyCryptoVerifierz$Verifies the signature on a message.c                     || _         y)zConstructor.

        Args:
            pubkey: OpenSSL.crypto.PKey (or equiv), The public key to verify
            with.
        N)_pubkey)selfpubkeys     /lib/third_party/oauth2client/_pycrypto_crypt.py__init__zPyCryptoVerifier.__init__   s         c                     t        j                  |d      }t        j                  | j                        j                  t        j                  |      |      S )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.

        Returns:
            True if message was signed by the private key associated with the
            public key that this object was constructed with.
        utf-8encoding)r   	_to_bytesr   newr   verifyr   )r   message	signatures      r   r   zPyCryptoVerifier.verify$   sD     $$Ww?~~dll+22JJw, 	,r   c                    |rt        j                  |       } | j                  dd      j                         }t        j                  dj                  |dd             }t               }|j                  |       t               }|j                  |d          t        j                  |d         }t        |      S 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.
            r      r      )r   r   replacesplit_urlsafe_b64decodejoinr   decoder   	importKeyr	   )key_pemis_x509_certpemLinescertDercertSeqtbsSeqr   s          r   from_stringzPyCryptoVerifier.from_string4   s     ((1GtS1779H11#((8Ab>2JKG!mGNN7# ]FMM'!*%]]6!9-F  '' ]]7+F''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)PyCryptoSignerz"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PyCryptoSigner.__init__Q   s     	r   c                     t        j                  |d      }t        j                  | j                        j                  t        j                  |            S )zSigns a message.

        Args:
            message: string, Message to be signed.

        Returns:
            string, The signature of the message for the given key.
        r   r   )r   r   r   r   r5   signr   )r   r   s     r   r8   zPyCryptoSigner.signY   s=     $$Ww?~~dii(--fjj.ABBr   c                     t        j                  t        j                  |             }|r t        j                  |      }t        |      S t        d      )a_  Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        zpNo key in PEM format was detected. This implementation can only use the PyCrypto library for keys in PEM format.)r   _parse_pem_keyr   r   r$   NotImplementedErrorr3   )keypasswordparsed_pem_keyr6   s       r   r+   zPyCryptoSigner.from_stringe   sQ     "001C1CC1HI==0D d##	 & r   N)
notasecret)r,   r-   r.   r/   r   r8   r0   r+   r1   r   r   r3   r3   N   s$    ,
C $ $r   r3   N)r/   Crypto.Hashr   Crypto.PublicKeyr   Crypto.Signaturer   Crypto.Util.asn1r   oauth2clientr   objectr	   r3   r1   r   r   <module>rF      s2    9    ' ( !3(v 3(l.$V .$r   