Skip to content

Commit

Permalink
Add support for X509 and RSA public in encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd-ntrf committed May 2, 2024
1 parent 57cdc1b commit 224d91e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/hiera/backend/eyaml/encryptors/pkcs7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ def self.encrypt(plaintext)
LoggingHelper.trace 'PKCS7 encrypt'

public_key_pem = self.load_public_key_pem()
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
if /BEGIN CERTIFICATE/.match(public_key_pem) != nil
public_key_x509 = OpenSSL::X509::Certificate.new(public_key_pem)
elsif /BEGIN PUBLIC KEY/.match(public_key_pem) != nil
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 224d91e

Please sign in to comment.