
    9iV                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SSKr\R&                  " \5      r " S	 S
\	5      rg)z.
This module provides a client class for KMS.
    N)BceBaseClient)required)bce_v1_signer)bce_http_client)handler)http_methodsc            	          \ rS rSrSrS%S jrS%S jr  S&S jr\" \	\
4\	\
4\	\
4\S9 S'S j5       r\" \S	9S(S
 j5       r\" \
\	4\
\	4S9S%S j5       r\" \
\	4\
\	4S9S%S j5       r\" \
\	4\
\	4\
\	4S9S)S j5       r\" \
\	4\
\	4\
\	4\
\	4S9S)S j5       r\" \
\	4\
\	4S9S*S j5       r\" \
\	4\S9S%S j5       r\" \
\	4S9S%S j5       r\" \
\	4S9S%S j5       r\" \
\	4\S9S%S j5       r\" \
\	4S9S%S j5       r\" \
\	4S9S%S j5       r\" \
\	4S9  S+S j5       r\" \
\	4\
\	4\
\	4\
\	4S9 S,S  j5       r\" \
\	4\
\	4\
\	4\
\	4\
\	4\S!9 S,S" j5       r\" \
\	4\
\	4\
\	4\
\	4\
\	4\S!9 S,S# j5       rS$rg)-	KmsClient#   z
sdk client
Nc                 0    [         R                  " X5        g N)r   __init__)selfconfigs     `/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/baidubce/services/kms/kms_client.pyr   KmsClient.__init__'   s    t,    c                     Uc  U R                   $ [        R                  " U R                   5      nUR                  U5        U$ r   )r   copymerge_non_none_values)r   r   
new_configs      r   _merge_configKmsClient._merge_config*   s7    >;;4;;/J,,V4r   c           
          U R                  U5      nUc  [        R                  nUc  SSS.n[        R                  " U[
        R                  [        R                  U/XX4U5      $ )Ns   */*s   application/json;charset=utf-8)s   Accepts   Content-Type)r   r   
parse_jsonr   send_requestr   signparse_error)r   http_methodpathbodyheadersparamsr   body_parsers           r   _send_requestKmsClient._send_request2   sk     ##F+!,,K?"((IKG++FM4F4F-4-@-@+,N,7t,24 	4r   )protectedBykeySpecoriginrotateCyclec                     Sn0 n	SU	S'   0 n
U(       a  XS'   X*S'   X:S'   XJS'   XZS'   XjS	'   U R                  [        R                  U[        R                  " U
5      XS
9$ )a  
create a master key with the specified options.
:type description: string
:param description: a description about the master key

:type protectedBy: constants.ProtectedBy
:param protectedBy: the protect level about the master key, you can choose HSM or SOFTWARE

:type keySpec: constants.KeySpec
:param keySpec:  key specification about the master key. now you can choose the BAIDU_AES_256, 
AES_128, AES_256, RSA_1024, RSA_2048, RSA_4096

:type keyUsage: string
:param keyUsage:  default "ENCRYPT_DECRYPT"

:type origin: constants.Origin
:param origin:  origin of the master key. you can choose BAIDU_KMS or EXTERNAL

:type rotateCycle: int
:param rotateCycle: rotateCycle of the master key.
   /s	   CreateKeyactiondescriptionr'   r(   r)   keyUsager*   r#   r   r%   r   POSTjsondumps)r   r.   r'   r(   r)   r/   r*   r   r    r#   r!   s              r   create_masterKeyKmsClient.create_masterKey@   s    0 'x"-)]!YX#Z)]!!,"3"3T4::d;K)/ " @ 	@r   )limitc                     Sn0 nSUS'   0 nXS'   X&S'   U R                  [        R                  U[        R                  " U5      XSS9$ )z
list your masterkey 
:type limit: int
:param limit: the number of masterKey you want list

:type marker: string
:param marker: the marker keyid , kms will search from the marker, default ""
r,   s   ListKeysr-   r7   markerr0   r1   )r   r7   r9   r   r    r#   r!   s          r   list_masterKeyKmsClient.list_masterKeyf   s]     &xWX!!,"3"3T4::d;K)/ " @ 	@r   )keyId	plaintextc                     Sn0 nSUS'   0 nXS'   X&S'    [         R                  " U5        U R                  [        R
                  U[        R                  " U5      XSS9$ ! [         a    [        S5      ef = f)z
encrypt the plaintext
:type keyId: string
:param keyId: indicate kms will use which masterkey to encrypt

:type plaintext: string
:param plaintext: the plaintext need encrypted by kms
r,   s   Encryptr-   r<   r=   please input base64 stringr0   base64	b64decode	TypeErrorr%   r   r2   r3   r4   )r   r<   r=   r   r    r#   r!   s          r   encryptKmsClient.encrypty   s     %xW%[	:Y' !!,"3"3T4::d;K)/ " @ 	@  	:899	:   A A5)r<   
ciphertextc                     Sn0 nSUS'   0 nXS'   X&S'    [         R                  " U5        U R                  [        R
                  U[        R                  " U5      XSS9$ ! [         a    [        S5      ef = f)z
decrypt the ciphertext
:type keyId: string
:param keyId: indicate kms will use which masterkey to decrypt

:type ciphertext: string
:param ciphertext:  the ciphertext need decrypted by kms
r,   s   Decryptr-   r<   rG   r?   r0   r@   )r   r<   rG   r   r    r#   r!   s          r   decryptKmsClient.decrypt   s     %xW'\	:Z( !!,"3"3T4::d;K)/ " @ 	@  	:899	:rF   )r<   	algorithmmessagec                    US;  a  [        S5      eUS:X  a  [        U5      S:  a  [        S5      eO8US:X  a2   [        R                  " U5      n[        U5      S:w  a  [        S5      e S
n0 n	SU	S'   UUUUS.n
U(       a  XJS'   U R                  [        R                  U[        R                  " U
5      XS9$ ! [        R
                  [        4 a    [        S	5      ef = f)a  
Sign the message using asymmetric key
:type keyId: string
:param keyId: indicate which masterkey to use for signing

:type algorithm: string
:param algorithm: signing algorithm (RSA_PKCS1_SHA_256/SM2DSA)

:type message: string
:param message: message to sign (Base64 encoded)

:type keyVersion: string
:param keyVersion: key version (optional)

:type messageType: string
:param messageType: message type (RAW/DIGEST, default RAW)
:raises: ValueError if message length or format is invalid
RAWDIGEST,messageType must be either 'RAW' or 'DIGEST'rO      7Base64 encoded RAW message length must be <= 4096 bytesrP       4Digest length must be 32 bytes after base64 decodingmessage must be base64 encodedr,   s   Signr-   )r<   rK   rL   messageType
keyVersionr0   
ValueErrorlenrA   rB   binasciiErrorrC   r%   r   r2   r3   r4   )r   r<   rK   rL   rX   rW   r   decodedr    r#   r!   s              r   r   KmsClient.sign   s   ( //KLL %Gt# !Z[[ $H$C **73w<2%$%[\\ & "x"&	
 !+!!,"3"3T4::d;K)/ " @ 	@ NNI. C !ABBCs   0B: :&C )r<   rK   	signaturerL   c                    US;  a  [        S5      eUS:X  a  [        U5      S:  a  [        S5      eO8US:X  a2   [        R                  " U5      n[        U5      S:w  a  [        S5      e  [        R                  " U5        Sn	0 n
SU
S'   UUUUUS.nU(       a  X[S'    [        R                  " U5        [        R                  " U5        U R                  [        R                  U	[        R                  " U5      XS9$ ! [        R
                  [        4 a    [        S	5      ef = f! [        R
                  [        4 a    [        S
5      ef = f! [        R
                  [        4 a    [        S5      ef = f)a	  
Verify the signature using asymmetric key
:type keyId: string
:param keyId: indicate which masterkey to use for verification

:type algorithm: string
:param algorithm: signing algorithm (RSA_PKCS1_SHA_256/SM2DSA)

:type signature: string
:param signature: signature to verify (Base64 encoded)

:type message: string
:param message: original message (Base64 encoded)

:type keyVersion: string
:param keyVersion: key version (optional)

:type messageType: string
:param messageType: message type (RAW/DIGEST, default RAW)
rN   rQ   rO   rR   rS   rP   rT   rU   rV   z signature must be base64 encodedr,   s   Verifyr-   )r<   rK   r`   rL   rW   rX   z,message and signature must be base64 encodedr0   rY   )r   r<   rK   r`   rL   rX   rW   r   r^   r    r#   r!   s               r   verifyKmsClient.verify   s   . //KLL %7|d" !Z[[ #H$C **73w<2%$%[\\ &	AY' $x""&
 !+	MW%Y' !!,"3"3T4::d;K)/ " @ 	@9 NNI. C !ABBC 	* 	A?@@	A& 	* 	MKLL	Ms#   0C? ,D( ,E ?&D%(&E&E7)r<   r(   c                     Sn0 nSUS'   0 nXS'   US:w  a  US:w  a  [        S5      eX'S'   X7S	'   U R                  [        R                  U[        R
                  " U5      XdS
9$ )a  
generate a data key by master key
:type keyId: string
:param keyId: indicate kms will use which masterkey to generate data key

:type keySpec: string
:param keySpec: AES_128 or AES_256

:type numberOfBytes: int
:param numberOfBytes: The length of data key
r,   s   GenerateDataKeyr-   r<   AES_128AES_256z only support AES_128 and AES_256r(   numberOfBytesr0   rZ   r%   r   r2   r3   r4   )r   r<   r(   rg   r   r    r#   r!   s           r   generate_dataKeyKmsClient.generate_dataKey  s     -xWiGy$8?@@!Y -_!!,"3"3T4::d;K)/ " @ 	@r   )r<   r*   c                     Sn0 nSUS'   0 nXS'   X&S'   U R                  [        R                  U[        R                  " U5      XSS9$ )z
update your master key rptation
:type keyId: string
:type rotateCycle: int
:param keyId: the keyId of masterkey will be enable
:param rotateCycle: the rotatecycle of masterkey
r,   s   EnableRotationr-   r<   r*   r0   r1   )r   r<   r*   r   r    r#   r!   s          r   updaterotation_masterKey"KmsClient.updaterotation_masterKey3  s^     ,xW)]!!,"3"3T4::d;K)/ " @ 	@r   )r<   c                     Sn0 nSUS'   0 nXS'   U R                  [        R                  U[        R                  " U5      XBS9$ )z`
enable your master key
:type keyId: string
:param keyId: the keyId of masterkey will be enable
r,   s	   EnableKeyr-   r<   r0   r1   r   r<   r   r    r#   r!   s         r   enable_masterKeyKmsClient.enable_masterKeyE  sV     'xW!!,"3"3T4::d;K)/ " @ 	@r   c                     Sn0 nSUS'   0 nXS'   U R                  [        R                  U[        R                  " U5      XBS9$ )za
disable your master key
:type keyId: string
:param keyId: the keyId of masterkey will be diable
r,   s
   DisableKeyr-   r<   r0   r1   ro   s         r   disable_masterKeyKmsClient.disable_masterKeyT  sV     (xW!!,"3"3T4::d;K)/ " @ 	@r   )r<   pendingWindowInDaysc                     Sn0 nSUS'   0 nXS'   US:  d  US:  a  [        S5      eX&S'   U R                  [        R                  U[        R
                  " U5      XSS	9$ )
z
schedule delete master key
:type keyId: string
:param keyId: the keyId of masterkey will be deleted

:type pendingWindowInDays: int
:pram pendingWindowInDays: kms will wait pendingWindowInDays day then delete the key
r,   s   ScheduleKeyDeletionr-   r<         z-please input pendingWindowInDays >=7 and <=30ru   r0   rh   )r   r<   ru   r   r    r#   r!   s          r   scheduleDelete_masterKey"KmsClient.scheduleDelete_masterKeyc  s{     1xW#':Q'>LMM&9"#!!,"3"3T4::d;K)/ " @ 	@r   c                     Sn0 nSUS'   0 nXS'   U R                  [        R                  U[        R                  " U5      XBS9$ )zf
cancel delete master key
:type keyId: string
:param keyId: the keyId of masterkey will cancel delete
r,   s   CancelKeyDeletionr-   r<   r0   r1   ro   s         r   cancelDelete_masterKey KmsClient.cancelDelete_masterKeyx  sV     /xW!!,"3"3T4::d;K)/ " @ 	@r   c                     Sn0 nSUS'   0 nXS'   U R                  [        R                  U[        R                  " U5      XBS9$ )zS
descript the master key
:type keyId: string
:param keyId: the keyId of masterkey 
r,   s   DescribeKeyr-   r<   r0   r1   ro   s         r   describe_masterKeyKmsClient.describe_masterKey  sV     )xW!!,"3"3T4::d;K)/ " @ 	@r   c                    Sn0 nSUS'   0 nXS'   US:w  a  [        S5      eX8S'   US:w  a  [        S	5      eXHS
'   US:w  a  US:w  a  US:w  a  [        S5      eX(S'   U R                  [        R                  U[
        R                  " U5      XuS9$ )a  
get parameters for import
:type keyId: string
:param keyId: the keyId of masterkey

:type wrappingAlgorithm: string
:param wrappingAlgorithm: the algorithm for user encrypt local key

:type wrappingKeySpec:string
:param wrappingKeySpec: the pubkey spec for user encrypt local key
r,   s   GetParametersForImportr-   r<   RSAES_PKCS1_V1_5zonly support RSAES_PKCS1_V1_5wrappingAlgorithmRSA_2048zonly support RSA_2048wrappingKeySpecRAW_HEXBASE64PEMz%only support RAW_HEX or BASE64 or PEMpublicKeyEncodingr0   )rC   rZ   r%   r   r2   r3   r4   )	r   r<   r   r   r   r   r    r#   r!   s	            r   get_parameters_for_import#KmsClient.get_parameters_for_import  s     4xW 22;<<$5 !j(344"1	).?8.KPaejPjDEE$5 !!!,"3"3T4::d;K)/ " @ 	@r   )r<   importTokenencryptedKeyr(   c                     Sn0 nSUS'   0 n	XS'   X)S'   X9S'   XIS'   XYS'   U R                  [        R                  U[        R                  " U	5      XS	9$ )
ao  
import symmetric key
:type keyId: string
:param keyId: the keyId of masterkey

:type importToken: string
:param importToken: token from import parameter

:type encryptedKey: string
:param encryptedKey: the symmetric key encrypted by pubkey

:type keySpec: string
:param keySpec: the import key spec

:type keyUsage: string
:param keyUsage: default "ENCRYPT_DECRYPT"
r,   s	   ImportKeyr-   r<   r   r   r(   r/   r0   r1   )
r   r<   r   r   r(   r/   r   r    r#   r!   s
             r   import_symmetricMasterKey#KmsClient.import_symmetricMasterKey  su    ( 'xW)]+^!Y#Z!!,"3"3T4::d;K)/ " @ 	@r   )r<   r   asymmetricKeySpecasymmetricKeyUsageencryptedKeyEncryptionKeyasymmetricKeyc                 :   Sn0 n	SU	S'   0 n
XS'   X*S'   X:S'   XZS'   XJS'   0 U
S	'   US
   c  [        S5      eUS
   U
S	   S
'   US   c  [        S5      eUS   U
S	   S'   US   c  [        S5      eUS   U
S	   S'   US   c  [        S5      eUS   U
S	   S'   US   c  [        S5      eUS   U
S	   S'   US   c  [        S5      eUS   U
S	   S'   US   c  [        S5      eUS   U
S	   S'   U R                  [        R                  U[        R
                  " U
5      XS9$ )a  
import asymmetric key
:type keyId: string
:param keyId: the keyId of masterkey

:type importToken: string
:param importToken: token from import parameter

:type asymmetricKeySpec: string
:param asymmetricKeySpec: the import key spec

:type encryptedKeyEncryptionKey: string
:param encryptedKeyEncryptionKey: EncryptionKey

:type asymmetricKey: **args
:param asymmetricKey: include publicKeyDer encryptedD encryptedP encryptedQ encryptedDp encryptedDq encryptedQinv
r,      ImportAsymmetricKeyr-   r<   r   r   r   r   encryptedRsaKeypublicKeyDer%arg "publicKeyDer" should not be None
encryptedDz#arg "encryptedD" should not be None
encryptedPz#arg "encryptedP" should not be None
encryptedQz#arg "encryptedQ" should not be NoneencryptedDpz$arg "encryptedDp" should not be NoneencryptedDqz$arg "encryptedDq" should not be NoneencryptedQinvz&arg "encryptedQinv" should not be Noner0   rh   r   r<   r   r   r   r   r   kwargsr    r#   r!   s              r   import_asymmetricMasterKey$KmsClient.import_asymmetricMasterKey  s   2 1xW)]$5 !%7!",E()"$.!)DEE282H/,'BCC06|0D-,'BCC06|0D-,'BCC06|0D-- (CDD171F.- (CDD171F./"*EFF39/3J0!!,"3"3T4::d;K)/ " @ 	@r   c                 "   Sn0 n	SU	S'   0 n
XS'   X*S'   X:S'   XZS'   XJS'   0 U
S	'   US
   c  [        S5      eUS
   U
S	   S
'   US   c  [        S5      eUS   U
S	   S'   U R                  [        R                  U[        R
                  " U
5      XS9$ )a  
import asymmetric key
:type keyId: string
:param keyId: the keyId of masterkey

:type importToken: string
:param importToken: token from import parameter

:type asymmetricKeySpec: string
:param asymmetricKeySpec: the import key spec

:type encryptedKeyEncryptionKey: string
:param encryptedKeyEncryptionKey: EncryptionKey

:type asymmetricKey: **args
:param asymmetricKey: include publicKeyDer encryptedPrivateKey
r,   r   r-   r<   r   r   r   r   encryptedSm2Keyr   r   encryptedPrivateKeyz,arg "encryptedPrivateKey" should not be Noner0   rh   r   s              r   import_asymmetricSM2MasterKey'KmsClient.import_asymmetricSM2MasterKey  s    2 1xW)]$5 !%7!",E()"$.!)DEE282H/'(0KLL9?@U9V 56!!,"3"3T4::d;K)/ " @ 	@r    r   )NNNNN)ENCRYPT_DECRYPTr   N) N)NrO   N)N)r   r   N)r   N)__name__
__module____qualname____firstlineno____doc__r   r   r%   r   bytesstrintr5   r:   rD   rI   r   rb   ri   rl   rp   rs   ry   r|   r   r   r   objectr   r   __static_attributes__r   r   r   r
   r
   #   s   - 59/34 5#,eS\`ceRV#@ f#@J C@ @$ S%LS%L9@ :@, S%Lc5\:@ ;@, S%LS%L3,O.@ P.@` S%LS%LS%L[^`eZfg?@ h?@B S%L3,7@ 8@0 S%Ls4@ 5@" S%L!@ "@ S%L!@ "@ S%Lc:@ ;@( S%L!@ "@ S%L!@ "@ S%L!TfCG@ "@< S%LsEl#u`cej_klCG@ m@> S%Le"El #U|'*El " NR3@"3@j S%Le"El #U|'*El " NR$@"$@r   r
   )r   r   r3   loggingrandomstringuuidr\   baidubce.bce_base_clientr   baidubce.utilsr   baidubce.authr   baidubce.httpr   r   r   rA   	getLoggerr   _loggerr
   r   r   r   <module>r      sV           2 # ' ) ! & 


H
%W@ W@r   