Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-kms #8470

Merged
merged 136 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
c3dae48
Python KMS Apiary P1 samples [(#779)](https://github.com/GoogleCloudP…
ryanmats Feb 7, 2017
c031a34
random generation of keyring / cryptokey names [(#786)](https://githu…
ryanmats Feb 8, 2017
cc8e1b4
Auto-update dependencies. [(#790)](https://github.com/GoogleCloudPlat…
dpebot Feb 9, 2017
7147ccf
Remove usage of GoogleCredentials [(#810)](https://github.com/GoogleC…
Feb 17, 2017
69a7649
Updates Could-KMS sample code to use V1 libraries. [(#856)](https://g…
philcoakley Mar 16, 2017
e67c399
Remove cloud config fixture [(#887)](https://github.com/GoogleCloudPl…
Apr 4, 2017
5b298d5
Re-generate all readmes
Apr 27, 2017
480435a
KMS: Clean up base64 logic in the encrypt and decrypt functions. [(#1…
Aug 16, 2017
adefefe
KMS: Make the samples consistent with the other languages. [(#1075)](…
Aug 17, 2017
b97884f
Auto-update dependencies. [(#1094)](https://github.com/GoogleCloudPla…
dpebot Aug 30, 2017
38d062d
Update all generated readme auth instructions [(#1121)](https://githu…
Sep 18, 2017
832a6be
Auto-update dependencies. [(#1133)](https://github.com/GoogleCloudPla…
dpebot Sep 21, 2017
d65342d
Added Link to Python Setup Guide [(#1158)](https://github.com/GoogleC…
Oct 12, 2017
409a57f
Auto-update dependencies. [(#1186)](https://github.com/GoogleCloudPla…
dpebot Nov 1, 2017
6d2a5f9
Adds snippets for enabling and restoring a key version [(#1196)](http…
WalterHub Nov 7, 2017
38da194
Auto-update dependencies. [(#1217)](https://github.com/GoogleCloudPla…
dpebot Nov 15, 2017
515d306
Added "Open in Cloud Shell" buttons to README files [(#1254)](https:/…
Dec 7, 2017
a4fe396
Auto-update dependencies. [(#1309)](https://github.com/GoogleCloudPla…
dpebot Jan 10, 2018
6db525e
Auto-update dependencies. [(#1320)](https://github.com/GoogleCloudPla…
dpebot Feb 2, 2018
650cc53
Auto-update dependencies. [(#1355)](https://github.com/GoogleCloudPla…
dpebot Feb 9, 2018
3ad2bc0
Auto-update dependencies. [(#1359)](https://github.com/GoogleCloudPla…
dpebot Feb 26, 2018
3161da4
Auto-update dependencies.
dpebot Apr 2, 2018
00bcfc8
Regenerate the README files and fix the Open in Cloud Shell link for …
Apr 7, 2018
72fee98
Update READMEs to fix numbering and add git clone [(#1464)](https://g…
frankyn Apr 26, 2018
8aa3024
added kms asymmetric samples [(#1638)](https://github.com/GoogleCloud…
daniel-sanche Aug 17, 2018
366fa72
kms text fixes [(#1647)](https://github.com/GoogleCloudPlatform/pytho…
daniel-sanche Aug 22, 2018
887f48f
KMS test fix [(#1690)](https://github.com/GoogleCloudPlatform/python-…
daniel-sanche Sep 7, 2018
909a24b
KMS changes [(#1723)](https://github.com/GoogleCloudPlatform/python-d…
daniel-sanche Sep 28, 2018
ce34f10
KMS import comments [(#1771)](https://github.com/GoogleCloudPlatform/…
daniel-sanche Oct 25, 2018
d889421
Auto-update dependencies. [(#1846)](https://github.com/GoogleCloudPla…
dpebot Nov 20, 2018
4f159ea
Auto-update dependencies. [(#1862)](https://github.com/GoogleCloudPla…
dpebot Nov 21, 2018
df03dc0
KMS: Updated for new client library [(#1903)](https://github.com/Goog…
daniel-sanche Jan 7, 2019
6f2c0dc
Auto-update dependencies. [(#1980)](https://github.com/GoogleCloudPla…
dpebot Feb 6, 2019
ac7a0aa
Move import statements into tagged regions [(#2219)](https://github.c…
engelke Jun 13, 2019
821e6aa
Adds split updates for Firebase ... opencensus [(#2438)](https://gith…
gguuss Oct 8, 2019
ef44d09
Auto-update dependencies. [(#2005)](https://github.com/GoogleCloudPla…
dpebot Dec 21, 2019
eecdc66
chore(deps): update dependency google-cloud-kms to v1.3.0 [(#3160)](h…
renovate-bot Mar 30, 2020
2dd0308
Simplify noxfile setup. [(#2806)](https://github.com/GoogleCloudPlatf…
kurtisvg Apr 2, 2020
6f8f31d
[kms] fix flaky test [(#3268)](https://github.com/GoogleCloudPlatform…
daniel-sanche Apr 7, 2020
966796a
Update dependency google-cloud-kms to v1.4.0 [(#3410)](https://github…
renovate-bot Apr 21, 2020
5fb1511
chore(deps): update dependency cryptography to v2.9 [(#3266)](https:/…
renovate-bot Apr 22, 2020
48170cb
fix: use `crypto_key_path` instead of `crypto_key_path_path` [(#3319)…
busunkim96 Apr 22, 2020
5c1926a
chore(deps): update dependency cryptography to v2.9.1 [(#3463)](https…
renovate-bot Apr 22, 2020
e6944ef
[kms] chore: remove gcp-devrel-py-tools [(#3479)](https://github.com/…
Apr 23, 2020
c6a8bfa
Update dependency cryptography to v2.9.2 [(#3473)](https://github.com…
renovate-bot Apr 23, 2020
9992fab
[kms] fix: use unique ids for test [(#3563)](https://github.com/Googl…
Apr 28, 2020
c24b425
Update and add Cloud KMS samples [(#3690)](https://github.com/GoogleC…
sethvargo May 5, 2020
005b35b
chore: some lint fixes [(#3748)](https://github.com/GoogleCloudPlatfo…
May 13, 2020
cd445b9
chore: update templates
busunkim96 May 28, 2020
b7f9366
chore: update templates
busunkim96 May 28, 2020
aecda49
Add kms/attestations/ directory with sample script to verify attestat…
bkuang May 1, 2020
c6a6f28
chore(deps): update dependency cryptography to v2.9.2 [(#3666)](https…
renovate-bot May 4, 2020
6360704
Update and add Cloud KMS samples [(#3690)](https://github.com/GoogleC…
sethvargo May 5, 2020
7084ad4
Add main function to attestation verification script [(#3705)](https:…
bkuang May 15, 2020
1bdb253
test: add noxfile
busunkim96 Jun 16, 2020
cd68144
chore(deps): update dependency cryptography to v3 (#24)
renovate-bot Jul 28, 2020
656593d
feat!: migrate to microgenerator. (#16)
busunkim96 Jul 30, 2020
7624359
docs: Generate using new common.py_samples() synthtool functionality …
runargs Aug 7, 2020
64056c1
chore: update templates (#42)
yoshi-automation Aug 27, 2020
f59e62c
chore(deps): update dependency cryptography to v3.1 (#44)
renovate-bot Aug 27, 2020
deeaedc
feat: regenerate client lib to pick up new mtls env (#55)
arithmetic1728 Sep 16, 2020
03427d8
chore(deps): update dependency google-cloud-kms to v2 (#29)
renovate-bot Sep 16, 2020
35d6e90
chore(deps): update dependency cryptography to v3.1.1 (#61)
renovate-bot Oct 16, 2020
bc54668
chore(deps): update dependency google-cloud-kms to v2.2.0 (#57)
renovate-bot Oct 16, 2020
9d82d9a
chore(deps): update dependency cryptography to v3.2.1 (#64)
renovate-bot Oct 28, 2020
d8e0fdf
samples: Add Python code snippets for importing-a-key doc (#67)
lwolfowitz-google Nov 5, 2020
3dd5d05
samples: Minor fixes for importing-a-key snippets (#68)
lwolfowitz-google Nov 9, 2020
abe0570
samples: add request/response integrity verification to encrypt_symme…
iamtamjam Nov 12, 2020
fb39d76
samples: Add request/response integrity verification to crypto operat…
iamtamjam Dec 14, 2020
057017e
chore(deps): update dependency cryptography to v3.3.1 (#78)
renovate-bot Dec 15, 2020
2b39571
chore(deps): update dependency pem to v21 (#82)
renovate-bot Jan 22, 2021
391284a
chore(deps): update dependency cryptography to v3.4.4 (#85)
renovate-bot Feb 10, 2021
6101b1f
chore(deps): update dependency cryptography to v3.4.5 (#88)
renovate-bot Feb 16, 2021
ea46a5e
chore(deps): update dependency cryptography to v3.4.6 (#89)
renovate-bot Feb 20, 2021
31fdee1
chore(deps): update dependency cryptography to v3.4.7 (#93)
renovate-bot Mar 26, 2021
462b1f7
chore(deps): update dependency pem to v21.2.0 (#96)
renovate-bot Apr 13, 2021
277ffb8
feat: add script to verify attestations with certificate chains (#99)
bkuang Apr 23, 2021
2597804
chore(deps): update dependency pytest to v6 (#110)
renovate-bot May 14, 2021
bcb80c6
chore(deps): update dependency google-cloud-kms to v2.3.0 (#121)
renovate-bot Jun 14, 2021
e695b22
docs: Include verify_attestation_chains.py help text to attestations …
bkuang Jul 12, 2021
079922c
chore(deps): update dependency google-cloud-kms to v2.4.0 (#135)
renovate-bot Jul 13, 2021
b88b9ba
chore(deps): update dependency google-cloud-kms to v2.4.1 (#146)
renovate-bot Jul 22, 2021
bf615fd
chore(deps): update dependency google-cloud-kms to v2.4.2 (#153)
renovate-bot Jul 28, 2021
8911a48
docs: update README for attestation verification scripts (#151)
bkuang Jul 29, 2021
4d72d4d
chore(deps): update dependency google-cloud-kms to v2.4.3 (#155)
renovate-bot Jul 29, 2021
af529ec
chore(deps): update dependency google-cloud-kms to v2.5.0 (#159)
renovate-bot Aug 9, 2021
8653eeb
feat(kms): add samples for new hmac and rng apis (#161)
sethvargo Aug 12, 2021
d52bbd8
chore(deps): update dependency cryptography to v3.4.8 (#164)
renovate-bot Aug 25, 2021
802d4c0
chore(deps): update dependency pytest to v6.2.5 (#169)
renovate-bot Aug 30, 2021
5378370
chore(deps): update dependency google-cloud-kms to v2.6.0 (#171)
renovate-bot Aug 30, 2021
06dbf90
chore: google-cloud-python now uses 'main' for default branch (#175)
gcf-owl-bot[bot] Sep 15, 2021
dec3cc3
chore(deps): update dependency google-cloud-kms to v2.6.1 (#180)
renovate-bot Sep 27, 2021
89a6f2e
chore(deps): update dependency cryptography to v35 (#182)
renovate-bot Sep 30, 2021
8121d9d
chore(deps): update dependency google-cloud-kms to v2.7.0 (#185)
renovate-bot Sep 30, 2021
7944d96
chore(deps): update dependency google-cloud-kms to v2.8.0 (#188)
renovate-bot Sep 30, 2021
5005d26
chore(deps): update dependency google-cloud-kms to v2.9.0 (#194)
renovate-bot Oct 29, 2021
f0c5cb5
chore(deps): update dependency google-cloud-kms to v2.10.0 (#199)
renovate-bot Oct 29, 2021
4b8836d
chore(deps): update dependency google-cloud-kms to v2.10.1 (#202)
renovate-bot Nov 2, 2021
804e44a
chore: update README and fix typos in attestation scripts (#203)
bkuang Nov 3, 2021
1473fc6
chore(deps): update dependency cryptography to v36 (#210)
renovate-bot Nov 23, 2021
8b949e3
chore(deps): update dependency cryptography to v36.0.1 (#216)
renovate-bot Dec 15, 2021
29c75d7
chore(samples): Add check for tests in directory (#221)
gcf-owl-bot[bot] Jan 11, 2022
df10185
chore(python): Noxfile recognizes that tests can live in a folder (#225)
gcf-owl-bot[bot] Jan 19, 2022
a0e8ddd
docs(samples): fix typo in verify_asymmetric_ec.py (#227)
pedroysb Jan 21, 2022
221607b
docs(samples): updated var name to avoid shadowing built-in (#238)
Sita04 Feb 16, 2022
c34019d
chore(deps): update all dependencies (#234)
renovate-bot Feb 28, 2022
55042fe
chore: Adding support for pytest-xdist and pytest-parallel (#248)
gcf-owl-bot[bot] Mar 4, 2022
c943350
chore(deps): update dependency google-cloud-kms to v2.11.1 (#250)
renovate-bot Mar 8, 2022
4d1ac92
chore(deps): update dependency pytest to v7.1.0 (#252)
renovate-bot Mar 13, 2022
7b81f6a
chore(deps): update all dependencies (#253)
renovate-bot Mar 19, 2022
0774515
chore(python): use black==22.3.0 (#256)
gcf-owl-bot[bot] Mar 29, 2022
62646ba
chore(python): add nox session to sort python imports (#267)
gcf-owl-bot[bot] Apr 21, 2022
4589f95
chore(deps): update dependency pytest to v7.1.2 (#270)
renovate-bot Apr 25, 2022
d52681b
chore(deps): update dependency cryptography to v37 (#271)
renovate-bot Apr 26, 2022
e897dad
chore(deps): update dependency cryptography to v37.0.1 (#273)
renovate-bot Apr 28, 2022
1ae0ad3
chore(deps): update dependency cryptography to v37.0.2 (#274)
renovate-bot May 5, 2022
f392f41
fix: require python 3.7+ (#313)
gcf-owl-bot[bot] Jul 7, 2022
d5a2f11
chore(deps): update all dependencies (#303)
renovate-bot Jul 12, 2022
a91c0cf
chore(deps): update all dependencies (#319)
renovate-bot Aug 2, 2022
955d67e
chore(deps): update dependency google-cloud-kms to v2.12.1 (#326)
renovate-bot Aug 19, 2022
f9e23a0
chore(deps): update dependency pytest to v7.1.3 (#338)
renovate-bot Sep 6, 2022
ba0fe23
chore(deps): update dependency cryptography to v38 (#339)
renovate-bot Sep 7, 2022
040450d
chore(deps): update dependency cryptography to v38.0.1 (#341)
renovate-bot Sep 7, 2022
181e085
chore: detect samples tests in nested directories (#345)
gcf-owl-bot[bot] Sep 13, 2022
74d51f2
chore(deps): update dependency google-cloud-kms to v2.12.2 (#349)
renovate-bot Oct 4, 2022
b9d6082
chore(deps): update all dependencies (#352)
renovate-bot Oct 18, 2022
e1e35bf
chore(deps): update dependency pytest to v7.2.0 (#353)
renovate-bot Oct 26, 2022
40dfca1
Updates to READMEs and CODEOWNERS
rsamborski Nov 8, 2022
81dd63a
Update requirements.txt to include requests module
rsamborski Nov 8, 2022
73b0204
License header updates and region tag rename
rsamborski Nov 8, 2022
062ea7c
Fix CODEOWNERS newline at the end of file
rsamborski Nov 8, 2022
b735728
Removed noxfile.py and added noxfile_config.py
rsamborski Nov 9, 2022
0fa2468
Turn off type hints enforcing for old samples
rsamborski Nov 9, 2022
5b8496c
Turn off type hints enforcing for old samples
rsamborski Nov 9, 2022
01ca228
Added kms label to blunderbuss.yml
rsamborski Nov 14, 2022
bb58291
Update blunderbuss.yml with cloudkms label
rsamborski Nov 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@
/trace/**/* @ymotongpoo @GoogleCloudPlatform/python-samples-reviewers
/translate/**/* @nicain @GoogleCloudPlatform/python-samples-reviewers
/workflows/**/* @GoogleCloudPlatform/python-samples-reviewers
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
rsamborski marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ assign_issues_by:
- 'api: monitoring'
to:
- GoogleCloudPlatform/dee-observability
- labels:
- 'api: kms'
rsamborski marked this conversation as resolved.
Show resolved Hide resolved
- 'api: cloudkms'
to:
- GoogleCloudPlatform/dee-infra

assign_prs_by:
- labels:
Expand Down
3 changes: 0 additions & 3 deletions kms/README.rst

This file was deleted.

120 changes: 120 additions & 0 deletions kms/attestations/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
Google Cloud Key Management Service Python Samples
===============================================================================

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/README.rst


This directory contains samples for Google Cloud Key Management Service. The `Cloud Key Management Service`_ allows you to create, import, and manage cryptographic keys and perform cryptographic operations in a single centralized cloud service.




.. _Cloud Key Management Service: https://cloud.google.com/kms/docs/





Setup
-------------------------------------------------------------------------------


Install Dependencies
++++++++++++++++++++

#. Clone python-kms and change directory to the sample directory you want to use.

.. code-block:: bash

$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.

.. _Python Development Environment Setup Guide:
https://cloud.google.com/python/setup

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash

$ virtualenv env
$ source env/bin/activate

#. Install the dependencies needed to run the samples.

.. code-block:: bash

$ pip install -r requirements.txt

.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Verify attestations and certificate chains for keys generated by Cloud HSM
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/verify_attestation_chains.py,kms/attestations/README.rst




To run this sample:

.. code-block:: bash

$ python verify_attestation_chains.py

usage: verify_attestation_chains.py [-h] [--certificates CERTIFICATES]
[--attestation ATTESTATION]

This application verifies HSM attestations using certificate chains
obtained from Cloud HSM and the HSM manufacturer.

For more information, visit https://cloud.google.com/kms/docs/attest-key.

optional arguments:
-h, --help show this help message and exit
--certificates CERTIFICATES
The certificate chains filename.
--attestation ATTESTATION
The attestation filename.



Verify attestations for keys generated by Cloud HSM
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/verify_attestation.py,kms/attestations/README.rst




To run this sample:

.. code-block:: bash

$ python verify_attestation.py

usage: verify_attestation.py [-h] attestation_file bundle_file

This application verifies HSM attestations using certificate bundles obtained
from Cloud HSM. For more information, visit https://cloud.google.com/kms/docs
/attest-key.

positional arguments:
attestation_file Name of attestation file.
bundle_file Name of certificate bundle file.

optional arguments:
-h, --help show this help message and exit





.. _Google Cloud SDK: https://cloud.google.com/sdk/
42 changes: 42 additions & 0 deletions kms/attestations/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": False,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
1 change: 1 addition & 0 deletions kms/attestations/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.2.0
3 changes: 3 additions & 0 deletions kms/attestations/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cryptography==38.0.2
pem==21.2.0
requests==2.28.1
86 changes: 86 additions & 0 deletions kms/attestations/verify_attestation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application verifies HSM attestations using certificate bundles
obtained from Cloud HSM.

For more information, visit https://cloud.google.com/kms/docs/attest-key.
"""

# [START kms_verify_attestations]
import argparse
import gzip

from cryptography import exceptions
from cryptography import x509
from cryptography.hazmat import backends
from cryptography.hazmat.primitives.asymmetric import padding
import pem


def verify(attestation_file, bundle_file):
"""Verifies an attestation using a bundle of certificates.

Args:
attestation_file: The name of the attestation file.
bundle_file: The name of the bundle file containing the certificates
used to verify the attestation.

Returns:
True if at least one of the certificates in bundle_file can verify the
attestation data and its signature.
"""
with gzip.open(attestation_file, 'rb') as f:
# An attestation file consists of a data portion and a 256 byte
# signature portion concatenated together.
attestation = f.read()
# Separate the components.
data = attestation[:-256]
signature = attestation[-256:]

# Verify the attestation with one of the certificates in the bundle
for cert in pem.parse_file(bundle_file):
cert_obj = x509.load_pem_x509_certificate(
str(cert).encode('utf-8'), backends.default_backend())
try:
# Check if the data was signed by the private key associated
# with the public key in the certificate. The data should have
# been signed with PKCS1v15 padding.
cert_obj.public_key().verify(
signature, data, padding.PKCS1v15(),
cert_obj.signature_hash_algorithm)
return True
except exceptions.InvalidSignature:
# Certificate bundles contain certificates that will not be
# able to verify the attestation, so the InvalidSignature
# errors can be ignored.
continue
return False
# [END kms_verify_attestations]


if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__)
parser.add_argument('attestation_file', help="Name of attestation file.")
parser.add_argument('bundle_file', help="Name of certificate bundle file.")

args = parser.parse_args()

if verify(args.attestation_file, args.bundle_file):
print('Signature verified.')
else:
print('Signature verification failed.')
Loading