

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

# 使用 CloudFront Connection Function 和 KVS 撤銷
<a name="revocation-connection-function-kvs"></a>

您可以結合 CloudFront Connection Functions 與 KeyValueStore，實作交互 TLS 身分驗證的憑證撤銷檢查。此方法提供可擴展的即時憑證撤銷機制，可補充 CloudFront 的內建憑證驗證。

Connection Functions 是在 CloudFront 節點建立 TLS 連線期間執行的 JavaScript 函數，可讓您實作 mTLS 身分驗證的自訂憑證驗證邏輯。如需連線函數的詳細資訊，請參閱 [關聯 CloudFront 連線函數](connection-functions.md)。

## 憑證撤銷如何與 Connection Functions 搭配使用
<a name="how-revocation-works"></a>

CloudFront 的標準憑證驗證會驗證憑證鏈、簽章和過期，但不包含內建憑證撤銷檢查。透過使用連線函數，您可以在 TLS 交握期間實作自訂撤銷檢查。

憑證撤銷程序的運作方式如下：

1. 將撤銷的憑證序號儲存在 CloudFront KeyValueStore 中。

1. 當用戶端提供憑證時，會叫用您的連線函數。

1. 函數會根據 KeyValueStore 檢查憑證的序號。

1. 如果在存放區中找到序號，則會撤銷憑證。

1. 您的函數拒絕已撤銷憑證的連線。

此方法提供跨 CloudFront 全球邊緣網路near-real-time的撤銷檢查。

## 設定已撤銷憑證的 KeyValueStore
<a name="setup-kvs-revoked-certs"></a>

首先，建立 KeyValueStore 以存放已撤銷憑證的序號：

### 建立 KeyValueStore （主控台）
<a name="create-kvs-console"></a>

1. 登入 AWS 管理主控台 並開啟位於 的 CloudFront 主控台[https://console.aws.amazon.com/cloudfront/v4/home](https://console.aws.amazon.com/cloudfront/v4/home)。

1. 在導覽窗格中，選擇**鍵值存放**區。

1. 選擇**建立索引鍵值存放區**。

1. 輸入金鑰值存放區的名稱 （例如，已撤銷憑證）。

1. (選用) 新增描述。

1. 選擇**建立索引鍵值存放區**。

### 建立 KeyValueStore (AWS CLI)
<a name="create-kvs-cli"></a>

下列範例示範如何建立 KeyValueStore：

```
aws cloudfront create-key-value-store \
  --name "revoked-certificates" \
  --comment "Store for revoked certificate serial numbers"
```

## 匯入已撤銷的憑證序號
<a name="import-revoked-serials"></a>

建立 KeyValueStore 之後，您需要匯入已撤銷憑證的序號：

### 準備撤銷資料
<a name="prepare-revocation-data"></a>

使用您撤銷的憑證序號建立 JSON 檔案：

```
{
  "data": [
    {
      "key": "ABC123DEF456",
      "value": ""
    },
    {
      "key": "789XYZ012GHI",
      "value": ""
    }
  ]
}
```

### 從 S3 匯入
<a name="import-from-s3"></a>

1. 將 JSON 檔案上傳至 S3 儲存貯體

1. 將檔案匯入至 KeyValueStore：

   ```
   aws cloudfront create-key-value-store \
     --name "revoked-certificates" \
     --import-source '{
       "SourceType": "S3",
       "SourceARN": "arn:aws:s3:::amzn-s3-demo-bucket1/revoked-serials.json"
     }'
   ```

## 建立用於撤銷檢查的連線函數
<a name="create-revocation-connection-function"></a>

建立連線函數，以檢查憑證序號與 KeyValueStore：

### Connection Function 程式碼範例
<a name="revocation-function-example"></a>

下列範例顯示執行憑證撤銷檢查的連線函數：

```
import cf from 'cloudfront';

async function connectionHandler(connection) {
    const kvsHandle = cf.kvs();
    
    // Get client certificate serial number
    const clientSerialNumber = connection.clientCertificate.certificates.leaf.serialNumber;
    
    // Check if the serial number exists in the KeyValueStore
    const isRevoked = await kvsHandle.exists(clientSerialNumber.replaceAll(':', ''));
    
    if (isRevoked) {
        console.log(`Certificate ${clientSerialNumber} is revoked. Denying connection.`);
        connection.logCustomData(`REVOKED:${clientSerialNumber}`);
        connection.deny();
    } else {
        console.log(`Certificate ${clientSerialNumber} is valid. Allowing connection.`);
        connection.allow();
    }
    
}
```

### 建立連線函數 (AWS CLI)
<a name="create-revocation-function-cli"></a>

下列範例示範如何建立與 KeyValueStore 關聯的連線函數：

```
aws cloudfront create-connection-function \
  --name "revocation-checker" \
  --connection-function-config '{
      "Comment": "Certificate revocation checking function",
      "Runtime": "cloudfront-js-2.0",
      "KeyValueStoreAssociations": {
          "Quantity": 1,
          "Items": [
              {
                  "KeyValueStoreARN": "arn:aws:cloudfront::123456789012:key-value-store/revoked-certificates"
              }
          ]
      }
  }' \
  --connection-function-code fileb://revocation-checker.js
```

## 將函數與您的分佈建立關聯
<a name="associate-revocation-function"></a>

建立和發佈連線函數之後，請將其與啟用 mTLS 的 CloudFront 分佈建立關聯，如 [關聯 CloudFront 連線函數](connection-functions.md)一節中所述。