For starters, I can't use any 3rd party modules and my code must work with Python 2.4 due to this being a large enterprise environment and this tool must work on vanilla 2.4 python installs with no third party modules.
I'm writing a wrapper for openssl that will be used to retrieve/store encrypted data, mainly usernames/passwords and other authentication type data.
I'm writing the create module which creates a new, empty encrypted pickle file.
Flow is as follows:
Create a picklelized object in memory which is basically a dictionary object. (pickle.dumps) Encrypt the resulting pickle string in memory (echo string | openssl.....) Write this new string out as a pickle object. (pickle.dump)
Here is my current attempt:
def create(self, wallet, cipher=None, passphrase=None, **kawrgs):
self.wallet = wallet
rawdata = None
encdata = None
outfile = None
try:
outfile = open(self.wallet, 'w')
except Exception, e:
raise OpenSSLWalletError("Failed to open '%s' for writing" % self.wallet, None, None, None)
if passphrase:
self.passphrase = "-k %s" % passphrase
else:
self.passphrase = "-k ''"
if cipher:
self.cipher = cipher
try:
rawdata = pickle.dumps(self.data)
cmd = "echo -en '%s' | %s %s -a -salt %s" % (rawdata, self.openssl, self.cipher, self.passphrase)
os_cmd = shlex.split(cmd)
proc = subprocess.Popen(os_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
encdata, stderr = proc.communicate()
pickle.dump(encdata, outfile)
except Exception, e:
raise OpenSSLWalletError("Failed to create encrypted wallet '%s'" % self.wallet, cmd.rstrip(), None, stderr.rstrip())
I believe my problem is related to pickle adding newlines in the middle of the object which then breaks the openssl command:
Here is what the string looks like after pickle.dumps (basically this is just an empty dictionary fed to pickle):
self.data='{}'
'(dp0
.'
If I print out the cmd before it's fed to Popen, it looks like this:
cmd='echo (dp0
. | /usr/bin/openssl aes-256-cbc -a -salt -k '''
I've tried escaping the string, shell=True, shell=False, etc....
Anyone know any tricks or ways I can work around this? I assume stripping out the newlines from the pickle object will then break the pickle format and it won't be loadable, correct?
Thanks for any help.