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
;
26 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair
, int localRegistrationId
) {
27 this.identityKeyPair
= identityKeyPair
;
28 this.localRegistrationId
= localRegistrationId
;
32 public IdentityKeyPair
getIdentityKeyPair() {
33 return identityKeyPair
;
37 public int getLocalRegistrationId() {
38 return localRegistrationId
;
42 public boolean saveIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
43 return saveIdentity(address
.getName(), identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
, null);
47 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
49 * @param name User name, i.e. phone number
50 * @param identityKey The user's public key
52 * @param added Added timestamp, if null and the key is newly added, the current time is used.
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
);
60 for (Identity id
: identities
) {
61 if (!id
.identityKey
.equals(identityKey
))
64 if (id
.trustLevel
.compareTo(trustLevel
) < 0) {
65 id
.trustLevel
= trustLevel
;
73 identities
.add(new Identity(identityKey
, trustLevel
, added
!= null ? added
: new Date()));
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) {
86 for (Identity id
: identities
) {
87 if (id
.identityKey
.equals(identityKey
)) {
88 return id
.isTrusted();
95 public Map
<String
, List
<Identity
>> getIdentities() {
100 public List
<Identity
> getIdentities(String name
) {
102 return trustedKeys
.get(name
);
105 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
108 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
109 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
112 int localRegistrationId
= node
.get("registrationId").asInt();
113 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
116 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
118 JsonNode trustedKeysNode
= node
.get("trustedKeys");
119 if (trustedKeysNode
.isArray()) {
120 for (JsonNode trustedKey
: trustedKeysNode
) {
121 String trustedKeyName
= trustedKey
.get("name").asText();
123 IdentityKey id
= new IdentityKey(Base64
.decode(trustedKey
.get("identityKey").asText()), 0);
124 TrustLevel trustLevel
= trustedKey
.has("trustLevel") ? TrustLevel
.fromInt(trustedKey
.get("trustLevel").asInt()) : TrustLevel
.TRUSTED_UNVERIFIED
;
125 Date added
= trustedKey
.has("addedTimestamp") ?
new Date(trustedKey
.get("addedTimestamp").asLong()) : new Date();
126 keyStore
.saveIdentity(trustedKeyName
, id
, trustLevel
, added
);
127 } catch (InvalidKeyException
| IOException e
) {
128 System
.out
.println(String
.format("Error while decoding key for: %s", trustedKeyName
));
134 } catch (InvalidKeyException e
) {
135 throw new IOException(e
);
140 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
143 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
144 json
.writeStartObject();
145 json
.writeNumberField("registrationId", jsonIdentityKeyStore
.getLocalRegistrationId());
146 json
.writeStringField("identityKey", Base64
.encodeBytes(jsonIdentityKeyStore
.getIdentityKeyPair().serialize()));
147 json
.writeArrayFieldStart("trustedKeys");
148 for (Map
.Entry
<String
, List
<Identity
>> trustedKey
: jsonIdentityKeyStore
.trustedKeys
.entrySet()) {
149 for (Identity id
: trustedKey
.getValue()) {
150 json
.writeStartObject();
151 json
.writeStringField("name", trustedKey
.getKey());
152 json
.writeStringField("identityKey", Base64
.encodeBytes(id
.identityKey
.serialize()));
153 json
.writeNumberField("trustLevel", id
.trustLevel
.ordinal());
154 json
.writeNumberField("addedTimestamp", id
.added
.getTime());
155 json
.writeEndObject();
158 json
.writeEndArray();
159 json
.writeEndObject();
163 public class Identity
{
164 IdentityKey identityKey
;
165 TrustLevel trustLevel
;
168 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
) {
169 this.identityKey
= identityKey
;
170 this.trustLevel
= trustLevel
;
171 this.added
= new Date();
174 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
175 this.identityKey
= identityKey
;
176 this.trustLevel
= trustLevel
;
180 public boolean isTrusted() {
181 return trustLevel
== TrustLevel
.TRUSTED_UNVERIFIED
||
182 trustLevel
== TrustLevel
.TRUSTED_VERIFIED
;
185 public IdentityKey
getIdentityKey() {
186 return this.identityKey
;
189 public TrustLevel
getTrustLevel() {
190 return this.trustLevel
;
193 public Date
getDateAdded() {
197 public byte[] getFingerprint() {
198 return identityKey
.getPublicKey().serialize();