Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Rubrik::Sign param from public_key to certificate #6

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ input_pdf = File.open("example.pdf", "rb")
output_pdf = File.open("signed_example.pdf", "wb+") # needs read permission

# Load Certificate(s)
certificate = File.open("example_cert.pem", "rb")
private_key = OpenSSL::PKey::RSA.new(certificate, "")
certificate.rewind
public_key = OpenSSL::X509::Certificate.new(certificate)
certificate.close
certificate_file = File.open("example_cert.pem", "rb")
private_key = OpenSSL::PKey::RSA.new(certificate_file, "")
certificate_file.rewind
certificate = OpenSSL::X509::Certificate.new(certificate_file)
certificate_file.close

# Will write the signed document to `output_pdf`
Rubrik::Sign.call(input_pdf, output_pdf, private_key:, public_key:, certificate_chain: [])
Rubrik::Sign.call(input_pdf, output_pdf, private_key:, certificate:, certificate_chain: [])

# Don't forget to close the files
input_pdf.close
Expand Down
6 changes: 3 additions & 3 deletions lib/rubrik/fill_signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ module FillSignature
io: T.any(File, StringIO, Tempfile),
signature_value_ref: PDF::Reader::Reference,
private_key: OpenSSL::PKey::RSA,
public_key: OpenSSL::X509::Certificate,
certificate: OpenSSL::X509::Certificate,
certificate_chain: T::Array[OpenSSL::X509::Certificate])
.void}

FIRST_OFFSET = 0

def call(io, signature_value_ref:, private_key:, public_key:, certificate_chain: [])
def call(io, signature_value_ref:, private_key:, certificate:, certificate_chain: [])
io.rewind

signature_value_offset = PDF::Reader::XRef.new(io)[signature_value_ref]
Expand Down Expand Up @@ -53,7 +53,7 @@ def call(io, signature_value_ref:, private_key:, public_key:, certificate_chain:
io.pos = second_offset
data_to_sign += T.must(io.read(second_length))

signature = PKCS7Signature.call(data_to_sign, private_key:, certificate: public_key)
signature = PKCS7Signature.call(data_to_sign, private_key:, certificate:)
hex_signature = T.let(signature, String).unpack1("H*")

padded_contents_field = "<#{hex_signature.ljust(Document::SIGNATURE_SIZE, "0")}>"
Expand Down
6 changes: 3 additions & 3 deletions lib/rubrik/sign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module Sign
input: T.any(File, Tempfile, StringIO),
output: T.any(File, Tempfile, StringIO),
private_key: OpenSSL::PKey::RSA,
public_key: OpenSSL::X509::Certificate,
certificate: OpenSSL::X509::Certificate,
certificate_chain: T::Array[OpenSSL::X509::Certificate])
.void}
def self.call(input, output, private_key:, public_key:, certificate_chain: [])
def self.call(input, output, private_key:, certificate:, certificate_chain: [])
input.binmode
output.reopen(T.unsafe(output), "wb+") if !output.is_a?(StringIO)

Expand All @@ -22,7 +22,7 @@ def self.call(input, output, private_key:, public_key:, certificate_chain: [])

Document::Increment.call(document, io: output)

FillSignature.call(output, signature_value_ref:, private_key:, public_key:, certificate_chain:)
FillSignature.call(output, signature_value_ref:, private_key:, certificate:, certificate_chain:)
end
end
end
24 changes: 12 additions & 12 deletions test/rubrik/sign_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def test_document_with_interactive_form
# Arrange
input_pdf = File.open(SupportPDF["with_interactive_form"], "rb")
output_pdf = StringIO.new
certificate = File.open("test/support/demo_cert.pem", "rb")
certificate_file = File.open("test/support/demo_cert.pem", "rb")

private_key = OpenSSL::PKey::RSA.new(certificate, "")
certificate.rewind
public_key = OpenSSL::X509::Certificate.new(certificate)
private_key = OpenSSL::PKey::RSA.new(certificate_file, "")
certificate_file.rewind
certificate = OpenSSL::X509::Certificate.new(certificate_file)

# Act
Sign.call(input_pdf, output_pdf, private_key:, public_key:)
Sign.call(input_pdf, output_pdf, private_key:, certificate:)

# Assert
expected_output = File.open(SupportPDF["with_interactive_form.expected"], "rb")
Expand All @@ -35,7 +35,7 @@ def test_document_with_interactive_form
assert_equal(expected_line, actual_line)
end
ensure
certificate&.close
certificate_file&.close
output_pdf&.close
input_pdf&.close
expected_output&.close
Expand All @@ -45,14 +45,14 @@ def test_document_without_interactive_form
# Arrange
input_pdf = File.open(SupportPDF["without_interactive_form"], "rb")
output_pdf = StringIO.new
certificate = File.open("test/support/demo_cert.pem", "rb")
certificate_file = File.open("test/support/demo_cert.pem", "rb")

private_key = OpenSSL::PKey::RSA.new(certificate, "")
certificate.rewind
public_key = OpenSSL::X509::Certificate.new(certificate)
private_key = OpenSSL::PKey::RSA.new(certificate_file, "")
certificate_file.rewind
certificate = OpenSSL::X509::Certificate.new(certificate_file)

# Act
Sign.call(input_pdf, output_pdf, private_key:, public_key:)
Sign.call(input_pdf, output_pdf, private_key:, certificate:)

# Assert
expected_output = File.open(SupportPDF["without_interactive_form.expected"], "rb")
Expand All @@ -71,7 +71,7 @@ def test_document_without_interactive_form
assert_equal(expected_line, actual_line)
end
ensure
certificate&.close
certificate_file&.close
output_pdf&.close
input_pdf&.close
expected_output&.close
Expand Down