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
.state
.IdentityKeyStore
;
12 import java
.io
.IOException
;
15 class JsonIdentityKeyStore
implements IdentityKeyStore
{
17 private final Map
<String
, List
<Identity
>> trustedKeys
= new HashMap
<>();
19 private final IdentityKeyPair identityKeyPair
;
20 private final int localRegistrationId
;
23 public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair
, int localRegistrationId
) {
24 this.identityKeyPair
= identityKeyPair
;
25 this.localRegistrationId
= localRegistrationId
;
29 public IdentityKeyPair
getIdentityKeyPair() {
30 return identityKeyPair
;
34 public int getLocalRegistrationId() {
35 return localRegistrationId
;
39 public void saveIdentity(String name
, IdentityKey identityKey
) {
40 saveIdentity(name
, identityKey
, TrustLevel
.TRUSTED_UNVERIFIED
, null);
44 * Adds or updates the given identityKey for the user name and sets the trustLevel and added timestamp.
46 * @param name User name, i.e. phone number
47 * @param identityKey The user's public key
49 * @param added Added timestamp, if null and the key is newly added, the current time is used.
51 public void saveIdentity(String name
, IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
52 List
<Identity
> identities
= trustedKeys
.get(name
);
53 if (identities
== null) {
54 identities
= new ArrayList
<>();
55 trustedKeys
.put(name
, identities
);
57 for (Identity id
: identities
) {
58 if (!id
.identityKey
.equals(identityKey
))
61 id
.trustLevel
= trustLevel
;
68 identities
.add(new Identity(identityKey
, trustLevel
, added
!= null ? added
: new Date()));
72 public boolean isTrustedIdentity(String name
, IdentityKey identityKey
) {
73 List
<Identity
> identities
= trustedKeys
.get(name
);
74 if (identities
== null) {
79 for (Identity id
: identities
) {
80 if (id
.identityKey
.equals(identityKey
)) {
81 return id
.isTrusted();
88 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
91 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
92 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
95 int localRegistrationId
= node
.get("registrationId").asInt();
96 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
99 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
101 JsonNode trustedKeysNode
= node
.get("trustedKeys");
102 if (trustedKeysNode
.isArray()) {
103 for (JsonNode trustedKey
: trustedKeysNode
) {
104 String trustedKeyName
= trustedKey
.get("name").asText();
106 IdentityKey id
= new IdentityKey(Base64
.decode(trustedKey
.get("identityKey").asText()), 0);
107 TrustLevel trustLevel
= trustedKey
.has("trustLevel") ? TrustLevel
.fromInt(trustedKey
.get("trustLevel").asInt()) : TrustLevel
.TRUSTED_UNVERIFIED
;
108 Date added
= trustedKey
.has("addedTimestamp") ?
new Date(trustedKey
.get("addedTimestamp").asLong()) : new Date();
109 keyStore
.saveIdentity(trustedKeyName
, id
, trustLevel
, added
);
110 } catch (InvalidKeyException
| IOException e
) {
111 System
.out
.println(String
.format("Error while decoding key for: %s", trustedKeyName
));
117 } catch (InvalidKeyException e
) {
118 throw new IOException(e
);
123 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
126 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
127 json
.writeStartObject();
128 json
.writeNumberField("registrationId", jsonIdentityKeyStore
.getLocalRegistrationId());
129 json
.writeStringField("identityKey", Base64
.encodeBytes(jsonIdentityKeyStore
.getIdentityKeyPair().serialize()));
130 json
.writeArrayFieldStart("trustedKeys");
131 for (Map
.Entry
<String
, List
<Identity
>> trustedKey
: jsonIdentityKeyStore
.trustedKeys
.entrySet()) {
132 for (Identity id
: trustedKey
.getValue()) {
133 json
.writeStartObject();
134 json
.writeStringField("name", trustedKey
.getKey());
135 json
.writeStringField("identityKey", Base64
.encodeBytes(id
.identityKey
.serialize()));
136 json
.writeNumberField("trustLevel", id
.trustLevel
.ordinal());
137 json
.writeNumberField("addedTimestamp", id
.added
.getTime());
138 json
.writeEndObject();
141 json
.writeEndArray();
142 json
.writeEndObject();
146 public class Identity
{
147 IdentityKey identityKey
;
148 TrustLevel trustLevel
;
151 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
) {
152 this.identityKey
= identityKey
;
153 this.trustLevel
= trustLevel
;
154 this.added
= new Date();
157 public Identity(IdentityKey identityKey
, TrustLevel trustLevel
, Date added
) {
158 this.identityKey
= identityKey
;
159 this.trustLevel
= trustLevel
;
163 public boolean isTrusted() {
164 return trustLevel
== TrustLevel
.TRUSTED_UNVERIFIED
||
165 trustLevel
== TrustLevel
.TRUSTED_VERIFIED
;
168 public String
getFingerprint() {
169 return Hex
.toStringCondensed(identityKey
.getPublicKey().serialize());