Inheritance: ThirdParty.BouncyCastle.Utilities.IO.Pem.PemReader
        /// <summary>
        /// Gets the decrypted password using the RSA private key which can be found in the
        /// PEM file for the key pair.
        /// </summary>
        /// <param name="rsaPrivateKey">The RSA private key from the PEM file</param>
        /// <returns>The decrypted password</returns>
        public string GetDecryptedPassword(string rsaPrivateKey)
        {
            RSAParameters rsaParams;
            try
            {
                rsaParams = new PemReader(new StringReader(rsaPrivateKey.Trim())).ReadPrivatekey();
            }
            catch (Exception e)
            {
                throw new AmazonEC2Exception("Invalid RSA Private Key", e);
            }

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParams);

            byte[] encryptedBytes = Convert.FromBase64String(this.PasswordData);
            var decryptedBytes = rsa.Decrypt(encryptedBytes, false);

            string decrypted = Encoding.UTF8.GetString(decryptedBytes);
            return decrypted;
        }
Esempio n. 2
0
        private X509Certificate2 GetX509Certificate()
        {
            lock (certificateCache)
            {
                if (certificateCache.ContainsKey(this.SigningCertURL))
                {
                    return certificateCache[this.SigningCertURL];
                }
                else
                {
                    for (int retries = 1; retries <= MAX_RETRIES; retries++)
                    {
                        try
                        {
                            HttpWebRequest request = HttpWebRequest.Create(this.SigningCertURL) as HttpWebRequest;
                            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                            using (var reader = new StreamReader(response.GetResponseStream()))
                            {
                                var content = reader.ReadToEnd().Trim();
                                var pemObject = new PemReader(new StringReader(content)).ReadPemObject();

                                X509Certificate2 certificate = new X509Certificate2(pemObject.Content);
                                certificateCache[this.SigningCertURL] = certificate;
                                return certificate;
                            }
                        }
                        catch(Exception e)
                        {
                            if (retries == MAX_RETRIES)
                                throw new AmazonClientException(string.Format(CultureInfo.InvariantCulture,
                                    "Unable to download signing cert after {0} retries", MAX_RETRIES), e);
                            else
                                AWSSDKUtils.Sleep((int)(Math.Pow(4, retries) * 100));
                        }
                    }
                }

                throw new AmazonClientException(string.Format(CultureInfo.InvariantCulture,
                    "Unable to download signing cert after {0} retries", MAX_RETRIES));
            }
        }
        internal static RSAParameters ConvertPEMToRSAParameters(TextReader privateKeyReader)
        {
            RSAParameters rsaParams;
            try
            {
                rsaParams = new PemReader(privateKeyReader).ReadPrivatekey();
            }
            catch (Exception e)
            {
                throw new AmazonClientException("Invalid RSA Private Key", e);
            }

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParams);

            return rsaParams;
        }