單元 7:模擬硬體安全整合 - AWS IoT Greengrass

AWS IoT Greengrass Version 1 於 2023 年 6 月 30 日進入延長使用壽命階段。如需詳細資訊,請參閱AWS IoT Greengrass V1 維護政策。在此日期之後, AWS IoT Greengrass V1 將不會發行提供功能、增強功能、錯誤修正或安全性修補程式的更新。在上運行的設備 AWS IoT Greengrass V1 不會中斷,並將繼續運行並連接到雲。我們強烈建議您移轉至 AWS IoT Greengrass Version 2,這會增加重要的新功能,並支援其他平台

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

單元 7:模擬硬體安全整合

這項功能於AWS IoT Greengrass核心 v1.7 及更高版本。

這個進階單元會示範如何設定模擬的硬體安全模組 (HSM),以與 Greengrass 核心搭配使用。組態會使用 SoftHSM,即為使用 PKCS#11 應用程式設計界面 (API) 的純軟體實作。本單元的目的是讓您設定環境,以在其中學習,並對 PKCS#11 API 的純軟體實作進行初始測試。僅供學習和初始測試之用,不可用於任何類型的正式生產。

您可以使用此組態,以實驗使用 PKCS#11 相容服務來存放您的私有金鑰。如需純軟體實作的詳細資訊,請參閱 SoftHSM。如需將硬體安全整合到AWS IoT Greengrass核心(包括一般要求),請參閲硬體安全整合

重要

本單元僅供實驗之用。我們強烈建議您不要在生產環境中使用 SoftHSM,因為這可能會產生額外安全的錯覺。產生的組態並不會提供任何實際的安全優勢。在 SoftHSM 存放金鑰此一方法,不會比 Greengrass 環境中私密儲存的任何其他方法更安全。

本單元的目的是要讓您了解 PKCS#11 規格,並在您計劃未來使用真正的硬體型 HSM 時,執行軟體的初始測試。

您必須在正式生產前,對您未來的硬體實作進行個別、完整的測試,因為 SoftHSM 中提供的 PKCS#11 實作與硬體型實作之間可能有差異。

如果您需要協助以納入支援的硬體安全模組,請聯絡您的AWS企業 Support 代表。

開始之前,請先執行 Greengrass 裝置安裝指令碼,或確定您已完成入門教學課程的單元 1單元 2。在本單元中,我們假設您的核心已完成佈建且與AWS。此單元需約 30 分鐘完成。

安裝 SoftHSM 軟體

在此步驟中,您會安裝 SoftHSM 和 pkcs11 工具,以用來管理您的 SoftHSM 執行個體。

  • 在您的AWS IoT Greengrass核心裝置,請執行下列命令:

    sudo apt-get install softhsm2 libsofthsm2-dev pkcs11-dump

    如需這些套件的詳細資訊,請參閱安裝 softhsm2安裝 libsofthsm2-dev安裝 pkcs11-dump

    注意

    在系統上使用此命令時,如果遇到問題,請參閱 GitHub 上的 SoftHSM 第 2 版。此網站提供更多安裝資訊,包括如何從來源建置。

設定 SoftHSM

在此步驟中,您要設定 SoftHSM

  1. 切換至根使用者。

    sudo su
  2. 使用手冊頁面尋找全系統的 softhsm2.conf 位置。常見的位置為 /etc/softhsm/softhsm2.conf,但在某些系統上的位置可能會不同。

    man softhsm2.conf
  3. 在全系統位置中為 softhsm2 組態檔案建立目錄。此範例假設位置為 /etc/softhsm/softhsm2.conf

    mkdir -p /etc/softhsm
  4. /greengrass 目錄中建立字符目錄。

    注意

    如果略過此步驟,softhsm2-util 會報告 ERROR: Could not initialize the library

    mkdir -p /greengrass/softhsm2/tokens
  5. 設定字符目錄。

    echo "directories.tokendir = /greengrass/softhsm2/tokens" > /etc/softhsm/softhsm2.conf
  6. 設定檔案型後端。

    echo "objectstore.backend = file" >> /etc/softhsm/softhsm2.conf
注意

這些組態設定僅供實驗用。若要查看所有組態選項,請參閱組態檔的手冊頁面。

man softhsm2.conf

將私有金鑰匯入至 SoftHSM

在此步驟中,您要初始化 SoftHSM 字符、轉換私有金鑰格式,然後匯入私有金鑰。

  1. 初始化 SoftHSM 字符。

    softhsm2-util --init-token --slot 0 --label greengrass --so-pin 12345 --pin 1234
    注意

    出現提示時,請輸入 12345 的 SO PIN 和 1234 的使用者 PIN。AWS IoT Greengrass 不會使用 SO (監督員) PIN,所以您可以使用任何值。

    如果您收到錯誤訊息 CKR_SLOT_ID_INVALID: Slot 0 does not exist,請改用下列命令:

    softhsm2-util --init-token --free --label greengrass --so-pin 12345 --pin 1234
  2. 將私有金鑰轉換為 SoftHSM 匯入工具可以使用的格式。在本教學課程中,您轉換從預設羣組建立選項單元 2入門教學。

    openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in hash.private.key -out hash.private.pem
  3. 將私有金鑰匯入至 SoftHSM。根據您的 softhsm2-util 版本,僅執行以下其中一個命令。

    Raspbian softhsm2-util v2.2.0 語法
    softhsm2-util --import hash.private.pem --token greengrass --label iotkey --id 0000 --pin 12340
    Ubuntu softhsm2-util v2.0.0 語法
    softhsm2-util --import hash.private.pem --slot 0 --label iotkey --id 0000 --pin 1234

    此命令會將插槽識別為 0,並將金鑰標籤定義為 iotkey。您會在下一節中使用這些值。

匯入私有金鑰之後,您可以選擇將其從 /greengrass/certs 目錄中移除。務必將根 CA 和裝置憑證保留在目錄中。

設定 Greengrass 核心以使用 SoftHSM

在此步驟中,您會修改 Greengrass 核心組態檔以使用 SoftHSM。

  1. 在您的系統上尋找 SoftHSM 供應商程式庫 (libsofthsm2.so) 的路徑:

    1. 取得程式庫的已安裝套件清單。

      sudo dpkg -L libsofthsm2

      libsofthsm2.so 檔案位於 softhsm 目錄中。

    2. 複製檔案的完整路徑 (例如 /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so)。您稍後會使用此值。

  2. 停止 Greengrass 協助程式。

    cd /greengrass/ggc/core/ sudo ./greengrassd stop
  3. 開啟 Greengrass 組態檔。這是config.json文件中/greengrass/config目錄。

    注意

    這個過程中的示例是假設config.json文件使用的格式是從預設羣組建立選項單元 2入門教學。

  4. crypto.principals 物件中,插入下列 MQTT 伺服器憑證物件。視需要插入逗號以建立有效的 JSON 檔案。

    "MQTTServerCertificate": { "privateKeyPath": "path-to-private-key" }
  5. crypto 物件中,插入下列 PKCS11 物件。視需要插入逗號以建立有效的 JSON 檔案。

    "PKCS11": { "P11Provider": "/path-to-pkcs11-provider-so", "slotLabel": "crypto-token-name", "slotUserPin": "crypto-token-user-pin" }

    您的檔案應會如下所示:

    { "coreThing" : { "caPath" : "root.ca.pem", "certPath" : "hash.cert.pem", "keyPath" : "hash.private.key", "thingArn" : "arn:partition:iot:region:account-id:thing/core-thing-name", "iotHost" : "host-prefix.iot.region.amazonaws.com", "ggHost" : "greengrass.iot.region.amazonaws.com", "keepAlive" : 600 }, "runtime" : { "cgroup" : { "useSystemd" : "yes" } }, "managedRespawn" : false, "crypto": { "PKCS11": { "P11Provider": "/path-to-pkcs11-provider-so", "slotLabel": "crypto-token-name", "slotUserPin": "crypto-token-user-pin" }, "principals" : { "MQTTServerCertificate": { "privateKeyPath": "path-to-private-key" }, "IoTCertificate" : { "privateKeyPath" : "file:///greengrass/certs/hash.private.key", "certificatePath" : "file:///greengrass/certs/hash.cert.pem" }, "SecretsManager" : { "privateKeyPath" : "file:///greengrass/certs/hash.private.key" } }, "caPath" : "file:///greengrass/certs/root.ca.pem" } }
    注意

    使用 over-the-air (OTA) 更新以及硬體安全的PKCS11對象還必須包含OpenSSLEngine屬性。如需詳細資訊,請參閱 設定支援 over-the-air 更新

  6. 編輯 crypto 物件:

    1. 設定 PKCS11 物件。

      • 針對 P11Provider,輸入 libsofthsm2.so 的完整路徑。

      • 針對 slotLabel,輸入 greengrass

      • 針對 slotUserPin,輸入 1234

    2. principals 物件中設定私有金鑰路徑。請勿編輯 certificatePath 屬性。

      • 針對 privateKeyPath 屬性,請輸入下列 RFC 7512 PKCS#11 路徑 (指定金鑰的標籤)。對 IoTCertificateSecretsManagerMQTTServerCertificate 委託人執行此動作。

        pkcs11:object=iotkey;type=private
    3. 檢查 crypto 物件。其看起來與下列類似:

      "crypto": { "PKCS11": { "P11Provider": "/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so", "slotLabel": "greengrass", "slotUserPin": "1234" }, "principals": { "MQTTServerCertificate": { "privateKeyPath": "pkcs11:object=iotkey;type=private" }, "SecretsManager": { "privateKeyPath": "pkcs11:object=iotkey;type=private" }, "IoTCertificate": { "certificatePath": "file://certs/core.crt", "privateKeyPath": "pkcs11:object=iotkey;type=private" } }, "caPath": "file://certs/root.ca.pem" }
  7. coreThing 物件移除 caPathcertPathkeyPath 值。其看起來與下列類似:

    "coreThing" : { "thingArn" : "arn:partition:iot:region:account-id:thing/core-thing-name", "iotHost" : "host-prefix-ats.iot.region.amazonaws.com", "ggHost" : "greengrass-ats.iot.region.amazonaws.com", "keepAlive" : 600 }
注意

在本教學課程中,您會對所有委託人指定相同的私有金鑰。如需選擇本機 MQTT 伺服器之私有金鑰的詳細資訊,請參閱效能。如需本機 Secrets Manager 的詳細資訊,請參閱 將私密部署至 AWS IoT Greengrass 核心

測試組態

  • 啟動 Greengrass 協助程式。

    cd /greengrass/ggc/core/ sudo ./greengrassd start

    如果協助程式啟動成功,則表示您的核心已正確設定。

    您現在可以開始了解 PKCS#11 規格,並使用 SoftHSM 實作所提供的 PKCS#11 API 執行初始測試。

    重要

    再次提醒您,本單元僅供學習和測試之用,這點非常重要。這其實不會提高 Greengrass 環境的安全狀態。

    反之,單元的目的是讓您可以開始學習和測試,以準備將來使用真正的硬體型 HSM。屆時,在正式生產之前,您必須針對硬體型 HSM 個別且完整地測試您的軟體,因為相較於硬體型實作,SoftHSM 中提供的 PKCS#11 實作之間可能有差異。

另請參閱