SecurityAlgorithmSuite.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // SecurityAlgorithmSuite.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IdentityModel.Tokens;
  30. using System.Security.Cryptography.Xml;
  31. using System.ServiceModel;
  32. using System.ServiceModel.Security.Tokens;
  33. namespace System.ServiceModel.Security
  34. {
  35. public abstract class SecurityAlgorithmSuite
  36. {
  37. #region Internal Class
  38. class BasicSecurityAlgorithmSuite : SecurityAlgorithmSuiteImplBase
  39. {
  40. public BasicSecurityAlgorithmSuite (int size, bool sha, bool rsa)
  41. : base (size, sha, rsa, false)
  42. {
  43. }
  44. public override int DefaultSignatureKeyDerivationLength {
  45. get { return Size > 192 ? 192 : Size; }
  46. }
  47. public override bool IsAsymmetricKeyLengthSupported (int length)
  48. {
  49. switch (length) {
  50. case 128:
  51. case 192:
  52. return Size >= length;
  53. }
  54. return false;
  55. }
  56. public override bool IsSymmetricKeyLengthSupported (int length)
  57. {
  58. switch (length) {
  59. case 128:
  60. case 192:
  61. case 256:
  62. return Size >= length;
  63. }
  64. return false;
  65. }
  66. public override bool IsSymmetricKeyWrapAlgorithmSupported (string algorithm)
  67. {
  68. switch (Size) {
  69. case 256:
  70. if (algorithm == EncryptedXml.XmlEncAES256KeyWrapUrl)
  71. return true;
  72. goto case 192;
  73. case 192:
  74. if (algorithm == EncryptedXml.XmlEncAES192KeyWrapUrl)
  75. return true;
  76. goto case 128;
  77. case 128:
  78. return algorithm == EncryptedXml.XmlEncAES128KeyWrapUrl;
  79. }
  80. return false;
  81. }
  82. }
  83. class TripleDESSecurityAlgorithmSuite : SecurityAlgorithmSuiteImplBase
  84. {
  85. public TripleDESSecurityAlgorithmSuite (bool sha, bool rsa)
  86. : base (192, sha, rsa, true)
  87. {
  88. }
  89. public override int DefaultSignatureKeyDerivationLength {
  90. get { return 192; }
  91. }
  92. public override bool IsAsymmetricKeyLengthSupported (int length)
  93. {
  94. return length == 192;
  95. }
  96. public override bool IsSymmetricKeyLengthSupported (int length)
  97. {
  98. return length == 192;
  99. }
  100. public override bool IsSymmetricKeyWrapAlgorithmSupported (
  101. string algorithm)
  102. {
  103. return algorithm == EncryptedXml.XmlEncTripleDESKeyWrapUrl;
  104. }
  105. }
  106. abstract class SecurityAlgorithmSuiteImplBase : SecurityAlgorithmSuite
  107. {
  108. int size;
  109. bool rsa15, sha256, tdes;
  110. public SecurityAlgorithmSuiteImplBase (
  111. int size, bool sha256, bool rsa15, bool tripleDes)
  112. {
  113. this.size = size;
  114. this.sha256 = sha256;
  115. this.rsa15 = rsa15;
  116. this.tdes = tripleDes;
  117. }
  118. public int Size {
  119. get { return size; }
  120. }
  121. public bool Rsa15 {
  122. get { return rsa15; }
  123. }
  124. public bool Sha256 {
  125. get { return sha256; }
  126. }
  127. public override string DefaultAsymmetricKeyWrapAlgorithm {
  128. get { return rsa15 ? EncryptedXml.XmlEncRSA15Url : EncryptedXml.XmlEncRSAOAEPUrl; }
  129. }
  130. public override string DefaultAsymmetricSignatureAlgorithm {
  131. get { return sha256 ? SecurityAlgorithms.RsaSha256Signature : SignedXml.XmlDsigRSASHA1Url; }
  132. }
  133. public override string DefaultCanonicalizationAlgorithm {
  134. get { return SignedXml.XmlDsigExcC14NTransformUrl; }
  135. }
  136. public override string DefaultDigestAlgorithm {
  137. get { return sha256 ? EncryptedXml.XmlEncSHA256Url : SignedXml.XmlDsigSHA1Url; }
  138. }
  139. public override string DefaultEncryptionAlgorithm {
  140. get {
  141. if (tdes)
  142. return EncryptedXml.XmlEncTripleDESUrl;
  143. switch (size) {
  144. case 128:
  145. return EncryptedXml.XmlEncAES128Url;
  146. case 192:
  147. return EncryptedXml.XmlEncAES192Url;
  148. case 256:
  149. return EncryptedXml.XmlEncAES256Url;
  150. }
  151. throw new Exception ("Should not happen.");
  152. }
  153. }
  154. public override int DefaultEncryptionKeyDerivationLength {
  155. get { return size; }
  156. }
  157. public override int DefaultSymmetricKeyLength {
  158. get { return size; }
  159. }
  160. public override string DefaultSymmetricKeyWrapAlgorithm {
  161. get {
  162. if (tdes)
  163. return EncryptedXml.XmlEncTripleDESKeyWrapUrl;
  164. switch (size) {
  165. case 128:
  166. return EncryptedXml.XmlEncAES128KeyWrapUrl;
  167. case 192:
  168. return EncryptedXml.XmlEncAES192KeyWrapUrl;
  169. case 256:
  170. return EncryptedXml.XmlEncAES256KeyWrapUrl;
  171. }
  172. throw new Exception ("Should not happen.");
  173. }
  174. }
  175. public override string DefaultSymmetricSignatureAlgorithm {
  176. get { return sha256 ? SecurityAlgorithms.HmacSha256Signature : SignedXml.XmlDsigHMACSHA1Url; }
  177. }
  178. [MonoTODO]
  179. public override bool IsAsymmetricSignatureAlgorithmSupported (
  180. string algorithm)
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. [MonoTODO]
  185. public override bool IsCanonicalizationAlgorithmSupported (
  186. string algorithm)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. [MonoTODO]
  191. public override bool IsDigestAlgorithmSupported (string algorithm)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. public override bool IsEncryptionAlgorithmSupported (
  197. string algorithm)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. [MonoTODO]
  202. public override bool IsEncryptionKeyDerivationAlgorithmSupported (
  203. string algorithm)
  204. {
  205. throw new NotImplementedException ();
  206. }
  207. [MonoTODO]
  208. public override bool IsSignatureKeyDerivationAlgorithmSupported (
  209. string algorithm)
  210. {
  211. throw new NotImplementedException ();
  212. }
  213. [MonoTODO]
  214. public override bool IsSymmetricSignatureAlgorithmSupported (
  215. string algorithm)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. }
  220. #endregion
  221. #region Static members
  222. static SecurityAlgorithmSuite b128, b128r, b128s, b128sr;
  223. static SecurityAlgorithmSuite b192, b192r, b192s, b192sr;
  224. static SecurityAlgorithmSuite b256, b256r, b256s, b256sr;
  225. static SecurityAlgorithmSuite tdes, tdes_r, tdes_s, tdes_sr;
  226. static SecurityAlgorithmSuite ()
  227. {
  228. b128 = new BasicSecurityAlgorithmSuite (128, false, false);
  229. b128r = new BasicSecurityAlgorithmSuite (128, false, true);
  230. b128s = new BasicSecurityAlgorithmSuite (128, true, false);
  231. b128sr = new BasicSecurityAlgorithmSuite (128, true, true);
  232. b192 = new BasicSecurityAlgorithmSuite (192, false, false);
  233. b192r = new BasicSecurityAlgorithmSuite (192, false, true);
  234. b192s = new BasicSecurityAlgorithmSuite (192, true, false);
  235. b192sr = new BasicSecurityAlgorithmSuite (192, true, true);
  236. b256 = new BasicSecurityAlgorithmSuite (256, false, false);
  237. b256r = new BasicSecurityAlgorithmSuite (256, false, true);
  238. b256s = new BasicSecurityAlgorithmSuite (256, true, false);
  239. b256sr = new BasicSecurityAlgorithmSuite (256, true, true);
  240. tdes = new TripleDESSecurityAlgorithmSuite (false, false);
  241. tdes_r = new TripleDESSecurityAlgorithmSuite (false, true);
  242. tdes_s = new TripleDESSecurityAlgorithmSuite (true, false);
  243. tdes_sr = new TripleDESSecurityAlgorithmSuite (true, true);
  244. }
  245. public static SecurityAlgorithmSuite Default {
  246. get { return Basic256; }
  247. }
  248. public static SecurityAlgorithmSuite Basic128 {
  249. get { return b128; }
  250. }
  251. public static SecurityAlgorithmSuite Basic128Rsa15 {
  252. get { return b128r; }
  253. }
  254. public static SecurityAlgorithmSuite Basic128Sha256 {
  255. get { return b128s; }
  256. }
  257. public static SecurityAlgorithmSuite Basic128Sha256Rsa15 {
  258. get { return b128sr; }
  259. }
  260. public static SecurityAlgorithmSuite Basic192 {
  261. get { return b192; }
  262. }
  263. public static SecurityAlgorithmSuite Basic192Rsa15 {
  264. get { return b192r; }
  265. }
  266. public static SecurityAlgorithmSuite Basic192Sha256 {
  267. get { return b192s; }
  268. }
  269. public static SecurityAlgorithmSuite Basic192Sha256Rsa15 {
  270. get { return b192sr; }
  271. }
  272. public static SecurityAlgorithmSuite Basic256 {
  273. get { return b256; }
  274. }
  275. public static SecurityAlgorithmSuite Basic256Rsa15 {
  276. get { return b256r; }
  277. }
  278. public static SecurityAlgorithmSuite Basic256Sha256 {
  279. get { return b256s; }
  280. }
  281. public static SecurityAlgorithmSuite Basic256Sha256Rsa15 {
  282. get { return b256sr; }
  283. }
  284. public static SecurityAlgorithmSuite TripleDes {
  285. get { return tdes; }
  286. }
  287. public static SecurityAlgorithmSuite TripleDesRsa15 {
  288. get { return tdes_r; }
  289. }
  290. public static SecurityAlgorithmSuite TripleDesSha256 {
  291. get { return tdes_s; }
  292. }
  293. public static SecurityAlgorithmSuite TripleDesSha256Rsa15 {
  294. get { return tdes_sr; }
  295. }
  296. #endregion
  297. #region Instance members
  298. protected SecurityAlgorithmSuite ()
  299. {
  300. }
  301. public abstract string DefaultAsymmetricKeyWrapAlgorithm { get; }
  302. public abstract string DefaultAsymmetricSignatureAlgorithm { get; }
  303. public abstract string DefaultCanonicalizationAlgorithm { get; }
  304. public abstract string DefaultDigestAlgorithm { get; }
  305. public abstract string DefaultEncryptionAlgorithm { get; }
  306. public abstract int DefaultEncryptionKeyDerivationLength { get; }
  307. public abstract int DefaultSignatureKeyDerivationLength { get; }
  308. public abstract int DefaultSymmetricKeyLength { get; }
  309. public abstract string DefaultSymmetricKeyWrapAlgorithm { get; }
  310. public abstract string DefaultSymmetricSignatureAlgorithm { get; }
  311. public virtual bool IsAsymmetricKeyWrapAlgorithmSupported (
  312. string algorithm)
  313. {
  314. return algorithm == DefaultAsymmetricKeyWrapAlgorithm;
  315. }
  316. public abstract bool IsAsymmetricKeyLengthSupported (int length);
  317. public virtual bool IsAsymmetricSignatureAlgorithmSupported (
  318. string algorithm)
  319. {
  320. return algorithm == DefaultAsymmetricSignatureAlgorithm;
  321. }
  322. [MonoTODO]
  323. public virtual bool IsCanonicalizationAlgorithmSupported (
  324. string algorithm)
  325. {
  326. throw new NotImplementedException ();
  327. }
  328. [MonoTODO]
  329. public virtual bool IsDigestAlgorithmSupported (string algorithm)
  330. {
  331. throw new NotImplementedException ();
  332. }
  333. [MonoTODO]
  334. public virtual bool IsEncryptionAlgorithmSupported (
  335. string algorithm)
  336. {
  337. throw new NotImplementedException ();
  338. }
  339. [MonoTODO]
  340. public virtual bool IsEncryptionKeyDerivationAlgorithmSupported (
  341. string algorithm)
  342. {
  343. throw new NotImplementedException ();
  344. }
  345. [MonoTODO]
  346. public virtual bool IsSignatureKeyDerivationAlgorithmSupported (
  347. string algorithm)
  348. {
  349. throw new NotImplementedException ();
  350. }
  351. public abstract bool IsSymmetricKeyLengthSupported (int length);
  352. [MonoTODO]
  353. public virtual bool IsSymmetricKeyWrapAlgorithmSupported (
  354. string algorithm)
  355. {
  356. throw new NotImplementedException ();
  357. }
  358. [MonoTODO]
  359. public virtual bool IsSymmetricSignatureAlgorithmSupported (
  360. string algorithm)
  361. {
  362. throw new NotImplementedException ();
  363. }
  364. #endregion
  365. }
  366. }