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