Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/cryptography/x509/general_name.py: 43%

165 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:36 +0000

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 

6import abc 

7import ipaddress 

8import typing 

9from email.utils import parseaddr 

10 

11from cryptography.x509.name import Name 

12from cryptography.x509.oid import ObjectIdentifier 

13 

14_IPAddressTypes = typing.Union[ 

15 ipaddress.IPv4Address, 

16 ipaddress.IPv6Address, 

17 ipaddress.IPv4Network, 

18 ipaddress.IPv6Network, 

19] 

20 

21 

22class UnsupportedGeneralNameType(Exception): 

23 pass 

24 

25 

26class GeneralName(metaclass=abc.ABCMeta): 

27 @property 

28 @abc.abstractmethod 

29 def value(self) -> typing.Any: 

30 """ 

31 Return the value of the object 

32 """ 

33 

34 

35class RFC822Name(GeneralName): 

36 def __init__(self, value: str) -> None: 

37 if isinstance(value, str): 

38 try: 

39 value.encode("ascii") 

40 except UnicodeEncodeError: 

41 raise ValueError( 

42 "RFC822Name values should be passed as an A-label string. " 

43 "This means unicode characters should be encoded via " 

44 "a library like idna." 

45 ) 

46 else: 

47 raise TypeError("value must be string") 

48 

49 name, address = parseaddr(value) 

50 if name or not address: 

51 # parseaddr has found a name (e.g. Name <email>) or the entire 

52 # value is an empty string. 

53 raise ValueError("Invalid rfc822name value") 

54 

55 self._value = value 

56 

57 @property 

58 def value(self) -> str: 

59 return self._value 

60 

61 @classmethod 

62 def _init_without_validation(cls, value: str) -> "RFC822Name": 

63 instance = cls.__new__(cls) 

64 instance._value = value 

65 return instance 

66 

67 def __repr__(self) -> str: 

68 return f"<RFC822Name(value={self.value!r})>" 

69 

70 def __eq__(self, other: object) -> bool: 

71 if not isinstance(other, RFC822Name): 

72 return NotImplemented 

73 

74 return self.value == other.value 

75 

76 def __hash__(self) -> int: 

77 return hash(self.value) 

78 

79 

80class DNSName(GeneralName): 

81 def __init__(self, value: str) -> None: 

82 if isinstance(value, str): 

83 try: 

84 value.encode("ascii") 

85 except UnicodeEncodeError: 

86 raise ValueError( 

87 "DNSName values should be passed as an A-label string. " 

88 "This means unicode characters should be encoded via " 

89 "a library like idna." 

90 ) 

91 else: 

92 raise TypeError("value must be string") 

93 

94 self._value = value 

95 

96 @property 

97 def value(self) -> str: 

98 return self._value 

99 

100 @classmethod 

101 def _init_without_validation(cls, value: str) -> "DNSName": 

102 instance = cls.__new__(cls) 

103 instance._value = value 

104 return instance 

105 

106 def __repr__(self) -> str: 

107 return f"<DNSName(value={self.value!r})>" 

108 

109 def __eq__(self, other: object) -> bool: 

110 if not isinstance(other, DNSName): 

111 return NotImplemented 

112 

113 return self.value == other.value 

114 

115 def __hash__(self) -> int: 

116 return hash(self.value) 

117 

118 

119class UniformResourceIdentifier(GeneralName): 

120 def __init__(self, value: str) -> None: 

121 if isinstance(value, str): 

122 try: 

123 value.encode("ascii") 

124 except UnicodeEncodeError: 

125 raise ValueError( 

126 "URI values should be passed as an A-label string. " 

127 "This means unicode characters should be encoded via " 

128 "a library like idna." 

129 ) 

130 else: 

131 raise TypeError("value must be string") 

132 

133 self._value = value 

134 

135 @property 

136 def value(self) -> str: 

137 return self._value 

138 

139 @classmethod 

140 def _init_without_validation( 

141 cls, value: str 

142 ) -> "UniformResourceIdentifier": 

143 instance = cls.__new__(cls) 

144 instance._value = value 

145 return instance 

146 

147 def __repr__(self) -> str: 

148 return f"<UniformResourceIdentifier(value={self.value!r})>" 

149 

150 def __eq__(self, other: object) -> bool: 

151 if not isinstance(other, UniformResourceIdentifier): 

152 return NotImplemented 

153 

154 return self.value == other.value 

155 

156 def __hash__(self) -> int: 

157 return hash(self.value) 

158 

159 

160class DirectoryName(GeneralName): 

161 def __init__(self, value: Name) -> None: 

162 if not isinstance(value, Name): 

163 raise TypeError("value must be a Name") 

164 

165 self._value = value 

166 

167 @property 

168 def value(self) -> Name: 

169 return self._value 

170 

171 def __repr__(self) -> str: 

172 return f"<DirectoryName(value={self.value})>" 

173 

174 def __eq__(self, other: object) -> bool: 

175 if not isinstance(other, DirectoryName): 

176 return NotImplemented 

177 

178 return self.value == other.value 

179 

180 def __hash__(self) -> int: 

181 return hash(self.value) 

182 

183 

184class RegisteredID(GeneralName): 

185 def __init__(self, value: ObjectIdentifier) -> None: 

186 if not isinstance(value, ObjectIdentifier): 

187 raise TypeError("value must be an ObjectIdentifier") 

188 

189 self._value = value 

190 

191 @property 

192 def value(self) -> ObjectIdentifier: 

193 return self._value 

194 

195 def __repr__(self) -> str: 

196 return f"<RegisteredID(value={self.value})>" 

197 

198 def __eq__(self, other: object) -> bool: 

199 if not isinstance(other, RegisteredID): 

200 return NotImplemented 

201 

202 return self.value == other.value 

203 

204 def __hash__(self) -> int: 

205 return hash(self.value) 

206 

207 

208class IPAddress(GeneralName): 

209 def __init__(self, value: _IPAddressTypes) -> None: 

210 if not isinstance( 

211 value, 

212 ( 

213 ipaddress.IPv4Address, 

214 ipaddress.IPv6Address, 

215 ipaddress.IPv4Network, 

216 ipaddress.IPv6Network, 

217 ), 

218 ): 

219 raise TypeError( 

220 "value must be an instance of ipaddress.IPv4Address, " 

221 "ipaddress.IPv6Address, ipaddress.IPv4Network, or " 

222 "ipaddress.IPv6Network" 

223 ) 

224 

225 self._value = value 

226 

227 @property 

228 def value(self) -> _IPAddressTypes: 

229 return self._value 

230 

231 def _packed(self) -> bytes: 

232 if isinstance( 

233 self.value, (ipaddress.IPv4Address, ipaddress.IPv6Address) 

234 ): 

235 return self.value.packed 

236 else: 

237 return ( 

238 self.value.network_address.packed + self.value.netmask.packed 

239 ) 

240 

241 def __repr__(self) -> str: 

242 return f"<IPAddress(value={self.value})>" 

243 

244 def __eq__(self, other: object) -> bool: 

245 if not isinstance(other, IPAddress): 

246 return NotImplemented 

247 

248 return self.value == other.value 

249 

250 def __hash__(self) -> int: 

251 return hash(self.value) 

252 

253 

254class OtherName(GeneralName): 

255 def __init__(self, type_id: ObjectIdentifier, value: bytes) -> None: 

256 if not isinstance(type_id, ObjectIdentifier): 

257 raise TypeError("type_id must be an ObjectIdentifier") 

258 if not isinstance(value, bytes): 

259 raise TypeError("value must be a binary string") 

260 

261 self._type_id = type_id 

262 self._value = value 

263 

264 @property 

265 def type_id(self) -> ObjectIdentifier: 

266 return self._type_id 

267 

268 @property 

269 def value(self) -> bytes: 

270 return self._value 

271 

272 def __repr__(self) -> str: 

273 return "<OtherName(type_id={}, value={!r})>".format( 

274 self.type_id, self.value 

275 ) 

276 

277 def __eq__(self, other: object) -> bool: 

278 if not isinstance(other, OtherName): 

279 return NotImplemented 

280 

281 return self.type_id == other.type_id and self.value == other.value 

282 

283 def __hash__(self) -> int: 

284 return hash((self.type_id, self.value))