2 // CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
5 // Sebastien Pouliot (sebastien@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) 2004-2007, 2009 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
.Generic
;
34 namespace System
.Security
.Cryptography
{
36 public partial class CryptoConfig
38 static readonly object lockObject
= new object ();
39 static Dictionary
<string,Type
> algorithms
;
41 public static void AddAlgorithm (Type algorithm
, params string[] names
)
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
));
48 throw new ArgumentNullException (nameof (names
));
50 var algorithmNames
= new string [names
.Length
];
51 Array
.Copy (names
, algorithmNames
, algorithmNames
.Length
);
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.");
60 if (algorithms
== null) {
64 foreach (string name
in algorithmNames
) {
65 algorithms
[name
] = algorithm
;
70 public static byte[] EncodeOID (string str
)
73 throw new ArgumentNullException ("str");
74 char[] delim
= { '.' }
;
75 string[] parts
= str
.Split (delim
);
76 // according to X.208 n is always at least 2
77 if (parts
.Length
< 2) {
78 throw new CryptographicUnexpectedOperationException (
79 Locale
.GetText ("OID must have at least two parts"));
82 // we're sure that the encoded OID is shorter than its string representation
83 byte[] oid
= new byte [str
.Length
];
86 byte part0
= Convert
.ToByte (parts
[0]);
87 // OID[0] > 2 is invalid but "supported" in MS BCL
88 // uncomment next line to trap this error
89 // if (part0 > 2) throw new CryptographicUnexpectedOperationException ();
90 byte part1
= Convert
.ToByte (parts
[1]);
91 // OID[1] >= 40 is illegal for OID[0] < 2 because of the % 40
92 // however the syntax is "supported" in MS BCL
93 // uncomment next 2 lines to trap this error
94 //if ((part0 < 2) && (part1 >= 40))
95 // throw new CryptographicUnexpectedOperationException ();
96 oid
[2] = Convert
.ToByte (part0
* 40 + part1
);
99 throw new CryptographicUnexpectedOperationException (
100 Locale
.GetText ("Invalid OID"));
103 for (int i
= 2; i
< parts
.Length
; i
++) {
104 long x
= Convert
.ToInt64 (parts
[i
]);
106 byte[] num
= EncodeLongNumber (x
);
107 Buffer
.BlockCopy (num
, 0, oid
, j
, num
.Length
);
111 oid
[j
++] = Convert
.ToByte (x
);
115 // copy the exact number of byte required
116 byte[] oid2
= new byte [j
];
117 oid2
[0] = 0x06; // always - this tag means OID
120 // for compatibility with MS BCL
121 throw new CryptographicUnexpectedOperationException (
122 Locale
.GetText ("OID > 127 bytes"));
123 // comment exception and uncomment next 3 lines to remove restriction
124 //byte[] num = EncodeLongNumber (j);
125 //Buffer.BlockCopy (num, 0, oid, j, num.Length);
126 //k = num.Length + 1;
129 oid2
[1] = Convert
.ToByte (j
- 2);
131 Buffer
.BlockCopy (oid
, k
, oid2
, k
, j
- k
);
135 // encode (7bits array) number greater than 127
136 private static byte[] EncodeLongNumber (long x
)
138 // for MS BCL compatibility
139 // comment next two lines to remove restriction
140 if ((x
> Int32
.MaxValue
) || (x
< Int32
.MinValue
))
141 throw new OverflowException (Locale
.GetText ("Part of OID doesn't fit in Int32"));
144 // number of bytes required to encode this number
150 byte[] num
= new byte [n
];
152 for (int i
= 0; i
< n
; i
++) {
157 num
[n
-i
-1] = Convert
.ToByte (y
);
162 [MonoLimitation ("nothing is FIPS certified so it never make sense to restrict to this (empty) subset")]
163 public static bool AllowOnlyFipsAlgorithms
{
164 get { return false; }