예제 #1
0
        /**
         * Parse a {@link HeartbeatMessage} from a {@link Stream}.
         *
         * @param input
         *            the {@link Stream} to parse from.
         * @return a {@link HeartbeatMessage} object.
         * @throws IOException
         */
        public static HeartbeatMessage Parse(Stream input)
        {
            byte type = TlsUtilities.ReadUint8(input);

            if (!HeartbeatMessageType.IsValid(type))
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }

            int payload_length = TlsUtilities.ReadUint16(input);

            PayloadBuffer buf = new PayloadBuffer();

            Streams.PipeAll(input, buf);

            byte[] payload = buf.ToTruncatedByteArray(payload_length);
            if (payload == null)
            {
                /*
                 * RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the
                 * received HeartbeatMessage MUST be discarded silently.
                 */
                return(null);
            }

            TlsUtilities.CheckUint16(buf.Length);
            int padding_length = (int)buf.Length - payload.Length;

            /*
             * RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
             */
            return(new HeartbeatMessage(type, payload, padding_length));
        }
예제 #2
0
    public virtual void Encode(Stream output)
    {
        if (mCertificateTypes == null || mCertificateTypes.Length == 0)
        {
            TlsUtilities.WriteUint8(0, output);
        }
        else
        {
            TlsUtilities.WriteUint8ArrayWithUint8Length(mCertificateTypes, output);
        }
        if (mSupportedSignatureAlgorithms != null)
        {
            TlsUtilities.EncodeSupportedSignatureAlgorithms(mSupportedSignatureAlgorithms, allowAnonymous: false, output);
        }
        if (mCertificateAuthorities == null || mCertificateAuthorities.Count < 1)
        {
            TlsUtilities.WriteUint16(0, output);
            return;
        }
        IList list = Platform.CreateArrayList(mCertificateAuthorities.Count);
        int   num  = 0;

        foreach (Asn1Encodable mCertificateAuthority in mCertificateAuthorities)
        {
            byte[] encoded = mCertificateAuthority.GetEncoded("DER");
            list.Add(encoded);
            num += encoded.Length + 2;
        }
        TlsUtilities.CheckUint16(num);
        TlsUtilities.WriteUint16(num, output);
        foreach (byte[] item in list)
        {
            TlsUtilities.WriteOpaque16(item, output);
        }
    }
 public virtual void Encode(Stream output)
 {
     if (mResponderIDList == null || mResponderIDList.Count < 1)
     {
         TlsUtilities.WriteUint16(0, output);
     }
     else
     {
         MemoryStream memoryStream = new MemoryStream();
         for (int i = 0; i < mResponderIDList.Count; i++)
         {
             ResponderID responderID = (ResponderID)mResponderIDList[i];
             byte[]      encoded     = responderID.GetEncoded("DER");
             TlsUtilities.WriteOpaque16(encoded, memoryStream);
         }
         TlsUtilities.CheckUint16(memoryStream.Length);
         TlsUtilities.WriteUint16((int)memoryStream.Length, output);
         memoryStream.WriteTo(output);
     }
     if (mRequestExtensions == null)
     {
         TlsUtilities.WriteUint16(0, output);
         return;
     }
     byte[] encoded2 = mRequestExtensions.GetEncoded("DER");
     TlsUtilities.CheckUint16(encoded2.Length);
     TlsUtilities.WriteUint16(encoded2.Length, output);
     output.Write(encoded2, 0, encoded2.Length);
 }
예제 #4
0
        /**
         * Encode this {@link OcspStatusRequest} to a {@link Stream}.
         *
         * @param output
         *            the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            if (mResponderIDList == null || mResponderIDList.Count < 1)
            {
                TlsUtilities.WriteUint16(0, output);
            }
            else
            {
                MemoryStream buf = new MemoryStream();
                for (int i = 0; i < mResponderIDList.Count; ++i)
                {
                    ResponderID responderID = (ResponderID)mResponderIDList[i];
                    byte[]      derEncoding = responderID.GetEncoded(Asn1Encodable.Der);
                    TlsUtilities.WriteOpaque16(derEncoding, buf);
                }
                TlsUtilities.CheckUint16(buf.Length);
                TlsUtilities.WriteUint16((int)buf.Length, output);
                buf.WriteTo(output);
            }

            if (mRequestExtensions == null)
            {
                TlsUtilities.WriteUint16(0, output);
            }
            else
            {
                byte[] derEncoding = mRequestExtensions.GetEncoded(Asn1Encodable.Der);
                TlsUtilities.CheckUint16(derEncoding.Length);
                TlsUtilities.WriteUint16(derEncoding.Length, output);
                output.Write(derEncoding, 0, derEncoding.Length);
            }
        }
 public virtual void Encode(TlsContext context, Stream output)
 {
     TlsUtilities.WriteUint8(mType, output);
     TlsUtilities.CheckUint16(mPayload.Length);
     TlsUtilities.WriteUint16(mPayload.Length, output);
     output.Write(mPayload, 0, mPayload.Length);
     byte[] array = new byte[mPaddingLength];
     context.NonceRandomGenerator.NextBytes(array);
     output.Write(array, 0, array.Length);
 }
 public static void WriteNamedECParameters(int namedCurve, Stream output)
 {
     if (!NamedCurve.RefersToASpecificNamedCurve(namedCurve))
     {
         throw new TlsFatalAlert(80);
     }
     TlsUtilities.WriteUint8(3, output);
     TlsUtilities.CheckUint16(namedCurve);
     TlsUtilities.WriteUint16(namedCurve, output);
 }
예제 #7
0
        internal void EncodeTo(Stream output)
        {
            long num = Length - 2;

            TlsUtilities.CheckUint16(num);
            Position = 0L;
            TlsUtilities.WriteUint16((int)num, this);
            WriteTo(output);
            Platform.Dispose(this);
        }
예제 #8
0
            internal void EncodeTo(Stream output)
            {
                // Patch actual length back in
                long length = Length - 2;

                TlsUtilities.CheckUint16(length);
                this.Position = 0;
                TlsUtilities.WriteUint16((int)length, this);
                Streams.WriteBufTo(this, output);
                Platform.Dispose(this);
            }
예제 #9
0
            internal void EncodeTo(Stream output)
            {
                // Patch actual length back in
                long length = Length - 2;

                TlsUtilities.CheckUint16(length);
                this.Position = 0;
                TlsUtilities.WriteUint16((int)length, this);
                Streams.WriteBufTo(this, output);
                BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(this);
            }
예제 #10
0
        /**
         * Encode this {@link ServerNameList} to a {@link Stream}.
         *
         * @param output
         *            the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            MemoryStream buf = new MemoryStream();

            foreach (ServerName entry in ServerNames)
            {
                entry.Encode(buf);
            }

            TlsUtilities.CheckUint16(buf.Length);
            TlsUtilities.WriteUint16((int)buf.Length, output);
            buf.WriteTo(output);
        }
예제 #11
0
        public static void WriteExplicitECParameters(byte[] ecPointFormats, ECDomainParameters ecParameters,
                                                     Stream output)
        {
            ECCurve curve = ecParameters.Curve;

            if (ECAlgorithms.IsFpCurve(curve))
            {
                TlsUtilities.WriteUint8(ECCurveType.explicit_prime, output);

                WriteECParameter(curve.Field.Characteristic, output);
            }
            else if (ECAlgorithms.IsF2mCurve(curve))
            {
                IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
                int[] exponents = field.MinimalPolynomial.GetExponentsPresent();

                TlsUtilities.WriteUint8(ECCurveType.explicit_char2, output);

                int m = exponents[exponents.Length - 1];
                TlsUtilities.CheckUint16(m);
                TlsUtilities.WriteUint16(m, output);

                if (exponents.Length == 3)
                {
                    TlsUtilities.WriteUint8(ECBasisType.ec_basis_trinomial, output);
                    WriteECExponent(exponents[1], output);
                }
                else if (exponents.Length == 5)
                {
                    TlsUtilities.WriteUint8(ECBasisType.ec_basis_pentanomial, output);
                    WriteECExponent(exponents[1], output);
                    WriteECExponent(exponents[2], output);
                    WriteECExponent(exponents[3], output);
                }
                else
                {
                    throw new ArgumentException("Only trinomial and pentomial curves are supported");
                }
            }
            else
            {
                throw new ArgumentException("'ecParameters' not a known curve type");
            }

            WriteECFieldElement(curve.A, output);
            WriteECFieldElement(curve.B, output);
            TlsUtilities.WriteOpaque8(SerializeECPoint(ecPointFormats, ecParameters.G), output);
            WriteECParameter(ecParameters.N, output);
            WriteECParameter(ecParameters.H, output);
        }
예제 #12
0
    protected internal static void WriteSupplementalData(Stream output, IList supplementalData)
    {
        MemoryStream memoryStream = new MemoryStream();

        foreach (SupplementalDataEntry supplementalDatum in supplementalData)
        {
            int dataType = supplementalDatum.DataType;
            TlsUtilities.CheckUint16(dataType);
            TlsUtilities.WriteUint16(dataType, memoryStream);
            TlsUtilities.WriteOpaque16(supplementalDatum.Data, memoryStream);
        }
        byte[] buf = memoryStream.ToArray();
        TlsUtilities.WriteOpaque24(buf, output);
    }
예제 #13
0
    protected internal static void WriteExtensions(Stream output, IDictionary extensions)
    {
        MemoryStream memoryStream = new MemoryStream();

        foreach (object key in extensions.Keys)
        {
            int    num = (int)key;
            byte[] buf = (byte[])extensions[num];
            TlsUtilities.CheckUint16(num);
            TlsUtilities.WriteUint16(num, memoryStream);
            TlsUtilities.WriteOpaque16(buf, memoryStream);
        }
        byte[] buf2 = memoryStream.ToArray();
        TlsUtilities.WriteOpaque16(buf2, output);
    }
예제 #14
0
        public static void WriteNamedECParameters(int namedCurve, Stream output)
        {
            if (!NamedCurve.RefersToASpecificNamedCurve(namedCurve))
            {
                /*
                 * RFC 4492 5.4. All those values of NamedCurve are allowed that refer to a specific
                 * curve. Values of NamedCurve that indicate support for a class of explicitly defined
                 * curves are not allowed here [...].
                 */
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }

            TlsUtilities.WriteUint8(ECCurveType.named_curve, output);
            TlsUtilities.CheckUint16(namedCurve);
            TlsUtilities.WriteUint16(namedCurve, output);
        }
    public static void WriteExplicitECParameters(byte[] ecPointFormats, ECDomainParameters ecParameters, Stream output)
    {
        ECCurve curve = ecParameters.Curve;

        if (ECAlgorithms.IsFpCurve(curve))
        {
            TlsUtilities.WriteUint8(1, output);
            WriteECParameter(curve.Field.Characteristic, output);
        }
        else
        {
            if (!ECAlgorithms.IsF2mCurve(curve))
            {
                throw new ArgumentException("'ecParameters' not a known curve type");
            }
            IPolynomialExtensionField polynomialExtensionField = (IPolynomialExtensionField)curve.Field;
            int[] exponentsPresent = polynomialExtensionField.MinimalPolynomial.GetExponentsPresent();
            TlsUtilities.WriteUint8(2, output);
            int i = exponentsPresent[exponentsPresent.Length - 1];
            TlsUtilities.CheckUint16(i);
            TlsUtilities.WriteUint16(i, output);
            if (exponentsPresent.Length == 3)
            {
                TlsUtilities.WriteUint8(1, output);
                WriteECExponent(exponentsPresent[1], output);
            }
            else
            {
                if (exponentsPresent.Length != 5)
                {
                    throw new ArgumentException("Only trinomial and pentomial curves are supported");
                }
                TlsUtilities.WriteUint8(2, output);
                WriteECExponent(exponentsPresent[1], output);
                WriteECExponent(exponentsPresent[2], output);
                WriteECExponent(exponentsPresent[3], output);
            }
        }
        WriteECFieldElement(curve.A, output);
        WriteECFieldElement(curve.B, output);
        TlsUtilities.WriteOpaque8(SerializeECPoint(ecPointFormats, ecParameters.G), output);
        WriteECParameter(ecParameters.N, output);
        WriteECParameter(ecParameters.H, output);
    }
예제 #16
0
        /**
         * Encode this {@link CertificateRequest} to a {@link Stream}.
         *
         * @param output the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            if (mCertificateTypes == null || mCertificateTypes.Length == 0)
            {
                TlsUtilities.WriteUint8(0, output);
            }
            else
            {
                TlsUtilities.WriteUint8ArrayWithUint8Length(mCertificateTypes, output);
            }

            if (mSupportedSignatureAlgorithms != null)
            {
                // TODO Check whether SignatureAlgorithm.anonymous is allowed here
                TlsUtilities.EncodeSupportedSignatureAlgorithms(mSupportedSignatureAlgorithms, false, output);
            }

            if (mCertificateAuthorities == null || mCertificateAuthorities.Count < 1)
            {
                TlsUtilities.WriteUint16(0, output);
            }
            else
            {
                IList derEncodings = Platform.CreateArrayList(mCertificateAuthorities.Count);

                int totalLength = 0;
                foreach (Asn1Encodable certificateAuthority in mCertificateAuthorities)
                {
                    byte[] derEncoding = certificateAuthority.GetEncoded(Asn1Encodable.Der);
                    derEncodings.Add(derEncoding);
                    totalLength += derEncoding.Length + 2;
                }

                TlsUtilities.CheckUint16(totalLength);
                TlsUtilities.WriteUint16(totalLength, output);

                foreach (byte[] derEncoding in derEncodings)
                {
                    TlsUtilities.WriteOpaque16(derEncoding, output);
                }
            }
        }
예제 #17
0
        /**
         * Encode this {@link ServerNameList} to a {@link Stream}.
         *
         * @param output
         *            the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            MemoryStream buf = new MemoryStream();

            byte[] nameTypesSeen = TlsUtilities.EmptyBytes;
            foreach (ServerName entry in ServerNames)
            {
                nameTypesSeen = CheckNameType(nameTypesSeen, entry.NameType);
                if (nameTypesSeen == null)
                {
                    throw new TlsFatalAlert(AlertDescription.internal_error);
                }

                entry.Encode(buf);
            }

            TlsUtilities.CheckUint16(buf.Length);
            TlsUtilities.WriteUint16((int)buf.Length, output);
            Streams.WriteBufTo(buf, output);
        }
    public static HeartbeatMessage Parse(Stream input)
    {
        byte b = TlsUtilities.ReadUint8(input);

        if (!HeartbeatMessageType.IsValid(b))
        {
            throw new TlsFatalAlert(47);
        }
        int           payloadLength = TlsUtilities.ReadUint16(input);
        PayloadBuffer payloadBuffer = new PayloadBuffer();

        Streams.PipeAll(input, payloadBuffer);
        byte[] array = payloadBuffer.ToTruncatedByteArray(payloadLength);
        if (array == null)
        {
            return(null);
        }
        TlsUtilities.CheckUint16(payloadBuffer.Length);
        int paddingLength = (int)payloadBuffer.Length - array.Length;

        return(new HeartbeatMessage(b, array, paddingLength));
    }
예제 #19
0
        public static byte[] CreateALPNExtension(System.Collections.Generic.List <string> protocols)
        {
            if (protocols == null)
            {
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }

            // https://tools.ietf.org/html/rfc7301
            using (var buf = new BestHTTP.Extensions.BufferPoolMemoryStream())
            {
                using (var protoBuf = new BestHTTP.Extensions.BufferPoolMemoryStream())
                {
                    foreach (string protocol in protocols)
                    {
                        // Empty strings MUST NOT be included and byte strings MUST NOT be truncated.
                        if (string.IsNullOrEmpty(protocol))
                        {
                            continue;
                        }

                        byte[] asciiEncoding = Strings.ToAsciiByteArray(protocol);
                        if (asciiEncoding.Length < 1)
                        {
                            throw new TlsFatalAlert(AlertDescription.internal_error);
                        }
                        TlsUtilities.WriteOpaque8(asciiEncoding, protoBuf);
                        BestHTTP.PlatformSupport.Memory.BufferPool.Release(asciiEncoding);
                    }

                    TlsUtilities.CheckUint16(protoBuf.Length);
                    TlsUtilities.WriteUint16((int)protoBuf.Length, buf);
                    protoBuf.WriteTo(buf);
                }

                return(buf.ToArray());
            }
        }
예제 #20
0
 /// <exception cref="IOException"></exception>
 public static byte[] CreatePaddingExtension(int dataLength)
 {
     TlsUtilities.CheckUint16(dataLength);
     return(new byte[dataLength]);
 }