1 package org
.asamk
.textsecure
;
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
.libaxolotl
.IdentityKey
;
8 import org
.whispersystems
.libaxolotl
.IdentityKeyPair
;
9 import org
.whispersystems
.libaxolotl
.InvalidKeyException
;
10 import org
.whispersystems
.libaxolotl
.state
.IdentityKeyStore
;
12 import java
.io
.IOException
;
13 import java
.util
.HashMap
;
16 class JsonIdentityKeyStore
implements IdentityKeyStore
{
18 private final Map
<String
, IdentityKey
> 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
;
29 public void addTrustedKeys(Map
<String
, IdentityKey
> keyMap
) {
30 trustedKeys
.putAll(keyMap
);
34 public IdentityKeyPair
getIdentityKeyPair() {
35 return identityKeyPair
;
39 public int getLocalRegistrationId() {
40 return localRegistrationId
;
44 public void saveIdentity(String name
, IdentityKey identityKey
) {
45 trustedKeys
.put(name
, identityKey
);
49 public boolean isTrustedIdentity(String name
, IdentityKey identityKey
) {
50 IdentityKey trusted
= trustedKeys
.get(name
);
51 return (trusted
== null || trusted
.equals(identityKey
));
54 public static class JsonIdentityKeyStoreDeserializer
extends JsonDeserializer
<JsonIdentityKeyStore
> {
57 public JsonIdentityKeyStore
deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext
) throws IOException
, JsonProcessingException
{
58 JsonNode node
= jsonParser
.getCodec().readTree(jsonParser
);
61 int localRegistrationId
= node
.get("registrationId").asInt();
62 IdentityKeyPair identityKeyPair
= new IdentityKeyPair(Base64
.decode(node
.get("identityKey").asText()));
65 Map
<String
, IdentityKey
> trustedKeyMap
= new HashMap
<>();
66 JsonNode trustedKeysNode
= node
.get("trustedKeys");
67 if (trustedKeysNode
.isArray()) {
68 for (JsonNode trustedKey
: trustedKeysNode
) {
69 String trustedKeyName
= trustedKey
.get("name").asText();
71 trustedKeyMap
.put(trustedKeyName
, new IdentityKey(Base64
.decode(trustedKey
.get("identityKey").asText()), 0));
72 } catch (InvalidKeyException
| IOException e
) {
73 System
.out
.println(String
.format("Error while decoding key for: %s", trustedKeyName
));
78 JsonIdentityKeyStore keyStore
= new JsonIdentityKeyStore(identityKeyPair
, localRegistrationId
);
79 keyStore
.addTrustedKeys(trustedKeyMap
);
83 } catch (InvalidKeyException e
) {
84 throw new IOException(e
);
89 public static class JsonIdentityKeyStoreSerializer
extends JsonSerializer
<JsonIdentityKeyStore
> {
92 public void serialize(JsonIdentityKeyStore jsonIdentityKeyStore
, JsonGenerator json
, SerializerProvider serializerProvider
) throws IOException
, JsonProcessingException
{
93 json
.writeStartObject();
94 json
.writeNumberField("registrationId", jsonIdentityKeyStore
.getLocalRegistrationId());
95 json
.writeStringField("identityKey", Base64
.encodeBytes(jsonIdentityKeyStore
.getIdentityKeyPair().serialize()));
96 json
.writeArrayFieldStart("trustedKeys");
97 for (Map
.Entry
<String
, IdentityKey
> trustedKey
: jsonIdentityKeyStore
.trustedKeys
.entrySet()) {
98 json
.writeStartObject();
99 json
.writeStringField("name", trustedKey
.getKey());
100 json
.writeStringField("identityKey", Base64
.encodeBytes(trustedKey
.getValue().serialize()));
101 json
.writeEndObject();
103 json
.writeEndArray();
104 json
.writeEndObject();