/// <summary> /// Import the PKCS key. /// </summary> /// <param name="purpose">The purpose.</param> /// <param name="input">The input.</param> /// <param name="passwordPrompt">The pass phrase prompt.</param> /// <param name="official"></param> /// <param name="hint"></param> /// <returns></returns> /// <exception cref="InvalidKeySetException">DSA key cannot be used for encryption and decryption!</exception> /// <exception cref="InvalidKeySetException">Unsupported key type!</exception> public virtual ImportedKeySet PkcsKey(KeyPurpose purpose, Stream input, Func <string> passwordPrompt = null, bool official = false, KeyType hint = null) { using (var password = CachedPrompt.Password(passwordPrompt)) { AsymmetricKeyParameter bouncyKey; var resetStream = Utility.ResetStreamWhenFinished(input); using (var streamReader = new NondestructiveStreamReader(input)) { bouncyKey = new PemReader(streamReader, new PasswordFinder(password.Prompt)).ReadObject() as AsymmetricKeyParameter; } if (bouncyKey == null) { resetStream.Reset(); bouncyKey = passwordPrompt == null ? PrivateKeyFactory.CreateKey(input) : PrivateKeyFactory.DecryptKey( (password.Prompt() ?? String.Empty).ToCharArray(), input); } Key key; switch (bouncyKey) { case RsaPrivateCrtKeyParameters rsa: key = KeyFromBouncyCastle(rsa, purpose, official, hint); break; case DsaPrivateKeyParameters dsa: if (KeyPurpose.DecryptAndEncrypt == purpose) { throw new InvalidKeySetException("DSA key cannot be used for encryption and decryption!"); } key = KeyFromBouncyCastle(dsa); break; case RsaKeyParameters rsa: key = KeyFromBouncyCastle(rsa, purpose, official, hint); break; case DsaPublicKeyParameters dsa: if (KeyPurpose.Encrypt == purpose) { throw new InvalidKeySetException("DSA key cannot be used for encryption!"); } key = KeyFromBouncyCastle(dsa); break; default: throw new InvalidKeySetException("Unsupported key type!"); } return(new ImportedKeySet(key, purpose, "imported from pkcs file")); } }
/// <summary> /// Import the PKCS key. /// </summary> /// <param name="purpose">The purpose.</param> /// <param name="input">The input.</param> /// <param name="passwordPrompt">The pass phrase prompt.</param> /// <returns></returns> /// <exception cref="InvalidKeySetException">DSA key cannot be used for encryption and decryption!</exception> /// <exception cref="InvalidKeySetException">Unsupported key type!</exception> public virtual ImportedKeySet PkcsKey(KeyPurpose purpose, Stream input, Func <string> passwordPrompt = null) { using (var password = CachedPrompt.Password(passwordPrompt)) { AsymmetricKeyParameter bouncyKey; var resetStream = Utility.ResetStreamWhenFinished(input); using (var streamReader = new NondestructiveStreamReader(input)) { bouncyKey = new PemReader(streamReader, new PasswordFinder(password.Prompt)).ReadObject() as AsymmetricKeyParameter; } if (bouncyKey == null) { resetStream.Reset(); bouncyKey = passwordPrompt == null ? PrivateKeyFactory.CreateKey(input) : PrivateKeyFactory.DecryptKey( (password.Prompt() ?? String.Empty).ToCharArray(), input); } Key key; if (bouncyKey is RsaPrivateCrtKeyParameters) { var keyParam = bouncyKey as RsaPrivateCrtKeyParameters; key = new RsaPrivateKey() { PublicKey = new RsaPublicKey() { Modulus = keyParam.Modulus.ToSystemBigInteger(), PublicExponent = keyParam.PublicExponent.ToSystemBigInteger(), Size = keyParam.Modulus.BitLength, }, PrimeP = keyParam.P.ToSystemBigInteger(), PrimeExponentP = keyParam.DP.ToSystemBigInteger(), PrimeExponentQ = keyParam.DQ.ToSystemBigInteger(), PrimeQ = keyParam.Q.ToSystemBigInteger(), CrtCoefficient = keyParam.QInv.ToSystemBigInteger(), PrivateExponent = keyParam.Exponent.ToSystemBigInteger(), Size = keyParam.Modulus.BitLength, }; } else if (bouncyKey is DsaPrivateKeyParameters) { var keyParam = bouncyKey as DsaPrivateKeyParameters; if (KeyPurpose.DecryptAndEncrypt == purpose) { throw new InvalidKeySetException("DSA key cannot be used for encryption and decryption!"); } key = new DsaPrivateKey() { X = keyParam.X.ToSystemBigInteger(), PublicKey = new DsaPublicKey { Y = keyParam.Parameters.G.ModPow(keyParam.X, keyParam.Parameters.P) .ToSystemBigInteger(), G = keyParam.Parameters.G.ToSystemBigInteger(), P = keyParam.Parameters.P.ToSystemBigInteger(), Q = keyParam.Parameters.Q.ToSystemBigInteger(), Size = keyParam.Parameters.P.BitLength }, Size = keyParam.Parameters.P.BitLength }; } else { throw new InvalidKeySetException("Unsupported key type!"); } return(new ImportedKeySet(key, purpose, "imported from pkcs file")); } }
/// <summary> /// Import the PKCS key. /// </summary> /// <param name="purpose">The purpose.</param> /// <param name="input">The input.</param> /// <param name="passwordPrompt">The pass phrase prompt.</param> /// <returns></returns> /// <exception cref="InvalidKeySetException">DSA key cannot be used for encryption and decryption!</exception> /// <exception cref="InvalidKeySetException">Unsupported key type!</exception> public virtual ImportedKeySet PkcsKey(KeyPurpose purpose, Stream input, Func<string> passwordPrompt = null) { using (var password = CachedPrompt.Password(passwordPrompt)) { AsymmetricKeyParameter bouncyKey; var resetStream = Utility.ResetStreamWhenFinished(input); using (var streamReader = new NondestructiveStreamReader(input)) { bouncyKey = new PemReader(streamReader, new PasswordFinder(password.Prompt)).ReadObject() as AsymmetricKeyParameter; } if (bouncyKey == null) { resetStream.Reset(); bouncyKey = passwordPrompt == null ? PrivateKeyFactory.CreateKey(input) : PrivateKeyFactory.DecryptKey( (password.Prompt() ?? String.Empty).ToCharArray(), input); } Key key; if (bouncyKey is RsaPrivateCrtKeyParameters) { var keyParam = bouncyKey as RsaPrivateCrtKeyParameters; key = new RsaPrivateKey() { PublicKey = new RsaPublicKey() { Modulus = keyParam.Modulus.ToSystemBigInteger(), PublicExponent = keyParam.PublicExponent.ToSystemBigInteger(), Size = keyParam.Modulus.BitLength, }, PrimeP = keyParam.P.ToSystemBigInteger(), PrimeExponentP = keyParam.DP.ToSystemBigInteger(), PrimeExponentQ = keyParam.DQ.ToSystemBigInteger(), PrimeQ = keyParam.Q.ToSystemBigInteger(), CrtCoefficient = keyParam.QInv.ToSystemBigInteger(), PrivateExponent = keyParam.Exponent.ToSystemBigInteger(), Size = keyParam.Modulus.BitLength, }; } else if (bouncyKey is DsaPrivateKeyParameters) { var keyParam = bouncyKey as DsaPrivateKeyParameters; if (KeyPurpose.DecryptAndEncrypt == purpose) { throw new InvalidKeySetException("DSA key cannot be used for encryption and decryption!"); } key = new DsaPrivateKey() { X = keyParam.X.ToSystemBigInteger(), PublicKey = new DsaPublicKey { Y = keyParam.Parameters.G.ModPow(keyParam.X, keyParam.Parameters.P) .ToSystemBigInteger(), G = keyParam.Parameters.G.ToSystemBigInteger(), P = keyParam.Parameters.P.ToSystemBigInteger(), Q = keyParam.Parameters.Q.ToSystemBigInteger(), Size = keyParam.Parameters.P.BitLength }, Size = keyParam.Parameters.P.BitLength }; } else { throw new InvalidKeySetException("Unsupported key type!"); } return new ImportedKeySet(key, purpose, "imported from pkcs file"); } }