File: X500DistinguishedName.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (258 lines) | stat: -rw-r--r-- 7,928 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// System.Security.Cryptography.X509Certificates.X500DistinguishedName
//
// Author:
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// Copyright (C) 2004-2006 Novell Inc. (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if SECURITY_DEP

#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
using MonoSecurity::Mono.Security;
using MX = MonoSecurity::Mono.Security.X509;
#else
using Mono.Security;
using MX = Mono.Security.X509;
#endif

using System.Collections;
using System.Text;

namespace System.Security.Cryptography.X509Certificates {

	[MonoTODO ("Some X500DistinguishedNameFlags options aren't supported, like DoNotUsePlusSign, DoNotUseQuotes and ForceUTF8Encoding")]
	public sealed class X500DistinguishedName : AsnEncodedData {

		private const X500DistinguishedNameFlags AllFlags = X500DistinguishedNameFlags.Reversed |
			X500DistinguishedNameFlags.UseSemicolons | X500DistinguishedNameFlags.DoNotUsePlusSign | 
			X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.UseCommas | 
			X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.UseUTF8Encoding | 
			X500DistinguishedNameFlags.UseT61Encoding | X500DistinguishedNameFlags.ForceUTF8Encoding;

		private string name;
		private byte[] canonEncoding;


		public X500DistinguishedName (AsnEncodedData encodedDistinguishedName)
		{
			if (encodedDistinguishedName == null)
				throw new ArgumentNullException ("encodedDistinguishedName");

			RawData = encodedDistinguishedName.RawData;
			if (RawData.Length > 0)
				DecodeRawData ();
			else
				name = String.Empty;
		}

		public X500DistinguishedName (byte[] encodedDistinguishedName)
		{
			if (encodedDistinguishedName == null)
				throw new ArgumentNullException ("encodedDistinguishedName");

			Oid = new Oid ();
			RawData = encodedDistinguishedName;
			if (encodedDistinguishedName.Length > 0)
				DecodeRawData ();
			else
				name = String.Empty;
		}

		public X500DistinguishedName (string distinguishedName)
			: this (distinguishedName, X500DistinguishedNameFlags.Reversed)
		{
		}

		public X500DistinguishedName (string distinguishedName, X500DistinguishedNameFlags flag)
		{
			if (distinguishedName == null)
				throw new ArgumentNullException ("distinguishedName");
			if ((flag != 0) && ((flag & AllFlags) == 0))
				throw new ArgumentException ("flag");

			Oid = new Oid ();
			if (distinguishedName.Length == 0) {
				// empty (0x00) ASN.1 sequence (0x30)
				RawData = new byte [2] { 0x30, 0x00 };
				DecodeRawData ();
			} else {
				var dn = MX.X501.FromString (distinguishedName);
				if ((flag & X500DistinguishedNameFlags.Reversed) != 0) {
					ASN1 rdn = new ASN1 (0x30);
					for (int i = dn.Count - 1; i >= 0; i--)	
						rdn.Add (dn [i]);
					dn = rdn;
				}
				RawData = dn.GetBytes ();
				if (flag == X500DistinguishedNameFlags.None)
					name = distinguishedName;
				else
					name = Decode (flag);
			}
		}

		public X500DistinguishedName (X500DistinguishedName distinguishedName)
		{
			if (distinguishedName == null)
				throw new ArgumentNullException ("distinguishedName");

			Oid = new Oid ();
			RawData = distinguishedName.RawData;
			name = distinguishedName.name;
		}

		internal X500DistinguishedName (byte[] encoded, byte[] canonEncoding, string name)
		{
			this.canonEncoding = canonEncoding;
			this.name = name;

			Oid = new Oid ();
			RawData = encoded;
		}

		internal byte[] CanonicalEncoding {
			get { return canonEncoding; }
		}


		public string Name {
			get { return name; }
		}


		public string Decode (X500DistinguishedNameFlags flag)
		{
			if ((flag != 0) && ((flag & AllFlags) == 0))
				throw new ArgumentException ("flag");

			if (RawData.Length == 0)
				return String.Empty;

			// Mono.Security reversed isn't the same as fx 2.0 (which is the reverse of 1.x)
			bool reversed = ((flag & X500DistinguishedNameFlags.Reversed) != 0);
			bool quotes = ((flag & X500DistinguishedNameFlags.DoNotUseQuotes) == 0);
			string separator = GetSeparator (flag);

			ASN1 rdn = new ASN1 (RawData);
			return MX.X501.ToString (rdn, reversed, separator, quotes);
		}

		public override string Format (bool multiLine)
		{
			if (multiLine) {
				string s = Decode (X500DistinguishedNameFlags.UseNewLines);
				if (s.Length > 0)
					return s + Environment.NewLine;
				else
					return s;
			} else {
				return Decode (X500DistinguishedNameFlags.UseCommas);
			}
		}

		// private stuff

		private static string GetSeparator (X500DistinguishedNameFlags flag)
		{
			if ((flag & X500DistinguishedNameFlags.UseSemicolons) != 0)
				return "; ";
			if ((flag & X500DistinguishedNameFlags.UseCommas) != 0)
				return ", ";
			if ((flag & X500DistinguishedNameFlags.UseNewLines) != 0)
				return Environment.NewLine;
			return ", "; //default
		}

		// decode the DN using the (byte[]) RawData
		private void DecodeRawData ()
		{
			if ((RawData == null) || (RawData.Length < 3)) {
				name = String.Empty;
				return;
			}

			ASN1 sequence = new ASN1 (RawData);
			name = MX.X501.ToString (sequence, true, ", ", true);
		}

		private static string Canonize (string s)
		{
			int i = s.IndexOf ('=') + 1;
			StringBuilder r = new StringBuilder (s.Substring (0, i));
			// skip any white space starting the value
			while (i < s.Length && Char.IsWhiteSpace (s, i))
				i++;
			// ensure we skip white spaces at the end of the value
			s = s.TrimEnd ();
			// keep track of internal multiple spaces
			bool space = false;
			for (; i < s.Length; i++) {
				if (space) {
					space = Char.IsWhiteSpace (s, i);
					if (space)
						continue;
				}
				if (Char.IsWhiteSpace (s, i))
					space = true;
				r.Append (Char.ToUpperInvariant (s[i]));
			}
			return r.ToString ();
		}

		// of all X500DistinguishedNameFlags flags nothing can do a "correct" comparison :|
		internal static bool AreEqual (X500DistinguishedName name1, X500DistinguishedName name2)
		{
			if (name1 == null)
				return (name2 == null);
			if (name2 == null)
				return false;

			if (name1.canonEncoding != null && name2.canonEncoding != null) {
				if (name1.canonEncoding.Length != name2.canonEncoding.Length)
					return false;
				for (int i = 0; i < name1.canonEncoding.Length; i++) {
					if (name1.canonEncoding[i] != name2.canonEncoding[i])
						return false;
				}
				return true;
			}

			X500DistinguishedNameFlags flags = X500DistinguishedNameFlags.UseNewLines | X500DistinguishedNameFlags.DoNotUseQuotes;
			string[] split = new string[] { Environment.NewLine };
			string[] parts1 = name1.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
			string[] parts2 = name2.Decode (flags).Split (split, StringSplitOptions.RemoveEmptyEntries);
			if (parts1.Length != parts2.Length)
				return false;

			for (int i = 0; i < parts1.Length; i++) {
				if (Canonize (parts1[i]) != Canonize (parts2[i]))
					return false;
			}
			return true;
		}
	}
}

#endif