

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 管理 CA 证书
<a name="manage-your-CA-certs"></a>

本节介绍管理您自己的证书颁发机构（CA）证书的常见任务。

您可以注册您的证书颁发机构 (CA) Amazon IoT 如果你使用的是由 CA 签名的客户端证书 Amazon IoT 无法识别。

如果您希望客户自动将其客户证书注册到 Amazon IoT 当他们第一次连接时，必须向签署客户端证书的 CA 注册 Amazon IoT。否则，您不需要注册对客户端证书签发的 CA 证书。

**注意**  
在 `DEFAULT` 模式下，一个 CA 证书只能由一个区域中的一个账户注册。在 `SNI_ONLY` 模式下，一个 CA 证书可以由一个区域中的多个账户注册。

**Topics**
+ [创建 CA 证书](#create-your-CA-cert)
+ [注册 CA 证书](#register-CA-cert)
+ [停用 CA 证书](#deactivate-ca-cert)

## 创建 CA 证书
<a name="create-your-CA-cert"></a>

如果没有 CA 证书，则可以使用 [OpenSSL v1.1.1i](https://www.openssl.org/) 工具创建一个。

**注意**  
您无法在中执行此过程 Amazon IoT 控制台。

**使用创建 CA 证书 [OpenSSL v1.1.1i](https://www.openssl.org/) 工具**

1. 生成密钥对。

   ```
   openssl genrsa -out {{root_CA_key_filename.key}} 2048
   ```

1. 使用密钥对中的私有密钥生成 CA 证书。

   ```
   openssl req -x509 -new -nodes \
       -key {{root_CA_key_filename.key}} \
       -sha256 -days 1024 \
       -out {{root_CA_cert_filename.pem}}
   ```

## 注册 CA 证书
<a name="register-CA-cert"></a>

这些程序描述了如何从非亚马逊 CA 的证书颁发机构 (CA) 注册证书。 Amazon IoT Core 使用 CA 证书来验证证书的所有权。要使用由非亚马逊 CA 的 CA 签署的设备证书，您必须向注册 CA 证书 Amazon IoT Core 这样它就可以验证设备证书的所有权。

### 注册 CA 证书（控制台）
<a name="register-CA-cert-console"></a>

**注意**  
若要在控制台中注册 CA 证书，请在控制台中的 [Register CA certificate](https://console.amazonaws.cn/iot/home#/create/cacertificate)（注册 CA 证书）处开始注册。你可以在中注册你的 CA Multi-account 模式，无需提供验证证书或访问私钥。CA 可以在中注册 Multi-account 多人模式 Amazon Web Services 账户 在同一个地方 Amazon Web Services 区域。你可以在中注册你的 CA Single-account 模式：提供 CA 私钥的验证证书和所有权证明。

### 注册 CA 证书（CLI）
<a name="register-CA-cert-cli"></a>

您可以在 `DEFAULT` 模式或 `SNI_ONLY` 模式下注册 CA 证书。CA 可以在中注册 `DEFAULT` 逐一模式 Amazon Web Services 账户 合而为一 Amazon Web Services 区域。CA 可以在中注册 `SNI_ONLY` 多人模式 Amazon Web Services 账户 在同一个地方 Amazon Web Services 区域。有关 CA 证书模式的更多信息，请参阅 [certificateMode](https://docs.amazonaws.cn/iot/latest/apireference/API_CACertificateDescription.html#iot-Type-CACertificateDescription-certificateMode)。

**注意**  
我们建议您在 `SNI_ONLY` 模式下注册 CA。您无需提供验证证书或私钥访问权限，并且可以多次注册 CA Amazon Web Services 账户 在同一个地方 Amazon Web Services 区域。

#### 在 SNI\_ONLY 模式下注册 CA 证书（CLI）- 建议
<a name="register-CA-cert-SNI-cli"></a>

**先决条件**

继续操作之前，请确保电脑满足以下条件：
+ 根 CA 的证书文件（在以下示例中引用为 `{{root_CA_cert_filename.pem}}`）
+ [OpenSSL v1.1.1i](https://www.openssl.org/) 或更高版本

**要在中注册 CA 证书 `SNI_ONLY` 模式使用 Amazon CLI**

1. 向注册 CA 证书 Amazon IoT。使用 **register-ca-certificate** 命令，输入 CA 证书文件名。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [register-ca-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/register-ca-certificate.html)。

   ```
   aws iot register-ca-certificate \
       --ca-certificate file://{{root_CA_cert_filename.pem}} \
       --certificate-mode {{SNI_ONLY}}
   ```

   如果成功，则此命令返回 {{certificateId}}。

1. 此时，CA 证书已注册到 Amazon IoT 但处于非活动状态。CA 证书必须处于活跃状态，然后您才能注册由其签发的任何客户端证书。

   此步骤将激活 CA 证书。

   要激活 CA 证书，请使用 **update-certificate** 命令，如下所示。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [update-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/update-certificate.html)。

   ```
   aws iot update-ca-certificate \
       --certificate-id {{certificateId}} \
       --new-status ACTIVE
   ```

要查看 CA 证书的状态，请使用 **describe-ca-certificate** 命令。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [describe-ca-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/describe-ca-certificate.html)。

#### 在中注册 CA 证书 `默认` 模式 (CLI)
<a name="register-CA-cert-default-cli"></a>

**先决条件**

继续操作之前，请确保电脑满足以下条件：
+ 根 CA 的证书文件（在以下示例中引用为 `{{root_CA_cert_filename.pem}}`）
+ 根 CA 证书的私有密钥文件（在以下示例中引用为 `{{root_CA_key_filename.key}}`）
+ [OpenSSL v1.1.1i](https://www.openssl.org/) 或更高版本

**要在中注册 CA 证书 `默认` 模式使用 Amazon CLI**

1. 从中获取注册码 Amazon IoT，使用 **get-registration-code**。保存返回的 `registrationCode`，将其用作私有密钥验证证书的 `Common Name`。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [get-registration-code](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/get-registration-code.html)。

   ```
   aws iot get-registration-code
   ```

1. 为私有密钥验证证书生成密钥对：

   ```
   openssl genrsa -out {{verification_cert_key_filename.key}} 2048
   ```

1. 为私有密钥验证证书创建证书签名请求（CSR）。将证书的 `Common Name` 字段设置为 **get-registration-code** 返回的 `registrationCode`。

   ```
   openssl req -new \
       -key {{verification_cert_key_filename.key}} \
       -out {{verification_cert_csr_filename.csr}}
   ```

   将提示您输入一些信息，包括证书的`Common Name`。

   ```
   You are about to be asked to enter information that will be incorporated
   into your certificate request.
   What you are about to enter is what is called a Distinguished Name or a DN.
   There are quite a few fields but you can leave some blank
   For some fields there will be a default value,
   If you enter '.', the field will be left blank.
   -----
   Country Name (2 letter code) [AU]:
       State or Province Name (full name) []:
       Locality Name (for example, city) []:
       Organization Name (for example, company) []:
       Organizational Unit Name (for example, section) []:
       Common Name (e.g. server FQDN or YOUR name) []:{{your_registration_code}}
       Email Address []:
   
       Please enter the following 'extra' attributes
       to be sent with your certificate request
       A challenge password []:
       An optional company name []:
   ```

1. 使用 CSR 创建私有密钥验证证书：

   ```
   openssl x509 -req \
       -in {{verification_cert_csr_filename.csr}} \
       -CA {{root_CA_cert_filename.pem}} \
       -CAkey {{root_CA_key_filename.key}} \
       -CAcreateserial \
       -out {{verification_cert_filename.pem}} \
       -days 500 -sha256
   ```

1. 向注册 CA 证书 Amazon IoT。将 CA 证书文件名和私有密钥验证证书文件名传递给 **register-ca-certificate** 命令，如下所示：有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [register-ca-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/register-ca-certificate.html)。

   ```
   aws iot register-ca-certificate \
       --ca-certificate file://{{root_CA_cert_filename.pem}} \
       --verification-cert file://{{verification_cert_filename.pem}}
   ```

   此命令返回 {{certificateId}}，如果成功的话。

1. 此时，CA 证书已注册到 Amazon IoT 但不活跃。CA 证书必须处于活跃状态，然后您才能注册由其签发的任何客户端证书。

   此步骤将激活 CA 证书。

   要激活 CA 证书，请使用 **update-certificate** 命令，如下所示。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [update-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/update-certificate.html)。

   ```
   aws iot update-ca-certificate \
       --certificate-id {{certificateId}} \
       --new-status ACTIVE
   ```

要查看 CA 证书的状态，请使用 **describe-ca-certificate** 命令。有关更多信息，请参阅 *Amazon CLI 命令参考*中的 [describe-ca-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/describe-ca-certificate.html)。

### 创建 CA 验证证书以在控制台中注册 CA 证书
<a name="create-CA-verification-cert"></a>

**注意**  
此过程仅在您从中注册 CA 证书时使用 Amazon IoT 控制台。  
如果你不是从那里来这个程序的 Amazon IoT 控制台，在控制台中启动 CA 证书注册流程，地址为 [注册 CA 证书](https://console.amazonaws.cn/iot/home#/create/cacertificate)。

继续操作之前，请确保同一台电脑满足以下条件：
+ 根 CA 的证书文件（在以下示例中引用为 `{{root_CA_cert_filename.pem}}`）
+ 根 CA 证书的私有密钥文件（在以下示例中引用为 `{{root_CA_key_filename.key}}`）
+ [OpenSSL v1.1.1i](https://www.openssl.org/) 或更高版本

**使用命令行界面创建 CA 验证证书，以在控制台中注册 CA 证书**

1. 将 `{{verification_cert_key_filename.key}}` 替换为要创建的验证证书密钥文件的文件名（例如 **verification\_cert.key**）。然后运行此命令，为私有密钥验证证书生成密钥对：

   ```
   openssl genrsa -out {{verification_cert_key_filename.key}} 2048
   ```

1. 将 `{{verification_cert_key_filename.key}}` 替换为在步骤 1 中创建的密钥文件的名称。

   将 `{{verification_cert_csr_filename.csr}}` 替换为要创建的证书签名请求（CSR）文件的名称。例如 **verification\_cert.csr**。

   运行此命令以创建 CSR 文件。

   ```
   openssl req -new \
       -key {{verification_cert_key_filename.key}} \
       -out {{verification_cert_csr_filename.csr}}
   ```

   该命令会提示您输入其他信息（稍后说明）。

1. 在 Amazon IoT 控制台，在 **验证证书** 容器，复制注册码。

1. **openssl** 命令提示您输入的信息显示在以下示例中。除了 `Common Name` 字段，您可以在其它字段中输入自己的值或将其留空。

   在 `Common Name` 字段中，粘贴您在上一步骤中复制的注册码。

   ```
   You are about to be asked to enter information that will be incorporated
   into your certificate request.
   What you are about to enter is what is called a Distinguished Name or a DN.
   There are quite a few fields but you can leave some blank
   For some fields there will be a default value,
   If you enter '.', the field will be left blank.
   -----
   Country Name (2 letter code) [AU]:
       State or Province Name (full name) []:
       Locality Name (for example, city) []:
       Organization Name (for example, company) []:
       Organizational Unit Name (for example, section) []:
       Common Name (e.g. server FQDN or YOUR name) []:{{your_registration_code}}
       Email Address []:
   
       Please enter the following 'extra' attributes
       to be sent with your certificate request
       A challenge password []:
       An optional company name []:
   ```

   完成后，命令会创建 CSR 文件。

1. 将 `{{verification_cert_csr_filename.csr}}` 替换为在上一步骤中使用的 `{{verification_cert_csr_filename.csr}}`。

   将 `{{root_CA_cert_filename.pem}}` 替换为要注册的 CA 证书的文件名。

   将 `{{root_CA_key_filename.key}}` 替换为 CA 证书的私有密钥文件的文件名。

   将 `{{verification_cert_filename.pem}}` 替换为要创建的验证证书的文件名。例如 **verification\_cert.pem**。

   ```
   openssl x509 -req \
       -in {{verification_cert_csr_filename.csr}} \
       -CA {{root_CA_cert_filename.pem}} \
       -CAkey {{root_CA_key_filename.key}} \
       -CAcreateserial \
       -out {{verification_cert_filename.pem}} \
       -days 500 -sha256
   ```

1. 完成 OpenSSL 命令后，您应该准备好这些文件，以便在返回控制台时使用。
   + 您的 CA 证书文件（上一条命令中使用的 `{{root_CA_cert_filename.pem}}`）
   + 您在上一步中创建的验证证书 ({{verification\_cert\_filename.pem}} 在前面的命令中使用）

## 停用 CA 证书
<a name="deactivate-ca-cert"></a>

当证书颁发机构 (CA) 证书启用自动客户端证书注册时， Amazon IoT 检查 CA 证书以确保 CA 是 `ACTIVE`。如果 CA 证书是 `INACTIVE`， Amazon IoT 不允许注册客户证书。

通过将 CA 证书设置为 `INACTIVE`，可防止该 CA 颁发的任何新客户端证书自动注册。

**注意**  
除非您明确吊销由受损的 CA 证书签发的每个已注册客户端证书，否则所有此类证书均将继续工作。

### 停用 CA 证书（控制台）
<a name="deactivate-ca-cert-console"></a>

**要停用 CA 证书，请使用 Amazon IoT 控制台**

1. 登录 Amazon Web Services 管理控制台 然后打开 [Amazon IoT 控制台](https://console.amazonaws.cn/iot/home)。

1. 在左侧的导航窗格中，依次选择**安全**、**CA**。

1. 在证书颁发机构列表中，找到要停用的证书颁发机构，然后选择省略号图标以打开选项菜单。

1. 在选项菜单上，选择**停用**。

证书颁发机构应在列表中显示为**停用**。

**注意**  
这个 Amazon IoT 控制台不提供列出由您停用的 CA 签署的证书的方法。有关列出这些证书的 Amazon CLI 选项，请参阅 [停用 CA 证书（CLI）](#deactivate-ca-cert-cli)。

### 停用 CA 证书（CLI）
<a name="deactivate-ca-cert-cli"></a>

这个 Amazon CLI 提供了 [**update-ca-certificate**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/update-ca-certificate.html) 命令停用 CA 证书。

```
aws iot update-ca-certificate \
    --certificate-id {{certificateId}} \
    --new-status INACTIVE
```

使用 [**list-certificates-by-ca**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/list-certificates-by-ca.html) 命令获取已由指定 CA 签名的所有已注册客户端证书的列表。对于由指定 CA 证书签名的每个客户端证书，请使用 [**update-certificate**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/update-certificate.html) 命令吊销该客户端证书以避免使用它。

使用 [**describe-ca-certificate**](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/describe-ca-certificate.html) 命令查看 CA 证书的状态。