@@ -26,19 +26,13 @@ class Pkcs7 < Encryptor
26
26
}
27
27
28
28
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
34
29
35
30
def self . encrypt ( plaintext )
36
31
LoggingHelper . trace 'PKCS7 encrypt'
37
32
38
33
public_key_pem = self . load_public_key_pem ( )
39
34
public_key_rsa = OpenSSL ::PKey ::RSA . new ( public_key_pem )
40
35
public_key_x509 = OpenSSL ::X509 ::Certificate . new
41
- public_key_x509 . serial = Pkcs7 ::X509_SERIAL_NUMBER
42
36
public_key_x509 . public_key = public_key_rsa . public_key
43
37
44
38
cipher = OpenSSL ::Cipher . new ( 'aes-256-cbc' )
@@ -51,18 +45,19 @@ def self.decrypt(ciphertext)
51
45
private_key_pem = self . load_private_key_pem ( )
52
46
private_key_rsa = OpenSSL ::PKey ::RSA . new ( private_key_pem )
53
47
48
+ pkcs7 = OpenSSL ::PKCS7 . new ( ciphertext )
49
+
54
50
# Since ruby-openssl 2.2.0, it is possible to call OpenSSL::PKCS7#decrypt
55
51
# with the private key only. Reference:
56
52
# https://github.com/ruby/openssl/pull/183
57
53
if Gem ::Version ::new ( OpenSSL ::VERSION ) >= Gem ::Version ::new ( '2.2.0' )
58
54
public_key_x509 = nil
59
55
else
60
56
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
62
58
public_key_x509 . public_key = private_key_rsa . public_key
63
59
end
64
60
65
- pkcs7 = OpenSSL ::PKCS7 . new ( ciphertext )
66
61
pkcs7 . decrypt ( private_key_rsa , public_key_x509 )
67
62
end
68
63
0 commit comments