Exemple #1
0
 static void p5_crypto_get_key_details(ApplicationContext context, ActiveEventArgs e)
 {
     // Using common helper to iterate all secret keys.
     ObjectIterator.MatchingPublicKeys(context, e.Args, delegate(PgpPublicKey key) {
         // This key is matching specified filter criteria.
         var fingerprint = BitConverter.ToString(key.GetFingerprint()).Replace("-", "").ToLower();
         var node        = e.Args.Add(fingerprint).LastChild;
         node.Add("id", ((int)key.KeyId).ToString("X"));
         node.Add("algorithm", key.Algorithm.ToString());
         node.Add("strength", key.BitStrength);
         node.Add("creation-time", key.CreationTime);
         node.Add("is-encryption-key", key.IsEncryptionKey);
         node.Add("is-master-key", key.IsMasterKey);
         node.Add("is-revoked", key.IsRevoked());
         node.Add("version", key.Version);
         DateTime expires = key.CreationTime.AddSeconds(key.GetValidSeconds());
         node.Add("expires", expires);
         foreach (var idxUserId in key.GetUserIds())
         {
             if (idxUserId is string)
             {
                 node.FindOrInsert("user-ids").Add("", idxUserId);
             }
         }
         foreach (PgpSignature signature in key.GetSignatures())
         {
             node.FindOrInsert("signed-by").Add(((int)signature.KeyId).ToString("X"), signature.CreationTime);
         }
     }, false);
 }
Exemple #2
0
 static void p5_crypto_list_public_keys(ApplicationContext context, ActiveEventArgs e)
 {
     // Using common helper to iterate all secret keys.
     ObjectIterator.MatchingPublicKeys(context, e.Args, delegate(PgpPublicKey key) {
         // Retrieving fingerprint of currently iterated key, and returning to caller.
         var fingerprint = BitConverter.ToString(key.GetFingerprint()).Replace("-", "").ToLower();
         e.Args.Add(fingerprint);
     }, false);
 }
Exemple #3
0
        static void p5_crypto_get_public_key(ApplicationContext context, ActiveEventArgs e)
        {
            // Using common helper to iterate all secret keys.
            ObjectIterator.MatchingPublicKeys(context, e.Args, delegate(PgpPublicKey key) {
                // Retrieving fingerprint of currently iterated key, and returning to caller.
                var fingerprint = BitConverter.ToString(key.GetFingerprint()).Replace("-", "").ToLower();
                var node        = e.Args.Add(fingerprint).LastChild;

                // This is the key we're looking for
                using (var memStream = new MemoryStream()) {
                    using (var armored = new ArmoredOutputStream(memStream)) {
                        key.Encode(armored);
                        armored.Flush();
                    }
                    memStream.Flush();
                    memStream.Position = 0;
                    var sr             = new StreamReader(memStream);
                    node.Value         = sr.ReadToEnd();
                }
            }, false);
        }
Exemple #4
0
        static void p5_crypto_sign_public_key(ApplicationContext context, ActiveEventArgs e)
        {
            // Figuring out which private key to use for signing, and doing some basic sanity check.
            var fingerprint = e.Args.GetExChildValue("private-key", context, "").ToLower();

            if (fingerprint == "")
            {
                throw new LambdaException("No [private-key] argument supplied to [p5.crypto.sign-public-key]", e.Args, context);
            }

            // Finding password to use to extract private key from GnuPG context, and doing some basic sanity check.
            var password = e.Args.GetExChildValue("password", context, "");

            if (password == "")
            {
                throw new LambdaException("No [password] argument supplied to [p5.crypto.sign-public-key] to extract your private key", e.Args, context);
            }

            // Retrieving our private key to use for signing public key from GnuPG database.
            // Finding password to use to extract private key from GnuPG context, and doing some basic sanity check.
            var certain = e.Args.GetExChildValue("certain", context, false);

            PgpSecretKey signingKey = null;

            using (var ctx = new GnuPrivacyContext(false)) {
                // Iterating all secret keyrings.
                foreach (PgpSecretKeyRing idxRing in ctx.SecretKeyRingBundle.GetKeyRings())
                {
                    // Iterating all keys in currently iterated secret keyring.
                    foreach (PgpSecretKey idxSecretKey in idxRing.GetSecretKeys())
                    {
                        // Checking if caller provided filters, and if not, yielding "everything".
                        if (BitConverter.ToString(idxSecretKey.PublicKey.GetFingerprint()).Replace("-", "").ToLower() == fingerprint)
                        {
                            // No filters provided, matching everything.
                            signingKey = idxSecretKey;
                            break;
                        }
                    }
                    if (signingKey != null)
                    {
                        break;
                    }
                }
            }

            // Using common helper to iterate all public keys caller wants to sign.
            PgpPublicKeyRing sRing = null;

            ObjectIterator.MatchingPublicKeys(context, e.Args, delegate(PgpPublicKey idxKey) {
                // Retrieving fingerprint of currently iterated key, and returning to caller.
                var node = e.Args.Add(BitConverter.ToString(idxKey.GetFingerprint()).Replace("-", "").ToLower()).LastChild;

                // Doing the actual signing of currently iterated public key.
                sRing = new PgpPublicKeyRing(new MemoryStream(SignPublicKey(signingKey, password, idxKey, certain), false));
            }, false);

            // Creating new GnuPG context and importing signed key into context.
            using (var ctx = new GnuPrivacyContext(true)) {
                // Importing signed key.
                ctx.Import(sRing);
            }
        }