dlech.SshAgentLib.PpkFormatter.Deserialize C# (CSharp) Method

Deserialize() public method

Parses the data from a PuTTY Private Key (.ppk) file.
/// there was a problem parsing the file data /// /// data is encrypted and passphrase callback is null ///
public Deserialize ( Stream aStream ) : object
aStream Stream
return object
        public override object Deserialize(Stream aStream)
        {
            FileData fileData = new FileData();

              /* check for required parameters */
              if (aStream == null) {
            throw new ArgumentNullException("aStream");
              }

              string line;
              string[] pair = new string[2];
              int lineCount, i;

              StreamReader reader = new StreamReader(aStream, Encoding.GetEncoding(1252));
              char[] delimArray = { cDelimeter };

              try {
            /* read file version */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (!pair[0].StartsWith(puttyUserKeyFileKey)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          puttyUserKeyFileKey + " expected");
            }
            string ppkFileVersion = pair[0].Remove(0, puttyUserKeyFileKey.Length);
            if (!ppkFileVersion.TryParseVersion(ref fileData.ppkFileVersion)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileVersion);
            }
            if (fileData.ppkFileVersion == Version.V1) {
              if (WarnOldFileFormatCallbackMethod != null) {
            WarnOldFileFormatCallbackMethod.Invoke();
              }
            }

            /* read public key encryption algorithm type */
            string algorithm = pair[1].Trim();
            if (!algorithm.TryParsePublicKeyAlgorithm(ref fileData.publicKeyAlgorithm)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.PublicKeyEncryption);
            }

            /* read private key encryption algorithm type */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (pair[0] != privateKeyEncryptionKey) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          privateKeyEncryptionKey + " expected");
            }
            algorithm = pair[1].Trim();
            if (!algorithm.TryParsePrivateKeyAlgorithm(ref fileData.privateKeyAlgorithm)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.PrivateKeyEncryption);
            }

            /* read comment */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (pair[0] != commentKey) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          commentKey + " expected");
            }
            fileData.comment = pair[1].Trim();

            /* read public key */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (pair[0] != publicKeyLinesKey) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          publicKeyLinesKey + " expected");
            }
            if (!int.TryParse(pair[1], out lineCount)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          "integer expected");
            }
            string publicKeyString = string.Empty;
            for (i = 0; i < lineCount; i++) {
              publicKeyString += reader.ReadLine();
            }
            fileData.publicKeyBlob = Util.FromBase64(publicKeyString);

            /* read private key */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (pair[0] != privateKeyLinesKey) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          privateKeyLinesKey + " expected");
            }
            if (!int.TryParse(pair[1], out lineCount)) {
              throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                          "integer expected");
            }
            string privateKeyString = string.Empty;
            for (i = 0; i < lineCount; i++) {
              privateKeyString += reader.ReadLine();
            }
            fileData.privateKeyBlob =
              new PinnedArray<byte>(Util.FromBase64(privateKeyString));

            /* read MAC */
            line = reader.ReadLine();
            pair = line.Split(delimArray, 2);
            if (pair[0] != privateMACKey) {
              fileData.isHMAC = false;
              if (pair[0] != privateHashKey || fileData.ppkFileVersion != Version.V1) {
            throw new PpkFormatterException(PpkFormatterException.PpkErrorType.FileFormat,
                                            privateMACKey + " expected");
              }
            } else {
              fileData.isHMAC = true;
            }
            string privateMACString = pair[1].Trim();
            fileData.privateMAC = Util.FromHex(privateMACString);

            /* get passphrase and decrypt private key if required */
            if (fileData.privateKeyAlgorithm != PrivateKeyAlgorithm.None) {
              if (GetPassphraseCallbackMethod == null) {
            throw new CallbackNullException();
              }
              fileData.passphrase = GetPassphraseCallbackMethod.Invoke(fileData.comment);
              DecryptPrivateKey(ref fileData);
            }

            VerifyIntegrity(fileData);

            AsymmetricCipherKeyPair cipherKeyPair =
              CreateCipherKeyPair(fileData.publicKeyAlgorithm,
              fileData.publicKeyBlob, fileData.privateKeyBlob.Data);
            SshKey key = new SshKey(SshVersion.SSH2, cipherKeyPair, fileData.comment);
            return key;

              } catch (PpkFormatterException) {
            throw;
              } catch (CallbackNullException) {
            throw;
              } catch (Exception ex) {
            throw new PpkFormatterException(
            PpkFormatterException.PpkErrorType.FileFormat,
            "See inner exception.", ex);
              } finally {
            if (fileData.publicKeyBlob != null) {
              Array.Clear(fileData.publicKeyBlob, 0, fileData.publicKeyBlob.Length);
            }
            if (fileData.privateKeyBlob != null) {
              fileData.privateKeyBlob.Dispose();
            }
            if (fileData.privateMAC != null) {
              Array.Clear(fileData.privateMAC, 0, fileData.privateMAC.Length);
            }
            reader.Close();
              }
        }

Usage Example

Example #1
0
        public void GetFingerprintTest()
        {
            var formatter1 = new Ssh1KeyFormatter();

            var rsaSsh1Target = formatter1.Deserialize(Resources.rsa1_1);
            var rsaSsh1ExpectedFingerprint = Resources.rsa1_1_fp.Trim();
            var rsaSsh1Actual = "MD5:" + rsaSsh1Target.GetMD5Fingerprint().ToHexString();
            Assert.That(rsaSsh1ExpectedFingerprint, Is.EqualTo(rsaSsh1Actual));

            var formatter2 = new PpkFormatter();

            var rsaTarget = formatter2.Deserialize(Resources.ssh2_rsa_no_passphrase_ppk);
            var rsaExpectedFingerprint = "57:95:98:7f:c2:4e:98:1d:b9:5b:45:fe:6d:a4:6b:17";
            var rsaActual = rsaTarget.GetMD5Fingerprint().ToHexString();
            Assert.That(rsaExpectedFingerprint, Is.EqualTo(rsaActual));

            var dsaTarget = formatter2.Deserialize(Resources.ssh2_dsa_no_passphrase_ppk);
            var dsaExpectedFingerprint = "4e:f1:fc:5d:80:5b:37:b6:13:67:ce:df:4e:83:7b:0b";
            var dsaActual = dsaTarget.GetMD5Fingerprint().ToHexString();
            Assert.That(dsaExpectedFingerprint, Is.EqualTo(dsaActual));
        }
All Usage Examples Of dlech.SshAgentLib.PpkFormatter::Deserialize