|
| 1 | +#!/bin/python |
| 2 | +# Copyright 2018 Google Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from os import environ |
| 18 | +from time import sleep |
| 19 | + |
| 20 | +from cryptography.hazmat.backends.openssl.ec import _EllipticCurvePublicKey |
| 21 | +from cryptography.hazmat.backends.openssl.rsa import _RSAPublicKey |
| 22 | +from googleapiclient import discovery |
| 23 | +from googleapiclient.errors import HttpError |
| 24 | +import sample |
| 25 | + |
| 26 | + |
| 27 | +def create_key_helper(key_id, key_path, purpose, algorithm, t): |
| 28 | + try: |
| 29 | + t.client.projects() \ |
| 30 | + .locations() \ |
| 31 | + .keyRings() \ |
| 32 | + .cryptoKeys() \ |
| 33 | + .create(parent='{}/keyRings/{}'.format(t.parent, t.keyring), |
| 34 | + body={'purpose': purpose, |
| 35 | + 'versionTemplate': { |
| 36 | + 'algorithm': algorithm |
| 37 | + } |
| 38 | + }, |
| 39 | + cryptoKeyId=key_id) \ |
| 40 | + .execute() |
| 41 | + return True |
| 42 | + except HttpError: |
| 43 | + # key already exists |
| 44 | + return False |
| 45 | + |
| 46 | + |
| 47 | +def setup_module(module): |
| 48 | + """ |
| 49 | + Set up keys in project if needed |
| 50 | + """ |
| 51 | + t = TestKMSSamples() |
| 52 | + try: |
| 53 | + # create keyring |
| 54 | + t.client.projects() \ |
| 55 | + .locations() \ |
| 56 | + .keyRings() \ |
| 57 | + .create(parent=t.parent, body={}, keyRingId=t.keyring) \ |
| 58 | + .execute() |
| 59 | + except HttpError: |
| 60 | + # keyring already exists |
| 61 | + pass |
| 62 | + s1 = create_key_helper(t.rsaDecryptId, t.rsaDecrypt, 'ASYMMETRIC_DECRYPT', |
| 63 | + 'RSA_DECRYPT_OAEP_2048_SHA256', t) |
| 64 | + s2 = create_key_helper(t.rsaSignId, t.rsaSign, 'ASYMMETRIC_SIGN', |
| 65 | + 'RSA_SIGN_PSS_2048_SHA256', t) |
| 66 | + s3 = create_key_helper(t.ecSignId, t.ecSign, 'ASYMMETRIC_SIGN', |
| 67 | + 'EC_SIGN_P224_SHA256', t) |
| 68 | + if s1 or s2 or s3: |
| 69 | + # leave time for keys to initialize |
| 70 | + sleep(20) |
| 71 | + |
| 72 | + |
| 73 | +class TestKMSSamples: |
| 74 | + |
| 75 | + project_id = environ['GCLOUD_PROJECT'] |
| 76 | + keyring = 'kms-asymmetric-samples4' |
| 77 | + parent = 'projects/{}/locations/global'.format(project_id) |
| 78 | + |
| 79 | + rsaSignId = 'rsa-sign' |
| 80 | + rsaDecryptId = 'rsa-decrypt' |
| 81 | + ecSignId = 'ec-sign' |
| 82 | + |
| 83 | + rsaSign = '{}/keyRings/{}/cryptoKeys/{}/cryptoKeyVersions/1' \ |
| 84 | + .format(parent, keyring, rsaSignId) |
| 85 | + rsaDecrypt = '{}/keyRings/{}/cryptoKeys/{}/cryptoKeyVersions/1' \ |
| 86 | + .format(parent, keyring, rsaDecryptId) |
| 87 | + ecSign = '{}/keyRings/{}/cryptoKeys/{}/cryptoKeyVersions/1' \ |
| 88 | + .format(parent, keyring, ecSignId) |
| 89 | + |
| 90 | + message = 'test message 123' |
| 91 | + |
| 92 | + client = discovery.build('cloudkms', 'v1') |
| 93 | + |
| 94 | + def test_get_public_key(self): |
| 95 | + rsa_key = sample.getAsymmetricPublicKey(self.client, self.rsaDecrypt) |
| 96 | + assert isinstance(rsa_key, _RSAPublicKey), 'expected RSA key' |
| 97 | + ec_key = sample.getAsymmetricPublicKey(self.client, self.ecSign) |
| 98 | + assert isinstance(ec_key, _EllipticCurvePublicKey), 'expected EC key' |
| 99 | + |
| 100 | + def test_rsa_encrypt_decrypt(self): |
| 101 | + ciphertext = sample.encryptRSA(self.message, |
| 102 | + self.client, |
| 103 | + self.rsaDecrypt) |
| 104 | + # ciphertext should be 344 characters with base64 and RSA 2048 |
| 105 | + assert len(ciphertext) == 344, \ |
| 106 | + 'ciphertext should be 344 chars; got {}'.format(len(ciphertext)) |
| 107 | + assert ciphertext[-2:] == '==', 'cipher text should end with ==' |
| 108 | + plaintext = sample.decryptRSA(ciphertext, self.client, self.rsaDecrypt) |
| 109 | + assert plaintext == self.message |
| 110 | + |
| 111 | + def test_rsa_sign_verify(self): |
| 112 | + sig = sample.signAsymmetric(self.message, self.client, self.rsaSign) |
| 113 | + # ciphertext should be 344 characters with base64 and RSA 2048 |
| 114 | + assert len(sig) == 344, \ |
| 115 | + 'sig should be 344 chars; got {}'.format(len(sig)) |
| 116 | + assert sig[-2:] == '==', 'sig should end with ==' |
| 117 | + success = sample.verifySignatureRSA(sig, |
| 118 | + self.message, |
| 119 | + self.client, |
| 120 | + self.rsaSign) |
| 121 | + assert success is True, 'RSA verification failed' |
| 122 | + success = sample.verifySignatureRSA(sig, |
| 123 | + self.message+'.', |
| 124 | + self.client, |
| 125 | + self.rsaSign) |
| 126 | + assert success is False, 'verify should fail with modified message' |
| 127 | + |
| 128 | + def test_ec_sign_verify(self): |
| 129 | + sig = sample.signAsymmetric(self.message, self.client, self.ecSign) |
| 130 | + assert len(sig) > 50 and len(sig) < 300, \ |
| 131 | + 'sig outside expected length range' |
| 132 | + success = sample.verifySignatureEC(sig, |
| 133 | + self.message, |
| 134 | + self.client, |
| 135 | + self.ecSign) |
| 136 | + assert success is True, 'EC verification failed' |
| 137 | + success = sample.verifySignatureEC(sig, |
| 138 | + self.message+'.', |
| 139 | + self.client, |
| 140 | + self.ecSign) |
| 141 | + assert success is False, 'verify should fail with modified message' |
0 commit comments