Skip to content

Commit

Permalink
[corlib] Implements CryptoConfig::AddAlgorithm on all profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Apr 10, 2018
1 parent bf838da commit 363790b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@

using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Collections.Generic;

namespace System.Security.Cryptography {

[ComVisible (true)]
public partial class CryptoConfig {

public static void AddAlgorithm (Type algorithm, params string[] names)
{
throw new PlatformNotSupportedException ();
}

public static void AddOID (string oid, params string[] names)
{
throw new PlatformNotSupportedException ();
Expand Down Expand Up @@ -204,6 +200,16 @@ public static object CreateFromName (string name, params object[] args)
break;
}

lock (lockObject) {
Type algoClass = null;
if (algorithms?.TryGetValue (name, out algoClass) == true) {
try {
return Activator.CreateInstance (algoClass, args);
} catch {
}
}
}

try {
// last resort, the request type might be available (if care is taken for the type not to be linked
// away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
Expand Down Expand Up @@ -271,6 +277,11 @@ public static string MapNameToOID (string name)
return null;
}
}

static void Initialize ()
{
algorithms = new Dictionary<string, Type> (StringComparer.OrdinalIgnoreCase);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,43 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.Collections.Generic;

namespace System.Security.Cryptography {

public partial class CryptoConfig {
public partial class CryptoConfig
{
static readonly object lockObject = new object ();
static Dictionary<string,Type> algorithms;

public static void AddAlgorithm (Type algorithm, params string[] names)
{
if (algorithm == null)
throw new ArgumentNullException (nameof (algorithm));
if (!algorithm.IsVisible)
throw new ArgumentException ("Algorithms added to CryptoConfig must be accessable from outside their assembly.", nameof (algorithm));
if (names == null)
throw new ArgumentNullException (nameof (names));

var algorithmNames = new string [names.Length];
Array.Copy (names, algorithmNames, algorithmNames.Length);

foreach (string name in algorithmNames) {
if (string.IsNullOrEmpty (name)) {
throw new ArgumentException ("CryptoConfig cannot add a mapping for a null or empty name.");
}
}

lock (lockObject) {
if (algorithms == null) {
Initialize ();
}

foreach (string name in algorithmNames) {
algorithms [name] = algorithm;
}
}
}

public static byte[] EncodeOID (string str)
{
Expand Down
22 changes: 0 additions & 22 deletions mcs/class/corlib/System.Security.Cryptography/CryptoConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace System.Security.Cryptography {
[ComVisible (true)]
public partial class CryptoConfig {

static private object lockObject;
static private Dictionary<string,Type> algorithms;
static private Dictionary<string,string> unresolved_algorithms;
static private Dictionary<string,string> oids;

Expand Down Expand Up @@ -281,12 +279,6 @@ public partial class CryptoConfig {
// SHA512 provider
const string nameSHA512Provider = "System.Security.Cryptography.SHA512CryptoServiceProvider";
const string defaultSHA512Provider = "System.Security.Cryptography.SHA512CryptoServiceProvider" + system_core_assembly;
static CryptoConfig ()
{
// lock(this) is bad
// http://msdn.microsoft.com/library/en-us/dnaskdr/html/askgui06032003.asp?frame=true
lockObject = new object ();
}

private static void Initialize ()
{
Expand Down Expand Up @@ -562,20 +554,6 @@ public static string MapNameToOID (string name)
return result;
}

public static void AddAlgorithm (Type algorithm, params string[] names)
{
if (algorithm == null)
throw new ArgumentNullException ("algorithm");
if (names == null)
throw new ArgumentNullException ("names");

foreach (string name in names) {
if (String.IsNullOrWhiteSpace (name))
throw new ArithmeticException ("names");
algorithms [name] = algorithm;
}
}

public static void AddOID (string oid, params string[] names)
{
if (oid == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class CryptoConfigTest {

public class FakeAlgorithm
{
}

void CreateFromName (string name, string objectname)
{
object o = CryptoConfig.CreateFromName (name);
Expand Down Expand Up @@ -402,6 +406,14 @@ public void CCToString ()
CryptoConfig cc = new CryptoConfig ();
Assert.AreEqual ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
}

[Test]
public void AddAlgorithm ()
{
CryptoConfig.AddAlgorithm (typeof (FakeAlgorithm), "test");
Assert.IsNotNull (CryptoConfig.CreateFromName ("test"));

}
}

}

0 comments on commit 363790b

Please sign in to comment.