Skip to content

Commit 379eed6

Browse files
authored
Merge pull request #77 from brentru/add-certificate-handlers
Add user-defined certificate and private key handlers to ESP32SPI
2 parents 83fe4b3 + e14f3ef commit 379eed6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
_SET_ENT_UNAME_CMD = const(0x4B)
9595
_SET_ENT_PASSWD_CMD = const(0x4C)
9696
_SET_ENT_ENABLE_CMD = const(0x4F)
97+
_SET_CLI_CERT = const(0x40)
98+
_SET_PK = const(0x41)
9799

98100
_SET_PIN_MODE_CMD = const(0x50)
99101
_SET_DIGITAL_WRITE_CMD = const(0x51)
@@ -786,3 +788,38 @@ def get_time(self):
786788
if self.status in (WL_AP_LISTENING, WL_AP_CONNECTED):
787789
raise RuntimeError("Cannot obtain NTP while in AP mode, must be connected to internet")
788790
raise RuntimeError("Must be connected to WiFi before obtaining NTP.")
791+
792+
def set_certificate(self, client_certificate):
793+
"""Sets client certificate. Must be called
794+
BEFORE a network connection is established.
795+
Begins with -----BEGIN CERTIFICATE-----.
796+
:param str client_certificate: User-provided X.509 certificate up to 1300 bytes.
797+
"""
798+
if self._debug:
799+
print("** Setting client certificate")
800+
if self.status == WL_CONNECTED:
801+
raise RuntimeError("set_certificate must be called BEFORE a connection is established.")
802+
if isinstance(client_certificate, str):
803+
client_certificate = bytes(client_certificate, 'utf-8')
804+
assert len(client_certificate) < 1300, "X.509 certificate must be less than 1300 bytes."
805+
resp = self._send_command_get_response(_SET_CLI_CERT, (client_certificate,))
806+
if resp[0][0] != 1:
807+
raise RuntimeError("Failed to set client certificate")
808+
return resp[0]
809+
810+
def set_private_key(self, private_key):
811+
"""Sets private key. Must be called
812+
BEFORE a network connection is established.
813+
:param str private_key: User-provided private key up to 1700 bytes.
814+
"""
815+
if self._debug:
816+
print("** Setting client's private key.")
817+
if self.status == WL_CONNECTED:
818+
raise RuntimeError("set_private_key must be called BEFORE a connection is established.")
819+
if isinstance(private_key, str):
820+
private_key = bytes(private_key, 'utf-8')
821+
assert len(private_key) < 1700, "Private key must be less than 1700 bytes."
822+
resp = self._send_command_get_response(_SET_PK, (private_key,))
823+
if resp[0][0] != 1:
824+
raise RuntimeError("Failed to set private key.")
825+
return resp[0]

0 commit comments

Comments
 (0)