File: OptAuthentication.cpp

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (331 lines) | stat: -rw-r--r-- 10,064 bytes parent folder | download | duplicates (3)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Dibbler - a portable DHCPv6
 *
 * author: Michal Kowalczuk <michal@kowalczuk.eu>
 * changes: Tomek Mrugalski <thomson@klub.com.pl>
 *
 * released under GNU GPL v2 licence
 */

#include <string.h>
#include <stdlib.h>
#include "Portable.h"
#include "OptAuthentication.h"
#include "DHCPConst.h"
#include "Portable.h"
#include "Portable.h"
#include "Logger.h"
#include "Msg.h"
#include "Portable.h"

TOptAuthentication::TOptAuthentication(char* buf, size_t buflen, TMsg* parent)
    :TOpt(OPTION_AUTH, parent), authDataPtr_(NULL), AuthInfoLen_(0) {

    if (buflen < OPT_AUTH_FIXED_SIZE) {
        Valid = false;
        return;
    }

    uint8_t proto = buf[0];
    switch (proto) {
    case AUTH_PROTO_NONE:
    case AUTH_PROTO_DELAYED:
    case AUTH_PROTO_RECONFIGURE_KEY:
    case AUTH_PROTO_DIBBLER:
        proto_ = static_cast<AuthProtocols>(proto);
        break;
    default: {
        Log(Warning) << "Auth: unsupported protocol type " << proto
                     << " received in auth option." << LogEnd;
        Valid = false;
        return;
    }
    }

    algo_ = buf[1];

    uint8_t rdm = buf[2];
    if (rdm <= AUTH_REPLAY_MONOTONIC) {
        rdm_ = static_cast<AuthReplay>(rdm);
    } else {
        Log(Warning) << "Auth: unsupported rdm value " << rdm
                     << " received in auth option." << LogEnd;
        Valid = false;
        return;
    }

    replay_ = readUint64(buf);

    buf += OPT_AUTH_FIXED_SIZE;
    buflen -= OPT_AUTH_FIXED_SIZE;

    switch (proto_) {
    default:
    case AUTH_PROTO_NONE:
        data_ = std::vector<uint8_t>(buf, buf + buflen);
        if (Parent)
            Parent->setAuthDigestPtr(buf, buflen);
        break;
    case AUTH_PROTO_DELAYED: {
        // We can have 2 cases here:
        // 1. this is SOLICIT and client sends no data, buflen should be zero
        // 2. this is something else, so the data format should follow section 21.4.1
        //    i.e. have 4bytes key ID, 128bis of HMAC-MD5 and the rest is 'realm'
        if (buflen == 0) {
            // SOLICIT option, without any data.
            break;
        }
        if (buflen < DELAYED_AUTH_DIGEST_SIZE + DELAYED_AUTH_KEY_ID_SIZE) {
            Log(Warning) << "AUTH: protocol set to delayed-auth, but variable option data"
                         << " is smaller (" << buflen << ") than required 20. Invalid auth."
                         << LogEnd;
            Valid = false;
            return;
        }

        // The gentleman who designed delayed auth protocol was not exactly sober
        // during the act of creation.
        data_ = std::vector<uint8_t>(buf, buf + buflen); // let's store that crap in data_

        // First undetermined number of bytes is DHCP realm
        // (fine idea to start with variable field. Why not?)
        int realm_size = buflen - DELAYED_AUTH_DIGEST_SIZE - DELAYED_AUTH_KEY_ID_SIZE;
        realm_ = std::string(buf, buf + realm_size);
        buf += realm_size;
        buflen -= realm_size;

        // The next 4 bytes (RFC3315 never says that it's exactly 4 bytes. Nah, every
        // implementor will just guess the same size)
        if (Parent)
            Parent->setSPI(readUint32(buf));
        buf += sizeof(uint32_t);
        buflen -= sizeof(uint32_t);

        // The rest should be 16 bytes of of HMAC-MD5 data
        if (Parent)
            Parent->setAuthDigestPtr(buf, buflen); // that should be the last 16 bytes.
        break;
    }
    case AUTH_PROTO_RECONFIGURE_KEY: {
        data_ = std::vector<uint8_t>(buf, buf + buflen);
        if (Parent)
            Parent->setAuthDigestPtr(buf + 1, buflen);
        if (data_.size() != RECONFIGURE_KEY_AUTHINFO_SIZE) {
            Log(Warning) << "AUTH: Invalid reconfigure-key data received. Expected size is "
                         << RECONFIGURE_KEY_AUTHINFO_SIZE << ", but received " << data_.size()
                         << LogEnd;
            Valid = false;
            return;
        }
        break;
    }

    case AUTH_PROTO_DIBBLER: {
        if (algo_ >= DIGEST_INVALID) {
            Log(Warning) << "Unsupported digest type: " << algo_ << ", max supported "
                         << " type is " << DIGEST_INVALID - 1 << LogEnd;
            Valid = false;
            return;
        }
        if (!Parent) {
            Log(Error) << "Orphaned option AUTH: no parent set." << LogEnd;
            Valid = false;
            return;
        }
        Parent->DigestType_ = static_cast<DigestTypes>(algo_);
        Parent->setSPI(readUint32(buf));
        buf += sizeof(uint32_t);
        buflen -= sizeof(uint32_t);

        data_ = std::vector<uint8_t>(buf, buf + buflen);

        AuthInfoLen_ = getDigestSize(parent->DigestType_);
        if (buflen != AuthInfoLen_){
            Log(Warning) << "Auth: Invalid digest size for digest type " << algo_
                       << ", expected len=" << AuthInfoLen_ << ", received " << buflen << LogEnd;
            Valid = false;
            return;
        }

        Parent->setAuthDigestPtr(buf, AuthInfoLen_);
        PrintHex(std::string("Auth: Received digest ") + getDigestName(parent->DigestType_) + ": ",
                 (uint8_t*)buf, AuthInfoLen_);
    }
    }

    Valid = true;
}

uint8_t TOptAuthentication::getAlgorithm() const {
    return algo_;
}

AuthReplay TOptAuthentication::getRDM() const {
    return rdm_;
}

TOptAuthentication::TOptAuthentication(AuthProtocols proto, uint8_t algo,
                                       AuthReplay rdm, TMsg* parent)
    :TOpt(OPTION_AUTH, parent), proto_(proto), algo_(algo), rdm_(rdm), replay_(0),
    authDataPtr_(NULL) {
    switch (proto) {
    default:
        AuthInfoLen_ = 0;
        return;
    case AUTH_PROTO_DELAYED:
        if (algo != 1) {
            Log(Warning) << "AUTH: The only defined protocol for delayed-auth is 1, but "
                         << static_cast<int>(algo) << " was specified." << LogEnd;
        }
        AuthInfoLen_ = 0; // Keep it as zero (we don't know yet if it's a SOLICIT
                          // (no data at all) or any other (realm + key id + HMAC-MD5)
        return;
    case AUTH_PROTO_DIBBLER: {
        if (parent) {
            AuthInfoLen_ = getDigestSize(parent->DigestType_);
        } else {
            AuthInfoLen_ = 0;
        }
        return;
    }
    case AUTH_PROTO_RECONFIGURE_KEY:
        AuthInfoLen_ = RECONFIGURE_DIGEST_SIZE; // Sec section 21.5.1, RFC3315
        return;
    }
}

void TOptAuthentication::setRDM(AuthReplay value) {
    rdm_ = value;
}

size_t TOptAuthentication::getSize() {
    size_t tmp = TOpt::OPTION6_HDR_LEN + OPT_AUTH_FIXED_SIZE + AuthInfoLen_;
    switch (proto_) {
    default:
        return tmp;
    case AUTH_PROTO_DELAYED:
        if (realm_.empty()) {
            return tmp;
        } else {
            return tmp + realm_.size() + DELAYED_AUTH_KEY_ID_SIZE;
        }
    case AUTH_PROTO_RECONFIGURE_KEY:
        return tmp + 1;
    case AUTH_PROTO_DIBBLER:
        return tmp + sizeof(uint32_t); // +4 bytes for SPI
    }
}

bool TOpt::doDuties() {
    return true;
}

char* TOptAuthentication::storeSelf(char* buf) {

    // common part (the same for all auth protocols)
    buf = writeUint16(buf, OptType);
    buf = writeUint16(buf, getSize() - 4);
    buf = writeUint8(buf, static_cast<char>(proto_));
    buf = writeUint8(buf, algo_);
    buf = writeUint8(buf, static_cast<char>(rdm_));
    buf = writeUint64(buf, getReplayDetection());

    switch (proto_) {

    case AUTH_PROTO_RECONFIGURE_KEY: {
        if (!data_.size()) {
            return buf;
        }

        switch (data_[0]) {
        default:
        case 1: // reconfigure-key value
            authDataPtr_ = buf;
            buf = writeData(buf, (char*)&data_[0], data_.size());
            return buf;
        case 2: // HMAC-MD5 digest
            authDataPtr_ = buf + 1; // The first byte is 1 or 2 (see RFC3315, 21.5.1)
            buf[0] = data_[0]; // 2 = HMAC-MD5 digest
            memset(buf + 1, 0, RECONFIGURE_DIGEST_SIZE); // 16 bytes
            return buf + RECONFIGURE_KEY_AUTHINFO_SIZE;
        }
    }

    default:
    case AUTH_PROTO_NONE: {
        authDataPtr_ = buf;
        buf = writeData(buf, (char*)&data_[0], data_.size());
        return buf;
    }
    case AUTH_PROTO_DELAYED: {
        if (realm_.empty()) {
            // SOLICIT without any data
            return buf;
        } else {
            // Realm set, build the option for real

            memcpy(buf, &realm_[0], realm_.size()); // realm (variable)
            buf += realm_.size();
            if (Parent)
                buf = writeUint32(buf, Parent->getSPI()); // key id (4 bytes)
            else
                buf = writeUint32(buf, 0);
            authDataPtr_ = buf; // Digest will be stored here
            memset(buf, 0, AuthInfoLen_); // HMAC-MD5 (16 bytes)
            buf += AuthInfoLen_;
            return buf;
        }
    }

    case AUTH_PROTO_DIBBLER: {
        if (!Parent) {
            return writeUint32(buf, 0); // no SPI at all
        }

        AuthInfoLen_ = getDigestSize(Parent->DigestType_);

        // write SPI first
        buf = writeUint32(buf, Parent->getSPI());

        // Reserve space for the digest
        Parent->setAuthDigestPtr(buf, AuthInfoLen_);
        memset(buf, 0, AuthInfoLen_);
        buf += AuthInfoLen_;
    }
    }

    return buf;
}

AuthProtocols TOptAuthentication::getProto() const {
    return proto_;
}

void TOptAuthentication::setReplayDetection(uint64_t value) {
    replay_ = value;
}

uint64_t TOptAuthentication::getReplayDetection() {
    return replay_;
}

void TOptAuthentication::setPayload(const std::vector<uint8_t>& data) {
    data_ = data;
}

void TOptAuthentication::getPayload(std::vector<uint8_t>& data) {
    data = data_;
}

bool TOptAuthentication::doDuties() {
    return true;
}

void TOptAuthentication::setRealm(const std::string& realm) {
    realm_ = realm;
    if (realm_.empty()) {
        AuthInfoLen_ = 0;
    } else {
        AuthInfoLen_ = DELAYED_AUTH_DIGEST_SIZE;
    }
}