]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/storage/protocol/JsonIdentityKeyStore.java
5d8e0ea39671279548f52ac1d5f08b5775a918e2
[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.asamk.signal.util.Base64;
9 import org.whispersystems.libsignal.IdentityKey;
10 import org.whispersystems.libsignal.IdentityKeyPair;
11 import org.whispersystems.libsignal.InvalidKeyException;
12 import org.whispersystems.libsignal.SignalProtocolAddress;
13 import org.whispersystems.libsignal.state.IdentityKeyStore;
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 void saveIdentity(SignalProtocolAddress address, IdentityKey identityKey) {
43 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 void 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;
71 }
72 }
73 identities.add(new Identity(identityKey, trustLevel, added != null ? added : new Date()));
74 }
75
76 @Override
77 public boolean isTrustedIdentity(SignalProtocolAddress address, IdentityKey identityKey) {
78 List<Identity> identities = trustedKeys.get(address.getName());
79 if (identities == null) {
80 // Trust on first use
81 return true;
82 }
83
84 for (Identity id : identities) {
85 if (id.identityKey.equals(identityKey)) {
86 return id.isTrusted();
87 }
88 }
89
90 return false;
91 }
92
93 public Map<String, List<Identity>> getIdentities() {
94 // TODO deep copy
95 return trustedKeys;
96 }
97
98 public List<Identity> getIdentities(String name) {
99 // TODO deep copy
100 return trustedKeys.get(name);
101 }
102
103 public static class JsonIdentityKeyStoreDeserializer extends JsonDeserializer<JsonIdentityKeyStore> {
104
105 @Override
106 public JsonIdentityKeyStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
107 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
108
109 try {
110 int localRegistrationId = node.get("registrationId").asInt();
111 IdentityKeyPair identityKeyPair = new IdentityKeyPair(Base64.decode(node.get("identityKey").asText()));
112
113
114 JsonIdentityKeyStore keyStore = new JsonIdentityKeyStore(identityKeyPair, localRegistrationId);
115
116 JsonNode trustedKeysNode = node.get("trustedKeys");
117 if (trustedKeysNode.isArray()) {
118 for (JsonNode trustedKey : trustedKeysNode) {
119 String trustedKeyName = trustedKey.get("name").asText();
120 try {
121 IdentityKey id = new IdentityKey(Base64.decode(trustedKey.get("identityKey").asText()), 0);
122 TrustLevel trustLevel = trustedKey.has("trustLevel") ? TrustLevel.fromInt(trustedKey.get("trustLevel").asInt()) : TrustLevel.TRUSTED_UNVERIFIED;
123 Date added = trustedKey.has("addedTimestamp") ? new Date(trustedKey.get("addedTimestamp").asLong()) : new Date();
124 keyStore.saveIdentity(trustedKeyName, id, trustLevel, added);
125 } catch (InvalidKeyException | IOException e) {
126 System.out.println(String.format("Error while decoding key for: %s", trustedKeyName));
127 }
128 }
129 }
130
131 return keyStore;
132 } catch (InvalidKeyException e) {
133 throw new IOException(e);
134 }
135 }
136 }
137
138 public static class JsonIdentityKeyStoreSerializer extends JsonSerializer<JsonIdentityKeyStore> {
139
140 @Override
141 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
142 json.writeStartObject();
143 json.writeNumberField("registrationId", jsonIdentityKeyStore.getLocalRegistrationId());
144 json.writeStringField("identityKey", Base64.encodeBytes(jsonIdentityKeyStore.getIdentityKeyPair().serialize()));
145 json.writeArrayFieldStart("trustedKeys");
146 for (Map.Entry<String, List<Identity>> trustedKey : jsonIdentityKeyStore.trustedKeys.entrySet()) {
147 for (Identity id : trustedKey.getValue()) {
148 json.writeStartObject();
149 json.writeStringField("name", trustedKey.getKey());
150 json.writeStringField("identityKey", Base64.encodeBytes(id.identityKey.serialize()));
151 json.writeNumberField("trustLevel", id.trustLevel.ordinal());
152 json.writeNumberField("addedTimestamp", id.added.getTime());
153 json.writeEndObject();
154 }
155 }
156 json.writeEndArray();
157 json.writeEndObject();
158 }
159 }
160
161 public class Identity {
162 IdentityKey identityKey;
163 TrustLevel trustLevel;
164 Date added;
165
166 public Identity(IdentityKey identityKey, TrustLevel trustLevel) {
167 this.identityKey = identityKey;
168 this.trustLevel = trustLevel;
169 this.added = new Date();
170 }
171
172 public Identity(IdentityKey identityKey, TrustLevel trustLevel, Date added) {
173 this.identityKey = identityKey;
174 this.trustLevel = trustLevel;
175 this.added = added;
176 }
177
178 public boolean isTrusted() {
179 return trustLevel == TrustLevel.TRUSTED_UNVERIFIED ||
180 trustLevel == TrustLevel.TRUSTED_VERIFIED;
181 }
182
183 public IdentityKey getIdentityKey() {
184 return this.identityKey;
185 }
186
187 public TrustLevel getTrustLevel() {
188 return this.trustLevel;
189 }
190
191 public Date getDateAdded() {
192 return this.added;
193 }
194
195 public byte[] getFingerprint() {
196 return identityKey.getPublicKey().serialize();
197 }
198 }
199 }