예제 #1
0
        internal static PgpPublicKey ReadSubkey(BcpgInputStream bcpgInput)
        {
            PublicKeyPacket pk     = (PublicKeyPacket)bcpgInput.ReadPacket();
            TrustPacket     kTrust = ReadOptionalTrustPacket(bcpgInput);

            // PGP 8 actually leaves out the signature.
            IList sigList = ReadSignaturesAndTrust(bcpgInput);

            return(new PgpPublicKey(pk, kTrust, sigList));
        }
예제 #2
0
 /// <summary>Copy constructor - subkey.</summary>
 private PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket trust,
     ArrayList subSigs,
     PgpPublicKey pub)
 {
     this.secret  = secret;
     this.trust   = trust;
     this.subSigs = subSigs;
     this.pub     = pub;
 }
예제 #3
0
        internal PgpSignature(
            SignaturePacket	sigPacket,
            TrustPacket		trustPacket)
        {
			if (sigPacket == null)
				throw new ArgumentNullException("sigPacket");

			this.sigPck = sigPacket;
			this.signatureType = sigPck.SignatureType;
			this.trustPck = trustPacket;
        }
예제 #4
0
        /// <summary>Constructor for a sub-key.</summary>
        internal PgpPublicKey(
            PublicKeyPacket publicPk,
            TrustPacket trustPk,
            IList sigs)
        {
            this.publicPk = publicPk;
            this.trustPk  = trustPk;
            this.subSigs  = sigs;

            Init();
        }
예제 #5
0
 internal PgpSignature(SignaturePacket sigPacket, TrustPacket trustPacket)
 {
     //IL_000e: Unknown result type (might be due to invalid IL or missing references)
     if (sigPacket == null)
     {
         throw new ArgumentNullException("sigPacket");
     }
     sigPck        = sigPacket;
     signatureType = sigPck.SignatureType;
     trustPck      = trustPacket;
 }
예제 #6
0
 internal PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket trust,
     IDigest sha,
     ArrayList subSigs)
 {
     this.secret  = secret;
     this.trust   = trust;
     this.subSigs = subSigs;
     this.pub     = new PgpPublicKey(secret.PublicKeyPacket, trust, subSigs);
 }
예제 #7
0
        /// <summary>Constructor for a sub-key.</summary>
        internal PgpPublicKey(
            PublicKeyPacket publicPk,
            TrustPacket trustPk,
            IList <PgpSignature> sigs,
            IKeyFingerPrintCalculator fingerPrintCalculator)
        {
            this.publicPk = publicPk;
            this.trustPk  = trustPk;
            this.subSigs  = sigs;

            Init(fingerPrintCalculator);
        }
예제 #8
0
        internal PgpSignature(
            SignaturePacket sigPacket,
            TrustPacket trustPacket)
        {
            if (sigPacket == null)
            {
                throw new ArgumentNullException("sigPacket");
            }

            this.sigPck        = sigPacket;
            this.signatureType = sigPck.SignatureType;
            this.trustPck      = trustPacket;
        }
예제 #9
0
        internal PgpPublicKey(
            PgpPublicKey key,
            TrustPacket trust,
            IList subSigs)
        {
            this.publicPk = key.publicPk;
            this.trustPk  = trust;
            this.subSigs  = subSigs;

            this.fingerprint = key.fingerprint;
            this.keyId       = key.keyId;
            this.keyStrength = key.keyStrength;
        }
예제 #10
0
        public PgpSecretKey(
            int certificationLevel,
            PgpKeyPair keyPair,
            string id,
            SymmetricKeyAlgorithmTag encAlgorithm,
            char[]                                          passPhrase,
            bool useSHA1,
            PgpSignatureSubpacketVector hashedPackets,
            PgpSignatureSubpacketVector unhashedPackets,
            SecureRandom rand)
            : this(keyPair, encAlgorithm, passPhrase, useSHA1, rand)
        {
            try
            {
                this.trust = null;
                this.ids   = new ArrayList();
                ids.Add(id);

                this.idTrusts = new ArrayList();
                idTrusts.Add(null);

                this.idSigs = new ArrayList();

                PgpSignatureGenerator sGen = new PgpSignatureGenerator(
                    keyPair.PublicKey.Algorithm, HashAlgorithmTag.Sha1);

                //
                // Generate the certification
                //
                sGen.InitSign(certificationLevel, keyPair.PrivateKey);

                sGen.SetHashedSubpackets(hashedPackets);
                sGen.SetUnhashedSubpackets(unhashedPackets);

                PgpSignature certification = sGen.GenerateCertification(id, keyPair.PublicKey);
                this.pub = PgpPublicKey.AddCertification(keyPair.PublicKey, id, certification);

                ArrayList sigList = new ArrayList();
                sigList.Add(certification);
                idSigs.Add(sigList);
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception encrypting key", e);
            }
        }
예제 #11
0
        public PgpSecretKey(
			int							certificationLevel,
			PgpKeyPair					keyPair,
			string						id,
			SymmetricKeyAlgorithmTag	encAlgorithm,
			char[]						passPhrase,
			bool						useSHA1,
			PgpSignatureSubpacketVector	hashedPackets,
			PgpSignatureSubpacketVector	unhashedPackets,
			SecureRandom				rand)
            : this(keyPair, encAlgorithm, passPhrase, useSHA1, rand)
        {
            try
            {
                this.trust = null;
                this.ids = new ArrayList();
                ids.Add(id);

                this.idTrusts = new ArrayList();
                idTrusts.Add(null);

                this.idSigs = new ArrayList();

                PgpSignatureGenerator sGen = new PgpSignatureGenerator(
                    keyPair.PublicKey.Algorithm, HashAlgorithmTag.Sha1);

                //
                // Generate the certification
                //
                sGen.InitSign(certificationLevel, keyPair.PrivateKey);

                sGen.SetHashedSubpackets(hashedPackets);
                sGen.SetUnhashedSubpackets(unhashedPackets);

                PgpSignature certification = sGen.GenerateCertification(id, keyPair.PublicKey);
                this.pub = PgpPublicKey.AddCertification(keyPair.PublicKey, id, certification);

                ArrayList sigList = new ArrayList();
                sigList.Add(certification);
                idSigs.Add(sigList);
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception encrypting key", e);
            }
        }
예제 #12
0
        }        //IL_0002: Unknown result type (might be due to invalid IL or missing references)

        //IL_000c: Expected O, but got Unknown


        public PgpSecretKeyRing(Stream inputStream)
        {
            //IL_004b: Unknown result type (might be due to invalid IL or missing references)
            keys         = Platform.CreateArrayList();
            extraPubKeys = Platform.CreateArrayList();
            BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
            PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

            if (packetTag != PacketTag.SecretKey && packetTag != PacketTag.SecretSubkey)
            {
                int num = (int)packetTag;
                throw new IOException("secret key ring doesn't start with secret key tag: tag 0x" + num.ToString("X"));
            }
            SecretKeyPacket secretKeyPacket = (SecretKeyPacket)bcpgInputStream.ReadPacket();

            while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
            {
                bcpgInputStream.ReadPacket();
            }
            TrustPacket trustPk = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);

            global::System.Collections.IList keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
            PgpKeyRing.ReadUserIDs(bcpgInputStream, out var ids, out var idTrusts, out var idSigs);
            keys.Add((object)new PgpSecretKey(secretKeyPacket, new PgpPublicKey(secretKeyPacket.PublicKeyPacket, trustPk, keySigs, ids, idTrusts, idSigs)));
            while (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey || bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
            {
                if (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey)
                {
                    SecretSubkeyPacket secretSubkeyPacket = (SecretSubkeyPacket)bcpgInputStream.ReadPacket();
                    while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
                    {
                        bcpgInputStream.ReadPacket();
                    }
                    TrustPacket trustPk2 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                    global::System.Collections.IList sigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                    keys.Add((object)new PgpSecretKey(secretSubkeyPacket, new PgpPublicKey(secretSubkeyPacket.PublicKeyPacket, trustPk2, sigs)));
                }
                else
                {
                    PublicSubkeyPacket publicPk            = (PublicSubkeyPacket)bcpgInputStream.ReadPacket();
                    TrustPacket        trustPk3            = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                    global::System.Collections.IList sigs2 = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                    extraPubKeys.Add((object)new PgpPublicKey(publicPk, trustPk3, sigs2));
                }
            }
        }
예제 #13
0
 internal PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket trust,
     IDigest sha,
     ArrayList keySigs,
     ArrayList ids,
     ArrayList idTrusts,
     ArrayList idSigs)
 {
     this.secret   = secret;
     this.trust    = trust;
     this.keySigs  = keySigs;
     this.ids      = ids;
     this.idTrusts = idTrusts;
     this.idSigs   = idSigs;
     this.pub      = new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs);
 }
예제 #14
0
 /// <summary>Copy constructor - master key.</summary>
 private PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket trust,
     ArrayList keySigs,
     ArrayList ids,
     ArrayList idTrusts,
     ArrayList idSigs,
     PgpPublicKey pub)
 {
     this.secret   = secret;
     this.trust    = trust;
     this.keySigs  = keySigs;
     this.ids      = ids;
     this.idTrusts = idTrusts;
     this.idSigs   = idSigs;
     this.pub      = pub;
 }
예제 #15
0
        internal PgpPublicKey(
            PublicKeyPacket publicPk,
            TrustPacket trustPk,
            IList keySigs,
            IList ids,
            IList idTrusts,
            IList idSigs)
        {
            this.publicPk = publicPk;
            this.trustPk  = trustPk;
            this.keySigs  = keySigs;
            this.ids      = ids;
            this.idTrusts = idTrusts;
            this.idSigs   = idSigs;

            Init();
        }
    public PgpSecretKeyRing(Stream inputStream)
    {
        keys         = Platform.CreateArrayList();
        extraPubKeys = Platform.CreateArrayList();
        BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
        PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

        if (packetTag != PacketTag.SecretKey && packetTag != PacketTag.SecretSubkey)
        {
            int num = (int)packetTag;
            throw new IOException("secret key ring doesn't start with secret key tag: tag 0x" + num.ToString("X"));
        }
        SecretKeyPacket secretKeyPacket = (SecretKeyPacket)bcpgInputStream.ReadPacket();

        while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
        {
            bcpgInputStream.ReadPacket();
        }
        TrustPacket trustPk = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
        IList       keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);

        PgpKeyRing.ReadUserIDs(bcpgInputStream, out IList ids, out IList idTrusts, out IList idSigs);
        keys.Add(new PgpSecretKey(secretKeyPacket, new PgpPublicKey(secretKeyPacket.PublicKeyPacket, trustPk, keySigs, ids, idTrusts, idSigs)));
        while (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey || bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
        {
            if (bcpgInputStream.NextPacketTag() == PacketTag.SecretSubkey)
            {
                SecretSubkeyPacket secretSubkeyPacket = (SecretSubkeyPacket)bcpgInputStream.ReadPacket();
                while (bcpgInputStream.NextPacketTag() == PacketTag.Experimental2)
                {
                    bcpgInputStream.ReadPacket();
                }
                TrustPacket trustPk2 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                IList       sigs     = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                keys.Add(new PgpSecretKey(secretSubkeyPacket, new PgpPublicKey(secretSubkeyPacket.PublicKeyPacket, trustPk2, sigs)));
            }
            else
            {
                PublicSubkeyPacket publicPk = (PublicSubkeyPacket)bcpgInputStream.ReadPacket();
                TrustPacket        trustPk3 = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
                IList sigs2 = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
                extraPubKeys.Add(new PgpPublicKey(publicPk, trustPk3, sigs2));
            }
        }
    }
예제 #17
0
        internal PgpPublicKey(
            PublicKeyPacket publicPk,
            TrustPacket trustPk,
            IList <PgpSignature> keySigs,
            IList ids,
            IList idTrusts,
            IList <List <PgpSignature> > idSigs,
            IKeyFingerPrintCalculator fingerPrintCalculator)
        {
            this.publicPk = publicPk;
            this.trustPk  = trustPk;
            this.keySigs  = keySigs;
            this.ids      = ids;
            this.idTrusts = idTrusts;
            this.idSigs   = idSigs;

            Init(fingerPrintCalculator);
        }
예제 #18
0
 internal static IList ReadSignaturesAndTrust(BcpgInputStream bcpgInput)
 {
     try
     {
         IList list = Platform.CreateArrayList();
         while (bcpgInput.NextPacketTag() == PacketTag.Signature)
         {
             SignaturePacket sigPacket   = (SignaturePacket)bcpgInput.ReadPacket();
             TrustPacket     trustPacket = ReadOptionalTrustPacket(bcpgInput);
             list.Add(new PgpSignature(sigPacket, trustPacket));
         }
         return(list);
     }
     catch (PgpException ex)
     {
         throw new IOException("can't create signature object: " + ex.Message, ex);
     }
 }
예제 #19
0
 internal static global::System.Collections.IList ReadSignaturesAndTrust(BcpgInputStream bcpgInput)
 {
     //IL_0049: Unknown result type (might be due to invalid IL or missing references)
     try
     {
         global::System.Collections.IList list = Platform.CreateArrayList();
         while (bcpgInput.NextPacketTag() == PacketTag.Signature)
         {
             SignaturePacket sigPacket   = (SignaturePacket)bcpgInput.ReadPacket();
             TrustPacket     trustPacket = ReadOptionalTrustPacket(bcpgInput);
             list.Add((object)new PgpSignature(sigPacket, trustPacket));
         }
         return(list);
     }
     catch (PgpException ex)
     {
         throw new IOException("can't create signature object: " + ((global::System.Exception)ex).get_Message(), (global::System.Exception)ex);
     }
 }
예제 #20
0
        public PgpPublicKeyRing(
            Stream inputStream)
        {
            this.keys = new ArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

            PacketTag initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.PublicKey && initialTag != PacketTag.PublicSubkey)
            {
                throw new IOException("public key ring doesn't start with public key tag: "
                                      + "tag 0x" + ((int)initialTag).ToString("X"));
            }

            PublicKeyPacket pubPk   = (PublicKeyPacket)bcpgInput.ReadPacket();;
            TrustPacket     trustPk = ReadOptionalTrustPacket(bcpgInput);

            // direct signatures and revocations
            ArrayList keySigs = ReadSignaturesAndTrust(bcpgInput);

            ArrayList ids, idTrusts, idSigs;

            ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs);

            keys.Add(new PgpPublicKey(pubPk, trustPk, keySigs, ids, idTrusts, idSigs));


            // Read subkeys
            while (bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                PublicKeyPacket pk     = (PublicKeyPacket)bcpgInput.ReadPacket();
                TrustPacket     kTrust = ReadOptionalTrustPacket(bcpgInput);

                // PGP 8 actually leaves out the signature.
                ArrayList sigList = ReadSignaturesAndTrust(bcpgInput);

                keys.Add(new PgpPublicKey(pk, kTrust, sigList));
            }
        }
    public PgpPublicKeyRing(Stream inputStream)
    {
        keys = Platform.CreateArrayList();
        BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
        PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

        if (packetTag != PacketTag.PublicKey && packetTag != PacketTag.PublicSubkey)
        {
            int num = (int)packetTag;
            throw new IOException("public key ring doesn't start with public key tag: tag 0x" + num.ToString("X"));
        }
        PublicKeyPacket publicPk = (PublicKeyPacket)bcpgInputStream.ReadPacket();
        TrustPacket     trustPk  = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);
        IList           keySigs  = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);

        PgpKeyRing.ReadUserIDs(bcpgInputStream, out IList ids, out IList idTrusts, out IList idSigs);
        keys.Add(new PgpPublicKey(publicPk, trustPk, keySigs, ids, idTrusts, idSigs));
        while (bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
        {
            keys.Add(ReadSubkey(bcpgInputStream));
        }
    }
예제 #22
0
        /// <summary>Create a subkey</summary>
        internal PgpSecretKey(
            PgpKeyPair keyPair,
            TrustPacket trust,
            ArrayList subSigs,
            SymmetricKeyAlgorithmTag encAlgorithm,
            char[]                                              passPhrase,
            bool useSHA1,
            SecureRandom rand)
            : this(keyPair, encAlgorithm, passPhrase, useSHA1, rand)
        {
            this.secret = new SecretSubkeyPacket(
                secret.PublicKeyPacket,
                secret.EncAlgorithm,
                secret.S2kUsage,
                secret.S2k,
                secret.GetIV(),
                secret.GetSecretKeyData());

            this.trust   = trust;
            this.subSigs = subSigs;
            this.pub     = new PgpPublicKey(keyPair.PublicKey, trust, subSigs);
        }
예제 #23
0
        public PgpPublicKeyRing(Stream inputStream)
        {
            //IL_0041: Unknown result type (might be due to invalid IL or missing references)
            keys = Platform.CreateArrayList();
            BcpgInputStream bcpgInputStream = BcpgInputStream.Wrap(inputStream);
            PacketTag       packetTag       = bcpgInputStream.NextPacketTag();

            if (packetTag != PacketTag.PublicKey && packetTag != PacketTag.PublicSubkey)
            {
                int num = (int)packetTag;
                throw new IOException("public key ring doesn't start with public key tag: tag 0x" + num.ToString("X"));
            }
            PublicKeyPacket publicPk = (PublicKeyPacket)bcpgInputStream.ReadPacket();
            TrustPacket     trustPk  = PgpKeyRing.ReadOptionalTrustPacket(bcpgInputStream);

            global::System.Collections.IList keySigs = PgpKeyRing.ReadSignaturesAndTrust(bcpgInputStream);
            PgpKeyRing.ReadUserIDs(bcpgInputStream, out var ids, out var idTrusts, out var idSigs);
            keys.Add((object)new PgpPublicKey(publicPk, trustPk, keySigs, ids, idTrusts, idSigs));
            while (bcpgInputStream.NextPacketTag() == PacketTag.PublicSubkey)
            {
                keys.Add((object)ReadSubkey(bcpgInputStream));
            }
        }
예제 #24
0
        internal PgpSecretKey(
            PgpKeyPair keyPair,
            SymmetricKeyAlgorithmTag encAlgorithm,
            char[]                                              passPhrase,
            bool useSHA1,
            SecureRandom rand)
        {
            PublicKeyPacket pubPk = keyPair.PublicKey.publicPk;

            BcpgObject secKey;

            switch (keyPair.PublicKey.Algorithm)
            {
            case PublicKeyAlgorithmTag.RsaEncrypt:
            case PublicKeyAlgorithmTag.RsaSign:
            case PublicKeyAlgorithmTag.RsaGeneral:
                RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters)keyPair.PrivateKey.Key;
                secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q);
                break;

            case PublicKeyAlgorithmTag.Dsa:
                DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters)keyPair.PrivateKey.Key;
                secKey = new DsaSecretBcpgKey(dsK.X);
                break;

            case PublicKeyAlgorithmTag.ElGamalEncrypt:
            case PublicKeyAlgorithmTag.ElGamalGeneral:
                ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters)keyPair.PrivateKey.Key;
                secKey = new ElGamalSecretBcpgKey(esK.X);
                break;

            default:
                throw new PgpException("unknown key class");
            }

            string cName = PgpUtilities.GetSymmetricCipherName(encAlgorithm);

            IBufferedCipher c = null;

            if (cName != null)
            {
                try
                {
                    c = CipherUtilities.GetCipher(cName + "/CFB/NoPadding");
                }
                catch (Exception e)
                {
                    throw new PgpException("Exception creating cipher", e);
                }
            }

            try
            {
                MemoryStream     bOut = new MemoryStream();
                BcpgOutputStream pOut = new BcpgOutputStream(bOut);

                pOut.WriteObject(secKey);

                byte[] keyData       = bOut.ToArray();
                byte[] checksumBytes = Checksum(useSHA1, keyData, keyData.Length);

                pOut.Write(checksumBytes);

                byte[] bOutData = bOut.ToArray();

                if (c != null)
                {
                    byte[] iv = new byte[8];
                    rand.NextBytes(iv);

                    S2k          s2k = new S2k(HashAlgorithmTag.Sha1, iv, 0x60);
                    KeyParameter key = PgpUtilities.MakeKeyFromPassPhrase(encAlgorithm, s2k, passPhrase);

                    iv = new byte[c.GetBlockSize()];
                    rand.NextBytes(iv);
                    c.Init(true, new ParametersWithIV(key, iv));

                    byte[] encData = c.DoFinal(bOutData);

                    int usage = useSHA1
                                                ?       SecretKeyPacket.UsageSha1
                                                :       SecretKeyPacket.UsageChecksum;

                    this.secret = new SecretKeyPacket(pubPk, encAlgorithm, usage, s2k, iv, encData);
                }
                else
                {
                    this.secret = new SecretKeyPacket(pubPk, encAlgorithm, null, null, bOutData);
                }

                this.trust = null;
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception encrypting key", e);
            }

            this.keySigs = new ArrayList();
        }
예제 #25
0
		internal PgpPublicKey(
            PublicKeyPacket	publicPk,
            TrustPacket		trustPk,
            IList		    keySigs,
            IList		    ids,
            IList           idTrusts,
            IList           idSigs)
        {
            this.publicPk = publicPk;
            this.trustPk = trustPk;
            this.keySigs = keySigs;
            this.ids = ids;
            this.idTrusts = idTrusts;
            this.idSigs = idSigs;

			Init();
        }
예제 #26
0
		internal PgpPublicKey(
            PgpPublicKey	key,
            TrustPacket		trust,
            IList           subSigs)
        {
            this.publicPk = key.publicPk;
            this.trustPk = trust;
            this.subSigs = subSigs;

			this.fingerprint = key.fingerprint;
            this.keyId = key.keyId;
            this.keyStrength = key.keyStrength;
        }
예제 #27
0
		/// <summary>Constructor for a sub-key.</summary>
        internal PgpPublicKey(
            PublicKeyPacket	publicPk,
            TrustPacket		trustPk,
            IList           sigs)
        {
            this.publicPk = publicPk;
            this.trustPk = trustPk;
            this.subSigs = sigs;

			Init();
        }
        public PgpSecretKeyRing(
            Stream inputStream)
        {
            this.keys = new ArrayList();

            BcpgInputStream bcpgInput  = BcpgInputStream.Wrap(inputStream);
            PacketTag       initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.SecretKey && initialTag != PacketTag.SecretSubkey)
            {
                throw new IOException(
                          "secret key ring doesn't start with secret key tag: " +
                          "tag 0x" + initialTag.ToString("X"));
            }

            SecretKeyPacket secret   = (SecretKeyPacket)bcpgInput.ReadPacket();
            TrustPacket     trust    = null;
            ArrayList       keySigs  = new ArrayList();
            ArrayList       ids      = new ArrayList();
            ArrayList       idTrusts = new ArrayList();
            ArrayList       idSigs   = new ArrayList();
            IDigest         sha;

            try
            {
                sha = DigestUtilities.GetDigest("SHA1");
            }
            catch (Exception)
            {
                throw new IOException("can't find SHA1 digest");
            }

            //
            // ignore GPG comment packets if found.
            //
            while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
            {
                bcpgInput.ReadPacket();
            }

            if (bcpgInput.NextPacketTag() == PacketTag.Trust)
            {
                trust = (TrustPacket)bcpgInput.ReadPacket();  // ignore for the moment
            }

            //
            // revocation and direct signatures
            //
            while (bcpgInput.NextPacketTag() == PacketTag.Signature)
            {
                try
                {
                    keySigs.Add(new PgpSignature(bcpgInput));
                }
                catch (PgpException e)
                {
                    throw new IOException("can't create signature object: " + e.Message + ", cause: " + e.InnerException.ToString());
                }
            }

            while (bcpgInput.NextPacketTag() == PacketTag.UserId ||
                   bcpgInput.NextPacketTag() == PacketTag.UserAttribute)
            {
                object    obj     = bcpgInput.ReadPacket();
                ArrayList sigList = new ArrayList();

                if (obj is UserIdPacket)
                {
                    UserIdPacket id = (UserIdPacket)obj;
                    ids.Add(id.GetId());
                }
                else
                {
                    UserAttributePacket user = (UserAttributePacket)obj;
                    ids.Add(new PgpUserAttributeSubpacketVector(user.GetSubpackets()));
                }

                if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                {
                    idTrusts.Add(bcpgInput.ReadPacket());
                }
                else
                {
                    idTrusts.Add(null);
                }

                idSigs.Add(sigList);

                while (bcpgInput.NextPacketTag() == PacketTag.Signature)
                {
                    SignaturePacket s = (SignaturePacket)bcpgInput.ReadPacket();

                    if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                    {
                        sigList.Add(new PgpSignature(s, (TrustPacket)bcpgInput.ReadPacket()));
                    }
                    else
                    {
                        sigList.Add(new PgpSignature(s));
                    }
                }
            }

            keys.Add(new PgpSecretKey(secret, trust, sha, keySigs, ids, idTrusts, idSigs));

            while (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey)
            {
                SecretSubkeyPacket sub      = (SecretSubkeyPacket)bcpgInput.ReadPacket();
                TrustPacket        subTrust = null;
                ArrayList          sigList  = new ArrayList();

                //
                // ignore GPG comment packets if found.
                //
                while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
                {
                    bcpgInput.ReadPacket();
                }

                if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                {
                    subTrust = (TrustPacket)bcpgInput.ReadPacket();
                }

                while (bcpgInput.NextPacketTag() == PacketTag.Signature)
                {
                    SignaturePacket s = (SignaturePacket)bcpgInput.ReadPacket();

                    if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                    {
                        sigList.Add(new PgpSignature(s, (TrustPacket)bcpgInput.ReadPacket()));
                    }
                    else
                    {
                        sigList.Add(new PgpSignature(s));
                    }
                }

                keys.Add(new PgpSecretKey(sub, subTrust, sha, sigList));
            }
        }
예제 #29
0
        /// <summary>Create a subkey</summary>
        internal PgpSecretKey(
            PgpKeyPair					keyPair,
            TrustPacket					trust,
            ArrayList					subSigs,
            SymmetricKeyAlgorithmTag	encAlgorithm,
            char[]						passPhrase,
			bool						useSHA1,
			SecureRandom				rand)
            : this(keyPair, encAlgorithm, passPhrase, useSHA1, rand)
        {
            this.secret = new SecretSubkeyPacket(
                secret.PublicKeyPacket,
                secret.EncAlgorithm,
                secret.S2kUsage,
                secret.S2k,
                secret.GetIV(),
                secret.GetSecretKeyData());

            this.trust = trust;
            this.subSigs = subSigs;
            this.pub = new PgpPublicKey(keyPair.PublicKey, trust, subSigs);
        }
예제 #30
0
        internal PgpSecretKey(
            PgpKeyPair					keyPair,
            SymmetricKeyAlgorithmTag	encAlgorithm,
            char[]						passPhrase,
			bool						useSHA1,
			SecureRandom				rand)
        {
            PublicKeyPacket pubPk = keyPair.PublicKey.publicPk;

            BcpgObject secKey;
            switch (keyPair.PublicKey.Algorithm)
            {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaSign:
                case PublicKeyAlgorithmTag.RsaGeneral:
                    RsaPrivateCrtKeyParameters rsK = (RsaPrivateCrtKeyParameters) keyPair.PrivateKey.Key;
                    secKey = new RsaSecretBcpgKey(rsK.Exponent, rsK.P, rsK.Q);
                    break;
                case PublicKeyAlgorithmTag.Dsa:
                    DsaPrivateKeyParameters dsK = (DsaPrivateKeyParameters) keyPair.PrivateKey.Key;
                    secKey = new DsaSecretBcpgKey(dsK.X);
                    break;
                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    ElGamalPrivateKeyParameters esK = (ElGamalPrivateKeyParameters) keyPair.PrivateKey.Key;
                    secKey = new ElGamalSecretBcpgKey(esK.X);
                    break;
                default:
                    throw new PgpException("unknown key class");
            }

            string cName = PgpUtilities.GetSymmetricCipherName(encAlgorithm);

            IBufferedCipher c = null;
            if (cName != null)
            {
                try
                {
                    c = CipherUtilities.GetCipher(cName + "/CFB/NoPadding");
                }
                catch (Exception e)
                {
                    throw new PgpException("Exception creating cipher", e);
                }
            }

            try
            {
                MemoryStream bOut = new MemoryStream();
                BcpgOutputStream pOut = new BcpgOutputStream(bOut);

                pOut.WriteObject(secKey);

                byte[] keyData = bOut.ToArray();
                byte[] checksumBytes = Checksum(useSHA1, keyData, keyData.Length);

                pOut.Write(checksumBytes);

                byte[] bOutData = bOut.ToArray();

                if (c != null)
                {
                    byte[] iv = new byte[8];
                    rand.NextBytes(iv);

                    S2k s2k = new S2k(HashAlgorithmTag.Sha1, iv, 0x60);
                    KeyParameter key = PgpUtilities.MakeKeyFromPassPhrase(encAlgorithm, s2k, passPhrase);

                    iv = new byte[c.GetBlockSize()];
                    rand.NextBytes(iv);
                    c.Init(true, new ParametersWithIV(key, iv));

                    byte[] encData = c.DoFinal(bOutData);

                    int usage = useSHA1
                        ?	SecretKeyPacket.UsageSha1
                        :	SecretKeyPacket.UsageChecksum;

                    this.secret = new SecretKeyPacket(pubPk, encAlgorithm, usage, s2k, iv, encData);
                }
                else
                {
                    this.secret = new SecretKeyPacket(pubPk, encAlgorithm, null, null, bOutData);
                }

                this.trust = null;
            }
            catch (PgpException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new PgpException("Exception encrypting key", e);
            }

            this.keySigs = new ArrayList();
        }
예제 #31
0
 internal PgpSecretKey(
     SecretKeyPacket	secret,
     TrustPacket		trust,
     ArrayList		keySigs,
     ArrayList		ids,
     ArrayList		idTrusts,
     ArrayList		idSigs)
 {
     this.secret = secret;
     this.trust = trust;
     this.keySigs = keySigs;
     this.ids = ids;
     this.idTrusts = idTrusts;
     this.idSigs = idSigs;
     this.pub = new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs);
 }
예제 #32
0
        public PgpPublicKeyRing(
            Stream inputStream)
        {
            this.keys = new ArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

            PacketTag initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.PublicKey && initialTag != PacketTag.PublicSubkey)
            {
                throw new IOException(
                          "public key ring doesn't start with public key tag: " +
                          "tag 0x" + initialTag.ToString("X"));
            }

            PublicKeyPacket pubPk;
            TrustPacket     trustPk;
            ArrayList       keySigs = new ArrayList();
            ArrayList       ids     = new ArrayList();
            ArrayList       idTrust = new ArrayList();
            ArrayList       idSigs  = new ArrayList();

            pubPk   = (PublicKeyPacket)bcpgInput.ReadPacket();
            trustPk = null;
            if (bcpgInput.NextPacketTag() == PacketTag.Trust)
            {
                trustPk = (TrustPacket)bcpgInput.ReadPacket();
            }

            //
            // direct signatures and revocations
            //
            while (bcpgInput.NextPacketTag() == PacketTag.Signature)
            {
                try
                {
                    SignaturePacket s = (SignaturePacket)bcpgInput.ReadPacket();

                    if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                    {
                        keySigs.Add(new PgpSignature(s, (TrustPacket)bcpgInput.ReadPacket()));
                    }
                    else
                    {
                        keySigs.Add(new PgpSignature(s));
                    }
                }
                catch (PgpException e)
                {
                    throw new IOException("can't create signature object: " + e.Message + ", cause: " + e.InnerException.ToString());
                }
            }

            while (bcpgInput.NextPacketTag() == PacketTag.UserId ||
                   bcpgInput.NextPacketTag() == PacketTag.UserAttribute)
            {
                object obj = bcpgInput.ReadPacket();

                if (obj is UserIdPacket)
                {
                    UserIdPacket id = (UserIdPacket)obj;
                    ids.Add(id.GetId());
                }
                else
                {
                    UserAttributePacket user = (UserAttributePacket)obj;
                    ids.Add(new PgpUserAttributeSubpacketVector(user.GetSubpackets()));
                }

                if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                {
                    idTrust.Add(bcpgInput.ReadPacket());
                }
                else
                {
                    idTrust.Add(null);
                }

                ArrayList sigList = new ArrayList();

                idSigs.Add(sigList);

                while (bcpgInput.NextPacketTag() == PacketTag.Signature)
                {
                    try
                    {
                        SignaturePacket s    = (SignaturePacket)bcpgInput.ReadPacket();
                        PacketTag       next = bcpgInput.NextPacketTag();
                        TrustPacket     tp   = (next == PacketTag.Trust)
                            ?    (TrustPacket)bcpgInput.ReadPacket()
                            :    null;

                        PgpSignature pgpSig = new PgpSignature(s, tp);
                        sigList.Add(pgpSig);
                    }
                    catch (PgpException e)
                    {
                        throw new IOException("can't create signature object: " + e.Message + ", cause: " + e.InnerException.ToString());
                    }
                }
            }

            keys.Add(new PgpPublicKey(pubPk, trustPk, keySigs, ids, idTrust, idSigs));

            while (bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                PublicKeyPacket pk     = (PublicKeyPacket)bcpgInput.ReadPacket();
                TrustPacket     kTrust = null;

                if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                {
                    kTrust = (TrustPacket)bcpgInput.ReadPacket();
                }

                ArrayList sigList = new ArrayList();

                try
                {
                    //
                    // PGP 8 actually leaves out the signature.
                    //
                    while (bcpgInput.NextPacketTag() == PacketTag.Signature)
                    {
                        SignaturePacket s = (SignaturePacket)bcpgInput.ReadPacket();

                        if (bcpgInput.NextPacketTag() == PacketTag.Trust)
                        {
                            sigList.Add(new PgpSignature(s, (TrustPacket)bcpgInput.ReadPacket()));
                        }
                        else
                        {
                            sigList.Add(new PgpSignature(s));
                        }
                    }
                }
                catch (PgpException e)
                {
                    throw new IOException("can't create signature object: " + e.Message + ", cause: " + e.InnerException.ToString());
                }

                keys.Add(new PgpPublicKey(pk, kTrust, sigList));
            }
        }
예제 #33
0
 /// <summary>Copy constructor - master key.</summary>
 private PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket     trust,
     ArrayList       keySigs,
     ArrayList       ids,
     ArrayList       idTrusts,
     ArrayList       idSigs,
     PgpPublicKey    pub)
 {
     this.secret = secret;
     this.trust = trust;
     this.keySigs = keySigs;
     this.ids = ids;
     this.idTrusts = idTrusts;
     this.idSigs = idSigs;
     this.pub = pub;
 }
예제 #34
0
 internal PgpSecretKey(
     SecretKeyPacket	secret,
     TrustPacket		trust,
     ArrayList		subSigs)
 {
     this.secret = secret;
     this.trust = trust;
     this.subSigs = subSigs;
     this.pub = new PgpPublicKey(secret.PublicKeyPacket, trust, subSigs);
 }
예제 #35
0
        public PgpSecretKeyRing(
            Stream inputStream)
        {
            this.keys         = Platform.CreateArrayList();
            this.extraPubKeys = Platform.CreateArrayList();

            BcpgInputStream bcpgInput = BcpgInputStream.Wrap(inputStream);

            PacketTag initialTag = bcpgInput.NextPacketTag();

            if (initialTag != PacketTag.SecretKey && initialTag != PacketTag.SecretSubkey)
            {
                throw new IOException("secret key ring doesn't start with secret key tag: "
                                      + "tag 0x" + ((int)initialTag).ToString("X"));
            }

            SecretKeyPacket secret = (SecretKeyPacket)bcpgInput.ReadPacket();

            //
            // ignore GPG comment packets if found.
            //
            while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
            {
                bcpgInput.ReadPacket();
            }

            TrustPacket trust = ReadOptionalTrustPacket(bcpgInput);

            // revocation and direct signatures
            IList keySigs = ReadSignaturesAndTrust(bcpgInput);

            IList ids, idTrusts, idSigs;

            ReadUserIDs(bcpgInput, out ids, out idTrusts, out idSigs);

            keys.Add(new PgpSecretKey(secret, new PgpPublicKey(secret.PublicKeyPacket, trust, keySigs, ids, idTrusts, idSigs)));


            // Read subkeys
            while (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey ||
                   bcpgInput.NextPacketTag() == PacketTag.PublicSubkey)
            {
                if (bcpgInput.NextPacketTag() == PacketTag.SecretSubkey)
                {
                    SecretSubkeyPacket sub = (SecretSubkeyPacket)bcpgInput.ReadPacket();

                    //
                    // ignore GPG comment packets if found.
                    //
                    while (bcpgInput.NextPacketTag() == PacketTag.Experimental2)
                    {
                        bcpgInput.ReadPacket();
                    }

                    TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput);
                    IList       sigList  = ReadSignaturesAndTrust(bcpgInput);

                    keys.Add(new PgpSecretKey(sub, new PgpPublicKey(sub.PublicKeyPacket, subTrust, sigList)));
                }
                else
                {
                    PublicSubkeyPacket sub = (PublicSubkeyPacket)bcpgInput.ReadPacket();

                    TrustPacket subTrust = ReadOptionalTrustPacket(bcpgInput);
                    IList       sigList  = ReadSignaturesAndTrust(bcpgInput);

                    extraPubKeys.Add(new PgpPublicKey(sub, subTrust, sigList));
                }
            }
        }
예제 #36
0
 /// <summary>Copy constructor - subkey.</summary>
 private PgpSecretKey(
     SecretKeyPacket secret,
     TrustPacket     trust,
     ArrayList       subSigs,
     PgpPublicKey    pub)
 {
     this.secret = secret;
     this.trust = trust;
     this.subSigs = subSigs;
     this.pub = pub;
 }