[英]Origami & OpenSSL error while validating digital signature of a pdf in rails
[英]Insert digital signature into existing pdf file
我需要使用rails應用程序服務器將數字簽名插入到現有的pdf文件中。 (基本上,客戶端上傳pdf文件,服務器使用本地證書對其進行簽名)
我一直在使用JSignpdf將數字簽名插入到pdf文件中,並開始探索寶石的寶石......
我在rubypdf網站http://soft.rubypdf.com/software/pdf-digital-signe上找到了另一個可移植文件,但在ruby中找不到任何寶石甚至示例代碼。
我還看了OpenSSL的數字簽名驗證 ,但無法理解如何使用本地證書文件對現有文檔進行實際簽名。
我還在http://code.google.com/p/origami-pdf/上達到了一個高峰,但對於一個假設“簡單”(至少在概念上)的任務來說,這似乎有點苛刻。
有什么想法/建議嗎?
謝謝
經過一些研究,重復使用OpenSSL文檔並探索Origami解決方案 ,我構建了下面的代碼,並設法將本地生成的簽名/證書插入到pdf文檔中。 現在我只需要弄清楚如何使用外部生成的證書(檢查下面的版本2,我解決了它)。 我已經打開了一個新問題 ,你可以在其中找到一些關於我使用OpenSSL和DER編碼證書的難度的細節。
為了開發版本2,我還花了一些時間想知道如何添加注釋 - 因此簽名在Adobe閱讀器中變得可見 - 而無需在文檔中添加新頁面。 從origami文檔中 ,我找到了get_page方法,它解決了我的最后一個問題。 我正在使用Adobe Reader X進行記錄。
希望你發現這有用,因為我會;-)。
版本1 - 生成證書和密鑰文件,並將它們直接插入到文檔中
require 'openssl'
begin
require 'origami'
rescue LoadError
ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
$: << ORIGAMIDIR
require 'origami'
end
include Origami
# Code below is based on documentation available on
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html
key = OpenSSL::PKey::RSA.new 2048
open 'private_key.pem', 'w' do |io| io.write key.to_pem end
open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
cipher = OpenSSL::Cipher::Cipher.new 'AES-128-CBC'
pass_phrase = 'Origami rocks'
key_secure = key.export cipher, pass_phrase
open 'private_key.pem', 'w' do |io|
io.write key_secure
end
#Create the certificate
name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example'
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 0
cert.not_before = Time.now
cert.not_after = Time.now + 3600
cert.public_key = key.public_key
cert.subject = name
OUTPUTFILE = "test.pdf"
contents = ContentStream.new.setFilter(:FlateDecode)
contents.write OUTPUTFILE,
:x => 350, :y => 750, :rendering => Text::Rendering::STROKE, :size => 30
pdf = PDF.read('Sample.pdf')
# Open certificate files
#sigannot = Annotation::Widget::Signature.new
#sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
#page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
#:annotation => sigannot,
:location => "Portugal",
:contact => "myemail@email.tt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(OUTPUTFILE)
版本2 - 使用現有證書簽署pdf文檔
require 'openssl'
begin
require 'origami'
rescue LoadError
ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
$: << ORIGAMIDIR
require 'origami'
end
include Origami
INPUTFILE = "Sample.pdf"
@inputfile = String.new(INPUTFILE)
OUTPUTFILE = @inputfile.insert(INPUTFILE.rindex("."),"_signed")
CERTFILE = "certificate.pem"
RSAKEYFILE = "private_key.pem"
passphrase = "your passphrase"
key4pem=File.read RSAKEYFILE
key = OpenSSL::PKey::RSA.new key4pem, passphrase
cert = OpenSSL::X509::Certificate.new(File.read CERTFILE)
pdf = PDF.read(INPUTFILE)
page = pdf.get_page(1)
# Add signature annotation (so it becomes visibles in pdf document)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Portugal",
:contact => "myemail@email.tt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(OUTPUTFILE)
如果您正在開發一個付費項目,您可能需要考慮jPDFSecure ,這是一個商業Java庫,專為開發人員設計,可以對PDF文檔進行數字簽名並更改PDF文檔的安全設置。 使用jPDFSecure,您的應用程序或Java小程序可以加密PDF文檔,設置權限和密碼,以及創建和應用數字簽名。 jPDFSecure針對性能進行了優化,基於Qoppa專有的PDF技術構建,因此無需任何第三方軟件或驅動程序。
jPDFSecure有一個簡單的界面,可以從文件,網絡驅動器,URL甚至輸入流加載PDF文檔,這些文件可以生成運行時或直接來自數據庫。 更改安全設置后,jPDFSecure可以在Java EE應用程序服務器中運行時將文檔保存到文件,java.io.OutputStream或javax.servlet.ServletOutputStream,以將文件直接輸出到瀏覽器。
jPDFSecure獨立於平台,可用於支持Java的任何環境,包括Windows,Mac OSX和Linux。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.