You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

423 lines
13 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. import abc
  6. import datetime
  7. from enum import Enum
  8. import six
  9. from cryptography import x509
  10. from cryptography.hazmat.primitives import hashes
  11. from cryptography.x509.base import (
  12. _EARLIEST_UTC_TIME, _convert_to_naive_utc_time, _reject_duplicate_extension
  13. )
  14. _OIDS_TO_HASH = {
  15. "1.3.14.3.2.26": hashes.SHA1(),
  16. "2.16.840.1.101.3.4.2.4": hashes.SHA224(),
  17. "2.16.840.1.101.3.4.2.1": hashes.SHA256(),
  18. "2.16.840.1.101.3.4.2.2": hashes.SHA384(),
  19. "2.16.840.1.101.3.4.2.3": hashes.SHA512(),
  20. }
  21. class OCSPResponderEncoding(Enum):
  22. HASH = "By Hash"
  23. NAME = "By Name"
  24. class OCSPResponseStatus(Enum):
  25. SUCCESSFUL = 0
  26. MALFORMED_REQUEST = 1
  27. INTERNAL_ERROR = 2
  28. TRY_LATER = 3
  29. SIG_REQUIRED = 5
  30. UNAUTHORIZED = 6
  31. _RESPONSE_STATUS_TO_ENUM = dict((x.value, x) for x in OCSPResponseStatus)
  32. _ALLOWED_HASHES = (
  33. hashes.SHA1, hashes.SHA224, hashes.SHA256,
  34. hashes.SHA384, hashes.SHA512
  35. )
  36. def _verify_algorithm(algorithm):
  37. if not isinstance(algorithm, _ALLOWED_HASHES):
  38. raise ValueError(
  39. "Algorithm must be SHA1, SHA224, SHA256, SHA384, or SHA512"
  40. )
  41. class OCSPCertStatus(Enum):
  42. GOOD = 0
  43. REVOKED = 1
  44. UNKNOWN = 2
  45. _CERT_STATUS_TO_ENUM = dict((x.value, x) for x in OCSPCertStatus)
  46. def load_der_ocsp_request(data):
  47. from cryptography.hazmat.backends.openssl.backend import backend
  48. return backend.load_der_ocsp_request(data)
  49. def load_der_ocsp_response(data):
  50. from cryptography.hazmat.backends.openssl.backend import backend
  51. return backend.load_der_ocsp_response(data)
  52. class OCSPRequestBuilder(object):
  53. def __init__(self, request=None, extensions=[]):
  54. self._request = request
  55. self._extensions = extensions
  56. def add_certificate(self, cert, issuer, algorithm):
  57. if self._request is not None:
  58. raise ValueError("Only one certificate can be added to a request")
  59. _verify_algorithm(algorithm)
  60. if (
  61. not isinstance(cert, x509.Certificate) or
  62. not isinstance(issuer, x509.Certificate)
  63. ):
  64. raise TypeError("cert and issuer must be a Certificate")
  65. return OCSPRequestBuilder((cert, issuer, algorithm), self._extensions)
  66. def add_extension(self, extension, critical):
  67. if not isinstance(extension, x509.ExtensionType):
  68. raise TypeError("extension must be an ExtensionType")
  69. extension = x509.Extension(extension.oid, critical, extension)
  70. _reject_duplicate_extension(extension, self._extensions)
  71. return OCSPRequestBuilder(
  72. self._request, self._extensions + [extension]
  73. )
  74. def build(self):
  75. from cryptography.hazmat.backends.openssl.backend import backend
  76. if self._request is None:
  77. raise ValueError("You must add a certificate before building")
  78. return backend.create_ocsp_request(self)
  79. class _SingleResponse(object):
  80. def __init__(self, cert, issuer, algorithm, cert_status, this_update,
  81. next_update, revocation_time, revocation_reason):
  82. if (
  83. not isinstance(cert, x509.Certificate) or
  84. not isinstance(issuer, x509.Certificate)
  85. ):
  86. raise TypeError("cert and issuer must be a Certificate")
  87. _verify_algorithm(algorithm)
  88. if not isinstance(this_update, datetime.datetime):
  89. raise TypeError("this_update must be a datetime object")
  90. if (
  91. next_update is not None and
  92. not isinstance(next_update, datetime.datetime)
  93. ):
  94. raise TypeError("next_update must be a datetime object or None")
  95. self._cert = cert
  96. self._issuer = issuer
  97. self._algorithm = algorithm
  98. self._this_update = this_update
  99. self._next_update = next_update
  100. if not isinstance(cert_status, OCSPCertStatus):
  101. raise TypeError(
  102. "cert_status must be an item from the OCSPCertStatus enum"
  103. )
  104. if cert_status is not OCSPCertStatus.REVOKED:
  105. if revocation_time is not None:
  106. raise ValueError(
  107. "revocation_time can only be provided if the certificate "
  108. "is revoked"
  109. )
  110. if revocation_reason is not None:
  111. raise ValueError(
  112. "revocation_reason can only be provided if the certificate"
  113. " is revoked"
  114. )
  115. else:
  116. if not isinstance(revocation_time, datetime.datetime):
  117. raise TypeError("revocation_time must be a datetime object")
  118. revocation_time = _convert_to_naive_utc_time(revocation_time)
  119. if revocation_time < _EARLIEST_UTC_TIME:
  120. raise ValueError('The revocation_time must be on or after'
  121. ' 1950 January 1.')
  122. if (
  123. revocation_reason is not None and
  124. not isinstance(revocation_reason, x509.ReasonFlags)
  125. ):
  126. raise TypeError(
  127. "revocation_reason must be an item from the ReasonFlags "
  128. "enum or None"
  129. )
  130. self._cert_status = cert_status
  131. self._revocation_time = revocation_time
  132. self._revocation_reason = revocation_reason
  133. class OCSPResponseBuilder(object):
  134. def __init__(self, response=None, responder_id=None, certs=None,
  135. extensions=[]):
  136. self._response = response
  137. self._responder_id = responder_id
  138. self._certs = certs
  139. self._extensions = extensions
  140. def add_response(self, cert, issuer, algorithm, cert_status, this_update,
  141. next_update, revocation_time, revocation_reason):
  142. if self._response is not None:
  143. raise ValueError("Only one response per OCSPResponse.")
  144. singleresp = _SingleResponse(
  145. cert, issuer, algorithm, cert_status, this_update, next_update,
  146. revocation_time, revocation_reason
  147. )
  148. return OCSPResponseBuilder(
  149. singleresp, self._responder_id,
  150. self._certs, self._extensions,
  151. )
  152. def responder_id(self, encoding, responder_cert):
  153. if self._responder_id is not None:
  154. raise ValueError("responder_id can only be set once")
  155. if not isinstance(responder_cert, x509.Certificate):
  156. raise TypeError("responder_cert must be a Certificate")
  157. if not isinstance(encoding, OCSPResponderEncoding):
  158. raise TypeError(
  159. "encoding must be an element from OCSPResponderEncoding"
  160. )
  161. return OCSPResponseBuilder(
  162. self._response, (responder_cert, encoding),
  163. self._certs, self._extensions,
  164. )
  165. def certificates(self, certs):
  166. if self._certs is not None:
  167. raise ValueError("certificates may only be set once")
  168. certs = list(certs)
  169. if len(certs) == 0:
  170. raise ValueError("certs must not be an empty list")
  171. if not all(isinstance(x, x509.Certificate) for x in certs):
  172. raise TypeError("certs must be a list of Certificates")
  173. return OCSPResponseBuilder(
  174. self._response, self._responder_id,
  175. certs, self._extensions,
  176. )
  177. def add_extension(self, extension, critical):
  178. if not isinstance(extension, x509.ExtensionType):
  179. raise TypeError("extension must be an ExtensionType")
  180. extension = x509.Extension(extension.oid, critical, extension)
  181. _reject_duplicate_extension(extension, self._extensions)
  182. return OCSPResponseBuilder(
  183. self._response, self._responder_id,
  184. self._certs, self._extensions + [extension],
  185. )
  186. def sign(self, private_key, algorithm):
  187. from cryptography.hazmat.backends.openssl.backend import backend
  188. if self._response is None:
  189. raise ValueError("You must add a response before signing")
  190. if self._responder_id is None:
  191. raise ValueError("You must add a responder_id before signing")
  192. if not isinstance(algorithm, hashes.HashAlgorithm):
  193. raise TypeError("Algorithm must be a registered hash algorithm.")
  194. return backend.create_ocsp_response(
  195. OCSPResponseStatus.SUCCESSFUL, self, private_key, algorithm
  196. )
  197. @classmethod
  198. def build_unsuccessful(cls, response_status):
  199. from cryptography.hazmat.backends.openssl.backend import backend
  200. if not isinstance(response_status, OCSPResponseStatus):
  201. raise TypeError(
  202. "response_status must be an item from OCSPResponseStatus"
  203. )
  204. if response_status is OCSPResponseStatus.SUCCESSFUL:
  205. raise ValueError("response_status cannot be SUCCESSFUL")
  206. return backend.create_ocsp_response(response_status, None, None, None)
  207. @six.add_metaclass(abc.ABCMeta)
  208. class OCSPRequest(object):
  209. @abc.abstractproperty
  210. def issuer_key_hash(self):
  211. """
  212. The hash of the issuer public key
  213. """
  214. @abc.abstractproperty
  215. def issuer_name_hash(self):
  216. """
  217. The hash of the issuer name
  218. """
  219. @abc.abstractproperty
  220. def hash_algorithm(self):
  221. """
  222. The hash algorithm used in the issuer name and key hashes
  223. """
  224. @abc.abstractproperty
  225. def serial_number(self):
  226. """
  227. The serial number of the cert whose status is being checked
  228. """
  229. @abc.abstractmethod
  230. def public_bytes(self, encoding):
  231. """
  232. Serializes the request to DER
  233. """
  234. @abc.abstractproperty
  235. def extensions(self):
  236. """
  237. The list of request extensions. Not single request extensions.
  238. """
  239. @six.add_metaclass(abc.ABCMeta)
  240. class OCSPResponse(object):
  241. @abc.abstractproperty
  242. def response_status(self):
  243. """
  244. The status of the response. This is a value from the OCSPResponseStatus
  245. enumeration
  246. """
  247. @abc.abstractproperty
  248. def signature_algorithm_oid(self):
  249. """
  250. The ObjectIdentifier of the signature algorithm
  251. """
  252. @abc.abstractproperty
  253. def signature_hash_algorithm(self):
  254. """
  255. Returns a HashAlgorithm corresponding to the type of the digest signed
  256. """
  257. @abc.abstractproperty
  258. def signature(self):
  259. """
  260. The signature bytes
  261. """
  262. @abc.abstractproperty
  263. def tbs_response_bytes(self):
  264. """
  265. The tbsResponseData bytes
  266. """
  267. @abc.abstractproperty
  268. def certificates(self):
  269. """
  270. A list of certificates used to help build a chain to verify the OCSP
  271. response. This situation occurs when the OCSP responder uses a delegate
  272. certificate.
  273. """
  274. @abc.abstractproperty
  275. def responder_key_hash(self):
  276. """
  277. The responder's key hash or None
  278. """
  279. @abc.abstractproperty
  280. def responder_name(self):
  281. """
  282. The responder's Name or None
  283. """
  284. @abc.abstractproperty
  285. def produced_at(self):
  286. """
  287. The time the response was produced
  288. """
  289. @abc.abstractproperty
  290. def certificate_status(self):
  291. """
  292. The status of the certificate (an element from the OCSPCertStatus enum)
  293. """
  294. @abc.abstractproperty
  295. def revocation_time(self):
  296. """
  297. The date of when the certificate was revoked or None if not
  298. revoked.
  299. """
  300. @abc.abstractproperty
  301. def revocation_reason(self):
  302. """
  303. The reason the certificate was revoked or None if not specified or
  304. not revoked.
  305. """
  306. @abc.abstractproperty
  307. def this_update(self):
  308. """
  309. The most recent time at which the status being indicated is known by
  310. the responder to have been correct
  311. """
  312. @abc.abstractproperty
  313. def next_update(self):
  314. """
  315. The time when newer information will be available
  316. """
  317. @abc.abstractproperty
  318. def issuer_key_hash(self):
  319. """
  320. The hash of the issuer public key
  321. """
  322. @abc.abstractproperty
  323. def issuer_name_hash(self):
  324. """
  325. The hash of the issuer name
  326. """
  327. @abc.abstractproperty
  328. def hash_algorithm(self):
  329. """
  330. The hash algorithm used in the issuer name and key hashes
  331. """
  332. @abc.abstractproperty
  333. def serial_number(self):
  334. """
  335. The serial number of the cert whose status is being checked
  336. """
  337. @abc.abstractproperty
  338. def extensions(self):
  339. """
  340. The list of response extensions. Not single response extensions.
  341. """