Exemplo n.º 1
0
        /// <summary>
        /// Delete a public pgp keyring.
        /// </summary>
        /// <remarks>
        /// Deletes a public pgp keyring.
        /// </remarks>
        /// <param name="keyring">The pgp keyring.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="keyring"/> is <c>null</c>.
        /// </exception>
        public virtual void Delete(PgpPublicKeyRing keyring)
        {
            if (keyring == null)
            {
                throw new ArgumentNullException(nameof(keyring));
            }

            PublicKeyRingBundle = PgpPublicKeyRingBundle.RemovePublicKeyRing(PublicKeyRingBundle, keyring);
            SavePublicKeyRingBundle();
        }
Exemplo n.º 2
0
        static void p5_crypto_delete_public_key(ApplicationContext context, ActiveEventArgs e)
        {
            // House cleaning
            using (new ArgsRemover(e.Args, true)) {
                // Creating new GnuPG context
                using (var ctx = new GnuPrivacyContext(true)) {
                    // Signaler boolean
                    bool somethingWasRemoved = false;
                    var  bundle = ctx.PublicKeyRingBundle;

                    // Looping through each ID given by caller
                    foreach (var idxId in XUtil.Iterate <string> (context, e.Args))
                    {
                        // Looping through each public key ring in GnuPG database until we find given ID
                        foreach (PgpPublicKeyRing idxPublicKeyRing in bundle.GetKeyRings())
                        {
                            // Looping through each key in keyring
                            foreach (PgpPublicKey idxPublicKey in idxPublicKeyRing.GetPublicKeys())
                            {
                                // Checking for a match, making sure we do not match UserIDs.
                                if (ObjectIterator.IsMatch(idxPublicKey, idxId, false))
                                {
                                    // Removing entire keyring, and signaling to save keyring bundle
                                    somethingWasRemoved = true;
                                    bundle = PgpPublicKeyRingBundle.RemovePublicKeyRing(bundle, idxPublicKeyRing);

                                    // Breaking inner most foreach
                                    break;
                                }
                            }

                            // Checking if currently iterated filter was found in currently iterated secret keyring.
                            if (somethingWasRemoved)
                            {
                                break;
                            }
                        }
                    }

                    // Checking to see if something was removed, and if so, saving GnuPG context
                    if (somethingWasRemoved)
                    {
                        ctx.SavePublicKeyRingBundle(bundle);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sign a public key.
        /// </summary>
        /// <remarks>
        /// <para>Signs a public key using the specified secret key.</para>
        /// <para>Most OpenPGP implementations use <see cref="OpenPgpKeyCertification.GenericCertification"/>
        /// to make their "key signatures". Some implementations are known to use the other
        /// certification types, but few differentiate between them.</para>
        /// </remarks>
        /// <param name="secretKey">The secret key to use for signing.</param>
        /// <param name="publicKey">The public key to sign.</param>
        /// <param name="digestAlgo">The digest algorithm.</param>
        /// <param name="certification">The certification to give the signed key.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="secretKey"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="publicKey"/> is <c>null</c>.</para>
        /// </exception>
        public void SignKey(PgpSecretKey secretKey, PgpPublicKey publicKey, DigestAlgorithm digestAlgo = DigestAlgorithm.Sha1, OpenPgpKeyCertification certification = OpenPgpKeyCertification.GenericCertification)
        {
            if (secretKey == null)
            {
                throw new ArgumentNullException(nameof(secretKey));
            }

            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            var privateKey         = GetPrivateKey(secretKey);
            var signatureGenerator = new PgpSignatureGenerator(secretKey.PublicKey.Algorithm, GetHashAlgorithm(digestAlgo));

            signatureGenerator.InitSign((int)certification, privateKey);
            signatureGenerator.GenerateOnePassVersion(false);

            var subpacketGenerator = new PgpSignatureSubpacketGenerator();
            var subpacketVector    = subpacketGenerator.Generate();

            signatureGenerator.SetHashedSubpackets(subpacketVector);

            var signedKey            = PgpPublicKey.AddCertification(publicKey, signatureGenerator.Generate());
            PgpPublicKeyRing keyring = null;

            foreach (var ring in EnumeratePublicKeyRings())
            {
                foreach (PgpPublicKey key in ring.GetPublicKeys())
                {
                    if (key.KeyId == publicKey.KeyId)
                    {
                        PublicKeyRingBundle = PgpPublicKeyRingBundle.RemovePublicKeyRing(PublicKeyRingBundle, ring);
                        keyring             = PgpPublicKeyRing.InsertPublicKey(ring, signedKey);
                        break;
                    }
                }
            }

            if (keyring == null)
            {
                keyring = new PgpPublicKeyRing(signedKey.GetEncoded());
            }

            Import(keyring);
        }
Exemplo n.º 4
0
        private static void p5_crypto_delete_public_key(ApplicationContext context, ActiveEventArgs e)
        {
            // House cleaning
            using (new ArgsRemover(e.Args, true)) {
                // Creating new GnuPG context
                using (var ctx = new GnuPrivacyContext()) {
                    // Signaler boolean
                    bool somethingWasRemoved = false;
                    var  bundle = ctx.PublicKeyRingBundle;

                    // Looping through each ID given by caller
                    foreach (var idxId in XUtil.Iterate <string> (context, e.Args))
                    {
                        // Looping through each public key ring in GnuPG database until we find given ID
                        foreach (PgpPublicKeyRing idxPublicKeyRing in bundle.GetKeyRings())
                        {
                            // Looping through each key in keyring
                            foreach (PgpPublicKey idxPublicKey in idxPublicKeyRing.GetPublicKeys())
                            {
                                if (idxId == idxPublicKey.KeyId.ToString("X"))
                                {
                                    // Removing entire keyring, and signaling to save keyring bundle
                                    somethingWasRemoved = true;
                                    bundle = PgpPublicKeyRingBundle.RemovePublicKeyRing(bundle, idxPublicKeyRing);

                                    // Breaking inner most foreach
                                    break;
                                }
                            }
                        }
                    }

                    // Checking to see if something was removed, and if so, saving GnuPG context
                    if (somethingWasRemoved)
                    {
                        ctx.SavePublicKeyRingBundle(bundle);
                    }
                }
            }
        }