public static void p5_crypto_create_pgp_keypair(ApplicationContext context, ActiveEventArgs e) { // Making sure we clean up after ourselves using (new ArgsRemover(e.Args, true)) { // Retrieving identity (normally an email address), in addition to password. string identity = e.Args.GetExChildValue <string> ("identity", context); string password = e.Args.GetExChildValue <string> ("password", context); if (string.IsNullOrEmpty(identity) || string.IsNullOrEmpty(password)) { throw new LambdaException( "Minimum [identity] and [password] needs to be supplied to create a PGP keypair", e.Args, context); } // Retrieving other parameters to PGP keypair creation. DateTime expires = e.Args.GetExChildValue("expires", context, DateTime.Now.AddYears(3)); int strength = e.Args.GetExChildValue <int> ("strength", context, 4096); long publicExponent = e.Args.GetExChildValue("public-exponent", context, 65537L); int certainty = e.Args.GetExChildValue("certainty", context, 5); // Generate public/secret keys. PgpKeyRingGenerator generator = GetKeyRingGenerator( context, e.Args, identity, password, expires, strength, publicExponent, certainty); PgpPublicKeyRing publicRing = generator.GeneratePublicKeyRing(); PgpSecretKeyRing secretRing = generator.GenerateSecretKeyRing(); // Creating GnuPG context to let MimeKit import keys into GnuPG database using (var ctx = new GnuPrivacyContext(true)) { // Saves public keyring ctx.Import(publicRing); // Saves private keyring ctx.Import(secretRing); } // In case no [seed] was given, we remove the automatically generated seed ... e.Args ["seed"].UnTie(); e.Args.Add("fingerprint", BitConverter.ToString(publicRing.GetPublicKey().GetFingerprint()).Replace("-", "")); e.Args.Add("key-id", ((int)publicRing.GetPublicKey().KeyId).ToString("X")); } }
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); } } } }
private static void p5_crypto_get_key_details(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new ArgsRemover(e.Args, true)) { // Getting key ID to look for string keyID = e.Args.GetExValue <string> (context, null); if (string.IsNullOrEmpty(keyID)) { throw new LambdaException("No ID given to use for looking up key", e.Args, context); } // Creating new GnuPG context using (var ctx = new GnuPrivacyContext()) { // Looping through each public key in GnuPG database foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings()) { // Looping through each key in keyring foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys()) { // Checking if this is the requested key if (idxPublicKey.KeyId.ToString("X") == keyID) { // This is the key we're looking for e.Args.Add("algorithm", idxPublicKey.Algorithm.ToString()); e.Args.Add("strength", idxPublicKey.BitStrength); e.Args.Add("creation-time", idxPublicKey.CreationTime); e.Args.Add("is-encryption-key", idxPublicKey.IsEncryptionKey); e.Args.Add("is-master-key", idxPublicKey.IsMasterKey); e.Args.Add("is-revoked", idxPublicKey.IsRevoked()); e.Args.Add("version", idxPublicKey.Version); DateTime expires = idxPublicKey.CreationTime.AddSeconds(idxPublicKey.GetValidSeconds()); e.Args.Add("expires", expires); e.Args.Add("fingerprint", BitConverter.ToString(idxPublicKey.GetFingerprint()).Replace("-", "")); foreach (var idxUserId in idxPublicKey.GetUserIds()) { e.Args.FindOrInsert("user-ids").Add("", idxUserId); } return; } } } } } }
private static void p5_crypto_import_public_pgp_key(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new ArgsRemover(e.Args, true)) { // Creating new GnuPG context using (var ctx = new GnuPrivacyContext()) { // Looping through each public key (in ascii armored format) and importing into GnuPG database foreach (var idxKey in XUtil.Iterate <string> (context, e.Args)) { // Creating armored input stream to wrap key using (var memStream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(idxKey))) { using (var armored = new ArmoredInputStream(memStream)) { var key = new PgpPublicKeyRing(armored); ctx.Import(key); } } } } } }
private static void p5_crypto_import_private_pgp_key(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new ArgsRemover(e.Args, true)) { // Creating new GnuPG context using (var ctx = new GnuPrivacyContext(true)) { // Looping through each public key (in ascii armored format) and importing into GnuPG database foreach (var idxKey in XUtil.Iterate <string> (context, e.Args)) { // Creating armored input stream to wrap key using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(idxKey.Replace("\r\n", "\n")))) { using (var armored = new ArmoredInputStream(memStream)) { var key = new PgpSecretKeyRing(armored); ctx.Import(key); e.Args.Add(BitConverter.ToString(key.GetPublicKey().GetFingerprint()).Replace("-", "")); } } } } } }
public static void _p5_crypto_pgp_keys_context_create(ApplicationContext context, ActiveEventArgs e) { // Creating GnuPG Context. var ctx = new GnuPrivacyContext( e.Args.Get <bool> (context), e.Args.GetChildValue <string> ("fingerprint", context, null), e.Args.GetChildValue <string> ("password", context, null)); // Making sure we set the key server for the context, if one is given. var keyServer = context.RaiseEvent(".p5.config.get", new Node(".p5.config.get", "p5.crypto.key-server")) [0]?.Get <string> (context) ?? null; if (!string.IsNullOrEmpty(keyServer)) { // Some key server was declared in web.config. ctx.KeyServer = new Uri(keyServer); ctx.AutoKeyRetrieve = true; } // Returning context to caller. e.Args.Value = ctx; }
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); } } } }
private static void p5_crypto_get_public_key(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new ArgsRemover(e.Args)) { // Getting key ID to look for string keyID = e.Args.GetExValue <string> (context, null); if (string.IsNullOrEmpty(keyID)) { throw new LambdaException("No ID given to use for looking up key", e.Args, context); } // Creating new GnuPG context using (var ctx = new GnuPrivacyContext()) { // Looping through each public key in GnuPG database foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings()) { // Looping through each key in keyring foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys()) { // Checking if this is the requested key if (idxPublicKey.KeyId.ToString("X") == keyID) { // This is the key we're looking for using (var memStream = new MemoryStream()) { using (var armored = new ArmoredOutputStream(memStream)) { idxPublicKey.Encode(armored); armored.Flush(); } memStream.Flush(); memStream.Position = 0; var sr = new StreamReader(memStream); e.Args.Value = sr.ReadToEnd(); } return; } } } } } }
private static void p5_crypto_list_public_keys(ApplicationContext context, ActiveEventArgs e) { // House cleaning using (new ArgsRemover(e.Args, true)) { // Checking if user provided a filter string filter = e.Args.GetExValue <string> (context, null); // Creating new GnuPG context using (var ctx = new GnuPrivacyContext()) { // Looping through each public key in GnuPG database foreach (PgpPublicKeyRing idxRing in ctx.PublicKeyRingBundle.GetKeyRings()) { // Looping through each key in keyring foreach (PgpPublicKey idxPublicKey in idxRing.GetPublicKeys()) { // Finding identity of key foreach (var idxUserID in idxPublicKey.GetUserIds()) { // Converting to a string, before checking for a match, but only if object is a string if (idxUserID is string) { var userID = idxUserID.ToString(); // Checking if filter is not null, and if so, making sure identity of currently iterated key matches filter if (string.IsNullOrEmpty(filter) || userID.Contains(filter)) { // Returning identity and key ID to caller e.Args.Add(userID, idxPublicKey.KeyId.ToString("X")); // We'll risk adding the same key twice unless we break here! break; } } } } } } } }
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); } }