如何创建自己的证书链?

Sta*_*ked 73 certificate ssl

我想设置我自己的 OCSP Responder 用于测试目的,这需要我有一个根证书,其中包含一些从中生成的证书。

我已经设法使用 来创建自签名证书openssl,并且我想将其用作根证书。下一步是创建派生证书,但是,我似乎找不到有关如何执行此操作的文档。有谁知道我在哪里可以找到这些信息?

  • 编辑:
    回想起来,我的问题还没有完全回答,为了澄清这个问题,我将这样表示我的证书链:Root > A > B > C > ...

我目前可以通过下面的方式创建 Root 和 A 证书,但我还没有找到如何制作更长的链:

# Root certificate is created like this:
  openssl req -new -newkey rsa:1024 -nodes -out ca.csr -keyout ca.key
  openssl x509 -trustout -signkey ca.key -days 365 -req -in ca.csr -out ca.pem

# Certificate A is created like this:
  openssl genrsa -out client.key 1024
  openssl req -new -key client.key -out client.csr
  openssl ca -in client.csr -out client.cer
Run Code Online (Sandbox Code Playgroud)
  • 该命令隐式依赖于根证书,它会在 OpenSSL 配置文件中找到所需的信息,但是,证书 B 必须仅依赖于未在配置文件中注册的 A,因此前面的命令在这里不起作用.

我应该使用什么命令来创建证书 B 及以上?

  • 编辑:
    我在这篇文章中找到了答案:可以使用这两个命令创建证书 B(链 A -> B),并且这种方法似乎运行良好。:

    # Create a certificate request
    openssl req -new -keyout B.key -out B.request -days 365
    
    # Create and sign the certificate
    openssl ca -policy policy_anything -keyfile A.key -cert A.pem -out B.pem -infiles B.request
    
    Run Code Online (Sandbox Code Playgroud)

    我还更改了openssl.cnf文件:

    [ usr_cert ]
    basicConstraints=CA:TRUE # prev value was FALSE
    
    Run Code Online (Sandbox Code Playgroud)

小智 38

您可以直接使用 OpenSSL。

  1. 创建证书颁发机构私钥(这是您最重要的密钥):

    openssl req -new -newkey rsa:1024 -nodes -out ca.csr -keyout ca.key
    
    Run Code Online (Sandbox Code Playgroud)
  2. 创建您的 CA 自签名证书:

    openssl x509 -trustout -signkey ca.key -days 365 -req -in ca.csr -out ca.pem
    
    Run Code Online (Sandbox Code Playgroud)
  3. 首先生成密钥,然后请求(或使用外部系统提供的密钥)颁发客户端证书,然后使用 CA 的私钥对证书进行签名:

    openssl genrsa -out client.key 1024
    openssl req -new -key client.key -out client.csr
    openssl ca -in client.csr -out client.cer
    
    Run Code Online (Sandbox Code Playgroud)

(您可能需要添加一些选项,因为我将这些命令与我的 openssl.conf 文件一起使用。您可能需要先设置自己的 .conf 文件。)

  • “您可能需要添加一些选项......”确实从这个答案中删除了该实用程序。 (46认同)
  • 最后一步失败,“无法加载 CA 私钥”;我可以通过提供带有 `openssl ca -in client.csr -out client.cer -cern ca.pem -keyfile ca.key` 的密钥和证书来实现,但它需要一个 `demoCA` 目录和各种装备。 (6认同)
  • @twk:请注意,要获得完整答案,该问题还需要一个步骤——如何创建另一个仅依赖于在步骤 3 中创建的证书而不依赖于根证书的证书。 (3认同)
  • 完成最后一步(来自下面的响应)`openssl x509 -req -days 365 -in client.csr -CA ca.pem -CAkey ca.key -set_serial 01 -out client.cer` (2认同)

Mr_*_*s_D 24

创建 CA 后,您可以使用它来签署证书:

更改以下内容意味着您颁发的证书可用于签署其他证书:

basicConstraints=CA:TRUE  # prev value was FALSE
Run Code Online (Sandbox Code Playgroud)

  • 什么 .crt 文件? (3认同)

Spi*_*iff 10

OpenSSL 附带一个 Perl 脚本CA.pl,可帮助您创建自签名根CA证书以及匹配的私钥,以及一些简单的文件和目录,以帮助跟踪您使用该根 CA 签署的任何未来证书(也称为问题) . 它还可以帮助您生成其他密钥对和证书签名请求 (CSR),并帮助您处理这些 CSR(即为它们颁发证书)等等。

请注意,许多产品要求 CA 证书包含将它们标记为 CA 证书的特定属性,否则它们将不会被接受为其他证书的有效签名者/颁发者。如果您创建的自签名证书不包含该属性,则您可能无法让其他软件将其视为有效的根 CA 证书。

如果我没记错的话,语法是这样的:

CA.pl -newca    # Create a new root CA  

CA.pl -newreq   # Create a new CSR

CA.pl -sign     # Sign a CSR, creating a cert  

CA.pl -pkcs12   # Turn an issued cert, plus its matching private key and trust chain, 
                # into a .p12 file you can install on another machine    
Run Code Online (Sandbox Code Playgroud)

  • 这很有帮助。在 Ubuntu 14.04 上,我在 `/usr/lib/ssl/misc/CA.pl` 找到了该文件 (4认同)

Mil*_*ith 7

概括

用于创建根 CA、中间 CA 和叶证书的命令摘要:

openssl genrsa -out root.key 2048
openssl req -new -key root.key -out root.csr -config root_req.config
openssl ca -in root.csr -out root.pem -config root.config -selfsign -extfile ca.ext -days 1095

openssl genrsa -out intermediate.key 2048
openssl req -new -key intermediate.key -out intermediate.csr -config intermediate_req.config
openssl ca -in intermediate.csr -out intermediate.pem -config root.config -extfile ca.ext -days 730

openssl genrsa -out leaf.key 2048
openssl req -new -key leaf.key -out leaf.csr -config leaf_req.config
openssl ca -in leaf.csr -out leaf.pem -config intermediate.config -days 365

openssl verify -x509_strict -CAfile root.pem -untrusted intermediate.pem leaf.pem
Run Code Online (Sandbox Code Playgroud)

这些命令依赖于我将在下面描述的一些设置。如果您只想要链中的一些证书,那么它们有点矫枉过正,这可以通过 x509 命令来完成。这些命令还将在文本数据库中跟踪您的证书并自动增加序列号。我建议openssl ca在阅读此答案之前或之后阅读手册页的警告和错误部分。

目录结构

在开始之前,我们将需要以下目录结构。

ca.ext              # the extensions required for a CA certificate for signing certs
intermediate.config # configuration for the intermediate CA
root.config         # configuration for the root CA

leaf_req.config         # configuration for the leaf cert's csr
intermediate_req.config # configuration for the intermediate CA's csr
root_req.config         # configuration for the root CA's csr

intermediate_ca/    # state files specific to the intermediate CA
    index           # a text database of issued certificates
    serial          # an auto-incrementing serial number for issued certificates
root_ca/            # state files specific to the root CA
    index           # a text database of issued certificates
    serial          # an auto-incrementing serial number for issued certificates
Run Code Online (Sandbox Code Playgroud)

如果这是一个更永久的 CA,则以下更改可能是一个好主意:

  1. 将每个 CA 的配置文件、私钥(稍后生成)和证书文件(稍后生成)移动到 CA 的目录中。这将需要更改配置文件。
  2. 在 CA 的目录中为颁发的证书创建一个子目录。这需要更改配置文件
  3. 加密私钥
  4. 在 CA 配置文件中为颁发的证书设置默认天数

启动目录结构文件内容

目录结构中各个文件的内容如下:

目录

[ default ]
basicConstraints = critical,CA:true     # recommended to be marked critical. required for a ca
keyUsage         = critical,keyCertSign # required to be marked critical. required for signing certs
Run Code Online (Sandbox Code Playgroud)

中间配置文件

[ ca ]
default_ca      = CA_default

[ CA_default]
dir             = ./intermediate_ca   # helper variable pointing to ca specific files
database        = $dir/index          # database of certs generated by the ca
new_certs_dir   = ./                  # one dir up to make the demo easier
certificate     = ./intermediate.pem  # one dir up to make the demo easier
serial          = $dir/serial         # file with incrementing hex serial number for certs
private_key     = ./intermediate.key

policy          = policy_any
email_in_dn     = no                  # recommended
unique_subject  = no                  # recommended for easier certificate rollover
copy_extensions = none                # don't honor the extensions in the csr
default_md      = sha256

[ policy_any ]
countryName            = optional
stateOrProvinceName    = optional
organizationName       = optional
organizationalUnitName = optional
commonName             = supplied
Run Code Online (Sandbox Code Playgroud)

根配置文件

[ ca ]
default_ca      = CA_default

[ CA_default]
dir             = ./root_ca      # helper variable pointing to ca specific files
database        = $dir/index     # database of certs generated by the ca
new_certs_dir   = ./             # one dir up to make the demo easier
certificate     = ./root.pem     # one dir up to make the demo easier
serial          = $dir/serial    # file with incrementing hex serial number for certs
private_key     = ./root.key

policy          = policy_any
email_in_dn     = no             # recommended
unique_subject  = no             # recommended for easier certificate rollover
copy_extensions = none           # don't honor the extensions in the csr
default_md      = sha256

[ policy_any ]
countryName            = optional
stateOrProvinceName    = optional
organizationName       = optional
organizationalUnitName = optional
commonName             = supplied
Run Code Online (Sandbox Code Playgroud)

Leaf_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Leaf
Run Code Online (Sandbox Code Playgroud)

middle_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Intermediate CA
Run Code Online (Sandbox Code Playgroud)

root_req.config

[ req ]
distinguished_name = req_distinguished_name
prompt             = no

[ req_distinguished_name ]
countryName = US
commonName  = Test Root CA
Run Code Online (Sandbox Code Playgroud)

middle_ca/index(空文件)。已颁发证书的数据库。自动更新

[empty]
Run Code Online (Sandbox Code Playgroud)

middle_ca/serial(单个 0 不起作用)。此文件自动递增

00
Run Code Online (Sandbox Code Playgroud)

root_ca/index(空文件)。已颁发证书的数据库。自动更新

[empty]
Run Code Online (Sandbox Code Playgroud)

root_ca/serial(单个 0 不起作用)。此文件自动递增

00
Run Code Online (Sandbox Code Playgroud)

详细命令

现在我们可以从这个答案的开头运行命令:

# create the private key for the root CA
openssl genrsa 
    -out root.key # output file
    2048          # bitcount

# create the csr for the root CA
openssl req 
    -new 
    -key root.key           # private key associated with the csr
    -out root.csr           # output file
    -config root_req.config # contains config for generating the csr such as the distinguished name

# create the root CA cert
openssl ca 
    -in root.csr        # csr file
    -out root.pem       # output certificate file
    -config root.config # CA configuration file
    -selfsign           # create a self-signed certificate
    -extfile ca.ext     # extensions that must be present for CAs that sign certificates
    -days 1095          # 3 years

# create the private key for the intermediate CA
openssl genrsa 
    -out intermediate.key # output file
    2048                  # bitcount

# create the csr for the intermediate CA
openssl req 
    -new 
    -key intermediate.key           # private key associated with the csr
    -out intermediate.csr           # output file
    -config intermediate_req.config # contains config for generating the csr such as the distinguished name

# create the intermediate CA cert
openssl ca 
    -in intermediate.csr  # csr file
    -out intermediate.pem # output certificate file
    -config root.config   # CA configuration file (note: root is still issuing)
    -extfile ca.ext       # extensions that must be present for CAs that sign certificates
    -days 730             # 2 years

# create the private key for the leaf certificate
openssl genrsa 
    -out leaf.key # output file
    2048          # bitcount

# create the csr for the leaf certificate
openssl req 
    -new 
    -key leaf.key           # private key associated with the csr
    -out leaf.csr           # output file
    -config leaf_req.config # contains config for generating the csr such as the distinguished name

# create the leaf certificate (note: no ca.ext. this certificate is not a CA)
openssl ca 
    -in leaf.csr                # csr file
    -out leaf.pem               # output certificate file
    -config intermediate.config # CA configuration file (note: intermediate is issuing)
    -days 365                   # 1 year

# verify the certificate chain
openssl verify 
    -x509_strict                # strict adherence to rules
    -CAfile root.pem            # root certificate
    -untrusted intermediate.pem # file with all intermediates
    leaf.pem                    # leaf certificate to verify
Run Code Online (Sandbox Code Playgroud)

最后的想法

如果您希望在生产中使用 CA,请阅读openssl ca手册页(或整个手册页)的警告和错误部分。

  • 很棒的答案,非常详细且有帮助!谢谢 (3认同)
  • 我可以为“openssl x509”写另一个答案。配置文件更短,并且没有任何数据库/序列文件,因此一次性证书链创建更容易 (2认同)

Iva*_*gin 6

根据这个问题(以及相关问题和文章)的各种答案,我找到了一组命令,允许我使用默认的 openssl 配置创建根 ca、中间 ca 和测试证书以用于测试目的。因此,我想将其发布给我自己以及对适合测试目的的快速解决方案感兴趣的其他人:

先决条件:OpenSSL 1.1.1

  1. 生成CA
openssl req -new -newkey rsa:2048 -nodes -out ca.csr -keyout ca.key -extensions v3_ca
openssl x509 -signkey ca.key -days 365 -req -in ca.csr -set_serial 01 -out ca.crt
Run Code Online (Sandbox Code Playgroud)
  1. 生成中间CA
openssl req -new -newkey rsa:2048 -nodes -out inter.csr -keyout inter.key -addext basicConstraints=CA:TRUE
openssl x509 -CA ca.crt -CAkey ca.key -days 365 -req -in inter.csr -set_serial 02 -out inter.crt
Run Code Online (Sandbox Code Playgroud)
  1. 生成目标证书请求。
openssl req -new -newkey rsa:2048 -nodes -out test.csr -keyout test.key
Run Code Online (Sandbox Code Playgroud)
  1. 使用中间 CA 签署目标证书请求
openssl x509 -CA inter.crt -CAkey inter.key -days 365 -req -in test.csr -set_serial 03 -out test.crt
Run Code Online (Sandbox Code Playgroud)
  1. 将带有私钥的测试证书和链证书导出到 PFX
openssl pkcs12 -export -out test.pfx -inkey test.key -in test.crt -certfile inter.crt -certfile ca.crt
Run Code Online (Sandbox Code Playgroud)