File: GeneralNames.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 (256 lines) | stat: -rw-r--r-- 7,191 bytes parent folder | download | duplicates (12)
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
//
// GeneralNames.cs: Handles GeneralNames for SubjectAltNameExtension and
//	CRLDistributionPointsExtension
//
// Author:
//	Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 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.
//

using System;
using System.Net;
using System.Collections;
using System.Text;

using Mono.Security;
using Mono.Security.X509;

namespace Mono.Security.X509.Extensions {

	/*
	 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
	 * 
	 * GeneralName ::= CHOICE {
	 *    otherName                       [0]     OtherName,
	 *    rfc822Name                      [1]     IA5String,
	 *    dNSName                         [2]     IA5String,
	 *    x400Address                     [3]     ORAddress,
	 *    directoryName                   [4]     Name,
	 *    ediPartyName                    [5]     EDIPartyName,
	 *    uniformResourceIdentifier       [6]     IA5String,
	 *    iPAddress                       [7]     OCTET STRING,
	 *    registeredID                    [8]     OBJECT IDENTIFIER 
	 * }
	 * 
	 * OtherName ::= SEQUENCE {
	 *    type-id    OBJECT IDENTIFIER,
	 *    value      [0] EXPLICIT ANY DEFINED BY type-id 
	 * }
	 * 
	 * EDIPartyName ::= SEQUENCE {
	 *    nameAssigner            [0]     DirectoryString OPTIONAL,
	 *    partyName               [1]     DirectoryString 
	 * }
	 */

	// TODO - incomplete (only rfc822Name, dNSName are supported)
	internal class GeneralNames {

		private ArrayList rfc822Name;
		private ArrayList dnsName;
		private ArrayList directoryNames;
		private ArrayList uris;
		private ArrayList ipAddr;
		private ASN1 asn;

		public GeneralNames ()
		{
		}

		public GeneralNames (string[] rfc822s, string[] dnsNames, string[] ipAddresses, string[] uris)
		{
			// This is an extension
			asn = new ASN1 (0x30);

			if (rfc822s != null) {
				rfc822Name = new ArrayList ();
				foreach (string rfc822 in rfc822s) {
					asn.Add (new ASN1 (0x81, Encoding.ASCII.GetBytes (rfc822)));
					rfc822Name.Add (rfc822s);
				}
			}

			if (dnsNames != null) {
				dnsName = new ArrayList ();
				foreach (string dnsname in dnsNames) {
					asn.Add (new ASN1 (0x82, Encoding.ASCII.GetBytes (dnsname)));
					dnsName.Add(dnsname);
				}
			}

			if (ipAddresses != null) {
				ipAddr = new ArrayList ();
				foreach (string ipaddress in ipAddresses) {
					string[] parts = ipaddress.Split ('.', ':');
					byte[] bytes = new byte[parts.Length];
					for (int i = 0; i < parts.Length; i++) {
						bytes[i] = Byte.Parse (parts[i]);
					}
					asn.Add (new ASN1 (0x87, bytes));
					ipAddr.Add (ipaddress);
				}
			}

			if (uris != null) {
				this.uris = new ArrayList();
				foreach (string uri in uris) {
					asn.Add (new ASN1 (0x86, Encoding.ASCII.GetBytes (uri)));
					this.uris.Add (uri);
				}
			}
		}

		public GeneralNames (ASN1 sequence)
		{
			for (int i = 0; i < sequence.Count; i++) {
				switch (sequence[i].Tag) {
				case 0x81: // rfc822Name			[1]	IA5String
					if (rfc822Name == null)
						rfc822Name = new ArrayList ();
					rfc822Name.Add (Encoding.ASCII.GetString (sequence[i].Value));
					break;
				case 0x82: // dNSName				[2]     IA5String
					if (dnsName == null)
						dnsName = new ArrayList ();
					dnsName.Add (Encoding.ASCII.GetString (sequence[i].Value));
					break;
				case 0x84: // directoryName			[4]     Name
				case 0xA4:
					if (directoryNames == null)
						directoryNames = new ArrayList ();
					directoryNames.Add (X501.ToString (sequence[i][0]));
					break;
				case 0x86:  // uniformResourceIdentifier	[6]     IA5String
					if (uris == null)
						uris = new ArrayList ();
					uris.Add (Encoding.ASCII.GetString (sequence[i].Value));
					break;
				case 0x87: // iPAddress				[7]     OCTET STRING
					if (ipAddr == null)
						ipAddr = new ArrayList ();
					byte[] bytes = sequence[i].Value;
					string space = (bytes.Length == 4) ? "." : ":";
					StringBuilder sb = new StringBuilder();
					for (int j = 0; j < bytes.Length; j++) {
						sb.Append (bytes[j].ToString ());
						if (j < bytes.Length - 1)
							sb.Append (space); 
					}
					ipAddr.Add (sb.ToString());
					if (ipAddr == null)
						ipAddr = new ArrayList ();
					break;
				default:
					break;
				}
			}
		}

		public string[] RFC822 {
			get {
				if (rfc822Name == null)
					return new string[0];
				return (string[])rfc822Name.ToArray (typeof (string));
			}
		}

		public string[] DirectoryNames {
			get {
				if (directoryNames == null)
					return new string[0];
				return (string[])directoryNames.ToArray (typeof (string));
			}
		}

		public string[] DNSNames {
			get {
				if (dnsName == null)
					return new string[0];
				return (string[])dnsName.ToArray (typeof (string));
			}
		}

		public string[] UniformResourceIdentifiers {
			get {
				if (uris == null)
					return new string[0];
				return (string[])uris.ToArray (typeof (string));
			}
		}

		public string[] IPAddresses {
			get {
				if (ipAddr == null)
					return new string[0];
				return (string[])ipAddr.ToArray (typeof (string));
			}
		}

		public byte[] GetBytes ()
		{
			return asn.GetBytes ();
		}

		public override string ToString ()
		{
			StringBuilder sb = new StringBuilder ();
			if (rfc822Name != null) {
				foreach (string s in rfc822Name) {
					sb.Append ("RFC822 Name=");
					sb.Append (s);
					sb.Append (Environment.NewLine);
				}
			}
			if (dnsName != null) {
				foreach (string s in dnsName) {
					sb.Append ("DNS Name=");
					sb.Append (s);
					sb.Append (Environment.NewLine);
				}
			}
			if (directoryNames != null) {
				foreach (string s in directoryNames) {
					sb.Append ("Directory Address: ");
					sb.Append (s);
					sb.Append (Environment.NewLine);
				}
			}
			if (uris != null) {
				foreach (string s in uris) {
					sb.Append ("URL=");
					sb.Append (s);
					sb.Append (Environment.NewLine);
				}
			}
			if (ipAddr != null) {
				foreach (string s in ipAddr) {
					sb.Append ("IP Address=");
					sb.Append (s);
					sb.Append (Environment.NewLine);
				}
			}
			return sb.ToString ();
		}
	}
}