Example #1
0
        private void testMutualVerification(Srp6GroupParameters group)
        {
            byte[] I = Encoding.UTF8.GetBytes("username");
            byte[] P = Encoding.UTF8.GetBytes("password");
            byte[] s = new byte[16];
            random.NextBytes(s);

            Srp6VerifierGenerator gen = new Srp6VerifierGenerator();

            gen.Init(group, new Sha256Digest());
            BigInteger v = gen.GenerateVerifier(s, I, P);

            Srp6Client client = new Srp6Client();

            client.Init(group, new Sha256Digest(), random);

            Srp6Server server = new Srp6Server();

            server.Init(group, v, new Sha256Digest(), random);

            BigInteger A = client.GenerateClientCredentials(s, I, P);
            BigInteger B = server.GenerateServerCredentials();

            BigInteger clientS = client.CalculateSecret(B);
            BigInteger serverS = server.CalculateSecret(A);

            if (!clientS.Equals(serverS))
            {
                Fail("SRP agreement failed - client/server calculated different secrets");
            }
        }
Example #2
0
        public void InitSrp(string login)
        {
            const string password = "******"; //TODO: Get account password

            BigInteger srpV = VerifierGenerator.GenerateVerifier(
                SrpSBytes,
                Encoding.ASCII.GetBytes(login),
                Encoding.ASCII.GetBytes(password));

            SrpServer = new Srp6Server();
            SrpServer.Init(SrpN, SrpG, srpV, new Sha1Digest(), new SecureRandom());
            SrpB = SrpServer.GenerateServerCredentials().ToByteArrayUnsigned();
        }
Example #3
0
        private void testServerCatchesBadA(Srp6GroupParameters group)
        {
            byte[] I = Encoding.UTF8.GetBytes("username");
            byte[] P = Encoding.UTF8.GetBytes("password");
            byte[] s = new byte[16];
            random.NextBytes(s);

            Srp6VerifierGenerator gen = new Srp6VerifierGenerator();

            gen.Init(group, new Sha256Digest());
            BigInteger v = gen.GenerateVerifier(s, I, P);

            Srp6Server server = new Srp6Server();

            server.Init(group, v, new Sha256Digest(), random);

            server.GenerateServerCredentials();

            try
            {
                server.CalculateSecret(BigInteger.Zero);
                Fail("Client failed to detect invalid value for 'A'");
            }
            catch (CryptoException)
            {
                // Expected
            }

            try
            {
                server.CalculateSecret(group.N);
                Fail("Client failed to detect invalid value for 'A'");
            }
            catch (CryptoException)
            {
                // Expected
            }
        }
Example #4
0
        public override byte[] GenerateServerKeyExchange()
        {
            mSrpServer.Init(mSrpGroup, mSrpVerifier, TlsUtilities.CreateHash(HashAlgorithm.sha1), mContext.SecureRandom);
            BigInteger B = mSrpServer.GenerateServerCredentials();

            ServerSrpParams srpParams = new ServerSrpParams(mSrpGroup.N, mSrpGroup.G, mSrpSalt, B);

            DigestInputBuffer buf = new DigestInputBuffer();

            srpParams.Encode(buf);

            if (mServerCredentials != null)
            {
                /*
                 * RFC 5246 4.7. digitally-signed element needs SignatureAndHashAlgorithm from TLS 1.2
                 */
                SignatureAndHashAlgorithm signatureAndHashAlgorithm = TlsUtilities.GetSignatureAndHashAlgorithm(
                    mContext, mServerCredentials);

                IDigest d = TlsUtilities.CreateHash(signatureAndHashAlgorithm);

                SecurityParameters securityParameters = mContext.SecurityParameters;
                d.BlockUpdate(securityParameters.clientRandom, 0, securityParameters.clientRandom.Length);
                d.BlockUpdate(securityParameters.serverRandom, 0, securityParameters.serverRandom.Length);
                buf.UpdateDigest(d);

                byte[] hash = new byte[d.GetDigestSize()];
                d.DoFinal(hash, 0);

                byte[] signature = mServerCredentials.GenerateCertificateSignature(hash);

                DigitallySigned signed_params = new DigitallySigned(signatureAndHashAlgorithm, signature);
                signed_params.Encode(buf);
            }

            return(buf.ToArray());
        }
        public override byte[] GenerateServerKeyExchange()
        {
            mSrpServer.Init(mSrpGroup, mSrpVerifier, TlsUtilities.CreateHash(2), mContext.SecureRandom);
            BigInteger        b = mSrpServer.GenerateServerCredentials();
            ServerSrpParams   serverSrpParams   = new ServerSrpParams(mSrpGroup.N, mSrpGroup.G, mSrpSalt, b);
            DigestInputBuffer digestInputBuffer = new DigestInputBuffer();

            serverSrpParams.Encode((Stream)(object)digestInputBuffer);
            if (mServerCredentials != null)
            {
                SignatureAndHashAlgorithm signatureAndHashAlgorithm = TlsUtilities.GetSignatureAndHashAlgorithm(mContext, mServerCredentials);
                IDigest            digest             = TlsUtilities.CreateHash(signatureAndHashAlgorithm);
                SecurityParameters securityParameters = mContext.SecurityParameters;
                digest.BlockUpdate(securityParameters.clientRandom, 0, securityParameters.clientRandom.Length);
                digest.BlockUpdate(securityParameters.serverRandom, 0, securityParameters.serverRandom.Length);
                digestInputBuffer.UpdateDigest(digest);
                byte[] array = new byte[digest.GetDigestSize()];
                digest.DoFinal(array, 0);
                byte[]          signature       = mServerCredentials.GenerateCertificateSignature(array);
                DigitallySigned digitallySigned = new DigitallySigned(signatureAndHashAlgorithm, signature);
                digitallySigned.Encode((Stream)(object)digestInputBuffer);
            }
            return(((MemoryStream)digestInputBuffer).ToArray());
        }