TlsCipherSuiteManager.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Tls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Tls » TlsCipherSuiteManager.cs
using System;
using System.IO;

using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;

namespace Org.BouncyCastle.Crypto.Tls{
  /// <remarks>
  /// A manager for ciphersuite. This class does manage all ciphersuites
  /// which are used by MicroTLS.
  /// </remarks>
  public class TlsCipherSuiteManager
  {
    private const int TLS_RSA_WITH_3DES_EDE_CBC_SHA = 0x000a;
    private const int TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA = 0x0013;
    private const int TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA = 0x0016;
    private const int TLS_RSA_WITH_AES_128_CBC_SHA = 0x002f;
    private const int TLS_DHE_DSS_WITH_AES_128_CBC_SHA = 0x0032;
    private const int TLS_DHE_RSA_WITH_AES_128_CBC_SHA = 0x0033;
    private const int TLS_RSA_WITH_AES_256_CBC_SHA = 0x0035;
    private const int TLS_DHE_DSS_WITH_AES_256_CBC_SHA = 0x0038;
    private const int TLS_DHE_RSA_WITH_AES_256_CBC_SHA = 0x0039;

//    private const int TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA = 0xC01A;    
//    private const int TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA = 0xC01B;
//    private const int TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA = 0xC01C;
//    private const int TLS_SRP_SHA_WITH_AES_128_CBC_SHA = 0xC01D;
//    private const int TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA = 0xC01E;
//    private const int TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA = 0xC01F;
//    private const int TLS_SRP_SHA_WITH_AES_256_CBC_SHA = 0xC020;
//    private const int TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA = 0xC021;
//    private const int TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA = 0xC022;

    internal static void WriteCipherSuites(
      Stream outStr)
    {
      int[] suites = new int[]
      {
        TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
        TLS_DHE_DSS_WITH_AES_256_CBC_SHA,
        TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
        TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
        TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
        TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA,
        TLS_RSA_WITH_AES_256_CBC_SHA,
        TLS_RSA_WITH_AES_128_CBC_SHA,
        TLS_RSA_WITH_3DES_EDE_CBC_SHA,

//        TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
//        TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
//        TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA,
//        TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
//        TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
//        TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA,
//        TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
//        TLS_SRP_SHA_WITH_AES_128_CBC_SHA,
//        TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA,
      };

      TlsUtilities.WriteUint16(2 * suites.Length, outStr);
      for (int i = 0; i < suites.Length; ++i)
      {
        TlsUtilities.WriteUint16(suites[i], outStr);
      }
    }

    internal static TlsCipherSuite GetCipherSuite(
      int          number,
      TlsProtocolHandler  handler)
    {
      switch (number)
      {
        case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_RSA);

        case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_DHE_DSS);

        case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_DHE_RSA);

        case TLS_RSA_WITH_AES_128_CBC_SHA:
          return createAesCipherSuite(16, TlsCipherSuite.KE_RSA);

        case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
          return createAesCipherSuite(16, TlsCipherSuite.KE_DHE_DSS);

        case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
          return createAesCipherSuite(16, TlsCipherSuite.KE_DHE_RSA);

        case TLS_RSA_WITH_AES_256_CBC_SHA:
          return createAesCipherSuite(32, TlsCipherSuite.KE_RSA);

        case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
          return createAesCipherSuite(32, TlsCipherSuite.KE_DHE_DSS);

        case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
          return createAesCipherSuite(32, TlsCipherSuite.KE_DHE_RSA);

//        case TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
//          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_SRP);
//        
//        case TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
//          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_SRP_RSA);
//        
//        case TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
//          return createDesEdeCipherSuite(24, TlsCipherSuite.KE_SRP_DSS);
//        
//        case TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
//          return createAesCipherSuite(16, TlsCipherSuite.KE_SRP);
//        
//        case TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
//          return createAesCipherSuite(16, TlsCipherSuite.KE_SRP_RSA);
//        
//        case TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
//          return createAesCipherSuite(16, TlsCipherSuite.KE_SRP_DSS);
//        
//        case TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
//          return createAesCipherSuite(32, TlsCipherSuite.KE_SRP);
//        
//        case TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
//          return createAesCipherSuite(32, TlsCipherSuite.KE_SRP_RSA);
//        
//        case TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
//          return createAesCipherSuite(32, TlsCipherSuite.KE_SRP_DSS);

        default:
          handler.FailWithError(TlsProtocolHandler.AL_fatal, TlsProtocolHandler.AP_handshake_failure);

          /*
          * Unreachable Code, failWithError will always throw an exception!
          */
          return null;
      }
    }

      private static TlsCipherSuite createAesCipherSuite(int cipherKeySize, short keyExchange)
      {
          return new TlsBlockCipherCipherSuite(createAesCipher(), createAesCipher(),
              new Sha1Digest(), new Sha1Digest(), cipherKeySize, keyExchange);
      }

      private static TlsCipherSuite createDesEdeCipherSuite(int cipherKeySize, short keyExchange)
      {
          return new TlsBlockCipherCipherSuite(createDesEdeCipher(), createDesEdeCipher(),
              new Sha1Digest(), new Sha1Digest(), cipherKeySize, keyExchange);
      }

      private static CbcBlockCipher createAesCipher()
      {
          return new CbcBlockCipher(new AesFastEngine());
      }

      private static CbcBlockCipher createDesEdeCipher()
      {
          return new CbcBlockCipher(new DesEdeEngine());
      }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.