blob: 16ff2e0bddc9df22ab71b186c4d37bcb33b9de71 [file] [log] [blame]
Jon Wayne Parrott9281ca02017-08-11 14:36: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 os
16
17from google.auth import crypt
18
19
Bu Sun Kim9eec0912019-10-21 17:04:21 -070020DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070021
22# To generate privatekey.pem, privatekey.pub, and public_cert.pem:
23# $ openssl req -new -newkey rsa:1024 -x509 -nodes -out public_cert.pem \
24# > -keyout privatekey.pem
25# $ openssl rsa -in privatekey.pem -pubout -out privatekey.pub
26
Bu Sun Kim9eec0912019-10-21 17:04:21 -070027with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh:
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070028 PRIVATE_KEY_BYTES = fh.read()
29
Bu Sun Kim9eec0912019-10-21 17:04:21 -070030with open(os.path.join(DATA_DIR, "public_cert.pem"), "rb") as fh:
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070031 PUBLIC_CERT_BYTES = fh.read()
32
33# To generate other_cert.pem:
34# $ openssl req -new -newkey rsa:1024 -x509 -nodes -out other_cert.pem
35
Bu Sun Kim9eec0912019-10-21 17:04:21 -070036with open(os.path.join(DATA_DIR, "other_cert.pem"), "rb") as fh:
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070037 OTHER_CERT_BYTES = fh.read()
38
39
40def test_verify_signature():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070041 to_sign = b"foo"
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070042 signer = crypt.RSASigner.from_string(PRIVATE_KEY_BYTES)
43 signature = signer.sign(to_sign)
44
Bu Sun Kim9eec0912019-10-21 17:04:21 -070045 assert crypt.verify_signature(to_sign, signature, PUBLIC_CERT_BYTES)
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070046
47 # List of certs
48 assert crypt.verify_signature(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070049 to_sign, signature, [OTHER_CERT_BYTES, PUBLIC_CERT_BYTES]
50 )
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070051
52
53def test_verify_signature_failure():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070054 to_sign = b"foo"
Jon Wayne Parrott9281ca02017-08-11 14:36:42 -070055 signer = crypt.RSASigner.from_string(PRIVATE_KEY_BYTES)
56 signature = signer.sign(to_sign)
57
Bu Sun Kim9eec0912019-10-21 17:04:21 -070058 assert not crypt.verify_signature(to_sign, signature, OTHER_CERT_BYTES)