|
| 1 | +# Copyright 2020 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 | + |
| 15 | +import tempfile |
| 16 | + |
| 17 | +import verify_attestation |
| 18 | + |
| 19 | +# Test certificate bundles can be generated with the following steps: |
| 20 | +# 1. Generate test key pairs. |
| 21 | +# - openssl genrsa -out test1.key 2048 |
| 22 | +# - openssl genrsa -out test2.key 2048 |
| 23 | +# 2. Generate test certificates using the key pairs. |
| 24 | +# - openssl req -x509 -key test1.key -days 3650 -out test1.pem |
| 25 | +# - openssl req -x509 -key test2.key -days 3650 -out test2.pem |
| 26 | +# 3. Create a bundle using the test certificates. |
| 27 | +# - cat test1.pem test2.pem > bundle.pem |
| 28 | +# For instructions on downloading certificate bundles from Cloud HSM, refer to: |
| 29 | +# https://cloud.google.com/kms/docs/attest-key#downloading_the_certificates |
| 30 | +TEST_CERT_BUNDLE = b"""-----BEGIN CERTIFICATE----- |
| 31 | +MIIDZDCCAkwCFE2PSNf++wMw+Jv86m41lbsa9aUMMA0GCSqGSIb3DQEBCwUAMFkx |
| 32 | +CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl |
| 33 | +cm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMMCVRlc3QgQ2FyZDAeFw0yMDAz |
| 34 | +MzEyMTQ0MjNaFw0zMDAzMjkyMTQ0MjNaMIGDMQswCQYDVQQGEwJBVTETMBEGA1UE |
| 35 |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hSgTASP+1GYjqOn1fZaa28m39/mx9Mrw7K |
| 67 | +xtXOtrdKqJhWCWJPprfC5sYNCYTA2HXVmBU2Y4AP4A4w+A1gCAdzvH8EEyDjnvxJ |
| 68 | +GEa4uczhA3n+NmhLipg1aGbxJO5ZHXdyFF2rTXVVXSiX8EEasnwcTDjeXDKhdpu6 |
| 69 | +biaxW9fnsJIXirAE03FFkC/tWelkGFkmMfs= |
| 70 | +-----END CERTIFICATE-----""" |
| 71 | + |
| 72 | +# Test attestations can be generated with the following steps: |
| 73 | +# 1. Create a file containing the test attestation statement. |
| 74 | +# - echo "content" > attestation.dat |
| 75 | +# 2. Sign the file with one the key pairs used to create the test certificates. |
| 76 | +# - openssl dgst -sha256 -sign test1.key -out signature.dat attestation.dat |
| 77 | +# 3. Concatenate the signature to the statement to create an attestation. |
| 78 | +# - cat signature.dat >> attestation.dat |
| 79 | +# 4. Compress the test attestation. |
| 80 | +# - gzip attestation.dat |
| 81 | +# For instructions on downloading attestations from Cloud HSM, refer to: |
| 82 | +# https://cloud.google.com/kms/docs/attest-key#downloading_the_attestation_statement |
| 83 | +TEST_ATTESTATION_GZ = ( |
| 84 | + b'\x1f\x8b\x08\x08\xda\xde\x84^\x00\x03attestation\x00\x01\x06\x01\xf9\xfe' |
| 85 | + b'\x15\xa7~W\xdazHq03\x95\xd1F\xcf\x1d\n\xe0\xbbv\x11\xed\xae\x186\xc0\xcc' |
| 86 | + b'.\xcf)\xf1?\xf7!\xf3\xd6M\x85\xfe\xb9\x84\xb2\x08V2(\xa1\x87]\xab\x01=' |
| 87 | + b'\xb5\x0f)~\x06\xee\xfa/\x94\xa6x\x96o\xb1\xcb$\x82\x90\xe03J\t\x03\xf0' |
| 88 | + b'\xa4\xa5\xa9\xf9\xb2\xce\xdd2\xfam\x94W\x07\x00~\xa5\xc2\xcdq\xa1\x81' |
| 89 | + b'\x18\x83\xe0\xd9\x11k]\xbd\xf8\x81@\x9c*\x80\x91R\xb0\xae\x9d\x88\xb8T' |
| 90 | + b'\xd1>\xf6;\xe4\x83q%_\x8aw\x894\xb5:\xeab\xd2\x9a\x81\xdd\xa6\xf9\x94' |
| 91 | + b'\xff8\xb1\xed\x7fs\x0e\xc0\xde\x89\x00]\x8fL\x82\x8a\x11\x8f\\\xe483\x9d' |
| 92 | + b'&\x0b%\xfd\x0et\x8f\xa8\x1a\xb5K\xb4\xc7\x96\xd1}\x06\xdd\x93i\x1f\xc1@' |
| 93 | + b'\x92\xef}(B\x0f\xd1\x03\xaaYo\x9b\xad\xa9zw#\xc8\x9a\xad\x94\xfc\x07q]x' |
| 94 | + b'\xeb\xa2CA\xf8\xac\x96\xd9\xe5y4\xae]\x81\xb0$\x93\tx\xdb\xcc\x08:%\x1d' |
| 95 | + b'\xe2q\xaa\xc8:\xc2<C\xb5\x9b\xd1\xb0<\x12\x8a\x99f\x11\x9a"Q\x1d]\xac' |
| 96 | + b'\x81\xe2\x05\x06\x01\x00\x00') |
| 97 | + |
| 98 | + |
| 99 | +def test_verify(): |
| 100 | + with tempfile.NamedTemporaryFile() as attestation_file: |
| 101 | + with tempfile.NamedTemporaryFile() as bundle_file: |
| 102 | + attestation_file.write(TEST_ATTESTATION_GZ) |
| 103 | + bundle_file.write(TEST_CERT_BUNDLE) |
| 104 | + |
| 105 | + attestation_file.seek(0) |
| 106 | + bundle_file.seek(0) |
| 107 | + |
| 108 | + assert verify_attestation.verify( |
| 109 | + attestation_file.name, bundle_file.name) |
0 commit comments