private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secret_key_ring_bundle)
    {
        IEnumerable pgpKeyRings = secret_key_ring_bundle.GetKeyRings();



        return((from PgpSecretKeyRing kring in secret_key_ring_bundle.GetKeyRings()
                select kring.GetSecretKeys().Cast <PgpSecretKey>().
                LastOrDefault(k => k.IsSigningKey)).LastOrDefault(key => key != null));
    }
Esempio n. 2
0
        /// <summary>
        /// Reads secret key from given privateKey
        /// </summary>
        /// <param name="privateKeyFile">Path to private key file</param>
        /// <returns>PgpSecretKey of the given privateKey</returns>
        internal static PgpSecretKey ReadSecretKey(string privateKeyFile)
        {
            PgpSecretKey secretKey = null;

            using (Stream secretKeyStream = File.OpenRead(privateKeyFile))
            {
                var secretKeyRingBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretKeyStream));

                foreach (PgpSecretKeyRing keyRing in secretKeyRingBundle.GetKeyRings())
                {
                    foreach (PgpSecretKey key in keyRing.GetSecretKeys())
                    {
                        if (key.IsSigningKey)
                        {
                            secretKey = key;
                        }
                    }
                }

                if (secretKey == null)
                {
                    throw new Exception("Wrong private key - Can't find signing key in key ring.");
                }
            }

            return(secretKey);
        }
Esempio n. 3
0
        private PgpSecretKey GetsecretKeyByUserId(PgpSecretKeyRingBundle secretKeyRingBundle, String secretKeyUserId)
        {
            foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
            {
                PgpSecretKey key = kRing.GetSecretKeys()

                                   .Cast <PgpSecretKey>()

                                   .Where(k => k.IsSigningKey)

                                   .FirstOrDefault();


                if (key != null)
                {
                    foreach (String userId in key.UserIds)
                    {
                        if (userId.Contains(secretKeyUserId))
                        {
                            return(key);
                        }
                    }
                }
            }

            return(null);
        }
Esempio n. 4
0
        private PgpSecretKey readSecretKey(Stream privateKeyStream)
        {
            PgpSecretKeyRingBundle pgpSec;

            try
            {
                pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
            }
            catch (Exception e)
            {
                throw new Exception("Invalid private key stream, reason: " + e.Message);
            }

            var keyRings = pgpSec.GetKeyRings();

            foreach (PgpSecretKeyRing keyRing in keyRings)
            {
                foreach (PgpSecretKey key in keyRing.GetSecretKeys())
                {
                    if (key.UserIds.Cast <String>().Where(id => id == _userId).Count() > 0)
                    {
                        try
                        {
                            key.ExtractPrivateKey(_passPhrase.ToCharArray());
                            return(key);
                        }
                        catch { continue; }
                    }
                }
            }

            throw new Exception("Could not find a valid signing key");
        }
        /// <summary>
        /// Return the first key we can use to encrypt.
        /// Note: A file can contain multiple keys (stored in "key rings")
        /// </summary>
        private PgpSecretKey getFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
        {
            foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
            {
                // Note: You may need to use something other than the first key
                //  in your key ring. Keep that in mind.
                // ex: .Where(k => !k.IsSigningKey)

                /*
                 * PgpSecretKey key = kRing.GetSecretKeys()
                 *  .Cast<PgpSecretKey>()
                 *  .Where(k => k.IsSigningKey)
                 *  .FirstOrDefault();
                 *
                 * if (key != null)
                 * {
                 *  return key;
                 * }
                 */
                IEnumerator ikeys = kRing.GetSecretKeys().GetEnumerator();
                if (ikeys.MoveNext())
                {
                    PgpSecretKey key = (PgpSecretKey)ikeys.Current;
                    if (key.IsSigningKey)
                    {
                        return(key);
                    }
                }
            }

            return(null);
        }
 private PgpSecretKey GetLastSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
 {
     return((from PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings()
             select kRing.GetSecretKeys().Cast <PgpSecretKey>()
             .LastOrDefault(k => k.IsSigningKey))
            .LastOrDefault(key => key != null));
 }
        private PgpSecretKey ReadSecretKey(
            Stream inputStream)
        {
            PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(inputStream);

            //
            // we just loop through the collection till we find a key suitable for encryption, in the real
            // world you would probably want to be a bit smarter about this.
            //
            //
            // iterate through the key rings.
            //
            foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings())
            {
                foreach (PgpSecretKey k in kRing.GetSecretKeys())
                {
                    if (k.IsSigningKey)
                    {
                        return(k);
                    }
                }
            }

            throw new ArgumentException("Can't find signing key in key ring.");
        }
Esempio n. 8
0
        private static PgpSecretKey ReadSigningSecretKey(System.IO.Stream keyInStream)
        {
            PgpSecretKeyRingBundle pgpSec = CreatePgpSecretKeyRingBundle(keyInStream);
            PgpSecretKey           key    = null;

            System.Collections.IEnumerator rIt = pgpSec.GetKeyRings().GetEnumerator();
            while (key == null && rIt.MoveNext())
            {
                PgpSecretKeyRing kRing             = (PgpSecretKeyRing)rIt.Current;
                System.Collections.IEnumerator kIt = kRing.GetSecretKeys().GetEnumerator();
                while (key == null && kIt.MoveNext())
                {
                    PgpSecretKey k = (PgpSecretKey)kIt.Current;
                    if (k.IsSigningKey)
                    {
                        key = k;
                    }
                }
            }

            if (key == null)
            {
                throw new System.Exception("Wrong private key - Can't find signing key in key ring.");
            }
            else
            {
                return(key);
            }
        }
Esempio n. 9
0
        public PgpSecretKey LoadSecretKey(Stream key)
        {
            var armoredStream      = PgpUtilities.GetDecoderStream(key);
            var pgpSecretKeyBundle = new PgpSecretKeyRingBundle(armoredStream);

            foreach (PgpSecretKeyRing keyRing in pgpSecretKeyBundle.GetKeyRings())
            {
                return(keyRing.GetSecretKey());
            }

            throw new ArgumentException("Can't find secret key in key ring.");
        }
 /// <summary>
 /// Return the first key we can use to encrypt.
 /// Note: A file can contain multiple keys (stored in "key rings")
 /// </summary>
 private PgpSecretKey GetFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
 {
     foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
     {
         PgpSecretKey key = kRing.GetSecretKeys().Cast <PgpSecretKey>().Where(k => k.IsSigningKey).FirstOrDefault();
         if (key != null)
         {
             return(key);
         }
     }
     return(null);
 }
Esempio n. 11
0
        public PgpSecretKey GetSecretKeyForSigning(string email)
        {
            using (var inputStream = File.OpenRead(Context.PrivateKeyRingFile))
            {
                using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    var pgpPub      = new PgpSecretKeyRingBundle(decoderStream);
                    var emailSearch = "<" + email.ToLower().Trim() + ">";

                    foreach (PgpSecretKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpSecretKey k in kRing.GetSecretKeys())
                        {
                            if (!IsSigningKey(k.PublicKey))
                            {
                                continue;
                            }

                            if (!k.IsMasterKey)
                            {
                                foreach (PgpSignature sig in k.PublicKey.GetSignaturesOfType(24))
                                {
                                    var pubKey = this.GetPublicKey(sig.KeyId);
                                    if (!pubKey.IsMasterKey)
                                    {
                                        continue;
                                    }

                                    foreach (string id in pubKey.GetUserIds())
                                    {
                                        if (id.ToLower().IndexOf(emailSearch) > -1)
                                        {
                                            return(k);
                                        }
                                    }
                                }
                            }

                            foreach (string id in k.PublicKey.GetUserIds())
                            {
                                if (id.ToLower().IndexOf(emailSearch) > -1)
                                {
                                    return(k);
                                }
                            }
                        }
                    }

                    return(null);
                }
            }
        }
        private PgpSecretKey readSecretKey(PgpSecretKeyRingBundle bundle)
        {
            var keyRings = bundle.GetKeyRings();

            foreach (var keyRing in keyRings)
            {
                if (keyRing is PgpSecretKeyRing)
                {
                    return(((PgpSecretKeyRing)keyRing).GetSecretKey());
                }
            }
            throw new ArgumentException("Secret Key for message not found");
        }
Esempio n. 13
0
 public static PgpPrivateKey FindSecretKeyByUserId(PgpSecretKeyRingBundle pgpSec, string userId, char[] pass)
 {
     foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings(userId, true, true))
     {
         foreach (PgpSecretKey key in keyRing.GetSecretKeys())
         {
             if (key.IsSigningKey)
             {
                 return(key.ExtractPrivateKey(pass));
             }
         }
     }
     return(null);
 }
Esempio n. 14
0
        /// <summary>
        /// Return the first key we can use to encrypt.
        /// Note: A file can contain multiple keys (stored in "key rings")
        /// </summary>
        protected PgpSecretKey GetFirstSecretKey(PgpSecretKeyRingBundle PGP_SecretKeyRingBundle)
        {
            foreach (PgpSecretKeyRing PGP_SecretKeyRing in PGP_SecretKeyRingBundle.GetKeyRings())
            {
                foreach (PgpSecretKey PGP_SecretKey in PGP_SecretKeyRing.GetSecretKeys())
                {
                    if (PGP_SecretKey.IsSigningKey)
                    {
                        return(PGP_SecretKey);
                    }
                }
            }

            return(null);
        }
Esempio n. 15
0
        public string[] GetSecretKeyUserIds()
        {
            using (var inputStream = File.OpenRead(Context.PrivateKeyRingFile))
            {
                using (var decoderStream = PgpUtilities.GetDecoderStream(inputStream))
                {
                    var pgpPub     = new PgpSecretKeyRingBundle(decoderStream);
                    var keyUserIds = new List <string>();

                    foreach (PgpSecretKeyRing kRing in pgpPub.GetKeyRings())
                    {
                        foreach (PgpSecretKey k in kRing.GetSecretKeys())
                        {
                            if (!k.IsMasterKey)
                            {
                                foreach (PgpSignature sig in k.PublicKey.GetSignaturesOfType(24))
                                {
                                    var pubKey = this.GetPublicKey(sig.KeyId);
                                    if (!pubKey.IsMasterKey)
                                    {
                                        continue;
                                    }

                                    foreach (string id in pubKey.GetUserIds())
                                    {
                                        if (!keyUserIds.Contains(id))
                                        {
                                            keyUserIds.Add(id);
                                        }
                                    }
                                }
                            }

                            foreach (string id in k.PublicKey.GetUserIds())
                            {
                                if (!keyUserIds.Contains(id))
                                {
                                    keyUserIds.Add(id);
                                }
                            }
                        }
                    }

                    return(keyUserIds.ToArray());
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Opens a key ring file and loads the first available key suitable for signature generation.
        /// </summary>
        /// <param name="inputStream"></param>
        /// <returns></returns>
        /// <exception cref="IOException"></exception>
        /// <exception cref="PgpException"></exception>
        internal static PgpSecretKey ReadSecretKey(Stream inputStream)
        {
            PgpSecretKeyRingBundle _pgpSecKeyRingBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(inputStream));

            foreach (PgpSecretKeyRing _pgpSecKeyRing in _pgpSecKeyRingBundle.GetKeyRings())
            {
                foreach (PgpSecretKey _pgpSecKey in _pgpSecKeyRing.GetSecretKeys())
                {
                    if (_pgpSecKey.IsSigningKey)
                    {
                        return(_pgpSecKey);
                    }
                }
            }

            throw new ArgumentException("Signing key not found in key ring.");
        }
        /// <summary>
        /// Simple routine that opens a key ring file and loads the first available key
        /// suitable for signature generation.
        /// </summary>
        /// <param name="keyringStream">
        /// Input stream to read the secret key ring collection from.
        /// </param>
        /// <returns>
        /// Returns the first available <see cref="PgpSecretKey"/>.
        /// </returns>
        public static PgpSecretKey ReadSecretKey(Stream keyringStream)
        {
            var secretKeyRingBundle = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(keyringStream));

            foreach (PgpSecretKeyRing secretKeyRing in secretKeyRingBundle.GetKeyRings())
            {
                foreach (PgpSecretKey secretKey in secretKeyRing.GetSecretKeys())
                {
                    if (secretKey.IsSigningKey)
                    {
                        return(secretKey);
                    }
                }
            }

            throw new ArgumentException("Can't find signing key in key ring.");
        }
Esempio n. 18
0
 public static PgpSecretKey LoadSecretKey(string key)
 {
     using (Stream s = Tools.GenerateStreamFromString(key)) {
         var pgp = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(s));
         foreach (PgpSecretKeyRing keyRing in pgp.GetKeyRings())
         {
             foreach (PgpSecretKey secretKey in keyRing.GetSecretKeys())
             {
                 if (secretKey.IsSigningKey)
                 {
                     return(secretKey);
                 }
             }
         }
     }
     return(null);
 }
Esempio n. 19
0
        /**
         * A simple routine that opens a key ring file and loads the first available key
         * suitable for signature generation.
         *
         * @param input stream to read the secret key ring collection from.
         * @return a secret key.
         * @throws IOException on a problem with using the input stream.
         * @throws PGPException if there is an issue parsing the input stream.
         */
        internal static PgpSecretKey ReadSecretKey(Stream input)
        {
            var pgpSec = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(input));

            foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings())
            {
                foreach (PgpSecretKey key in keyRing.GetSecretKeys())
                {
                    if (key.IsSigningKey)
                    {
                        return(key);
                    }
                }
            }

            throw new ArgumentException("Can't find signing key in key ring.");
        }
Esempio n. 20
0
        private static PgpPrivateKey ReadPrivateKey(string keyFile, string passPhrase)
        {
            var keyStream     = File.OpenRead(keyFile);
            var decoderStream = PgpUtilities.GetDecoderStream(keyStream);
            var keyBundle     = new PgpSecretKeyRingBundle(decoderStream);

            foreach (var keyRing in keyBundle.GetKeyRings().Cast <PgpSecretKeyRing>())
            {
                foreach (var key in keyRing.GetSecretKeys().Cast <PgpSecretKey>())
                {
                    if (!key.IsPrivateKeyEmpty)
                    {
                        return(key.ExtractPrivateKey(passPhrase.ToCharArray()));
                    }
                }
            }
            return(null);
        }
Esempio n. 21
0
        public static string GetKeyFingerprint(string keyFile)
        {
            var keyStream     = File.OpenRead(keyFile);
            var decoderStream = PgpUtilities.GetDecoderStream(keyStream);
            var keyBundle     = new PgpSecretKeyRingBundle(decoderStream);

            foreach (var keyRing in keyBundle.GetKeyRings().Cast <PgpSecretKeyRing>())
            {
                foreach (var key in keyRing.GetSecretKeys().Cast <PgpSecretKey>())
                {
                    if (key.IsMasterKey)
                    {
                        return(Hex.ToHexString(key.PublicKey.GetFingerprint()));
                    }
                }
            }
            return(null);
        }
Esempio n. 22
0
 public PgpSecretKey LoadSecKey(string path)
 {
     using (Stream keyIn = File.OpenRead(path)) {
         Stream decStream = PgpUtilities.GetDecoderStream(keyIn);
         PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(decStream);
         foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings())
         {
             foreach (PgpSecretKey key in keyRing.GetSecretKeys())
             {
                 if (key.IsSigningKey)
                 {
                     return(key);
                 }
             }
         }
         throw new ArgumentException("Can't find signing key in key ring.");
     }
 }
Esempio n. 23
0
        /// <summary>
        ///     Reads and returns the PGP secret key from the specified input stream.
        /// </summary>
        /// <param name="input">The input stream containing the PGP secret key to be read.</param>
        /// <returns>The retrieved PGP secret key.</returns>
        private static PgpSecretKey ReadSecretKey(Stream input)
        {
            try {
                PgpSecretKeyRingBundle pgpSec       = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(input));
                PgpSecretKeyRing       pgpKeyRing   = pgpSec.GetKeyRings().OfType <PgpSecretKeyRing>().FirstOrDefault();
                PgpSecretKey           pgpSecretKey = pgpKeyRing.GetSecretKeys().OfType <PgpSecretKey>().FirstOrDefault();

                if (pgpSecretKey.IsSigningKey)
                {
                    return(pgpSecretKey);
                }
                else
                {
                    throw new Exception();
                }
            } catch (Exception) {
                throw new PgpKeyValidationException("Can't find a valid signing key in the specified key ring.");
            }
        }
Esempio n. 24
0
        private static PgpSecretKey ReadSigningKey(string keyFile)
        {
            var          keyStream     = File.OpenRead(keyFile);
            var          decoderStream = PgpUtilities.GetDecoderStream(keyStream);
            var          keyBundle     = new PgpSecretKeyRingBundle(decoderStream);
            PgpSecretKey signingKey    = null;

            foreach (var keyRing in keyBundle.GetKeyRings().Cast <PgpSecretKeyRing>())
            {
                foreach (var key in keyRing.GetSecretKeys().Cast <PgpSecretKey>())
                {
                    if (key.IsSigningKey)
                    {
                        signingKey = key;
                    }
                }
            }
            keyStream.Dispose();
            return(signingKey);
        }
Esempio n. 25
0
        private static PgpSecretKey ReadSecretKey(Stream inputStream)
        {
            var decodedInputStream  = PgpUtilities.GetDecoderStream(inputStream);
            var secretKeyRingBundle = new PgpSecretKeyRingBundle(decodedInputStream);

            // we just loop through the collection till we find a key suitable for encryption, in the real
            // world you would probably want to be a bit smarter about this.
            foreach (PgpSecretKeyRing keyRing in secretKeyRingBundle.GetKeyRings())
            {
                foreach (PgpSecretKey key in keyRing.GetSecretKeys())
                {
                    if (key.IsSigningKey)
                    {
                        return(key);
                    }
                }
            }

            throw new ArgumentException("Can't find signing key in key ring.");
        }
Esempio n. 26
0
        /// <summary>
        /// The read secret key.
        /// </summary>
        /// <param name="privateKeyPath">
        /// The private key path.
        /// </param>
        /// <param name="signing">
        /// Is it the signing key we are looking for?
        /// </param>
        /// <returns>
        /// The <see cref="PgpSecretKey"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Throws a <see cref="ArgumentException"/> if no key is available.
        /// </exception>
        private PgpSecretKey ReadSecretKey(string privateKeyPath, bool signing)
        {
            using (Stream keyringStream = File.OpenRead(privateKeyPath))
                using (Stream inputStream = PgpUtilities.GetDecoderStream(keyringStream))
                {
                    PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream);
                    foreach (PgpSecretKeyRing keyRing in secretKeyRingBundle.GetKeyRings())
                    {
                        foreach (PgpSecretKey key in keyRing.GetSecretKeys())
                        {
                            if (key != null && key.IsSigningKey == signing)
                            {
                                return(key);
                            }
                        }
                    }
                }

            throw new ArgumentException("Can't find signing key in key ring.");
        }
Esempio n. 27
0
        /// <summary>
        /// Imports a secret pgp keyring bundle.
        /// </summary>
        /// <remarks>
        /// Imports a secret pgp keyring bundle.
        /// </remarks>
        /// <param name="bundle">The pgp keyring bundle.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="bundle"/> is <c>null</c>.
        /// </exception>
        public virtual void Import(PgpSecretKeyRingBundle bundle)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }

            int secretKeysAdded = 0;

            foreach (PgpSecretKeyRing secring in bundle.GetKeyRings())
            {
                SecretKeyRingBundle = PgpSecretKeyRingBundle.AddSecretKeyRing(SecretKeyRingBundle, secring);
                secretKeysAdded++;
            }

            if (secretKeysAdded > 0)
            {
                SaveSecretKeyRingBundle();
            }
        }
Esempio n. 28
0
        public static bool ValidatePassPhrase(byte[] privateKey, string passPhrase)
        {
            using (MemoryStream privateKeyStream = new MemoryStream(privateKey))
            {
                PgpSecretKeyRingBundle pgpKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream));
                var items = pgpKeyRing.GetKeyRings();
                foreach (var item in items)
                {
                    try {
                        var i   = (PgpSecretKeyRing)item;
                        var key = i.GetSecretKey().ExtractPrivateKey(passPhrase.ToCharArray());
                        return(true);
                    } catch (Exception ex) {
                        Insights.Report(ex);
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 29
0
        /// <summary>
        /// Return the first key we can use to encrypt.
        /// Note: A file can contain multiple keys (stored in "key rings")
        /// </summary>
        private PgpSecretKey getFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
        {
            foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
            {
                // Note: You may need to use something other than the first key
                //  in your key ring. Keep that in mind.
                // ex: .Where(k => !k.IsSigningKey)
                PgpSecretKey key = kRing.GetSecretKeys()
                                   .Cast <PgpSecretKey>()
                                   //.Where(k => k.IsSigningKey)
                                   .Where(k => !k.IsSigningKey)
                                   .FirstOrDefault();

                if (key != null)
                {
                    return(key);
                }
            }

            return(null);
        }
Esempio n. 30
0
 static PgpSecretKey GetSecretKey(string path)
 {
     using (Stream keyIn = File.OpenRead(path))
     {
         using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn))
         {
             var secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream);
             foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
             {
                 PgpSecretKey key = kRing.GetSecretKeys()
                                    .Cast <PgpSecretKey>()
                                    .Where(k => k.IsSigningKey)
                                    .FirstOrDefault();
                 if (key != null)
                 {
                     return(key);
                 }
             }
         }
     }
     return(null);
 }
Esempio n. 31
0
        public void PerformTest1()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub1);

            int count = 0;

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;
                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pubKey in pgpPub2.GetPublicKeys())
                {
                    keyCount++;

                    foreach (PgpSignature sig in pubKey.GetSignatures())
                    {
                        if (sig == null)
                            Fail("null signature found");
                    }
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings");
            }

            //
            // exact match
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub3 in pubRings.GetKeyRings("test (Test key) <*****@*****.**>"))
            {
                if (pgpPub3 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on exact match");
            }

            //
            // partial match 1 expected
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub4 in pubRings.GetKeyRings("test", true))
            {
                if (pgpPub4 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on partial match 1");
            }

            //
            // partial match 0 expected
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub5 in pubRings.GetKeyRings("XXX", true))
            {
                if (pgpPub5 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 0)
            {
                Fail("wrong number of public keyrings on partial match 0");
            }

            //
            // case-insensitive partial match
            //
            count = 0;
            foreach (PgpPublicKeyRing pgpPub6 in pubRings.GetKeyRings("*****@*****.**", true, true))
            {
                if (pgpPub6 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings on case-insensitive partial match");
            }

            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(sec1);
            count = 0;

            foreach (PgpSecretKeyRing pgpSec1 in secretRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    PgpPublicKey pk = k.PublicKey;

                    pk.GetSignatures();

                    byte[] pkBytes = pk.GetEncoded();

                    PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }

            //
            // exact match
            //
            count = 0;
            foreach (PgpSecretKeyRing o1 in secretRings.GetKeyRings("test (Test key) <*****@*****.**>"))
            {
                if (o1 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on exact match");
            }

            //
            // partial match 1 expected
            //
            count = 0;
            foreach (PgpSecretKeyRing o2 in secretRings.GetKeyRings("test", true))
            {
                if (o2 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on partial match 1");
            }

            //
            // exact match 0 expected
            //
            count = 0;
            foreach (PgpSecretKeyRing o3 in secretRings.GetKeyRings("test", false))
            {
                if (o3 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 0)
            {
                Fail("wrong number of secret keyrings on partial match 0");
            }

            //
            // case-insensitive partial match
            //
            count = 0;
            foreach (PgpSecretKeyRing o4 in secretRings.GetKeyRings("*****@*****.**", true, true))
            {
                if (o4 == null)
                    Fail("null keyring found");

                count++;
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings on case-insensitive partial match");
            }
        }
        public void EccKeyTest()
        {
            var secretRings = new PgpSecretKeyRingBundle(_eccSec1);
            var count = 0;

            foreach (PgpSecretKeyRing secretKeyRing in secretRings.GetKeyRings())
            {
                count++;

                CheckEccKey(secretKeyRing);
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
		private PgpSecretKey ReadSecretKey(
			Stream    inputStream)
		{
			PgpSecretKeyRingBundle        pgpSec = new PgpSecretKeyRingBundle(inputStream);

			//
			// we just loop through the collection till we find a key suitable for encryption, in the real
			// world you would probably want to be a bit smarter about this.
			//
			//
			// iterate through the key rings.
			//
			foreach (PgpSecretKeyRing kRing in pgpSec.GetKeyRings())
			{
				foreach (PgpSecretKey k in kRing.GetSecretKeys())
				{
					if (k.IsSigningKey)
					{
						return k;
					}
				}
			}

			throw new ArgumentException("Can't find signing key in key ring.");
		}
Esempio n. 34
0
        public void RewrapTest()
        {
            SecureRandom rand = new SecureRandom();

            // Read the secret key rings
            PgpSecretKeyRingBundle privRings = new PgpSecretKeyRingBundle(
                new MemoryStream(rewrapKey, false));

            foreach (PgpSecretKeyRing pgpPrivEnum in privRings.GetKeyRings())
            {
                foreach (PgpSecretKey pgpKeyEnum in pgpPrivEnum.GetSecretKeys())
                {
                    // re-encrypt the key with an empty password
                    PgpSecretKeyRing pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPrivEnum, pgpKeyEnum);
                    PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword(
                        pgpKeyEnum,
                        rewrapPass,
                        null,
                        SymmetricKeyAlgorithmTag.Null,
                        rand);
                    pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey);

                    // this should succeed
                    PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(null);
                }
            }
        }
Esempio n. 35
0
        private void checkSecretKeyRingWithPersonalCertificate(
			byte[] keyRing)
        {
            PgpSecretKeyRingBundle secCol = new PgpSecretKeyRingBundle(keyRing);

            int count = 0;

            foreach (PgpSecretKeyRing ring in secCol.GetKeyRings())
            {
                IEnumerator e = ring.GetExtraPublicKeys().GetEnumerator();
                while (e.MoveNext())
                {
                    ++count;
                }
            }

            if (count != 1)
            {
                Fail("personal certificate data subkey not found - count = " + count);
            }
        }
Esempio n. 36
0
        public void PerformTest9()
        {
            PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec9);

            int count = 0;

            byte[] encRing = secretRings1.GetEncoded();

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;

                    PgpPrivateKey pKey = k.ExtractPrivateKey(sec9pass);
                    if (keyCount == 1 && pKey != null)
                    {
                        Fail("primary secret key found, null expected");
                    }
                }

                if (keyCount != 3)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
Esempio n. 37
0
        public void PerformTest8()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub8);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey o in pgpPub2.GetPublicKeys())
                {
                    if (o == null)
                        Fail("null key found");

                    keyCount++;
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec8);

            count = 0;

            encRing = secretRings1.GetEncoded();

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    k.ExtractPrivateKey(sec8pass);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
Esempio n. 38
0
        public void PerformTest4()
        {
            PgpSecretKeyRingBundle secretRings1 = new PgpSecretKeyRingBundle(sec4);
            int count = 0;

            byte[] encRing = secretRings1.GetEncoded();

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings1.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    k.ExtractPrivateKey(sec3pass1);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
Esempio n. 39
0
        public void PerformTest3()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub3);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pubK in pgpPub2.GetPublicKeys())
                {
                    keyCount++;
                    pubK.GetSignatures();
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec3);

            count = 0;

            encRing = secretRings2.GetEncoded();

            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    k.ExtractPrivateKey(sec3pass1);
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 1)
            {
                Fail("wrong number of secret keyrings");
            }
        }
Esempio n. 40
0
		/**
		 * A simple routine that opens a key ring file and loads the first available key
		 * suitable for signature generation.
		 * 
		 * @param input stream to read the secret key ring collection from.
		 * @return a secret key.
		 * @throws IOException on a problem with using the input stream.
		 * @throws PGPException if there is an issue parsing the input stream.
		 */
		internal static PgpSecretKey ReadSecretKey(Stream input)
		{
			PgpSecretKeyRingBundle pgpSec = new PgpSecretKeyRingBundle(
				PgpUtilities.GetDecoderStream(input));

			//
			// we just loop through the collection till we find a key suitable for encryption, in the real
			// world you would probably want to be a bit smarter about this.
			//

			foreach (PgpSecretKeyRing keyRing in pgpSec.GetKeyRings())
			{
				foreach (PgpSecretKey key in keyRing.GetSecretKeys())
				{
					if (key.IsSigningKey)
					{
						return key;
					}
				}
			}

			throw new ArgumentException("Can't find signing key in key ring.");
		}
Esempio n. 41
0
        public void PerformTest2()
        {
            PgpPublicKeyRingBundle pubRings = new PgpPublicKeyRingBundle(pub2);

            int count = 0;

            byte[] encRing = pubRings.GetEncoded();

            pubRings = new PgpPublicKeyRingBundle(encRing);

            foreach (PgpPublicKeyRing pgpPub1 in pubRings.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpPub1.GetEncoded();

                PgpPublicKeyRing pgpPub2 = new PgpPublicKeyRing(bytes);

                foreach (PgpPublicKey pk in pgpPub2.GetPublicKeys())
                {
                    byte[] pkBytes = pk.GetEncoded();

                    PgpPublicKeyRing pkR = new PgpPublicKeyRing(pkBytes);

                    keyCount++;
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of public keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of public keyrings");
            }

            PgpSecretKeyRingBundle secretRings2 = new PgpSecretKeyRingBundle(sec2);

            count = 0;

            encRing = secretRings2.GetEncoded();
            PgpSecretKeyRingBundle secretRings = new PgpSecretKeyRingBundle(encRing);

            foreach (PgpSecretKeyRing pgpSec1 in secretRings2.GetKeyRings())
            {
                count++;

                int keyCount = 0;

                byte[] bytes = pgpSec1.GetEncoded();

                PgpSecretKeyRing pgpSec2 = new PgpSecretKeyRing(bytes);

                foreach (PgpSecretKey k in pgpSec2.GetSecretKeys())
                {
                    keyCount++;
                    PgpPublicKey pk = k.PublicKey;

                    if (pk.KeyId == -1413891222336124627L)
                    {
                        int sCount = 0;

                        foreach (PgpSignature pgpSignature in pk.GetSignaturesOfType(PgpSignature.SubkeyBinding))
                        {
                            int type = pgpSignature.SignatureType;
                            if (type != PgpSignature.SubkeyBinding)
                            {
                                Fail("failed to return correct signature type");
                            }
                            sCount++;
                        }

                        if (sCount != 1)
                        {
                            Fail("failed to find binding signature");
                        }
                    }

                    pk.GetSignatures();

                    if (k.KeyId == -4049084404703773049L
                        || k.KeyId == -1413891222336124627L)
                    {
                        k.ExtractPrivateKey(sec2pass1);
                    }
                    else if (k.KeyId == -6498553574938125416L
                        || k.KeyId == 59034765524361024L)
                    {
                        k.ExtractPrivateKey(sec2pass2);
                    }
                }

                if (keyCount != 2)
                {
                    Fail("wrong number of secret keys");
                }
            }

            if (count != 2)
            {
                Fail("wrong number of secret keyrings");
            }
        }
Esempio n. 42
0
        public void RewrapTestV3()
        {
            // Read the secret key rings
            PgpSecretKeyRingBundle privRings = new PgpSecretKeyRingBundle(
                new MemoryStream(privv3, false));

            char[] newPass = "******".ToCharArray();

            foreach (PgpSecretKeyRing pgpPrivEnum in privRings.GetKeyRings())
            {
                PgpSecretKeyRing pgpPriv = pgpPrivEnum;

                foreach (PgpSecretKey pgpKeyEnum in pgpPriv.GetSecretKeys())
                {
                    long oldKeyID = pgpKeyEnum.KeyId;

                    // re-encrypt the key with an empty password
                    pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPriv, pgpKeyEnum);
                    PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword(
                        pgpKeyEnum,
                        v3KeyPass,
                        null,
                        SymmetricKeyAlgorithmTag.Null,
                        Random);
                    pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey);

                    // this should succeed
                    PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(null);

                    if (pgpKey.KeyId != oldKeyID)
                    {
                        Fail("key ID mismatch");
                    }
                }

                foreach (PgpSecretKey pgpKeyEnum in pgpPriv.GetSecretKeys())
                {
                    long oldKeyID = pgpKeyEnum.KeyId;

                    // re-encrypt the key with an empty password
                    pgpPriv = PgpSecretKeyRing.RemoveSecretKey(pgpPriv, pgpKeyEnum);
                    PgpSecretKey pgpKey = PgpSecretKey.CopyWithNewPassword(
                        pgpKeyEnum,
                        null,
                        newPass,
                        SymmetricKeyAlgorithmTag.Cast5,
                        Random);
                    pgpPriv = PgpSecretKeyRing.InsertSecretKey(pgpPriv, pgpKey);

                    // this should succeed
                    PgpPrivateKey privTmp = pgpKey.ExtractPrivateKey(newPass);

                    if (pgpKey.KeyId != oldKeyID)
                    {
                        Fail("key ID mismatch");
                    }
                }
            }
        }