public override void GenerateClientKeyExchange(Stream output)
 {
     if (mPskIdentityHint == null)
     {
         mPskIdentity.SkipIdentityHint();
     }
     else
     {
         mPskIdentity.NotifyIdentityHint(mPskIdentityHint);
     }
     byte[] pskIdentity = mPskIdentity.GetPskIdentity();
     if (pskIdentity == null)
     {
         throw new TlsFatalAlert(80);
     }
     mPsk = mPskIdentity.GetPsk();
     if (mPsk == null)
     {
         throw new TlsFatalAlert(80);
     }
     TlsUtilities.WriteOpaque16(pskIdentity, output);
     mContext.SecurityParameters.pskIdentity = pskIdentity;
     if (mKeyExchange == 14)
     {
         mDHAgreePrivateKey = TlsDHUtilities.GenerateEphemeralClientKeyExchange(mContext.SecureRandom, mDHParameters, output);
     }
     else if (mKeyExchange == 24)
     {
         mECAgreePrivateKey = TlsEccUtilities.GenerateEphemeralClientKeyExchange(mContext.SecureRandom, mServerECPointFormats, mECAgreePublicKey.Parameters, output);
     }
     else if (mKeyExchange == 15)
     {
         mPremasterSecret = TlsRsaUtilities.GenerateEncryptedPreMasterSecret(mContext, mRsaServerPublicKey, output);
     }
 }
예제 #2
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 override byte[] GenerateServerKeyExchange()
    {
        mPskIdentityHint = mPskIdentityManager.GetHint();
        if (mPskIdentityHint == null && !RequiresServerKeyExchange)
        {
            return(null);
        }
        MemoryStream memoryStream = new MemoryStream();

        if (mPskIdentityHint == null)
        {
            TlsUtilities.WriteOpaque16(TlsUtilities.EmptyBytes, memoryStream);
        }
        else
        {
            TlsUtilities.WriteOpaque16(mPskIdentityHint, memoryStream);
        }
        if (mKeyExchange == 14)
        {
            if (mDHParameters == null)
            {
                throw new TlsFatalAlert(80);
            }
            mDHAgreePrivateKey = TlsDHUtilities.GenerateEphemeralServerKeyExchange(mContext.SecureRandom, mDHParameters, memoryStream);
        }
        else if (mKeyExchange == 24)
        {
            mECAgreePrivateKey = TlsEccUtilities.GenerateEphemeralServerKeyExchange(mContext.SecureRandom, mNamedCurves, mClientECPointFormats, memoryStream);
        }
        return(memoryStream.ToArray());
    }
 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);
 }
예제 #5
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);
        }
    }
예제 #6
0
 /**
  * Encode this {@link DigitallySigned} to a {@link Stream}.
  *
  * @param output
  *            the {@link Stream} to encode to.
  * @throws IOException
  */
 public virtual void Encode(Stream output)
 {
     if (mAlgorithm != null)
     {
         mAlgorithm.Encode(output);
     }
     TlsUtilities.WriteOpaque16(mSignature, output);
 }
    public override byte[] GeneratePremasterSecret()
    {
        byte[]       array        = GenerateOtherSecret(mPsk.Length);
        MemoryStream memoryStream = new MemoryStream(4 + array.Length + mPsk.Length);

        TlsUtilities.WriteOpaque16(array, memoryStream);
        TlsUtilities.WriteOpaque16(mPsk, memoryStream);
        Arrays.Fill(mPsk, 0);
        mPsk = null;
        return(memoryStream.ToArray());
    }
예제 #8
0
 public virtual void Encode(Stream output)
 {
     byte[] buf = Strings.ToByteArray(mUrl);
     TlsUtilities.WriteOpaque16(buf, output);
     if (mSha1Hash == null)
     {
         TlsUtilities.WriteUint8(0, output);
         return;
     }
     TlsUtilities.WriteUint8(1, output);
     output.Write(mSha1Hash, 0, mSha1Hash.Length);
 }
예제 #9
0
        public override byte[] GeneratePremasterSecret()
        {
            byte[] other_secret = GenerateOtherSecret(mPsk.Length);

            MemoryStream buf = new MemoryStream(4 + other_secret.Length + mPsk.Length);

            TlsUtilities.WriteOpaque16(other_secret, buf);
            TlsUtilities.WriteOpaque16(mPsk, buf);

            Arrays.Fill(mPsk, (byte)0);
            this.mPsk = null;

            return(buf.ToArray());
        }
예제 #10
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);
    }
예제 #11
0
        /**
         * Encode this {@link UrlAndHash} to a {@link Stream}.
         *
         * @param output the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            byte[] urlEncoding = Strings.ToByteArray(this.mUrl);
            TlsUtilities.WriteOpaque16(urlEncoding, output);

            if (this.mSha1Hash == null)
            {
                TlsUtilities.WriteUint8(0, output);
            }
            else
            {
                TlsUtilities.WriteUint8(1, output);
                output.Write(this.mSha1Hash, 0, this.mSha1Hash.Length);
            }
        }
예제 #12
0
 public virtual void Encode(Stream output)
 {
     TlsUtilities.WriteUint8(mNameType, output);
     if (mNameType == 0)
     {
         byte[] array = Strings.ToAsciiByteArray((string)mName);
         if (array.Length < 1)
         {
             throw new TlsFatalAlert(80);
         }
         TlsUtilities.WriteOpaque16(array, output);
         return;
     }
     throw new TlsFatalAlert(80);
 }
예제 #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
        /**
         * Encode this {@link ServerName} to a {@link Stream}.
         *
         * @param output
         *            the {@link Stream} to encode to.
         * @throws IOException
         */
        public virtual void Encode(Stream output)
        {
            TlsUtilities.WriteUint8(mNameType, output);

            switch (mNameType)
            {
            case Tls.NameType.host_name:
                byte[] asciiEncoding = Strings.ToAsciiByteArray((string)mName);
                if (asciiEncoding.Length < 1)
                {
                    throw new TlsFatalAlert(AlertDescription.internal_error);
                }
                TlsUtilities.WriteOpaque16(asciiEncoding, output);
                break;

            default:
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }
        }
예제 #15
0
        public override void GenerateClientKeyExchange(Stream output)
        {
            if (mPskIdentityHint == null)
            {
                mPskIdentity.SkipIdentityHint();
            }
            else
            {
                mPskIdentity.NotifyIdentityHint(mPskIdentityHint);
            }

            byte[] psk_identity = mPskIdentity.GetPskIdentity();
            if (psk_identity == null)
            {
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }

            this.mPsk = mPskIdentity.GetPsk();
            if (mPsk == null)
            {
                throw new TlsFatalAlert(AlertDescription.internal_error);
            }

            TlsUtilities.WriteOpaque16(psk_identity, output);

            mContext.SecurityParameters.pskIdentity = psk_identity;

            if (this.mKeyExchange == KeyExchangeAlgorithm.DHE_PSK)
            {
                this.mDHAgreePrivateKey = TlsDHUtilities.GenerateEphemeralClientKeyExchange(mContext.SecureRandom,
                                                                                            mDHParameters, output);
            }
            else if (this.mKeyExchange == KeyExchangeAlgorithm.ECDHE_PSK)
            {
                this.mECAgreePrivateKey = TlsEccUtilities.GenerateEphemeralClientKeyExchange(mContext.SecureRandom,
                                                                                             mServerECPointFormats, mECAgreePublicKey.Parameters, output);
            }
            else if (this.mKeyExchange == KeyExchangeAlgorithm.RSA_PSK)
            {
                this.mPremasterSecret = TlsRsaUtilities.GenerateEncryptedPreMasterSecret(mContext,
                                                                                         this.mRsaServerPublicKey, 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
        /// <exception cref="IOException"></exception>
        public static byte[] GenerateEncryptedPreMasterSecret(TlsContext context, RsaKeyParameters rsaServerPublicKey,
                                                              Stream output)
        {
            /*
             * Choose a PremasterSecret and send it encrypted to the server
             */
            byte[] premasterSecret = new byte[48];
            context.SecureRandom.NextBytes(premasterSecret);
            TlsUtilities.WriteVersion(context.ClientVersion, premasterSecret, 0);

            Pkcs1Encoding encoding = new Pkcs1Encoding(new RsaBlindedEngine());

            encoding.Init(true, new ParametersWithRandom(rsaServerPublicKey, context.SecureRandom));

            try
            {
                byte[] encryptedPreMasterSecret = encoding.ProcessBlock(premasterSecret, 0, premasterSecret.Length);

                if (TlsUtilities.IsSsl(context))
                {
                    // TODO Do any SSLv3 servers actually expect the length?
                    output.Write(encryptedPreMasterSecret, 0, encryptedPreMasterSecret.Length);
                }
                else
                {
                    TlsUtilities.WriteOpaque16(encryptedPreMasterSecret, output);
                }
            }
            catch (InvalidCipherTextException e)
            {
                /*
                 * This should never happen, only during decryption.
                 */
                throw new TlsFatalAlert(AlertDescription.internal_error, e);
            }

            return(premasterSecret);
        }
예제 #18
0
        public override byte[] GenerateServerKeyExchange()
        {
            this.mPskIdentityHint = mPskIdentityManager.GetHint();

            if (this.mPskIdentityHint == null && !RequiresServerKeyExchange)
            {
                return(null);
            }

            MemoryStream buf = new MemoryStream();

            if (this.mPskIdentityHint == null)
            {
                TlsUtilities.WriteOpaque16(TlsUtilities.EmptyBytes, buf);
            }
            else
            {
                TlsUtilities.WriteOpaque16(this.mPskIdentityHint, buf);
            }

            if (this.mKeyExchange == KeyExchangeAlgorithm.DHE_PSK)
            {
                if (this.mDHParameters == null)
                {
                    throw new TlsFatalAlert(AlertDescription.internal_error);
                }

                this.mDHAgreePrivateKey = TlsDHUtilities.GenerateEphemeralServerKeyExchange(mContext.SecureRandom,
                                                                                            this.mDHParameters, buf);
            }
            else if (this.mKeyExchange == KeyExchangeAlgorithm.ECDHE_PSK)
            {
                this.mECAgreePrivateKey = TlsEccUtilities.GenerateEphemeralServerKeyExchange(mContext.SecureRandom,
                                                                                             mNamedCurves, mClientECPointFormats, buf);
            }

            return(buf.ToArray());
        }
예제 #19
0
 public static void WriteDHParameter(BigInteger x, Stream output)
 {
     TlsUtilities.WriteOpaque16(BigIntegers.AsUnsignedByteArray(x), output);
 }
예제 #20
0
 /**
  * Encode this {@link NewSessionTicket} to a {@link Stream}.
  *
  * @param output the {@link Stream} to encode to.
  * @throws IOException
  */
 public virtual void Encode(Stream output)
 {
     TlsUtilities.WriteUint32(mTicketLifetimeHint, output);
     TlsUtilities.WriteOpaque16(mTicket, output);
 }