summary refs log tree commit diff
path: root/crypto/src/tls/DtlsEpoch.cs
blob: 9d88a340460a02fdd0ab4eb8d7bbe95e4a359a3c (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
62
63
64
65
66
67
68
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 readonly int m_recordHeaderLengthRead, m_recordHeaderLengthWrite;

        private long m_sequenceNumber = 0;

        internal DtlsEpoch(int epoch, TlsCipher cipher, int recordHeaderLengthRead, int recordHeaderLengthWrite)
        {
            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;
            this.m_recordHeaderLengthRead = recordHeaderLengthRead;
            this.m_recordHeaderLengthWrite = recordHeaderLengthWrite;
        }

        /// <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 int RecordHeaderLengthRead => m_recordHeaderLengthRead;

        internal int RecordHeaderLengthWrite => m_recordHeaderLengthWrite;

        internal DtlsReplayWindow ReplayWindow
        {
            get { return m_replayWindow; }
        }

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