Ejemplo n.º 1
0
        public bool IsValidIdentity(long recipientId,
                               IdentityKey theirIdentity)
        {
            try
            {
                var query = conn.Table<Identity>().Where(v => v.RecipientId.Equals(recipientId));

                if (query.Count() > 0)
                {
                    var identity = query.First();
                    String serializedIdentity = identity.Key;
                    String mac = identity.Mac;


                    IdentityKey ourIdentity = new IdentityKey(Base64.decode(serializedIdentity), 0);

                    return ourIdentity.Equals(theirIdentity);
                }
                else
                {
                    return true;
                }
            }
            catch (IOException e) { return false; }
            catch (InvalidKeyException e) { return false; }
            catch (Exception e) { return false; }
            
        }
Ejemplo n.º 2
0
 public PreKeyState(List<PreKeyEntity> preKeys, PreKeyEntity lastResortKey,
                    SignedPreKeyEntity signedPreKey, IdentityKey identityKey)
 {
     this.preKeys = preKeys;
     this.lastResortKey = lastResortKey;
     this.signedPreKey = signedPreKey;
     this.identityKey = identityKey;
 }
Ejemplo n.º 3
0
        public IdentityKeyMismatch(long recipientId, IdentityKey identityKey)
        {
            this.RecipientId = recipientId;
            this.IdentityKey = identityKey;

            /*
                        var ecKey = identityKey.getPublicKey();
                        var key = ecKey as DjbECPublicKey;
                        if (key != null) Key = key.getPublicKey();*/

        }
Ejemplo n.º 4
0
 public IdentityKeyPair(byte[] serialized)
 {
     try
     {
         IdentityKeyPairStructure structure = IdentityKeyPairStructure.ParseFrom(serialized);
         this.publicKey  = new IdentityKey(structure.PublicKey.ToByteArray(), 0);
         this.privateKey = Curve.decodePrivatePoint(structure.PrivateKey.ToByteArray());
     }
     catch (InvalidProtocolBufferException e)
     {
         throw new InvalidKeyException(e);
     }
 }
Ejemplo n.º 5
0
 public IdentityKeyPair(byte[] serialized)
 {
     try
     {
         IdentityKeyPairStructure structure = IdentityKeyPairStructure.ParseFrom(serialized);
         this.publicKey = new IdentityKey(structure.PublicKey.ToByteArray(), 0);
         this.privateKey = Curve.decodePrivatePoint(structure.PrivateKey.ToByteArray());
     }
     catch (InvalidProtocolBufferException e)
     {
         throw new InvalidKeyException(e);
     }
 }
Ejemplo n.º 6
0
        /**
         * Build a new session from a received {@link org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage}.
         *
         * After a session is constructed in this way, the embedded {@link org.whispersystems.libaxolotl.protocol.WhisperMessage}
         * can be decrypted.
         *
         * @param message The received {@link org.whispersystems.libaxolotl.protocol.PreKeyWhisperMessage}.
         * @throws org.whispersystems.libaxolotl.InvalidKeyIdException when there is no local
         *                                                             {@link org.whispersystems.libaxolotl.state.PreKeyRecord}
         *                                                             that corresponds to the PreKey ID in
         *                                                             the message.
         * @throws org.whispersystems.libaxolotl.InvalidKeyException when the message is formatted incorrectly.
         * @throws org.whispersystems.libaxolotl.UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted.
         */
        /*package*/
        internal May <uint> process(SessionRecord sessionRecord, PreKeyWhisperMessage message)
        {
            uint        messageVersion   = message.getMessageVersion();
            IdentityKey theirIdentityKey = message.getIdentityKey();

            May <uint> unsignedPreKeyId;

            if (!identityKeyStore.IsTrustedIdentity(remoteAddress.getName(), theirIdentityKey))
            {
                throw new UntrustedIdentityException(remoteAddress.getName(), theirIdentityKey);
            }

            switch (messageVersion)
            {
            case 2: unsignedPreKeyId = processV2(sessionRecord, message); break;

            case 3: unsignedPreKeyId = processV3(sessionRecord, message); break;

            default: throw new Exception("Unknown version: " + messageVersion);
            }

            identityKeyStore.SaveIdentity(remoteAddress.getName(), theirIdentityKey);
            return(unsignedPreKeyId);
        }
Ejemplo n.º 7
0
 public IdentityKeyPair(IdentityKey publicKey, ECPrivateKey privateKey)
 {
     this.publicKey  = publicKey;
     this.privateKey = privateKey;
 }
Ejemplo n.º 8
0
 public bool IsTrustedIdentity(string name, IdentityKey identityKey)
 {
     return identityKeyStore.IsTrustedIdentity(name, identityKey);
 }
Ejemplo n.º 9
0
 public bool SaveIdentity(string name, IdentityKey identityKey)
 {
     identityKeyStore.SaveIdentity(name, identityKey);
     return true;
 }
 /**
  * Register an identity key, last resort key, signed prekey, and list of one time prekeys
  * with the server.
  *
  * @param identityKey The client's long-term identity keypair.
  * @param lastResortKey The client's "last resort" prekey.
  * @param signedPreKey The client's signed prekey.
  * @param oneTimePreKeys The client's list of one-time prekeys.
  *
  * @throws IOException
  */
 public async Task<bool> setPreKeys(IdentityKey identityKey, PreKeyRecord lastResortKey,
                            SignedPreKeyRecord signedPreKey, IList<PreKeyRecord> oneTimePreKeys)
 //throws IOException
 {
     await this.pushServiceSocket.registerPreKeys(identityKey, lastResortKey, signedPreKey, oneTimePreKeys);
     return true;
 }
Ejemplo n.º 11
0
        public static void generateIdentityKeys()
        {
            ECKeyPair djbKeyPair = Curve.generateKeyPair();

            //MasterCipher masterCipher = new MasterCipher(masterSecret);
            IdentityKey djbIdentityKey = new IdentityKey(djbKeyPair.getPublicKey());
            byte[] djbPrivateKey = /*masterCipher.encryptKey(*/djbKeyPair.getPrivateKey().serialize()/*)*/;

            save(IDENTITY_PUBLIC_KEY_DJB_PREF, System.Convert.ToBase64String(djbIdentityKey.serialize()));
            save(IDENTITY_PRIVATE_KEY_DJB_PREF, System.Convert.ToBase64String(djbPrivateKey));
        }
Ejemplo n.º 12
0
        public long SaveIdentity(long recipientId, IdentityKey identityKey)
        {
            String identityKeyString = Base64.encodeBytes(identityKey.serialize()); // TODO: real mac
            var identity = new Identity() { RecipientId = recipientId, Key = identityKeyString, Mac = Base64.encode(identityKeyString) };

            return conn.InsertOrReplace(identity);
        }
Ejemplo n.º 13
0
        public async Task<bool> registerPreKeys(IdentityKey identityKey,
                                    PreKeyRecord lastResortKey,
                                    SignedPreKeyRecord signedPreKey,
                                    IList<PreKeyRecord> records)
        //throws IOException
        {
            List<PreKeyEntity> entities = new List<PreKeyEntity>();

            foreach (PreKeyRecord record in records)
            {
                PreKeyEntity entity = new PreKeyEntity(record.getId(),
                                                       record.getKeyPair().getPublicKey());

                entities.Add(entity);
            }

            PreKeyEntity lastResortEntity = new PreKeyEntity(lastResortKey.getId(),
                                                     lastResortKey.getKeyPair().getPublicKey());

            SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(),
                                                                   signedPreKey.getKeyPair().getPublicKey(),
                                                                   signedPreKey.getSignature());

            await makeRequest(string.Format(PREKEY_PATH, ""), "PUT",
                JsonUtil.toJson(new PreKeyState(entities, lastResortEntity,
                                                signedPreKeyEntity, identityKey)));
            return true;
        }
        public void testRatchetingSessionAsAlice()
        {
            byte[] bobPublic =
            {
                0x05, 0x2c, 0xb4, 0x97, 0x76,
                0xb8, 0x77, 0x02, 0x05, 0x74,
                0x5a, 0x3a, 0x6e, 0x24, 0xf5,
                0x79, 0xcd, 0xb4, 0xba, 0x7a,
                0x89, 0x04, 0x10, 0x05, 0x92,
                0x8e, 0xbb, 0xad, 0xc9, 0xc0,
                0x5a, 0xd4, 0x58
            };

            byte[] bobIdentityPublic =
            {
                0x05, 0xf1, 0xf4, 0x38, 0x74,
                0xf6, 0x96, 0x69, 0x56, 0xc2,
                0xdd, 0x47, 0x3f, 0x8f, 0xa1,
                0x5a, 0xde, 0xb7, 0x1d, 0x1c,
                0xb9, 0x91, 0xb2, 0x34, 0x16,
                0x92, 0x32, 0x4c, 0xef, 0xb1,
                0xc5, 0xe6, 0x26
            };

            byte[] aliceBasePublic =
            {
                0x05, 0x47, 0x2d, 0x1f, 0xb1,
                0xa9, 0x86, 0x2c, 0x3a, 0xf6,
                0xbe, 0xac, 0xa8, 0x92, 0x02,
                0x77, 0xe2, 0xb2, 0x6f, 0x4a,
                0x79, 0x21, 0x3e, 0xc7, 0xc9,
                0x06, 0xae, 0xb3, 0x5e, 0x03,
                0xcf, 0x89, 0x50
            };

            byte[] aliceBasePrivate =
            {
                0x11, 0xae, 0x7c, 0x64, 0xd1,
                0xe6, 0x1c, 0xd5, 0x96, 0xb7,
                0x6a, 0x0d, 0xb5, 0x01, 0x26,
                0x73, 0x39, 0x1c, 0xae, 0x66,
                0xed, 0xbf, 0xcf, 0x07, 0x3b,
                0x4d, 0xa8, 0x05, 0x16, 0xa4,
                0x74, 0x49
            };

            byte[] aliceEphemeralPublic =
            {
                0x05, 0x6c, 0x3e, 0x0d, 0x1f,
                0x52, 0x02, 0x83, 0xef, 0xcc,
                0x55, 0xfc, 0xa5, 0xe6, 0x70,
                0x75, 0xb9, 0x04, 0x00, 0x7f,
                0x18, 0x81, 0xd1, 0x51, 0xaf,
                0x76, 0xdf, 0x18, 0xc5, 0x1d,
                0x29, 0xd3, 0x4b
            };

            byte[] aliceEphemeralPrivate =
            {
                0xd1, 0xba, 0x38, 0xce, 0xa9,
                0x17, 0x43, 0xd3, 0x39, 0x39,
                0xc3, 0x3c, 0x84, 0x98, 0x65,
                0x09, 0x28, 0x01, 0x61, 0xb8,
                0xb6, 0x0f, 0xc7, 0x87, 0x0c,
                0x59, 0x9c, 0x1d, 0x46, 0x20,
                0x12, 0x48
            };

            byte[] aliceIdentityPublic =
            {
                0x05, 0xb4, 0xa8, 0x45, 0x56,
                0x60, 0xad, 0xa6, 0x5b, 0x40,
                0x10, 0x07, 0xf6, 0x15, 0xe6,
                0x54, 0x04, 0x17, 0x46, 0x43,
                0x2e, 0x33, 0x39, 0xc6, 0x87,
                0x51, 0x49, 0xbc, 0xee, 0xfc,
                0xb4, 0x2b, 0x4a
            };

            byte[] aliceIdentityPrivate =
            {
                0x90, 0x40, 0xf0, 0xd4, 0xe0,
                0x9c, 0xf3, 0x8f, 0x6d, 0xc7,
                0xc1, 0x37, 0x79, 0xc9, 0x08,
                0xc0, 0x15, 0xa1, 0xda, 0x4f,
                0xa7, 0x87, 0x37, 0xa0, 0x80,
                0xeb, 0x0a, 0x6f, 0x4f, 0x5f,
                0x8f, 0x58
            };

            byte[] receiverChain =
            {
                0xd2, 0x2f, 0xd5, 0x6d, 0x3f,
                0xec, 0x81, 0x9c, 0xf4, 0xc3,
                0xd5, 0x0c, 0x56, 0xed, 0xfb,
                0x1c, 0x28, 0x0a, 0x1b, 0x31,
                0x96, 0x45, 0x37, 0xf1, 0xd1,
                0x61, 0xe1, 0xc9, 0x31, 0x48,
                0xe3, 0x6b
            };

            IdentityKey bobIdentityKey = new IdentityKey(bobIdentityPublic, 0);
            ECPublicKey bobEphemeralPublicKey = Curve.decodePoint(bobPublic, 0);
            ECPublicKey bobBasePublicKey = bobEphemeralPublicKey;
            ECPublicKey aliceBasePublicKey = Curve.decodePoint(aliceBasePublic, 0);
            ECPrivateKey aliceBasePrivateKey = Curve.decodePrivatePoint(aliceBasePrivate);
            ECKeyPair aliceBaseKey = new ECKeyPair(aliceBasePublicKey, aliceBasePrivateKey);
            ECPublicKey aliceEphemeralPublicKey = Curve.decodePoint(aliceEphemeralPublic, 0);
            ECPrivateKey aliceEphemeralPrivateKey = Curve.decodePrivatePoint(aliceEphemeralPrivate);
            ECKeyPair aliceEphemeralKey = new ECKeyPair(aliceEphemeralPublicKey, aliceEphemeralPrivateKey);
            IdentityKey aliceIdentityPublicKey = new IdentityKey(aliceIdentityPublic, 0);
            ECPrivateKey aliceIdentityPrivateKey = Curve.decodePrivatePoint(aliceIdentityPrivate);
            IdentityKeyPair aliceIdentityKey = new IdentityKeyPair(aliceIdentityPublicKey, aliceIdentityPrivateKey);

            SessionState session = new SessionState();

            AliceAxolotlParameters parameters = AliceAxolotlParameters.newBuilder()
                .setOurBaseKey(aliceBaseKey)
                .setOurIdentityKey(aliceIdentityKey)
                .setTheirIdentityKey(bobIdentityKey)
                .setTheirSignedPreKey(bobBasePublicKey)
                .setTheirRatchetKey(bobEphemeralPublicKey)
                .setTheirOneTimePreKey(May<ECPublicKey>.NoValue)
                .create();

            RatchetingSession.initializeSession(session, 2, parameters);

            Assert.AreEqual(session.getLocalIdentityKey(), aliceIdentityKey.getPublicKey());
            Assert.AreEqual(session.getRemoteIdentityKey(), bobIdentityKey);
            CollectionAssert.AreEqual(session.getReceiverChainKey(bobEphemeralPublicKey).getKey(), receiverChain);
        }
 public UntrustedIdentityException(string s, string e164Number, IdentityKey identityKey) : base(s)
 {
     this.E164Number = e164Number;
     this.IdentityKey = identityKey;
 }
Ejemplo n.º 16
-1
 public IdentityKeyPair(IdentityKey publicKey, ECPrivateKey privateKey)
 {
     this.publicKey = publicKey;
     this.privateKey = privateKey;
 }