security0.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # APIs for interpreting and creating protobuf packets for
  16. # protocomm endpoint with security type protocomm_security0
  17. from __future__ import print_function
  18. from future.utils import tobytes
  19. import utils
  20. import proto
  21. from .security import *
  22. class Security0(Security):
  23. def __init__(self, verbose):
  24. # Initialize state of the security1 FSM
  25. self.session_state = 0
  26. self.verbose = verbose
  27. Security.__init__(self, self.security0_session)
  28. def security0_session(self, response_data):
  29. # protocomm security0 FSM which interprets/forms
  30. # protobuf packets according to present state of session
  31. if (self.session_state == 0):
  32. self.session_state = 1
  33. return self.setup0_request()
  34. if (self.session_state == 1):
  35. self.setup0_response(response_data)
  36. return None
  37. def setup0_request(self):
  38. # Form protocomm security0 request packet
  39. setup_req = proto.session_pb2.SessionData()
  40. setup_req.sec_ver = 0
  41. session_cmd = proto.sec0_pb2.S0SessionCmd()
  42. setup_req.sec0.sc.MergeFrom(session_cmd)
  43. return setup_req.SerializeToString().decode('latin-1')
  44. def setup0_response(self, response_data):
  45. # Interpret protocomm security0 response packet
  46. setup_resp = proto.session_pb2.SessionData()
  47. setup_resp.ParseFromString(tobytes(response_data))
  48. # Check if security scheme matches
  49. if setup_resp.sec_ver != proto.session_pb2.SecScheme0:
  50. print("Incorrect sec scheme")
  51. def encrypt_data(self, data):
  52. # Passive. No encryption when security0 used
  53. return data
  54. def decrypt_data(self, data):
  55. # Passive. No encryption when security0 used
  56. return data