Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _validate_keyset(keyset: tink_pb2.Keyset):
"""Raises tink_error.TinkError if keyset is not valid."""
for key in keyset.key:
if key.status != tink_pb2.DESTROYED:
_validate_key(key)
num_non_destroyed_keys = sum(
1 for key in keyset.key if key.status != tink_pb2.DESTROYED)
num_non_public_key_material = sum(
1 for key in keyset.key
if key.key_data.key_material_type != tink_pb2.KeyData.ASYMMETRIC_PUBLIC)
num_primary_keys = sum(
1 for key in keyset.key
if key.status == tink_pb2.ENABLED and key.key_id == keyset.primary_key_id)
if num_non_destroyed_keys == 0:
raise tink_error.TinkError('empty keyset')
if num_primary_keys > 1:
raise tink_error.TinkError('keyset contains multiple primary keys')
if num_primary_keys == 0 and num_non_public_key_material > 0:
raise tink_error.TinkError('keyset does not contain a valid primary key')
def public_key_data(cls,
private_key_data: tink_pb2.KeyData) -> tink_pb2.KeyData:
"""Generates a new key for the specified key_template."""
if (private_key_data.key_material_type !=
tink_pb2.KeyData.ASYMMETRIC_PRIVATE):
raise tink_error.TinkError('The keyset contains a non-private key')
key_mgr = cls.key_manager(private_key_data.type_url)
if not isinstance(key_mgr, km_module.PrivateKeyManager):
raise tink_error.TinkError(
'manager for key type {} is not a PrivateKeyManager'
.format(private_key_data.type_url))
return key_mgr.public_key_data(private_key_data)
def _assert_no_secret_key_material(keyset: tink_pb2.Keyset):
for key in keyset.key:
if key.key_data.key_material_type in (tink_pb2.KeyData.UNKNOWN_KEYMATERIAL,
tink_pb2.KeyData.SYMMETRIC,
tink_pb2.KeyData.ASYMMETRIC_PRIVATE):
raise tink_error.TinkError('keyset contains secret key material')
def _assert_no_secret_key_material(keyset: tink_pb2.Keyset):
for key in keyset.key:
if key.key_data.key_material_type in (tink_pb2.KeyData.UNKNOWN_KEYMATERIAL,
tink_pb2.KeyData.SYMMETRIC,
tink_pb2.KeyData.ASYMMETRIC_PRIVATE):
raise tink_error.TinkError('keyset contains secret key material')
def _assert_no_secret_key_material(keyset: tink_pb2.Keyset):
for key in keyset.key:
if key.key_data.key_material_type in (tink_pb2.KeyData.UNKNOWN_KEYMATERIAL,
tink_pb2.KeyData.SYMMETRIC,
tink_pb2.KeyData.ASYMMETRIC_PRIVATE):
raise tink_error.TinkError('keyset contains secret key material')