From 68c795fe81277f73aeb90d8ad4c6f4305f32c906 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 12 Jul 2021 15:15:36 +0700 Subject: Port of new TLS API from bc-java --- crypto/src/tls/ProtocolName.cs | 88 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 crypto/src/tls/ProtocolName.cs (limited to 'crypto/src/tls/ProtocolName.cs') diff --git a/crypto/src/tls/ProtocolName.cs b/crypto/src/tls/ProtocolName.cs new file mode 100644 index 000000000..529e81b19 --- /dev/null +++ b/crypto/src/tls/ProtocolName.cs @@ -0,0 +1,88 @@ +using System; +using System.IO; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Tls +{ + /// RFC 7301 Represents a protocol name for use with ALPN. + public sealed class ProtocolName + { + public static ProtocolName AsRawBytes(byte[] bytes) + { + return new ProtocolName(Arrays.Clone(bytes)); + } + + public static ProtocolName AsUtf8Encoding(String name) + { + return new ProtocolName(Strings.ToUtf8ByteArray(name)); + } + + public static readonly ProtocolName Http_1_1 = AsUtf8Encoding("http/1.1"); + public static readonly ProtocolName Spdy_1 = AsUtf8Encoding("spdy/1"); + public static readonly ProtocolName Spdy_2 = AsUtf8Encoding("spdy/2"); + public static readonly ProtocolName Spdy_3 = AsUtf8Encoding("spdy/3"); + public static readonly ProtocolName Stun_Turn = AsUtf8Encoding("stun.turn"); + public static readonly ProtocolName Stun_Nat_Discovery = AsUtf8Encoding("stun.nat-discovery"); + public static readonly ProtocolName Http_2_Tls = AsUtf8Encoding("h2"); + public static readonly ProtocolName Http_2_Tcp = AsUtf8Encoding("h2c"); + public static readonly ProtocolName WebRtc = AsUtf8Encoding("webrtc"); + public static readonly ProtocolName WebRtc_Confidential = AsUtf8Encoding("c-webrtc"); + public static readonly ProtocolName Ftp = AsUtf8Encoding("ftp"); + public static readonly ProtocolName Imap = AsUtf8Encoding("imap"); + public static readonly ProtocolName Pop3 = AsUtf8Encoding("pop3"); + public static readonly ProtocolName ManageSieve = AsUtf8Encoding("managesieve"); + public static readonly ProtocolName Coap = AsUtf8Encoding("coap"); + public static readonly ProtocolName Xmpp_Client = AsUtf8Encoding("xmpp-client"); + public static readonly ProtocolName Xmpp_Server = AsUtf8Encoding("xmpp-server"); + + private readonly byte[] m_bytes; + + private ProtocolName(byte[] bytes) + { + if (bytes == null) + throw new ArgumentNullException("bytes"); + if (bytes.Length < 1 || bytes.Length > 255) + throw new ArgumentException("must have length from 1 to 255", "bytes"); + + this.m_bytes = bytes; + } + + public byte[] GetBytes() + { + return Arrays.Clone(m_bytes); + } + + public string GetUtf8Decoding() + { + return Strings.FromUtf8ByteArray(m_bytes); + } + + /// Encode this to a . + /// the to encode to. + /// + public void Encode(Stream output) + { + TlsUtilities.WriteOpaque8(m_bytes, output); + } + + /// Parse a from a . + /// the to parse from. + /// a object. + /// + public static ProtocolName Parse(Stream input) + { + return new ProtocolName(TlsUtilities.ReadOpaque8(input, 1)); + } + + public override bool Equals(object obj) + { + return obj is ProtocolName && Arrays.AreEqual(m_bytes, ((ProtocolName)obj).m_bytes); + } + + public override int GetHashCode() + { + return Arrays.GetHashCode(m_bytes); + } + } +} -- cgit 1.4.1