Exemple #1
0
        public static DTLSRecord Deserialise(Stream stream)
        {
            DTLSRecord result = new DTLSRecord
            {
                _RecordType = (TRecordType)stream.ReadByte(),
                // could check here for a valid type, and bail out if invalid
                _Version        = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte()),
                _Epoch          = NetworkByteOrderConverter.ToUInt16(stream),
                _SequenceNumber = NetworkByteOrderConverter.ToInt48(stream),
                _Length         = NetworkByteOrderConverter.ToUInt16(stream)
            };

            if (result._Length > 0)
            {
                result._Fragment = new byte[result._Length];
                int length = stream.Read(result._Fragment, 0, result._Length);
                while (length < result._Length)
                {
                    int bytesRead = stream.Read(result._Fragment, length, result._Length - length);
                    if (bytesRead > 0)
                    {
                        length += bytesRead;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(result);
        }
Exemple #2
0
        public static CertificateVerify Deserialise(Stream stream, Version version)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            var result = new CertificateVerify();

            if (version > DTLSRecord.Version1_0)
            {
                var hash      = (THashAlgorithm)stream.ReadByte();
                var signature = (TSignatureAlgorithm)stream.ReadByte();
                result.SignatureHashAlgorithm = new SignatureHashAlgorithm()
                {
                    Hash = hash, Signature = signature
                };
            }

            var length = NetworkByteOrderConverter.ToUInt16(stream);

            if (length > 0)
            {
                result.Signature = new byte[length];
                stream.Read(result.Signature, 0, length);
            }

            return(result);
        }
        public static ECDHEServerKeyExchange Deserialise(System.IO.Stream stream, Version version)
        {
            ECDHEServerKeyExchange result = new ECDHEServerKeyExchange();

            result._EllipticCurveType = (TEllipticCurveType)stream.ReadByte();
            result._EllipticCurve     = (TEllipticCurve)NetworkByteOrderConverter.ToUInt16(stream);
            int length = stream.ReadByte();

            if (length > 0)
            {
                result._PublicKeyBytes = new byte[length];
                stream.Read(result._PublicKeyBytes, 0, length);
            }
            if (version >= DTLSRecord.Version1_2)
            {
                result._HashAlgorithm      = (THashAlgorithm)stream.ReadByte();
                result._SignatureAlgorithm = (TSignatureAlgorithm)stream.ReadByte();
            }
            int signatureLength = NetworkByteOrderConverter.ToUInt16(stream);

            if (signatureLength > 0)
            {
                result._Signature = new byte[signatureLength];
                stream.Read(result._Signature, 0, signatureLength);
            }
            return(result);
        }
Exemple #4
0
        public static Extensions Deserialise(Stream stream, bool client)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            Extensions result = null;

            if (stream.Position < stream.Length)
            {
                result = new Extensions();
                var length = NetworkByteOrderConverter.ToUInt16(stream);
                if (length > 0)
                {
                    var extension = Extension.Deserialise(stream, client);
                    while (extension != null)
                    {
                        result.Add(extension);
                        extension = Extension.Deserialise(stream, client);
                    }
                }
            }
            return(result);
        }
        public static CertificateRequest Deserialise(Stream stream, Version version)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (version == null)
            {
                throw new ArgumentNullException(nameof(version));
            }

            var result = new CertificateRequest();
            var certificateTypeCount = stream.ReadByte();

            if (certificateTypeCount > 0)
            {
                for (var index = 0; index < certificateTypeCount; index++)
                {
                    result.CertificateTypes.Add((TClientCertificateType)stream.ReadByte());
                }
            }

            if (version >= DTLSRecord.Version1_2)
            {
                var length = NetworkByteOrderConverter.ToUInt16(stream);
                var supportedAlgorithmsLength = (ushort)(length / 2);
                if (supportedAlgorithmsLength > 0)
                {
                    for (uint index = 0; index < supportedAlgorithmsLength; index++)
                    {
                        var hash      = (THashAlgorithm)stream.ReadByte();
                        var signature = (TSignatureAlgorithm)stream.ReadByte();
                        result.SupportedAlgorithms.Add(new SignatureHashAlgorithm()
                        {
                            Hash = hash, Signature = signature
                        });
                    }
                }
            }

            var certificateAuthoritiesLength = NetworkByteOrderConverter.ToUInt16(stream);

            if (certificateAuthoritiesLength > 0)
            {
                var read = 0;
                while (certificateAuthoritiesLength > read)
                {
                    var distinguishedNameLength = NetworkByteOrderConverter.ToUInt16(stream);
                    read += (2 + distinguishedNameLength);
                    var distinguishedName = new byte[distinguishedNameLength];
                    stream.Read(distinguishedName, 0, distinguishedNameLength);
                    result.CertificateAuthorities.Add(distinguishedName);
                }
            }
            return(result);
        }
Exemple #6
0
        public static HandshakeRecord Deserialise(System.IO.Stream stream)
        {
            HandshakeRecord result = new HandshakeRecord();

            result._MessageType    = (THandshakeType)stream.ReadByte();
            result._Length         = NetworkByteOrderConverter.ToUInt24(stream);
            result._MessageSeq     = NetworkByteOrderConverter.ToUInt16(stream);
            result._FragmentOffset = NetworkByteOrderConverter.ToUInt24(stream);
            result._FragmentLength = NetworkByteOrderConverter.ToUInt24(stream);
            return(result);
        }
        public static ClientCertificateTypeExtension Deserialise(Stream stream, bool client)
        {
            ClientCertificateTypeExtension result = new ClientCertificateTypeExtension();
            ushort length = NetworkByteOrderConverter.ToUInt16(stream);

            if (length > 0)
            {
                result._CertificateTypes = new byte[length];
                stream.Read(result._CertificateTypes, 0, length);
            }
            return(result);
        }
        public static HandshakeRecord Deserialise(Stream stream)
        {
            var result = new HandshakeRecord
            {
                MessageType    = (THandshakeType)stream.ReadByte(),
                Length         = NetworkByteOrderConverter.ToUInt24(stream),
                MessageSeq     = NetworkByteOrderConverter.ToUInt16(stream),
                FragmentOffset = NetworkByteOrderConverter.ToUInt24(stream),
                FragmentLength = NetworkByteOrderConverter.ToUInt24(stream)
            };

            return(result);
        }
Exemple #9
0
        public static PSKServerKeyExchange Deserialise(System.IO.Stream stream, Version version)
        {
            PSKServerKeyExchange result = new PSKServerKeyExchange();

            int pdkIdentityHintLenth = NetworkByteOrderConverter.ToUInt16(stream);

            if (pdkIdentityHintLenth > 0)
            {
                result._PSKIdentityHint = new byte[pdkIdentityHintLenth];
                stream.Read(result._PSKIdentityHint, 0, pdkIdentityHintLenth);
            }
            return(result);
        }
        public static PSKClientKeyExchange Deserialise(System.IO.Stream stream)
        {
            PSKClientKeyExchange result = null;
            ushort pskIdentityLength    = NetworkByteOrderConverter.ToUInt16(stream);

            if (pskIdentityLength > 0)
            {
                result = new PSKClientKeyExchange();
                result._PSKIdentity = new byte[pskIdentityLength];
                stream.Read(result._PSKIdentity, 0, pskIdentityLength);
            }
            return(result);
        }
Exemple #11
0
        public static ClientHello Deserialise(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var result = new ClientHello
            {
                ClientVersion = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte()),
                Random        = RandomData.Deserialise(stream)
            };

            var length = stream.ReadByte();

            if (length > 0)
            {
                result.SessionID = new byte[length];
                stream.Read(result.SessionID, 0, length);
            }

            length = stream.ReadByte();
            if (length > 0)
            {
                result.Cookie = new byte[length];
                stream.Read(result.Cookie, 0, length);
            }

            var cipherSuitesLength = (ushort)(NetworkByteOrderConverter.ToUInt16(stream) / 2);

            if (cipherSuitesLength > 0)
            {
                result.CipherSuites = new ushort[cipherSuitesLength];
                for (uint index = 0; index < cipherSuitesLength; index++)
                {
                    result.CipherSuites[index] = NetworkByteOrderConverter.ToUInt16(stream);
                }
            }

            length = stream.ReadByte();
            if (length > 0)
            {
                result.CompressionMethods = new byte[length];
                stream.Read(result.CompressionMethods, 0, length);
            }

            result.Extensions = Extensions.Deserialise(stream, true);
            return(result);
        }
        public static EllipticCurvesExtension Deserialise(Stream stream)
        {
            EllipticCurvesExtension result = new EllipticCurvesExtension();
            ushort length = NetworkByteOrderConverter.ToUInt16(stream);
            ushort supportedCurvesLength = (ushort)(length / 2);

            if (supportedCurvesLength > 0)
            {
                for (uint index = 0; index < supportedCurvesLength; index++)
                {
                    result.SupportedCurves.Add((TEllipticCurve)NetworkByteOrderConverter.ToUInt16(stream));
                }
            }
            return(result);
        }
        public static NewSessionTicket Deserialise(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var result = new NewSessionTicket
            {
                LifetimeHint = (uint)stream.ReadByte(),
                Length       = NetworkByteOrderConverter.ToUInt16(stream)
            };

            stream.Read(result.Ticket, 0, result.Length);
            return(result);
        }
        public static CertificateRequest Deserialise(Stream stream, Version version)
        {
            CertificateRequest result = new CertificateRequest();
            int certificateTypeCount  = stream.ReadByte();

            if (certificateTypeCount > 0)
            {
                for (int index = 0; index < certificateTypeCount; index++)
                {
                    result.CertificateTypes.Add((TClientCertificateType)stream.ReadByte());
                }
            }
            if (version >= DTLSRecord.Version1_2)
            {
                ushort length = NetworkByteOrderConverter.ToUInt16(stream);
                ushort supportedAlgorithmsLength = (ushort)(length / 2);
                if (supportedAlgorithmsLength > 0)
                {
                    for (uint index = 0; index < supportedAlgorithmsLength; index++)
                    {
                        THashAlgorithm      hash      = (THashAlgorithm)stream.ReadByte();
                        TSignatureAlgorithm signature = (TSignatureAlgorithm)stream.ReadByte();
                        result.SupportedAlgorithms.Add(new SignatureHashAlgorithm()
                        {
                            Hash = hash, Signature = signature
                        });
                    }
                }
            }
            ushort certificateAuthoritiesLength = NetworkByteOrderConverter.ToUInt16(stream);

            if (certificateAuthoritiesLength > 0)
            {
                int read = 0;
                while (certificateAuthoritiesLength > read)
                {
                    ushort distinguishedNameLength = NetworkByteOrderConverter.ToUInt16(stream);
                    read += (2 + distinguishedNameLength);
                    byte[] distinguishedName = new byte[distinguishedNameLength];
                    stream.Read(distinguishedName, 0, distinguishedNameLength);
                    result.CertificateAuthorities.Add(distinguishedName);
                }
            }
            return(result);
        }
Exemple #15
0
        public static PSKServerKeyExchange Deserialise(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var result = new PSKServerKeyExchange();

            int pdkIdentityHintLenth = NetworkByteOrderConverter.ToUInt16(stream);

            if (pdkIdentityHintLenth > 0)
            {
                result.PSKIdentityHint = new byte[pdkIdentityHintLenth];
                stream.Read(result.PSKIdentityHint, 0, pdkIdentityHintLenth);
            }
            return(result);
        }
        public static ClientCertificateTypeExtension Deserialise(Stream stream, bool client)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var result = new ClientCertificateTypeExtension();
            var length = NetworkByteOrderConverter.ToUInt16(stream);

            if (length > 0)
            {
                result.CertificateTypes = new byte[length];
                stream.Read(result.CertificateTypes, 0, length);
            }

            return(result);
        }
        public static ServerHello Deserialise(Stream stream)
        {
            ServerHello result = new ServerHello();

            result._ServerVersion = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte());
            result._Random        = RandomData.Deserialise(stream);
            int length = stream.ReadByte();

            if (length > 0)
            {
                result._SessionID = new byte[length];
                stream.Read(result._SessionID, 0, length);
            }
            result._CipherSuite       = NetworkByteOrderConverter.ToUInt16(stream);
            result._CompressionMethod = (byte)stream.ReadByte();
            result._Extensions        = Extensions.Deserialise(stream, false);
            return(result);
        }
Exemple #18
0
        public static Extension Deserialise(Stream stream, bool client)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            Extension result = null;

            if (stream.Position < stream.Length)
            {
                result = new Extension
                {
                    ExtensionType = (TExtensionType)NetworkByteOrderConverter.ToUInt16(stream)
                };

                var length = NetworkByteOrderConverter.ToUInt16(stream);
                if (length > 0)
                {
                    if (result.ExtensionType == TExtensionType.EllipticCurves)
                    {
                        result.SpecificExtension = EllipticCurvesExtension.Deserialise(stream);
                    }
                    else if (result.ExtensionType == TExtensionType.ClientCertificateType)
                    {
                        result.SpecificExtension = ClientCertificateTypeExtension.Deserialise(stream, client);
                    }
                    else if (result.ExtensionType == TExtensionType.ServerCertificateType)
                    {
                        result.SpecificExtension = ServerCertificateTypeExtension.Deserialise(stream, client);
                    }
                    else if (result.ExtensionType == TExtensionType.SignatureAlgorithms)
                    {
                        result.SpecificExtension = SignatureAlgorithmsExtension.Deserialise(stream);
                    }
                    else
                    {
                        result.Data = new byte[length];
                        stream.Read(result.Data, 0, length);
                    }
                }
            }
            return(result);
        }
Exemple #19
0
        public static CertificateVerify Deserialise(Stream stream, Version version)
        {
            CertificateVerify   result    = new CertificateVerify();
            THashAlgorithm      hash      = (THashAlgorithm)stream.ReadByte();
            TSignatureAlgorithm signature = (TSignatureAlgorithm)stream.ReadByte();

            result.SignatureHashAlgorithm = new SignatureHashAlgorithm()
            {
                Hash = hash, Signature = signature
            };
            ushort length = NetworkByteOrderConverter.ToUInt16(stream);

            if (length > 0)
            {
                result.Signature = new byte[length];
                stream.Read(result.Signature, 0, length);
            }
            return(result);
        }
        public static SignatureAlgorithmsExtension Deserialise(Stream stream)
        {
            SignatureAlgorithmsExtension result = new SignatureAlgorithmsExtension();
            ushort length = NetworkByteOrderConverter.ToUInt16(stream);
            ushort supportedAlgorithmsLength = (ushort)(length / 2);

            if (supportedAlgorithmsLength > 0)
            {
                for (uint index = 0; index < supportedAlgorithmsLength; index++)
                {
                    THashAlgorithm      hash      = (THashAlgorithm)stream.ReadByte();
                    TSignatureAlgorithm signature = (TSignatureAlgorithm)stream.ReadByte();
                    result.SupportedAlgorithms.Add(new SignatureHashAlgorithm()
                    {
                        Hash = hash, Signature = signature
                    });
                }
            }
            return(result);
        }
        public static RSAClientKeyExchange Deserialise(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            RSAClientKeyExchange result = null;
            var rsaPremasterLength      = NetworkByteOrderConverter.ToUInt16(stream);

            if (rsaPremasterLength > 0)
            {
                result = new RSAClientKeyExchange
                {
                    PremasterSecret = new byte[rsaPremasterLength]
                };
                stream.Read(result.PremasterSecret, 0, rsaPremasterLength);
            }
            return(result);
        }
        public static PSKClientKeyExchange Deserialise(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            PSKClientKeyExchange result = null;
            var pskIdentityLength       = NetworkByteOrderConverter.ToUInt16(stream);

            if (pskIdentityLength > 0)
            {
                result = new PSKClientKeyExchange
                {
                    PSKIdentity = new byte[pskIdentityLength]
                };
                stream.Read(result.PSKIdentity, 0, pskIdentityLength);
            }
            return(result);
        }
Exemple #23
0
        public static DTLSRecord Deserialise(Stream stream)
        {
            DTLSRecord result = new DTLSRecord();

            result._RecordType     = (TRecordType)stream.ReadByte();
            result._Version        = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte());
            result._Epoch          = NetworkByteOrderConverter.ToUInt16(stream);
            result._SequenceNumber = NetworkByteOrderConverter.ToInt48(stream);
            result._Length         = NetworkByteOrderConverter.ToUInt16(stream);
            if (result._Length > 0)
            {
                result._Fragment = new byte[result._Length];
                int length = stream.Read(result._Fragment, 0, result._Length);
                while (length < result._Length)
                {
                    length += stream.Read(result._Fragment, length, result._Length - length);
                }
            }
            return(result);
        }
        public static Extensions Deserialise(Stream stream, bool client)
        {
            Extensions result = null;

            if (stream.Position < stream.Length)
            {
                result = new Extensions();
                ushort length = NetworkByteOrderConverter.ToUInt16(stream);
                if (length > 0)
                {
                    Extension extension = Extension.Deserialise(stream, client);
                    while (extension != null)
                    {
                        result.Add(extension);
                        extension = Extension.Deserialise(stream, client);
                    }
                }
            }
            return(result);
        }
        public static ECDHEPSKClientKeyExchange Deserialise(System.IO.Stream stream)
        {
            ECDHEPSKClientKeyExchange result = null;
            ushort pskIdentityLength         = NetworkByteOrderConverter.ToUInt16(stream);

            if (pskIdentityLength > 0)
            {
                result = new ECDHEPSKClientKeyExchange
                {
                    _PSKIdentity = new byte[pskIdentityLength]
                };
                stream.Read(result._PSKIdentity, 0, pskIdentityLength);
                int length = stream.ReadByte();
                if (length > 0)
                {
                    result._PublicKeyBytes = new byte[length];
                    stream.Read(result._PublicKeyBytes, 0, length);
                }
            }
            return(result);
        }
Exemple #26
0
        public static ECDHEPSKServerKeyExchange Deserialise(System.IO.Stream stream, Version version)
        {
            ECDHEPSKServerKeyExchange result = new ECDHEPSKServerKeyExchange();

            int pdkIdentityHintLenth = NetworkByteOrderConverter.ToUInt16(stream);

            if (pdkIdentityHintLenth > 0)
            {
                result._PSKIdentityHint = new byte[pdkIdentityHintLenth];
                stream.Read(result._PSKIdentityHint, 0, pdkIdentityHintLenth);
            }
            result._EllipticCurveType = (TEllipticCurveType)stream.ReadByte();
            result._EllipticCurve     = (TEllipticCurve)NetworkByteOrderConverter.ToUInt16(stream);
            int length = stream.ReadByte();

            if (length > 0)
            {
                result._PublicKeyBytes = new byte[length];
                stream.Read(result._PublicKeyBytes, 0, length);
            }
            return(result);
        }
Exemple #27
0
        public static ClientHello Deserialise(Stream stream)
        {
            ClientHello result = new ClientHello
            {
                _ClientVersion = new Version(255 - stream.ReadByte(), 255 - stream.ReadByte()),
                _Random        = RandomData.Deserialise(stream)
            };
            int length = stream.ReadByte();

            if (length > 0)
            {
                result._SessionID = new byte[length];
                stream.Read(result._SessionID, 0, length);
            }
            length = stream.ReadByte();
            if (length > 0)
            {
                result._Cookie = new byte[length];
                stream.Read(result._Cookie, 0, length);
            }
            ushort cipherSuitesLength = (ushort)(NetworkByteOrderConverter.ToUInt16(stream) / 2);

            if (cipherSuitesLength > 0)
            {
                result._CipherSuites = new ushort[cipherSuitesLength];
                for (uint index = 0; index < cipherSuitesLength; index++)
                {
                    result._CipherSuites[index] = NetworkByteOrderConverter.ToUInt16(stream);
                }
            }
            length = stream.ReadByte();
            if (length > 0)
            {
                result._CompressionMethods = new byte[length];
                stream.Read(result._CompressionMethods, 0, length);
            }
            result._Extensions = Extensions.Deserialise(stream, true);
            return(result);
        }
Exemple #28
0
        public static Extension Deserialise(Stream stream, bool client)
        {
            Extension result = null;

            if (stream.Position < stream.Length)
            {
                result = new Extension();
                result._ExtensionType = (TExtensionType)NetworkByteOrderConverter.ToUInt16(stream);
                ushort length = NetworkByteOrderConverter.ToUInt16(stream);
                if (length > 0)
                {
                    if (result._ExtensionType == TExtensionType.EllipticCurves)
                    {
                        result._SpecifcExtension = EllipticCurvesExtension.Deserialise(stream);
                    }
                    else if (result._ExtensionType == TExtensionType.ClientCertificateType)
                    {
                        result._SpecifcExtension = ClientCertificateTypeExtension.Deserialise(stream, client);
                    }
                    else if (result._ExtensionType == TExtensionType.ServerCertificateType)
                    {
                        result._SpecifcExtension = ServerCertificateTypeExtension.Deserialise(stream, client);
                    }
                    else if (result._ExtensionType == TExtensionType.SignatureAlgorithms)
                    {
                        result._SpecifcExtension = SignatureAlgorithmsExtension.Deserialise(stream);
                    }
                    else
                    {
                        result._Data = new byte[length];
                        stream.Read(result._Data, 0, length);
                    }
                }
            }
            return(result);
        }