Oid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 oidNetscapeCertType = "2.16.840.1.113730.1.1";
  87. internal const string nameNetscapeCertType = "Netscape Cert Type";
  88. // TODO - find the complete list
  89. private string GetName (string oid)
  90. {
  91. switch (oid) {
  92. case oidRSA:
  93. return nameRSA;
  94. case oidPkcs7Data:
  95. return namePkcs7Data;
  96. case Pkcs9ContentType.oid:
  97. return Pkcs9ContentType.friendlyName;
  98. case Pkcs9MessageDigest.oid:
  99. return Pkcs9MessageDigest.friendlyName;
  100. case Pkcs9SigningTime.oid:
  101. return Pkcs9SigningTime.friendlyName;
  102. case oid3Des:
  103. return name3Des;
  104. case X509BasicConstraintsExtension.oid:
  105. return X509BasicConstraintsExtension.friendlyName;
  106. case X509KeyUsageExtension.oid:
  107. return X509KeyUsageExtension.friendlyName;
  108. case X509EnhancedKeyUsageExtension.oid:
  109. return X509EnhancedKeyUsageExtension.friendlyName;
  110. case X509SubjectKeyIdentifierExtension.oid:
  111. return X509SubjectKeyIdentifierExtension.friendlyName;
  112. case oidNetscapeCertType:
  113. return nameNetscapeCertType;
  114. case oidMd5:
  115. return nameMd5;
  116. case oidSha1:
  117. return nameSha1;
  118. default:
  119. return _name;
  120. }
  121. }
  122. // TODO - find the complete list
  123. private string GetValue (string name)
  124. {
  125. switch (name) {
  126. case nameRSA:
  127. return oidRSA;
  128. case namePkcs7Data:
  129. return oidPkcs7Data;
  130. case Pkcs9ContentType.friendlyName:
  131. return Pkcs9ContentType.oid;
  132. case Pkcs9MessageDigest.friendlyName:
  133. return Pkcs9MessageDigest.oid;
  134. case Pkcs9SigningTime.friendlyName:
  135. return Pkcs9SigningTime.oid;
  136. case name3Des:
  137. return oid3Des;
  138. case X509BasicConstraintsExtension.friendlyName:
  139. return X509BasicConstraintsExtension.oid;
  140. case X509KeyUsageExtension.friendlyName:
  141. return X509KeyUsageExtension.oid;
  142. case X509EnhancedKeyUsageExtension.friendlyName:
  143. return X509EnhancedKeyUsageExtension.oid;
  144. case X509SubjectKeyIdentifierExtension.friendlyName:
  145. return X509SubjectKeyIdentifierExtension.oid;
  146. case nameNetscapeCertType:
  147. return oidNetscapeCertType;
  148. case nameMd5:
  149. return oidMd5;
  150. case nameSha1:
  151. return oidSha1;
  152. default:
  153. return _value;
  154. }
  155. }
  156. }
  157. }
  158. #endif