dsa.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.exceptions import InvalidSignature
  6. from cryptography.hazmat.backends.openssl.utils import (
  7. _calculate_digest_and_algorithm,
  8. )
  9. from cryptography.hazmat.primitives import hashes, serialization
  10. from cryptography.hazmat.primitives.asymmetric import (
  11. dsa,
  12. utils as asym_utils,
  13. )
  14. if typing.TYPE_CHECKING:
  15. from cryptography.hazmat.backends.openssl.backend import Backend
  16. def _dsa_sig_sign(
  17. backend: "Backend", private_key: "_DSAPrivateKey", data: bytes
  18. ) -> bytes:
  19. sig_buf_len = backend._lib.DSA_size(private_key._dsa_cdata)
  20. sig_buf = backend._ffi.new("unsigned char[]", sig_buf_len)
  21. buflen = backend._ffi.new("unsigned int *")
  22. # The first parameter passed to DSA_sign is unused by OpenSSL but
  23. # must be an integer.
  24. res = backend._lib.DSA_sign(
  25. 0, data, len(data), sig_buf, buflen, private_key._dsa_cdata
  26. )
  27. backend.openssl_assert(res == 1)
  28. backend.openssl_assert(buflen[0])
  29. return backend._ffi.buffer(sig_buf)[: buflen[0]]
  30. def _dsa_sig_verify(
  31. backend: "Backend",
  32. public_key: "_DSAPublicKey",
  33. signature: bytes,
  34. data: bytes,
  35. ) -> None:
  36. # The first parameter passed to DSA_verify is unused by OpenSSL but
  37. # must be an integer.
  38. res = backend._lib.DSA_verify(
  39. 0, data, len(data), signature, len(signature), public_key._dsa_cdata
  40. )
  41. if res != 1:
  42. backend._consume_errors()
  43. raise InvalidSignature
  44. class _DSAParameters(dsa.DSAParameters):
  45. def __init__(self, backend: "Backend", dsa_cdata):
  46. self._backend = backend
  47. self._dsa_cdata = dsa_cdata
  48. def parameter_numbers(self) -> dsa.DSAParameterNumbers:
  49. p = self._backend._ffi.new("BIGNUM **")
  50. q = self._backend._ffi.new("BIGNUM **")
  51. g = self._backend._ffi.new("BIGNUM **")
  52. self._backend._lib.DSA_get0_pqg(self._dsa_cdata, p, q, g)
  53. self._backend.openssl_assert(p[0] != self._backend._ffi.NULL)
  54. self._backend.openssl_assert(q[0] != self._backend._ffi.NULL)
  55. self._backend.openssl_assert(g[0] != self._backend._ffi.NULL)
  56. return dsa.DSAParameterNumbers(
  57. p=self._backend._bn_to_int(p[0]),
  58. q=self._backend._bn_to_int(q[0]),
  59. g=self._backend._bn_to_int(g[0]),
  60. )
  61. def generate_private_key(self) -> dsa.DSAPrivateKey:
  62. return self._backend.generate_dsa_private_key(self)
  63. class _DSAPrivateKey(dsa.DSAPrivateKey):
  64. _key_size: int
  65. def __init__(self, backend: "Backend", dsa_cdata, evp_pkey):
  66. self._backend = backend
  67. self._dsa_cdata = dsa_cdata
  68. self._evp_pkey = evp_pkey
  69. p = self._backend._ffi.new("BIGNUM **")
  70. self._backend._lib.DSA_get0_pqg(
  71. dsa_cdata, p, self._backend._ffi.NULL, self._backend._ffi.NULL
  72. )
  73. self._backend.openssl_assert(p[0] != backend._ffi.NULL)
  74. self._key_size = self._backend._lib.BN_num_bits(p[0])
  75. @property
  76. def key_size(self) -> int:
  77. return self._key_size
  78. def private_numbers(self) -> dsa.DSAPrivateNumbers:
  79. p = self._backend._ffi.new("BIGNUM **")
  80. q = self._backend._ffi.new("BIGNUM **")
  81. g = self._backend._ffi.new("BIGNUM **")
  82. pub_key = self._backend._ffi.new("BIGNUM **")
  83. priv_key = self._backend._ffi.new("BIGNUM **")
  84. self._backend._lib.DSA_get0_pqg(self._dsa_cdata, p, q, g)
  85. self._backend.openssl_assert(p[0] != self._backend._ffi.NULL)
  86. self._backend.openssl_assert(q[0] != self._backend._ffi.NULL)
  87. self._backend.openssl_assert(g[0] != self._backend._ffi.NULL)
  88. self._backend._lib.DSA_get0_key(self._dsa_cdata, pub_key, priv_key)
  89. self._backend.openssl_assert(pub_key[0] != self._backend._ffi.NULL)
  90. self._backend.openssl_assert(priv_key[0] != self._backend._ffi.NULL)
  91. return dsa.DSAPrivateNumbers(
  92. public_numbers=dsa.DSAPublicNumbers(
  93. parameter_numbers=dsa.DSAParameterNumbers(
  94. p=self._backend._bn_to_int(p[0]),
  95. q=self._backend._bn_to_int(q[0]),
  96. g=self._backend._bn_to_int(g[0]),
  97. ),
  98. y=self._backend._bn_to_int(pub_key[0]),
  99. ),
  100. x=self._backend._bn_to_int(priv_key[0]),
  101. )
  102. def public_key(self) -> dsa.DSAPublicKey:
  103. dsa_cdata = self._backend._lib.DSAparams_dup(self._dsa_cdata)
  104. self._backend.openssl_assert(dsa_cdata != self._backend._ffi.NULL)
  105. dsa_cdata = self._backend._ffi.gc(
  106. dsa_cdata, self._backend._lib.DSA_free
  107. )
  108. pub_key = self._backend._ffi.new("BIGNUM **")
  109. self._backend._lib.DSA_get0_key(
  110. self._dsa_cdata, pub_key, self._backend._ffi.NULL
  111. )
  112. self._backend.openssl_assert(pub_key[0] != self._backend._ffi.NULL)
  113. pub_key_dup = self._backend._lib.BN_dup(pub_key[0])
  114. res = self._backend._lib.DSA_set0_key(
  115. dsa_cdata, pub_key_dup, self._backend._ffi.NULL
  116. )
  117. self._backend.openssl_assert(res == 1)
  118. evp_pkey = self._backend._dsa_cdata_to_evp_pkey(dsa_cdata)
  119. return _DSAPublicKey(self._backend, dsa_cdata, evp_pkey)
  120. def parameters(self) -> dsa.DSAParameters:
  121. dsa_cdata = self._backend._lib.DSAparams_dup(self._dsa_cdata)
  122. self._backend.openssl_assert(dsa_cdata != self._backend._ffi.NULL)
  123. dsa_cdata = self._backend._ffi.gc(
  124. dsa_cdata, self._backend._lib.DSA_free
  125. )
  126. return _DSAParameters(self._backend, dsa_cdata)
  127. def private_bytes(
  128. self,
  129. encoding: serialization.Encoding,
  130. format: serialization.PrivateFormat,
  131. encryption_algorithm: serialization.KeySerializationEncryption,
  132. ) -> bytes:
  133. return self._backend._private_key_bytes(
  134. encoding,
  135. format,
  136. encryption_algorithm,
  137. self,
  138. self._evp_pkey,
  139. self._dsa_cdata,
  140. )
  141. def sign(
  142. self,
  143. data: bytes,
  144. algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
  145. ) -> bytes:
  146. data, _ = _calculate_digest_and_algorithm(data, algorithm)
  147. return _dsa_sig_sign(self._backend, self, data)
  148. class _DSAPublicKey(dsa.DSAPublicKey):
  149. _key_size: int
  150. def __init__(self, backend: "Backend", dsa_cdata, evp_pkey):
  151. self._backend = backend
  152. self._dsa_cdata = dsa_cdata
  153. self._evp_pkey = evp_pkey
  154. p = self._backend._ffi.new("BIGNUM **")
  155. self._backend._lib.DSA_get0_pqg(
  156. dsa_cdata, p, self._backend._ffi.NULL, self._backend._ffi.NULL
  157. )
  158. self._backend.openssl_assert(p[0] != backend._ffi.NULL)
  159. self._key_size = self._backend._lib.BN_num_bits(p[0])
  160. @property
  161. def key_size(self) -> int:
  162. return self._key_size
  163. def public_numbers(self) -> dsa.DSAPublicNumbers:
  164. p = self._backend._ffi.new("BIGNUM **")
  165. q = self._backend._ffi.new("BIGNUM **")
  166. g = self._backend._ffi.new("BIGNUM **")
  167. pub_key = self._backend._ffi.new("BIGNUM **")
  168. self._backend._lib.DSA_get0_pqg(self._dsa_cdata, p, q, g)
  169. self._backend.openssl_assert(p[0] != self._backend._ffi.NULL)
  170. self._backend.openssl_assert(q[0] != self._backend._ffi.NULL)
  171. self._backend.openssl_assert(g[0] != self._backend._ffi.NULL)
  172. self._backend._lib.DSA_get0_key(
  173. self._dsa_cdata, pub_key, self._backend._ffi.NULL
  174. )
  175. self._backend.openssl_assert(pub_key[0] != self._backend._ffi.NULL)
  176. return dsa.DSAPublicNumbers(
  177. parameter_numbers=dsa.DSAParameterNumbers(
  178. p=self._backend._bn_to_int(p[0]),
  179. q=self._backend._bn_to_int(q[0]),
  180. g=self._backend._bn_to_int(g[0]),
  181. ),
  182. y=self._backend._bn_to_int(pub_key[0]),
  183. )
  184. def parameters(self) -> dsa.DSAParameters:
  185. dsa_cdata = self._backend._lib.DSAparams_dup(self._dsa_cdata)
  186. dsa_cdata = self._backend._ffi.gc(
  187. dsa_cdata, self._backend._lib.DSA_free
  188. )
  189. return _DSAParameters(self._backend, dsa_cdata)
  190. def public_bytes(
  191. self,
  192. encoding: serialization.Encoding,
  193. format: serialization.PublicFormat,
  194. ) -> bytes:
  195. return self._backend._public_key_bytes(
  196. encoding, format, self, self._evp_pkey, None
  197. )
  198. def verify(
  199. self,
  200. signature: bytes,
  201. data: bytes,
  202. algorithm: typing.Union[asym_utils.Prehashed, hashes.HashAlgorithm],
  203. ) -> None:
  204. data, _ = _calculate_digest_and_algorithm(data, algorithm)
  205. return _dsa_sig_verify(self._backend, self, signature, data)