コード例 #1
0
ファイル: PrivateKeyLoader.cs プロジェクト: yueker/poderosa
        /// <summary>
        /// Detect file format of a SSH private key file.
        /// </summary>
        /// <returns>format type</returns>
        /// <exception cref="IOException">File I/O error</exception>
        public PrivateKeyFileFormat ProbeFormat()
        {
            if (keyFile == null)
            {
                return(PrivateKeyFileFormat.UNKNOWN);
            }

            if (ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH1_HEADER)))
            {
                return(PrivateKeyFileFormat.SSH1);
            }

            if (ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_OPENSSH_HEADER_RSA)) ||
                ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_OPENSSH_HEADER_DSA)) ||
                ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_OPENSSH_HEADER_ECDSA)) ||
                ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_OPENSSH_HEADER_OPENSSH)))
            {
                return(PrivateKeyFileFormat.SSH2_OPENSSH);
            }

            if (ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_SSHCOM_HEADER)))
            {
                return(PrivateKeyFileFormat.SSH2_SSHCOM);
            }

            if (ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_PUTTY_HEADER_1)) ||
                ByteArrayUtil.ByteArrayStartsWith(keyFile, Encoding.ASCII.GetBytes(PrivateKeyFileHeader.SSH2_PUTTY_HEADER_2)))
            {
                return(PrivateKeyFileFormat.SSH2_PUTTY);
            }

            return(PrivateKeyFileFormat.UNKNOWN);
        }
コード例 #2
0
        /// <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>
        /// <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)
        {
            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();
            byte[] comment = reader.ReadString();
            byte[] prvt    = reader.ReadAll();
            //必要なら復号
            CipherAlgorithm algo = (CipherAlgorithm)cipher[1];

            if (algo != 0)
            {
                Cipher c   = CipherFactory.CreateCipher(SSHProtocol.SSH1, algo, SSH1PassphraseToKey(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();
        }