2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / Mono.Security / ASN1Convert.cs
blob3a1cf930183b92155e5fd1b872a803f6496de0e5
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-2007 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;
33 using System.Collections;
34 using System.Globalization;
35 using System.Security.Cryptography;
36 using System.Text;
38 namespace Mono.Security {
40 // References:
41 // a. ITU ASN.1 standards (free download)
42 // http://www.itu.int/ITU-T/studygroups/com17/languages/
44 #if INSIDE_CORLIB
45 internal
46 #else
47 public
48 #endif
49 static class ASN1Convert {
50 // RFC3280, section 4.2.1.5
51 // CAs conforming to this profile MUST always encode certificate
52 // validity dates through the year 2049 as UTCTime; certificate validity
53 // dates in 2050 or later MUST be encoded as GeneralizedTime.
55 // Under 1.x this API requires a Local datetime to be provided
56 // Under 2.0 it will also accept a Utc datetime
57 static public ASN1 FromDateTime (DateTime dt)
59 if (dt.Year < 2050) {
60 // UTCTIME
61 return new ASN1 (0x17, Encoding.ASCII.GetBytes (
62 dt.ToUniversalTime ().ToString ("yyMMddHHmmss",
63 CultureInfo.InvariantCulture) + "Z"));
65 else {
66 // GENERALIZEDTIME
67 return new ASN1 (0x18, Encoding.ASCII.GetBytes (
68 dt.ToUniversalTime ().ToString ("yyyyMMddHHmmss",
69 CultureInfo.InvariantCulture) + "Z"));
73 static public ASN1 FromInt32 (Int32 value)
75 byte[] integer = BitConverterLE.GetBytes (value);
76 Array.Reverse (integer);
77 int x = 0;
78 while ((x < integer.Length) && (integer [x] == 0x00))
79 x++;
80 ASN1 asn1 = new ASN1 (0x02);
81 switch (x) {
82 case 0:
83 asn1.Value = integer;
84 break;
85 case 4:
86 asn1.Value = new byte [1];
87 break;
88 default:
89 byte[] smallerInt = new byte [4 - x];
90 Buffer.BlockCopy (integer, x, smallerInt, 0, smallerInt.Length);
91 asn1.Value = smallerInt;
92 break;
94 return asn1;
97 static public ASN1 FromOid (string oid)
99 if (oid == null)
100 throw new ArgumentNullException ("oid");
102 return new ASN1 (CryptoConfig.EncodeOID (oid));
105 static public ASN1 FromUnsignedBigInteger (byte[] big)
107 if (big == null)
108 throw new ArgumentNullException ("big");
110 // check for numbers that could be interpreted as negative (first bit)
111 if (big [0] >= 0x80) {
112 // in thie cas we add a new, empty, byte (position 0) so we're
113 // sure this will always be interpreted an unsigned integer.
114 // However we can't feed it into RSAParameters or DSAParameters
115 int length = big.Length + 1;
116 byte[] uinteger = new byte [length];
117 Buffer.BlockCopy (big, 0, uinteger, 1, length - 1);
118 big = uinteger;
120 return new ASN1 (0x02, big);
123 static public int ToInt32 (ASN1 asn1)
125 if (asn1 == null)
126 throw new ArgumentNullException ("asn1");
127 if (asn1.Tag != 0x02)
128 throw new FormatException ("Only integer can be converted");
130 int x = 0;
131 for (int i=0; i < asn1.Value.Length; i++)
132 x = (x << 8) + asn1.Value [i];
133 return x;
136 // Convert a binary encoded OID to human readable string representation of
137 // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann.
138 static public string ToOid (ASN1 asn1)
140 if (asn1 == null)
141 throw new ArgumentNullException ("asn1");
143 byte[] aOID = asn1.Value;
144 StringBuilder sb = new StringBuilder ();
145 // Pick apart the OID
146 byte x = (byte) (aOID[0] / 40);
147 byte y = (byte) (aOID[0] % 40);
148 if (x > 2) {
149 // Handle special case for large y if x = 2
150 y += (byte) ((x - 2) * 40);
151 x = 2;
153 sb.Append (x.ToString (CultureInfo.InvariantCulture));
154 sb.Append (".");
155 sb.Append (y.ToString (CultureInfo.InvariantCulture));
156 ulong val = 0;
157 for (x = 1; x < aOID.Length; x++) {
158 val = ((val << 7) | ((byte) (aOID [x] & 0x7F)));
159 if ( !((aOID [x] & 0x80) == 0x80)) {
160 sb.Append (".");
161 sb.Append (val.ToString (CultureInfo.InvariantCulture));
162 val = 0;
165 return sb.ToString ();
168 static public DateTime ToDateTime (ASN1 time)
170 if (time == null)
171 throw new ArgumentNullException ("time");
173 string t = Encoding.ASCII.GetString (time.Value);
174 // to support both UTCTime and GeneralizedTime (and not so common format)
175 string mask = null;
176 int year;
177 switch (t.Length) {
178 case 11:
179 // illegal format, still it's supported for compatibility
180 mask = "yyMMddHHmmZ";
181 break;
182 case 13:
183 // RFC3280: 4.1.2.5.1 UTCTime
184 year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture);
185 // Where YY is greater than or equal to 50, the
186 // year SHALL be interpreted as 19YY; and
187 // Where YY is less than 50, the year SHALL be
188 // interpreted as 20YY.
189 if (year >= 50)
190 t = "19" + t;
191 else
192 t = "20" + t;
193 mask = "yyyyMMddHHmmssZ";
194 break;
195 case 15:
196 mask = "yyyyMMddHHmmssZ"; // GeneralizedTime
197 break;
198 case 17:
199 // another illegal format (990630000000+1000), again supported for compatibility
200 year = Convert.ToInt16 (t.Substring (0, 2), CultureInfo.InvariantCulture);
201 string century = (year >= 50) ? "19" : "20";
202 // ASN.1 (see ITU X.680 section 43.3) deals with offset differently than .NET
203 char sign = (t[12] == '+') ? '-' : '+';
204 t = String.Format ("{0}{1}{2}{3}{4}:{5}{6}", century, t.Substring (0, 12), sign,
205 t[13], t[14], t[15], t[16]);
206 mask = "yyyyMMddHHmmsszzz";
207 break;
209 return DateTime.ParseExact (t, mask, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);