test_es256.py 5.6 KB

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