本文简要介绍ruby语言中 OpenSSL::OCSP模块
的用法。
OpenSSL::OCSP
实现在线证书状态协议请求和响应。
创建和发送 OCSP
请求需要一个主题证书,该证书在 authorityInfoAccess 扩展中包含一个 OCSP
URL 和主题证书的颁发者证书。首先,加载颁发者和主题证书:
subject = OpenSSL::X509::Certificate.new subject_pem
issuer = OpenSSL::X509::Certificate.new issuer_pem
要创建请求,我们需要为主题证书创建一个证书 ID,以便 CA 知道我们询问的是哪个证书:
digest = OpenSSL::Digest.new('SHA1')
certificate_id =
OpenSSL::OCSP::CertificateId.new subject, issuer, digest
然后创建一个请求并将证书 ID 添加到其中:
request = OpenSSL::OCSP::Request.new
request.add_certid certificate_id
向请求添加随机数可以防止重放攻击,但并非所有 CA 都会处理随机数。
request.add_nonce
要将请求提交给 CA 进行验证,我们需要从主题证书中提取 OCSP
URI
:
ocsp_uris = subject.ocsp_uris
require 'uri'
ocsp_uri = URI ocsp_uris[0]
要提交请求,我们会将请求发布到 OCSP
URI
(根据 RFC 2560)。请注意,在此示例中,我们仅处理 HTTP 请求,不处理任何重定向,因此这不足以严重使用。
require 'net/http'
http_response =
Net::HTTP.start ocsp_uri.hostname, ocsp.port do |http|
http.post ocsp_uri.path, request.to_der,
'content-type' => 'application/ocsp-request'
end
response = OpenSSL::OCSP::Response.new http_response.body
response_basic = response.basic
首先,我们检查响应是否具有有效签名。如果没有有效的签名,我们将无法信任它。如果您在此处遇到故障,则可能缺少系统证书存储或可能缺少中间证书。
store = OpenSSL::X509::Store.new
store.set_default_paths
unless response_basic.verify [], store then
raise 'response is not signed by a trusted certificate'
end
响应包含状态信息(成功/失败)。我们可以将状态显示为字符串:
puts response.status_string #=> successful
接下来我们需要知道响应的详细信息来确定响应是否与我们的请求相匹配。首先我们检查随机数。同样,并非所有 CA 都支持 nonce。返回值的含义见 Request#check_nonce
。
p request.check_nonce basic_response #=> value from -1 to 3
然后从基本响应中提取证书的状态信息。
single_response = basic_response.find_response(certificate_id)
unless single_response
raise 'basic_response does not have the status for the certificate'
end
然后检查有效性。将来发布的状态必须被拒绝。
unless single_response.check_validity raise 'this_update is in the future or next_update time has passed' end case single_response.cert_status when OpenSSL::OCSP::V_CERTSTATUS_GOOD puts 'certificate is still valid' when OpenSSL::OCSP::V_CERTSTATUS_REVOKED puts "certificate has been revoked at #{single_response.revocation_time}" when OpenSSL::OCSP::V_CERTSTATUS_UNKNOWN puts 'responder doesn't know about the certificate' end
相关用法
- Ruby Object.instance_variable_get用法及代码示例
- Ruby Open3.capture3用法及代码示例
- Ruby Open3.capture2用法及代码示例
- Ruby OpenStruct.ostruct[name] =用法及代码示例
- Ruby Option.level用法及代码示例
- Ruby OpenSSL.fips_mode =用法及代码示例
- Ruby Object.display用法及代码示例
- Ruby Object.remove_instance_variable用法及代码示例
- Ruby Option.byte用法及代码示例
- Ruby Object.define_singleton_method用法及代码示例
- Ruby ObjectSpace.memsize_of_all用法及代码示例
- Ruby Object.methods用法及代码示例
- Ruby Object.public_send用法及代码示例
- Ruby ObjectSpace模块用法及代码示例
- Ruby Object.xmp用法及代码示例
- Ruby OpenSSL模块用法及代码示例
- Ruby Open3.capture2e用法及代码示例
- Ruby Object.singleton_methods用法及代码示例
- Ruby Option.family用法及代码示例
- Ruby Object.enum_for用法及代码示例
- Ruby ObjectSpace.count_symbols用法及代码示例
- Ruby Open3.popen2e用法及代码示例
- Ruby ObjectSpace.define_finalizer用法及代码示例
- Ruby ObjectSpace.count_imemo_objects用法及代码示例
- Ruby Object.freeze用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 OCSP模块。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。