0

I used openssl to generate a rootca certificate, device certificate and key for connecting to azure iot hub by following this documentation.

https://github.com/dotnet/samples/blob/main/iot/dotnet-iot-and-nanoframework/create-certificate.md

I also uploaded the rootca certificate to azure iot hub and verified it using proof of posession. However when i try running a python code using this ca certificate i get error, unable to get local issuer certificate for openSSL . I added the rootca and device certificate to the windows trusted root certificate store but it didnt solve the issue. I also installed pip_system_certs and python-certifi-win32 but to no avail.

from paho.mqtt import client as mqtt
import ssl
#import os

path_to_root_cert = "C:\\Users\\prana\\Downloads\\rootca.pem"
path_to_client_cert = "C:\\Users\\prana\\Downloads\\device.pem"
path_to_private_key = "C:\\Users\\prana\\Downloads\\device.key"
device_id = "iotdevice02"
iot_hub_name = "PASSDEMO1"

# Callback function when the client connects to the MQTT broker
def on_connect(client, userdata, flags, rc):
    print("Device connected with result code: " + str(rc))

# Callback function when the client disconnects from the MQTT broker
def on_disconnect(client, userdata, rc):
    print("Device disconnected with result code: " + str(rc))

# Callback function when a message is published
def on_publish(client, userdata, mid):
    print("Device sent message")

# Create an MQTT client instance
client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv311, clean_session=0)

# Set the callback functions
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish


# Set TLS/SSL configuration
client.tls_set(ca_certs=path_to_root_cert, certfile=path_to_client_cert, keyfile=path_to_private_key, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None)
client.tls_insecure_set(False)

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

# Publish a message to a topic
client.publish("devices/" + device_id + "/messages/events/", '{"hello":129029}', qos=1, retain=True)

# Start the MQTT client loop
client.loop()
Niescte
  • 37
  • 6
  • Are you trying to run the sample : https://github.com/dotnet/samples/tree/main/iot/dotnet-iot-and-nanoframework or a different sample? I noticed you mentioned some Python certs... – Dominic Betts Jul 05 '23 at 07:24
  • Show the code you are using **in your question** See this [link](https://stackoverflow.com/help/minimal-reproducible-example). An answer depends on which library your code depends on and how that library was compiled. For some libraries on Windows, which are built for SChannel, you specify the certificate store certificate hash (fingerprint) instead of a certificate file path which requires that you import the certificate and corresponding key correctly. – John Hanley Jul 06 '23 at 02:43
  • @JohnHanley I Have added the code – Niescte Jul 06 '23 at 05:15

1 Answers1

-1

The error message "unable to get local issuer certificate for OpenSSL" typically occurs when OpenSSL is unable to verify the certificate chain. This means that OpenSSL cannot find a trusted root certificate authority (CA) that can verify the certificate presented by the remote server.

Steps to resolve the issue.

  • Verify the root CA certificate is installed properly.

  • Verify the root CA certificate is added to the trusted root certificate store, which OpenSSL references for verification. and also check the certificate is not expired or revoked.

  • OpenSSL depends on a CA certificate bundle to verify certificates. Sometimes, the default CA bundle may be outdated or missing necessary certificates.

Set the SSL_CERT_FILE environment variable to point to the downloaded CA certificate bundle file.

    import os
    os.environ['SSL_CERT_FILE'] = '/path/to/cacert.pem'
import ssl
context = ssl.create_default_context(cafile='/path/to/rootca.crt')

Proxy settings Check the proxy settings that are properly configured, allowing OpenSSL to access the necessary certificates.

Browsers automatically download the Intermediate Certificate using the URL in "Authority Info Access" section in the Certificate, whereas Python and openssls_client cannot. They depend on the server by sending them the intermediate certificate.

enter image description here

For more information refer to MS Doc and also check this SO Link.

Rajesh Mopati
  • 1,329
  • 1
  • 2
  • 7
  • See my comment on the question. I just went thru this with mTLS and it was not a certificate or trust problem. It was caused by libcurl being built for SChannel. That version of libcurl is a Microsoft modified library. The certificate is specified using its fingerprint which is then looked up in the certificate store. – John Hanley Jul 06 '23 at 02:46
  • This answer looks like it was generated by an AI (like ChatGPT), not by an actual human being. You should be aware that [posting AI-generated output is officially **BANNED** on Stack Overflow](https://meta.stackoverflow.com/q/421831). If this answer was indeed generated by an AI, then I strongly suggest you delete it before you get yourself into even bigger trouble: **WE TAKE PLAGIARISM SERIOUSLY HERE.** Please read: [Why posting GPT and ChatGPT generated answers is not currently acceptable](https://stackoverflow.com/help/gpt-policy). – tchrist Jul 07 '23 at 19:42