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