class OpenSSL::Timestamp::Factory

用于从头生成一个 Response

请记住,实现始终会应用并优先使用请求中给出的策略对象标识符,而不是 Factory 中指定的默认策略 ID。因此,只有当没有给出 Request#policy_id 时,才会应用 default_policy_id。但也意味着,在创建 Response 之前,需要手动检查请求中的策略标识符,例如检查它是否符合一组特定的可接受策略。

除了时间戳证书之外,还可以添加证书(OpenSSL::X509::Certificate 的实例),如果 Request#cert_requested?true,则会将这些证书包含在生成的时间戳令牌中。理想情况下,还应该包括任何中间证书(可以省略根证书 - 为了信任它,任何验证方都必须拥有它)。这简化了时间戳的验证,因为这些中间证书“已经存在”,无需再将其作为外部参数传递给 Response#verify,从而最大限度地减少验证所需的外部资源。

示例:包含(不受信任的)中间证书

假设我们收到了一个时间戳请求,该请求已将 Request#policy_id 设置为 nil,并将 Request#cert_requested? 设置为 true。原始请求字节存储在名为 req_raw 的变量中。我们仍然希望集成必要的中间证书(在 inter1.cerinter2.cer 中),以简化对生成的 Response 的验证。ts.p12 是一个与 PKCS#12 兼容的文件,其中包含私钥和时间戳证书。

req = OpenSSL::Timestamp::Request.new(raw_bytes)
p12 = OpenSSL::PKCS12.new(File.binread('ts.p12'), 'pwd')
inter1 = OpenSSL::X509::Certificate.new(File.binread('inter1.cer'))
inter2 = OpenSSL::X509::Certificate.new(File.binread('inter2.cer'))
fac = OpenSSL::Timestamp::Factory.new
fac.gen_time = Time.now
fac.serial_number = 1
fac.allowed_digests = ["sha256", "sha384", "sha512"]
#needed because the Request contained no policy identifier
fac.default_policy_id = '1.2.3.4.5'
fac.additional_certificates = [ inter1, inter2 ]
timestamp = fac.create_timestamp(p12.key, p12.certificate, req)

属性

default_policy_id

如果 Request 中存在 Request#policy_id,则始终优先使用它,只有当 Request#policy_id 为 nil 时才会使用 default_policy。如果两者都不存在,则在尝试创建 Response 时将引发 TimestampError

调用序列

factory.default_policy_id = "string" -> string
factory.default_policy_id            -> string or nil

serial_number

设置或检索用于创建时间戳的序列号。必须存在才能创建时间戳。

调用序列

factory.serial_number = number -> number
factory.serial_number          -> number or nil

gen_time

设置或检索要在 Response 中使用的 Time 值。必须存在才能创建时间戳。

调用序列

factory.gen_time = Time -> Time
factory.gen_time        -> Time or nil

additional_certs

设置或检索除了时间戳证书之外,要添加到 Response 中的其他证书(例如中间证书)。必须是 Array 类型的 OpenSSL::X509::Certificate

调用序列

factory.additional_certs = [cert1, cert2] -> [ cert1, cert2 ]
factory.additional_certs                  -> array or nil

allowed_digests

设置或检索工厂允许为其创建时间戳的摘要算法。应尽可能不允许已知的易受攻击或弱算法。必须是 StringOpenSSL::Digest 子类实例的 Array

调用序列

factory.allowed_digests = ["sha1", OpenSSL::Digest.new('SHA256').new] -> [ "sha1", OpenSSL::Digest) ]
factory.allowed_digests                                               -> array or nil