Class MutableSerializationRegistry


  • public final class MutableSerializationRegistry
    extends java.lang.Object
    A Mutable version of the SerializationRegistry.

    This class probably shouldn't exist; it would be better if we had only the SerializationRegistry. However, at the moment, we need this, since a call to e.g.

     AesCmacKeyManager.register() 
    should register such an object into a global, mutable registry.
    • Constructor Detail

      • MutableSerializationRegistry

        public MutableSerializationRegistry()
    • Method Detail

      • registerKeySerializer

        public <KeyT extends Key> void registerKeySerializer​(KeySerializer<KeyT> serializer)
                                                      throws java.security.GeneralSecurityException
        Registers a key serializer for later use in serializeKey(KeyT, com.google.crypto.tink.SecretKeyAccess).

        This registers a key serializer which can later be used to serialize a key by calling serializeKey(KeyT, com.google.crypto.tink.SecretKeyAccess). If a serializer for the pair (KeyT, SerializationT) has already been registered, this checks if they are the same. If they are, the call is ignored, otherwise an exception is thrown, and the object is unchanged.

        Throws:
        java.security.GeneralSecurityException
      • hasParserForKey

        public boolean hasParserForKey​(ProtoKeySerialization serializedKey)
        Returns true if a parser for this serializedKey has been registered.
      • parseKey

        public Key parseKey​(ProtoKeySerialization serializedKey,
                            @Nullable
                            SecretKeyAccess access)
                     throws java.security.GeneralSecurityException
        Parses the given serialization into a Key.

        This will look up a previously registered parser for the passed in SerializationT class, and the used object identifier (as indicated by serializedKey.getObjectIdentifier()), and then parse the object with this parsers.

        Throws:
        java.security.GeneralSecurityException
      • parseKeyWithLegacyFallback

        public Key parseKeyWithLegacyFallback​(ProtoKeySerialization protoKeySerialization,
                                              @Nullable
                                              SecretKeyAccess access)
                                       throws java.security.GeneralSecurityException
        Returns a Key object from a protoKeySerialization, even if no parser has been registered.

        Falling back is useful because we want users to be able to call #getAt even for key types for which we did not yet register a parser; in this case we simply fall back to return a LegacyProtoKey.

        Throws:
        java.security.GeneralSecurityException - if a parser is registered but parsing fails or required SecretKeyAccess is missing
      • hasSerializerForKey

        public <KeyT extends Key> boolean hasSerializerForKey​(KeyT key,
                                                              java.lang.Class<?> serializationClass)
        Returns true if a parser for this serializedKey has been registered.
      • serializeKey

        public <KeyT extends Key> ProtoKeySerialization serializeKey​(KeyT key,
                                                                     @Nullable
                                                                     SecretKeyAccess access)
                                                              throws java.security.GeneralSecurityException
        Serializes a given Key into a "SerializationT" object.

        This will look up a previously registered serializer for the requested SerializationT class and the passed in key type, and then call serializeKey on the result.

        Throws:
        java.security.GeneralSecurityException
      • hasParserForParameters

        public boolean hasParserForParameters​(ProtoParametersSerialization serializedParameters)
        Returns true if a parser for this serializedKey has been registered.
      • parseParameters

        public Parameters parseParameters​(ProtoParametersSerialization serializedParameters)
                                   throws java.security.GeneralSecurityException
        Parses the given serialization into a Parameters object.

        This will look up a previously registered parser for the passed in SerializationT class, and the used object identifier (as indicated by serializedKey.getObjectIdentifier()), and then parse the object with this parsers.

        Throws:
        java.security.GeneralSecurityException
      • parseParametersWithLegacyFallback

        public Parameters parseParametersWithLegacyFallback​(ProtoParametersSerialization protoParametersSerialization)
                                                     throws java.security.GeneralSecurityException
        Returns a Parameters object from a protoKeySerialization, even if no parser has been registered.

        Falling back is useful because we need to have a parameters object even if no parser is registered (e.g. for when we create a Key from a key template/parameters name object).

        Throws:
        java.security.GeneralSecurityException
      • hasSerializerForParameters

        public <ParametersT extends Parameters> boolean hasSerializerForParameters​(ParametersT parameters,
                                                                                   java.lang.Class<?> serializationClass)
        Returns true if a parser for this serializedKey has been registered.
      • serializeParameters

        public <ParametersT extends Parameters> ProtoParametersSerialization serializeParameters​(ParametersT parameters)
                                                                                          throws java.security.GeneralSecurityException
        Serializes a given Parameters object into a "SerializationT" object.

        This will look up a previously registered serializer for the requested SerializationT class and the passed in key type, and then call serializeKey on the result.

        Throws:
        java.security.GeneralSecurityException