test_pem.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # https://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import unittest
  16. from rsa.pem import _markers
  17. import rsa.key
  18. # 512-bit key. Too small for practical purposes, but good enough for testing with.
  19. public_key_pem = """
  20. -----BEGIN PUBLIC KEY-----
  21. MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M
  22. 0c+h4sKMXwjhjbQAZdtWIw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQ==
  23. -----END PUBLIC KEY-----
  24. """
  25. private_key_pem = """
  26. -----BEGIN RSA PRIVATE KEY-----
  27. MIIBOwIBAAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M0c+h4sKMXwjhjbQAZdtW
  28. Iw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQJADwR36EpNzQTqDzusCFIq
  29. ZS+h9X8aIovgBK3RNhMIGO2ThpsnhiDTcqIvgQ56knbl6B2W4iOl54tJ6CNtf6l6
  30. zQIhANTaNLFGsJfOvZHcI0WL1r89+1A4JVxR+lpslJJwAvgDAiEAwsjqqZ2wY2F0
  31. F8p1J98BEbtjU2mEZIVCMn6vQuhWdl8CIDRL4IJl4eGKlB0QP0JJF1wpeGO/R76l
  32. DaPF5cMM7k3NAiEAss28m/ck9BWBfFVdNjx/vsdFZkx2O9AX9EJWoBSnSgECIQCa
  33. +sVQMUVJFGsdE/31C7wCIbE3IpB7ziABZ7mN+V3Dhg==
  34. -----END RSA PRIVATE KEY-----
  35. """
  36. # Private key components
  37. prime1 = 96275860229939261876671084930484419185939191875438854026071315955024109172739
  38. prime2 = 88103681619592083641803383393198542599284510949756076218404908654323473741407
  39. class TestMarkers(unittest.TestCase):
  40. def test_values(self):
  41. self.assertEqual(
  42. _markers("RSA PRIVATE KEY"),
  43. (b"-----BEGIN RSA PRIVATE KEY-----", b"-----END RSA PRIVATE KEY-----"),
  44. )
  45. class TestBytesAndStrings(unittest.TestCase):
  46. """Test that we can use PEM in both Unicode strings and bytes."""
  47. def test_unicode_public(self):
  48. key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
  49. self.assertEqual(prime1 * prime2, key.n)
  50. def test_bytes_public(self):
  51. key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode("ascii"))
  52. self.assertEqual(prime1 * prime2, key.n)
  53. def test_unicode_private(self):
  54. key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
  55. self.assertEqual(prime1 * prime2, key.n)
  56. def test_bytes_private(self):
  57. key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode("ascii"))
  58. self.assertEqual(prime1, key.p)
  59. self.assertEqual(prime2, key.q)
  60. class TestByteOutput(unittest.TestCase):
  61. """Tests that PEM and DER are returned as bytes."""
  62. def test_bytes_public(self):
  63. key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
  64. self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
  65. self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)
  66. def test_bytes_private(self):
  67. key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
  68. self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
  69. self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)
  70. class TestByteInput(unittest.TestCase):
  71. """Tests that PEM and DER can be loaded from bytes."""
  72. def test_bytes_public(self):
  73. key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode("ascii"))
  74. self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
  75. self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)
  76. def test_bytes_private(self):
  77. key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode("ascii"))
  78. self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
  79. self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)