static void Main(string[] args) { Context ctx = new Context(); if (ctx.Protocol != Protocol.OpenPGP) ctx.SetEngineInfo(Protocol.OpenPGP, null, null); Console.WriteLine("Search Bob's PGP key in the default keyring.."); String searchstr = "*****@*****.**"; IKeyStore keyring = ctx.KeyStore; // retrieve all keys that have Bob's email address Key[] keys = keyring.GetKeyList(searchstr, false); if (keys == null || keys.Length == 0) { Console.WriteLine("Cannot find Bob's PGP key {0} in your keyring.", searchstr); Console.WriteLine("You may want to create the PGP key by using the appropriate\n" + "sample in the Samples/ directory."); return; } // print a list of all returned keys foreach (Key key in keys) if (key.Uid != null && key.Fingerprint != null) Console.WriteLine("Found key {0} with fingerprint {1}", key.Uid.Name, key.Fingerprint); // we are going to use the first key in the list PgpKey bob = (PgpKey)keys[0]; if (bob.Uid == null || bob.Fingerprint == null) throw new InvalidKeyException(); Console.WriteLine("\nUsing key {0}", bob.Fingerprint); /////// CHANGE PASSPHRASE /////// Console.WriteLine("Change the secret key's password."); PgpPassphraseOptions passopts = new PgpPassphraseOptions(); /* We need to specify our own passphrase callback methods * in case the user does not use gpg-agent. */ passopts.OldPassphraseCallback = new PassphraseDelegate(MyPassphraseCallback); passopts.NewPassphraseCallback = new PassphraseDelegate(MyNewPassphraseCallback); passopts.EmptyOkay = false; // we do not allow an empty passphrase bob.ChangePassphrase(ctx, passopts); /////// ADD SUBKEY /////// Console.Write("Add a new subkey to Bob's key.. "); /* Set the password callback - needed if the user doesn't run * gpg-agent or any other password / pin-entry software. */ ctx.SetPassphraseFunction(new PassphraseDelegate(MyPassphraseCallback)); PgpSubkeyOptions subopts = new PgpSubkeyOptions(); subopts.Algorithm = PgpSubkeyAlgorithm.RSAEncryptOnly; /* Same as: subopts.SetAlgorithm(KeyAlgorithm.RSA); subopts.Capability = AlgorithmCapability.CanEncrypt; */ subopts.KeyLength = PgpSubkeyOptions.KEY_LENGTH_4096; subopts.ExpirationDate = DateTime.Now.AddDays(90); bob.AddSubkey(ctx, subopts); Console.WriteLine("Done."); /////// VIEW SUBKEYS /////// // Reload Bobs key bob = (PgpKey)keyring.GetKey(bob.Fingerprint, false); Console.WriteLine("Bob has now the following sub keys:"); int subkeycount = 0; foreach (Subkey subkey in bob.Subkeys) { subkeycount++; Console.WriteLine("{0}\n\tAlgorithm: {1}\n\t" + "Length: {2}\n\t" + "Expires: {3}\n", subkey.Fingerprint, Gpgme.GetPubkeyAlgoName(subkey.PubkeyAlgorithm), subkey.Length.ToString(), subkey.Expires.ToString()); } Console.WriteLine("Found {0} sub keys.", subkeycount.ToString()); /////// SET OWNER TRUST /////// Console.WriteLine("Set owner trust of Bob's key."); Console.Write("\tto never.. "); bob.SetOwnerTrust(ctx, PgpOwnerTrust.Never); Console.WriteLine("done."); Console.Write("\tto ultimate.. "); bob.SetOwnerTrust(ctx, PgpOwnerTrust.Ultimate); Console.WriteLine("done."); /////// ENABLE / DISABLE /////// Console.Write("Disable Bob's key.. "); bob.Disable(ctx); Console.WriteLine("done."); Console.Write("Enable Bob's key.. "); bob.Enable(ctx); Console.WriteLine("done."); /////// SET EXPIRE DATE /////// DateTime newdate = DateTime.Now.AddYears(5); Console.WriteLine("Set new expire date: {0}", newdate); PgpExpirationOptions expopts = new PgpExpirationOptions(); expopts.ExpirationDate = newdate; expopts.SelectedSubkeys = null; // only the primary key bob.SetExpirationDate(ctx, expopts); return; }
public void SetExpirationDate(Context ctx, PgpExpirationOptions options) { if (ctx == null) throw new ArgumentNullException("No context object supplied."); if (!ctx.IsValid) throw new InvalidContextException("An invalid context has been supplied."); if (options == null) throw new ArgumentNullException("No PgpExpireOptions object specified."); lock (settings.passLock) { lock (settings.expireLock) { settings.expireOptions = options; // reset object options.cmdSend = false; // reset subkey index options.nsubkey = 0; // reset enforced quit options.forceQuit = false; KeyEditOp op = KeyEditOp.Expire; GpgmeData data = new GpgmeMemoryData(); int err; try { err = StartEdit(ctx, (IntPtr)op, data); } catch (Exception ex) { throw ex; } gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); switch (errcode) { case gpg_err_code_t.GPG_ERR_NO_ERROR: break; case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE: throw new BadPassphraseException(settings.passSettings.GetPassphraseInfo()); default: throw new GpgmeException("An unknown error occurred. Error: " + err.ToString(), err); } } } }