Beispiel #1
0
        /// <exception cref="System.IO.IOException"></exception>
        internal override void Write(byte[] foo, int s, int l)
        {
            if (packet == null)
            {
                wbuf   = new Buffer(rmpsize);
                packet = new Packet(wbuf);
            }
            rbuf.Shift();
            if (rbuf.buffer.Length < rbuf.index + l)
            {
                byte[] newbuf = new byte[rbuf.s + l];
                System.Array.Copy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.Length);
                rbuf.buffer = newbuf;
            }
            rbuf.PutByte(foo, s, l);
            int mlen = rbuf.GetInt();

            if (mlen > rbuf.GetLength())
            {
                rbuf.s -= 4;
                return;
            }
            int     typ      = rbuf.GetByte();
            Session _session = null;

            try
            {
                _session = GetSession();
            }
            catch (JSchException e)
            {
                throw new IOException(e.ToString());
            }
            IdentityRepository irepo    = _session.jsch.GetIdentityRepository();
            UserInfo           userinfo = _session.GetUserInfo();

            mbuf.Reset();
            if (typ == SSH2_AGENTC_REQUEST_IDENTITIES)
            {
                mbuf.PutByte(SSH2_AGENT_IDENTITIES_ANSWER);
                ArrayList identities = irepo.GetIdentities();
                lock (identities)
                {
                    int count = 0;
                    for (int i = 0; i < identities.Count; i++)
                    {
                        Identity identity = (Identity)(identities[i]);
                        if (identity.GetPublicKeyBlob() != null)
                        {
                            count++;
                        }
                    }
                    mbuf.PutInt(count);
                    for (int i_1 = 0; i_1 < identities.Count; i_1++)
                    {
                        Identity identity   = (Identity)(identities[i_1]);
                        byte[]   pubkeyblob = identity.GetPublicKeyBlob();
                        if (pubkeyblob == null)
                        {
                            continue;
                        }
                        mbuf.PutString(pubkeyblob);
                        mbuf.PutString(Util.empty);
                    }
                }
            }
            else
            {
                if (typ == SSH_AGENTC_REQUEST_RSA_IDENTITIES)
                {
                    mbuf.PutByte(SSH_AGENT_RSA_IDENTITIES_ANSWER);
                    mbuf.PutInt(0);
                }
                else
                {
                    if (typ == SSH2_AGENTC_SIGN_REQUEST)
                    {
                        byte[] blob  = rbuf.GetString();
                        byte[] data  = rbuf.GetString();
                        int    flags = rbuf.GetInt();
                        //      if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
                        //        datafellows = SSH_BUG_SIGBLOB;
                        //      }
                        ArrayList identities = irepo.GetIdentities();
                        Identity  identity   = null;
                        lock (identities)
                        {
                            for (int i = 0; i < identities.Count; i++)
                            {
                                Identity _identity = (Identity)(identities[i]);
                                if (_identity.GetPublicKeyBlob() == null)
                                {
                                    continue;
                                }
                                if (!Util.Array_equals(blob, _identity.GetPublicKeyBlob()))
                                {
                                    continue;
                                }
                                if (_identity.IsEncrypted())
                                {
                                    if (userinfo == null)
                                    {
                                        continue;
                                    }
                                    while (_identity.IsEncrypted())
                                    {
                                        if (!userinfo.PromptPassphrase("Passphrase for " + _identity.GetName()))
                                        {
                                            break;
                                        }
                                        string _passphrase = userinfo.GetPassphrase();
                                        if (_passphrase == null)
                                        {
                                            break;
                                        }
                                        byte[] passphrase = Util.Str2byte(_passphrase);
                                        try
                                        {
                                            if (_identity.SetPassphrase(passphrase))
                                            {
                                                break;
                                            }
                                        }
                                        catch (JSchException)
                                        {
                                            break;
                                        }
                                    }
                                }
                                if (!_identity.IsEncrypted())
                                {
                                    identity = _identity;
                                    break;
                                }
                            }
                        }
                        byte[] signature = null;
                        if (identity != null)
                        {
                            signature = identity.GetSignature(data);
                        }
                        if (signature == null)
                        {
                            mbuf.PutByte(SSH2_AGENT_FAILURE);
                        }
                        else
                        {
                            mbuf.PutByte(SSH2_AGENT_SIGN_RESPONSE);
                            mbuf.PutString(signature);
                        }
                    }
                    else
                    {
                        if (typ == SSH2_AGENTC_REMOVE_IDENTITY)
                        {
                            byte[] blob = rbuf.GetString();
                            irepo.Remove(blob);
                            mbuf.PutByte(SSH_AGENT_SUCCESS);
                        }
                        else
                        {
                            if (typ == SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES)
                            {
                                mbuf.PutByte(SSH_AGENT_SUCCESS);
                            }
                            else
                            {
                                if (typ == SSH2_AGENTC_REMOVE_ALL_IDENTITIES)
                                {
                                    irepo.RemoveAll();
                                    mbuf.PutByte(SSH_AGENT_SUCCESS);
                                }
                                else
                                {
                                    if (typ == SSH2_AGENTC_ADD_IDENTITY)
                                    {
                                        int    fooo = rbuf.GetLength();
                                        byte[] tmp  = new byte[fooo];
                                        rbuf.GetByte(tmp);
                                        bool result = irepo.Add(tmp);
                                        mbuf.PutByte(result ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                                    }
                                    else
                                    {
                                        rbuf.Skip(rbuf.GetLength() - 1);
                                        mbuf.PutByte(SSH_AGENT_FAILURE);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            byte[] response = new byte[mbuf.GetLength()];
            mbuf.GetByte(response);
            Send(response);
        }