Oid.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Oid.cs - System.Security.Cryptography.Oid
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 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. #if NET_2_0 && SECURITY_DEP
  30. using System.Security.Cryptography.X509Certificates;
  31. namespace System.Security.Cryptography {
  32. public sealed class Oid {
  33. private string _value;
  34. private string _name;
  35. // constructors
  36. public Oid ()
  37. {
  38. }
  39. public Oid (string oid)
  40. {
  41. if (oid == null)
  42. throw new ArgumentNullException ("oid");
  43. _value = oid;
  44. _name = GetName (oid);
  45. }
  46. public Oid (string value, string friendlyName)
  47. {
  48. _value = value;
  49. _name = friendlyName;
  50. }
  51. public Oid (Oid oid)
  52. {
  53. if (oid == null)
  54. throw new ArgumentNullException ("oid");
  55. _value = oid.Value;
  56. _name = oid.FriendlyName;
  57. }
  58. // properties
  59. public string FriendlyName {
  60. get { return _name; }
  61. set {
  62. _name = value;
  63. _value = GetValue (_name);
  64. }
  65. }
  66. public string Value {
  67. get { return _value; }
  68. set {
  69. _value = value;
  70. _name = GetName (_value);
  71. }
  72. }
  73. // internal stuff
  74. // Known OID/Names not defined anywhere else (by OID order)
  75. internal const string oidRSA = "1.2.840.113549.1.1.1";
  76. internal const string nameRSA = "RSA";
  77. internal const string oidPkcs7Data = "1.2.840.113549.1.7.1";
  78. internal const string namePkcs7Data = "PKCS 7 Data";
  79. internal const string oidPkcs9ContentType = "1.2.840.113549.1.9.3";
  80. internal const string namePkcs9ContentType = "Content Type";
  81. internal const string oidPkcs9MessageDigest = "1.2.840.113549.1.9.4";
  82. internal const string namePkcs9MessageDigest = "Message Digest";
  83. internal const string oidPkcs9SigningTime = "1.2.840.113549.1.9.5";
  84. internal const string namePkcs9SigningTime = "Signing Time";
  85. internal const string oidMd5 = "1.2.840.113549.2.5";
  86. internal const string nameMd5 = "md5";
  87. internal const string oid3Des = "1.2.840.113549.3.7";
  88. internal const string name3Des = "3des";
  89. internal const string oidSha1 = "1.3.14.3.2.26";
  90. internal const string nameSha1 = "sha1";
  91. internal const string oidSubjectAltName = "2.5.29.17";
  92. internal const string nameSubjectAltName = "Subject Alternative Name";
  93. internal const string oidNetscapeCertType = "2.16.840.1.113730.1.1";
  94. internal const string nameNetscapeCertType = "Netscape Cert Type";
  95. // TODO - find the complete list
  96. private string GetName (string oid)
  97. {
  98. switch (oid) {
  99. case oidRSA:
  100. return nameRSA;
  101. case oidPkcs7Data:
  102. return namePkcs7Data;
  103. case oidPkcs9ContentType:
  104. return namePkcs9ContentType;
  105. case oidPkcs9MessageDigest:
  106. return namePkcs9MessageDigest;
  107. case oidPkcs9SigningTime:
  108. return namePkcs9SigningTime;
  109. case oid3Des:
  110. return name3Des;
  111. case X509BasicConstraintsExtension.oid:
  112. return X509BasicConstraintsExtension.friendlyName;
  113. case X509KeyUsageExtension.oid:
  114. return X509KeyUsageExtension.friendlyName;
  115. case X509EnhancedKeyUsageExtension.oid:
  116. return X509EnhancedKeyUsageExtension.friendlyName;
  117. case X509SubjectKeyIdentifierExtension.oid:
  118. return X509SubjectKeyIdentifierExtension.friendlyName;
  119. case oidSubjectAltName:
  120. return nameSubjectAltName;
  121. case oidNetscapeCertType:
  122. return nameNetscapeCertType;
  123. case oidMd5:
  124. return nameMd5;
  125. case oidSha1:
  126. return nameSha1;
  127. default:
  128. return _name;
  129. }
  130. }
  131. // TODO - find the complete list
  132. private string GetValue (string name)
  133. {
  134. switch (name) {
  135. case nameRSA:
  136. return oidRSA;
  137. case namePkcs7Data:
  138. return oidPkcs7Data;
  139. case namePkcs9ContentType:
  140. return oidPkcs9ContentType;
  141. case namePkcs9MessageDigest:
  142. return oidPkcs9MessageDigest;
  143. case namePkcs9SigningTime:
  144. return oidPkcs9SigningTime;
  145. case name3Des:
  146. return oid3Des;
  147. case X509BasicConstraintsExtension.friendlyName:
  148. return X509BasicConstraintsExtension.oid;
  149. case X509KeyUsageExtension.friendlyName:
  150. return X509KeyUsageExtension.oid;
  151. case X509EnhancedKeyUsageExtension.friendlyName:
  152. return X509EnhancedKeyUsageExtension.oid;
  153. case X509SubjectKeyIdentifierExtension.friendlyName:
  154. return X509SubjectKeyIdentifierExtension.oid;
  155. case nameSubjectAltName:
  156. return oidSubjectAltName;
  157. case nameNetscapeCertType:
  158. return oidNetscapeCertType;
  159. case nameMd5:
  160. return oidMd5;
  161. case nameSha1:
  162. return oidSha1;
  163. default:
  164. return _value;
  165. }
  166. }
  167. }
  168. }
  169. #endif