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