Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Move the SimpleParameters into LegacyProtoKey and rename it to Legacy…
Browse files Browse the repository at this point in the history
…ProtoParametersNotForCreation.

This is useful in many contexts when one has a LegacyProtoKey. While one still cannot create a new key out of it, one can at least call toString() on the parameters object, which should be useful in other cases too.

Also this simplifies the constructor of a PrimitiveSet Entry.

PiperOrigin-RevId: 466319231
  • Loading branch information
tholenst authored and copybara-github committed Aug 9, 2022
1 parent 270f07c commit 76008a2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 63 deletions.
64 changes: 3 additions & 61 deletions java_src/src/main/java/com/google/crypto/tink/PrimitiveSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

package com.google.crypto.tink;

import com.google.crypto.tink.annotations.Alpha;
import com.google.crypto.tink.internal.LegacyProtoKey;
import com.google.crypto.tink.internal.MutableSerializationRegistry;
import com.google.crypto.tink.internal.ProtoKeySerialization;
import com.google.crypto.tink.monitoring.MonitoringAnnotations;
import com.google.crypto.tink.proto.KeyStatusType;
import com.google.crypto.tink.proto.Keyset;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.subtle.Hex;
import com.google.errorprone.annotations.Immutable;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -57,51 +54,6 @@
*/
public final class PrimitiveSet<P> {

// A simple implementation of Parameters.
// Consider renaming this class and moving it into internal. And use it in LegacyProtoKey.
@Immutable
@Alpha
private static class SimpleParameters extends Parameters {

private final String typeUrl;
private final OutputPrefixType outputPrefixType;

@Override
public boolean hasIdRequirement() {
return outputPrefixType != OutputPrefixType.RAW;
}

// This function is needed because LiteProto do not have a good toString function.
private static String outputPrefixToString(OutputPrefixType outputPrefixType) {
switch (outputPrefixType) {
case TINK:
return "TINK";
case LEGACY:
return "LEGACY";
case RAW:
return "RAW";
case CRUNCHY:
return "CRUNCHY";
default:
return "UNKNOWN";
}
}

/**
* Returns the string representation. The exact details are unspecified and subject to change.
*/
@Override
public String toString() {
return String.format(
"(typeUrl=%s, outputPrefixType=%s)", typeUrl, outputPrefixToString(outputPrefixType));
}

private SimpleParameters(String typeUrl, OutputPrefixType outputPrefixType) {
this.typeUrl = typeUrl;
this.outputPrefixType = outputPrefixType;
}
}

/**
* A single entry in the set. In addition to the actual primitive it holds also some extra
* information about the primitive.
Expand All @@ -119,23 +71,20 @@ public static final class Entry<P> {
// The id of the key.
private final int keyId;
private final Key key;
private final Parameters parameters;

Entry(
P primitive,
final byte[] identifier,
KeyStatusType status,
OutputPrefixType outputPrefixType,
int keyId,
Key key,
Parameters parameters) {
Key key) {
this.primitive = primitive;
this.identifier = Arrays.copyOf(identifier, identifier.length);
this.status = status;
this.outputPrefixType = outputPrefixType;
this.keyId = keyId;
this.key = key;
this.parameters = parameters;
}

/**
Expand Down Expand Up @@ -174,7 +123,7 @@ public Key getKey() {
}

public Parameters getParameters() {
return parameters;
return key.getParameters();
}
}

Expand All @@ -195,21 +144,14 @@ private static <P> Entry<P> addEntryToMap(
key.getOutputPrefixType(),
idRequirement),
InsecureSecretKeyAccess.get());
Parameters parameters;
if (keyObject instanceof LegacyProtoKey) {
parameters = new SimpleParameters(key.getKeyData().getTypeUrl(), key.getOutputPrefixType());
} else {
parameters = keyObject.getParameters();
}
Entry<P> entry =
new Entry<P>(
primitive,
CryptoFormat.getOutputPrefix(key),
key.getStatus(),
key.getOutputPrefixType(),
key.getKeyId(),
keyObject,
parameters);
keyObject);
List<Entry<P>> list = new ArrayList<>();
list.add(entry);
// Cannot use byte[] as keys in hash map, convert to Prefix wrapper class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ java_library(
srcs = ["LegacyProtoKey.java"],
deps = [
":proto_key_serialization",
"//proto:tink_java_proto",
"//src/main/java/com/google/crypto/tink:key",
"//src/main/java/com/google/crypto/tink:parameters",
"//src/main/java/com/google/crypto/tink:secret_key_access",
Expand All @@ -356,6 +357,7 @@ android_library(
srcs = ["LegacyProtoKey.java"],
deps = [
":proto_key_serialization-android",
"//proto:tink_java_proto_lite",
"//src/main/java/com/google/crypto/tink:key-android",
"//src/main/java/com/google/crypto/tink:parameters-android",
"//src/main/java/com/google/crypto/tink:secret_key_access-android",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.crypto.tink.Key;
import com.google.crypto.tink.Parameters;
import com.google.crypto.tink.SecretKeyAccess;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.subtle.Bytes;
import com.google.errorprone.annotations.Immutable;
import java.security.GeneralSecurityException;
Expand All @@ -28,6 +29,53 @@
/** Implements a Key for legacy types where no actual parser is present. */
@Immutable
public final class LegacyProtoKey extends Key {
/**
* An implementation of Parameters which is returned by LegacyProtoKey.
*
* <p>In contrast to LegacyProtoParameters, this cannot be used to create a new LegacyProtoKey
* object.
*/
@Immutable
private static class LegacyProtoParametersNotForCreation extends Parameters {
private final String typeUrl;
private final OutputPrefixType outputPrefixType;

@Override
public boolean hasIdRequirement() {
return outputPrefixType != OutputPrefixType.RAW;
}

// This function is needed because LiteProto do not have a good toString function.
private static String outputPrefixToString(OutputPrefixType outputPrefixType) {
switch (outputPrefixType) {
case TINK:
return "TINK";
case LEGACY:
return "LEGACY";
case RAW:
return "RAW";
case CRUNCHY:
return "CRUNCHY";
default:
return "UNKNOWN";
}
}

/**
* Returns the string representation. The exact details are unspecified and subject to change.
*/
@Override
public String toString() {
return String.format(
"(typeUrl=%s, outputPrefixType=%s)", typeUrl, outputPrefixToString(outputPrefixType));
}

private LegacyProtoParametersNotForCreation(String typeUrl, OutputPrefixType outputPrefixType) {
this.typeUrl = typeUrl;
this.outputPrefixType = outputPrefixType;
}
}

private final ProtoKeySerialization serialization;

private static void throwIfMissingAccess(
Expand Down Expand Up @@ -104,9 +152,15 @@ public ProtoKeySerialization getSerialization(@Nullable SecretKeyAccess access)
return serialization;
}

/** Unsupported. */
/**
* Returns a LegacyParametersNotForCreation object.
*
* <p>Note: this is different from the {@code LegacyProtoParameters} object which was used to
* create this key. One cannot use the returned object to create a new key.
*/
@Override
public Parameters getParameters() {
throw new UnsupportedOperationException("Cannot get parameters on LegacyProtoKey");
return new LegacyProtoParametersNotForCreation(
serialization.getTypeUrl(), serialization.getOutputPrefixType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public void testLegacyProtoKeyCreate() throws Exception {
assertThat(key.getSerialization(ACCESS)).isSameInstanceAs(serialization);
}

@Test
public void testLegacyProtoKey_getParameters() throws Exception {
ProtoKeySerialization serialization =
ProtoKeySerialization.create(
"myTypeUrl",
ByteString.EMPTY,
KeyMaterialType.SYMMETRIC,
OutputPrefixType.RAW,
/*idRequirement = */ null);
LegacyProtoKey key = new LegacyProtoKey(serialization, ACCESS);
assertThat(key.getSerialization(ACCESS)).isSameInstanceAs(serialization);

assertThat(key.getParameters().toString()).contains("typeUrl=myTypeUrl");
assertThat(key.getParameters().toString()).contains("outputPrefixType=RAW");
}

@Test
public void testGetIdRequirementOrNull() throws Exception {
// RAW
Expand Down

0 comments on commit 76008a2

Please sign in to comment.