blob: b9bd93c429991f2a8304561020867aacdc193b38 [file] [log] [blame]
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +05301#!/usr/bin/env python
2# -*- coding: utf-8 -*-
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +01003#
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#
Sybren A. Stüvel3934ab42016-02-05 16:01:20 +010010# https://www.apache.org/licenses/LICENSE-2.0
Roy Kokkelkoren0659aac2015-10-25 16:12:11 +010011#
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.
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053017
Sybren A. Stüveled1c81d2016-01-14 12:23:32 +010018import unittest
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010019
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053020from rsa.pem import _markers
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010021import rsa.key
22
23# 512-bit key. Too small for practical purposes, but good enough for testing with.
24public_key_pem = '''
25-----BEGIN PUBLIC KEY-----
26MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M
270c+h4sKMXwjhjbQAZdtWIw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQ==
28-----END PUBLIC KEY-----
29'''
30
31private_key_pem = '''
32-----BEGIN RSA PRIVATE KEY-----
33MIIBOwIBAAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M0c+h4sKMXwjhjbQAZdtW
34Iw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQJADwR36EpNzQTqDzusCFIq
35ZS+h9X8aIovgBK3RNhMIGO2ThpsnhiDTcqIvgQ56knbl6B2W4iOl54tJ6CNtf6l6
36zQIhANTaNLFGsJfOvZHcI0WL1r89+1A4JVxR+lpslJJwAvgDAiEAwsjqqZ2wY2F0
37F8p1J98BEbtjU2mEZIVCMn6vQuhWdl8CIDRL4IJl4eGKlB0QP0JJF1wpeGO/R76l
38DaPF5cMM7k3NAiEAss28m/ck9BWBfFVdNjx/vsdFZkx2O9AX9EJWoBSnSgECIQCa
39+sVQMUVJFGsdE/31C7wCIbE3IpB7ziABZ7mN+V3Dhg==
40-----END RSA PRIVATE KEY-----
41'''
42
43# Private key components
44prime1 = 96275860229939261876671084930484419185939191875438854026071315955024109172739
45prime2 = 88103681619592083641803383393198542599284510949756076218404908654323473741407
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053046
47
Sybren A. Stüveld3d10342016-01-22 11:36:06 +010048class TestMarkers(unittest.TestCase):
Yesudeep Mangalapilly5bfe5ff2011-08-16 14:58:32 +053049 def test_values(self):
50 self.assertEqual(_markers('RSA PRIVATE KEY'),
adamantike9f577402016-05-08 15:36:57 -030051 (b'-----BEGIN RSA PRIVATE KEY-----',
52 b'-----END RSA PRIVATE KEY-----'))
Sybren A. Stüvelbd4e2132016-03-17 12:35:48 +010053
54
55class TestBytesAndStrings(unittest.TestCase):
56 """Test that we can use PEM in both Unicode strings and bytes."""
57
58 def test_unicode_public(self):
59 key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
60 self.assertEqual(prime1 * prime2, key.n)
61
62 def test_bytes_public(self):
63 key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii'))
64 self.assertEqual(prime1 * prime2, key.n)
65
66 def test_unicode_private(self):
67 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
68 self.assertEqual(prime1 * prime2, key.n)
69
70 def test_bytes_private(self):
71 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
72 self.assertEqual(prime1, key.p)
73 self.assertEqual(prime2, key.q)
Sybren A. Stüvelfedcaa12016-03-17 14:34:22 +010074
75
76class TestByteOutput(unittest.TestCase):
77 """Tests that PEM and DER are returned as bytes."""
78
79 def test_bytes_public(self):
80 key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem)
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020081 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
82 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelfedcaa12016-03-17 14:34:22 +010083
84 def test_bytes_private(self):
85 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem)
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020086 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
87 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelf0627be2016-03-17 15:52:23 +010088
89
90class TestByteInput(unittest.TestCase):
91 """Tests that PEM and DER can be loaded from bytes."""
92
93 def test_bytes_public(self):
94 key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii'))
Sybren A. Stüvelded036c2019-08-04 15:02:20 +020095 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
96 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)
Sybren A. Stüvelf0627be2016-03-17 15:52:23 +010097
98 def test_bytes_private(self):
99 key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii'))
Sybren A. Stüvelded036c2019-08-04 15:02:20 +0200100 self.assertIsInstance(key.save_pkcs1(format='DER'), bytes)
101 self.assertIsInstance(key.save_pkcs1(format='PEM'), bytes)