summary refs log tree commit diff
path: root/crypto/src/tls/DtlsEpoch.cs
blob: e4ce8494870487ff01958d3334a1742f53446f1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.IO;

using Org.BouncyCastle.Tls.Crypto;

namespace Org.BouncyCastle.Tls
{
    internal sealed class DtlsEpoch
    {
        private readonly DtlsReplayWindow m_replayWindow = new DtlsReplayWindow();

        private readonly int m_epoch;
        private readonly TlsCipher m_cipher;

        private long m_sequenceNumber = 0;

        internal DtlsEpoch(int epoch, TlsCipher cipher)
        {
            if (epoch < 0)
                throw new ArgumentException("must be >= 0", "epoch");
            if (cipher == null)
                throw new ArgumentNullException("cipher");

            this.m_epoch = epoch;
            this.m_cipher = cipher;
        }

        /// <exception cref="IOException"/>
        internal long AllocateSequenceNumber()
        {
            lock (this)
            {
                if (m_sequenceNumber >= (1L << 48))
                    throw new TlsFatalAlert(AlertDescription.internal_error);

                return m_sequenceNumber++;
            }
        }

        internal TlsCipher Cipher
        {
            get { return m_cipher; }
        }

        internal int Epoch
        {
            get { return m_epoch; }
        }

        internal DtlsReplayWindow ReplayWindow
        {
            get { return m_replayWindow; }
        }

        internal long SequenceNumber
        {
            get { lock (this) return m_sequenceNumber; }
            set { lock (this) this.m_sequenceNumber = value; }
        }
    }
}