class OpenSSL::X509::Certificate

RFC 5280 中定义的 X.509 证书的实现。提供对证书属性的访问,允许从字符串读取证书,同时也支持从头开始创建新证书。

从文件读取证书

Certificate 能够处理 DER 编码的证书和 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 密钥(另请参阅 OpenSSL::PKey::RSAOpenSSL::PKey::DSAOpenSSL::PKey::EC),公钥本身存储在证书内,可以通过 OpenSSL::PKey 对象访问。证书通常用于将某种身份与密钥对相关联,例如,通过 HTTPS 提供页面的 Web 服务器使用证书向用户进行身份验证。

公钥基础设施 (PKI) 模型依赖于颁发这些证书的可信证书颁发机构(“根 CA”),因此最终用户只需依赖少数选定的 CA,这些 CA 又为向最终用户颁发证书的子 CA 提供担保。

OpenSSL::X509 模块提供了设置独立 PKI 的工具,类似于使用 ‘openssl’ 命令行工具在私有 PKI 中颁发证书的场景。

创建根 CA 证书和终端实体证书

首先,我们需要创建一个“自签名”的根证书。为此,我们首先需要生成一个密钥。请注意,选择“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 证书创建终端实体证书。

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'))