]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/storage/protocol/JsonIdentityKeyStore.java
e086b929e37a92b1c6dc0bde5893064bf3729566
[signal-cli] / src / main / java / org / asamk / signal / storage / protocol / JsonIdentityKeyStore.java
1 package org.asamk.signal.storage.protocol;
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.asamk.signal.TrustLevel;
8 import org.whispersystems.libsignal.IdentityKey;
9 import org.whispersystems.libsignal.IdentityKeyPair;
10 import org.whispersystems.libsignal.InvalidKeyException;
11 import org.whispersystems.libsignal.SignalProtocolAddress;
12 import org.whispersystems.libsignal.state.IdentityKeyStore;
13 import org.whispersystems.signalservice.internal.util.Base64;
14
15 import java.io.IOException;
16 import java.util.*;
17
18 public class JsonIdentityKeyStore implements IdentityKeyStore {
19
20 private final Map<String, List<Identity>> trustedKeys = new HashMap<>();
21
22 private final IdentityKeyPair identityKeyPair;
23 private final int localRegistrationId;
24
25 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) {
26 this.identityKeyPair = identityKeyPair;
27 this.localRegistrationId = localRegistrationId;
28 }
29
30 @Override
31 public IdentityKeyPair getIdentityKeyPair() {
32 return identityKeyPair;
33 }
34
35 @Override
36 public int getLocalRegistrationId() {
37 return localRegistrationId;
38 }
39
40 @Override
41 public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey) {
42 return saveIdentity(address.getName(), identityKey, TrustLevel.TRUSTED_UNVERIFIED, null);
43 }
44
45 /**
46 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
47 *
48 * @param name User name, i.e. phone number
49 * @param identityKey The user's public key
50 * @param trustLevel
51 * @param added Added timestamp, if null and the key is newly added, the current time is used.
52 */
53 public boolean saveIdentity(String name, IdentityKey identityKey, TrustLevel trustLevel, Date added) {
54 List<Identity> identities = trustedKeys.get(name);
55 if (identities == null) {
56 identities = new ArrayList<>();
57 trustedKeys.put(name, identities);
58 } else {
59 for (Identity id : identities) {
60 if (!id.identityKey.equals(identityKey))
61 continue;
62
63 if (id.trustLevel.compareTo(trustLevel) < 0) {
64 id.trustLevel = trustLevel;
65 }
66 if (added != null) {
67 id.added = added;
68 }
69 return true;
70 }
71 }
72 identities.add(new Identity(identityKey, trustLevel, added != null ? added : new Date()));
73 return false;
74 }
75
76 @Override
77 public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey, Direction direction) {
78 // TODO implement possibility for different handling of incoming/outgoing trust decisions
79 List<Identity> identities = trustedKeys.get(address.getName());
80 if (identities == null) {
81 // Trust on first use
82 return true;
83 }
84
85 for (Identity id : identities) {
86 if (id.identityKey.equals(identityKey)) {
87 return id.isTrusted();
88 }
89 }
90
91 return false;
92 }
93
94 @Override
95 public IdentityKey getIdentity(SignalProtocolAddress address) {
96 List<Identity> identities = trustedKeys.get(address.getName());
97 if (identities == null) {
98 return null;
99 }
100
101 long maxDate = 0;
102 Identity maxIdentity = null;
103 for (Identity id : identities) {
104 final long time = id.getDateAdded().getTime();
105 if (maxDate <= time) {
106 maxDate = time;
107 maxIdentity = id;
108 }
109 }
110 return maxIdentity.getIdentityKey();
111 }
112
113 public Map<String, List<Identity>> getIdentities() {
114 // TODO deep copy
115 return trustedKeys;
116 }
117
118 public List<Identity> getIdentities(String name) {
119 // TODO deep copy
120 return trustedKeys.get(name);
121 }
122
123 public static class JsonIdentityKeyStoreDeserializer extends JsonDeserializer<JsonIdentityKeyStore> {
124
125 @Override
126 public JsonIdentityKeyStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
127 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
128
129 try {
130 int localRegistrationId = node.get("registrationId").asInt();
131 IdentityKeyPair identityKeyPair = new IdentityKeyPair(Base64.decode(node.get("identityKey").asText()));
132
133 JsonIdentityKeyStore keyStore = new JsonIdentityKeyStore(identityKeyPair, localRegistrationId);
134
135 JsonNode trustedKeysNode = node.get("trustedKeys");
136 if (trustedKeysNode.isArray()) {
137 for (JsonNode trustedKey : trustedKeysNode) {
138 String trustedKeyName = trustedKey.get("name").asText();
139 try {
140 IdentityKey id = new IdentityKey(Base64.decode(trustedKey.get("identityKey").asText()), 0);
141 TrustLevel trustLevel = trustedKey.has("trustLevel") ? TrustLevel.fromInt(trustedKey.get("trustLevel").asInt()) : TrustLevel.TRUSTED_UNVERIFIED;
142 Date added = trustedKey.has("addedTimestamp") ? new Date(trustedKey.get("addedTimestamp").asLong()) : new Date();
143 keyStore.saveIdentity(trustedKeyName, id, trustLevel, added);
144 } catch (InvalidKeyException | IOException e) {
145 System.out.println(String.format("Error while decoding key for: %s", trustedKeyName));
146 }
147 }
148 }
149
150 return keyStore;
151 } catch (InvalidKeyException e) {
152 throw new IOException(e);
153 }
154 }
155 }
156
157 public static class JsonIdentityKeyStoreSerializer extends JsonSerializer<JsonIdentityKeyStore> {
158
159 @Override
160 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
161 json.writeStartObject();
162 json.writeNumberField("registrationId", jsonIdentityKeyStore.getLocalRegistrationId());
163 json.writeStringField("identityKey", Base64.encodeBytes(jsonIdentityKeyStore.getIdentityKeyPair().serialize()));
164 json.writeArrayFieldStart("trustedKeys");
165 for (Map.Entry<String, List<Identity>> trustedKey : jsonIdentityKeyStore.trustedKeys.entrySet()) {
166 for (Identity id : trustedKey.getValue()) {
167 json.writeStartObject();
168 json.writeStringField("name", trustedKey.getKey());
169 json.writeStringField("identityKey", Base64.encodeBytes(id.identityKey.serialize()));
170 json.writeNumberField("trustLevel", id.trustLevel.ordinal());
171 json.writeNumberField("addedTimestamp", id.added.getTime());
172 json.writeEndObject();
173 }
174 }
175 json.writeEndArray();
176 json.writeEndObject();
177 }
178 }
179
180 public class Identity {
181
182 IdentityKey identityKey;
183 TrustLevel trustLevel;
184 Date added;
185
186 public Identity(IdentityKey identityKey, TrustLevel trustLevel) {
187 this.identityKey = identityKey;
188 this.trustLevel = trustLevel;
189 this.added = new Date();
190 }
191
192 public Identity(IdentityKey identityKey, TrustLevel trustLevel, Date added) {
193 this.identityKey = identityKey;
194 this.trustLevel = trustLevel;
195 this.added = added;
196 }
197
198 public boolean isTrusted() {
199 return trustLevel == TrustLevel.TRUSTED_UNVERIFIED ||
200 trustLevel == TrustLevel.TRUSTED_VERIFIED;
201 }
202
203 public IdentityKey getIdentityKey() {
204 return this.identityKey;
205 }
206
207 public TrustLevel getTrustLevel() {
208 return this.trustLevel;
209 }
210
211 public Date getDateAdded() {
212 return this.added;
213 }
214
215 public byte[] getFingerprint() {
216 return identityKey.getPublicKey().serialize();
217 }
218 }
219 }