コード例 #1
0
        public override void Serialize(Stream aStream, object aObject)
        {
            /* check for required parameters */
            if (aStream == null)
            {
                throw new ArgumentNullException("aStream");
            }

            if (aObject == null)
            {
                throw new ArgumentNullException("aObject");
            }

            PasswordFinder pwFinder = null;

            if (GetPassphraseCallbackMethod != null)
            {
                pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
            }
            PinnedArray <char> passphrase = null;

            if (pwFinder != null)
            {
                passphrase      = new PinnedArray <char>(0);
                passphrase.Data = pwFinder.GetPassword();
            }

            byte cipherType;

            if (passphrase == null || passphrase.Data.Length == 0)
            {
                cipherType = SSH_CIPHER_NONE;
            }
            else
            {
                cipherType = SSH_CIPHER_3DES;
            }

            BlobBuilder builder = new BlobBuilder();

            ISshKey          sshKey          = aObject as ISshKey;
            RsaKeyParameters publicKeyParams = sshKey.GetPublicKeyParameters()
                                               as RsaKeyParameters;
            RsaPrivateCrtKeyParameters privateKeyParams = sshKey.GetPrivateKeyParameters()
                                                          as RsaPrivateCrtKeyParameters;

            /* writing info headers */
            builder.AddBytes(Encoding.ASCII.GetBytes(FILE_HEADER_LINE + "\n"));
            builder.AddUInt8(0);          //end of string
            builder.AddUInt8(cipherType); //cipher
            builder.AddInt(0);            //reserved

            /* writing public key */
            builder.AddInt(sshKey.Size);
            builder.AddSsh1BigIntBlob(publicKeyParams.Modulus);
            builder.AddSsh1BigIntBlob(publicKeyParams.Exponent);
            builder.AddStringBlob(sshKey.Comment);

            /* writing private key */
            BlobBuilder privateKeyBuilder = new BlobBuilder();

            /* adding some control values */
            Random random = new Random();

            byte[] resultCheck = new byte[2];
            random.NextBytes(resultCheck);

            privateKeyBuilder.AddUInt8(resultCheck[0]);
            privateKeyBuilder.AddUInt8(resultCheck[1]);
            privateKeyBuilder.AddUInt8(resultCheck[0]);
            privateKeyBuilder.AddUInt8(resultCheck[1]);
            privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Exponent);
            privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.DQ);
            privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.P);
            privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Q);

            if (cipherType == SSH_CIPHER_NONE)
            {
                /* plain-text */
                builder.AddBytes(privateKeyBuilder.GetBlobAsPinnedByteArray().Data);
            }
            else
            {
                byte[] keydata;
                using (MD5 md5 = MD5.Create()) {
                    keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data));
                }

                /* encryption */
                DesSsh1Engine desEngine = new DesSsh1Engine();
                desEngine.Init(true, new KeyParameter(keydata));

                BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine);
                byte[] ouputBuffer = bufferedBlockCipher.ProcessBytes(
                    privateKeyBuilder.GetBlobAsPinnedByteArray().Data);

                builder.AddBytes(ouputBuffer);

                passphrase.Dispose();
            }

            /* writing result to file */
            var builderOutput = builder.GetBlobAsPinnedByteArray();

            aStream.Write(builderOutput.Data, 0, builderOutput.Data.Length);
            aStream.Close();
        }
コード例 #2
0
        public override object Deserialize(Stream aStream)
        {
            /* check for required parameters */
            if (aStream == null)
            {
                throw new ArgumentNullException("aStream");
            }

            /* reading unencrypted part */
            BlobParser parser = new BlobParser(aStream);

            parser.ReadBytes((uint)FILE_HEADER_LINE.Length + 2); //Skipping header line

            byte cipherType = parser.ReadUInt8();

            if (cipherType != SSH_CIPHER_3DES && cipherType != SSH_CIPHER_NONE)
            {
                //TripleDes is the only encryption supported
                throw new KeyFormatterException("Unsupported cypherType: " + cipherType);
            }

            parser.ReadUInt32(); //reserved

            /* reading public key */
            AsymmetricKeyParameter aPublicKeyParameter =
                parser.ReadSsh1PublicKeyData(false);
            String keyComment = parser.ReadString();

            /* reading private key */
            byte[] inputBuffer = new byte[aStream.Length];
            aStream.Read(inputBuffer, 0, inputBuffer.Length);
            byte[] ouputBuffer;

            try {
                if (cipherType == 3)
                {
                    /* private key is 3DES encrypted */
                    PasswordFinder pwFinder = null;
                    if (GetPassphraseCallbackMethod != null)
                    {
                        pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
                    }

                    byte[] keydata;
                    try {
                        using (MD5 md5 = MD5.Create()) {
                            char[] md5Buffer = pwFinder.GetPassword();
                            keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(md5Buffer));
                        }
                    } catch (PasswordException ex) {
                        if (GetPassphraseCallbackMethod == null)
                        {
                            throw new CallbackNullException();
                        }
                        throw new KeyFormatterException("see inner exception", ex);
                    }

                    /* decryption */
                    DesSsh1Engine desEngine = new DesSsh1Engine();
                    desEngine.Init(false, new KeyParameter(keydata));

                    BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine);
                    ouputBuffer = bufferedBlockCipher.ProcessBytes(inputBuffer);
                }
                else
                {
                    /* private key is stored in plain text */
                    ouputBuffer = inputBuffer;
                }

                var privateKeyParser = new BlobParser(ouputBuffer);

                /* checking result of decryption */
                byte[] resultCheck = privateKeyParser.ReadBytes(4);
                if (resultCheck[0] != resultCheck[2] || resultCheck[1] != resultCheck[3])
                {
                    throw new KeyFormatterException("bad passphrase");
                }

                /* reading private key */
                var    keyPair = privateKeyParser.ReadSsh1KeyData(aPublicKeyParameter);
                SshKey key     = new SshKey(SshVersion.SSH1, keyPair);
                key.Comment = keyComment;
                return(key);
            } catch (KeyFormatterException) {
                throw;
            } catch (Exception ex) {
                throw new KeyFormatterException("see inner exception", ex);
            }
        }