| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- //
- // SecurityAlgorithmSuite.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.IdentityModel.Tokens;
- using System.Security.Cryptography.Xml;
- using System.ServiceModel;
- using System.ServiceModel.Security.Tokens;
- namespace System.ServiceModel.Security
- {
- public abstract class SecurityAlgorithmSuite
- {
- #region Internal Class
- class BasicSecurityAlgorithmSuite : SecurityAlgorithmSuiteImplBase
- {
- public BasicSecurityAlgorithmSuite (int size, bool sha, bool rsa)
- : base (size, sha, rsa, false)
- {
- }
- public override int DefaultSignatureKeyDerivationLength {
- get { return Size > 192 ? 192 : Size; }
- }
- public override bool IsAsymmetricKeyLengthSupported (int length)
- {
- switch (length) {
- case 128:
- case 192:
- return Size >= length;
- }
- return false;
- }
- public override bool IsSymmetricKeyLengthSupported (int length)
- {
- switch (length) {
- case 128:
- case 192:
- case 256:
- return Size >= length;
- }
- return false;
- }
- public override bool IsSymmetricKeyWrapAlgorithmSupported (string algorithm)
- {
- switch (Size) {
- case 256:
- if (algorithm == EncryptedXml.XmlEncAES256KeyWrapUrl)
- return true;
- goto case 192;
- case 192:
- if (algorithm == EncryptedXml.XmlEncAES192KeyWrapUrl)
- return true;
- goto case 128;
- case 128:
- return algorithm == EncryptedXml.XmlEncAES128KeyWrapUrl;
- }
- return false;
- }
- }
- class TripleDESSecurityAlgorithmSuite : SecurityAlgorithmSuiteImplBase
- {
- public TripleDESSecurityAlgorithmSuite (bool sha, bool rsa)
- : base (192, sha, rsa, true)
- {
- }
- public override int DefaultSignatureKeyDerivationLength {
- get { return 192; }
- }
- public override bool IsAsymmetricKeyLengthSupported (int length)
- {
- return length == 192;
- }
- public override bool IsSymmetricKeyLengthSupported (int length)
- {
- return length == 192;
- }
- public override bool IsSymmetricKeyWrapAlgorithmSupported (
- string algorithm)
- {
- return algorithm == EncryptedXml.XmlEncTripleDESKeyWrapUrl;
- }
- }
- abstract class SecurityAlgorithmSuiteImplBase : SecurityAlgorithmSuite
- {
- int size;
- bool rsa15, sha256, tdes;
- public SecurityAlgorithmSuiteImplBase (
- int size, bool sha256, bool rsa15, bool tripleDes)
- {
- this.size = size;
- this.sha256 = sha256;
- this.rsa15 = rsa15;
- this.tdes = tripleDes;
- }
- public int Size {
- get { return size; }
- }
- public bool Rsa15 {
- get { return rsa15; }
- }
- public bool Sha256 {
- get { return sha256; }
- }
- public override string DefaultAsymmetricKeyWrapAlgorithm {
- get { return rsa15 ? EncryptedXml.XmlEncRSA15Url : EncryptedXml.XmlEncRSAOAEPUrl; }
- }
- public override string DefaultAsymmetricSignatureAlgorithm {
- get { return sha256 ? SecurityAlgorithms.RsaSha256Signature : SignedXml.XmlDsigRSASHA1Url; }
- }
- public override string DefaultCanonicalizationAlgorithm {
- get { return SignedXml.XmlDsigExcC14NTransformUrl; }
- }
- public override string DefaultDigestAlgorithm {
- get { return sha256 ? EncryptedXml.XmlEncSHA256Url : SignedXml.XmlDsigSHA1Url; }
- }
- public override string DefaultEncryptionAlgorithm {
- get {
- if (tdes)
- return EncryptedXml.XmlEncTripleDESUrl;
- switch (size) {
- case 128:
- return EncryptedXml.XmlEncAES128Url;
- case 192:
- return EncryptedXml.XmlEncAES192Url;
- case 256:
- return EncryptedXml.XmlEncAES256Url;
- }
- throw new Exception ("Should not happen.");
- }
- }
- public override int DefaultEncryptionKeyDerivationLength {
- get { return size; }
- }
- public override int DefaultSymmetricKeyLength {
- get { return size; }
- }
- public override string DefaultSymmetricKeyWrapAlgorithm {
- get {
- if (tdes)
- return EncryptedXml.XmlEncTripleDESKeyWrapUrl;
- switch (size) {
- case 128:
- return EncryptedXml.XmlEncAES128KeyWrapUrl;
- case 192:
- return EncryptedXml.XmlEncAES192KeyWrapUrl;
- case 256:
- return EncryptedXml.XmlEncAES256KeyWrapUrl;
- }
- throw new Exception ("Should not happen.");
- }
- }
- public override string DefaultSymmetricSignatureAlgorithm {
- get { return sha256 ? SecurityAlgorithms.HmacSha256Signature : SignedXml.XmlDsigHMACSHA1Url; }
- }
- [MonoTODO]
- public override bool IsAsymmetricSignatureAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsCanonicalizationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsDigestAlgorithmSupported (string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsEncryptionAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsEncryptionKeyDerivationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsSignatureKeyDerivationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public override bool IsSymmetricSignatureAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- }
- #endregion
- #region Static members
- static SecurityAlgorithmSuite b128, b128r, b128s, b128sr;
- static SecurityAlgorithmSuite b192, b192r, b192s, b192sr;
- static SecurityAlgorithmSuite b256, b256r, b256s, b256sr;
- static SecurityAlgorithmSuite tdes, tdes_r, tdes_s, tdes_sr;
- static SecurityAlgorithmSuite ()
- {
- b128 = new BasicSecurityAlgorithmSuite (128, false, false);
- b128r = new BasicSecurityAlgorithmSuite (128, false, true);
- b128s = new BasicSecurityAlgorithmSuite (128, true, false);
- b128sr = new BasicSecurityAlgorithmSuite (128, true, true);
- b192 = new BasicSecurityAlgorithmSuite (192, false, false);
- b192r = new BasicSecurityAlgorithmSuite (192, false, true);
- b192s = new BasicSecurityAlgorithmSuite (192, true, false);
- b192sr = new BasicSecurityAlgorithmSuite (192, true, true);
- b256 = new BasicSecurityAlgorithmSuite (256, false, false);
- b256r = new BasicSecurityAlgorithmSuite (256, false, true);
- b256s = new BasicSecurityAlgorithmSuite (256, true, false);
- b256sr = new BasicSecurityAlgorithmSuite (256, true, true);
- tdes = new TripleDESSecurityAlgorithmSuite (false, false);
- tdes_r = new TripleDESSecurityAlgorithmSuite (false, true);
- tdes_s = new TripleDESSecurityAlgorithmSuite (true, false);
- tdes_sr = new TripleDESSecurityAlgorithmSuite (true, true);
- }
- public static SecurityAlgorithmSuite Default {
- get { return Basic256; }
- }
- public static SecurityAlgorithmSuite Basic128 {
- get { return b128; }
- }
- public static SecurityAlgorithmSuite Basic128Rsa15 {
- get { return b128r; }
- }
- public static SecurityAlgorithmSuite Basic128Sha256 {
- get { return b128s; }
- }
- public static SecurityAlgorithmSuite Basic128Sha256Rsa15 {
- get { return b128sr; }
- }
- public static SecurityAlgorithmSuite Basic192 {
- get { return b192; }
- }
- public static SecurityAlgorithmSuite Basic192Rsa15 {
- get { return b192r; }
- }
- public static SecurityAlgorithmSuite Basic192Sha256 {
- get { return b192s; }
- }
- public static SecurityAlgorithmSuite Basic192Sha256Rsa15 {
- get { return b192sr; }
- }
- public static SecurityAlgorithmSuite Basic256 {
- get { return b256; }
- }
- public static SecurityAlgorithmSuite Basic256Rsa15 {
- get { return b256r; }
- }
- public static SecurityAlgorithmSuite Basic256Sha256 {
- get { return b256s; }
- }
- public static SecurityAlgorithmSuite Basic256Sha256Rsa15 {
- get { return b256sr; }
- }
- public static SecurityAlgorithmSuite TripleDes {
- get { return tdes; }
- }
- public static SecurityAlgorithmSuite TripleDesRsa15 {
- get { return tdes_r; }
- }
- public static SecurityAlgorithmSuite TripleDesSha256 {
- get { return tdes_s; }
- }
- public static SecurityAlgorithmSuite TripleDesSha256Rsa15 {
- get { return tdes_sr; }
- }
- #endregion
- #region Instance members
- protected SecurityAlgorithmSuite ()
- {
- }
- public abstract string DefaultAsymmetricKeyWrapAlgorithm { get; }
- public abstract string DefaultAsymmetricSignatureAlgorithm { get; }
- public abstract string DefaultCanonicalizationAlgorithm { get; }
- public abstract string DefaultDigestAlgorithm { get; }
- public abstract string DefaultEncryptionAlgorithm { get; }
- public abstract int DefaultEncryptionKeyDerivationLength { get; }
- public abstract int DefaultSignatureKeyDerivationLength { get; }
- public abstract int DefaultSymmetricKeyLength { get; }
- public abstract string DefaultSymmetricKeyWrapAlgorithm { get; }
- public abstract string DefaultSymmetricSignatureAlgorithm { get; }
- public virtual bool IsAsymmetricKeyWrapAlgorithmSupported (
- string algorithm)
- {
- return algorithm == DefaultAsymmetricKeyWrapAlgorithm;
- }
- public abstract bool IsAsymmetricKeyLengthSupported (int length);
- public virtual bool IsAsymmetricSignatureAlgorithmSupported (
- string algorithm)
- {
- return algorithm == DefaultAsymmetricSignatureAlgorithm;
- }
- [MonoTODO]
- public virtual bool IsCanonicalizationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual bool IsDigestAlgorithmSupported (string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual bool IsEncryptionAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual bool IsEncryptionKeyDerivationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual bool IsSignatureKeyDerivationAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- public abstract bool IsSymmetricKeyLengthSupported (int length);
- [MonoTODO]
- public virtual bool IsSymmetricKeyWrapAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public virtual bool IsSymmetricSignatureAlgorithmSupported (
- string algorithm)
- {
- throw new NotImplementedException ();
- }
- #endregion
- }
- }
|