Пример #1
0
        /// <summary>
        /// Reads file and loads key into agent
        /// </summary>
        /// <param name="aAgent">the agent</param>
        /// <param name="aFileName">pathname of file to read</param>
        /// <param name="aGetPassPhraseCallback">method that returns passphrase</param>
        /// <param name="aConstraints">additional constraints</param>
        /// <exception cref="AgentFailureException">
        /// Agent returned SSH_AGENT_FAILURE
        /// </exception>
        /// <exception cref="KeyFormatterException">
        /// File format was not recognized
        /// </exception>
        /// <exception cref="UnauthorizedAccessException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="PathTooLongException"></exception>
        /// <exception cref="DirectoryNotFoundException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns>The ssh key that was read from the file</returns>
        public static ISshKey AddKeyFromFile(this IAgent aAgent, string aFileName,
                                             KeyFormatter.GetPassphraseCallback aGetPassPhraseCallback,
                                             ICollection <Agent.KeyConstraint> aConstraints = null)
        {
            string firstLine;

            using (var fileReader = File.OpenText(aFileName)) {
                firstLine = fileReader.ReadLine();
            }
            var formatter = KeyFormatter.GetFormatter(firstLine);

            formatter.GetPassphraseCallbackMethod = aGetPassPhraseCallback;
            var key = formatter.DeserializeFile(aFileName);

            if (aConstraints != null)
            {
                foreach (var constraint in aConstraints)
                {
                    key.AddConstraint(constraint);
                }
            }
            // prevent error in Pageant by attempting to remove key before adding it
            // this makes behavior more consistent with OpenSSH
            if (aAgent is PageantClient)
            {
                try {
                    aAgent.RemoveKey(key);
                } catch (Exception) { /* error will occur if key is not loaded */ }
            }
            aAgent.AddKey(key);
            return(key);
        }
Пример #2
0
 /// <summary>
 /// Auto-detect data format, read data and create key object
 /// </summary>
 /// <param name="aStream"></param>
 /// <returns></returns>
 public static ISshKey ReadSshKey(this Stream aStream,
                                  KeyFormatter.GetPassphraseCallback aGetPassphraseCallback = null)
 {
     using (var reader = new StreamReader(aStream)) {
         var firstLine = reader.ReadLine();
         var formatter = KeyFormatter.GetFormatter(firstLine);
         formatter.GetPassphraseCallbackMethod = aGetPassphraseCallback;
         aStream.Position = 0;
         return(formatter.Deserialize(aStream) as ISshKey);
     }
 }
Пример #3
0
 static ISshKey GetSshKey(Func<Stream> getPrivateKeyStream,
     Func<Stream> getPublicKeyStream,
     string fallbackComment,
     KeyFormatter.GetPassphraseCallback getPassphrase)
 {
     ISshKey key;
       using (var privateKeyStream = getPrivateKeyStream())
     key = privateKeyStream.ReadSshKey(getPassphrase);
       if (string.IsNullOrWhiteSpace(key.Comment) && getPublicKeyStream != null) {
     using (var stream = getPublicKeyStream())
       key.Comment = KeyFormatter.GetComment(stream.ReadAllLines(Encoding.UTF8));
       }
       if (string.IsNullOrWhiteSpace(key.Comment)) {
     key.Comment = fallbackComment;
       }
       return key;
 }
Пример #4
0
   /// <summary>
   /// Reads file and loads key into agent
   /// </summary>
   /// <param name="aAgent">the agent</param>
   /// <param name="aFileName">pathname of file to read</param>
   /// <param name="aGetPassPhraseCallback">method that returns passphrase</param>
   /// <param name="aConstraints">additional constraints</param>
   /// <exception cref="AgentFailureException">
   /// Agent returned SSH_AGENT_FAILURE
   /// </exception>
   /// <exception cref="KeyFormatterException">
   /// File format was not recognized
   /// </exception>
   /// <exception cref="UnauthorizedAccessException"></exception>
   /// <exception cref="ArgumentException"></exception>
   /// <exception cref="ArgumentNullException"></exception>
   /// <exception cref="PathTooLongException"></exception>
   /// <exception cref="DirectoryNotFoundException"></exception>
   /// <exception cref="FileNotFoundException"></exception>
   /// <exception cref="NotSupportedException"></exception>
   /// <returns>The ssh key that was read from the file</returns>
   public static ISshKey AddKeyFromFile(this IAgent aAgent, string aFileName,
 KeyFormatter.GetPassphraseCallback aGetPassPhraseCallback,
 ICollection<Agent.KeyConstraint> aConstraints = null)
   {
       string firstLine;
         using (var fileReader = File.OpenText(aFileName)) {
       firstLine = fileReader.ReadLine();
         }
         var formatter = KeyFormatter.GetFormatter(firstLine);
         formatter.GetPassphraseCallbackMethod = aGetPassPhraseCallback;
         var key = formatter.DeserializeFile(aFileName);
         if (aConstraints != null) {
       foreach (var constraint in aConstraints) {
         key.AddConstraint(constraint);
       }
         }
         // prevent error in Pageant by attempting to remove key before adding it
         // this makes behavior more consistent with OpenSSH
         if (aAgent is PageantClient) {
       try {
         aAgent.RemoveKey (key);
       } catch (Exception) { /* error will occur if key is not loaded */ }
         }
         aAgent.AddKey(key);
         return key;
   }
Пример #5
0
 /// <summary>
 /// Auto-detect data format, read data and create key object
 /// </summary>
 /// <param name="aStream"></param>
 /// <returns></returns>
 public static ISshKey ReadSshKey(this byte[] aData, KeyFormatter.GetPassphraseCallback aGetPassphraseCallback = null)
 {
     using (var stream = new MemoryStream(aData)) {
     return stream.ReadSshKey(aGetPassphraseCallback);
       }
 }
Пример #6
0
 /// <summary>
 /// Auto-detect data format, read data and create key object
 /// </summary>
 /// <param name="aStream"></param>
 /// <returns></returns>
 public static ISshKey ReadSshKey(this Stream aStream,
                              KeyFormatter.GetPassphraseCallback aGetPassphraseCallback = null)
 {
     using (var reader = new StreamReader(aStream)) {
     var firstLine = reader.ReadLine();
     var formatter = KeyFormatter.GetFormatter(firstLine);
     formatter.GetPassphraseCallbackMethod = aGetPassphraseCallback;
     aStream.Position = 0;
     return formatter.Deserialize(aStream) as ISshKey;
       }
 }