Skip to content

Commit 363790b

Browse files
committed
[corlib] Implements CryptoConfig::AddAlgorithm on all profiles
1 parent bf838da commit 363790b

4 files changed

Lines changed: 63 additions & 28 deletions

File tree

‎mcs/class/corlib/System.Security.Cryptography/CryptoConfig.Mobile.cs‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@
3838

3939
using System.Runtime.InteropServices;
4040
using System.Security.Permissions;
41+
using System.Collections.Generic;
4142

4243
namespace System.Security.Cryptography {
4344

4445
[ComVisible (true)]
4546
public partial class CryptoConfig {
4647

47-
public static void AddAlgorithm (Type algorithm, params string[] names)
48-
{
49-
throw new PlatformNotSupportedException ();
50-
}
51-
5248
public static void AddOID (string oid, params string[] names)
5349
{
5450
throw new PlatformNotSupportedException ();
@@ -204,6 +200,16 @@ public static object CreateFromName (string name, params object[] args)
204200
break;
205201
}
206202

203+
lock (lockObject) {
204+
Type algoClass = null;
205+
if (algorithms?.TryGetValue (name, out algoClass) == true) {
206+
try {
207+
return Activator.CreateInstance (algoClass, args);
208+
} catch {
209+
}
210+
}
211+
}
212+
207213
try {
208214
// last resort, the request type might be available (if care is taken for the type not to be linked
209215
// away) and that can allow some 3rd party code to work (e.g. extra algorithms) and make a few more
@@ -271,6 +277,11 @@ public static string MapNameToOID (string name)
271277
return null;
272278
}
273279
}
280+
281+
static void Initialize ()
282+
{
283+
algorithms = new Dictionary<string, Type> (StringComparer.OrdinalIgnoreCase);
284+
}
274285
}
275286
}
276287

‎mcs/class/corlib/System.Security.Cryptography/CryptoConfig.common.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,43 @@
2929
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3030
//
3131

32+
using System.Collections.Generic;
33+
3234
namespace System.Security.Cryptography {
3335

34-
public partial class CryptoConfig {
36+
public partial class CryptoConfig
37+
{
38+
static readonly object lockObject = new object ();
39+
static Dictionary<string,Type> algorithms;
40+
41+
public static void AddAlgorithm (Type algorithm, params string[] names)
42+
{
43+
if (algorithm == null)
44+
throw new ArgumentNullException (nameof (algorithm));
45+
if (!algorithm.IsVisible)
46+
throw new ArgumentException ("Algorithms added to CryptoConfig must be accessable from outside their assembly.", nameof (algorithm));
47+
if (names == null)
48+
throw new ArgumentNullException (nameof (names));
49+
50+
var algorithmNames = new string [names.Length];
51+
Array.Copy (names, algorithmNames, algorithmNames.Length);
52+
53+
foreach (string name in algorithmNames) {
54+
if (string.IsNullOrEmpty (name)) {
55+
throw new ArgumentException ("CryptoConfig cannot add a mapping for a null or empty name.");
56+
}
57+
}
58+
59+
lock (lockObject) {
60+
if (algorithms == null) {
61+
Initialize ();
62+
}
63+
64+
foreach (string name in algorithmNames) {
65+
algorithms [name] = algorithm;
66+
}
67+
}
68+
}
3569

3670
public static byte[] EncodeOID (string str)
3771
{

‎mcs/class/corlib/System.Security.Cryptography/CryptoConfig.cs‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ namespace System.Security.Cryptography {
5050
[ComVisible (true)]
5151
public partial class CryptoConfig {
5252

53-
static private object lockObject;
54-
static private Dictionary<string,Type> algorithms;
5553
static private Dictionary<string,string> unresolved_algorithms;
5654
static private Dictionary<string,string> oids;
5755

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

291283
private static void Initialize ()
292284
{
@@ -562,20 +554,6 @@ public static string MapNameToOID (string name)
562554
return result;
563555
}
564556

565-
public static void AddAlgorithm (Type algorithm, params string[] names)
566-
{
567-
if (algorithm == null)
568-
throw new ArgumentNullException ("algorithm");
569-
if (names == null)
570-
throw new ArgumentNullException ("names");
571-
572-
foreach (string name in names) {
573-
if (String.IsNullOrWhiteSpace (name))
574-
throw new ArithmeticException ("names");
575-
algorithms [name] = algorithm;
576-
}
577-
}
578-
579557
public static void AddOID (string oid, params string[] names)
580558
{
581559
if (oid == null)

‎mcs/class/corlib/Test/System.Security.Cryptography/CryptoConfigTest.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ namespace MonoTests.System.Security.Cryptography {
3737
[TestFixture]
3838
public class CryptoConfigTest {
3939

40+
public class FakeAlgorithm
41+
{
42+
}
43+
4044
void CreateFromName (string name, string objectname)
4145
{
4246
object o = CryptoConfig.CreateFromName (name);
@@ -402,6 +406,14 @@ public void CCToString ()
402406
CryptoConfig cc = new CryptoConfig ();
403407
Assert.AreEqual ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
404408
}
409+
410+
[Test]
411+
public void AddAlgorithm ()
412+
{
413+
CryptoConfig.AddAlgorithm (typeof (FakeAlgorithm), "test");
414+
Assert.IsNotNull (CryptoConfig.CreateFromName ("test"));
415+
416+
}
405417
}
406418

407419
}

0 commit comments

Comments
 (0)