name.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. import binascii
  5. import re
  6. import sys
  7. import typing
  8. import warnings
  9. from cryptography import utils
  10. from cryptography.hazmat.bindings._rust import x509 as rust_x509
  11. from cryptography.x509.oid import NameOID, ObjectIdentifier
  12. class _ASN1Type(utils.Enum):
  13. BitString = 3
  14. OctetString = 4
  15. UTF8String = 12
  16. NumericString = 18
  17. PrintableString = 19
  18. T61String = 20
  19. IA5String = 22
  20. UTCTime = 23
  21. GeneralizedTime = 24
  22. VisibleString = 26
  23. UniversalString = 28
  24. BMPString = 30
  25. _ASN1_TYPE_TO_ENUM = {i.value: i for i in _ASN1Type}
  26. _NAMEOID_DEFAULT_TYPE: typing.Dict[ObjectIdentifier, _ASN1Type] = {
  27. NameOID.COUNTRY_NAME: _ASN1Type.PrintableString,
  28. NameOID.JURISDICTION_COUNTRY_NAME: _ASN1Type.PrintableString,
  29. NameOID.SERIAL_NUMBER: _ASN1Type.PrintableString,
  30. NameOID.DN_QUALIFIER: _ASN1Type.PrintableString,
  31. NameOID.EMAIL_ADDRESS: _ASN1Type.IA5String,
  32. NameOID.DOMAIN_COMPONENT: _ASN1Type.IA5String,
  33. }
  34. # Type alias
  35. _OidNameMap = typing.Mapping[ObjectIdentifier, str]
  36. _NameOidMap = typing.Mapping[str, ObjectIdentifier]
  37. #: Short attribute names from RFC 4514:
  38. #: https://tools.ietf.org/html/rfc4514#page-7
  39. _NAMEOID_TO_NAME: _OidNameMap = {
  40. NameOID.COMMON_NAME: "CN",
  41. NameOID.LOCALITY_NAME: "L",
  42. NameOID.STATE_OR_PROVINCE_NAME: "ST",
  43. NameOID.ORGANIZATION_NAME: "O",
  44. NameOID.ORGANIZATIONAL_UNIT_NAME: "OU",
  45. NameOID.COUNTRY_NAME: "C",
  46. NameOID.STREET_ADDRESS: "STREET",
  47. NameOID.DOMAIN_COMPONENT: "DC",
  48. NameOID.USER_ID: "UID",
  49. }
  50. _NAME_TO_NAMEOID = {v: k for k, v in _NAMEOID_TO_NAME.items()}
  51. def _escape_dn_value(val: typing.Union[str, bytes]) -> str:
  52. """Escape special characters in RFC4514 Distinguished Name value."""
  53. if not val:
  54. return ""
  55. # RFC 4514 Section 2.4 defines the value as being the # (U+0023) character
  56. # followed by the hexadecimal encoding of the octets.
  57. if isinstance(val, bytes):
  58. return "#" + binascii.hexlify(val).decode("utf8")
  59. # See https://tools.ietf.org/html/rfc4514#section-2.4
  60. val = val.replace("\\", "\\\\")
  61. val = val.replace('"', '\\"')
  62. val = val.replace("+", "\\+")
  63. val = val.replace(",", "\\,")
  64. val = val.replace(";", "\\;")
  65. val = val.replace("<", "\\<")
  66. val = val.replace(">", "\\>")
  67. val = val.replace("\0", "\\00")
  68. if val[0] in ("#", " "):
  69. val = "\\" + val
  70. if val[-1] == " ":
  71. val = val[:-1] + "\\ "
  72. return val
  73. def _unescape_dn_value(val: str) -> str:
  74. if not val:
  75. return ""
  76. # See https://tools.ietf.org/html/rfc4514#section-3
  77. # special = escaped / SPACE / SHARP / EQUALS
  78. # escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  79. def sub(m):
  80. val = m.group(1)
  81. # Regular escape
  82. if len(val) == 1:
  83. return val
  84. # Hex-value scape
  85. return chr(int(val, 16))
  86. return _RFC4514NameParser._PAIR_RE.sub(sub, val)
  87. class NameAttribute:
  88. def __init__(
  89. self,
  90. oid: ObjectIdentifier,
  91. value: typing.Union[str, bytes],
  92. _type: typing.Optional[_ASN1Type] = None,
  93. *,
  94. _validate: bool = True,
  95. ) -> None:
  96. if not isinstance(oid, ObjectIdentifier):
  97. raise TypeError(
  98. "oid argument must be an ObjectIdentifier instance."
  99. )
  100. if _type == _ASN1Type.BitString:
  101. if oid != NameOID.X500_UNIQUE_IDENTIFIER:
  102. raise TypeError(
  103. "oid must be X500_UNIQUE_IDENTIFIER for BitString type."
  104. )
  105. if not isinstance(value, bytes):
  106. raise TypeError("value must be bytes for BitString")
  107. else:
  108. if not isinstance(value, str):
  109. raise TypeError("value argument must be a str")
  110. if (
  111. oid == NameOID.COUNTRY_NAME
  112. or oid == NameOID.JURISDICTION_COUNTRY_NAME
  113. ):
  114. assert isinstance(value, str)
  115. c_len = len(value.encode("utf8"))
  116. if c_len != 2 and _validate is True:
  117. raise ValueError(
  118. "Country name must be a 2 character country code"
  119. )
  120. elif c_len != 2:
  121. warnings.warn(
  122. "Country names should be two characters, but the "
  123. "attribute is {} characters in length.".format(c_len),
  124. stacklevel=2,
  125. )
  126. # The appropriate ASN1 string type varies by OID and is defined across
  127. # multiple RFCs including 2459, 3280, and 5280. In general UTF8String
  128. # is preferred (2459), but 3280 and 5280 specify several OIDs with
  129. # alternate types. This means when we see the sentinel value we need
  130. # to look up whether the OID has a non-UTF8 type. If it does, set it
  131. # to that. Otherwise, UTF8!
  132. if _type is None:
  133. _type = _NAMEOID_DEFAULT_TYPE.get(oid, _ASN1Type.UTF8String)
  134. if not isinstance(_type, _ASN1Type):
  135. raise TypeError("_type must be from the _ASN1Type enum")
  136. self._oid = oid
  137. self._value = value
  138. self._type = _type
  139. @property
  140. def oid(self) -> ObjectIdentifier:
  141. return self._oid
  142. @property
  143. def value(self) -> typing.Union[str, bytes]:
  144. return self._value
  145. @property
  146. def rfc4514_attribute_name(self) -> str:
  147. """
  148. The short attribute name (for example "CN") if available,
  149. otherwise the OID dotted string.
  150. """
  151. return _NAMEOID_TO_NAME.get(self.oid, self.oid.dotted_string)
  152. def rfc4514_string(
  153. self, attr_name_overrides: typing.Optional[_OidNameMap] = None
  154. ) -> str:
  155. """
  156. Format as RFC4514 Distinguished Name string.
  157. Use short attribute name if available, otherwise fall back to OID
  158. dotted string.
  159. """
  160. attr_name = (
  161. attr_name_overrides.get(self.oid) if attr_name_overrides else None
  162. )
  163. if attr_name is None:
  164. attr_name = self.rfc4514_attribute_name
  165. return f"{attr_name}={_escape_dn_value(self.value)}"
  166. def __eq__(self, other: object) -> bool:
  167. if not isinstance(other, NameAttribute):
  168. return NotImplemented
  169. return self.oid == other.oid and self.value == other.value
  170. def __hash__(self) -> int:
  171. return hash((self.oid, self.value))
  172. def __repr__(self) -> str:
  173. return "<NameAttribute(oid={0.oid}, value={0.value!r})>".format(self)
  174. class RelativeDistinguishedName:
  175. def __init__(self, attributes: typing.Iterable[NameAttribute]):
  176. attributes = list(attributes)
  177. if not attributes:
  178. raise ValueError("a relative distinguished name cannot be empty")
  179. if not all(isinstance(x, NameAttribute) for x in attributes):
  180. raise TypeError("attributes must be an iterable of NameAttribute")
  181. # Keep list and frozenset to preserve attribute order where it matters
  182. self._attributes = attributes
  183. self._attribute_set = frozenset(attributes)
  184. if len(self._attribute_set) != len(attributes):
  185. raise ValueError("duplicate attributes are not allowed")
  186. def get_attributes_for_oid(
  187. self, oid: ObjectIdentifier
  188. ) -> typing.List[NameAttribute]:
  189. return [i for i in self if i.oid == oid]
  190. def rfc4514_string(
  191. self, attr_name_overrides: typing.Optional[_OidNameMap] = None
  192. ) -> str:
  193. """
  194. Format as RFC4514 Distinguished Name string.
  195. Within each RDN, attributes are joined by '+', although that is rarely
  196. used in certificates.
  197. """
  198. return "+".join(
  199. attr.rfc4514_string(attr_name_overrides)
  200. for attr in self._attributes
  201. )
  202. def __eq__(self, other: object) -> bool:
  203. if not isinstance(other, RelativeDistinguishedName):
  204. return NotImplemented
  205. return self._attribute_set == other._attribute_set
  206. def __hash__(self) -> int:
  207. return hash(self._attribute_set)
  208. def __iter__(self) -> typing.Iterator[NameAttribute]:
  209. return iter(self._attributes)
  210. def __len__(self) -> int:
  211. return len(self._attributes)
  212. def __repr__(self) -> str:
  213. return "<RelativeDistinguishedName({})>".format(self.rfc4514_string())
  214. class Name:
  215. @typing.overload
  216. def __init__(self, attributes: typing.Iterable[NameAttribute]) -> None:
  217. ...
  218. @typing.overload
  219. def __init__(
  220. self, attributes: typing.Iterable[RelativeDistinguishedName]
  221. ) -> None:
  222. ...
  223. def __init__(
  224. self,
  225. attributes: typing.Iterable[
  226. typing.Union[NameAttribute, RelativeDistinguishedName]
  227. ],
  228. ) -> None:
  229. attributes = list(attributes)
  230. if all(isinstance(x, NameAttribute) for x in attributes):
  231. self._attributes = [
  232. RelativeDistinguishedName([typing.cast(NameAttribute, x)])
  233. for x in attributes
  234. ]
  235. elif all(isinstance(x, RelativeDistinguishedName) for x in attributes):
  236. self._attributes = typing.cast(
  237. typing.List[RelativeDistinguishedName], attributes
  238. )
  239. else:
  240. raise TypeError(
  241. "attributes must be a list of NameAttribute"
  242. " or a list RelativeDistinguishedName"
  243. )
  244. @classmethod
  245. def from_rfc4514_string(
  246. cls,
  247. data: str,
  248. attr_name_overrides: typing.Optional[_NameOidMap] = None,
  249. ) -> "Name":
  250. return _RFC4514NameParser(data, attr_name_overrides or {}).parse()
  251. def rfc4514_string(
  252. self, attr_name_overrides: typing.Optional[_OidNameMap] = None
  253. ) -> str:
  254. """
  255. Format as RFC4514 Distinguished Name string.
  256. For example 'CN=foobar.com,O=Foo Corp,C=US'
  257. An X.509 name is a two-level structure: a list of sets of attributes.
  258. Each list element is separated by ',' and within each list element, set
  259. elements are separated by '+'. The latter is almost never used in
  260. real world certificates. According to RFC4514 section 2.1 the
  261. RDNSequence must be reversed when converting to string representation.
  262. """
  263. return ",".join(
  264. attr.rfc4514_string(attr_name_overrides)
  265. for attr in reversed(self._attributes)
  266. )
  267. def get_attributes_for_oid(
  268. self, oid: ObjectIdentifier
  269. ) -> typing.List[NameAttribute]:
  270. return [i for i in self if i.oid == oid]
  271. @property
  272. def rdns(self) -> typing.List[RelativeDistinguishedName]:
  273. return self._attributes
  274. def public_bytes(self, backend: typing.Any = None) -> bytes:
  275. return rust_x509.encode_name_bytes(self)
  276. def __eq__(self, other: object) -> bool:
  277. if not isinstance(other, Name):
  278. return NotImplemented
  279. return self._attributes == other._attributes
  280. def __hash__(self) -> int:
  281. # TODO: this is relatively expensive, if this looks like a bottleneck
  282. # for you, consider optimizing!
  283. return hash(tuple(self._attributes))
  284. def __iter__(self) -> typing.Iterator[NameAttribute]:
  285. for rdn in self._attributes:
  286. for ava in rdn:
  287. yield ava
  288. def __len__(self) -> int:
  289. return sum(len(rdn) for rdn in self._attributes)
  290. def __repr__(self) -> str:
  291. rdns = ",".join(attr.rfc4514_string() for attr in self._attributes)
  292. return "<Name({})>".format(rdns)
  293. class _RFC4514NameParser:
  294. _OID_RE = re.compile(r"(0|([1-9]\d*))(\.(0|([1-9]\d*)))+")
  295. _DESCR_RE = re.compile(r"[a-zA-Z][a-zA-Z\d-]*")
  296. _PAIR = r"\\([\\ #=\"\+,;<>]|[\da-zA-Z]{2})"
  297. _PAIR_RE = re.compile(_PAIR)
  298. _LUTF1 = r"[\x01-\x1f\x21\x24-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
  299. _SUTF1 = r"[\x01-\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
  300. _TUTF1 = r"[\x01-\x1F\x21\x23-\x2A\x2D-\x3A\x3D\x3F-\x5B\x5D-\x7F]"
  301. _UTFMB = rf"[\x80-{chr(sys.maxunicode)}]"
  302. _LEADCHAR = rf"{_LUTF1}|{_UTFMB}"
  303. _STRINGCHAR = rf"{_SUTF1}|{_UTFMB}"
  304. _TRAILCHAR = rf"{_TUTF1}|{_UTFMB}"
  305. _STRING_RE = re.compile(
  306. rf"""
  307. (
  308. ({_LEADCHAR}|{_PAIR})
  309. (
  310. ({_STRINGCHAR}|{_PAIR})*
  311. ({_TRAILCHAR}|{_PAIR})
  312. )?
  313. )?
  314. """,
  315. re.VERBOSE,
  316. )
  317. _HEXSTRING_RE = re.compile(r"#([\da-zA-Z]{2})+")
  318. def __init__(self, data: str, attr_name_overrides: _NameOidMap) -> None:
  319. self._data = data
  320. self._idx = 0
  321. self._attr_name_overrides = attr_name_overrides
  322. def _has_data(self) -> bool:
  323. return self._idx < len(self._data)
  324. def _peek(self) -> typing.Optional[str]:
  325. if self._has_data():
  326. return self._data[self._idx]
  327. return None
  328. def _read_char(self, ch: str) -> None:
  329. if self._peek() != ch:
  330. raise ValueError
  331. self._idx += 1
  332. def _read_re(self, pat) -> str:
  333. match = pat.match(self._data, pos=self._idx)
  334. if match is None:
  335. raise ValueError
  336. val = match.group()
  337. self._idx += len(val)
  338. return val
  339. def parse(self) -> Name:
  340. """
  341. Parses the `data` string and converts it to a Name.
  342. According to RFC4514 section 2.1 the RDNSequence must be
  343. reversed when converting to string representation. So, when
  344. we parse it, we need to reverse again to get the RDNs on the
  345. correct order.
  346. """
  347. rdns = [self._parse_rdn()]
  348. while self._has_data():
  349. self._read_char(",")
  350. rdns.append(self._parse_rdn())
  351. return Name(reversed(rdns))
  352. def _parse_rdn(self) -> RelativeDistinguishedName:
  353. nas = [self._parse_na()]
  354. while self._peek() == "+":
  355. self._read_char("+")
  356. nas.append(self._parse_na())
  357. return RelativeDistinguishedName(nas)
  358. def _parse_na(self) -> NameAttribute:
  359. try:
  360. oid_value = self._read_re(self._OID_RE)
  361. except ValueError:
  362. name = self._read_re(self._DESCR_RE)
  363. oid = self._attr_name_overrides.get(
  364. name, _NAME_TO_NAMEOID.get(name)
  365. )
  366. if oid is None:
  367. raise ValueError
  368. else:
  369. oid = ObjectIdentifier(oid_value)
  370. self._read_char("=")
  371. if self._peek() == "#":
  372. value = self._read_re(self._HEXSTRING_RE)
  373. value = binascii.unhexlify(value[1:]).decode()
  374. else:
  375. raw_value = self._read_re(self._STRING_RE)
  376. value = _unescape_dn_value(raw_value)
  377. return NameAttribute(oid, value)