1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
//
// SecurityAlgorithmSuite.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// 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
}
}
|