void send_pico_auth_message(Channel channel, EncKeys keys, Nonce serviceNonce, KeyPair picoIdentityKey, KeyPair picoEphemeralKey, string extra_data_to_send) { Json json = new Json(); PicoBuffer buf = new PicoBuffer(0); PicoBuffer toEncrypt = new PicoBuffer(0); picoIdentityKey.getpublicder(buf); toEncrypt.append_lengthprepend(buf); PicoBuffer toSign = new PicoBuffer(0); toSign.append(serviceNonce.get_buffer(), serviceNonce.get_length()); toSign.append(new byte[] { 0x00, 0x00, 0x00, 0x00 }); buf.clear(); picoEphemeralKey.getpublicder(buf); toSign.append(buf); buf.clear(); picoIdentityKey.sign_data(toSign, buf); toEncrypt.append_lengthprepend(buf); PicoBuffer mac = new PicoBuffer(0); buf.clear(); picoIdentityKey.getpublicder(buf); CryptoSupport.generate_mac(keys.pMacKey, buf, mac); toEncrypt.append_lengthprepend(mac); PicoBuffer extraData = new PicoBuffer(0); extraData.append(extra_data_to_send); toEncrypt.append_lengthprepend(extraData); PicoBuffer iv = new PicoBuffer(16); CryptoSupport.generate_iv(iv); PicoBuffer encrypted = new PicoBuffer(0); CryptoSupport.encrypt(keys.pEncKey, iv, toEncrypt, encrypted); buf.clear(); Base64.encode(encrypted, buf); json.add("encryptedData", buf); buf.clear(); Base64.encode(iv, buf); json.add("iv", buf); json.add("sessionId", 0); buf.clear(); json.serialize(buf); channel.write_buffer(buf); json.delete(); buf.delete(); toEncrypt.delete(); toSign.delete(); mac.delete(); extraData.delete(); iv.delete(); encrypted.delete(); }
void echo_main(string channel_name) { Channel channel = Channel.connect(channel_name); PicoBuffer buf = new PicoBuffer(0); PicoBuffer toSend = new PicoBuffer(0); channel.read(buf); toSend.append_lengthprepend(buf); byte[] bytesToSend = new byte[toSend.get_pos() + 1]; toSend.copy_to_array(bytesToSend); channel.write(bytesToSend, bytesToSend.Length); buf.delete(); toSend.delete(); channel.delete(); }
public void length_prepend() { PicoBuffer b; PicoBuffer b2; b = new PicoBuffer(3); b2 = new PicoBuffer(3); b2.append("67890"); Assert.AreEqual(b2.get_pos(), 5); Assert.AreEqual(b2.get_size() % 3, 0); b.append_lengthprepend(b2); byte[] expected = new byte[] { 0x00, 0x00, 0x00, 0x05, Convert.ToByte('6'), Convert.ToByte('7'), Convert.ToByte('8'), Convert.ToByte('9'), Convert.ToByte('0'), 0x00 }; byte[] ret = new byte[10]; b.copy_to_array(ret, 10); CollectionAssert.AreEqual(ret, expected); b.delete(); b2.delete(); }