Oid.cs 5.2 KB

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