• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import unittest
17
18from rsa.pem import _markers
19import rsa.key
20
21# 512-bit key. Too small for practical purposes, but good enough for testing with.
22public_key_pem = '''
23-----BEGIN PUBLIC KEY-----
24MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M
250c+h4sKMXwjhjbQAZdtWIw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQ==
26-----END PUBLIC KEY-----
27'''
28
29private_key_pem = '''
30-----BEGIN RSA PRIVATE KEY-----
31MIIBOwIBAAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M0c+h4sKMXwjhjbQAZdtW
32Iw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQJADwR36EpNzQTqDzusCFIq
33ZS+h9X8aIovgBK3RNhMIGO2ThpsnhiDTcqIvgQ56knbl6B2W4iOl54tJ6CNtf6l6
34zQIhANTaNLFGsJfOvZHcI0WL1r89+1A4JVxR+lpslJJwAvgDAiEAwsjqqZ2wY2F0
35F8p1J98BEbtjU2mEZIVCMn6vQuhWdl8CIDRL4IJl4eGKlB0QP0JJF1wpeGO/R76l
36DaPF5cMM7k3NAiEAss28m/ck9BWBfFVdNjx/vsdFZkx2O9AX9EJWoBSnSgECIQCa
37+sVQMUVJFGsdE/31C7wCIbE3IpB7ziABZ7mN+V3Dhg==
38-----END RSA PRIVATE KEY-----
39'''
40
41# Private key components
42prime1 = 96275860229939261876671084930484419185939191875438854026071315955024109172739
43prime2 = 88103681619592083641803383393198542599284510949756076218404908654323473741407
44
45
46class TestMarkers(unittest.TestCase):
47    def test_values(self):
48        self.assertEqual(_markers('RSA PRIVATE KEY'),
49                         (b'-----BEGIN RSA PRIVATE KEY-----',
50                          b'-----END RSA PRIVATE KEY-----'))
51
52
53class TestBytesAndStrings(unittest.TestCase):
54    """Test that we can use PEM in both Unicode strings and bytes."""
55
56    def test_unicode_public(self):
57        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
58        self.assertEqual(prime1 * prime2, key.n)
59
60    def test_bytes_public(self):
61        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii'))
62        self.assertEqual(prime1 * prime2, key.n)
63
64    def test_unicode_private(self):
65        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
66        self.assertEqual(prime1 * prime2, key.n)
67
68    def test_bytes_private(self):
69        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
70        self.assertEqual(prime1, key.p)
71        self.assertEqual(prime2, key.q)
72
73
74class TestByteOutput(unittest.TestCase):
75    """Tests that PEM and DER are returned as bytes."""
76
77    def test_bytes_public(self):
78        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
79        self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
80        self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
81
82    def test_bytes_private(self):
83        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
84        self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
85        self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
86
87
88class TestByteInput(unittest.TestCase):
89    """Tests that PEM and DER can be loaded from bytes."""
90
91    def test_bytes_public(self):
92        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii'))
93        self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
94        self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
95
96    def test_bytes_private(self):
97        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
98        self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
99        self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
100