Ejemplo n.º 1
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");
            }
        }
Ejemplo n.º 2
0
        private void Init()
        {
            IBcpgKey key = publicPk.Key;

            fingerprint = CalculateFingerprint(publicPk);
            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rsaPublicBcpgKey = (RsaPublicBcpgKey)key;
                keyId       = rsaPublicBcpgKey.Modulus.LongValue;
                keyStrength = rsaPublicBcpgKey.Modulus.BitLength;
                return;
            }
            keyId = (long)(((ulong)fingerprint[fingerprint.Length - 8] << 56) | ((ulong)fingerprint[fingerprint.Length - 7] << 48) | ((ulong)fingerprint[fingerprint.Length - 6] << 40) | ((ulong)fingerprint[fingerprint.Length - 5] << 32) | ((ulong)fingerprint[fingerprint.Length - 4] << 24) | ((ulong)fingerprint[fingerprint.Length - 3] << 16) | ((ulong)fingerprint[fingerprint.Length - 2] << 8) | fingerprint[fingerprint.Length - 1]);
            if (key is RsaPublicBcpgKey)
            {
                keyStrength = ((RsaPublicBcpgKey)key).Modulus.BitLength;
            }
            else if (key is DsaPublicBcpgKey)
            {
                keyStrength = ((DsaPublicBcpgKey)key).P.BitLength;
            }
            else if (key is ElGamalPublicBcpgKey)
            {
                keyStrength = ((ElGamalPublicBcpgKey)key).P.BitLength;
            }
            else if (key is ECPublicBcpgKey)
            {
                keyStrength = ECKeyPairGenerator.FindECCurveByOid(((ECPublicBcpgKey)key).CurveOid).Curve.FieldSize;
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
            }
        }
        public byte[] CalculateFingerprint(PublicKeyPacket publicPk)
        {
            IBcpgKey key = publicPk.Key;

            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key;

                try
                {
                    // TODO: MD5 needs to go in the main API...
                    MD5Digest digest = new MD5Digest();

                    byte[] bytes = new MPInteger(rK.Modulus).GetEncoded();
                    digest.BlockUpdate(bytes, 2, bytes.Length - 2);

                    bytes = new MPInteger(rK.PublicExponent).GetEncoded();
                    digest.BlockUpdate(bytes, 2, bytes.Length - 2);

                    byte[] digBuf = new byte[digest.GetDigestSize()];

                    digest.DoFinal(digBuf, 0);

                    return(digBuf);
                }
                catch (IOException e)
                {
                    throw new PgpException("can't encode key components: " + e.Message, e);
                }
            }
            else
            {
                try
                {
                    byte[] kBytes = publicPk.GetEncodedContents();

                    IStreamCalculator <IBlockResult> hashCalc = CryptoServicesRegistrar.CreateService(FipsShs.Sha1).CreateCalculator();
                    Stream hStream = hashCalc.Stream;

                    hStream.WriteByte((byte)0x99);
                    hStream.WriteByte((byte)(kBytes.Length >> 8));
                    hStream.WriteByte((byte)kBytes.Length);
                    hStream.Write(kBytes, 0, kBytes.Length);

                    hStream.Close();

                    return(hashCalc.GetResult().Collect());
                }
                catch (IOException e)
                {
                    throw new PgpException("can't encode key components: " + e.Message, e);
                }
            }
        }
        public ISignatureWithDigestFactory <PgpSignatureIdentifier> Build(PgpPrivateKey privateKey)
        {
            RsaSecretBcpgKey rsaKey = privateKey.Key as RsaSecretBcpgKey;

            if (rsaKey != null)
            {
                RsaPublicBcpgKey pubKey = (RsaPublicBcpgKey)privateKey.PublicKeyPacket.Key;

                return(Build(privateKey.KeyId, new AsymmetricRsaPrivateKey(FipsRsa.Alg, rsaKey.Modulus, pubKey.PublicExponent, rsaKey.PrivateExponent, rsaKey.PrimeP, rsaKey.PrimeQ, rsaKey.PrimeExponentP, rsaKey.PrimeExponentQ, rsaKey.CrtCoefficient)));
            }

            throw new ArgumentException("unknown key algorithm");
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
0
        private void Init(IKeyFingerPrintCalculator fingerPrintCalculator)
        {
            IBcpgKey key = publicPk.Key;

            this.fingerprint = fingerPrintCalculator.CalculateFingerprint(publicPk);

            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key;

                this.keyId       = rK.Modulus.LongValue;
                this.keyStrength = rK.Modulus.BitLength;
            }
            else
            {
                this.keyId = ((long)(fingerprint[fingerprint.Length - 8] & 0xff) << 56)
                             | ((long)(fingerprint[fingerprint.Length - 7] & 0xff) << 48)
                             | ((long)(fingerprint[fingerprint.Length - 6] & 0xff) << 40)
                             | ((long)(fingerprint[fingerprint.Length - 5] & 0xff) << 32)
                             | ((long)(fingerprint[fingerprint.Length - 4] & 0xff) << 24)
                             | ((long)(fingerprint[fingerprint.Length - 3] & 0xff) << 16)
                             | ((long)(fingerprint[fingerprint.Length - 2] & 0xff) << 8)
                             | ((fingerprint[fingerprint.Length - 1] & 0xff));

                if (key is RsaPublicBcpgKey)
                {
                    this.keyStrength = ((RsaPublicBcpgKey)key).Modulus.BitLength;
                }
                else if (key is DsaPublicBcpgKey)
                {
                    this.keyStrength = ((DsaPublicBcpgKey)key).P.BitLength;
                }
                else if (key is ElGamalPublicBcpgKey)
                {
                    this.keyStrength = ((ElGamalPublicBcpgKey)key).P.BitLength;
                }
                else if (key is ECPublicBcpgKey)
                {
                    this.keyStrength = ECNamedCurveTable.GetByOid(((ECPublicBcpgKey)key).CurveOid).Curve.FieldSize;
                }
            }
        }
Ejemplo n.º 12
0
        public static byte[] CalculateFingerprint(PublicKeyPacket publicPk)
        {
            IBcpgKey key = publicPk.Key;
            IDigest  digest;

            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key;

                try
                {
                    digest = DigestUtilities.GetDigest("MD5");
                    UpdateDigest(digest, rK.Modulus);
                    UpdateDigest(digest, rK.PublicExponent);
                }
                catch (Exception e)
                {
                    throw new PgpException("can't encode key components: " + e.Message, e);
                }
            }
            else
            {
                try
                {
                    byte[] kBytes = publicPk.GetEncodedContents();

                    digest = DigestUtilities.GetDigest("SHA1");

                    digest.Update(0x99);
                    digest.Update((byte)(kBytes.Length >> 8));
                    digest.Update((byte)kBytes.Length);
                    digest.BlockUpdate(kBytes, 0, kBytes.Length);
                }
                catch (Exception e)
                {
                    throw new PgpException("can't encode key components: " + e.Message, e);
                }
            }

            return(DigestUtilities.DoFinal(digest));
        }
Ejemplo n.º 13
0
        public static byte[] CalculateFingerprint(PublicKeyPacket publicPk)
        {
            IBcpgKey key = publicPk.Key;
            IDigest  digest;

            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rsaPublicBcpgKey = (RsaPublicBcpgKey)key;
                try
                {
                    digest = DigestUtilities.GetDigest("MD5");
                    UpdateDigest(digest, rsaPublicBcpgKey.Modulus);
                    UpdateDigest(digest, rsaPublicBcpgKey.PublicExponent);
                }
                catch (global::System.Exception ex)
                {
                    throw new PgpException("can't encode key components: " + ex.get_Message(), ex);
                }
            }
            else
            {
                try
                {
                    byte[] encodedContents = publicPk.GetEncodedContents();
                    digest = DigestUtilities.GetDigest("SHA1");
                    digest.Update(153);
                    digest.Update((byte)(encodedContents.Length >> 8));
                    digest.Update((byte)encodedContents.Length);
                    digest.BlockUpdate(encodedContents, 0, encodedContents.Length);
                }
                catch (global::System.Exception ex2)
                {
                    throw new PgpException("can't encode key components: " + ex2.get_Message(), ex2);
                }
            }
            return(DigestUtilities.DoFinal(digest));
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
0
        private void Init()
        {
            IBcpgKey key = publicPk.Key;

            if (publicPk.Version <= 3)
            {
                RsaPublicBcpgKey rK = (RsaPublicBcpgKey)key;

                this.keyId = rK.Modulus.LongValue;

                try
                {
                    IDigest digest = DigestUtilities.GetDigest("MD5");

                    byte[] bytes = rK.Modulus.ToByteArrayUnsigned();
                    digest.BlockUpdate(bytes, 0, bytes.Length);

                    bytes = rK.PublicExponent.ToByteArrayUnsigned();
                    digest.BlockUpdate(bytes, 0, bytes.Length);

                    this.fingerprint = DigestUtilities.DoFinal(digest);
                }
                //catch (NoSuchAlgorithmException)
                catch (Exception e)
                {
                    throw new IOException("can't find MD5", e);
                }

                this.keyStrength = rK.Modulus.BitLength;
            }
            else
            {
                byte[] kBytes = publicPk.GetEncodedContents();

                try
                {
                    IDigest digest = DigestUtilities.GetDigest("SHA1");

                    digest.Update(0x99);
                    digest.Update((byte)(kBytes.Length >> 8));
                    digest.Update((byte)kBytes.Length);
                    digest.BlockUpdate(kBytes, 0, kBytes.Length);
                    this.fingerprint = DigestUtilities.DoFinal(digest);
                }
                catch (Exception e)
                {
                    throw new IOException("can't find SHA1", e);
                }

                this.keyId = (long)(((ulong)fingerprint[fingerprint.Length - 8] << 56)
                                    | ((ulong)fingerprint[fingerprint.Length - 7] << 48)
                                    | ((ulong)fingerprint[fingerprint.Length - 6] << 40)
                                    | ((ulong)fingerprint[fingerprint.Length - 5] << 32)
                                    | ((ulong)fingerprint[fingerprint.Length - 4] << 24)
                                    | ((ulong)fingerprint[fingerprint.Length - 3] << 16)
                                    | ((ulong)fingerprint[fingerprint.Length - 2] << 8)
                                    | (ulong)fingerprint[fingerprint.Length - 1]);

                if (key is RsaPublicBcpgKey)
                {
                    this.keyStrength = ((RsaPublicBcpgKey)key).Modulus.BitLength;
                }
                else if (key is DsaPublicBcpgKey)
                {
                    this.keyStrength = ((DsaPublicBcpgKey)key).P.BitLength;
                }
                else if (key is ElGamalPublicBcpgKey)
                {
                    this.keyStrength = ((ElGamalPublicBcpgKey)key).P.BitLength;
                }
            }
        }
Ejemplo n.º 16
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);
            }
        }
Ejemplo n.º 17
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);
            }
        }