hiera-eyaml
hiera-eyaml copied to clipboard
Binary encrypt on Windows provides malformed binary when decrypted.
When encrypting a binary on Windows the resulting decrypted content does not provide a working binary.
Non-working example on Windows with Ruby 1.8.7 using Powershell:
$String = eyaml encrypt -f certificate.pfx -o example
eyaml decrypt -s $String > certificate_copy.pfx
Working example on CentOS 6.6 with Ruby 1.8.7 using Bash:
String=$(eyaml encrypt -f certificate.pfx -o example)
eyaml decrypt -s $String > certificate_copy.pfx
Does eyaml work properly with strings on your system? I'm guessing that it might be something to do with the way powershell (or maybe ruby) streams/encodes binaries when it reads or writes, but I'm not that familiar with powershell so I'd have to spend some time playing around, unless someone else has any great ideas!?
Most likely this is a result of newline issues. This would probably be fixed by replacing line 60 of encrypt.rb with the following:
File.open(options[:file], 'rb') do |file|
file.read
end
@hathoward: Would you be able to test this out? I can, but it will have to wait until at least tonight if not longer.
Actually, hm, there might be other places where we'd need to set things as binary. I'll need to do some digging.
I stumbled upon this while trying to encrypt Kerberos keytab files under Windows : the file gets corrupted when read from disk, because the (implicit) "mode" when reading data with File.read() is "text" and not "binary".
If I modify line 61 of encrypt.rb similar to what @elyscape suggested, the encrypted data suddenly doubles in size, because it is no longer truncated randomly in the middle :-)
File.read options[:file], mode: 'rb'
This fix does change the way line endings are handled by Ruby, so it could cause regressions for some usage. Maybe add some heuristics to handle binary and text files differently?