2020import requests
2121from pgpy import PGPMessage
2222from pgpy .constants import HashAlgorithm , SymmetricKeyAlgorithm , CompressionAlgorithm , KeyFlags
23-
23+ from pgpy . packet . subpackets . signature import FlagList
2424
2525def _encrypt_file_part (file , server_secret , client_secret , path = True ):
2626 """
@@ -104,6 +104,48 @@ def _encrypt_message(message_to_encrypt, server_secret, client_secret):
104104 hash = HashAlgorithm .SHA256 )
105105 return base64 .b64encode (bytes (cipher_message )).decode ('utf-8' )
106106
107+ def _inject_encryption_flags (user , key_flags = True , hash = True , symmetric = True , compression = True ):
108+ if not key_flags :
109+ user .selfsig ._signature .subpackets .addnew ('KeyFlags' , hashed = True ,
110+ flags = {KeyFlags .EncryptCommunications ,
111+ KeyFlags .EncryptStorage })
112+ user .selfsig ._signature .subpackets ['h_KeyFlags' ] = user .selfsig ._signature .subpackets ['KeyFlags' ][0 ]
113+ if not hash :
114+ user .selfsig ._signature .subpackets .addnew ('PreferredHashAlgorithms' , hashed = True , flags = [HashAlgorithm .SHA256 ])
115+ if not symmetric :
116+ user .selfsig ._signature .subpackets .addnew ('PreferredSymmetricAlgorithms' , hashed = True ,
117+ flags = [SymmetricKeyAlgorithm .AES256 ])
118+ if not compression :
119+ user .selfsig ._signature .subpackets .addnew ('PreferredCompressionAlgorithms' , hashed = True ,
120+ flags = [CompressionAlgorithm .Uncompressed ])
121+
122+ def _enforce_encryption_flags (user ):
123+ # https://github.com/SecurityInnovation/PGPy/issues/257
124+ # PGPY requires KeyFlags.EncryptCommunications and KeyFlags.EncryptStorage for public key to encrypt
125+ # which we are not setting in our current APIs
126+ # the following code injects the require attributes to the public key signature to bypass PGPY check
127+ has_key_flags , has_hash , has_symmetric , has_compression = True , True , True , True
128+ key_flags = user .selfsig ._signature .subpackets ['h_KeyFlags' ]
129+ hash = user .selfsig ._signature .subpackets ['h_PreferredHashAlgorithms' ]
130+ symmetric = user .selfsig ._signature .subpackets ['h_PreferredSymmetricAlgorithms' ]
131+ compression = user .selfsig ._signature .subpackets ['h_PreferredCompressionAlgorithms' ]
132+ if (len (key_flags ) > 0 and len (hash ) > 0 and len (symmetric ) > 0 and len (compression ) > 0 ):
133+ key_flags , hash , symmetric , compression = key_flags [0 ], hash [0 ], symmetric [0 ], compression [0 ]
134+ else :
135+ _inject_encryption_flags (user , False , False , False , False )
136+ return
137+
138+ if not (KeyFlags .EncryptStorage in key_flags .__flags__ and KeyFlags .EncryptCommunications in key_flags .__flags__ ):
139+ has_key_flags = False
140+ if not HashAlgorithm .SHA256 in hash .__flags__ :
141+ has_hash = False
142+ if not SymmetricKeyAlgorithm .AES256 in symmetric .__flags__ :
143+ has_symmetric = False
144+ if not CompressionAlgorithm .Uncompressed in compression .__flags__ :
145+ has_compression = False
146+
147+ _inject_encryption_flags (user , has_key_flags , has_hash , has_symmetric , has_compression )
148+ return user
107149
108150def _encrypt_keycode (keycode , public_key ):
109151 """
@@ -113,11 +155,6 @@ def _encrypt_keycode(keycode, public_key):
113155 :return: The encrypted keycode
114156 """
115157 key_pair = pgpy .PGPKey .from_blob (public_key )[0 ]
116-
117- # https://github.com/SecurityInnovation/PGPy/issues/257
118- # PGPY requires KeyFlags.EncryptCommunications and KeyFlags.EncryptStorage for public key to encrypt
119- # which we are not setting in our current APIs
120- # the following code injects the require attributes to the public key signature to bypass PGPY check
121158 user = None
122159 if key_pair .is_primary :
123160 if user is not None :
@@ -126,21 +163,12 @@ def _encrypt_keycode(keycode, public_key):
126163 user = next (iter (key_pair .userids ))
127164
128165 if user is not None :
129- user .selfsig ._signature .subpackets .addnew ('KeyFlags' , hashed = True ,
130- flags = {KeyFlags .EncryptCommunications ,
131- KeyFlags .EncryptStorage })
132- user .selfsig ._signature .subpackets ['h_KeyFlags' ] = user .selfsig ._signature .subpackets ['KeyFlags' ][0 ]
133- user .selfsig ._signature .subpackets .addnew ('PreferredHashAlgorithms' , hashed = True , flags = [HashAlgorithm .SHA256 ])
134- user .selfsig ._signature .subpackets .addnew ('PreferredSymmetricAlgorithms' , hashed = True ,
135- flags = [SymmetricKeyAlgorithm .AES256 ])
136- user .selfsig ._signature .subpackets .addnew ('PreferredCompressionAlgorithms' , hashed = True ,
137- flags = [CompressionAlgorithm .Uncompressed ])
138-
139- message = PGPMessage .new (keycode , compression = CompressionAlgorithm .Uncompressed ,
140- cipher = SymmetricKeyAlgorithm .AES256 ,
141- hash = HashAlgorithm .SHA256 )
142- cipher_message = key_pair .encrypt (message )
143- return str (cipher_message )
166+ _enforce_encryption_flags (user )
167+ message = PGPMessage .new (keycode , compression = CompressionAlgorithm .Uncompressed ,
168+ cipher = SymmetricKeyAlgorithm .AES256 ,
169+ hash = HashAlgorithm .SHA256 )
170+ cipher_message = key_pair .encrypt (message )
171+ return str (cipher_message )
144172
145173
146174def _decrypt_message (message_to_decrypt , server_secret , client_secret ):
0 commit comments