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
.*;
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
;
16 import java
.io
.IOException
;
19 public class JsonIdentityKeyStore
implements IdentityKeyStore
{
21 private final Map
<String
, List
<Identity
>> trustedKeys
= new HashMap
<>();
23 private final IdentityKeyPair identityKeyPair
;
24 private final int localRegistrationId
;
27 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair
, int localRegistrationId
) {
28 this.identityKeyPair
= identityKeyPair
;
29 this.localRegistrationId
= localRegistrationId
;
33 public IdentityKeyPair
getIdentityKeyPair() {
34 return identityKeyPair
;
38 public int getLocalRegistrationId() {
39 return localRegistrationId
;
43 public void saveIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
44 saveIdentity(address
.getName(), identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
, null);
48 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
50 * @param name User name, i.e. phone number
51 * @param identityKey The user's public key
53 * @param added Added timestamp, if null and the key is newly added, the current time is used.
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
);
61 for (Identity id
: identities
) {
62 if (!id
.identityKey
.equals(identityKey
))
65 if (id
.trustLevel
.compareTo(trustLevel
) < 0) {
66 id
.trustLevel
= trustLevel
;
74 identities
.add(new Identity(identityKey
, trustLevel
, added
!= null ? added
: new Date()));
78 public boolean isTrustedIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
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();
94 public Map
<String
, List
<Identity
>> getIdentities() {
99 public List
<Identity
> getIdentities(String name
) {
101 return trustedKeys
.get(name
);
104 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
107 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
108 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
111 int localRegistrationId
= node
.get("registrationId").asInt();
112 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
115 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
117 JsonNode trustedKeysNode
= node
.get("trustedKeys");
118 if (trustedKeysNode
.isArray()) {
119 for (JsonNode trustedKey
: trustedKeysNode
) {
120 String trustedKeyName
= trustedKey
.get("name").asText();
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
));
133 } catch (InvalidKeyException e
) {
134 throw new IOException(e
);
139 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
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();
157 json
.writeEndArray();
158 json
.writeEndObject();
162 public class Identity
{
163 IdentityKey identityKey
;
164 TrustLevel trustLevel
;
167 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
) {
168 this.identityKey
= identityKey
;
169 this.trustLevel
= trustLevel
;
170 this.added
= new Date();
173 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
174 this.identityKey
= identityKey
;
175 this.trustLevel
= trustLevel
;
179 public boolean isTrusted() {
180 return trustLevel
== TrustLevel
.TRUSTED_UNVERIFIED
||
181 trustLevel
== TrustLevel
.TRUSTED_VERIFIED
;
184 public IdentityKey
getIdentityKey() {
185 return this.identityKey
;
188 public TrustLevel
getTrustLevel() {
189 return this.trustLevel
;
192 public Date
getDateAdded() {
196 public byte[] getFingerprint() {
197 return identityKey
.getPublicKey().serialize();