aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/rsa/py3/tests/test_pem.py
blob: 7440431fc0788563f79406366607008a8b20890c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
#  Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import unittest

from rsa.pem import _markers
import rsa.key

# 512-bit key. Too small for practical purposes, but good enough for testing with.
public_key_pem = """
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M
0c+h4sKMXwjhjbQAZdtWIw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQ==
-----END PUBLIC KEY-----
"""

private_key_pem = """
-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M0c+h4sKMXwjhjbQAZdtW
Iw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQJADwR36EpNzQTqDzusCFIq
ZS+h9X8aIovgBK3RNhMIGO2ThpsnhiDTcqIvgQ56knbl6B2W4iOl54tJ6CNtf6l6
zQIhANTaNLFGsJfOvZHcI0WL1r89+1A4JVxR+lpslJJwAvgDAiEAwsjqqZ2wY2F0
F8p1J98BEbtjU2mEZIVCMn6vQuhWdl8CIDRL4IJl4eGKlB0QP0JJF1wpeGO/R76l
DaPF5cMM7k3NAiEAss28m/ck9BWBfFVdNjx/vsdFZkx2O9AX9EJWoBSnSgECIQCa
+sVQMUVJFGsdE/31C7wCIbE3IpB7ziABZ7mN+V3Dhg==
-----END RSA PRIVATE KEY-----
"""

# Private key components
prime1 = 96275860229939261876671084930484419185939191875438854026071315955024109172739
prime2 = 88103681619592083641803383393198542599284510949756076218404908654323473741407


class TestMarkers(unittest.TestCase):
    def test_values(self):
        self.assertEqual(
            _markers("RSA PRIVATE KEY"),
            (b"-----BEGIN RSA PRIVATE KEY-----", b"-----END RSA PRIVATE KEY-----"),
        )


class TestBytesAndStrings(unittest.TestCase):
    """Test that we can use PEM in both Unicode strings and bytes."""

    def test_unicode_public(self):
        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
        self.assertEqual(prime1 * prime2, key.n)

    def test_bytes_public(self):
        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode("ascii"))
        self.assertEqual(prime1 * prime2, key.n)

    def test_unicode_private(self):
        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
        self.assertEqual(prime1 * prime2, key.n)

    def test_bytes_private(self):
        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode("ascii"))
        self.assertEqual(prime1, key.p)
        self.assertEqual(prime2, key.q)


class TestByteOutput(unittest.TestCase):
    """Tests that PEM and DER are returned as bytes."""

    def test_bytes_public(self):
        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
        self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
        self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)

    def test_bytes_private(self):
        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
        self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
        self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)


class TestByteInput(unittest.TestCase):
    """Tests that PEM and DER can be loaded from bytes."""

    def test_bytes_public(self):
        key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode("ascii"))
        self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
        self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)

    def test_bytes_private(self):
        key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode("ascii"))
        self.assertIsInstance(key.save_pkcs1(format="DER"), bytes)
        self.assertIsInstance(key.save_pkcs1(format="PEM"), bytes)