本文簡要介紹ruby語言中 OpenSSL::X509::Certificate類
的用法。
RFC 5280 中指定的 X.509 證書的實現。提供對證書屬性的訪問並允許從字符串中讀取證書,但也支持從頭開始創建新證書。
從文件中讀取證書
Certificate
能夠處理 DER-encoded 證書和以 OpenSSL 的 PEM 格式編碼的證書。
raw = File.binread "cert.cer" # DER- or PEM-encoded
certificate = OpenSSL::X509::Certificate.new raw
將證書保存到文件
證書可以以 DER 格式編碼
cert = ...
File.open("cert.cer", "wb") { |f| f.print cert.to_der }
或 PEM 格式
cert = ...
File.open("cert.pem", "wb") { |f| f.print cert.to_pem }
X.509 證書與私鑰/公鑰對相關聯,通常是 RSA、DSA 或 ECC key (另請參見 OpenSSL::PKey::RSA
、 OpenSSL::PKey::DSA
和 OpenSSL::PKey::EC
),公鑰本身存儲在證書中並且可以以 OpenSSL::PKey
的形式訪問。證書通常用於能夠將某種形式的身份與 key 對相關聯,例如,通過 HTTPs 提供頁麵的 Web 服務器使用證書向用戶驗證自己的身份。
公鑰基礎設施 (PKI) 模型依賴於頒發這些證書的受信任的證書頒發機構 (“root CAs”),因此最終用戶隻需將信任建立在選定的少數頒發機構上,這些頒發機構自己再次擔保從屬 CA 頒發其證書以結束用戶。
OpenSSL::X509
模塊提供了設置獨立 PKI 的工具,類似於使用 ‘openssl’ 命令行工具在私有 PKI 中頒發證書的場景。
創建根 CA 證書和 end-entity 證書
首先,我們需要創建一個“self-signed” 根證書。為此,我們需要先生成一個 key 。請注意,選擇“1” 作為序列號被認為是真實證書的安全漏洞。安全選擇是兩位字節範圍內的整數,理想情況下不是連續的而是安全的隨機數,此處省略步驟以保持示例簡潔。
root_key = OpenSSL::PKey::RSA.new 2048 # the CA's public/private key
root_ca = OpenSSL::X509::Certificate.new
root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
root_ca.serial = 1
root_ca.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby CA"
root_ca.issuer = root_ca.subject # root CA's are "self-signed"
root_ca.public_key = root_key.public_key
root_ca.not_before = Time.now
root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = root_ca
ef.issuer_certificate = root_ca
root_ca.add_extension(ef.create_extension("basicConstraints","CA:TRUE",true))
root_ca.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true))
root_ca.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
root_ca.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false))
root_ca.sign(root_key, OpenSSL::Digest.new('SHA256'))
下一步是使用根 CA 證書創建 end-entity 證書。
key = OpenSSL::PKey::RSA.new 2048
cert = OpenSSL::X509::Certificate.new
cert.version = 2
cert.serial = 2
cert.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby certificate"
cert.issuer = root_ca.subject # root CA is the issuer
cert.public_key = key.public_key
cert.not_before = Time.now
cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 years validity
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = root_ca
cert.add_extension(ef.create_extension("keyUsage","digitalSignature", true))
cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
cert.sign(root_key, OpenSSL::Digest.new('SHA256'))
相關用法
- Ruby CStructEntity.[]=用法及代碼示例
- Ruby Context.save_history=用法及代碼示例
- Ruby CSV.header_convert用法及代碼示例
- Ruby Constants模塊用法及代碼示例
- Ruby CMath tanh()用法及代碼示例
- Ruby CSV.skip_lines用法及代碼示例
- Ruby CGI.new用法及代碼示例
- Ruby Comparable.between?用法及代碼示例
- Ruby CGI.print用法及代碼示例
- Ruby CGI.http_header用法及代碼示例
- Ruby Class類用法及代碼示例
- Ruby CMath cos()用法及代碼示例
- Ruby Complex.arg用法及代碼示例
- Ruby CSV.table用法及代碼示例
- Ruby CSV.force_quotes?用法及代碼示例
- Ruby CParser模塊用法及代碼示例
- Ruby CSV.unconverted_fields?用法及代碼示例
- Ruby ComposedSet類用法及代碼示例
- Ruby C.handle_constants用法及代碼示例
- Ruby Continuation類用法及代碼示例
- Ruby Closure類用法及代碼示例
- Ruby CStructEntity.[]用法及代碼示例
- Ruby Complex.abs2用法及代碼示例
- Ruby CSV類用法及代碼示例
- Ruby Context.echo_on_assignment?用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Certificate類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。