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