Skip to content

Commit 098431f

Browse files
authored
Merge pull request #79 from brentru/check-pem-begin-statement
Check PEM file header text in set_certificate/set_private_key
2 parents 602250a + 2a234e7 commit 098431f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,17 @@ def get_time(self):
794794
def set_certificate(self, client_certificate):
795795
"""Sets client certificate. Must be called
796796
BEFORE a network connection is established.
797-
Begins with -----BEGIN CERTIFICATE-----.
798-
:param str client_certificate: User-provided X.509 certificate up to 1300 bytes.
797+
:param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
799798
"""
800799
if self._debug:
801800
print("** Setting client certificate")
802801
if self.status == WL_CONNECTED:
803802
raise RuntimeError("set_certificate must be called BEFORE a connection is established.")
804803
if isinstance(client_certificate, str):
805804
client_certificate = bytes(client_certificate, 'utf-8')
806-
assert len(client_certificate) < 1300, "X.509 certificate must be less than 1300 bytes."
805+
if "-----BEGIN CERTIFICATE" not in client_certificate:
806+
raise TypeError(".PEM must start with -----BEGIN CERTIFICATE")
807+
assert len(client_certificate) < 1300, ".PEM must be less than 1300 bytes."
807808
resp = self._send_command_get_response(_SET_CLI_CERT, (client_certificate,))
808809
if resp[0][0] != 1:
809810
raise RuntimeError("Failed to set client certificate")
@@ -813,15 +814,17 @@ def set_certificate(self, client_certificate):
813814
def set_private_key(self, private_key):
814815
"""Sets private key. Must be called
815816
BEFORE a network connection is established.
816-
:param str private_key: User-provided private key up to 1700 bytes.
817+
:param str private_key: User-provided .PEM file up to 1700 bytes.
817818
"""
818819
if self._debug:
819820
print("** Setting client's private key.")
820821
if self.status == WL_CONNECTED:
821822
raise RuntimeError("set_private_key must be called BEFORE a connection is established.")
822823
if isinstance(private_key, str):
823824
private_key = bytes(private_key, 'utf-8')
824-
assert len(private_key) < 1700, "Private key must be less than 1700 bytes."
825+
if "-----BEGIN RSA" not in private_key:
826+
raise TypeError(".PEM must start with -----BEGIN RSA")
827+
assert len(private_key) < 1700, ".PEM must be less than 1700 bytes."
825828
resp = self._send_command_get_response(_SET_PK, (private_key,))
826829
if resp[0][0] != 1:
827830
raise RuntimeError("Failed to set private key.")

0 commit comments

Comments
 (0)