James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 1 | # Copyright 2017 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Clark Boylan | 5ac7638 | 2018-02-06 15:18:45 -0800 | [diff] [blame] | 15 | import fixtures |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 16 | import os |
| 17 | import subprocess |
| 18 | import tempfile |
| 19 | |
| 20 | from zuul.lib import encryption |
| 21 | |
| 22 | from tests.base import BaseTestCase |
| 23 | |
| 24 | |
| 25 | class TestEncryption(BaseTestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | super(TestEncryption, self).setUp() |
| 29 | self.private, self.public = encryption.generate_rsa_keypair() |
Clark Boylan | 5ac7638 | 2018-02-06 15:18:45 -0800 | [diff] [blame] | 30 | # Because we set delete to False when using NamedTemporaryFile below |
| 31 | # we need to stick our usage of temporary files in the NestedTempfile |
| 32 | # fixture ensuring everything gets cleaned up when it is done. |
| 33 | self.useFixture(fixtures.NestedTempfile()) |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 34 | |
| 35 | def test_serialization(self): |
| 36 | "Verify key serialization" |
| 37 | pem_private = encryption.serialize_rsa_private_key(self.private) |
| 38 | private2, public2 = encryption.deserialize_rsa_keypair(pem_private) |
| 39 | |
| 40 | # cryptography public / private key objects don't implement |
| 41 | # equality testing, so we make sure they have the same numbers. |
| 42 | self.assertEqual(self.private.private_numbers(), |
| 43 | private2.private_numbers()) |
| 44 | self.assertEqual(self.public.public_numbers(), |
| 45 | public2.public_numbers()) |
| 46 | |
James E. Blair | 717e8e9 | 2017-03-17 11:03:27 -0700 | [diff] [blame] | 47 | def test_pkcs1_oaep(self): |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 48 | "Verify encryption and decryption" |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 49 | orig_plaintext = b"some text to encrypt" |
James E. Blair | 717e8e9 | 2017-03-17 11:03:27 -0700 | [diff] [blame] | 50 | ciphertext = encryption.encrypt_pkcs1_oaep(orig_plaintext, self.public) |
| 51 | plaintext = encryption.decrypt_pkcs1_oaep(ciphertext, self.private) |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 52 | self.assertEqual(orig_plaintext, plaintext) |
| 53 | |
James E. Blair | 717e8e9 | 2017-03-17 11:03:27 -0700 | [diff] [blame] | 54 | def test_openssl_pkcs1_oaep(self): |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 55 | "Verify that we can decrypt something encrypted with OpenSSL" |
Clint Byrum | f322fe2 | 2017-05-10 20:53:12 -0700 | [diff] [blame] | 56 | orig_plaintext = b"some text to encrypt" |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 57 | pem_public = encryption.serialize_rsa_public_key(self.public) |
| 58 | public_file = tempfile.NamedTemporaryFile(delete=False) |
| 59 | try: |
| 60 | public_file.write(pem_public) |
| 61 | public_file.close() |
| 62 | |
| 63 | p = subprocess.Popen(['openssl', 'rsautl', '-encrypt', |
| 64 | '-oaep', '-pubin', '-inkey', |
| 65 | public_file.name], |
| 66 | stdin=subprocess.PIPE, |
| 67 | stdout=subprocess.PIPE) |
| 68 | (stdout, stderr) = p.communicate(orig_plaintext) |
| 69 | ciphertext = stdout |
| 70 | finally: |
| 71 | os.unlink(public_file.name) |
| 72 | |
James E. Blair | 717e8e9 | 2017-03-17 11:03:27 -0700 | [diff] [blame] | 73 | plaintext = encryption.decrypt_pkcs1_oaep(ciphertext, self.private) |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 74 | self.assertEqual(orig_plaintext, plaintext) |