Skip to content

Commit

Permalink
Add support to encrypt with an RSA public key
Browse files Browse the repository at this point in the history
Based on the header of the public key, we can identify if we have
a X509 certificate or an RSA public key. If we have an RSA public
key, we simply generate a X509 certificate on the fly that will
contain only the information required by encrypt.
  • Loading branch information
cmd-ntrf committed May 6, 2024
1 parent f12211f commit 3e5e1b1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 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,13 @@ 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
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 3e5e1b1

Please sign in to comment.