diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-07-12 15:15:36 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-07-12 15:15:36 +0700 |
commit | 68c795fe81277f73aeb90d8ad4c6f4305f32c906 (patch) | |
tree | 59643344aafef91bbd4c4a3a7973deba3d837a00 /crypto/src/tls/KeyShareEntry.cs | |
parent | TLS test tweaks (diff) | |
download | BouncyCastle.NET-ed25519-68c795fe81277f73aeb90d8ad4c6f4305f32c906.tar.xz |
Port of new TLS API from bc-java
Diffstat (limited to 'crypto/src/tls/KeyShareEntry.cs')
-rw-r--r-- | crypto/src/tls/KeyShareEntry.cs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/crypto/src/tls/KeyShareEntry.cs b/crypto/src/tls/KeyShareEntry.cs new file mode 100644 index 000000000..c4be657c0 --- /dev/null +++ b/crypto/src/tls/KeyShareEntry.cs @@ -0,0 +1,62 @@ +using System; +using System.IO; + +namespace Org.BouncyCastle.Tls +{ + public sealed class KeyShareEntry + { + private static bool CheckKeyExchangeLength(int length) + { + return 0 < length && length < (1 << 16); + } + + private readonly int m_namedGroup; + private readonly byte[] m_keyExchange; + + /// <param name="namedGroup"><see cref="NamedGroup"/></param> + /// <param name="keyExchange"></param> + public KeyShareEntry(int namedGroup, byte[] keyExchange) + { + if (!TlsUtilities.IsValidUint16(namedGroup)) + throw new ArgumentException("should be a uint16", "namedGroup"); + if (null == keyExchange) + throw new ArgumentNullException("keyExchange"); + if (!CheckKeyExchangeLength(keyExchange.Length)) + throw new ArgumentException("must have length from 1 to (2^16 - 1)", "keyExchange"); + + this.m_namedGroup = namedGroup; + this.m_keyExchange = keyExchange; + } + + /// <returns><see cref="NamedGroup"/></returns> + public int NamedGroup + { + get { return m_namedGroup; } + } + + public byte[] KeyExchange + { + get { return m_keyExchange; } + } + + /// <summary>Encode this <see cref="KeyShareEntry"/> to a <see cref="Stream"/>.</summary> + /// <param name="output">the <see cref="Stream"/> to encode to.</param> + /// <exception cref="IOException"/> + public void Encode(Stream output) + { + TlsUtilities.WriteUint16(NamedGroup, output); + TlsUtilities.WriteOpaque16(KeyExchange, output); + } + + /// <summary>Parse a <see cref="KeyShareEntry"/> from a <see cref="Stream"/>.</summary> + /// <param name="input">the <see cref="Stream"/> to parse from.</param> + /// <returns>a <see cref="KeyShareEntry"/> object.</returns> + /// <exception cref="IOException"/> + public static KeyShareEntry Parse(Stream input) + { + int namedGroup = TlsUtilities.ReadUint16(input); + byte[] keyExchange = TlsUtilities.ReadOpaque16(input, 1); + return new KeyShareEntry(namedGroup, keyExchange); + } + } +} |