/// <summary> /// Send a request to get cipher keys from an user /// </summary> /// <param name="number">Phone number of the user you want to get the cipher keys</param> /// <param name="replaceKey"></param> public void sendGetCipherKeysFromUser(string number, bool replaceKeyIn = false) { replaceKey = replaceKeyIn; var msgId = TicketManager.GenerateId(); cipherKeys.Add(msgId); ProtocolTreeNode user = new ProtocolTreeNode("user", new[] { new KeyValue("jid", ApiBase.GetJID(number)), }, null, null); ProtocolTreeNode keyNode = new ProtocolTreeNode("key", null, new ProtocolTreeNode[] { user }, null); ProtocolTreeNode Node = new ProtocolTreeNode("iq", new[] { new KeyValue("id", msgId), new KeyValue("xmlns", "encrypt"), new KeyValue("type", "get"), new KeyValue("to", "s.whatsapp.net") }, new ProtocolTreeNode[] { keyNode }, null); this.SendNode(Node); }
/// <summary> /// Generate the keysets for ourself /// </summary> /// <returns></returns> public bool sendSetPreKeys(bool isnew = false) { uint registrationId = 0; if (!isnew) { registrationId = (uint)this.GetLocalRegistrationId(); } else { registrationId = libaxolotl.util.KeyHelper.generateRegistrationId(true); } Random random = new Random(); uint randomid = (uint)libaxolotl.util.KeyHelper.getRandomSequence(5000);//65536 IdentityKeyPair identityKeyPair = libaxolotl.util.KeyHelper.generateIdentityKeyPair(); byte[] privateKey = identityKeyPair.getPrivateKey().serialize(); byte[] publicKey = identityKeyPair.getPublicKey().getPublicKey().serialize(); IList <PreKeyRecord> preKeys = libaxolotl.util.KeyHelper.generatePreKeys((uint)random.Next(), 200); SignedPreKeyRecord signedPreKey = libaxolotl.util.KeyHelper.generateSignedPreKey(identityKeyPair, randomid); PreKeyRecord lastResortKey = libaxolotl.util.KeyHelper.generateLastResortPreKey(); this.StorePreKeys(preKeys); this.StoreLocalData(registrationId, identityKeyPair.getPublicKey().serialize(), identityKeyPair.getPrivateKey().serialize()); this.StoreSignedPreKey(signedPreKey.getId(), signedPreKey); // FOR INTERNAL TESTING ONLY //this.InMemoryTestSetup(identityKeyPair, registrationId); ProtocolTreeNode[] preKeyNodes = new ProtocolTreeNode[200]; for (int i = 0; i < 200; i++) { byte[] prekeyId = adjustId(preKeys[i].getId().ToString()).Skip(1).ToArray(); byte[] prekey = preKeys[i].getKeyPair().getPublicKey().serialize().Skip(1).ToArray(); ProtocolTreeNode NodeId = new ProtocolTreeNode("id", null, null, prekeyId); ProtocolTreeNode NodeValue = new ProtocolTreeNode("value", null, null, prekey); preKeyNodes[i] = new ProtocolTreeNode("key", null, new ProtocolTreeNode[] { NodeId, NodeValue }, null); } ProtocolTreeNode registration = new ProtocolTreeNode("registration", null, null, adjustId(registrationId.ToString())); ProtocolTreeNode type = new ProtocolTreeNode("type", null, null, new byte[] { Curve.DJB_TYPE }); ProtocolTreeNode list = new ProtocolTreeNode("list", null, preKeyNodes, null); ProtocolTreeNode sid = new ProtocolTreeNode("id", null, null, adjustId(signedPreKey.getId().ToString(), true)); ProtocolTreeNode identity = new ProtocolTreeNode("identity", null, null, identityKeyPair.getPublicKey().getPublicKey().serialize().Skip(1).ToArray()); ProtocolTreeNode value = new ProtocolTreeNode("value", null, null, signedPreKey.getKeyPair().getPublicKey().serialize().Skip(1).ToArray()); ProtocolTreeNode signature = new ProtocolTreeNode("signature", null, null, signedPreKey.getSignature()); ProtocolTreeNode secretKey = new ProtocolTreeNode("skey", null, new ProtocolTreeNode[] { sid, value, signature }, null); String id = TicketManager.GenerateId(); Helper.DebugAdapter.Instance.fireOnPrintDebug(string.Format("axolotl id = {0}", id)); ProtocolTreeNode Node = new ProtocolTreeNode("iq", new KeyValue[] { new KeyValue("id", id), new KeyValue("to", "s.whatsapp.net"), new KeyValue("type", "set"), new KeyValue("xmlns", "encrypt") }, new ProtocolTreeNode[] { identity, registration, type, list, secretKey }, null); this.SendNode(Node); return(true); }