Exemplo n.º 1
0
        /// <summary>
        /// constructs from file
        /// </summary>
        /// <param name="path">file name</param>
        /// <param name="passphrase">passphrase or empty string if passphrase is not required</param>
        public SSH1UserAuthKey(string path, string passphrase)
        {
            #if PODEROSA_KEYFORMAT
            PrivateKeyLoader loader = new PrivateKeyLoader(path);
            loader.LoadSSH1PrivateKey(
                            passphrase,
                            out _modulus,
                            out _publicExponent,
                            out _privateExponent,
                            out _primeP,
                            out _primeQ,
                            out _crtCoefficient,
                            out _comment);
            #else
            Stream s = File.Open(path, FileMode.Open);
            byte[] header = new byte[32];
            s.Read(header, 0, header.Length);
            if (Encoding.ASCII.GetString(header) != "SSH PRIVATE KEY FILE FORMAT 1.1\n")
                throw new SSHException(String.Format(Strings.GetString("BrokenKeyFile"), path));

            SSH1DataReader reader = new SSH1DataReader(ReadAll(s));
            s.Close();

            byte[] cipher = reader.Read(2); //first 2 bytes indicates algorithm and next 8 bytes is space
            reader.Read(8);

            _modulus = reader.ReadMPInt();
            _publicExponent = reader.ReadMPInt();
            _comment = reader.ReadString();
            byte[] prvt = reader.GetRemainingDataView().GetBytes();
            //必要なら復号
            CipherAlgorithm algo = (CipherAlgorithm)cipher[1];
            if (algo != 0) {
                Cipher c = CipherFactory.CreateCipher(SSHProtocol.SSH1, algo, ConvertToKey(passphrase));
                byte[] buf = new byte[prvt.Length];
                c.Decrypt(prvt, 0, prvt.Length, buf, 0);
                prvt = buf;
            }

            SSH1DataReader prvtreader = new SSH1DataReader(prvt);
            byte[] mark = prvtreader.Read(4);
            if (mark[0] != mark[2] || mark[1] != mark[3])
                throw new SSHException(Strings.GetString("WrongPassphrase"));

            _privateExponent = prvtreader.ReadMPInt();
            _crtCoefficient = prvtreader.ReadMPInt();
            _primeP = prvtreader.ReadMPInt();
            _primeQ = prvtreader.ReadMPInt();
            #endif
        }
Exemplo n.º 2
0
 internal void AsyncReceivePacket(DataFragment data)
 {
     try {
         SSH1DataReader re = new SSH1DataReader(data);
         SSH1PacketType pt = (SSH1PacketType)re.ReadByte();
         switch (pt) {
             case SSH1PacketType.SSH_SMSG_STDOUT_DATA: {
                     int len = re.ReadInt32();
                     DataFragment frag = re.GetRemainingDataView(len);
                     _channel_collection.FindChannelEntry(_shellID).Receiver.OnData(frag.Data, frag.Offset, frag.Length);
                 }
                 break;
             case SSH1PacketType.SSH_SMSG_STDERR_DATA: {
                     _channel_collection.FindChannelEntry(_shellID).Receiver.OnExtendedData((int)SSH1PacketType.SSH_SMSG_STDERR_DATA, re.ReadByteString());
                 }
                 break;
             case SSH1PacketType.SSH_MSG_CHANNEL_DATA: {
                     int channel = re.ReadInt32();
                     int len = re.ReadInt32();
                     DataFragment frag = re.GetRemainingDataView(len);
                     _channel_collection.FindChannelEntry(channel).Receiver.OnData(frag.Data, frag.Offset, frag.Length);
                 }
                 break;
             case SSH1PacketType.SSH_MSG_PORT_OPEN:
                 ProcessPortforwardingRequest(_eventReceiver, re);
                 break;
             case SSH1PacketType.SSH_MSG_CHANNEL_CLOSE: {
                     int channel = re.ReadInt32();
                     ISSHChannelEventReceiver r = _channel_collection.FindChannelEntry(channel).Receiver;
                     _channel_collection.UnregisterChannelEventReceiver(channel);
                     r.OnChannelClosed();
                 }
                 break;
             case SSH1PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION: {
                     int channel = re.ReadInt32();
                 }
                 break;
             case SSH1PacketType.SSH_MSG_DISCONNECT:
                 _eventReceiver.OnConnectionClosed();
                 break;
             case SSH1PacketType.SSH_SMSG_EXITSTATUS:
                 _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelClosed();
                 break;
             case SSH1PacketType.SSH_MSG_DEBUG:
                 _eventReceiver.OnDebugMessage(false, re.ReadByteString());
                 break;
             case SSH1PacketType.SSH_MSG_IGNORE:
                 _eventReceiver.OnIgnoreMessage(re.ReadByteString());
                 break;
             case SSH1PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION: {
                     int local = re.ReadInt32();
                     int remote = re.ReadInt32();
                     _channel_collection.FindChannelEntry(local).Receiver.OnChannelReady();
                 }
                 break;
             case SSH1PacketType.SSH_SMSG_SUCCESS:
                 if (_executingShell) {
                     ExecShell();
                     _channel_collection.FindChannelEntry(_shellID).Receiver.OnChannelReady();
                     _executingShell = false;
                 }
                 break;
             default:
                 _eventReceiver.OnUnknownMessage((byte)pt, data.GetBytes());
                 break;
         }
     }
     catch (Exception ex) {
         _eventReceiver.OnError(ex);
     }
 }
        /// <summary>
        /// Read SSH1 private key parameters.
        /// </summary>
        /// <param name="passphrase">passphrase for decrypt the key file</param>
        /// <param name="modulus">private key parameter</param>
        /// <param name="publicExponent">private key parameter</param>
        /// <param name="privateExponent">private key parameter</param>
        /// <param name="primeP">private key parameter</param>
        /// <param name="primeQ">private key parameter</param>
        /// <param name="crtCoefficient">private key parameter</param>
        /// <param name="comment">comment</param>
        /// <exception cref="SSHException">failed to parse</exception>
        public void Load(
                            string passphrase,
                            out BigInteger modulus,
                            out BigInteger publicExponent,
                            out BigInteger privateExponent,
                            out BigInteger primeP,
                            out BigInteger primeQ,
                            out BigInteger crtCoefficient,
                            out string comment)
        {
            if (keyFile == null)
                throw new SSHException("A key file is not loaded yet");
            byte[] hdr = Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH1_HEADER);
            if (!ByteArrayUtil.ByteArrayStartsWith(keyFile, hdr))
                throw new SSHException(Strings.GetString("NotValidPrivateKeyFile"));

            SSH1DataReader reader = new SSH1DataReader(keyFile);
            reader.Read(hdr.Length);

            byte[] cipher = reader.Read(2); //first 2 bytes indicates algorithm and next 8 bytes is space
            reader.Read(8);

            modulus = reader.ReadMPInt();
            publicExponent = reader.ReadMPInt();
            comment = reader.ReadString();
            byte[] prvt = reader.GetRemainingDataView().GetBytes();
            //必要なら復号
            CipherAlgorithm algo = (CipherAlgorithm)cipher[1];
            if (algo != 0) {
                Cipher c = CipherFactory.CreateCipher(SSHProtocol.SSH1, algo, SSH1PassphraseToKey(passphrase));
                c.Decrypt(prvt, 0, prvt.Length, prvt, 0);
            }

            SSH1DataReader prvtreader = new SSH1DataReader(prvt);
            byte[] mark = prvtreader.Read(4);
            if (mark[0] != mark[2] || mark[1] != mark[3])
                throw new SSHException(Strings.GetString("WrongPassphrase"));

            privateExponent = prvtreader.ReadMPInt();
            crtCoefficient = prvtreader.ReadMPInt();
            primeP = prvtreader.ReadMPInt();
            primeQ = prvtreader.ReadMPInt();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process packet about this channel.
        /// </summary>
        /// <param name="packetType">a packet type (message number)</param>
        /// <param name="packetFragment">a packet image except message number and recipient channel.</param>
        public override void ProcessPacket(SSH1PacketType packetType, DataFragment packetFragment)
        {
            if (_state == State.Closed) {
                return; // ignore
            }

            DataFragment dataFragmentArg;

            lock (_stateSync) {
                switch (_state) {
                    case State.Initial:
                        break;
                    case State.WaitStartPTYResponse:
                        if (packetType == SSH1PacketType.SSH_SMSG_SUCCESS) {
                            _state = State.StartPTYSuccess;
                            Monitor.PulseAll(_stateSync);   // notifies state change
                            _receivedPacket.TrySet(packetFragment, PASSING_TIMEOUT);
                        }
                        else if (packetType == SSH1PacketType.SSH_SMSG_FAILURE) {
                            _state = State.StartPTYFailure;
                            Monitor.PulseAll(_stateSync);   // notifies state change
                            _receivedPacket.TrySet(packetFragment, PASSING_TIMEOUT);
                        }
                        break;
                    case State.Established:
                        break;
                    case State.Ready:
                        switch (packetType) {
                            case SSH1PacketType.SSH_SMSG_STDOUT_DATA: {
                                    SSH1DataReader reader = new SSH1DataReader(packetFragment);
                                    int len = reader.ReadInt32();
                                    dataFragmentArg = reader.GetRemainingDataView(len);
                                }
                                goto OnData;    // do it out of the lock block
                            case SSH1PacketType.SSH_SMSG_STDERR_DATA: {
                                    SSH1DataReader reader = new SSH1DataReader(packetFragment);
                                    int len = reader.ReadInt32();
                                    dataFragmentArg = reader.GetRemainingDataView(len);
                                }
                                goto OnData;    // do it out of the lock block
                            case SSH1PacketType.SSH_SMSG_EXITSTATUS:
                                Transmit(
                                    new SSH1Packet(SSH1PacketType.SSH_CMSG_EXIT_CONFIRMATION)
                                );
                                goto SetStateClosedByServer;    // do it out of the lock block
                        }
                        goto OnUnhandledPacket; // do it out of the lock block
                }
            }

            return;

            OnData:
            Handler.OnData(dataFragmentArg);
            return;

            SetStateClosedByServer:
            Trace("CH[{0}] closed by server", LocalChannel);
            SetStateClosed(true);
            return;

            OnUnhandledPacket:
            Handler.OnUnhandledPacket((byte)packetType, packetFragment);
            return;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Process packet about this channel.
        /// </summary>
        /// <param name="packetType">a packet type (message number)</param>
        /// <param name="packetFragment">a packet image except message number and recipient channel.</param>
        public override void ProcessPacket(SSH1PacketType packetType, DataFragment packetFragment)
        {
            if (_state == State.Closed) {
                return; // ignore
            }

            DataFragment dataFragmentArg;

            lock (_stateSync) {
                switch (_state) {
                    case State.InitiatedByServer:
                        break;
                    case State.InitiatedByClient:
                        if (packetType == SSH1PacketType.SSH_MSG_CHANNEL_OPEN_CONFIRMATION) {
                            SSH1DataReader reader = new SSH1DataReader(packetFragment);
                            SetRemoteChannel(reader.ReadUInt32());
                            _state = State.Established;
                            Monitor.PulseAll(_stateSync);   // notifies state change
                            dataFragmentArg = new DataFragment(0);
                            goto OnEstablished; // do it out of the lock block
                        }
                        if (packetType == SSH1PacketType.SSH_MSG_CHANNEL_OPEN_FAILURE) {
                            goto RequestFailed; // do it out of the lock block
                        }
                        break;
                    case State.Closing:
                        if (packetType == SSH1PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION) {
                            goto SetStateClosedByClient;    // do it out of the lock block
                        }
                        break;
                    case State.Established:
                    case State.Ready:
                        if (ProcessPacketSub(packetType, packetFragment) == SubPacketProcessResult.Consumed) {
                            return;
                        }
                        switch (packetType) {
                            case SSH1PacketType.SSH_MSG_CHANNEL_DATA: {
                                    SSH1DataReader reader = new SSH1DataReader(packetFragment);
                                    int len = reader.ReadInt32();
                                    dataFragmentArg = reader.GetRemainingDataView(len);
                                }
                                goto OnData;    // do it out of the lock block
                            case SSH1PacketType.SSH_MSG_CHANNEL_CLOSE:
                                Transmit(
                                    new SSH1Packet(SSH1PacketType.SSH_MSG_CHANNEL_CLOSE_CONFIRMATION)
                                        .WriteUInt32(RemoteChannel)
                                );
                                goto SetStateClosedByServer;    // do it out of the lock block
                        }
                        goto OnUnhandledPacket; // do it out of the lock block
                }
            }

            return;

            OnEstablished:
            Trace("CH[{0}] channel opend : remoteChannel={1}", LocalChannel, RemoteChannel);
            Handler.OnEstablished(dataFragmentArg);
            OnChannelEstablished();
            return;

            RequestFailed:
            Trace("CH[{0}] request failed", LocalChannel);
            RequestFailed();
            return;

            SetStateClosedByClient:
            Trace("CH[{0}] closed by client", LocalChannel);
            SetStateClosed(false);
            return;

            SetStateClosedByServer:
            Trace("CH[{0}] closed by server", LocalChannel);
            SetStateClosed(true);
            return;

            OnData:
            Handler.OnData(dataFragmentArg);
            return;

            OnUnhandledPacket:
            Handler.OnUnhandledPacket((byte)packetType, packetFragment);
            return;
        }