Merge remote-tracking branch 'upstream/master'
[mono.git] / mcs / class / System / System.Security.Cryptography.X509Certificates / X500DistinguishedName.cs
1 //
2 // System.Security.Cryptography.X509Certificates.X500DistinguishedName
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004-2006 Novell Inc. (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if SECURITY_DEP
30
31 extern alias MonoSecurity;
32
33 using System.Collections;
34 using System.Text;
35
36 using MonoSecurity::Mono.Security;
37 using MX = MonoSecurity::Mono.Security.X509;
38
39 namespace System.Security.Cryptography.X509Certificates {
40
41         [MonoTODO ("Some X500DistinguishedNameFlags options aren't supported, like DoNotUsePlusSign, DoNotUseQuotes and ForceUTF8Encoding")]
42         public sealed class X500DistinguishedName : AsnEncodedData {
43
44                 private const X500DistinguishedNameFlags AllFlags = X500DistinguishedNameFlags.Reversed |
45                         X500DistinguishedNameFlags.UseSemicolons | X500DistinguishedNameFlags.DoNotUsePlusSign | 
46                         X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.UseCommas | 
47                         X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.UseUTF8Encoding | 
48                         X500DistinguishedNameFlags.UseT61Encoding | X500DistinguishedNameFlags.ForceUTF8Encoding;
49
50                 private string name;
51
52
53                 public X500DistinguishedName (AsnEncodedData encodedDistinguishedName)
54                 {
55                         if (encodedDistinguishedName == null)
56                                 throw new ArgumentNullException ("encodedDistinguishedName");
57
58                         RawData = encodedDistinguishedName.RawData;
59                         if (RawData.Length > 0)
60                                 DecodeRawData ();
61                         else
62                                 name = String.Empty;
63                 }
64
65                 public X500DistinguishedName (byte[] encodedDistinguishedName)
66                 {
67                         if (encodedDistinguishedName == null)
68                                 throw new ArgumentNullException ("encodedDistinguishedName");
69
70                         Oid = new Oid ();
71                         RawData = encodedDistinguishedName;
72                         if (encodedDistinguishedName.Length > 0)
73                                 DecodeRawData ();
74                         else
75                                 name = String.Empty;
76                 }
77
78                 public X500DistinguishedName (string distinguishedName)
79                         : this (distinguishedName, X500DistinguishedNameFlags.Reversed)
80                 {
81                 }
82
83                 public X500DistinguishedName (string distinguishedName, X500DistinguishedNameFlags flag)
84                 {
85                         if (distinguishedName == null)
86                                 throw new ArgumentNullException ("distinguishedName");
87                         if ((flag != 0) && ((flag & AllFlags) == 0))
88                                 throw new ArgumentException ("flag");
89
90                         Oid = new Oid ();
91                         if (distinguishedName.Length == 0) {
92                                 // empty (0x00) ASN.1 sequence (0x30)
93                                 RawData = new byte [2] { 0x30, 0x00 };
94                                 DecodeRawData ();
95                         } else {
96                                 var dn = MX.X501.FromString (distinguishedName);
97                                 if ((flag & X500DistinguishedNameFlags.Reversed) != 0) {
98                                         ASN1 rdn = new ASN1 (0x30);
99                                         for (int i = dn.Count - 1; i >= 0; i--) 
100                                                 rdn.Add (dn [i]);
101                                         dn = rdn;
102                                 }
103                                 RawData = dn.GetBytes ();
104                                 if (flag == X500DistinguishedNameFlags.None)
105                                         name = distinguishedName;
106                                 else
107                                         name = Decode (flag);
108                         }
109                 }
110
111                 public X500DistinguishedName (X500DistinguishedName distinguishedName)
112                 {
113                         if (distinguishedName == null)
114                                 throw new ArgumentNullException ("distinguishedName");
115
116                         Oid = new Oid ();
117                         RawData = distinguishedName.RawData;
118                         name = distinguishedName.name;
119                 }
120
121
122                 public string Name {
123                         get { return name; }
124                 }
125
126
127                 public string Decode (X500DistinguishedNameFlags flag)
128                 {
129                         if ((flag != 0) && ((flag & AllFlags) == 0))
130                                 throw new ArgumentException ("flag");
131
132                         if (RawData.Length == 0)
133                                 return String.Empty;
134
135                         // Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
136                         bool reversed = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
137                         bool quotes = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
138                         string separator = GetSeparator (flag);
139
140                         ASN1 rdn = new ASN1 (RawData);
141                         return MX.X501.ToString (rdn, reversed, separator, quotes);
142                 }
143
144                 public override string Format (bool multiLine)
145                 {
146                         if (multiLine) {
147                                 string s = Decode (X500DistinguishedNameFlags.UseNewLines);
148                                 if (s.Length > 0)
149                                         return s + Environment.NewLine;
150                                 else
151                                         return s;
152                         } else {
153                                 return Decode (X500DistinguishedNameFlags.UseCommas);
154                         }
155                 }
156
157                 // private stuff
158
159                 private static string GetSeparator (X500DistinguishedNameFlags flag)
160                 {
161                         if ((flag & X500DistinguishedNameFlags.UseSemicolons) != 0)
162                                 return "; ";
163                         if ((flag & X500DistinguishedNameFlags.UseCommas) != 0)
164                                 return ", ";
165                         if ((flag & X500DistinguishedNameFlags.UseNewLines) != 0)
166                                 return Environment.NewLine;
167                         return ", "; //default
168                 }
169
170                 // decode the DN using the (byte[]) RawData
171                 private void DecodeRawData ()
172                 {
173                         if ((RawData == null) || (RawData.Length < 3)) {
174                                 name = String.Empty;
175                                 return;
176                         }
177
178                         ASN1 sequence = new ASN1 (RawData);
179                         name = MX.X501.ToString (sequence, true, ", ", true);
180                 }
181
182                 private static string Canonize (string s)
183                 {
184                         int i = s.IndexOf ('=') + 1;
185                         StringBuilder r = new StringBuilder (s.Substring (0, i));
186                         // skip any white space starting the value
187                         while (i < s.Length && Char.IsWhiteSpace (s, i))
188                                 i++;
189                         // ensure we skip white spaces at the end of the value
190                         s = s.TrimEnd ();
191                         // keep track of internal multiple spaces
192                         bool space = false;
193                         for (; i < s.Length; i++) {
194                                 if (space) {
195                                         space = Char.IsWhiteSpace (s, i);
196                                         if (space)
197                                                 continue;
198                                 }
199                                 if (Char.IsWhiteSpace (s, i))
200                                         space = true;
201                                 r.Append (Char.ToUpperInvariant (s[i]));
202                         }
203                         return r.ToString ();
204                 }
205
206                 // of all X500DistinguishedNameFlags flags nothing can do a "correct" comparison :|
207                 internal static bool AreEqual (X500DistinguishedName name1, X500DistinguishedName name2)
208                 {
209                         if (name1 == null)
210                                 return (name2 == null);
211                         if (name2 == null)
212                                 return false;
213
214                         X500DistinguishedNameFlags flags = X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.DoNotUseQuotes;
215                         string[] split = new string[] { Environment.NewLine };
216                         string[] parts1 = name1.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
217                         string[] parts2 = name2.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
218                         if (parts1.Length != parts2.Length)
219                                 return false;
220
221                         for (int i = 0; i < parts1.Length; i++) {
222                                 if (Canonize (parts1[i]) != Canonize (parts2[i]))
223                                         return false;
224                         }
225                         return true;
226                 }
227         }
228 }
229
230 #endif