TlsUtilities.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 » TlsUtilities.cs
using System;
using System.IO;
using System.Text;

using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Crypto.Tls{
  /// <remarks>Some helper fuctions for MicroTLS.</remarks>
  public class TlsUtilities
  {
    internal static void WriteUint8(short i, Stream os)
    {
      os.WriteByte((byte)i);
    }

    internal static void WriteUint8(short i, byte[] buf, int offset)
    {
      buf[offset] = (byte)i;
    }

    internal static void WriteUint16(int i, Stream os)
    {
      os.WriteByte((byte)(i >> 8));
      os.WriteByte((byte)i);
    }

    internal static void WriteUint16(int i, byte[] buf, int offset)
    {
      buf[offset] = (byte)(i >> 8);
      buf[offset + 1] = (byte)i;
    }

    internal static void WriteUint24(int i, Stream os)
    {
      os.WriteByte((byte)(i >> 16));
      os.WriteByte((byte)(i >> 8));
      os.WriteByte((byte)i);
    }

    internal static void WriteUint24(int i, byte[] buf, int offset)
    {
      buf[offset] = (byte)(i >> 16);
      buf[offset + 1] = (byte)(i >> 8);
      buf[offset + 2] = (byte)(i);
    }

    internal static void WriteUint64(long i, Stream os)
    {
      os.WriteByte((byte)(i >> 56));
      os.WriteByte((byte)(i >> 48));
      os.WriteByte((byte)(i >> 40));
      os.WriteByte((byte)(i >> 32));
      os.WriteByte((byte)(i >> 24));
      os.WriteByte((byte)(i >> 16));
      os.WriteByte((byte)(i >> 8));
      os.WriteByte((byte)i);
    }

    internal static void WriteUint64(long i, byte[] buf, int offset)
    {
      buf[offset] = (byte)(i >> 56);
      buf[offset + 1] = (byte)(i >> 48);
      buf[offset + 2] = (byte)(i >> 40);
      buf[offset + 3] = (byte)(i >> 32);
      buf[offset + 4] = (byte)(i >> 24);
      buf[offset + 5] = (byte)(i >> 16);
      buf[offset + 6] = (byte)(i >> 8);
      buf[offset + 7] = (byte)(i);
    }

    internal static void WriteOpaque8(byte[] buf, Stream os)
    {
      WriteUint8((short)buf.Length, os);
      os.Write(buf, 0, buf.Length);
    }

    internal static void WriteOpaque16(byte[] buf, Stream os)
    {
      WriteUint16(buf.Length, os);
      os.Write(buf, 0, buf.Length);
    }

    internal static void WriteOpaque24(byte[] buf, Stream os)
    {
      WriteUint24(buf.Length, os);
      os.Write(buf, 0, buf.Length);
    }

    internal static short ReadUint8(Stream inStr)
    {
      int i = inStr.ReadByte();
      if (i < 0)
      {
        throw new EndOfStreamException();
      }
      return (short)i;
    }

    internal static int ReadUint16(Stream inStr)
    {
      int i1 = inStr.ReadByte();
      int i2 = inStr.ReadByte();
      if ((i1 | i2) < 0)
      {
        throw new EndOfStreamException();
      }
      return i1 << 8 | i2;
    }

    internal static int ReadUint24(Stream inStr)
    {
      int i1 = inStr.ReadByte();
      int i2 = inStr.ReadByte();
      int i3 = inStr.ReadByte();
      if ((i1 | i2 | i3) < 0)
      {
        throw new EndOfStreamException();
      }
      return (i1 << 16) | (i2 << 8) | i3;
    }

    internal static void ReadFully(byte[] buf, Stream inStr)
    {
      if (Streams.ReadFully(inStr, buf, 0, buf.Length) < buf.Length)
        throw new EndOfStreamException();
    }

    internal static byte[] ReadOpaque8(Stream inStr)
    {
      short length = ReadUint8(inStr);
      byte[] bytes = new byte[length];
      ReadFully(bytes, inStr);
      return bytes;
    }

    internal static byte[] ReadOpaque16(Stream inStr)
    {
      int length = ReadUint16(inStr);
      byte[] bytes = new byte[length];
      ReadFully(bytes, inStr);
      return bytes;
    }

    internal static void CheckVersion(byte[] readVersion, TlsProtocolHandler handler)
    {
      if ((readVersion[0] != 3) || (readVersion[1] != 1))
      {
        handler.FailWithError(TlsProtocolHandler.AL_fatal, TlsProtocolHandler.AP_protocol_version);
      }
    }

    internal static void CheckVersion(Stream inStr, TlsProtocolHandler handler)
    {
      int i1 = inStr.ReadByte();
      int i2 = inStr.ReadByte();
      if ((i1 != 3) || (i2 != 1))
      {
        handler.FailWithError(TlsProtocolHandler.AL_fatal, TlsProtocolHandler.AP_protocol_version);
      }
    }

    internal static void WriteVersion(Stream os)
    {
      os.WriteByte(3);
      os.WriteByte(1);
    }

    internal static void WriteVersion(byte[] buf, int offset)
    {
      buf[offset] = 3;
      buf[offset + 1] = 1;
    }

    private static void hmac_hash(IDigest digest, byte[] secret, byte[] seed, byte[] output)
    {
      HMac mac = new HMac(digest);
      KeyParameter param = new KeyParameter(secret);
      byte[] a = seed;
      int size = digest.GetDigestSize();
      int iterations = (output.Length + size - 1) / size;
      byte[] buf = new byte[mac.GetMacSize()];
      byte[] buf2 = new byte[mac.GetMacSize()];
      for (int i = 0; i < iterations; i++)
      {
        mac.Init(param);
        mac.BlockUpdate(a, 0, a.Length);
        mac.DoFinal(buf, 0);
        a = buf;
        mac.Init(param);
        mac.BlockUpdate(a, 0, a.Length);
        mac.BlockUpdate(seed, 0, seed.Length);
        mac.DoFinal(buf2, 0);
        Array.Copy(buf2, 0, output, (size * i), System.Math.Min(size, output.Length - (size * i)));
      }
    }

    internal static void PRF(
      byte[]  secret,
      String  asciiLabel,
      byte[]  seed,
      byte[]  buf)
    {
      byte[] label = Encoding.ASCII.GetBytes(asciiLabel);

      int s_half = (secret.Length + 1) / 2;
      byte[] s1 = new byte[s_half];
      byte[] s2 = new byte[s_half];
      Array.Copy(secret, 0, s1, 0, s_half);
      Array.Copy(secret, secret.Length - s_half, s2, 0, s_half);

      byte[] ls = new byte[label.Length + seed.Length];
      Array.Copy(label, 0, ls, 0, label.Length);
      Array.Copy(seed, 0, ls, label.Length, seed.Length);

      byte[] prf = new byte[buf.Length];
      hmac_hash(new MD5Digest(), s1, ls, prf);
      hmac_hash(new Sha1Digest(), s2, ls, buf);
      for (int i = 0; i < buf.Length; i++)
      {
        buf[i] ^= prf[i];
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.