X520Attributes.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //
  2. // X520.cs: X.520 related stuff (attributes, RDN)
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Globalization;
  31. using System.Text;
  32. using Mono.Security;
  33. namespace Mono.Security.X509 {
  34. // References:
  35. // 1. Information technology - Open Systems Interconnection - The Directory: Selected attribute types
  36. // http://www.itu.int/rec/recommendation.asp?type=folders&lang=e&parent=T-REC-X.520
  37. // 2. Internet X.509 Public Key Infrastructure Certificate and CRL Profile
  38. // http://www.ietf.org/rfc/rfc3280.txt
  39. // 3. A Summary of the X.500(96) User Schema for use with LDAPv3
  40. // http://www.faqs.org/rfcs/rfc2256.html
  41. // 4. RFC 2247 - Using Domains in LDAP/X.500 Distinguished Names
  42. // http://www.faqs.org/rfcs/rfc2247.html
  43. /*
  44. * AttributeTypeAndValue ::= SEQUENCE {
  45. * type AttributeType,
  46. * value AttributeValue
  47. * }
  48. *
  49. * AttributeType ::= OBJECT IDENTIFIER
  50. *
  51. * AttributeValue ::= ANY DEFINED BY AttributeType
  52. */
  53. #if INSIDE_CORLIB
  54. internal
  55. #else
  56. public
  57. #endif
  58. class X520 {
  59. public abstract class AttributeTypeAndValue {
  60. private string oid;
  61. private string attrValue;
  62. private int upperBound;
  63. private byte encoding;
  64. protected AttributeTypeAndValue (string oid, int upperBound)
  65. {
  66. this.oid = oid;
  67. this.upperBound = upperBound;
  68. this.encoding = 0xFF;
  69. }
  70. protected AttributeTypeAndValue (string oid, int upperBound, byte encoding)
  71. {
  72. this.oid = oid;
  73. this.upperBound = upperBound;
  74. this.encoding = encoding;
  75. }
  76. public string Value {
  77. get { return attrValue; }
  78. set {
  79. if ((attrValue != null) && (attrValue.Length > upperBound)) {
  80. string msg = ("Value length bigger than upperbound ({0}).");
  81. throw new FormatException (String.Format (msg, upperBound));
  82. }
  83. attrValue = value;
  84. }
  85. }
  86. public ASN1 ASN1 {
  87. get { return GetASN1 (); }
  88. }
  89. internal ASN1 GetASN1 (byte encoding)
  90. {
  91. byte encode = encoding;
  92. if (encode == 0xFF)
  93. encode = SelectBestEncoding ();
  94. ASN1 asn1 = new ASN1 (0x30);
  95. asn1.Add (ASN1Convert.FromOid (oid));
  96. switch (encode) {
  97. case 0x13:
  98. // PRINTABLESTRING
  99. asn1.Add (new ASN1 (0x13, Encoding.ASCII.GetBytes (attrValue)));
  100. break;
  101. case 0x16:
  102. // IA5STRING
  103. asn1.Add (new ASN1 (0x16, Encoding.ASCII.GetBytes (attrValue)));
  104. break;
  105. case 0x1E:
  106. // BMPSTRING
  107. asn1.Add (new ASN1 (0x1E, Encoding.BigEndianUnicode.GetBytes (attrValue)));
  108. break;
  109. }
  110. return asn1;
  111. }
  112. internal ASN1 GetASN1 ()
  113. {
  114. return GetASN1 (encoding);
  115. }
  116. public byte[] GetBytes (byte encoding)
  117. {
  118. return GetASN1 (encoding) .GetBytes ();
  119. }
  120. public byte[] GetBytes ()
  121. {
  122. return GetASN1 () .GetBytes ();
  123. }
  124. private byte SelectBestEncoding ()
  125. {
  126. foreach (char c in attrValue) {
  127. switch (c) {
  128. case '@':
  129. case '_':
  130. return 0x1E; // BMPSTRING
  131. default:
  132. if (c > 127)
  133. return 0x1E; // BMPSTRING
  134. break;
  135. }
  136. }
  137. return 0x13; // PRINTABLESTRING
  138. }
  139. }
  140. public class Name : AttributeTypeAndValue {
  141. public Name () : base ("2.5.4.41", 32768)
  142. {
  143. }
  144. }
  145. public class CommonName : AttributeTypeAndValue {
  146. public CommonName () : base ("2.5.4.3", 64)
  147. {
  148. }
  149. }
  150. // RFC2256, Section 5.6
  151. public class SerialNumber : AttributeTypeAndValue {
  152. // max length 64 bytes, Printable String only
  153. public SerialNumber ()
  154. : base ("2.5.4.5", 64, 0x13)
  155. {
  156. }
  157. }
  158. public class LocalityName : AttributeTypeAndValue {
  159. public LocalityName () : base ("2.5.4.7", 128)
  160. {
  161. }
  162. }
  163. public class StateOrProvinceName : AttributeTypeAndValue {
  164. public StateOrProvinceName () : base ("2.5.4.8", 128)
  165. {
  166. }
  167. }
  168. public class OrganizationName : AttributeTypeAndValue {
  169. public OrganizationName () : base ("2.5.4.10", 64)
  170. {
  171. }
  172. }
  173. public class OrganizationalUnitName : AttributeTypeAndValue {
  174. public OrganizationalUnitName () : base ("2.5.4.11", 64)
  175. {
  176. }
  177. }
  178. // NOTE: Not part of RFC2253
  179. public class EmailAddress : AttributeTypeAndValue {
  180. public EmailAddress () : base ("1.2.840.113549.1.9.1", 128, 0x16)
  181. {
  182. }
  183. }
  184. // RFC2247, Section 4
  185. public class DomainComponent : AttributeTypeAndValue {
  186. // no maximum length defined
  187. public DomainComponent ()
  188. : base ("0.9.2342.19200300.100.1.25", Int32.MaxValue, 0x16)
  189. {
  190. }
  191. }
  192. // RFC1274, Section 9.3.1
  193. public class UserId : AttributeTypeAndValue {
  194. public UserId ()
  195. : base ("0.9.2342.19200300.100.1.1", 256)
  196. {
  197. }
  198. }
  199. public class Oid : AttributeTypeAndValue {
  200. public Oid (string oid)
  201. : base (oid, Int32.MaxValue)
  202. {
  203. }
  204. }
  205. /* -- Naming attributes of type X520Title
  206. * id-at-title AttributeType ::= { id-at 12 }
  207. *
  208. * X520Title ::= CHOICE {
  209. * teletexString TeletexString (SIZE (1..ub-title)),
  210. * printableString PrintableString (SIZE (1..ub-title)),
  211. * universalString UniversalString (SIZE (1..ub-title)),
  212. * utf8String UTF8String (SIZE (1..ub-title)),
  213. * bmpString BMPString (SIZE (1..ub-title))
  214. * }
  215. */
  216. public class Title : AttributeTypeAndValue {
  217. public Title () : base ("2.5.4.12", 64)
  218. {
  219. }
  220. }
  221. public class CountryName : AttributeTypeAndValue {
  222. // (0x13) PRINTABLESTRING
  223. public CountryName () : base ("2.5.4.6", 2, 0x13)
  224. {
  225. }
  226. }
  227. public class DnQualifier : AttributeTypeAndValue {
  228. // (0x13) PRINTABLESTRING
  229. public DnQualifier () : base ("2.5.4.46", 2, 0x13)
  230. {
  231. }
  232. }
  233. public class Surname : AttributeTypeAndValue {
  234. public Surname () : base ("2.5.4.4", 32768)
  235. {
  236. }
  237. }
  238. public class GivenName : AttributeTypeAndValue {
  239. public GivenName () : base ("2.5.4.42", 16)
  240. {
  241. }
  242. }
  243. public class Initial : AttributeTypeAndValue {
  244. public Initial () : base ("2.5.4.43", 5)
  245. {
  246. }
  247. }
  248. }
  249. /* From RFC3280
  250. * -- specifications of Upper Bounds MUST be regarded as mandatory
  251. * -- from Annex B of ITU-T X.411 Reference Definition of MTS Parameter
  252. *
  253. * -- Upper Bounds
  254. *
  255. * ub-name INTEGER ::= 32768
  256. * ub-common-name INTEGER ::= 64
  257. * ub-locality-name INTEGER ::= 128
  258. * ub-state-name INTEGER ::= 128
  259. * ub-organization-name INTEGER ::= 64
  260. * ub-organizational-unit-name INTEGER ::= 64
  261. * ub-title INTEGER ::= 64
  262. * ub-serial-number INTEGER ::= 64
  263. * ub-match INTEGER ::= 128
  264. * ub-emailaddress-length INTEGER ::= 128
  265. * ub-common-name-length INTEGER ::= 64
  266. * ub-country-name-alpha-length INTEGER ::= 2
  267. * ub-country-name-numeric-length INTEGER ::= 3
  268. * ub-domain-defined-attributes INTEGER ::= 4
  269. * ub-domain-defined-attribute-type-length INTEGER ::= 8
  270. * ub-domain-defined-attribute-value-length INTEGER ::= 128
  271. * ub-domain-name-length INTEGER ::= 16
  272. * ub-extension-attributes INTEGER ::= 256
  273. * ub-e163-4-number-length INTEGER ::= 15
  274. * ub-e163-4-sub-address-length INTEGER ::= 40
  275. * ub-generation-qualifier-length INTEGER ::= 3
  276. * ub-given-name-length INTEGER ::= 16
  277. * ub-initials-length INTEGER ::= 5
  278. * ub-integer-options INTEGER ::= 256
  279. * ub-numeric-user-id-length INTEGER ::= 32
  280. * ub-organization-name-length INTEGER ::= 64
  281. * ub-organizational-unit-name-length INTEGER ::= 32
  282. * ub-organizational-units INTEGER ::= 4
  283. * ub-pds-name-length INTEGER ::= 16
  284. * ub-pds-parameter-length INTEGER ::= 30
  285. * ub-pds-physical-address-lines INTEGER ::= 6
  286. * ub-postal-code-length INTEGER ::= 16
  287. * ub-pseudonym INTEGER ::= 128
  288. * ub-surname-length INTEGER ::= 40
  289. * ub-terminal-id-length INTEGER ::= 24
  290. * ub-unformatted-address-length INTEGER ::= 180
  291. * ub-x121-address-length INTEGER ::= 16
  292. *
  293. * -- Note - upper bounds on string types, such as TeletexString, are
  294. * -- measured in characters. Excepting PrintableString or IA5String, a
  295. * -- significantly greater number of octets will be required to hold
  296. * -- such a value. As a minimum, 16 octets, or twice the specified
  297. * -- upper bound, whichever is the larger, should be allowed for
  298. * -- TeletexString. For UTF8String or UniversalString at least four
  299. * -- times the upper bound should be allowed.
  300. */
  301. }