__init__.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Crypto.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, public_key, params):
  54. if params is None:
  55. algorithm = DerSequence([DerObjectId(algo_oid)])
  56. else:
  57. algorithm = DerSequence([DerObjectId(algo_oid), params])
  58. spki = DerSequence([algorithm,
  59. DerBitString(public_key)
  60. ])
  61. return spki.encode()
  62. def _extract_subject_public_key_info(x509_certificate):
  63. """Extract subjectPublicKeyInfo from a DER X.509 certificate."""
  64. certificate = DerSequence().decode(x509_certificate, nr_elements=3)
  65. tbs_certificate = DerSequence().decode(certificate[0],
  66. nr_elements=range(6, 11))
  67. index = 5
  68. try:
  69. tbs_certificate[0] + 1
  70. # Version not present
  71. version = 1
  72. except TypeError:
  73. version = DerInteger(explicit=0).decode(tbs_certificate[0]).value
  74. if version not in (2, 3):
  75. raise ValueError("Incorrect X.509 certificate version")
  76. index = 6
  77. return tbs_certificate[index]