1 package org
.asamk
.signal
.storage
.protocol
;
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
;
15 import java
.io
.IOException
;
18 public class JsonIdentityKeyStore
implements IdentityKeyStore
{
20 private final Map
<String
, List
<Identity
>> trustedKeys
= new HashMap
<>();
22 private final IdentityKeyPair identityKeyPair
;
23 private final int localRegistrationId
;
25 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair
, int localRegistrationId
) {
26 this.identityKeyPair
= identityKeyPair
;
27 this.localRegistrationId
= localRegistrationId
;
31 public IdentityKeyPair
getIdentityKeyPair() {
32 return identityKeyPair
;
36 public int getLocalRegistrationId() {
37 return localRegistrationId
;
41 public boolean saveIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
42 return saveIdentity(address
.getName(), identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
, null);
46 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
48 * @param name User name, i.e. phone number
49 * @param identityKey The user's public key
51 * @param added Added timestamp, if null and the key is newly added, the current time is used.
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
);
59 for (Identity id
: identities
) {
60 if (!id
.identityKey
.equals(identityKey
))
63 if (id
.trustLevel
.compareTo(trustLevel
) < 0) {
64 id
.trustLevel
= trustLevel
;
72 identities
.add(new Identity(identityKey
, trustLevel
, added
!= null ? added
: new Date()));
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) {
85 for (Identity id
: identities
) {
86 if (id
.identityKey
.equals(identityKey
)) {
87 return id
.isTrusted();
95 public IdentityKey
getIdentity(SignalProtocolAddress address
) {
96 List
<Identity
> identities
= trustedKeys
.get(address
.getName());
97 if (identities
== null) {
102 Identity maxIdentity
= null;
103 for (Identity id
: identities
) {
104 final long time
= id
.getDateAdded().getTime();
105 if (maxDate
<= time
) {
110 return maxIdentity
.getIdentityKey();
113 public Map
<String
, List
<Identity
>> getIdentities() {
118 public List
<Identity
> getIdentities(String name
) {
120 return trustedKeys
.get(name
);
123 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
126 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
127 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
130 int localRegistrationId
= node
.get("registrationId").asInt();
131 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
133 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
135 JsonNode trustedKeysNode
= node
.get("trustedKeys");
136 if (trustedKeysNode
.isArray()) {
137 for (JsonNode trustedKey
: trustedKeysNode
) {
138 String trustedKeyName
= trustedKey
.get("name").asText();
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
));
151 } catch (InvalidKeyException e
) {
152 throw new IOException(e
);
157 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
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();
175 json
.writeEndArray();
176 json
.writeEndObject();
180 public class Identity
{
182 IdentityKey identityKey
;
183 TrustLevel trustLevel
;
186 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
) {
187 this.identityKey
= identityKey
;
188 this.trustLevel
= trustLevel
;
189 this.added
= new Date();
192 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
193 this.identityKey
= identityKey
;
194 this.trustLevel
= trustLevel
;
198 public boolean isTrusted() {
199 return trustLevel
== TrustLevel
.TRUSTED_UNVERIFIED
||
200 trustLevel
== TrustLevel
.TRUSTED_VERIFIED
;
203 public IdentityKey
getIdentityKey() {
204 return this.identityKey
;
207 public TrustLevel
getTrustLevel() {
208 return this.trustLevel
;
211 public Date
getDateAdded() {
215 public byte[] getFingerprint() {
216 return identityKey
.getPublicKey().serialize();