2 // X501Name.cs: X.501 Distinguished Names stuff
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
38 namespace Mono.Security.X509 {
41 // 1. Information technology - Open Systems Interconnection - The Directory: Models
42 // http://www.itu.int/rec/recommendation.asp?type=items&lang=e&parent=T-REC-X.501-200102-I
43 // 2. RFC2253: Lightweight Directory Access Protocol (v3): UTF-8 String Representation of Distinguished Names
44 // http://www.ietf.org/rfc/rfc2253.txt
47 * Name ::= CHOICE { RDNSequence }
49 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
51 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
60 static byte[] countryName = { 0x55, 0x04, 0x06 };
61 static byte[] organizationName = { 0x55, 0x04, 0x0A };
62 static byte[] organizationalUnitName = { 0x55, 0x04, 0x0B };
63 static byte[] commonName = { 0x55, 0x04, 0x03 };
64 static byte[] localityName = { 0x55, 0x04, 0x07 };
65 static byte[] stateOrProvinceName = { 0x55, 0x04, 0x08 };
66 static byte[] streetAddress = { 0x55, 0x04, 0x09 };
67 static byte[] serialNumber = { 0x55, 0x04, 0x05 };
68 static byte[] domainComponent = { 0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x19 };
69 static byte[] userid = { 0x09, 0x92, 0x26, 0x89, 0x93, 0xF2, 0x2C, 0x64, 0x01, 0x01 };
70 static byte[] email = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01 };
76 static public string ToString (ASN1 seq)
78 StringBuilder sb = new StringBuilder ();
79 for (int i = 0; i < seq.Count; i++) {
81 ASN1 pair = entry [0];
91 if (poid.CompareValue (countryName))
93 else if (poid.CompareValue (organizationName))
95 else if (poid.CompareValue (organizationalUnitName))
97 else if (poid.CompareValue (commonName))
99 else if (poid.CompareValue (localityName))
101 else if (poid.CompareValue (stateOrProvinceName))
102 sb.Append ("S="); // NOTE: RFC2253 uses ST=
103 else if (poid.CompareValue (streetAddress))
104 sb.Append ("STREET=");
105 else if (poid.CompareValue (domainComponent))
107 else if (poid.CompareValue (userid))
109 else if (poid.CompareValue (email))
110 sb.Append ("E="); // NOTE: Not part of RFC2253
113 sb.Append ("OID."); // NOTE: Not present as RFC2253
114 sb.Append (ASN1Convert.ToOid (poid));
118 string sValue = null;
119 // 16bits or 8bits string ? TODO not complete (+special chars!)
122 StringBuilder sb2 = new StringBuilder ();
123 for (int j = 1; j < s.Value.Length; j+=2)
124 sb2.Append ((char) s.Value[j]);
125 sValue = sb2.ToString ();
128 sValue = System.Text.Encoding.UTF8.GetString (s.Value);
129 // in some cases we must quote (") the value
130 // Note: this doesn't seems to conform to RFC2253
131 char[] specials = { ',', '+', '"', '\\', '<', '>', ';' };
132 if (sValue.IndexOfAny(specials, 0, sValue.Length) > 0)
133 sValue = "\"" + sValue + "\"";
134 else if (sValue.StartsWith (" "))
135 sValue = "\"" + sValue + "\"";
136 else if (sValue.EndsWith (" "))
137 sValue = "\"" + sValue + "\"";
142 // separator (not on last iteration)
143 if (i < seq.Count - 1)
146 return sb.ToString ();
149 static private X520.AttributeTypeAndValue GetAttributeFromOid (string attributeType)
151 switch (attributeType.ToUpper (CultureInfo.InvariantCulture).Trim ()) {
153 return new X520.CountryName ();
155 return new X520.OrganizationName ();
157 return new X520.OrganizationalUnitName ();
159 return new X520.CommonName ();
161 return new X520.LocalityName ();
162 case "S": // Microsoft
163 case "ST": // RFC2253
164 return new X520.StateOrProvinceName ();
166 // return streetAddress;
168 // return domainComponent;
174 static public ASN1 FromString (string rdn)
177 throw new ArgumentNullException ("rdn");
178 // get string from here to ',' or end of string
181 ASN1 asn1 = new ASN1 (0x30);
182 while (start < rdn.Length) {
183 end = rdn.IndexOf (',', end) + 1;
185 end = rdn.Length + 1;
186 string av = rdn.Substring (start, end - start - 1);
187 // get '=' position in substring
188 int equal = av.IndexOf ('=');
190 string attributeType = av.Substring (0, equal);
192 string attributeValue = av.Substring (equal + 1);
194 X520.AttributeTypeAndValue atv = GetAttributeFromOid (attributeType);
195 atv.Value = attributeValue;
196 asn1.Add (new ASN1 (0x31, atv.GetBytes ()));
201 if (end > rdn.Length)