blob: dd03cca99e1b8d6f0b9bd2d6f33db1b0ea8d4ebe [file] [log] [blame]
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +05301#!/usr/bin/env python
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01002# 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#
Sybren A. Stüvel3934ab42016-02-05 16:01:20 +01008# https://www.apache.org/licenses/LICENSE-2.0
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01009#
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.
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053015
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010016import unittest
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010017
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053018from rsa.pem import _markers
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010019import 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
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053044
45
Sybren A. Stüveld3d10342016-01-22 11:36:06 +010046class TestMarkers(unittest.TestCase):
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053047 def test_values(self):
48 self.assertEqual(_markers('RSA PRIVATE KEY'),
adamantike9f577402016-05-08 15:36:57 -030049 (b'-----BEGIN RSA PRIVATE KEY-----',
50 b'-----END RSA PRIVATE KEY-----'))
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010051
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)
Sybren A. Stüvelfedcaa12016-03-17 14:34:22 +010072
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)
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020079 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
80 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelfedcaa12016-03-17 14:34:22 +010081
82 def test_bytes_private(self):
83 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020084 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
85 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelf0627be2016-03-17 15:52:23 +010086
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'))
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020093 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
94 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelf0627be2016-03-17 15:52:23 +010095
96 def test_bytes_private(self):
97 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020098 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
99 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)