Beispiel #1
0
        private void TransmitWriter(SSH2DataWriter wr)
        {
            int o = wr.Length;

            wr.SetOffset(0);
            wr.WriteInt32(o - 4); //length of int32
            _channel.Transmit(wr.UnderlyingBuffer, 0, o);
        }
Beispiel #2
0
        //writer util
        private SSH2DataWriter OpenWriter(AgentForwadPacketType pt)
        {
            SSH2DataWriter wr = new SSH2DataWriter();

            wr.WriteInt32(0); //length field
            wr.WriteByte((byte)pt);
            return(wr);
        }
Beispiel #3
0
        /// <summary>
        /// Open packet with specifying a packet type.
        /// </summary>
        /// <param name="packetType">SFTP packet type.</param>
        /// <param name="remoteChannel">remote channel number</param>
        public void Open(SFTPPacketType packetType, int remoteChannel)
        {
            base.Open();
            SSH2DataWriter writer = DataWriter;

            writer.WritePacketType(Granados.SSH2.PacketType.SSH_MSG_CHANNEL_DATA);
            writer.WriteInt32(remoteChannel);
            writer.SetOffset(OFFSET_SFTP_PACKET_TYPE);
            writer.WriteByte((byte)packetType);
        }
Beispiel #4
0
        // Derived class can override this method to modify the buffer.
        public virtual DataFragment Close(Cipher cipher, Random rnd, MAC mac, int sequence)
        {
            if (!_isOpen)
            {
                throw new SSHException("internal state error");
            }

            int blocksize     = cipher == null ? 8 : cipher.BlockSize;
            int payloadLength = _writer.Length - (SEQUENCE_MARGIN + LENGTH_MARGIN + PADDING_MARGIN);
            int paddingLength = 11 - payloadLength % blocksize;

            while (paddingLength < 4)
            {
                paddingLength += blocksize;
            }
            int packetLength = PADDING_MARGIN + payloadLength + paddingLength;
            int imageLength  = packetLength + LENGTH_MARGIN;

            //fill padding
            for (int i = 0; i < paddingLength; i += 4)
            {
                _writer.WriteInt32(rnd.Next());
            }

            //manipulate stream
            byte[] rawbuf = _writer.UnderlyingBuffer;
            SSHUtil.WriteIntToByteArray(rawbuf, 0, sequence);
            SSHUtil.WriteIntToByteArray(rawbuf, SEQUENCE_MARGIN, packetLength);
            rawbuf[SEQUENCE_MARGIN + LENGTH_MARGIN] = (byte)paddingLength;

            //mac
            if (mac != null)
            {
                byte[] macCode = mac.ComputeHash(rawbuf, 0, packetLength + LENGTH_MARGIN + SEQUENCE_MARGIN);
                Array.Copy(macCode, 0, rawbuf, packetLength + LENGTH_MARGIN + SEQUENCE_MARGIN, macCode.Length);
                imageLength += macCode.Length;
            }

            //encrypt
            if (cipher != null)
            {
                cipher.Encrypt(rawbuf, SEQUENCE_MARGIN, packetLength + LENGTH_MARGIN, rawbuf, SEQUENCE_MARGIN);
            }

            _dataFragment.Init(rawbuf, SEQUENCE_MARGIN, imageLength);
            _isOpen = false;
            return(_dataFragment);
        }
Beispiel #5
0
        private void SendKeyList()
        {
            SSH2DataWriter wr = OpenWriter(AgentForwadPacketType.SSH2_AGENT_IDENTITIES_ANSWER);

            // keycount, ((blob-len, pubkey-blob, comment-len, comment) * keycount)
            SSH2UserAuthKey[] keys = _client.GetAvailableSSH2UserAuthKeys();
            wr.WriteInt32(keys.Length);
            foreach (SSH2UserAuthKey key in keys)
            {
                byte[] blob = key.GetPublicKeyBlob();
                wr.WriteAsString(blob);
                Debug.WriteLine("Userkey comment=" + key.Comment);
                wr.WriteAsString(Encoding.UTF8.GetBytes(key.Comment));
            }
            TransmitWriter(wr);
        }
Beispiel #6
0
        public void WritePrivatePartInSECSHStyleFile(Stream dest, string comment, string passphrase)
        {
            //step1 key body
            SSH2DataWriter wr = new SSH2DataWriter();

            wr.WriteInt32(0); //this field is filled later
            if (_keypair.Algorithm == PublicKeyAlgorithm.RSA)
            {
                RSAKeyPair   rsa = (RSAKeyPair)_keypair;
                RSAPublicKey pub = (RSAPublicKey)_keypair.PublicKey;
                wr.WriteBigIntWithBits(pub.Exponent);
                wr.WriteBigIntWithBits(rsa.D);
                wr.WriteBigIntWithBits(pub.Modulus);
                wr.WriteBigIntWithBits(rsa.U);
                wr.WriteBigIntWithBits(rsa.P);
                wr.WriteBigIntWithBits(rsa.Q);
            }
            else
            {
                DSAKeyPair   dsa = (DSAKeyPair)_keypair;
                DSAPublicKey pub = (DSAPublicKey)_keypair.PublicKey;
                wr.WriteInt32(0);
                wr.WriteBigIntWithBits(pub.P);
                wr.WriteBigIntWithBits(pub.G);
                wr.WriteBigIntWithBits(pub.Q);
                wr.WriteBigIntWithBits(pub.Y);
                wr.WriteBigIntWithBits(dsa.X);
            }

            int padding_len = 0;

            if (passphrase != null)
            {
                padding_len = 8 - (int)wr.Length % 8;
                wr.Write(new byte[padding_len]);
            }
            byte[] encrypted_body = wr.ToByteArray();
            SSHUtil.WriteIntToByteArray(encrypted_body, 0, encrypted_body.Length - padding_len - 4);

            //encrypt if necessary
            if (passphrase != null)
            {
                Cipher c = CipherFactory.CreateCipher(SSHProtocol.SSH2, CipherAlgorithm.TripleDES, PassphraseToKey(passphrase, 24));
                Debug.Assert(encrypted_body.Length % 8 == 0);
                byte[] tmp = new Byte[encrypted_body.Length];
                c.Encrypt(encrypted_body, 0, encrypted_body.Length, tmp, 0);
                encrypted_body = tmp;
            }

            //step2 make binary key data
            wr = new SSH2DataWriter();
            wr.WriteInt32(MAGIC_VAL);
            wr.WriteInt32(0); //for total size
            wr.WriteString(_keypair.Algorithm == PublicKeyAlgorithm.RSA ?
                           "if-modn{sign{rsa-pkcs1-sha1},encrypt{rsa-pkcs1v2-oaep}}" :
                           "dl-modp{sign{dsa-nist-sha1},dh{plain}}");

            wr.WriteString(passphrase == null ? "none" : "3des-cbc");
            wr.WriteAsString(encrypted_body);

            byte[] rawdata = wr.ToByteArray();
            SSHUtil.WriteIntToByteArray(rawdata, 4, rawdata.Length); //fix total length

            //step3 write final data
            StreamWriter sw = new StreamWriter(dest, Encoding.ASCII);

            sw.WriteLine("---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----");
            if (comment != null)
            {
                WriteKeyFileBlock(sw, "Comment: " + comment, true);
            }
            WriteKeyFileBlock(sw, Encoding.ASCII.GetString(Base64.Encode(rawdata)), false);
            sw.WriteLine("---- END SSH2 ENCRYPTED PRIVATE KEY ----");
            sw.Close();
        }