summary refs log tree commit diff
path: root/crypto/src/util/ssh/SSHBuffer.cs
blob: 8d3c3f977e870e417b8e25cb5a3a98334698256e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using Org.BouncyCastle.Math;

namespace Org.BouncyCastle.Utilities.SSH
{
    public class SSHBuffer
    {
        private readonly byte[] buffer;
        private int pos = 0;

        public SSHBuffer(byte[] magic, byte[] buffer)
        {
            this.buffer = buffer;
            for (int i = 0; i != magic.Length; i++)
            {
                if (magic[i] != buffer[i])
                {
                    throw new ArgumentException("magic-number incorrect");
                }
            }

            pos += magic.Length;
        }

        public SSHBuffer(byte[] buffer)
        {
            this.buffer = buffer;
        }

        public int ReadU32()
        {
            if (pos > (buffer.Length - 4))
            {
                throw new ArgumentOutOfRangeException("4 bytes for U32 exceeds buffer.");
            }

            int i = (buffer[pos++] & 0xFF) << 24;
            i |= (buffer[pos++] & 0xFF) << 16;
            i |= (buffer[pos++] & 0xFF) << 8;
            i |= (buffer[pos++] & 0xFF);

            return i;
        }

        public String ReadString()
        {
            return Strings.FromByteArray(ReadBlock());
        }

        public byte[] ReadBlock()
        {
            int len = ReadU32();
            if (len == 0)
            {
                return new byte[0];
            }

            if (pos > (buffer.Length - len))
            {
                throw new ArgumentException("not enough data for block");
            }

            int start = pos; pos += len;
            return Arrays.CopyOfRange(buffer, start, pos);
        }

        public void SkipBlock()
        {
            int len = ReadU32();
            if (pos > (buffer.Length - len))
            {
                throw new ArgumentException("not enough data for block");
            }

            pos += len;
        }

        public byte[] ReadPaddedBlock()
        {
            return ReadPaddedBlock(8);
        }

        public byte[] ReadPaddedBlock(int blockSize)
        {
            int len = ReadU32();
            if (len == 0)
            {
                return new byte[0];
            }

            if (pos > (buffer.Length - len))
            {
                throw new ArgumentException("not enough data for block");
            }

            int align = len % blockSize;
            if (0 != align)
            {
                throw new ArgumentException("missing padding");
            }

            int start = pos; pos += len;
            int end = pos;

            if (len > 0)
            {
                // TODO If encryption is supported, should be constant-time
                int lastByte = buffer[pos - 1] & 0xFF;
                if (0 < lastByte && lastByte < blockSize)
                {
                    int padCount = lastByte;
                    end -= padCount;

                    for (int i = 1, padPos = end; i <= padCount; ++i, ++padPos)
                    {
                        if (i != (buffer[padPos] & 0xFF))
                        {
                            throw new ArgumentException("incorrect padding");
                        }
                    }
                }
            }

            return Arrays.CopyOfRange(buffer, start, end);
        }

        public BigInteger ReadBigNumPositive()
        {
            int len = ReadU32();
            if (pos + len > buffer.Length)
            {
                throw new ArgumentException("not enough data for big num");
            }

            int start = pos; pos += len;
            byte[] d = Arrays.CopyOfRange(buffer, start, pos);
            return new BigInteger(1, d);
        }

        public byte[] GetBuffer()
        {
            return Arrays.Clone(buffer);
        }

        public Boolean HasRemaining()
        {
            return pos < buffer.Length;
        }
    }
}