Merge pull request #823 from DavidKarlas/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 #if MONOTOUCH
32 using Mono.Security;
33 using MX = Mono.Security.X509;
34 #else
35 extern alias MonoSecurity;
36 using MonoSecurity::Mono.Security;
37 using MX = MonoSecurity::Mono.Security.X509;
38 #endif
39
40 using System.Collections;
41 using System.Text;
42
43 namespace System.Security.Cryptography.X509Certificates {
44
45         [MonoTODO ("Some X500DistinguishedNameFlags options aren't supported, like DoNotUsePlusSign, DoNotUseQuotes and ForceUTF8Encoding")]
46         public sealed class X500DistinguishedName : AsnEncodedData {
47
48                 private const X500DistinguishedNameFlags AllFlags = X500DistinguishedNameFlags.Reversed |
49                         X500DistinguishedNameFlags.UseSemicolons | X500DistinguishedNameFlags.DoNotUsePlusSign | 
50                         X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.UseCommas | 
51                         X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.UseUTF8Encoding | 
52                         X500DistinguishedNameFlags.UseT61Encoding | X500DistinguishedNameFlags.ForceUTF8Encoding;
53
54                 private string name;
55
56
57                 public X500DistinguishedName (AsnEncodedData encodedDistinguishedName)
58                 {
59                         if (encodedDistinguishedName == null)
60                                 throw new ArgumentNullException ("encodedDistinguishedName");
61
62                         RawData = encodedDistinguishedName.RawData;
63                         if (RawData.Length > 0)
64                                 DecodeRawData ();
65                         else
66                                 name = String.Empty;
67                 }
68
69                 public X500DistinguishedName (byte[] encodedDistinguishedName)
70                 {
71                         if (encodedDistinguishedName == null)
72                                 throw new ArgumentNullException ("encodedDistinguishedName");
73
74                         Oid = new Oid ();
75                         RawData = encodedDistinguishedName;
76                         if (encodedDistinguishedName.Length > 0)
77                                 DecodeRawData ();
78                         else
79                                 name = String.Empty;
80                 }
81
82                 public X500DistinguishedName (string distinguishedName)
83                         : this (distinguishedName, X500DistinguishedNameFlags.Reversed)
84                 {
85                 }
86
87                 public X500DistinguishedName (string distinguishedName, X500DistinguishedNameFlags flag)
88                 {
89                         if (distinguishedName == null)
90                                 throw new ArgumentNullException ("distinguishedName");
91                         if ((flag != 0) && ((flag & AllFlags) == 0))
92                                 throw new ArgumentException ("flag");
93
94                         Oid = new Oid ();
95                         if (distinguishedName.Length == 0) {
96                                 // empty (0x00) ASN.1 sequence (0x30)
97                                 RawData = new byte [2] { 0x30, 0x00 };
98                                 DecodeRawData ();
99                         } else {
100                                 var dn = MX.X501.FromString (distinguishedName);
101                                 if ((flag & X500DistinguishedNameFlags.Reversed) != 0) {
102                                         ASN1 rdn = new ASN1 (0x30);
103                                         for (int i = dn.Count - 1; i >= 0; i--) 
104                                                 rdn.Add (dn [i]);
105                                         dn = rdn;
106                                 }
107                                 RawData = dn.GetBytes ();
108                                 if (flag == X500DistinguishedNameFlags.None)
109                                         name = distinguishedName;
110                                 else
111                                         name = Decode (flag);
112                         }
113                 }
114
115                 public X500DistinguishedName (X500DistinguishedName distinguishedName)
116                 {
117                         if (distinguishedName == null)
118                                 throw new ArgumentNullException ("distinguishedName");
119
120                         Oid = new Oid ();
121                         RawData = distinguishedName.RawData;
122                         name = distinguishedName.name;
123                 }
124
125
126                 public string Name {
127                         get { return name; }
128                 }
129
130
131                 public string Decode (X500DistinguishedNameFlags flag)
132                 {
133                         if ((flag != 0) && ((flag & AllFlags) == 0))
134                                 throw new ArgumentException ("flag");
135
136                         if (RawData.Length == 0)
137                                 return String.Empty;
138
139                         // Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
140                         bool reversed = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
141                         bool quotes = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
142                         string separator = GetSeparator (flag);
143
144                         ASN1 rdn = new ASN1 (RawData);
145                         return MX.X501.ToString (rdn, reversed, separator, quotes);
146                 }
147
148                 public override string Format (bool multiLine)
149                 {
150                         if (multiLine) {
151                                 string s = Decode (X500DistinguishedNameFlags.UseNewLines);
152                                 if (s.Length > 0)
153                                         return s + Environment.NewLine;
154                                 else
155                                         return s;
156                         } else {
157                                 return Decode (X500DistinguishedNameFlags.UseCommas);
158                         }
159                 }
160
161                 // private stuff
162
163                 private static string GetSeparator (X500DistinguishedNameFlags flag)
164                 {
165                         if ((flag & X500DistinguishedNameFlags.UseSemicolons) != 0)
166                                 return "; ";
167                         if ((flag & X500DistinguishedNameFlags.UseCommas) != 0)
168                                 return ", ";
169                         if ((flag & X500DistinguishedNameFlags.UseNewLines) != 0)
170                                 return Environment.NewLine;
171                         return ", "; //default
172                 }
173
174                 // decode the DN using the (byte[]) RawData
175                 private void DecodeRawData ()
176                 {
177                         if ((RawData == null) || (RawData.Length < 3)) {
178                                 name = String.Empty;
179                                 return;
180                         }
181
182                         ASN1 sequence = new ASN1 (RawData);
183                         name = MX.X501.ToString (sequence, true, ", ", true);
184                 }
185
186                 private static string Canonize (string s)
187                 {
188                         int i = s.IndexOf ('=') + 1;
189                         StringBuilder r = new StringBuilder (s.Substring (0, i));
190                         // skip any white space starting the value
191                         while (i < s.Length && Char.IsWhiteSpace (s, i))
192                                 i++;
193                         // ensure we skip white spaces at the end of the value
194                         s = s.TrimEnd ();
195                         // keep track of internal multiple spaces
196                         bool space = false;
197                         for (; i < s.Length; i++) {
198                                 if (space) {
199                                         space = Char.IsWhiteSpace (s, i);
200                                         if (space)
201                                                 continue;
202                                 }
203                                 if (Char.IsWhiteSpace (s, i))
204                                         space = true;
205                                 r.Append (Char.ToUpperInvariant (s[i]));
206                         }
207                         return r.ToString ();
208                 }
209
210                 // of all X500DistinguishedNameFlags flags nothing can do a "correct" comparison :|
211                 internal static bool AreEqual (X500DistinguishedName name1, X500DistinguishedName name2)
212                 {
213                         if (name1 == null)
214                                 return (name2 == null);
215                         if (name2 == null)
216                                 return false;
217
218                         X500DistinguishedNameFlags flags = X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.DoNotUseQuotes;
219                         string[] split = new string[] { Environment.NewLine };
220                         string[] parts1 = name1.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
221                         string[] parts2 = name2.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
222                         if (parts1.Length != parts2.Length)
223                                 return false;
224
225                         for (int i = 0; i < parts1.Length; i++) {
226                                 if (Canonize (parts1[i]) != Canonize (parts2[i]))
227                                         return false;
228                         }
229                         return true;
230                 }
231         }
232 }
233
234 #endif