test__python_rsa.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Copyright 2016 Google LLC
  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. import io
  15. import json
  16. import os
  17. import mock
  18. from pyasn1_modules import pem # type: ignore
  19. import pytest # type: ignore
  20. import rsa # type: ignore
  21. from google.auth import _helpers
  22. from google.auth.crypt import _python_rsa
  23. from google.auth.crypt import base
  24. import yatest.common as yc
  25. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "..", "data")
  26. # To generate privatekey.pem, privatekey.pub, and public_cert.pem:
  27. # $ openssl req -new -newkey rsa:1024 -x509 -nodes -out public_cert.pem \
  28. # > -keyout privatekey.pem
  29. # $ openssl rsa -in privatekey.pem -pubout -out privatekey.pub
  30. with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh:
  31. PRIVATE_KEY_BYTES = fh.read()
  32. PKCS1_KEY_BYTES = PRIVATE_KEY_BYTES
  33. with open(os.path.join(DATA_DIR, "privatekey.pub"), "rb") as fh:
  34. PUBLIC_KEY_BYTES = fh.read()
  35. with open(os.path.join(DATA_DIR, "public_cert.pem"), "rb") as fh:
  36. PUBLIC_CERT_BYTES = fh.read()
  37. # To generate pem_from_pkcs12.pem and privatekey.p12:
  38. # $ openssl pkcs12 -export -out privatekey.p12 -inkey privatekey.pem \
  39. # > -in public_cert.pem
  40. # $ openssl pkcs12 -in privatekey.p12 -nocerts -nodes \
  41. # > -out pem_from_pkcs12.pem
  42. with open(os.path.join(DATA_DIR, "pem_from_pkcs12.pem"), "rb") as fh:
  43. PKCS8_KEY_BYTES = fh.read()
  44. with open(os.path.join(DATA_DIR, "privatekey.p12"), "rb") as fh:
  45. PKCS12_KEY_BYTES = fh.read()
  46. # The service account JSON file can be generated from the Google Cloud Console.
  47. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
  48. with open(SERVICE_ACCOUNT_JSON_FILE, "rb") as fh:
  49. SERVICE_ACCOUNT_INFO = json.load(fh)
  50. class TestRSAVerifier(object):
  51. def test_verify_success(self):
  52. to_sign = b"foo"
  53. signer = _python_rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
  54. actual_signature = signer.sign(to_sign)
  55. verifier = _python_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  56. assert verifier.verify(to_sign, actual_signature)
  57. def test_verify_unicode_success(self):
  58. to_sign = u"foo"
  59. signer = _python_rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
  60. actual_signature = signer.sign(to_sign)
  61. verifier = _python_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  62. assert verifier.verify(to_sign, actual_signature)
  63. def test_verify_failure(self):
  64. verifier = _python_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  65. bad_signature1 = b""
  66. assert not verifier.verify(b"foo", bad_signature1)
  67. bad_signature2 = b"a"
  68. assert not verifier.verify(b"foo", bad_signature2)
  69. def test_from_string_pub_key(self):
  70. verifier = _python_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  71. assert isinstance(verifier, _python_rsa.RSAVerifier)
  72. assert isinstance(verifier._pubkey, rsa.key.PublicKey)
  73. def test_from_string_pub_key_unicode(self):
  74. public_key = _helpers.from_bytes(PUBLIC_KEY_BYTES)
  75. verifier = _python_rsa.RSAVerifier.from_string(public_key)
  76. assert isinstance(verifier, _python_rsa.RSAVerifier)
  77. assert isinstance(verifier._pubkey, rsa.key.PublicKey)
  78. def test_from_string_pub_cert(self):
  79. verifier = _python_rsa.RSAVerifier.from_string(PUBLIC_CERT_BYTES)
  80. assert isinstance(verifier, _python_rsa.RSAVerifier)
  81. assert isinstance(verifier._pubkey, rsa.key.PublicKey)
  82. def test_from_string_pub_cert_unicode(self):
  83. public_cert = _helpers.from_bytes(PUBLIC_CERT_BYTES)
  84. verifier = _python_rsa.RSAVerifier.from_string(public_cert)
  85. assert isinstance(verifier, _python_rsa.RSAVerifier)
  86. assert isinstance(verifier._pubkey, rsa.key.PublicKey)
  87. def test_from_string_pub_cert_failure(self):
  88. cert_bytes = PUBLIC_CERT_BYTES
  89. true_der = rsa.pem.load_pem(cert_bytes, "CERTIFICATE")
  90. load_pem_patch = mock.patch(
  91. "rsa.pem.load_pem", return_value=true_der + b"extra", autospec=True
  92. )
  93. with load_pem_patch as load_pem:
  94. with pytest.raises(ValueError):
  95. _python_rsa.RSAVerifier.from_string(cert_bytes)
  96. load_pem.assert_called_once_with(cert_bytes, "CERTIFICATE")
  97. class TestRSASigner(object):
  98. def test_from_string_pkcs1(self):
  99. signer = _python_rsa.RSASigner.from_string(PKCS1_KEY_BYTES)
  100. assert isinstance(signer, _python_rsa.RSASigner)
  101. assert isinstance(signer._key, rsa.key.PrivateKey)
  102. def test_from_string_pkcs1_unicode(self):
  103. key_bytes = _helpers.from_bytes(PKCS1_KEY_BYTES)
  104. signer = _python_rsa.RSASigner.from_string(key_bytes)
  105. assert isinstance(signer, _python_rsa.RSASigner)
  106. assert isinstance(signer._key, rsa.key.PrivateKey)
  107. def test_from_string_pkcs8(self):
  108. signer = _python_rsa.RSASigner.from_string(PKCS8_KEY_BYTES)
  109. assert isinstance(signer, _python_rsa.RSASigner)
  110. assert isinstance(signer._key, rsa.key.PrivateKey)
  111. def test_from_string_pkcs8_extra_bytes(self):
  112. key_bytes = PKCS8_KEY_BYTES
  113. _, pem_bytes = pem.readPemBlocksFromFile(
  114. io.StringIO(_helpers.from_bytes(key_bytes)), _python_rsa._PKCS8_MARKER
  115. )
  116. key_info, remaining = None, "extra"
  117. decode_patch = mock.patch(
  118. "pyasn1.codec.der.decoder.decode",
  119. return_value=(key_info, remaining),
  120. autospec=True,
  121. )
  122. with decode_patch as decode:
  123. with pytest.raises(ValueError):
  124. _python_rsa.RSASigner.from_string(key_bytes)
  125. # Verify mock was called.
  126. decode.assert_called_once_with(pem_bytes, asn1Spec=_python_rsa._PKCS8_SPEC)
  127. def test_from_string_pkcs8_unicode(self):
  128. key_bytes = _helpers.from_bytes(PKCS8_KEY_BYTES)
  129. signer = _python_rsa.RSASigner.from_string(key_bytes)
  130. assert isinstance(signer, _python_rsa.RSASigner)
  131. assert isinstance(signer._key, rsa.key.PrivateKey)
  132. def test_from_string_pkcs12(self):
  133. with pytest.raises(ValueError):
  134. _python_rsa.RSASigner.from_string(PKCS12_KEY_BYTES)
  135. def test_from_string_bogus_key(self):
  136. key_bytes = "bogus-key"
  137. with pytest.raises(ValueError):
  138. _python_rsa.RSASigner.from_string(key_bytes)
  139. def test_from_service_account_info(self):
  140. signer = _python_rsa.RSASigner.from_service_account_info(SERVICE_ACCOUNT_INFO)
  141. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  142. assert isinstance(signer._key, rsa.key.PrivateKey)
  143. def test_from_service_account_info_missing_key(self):
  144. with pytest.raises(ValueError) as excinfo:
  145. _python_rsa.RSASigner.from_service_account_info({})
  146. assert excinfo.match(base._JSON_FILE_PRIVATE_KEY)
  147. def test_from_service_account_file(self):
  148. signer = _python_rsa.RSASigner.from_service_account_file(
  149. SERVICE_ACCOUNT_JSON_FILE
  150. )
  151. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  152. assert isinstance(signer._key, rsa.key.PrivateKey)