Search in sources :

Example 1 with GetResponse

use of com.google.nigori.common.NigoriMessages.GetResponse in project nigori by ucam-cl-dtg.

the class CryptoNigoriDatastore method get.

/**
 * Retrieve the value associated with {@code index} on the server.
 *
 * @param index
 * @param revision
 * @return a list of RevValues containing the data associated with {@code index} or {@code null}
 *         if no data exists. WARNING: there is no assurance that the value for the revision is a
 *         pair once specified by a valid client - the server can pair any value with any
 *         revision.
 */
private List<RevValue> get(byte[] encKey, Index index, Revision revision) throws IOException, NigoriCryptographyException, UnauthorisedException {
    byte[] encIndex;
    byte[] encRevision = null;
    if (encKey == null) {
        encIndex = keyManager.encryptDeterministically(index.getBytes());
        if (revision != null) {
            encRevision = keyManager.encryptDeterministically(revision.getBytes());
        }
    } else {
        encIndex = keyManager.encryptDeterministically(encKey, index.getBytes());
        if (revision != null) {
            encRevision = keyManager.encryptDeterministically(encKey, revision.getBytes());
        }
    }
    try {
        GetResponse getResponse = protocol.get(MessageLibrary.getRequestAsProtobuf(keyManager.getServerName(), keyManager.signer(), encIndex, encRevision));
        if (getResponse == null) {
            return null;
        }
        List<RevisionValue> revisions = getResponse.getRevisionsList();
        List<RevValue> answer = new ArrayList<RevValue>(revisions.size());
        for (RevisionValue revisionValue : revisions) {
            byte[] revisionciphertext = revisionValue.getRevision().toByteArray();
            byte[] valueciphertext = revisionValue.getValue().toByteArray();
            if (encKey == null) {
                answer.add(new RevValue(keyManager.decrypt(revisionciphertext), keyManager.decrypt(valueciphertext)));
            } else {
                answer.add(new RevValue(keyManager.decrypt(encKey, revisionciphertext), keyManager.decrypt(encKey, valueciphertext)));
            }
        }
        return answer;
    } catch (NotFoundException e) {
        return null;
    }
}
Also used : RevisionValue(com.google.nigori.common.NigoriMessages.RevisionValue) RevValue(com.google.nigori.common.RevValue) ArrayList(java.util.ArrayList) NotFoundException(com.google.nigori.common.NotFoundException) GetResponse(com.google.nigori.common.NigoriMessages.GetResponse)

Example 2 with GetResponse

use of com.google.nigori.common.NigoriMessages.GetResponse in project nigori by ucam-cl-dtg.

the class NigoriServletTest method testGetRequestKeyExists.

@Test
public void testGetRequestKeyExists() throws Exception {
    final byte[] key = toBytes("a key");
    final byte[] revision = toBytes("a revision");
    final byte[] value = toBytes("a value");
    final byte[] publicHash = keyManager.signer().getPublicHash();
    final String jsonGet = MessageLibrary.getRequestAsJson(serverName, keyManager.signer(), key, null);
    expectedCallsForJsonRequest(jsonGet, MessageLibrary.REQUEST_GET);
    expectedCallsToAuthenticateUser(publicHash);
    expect(database.getRecord(eq(user), aryEq(key))).andReturn(Arrays.asList(new RevValue[] { new RevValue(revision, value) }));
    ServletOutputStream out = expectedCallsForJsonResponse();
    Capture<byte[]> result = new Capture<byte[]>();
    Capture<Integer> size = new Capture<Integer>();
    out.write(capture(result), eq(0), capture(size));
    out.flush();
    runReplayVerifyWithDoPost(out);
    String jsonResponse = new String(result.getValue(), 0, size.getValue(), MessageLibrary.CHARSET);
    GetResponse getResponse = MessageLibrary.getResponseFromJson(jsonResponse);
    List<RevisionValue> revs = getResponse.getRevisionsList();
    assertEquals(1, revs.size());
    for (RevisionValue rev : revs) {
        assertArrayEquals(revision, rev.getRevision().toByteArray());
        assertArrayEquals(value, rev.getValue().toByteArray());
    }
}
Also used : RevValue(com.google.nigori.common.RevValue) ServletOutputStream(javax.servlet.ServletOutputStream) RevisionValue(com.google.nigori.common.NigoriMessages.RevisionValue) GetResponse(com.google.nigori.common.NigoriMessages.GetResponse) Capture(org.easymock.Capture) Test(org.junit.Test)

Example 3 with GetResponse

use of com.google.nigori.common.NigoriMessages.GetResponse in project nigori by ucam-cl-dtg.

the class MessageLibrary method getResponseAsProtobuf.

public static GetResponse getResponseAsProtobuf(Collection<RevValue> revisions) {
    // TODO(drt24) add index
    List<RevisionValue> protoRevisions = new ArrayList<RevisionValue>(revisions.size());
    for (RevValue rv : revisions) {
        protoRevisions.add(RevisionValue.newBuilder().setRevision(ByteString.copyFrom(rv.getRevision().getBytes())).setValue(ByteString.copyFrom(rv.getValue())).build());
    }
    GetResponse resp = GetResponse.newBuilder().addAllRevisions(protoRevisions).build();
    return resp;
}
Also used : RevisionValue(com.google.nigori.common.NigoriMessages.RevisionValue) ArrayList(java.util.ArrayList) GetResponse(com.google.nigori.common.NigoriMessages.GetResponse)

Aggregations

GetResponse (com.google.nigori.common.NigoriMessages.GetResponse)3 RevisionValue (com.google.nigori.common.NigoriMessages.RevisionValue)3 RevValue (com.google.nigori.common.RevValue)2 ArrayList (java.util.ArrayList)2 NotFoundException (com.google.nigori.common.NotFoundException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Capture (org.easymock.Capture)1 Test (org.junit.Test)1