interfaces.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 abc
  5. import typing
  6. if typing.TYPE_CHECKING:
  7. from cryptography.hazmat.primitives.asymmetric.types import (
  8. PRIVATE_KEY_TYPES,
  9. )
  10. from cryptography.hazmat.primitives import hashes
  11. from cryptography.x509.base import (
  12. Certificate,
  13. CertificateBuilder,
  14. CertificateRevocationList,
  15. CertificateRevocationListBuilder,
  16. CertificateSigningRequest,
  17. CertificateSigningRequestBuilder,
  18. RevokedCertificate,
  19. RevokedCertificateBuilder,
  20. )
  21. from cryptography.x509.name import Name
  22. class CipherBackend(metaclass=abc.ABCMeta):
  23. @abc.abstractmethod
  24. def cipher_supported(self, cipher, mode):
  25. """
  26. Return True if the given cipher and mode are supported.
  27. """
  28. @abc.abstractmethod
  29. def create_symmetric_encryption_ctx(self, cipher, mode):
  30. """
  31. Get a CipherContext that can be used for encryption.
  32. """
  33. @abc.abstractmethod
  34. def create_symmetric_decryption_ctx(self, cipher, mode):
  35. """
  36. Get a CipherContext that can be used for decryption.
  37. """
  38. class HashBackend(metaclass=abc.ABCMeta):
  39. @abc.abstractmethod
  40. def hash_supported(self, algorithm):
  41. """
  42. Return True if the hash algorithm is supported by this backend.
  43. """
  44. @abc.abstractmethod
  45. def create_hash_ctx(self, algorithm):
  46. """
  47. Create a HashContext for calculating a message digest.
  48. """
  49. class HMACBackend(metaclass=abc.ABCMeta):
  50. @abc.abstractmethod
  51. def hmac_supported(self, algorithm):
  52. """
  53. Return True if the hash algorithm is supported for HMAC by this
  54. backend.
  55. """
  56. @abc.abstractmethod
  57. def create_hmac_ctx(self, key, algorithm):
  58. """
  59. Create a context for calculating a message authentication code.
  60. """
  61. class CMACBackend(metaclass=abc.ABCMeta):
  62. @abc.abstractmethod
  63. def cmac_algorithm_supported(self, algorithm):
  64. """
  65. Returns True if the block cipher is supported for CMAC by this backend
  66. """
  67. @abc.abstractmethod
  68. def create_cmac_ctx(self, algorithm):
  69. """
  70. Create a context for calculating a message authentication code.
  71. """
  72. class PBKDF2HMACBackend(metaclass=abc.ABCMeta):
  73. @abc.abstractmethod
  74. def pbkdf2_hmac_supported(self, algorithm):
  75. """
  76. Return True if the hash algorithm is supported for PBKDF2 by this
  77. backend.
  78. """
  79. @abc.abstractmethod
  80. def derive_pbkdf2_hmac(
  81. self, algorithm, length, salt, iterations, key_material
  82. ):
  83. """
  84. Return length bytes derived from provided PBKDF2 parameters.
  85. """
  86. class RSABackend(metaclass=abc.ABCMeta):
  87. @abc.abstractmethod
  88. def generate_rsa_private_key(self, public_exponent, key_size):
  89. """
  90. Generate an RSAPrivateKey instance with public_exponent and a modulus
  91. of key_size bits.
  92. """
  93. @abc.abstractmethod
  94. def rsa_padding_supported(self, padding):
  95. """
  96. Returns True if the backend supports the given padding options.
  97. """
  98. @abc.abstractmethod
  99. def generate_rsa_parameters_supported(self, public_exponent, key_size):
  100. """
  101. Returns True if the backend supports the given parameters for key
  102. generation.
  103. """
  104. @abc.abstractmethod
  105. def load_rsa_private_numbers(self, numbers):
  106. """
  107. Returns an RSAPrivateKey provider.
  108. """
  109. @abc.abstractmethod
  110. def load_rsa_public_numbers(self, numbers):
  111. """
  112. Returns an RSAPublicKey provider.
  113. """
  114. class DSABackend(metaclass=abc.ABCMeta):
  115. @abc.abstractmethod
  116. def generate_dsa_parameters(self, key_size):
  117. """
  118. Generate a DSAParameters instance with a modulus of key_size bits.
  119. """
  120. @abc.abstractmethod
  121. def generate_dsa_private_key(self, parameters):
  122. """
  123. Generate a DSAPrivateKey instance with parameters as a DSAParameters
  124. object.
  125. """
  126. @abc.abstractmethod
  127. def generate_dsa_private_key_and_parameters(self, key_size):
  128. """
  129. Generate a DSAPrivateKey instance using key size only.
  130. """
  131. @abc.abstractmethod
  132. def dsa_hash_supported(self, algorithm):
  133. """
  134. Return True if the hash algorithm is supported by the backend for DSA.
  135. """
  136. @abc.abstractmethod
  137. def dsa_parameters_supported(self, p, q, g):
  138. """
  139. Return True if the parameters are supported by the backend for DSA.
  140. """
  141. @abc.abstractmethod
  142. def load_dsa_private_numbers(self, numbers):
  143. """
  144. Returns a DSAPrivateKey provider.
  145. """
  146. @abc.abstractmethod
  147. def load_dsa_public_numbers(self, numbers):
  148. """
  149. Returns a DSAPublicKey provider.
  150. """
  151. @abc.abstractmethod
  152. def load_dsa_parameter_numbers(self, numbers):
  153. """
  154. Returns a DSAParameters provider.
  155. """
  156. class EllipticCurveBackend(metaclass=abc.ABCMeta):
  157. @abc.abstractmethod
  158. def elliptic_curve_signature_algorithm_supported(
  159. self, signature_algorithm, curve
  160. ):
  161. """
  162. Returns True if the backend supports the named elliptic curve with the
  163. specified signature algorithm.
  164. """
  165. @abc.abstractmethod
  166. def elliptic_curve_supported(self, curve):
  167. """
  168. Returns True if the backend supports the named elliptic curve.
  169. """
  170. @abc.abstractmethod
  171. def generate_elliptic_curve_private_key(self, curve):
  172. """
  173. Return an object conforming to the EllipticCurvePrivateKey interface.
  174. """
  175. @abc.abstractmethod
  176. def load_elliptic_curve_public_numbers(self, numbers):
  177. """
  178. Return an EllipticCurvePublicKey provider using the given numbers.
  179. """
  180. @abc.abstractmethod
  181. def load_elliptic_curve_private_numbers(self, numbers):
  182. """
  183. Return an EllipticCurvePrivateKey provider using the given numbers.
  184. """
  185. @abc.abstractmethod
  186. def elliptic_curve_exchange_algorithm_supported(self, algorithm, curve):
  187. """
  188. Returns whether the exchange algorithm is supported by this backend.
  189. """
  190. @abc.abstractmethod
  191. def derive_elliptic_curve_private_key(self, private_value, curve):
  192. """
  193. Compute the private key given the private value and curve.
  194. """
  195. class PEMSerializationBackend(metaclass=abc.ABCMeta):
  196. @abc.abstractmethod
  197. def load_pem_private_key(self, data, password):
  198. """
  199. Loads a private key from PEM encoded data, using the provided password
  200. if the data is encrypted.
  201. """
  202. @abc.abstractmethod
  203. def load_pem_public_key(self, data):
  204. """
  205. Loads a public key from PEM encoded data.
  206. """
  207. @abc.abstractmethod
  208. def load_pem_parameters(self, data):
  209. """
  210. Load encryption parameters from PEM encoded data.
  211. """
  212. class DERSerializationBackend(metaclass=abc.ABCMeta):
  213. @abc.abstractmethod
  214. def load_der_private_key(self, data, password):
  215. """
  216. Loads a private key from DER encoded data. Uses the provided password
  217. if the data is encrypted.
  218. """
  219. @abc.abstractmethod
  220. def load_der_public_key(self, data):
  221. """
  222. Loads a public key from DER encoded data.
  223. """
  224. @abc.abstractmethod
  225. def load_der_parameters(self, data):
  226. """
  227. Load encryption parameters from DER encoded data.
  228. """
  229. class X509Backend(metaclass=abc.ABCMeta):
  230. @abc.abstractmethod
  231. def create_x509_csr(
  232. self,
  233. builder: "CertificateSigningRequestBuilder",
  234. private_key: "PRIVATE_KEY_TYPES",
  235. algorithm: typing.Optional["hashes.HashAlgorithm"],
  236. ) -> "CertificateSigningRequest":
  237. """
  238. Create and sign an X.509 CSR from a CSR builder object.
  239. """
  240. @abc.abstractmethod
  241. def create_x509_certificate(
  242. self,
  243. builder: "CertificateBuilder",
  244. private_key: "PRIVATE_KEY_TYPES",
  245. algorithm: typing.Optional["hashes.HashAlgorithm"],
  246. ) -> "Certificate":
  247. """
  248. Create and sign an X.509 certificate from a CertificateBuilder object.
  249. """
  250. @abc.abstractmethod
  251. def create_x509_crl(
  252. self,
  253. builder: "CertificateRevocationListBuilder",
  254. private_key: "PRIVATE_KEY_TYPES",
  255. algorithm: typing.Optional["hashes.HashAlgorithm"],
  256. ) -> "CertificateRevocationList":
  257. """
  258. Create and sign an X.509 CertificateRevocationList from a
  259. CertificateRevocationListBuilder object.
  260. """
  261. @abc.abstractmethod
  262. def create_x509_revoked_certificate(
  263. self, builder: "RevokedCertificateBuilder"
  264. ) -> "RevokedCertificate":
  265. """
  266. Create a RevokedCertificate object from a RevokedCertificateBuilder
  267. object.
  268. """
  269. @abc.abstractmethod
  270. def x509_name_bytes(self, name: "Name") -> bytes:
  271. """
  272. Compute the DER encoded bytes of an X509 Name object.
  273. """
  274. class DHBackend(metaclass=abc.ABCMeta):
  275. @abc.abstractmethod
  276. def generate_dh_parameters(self, generator, key_size):
  277. """
  278. Generate a DHParameters instance with a modulus of key_size bits.
  279. Using the given generator. Often 2 or 5.
  280. """
  281. @abc.abstractmethod
  282. def generate_dh_private_key(self, parameters):
  283. """
  284. Generate a DHPrivateKey instance with parameters as a DHParameters
  285. object.
  286. """
  287. @abc.abstractmethod
  288. def generate_dh_private_key_and_parameters(self, generator, key_size):
  289. """
  290. Generate a DHPrivateKey instance using key size only.
  291. Using the given generator. Often 2 or 5.
  292. """
  293. @abc.abstractmethod
  294. def load_dh_private_numbers(self, numbers):
  295. """
  296. Load a DHPrivateKey from DHPrivateNumbers
  297. """
  298. @abc.abstractmethod
  299. def load_dh_public_numbers(self, numbers):
  300. """
  301. Load a DHPublicKey from DHPublicNumbers.
  302. """
  303. @abc.abstractmethod
  304. def load_dh_parameter_numbers(self, numbers):
  305. """
  306. Load DHParameters from DHParameterNumbers.
  307. """
  308. @abc.abstractmethod
  309. def dh_parameters_supported(self, p, g, q=None):
  310. """
  311. Returns whether the backend supports DH with these parameter values.
  312. """
  313. @abc.abstractmethod
  314. def dh_x942_serialization_supported(self):
  315. """
  316. Returns True if the backend supports the serialization of DH objects
  317. with subgroup order (q).
  318. """
  319. class ScryptBackend(metaclass=abc.ABCMeta):
  320. @abc.abstractmethod
  321. def derive_scrypt(self, key_material, salt, length, n, r, p):
  322. """
  323. Return bytes derived from provided Scrypt parameters.
  324. """
  325. @abc.abstractmethod
  326. def scrypt_supported(self):
  327. """
  328. Return True if Scrypt is supported.
  329. """
  330. # This is the catch-all for future backend methods and inherits all the
  331. # other interfaces as well so we can just use Backend for typing.
  332. class Backend(
  333. CipherBackend,
  334. CMACBackend,
  335. DERSerializationBackend,
  336. DHBackend,
  337. DSABackend,
  338. EllipticCurveBackend,
  339. HashBackend,
  340. HMACBackend,
  341. PBKDF2HMACBackend,
  342. RSABackend,
  343. PEMSerializationBackend,
  344. ScryptBackend,
  345. X509Backend,
  346. metaclass=abc.ABCMeta,
  347. ):
  348. @abc.abstractmethod
  349. def load_pem_pkcs7_certificates(self, data):
  350. """
  351. Returns a list of x509.Certificate
  352. """
  353. @abc.abstractmethod
  354. def load_der_pkcs7_certificates(self, data):
  355. """
  356. Returns a list of x509.Certificate
  357. """
  358. @abc.abstractmethod
  359. def pkcs7_sign(self, builder, encoding, options):
  360. """
  361. Returns bytes
  362. """
  363. @abc.abstractmethod
  364. def load_key_and_certificates_from_pkcs12(self, data, password):
  365. """
  366. Returns a tuple of (key, cert, [certs])
  367. """
  368. @abc.abstractmethod
  369. def serialize_key_and_certificates_to_pkcs12(
  370. self, name, key, cert, cas, encryption_algorithm
  371. ):
  372. """
  373. Returns bytes
  374. """