

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

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

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

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

如果您希望客户端在首次连接 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 控制台中执行此过程。

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

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，而无需提供验证证书或访问私有密钥的权限。在多账户模式下，多个 Amazon Web Services 账户 可以在同一个 Amazon Web Services 区域中注册 CA。您可以通过提供验证证书和 CA 私有密钥的所有权证明，在单账户模式下注册您的 CA。

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

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

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

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

**先决条件**

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

**要在`SNI_ONLY`模式下注册 CA 证书，请使用 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)。

#### 在 `DEFAULT` 模式下注册 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/) 或更高版本

**要在`DEFAULT`模式下注册 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>

**注意**  
此过程仅适用于从 Amazon IoT 控制台注册 CA 证书的情况。  
如果您不是从控制台进入此过程，请在 Amazon IoT 控制台的 Register 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\$1cert.key**）。然后运行此命令，为私有密钥验证证书生成密钥对：

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

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

   将 `verification_cert_csr_filename.csr` 替换为要创建的证书签名请求（CSR）文件的名称。例如 **verification\$1cert.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\$1cert.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\$1cert\$1filename.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. 在左侧导航窗格中，选择 “**安全**”，选择**CAs**。

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

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

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

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

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

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

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

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

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