security0.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. import proto
  19. from future.utils import tobytes
  20. from .security import Security
  21. class Security0(Security):
  22. def __init__(self, verbose):
  23. # Initialize state of the security1 FSM
  24. self.session_state = 0
  25. self.verbose = verbose
  26. Security.__init__(self, self.security0_session)
  27. def security0_session(self, response_data):
  28. # protocomm security0 FSM which interprets/forms
  29. # protobuf packets according to present state of session
  30. if (self.session_state == 0):
  31. self.session_state = 1
  32. return self.setup0_request()
  33. if (self.session_state == 1):
  34. self.setup0_response(response_data)
  35. return None
  36. def setup0_request(self):
  37. # Form protocomm security0 request packet
  38. setup_req = proto.session_pb2.SessionData()
  39. setup_req.sec_ver = 0
  40. session_cmd = proto.sec0_pb2.S0SessionCmd()
  41. setup_req.sec0.sc.MergeFrom(session_cmd)
  42. return setup_req.SerializeToString().decode('latin-1')
  43. def setup0_response(self, response_data):
  44. # Interpret protocomm security0 response packet
  45. setup_resp = proto.session_pb2.SessionData()
  46. setup_resp.ParseFromString(tobytes(response_data))
  47. # Check if security scheme matches
  48. if setup_resp.sec_ver != proto.session_pb2.SecScheme0:
  49. print('Incorrect sec scheme')
  50. def encrypt_data(self, data):
  51. # Passive. No encryption when security0 used
  52. return data
  53. def decrypt_data(self, data):
  54. # Passive. No encryption when security0 used
  55. return data