Skip to content

Commit

Permalink
Refactored OpenPgpContext to separate out key storage implementation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hansen authored Jun 4, 2020
1 parent 7090df5 commit 878090d
Show file tree
Hide file tree
Showing 6 changed files with 1,893 additions and 1,680 deletions.
14 changes: 7 additions & 7 deletions MimeKit/Cryptography/CryptographyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class CryptographyContext : IDisposable
{
const string SubclassAndRegisterFormat = "You need to subclass {0} and then register it with MimeKit.Cryptography.CryptographyContext.Register().";
static Func<SecureMimeContext> SecureMimeContextFactory;
static Func<OpenPgpContext> OpenPgpContextFactory;
static Func<PgpContext> PgpContextFactory;
static readonly object mutex = new object ();

EncryptionAlgorithm[] encryptionAlgorithmRank;
Expand Down Expand Up @@ -549,8 +549,8 @@ public static CryptographyContext Create (string protocol)
case "application/pgp-encrypted":
case "application/x-pgp-keys":
case "application/pgp-keys":
if (OpenPgpContextFactory != null)
return OpenPgpContextFactory ();
if (PgpContextFactory != null)
return PgpContextFactory ();

throw new NotSupportedException (string.Format (SubclassAndRegisterFormat, "MimeKit.Cryptography.OpenPgpContext or MimeKit.Cryptography.GnuPGContext"));
default:
Expand Down Expand Up @@ -596,9 +596,9 @@ public static void Register (Type type)
lock (mutex) {
SecureMimeContextFactory = () => (SecureMimeContext) ctor.Invoke (new object[0]);
}
} else if (info.IsSubclassOf (typeof (OpenPgpContext))) {
} else if (info.IsSubclassOf (typeof (PgpContext))) {
lock (mutex) {
OpenPgpContextFactory = () => (OpenPgpContext) ctor.Invoke (new object[0]);
PgpContextFactory = () => (PgpContext) ctor.Invoke (new object[0]);
}
} else {
throw new ArgumentException ("The specified type must be a subclass of SecureMimeContext or OpenPgpContext.", nameof (type));
Expand Down Expand Up @@ -635,13 +635,13 @@ public static void Register (Func<SecureMimeContext> factory)
/// <exception cref="System.ArgumentNullException">
/// <paramref name="factory"/> is <c>null</c>.
/// </exception>
public static void Register (Func<OpenPgpContext> factory)
public static void Register (Func<PgpContext> factory)
{
if (factory == null)
throw new ArgumentNullException(nameof (factory));

lock (mutex) {
OpenPgpContextFactory = factory;
PgpContextFactory = factory;
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions MimeKit/Cryptography/KeyRetrievalResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// OpenPgpContext.cs
//
// Author: Jeffrey Stedfast <[email protected]>
//
// Copyright (c) 2013-2020 Xamarin Inc. (www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using Org.BouncyCastle.Bcpg.OpenPgp;

namespace MimeKit.Cryptography
{
public abstract partial class PgpContext
{
/// <summary>
/// Helper class to return both public keyring and public key associated with each other.
/// </summary>
public class KeyRetrievalResults
{
public readonly PgpPublicKeyRing KeyRing;
public readonly PgpPublicKey Key;

public KeyRetrievalResults (PgpPublicKeyRing keyring, PgpPublicKey pubkey)
{
KeyRing = keyring;
Key = pubkey;
}
}
}
}
Loading

0 comments on commit 878090d

Please sign in to comment.