]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/textsecure/JsonIdentityKeyStore.java
050158fc663cce7b50cbdcecc9f2938ac6fd6365
[signal-cli] / src / main / java / org / asamk / textsecure / JsonIdentityKeyStore.java
1 package org.asamk.textsecure;
2
3 import com.fasterxml.jackson.core.JsonGenerator;
4 import com.fasterxml.jackson.core.JsonParser;
5 import com.fasterxml.jackson.core.JsonProcessingException;
6 import com.fasterxml.jackson.databind.*;
7 import org.whispersystems.libaxolotl.IdentityKey;
8 import org.whispersystems.libaxolotl.IdentityKeyPair;
9 import org.whispersystems.libaxolotl.InvalidKeyException;
10 import org.whispersystems.libaxolotl.state.IdentityKeyStore;
11
12 import java.io.IOException;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 class JsonIdentityKeyStore implements IdentityKeyStore {
17
18 private final Map<String, IdentityKey> trustedKeys = new HashMap<>();
19
20 private final IdentityKeyPair identityKeyPair;
21 private final int localRegistrationId;
22
23
24 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) {
25 this.identityKeyPair = identityKeyPair;
26 this.localRegistrationId = localRegistrationId;
27 }
28
29 public void addTrustedKeys(Map<String, IdentityKey> keyMap) {
30 trustedKeys.putAll(keyMap);
31 }
32
33 @Override
34 public IdentityKeyPair getIdentityKeyPair() {
35 return identityKeyPair;
36 }
37
38 @Override
39 public int getLocalRegistrationId() {
40 return localRegistrationId;
41 }
42
43 @Override
44 public void saveIdentity(String name, IdentityKey identityKey) {
45 trustedKeys.put(name, identityKey);
46 }
47
48 @Override
49 public boolean isTrustedIdentity(String name, IdentityKey identityKey) {
50 IdentityKey trusted = trustedKeys.get(name);
51 return (trusted == null || trusted.equals(identityKey));
52 }
53
54 public static class JsonIdentityKeyStoreDeserializer extends JsonDeserializer<JsonIdentityKeyStore> {
55
56 @Override
57 public JsonIdentityKeyStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
58 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
59
60 try {
61 int localRegistrationId = node.get("registrationId").asInt();
62 IdentityKeyPair identityKeyPair = new IdentityKeyPair(Base64.decode(node.get("identityKey").asText()));
63
64
65 Map<String, IdentityKey> trustedKeyMap = new HashMap<>();
66 JsonNode trustedKeysNode = node.get("trustedKeys");
67 if (trustedKeysNode.isArray()) {
68 for (JsonNode trustedKey : trustedKeysNode) {
69 String trustedKeyName = trustedKey.get("name").asText();
70 try {
71 trustedKeyMap.put(trustedKeyName, new IdentityKey(Base64.decode(trustedKey.get("identityKey").asText()), 0));
72 } catch (InvalidKeyException | IOException e) {
73 System.out.println(String.format("Error while decoding key for: %s", trustedKeyName));
74 }
75 }
76 }
77
78 JsonIdentityKeyStore keyStore = new JsonIdentityKeyStore(identityKeyPair, localRegistrationId);
79 keyStore.addTrustedKeys(trustedKeyMap);
80
81 return keyStore;
82
83 } catch (InvalidKeyException e) {
84 throw new IOException(e);
85 }
86 }
87 }
88
89 public static class JsonIdentityKeyStoreSerializer extends JsonSerializer<JsonIdentityKeyStore> {
90
91 @Override
92 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
93 json.writeStartObject();
94 json.writeNumberField("registrationId", jsonIdentityKeyStore.getLocalRegistrationId());
95 json.writeStringField("identityKey", Base64.encodeBytes(jsonIdentityKeyStore.getIdentityKeyPair().serialize()));
96 json.writeArrayFieldStart("trustedKeys");
97 for (Map.Entry<String, IdentityKey> trustedKey : jsonIdentityKeyStore.trustedKeys.entrySet()) {
98 json.writeStartObject();
99 json.writeStringField("name", trustedKey.getKey());
100 json.writeStringField("identityKey", Base64.encodeBytes(trustedKey.getValue().serialize()));
101 json.writeEndObject();
102 }
103 json.writeEndArray();
104 json.writeEndObject();
105 }
106 }
107 }