Class KeysetHandle
- java.lang.Object
-
- com.google.crypto.tink.KeysetHandle
-
- All Implemented Interfaces:
KeysetHandleInterface
public final class KeysetHandle extends java.lang.Object implements KeysetHandleInterface
A KeysetHandle provides abstracted access toKeyset, to limit the exposure of actual protocol buffers that hold sensitive key material.This class allows reading and writing encrypted keysets. Users that want to read or write can use the restricted API
CleartextKeysetHandle. Users can also load keysets that don't contain any secret key material withNoSecretKeysetHandle.- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKeysetHandle.BuilderUsed to create newKeysetHandleobjects.static classKeysetHandle.EntryRepresents a single entry in a keyset.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static KeysetHandlecreateFromKey(KeyHandle keyHandle, KeyAccess access)Deprecated.UseKeysetHandle.Builder.addEntryinstead.booleanequalsKeyset(KeysetHandle other)Returns true if this keyset is equal toother, ignoring monitoring annotations.static KeysetHandle.Builder.EntrygenerateEntryFromParameters(Parameters parameters)Creates a new entry with Status "ENABLED" and a new key created from the parameters.static KeysetHandle.Builder.EntrygenerateEntryFromParametersName(java.lang.String parametersName)Creates a new entry with Status "ENABLED" and a new key created from the named parameters.static KeysetHandlegenerateNew(KeyTemplate keyTemplate)Generates a newKeysetHandlethat contains a single fresh key generated according tokeyTemplate.static KeysetHandlegenerateNew(Parameters parameters)Generates a newKeysetHandlethat contains a single fresh key generated key with the givenParametersobject.static KeysetHandlegenerateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate)Deprecated.Use the overload taking a Parameters object instead.KeysetHandle.EntrygetAt(int i)Returns the entry at index i.java.util.List<KeyHandle>getKeys()Deprecated.Use "getAt" instead.com.google.crypto.tink.proto.KeysetInfogetKeysetInfo()Deprecated.Most information can be obtained by callinggetPrimary()orgetAt(int)and inspecting the result.KeysetHandle.EntrygetPrimary()Returns the unique entry where isPrimary() = true and getStatus() = ENABLED.<P> PgetPrimitive(Configuration configuration, java.lang.Class<P> targetClassObject)Returns a primitive from this keyset using the providedConfigurationto create resources used in creating the primitive.<P> PgetPrimitive(java.lang.Class<P> targetClassObject)Deprecated.New users should useKeysetHandle.getPrimitivewith a suitableConfiguration.KeysetHandlegetPublicKeysetHandle()If the managed keyset contains private keys, returns aKeysetHandleof the public keys.static KeysetHandle.Builder.EntryimportKey(Key key)Creates a new entry with a fixed key.static KeysetHandle.BuildernewBuilder()Creates a new builder.static KeysetHandle.BuildernewBuilder(KeysetHandle handle)Creates a new builder, initially containing all entries fromhandle.KeyHandleprimaryKey()Deprecated.UsegetPrimary()instead.static KeysetHandleread(KeysetReader reader, Aead masterKey)Deprecated.New users should prefer TinkProtoKeysetFormat.static KeysetHandlereadNoSecret(byte[] serialized)Deprecated.Call {TinkProtoKeysetFormat.parseKeysetWithoutSecret} instead.static KeysetHandlereadNoSecret(KeysetReader reader)Deprecated.New users should prefer TinkProtoKeysetFormat.static KeysetHandlereadWithAssociatedData(KeysetReader reader, Aead masterKey, byte[] associatedData)Deprecated.New users should prefer TinkProtoKeysetFormat.intsize()Returns the size of this keyset.java.lang.StringtoString()Extracts and returns the string representation of theKeysetInfoof the managed keyset.voidwrite(KeysetWriter keysetWriter, Aead masterKey)Deprecated.New users should prefer TinkProtoKeysetFormat.voidwriteNoSecret(KeysetWriter writer)Deprecated.New users should prefer TinkProtoKeysetFormat.voidwriteWithAssociatedData(KeysetWriter keysetWriter, Aead masterKey, byte[] associatedData)Deprecated.New users should prefer TinkProtoKeysetFormat.
-
-
-
Method Detail
-
importKey
public static KeysetHandle.Builder.Entry importKey(Key key)
Creates a new entry with a fixed key.If the Key has an IdRequirement, the default will be fixed to this ID. Otherwise, the user has to specify the ID to be used and call one of
withFixedId(i)orwithRandomId()on the returned entry.
-
generateEntryFromParametersName
public static KeysetHandle.Builder.Entry generateEntryFromParametersName(java.lang.String parametersName) throws java.security.GeneralSecurityException
Creates a new entry with Status "ENABLED" and a new key created from the named parameters. No ID is set.namedParametersis the key template name that fully specifies the parameters, e.g. "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_128_GCM".- Throws:
java.security.GeneralSecurityException
-
generateEntryFromParameters
public static KeysetHandle.Builder.Entry generateEntryFromParameters(Parameters parameters)
Creates a new entry with Status "ENABLED" and a new key created from the parameters. No ID is set.
-
newBuilder
public static KeysetHandle.Builder newBuilder()
Creates a new builder.
-
newBuilder
public static KeysetHandle.Builder newBuilder(KeysetHandle handle)
Creates a new builder, initially containing all entries fromhandle.
-
getPrimary
public KeysetHandle.Entry getPrimary()
Returns the unique entry where isPrimary() = true and getStatus() = ENABLED.Note: currently this may throw IllegalStateException, since it is possible that keysets are parsed without a primary. In the future, such keysets will be rejected when the keyset is parsed.
- Specified by:
getPrimaryin interfaceKeysetHandleInterface
-
size
public int size()
Returns the size of this keyset.- Specified by:
sizein interfaceKeysetHandleInterface
-
getAt
public KeysetHandle.Entry getAt(int i)
Returns the entry at index i. The order is preserved and depends on the order at which the entries were inserted when the KeysetHandle was built.Currently, this may throw "IllegalStateException" in case the status entry of the Key in the keyset was wrongly set. In this case, we call this KeysetHandle invalid. In the future, Tink will throw at parsing time in this case, and we will not have invalid KeysetHandles.
If you want to ensure that this does not throw an IllegalStateException, please first re-parse the KeysetHandle:
KeysetHandle guaranteedValid = KeysetHandle.newBuilder(maybeInvalidHandle).build();(This would throw aGeneralSecurityExceptionif themaybeInvalidHandlehandle is invalid).- Specified by:
getAtin interfaceKeysetHandleInterface- Throws:
java.lang.IndexOutOfBoundsException- if i < 0 or i >= size();
-
getKeys
@Deprecated public java.util.List<KeyHandle> getKeys()
Deprecated.Use "getAt" instead.Returns the keyset data as a list ofKeyHandles.Please do not use this function in new code. Instead, use
getAt(int).
-
getKeysetInfo
@Deprecated public com.google.crypto.tink.proto.KeysetInfo getKeysetInfo()
Deprecated.Most information can be obtained by callinggetPrimary()orgetAt(int)and inspecting the result. For legacy code,LegacyKeysetSerialization.getKeysetInfogives the exact same output.Returns theKeysetInfothat doesn't contain actual key material.
-
generateNew
public static final KeysetHandle generateNew(Parameters parameters) throws java.security.GeneralSecurityException
Generates a newKeysetHandlethat contains a single fresh key generated key with the givenParametersobject.- Throws:
java.security.GeneralSecurityException- if no generation method for the givenparametershas been registered.
-
generateNew
@Deprecated public static final KeysetHandle generateNew(com.google.crypto.tink.proto.KeyTemplate keyTemplate) throws java.security.GeneralSecurityException
Deprecated.Use the overload taking a Parameters object instead.Generates a newKeysetHandlethat contains a single fresh key generated according tokeyTemplate.Please do not use this function. Instead, use
generateNew(Parameters).For existing usage, try to use refaster https://github.com/tink-crypto/tink-java/tree/main/tools/refaster to replace usage automatically. This will replaces calls
KeysetHandle.generateNew(XYZKeyTemplates.ABC);withKeysetHandle.generateNew(PredefinedXYZParameters.ABC);which is a NO-OP.If this is not possible, please inline the function in your code.
- Throws:
java.security.GeneralSecurityException- if the key template is invalid.
-
generateNew
public static final KeysetHandle generateNew(KeyTemplate keyTemplate) throws java.security.GeneralSecurityException
Generates a newKeysetHandlethat contains a single fresh key generated according tokeyTemplate.Please do not use this function. Instead, inline it: replace calls with
generateNew(t)withgenerateNew(t.toParameters()).- Throws:
java.security.GeneralSecurityException- if the key template is invalid.
-
createFromKey
@Deprecated public static final KeysetHandle createFromKey(KeyHandle keyHandle, KeyAccess access) throws java.security.GeneralSecurityException
Deprecated.UseKeysetHandle.Builder.addEntryinstead.Returns aKeysetHandlethat contains the singleKeyHandlepassed as input.- Throws:
java.security.GeneralSecurityException
-
read
@Deprecated public static final KeysetHandle read(KeysetReader reader, Aead masterKey) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Tries to create aKeysetHandlefrom an encrypted keyset obtained viareader.Users that need to load cleartext keysets can use
CleartextKeysetHandle.- Returns:
- a new
KeysetHandlefromencryptedKeysetProtothat was encrypted withmasterKey - Throws:
java.security.GeneralSecurityException- if cannot decrypt the keyset or it doesn't contain encrypted key materialjava.io.IOException
-
readWithAssociatedData
@Deprecated public static final KeysetHandle readWithAssociatedData(KeysetReader reader, Aead masterKey, byte[] associatedData) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Tries to create aKeysetHandlefrom an encrypted keyset obtained viareader, using the provided associated data.Users that need to load cleartext keysets can use
CleartextKeysetHandle.- Returns:
- a new
KeysetHandlefromencryptedKeysetProtothat was encrypted withmasterKey - Throws:
java.security.GeneralSecurityException- if cannot decrypt the keyset or it doesn't contain encrypted key materialjava.io.IOException
-
readNoSecret
@Deprecated public static final KeysetHandle readNoSecret(KeysetReader reader) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Tries to create aKeysetHandlefrom a keyset, obtained viareader, which contains no secret key material.This can be used to load public keysets or envelope encryption keysets. Users that need to load cleartext keysets can use
CleartextKeysetHandle.- Returns:
- a new
KeysetHandlefromserializedthat is a serializedKeyset - Throws:
java.security.GeneralSecurityException- if the keyset is invalidjava.io.IOException
-
readNoSecret
@Deprecated public static final KeysetHandle readNoSecret(byte[] serialized) throws java.security.GeneralSecurityException
Deprecated.Call {TinkProtoKeysetFormat.parseKeysetWithoutSecret} instead.Tries to create aKeysetHandlefrom a serialized keyset which contains no secret key material.This can be used to load public keysets or envelope encryption keysets. Users that need to load cleartext keysets can use
CleartextKeysetHandle.Note: new code should call
TinkProtoKeysetFormat(serialized)instead.- Returns:
- a new
KeysetHandlefromserializedthat is a serializedKeyset - Throws:
java.security.GeneralSecurityException- if the keyset is invalid
-
write
@Deprecated public void write(KeysetWriter keysetWriter, Aead masterKey) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Serializes, encrypts withmasterKeyand writes the keyset tooutputStream.- Throws:
java.security.GeneralSecurityExceptionjava.io.IOException
-
writeWithAssociatedData
@Deprecated public void writeWithAssociatedData(KeysetWriter keysetWriter, Aead masterKey, byte[] associatedData) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Serializes, encrypts withmasterKeyand writes the keyset tooutputStreamusing the provided associated data.- Throws:
java.security.GeneralSecurityExceptionjava.io.IOException
-
writeNoSecret
@Deprecated public void writeNoSecret(KeysetWriter writer) throws java.security.GeneralSecurityException, java.io.IOException
Deprecated.New users should prefer TinkProtoKeysetFormat. Existing users can use LegacyKeysetSerialization for exactly the same behavior.Tries to write towriterthis keyset which must not contain any secret key material.This can be used to persist public keysets or envelope encryption keysets. Users that need to persist cleartext keysets can use
CleartextKeysetHandle.- Throws:
java.security.GeneralSecurityException- if the keyset contains any secret key materialjava.io.IOException
-
getPublicKeysetHandle
public KeysetHandle getPublicKeysetHandle() throws java.security.GeneralSecurityException
If the managed keyset contains private keys, returns aKeysetHandleof the public keys.- Throws:
java.security.GeneralSecurityException- if the managed keyset is null or if it contains any non-private keys.
-
toString
public java.lang.String toString()
Extracts and returns the string representation of theKeysetInfoof the managed keyset.- Overrides:
toStringin classjava.lang.Object
-
getPrimitive
public <P> P getPrimitive(Configuration configuration, java.lang.Class<P> targetClassObject) throws java.security.GeneralSecurityException
Returns a primitive from this keyset using the providedConfigurationto create resources used in creating the primitive.- Throws:
java.security.GeneralSecurityException
-
getPrimitive
@Deprecated @InlineMe(replacement="this.getPrimitive(RegistryConfiguration.get(), targetClassObject)", imports="com.google.crypto.tink.RegistryConfiguration") public <P> P getPrimitive(java.lang.Class<P> targetClassObject) throws java.security.GeneralSecurityExceptionDeprecated.New users should useKeysetHandle.getPrimitivewith a suitableConfiguration. Existing users can useKeysetHandle.getPrimitive(RegistryConfiguration.get(), targetClassObject)for exactly the same behavior.Returns a primitive from this keyset, using the global registry to create resources creating the primitive.- Throws:
java.security.GeneralSecurityException
-
primaryKey
@Deprecated public KeyHandle primaryKey() throws java.security.GeneralSecurityException
Deprecated.UsegetPrimary()instead.Searches the keyset to find the primary key of thisKeysetHandle, and returns the key wrapped in aKeyHandle.Please do not use this function in new code. Instead, use
getPrimary().- Throws:
java.security.GeneralSecurityException
-
equalsKeyset
public boolean equalsKeyset(KeysetHandle other)
Returns true if this keyset is equal toother, ignoring monitoring annotations.Note: this may return false even if the keysets represent the same set of functions. For example, this can happen if the keys store zero-byte padding of a
BigInteger, which are irrelevant to the function computed. Currently, keysets can also be invalid in which case this will return false.
-
-