Пример #1
0
        /// <summary>The public key contained in the object.</summary>
        /// <returns>A lightweight public key.</returns>
        /// <exception cref="PgpException">If the key algorithm is not recognised.</exception>
        public AsymmetricKeyParameter GetKey()
        {
            try
            {
                switch (publicPk.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaGeneral:
                case PublicKeyAlgorithmTag.RsaSign:
                    RsaPublicBcpgKey rsaK = (RsaPublicBcpgKey)publicPk.Key;
                    return(new RsaKeyParameters(false, rsaK.Modulus, rsaK.PublicExponent));

                case PublicKeyAlgorithmTag.Dsa:
                    DsaPublicBcpgKey dsaK = (DsaPublicBcpgKey)publicPk.Key;
                    return(new DsaPublicKeyParameters(dsaK.Y, new DsaParameters(dsaK.P, dsaK.Q, dsaK.G)));

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    ElGamalPublicBcpgKey elK = (ElGamalPublicBcpgKey)publicPk.Key;
                    return(new ElGamalPublicKeyParameters(elK.Y, new ElGamalParameters(elK.P, elK.G)));

                default:
                    throw new PgpException("unknown public key algorithm encountered");
                }
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("exception constructing public key", e);
            }
        }
Пример #2
0
        public static IAsymmetricPublicKey ConvertPublic(PgpPublicKey publicKey)
        {
            PublicKeyPacket publicPk = publicKey.PublicKeyPacket;

            switch (publicKey.Algorithm)
            {
            case PublicKeyAlgorithmTag.RsaEncrypt:
            case PublicKeyAlgorithmTag.RsaGeneral:
            case PublicKeyAlgorithmTag.RsaSign:
                RsaPublicBcpgKey rsaK = (RsaPublicBcpgKey)publicPk.Key;

                return(new AsymmetricRsaPublicKey(FipsRsa.Alg, rsaK.Modulus, rsaK.PublicExponent));

            case PublicKeyAlgorithmTag.Dsa:
                DsaPublicBcpgKey dsaK = (DsaPublicBcpgKey)publicPk.Key;

                return(new AsymmetricDsaPublicKey(FipsDsa.Alg, new DsaDomainParameters(dsaK.P, dsaK.Q, dsaK.G), dsaK.Y));

            case PublicKeyAlgorithmTag.ElGamalEncrypt:
            case PublicKeyAlgorithmTag.ElGamalGeneral:
                ElGamalPublicBcpgKey elK = (ElGamalPublicBcpgKey)publicPk.Key;

                return(new AsymmetricDHPublicKey(FipsDsa.Alg, new DHDomainParameters(elK.P, elK.G), elK.Y));

            default:
                throw new PgpException("unknown public key algorithm encountered");
            }
        }
Пример #3
0
        /// <summary>
        /// Create a PgpPublicKey from the passed in lightweight one.
        /// </summary>
        /// <remarks>
        /// Note: the time passed in affects the value of the key's keyId, so you probably only want
        /// to do this once for a lightweight key, or make sure you keep track of the time you used.
        /// </remarks>
        /// <param name="algorithm">Asymmetric algorithm type representing the public key.</param>
        /// <param name="pubKey">Actual public key to associate.</param>
        /// <param name="time">Date of creation.</param>
        /// <exception cref="ArgumentException">If <c>pubKey</c> is not public.</exception>
        /// <exception cref="PgpException">On key creation problem.</exception>
        public PgpPublicKey(PublicKeyAlgorithmTag algorithm, IAsymmetricKeyParameter pubKey, DateTime time)
        {
            if (pubKey.IsPrivate)
            {
                throw new ArgumentException(@"Expected a public key", "pubKey");
            }

            IBcpgPublicKey bcpgKey;

            if (pubKey is RsaKeyParameters)
            {
                var rK = (RsaKeyParameters)pubKey;

                bcpgKey = new RsaPublicBcpgKey(rK.Modulus, rK.Exponent);
            }
            else if (pubKey is DsaPublicKeyParameters)
            {
                var dK = (DsaPublicKeyParameters)pubKey;
                var dP = dK.Parameters;

                bcpgKey = new DsaPublicBcpgKey(dP.P, dP.Q, dP.G, dK.Y);
            }
            else if (pubKey is ElGamalPublicKeyParameters)
            {
                var eK = (ElGamalPublicKeyParameters)pubKey;
                var eS = eK.Parameters;

                bcpgKey = new ElGamalPublicBcpgKey(eS.P, eS.G, eK.Y);
            }
            else if (pubKey is ECDHPublicKeyParameters)
            {
                var ecdh = (ECDHPublicKeyParameters)pubKey;

                bcpgKey = new ECDHPublicBcpgKey(ecdh.Q, ecdh.PublicKeyParamSet, ecdh.HashAlgorithm, ecdh.SymmetricKeyAlgorithm);
            }
            else if (pubKey is ECPublicKeyParameters)
            {
                var ecdsa = (ECPublicKeyParameters)pubKey;
                bcpgKey = new ECDSAPublicBcpgKey(ecdsa.Q, ecdsa.PublicKeyParamSet);
            }
            else
            {
                throw new PgpException("unknown key class");
            }

            _publicPk = new PublicKeyPacket(algorithm, time, bcpgKey);
            _ids      = Platform.CreateArrayList();
            _idSigs   = Platform.CreateArrayList <IList <IPgpSignature> >();

            try
            {
                Init();
            }
            catch (IOException e)
            {
                throw new PgpException("exception calculating keyId", e);
            }
        }
        /// <summary>
        /// Create a PgpPublicKey from the passed in lightweight one.
        /// </summary>
        /// <remarks>
        /// Note: the time passed in affects the value of the key's keyId, so you probably only want
        /// to do this once for a lightweight key, or make sure you keep track of the time you used.
        /// </remarks>
        /// <param name="algorithm">Asymmetric algorithm type representing the public key.</param>
        /// <param name="pubKey">Actual public key to associate.</param>
        /// <param name="time">Date of creation.</param>
        /// <exception cref="ArgumentException">If <c>pubKey</c> is not public.</exception>
        /// <exception cref="PgpException">On key creation problem.</exception>
        public PgpPublicKey(PublicKeyAlgorithmTag algorithm, IAsymmetricKeyParameter pubKey, DateTime time)
        {
            if (pubKey.IsPrivate)
                throw new ArgumentException(@"Expected a public key", "pubKey");

            IBcpgPublicKey bcpgKey;

            if (pubKey is RsaKeyParameters)
            {
                var rK = (RsaKeyParameters)pubKey;

                bcpgKey = new RsaPublicBcpgKey(rK.Modulus, rK.Exponent);
            }
            else if (pubKey is DsaPublicKeyParameters)
            {
                var dK = (DsaPublicKeyParameters)pubKey;
                var dP = dK.Parameters;

                bcpgKey = new DsaPublicBcpgKey(dP.P, dP.Q, dP.G, dK.Y);
            }
            else if (pubKey is ElGamalPublicKeyParameters)
            {
                var eK = (ElGamalPublicKeyParameters)pubKey;
                var eS = eK.Parameters;

                bcpgKey = new ElGamalPublicBcpgKey(eS.P, eS.G, eK.Y);
            }
            else if (pubKey is ECDHPublicKeyParameters)
            {
                var ecdh = (ECDHPublicKeyParameters)pubKey;

                bcpgKey = new ECDHPublicBcpgKey(ecdh.Q, ecdh.PublicKeyParamSet, ecdh.HashAlgorithm, ecdh.SymmetricKeyAlgorithm);
            }
            else if (pubKey is ECPublicKeyParameters)
            {
                var ecdsa = (ECPublicKeyParameters)pubKey;
                bcpgKey = new ECDSAPublicBcpgKey(ecdsa.Q, ecdsa.PublicKeyParamSet);
            }
            else
            {
                throw new PgpException("unknown key class");
            }

            _publicPk = new PublicKeyPacket(algorithm, time, bcpgKey);
            _ids = Platform.CreateArrayList();
            _idSigs = Platform.CreateArrayList<IList<IPgpSignature>>();

            try
            {
                Init();
            }
            catch (IOException e)
            {
                throw new PgpException("exception calculating keyId", e);
            }
        }
Пример #5
0
        /// <summary>
        /// Create a PgpPublicKey from the passed in lightweight one.
        /// </summary>
        /// <remarks>
        /// Note: the time passed in affects the value of the key's keyId, so you probably only want
        /// to do this once for a lightweight key, or make sure you keep track of the time you used.
        /// </remarks>
        /// <param name="algorithm">Asymmetric algorithm type representing the public key.</param>
        /// <param name="pubKey">Actual public key to associate.</param>
        /// <param name="time">Date of creation.</param>
        /// <exception cref="ArgumentException">If <c>pubKey</c> is not public.</exception>
        /// <exception cref="PgpException">On key creation problem.</exception>
        public PgpPublicKey(
            PublicKeyAlgorithmTag algorithm,
            AsymmetricKeyParameter pubKey,
            DateTime time)
        {
            if (pubKey.IsPrivate)
            {
                throw new ArgumentException("Expected a public key", "pubKey");
            }

            IBcpgKey bcpgKey;

            if (pubKey is RsaKeyParameters)
            {
                RsaKeyParameters rK = (RsaKeyParameters)pubKey;

                bcpgKey = new RsaPublicBcpgKey(rK.Modulus, rK.Exponent);
            }
            else if (pubKey is DsaPublicKeyParameters)
            {
                DsaPublicKeyParameters dK = (DsaPublicKeyParameters)pubKey;
                DsaParameters          dP = dK.Parameters;

                bcpgKey = new DsaPublicBcpgKey(dP.P, dP.Q, dP.G, dK.Y);
            }
            else if (pubKey is ElGamalPublicKeyParameters)
            {
                ElGamalPublicKeyParameters eK = (ElGamalPublicKeyParameters)pubKey;
                ElGamalParameters          eS = eK.Parameters;

                bcpgKey = new ElGamalPublicBcpgKey(eS.P, eS.G, eK.Y);
            }
            else
            {
                throw new PgpException("unknown key class");
            }

            this.publicPk = new PublicKeyPacket(algorithm, time, bcpgKey);
            this.ids      = Platform.CreateArrayList();
            this.idSigs   = Platform.CreateArrayList();

            try
            {
                Init();
            }
            catch (IOException e)
            {
                throw new PgpException("exception calculating keyId", e);
            }
        }
Пример #6
0
        /// <summary>
        /// Create a PgpPublicKey from the passed in JCA one.
        /// <p>
        /// Note: the time passed in affects the value of the key's keyId, so you probably only want
        /// to do this once for a JCA key, or make sure you keep track of the time you used.</p>
        /// </summary>
        public PgpPublicKey(
            PublicKeyAlgorithmTag	algorithm,
            AsymmetricKeyParameter	pubKey,
            DateTime				time)
        {
            if (pubKey.IsPrivate)
                throw new ArgumentException("Expected a public key", "pubKey");

            IBcpgKey bcpgKey;
            if (pubKey is RsaKeyParameters)
            {
                RsaKeyParameters rK = (RsaKeyParameters) pubKey;

                bcpgKey = new RsaPublicBcpgKey(rK.Modulus, rK.Exponent);
            }
            else if (pubKey is DsaPublicKeyParameters)
            {
                DsaPublicKeyParameters dK = (DsaPublicKeyParameters) pubKey;
                DsaParameters dP = dK.Parameters;

                bcpgKey = new DsaPublicBcpgKey(dP.P, dP.Q, dP.G, dK.Y);
            }
            else if (pubKey is ElGamalPublicKeyParameters)
            {
                ElGamalPublicKeyParameters eK = (ElGamalPublicKeyParameters) pubKey;
                ElGamalParameters eS = eK.Parameters;

                bcpgKey = new ElGamalPublicBcpgKey(eS.P, eS.G, eK.Y);
            }
            else
            {
                throw new PgpException("unknown key class");
            }

            this.publicPk = new PublicKeyPacket(algorithm, time, bcpgKey);
            this.ids = new ArrayList();
            this.idSigs = new ArrayList();

            try
            {
                Init();
            }
            catch (IOException e)
            {
                throw new PgpException("exception calculating keyId", e);
            }
        }
Пример #7
0
        public AsymmetricKeyParameter GetKey()
        {
            try
            {
                switch (publicPk.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaGeneral:
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaSign:
                {
                    RsaPublicBcpgKey rsaPublicBcpgKey = (RsaPublicBcpgKey)publicPk.Key;
                    return(new RsaKeyParameters(isPrivate: false, rsaPublicBcpgKey.Modulus, rsaPublicBcpgKey.PublicExponent));
                }

                case PublicKeyAlgorithmTag.Dsa:
                {
                    DsaPublicBcpgKey dsaPublicBcpgKey = (DsaPublicBcpgKey)publicPk.Key;
                    return(new DsaPublicKeyParameters(dsaPublicBcpgKey.Y, new DsaParameters(dsaPublicBcpgKey.P, dsaPublicBcpgKey.Q, dsaPublicBcpgKey.G)));
                }

                case PublicKeyAlgorithmTag.ECDsa:
                    return(GetECKey("ECDSA"));

                case PublicKeyAlgorithmTag.EC:
                    return(GetECKey("ECDH"));

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                {
                    ElGamalPublicBcpgKey elGamalPublicBcpgKey = (ElGamalPublicBcpgKey)publicPk.Key;
                    return(new ElGamalPublicKeyParameters(elGamalPublicBcpgKey.Y, new ElGamalParameters(elGamalPublicBcpgKey.P, elGamalPublicBcpgKey.G)));
                }

                default:
                    throw new PgpException("unknown public key algorithm encountered");
                }
            }
            catch (PgpException ex)
            {
                throw ex;
            }
            catch (global::System.Exception exception)
            {
                throw new PgpException("exception constructing public key", exception);
            }
        }
Пример #8
0
        /// <summary>Extract a <c>PgpPrivateKey</c> from this secret key's encrypted contents.</summary>
        public PgpPrivateKey ExtractPrivateKey(
            char[] passPhrase)
        {
            if (IsPrivateKeyEmpty)
            {
                return(null);
            }

            PublicKeyPacket pubPk = secret.PublicKeyPacket;

            try
            {
                byte[]                 data   = ExtractKeyData(passPhrase);
                BcpgInputStream        bcpgIn = BcpgInputStream.Wrap(new MemoryStream(data, false));
                AsymmetricKeyParameter privateKey;
                switch (pubPk.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaGeneral:
                case PublicKeyAlgorithmTag.RsaSign:
                    RsaPublicBcpgKey           rsaPub      = (RsaPublicBcpgKey)pubPk.Key;
                    RsaSecretBcpgKey           rsaPriv     = new RsaSecretBcpgKey(bcpgIn);
                    RsaPrivateCrtKeyParameters rsaPrivSpec = new RsaPrivateCrtKeyParameters(
                        rsaPriv.Modulus,
                        rsaPub.PublicExponent,
                        rsaPriv.PrivateExponent,
                        rsaPriv.PrimeP,
                        rsaPriv.PrimeQ,
                        rsaPriv.PrimeExponentP,
                        rsaPriv.PrimeExponentQ,
                        rsaPriv.CrtCoefficient);
                    privateKey = rsaPrivSpec;
                    break;

                case PublicKeyAlgorithmTag.Dsa:
                    DsaPublicBcpgKey dsaPub    = (DsaPublicBcpgKey)pubPk.Key;
                    DsaSecretBcpgKey dsaPriv   = new DsaSecretBcpgKey(bcpgIn);
                    DsaParameters    dsaParams = new DsaParameters(dsaPub.P, dsaPub.Q, dsaPub.G);
                    privateKey = new DsaPrivateKeyParameters(dsaPriv.X, dsaParams);
                    break;

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    ElGamalPublicBcpgKey elPub    = (ElGamalPublicBcpgKey)pubPk.Key;
                    ElGamalSecretBcpgKey elPriv   = new ElGamalSecretBcpgKey(bcpgIn);
                    ElGamalParameters    elParams = new ElGamalParameters(elPub.P, elPub.G);
                    privateKey = new ElGamalPrivateKeyParameters(elPriv.X, elParams);
                    break;

                default:
                    throw new PgpException("unknown public key algorithm encountered");
                }

                return(new PgpPrivateKey(privateKey, KeyId));
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception constructing key", e);
            }
        }
Пример #9
0
        public PgpPublicKey(PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, global::System.DateTime time)
        {
            //IL_0044: Unknown result type (might be due to invalid IL or missing references)
            //IL_016b: Expected O, but got Unknown
            if (pubKey.IsPrivate)
            {
                throw new ArgumentException("Expected a public key", "pubKey");
            }
            IBcpgKey key;

            if (pubKey is RsaKeyParameters)
            {
                RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)pubKey;
                key = new RsaPublicBcpgKey(rsaKeyParameters.Modulus, rsaKeyParameters.Exponent);
            }
            else if (pubKey is DsaPublicKeyParameters)
            {
                DsaPublicKeyParameters dsaPublicKeyParameters = (DsaPublicKeyParameters)pubKey;
                DsaParameters          parameters             = dsaPublicKeyParameters.Parameters;
                key = new DsaPublicBcpgKey(parameters.P, parameters.Q, parameters.G, dsaPublicKeyParameters.Y);
            }
            else if (pubKey is ECPublicKeyParameters)
            {
                ECPublicKeyParameters eCPublicKeyParameters = (ECPublicKeyParameters)pubKey;
                switch (algorithm)
                {
                case PublicKeyAlgorithmTag.EC:
                    key = new ECDHPublicBcpgKey(eCPublicKeyParameters.PublicKeyParamSet, eCPublicKeyParameters.Q, HashAlgorithmTag.Sha256, SymmetricKeyAlgorithmTag.Aes128);
                    break;

                case PublicKeyAlgorithmTag.ECDsa:
                    key = new ECDsaPublicBcpgKey(eCPublicKeyParameters.PublicKeyParamSet, eCPublicKeyParameters.Q);
                    break;

                default:
                    throw new PgpException("unknown EC algorithm");
                }
            }
            else
            {
                if (!(pubKey is ElGamalPublicKeyParameters))
                {
                    throw new PgpException("unknown key class");
                }
                ElGamalPublicKeyParameters elGamalPublicKeyParameters = (ElGamalPublicKeyParameters)pubKey;
                ElGamalParameters          parameters2 = elGamalPublicKeyParameters.Parameters;
                key = new ElGamalPublicBcpgKey(parameters2.P, parameters2.G, elGamalPublicKeyParameters.Y);
            }
            publicPk = new PublicKeyPacket(algorithm, time, key);
            ids      = Platform.CreateArrayList();
            idSigs   = Platform.CreateArrayList();
            try
            {
                Init();
            }
            catch (IOException val)
            {
                IOException exception = val;
                throw new PgpException("exception calculating keyId", (global::System.Exception)(object) exception);
            }
        }
Пример #10
0
        internal PgpPrivateKey DoExtractPrivateKey(byte[] rawPassPhrase, bool clearPassPhrase)
        {
            //IL_0021: Unknown result type (might be due to invalid IL or missing references)
            //IL_002b: Expected O, but got Unknown
            if (IsPrivateKeyEmpty)
            {
                return(null);
            }
            PublicKeyPacket publicKeyPacket = secret.PublicKeyPacket;

            try
            {
                byte[]                 array  = ExtractKeyData(rawPassPhrase, clearPassPhrase);
                BcpgInputStream        bcpgIn = BcpgInputStream.Wrap((Stream) new MemoryStream(array, false));
                AsymmetricKeyParameter privateKey;
                switch (publicKeyPacket.Algorithm)
                {
                case PublicKeyAlgorithmTag.RsaGeneral:
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaSign:
                {
                    RsaPublicBcpgKey           rsaPublicBcpgKey           = (RsaPublicBcpgKey)publicKeyPacket.Key;
                    RsaSecretBcpgKey           rsaSecretBcpgKey           = new RsaSecretBcpgKey(bcpgIn);
                    RsaPrivateCrtKeyParameters rsaPrivateCrtKeyParameters = new RsaPrivateCrtKeyParameters(rsaSecretBcpgKey.Modulus, rsaPublicBcpgKey.PublicExponent, rsaSecretBcpgKey.PrivateExponent, rsaSecretBcpgKey.PrimeP, rsaSecretBcpgKey.PrimeQ, rsaSecretBcpgKey.PrimeExponentP, rsaSecretBcpgKey.PrimeExponentQ, rsaSecretBcpgKey.CrtCoefficient);
                    privateKey = rsaPrivateCrtKeyParameters;
                    break;
                }

                case PublicKeyAlgorithmTag.Dsa:
                {
                    DsaPublicBcpgKey dsaPublicBcpgKey = (DsaPublicBcpgKey)publicKeyPacket.Key;
                    DsaSecretBcpgKey dsaSecretBcpgKey = new DsaSecretBcpgKey(bcpgIn);
                    DsaParameters    parameters2      = new DsaParameters(dsaPublicBcpgKey.P, dsaPublicBcpgKey.Q, dsaPublicBcpgKey.G);
                    privateKey = new DsaPrivateKeyParameters(dsaSecretBcpgKey.X, parameters2);
                    break;
                }

                case PublicKeyAlgorithmTag.EC:
                    privateKey = GetECKey("ECDH", bcpgIn);
                    break;

                case PublicKeyAlgorithmTag.ECDsa:
                    privateKey = GetECKey("ECDSA", bcpgIn);
                    break;

                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                {
                    ElGamalPublicBcpgKey elGamalPublicBcpgKey = (ElGamalPublicBcpgKey)publicKeyPacket.Key;
                    ElGamalSecretBcpgKey elGamalSecretBcpgKey = new ElGamalSecretBcpgKey(bcpgIn);
                    ElGamalParameters    parameters           = new ElGamalParameters(elGamalPublicBcpgKey.P, elGamalPublicBcpgKey.G);
                    privateKey = new ElGamalPrivateKeyParameters(elGamalSecretBcpgKey.X, parameters);
                    break;
                }

                default:
                    throw new PgpException("unknown public key algorithm encountered");
                }
                return(new PgpPrivateKey(KeyId, publicKeyPacket, privateKey));
            }
            catch (PgpException ex)
            {
                throw ex;
            }
            catch (global::System.Exception exception)
            {
                throw new PgpException("Exception constructing key", exception);
            }
        }