您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

68 行
2.1 KiB

  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. from cryptography import utils
  6. class ObjectIdentifier(object):
  7. def __init__(self, dotted_string):
  8. self._dotted_string = dotted_string
  9. nodes = self._dotted_string.split(".")
  10. intnodes = []
  11. # There must be at least 2 nodes, the first node must be 0..2, and
  12. # if less than 2, the second node cannot have a value outside the
  13. # range 0..39. All nodes must be integers.
  14. for node in nodes:
  15. try:
  16. intnodes.append(int(node, 0))
  17. except ValueError:
  18. raise ValueError(
  19. "Malformed OID: %s (non-integer nodes)" % (
  20. self._dotted_string))
  21. if len(nodes) < 2:
  22. raise ValueError(
  23. "Malformed OID: %s (insufficient number of nodes)" % (
  24. self._dotted_string))
  25. if intnodes[0] > 2:
  26. raise ValueError(
  27. "Malformed OID: %s (first node outside valid range)" % (
  28. self._dotted_string))
  29. if intnodes[0] < 2 and intnodes[1] >= 40:
  30. raise ValueError(
  31. "Malformed OID: %s (second node outside valid range)" % (
  32. self._dotted_string))
  33. def __eq__(self, other):
  34. if not isinstance(other, ObjectIdentifier):
  35. return NotImplemented
  36. return self.dotted_string == other.dotted_string
  37. def __ne__(self, other):
  38. return not self == other
  39. def __repr__(self):
  40. return "<ObjectIdentifier(oid={}, name={})>".format(
  41. self.dotted_string,
  42. self._name
  43. )
  44. def __hash__(self):
  45. return hash(self.dotted_string)
  46. @property
  47. def _name(self):
  48. # Lazy import to avoid an import cycle
  49. from cryptography.x509.oid import _OID_NAMES
  50. return _OID_NAMES.get(self, "Unknown OID")
  51. dotted_string = utils.read_only_property("_dotted_string")