1 package org
.asamk
.signal
;
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
.whispersystems
.libsignal
.IdentityKey
;
8 import org
.whispersystems
.libsignal
.IdentityKeyPair
;
9 import org
.whispersystems
.libsignal
.InvalidKeyException
;
10 import org
.whispersystems
.libsignal
.SignalProtocolAddress
;
11 import org
.whispersystems
.libsignal
.state
.IdentityKeyStore
;
13 import java
.io
.IOException
;
16 class JsonIdentityKeyStore
implements IdentityKeyStore
{
18 private final Map
<String
, List
<Identity
>> trustedKeys
= new HashMap
<>();
20 private final IdentityKeyPair identityKeyPair
;
21 private final int localRegistrationId
;
24 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair
, int localRegistrationId
) {
25 this.identityKeyPair
= identityKeyPair
;
26 this.localRegistrationId
= localRegistrationId
;
30 public IdentityKeyPair
getIdentityKeyPair() {
31 return identityKeyPair
;
35 public int getLocalRegistrationId() {
36 return localRegistrationId
;
40 public void saveIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
41 saveIdentity(address
.getName(), identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
, null);
45 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
47 * @param name User name, i.e. phone number
48 * @param identityKey The user's public key
50 * @param added Added timestamp, if null and the key is newly added, the current time is used.
52 public void saveIdentity(String name
, IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
53 List
<Identity
> identities
= trustedKeys
.get(name
);
54 if (identities
== null) {
55 identities
= new ArrayList
<>();
56 trustedKeys
.put(name
, identities
);
58 for (Identity id
: identities
) {
59 if (!id
.identityKey
.equals(identityKey
))
62 if (id
.trustLevel
.compareTo(trustLevel
) < 0) {
63 id
.trustLevel
= trustLevel
;
71 identities
.add(new Identity(identityKey
, trustLevel
, added
!= null ? added
: new Date()));
75 public boolean isTrustedIdentity(SignalProtocolAddress address
, IdentityKey identityKey
) {
76 List
<Identity
> identities
= trustedKeys
.get(address
.getName());
77 if (identities
== null) {
82 for (Identity id
: identities
) {
83 if (id
.identityKey
.equals(identityKey
)) {
84 return id
.isTrusted();
91 public Map
<String
, List
<Identity
>> getIdentities() {
96 public List
<Identity
> getIdentities(String name
) {
98 return trustedKeys
.get(name
);
101 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
104 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
105 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
108 int localRegistrationId
= node
.get("registrationId").asInt();
109 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
112 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
114 JsonNode trustedKeysNode
= node
.get("trustedKeys");
115 if (trustedKeysNode
.isArray()) {
116 for (JsonNode trustedKey
: trustedKeysNode
) {
117 String trustedKeyName
= trustedKey
.get("name").asText();
119 IdentityKey id
= new IdentityKey(Base64
.decode(trustedKey
.get("identityKey").asText()), 0);
120 TrustLevel trustLevel
= trustedKey
.has("trustLevel") ? TrustLevel
.fromInt(trustedKey
.get("trustLevel").asInt()) : TrustLevel
.TRUSTED_UNVERIFIED
;
121 Date added
= trustedKey
.has("addedTimestamp") ?
new Date(trustedKey
.get("addedTimestamp").asLong()) : new Date();
122 keyStore
.saveIdentity(trustedKeyName
, id
, trustLevel
, added
);
123 } catch (InvalidKeyException
| IOException e
) {
124 System
.out
.println(String
.format("Error while decoding key for: %s", trustedKeyName
));
130 } catch (InvalidKeyException e
) {
131 throw new IOException(e
);
136 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
139 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
140 json
.writeStartObject();
141 json
.writeNumberField("registrationId", jsonIdentityKeyStore
.getLocalRegistrationId());
142 json
.writeStringField("identityKey", Base64
.encodeBytes(jsonIdentityKeyStore
.getIdentityKeyPair().serialize()));
143 json
.writeArrayFieldStart("trustedKeys");
144 for (Map
.Entry
<String
, List
<Identity
>> trustedKey
: jsonIdentityKeyStore
.trustedKeys
.entrySet()) {
145 for (Identity id
: trustedKey
.getValue()) {
146 json
.writeStartObject();
147 json
.writeStringField("name", trustedKey
.getKey());
148 json
.writeStringField("identityKey", Base64
.encodeBytes(id
.identityKey
.serialize()));
149 json
.writeNumberField("trustLevel", id
.trustLevel
.ordinal());
150 json
.writeNumberField("addedTimestamp", id
.added
.getTime());
151 json
.writeEndObject();
154 json
.writeEndArray();
155 json
.writeEndObject();
159 public class Identity
{
160 IdentityKey identityKey
;
161 TrustLevel trustLevel
;
164 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
) {
165 this.identityKey
= identityKey
;
166 this.trustLevel
= trustLevel
;
167 this.added
= new Date();
170 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
171 this.identityKey
= identityKey
;
172 this.trustLevel
= trustLevel
;
176 public boolean isTrusted() {
177 return trustLevel
== TrustLevel
.TRUSTED_UNVERIFIED
||
178 trustLevel
== TrustLevel
.TRUSTED_VERIFIED
;
181 public byte[] getFingerprint() {
182 return identityKey
.getPublicKey().serialize();