PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop/Configuration/SecurityAlgorithmConverter.cs

#
C# | 222 lines | 143 code | 43 blank | 36 comment | 45 complexity | f9a1263a04f55130f9bdbf5e5f543ee6 MD5 | raw file
  1// <copyright file="SecurityAlgorithmConverter.cs" company="Microsoft Corporation">
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ServiceModel.Interop.Configuration
  6{
  7    using System;
  8    using System.ComponentModel;
  9    using System.ComponentModel.Design.Serialization;
 10    using System.Globalization;
 11    using System.ServiceModel.Security;
 12    using Microsoft.ServiceModel.Interop.Properties;
 13
 14    /// <summary>
 15    /// Type converter implementation for security algorithms
 16    /// </summary>
 17    internal class SecurityAlgorithmSuiteConverter : TypeConverter
 18    {
 19        /// <summary>
 20        /// Gets a value indicating whether this converter can convert an object in the
 21        /// given source type to a Security algorithm suite using the specified context.
 22        /// </summary>
 23        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 24        /// <param name="sourceType">A System.Type that represents the type you wish to convert from.</param>
 25        /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
 26        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 27        {
 28            return (typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType);
 29        }
 30
 31        /// <summary>
 32        /// Gets a value indicating whether this converter can convert an object to the
 33        /// given destination type using the context.
 34        /// </summary>
 35        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 36        /// <param name="destinationType">A System.Type that represents the type you wish to convert to.</param>
 37        /// <returns>true if this converter can perform the conversion; otherwise, false.</returns>
 38        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 39        {
 40            return (typeof(InstanceDescriptor) == destinationType) || base.CanConvertTo(context, destinationType);
 41        }
 42
 43        /// <summary>
 44        /// Converts the given object to a Security Algorithm Suite.
 45        /// </summary>
 46        /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that provides a format context.</param>
 47        /// <param name="culture">An optional System.Globalization.CultureInfo. If not supplied, the current
 48        /// culture is assumed.</param>
 49        /// <param name="value">The System.Object to convert.</param>
 50        /// <returns>An System.Object that represents the converted value.</returns>
 51        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 52        {
 53            string str = value as string;
 54
 55            if (str == null)
 56            {
 57                return base.ConvertFrom(context, culture, value);
 58            }
 59
 60            switch (str)
 61            {
 62                case "Default":
 63                    return SecurityAlgorithmSuite.Default;
 64
 65                case "Basic256":
 66                    return SecurityAlgorithmSuite.Basic256;
 67
 68                case "Basic192":
 69                    return SecurityAlgorithmSuite.Basic192;
 70
 71                case "Basic128":
 72                    return SecurityAlgorithmSuite.Basic128;
 73
 74                case "TripleDes":
 75                    return SecurityAlgorithmSuite.TripleDes;
 76
 77                case "Basic256Rsa15":
 78                    return SecurityAlgorithmSuite.Basic256Rsa15;
 79
 80                case "Basic192Rsa15":
 81                    return SecurityAlgorithmSuite.Basic192Rsa15;
 82
 83                case "Basic128Rsa15":
 84                    return SecurityAlgorithmSuite.Basic128Rsa15;
 85
 86                case "TripleDesRsa15":
 87                    return SecurityAlgorithmSuite.TripleDesRsa15;
 88
 89                case "Basic256Sha256":
 90                    return SecurityAlgorithmSuite.Basic256Sha256;
 91
 92                case "Basic192Sha256":
 93                    return SecurityAlgorithmSuite.Basic192Sha256;
 94
 95                case "Basic128Sha256":
 96                    return SecurityAlgorithmSuite.Basic128Sha256;
 97
 98                case "TripleDesSha256":
 99                    return SecurityAlgorithmSuite.TripleDesSha256;
100
101                case "Basic256Sha256Rsa15":
102                    return SecurityAlgorithmSuite.Basic256Sha256Rsa15;
103
104                case "Basic192Sha256Rsa15":
105                    return SecurityAlgorithmSuite.Basic192Sha256Rsa15;
106
107                case "Basic128Sha256Rsa15":
108                    return SecurityAlgorithmSuite.Basic128Sha256Rsa15;
109
110                case "TripleDesSha256Rsa15":
111                    return SecurityAlgorithmSuite.TripleDesSha256Rsa15;
112            }
113
114            throw new ArgumentOutOfRangeException("value", string.Format(CultureInfo.InvariantCulture, Strings.Not_Supported_Algorithm, typeof(SecurityAlgorithmSuite).FullName));
115        }
116
117        /// <summary>
118        /// Converts the given object to another type.
119        /// </summary>
120        /// <param name="context">A formatter context.</param>
121        /// <param name="culture">The culture into which value will be converted.</param>
122        /// <param name="value">The object to convert.</param>
123        /// <param name="destinationType">The type to convert the object to.</param>
124        /// <returns>The converted object.</returns>
125        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
126        {
127            SecurityAlgorithmSuite suite = value as SecurityAlgorithmSuite;
128
129            if (!(typeof(string) == destinationType) || suite == null)
130            {
131                return base.ConvertTo(context, culture, value, destinationType);
132            }
133
134            if (suite == SecurityAlgorithmSuite.Default)
135            {
136                return "Default";
137            }
138
139            if (suite == SecurityAlgorithmSuite.Basic256)
140            {
141                return "Basic256";
142            }
143
144            if (suite == SecurityAlgorithmSuite.Basic192)
145            {
146                return "Basic192";
147            }
148
149            if (suite == SecurityAlgorithmSuite.Basic128)
150            {
151                return "Basic128";
152            }
153
154            if (suite == SecurityAlgorithmSuite.TripleDes)
155            {
156                return "TripleDes";
157            }
158
159            if (suite == SecurityAlgorithmSuite.Basic256Rsa15)
160            {
161                return "Basic256Rsa15";
162            }
163
164            if (suite == SecurityAlgorithmSuite.Basic192Rsa15)
165            {
166                return "Basic192Rsa15";
167            }
168
169            if (suite == SecurityAlgorithmSuite.Basic128Rsa15)
170            {
171                return "Basic128Rsa15";
172            }
173
174            if (suite == SecurityAlgorithmSuite.TripleDesRsa15)
175            {
176                return "TripleDesRsa15";
177            }
178
179            if (suite == SecurityAlgorithmSuite.Basic256Sha256)
180            {
181                return "Basic256Sha256";
182            }
183
184            if (suite == SecurityAlgorithmSuite.Basic192Sha256)
185            {
186                return "Basic192Sha256";
187            }
188
189            if (suite == SecurityAlgorithmSuite.Basic128Sha256)
190            {
191                return "Basic128Sha256";
192            }
193
194            if (suite == SecurityAlgorithmSuite.TripleDesSha256)
195            {
196                return "TripleDesSha256";
197            }
198
199            if (suite == SecurityAlgorithmSuite.Basic256Sha256Rsa15)
200            {
201                return "Basic256Sha256Rsa15";
202            }
203
204            if (suite == SecurityAlgorithmSuite.Basic192Sha256Rsa15)
205            {
206                return "Basic192Sha256Rsa15";
207            }
208
209            if (suite == SecurityAlgorithmSuite.Basic128Sha256Rsa15)
210            {
211                return "Basic128Sha256Rsa15";
212            }
213
214            if (suite != SecurityAlgorithmSuite.TripleDesSha256Rsa15)
215            {
216                throw new ArgumentOutOfRangeException("value", string.Format(CultureInfo.InvariantCulture, Strings.Not_Supported_Algorithm, typeof(SecurityAlgorithmSuite).FullName));
217            }
218
219            return "TripleDesSha256Rsa15";
220        }
221    }
222}