예제 #1
0
 /**
  * Create a new PemReader with a password finder
  *
  * @param reader the Reader
  * @param pFinder the password finder
  */
 public PemReader(
     TextReader reader,
     IPasswordFinder pFinder)
     : base(reader)
 {
     this.pFinder = pFinder;
 }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Sawtooth.Sdk.Client.Signer"/> class from a PEM data stream
        /// </summary>
        /// <param name="pemStream">Pem stream.</param>
        /// <param name="passwordFinder">Password finder.</param>
        public Signer(Stream pemStream, IPasswordFinder passwordFinder = null)
        {
            var pemReader    = new PemReader(new StreamReader(pemStream), passwordFinder);
            var KeyParameter = (AsymmetricCipherKeyPair)pemReader.ReadObject();

            PrivateKey = (ECPrivateKeyParameters)KeyParameter.Private;
        }
예제 #3
0
                private (string certificate, string key) TransformPfx(IPasswordFinder passwordFinder)
                {
                    var certOutput = new StringWriter();
                    var keyOutput  = new StringWriter();

                    using (var input = File.OpenRead(CertificatePath))
                    {
                        var certWriter = new PemWriter(certOutput);
                        var keyWriter  = new PemWriter(keyOutput);
                        var store      = new Pkcs12Store(input, passwordFinder.GetPassword());
                        foreach (string alias in store.Aliases)
                        {
                            var cert = store.GetCertificate(alias);
                            if (cert != null)
                            {
                                certWriter.WriteObject(cert.Certificate);
                            }

                            var key = store.GetKey(alias);
                            if (key != null && key.Key.IsPrivate)
                            {
                                keyWriter.WriteObject(key.Key);
                            }
                        }
                    }

                    return(certOutput.ToString(), keyOutput.ToString());
                }
        /**
        * Create a new PemReader with a password finder
        *
        * @param reader the Reader
        * @param pFinder the password finder
        */
        public PemReader(
			TextReader		reader,
			IPasswordFinder	pFinder)
            : base(reader)
        {
            this.pFinder = pFinder;
        }
예제 #5
0
        private static PemReader OpenPemResource(
            string fileName,
            IPasswordFinder pGet)
        {
            Stream     data = GetTestDataAsStream("openssl." + fileName);
            TextReader tr   = new StreamReader(data);

            return(new PemReader(tr, pGet));
        }
예제 #6
0
 private IEnumerable <T> ReadPemObjects <T>(string path, IPasswordFinder passwordFinder = null)
 {
     using (var input = File.OpenText(path))
     {
         foreach (var obj in ReadPemObjects <T>(input, passwordFinder))
         {
             yield return(obj);
         }
     }
 }
예제 #7
0
		/**
		* Create a new PemReader with a password finder
		*
		* @param reader the Reader
		* @param pFinder the password finder
		*/
		public PemReader(
			TextReader		reader,
			IPasswordFinder	pFinder)
		{
			if (reader == null)
				throw new ArgumentNullException("reader");

			this.reader = reader;
			this.pFinder = pFinder;
		}
예제 #8
0
        /// <summary>
        /// Import OpenSSH PEM private key string into MS RSACryptoServiceProvider
        /// </summary>
        /// <param name="pem"></param>
        /// <returns></returns>
        public static RSACryptoServiceProvider ImportPrivateKey(string pem, IPasswordFinder passwordFinder)
        {
            PemReader pr = new PemReader(new StringReader(pem), passwordFinder);
            AsymmetricCipherKeyPair KeyPair   = (AsymmetricCipherKeyPair)pr.ReadObject();
            RSAParameters           rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)KeyPair.Private);

            RSACryptoServiceProvider csp = new RSACryptoServiceProvider();            // cspParams);

            csp.ImportParameters(rsaParams);
            return(csp);
        }
        /**
         * Create a new PemReader with a password finder
         *
         * @param reader the Reader
         * @param pFinder the password finder
         */
        public PemReader(
            TextReader reader,
            IPasswordFinder pFinder)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            this.reader  = reader;
            this.pFinder = pFinder;
        }
예제 #10
0
                private IEnumerable <T> ReadPemObjects <T>(StreamReader input, IPasswordFinder passwordFinder = null)
                {
                    var reader = new PemReader(input, passwordFinder);

                    do
                    {
                        var obj = reader.ReadObject();
                        if (obj is T t)
                        {
                            yield return(t);
                        }
                    } while (!input.EndOfStream);
                }
예제 #11
0
        protected override String DoSolve(String[] input)
        {
            String nextPwd = "";

            //read the input
            foreach (String line in input)
            {
                IPasswordFinder password = Factory.CreatePasswordFinder(line);

                nextPwd = password.FindNext();
                nextPwd = password.FindNext();
            }

            return($"Next Password is {nextPwd}.");
        }
예제 #12
0
                private string ReadPemKeys(IPasswordFinder passwordFinder)
                {
                    try
                    {
                        if (!ReadPrivateKeys(null).Any())
                        {
                            throw new InvalidOperationException($"The file \"{CertificateKeyPath}\" contains no private keys or has unsupported format.");
                        }

                        var key = File.ReadAllText(CertificateKeyPath, Encoding.ASCII);
                        return(key);
                    }
                    catch (PasswordException)
                    {
                        var privateKeys = ReadPrivateKeys(passwordFinder).ToList();
                        if (!privateKeys.Any())
                        {
                            throw new InvalidOperationException($"The file \"{CertificateKeyPath}\" contains no private keys.");
                        }

                        return(WritePrivateKeys(privateKeys));
                    }


                    IEnumerable <AsymmetricKeyParameter> ReadPrivateKeys(IPasswordFinder pf)
                    {
                        using (var input = File.OpenText(CertificateKeyPath))
                        {
                            var reader = new PemReader(input, pf);
                            do
                            {
                                var obj = reader.ReadObject();
                                if (obj is AsymmetricKeyParameter key && key.IsPrivate)
                                {
                                    yield return(key);
                                }
                                else if (obj is AsymmetricCipherKeyPair pair)
                                {
                                    yield return(pair.Private);
                                }
                            } while (!input.EndOfStream);
                        }
                    }
        private static PemReader OpenPemResource(
			string			fileName,
			IPasswordFinder	pGet)
        {
            Stream data = GetTestDataAsStream("openssl." + fileName);
            TextReader tr = new StreamReader(data);
            return new PemReader(tr, pGet);
        }
예제 #14
0
 public RsaTokenDecoder(IPasswordFinder passwordFinder, IKeyProvider keyProvider)
 {
     _passwordFinder = passwordFinder;
     _keyProvider    = keyProvider;
 }
예제 #15
0
 public PasswordChecker(PasswordFinderFactory passwordFinderFactory)
 {
     this.passwordFinder = passwordFinderFactory.GetNewPasswordFinder();
 }
예제 #16
0
 public RsaTokenGenerator(IPasswordFinder passwordFinder, IKeyProvider keyProvider)
 {
     _passwordFinder = passwordFinder;
     _keyProvider    = keyProvider;
 }