Skip to content

Commit

Permalink
Merge pull request #132 from railslove/bug/support-key-decrypting-on-…
Browse files Browse the repository at this point in the history
…init-for-openssl3
  • Loading branch information
tobischo committed Aug 29, 2022
2 parents 06f7e63 + 33d419b commit fdd7d7c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/epics/client.rb
Expand Up @@ -265,14 +265,16 @@ def dump_keys
JSON.dump(keys.each_with_object({}) {|(k,v),m| m[k]= encrypt(v.key.to_pem)})
end

def cipher
@cipher ||= OpenSSL::Cipher.new("aes-256-cbc")
def new_cipher
# Re-using the cipher between keys has weird behaviours with openssl3
# Using a fresh key instead of memoizing it on the client simplifies things
OpenSSL::Cipher.new('aes-256-cbc')
end

def encrypt(data)
salt = OpenSSL::Random.random_bytes(8)

setup_cipher(:encrypt, self.passphrase, salt)
cipher = setup_cipher(:encrypt, self.passphrase, salt)
Base64.strict_encode64([salt, cipher.update(data) + cipher.final].join)
end

Expand All @@ -281,13 +283,15 @@ def decrypt(data)
salt = data[0..7]
data = data[8..-1]

setup_cipher(:decrypt, self.passphrase, salt)
cipher = setup_cipher(:decrypt, self.passphrase, salt)
cipher.update(data) + cipher.final
end

def setup_cipher(method, passphrase, salt)
cipher = new_cipher
cipher.send(method)
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(passphrase, salt, 1, cipher.key_len)
cipher
end

def verify_ssl?
Expand Down

0 comments on commit fdd7d7c

Please sign in to comment.