X520Attributes.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.Text;
  31. namespace Emby.Server.Core.Cryptography
  32. {
  33. // References:
  34. // 1. Information technology - Open Systems Interconnection - The Directory: Selected attribute types
  35. // http://www.itu.int/rec/recommendation.asp?type=folders&lang=e&parent=T-REC-X.520
  36. // 2. Internet X.509 Public Key Infrastructure Certificate and CRL Profile
  37. // http://www.ietf.org/rfc/rfc3280.txt
  38. // 3. A Summary of the X.500(96) User Schema for use with LDAPv3
  39. // http://www.faqs.org/rfcs/rfc2256.html
  40. // 4. RFC 2247 - Using Domains in LDAP/X.500 Distinguished Names
  41. // http://www.faqs.org/rfcs/rfc2247.html
  42. /*
  43. * AttributeTypeAndValue ::= SEQUENCE {
  44. * type AttributeType,
  45. * value AttributeValue
  46. * }
  47. *
  48. * AttributeType ::= OBJECT IDENTIFIER
  49. *
  50. * AttributeValue ::= ANY DEFINED BY AttributeType
  51. */
  52. public class X520 {
  53. public abstract class AttributeTypeAndValue {
  54. private string oid;
  55. private string attrValue;
  56. private int upperBound;
  57. private byte encoding;
  58. protected AttributeTypeAndValue (string oid, int upperBound)
  59. {
  60. this.oid = oid;
  61. this.upperBound = upperBound;
  62. this.encoding = 0xFF;
  63. }
  64. protected AttributeTypeAndValue (string oid, int upperBound, byte encoding)
  65. {
  66. this.oid = oid;
  67. this.upperBound = upperBound;
  68. this.encoding = encoding;
  69. }
  70. public string Value {
  71. get { return attrValue; }
  72. set {
  73. if ((attrValue != null) && (attrValue.Length > upperBound)) {
  74. string msg = ("Value length bigger than upperbound ({0}).");
  75. throw new FormatException (String.Format (msg, upperBound));
  76. }
  77. attrValue = value;
  78. }
  79. }
  80. public ASN1 ASN1 {
  81. get { return GetASN1 (); }
  82. }
  83. internal ASN1 GetASN1 (byte encoding)
  84. {
  85. byte encode = encoding;
  86. if (encode == 0xFF)
  87. encode = SelectBestEncoding ();
  88. ASN1 asn1 = new ASN1 (0x30);
  89. asn1.Add (ASN1Convert.FromOid (oid));
  90. switch (encode) {
  91. case 0x13:
  92. // PRINTABLESTRING
  93. asn1.Add (new ASN1 (0x13, Encoding.ASCII.GetBytes (attrValue)));
  94. break;
  95. case 0x16:
  96. // IA5STRING
  97. asn1.Add (new ASN1 (0x16, Encoding.ASCII.GetBytes (attrValue)));
  98. break;
  99. case 0x1E:
  100. // BMPSTRING
  101. asn1.Add (new ASN1 (0x1E, Encoding.BigEndianUnicode.GetBytes (attrValue)));
  102. break;
  103. }
  104. return asn1;
  105. }
  106. internal ASN1 GetASN1 ()
  107. {
  108. return GetASN1 (encoding);
  109. }
  110. public byte[] GetBytes (byte encoding)
  111. {
  112. return GetASN1 (encoding) .GetBytes ();
  113. }
  114. public byte[] GetBytes ()
  115. {
  116. return GetASN1 () .GetBytes ();
  117. }
  118. private byte SelectBestEncoding ()
  119. {
  120. foreach (char c in attrValue) {
  121. switch (c) {
  122. case '@':
  123. case '_':
  124. return 0x1E; // BMPSTRING
  125. default:
  126. if (c > 127)
  127. return 0x1E; // BMPSTRING
  128. break;
  129. }
  130. }
  131. return 0x13; // PRINTABLESTRING
  132. }
  133. }
  134. public class Name : AttributeTypeAndValue {
  135. public Name () : base ("2.5.4.41", 32768)
  136. {
  137. }
  138. }
  139. public class CommonName : AttributeTypeAndValue {
  140. public CommonName () : base ("2.5.4.3", 64)
  141. {
  142. }
  143. }
  144. // RFC2256, Section 5.6
  145. public class SerialNumber : AttributeTypeAndValue {
  146. // max length 64 bytes, Printable String only
  147. public SerialNumber ()
  148. : base ("2.5.4.5", 64, 0x13)
  149. {
  150. }
  151. }
  152. public class LocalityName : AttributeTypeAndValue {
  153. public LocalityName () : base ("2.5.4.7", 128)
  154. {
  155. }
  156. }
  157. public class StateOrProvinceName : AttributeTypeAndValue {
  158. public StateOrProvinceName () : base ("2.5.4.8", 128)
  159. {
  160. }
  161. }
  162. public class OrganizationName : AttributeTypeAndValue {
  163. public OrganizationName () : base ("2.5.4.10", 64)
  164. {
  165. }
  166. }
  167. public class OrganizationalUnitName : AttributeTypeAndValue {
  168. public OrganizationalUnitName () : base ("2.5.4.11", 64)
  169. {
  170. }
  171. }
  172. // NOTE: Not part of RFC2253
  173. public class EmailAddress : AttributeTypeAndValue {
  174. public EmailAddress () : base ("1.2.840.113549.1.9.1", 128, 0x16)
  175. {
  176. }
  177. }
  178. // RFC2247, Section 4
  179. public class DomainComponent : AttributeTypeAndValue {
  180. // no maximum length defined
  181. public DomainComponent ()
  182. : base ("0.9.2342.19200300.100.1.25", Int32.MaxValue, 0x16)
  183. {
  184. }
  185. }
  186. // RFC1274, Section 9.3.1
  187. public class UserId : AttributeTypeAndValue {
  188. public UserId ()
  189. : base ("0.9.2342.19200300.100.1.1", 256)
  190. {
  191. }
  192. }
  193. public class Oid : AttributeTypeAndValue {
  194. public Oid (string oid)
  195. : base (oid, Int32.MaxValue)
  196. {
  197. }
  198. }
  199. /* -- Naming attributes of type X520Title
  200. * id-at-title AttributeType ::= { id-at 12 }
  201. *
  202. * X520Title ::= CHOICE {
  203. * teletexString TeletexString (SIZE (1..ub-title)),
  204. * printableString PrintableString (SIZE (1..ub-title)),
  205. * universalString UniversalString (SIZE (1..ub-title)),
  206. * utf8String UTF8String (SIZE (1..ub-title)),
  207. * bmpString BMPString (SIZE (1..ub-title))
  208. * }
  209. */
  210. public class Title : AttributeTypeAndValue {
  211. public Title () : base ("2.5.4.12", 64)
  212. {
  213. }
  214. }
  215. public class CountryName : AttributeTypeAndValue {
  216. // (0x13) PRINTABLESTRING
  217. public CountryName () : base ("2.5.4.6", 2, 0x13)
  218. {
  219. }
  220. }
  221. public class DnQualifier : AttributeTypeAndValue {
  222. // (0x13) PRINTABLESTRING
  223. public DnQualifier () : base ("2.5.4.46", 2, 0x13)
  224. {
  225. }
  226. }
  227. public class Surname : AttributeTypeAndValue {
  228. public Surname () : base ("2.5.4.4", 32768)
  229. {
  230. }
  231. }
  232. public class GivenName : AttributeTypeAndValue {
  233. public GivenName () : base ("2.5.4.42", 16)
  234. {
  235. }
  236. }
  237. public class Initial : AttributeTypeAndValue {
  238. public Initial () : base ("2.5.4.43", 5)
  239. {
  240. }
  241. }
  242. }
  243. /* From RFC3280
  244. * -- specifications of Upper Bounds MUST be regarded as mandatory
  245. * -- from Annex B of ITU-T X.411 Reference Definition of MTS Parameter
  246. *
  247. * -- Upper Bounds
  248. *
  249. * ub-name INTEGER ::= 32768
  250. * ub-common-name INTEGER ::= 64
  251. * ub-locality-name INTEGER ::= 128
  252. * ub-state-name INTEGER ::= 128
  253. * ub-organization-name INTEGER ::= 64
  254. * ub-organizational-unit-name INTEGER ::= 64
  255. * ub-title INTEGER ::= 64
  256. * ub-serial-number INTEGER ::= 64
  257. * ub-match INTEGER ::= 128
  258. * ub-emailaddress-length INTEGER ::= 128
  259. * ub-common-name-length INTEGER ::= 64
  260. * ub-country-name-alpha-length INTEGER ::= 2
  261. * ub-country-name-numeric-length INTEGER ::= 3
  262. * ub-domain-defined-attributes INTEGER ::= 4
  263. * ub-domain-defined-attribute-type-length INTEGER ::= 8
  264. * ub-domain-defined-attribute-value-length INTEGER ::= 128
  265. * ub-domain-name-length INTEGER ::= 16
  266. * ub-extension-attributes INTEGER ::= 256
  267. * ub-e163-4-number-length INTEGER ::= 15
  268. * ub-e163-4-sub-address-length INTEGER ::= 40
  269. * ub-generation-qualifier-length INTEGER ::= 3
  270. * ub-given-name-length INTEGER ::= 16
  271. * ub-initials-length INTEGER ::= 5
  272. * ub-integer-options INTEGER ::= 256
  273. * ub-numeric-user-id-length INTEGER ::= 32
  274. * ub-organization-name-length INTEGER ::= 64
  275. * ub-organizational-unit-name-length INTEGER ::= 32
  276. * ub-organizational-units INTEGER ::= 4
  277. * ub-pds-name-length INTEGER ::= 16
  278. * ub-pds-parameter-length INTEGER ::= 30
  279. * ub-pds-physical-address-lines INTEGER ::= 6
  280. * ub-postal-code-length INTEGER ::= 16
  281. * ub-pseudonym INTEGER ::= 128
  282. * ub-surname-length INTEGER ::= 40
  283. * ub-terminal-id-length INTEGER ::= 24
  284. * ub-unformatted-address-length INTEGER ::= 180
  285. * ub-x121-address-length INTEGER ::= 16
  286. *
  287. * -- Note - upper bounds on string types, such as TeletexString, are
  288. * -- measured in characters. Excepting PrintableString or IA5String, a
  289. * -- significantly greater number of octets will be required to hold
  290. * -- such a value. As a minimum, 16 octets, or twice the specified
  291. * -- upper bound, whichever is the larger, should be allowed for
  292. * -- TeletexString. For UTF8String or UniversalString at least four
  293. * -- times the upper bound should be allowed.
  294. */
  295. }