internal PgpPublicKey(IPublicKeyPacket publicPk, IList ids, IList <IList <IPgpSignature> > idSigs)
 {
     _publicPk = publicPk;
     _ids      = ids;
     _idSigs   = idSigs;
     Init();
 }
        /// <summary>Constructor for a sub-key.</summary>
        internal PgpPublicKey(IPublicKeyPacket publicPk, ITrustPacket trustPk, IList <IPgpSignature> sigs)
        {
            _publicPk = publicPk;
            _trustPk  = trustPk;
            _subSigs  = sigs;

            Init();
        }
        /// <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);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SecretKeyPacket"/> class.
 /// </summary>
 /// <param name="pubKeyPacket">The pub key packet.</param>
 /// <param name="encAlgorithm">The enc algorithm.</param>
 /// <param name="s2KUsage">The s2 K usage.</param>
 /// <param name="s2K">The s2 K.</param>
 /// <param name="iv">The iv.</param>
 /// <param name="secKeyData">The sec key data.</param>
 public SecretKeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, int s2KUsage, S2k s2K, byte[] iv, byte[] secKeyData)
 {
     this.PublicKeyPacket = pubKeyPacket;
     this.EncAlgorithm    = encAlgorithm;
     this.S2KUsage        = s2KUsage;
     this.S2K             = s2K;
     _iv         = Arrays.Clone(iv);
     _secKeyData = secKeyData;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecretKeyPacket"/> class.
 /// </summary>
 /// <param name="pubKeyPacket">The pub key packet.</param>
 /// <param name="encAlgorithm">The enc algorithm.</param>
 /// <param name="s2KUsage">The s2 K usage.</param>
 /// <param name="s2K">The s2 K.</param>
 /// <param name="iv">The iv.</param>
 /// <param name="secKeyData">The sec key data.</param>
 public SecretKeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, int s2KUsage, S2k s2K, byte[] iv, byte[] secKeyData)
 {
     this.PublicKeyPacket = pubKeyPacket;
     this.EncAlgorithm = encAlgorithm;
     this.S2KUsage = s2KUsage;
     this.S2K = s2K;
     _iv = Arrays.Clone(iv);
     _secKeyData = secKeyData;
 }
        internal PgpPublicKey(IPgpPublicKey key, ITrustPacket trust, IList <IPgpSignature> subSigs)
        {
            _publicPk = key.PublicKeyPacket;
            _trustPk  = trust;
            _subSigs  = subSigs;

            _fingerprint = key.GetFingerprint();
            KeyId        = key.KeyId;
            BitStrength  = key.BitStrength;
        }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SecretKeyPacket"/> class.
        /// </summary>
        /// <param name="pubKeyPacket">The pub key packet.</param>
        /// <param name="encAlgorithm">The enc algorithm.</param>
        /// <param name="s2K">The s2 K.</param>
        /// <param name="iv">The iv.</param>
        /// <param name="secKeyData">The sec key data.</param>
        public SecretKeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, S2k s2K, byte[] iv, byte[] secKeyData)
        {
            this.PublicKeyPacket = pubKeyPacket;
            this.EncAlgorithm    = encAlgorithm;

            this.S2KUsage = encAlgorithm != SymmetricKeyAlgorithmTag.Null ? UsageChecksum : UsageNone;

            this.S2K    = s2K;
            _iv         = Arrays.Clone(iv);
            _secKeyData = secKeyData;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SecretKeyPacket"/> class.
        /// </summary>
        /// <param name="pubKeyPacket">The pub key packet.</param>
        /// <param name="encAlgorithm">The enc algorithm.</param>
        /// <param name="s2K">The s2 K.</param>
        /// <param name="iv">The iv.</param>
        /// <param name="secKeyData">The sec key data.</param>
        public SecretKeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, S2k s2K, byte[] iv, byte[] secKeyData)
        {
            this.PublicKeyPacket = pubKeyPacket;
            this.EncAlgorithm = encAlgorithm;

            this.S2KUsage = encAlgorithm != SymmetricKeyAlgorithmTag.Null ? UsageChecksum : UsageNone;

            this.S2K = s2K;
            _iv = Arrays.Clone(iv);
            _secKeyData = secKeyData;
        }
        internal PgpPublicKey(IPublicKeyPacket publicPk, ITrustPacket trustPk, IList <IPgpSignature> keySigs, IList ids, IList <ITrustPacket> idTrusts, IList <IList <IPgpSignature> > idSigs)
        {
            _publicPk = publicPk;
            _trustPk  = trustPk;
            _keySigs  = keySigs;
            _ids      = ids;
            _idTrusts = idTrusts;
            _idSigs   = idSigs;

            Init();
        }
Esempio n. 10
0
        /// <summary>Copy constructor.</summary>
        /// <param name="pubKey">The public key to copy.</param>
        internal PgpPublicKey(IPgpPublicKey pubKey)
        {
            _publicPk = pubKey.PublicKeyPacket;

            _keySigs  = Platform.CreateArrayList(pubKey.KeySigs);
            _ids      = Platform.CreateArrayList(pubKey.Ids);
            _idTrusts = Platform.CreateArrayList(pubKey.IdTrusts);
            _idSigs   = Platform.CreateArrayList(pubKey.IdSigs);

            if (pubKey.SubSigs != null)
            {
                _subSigs = Platform.CreateArrayList(pubKey.SubSigs);
            }

            _fingerprint = pubKey.GetFingerprint();
            KeyId        = pubKey.KeyId;
            BitStrength  = pubKey.BitStrength;
        }
Esempio n. 11
0
        private static byte[] BuildFingerprintSha1(IPublicKeyPacket publicPk)
        {
            var kBytes = publicPk.GetEncodedContents();

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

                digest.Update(0x99);
                digest.Update((byte)(kBytes.Length >> 8));
                digest.Update((byte)kBytes.Length);
                digest.BlockUpdate(kBytes, 0, kBytes.Length);
                return(DigestUtilities.DoFinal(digest));
            }
            catch (Exception e)
            {
                throw new IOException("can't find SHA1", e);
            }
        }
Esempio n. 12
0
        private static byte[] BuildFingerprintMd5(IPublicKeyPacket publicPk)
        {
            var rK = (RsaPublicBcpgKey)publicPk.Key;

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

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

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

                return(DigestUtilities.DoFinal(digest));
            }
            catch (Exception e)
            {
                throw new IOException("can't find MD5", 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 static byte[] BuildFingerprint(IPublicKeyPacket publicPk)
 {
     return publicPk.Version <= 3 ? BuildFingerprintMd5(publicPk) : BuildFingerprintSha1(publicPk);
 }
 internal PgpPublicKey(IPublicKeyPacket publicPk, IList ids, IList<IList<IPgpSignature>> idSigs)
 {
     _publicPk = publicPk;
     _ids = ids;
     _idSigs = idSigs;
     Init();
 }
        internal PgpPublicKey(IPublicKeyPacket publicPk, ITrustPacket trustPk, IList<IPgpSignature> keySigs, IList ids, IList<ITrustPacket> idTrusts, IList<IList<IPgpSignature>> idSigs)
        {
            _publicPk = publicPk;
            _trustPk = trustPk;
            _keySigs = keySigs;
            _ids = ids;
            _idTrusts = idTrusts;
            _idSigs = idSigs;

            Init();
        }
        /// <summary>Copy constructor.</summary>
        /// <param name="pubKey">The public key to copy.</param>
        internal PgpPublicKey(IPgpPublicKey pubKey)
        {
            _publicPk = pubKey.PublicKeyPacket;

            _keySigs = Platform.CreateArrayList(pubKey.KeySigs);
            _ids = Platform.CreateArrayList(pubKey.Ids);
            _idTrusts = Platform.CreateArrayList(pubKey.IdTrusts);
            _idSigs = Platform.CreateArrayList(pubKey.IdSigs);

            if (pubKey.SubSigs != null)
            {
                _subSigs = Platform.CreateArrayList(pubKey.SubSigs);
            }

            _fingerprint = pubKey.GetFingerprint();
            KeyId = pubKey.KeyId;
            BitStrength = pubKey.BitStrength;
        }
        internal PgpPublicKey(IPgpPublicKey key, ITrustPacket trust, IList<IPgpSignature> subSigs)
        {
            _publicPk = key.PublicKeyPacket;
            _trustPk = trust;
            _subSigs = subSigs;

            _fingerprint = key.GetFingerprint();
            KeyId = key.KeyId;
            BitStrength = key.BitStrength;
        }
        /// <summary>Constructor for a sub-key.</summary>
        internal PgpPublicKey(IPublicKeyPacket publicPk, ITrustPacket trustPk, IList<IPgpSignature> sigs)
        {
            _publicPk = publicPk;
            _trustPk = trustPk;
            _subSigs = sigs;

            Init();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecretSubkeyPacket"/> class.
 /// </summary>
 /// <param name="pubKeyPacket">The pub key packet.</param>
 /// <param name="encAlgorithm">The enc algorithm.</param>
 /// <param name="s2KUsage">The s2 K usage.</param>
 /// <param name="s2K">The s2 K.</param>
 /// <param name="iv">The iv.</param>
 /// <param name="secKeyData">The sec key data.</param>
 public SecretSubkeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, int s2KUsage, S2k s2K, byte[] iv, byte[] secKeyData)
     : base(pubKeyPacket, encAlgorithm, s2KUsage, s2K, iv, secKeyData)
 {
 }
        private static byte[] BuildFingerprintMd5(IPublicKeyPacket publicPk)
        {
            var rK = (RsaPublicBcpgKey)publicPk.Key;

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

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

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

                return DigestUtilities.DoFinal(digest);
            }
            catch (Exception e)
            {
                throw new IOException("can't find MD5", e);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecretSubkeyPacket"/> class.
 /// </summary>
 /// <param name="pubKeyPacket">The pub key packet.</param>
 /// <param name="encAlgorithm">The enc algorithm.</param>
 /// <param name="s2KUsage">The s2 K usage.</param>
 /// <param name="s2K">The s2 K.</param>
 /// <param name="iv">The iv.</param>
 /// <param name="secKeyData">The sec key data.</param>
 public SecretSubkeyPacket(IPublicKeyPacket pubKeyPacket, SymmetricKeyAlgorithmTag encAlgorithm, int s2KUsage, S2k s2K, byte[] iv, byte[] secKeyData)
     : base(pubKeyPacket, encAlgorithm, s2KUsage, s2K, iv, secKeyData)
 {
 }
        private static byte[] BuildFingerprintSha1(IPublicKeyPacket publicPk)
        {
            var kBytes = publicPk.GetEncodedContents();

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

                digest.Update(0x99);
                digest.Update((byte)(kBytes.Length >> 8));
                digest.Update((byte)kBytes.Length);
                digest.BlockUpdate(kBytes, 0, kBytes.Length);
                return DigestUtilities.DoFinal(digest);
            }
            catch (Exception e)
            {
                throw new IOException("can't find SHA1", e);
            }
        }
Esempio n. 24
0
 public static byte[] BuildFingerprint(IPublicKeyPacket publicPk)
 {
     return(publicPk.Version <= 3 ? BuildFingerprintMd5(publicPk) : BuildFingerprintSha1(publicPk));
 }