Skip to content

Commit

Permalink
Replace org.json with GSON.
Browse files Browse the repository at this point in the history
The former uses a non-free license, see tink-crypto#450.

PiperOrigin-RevId: 357296560
  • Loading branch information
ise-crypto authored and copybara-github committed Feb 13, 2021
1 parent 69a8f22 commit 0a82140
Show file tree
Hide file tree
Showing 28 changed files with 451 additions and 448 deletions.
6 changes: 2 additions & 4 deletions java_src/src/main/java/com/google/crypto/tink/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ java_library(
":keyset_writer",
"//proto:tink_java_proto",
"//src/main/java/com/google/crypto/tink/subtle:base64",
"@maven//:com_google_code_gson_gson",
"@maven//:org_json_json",
],
)

Expand All @@ -139,7 +139,6 @@ android_library(
":keyset_writer-android",
"//proto:tink_java_proto_lite",
"//src/main/java/com/google/crypto/tink/subtle:base64",
"@maven//:com_google_code_gson_gson",
],
)

Expand Down Expand Up @@ -184,7 +183,7 @@ java_library(
"//proto:tink_java_proto",
"//src/main/java/com/google/crypto/tink/subtle:base64",
"@com_google_protobuf//:protobuf_javalite",
"@maven//:com_google_code_gson_gson",
"@maven//:org_json_json",
],
)

Expand All @@ -197,7 +196,6 @@ android_library(
"//proto:tink_java_proto_lite",
"//src/main/java/com/google/crypto/tink/subtle:base64",
"@com_google_protobuf//:protobuf_javalite",
"@maven//:com_google_code_gson_gson",
],
)

Expand Down
122 changes: 58 additions & 64 deletions java_src/src/main/java/com/google/crypto/tink/JsonKeysetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import com.google.crypto.tink.proto.KeysetInfo;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.subtle.Base64;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.protobuf.ByteString;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand All @@ -36,6 +32,9 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Path;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
* A {@link KeysetReader} that can read from source source cleartext or encrypted keysets in <a
Expand All @@ -48,15 +47,15 @@ public final class JsonKeysetReader implements KeysetReader {
private static final Charset UTF_8 = Charset.forName("UTF-8");

private final InputStream inputStream;
private final JsonObject json;
private final JSONObject json;
private boolean urlSafeBase64 = false;

private JsonKeysetReader(InputStream inputStream) {
this.inputStream = inputStream;
json = null;
}

private JsonKeysetReader(JsonObject json) {
private JsonKeysetReader(JSONObject json) {
this.json = json;
this.inputStream = null;
}
Expand All @@ -71,14 +70,9 @@ public static KeysetReader withInputStream(InputStream input) throws IOException
return new JsonKeysetReader(input);
}

/**
* Static method to create a JsonKeysetReader from an {@link JsonObject}.
*
* @deprecated Use {@code #withString}
*/
@Deprecated
public static JsonKeysetReader withJsonObject(Object input) {
return withString(input.toString());
/** Static method to create a JsonKeysetReader from an {@link JSONObject}. */
public static JsonKeysetReader withJsonObject(JSONObject input) {
return new JsonKeysetReader(input);
}

/** Static method to create a JsonKeysetReader from a string. */
Expand Down Expand Up @@ -136,10 +130,10 @@ public Keyset read() throws IOException {
if (json != null) {
return keysetFromJson(json);
} else {
return keysetFromJson(
JsonParser.parseString(new String(Util.readAll(inputStream), UTF_8)).getAsJsonObject());
return keysetFromJson(new JSONObject(
new String(Util.readAll(inputStream), UTF_8)));
}
} catch (JsonParseException | IllegalStateException e) {
} catch (JSONException e) {
throw new IOException(e);
} finally {
if (inputStream != null) {
Expand All @@ -154,10 +148,10 @@ public EncryptedKeyset readEncrypted() throws IOException {
if (json != null) {
return encryptedKeysetFromJson(json);
} else {
return encryptedKeysetFromJson(
JsonParser.parseString(new String(Util.readAll(inputStream), UTF_8)).getAsJsonObject());
return encryptedKeysetFromJson(new JSONObject(
new String(Util.readAll(inputStream), UTF_8)));
}
} catch (JsonParseException | IllegalStateException e) {
} catch (JSONException e) {
throw new IOException(e);
} finally {
if (inputStream != null) {
Expand All @@ -166,91 +160,91 @@ public EncryptedKeyset readEncrypted() throws IOException {
}
}

private Keyset keysetFromJson(JsonObject json) {
private Keyset keysetFromJson(JSONObject json) throws JSONException {
validateKeyset(json);
Keyset.Builder builder = Keyset.newBuilder();
if (json.has("primaryKeyId")) {
builder.setPrimaryKeyId(json.get("primaryKeyId").getAsInt());
builder.setPrimaryKeyId(json.getInt("primaryKeyId"));
}
JsonArray keys = json.getAsJsonArray("key");
for (int i = 0; i < keys.size(); i++) {
builder.addKey(keyFromJson(keys.get(i).getAsJsonObject()));
JSONArray keys = json.getJSONArray("key");
for (int i = 0; i < keys.length(); i++) {
builder.addKey(keyFromJson(keys.getJSONObject(i)));
}
return builder.build();
}

private EncryptedKeyset encryptedKeysetFromJson(JsonObject json) {
private EncryptedKeyset encryptedKeysetFromJson(JSONObject json) throws JSONException {
validateEncryptedKeyset(json);
byte[] encryptedKeyset;
if (urlSafeBase64) {
encryptedKeyset = Base64.urlSafeDecode(json.get("encryptedKeyset").getAsString());
encryptedKeyset = Base64.urlSafeDecode(json.getString("encryptedKeyset"));
} else {
encryptedKeyset = Base64.decode(json.get("encryptedKeyset").getAsString());
encryptedKeyset = Base64.decode(json.getString("encryptedKeyset"));
}
return EncryptedKeyset.newBuilder()
.setEncryptedKeyset(ByteString.copyFrom(encryptedKeyset))
.setKeysetInfo(keysetInfoFromJson(json.getAsJsonObject("keysetInfo")))
.setKeysetInfo(keysetInfoFromJson(json.getJSONObject("keysetInfo")))
.build();
}

private Keyset.Key keyFromJson(JsonObject json) {
private Keyset.Key keyFromJson(JSONObject json) throws JSONException {
validateKey(json);
return Keyset.Key.newBuilder()
.setStatus(getStatus(json.get("status").getAsString()))
.setKeyId(json.get("keyId").getAsInt())
.setOutputPrefixType(getOutputPrefixType(json.get("outputPrefixType").getAsString()))
.setKeyData(keyDataFromJson(json.getAsJsonObject("keyData")))
.setStatus(getStatus(json.getString("status")))
.setKeyId(json.getInt("keyId"))
.setOutputPrefixType(getOutputPrefixType(json.getString("outputPrefixType")))
.setKeyData(keyDataFromJson(json.getJSONObject("keyData")))
.build();
}

private static KeysetInfo keysetInfoFromJson(JsonObject json) {
private static KeysetInfo keysetInfoFromJson(JSONObject json) throws JSONException {
KeysetInfo.Builder builder = KeysetInfo.newBuilder();
if (json.has("primaryKeyId")) {
builder.setPrimaryKeyId(json.get("primaryKeyId").getAsInt());
builder.setPrimaryKeyId(json.getInt("primaryKeyId"));
}
if (json.has("keyInfo")) {
JsonArray keyInfos = json.getAsJsonArray("keyInfo");
for (int i = 0; i < keyInfos.size(); i++) {
builder.addKeyInfo(keyInfoFromJson(keyInfos.get(i).getAsJsonObject()));
JSONArray keyInfos = json.getJSONArray("keyInfo");
for (int i = 0; i < keyInfos.length(); i++) {
builder.addKeyInfo(keyInfoFromJson(keyInfos.getJSONObject(i)));
}
}
return builder.build();
}

private static KeysetInfo.KeyInfo keyInfoFromJson(JsonObject json) {
private static KeysetInfo.KeyInfo keyInfoFromJson(JSONObject json) throws JSONException {
return KeysetInfo.KeyInfo.newBuilder()
.setStatus(getStatus(json.get("status").getAsString()))
.setKeyId(json.get("keyId").getAsInt())
.setOutputPrefixType(getOutputPrefixType(json.get("outputPrefixType").getAsString()))
.setTypeUrl(json.get("typeUrl").getAsString())
.setStatus(getStatus(json.getString("status")))
.setKeyId(json.getInt("keyId"))
.setOutputPrefixType(getOutputPrefixType(json.getString("outputPrefixType")))
.setTypeUrl(json.getString("typeUrl"))
.build();
}

private KeyData keyDataFromJson(JsonObject json) {
private KeyData keyDataFromJson(JSONObject json) throws JSONException {
validateKeyData(json);
byte[] value;
if (urlSafeBase64) {
value = Base64.urlSafeDecode(json.get("value").getAsString());
value = Base64.urlSafeDecode(json.getString("value"));
} else {
value = Base64.decode(json.get("value").getAsString());
value = Base64.decode(json.getString("value"));
}
return KeyData.newBuilder()
.setTypeUrl(json.get("typeUrl").getAsString())
.setTypeUrl(json.getString("typeUrl"))
.setValue(ByteString.copyFrom(value))
.setKeyMaterialType(getKeyMaterialType(json.get("keyMaterialType").getAsString()))
.setKeyMaterialType(getKeyMaterialType(json.getString("keyMaterialType")))
.build();
}

private static KeyStatusType getStatus(String status) {
private static KeyStatusType getStatus(String status) throws JSONException {
if (status.equals("ENABLED")) {
return KeyStatusType.ENABLED;
} else if (status.equals("DISABLED")) {
return KeyStatusType.DISABLED;
}
throw new JsonParseException("unknown status: " + status);
throw new JSONException("unknown status: " + status);
}

private static OutputPrefixType getOutputPrefixType(String type) {
private static OutputPrefixType getOutputPrefixType(String type) throws JSONException {
if (type.equals("TINK")) {
return OutputPrefixType.TINK;
} else if (type.equals("RAW")) {
Expand All @@ -260,10 +254,10 @@ private static OutputPrefixType getOutputPrefixType(String type) {
} else if (type.equals("CRUNCHY")) {
return OutputPrefixType.CRUNCHY;
}
throw new JsonParseException("unknown output prefix type: " + type);
throw new JSONException("unknown output prefix type: " + type);
}

private static KeyMaterialType getKeyMaterialType(String type) {
private static KeyMaterialType getKeyMaterialType(String type) throws JSONException {
if (type.equals("SYMMETRIC")) {
return KeyMaterialType.SYMMETRIC;
} else if (type.equals("ASYMMETRIC_PRIVATE")) {
Expand All @@ -273,33 +267,33 @@ private static KeyMaterialType getKeyMaterialType(String type) {
} else if (type.equals("REMOTE")) {
return KeyMaterialType.REMOTE;
}
throw new JsonParseException("unknown key material type: " + type);
throw new JSONException("unknown key material type: " + type);
}

private static void validateKeyset(JsonObject json) {
if (!json.has("key") || json.getAsJsonArray("key").size() == 0) {
throw new JsonParseException("invalid keyset");
private static void validateKeyset(JSONObject json) throws JSONException {
if (!json.has("key") || json.getJSONArray("key").length() == 0) {
throw new JSONException("invalid keyset");
}
}

private static void validateEncryptedKeyset(JsonObject json) {
private static void validateEncryptedKeyset(JSONObject json) throws JSONException {
if (!json.has("encryptedKeyset")) {
throw new JsonParseException("invalid encrypted keyset");
throw new JSONException("invalid encrypted keyset");
}
}

private static void validateKey(JsonObject json) {
private static void validateKey(JSONObject json) throws JSONException {
if (!json.has("keyData")
|| !json.has("status")
|| !json.has("keyId")
|| !json.has("outputPrefixType")) {
throw new JsonParseException("invalid key");
throw new JSONException("invalid key");
}
}

private static void validateKeyData(JsonObject json) {
private static void validateKeyData(JSONObject json) throws JSONException {
if (!json.has("typeUrl") || !json.has("value") || !json.has("keyMaterialType")) {
throw new JsonParseException("invalid keyData");
throw new JSONException("invalid keyData");
}
}
}
Loading

0 comments on commit 0a82140

Please sign in to comment.