
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlm	Z	 ddl
mZ ddlZdZ G d	 d
e	j                        Zd Z G d de      Zy)z1Utilities for encryption functions using openssl.    )absolute_import)division)unicode_literalsN)
exceptions)logi   c                       e Zd ZdZy)OpenSSLExceptionz.Exception for problems with OpenSSL functions.N)__name__
__module____qualname____doc__     >lib/googlecloudsdk/api_lib/compute/openssl_encryption_utils.pyr	   r	   "   s    6r   r	   c                     | j                         } | j                  d      }|d   j                  d      r|d   j                  d      st        dj	                  |             dj                  |dd       S )z9Returns key with header, footer and all newlines removed.   
r   s   -----z;The following key does not appear to be in PEM format: 
{0}r      )stripsplit
startswithr	   formatjoin)key	key_liness     r   StripKeyr   &   so    		#ii)
A,
!
!(
+r]%%h/
F	  
)Ab/	""r   c                   @    e Zd ZdZd Zd	dZefdZd Zd
dZ	efdZ
y)OpensslCryptz,Base Class for OpenSSL encryption functions.c                     || _         y N)openssl_executable)selfr!   s     r   __init__zOpensslCrypt.__init__5   s
    0Dr   Nc                 6   | j                   g}|j                  |       	 t        j                  |t        j                  t        j                  t        j                        }|j                  |      \  }}t        j                  dj                  dj                  |      |             |j                  r)t        dj                  |d   |j                  |            |S # t        $ r-}t        dj                  |d   |j                              d}~ww xY w)zARun an openssl command with optional input and return the output.)stdinstdoutstderrz-Ran command "{0}" with standard error of:
{1} z[{0}] exited with [{1}].r   Nz)[{0}] exited with return code [{1}]:
{2}.)r!   extend
subprocessPopenPIPEcommunicater   debugr   r   OSErrorr	   strerror
returncode)r"   cmd_args	cmd_inputcommandpoutputr'   es           r   
RunOpenSSLzOpensslCrypt.RunOpenSSL8   s     &&'GNN8E


7*//",//*//Ka}}Y/nff	ii@)624 	|| I$fWQZvFH HM  E 
$
+
+GAJ


CE E	Es   BC" "	D+(DDc                 N    | j                  dt        j                  |      g      S )z&Returns an RSA key pair (private key).genrsa)r8   six	text_type)r"   
key_lengths     r   
GetKeyPairzOpensslCrypt.GetKeyPairR   s    ??HcmmJ&?@AAr   c                 ,    | j                  ddg|      S )z%Returns a public key from a key pair.rsaz-puboutr3   )r8   )r"   r   s     r   GetPublicKeyzOpensslCrypt.GetPublicKeyV   s    ??E9-?==r   c                    ~t        j                  |      }t        j                         5 }|j	                  |       |j                          dddd|j                  g}| j                  ||      }ddd       |S # 1 sw Y   S xY w)a  Returns a decrypted message using the provided key.

    Args:
      key: An openssl key pair (private key).
      enc_message: a base64 encoded encrypted message.
      destroy_key: Unused for OpenSSL.
    Returns:
      Decrypted version of enc_message
    rsautlz-decryptz-oaepz-inkeyrA   N)base64	b64decodetempfileNamedTemporaryFilewriteflushnamer8   )r"   r   enc_messagedestroy_keyencrypted_message_datatfopenssl_argsmessages           r   DecryptMessagezOpensslCrypt.DecryptMessageZ   s|     	#--k: 
	$	$	&"hhsmhhj
GXrwwGl8NOg	 
'
 N 
'
 Ns   AA;;Bc                     t        |      }t        j                  |      }|dd }|dz  }|dz  r|dz  }d|z
  }||d }t        j                  |      }	t        j                  |      }
|	|
fS )zBReturns a base64 encoded modulus and exponent from the public key.N   r   )r   rE   rF   	b64encode)r"   
public_keyr=   r   decoded_keyexponent	key_bytesmodulus_startmodulusb64_modb64_exps              r   GetModulusExponentFromPublicKeyz,OpensslCrypt.GetModulusExponentFromPublicKeyp   s     :
C""3'K 23H aIA~1niNM-+Gw'Gx(GWr   r    )F)r
   r   r   r   r#   r8   DEFAULT_KEY_LENGTHr>   rB   rR   r`   r   r   r   r   r   2   s/    414 #5 B>. 2Dr   r   )r   
__future__r   r   r   rE   r*   rG   googlecloudsdk.api_lib.computer   googlecloudsdk.corer   r;   ra   Errorr	   r   objectr   r   r   r   <module>rg      sR    8 &  '    5 # 
  7z'' 7	#[6 [r   