_oid.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 typing
  5. from cryptography.hazmat.primitives import hashes
  6. class ObjectIdentifier:
  7. def __init__(self, dotted_string: str) -> None:
  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. node_value = int(node, 10)
  17. except ValueError:
  18. raise ValueError(
  19. f"Malformed OID: {dotted_string} (non-integer nodes)"
  20. )
  21. if node_value < 0:
  22. raise ValueError(
  23. f"Malformed OID: {dotted_string} (negative-integer nodes)"
  24. )
  25. intnodes.append(node_value)
  26. if len(nodes) < 2:
  27. raise ValueError(
  28. f"Malformed OID: {dotted_string} "
  29. "(insufficient number of nodes)"
  30. )
  31. if intnodes[0] > 2:
  32. raise ValueError(
  33. f"Malformed OID: {dotted_string} "
  34. "(first node outside valid range)"
  35. )
  36. if intnodes[0] < 2 and intnodes[1] >= 40:
  37. raise ValueError(
  38. f"Malformed OID: {dotted_string} "
  39. "(second node outside valid range)"
  40. )
  41. def __eq__(self, other: object) -> bool:
  42. if not isinstance(other, ObjectIdentifier):
  43. return NotImplemented
  44. return self.dotted_string == other.dotted_string
  45. def __repr__(self) -> str:
  46. return "<ObjectIdentifier(oid={}, name={})>".format(
  47. self.dotted_string, self._name
  48. )
  49. def __hash__(self) -> int:
  50. return hash(self.dotted_string)
  51. @property
  52. def _name(self) -> str:
  53. return _OID_NAMES.get(self, "Unknown OID")
  54. @property
  55. def dotted_string(self) -> str:
  56. return self._dotted_string
  57. class ExtensionOID:
  58. SUBJECT_DIRECTORY_ATTRIBUTES = ObjectIdentifier("2.5.29.9")
  59. SUBJECT_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.14")
  60. KEY_USAGE = ObjectIdentifier("2.5.29.15")
  61. SUBJECT_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.17")
  62. ISSUER_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.18")
  63. BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19")
  64. NAME_CONSTRAINTS = ObjectIdentifier("2.5.29.30")
  65. CRL_DISTRIBUTION_POINTS = ObjectIdentifier("2.5.29.31")
  66. CERTIFICATE_POLICIES = ObjectIdentifier("2.5.29.32")
  67. POLICY_MAPPINGS = ObjectIdentifier("2.5.29.33")
  68. AUTHORITY_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.35")
  69. POLICY_CONSTRAINTS = ObjectIdentifier("2.5.29.36")
  70. EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37")
  71. FRESHEST_CRL = ObjectIdentifier("2.5.29.46")
  72. INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54")
  73. ISSUING_DISTRIBUTION_POINT = ObjectIdentifier("2.5.29.28")
  74. AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1")
  75. SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11")
  76. OCSP_NO_CHECK = ObjectIdentifier("1.3.6.1.5.5.7.48.1.5")
  77. TLS_FEATURE = ObjectIdentifier("1.3.6.1.5.5.7.1.24")
  78. CRL_NUMBER = ObjectIdentifier("2.5.29.20")
  79. DELTA_CRL_INDICATOR = ObjectIdentifier("2.5.29.27")
  80. PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS = ObjectIdentifier(
  81. "1.3.6.1.4.1.11129.2.4.2"
  82. )
  83. PRECERT_POISON = ObjectIdentifier("1.3.6.1.4.1.11129.2.4.3")
  84. SIGNED_CERTIFICATE_TIMESTAMPS = ObjectIdentifier("1.3.6.1.4.1.11129.2.4.5")
  85. class OCSPExtensionOID:
  86. NONCE = ObjectIdentifier("1.3.6.1.5.5.7.48.1.2")
  87. class CRLEntryExtensionOID:
  88. CERTIFICATE_ISSUER = ObjectIdentifier("2.5.29.29")
  89. CRL_REASON = ObjectIdentifier("2.5.29.21")
  90. INVALIDITY_DATE = ObjectIdentifier("2.5.29.24")
  91. class NameOID:
  92. COMMON_NAME = ObjectIdentifier("2.5.4.3")
  93. COUNTRY_NAME = ObjectIdentifier("2.5.4.6")
  94. LOCALITY_NAME = ObjectIdentifier("2.5.4.7")
  95. STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8")
  96. STREET_ADDRESS = ObjectIdentifier("2.5.4.9")
  97. ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10")
  98. ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11")
  99. SERIAL_NUMBER = ObjectIdentifier("2.5.4.5")
  100. SURNAME = ObjectIdentifier("2.5.4.4")
  101. GIVEN_NAME = ObjectIdentifier("2.5.4.42")
  102. TITLE = ObjectIdentifier("2.5.4.12")
  103. GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44")
  104. X500_UNIQUE_IDENTIFIER = ObjectIdentifier("2.5.4.45")
  105. DN_QUALIFIER = ObjectIdentifier("2.5.4.46")
  106. PSEUDONYM = ObjectIdentifier("2.5.4.65")
  107. USER_ID = ObjectIdentifier("0.9.2342.19200300.100.1.1")
  108. DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25")
  109. EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1")
  110. JURISDICTION_COUNTRY_NAME = ObjectIdentifier("1.3.6.1.4.1.311.60.2.1.3")
  111. JURISDICTION_LOCALITY_NAME = ObjectIdentifier("1.3.6.1.4.1.311.60.2.1.1")
  112. JURISDICTION_STATE_OR_PROVINCE_NAME = ObjectIdentifier(
  113. "1.3.6.1.4.1.311.60.2.1.2"
  114. )
  115. BUSINESS_CATEGORY = ObjectIdentifier("2.5.4.15")
  116. POSTAL_ADDRESS = ObjectIdentifier("2.5.4.16")
  117. POSTAL_CODE = ObjectIdentifier("2.5.4.17")
  118. INN = ObjectIdentifier("1.2.643.3.131.1.1")
  119. OGRN = ObjectIdentifier("1.2.643.100.1")
  120. SNILS = ObjectIdentifier("1.2.643.100.3")
  121. UNSTRUCTURED_NAME = ObjectIdentifier("1.2.840.113549.1.9.2")
  122. class SignatureAlgorithmOID:
  123. RSA_WITH_MD5 = ObjectIdentifier("1.2.840.113549.1.1.4")
  124. RSA_WITH_SHA1 = ObjectIdentifier("1.2.840.113549.1.1.5")
  125. # This is an alternate OID for RSA with SHA1 that is occasionally seen
  126. _RSA_WITH_SHA1 = ObjectIdentifier("1.3.14.3.2.29")
  127. RSA_WITH_SHA224 = ObjectIdentifier("1.2.840.113549.1.1.14")
  128. RSA_WITH_SHA256 = ObjectIdentifier("1.2.840.113549.1.1.11")
  129. RSA_WITH_SHA384 = ObjectIdentifier("1.2.840.113549.1.1.12")
  130. RSA_WITH_SHA512 = ObjectIdentifier("1.2.840.113549.1.1.13")
  131. RSA_WITH_SHA3_224 = ObjectIdentifier("2.16.840.1.101.3.4.3.13")
  132. RSA_WITH_SHA3_256 = ObjectIdentifier("2.16.840.1.101.3.4.3.14")
  133. RSA_WITH_SHA3_384 = ObjectIdentifier("2.16.840.1.101.3.4.3.15")
  134. RSA_WITH_SHA3_512 = ObjectIdentifier("2.16.840.1.101.3.4.3.16")
  135. RSASSA_PSS = ObjectIdentifier("1.2.840.113549.1.1.10")
  136. ECDSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10045.4.1")
  137. ECDSA_WITH_SHA224 = ObjectIdentifier("1.2.840.10045.4.3.1")
  138. ECDSA_WITH_SHA256 = ObjectIdentifier("1.2.840.10045.4.3.2")
  139. ECDSA_WITH_SHA384 = ObjectIdentifier("1.2.840.10045.4.3.3")
  140. ECDSA_WITH_SHA512 = ObjectIdentifier("1.2.840.10045.4.3.4")
  141. ECDSA_WITH_SHA3_224 = ObjectIdentifier("2.16.840.1.101.3.4.3.9")
  142. ECDSA_WITH_SHA3_256 = ObjectIdentifier("2.16.840.1.101.3.4.3.10")
  143. ECDSA_WITH_SHA3_384 = ObjectIdentifier("2.16.840.1.101.3.4.3.11")
  144. ECDSA_WITH_SHA3_512 = ObjectIdentifier("2.16.840.1.101.3.4.3.12")
  145. DSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10040.4.3")
  146. DSA_WITH_SHA224 = ObjectIdentifier("2.16.840.1.101.3.4.3.1")
  147. DSA_WITH_SHA256 = ObjectIdentifier("2.16.840.1.101.3.4.3.2")
  148. DSA_WITH_SHA384 = ObjectIdentifier("2.16.840.1.101.3.4.3.3")
  149. DSA_WITH_SHA512 = ObjectIdentifier("2.16.840.1.101.3.4.3.4")
  150. ED25519 = ObjectIdentifier("1.3.101.112")
  151. ED448 = ObjectIdentifier("1.3.101.113")
  152. GOSTR3411_94_WITH_3410_2001 = ObjectIdentifier("1.2.643.2.2.3")
  153. GOSTR3410_2012_WITH_3411_2012_256 = ObjectIdentifier("1.2.643.7.1.1.3.2")
  154. GOSTR3410_2012_WITH_3411_2012_512 = ObjectIdentifier("1.2.643.7.1.1.3.3")
  155. _SIG_OIDS_TO_HASH: typing.Dict[
  156. ObjectIdentifier, typing.Optional[hashes.HashAlgorithm]
  157. ] = {
  158. SignatureAlgorithmOID.RSA_WITH_MD5: hashes.MD5(),
  159. SignatureAlgorithmOID.RSA_WITH_SHA1: hashes.SHA1(),
  160. SignatureAlgorithmOID._RSA_WITH_SHA1: hashes.SHA1(),
  161. SignatureAlgorithmOID.RSA_WITH_SHA224: hashes.SHA224(),
  162. SignatureAlgorithmOID.RSA_WITH_SHA256: hashes.SHA256(),
  163. SignatureAlgorithmOID.RSA_WITH_SHA384: hashes.SHA384(),
  164. SignatureAlgorithmOID.RSA_WITH_SHA512: hashes.SHA512(),
  165. SignatureAlgorithmOID.ECDSA_WITH_SHA1: hashes.SHA1(),
  166. SignatureAlgorithmOID.ECDSA_WITH_SHA224: hashes.SHA224(),
  167. SignatureAlgorithmOID.ECDSA_WITH_SHA256: hashes.SHA256(),
  168. SignatureAlgorithmOID.ECDSA_WITH_SHA384: hashes.SHA384(),
  169. SignatureAlgorithmOID.ECDSA_WITH_SHA512: hashes.SHA512(),
  170. SignatureAlgorithmOID.DSA_WITH_SHA1: hashes.SHA1(),
  171. SignatureAlgorithmOID.DSA_WITH_SHA224: hashes.SHA224(),
  172. SignatureAlgorithmOID.DSA_WITH_SHA256: hashes.SHA256(),
  173. SignatureAlgorithmOID.ED25519: None,
  174. SignatureAlgorithmOID.ED448: None,
  175. SignatureAlgorithmOID.GOSTR3411_94_WITH_3410_2001: None,
  176. SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_256: None,
  177. SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_512: None,
  178. }
  179. class ExtendedKeyUsageOID:
  180. SERVER_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.1")
  181. CLIENT_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.2")
  182. CODE_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.3")
  183. EMAIL_PROTECTION = ObjectIdentifier("1.3.6.1.5.5.7.3.4")
  184. TIME_STAMPING = ObjectIdentifier("1.3.6.1.5.5.7.3.8")
  185. OCSP_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.9")
  186. ANY_EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37.0")
  187. SMARTCARD_LOGON = ObjectIdentifier("1.3.6.1.4.1.311.20.2.2")
  188. KERBEROS_PKINIT_KDC = ObjectIdentifier("1.3.6.1.5.2.3.5")
  189. IPSEC_IKE = ObjectIdentifier("1.3.6.1.5.5.7.3.17")
  190. class AuthorityInformationAccessOID:
  191. CA_ISSUERS = ObjectIdentifier("1.3.6.1.5.5.7.48.2")
  192. OCSP = ObjectIdentifier("1.3.6.1.5.5.7.48.1")
  193. class SubjectInformationAccessOID:
  194. CA_REPOSITORY = ObjectIdentifier("1.3.6.1.5.5.7.48.5")
  195. class CertificatePoliciesOID:
  196. CPS_QUALIFIER = ObjectIdentifier("1.3.6.1.5.5.7.2.1")
  197. CPS_USER_NOTICE = ObjectIdentifier("1.3.6.1.5.5.7.2.2")
  198. ANY_POLICY = ObjectIdentifier("2.5.29.32.0")
  199. class AttributeOID:
  200. CHALLENGE_PASSWORD = ObjectIdentifier("1.2.840.113549.1.9.7")
  201. UNSTRUCTURED_NAME = ObjectIdentifier("1.2.840.113549.1.9.2")
  202. _OID_NAMES = {
  203. NameOID.COMMON_NAME: "commonName",
  204. NameOID.COUNTRY_NAME: "countryName",
  205. NameOID.LOCALITY_NAME: "localityName",
  206. NameOID.STATE_OR_PROVINCE_NAME: "stateOrProvinceName",
  207. NameOID.STREET_ADDRESS: "streetAddress",
  208. NameOID.ORGANIZATION_NAME: "organizationName",
  209. NameOID.ORGANIZATIONAL_UNIT_NAME: "organizationalUnitName",
  210. NameOID.SERIAL_NUMBER: "serialNumber",
  211. NameOID.SURNAME: "surname",
  212. NameOID.GIVEN_NAME: "givenName",
  213. NameOID.TITLE: "title",
  214. NameOID.GENERATION_QUALIFIER: "generationQualifier",
  215. NameOID.X500_UNIQUE_IDENTIFIER: "x500UniqueIdentifier",
  216. NameOID.DN_QUALIFIER: "dnQualifier",
  217. NameOID.PSEUDONYM: "pseudonym",
  218. NameOID.USER_ID: "userID",
  219. NameOID.DOMAIN_COMPONENT: "domainComponent",
  220. NameOID.EMAIL_ADDRESS: "emailAddress",
  221. NameOID.JURISDICTION_COUNTRY_NAME: "jurisdictionCountryName",
  222. NameOID.JURISDICTION_LOCALITY_NAME: "jurisdictionLocalityName",
  223. NameOID.JURISDICTION_STATE_OR_PROVINCE_NAME: (
  224. "jurisdictionStateOrProvinceName"
  225. ),
  226. NameOID.BUSINESS_CATEGORY: "businessCategory",
  227. NameOID.POSTAL_ADDRESS: "postalAddress",
  228. NameOID.POSTAL_CODE: "postalCode",
  229. NameOID.INN: "INN",
  230. NameOID.OGRN: "OGRN",
  231. NameOID.SNILS: "SNILS",
  232. NameOID.UNSTRUCTURED_NAME: "unstructuredName",
  233. SignatureAlgorithmOID.RSA_WITH_MD5: "md5WithRSAEncryption",
  234. SignatureAlgorithmOID.RSA_WITH_SHA1: "sha1WithRSAEncryption",
  235. SignatureAlgorithmOID.RSA_WITH_SHA224: "sha224WithRSAEncryption",
  236. SignatureAlgorithmOID.RSA_WITH_SHA256: "sha256WithRSAEncryption",
  237. SignatureAlgorithmOID.RSA_WITH_SHA384: "sha384WithRSAEncryption",
  238. SignatureAlgorithmOID.RSA_WITH_SHA512: "sha512WithRSAEncryption",
  239. SignatureAlgorithmOID.RSASSA_PSS: "RSASSA-PSS",
  240. SignatureAlgorithmOID.ECDSA_WITH_SHA1: "ecdsa-with-SHA1",
  241. SignatureAlgorithmOID.ECDSA_WITH_SHA224: "ecdsa-with-SHA224",
  242. SignatureAlgorithmOID.ECDSA_WITH_SHA256: "ecdsa-with-SHA256",
  243. SignatureAlgorithmOID.ECDSA_WITH_SHA384: "ecdsa-with-SHA384",
  244. SignatureAlgorithmOID.ECDSA_WITH_SHA512: "ecdsa-with-SHA512",
  245. SignatureAlgorithmOID.DSA_WITH_SHA1: "dsa-with-sha1",
  246. SignatureAlgorithmOID.DSA_WITH_SHA224: "dsa-with-sha224",
  247. SignatureAlgorithmOID.DSA_WITH_SHA256: "dsa-with-sha256",
  248. SignatureAlgorithmOID.ED25519: "ed25519",
  249. SignatureAlgorithmOID.ED448: "ed448",
  250. SignatureAlgorithmOID.GOSTR3411_94_WITH_3410_2001: (
  251. "GOST R 34.11-94 with GOST R 34.10-2001"
  252. ),
  253. SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_256: (
  254. "GOST R 34.10-2012 with GOST R 34.11-2012 (256 bit)"
  255. ),
  256. SignatureAlgorithmOID.GOSTR3410_2012_WITH_3411_2012_512: (
  257. "GOST R 34.10-2012 with GOST R 34.11-2012 (512 bit)"
  258. ),
  259. ExtendedKeyUsageOID.SERVER_AUTH: "serverAuth",
  260. ExtendedKeyUsageOID.CLIENT_AUTH: "clientAuth",
  261. ExtendedKeyUsageOID.CODE_SIGNING: "codeSigning",
  262. ExtendedKeyUsageOID.EMAIL_PROTECTION: "emailProtection",
  263. ExtendedKeyUsageOID.TIME_STAMPING: "timeStamping",
  264. ExtendedKeyUsageOID.OCSP_SIGNING: "OCSPSigning",
  265. ExtendedKeyUsageOID.SMARTCARD_LOGON: "msSmartcardLogin",
  266. ExtendedKeyUsageOID.KERBEROS_PKINIT_KDC: "pkInitKDC",
  267. ExtensionOID.SUBJECT_DIRECTORY_ATTRIBUTES: "subjectDirectoryAttributes",
  268. ExtensionOID.SUBJECT_KEY_IDENTIFIER: "subjectKeyIdentifier",
  269. ExtensionOID.KEY_USAGE: "keyUsage",
  270. ExtensionOID.SUBJECT_ALTERNATIVE_NAME: "subjectAltName",
  271. ExtensionOID.ISSUER_ALTERNATIVE_NAME: "issuerAltName",
  272. ExtensionOID.BASIC_CONSTRAINTS: "basicConstraints",
  273. ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS: (
  274. "signedCertificateTimestampList"
  275. ),
  276. ExtensionOID.SIGNED_CERTIFICATE_TIMESTAMPS: (
  277. "signedCertificateTimestampList"
  278. ),
  279. ExtensionOID.PRECERT_POISON: "ctPoison",
  280. CRLEntryExtensionOID.CRL_REASON: "cRLReason",
  281. CRLEntryExtensionOID.INVALIDITY_DATE: "invalidityDate",
  282. CRLEntryExtensionOID.CERTIFICATE_ISSUER: "certificateIssuer",
  283. ExtensionOID.NAME_CONSTRAINTS: "nameConstraints",
  284. ExtensionOID.CRL_DISTRIBUTION_POINTS: "cRLDistributionPoints",
  285. ExtensionOID.CERTIFICATE_POLICIES: "certificatePolicies",
  286. ExtensionOID.POLICY_MAPPINGS: "policyMappings",
  287. ExtensionOID.AUTHORITY_KEY_IDENTIFIER: "authorityKeyIdentifier",
  288. ExtensionOID.POLICY_CONSTRAINTS: "policyConstraints",
  289. ExtensionOID.EXTENDED_KEY_USAGE: "extendedKeyUsage",
  290. ExtensionOID.FRESHEST_CRL: "freshestCRL",
  291. ExtensionOID.INHIBIT_ANY_POLICY: "inhibitAnyPolicy",
  292. ExtensionOID.ISSUING_DISTRIBUTION_POINT: ("issuingDistributionPoint"),
  293. ExtensionOID.AUTHORITY_INFORMATION_ACCESS: "authorityInfoAccess",
  294. ExtensionOID.SUBJECT_INFORMATION_ACCESS: "subjectInfoAccess",
  295. ExtensionOID.OCSP_NO_CHECK: "OCSPNoCheck",
  296. ExtensionOID.CRL_NUMBER: "cRLNumber",
  297. ExtensionOID.DELTA_CRL_INDICATOR: "deltaCRLIndicator",
  298. ExtensionOID.TLS_FEATURE: "TLSFeature",
  299. AuthorityInformationAccessOID.OCSP: "OCSP",
  300. AuthorityInformationAccessOID.CA_ISSUERS: "caIssuers",
  301. SubjectInformationAccessOID.CA_REPOSITORY: "caRepository",
  302. CertificatePoliciesOID.CPS_QUALIFIER: "id-qt-cps",
  303. CertificatePoliciesOID.CPS_USER_NOTICE: "id-qt-unotice",
  304. OCSPExtensionOID.NONCE: "OCSPNonce",
  305. AttributeOID.CHALLENGE_PASSWORD: "challengePassword",
  306. }