Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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 

5 

6from cryptography import utils 

7 

8 

9class ObjectIdentifier(object): 

10 def __init__(self, dotted_string: str): 

11 self._dotted_string = dotted_string 

12 

13 nodes = self._dotted_string.split(".") 

14 intnodes = [] 

15 

16 # There must be at least 2 nodes, the first node must be 0..2, and 

17 # if less than 2, the second node cannot have a value outside the 

18 # range 0..39. All nodes must be integers. 

19 for node in nodes: 

20 try: 

21 node_value = int(node, 10) 

22 except ValueError: 

23 raise ValueError( 

24 "Malformed OID: %s (non-integer nodes)" 

25 % (self._dotted_string) 

26 ) 

27 if node_value < 0: 

28 raise ValueError( 

29 "Malformed OID: %s (negative-integer nodes)" 

30 % (self._dotted_string) 

31 ) 

32 intnodes.append(node_value) 

33 

34 if len(nodes) < 2: 

35 raise ValueError( 

36 "Malformed OID: %s (insufficient number of nodes)" 

37 % (self._dotted_string) 

38 ) 

39 

40 if intnodes[0] > 2: 

41 raise ValueError( 

42 "Malformed OID: %s (first node outside valid range)" 

43 % (self._dotted_string) 

44 ) 

45 

46 if intnodes[0] < 2 and intnodes[1] >= 40: 

47 raise ValueError( 

48 "Malformed OID: %s (second node outside valid range)" 

49 % (self._dotted_string) 

50 ) 

51 

52 def __eq__(self, other): 

53 if not isinstance(other, ObjectIdentifier): 

54 return NotImplemented 

55 

56 return self.dotted_string == other.dotted_string 

57 

58 def __ne__(self, other): 

59 return not self == other 

60 

61 def __repr__(self): 

62 return "<ObjectIdentifier(oid={}, name={})>".format( 

63 self.dotted_string, self._name 

64 ) 

65 

66 def __hash__(self): 

67 return hash(self.dotted_string) 

68 

69 @property 

70 def _name(self): 

71 # Lazy import to avoid an import cycle 

72 from cryptography.x509.oid import _OID_NAMES 

73 

74 return _OID_NAMES.get(self, "Unknown OID") 

75 

76 dotted_string = utils.read_only_property("_dotted_string")