Skip to content

Check PEM file header text in set_certificate/set_private_key #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,16 +794,17 @@ def get_time(self):
def set_certificate(self, client_certificate):
"""Sets client certificate. Must be called
BEFORE a network connection is established.
Begins with -----BEGIN CERTIFICATE-----.
:param str client_certificate: User-provided X.509 certificate up to 1300 bytes.
:param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
"""
if self._debug:
print("** Setting client certificate")
if self.status == WL_CONNECTED:
raise RuntimeError("set_certificate must be called BEFORE a connection is established.")
if isinstance(client_certificate, str):
client_certificate = bytes(client_certificate, 'utf-8')
assert len(client_certificate) < 1300, "X.509 certificate must be less than 1300 bytes."
if "-----BEGIN CERTIFICATE" not in client_certificate:
raise TypeError(".PEM must start with -----BEGIN CERTIFICATE")
assert len(client_certificate) < 1300, ".PEM must be less than 1300 bytes."
resp = self._send_command_get_response(_SET_CLI_CERT, (client_certificate,))
if resp[0][0] != 1:
raise RuntimeError("Failed to set client certificate")
Expand All @@ -813,15 +814,17 @@ def set_certificate(self, client_certificate):
def set_private_key(self, private_key):
"""Sets private key. Must be called
BEFORE a network connection is established.
:param str private_key: User-provided private key up to 1700 bytes.
:param str private_key: User-provided .PEM file up to 1700 bytes.
"""
if self._debug:
print("** Setting client's private key.")
if self.status == WL_CONNECTED:
raise RuntimeError("set_private_key must be called BEFORE a connection is established.")
if isinstance(private_key, str):
private_key = bytes(private_key, 'utf-8')
assert len(private_key) < 1700, "Private key must be less than 1700 bytes."
if "-----BEGIN RSA" not in private_key:
raise TypeError(".PEM must start with -----BEGIN RSA")
assert len(private_key) < 1700, ".PEM must be less than 1700 bytes."
resp = self._send_command_get_response(_SET_PK, (private_key,))
if resp[0][0] != 1:
raise RuntimeError("Failed to set private key.")
Expand Down