New test.
[mono.git] / mcs / class / corlib / Mono.Security / ASN1Convert.cs
1 //
2 // ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Jesper Pedersen  <jep@itplus.dk>
7 //
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 IT+ A/S (http://www.itplus.dk)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
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.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Globalization;
35 using System.Security.Cryptography;
36 using System.Text;
37
38 namespace Mono.Security {
39
40         // References:
41         // a.   ITU ASN.1 standards (free download)
42         //      http://www.itu.int/ITU-T/studygroups/com17/languages/
43
44 #if INSIDE_CORLIB
45         internal
46 #else
47         public
48 #endif
49 #if NET_2_0
50         static class ASN1Convert {
51 #else
52         sealed class ASN1Convert {
53
54                 private ASN1Convert ()
55                 {
56                 }
57 #endif
58                 // RFC3280, section 4.2.1.5
59                 // CAs conforming to this profile MUST always encode certificate
60                 // validity dates through the year 2049 as UTCTime; certificate validity
61                 // dates in 2050 or later MUST be encoded as GeneralizedTime.
62                 static public ASN1 FromDateTime (DateTime dt) 
63                 {
64                         if (dt.Year < 2050) {
65                                 // UTCTIME
66                                 return new ASN1 (0x17, Encoding.ASCII.GetBytes (
67                                         dt.ToUniversalTime ().ToString ("yyMMddHHmmss",
68                                         CultureInfo.InvariantCulture) + "Z"));
69                         }
70                         else {
71                                 // GENERALIZEDTIME
72                                 return new ASN1 (0x18, Encoding.ASCII.GetBytes (
73                                         dt.ToUniversalTime ().ToString ("yyyyMMddHHmmss", 
74                                         CultureInfo.InvariantCulture) + "Z"));
75                         }
76                 }
77
78                 static public ASN1 FromInt32 (Int32 value) 
79                 {
80                         byte[] integer = BitConverterLE.GetBytes (value);
81                         Array.Reverse (integer);
82                         int x = 0;
83                         while ((x < integer.Length) && (integer [x] == 0x00))
84                                 x++;
85                         ASN1 asn1 = new ASN1 (0x02);
86                         switch (x) {
87                         case 0:
88                                 asn1.Value = integer;
89                                 break;
90                         case 4:
91                                 asn1.Value = new byte [1];
92                                 break;
93                         default:
94                                 byte[] smallerInt = new byte [4 - x];
95                                 Buffer.BlockCopy (integer, x, smallerInt, 0, smallerInt.Length);
96                                 asn1.Value = smallerInt;
97                                 break;
98                         }
99                         return asn1;
100                 }
101
102                 static public ASN1 FromOid (string oid) 
103                 {
104                         if (oid == null)
105                                 throw new ArgumentNullException ("oid");
106
107                         return new ASN1 (CryptoConfig.EncodeOID (oid));
108                 }
109
110                 static public ASN1 FromUnsignedBigInteger (byte[] big) 
111                 {
112                         if (big == null)
113                                 throw new ArgumentNullException ("big");
114
115                         // check for numbers that could be interpreted as negative (first bit)
116                         if (big [0] >= 0x80) {
117                                 // in thie cas we add a new, empty, byte (position 0) so we're
118                                 // sure this will always be interpreted an unsigned integer.
119                                 // However we can't feed it into RSAParameters or DSAParameters
120                                 int length = big.Length + 1;
121                                 byte[] uinteger = new byte [length];
122                                 Buffer.BlockCopy (big, 0, uinteger, 1, length - 1);
123                                 big = uinteger;
124                         }
125                         return new ASN1 (0x02, big);
126                 }
127
128                 static public int ToInt32 (ASN1 asn1) 
129                 {
130                         if (asn1 == null)
131                                 throw new ArgumentNullException ("asn1");
132                         if (asn1.Tag != 0x02)
133                                 throw new FormatException ("Only integer can be converted");
134
135                         int x = 0;
136                         for (int i=0; i < asn1.Value.Length; i++)
137                                 x = (x << 8) + asn1.Value [i];
138                         return x;
139                 }
140
141                 // Convert a binary encoded OID to human readable string representation of 
142                 // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann.
143                 static public string ToOid (ASN1 asn1) 
144                 {
145                         if (asn1 == null)
146                                 throw new ArgumentNullException ("asn1");
147
148                         byte[] aOID = asn1.Value;
149                         StringBuilder sb = new StringBuilder ();
150                         // Pick apart the OID
151                         byte x = (byte) (aOID[0] / 40);
152                         byte y = (byte) (aOID[0] % 40);
153                         if (x > 2) {
154                                 // Handle special case for large y if x = 2
155                                 y += (byte) ((x - 2) * 40);
156                                 x = 2;
157                         }
158                         sb.Append (x.ToString (CultureInfo.InvariantCulture));
159                         sb.Append (".");
160                         sb.Append (y.ToString (CultureInfo.InvariantCulture));
161                         ulong val = 0;
162                         for (x = 1; x < aOID.Length; x++) {
163                                 val = ((val << 7) | ((byte) (aOID [x] & 0x7F)));
164                                 if ( !((aOID [x] & 0x80) == 0x80)) {
165                                         sb.Append (".");
166                                         sb.Append (val.ToString (CultureInfo.InvariantCulture));
167                                         val = 0;
168                                 }
169                         }
170                         return sb.ToString ();
171                 }
172
173                 static public DateTime ToDateTime (ASN1 time) 
174                 {
175                         if (time == null)
176                                 throw new ArgumentNullException ("time");
177
178                         string t = Encoding.ASCII.GetString (time.Value);
179                         // to support both UTCTime and GeneralizedTime (and not so common format)
180                         string mask = null;
181                         switch (t.Length) {
182                                 case 11:
183                                         mask = "yyMMddHHmmZ"; // illegal I think ... must check
184                                         break;
185                                 case 13: 
186                                         // RFC3280: 4.1.2.5.1  UTCTime
187                                         int year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture);
188                                         // Where YY is greater than or equal to 50, the 
189                                         // year SHALL be interpreted as 19YY; and 
190                                         // Where YY is less than 50, the year SHALL be 
191                                         // interpreted as 20YY.
192                                         if (year >= 50)
193                                                 t = "19" + t;
194                                         else
195                                                 t = "20" + t;
196                                         mask = "yyyyMMddHHmmssZ";
197                                         break;
198                                 case 15:
199                                         mask = "yyyyMMddHHmmssZ"; // GeneralizedTime
200                                         break;
201                         }
202                         return DateTime.ParseExact (t, mask, null);
203                 }
204         }
205 }