Skip to content

Commit

Permalink
Merge pull request #379 from cmd-ntrf/new_encrypt
Browse files Browse the repository at this point in the history
Add support to encrypt with an RSA public key
  • Loading branch information
bastelfreak authored May 10, 2024
2 parents f12211f + 87544ab commit 1a4a85d
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/hiera/backend/eyaml/encryptors/pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def self.encrypt(plaintext)
LoggingHelper.trace 'PKCS7 encrypt'

public_key_pem = load_public_key_pem
public_key_x509 = OpenSSL::X509::Certificate.new(public_key_pem)
if public_key_pem.include? 'BEGIN CERTIFICATE'
public_key_x509 = OpenSSL::X509::Certificate.new(public_key_pem)
elsif public_key_pem.include? 'BEGIN PUBLIC KEY'
public_key_rsa = OpenSSL::PKey::RSA.new(public_key_pem)
public_key_x509 = OpenSSL::X509::Certificate.new
public_key_x509.public_key = public_key_rsa.public_key
else
raise StandardError, "file #{public_key_pem} cannot be used to encrypt - invalid public key format"
end

cipher = OpenSSL::Cipher.new('aes-256-cbc')
OpenSSL::PKCS7.encrypt([public_key_x509], plaintext, cipher, OpenSSL::PKCS7::BINARY).to_der
Expand Down

0 comments on commit 1a4a85d

Please sign in to comment.