Пример #1
0
        private void SendSign(SSH2DataReader r)
        {
            byte[] blob = r.ReadString();
            byte[] data = r.ReadString();
            //Debug.WriteLine(String.Format("SignRequest blobsize={0} datasize={1}", blob.Length, data.Length));

            SSH2UserAuthKey[] keys = _client.GetAvailableSSH2UserAuthKeys();
            SSH2UserAuthKey   key  = FindKey(keys, blob);

            if (key == null)
            {
                TransmitWriter(OpenWriter(AgentForwadPacketType.SSH_AGENT_FAILURE));
                _client.NotifyPublicKeyDidNotMatch();
            }
            else
            {
                SSH2DataWriter signpack = new SSH2DataWriter();
                signpack.WriteString(SSH2Util.PublicKeyAlgorithmName(key.Algorithm));
                signpack.WriteAsString(key.Sign(data));

                SSH2DataWriter wr = OpenWriter(AgentForwadPacketType.SSH2_AGENT_SIGN_RESPONSE);
                wr.WriteAsString(signpack.ToByteArray());
                TransmitWriter(wr);
            }
        }
Пример #2
0
        private void OnOK(object sender, EventArgs args)
        {
            this.DialogResult = DialogResult.None;

            try {
                SSH2UserAuthKey key = SSH2UserAuthKey.FromSECSHStyleFile(_tKeyFile.Text, _tCurrentPassphrase.Text);
                if (_tNewPassphrase.Text != _tNewPassphraseAgain.Text)
                {
                    GUtil.Warning(this, GApp.Strings.GetString("Message.ChangePassphrase.PassphraseMismatch"));
                }
                else
                {
                    if (_tNewPassphrase.Text.Length > 0 || GUtil.AskUserYesNo(this, GApp.Strings.GetString("Message.ChangePassphrase.AskEmptyPassphrase")) == DialogResult.Yes)
                    {
                        FileStream s = new FileStream(_tKeyFile.Text, FileMode.Create);
                        key.WritePrivatePartInSECSHStyleFile(s, "", _tNewPassphrase.Text);
                        s.Close();
                        GUtil.Warning(this, GApp.Strings.GetString("Message.ChangePassphrase.NotifyChanged"), MessageBoxIcon.Information);
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                }
            }
            catch (Exception ex) {
                GUtil.Warning(this, ex.Message);
            }
        }
Пример #3
0
 public SSH2UserAuthKey[] GetAvailableSSH2UserAuthKeys() {
     if(_keys==null) {
         SSH2UserAuthKey k = SSH2UserAuthKey.FromSECSHStyleFile(@"C:\P4\Tools\keys\aaa", "aaa");
         _keys = new SSH2UserAuthKey[] { k };
     }
     return _keys;
 }
Пример #4
0
        /// <summary>
        /// SSH2 private key signature
        /// </summary>
        private void SSH2Sign(byte[] blob, byte[] data, uint flags)
        {
            if ((flags & SSH_AGENT_OLD_SIGNATURE) != 0)
            {
                SendFailure();
                return;
            }

            SSH2UserAuthKey key = SSH2FindKey(blob);

            if (key == null)
            {
                SendFailure();
                return;
            }

            SSH2PayloadImageBuilder image = new SSH2PayloadImageBuilder();

            image.WriteString(key.Algorithm.GetAlgorithmName());
            image.WriteAsString(key.Sign(data));
            byte[] signatureBlob = image.GetBytes();

            Send(
                new OpenSSHAgentForwardingMessage(OpenSSHAgentForwardingMessageType.SSH2_AGENT_SIGN_RESPONSE)
                .WriteAsString(signatureBlob)
                );
        }
Пример #5
0
 private void OnOK(object sender, EventArgs args)
 {
     this.DialogResult = DialogResult.None;
     try {
         SSH2UserAuthKey key = SSH2UserAuthKey.FromSECSHStyleFile(_key.FileName, _passphraseBox.Text);
         Debug.Assert(key != null); //例外でなければ成功
         _key.SetStatus(PrivateKeyStatus.OK, key);
         this.DialogResult = DialogResult.OK;
     }
     catch (Exception ex) {
         GUtil.Warning(this, ex.Message);
     }
 }
Пример #6
0
 public SSH2UserAuthKey[] GetAvailableSSH2UserAuthKeys()
 {
     if (_ssh2Keys == null)
     {
         try {
             SSH2UserAuthKey k = SSH2UserAuthKey.FromSECSHStyleFile(@"C:\P4\Tools\keys\aaa", "aaa");
             _ssh2Keys = new SSH2UserAuthKey[] { k };
         }
         catch (Exception e) {
             Debug.WriteLine(e.Message);
             _ssh2Keys = new SSH2UserAuthKey[0];
         }
     }
     return(_ssh2Keys);
 }
Пример #7
0
        //Tutorial: Generating a new DSA key for user authentication
        private static void GenerateDSAKey()
        {
            //DSA KEY GENERATION TEST
            byte[]     testdata = Encoding.ASCII.GetBytes("CHRISTIAN VIERI");
            DSAKeyPair kp       = DSAKeyPair.GenerateNew(2048, new Random());

            //sign and verify test
            byte[] sig = kp.Sign(testdata);
            kp.Verify(sig, testdata);

            //export / import test
            SSH2UserAuthKey key = new SSH2UserAuthKey(kp);

            key.WritePublicPartInOpenSSHStyle(new FileStream("newdsakey.pub", FileMode.Create));
            key.WritePrivatePartInSECSHStyleFile(new FileStream("newrsakey.bin", FileMode.Create), "comment", "passphrase");
            //read test
            SSH2UserAuthKey newpk = SSH2UserAuthKey.FromSECSHStyleFile("newrsakey.bin", "passphrase");
        }
        /// <summary>
        /// Read SSH.com SSH2 private key parameters.
        /// </summary>
        /// <param name="passphrase">passphrase for decrypt the key file</param>
        /// <param name="keyPair">key pair</param>
        /// <param name="comment">comment or empty if it didn't exist</param>
        /// <exception cref="SSHException">failed to parse</exception>
        public void Load(string passphrase, out KeyPair keyPair, out string comment)
        {
            if (keyFile == null)
            {
                throw new SSHException("A key file is not loaded yet");
            }

            String base64Text;

            using (StreamReader sreader = GetStreamReader()) {
                string line = sreader.ReadLine();
                if (line == null || line != PrivateKeyFileHeader.SSH2_SSHCOM_HEADER)
                {
                    throw new SSHException(Strings.GetString("NotValidPrivateKeyFile") + " (missing header)");
                }

                StringBuilder buf = new StringBuilder();
                comment = String.Empty;
                while (true)
                {
                    line = sreader.ReadLine();
                    if (line == null)
                    {
                        throw new SSHException(Strings.GetString("NotValidPrivateKeyFile") + " (unexpected eof)");
                    }
                    if (line == PrivateKeyFileHeader.SSH2_SSHCOM_FOOTER)
                    {
                        break;
                    }
                    if (line.IndexOf(':') >= 0)
                    {
                        if (line.StartsWith("Comment: "))
                        {
                            comment = line.Substring("Comment: ".Length);
                        }
                    }
                    else if (line[line.Length - 1] == '\\')
                    {
                        buf.Append(line, 0, line.Length - 1);
                    }
                    else
                    {
                        buf.Append(line);
                    }
                }
                base64Text = buf.ToString();
            }

            byte[] keydata = Base64.Decode(Encoding.ASCII.GetBytes(base64Text));
            //Debug.WriteLine(DebugUtil.DumpByteArray(keydata));

            SSH2DataReader reader = new SSH2DataReader(keydata);
            int            magic  = reader.ReadInt32();

            if (magic != MAGIC)
            {
                throw new SSHException(Strings.GetString("NotValidPrivateKeyFile") + " (magic code unmatched)");
            }
            int    privateKeyLen = reader.ReadInt32();
            string type          = Encoding.ASCII.GetString(reader.ReadString());

            string ciphername = Encoding.ASCII.GetString(reader.ReadString());
            int    bufLen     = reader.ReadInt32();

            if (ciphername != "none")
            {
                CipherAlgorithm algo = CipherFactory.SSH2NameToAlgorithm(ciphername);
                byte[]          key  = SSH2UserAuthKey.PassphraseToKey(passphrase, CipherFactory.GetKeySize(algo));
                Cipher          c    = CipherFactory.CreateCipher(SSHProtocol.SSH2, algo, key);
                byte[]          tmp  = new Byte[reader.Image.Length - reader.Offset];
                c.Decrypt(reader.Image, reader.Offset, reader.Image.Length - reader.Offset, tmp, 0);
                reader = new SSH2DataReader(tmp);
            }

            int parmLen = reader.ReadInt32();

            if (parmLen < 0 || parmLen > reader.Rest)
            {
                throw new SSHException(Strings.GetString("WrongPassphrase"));
            }

            if (type.IndexOf("if-modn") != -1)
            {
                //mindterm mistaken this order of BigIntegers
                BigInteger e = reader.ReadBigIntWithBits();
                BigInteger d = reader.ReadBigIntWithBits();
                BigInteger n = reader.ReadBigIntWithBits();
                BigInteger u = reader.ReadBigIntWithBits();
                BigInteger p = reader.ReadBigIntWithBits();
                BigInteger q = reader.ReadBigIntWithBits();
                keyPair = new RSAKeyPair(e, d, n, u, p, q);
            }
            else if (type.IndexOf("dl-modp") != -1)
            {
                if (reader.ReadInt32() != 0)
                {
                    throw new SSHException(Strings.GetString("UnsupportedPrivateKeyFormat")
                                           + " (" + Strings.GetString("Reason_UnsupportedDSAKeyFormat") + ")");
                }
                BigInteger p = reader.ReadBigIntWithBits();
                BigInteger g = reader.ReadBigIntWithBits();
                BigInteger q = reader.ReadBigIntWithBits();
                BigInteger y = reader.ReadBigIntWithBits();
                BigInteger x = reader.ReadBigIntWithBits();
                keyPair = new DSAKeyPair(p, g, q, y, x);
            }
            else
            {
                throw new SSHException(Strings.GetString("UnsupportedAuthenticationMethod"));
            }
        }
Пример #9
0
 public void SetResultKey(SSH2UserAuthKey key)
 {
     _resultKey = key;
 }
Пример #10
0
 //状態更新 パスフレーズ入力ダイアログから
 public void SetStatus(PrivateKeyStatus st, SSH2UserAuthKey key)
 {
     _status = st;
     _key    = key;
 }
Пример #11
0
        private SSH2UserAuthKey _key; //有効化するまではnull

        public AgentPrivateKey(string filename)
        {
            _filename = filename;
            _status   = PrivateKeyStatus.Initial;
            _key      = null;
        }
Пример #12
0
 public void SetResultKey(SSH2UserAuthKey key)
 {
     _resultKey = key;
     CheckGenerationComplete();
 }