test_es256.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright 2016 Google Inc.
  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 base64
  15. import json
  16. import os
  17. import pickle
  18. from cryptography.hazmat.primitives.asymmetric import ec
  19. import pytest # type: ignore
  20. from google.auth import _helpers
  21. from google.auth.crypt import base
  22. from google.auth.crypt import es256
  23. import yatest.common as yc
  24. DATA_DIR = os.path.join(os.path.dirname(yc.source_path(__file__)), "..", "data")
  25. # To generate es256_privatekey.pem, es256_privatekey.pub, and
  26. # es256_public_cert.pem:
  27. # $ openssl ecparam -genkey -name prime256v1 -noout -out es256_privatekey.pem
  28. # $ openssl ec -in es256-private-key.pem -pubout -out es256-publickey.pem
  29. # $ openssl req -new -x509 -key es256_privatekey.pem -out \
  30. # > es256_public_cert.pem
  31. with open(os.path.join(DATA_DIR, "es256_privatekey.pem"), "rb") as fh:
  32. PRIVATE_KEY_BYTES = fh.read()
  33. PKCS1_KEY_BYTES = PRIVATE_KEY_BYTES
  34. with open(os.path.join(DATA_DIR, "es256_publickey.pem"), "rb") as fh:
  35. PUBLIC_KEY_BYTES = fh.read()
  36. with open(os.path.join(DATA_DIR, "es256_public_cert.pem"), "rb") as fh:
  37. PUBLIC_CERT_BYTES = fh.read()
  38. SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "es256_service_account.json")
  39. with open(SERVICE_ACCOUNT_JSON_FILE, "rb") as fh:
  40. SERVICE_ACCOUNT_INFO = json.load(fh)
  41. class TestES256Verifier(object):
  42. def test_verify_success(self):
  43. to_sign = b"foo"
  44. signer = es256.ES256Signer.from_string(PRIVATE_KEY_BYTES)
  45. actual_signature = signer.sign(to_sign)
  46. verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES)
  47. assert verifier.verify(to_sign, actual_signature)
  48. def test_verify_unicode_success(self):
  49. to_sign = u"foo"
  50. signer = es256.ES256Signer.from_string(PRIVATE_KEY_BYTES)
  51. actual_signature = signer.sign(to_sign)
  52. verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES)
  53. assert verifier.verify(to_sign, actual_signature)
  54. def test_verify_failure(self):
  55. verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES)
  56. bad_signature1 = b""
  57. assert not verifier.verify(b"foo", bad_signature1)
  58. bad_signature2 = b"a"
  59. assert not verifier.verify(b"foo", bad_signature2)
  60. def test_verify_failure_with_wrong_raw_signature(self):
  61. to_sign = b"foo"
  62. # This signature has a wrong "r" value in the "(r,s)" raw signature.
  63. wrong_signature = base64.urlsafe_b64decode(
  64. b"m7oaRxUDeYqjZ8qiMwo0PZLTMZWKJLFQREpqce1StMIa_yXQQ-C5WgeIRHW7OqlYSDL0XbUrj_uAw9i-QhfOJQ=="
  65. )
  66. verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES)
  67. assert not verifier.verify(to_sign, wrong_signature)
  68. def test_from_string_pub_key(self):
  69. verifier = es256.ES256Verifier.from_string(PUBLIC_KEY_BYTES)
  70. assert isinstance(verifier, es256.ES256Verifier)
  71. assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
  72. def test_from_string_pub_key_unicode(self):
  73. public_key = _helpers.from_bytes(PUBLIC_KEY_BYTES)
  74. verifier = es256.ES256Verifier.from_string(public_key)
  75. assert isinstance(verifier, es256.ES256Verifier)
  76. assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
  77. def test_from_string_pub_cert(self):
  78. verifier = es256.ES256Verifier.from_string(PUBLIC_CERT_BYTES)
  79. assert isinstance(verifier, es256.ES256Verifier)
  80. assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
  81. def test_from_string_pub_cert_unicode(self):
  82. public_cert = _helpers.from_bytes(PUBLIC_CERT_BYTES)
  83. verifier = es256.ES256Verifier.from_string(public_cert)
  84. assert isinstance(verifier, es256.ES256Verifier)
  85. assert isinstance(verifier._pubkey, ec.EllipticCurvePublicKey)
  86. class TestES256Signer(object):
  87. def test_from_string_pkcs1(self):
  88. signer = es256.ES256Signer.from_string(PKCS1_KEY_BYTES)
  89. assert isinstance(signer, es256.ES256Signer)
  90. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
  91. def test_from_string_pkcs1_unicode(self):
  92. key_bytes = _helpers.from_bytes(PKCS1_KEY_BYTES)
  93. signer = es256.ES256Signer.from_string(key_bytes)
  94. assert isinstance(signer, es256.ES256Signer)
  95. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
  96. def test_from_string_bogus_key(self):
  97. key_bytes = "bogus-key"
  98. with pytest.raises(ValueError):
  99. es256.ES256Signer.from_string(key_bytes)
  100. def test_from_service_account_info(self):
  101. signer = es256.ES256Signer.from_service_account_info(SERVICE_ACCOUNT_INFO)
  102. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  103. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
  104. def test_from_service_account_info_missing_key(self):
  105. with pytest.raises(ValueError) as excinfo:
  106. es256.ES256Signer.from_service_account_info({})
  107. assert excinfo.match(base._JSON_FILE_PRIVATE_KEY)
  108. def test_from_service_account_file(self):
  109. signer = es256.ES256Signer.from_service_account_file(SERVICE_ACCOUNT_JSON_FILE)
  110. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  111. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
  112. def test_pickle(self):
  113. signer = es256.ES256Signer.from_service_account_file(SERVICE_ACCOUNT_JSON_FILE)
  114. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  115. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
  116. pickled_signer = pickle.dumps(signer)
  117. signer = pickle.loads(pickled_signer)
  118. assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
  119. assert isinstance(signer._key, ec.EllipticCurvePrivateKey)