Search in sources :

Example 1 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JsonKeysetReader method keysetFromJson.

private Keyset keysetFromJson(JSONObject json) throws JSONException {
    validateKeyset(json);
    Keyset.Builder builder = Keyset.newBuilder();
    if (json.has("primaryKeyId")) {
        builder.setPrimaryKeyId(json.getInt("primaryKeyId"));
    }
    JSONArray keys = json.getJSONArray("key");
    for (int i = 0; i < keys.length(); i++) {
        builder.addKey(keyFromJson(keys.getJSONObject(i)));
    }
    return builder.build();
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) JSONArray(org.json.JSONArray)

Example 2 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JsonKeysetReader method keysetFromJson.

private Keyset keysetFromJson(JsonObject json) {
    validateKeyset(json);
    Keyset.Builder builder = Keyset.newBuilder();
    if (json.has("primaryKeyId")) {
        builder.setPrimaryKeyId(json.get("primaryKeyId").getAsInt());
    }
    JsonArray keys = json.getAsJsonArray("key");
    for (int i = 0; i < keys.size(); i++) {
        builder.addKey(keyFromJson(keys.get(i).getAsJsonObject()));
    }
    return builder.build();
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) JsonArray(com.google.gson.JsonArray)

Example 3 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandle method getKeys.

/**
 * Returns the keyset data as a list of {@link KeyHandle}s.
 */
public List<KeyHandle> getKeys() {
    ArrayList<KeyHandle> result = new ArrayList<>();
    for (Keyset.Key key : keyset.getKeyList()) {
        KeyData keyData = key.getKeyData();
        result.add(new InternalKeyHandle(new ProtoKey(keyData, KeyTemplate.fromProto(key.getOutputPrefixType())), key.getStatus(), key.getKeyId()));
    }
    return Collections.unmodifiableList(result);
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) InternalKeyHandle(com.google.crypto.tink.tinkkey.internal.InternalKeyHandle) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) ArrayList(java.util.ArrayList) KeyHandle(com.google.crypto.tink.tinkkey.KeyHandle) InternalKeyHandle(com.google.crypto.tink.tinkkey.internal.InternalKeyHandle) KeyData(com.google.crypto.tink.proto.KeyData)

Example 4 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandle method decrypt.

/**
 * Decrypts the encrypted keyset with the {@link Aead} master key.
 */
private static Keyset decrypt(EncryptedKeyset encryptedKeyset, Aead masterKey, byte[] associatedData) throws GeneralSecurityException {
    try {
        Keyset keyset = Keyset.parseFrom(masterKey.decrypt(encryptedKeyset.getEncryptedKeyset().toByteArray(), associatedData), ExtensionRegistryLite.getEmptyRegistry());
        // check emptiness here too, in case the encrypted keys unwrapped to nothing?
        assertEnoughKeyMaterial(keyset);
        return keyset;
    } catch (@SuppressWarnings("UnusedException") InvalidProtocolBufferException e) {
        // Do not propagate InvalidProtocolBufferException to guarantee no key material is leaked
        throw new GeneralSecurityException("invalid keyset, corrupted key material");
    }
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException)

Example 5 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JsonKeysetWriter method toJson.

private JsonObject toJson(Keyset keyset) {
    JsonObject json = new JsonObject();
    json.addProperty("primaryKeyId", toUnsignedLong(keyset.getPrimaryKeyId()));
    JsonArray keys = new JsonArray();
    for (Keyset.Key key : keyset.getKeyList()) {
        keys.add(toJson(key));
    }
    json.add("key", keys);
    return json;
}
Also used : JsonArray(com.google.gson.JsonArray) EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) JsonObject(com.google.gson.JsonObject)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6