|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain 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, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for oauth2client._pure_python_crypt.""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import mock |
| 20 | +from pyasn1_modules import pem |
| 21 | +import rsa |
| 22 | +import six |
| 23 | +import unittest2 |
| 24 | + |
| 25 | +from oauth2client._helpers import _from_bytes |
| 26 | +from oauth2client import _pure_python_crypt |
| 27 | +from oauth2client.crypt import RsaSigner |
| 28 | +from oauth2client.crypt import RsaVerifier |
| 29 | + |
| 30 | + |
| 31 | +class TestRsaVerifier(unittest2.TestCase): |
| 32 | + |
| 33 | + PUBLIC_KEY_FILENAME = os.path.join(os.path.dirname(__file__), |
| 34 | + 'data', 'privatekey.pub') |
| 35 | + PUBLIC_CERT_FILENAME = os.path.join(os.path.dirname(__file__), |
| 36 | + 'data', 'public_cert.pem') |
| 37 | + PRIVATE_KEY_FILENAME = os.path.join(os.path.dirname(__file__), |
| 38 | + 'data', 'privatekey.pem') |
| 39 | + |
| 40 | + def _load_public_key_bytes(self): |
| 41 | + with open(self.PUBLIC_KEY_FILENAME, 'rb') as fh: |
| 42 | + return fh.read() |
| 43 | + |
| 44 | + def _load_public_cert_bytes(self): |
| 45 | + with open(self.PUBLIC_CERT_FILENAME, 'rb') as fh: |
| 46 | + return fh.read() |
| 47 | + |
| 48 | + def _load_private_key_bytes(self): |
| 49 | + with open(self.PRIVATE_KEY_FILENAME, 'rb') as fh: |
| 50 | + return fh.read() |
| 51 | + |
| 52 | + def test_verify_success(self): |
| 53 | + to_sign = b'foo' |
| 54 | + signer = RsaSigner.from_string(self._load_private_key_bytes()) |
| 55 | + actual_signature = signer.sign(to_sign) |
| 56 | + |
| 57 | + verifier = RsaVerifier.from_string(self._load_public_key_bytes(), |
| 58 | + is_x509_cert=False) |
| 59 | + self.assertTrue(verifier.verify(to_sign, actual_signature)) |
| 60 | + |
| 61 | + def test_verify_unicode_success(self): |
| 62 | + to_sign = u'foo' |
| 63 | + signer = RsaSigner.from_string(self._load_private_key_bytes()) |
| 64 | + actual_signature = signer.sign(to_sign) |
| 65 | + |
| 66 | + verifier = RsaVerifier.from_string(self._load_public_key_bytes(), |
| 67 | + is_x509_cert=False) |
| 68 | + self.assertTrue(verifier.verify(to_sign, actual_signature)) |
| 69 | + |
| 70 | + def test_verify_failure(self): |
| 71 | + verifier = RsaVerifier.from_string(self._load_public_key_bytes(), |
| 72 | + is_x509_cert=False) |
| 73 | + bad_signature1 = b'' |
| 74 | + self.assertFalse(verifier.verify(b'foo', bad_signature1)) |
| 75 | + bad_signature2 = b'a' |
| 76 | + self.assertFalse(verifier.verify(b'foo', bad_signature2)) |
| 77 | + |
| 78 | + def test_from_string_pub_key(self): |
| 79 | + public_key = self._load_public_key_bytes() |
| 80 | + verifier = RsaVerifier.from_string(public_key, is_x509_cert=False) |
| 81 | + self.assertIsInstance(verifier, RsaVerifier) |
| 82 | + self.assertIsInstance(verifier._pubkey, rsa.key.PublicKey) |
| 83 | + |
| 84 | + def test_from_string_pub_key_unicode(self): |
| 85 | + public_key = _from_bytes(self._load_public_key_bytes()) |
| 86 | + verifier = RsaVerifier.from_string(public_key, is_x509_cert=False) |
| 87 | + self.assertIsInstance(verifier, RsaVerifier) |
| 88 | + self.assertIsInstance(verifier._pubkey, rsa.key.PublicKey) |
| 89 | + |
| 90 | + def test_from_string_pub_cert(self): |
| 91 | + public_cert = self._load_public_cert_bytes() |
| 92 | + verifier = RsaVerifier.from_string(public_cert, is_x509_cert=True) |
| 93 | + self.assertIsInstance(verifier, RsaVerifier) |
| 94 | + self.assertIsInstance(verifier._pubkey, rsa.key.PublicKey) |
| 95 | + |
| 96 | + def test_from_string_pub_cert_unicode(self): |
| 97 | + public_cert = _from_bytes(self._load_public_cert_bytes()) |
| 98 | + verifier = RsaVerifier.from_string(public_cert, is_x509_cert=True) |
| 99 | + self.assertIsInstance(verifier, RsaVerifier) |
| 100 | + self.assertIsInstance(verifier._pubkey, rsa.key.PublicKey) |
| 101 | + |
| 102 | + def test_from_string_pub_cert_failure(self): |
| 103 | + cert_bytes = self._load_public_cert_bytes() |
| 104 | + true_der = rsa.pem.load_pem(cert_bytes, 'CERTIFICATE') |
| 105 | + with mock.patch('rsa.pem.load_pem', |
| 106 | + return_value=true_der + b'extra') as load_pem: |
| 107 | + with self.assertRaises(ValueError): |
| 108 | + RsaVerifier.from_string(cert_bytes, is_x509_cert=True) |
| 109 | + load_pem.assert_called_once_with(cert_bytes, 'CERTIFICATE') |
| 110 | + |
| 111 | + |
| 112 | +class TestRsaSigner(unittest2.TestCase): |
| 113 | + |
| 114 | + PKCS1_KEY_FILENAME = os.path.join(os.path.dirname(__file__), |
| 115 | + 'data', 'privatekey.pem') |
| 116 | + PKCS8_KEY_FILENAME = os.path.join(os.path.dirname(__file__), |
| 117 | + 'data', 'pem_from_pkcs12.pem') |
| 118 | + PKCS12_KEY_FILENAME = os.path.join(os.path.dirname(__file__), |
| 119 | + 'data', 'privatekey.p12') |
| 120 | + |
| 121 | + def _load_pkcs1_key_bytes(self): |
| 122 | + with open(self.PKCS1_KEY_FILENAME, 'rb') as fh: |
| 123 | + return fh.read() |
| 124 | + |
| 125 | + def _load_pkcs8_key_bytes(self): |
| 126 | + with open(self.PKCS8_KEY_FILENAME, 'rb') as fh: |
| 127 | + return fh.read() |
| 128 | + |
| 129 | + def _load_pkcs12_key_bytes(self): |
| 130 | + with open(self.PKCS12_KEY_FILENAME, 'rb') as fh: |
| 131 | + return fh.read() |
| 132 | + |
| 133 | + def test_from_string_pkcs1(self): |
| 134 | + key_bytes = self._load_pkcs1_key_bytes() |
| 135 | + signer = RsaSigner.from_string(key_bytes) |
| 136 | + self.assertIsInstance(signer, RsaSigner) |
| 137 | + self.assertIsInstance(signer._key, rsa.key.PrivateKey) |
| 138 | + |
| 139 | + def test_from_string_pkcs1_unicode(self): |
| 140 | + key_bytes = _from_bytes(self._load_pkcs1_key_bytes()) |
| 141 | + signer = RsaSigner.from_string(key_bytes) |
| 142 | + self.assertIsInstance(signer, RsaSigner) |
| 143 | + self.assertIsInstance(signer._key, rsa.key.PrivateKey) |
| 144 | + |
| 145 | + def test_from_string_pkcs8(self): |
| 146 | + key_bytes = self._load_pkcs8_key_bytes() |
| 147 | + signer = RsaSigner.from_string(key_bytes) |
| 148 | + self.assertIsInstance(signer, RsaSigner) |
| 149 | + self.assertIsInstance(signer._key, rsa.key.PrivateKey) |
| 150 | + |
| 151 | + def test_from_string_pkcs8_extra_bytes(self): |
| 152 | + key_bytes = self._load_pkcs8_key_bytes() |
| 153 | + _, pem_bytes = pem.readPemBlocksFromFile( |
| 154 | + six.StringIO(_from_bytes(key_bytes)), |
| 155 | + _pure_python_crypt._PKCS8_MARKER) |
| 156 | + |
| 157 | + with mock.patch('pyasn1.codec.der.decoder.decode') as mock_decode: |
| 158 | + key_info, remaining = None, 'extra' |
| 159 | + mock_decode.return_value = (key_info, remaining) |
| 160 | + with self.assertRaises(ValueError): |
| 161 | + RsaSigner.from_string(key_bytes) |
| 162 | + # Verify mock was called. |
| 163 | + mock_decode.assert_called_once_with( |
| 164 | + pem_bytes, asn1Spec=_pure_python_crypt._PKCS8_SPEC) |
| 165 | + |
| 166 | + def test_from_string_pkcs8_unicode(self): |
| 167 | + key_bytes = _from_bytes(self._load_pkcs8_key_bytes()) |
| 168 | + signer = RsaSigner.from_string(key_bytes) |
| 169 | + self.assertIsInstance(signer, RsaSigner) |
| 170 | + self.assertIsInstance(signer._key, rsa.key.PrivateKey) |
| 171 | + |
| 172 | + def test_from_string_pkcs12(self): |
| 173 | + key_bytes = self._load_pkcs12_key_bytes() |
| 174 | + with self.assertRaises(ValueError): |
| 175 | + RsaSigner.from_string(key_bytes) |
| 176 | + |
| 177 | + |
| 178 | +if __name__ == '__main__': # pragma: NO COVER |
| 179 | + unittest2.main() |
0 commit comments