2 // ASN1Convert.cs: Abstract Syntax Notation 1 convertion routines
5 // Sebastien Pouliot (spouliot@motus.com)
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
11 using System.Collections;
12 using System.Security.Cryptography;
15 namespace Mono.Security {
18 // a. ITU ASN.1 standards (free download)
19 // http://www.itu.int/ITU-T/studygroups/com17/languages/
28 // RFC3280, section 4.2.1.5
29 // CAs conforming to this profile MUST always encode certificate
30 // validity dates through the year 2049 as UTCTime; certificate validity
31 // dates in 2050 or later MUST be encoded as GeneralizedTime.
32 static public ASN1 FromDateTime (DateTime dt)
36 return new ASN1 (0x17, Encoding.ASCII.GetBytes (dt.ToString ("yyMMddHHmmss") + "Z"));
40 return new ASN1 (0x18, Encoding.ASCII.GetBytes (dt.ToString ("yyyyMMddHHmmss") + "Z"));
44 static public ASN1 FromInt32 (Int32 value)
46 byte[] integer = BitConverter.GetBytes (value);
48 while (integer [x] == 0x00)
50 ASN1 asn1 = new ASN1 (0x02);
54 byte[] smallerInt = new byte [x + 1];
55 Array.Copy (integer, 0, smallerInt, 0, smallerInt.Length);
56 asn1.Value = smallerInt;
61 static public ASN1 FromOID (string oid)
63 return new ASN1 (CryptoConfig.EncodeOID (oid));
66 static public ASN1 FromUnsignedBigInteger (byte[] integer)
68 if (integer [0] == 0x00) {
69 // this first byte is added so we're sure it's an unsigned integer
70 // however we can't feed it into RSAParameters or DSAParameters
71 int length = integer.Length + 1;
72 byte[] uinteger = new byte [length];
73 Array.Copy (integer, 0, uinteger, 1, length);
76 return new ASN1 (0x02, integer);
79 static public int ToInt32 (ASN1 asn1)
82 throw new NotSupportedException ("Only integer can be converted");
84 for (int i=0; i < asn1.Value.Length; i++)
85 x = (x << 8) + asn1.Value [i];
89 // Convert a binary encoded OID to human readable string representation of
90 // an OID (IETF style). Based on DUMPASN1.C from Peter Gutmann.
91 static public string ToOID (ASN1 asn1)
93 byte[] aOID = asn1.Value;
94 StringBuilder sb = new StringBuilder ();
96 byte x = (byte) (aOID[0] / 40);
97 byte y = (byte) (aOID[0] % 40);
99 // Handle special case for large y if x = 2
100 y += (byte) ((x - 2) * 40);
103 sb.Append (x.ToString ());
105 sb.Append (y.ToString ());
107 for (x = 1; x < aOID.Length; x++) {
108 val = ((val << 7) | ((byte) (aOID [x] & 0x7F)));
109 if ( !((aOID [x] & 0x80) == 0x80)) {
111 sb.Append (val.ToString ());
115 return sb.ToString ();
118 static public DateTime ToDateTime (ASN1 time)
120 string t = Encoding.ASCII.GetString (time.Value);
121 // to support both UTCTime and GeneralizedTime (and not so common format)
125 mask = "yyMMddHHmmZ"; // illegal I think ... must check
128 // RFC3280: 4.1.2.5.1 UTCTime
129 int year = Convert.ToInt16 (t.Substring (0, 2));
130 // Where YY is greater than or equal to 50, the
131 // year SHALL be interpreted as 19YY; and
132 // Where YY is less than 50, the year SHALL be
133 // interpreted as 20YY.
138 mask = "yyyyMMddHHmmssZ";
141 mask = "yyyyMMddHHmmssZ"; // GeneralizedTime
144 return DateTime.ParseExact (t, mask, null);