JSONObject response = (JSONObject) JSONValue.parse(sthResponse);
long treeSize = (Long) response.get("tree_size");
long timeStamp = (Long) response.get("timestamp");
if (treeSize < 0 || timeStamp < 0) {
throw new CertificateTransparencyException(
String.format("Bad response. Size of tree or timestamp cannot be a negative value. "
+ "Log Tree size: %d Timestamp: %d", treeSize, timeStamp));
}
String base64Signature = (String) response.get("tree_head_signature");
String sha256RootHash = (String) response.get("sha256_root_hash");
Ct.SignedTreeHead.Builder builder = Ct.SignedTreeHead.newBuilder();
builder.setVersion(Ct.Version.V1);
builder.setTreeSize(treeSize);
builder.setTimestamp(timeStamp);
builder.setSha256RootHash(ByteString.copyFrom(Base64.decodeBase64(sha256RootHash)));
builder.setSignature(Deserializer.parseDigitallySignedFromBinary(
new ByteArrayInputStream(Base64.decodeBase64(base64Signature))));
if (builder.getSha256RootHash().size() != 32) {
throw new CertificateTransparencyException(
String.format("Bad response. The root hash of the Merkle Hash Tree must be 32 bytes. "
+ "The size of the root hash is %d", builder.getSha256RootHash().size()));
}
return builder.build();
}