[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / CryptoConfig.common.cs
blob769279d1c69915f0026f4e0bec6cbb3005149351
1 //
2 // CryptoConfig.cs: Handles cryptographic implementations and OIDs mappings.
3 //
4 // Author:
5 // Sebastien Pouliot (sebastien@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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));
47 if (names == null)
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.");
59 lock (lockObject) {
60 if (algorithms == null) {
61 Initialize ();
64 foreach (string name in algorithmNames) {
65 algorithms [name] = algorithm;
70 public static byte[] EncodeOID (string str)
72 if (str == null)
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];
84 // now encoding value
85 try {
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);
98 catch {
99 throw new CryptographicUnexpectedOperationException (
100 Locale.GetText ("Invalid OID"));
102 int j = 3;
103 for (int i = 2; i < parts.Length; i++) {
104 long x = Convert.ToInt64 (parts [i]);
105 if (x > 0x7F) {
106 byte[] num = EncodeLongNumber (x);
107 Buffer.BlockCopy (num, 0, oid, j, num.Length);
108 j += num.Length;
110 else
111 oid[j++] = Convert.ToByte (x);
114 int k = 2;
115 // copy the exact number of byte required
116 byte[] oid2 = new byte [j];
117 oid2[0] = 0x06; // always - this tag means OID
118 // Length (of value)
119 if (j > 0x7F) {
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;
128 else
129 oid2 [1] = Convert.ToByte (j - 2);
131 Buffer.BlockCopy (oid, k, oid2, k, j - k);
132 return oid2;
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"));
143 long y = x;
144 // number of bytes required to encode this number
145 int n = 1;
146 while (y > 0x7F) {
147 y = y >> 7;
148 n++;
150 byte[] num = new byte [n];
151 // encode all bytes
152 for (int i = 0; i < n; i++) {
153 y = x >> (7 * i);
154 y = y & 0x7F;
155 if (i != 0)
156 y += 0x80;
157 num[n-i-1] = Convert.ToByte (y);
159 return num;
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; }