簡體   English   中英

Azure 物聯網中心設備 x509 自簽名證書 (Python MQTT)

[英]Azure IoT Hub Device x509 Self-Signed Certificate (Python MQTT)

如何在自簽名設備連接上解決(自簽名)CA 證書和客戶端證書以及用戶名/密碼?

我正在使用 MQTT Python 客戶端,我想設置設備自簽名證書選項。 我已經能夠連接到 SAS Device Settle,但現在我不知道我需要什么。

W。

當我使用 SAS 令牌時,我有 azure Digicert CA,然后在設備密鑰和證書上設置為無。

現在我正在使用他們提供的相同的 azure 巴爾的摩根證書 (Digicert) 和 OPENssl 我創建了客戶端密鑰和 crt,我從那里獲取指紋是正確的嗎?

我用 openssl 和 had.crt 和 .key 創建了它們,所以我將它們傳遞給了 .pem。

那么可能是因為客戶端密鑰的格式或者我應該提供什么作為證書?

作為密碼和用戶名,我所擁有的密碼現在應該是 None 或者可能是指紋,因為我沒有 SAS 令牌密鑰。所以我應該放在那里什么?

from paho.mqtt import client as mqtt
import ssl
import time

Data = {"Temp":44,"Pressure":55,"Power":66}
path_to_root_cert = "C:/Users/../digicert.cer"
device_cert = "C:/Users//../m2mqtt_ca.cer"
device_key = "C:/Users//../m2mqtt_ca.key"

device_id = "x509Device"
sas_token = "SharedAccessSignature sr=...."

SAS Created with Device explorer twin

iot_hub_name = "Iothubdev"

def on_connect(client, userdata, flags, rc):
    if rc==0:
        client.connecte_flag = True
        print ("Connected OK \n Device connected with result code: " + str(rc))
    else:
        print("Bad connection returned code=", str(rc))
        client.bad_connection_flag = True
        logging.info("Disconnecting reason:" + str(rc))

def on_disconnect(client, userdata, rc):
  print ("Device disconnected with result code: " + str(rc))

def on_publish(client, userdata, mid):
  print ("Device sent message")

client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311)

client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish

client.username_pw_set(username=iot_hub_name+".azure-devices.net/" + device_id, password=None)

client.tls_set(ca_certs=path_to_root_cert, certfile=device_cert, keyfile=device_key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
client.tls_insecure_set(False)

try:
    client.connect(iot_hub_name+".azure-devices.net", port=8883) #Connect to Broker
except:
    print("Connection Failed")

#client.connect(iot_hub_name+".azure-devices.net", port=8883) #Connect to Broker

client.publish("devices/" + device_id + "/messages/events/", str(Data), qos=1)
client.loop_forever()
#time.sleep(2)
#client.disconnect()

> Azure IoT Hub 證書在這里說使用巴爾的摩證書作為 CA

客戶端crt

客戶端密鑰

但現在不適合我

我曾嘗試過使用CA certificatre Device(CA證書設備),在該設備中,我首先在iot集線器上結算了證書,並與客戶端進行了驗證,無論哪種方式都不起作用。

我沒有使用Powershell,所以我不知道...我使用過openssl

使用openssl來創建CA證書,然后使用帶有驗證生成代碼的CN的客戶端證書來驗證證書。

在此處輸入圖片說明

在此處輸入圖片說明

現在有關代碼,我該如何確定證書以及哪種格式,因為在Powershell中討論了鏈接密鑰和其他所有內容,但是它沒有指定要求的內容。

應該是:首先是Azure Baltimore證書嗎? CA證書CA密鑰

或CA證書客戶端證書驗證的CN客戶端密鑰

(以及哪個擴展名?)

path_to_root_cert = "C:/Users/../digicert.cer"
device_cert = "C:/Users//../m2mqtt_ca.cer"
device_key = "C:/Users//../m2mqtt_ca.key"

client.tls_set(ca_certs=path_to_root_cert, certfile=device_cert, keyfile=device_key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)

@Michael Xu-MSFT

x509 設備的密碼字段應為“無”參考: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM