Exemplo n.º 1
0
		/// <summary>Construct a version 4 public subkey packet.</summary>
        public PublicSubkeyPacket(
            PublicKeyAlgorithmTag	algorithm,
            DateTime				time,
            IBcpgKey				key)
            : base(algorithm, time, key)
        {
        }
Exemplo n.º 2
0
 /// <summary>Construct a version 4 public subkey packet.</summary>
 public PublicSubkeyPacket(
     PublicKeyAlgorithmTag algorithm,
     DateTime time,
     IBcpgKey key)
     : base(algorithm, time, key)
 {
 }
Exemplo n.º 3
0
        internal PublicKeyPacket(
            BcpgInputStream bcpgIn)
        {
            version = bcpgIn.ReadByte();

            time = ((uint)bcpgIn.ReadByte() << 24) | ((uint)bcpgIn.ReadByte() << 16)
                | ((uint)bcpgIn.ReadByte() << 8) | (uint)bcpgIn.ReadByte();

            if (version <= 3)
            {
                validDays = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
            }

            algorithm = (PublicKeyAlgorithmTag) bcpgIn.ReadByte();

            switch ((PublicKeyAlgorithmTag) algorithm)
            {
                case PublicKeyAlgorithmTag.RsaEncrypt:
                case PublicKeyAlgorithmTag.RsaGeneral:
                case PublicKeyAlgorithmTag.RsaSign:
                    key = new RsaPublicBcpgKey(bcpgIn);
                    break;
                case PublicKeyAlgorithmTag.Dsa:
                    key = new DsaPublicBcpgKey(bcpgIn);
                    break;
                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    key = new ElGamalPublicBcpgKey(bcpgIn);
                    break;
                default:
                    throw new IOException("unknown PGP public key algorithm encountered");
            }
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
0
 public PublicKeyPacket(PublicKeyAlgorithmTag algorithm, global::System.DateTime time, IBcpgKey key)
 {
     version        = 4;
     this.time      = DateTimeUtilities.DateTimeToUnixMs(time) / 1000;
     this.algorithm = algorithm;
     this.key       = key;
 }
Exemplo n.º 6
0
        internal PublicKeyPacket(BcpgInputStream bcpgIn)
        {
            this.version = bcpgIn.ReadByte();
            this.time    = (long)((ulong)(bcpgIn.ReadByte() << 24 | bcpgIn.ReadByte() << 16 | bcpgIn.ReadByte() << 8 | bcpgIn.ReadByte()));
            if (this.version <= 3)
            {
                this.validDays = (bcpgIn.ReadByte() << 8 | bcpgIn.ReadByte());
            }
            this.algorithm = (PublicKeyAlgorithmTag)bcpgIn.ReadByte();
            PublicKeyAlgorithmTag publicKeyAlgorithmTag = this.algorithm;

            switch (publicKeyAlgorithmTag)
            {
            case PublicKeyAlgorithmTag.RsaGeneral:
            case PublicKeyAlgorithmTag.RsaEncrypt:
            case PublicKeyAlgorithmTag.RsaSign:
                this.key = new RsaPublicBcpgKey(bcpgIn);
                return;

            default:
                switch (publicKeyAlgorithmTag)
                {
                case PublicKeyAlgorithmTag.ElGamalEncrypt:
                case PublicKeyAlgorithmTag.ElGamalGeneral:
                    this.key = new ElGamalPublicBcpgKey(bcpgIn);
                    return;

                case PublicKeyAlgorithmTag.Dsa:
                    this.key = new DsaPublicBcpgKey(bcpgIn);
                    return;
                }
                throw new IOException("unknown PGP public key algorithm encountered");
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Create a PgpPrivateKey from a keyID, the associated public data packet, and a regular private key.
 /// </summary>
 /// <param name="keyID">ID of the corresponding public key.</param>
 /// <param name="publicKeyPacket">the public key data packet to be associated with this private key.</param>
 /// <param name="privateKey">the private key packet to be associated with this private key.</param>
 public PgpPrivateKey(
     long keyID,
     PublicKeyPacket publicKeyPacket,
     IBcpgKey privateKey)
 {
     this.keyID           = keyID;
     this.publicKeyPacket = publicKeyPacket;
     this.privateKey      = privateKey;
 }
Exemplo n.º 8
0
		/// <summary>Construct a version 4 public key packet.</summary>
        public PublicKeyPacket(
            PublicKeyAlgorithmTag	algorithm,
            DateTime				time,
            IBcpgKey				key)
        {
			this.version = 4;
            this.time = DateTimeUtilities.DateTimeToUnixMs(time) / 1000L;
            this.algorithm = algorithm;
            this.key = key;
        }
        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);
                }
            }
        }
Exemplo n.º 10
0
        internal PublicKeyPacket(
            BcpgInputStream bcpgIn)
        {
            version = bcpgIn.ReadByte();

            time = ((uint)bcpgIn.ReadByte() << 24) | ((uint)bcpgIn.ReadByte() << 16)
                   | ((uint)bcpgIn.ReadByte() << 8) | (uint)bcpgIn.ReadByte();

            if (version <= 3)
            {
                validDays = (bcpgIn.ReadByte() << 8) | bcpgIn.ReadByte();
            }

            algorithm = (PublicKeyAlgorithmTag)bcpgIn.ReadByte();

            switch ((PublicKeyAlgorithmTag)algorithm)
            {
            case PublicKeyAlgorithmTag.RsaEncrypt:
            case PublicKeyAlgorithmTag.RsaGeneral:
            case PublicKeyAlgorithmTag.RsaSign:
                key = new RsaPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.Dsa:
                key = new DsaPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.ElGamalEncrypt:
            case PublicKeyAlgorithmTag.ElGamalGeneral:
                key = new ElGamalPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.ECDH:
                key = new ECDHPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.ECDsa:
                key = new ECDsaPublicBcpgKey(bcpgIn);
                break;

            default:
                throw new IOException("unknown PGP public key algorithm encountered");
            }
        }
Exemplo 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;
                }
            }
        }
Exemplo 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));
        }
Exemplo n.º 13
0
        internal PublicKeyPacket(BcpgInputStream bcpgIn)
        {
            //IL_00e6: Unknown result type (might be due to invalid IL or missing references)
            version = ((Stream)bcpgIn).ReadByte();
            time    = (uint)((((Stream)bcpgIn).ReadByte() << 24) | (((Stream)bcpgIn).ReadByte() << 16) | (((Stream)bcpgIn).ReadByte() << 8) | ((Stream)bcpgIn).ReadByte());
            if (version <= 3)
            {
                validDays = (((Stream)bcpgIn).ReadByte() << 8) | ((Stream)bcpgIn).ReadByte();
            }
            algorithm = (PublicKeyAlgorithmTag)((Stream)bcpgIn).ReadByte();
            switch (algorithm)
            {
            case PublicKeyAlgorithmTag.RsaGeneral:
            case PublicKeyAlgorithmTag.RsaEncrypt:
            case PublicKeyAlgorithmTag.RsaSign:
                key = new RsaPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.Dsa:
                key = new DsaPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.ElGamalEncrypt:
            case PublicKeyAlgorithmTag.ElGamalGeneral:
                key = new ElGamalPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.EC:
                key = new ECDHPublicBcpgKey(bcpgIn);
                break;

            case PublicKeyAlgorithmTag.ECDsa:
                key = new ECDsaPublicBcpgKey(bcpgIn);
                break;

            default:
                throw new IOException("unknown PGP public key algorithm encountered");
            }
        }
Exemplo n.º 14
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));
        }
Exemplo 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;
                }
            }
        }
Exemplo n.º 16
0
 public PublicSubkeyPacket(PublicKeyAlgorithmTag algorithm, global::System.DateTime time, IBcpgKey key)
     : base(algorithm, time, key)
 {
 }