__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. #
  3. # ===================================================================
  4. # The contents of this file are dedicated to the public domain. To
  5. # the extent that dedication to the public domain is not available,
  6. # everyone is granted a worldwide, perpetual, royalty-free,
  7. # non-exclusive license to exercise all rights associated with the
  8. # contents of this file for any purpose whatsoever.
  9. # No rights are reserved.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. # SOFTWARE.
  19. # ===================================================================
  20. from Cryptodome.Util.asn1 import (DerSequence, DerInteger, DerBitString,
  21. DerObjectId, DerNull)
  22. def _expand_subject_public_key_info(encoded):
  23. """Parse a SubjectPublicKeyInfo structure.
  24. It returns a triple with:
  25. * OID (string)
  26. * encoded public key (bytes)
  27. * Algorithm parameters (bytes or None)
  28. """
  29. #
  30. # SubjectPublicKeyInfo ::= SEQUENCE {
  31. # algorithm AlgorithmIdentifier,
  32. # subjectPublicKey BIT STRING
  33. # }
  34. #
  35. # AlgorithmIdentifier ::= SEQUENCE {
  36. # algorithm OBJECT IDENTIFIER,
  37. # parameters ANY DEFINED BY algorithm OPTIONAL
  38. # }
  39. #
  40. spki = DerSequence().decode(encoded, nr_elements=2)
  41. algo = DerSequence().decode(spki[0], nr_elements=(1,2))
  42. algo_oid = DerObjectId().decode(algo[0])
  43. spk = DerBitString().decode(spki[1]).value
  44. if len(algo) == 1:
  45. algo_params = None
  46. else:
  47. try:
  48. DerNull().decode(algo[1])
  49. algo_params = None
  50. except:
  51. algo_params = algo[1]
  52. return algo_oid.value, spk, algo_params
  53. def _create_subject_public_key_info(algo_oid, secret_key, params=None):
  54. if params is None:
  55. params = DerNull()
  56. spki = DerSequence([
  57. DerSequence([
  58. DerObjectId(algo_oid),
  59. params]),
  60. DerBitString(secret_key)
  61. ])
  62. return spki.encode()
  63. def _extract_subject_public_key_info(x509_certificate):
  64. """Extract subjectPublicKeyInfo from a DER X.509 certificate."""
  65. certificate = DerSequence().decode(x509_certificate, nr_elements=3)
  66. tbs_certificate = DerSequence().decode(certificate[0],
  67. nr_elements=range(6, 11))
  68. index = 5
  69. try:
  70. tbs_certificate[0] + 1
  71. # Version not present
  72. version = 1
  73. except TypeError:
  74. version = DerInteger(explicit=0).decode(tbs_certificate[0]).value
  75. if version not in (2, 3):
  76. raise ValueError("Incorrect X.509 certificate version")
  77. index = 6
  78. return tbs_certificate[index]