test_pem.py 3.8 KB

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