test__cryptography_rsa.py 6.4 KB

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