

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 管理您的憑證授權機構憑證
<a name="manage-your-CA-certs"></a>

本節說明管理您自己的憑證授權機構 (CA) 憑證的一般工作。

 AWS IoT 如果您使用的用戶端憑證是由 AWS IoT 無法辨識的 CA 簽署，則可以向 註冊憑證授權機構 (CA)。

如果您希望用戶端在第一次連線 AWS IoT 時自動向 註冊其用戶端憑證，則必須向簽署用戶端憑證的 CA 註冊 AWS IoT。否則，您不需要註冊已簽署用戶端憑證的憑證授權機構憑證。

**注意**  
一個 CA 憑證只能由一個區域中的一個帳戶在 `DEFAULT` 模式下註冊。一個 CA 憑證可以由一個區域中的多個帳戶在 `SNI_ONLY` 模式下註冊。

**Topics**
+ [建立憑證授權機構憑證](#create-your-CA-cert)
+ [註冊您的憑證授權機構憑證](#register-CA-cert)
+ [停用憑證授權機構憑證](#deactivate-ca-cert)

## 建立憑證授權機構憑證
<a name="create-your-CA-cert"></a>

如果沒有 憑證授權機構憑證，您可以使用 [OpenSSL v1.1.1i](https://www.openssl.org/) 工具來建立憑證授權機構憑證。

**注意**  
您無法在 AWS IoT 主控台中執行此程序。

**使用 [OpenSSL v1.1.1i](https://www.openssl.org/) 工具來建立憑證授權機構憑證**

1. 產生金鑰對。

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

1. 透過該金鑰對的私有金鑰來產生憑證授權機構憑證。

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

## 註冊您的憑證授權機構憑證
<a name="register-CA-cert"></a>

這些程序說明如何從不是 Amazon CA 的憑證授權單位 (CA) 註冊憑證。 AWS IoT Core 會使用 CA 憑證來驗證憑證的擁有權。若要使用由非 Amazon CA 的 CA 簽署的裝置憑證，您必須向 註冊 CA 憑證， AWS IoT Core 以便驗證裝置憑證的擁有權。

### 註冊憑證授權機構憑證 (主控台)。
<a name="register-CA-cert-console"></a>

**注意**  
若要在主控台中註冊憑證授權機構憑證，請在主控台中的[註冊憑證授權機構憑證](https://console.aws.amazon.com//iot/home#/create/cacertificate)開始。您可在多帳戶模式下註冊您的 CA，而無需提供驗證憑證或私有金鑰的存取權。一個 CA 可由相同 AWS 區域中的多個 AWS 帳戶 以多帳戶模式進行註冊。您可透過提供驗證憑證和 CA 私有金鑰的擁有權證明，以單一帳戶模式註冊您的 CA。

### 註冊憑證授權機構憑證 (CLI)
<a name="register-CA-cert-cli"></a>

您可以在 `DEFAULT` 模式或 `SNI_ONLY` 模式下註冊 CA 憑證。CA 可以在`DEFAULT`模式中以一個 AWS 帳戶 註冊 AWS 區域。CA 可以由相同 AWS 帳戶 中的多個 以 `SNI_ONLY` 模式註冊 AWS 區域。如需 CA 憑證模式的詳細資訊，請參閱 [certificateMode](https://docs.aws.amazon.com//iot/latest/apireference/API_CACertificateDescription.html#iot-Type-CACertificateDescription-certificateMode)。

**注意**  
我們建議您在 `SNI_ONLY` 模式下註冊 CA。您不需要提供驗證憑證或存取私有金鑰，而且您可以在相同 AWS 帳戶 中透過多個 註冊 CA AWS 區域。

#### 在 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/) 或更新版本

**使用 在 `SNI_ONLY` 模式下註冊 CA 憑證 AWS CLI**

1. 向 註冊 CA 憑證 AWS IoT。使用 **register-ca-certificate** 命令，輸入 CA 憑證檔案名稱。如需詳細資訊，請參閱 *AWS 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 憑證已向 註冊， AWS IoT 但處於非作用中狀態。CA 憑證必須處於作用中狀態，您才能註冊已簽署的任何用戶端憑證。

   這個步驟會啟動 CA 憑證。

   若要啟用該 CA 憑證，請按如下所示使用 **update-certificate** 命令。如需詳細資訊，請參閱 *AWS 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** 命令。如需詳細資訊，請參閱 *AWS 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 憑證 AWS CLI**

1. 若要從 取得註冊碼 AWS IoT，請使用 **get-registration-code**。將傳回的 `registrationCode` 儲存為私有金鑰驗證憑證的 `Common Name`。如需詳細資訊，請參閱 *AWS 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 憑證 AWS IoT。將 CA 憑證檔案名稱和私有金鑰驗證憑證檔案名稱傳遞給 **register-ca-certificate** 命令，如下所示。如需詳細資訊，請參閱 *AWS 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 憑證已向 註冊， AWS IoT 但未處於作用中狀態。CA 憑證必須處於作用中狀態，您才能註冊已簽署的任何用戶端憑證。

   這個步驟會啟動 CA 憑證。

   若要啟用該 CA 憑證，請按如下所示使用 **update-certificate** 命令。如需詳細資訊，請參閱 *AWS 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** 命令。如需詳細資訊，請參閱 *AWS CLI 命令參考*中的 [describe-ca-certificate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iot/describe-ca-certificate.html)。

### 建立憑證授權機構驗證憑證以在主控台中註冊憑證授權機構憑證
<a name="create-CA-verification-cert"></a>

**注意**  
此程序僅適用於從 AWS IoT 主控台註冊 CA 憑證的情況。  
如果您不是從 AWS IoT 主控台前往此程序，請在註冊 CA 憑證的主控台中啟動 [CA 憑證註冊](https://console.aws.amazon.com//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. 在 AWS 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 命令完成後，您應準備好這些檔案，以供返回主控台時使用。
   + 您的憑證授權機構憑證檔案 (在上一個命令中使用的 `{{root_CA_cert_filename.pem}}`)
   + 您在上一個步驟中建立的驗證憑證 (用於上一個命令中的 {{verification\_cert\_filename.pem}})

## 停用憑證授權機構憑證
<a name="deactivate-ca-cert"></a>

當憑證授權機構 (CA) 憑證啟用自動用戶端憑證註冊時， 會 AWS IoT 檢查 CA 憑證，以確保 CA 為 `ACTIVE`。如果 CA 憑證為 `INACTIVE`， AWS IoT 則不允許註冊用戶端憑證。

藉由將 CA 憑證設定為 `INACTIVE`，您可防止自動註冊 CA 所核發的任何新用戶端憑證。

**注意**  
由有風險之憑證授權機構憑證簽署的任何已註冊裝置憑證將持續運作，直到您確實逐一撤銷這些憑證為止。

### 停用憑證授權機構憑證 (主控台)
<a name="deactivate-ca-cert-console"></a>

**使用 AWS IoT 主控台停用 CA 憑證**

1. 登入 AWS 管理主控台 並開啟 [AWS IoT 主控台](https://console.aws.amazon.com/iot/home)。

1. 請在左側導覽窗格中，選擇 **Secure** (安全)、**CAs** (CA)。

1. 於憑證授權單位清單中，找出您要停用的憑證授權單位，接著選擇省略號圖示開啟選項選單。

1. 在選項選單上，選擇 **Deactivate (停用)**。

憑證授權機構應在清單中顯示為 **Inactive (非作用中)**。

**注意**  
 AWS IoT 主控台不提供列出您停用之 CA 簽署的憑證的方法。如需列出這些憑證的 AWS CLI 選項，請參閱 [停用憑證授權機構憑證 (CLI)](#deactivate-ca-cert-cli)。

### 停用憑證授權機構憑證 (CLI)
<a name="deactivate-ca-cert-cli"></a>

 AWS CLI 提供 [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)命令來停用 CA 憑證。

```
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 簽署的所有已註冊裝置憑證清單。針對由指定憑證授權機構憑證所簽署的每一裝置憑證，請使用 [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 憑證的狀態。