test__cryptography_rsa.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 json
  15. import os
  16. import pickle
  17. from cryptography.hazmat.primitives.asymmetric import rsa
  18. import pytest # type: ignore
  19. from google.auth import _helpers
  20. from google.auth.crypt import _cryptography_rsa
  21. from google.auth.crypt import base
  22. import yatest.common as yc
  23. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "..", "data")
  24. # To generate privatekey.pem, privatekey.pub, and public_cert.pem:
  25. # $ openssl req -new -newkey rsa:1024 -x509 -nodes -out public_cert.pem \
  26. # > -keyout privatekey.pem
  27. # $ openssl rsa -in privatekey.pem -pubout -out privatekey.pub
  28. with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh:
  29. PRIVATE_KEY_BYTES = fh.read()
  30. PKCS1_KEY_BYTES = PRIVATE_KEY_BYTES
  31. with open(os.path.join(DATA_DIR, "privatekey.pub"), "rb") as fh:
  32. PUBLIC_KEY_BYTES = fh.read()
  33. with open(os.path.join(DATA_DIR, "public_cert.pem"), "rb") as fh:
  34. PUBLIC_CERT_BYTES = fh.read()
  35. # To generate pem_from_pkcs12.pem and privatekey.p12:
  36. # $ openssl pkcs12 -export -out privatekey.p12 -inkey privatekey.pem \
  37. # > -in public_cert.pem
  38. # $ openssl pkcs12 -in privatekey.p12 -nocerts -nodes \
  39. # > -out pem_from_pkcs12.pem
  40. with open(os.path.join(DATA_DIR, "pem_from_pkcs12.pem"), "rb") as fh:
  41. PKCS8_KEY_BYTES = fh.read()
  42. with open(os.path.join(DATA_DIR, "privatekey.p12"), "rb") as fh:
  43. PKCS12_KEY_BYTES = fh.read()
  44. # The service account JSON file can be generated from the Google Cloud Console.
  45. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json")
  46. with open(SERVICE_ACCOUNT_JSON_FILE, "rb") as fh:
  47. SERVICE_ACCOUNT_INFO = json.load(fh)
  48. class TestRSAVerifier(object):
  49. def test_verify_success(self):
  50. to_sign = b"foo"
  51. signer = _cryptography_rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
  52. actual_signature = signer.sign(to_sign)
  53. verifier = _cryptography_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  54. assert verifier.verify(to_sign, actual_signature)
  55. def test_verify_unicode_success(self):
  56. to_sign = u"foo"
  57. signer = _cryptography_rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
  58. actual_signature = signer.sign(to_sign)
  59. verifier = _cryptography_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  60. assert verifier.verify(to_sign, actual_signature)
  61. def test_verify_failure(self):
  62. verifier = _cryptography_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  63. bad_signature1 = b""
  64. assert not verifier.verify(b"foo", bad_signature1)
  65. bad_signature2 = b"a"
  66. assert not verifier.verify(b"foo", bad_signature2)
  67. def test_from_string_pub_key(self):
  68. verifier = _cryptography_rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
  69. assert isinstance(verifier, _cryptography_rsa.RSAVerifier)
  70. assert isinstance(verifier._pubkey, rsa.RSAPublicKey)
  71. def test_from_string_pub_key_unicode(self):
  72. public_key = _helpers.from_bytes(PUBLIC_KEY_BYTES)
  73. verifier = _cryptography_rsa.RSAVerifier.from_string(public_key)
  74. assert isinstance(verifier, _cryptography_rsa.RSAVerifier)
  75. assert isinstance(verifier._pubkey, rsa.RSAPublicKey)
  76. def test_from_string_pub_cert(self):
  77. verifier = _cryptography_rsa.RSAVerifier.from_string(PUBLIC_CERT_BYTES)
  78. assert isinstance(verifier, _cryptography_rsa.RSAVerifier)
  79. assert isinstance(verifier._pubkey, rsa.RSAPublicKey)
  80. def test_from_string_pub_cert_unicode(self):
  81. public_cert = _helpers.from_bytes(PUBLIC_CERT_BYTES)
  82. verifier = _cryptography_rsa.RSAVerifier.from_string(public_cert)
  83. assert isinstance(verifier, _cryptography_rsa.RSAVerifier)
  84. assert isinstance(verifier._pubkey, rsa.RSAPublicKey)
  85. class TestRSASigner(object):
  86. def test_from_string_pkcs1(self):
  87. signer = _cryptography_rsa.RSASigner.from_string(PKCS1_KEY_BYTES)
  88. assert isinstance(signer, _cryptography_rsa.RSASigner)
  89. assert isinstance(signer._key, rsa.RSAPrivateKey)
  90. def test_from_string_pkcs1_unicode(self):
  91. key_bytes = _helpers.from_bytes(PKCS1_KEY_BYTES)
  92. signer = _cryptography_rsa.RSASigner.from_string(key_bytes)
  93. assert isinstance(signer, _cryptography_rsa.RSASigner)
  94. assert isinstance(signer._key, rsa.RSAPrivateKey)
  95. def test_from_string_pkcs8(self):
  96. signer = _cryptography_rsa.RSASigner.from_string(PKCS8_KEY_BYTES)
  97. assert isinstance(signer, _cryptography_rsa.RSASigner)
  98. assert isinstance(signer._key, rsa.RSAPrivateKey)
  99. def test_from_string_pkcs8_unicode(self):
  100. key_bytes = _helpers.from_bytes(PKCS8_KEY_BYTES)
  101. signer = _cryptography_rsa.RSASigner.from_string(key_bytes)
  102. assert isinstance(signer, _cryptography_rsa.RSASigner)
  103. assert isinstance(signer._key, rsa.RSAPrivateKey)
  104. def test_from_string_pkcs12(self):
  105. with pytest.raises(ValueError):
  106. _cryptography_rsa.RSASigner.from_string(PKCS12_KEY_BYTES)
  107. def test_from_string_bogus_key(self):
  108. key_bytes = "bogus-key"
  109. with pytest.raises(ValueError):
  110. _cryptography_rsa.RSASigner.from_string(key_bytes)
  111. def test_from_service_account_info(self):
  112. signer = _cryptography_rsa.RSASigner.from_service_account_info(
  113. SERVICE_ACCOUNT_INFO
  114. )
  115. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  116. assert isinstance(signer._key, rsa.RSAPrivateKey)
  117. def test_from_service_account_info_missing_key(self):
  118. with pytest.raises(ValueError) as excinfo:
  119. _cryptography_rsa.RSASigner.from_service_account_info({})
  120. assert excinfo.match(base._JSON_FILE_PRIVATE_KEY)
  121. def test_from_service_account_file(self):
  122. signer = _cryptography_rsa.RSASigner.from_service_account_file(
  123. SERVICE_ACCOUNT_JSON_FILE
  124. )
  125. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  126. assert isinstance(signer._key, rsa.RSAPrivateKey)
  127. def test_pickle(self):
  128. signer = _cryptography_rsa.RSASigner.from_service_account_file(
  129. SERVICE_ACCOUNT_JSON_FILE
  130. )
  131. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  132. assert isinstance(signer._key, rsa.RSAPrivateKey)
  133. pickled_signer = pickle.dumps(signer)
  134. signer = pickle.loads(pickled_signer)
  135. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  136. assert isinstance(signer._key, rsa.RSAPrivateKey)