Skip to content

Commit ee5b9c2

Browse files
committed
Retrieve certificate serial number for data when decrypting
1 parent 64d1f88 commit ee5b9c2

File tree

1 file changed

+3
-8
lines changed
  • lib/hiera/backend/eyaml/encryptors

1 file changed

+3
-8
lines changed

lib/hiera/backend/eyaml/encryptors/pkcs7.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,13 @@ class Pkcs7 < Encryptor
2626
}
2727

2828
self.tag = 'PKCS7'
29-
# The public certificate serial could be any number,
30-
# but the tests encrypted data were signed with a certificate with the
31-
# serial number 0. It was later changed to 1 in f9fde79,
32-
# but tests data were not re-generated.
33-
X509_SERIAL_NUMBER = 0
3429

3530
def self.encrypt(plaintext)
3631
LoggingHelper.trace 'PKCS7 encrypt'
3732

3833
public_key_pem = self.load_public_key_pem()
3934
public_key_rsa = OpenSSL::PKey::RSA.new(public_key_pem)
4035
public_key_x509 = OpenSSL::X509::Certificate.new
41-
public_key_x509.serial = Pkcs7::X509_SERIAL_NUMBER
4236
public_key_x509.public_key = public_key_rsa.public_key
4337

4438
cipher = OpenSSL::Cipher.new('aes-256-cbc')
@@ -51,18 +45,19 @@ def self.decrypt(ciphertext)
5145
private_key_pem = self.load_private_key_pem()
5246
private_key_rsa = OpenSSL::PKey::RSA.new(private_key_pem)
5347

48+
pkcs7 = OpenSSL::PKCS7.new(ciphertext)
49+
5450
# Since ruby-openssl 2.2.0, it is possible to call OpenSSL::PKCS7#decrypt
5551
# with the private key only. Reference:
5652
# https://github.com/ruby/openssl/pull/183
5753
if Gem::Version::new(OpenSSL::VERSION) >= Gem::Version::new('2.2.0')
5854
public_key_x509 = nil
5955
else
6056
public_key_x509 = OpenSSL::X509::Certificate.new
61-
public_key_x509.serial = Pkcs7::X509_SERIAL_NUMBER
57+
public_key_x509.serial = pkcs7.recipients[0].serial
6258
public_key_x509.public_key = private_key_rsa.public_key
6359
end
6460

65-
pkcs7 = OpenSSL::PKCS7.new(ciphertext)
6661
pkcs7.decrypt(private_key_rsa, public_key_x509)
6762
end
6863

0 commit comments

Comments
 (0)