Exemplo n.º 1
0
        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();
        }