Пример #1
0
        public static bool TryParse(MemoryCursor cursor, out Certificate result)
        {
            result = new Certificate();

            if (!HandshakeType.TrySlice(cursor, HandshakeType.Certificate))
            {
                return(false);
            }

            var body = HandshakeLength.SliceBytes(cursor);

            using var bodyContext = body.SetCursor(cursor);

            CertificateContext.SkipBytes(cursor);

            var payloadBytes = ByteVector.SliceVectorBytes(cursor, 0..ByteVector.MaxUInt24);

            if (!cursor.IsEnd())
            {
                throw new EncodingException();
            }

            result = new Certificate(payloadBytes);

            return(true);
        }
Пример #2
0
        public static bool TryParse(MemoryCursor cursor, out CertificateVerify result)
        {
            result = new CertificateVerify();

            if (!HandshakeType.TrySlice(cursor, HandshakeType.CertificateVerify))
            {
                return(false);
            }

            var body = HandshakeLength.SliceBytes(cursor);

            using var bodyContext = body.SetCursor(cursor);

            var scheme    = SignatureScheme.Parse(cursor);
            var signature = ByteVector.SliceVectorBytes(cursor, 0..ushort.MaxValue);

            if (!cursor.IsEnd())
            {
                throw new EncodingException();
            }

            result = new CertificateVerify(scheme, signature);

            return(true);
        }
Пример #3
0
        public static CursorHandshakeWritingContext StartWriting(MemoryCursor cursor)
        {
            HandshakeType.Certificate.WriteBytes(cursor);

            var payloadContext = HandshakeLength.StartWriting(cursor);

            CertificateContext.WriteEmpty(cursor);

            var certificatesContext = ByteVector.StartVectorWriting(cursor, 0..ByteVector.MaxUInt24);

            return(new CursorHandshakeWritingContext(payloadContext, certificatesContext));
        }
Пример #4
0
        public static CursorHandshakeWritingContext StartWriting(MemoryCursor cursor, SignatureScheme scheme)
        {
            HandshakeType.CertificateVerify.WriteBytes(cursor);

            var payloadContext = HandshakeLength.StartWriting(cursor);

            scheme.WriteBytes(cursor);

            var signatureContext = ByteVector.StartVectorWriting(cursor, 0..ushort.MaxValue);

            return(new CursorHandshakeWritingContext(payloadContext, signatureContext));
        }
Пример #5
0
        public static CursorHandshakeWritingContext StartWriting(MemoryCursor cursor,
                                                                 HandshakeRandom random,
                                                                 Cipher cipher,
                                                                 SessionId sessionId)
        {
            HandshakeType.ServerHello.WriteBytes(cursor);

            var payloadContext = HandshakeLength.StartWriting(cursor);

            ProtocolVersion.Tls12.WriteBytes(cursor);
            random.WriteBytes(cursor);
            sessionId.WriteBytes(cursor);
            cipher.WriteBytes(cursor);
            CompressionMethod.WriteEmptyValue(cursor);

            var extensionsContext = ByteVector.StartVectorWriting(cursor, 0..ushort.MaxValue);

            return(new CursorHandshakeWritingContext(payloadContext, extensionsContext));
        }
Пример #6
0
        public static CursorHandshakeWritingContext StartWriting(MemoryCursor cursor,
                                                                 HandshakeRandom random,
                                                                 ReadOnlyMemory <Cipher> ciphers,
                                                                 SessionId sessionId)
        {
            HandshakeType.ClientHello.WriteBytes(cursor);

            var payloadContext = HandshakeLength.StartWriting(cursor);

            ProtocolVersion.Tls12.WriteBytes(cursor);
            random.WriteBytes(cursor);
            sessionId.WriteBytes(cursor);
            CipherSuite.Write(cursor, ciphers);
            CompressionMethod.WriteEmptyList(cursor);

            var extensionsContext = ByteVector.StartVectorWriting(cursor, 0..ushort.MaxValue);

            return(new CursorHandshakeWritingContext(payloadContext, extensionsContext));
        }
Пример #7
0
        public static bool TryParse(MemoryCursor cursor, out ServerHello result)
        {
            result = new ServerHello();

            if (!HandshakeType.TrySlice(cursor, HandshakeType.ServerHello))
            {
                return(false);
            }

            using (HandshakeLength.SliceBytes(cursor).SetCursor(cursor))
            {
                if (!ProtocolVersion.TrySlice(cursor, ProtocolVersion.Tls12))
                {
                    throw new EncodingException();
                }

                var random    = HandshakeRandom.Parse(cursor);
                var sessionId = SessionId.Parse(cursor);
                var cipher    = Cipher.Parse(cursor);

                if (!CompressionMethod.TrySliceEmptyValue(cursor))
                {
                    throw new EncodingException();
                }

                var payload = ByteVector.SliceVectorBytes(cursor, 0..ushort.MaxValue);

                if (!cursor.IsEnd())
                {
                    throw new EncodingException();
                }

                result = new ServerHello(random, cipher, sessionId, payload);

                return(true);
            }
        }