File: Key.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 (65 lines) | stat: -rw-r--r-- 1,517 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
/*
 * Dibbler - a portable DHCPv6
 *
 * author: Tomasz Mrugalski <thomson@klub.com.pl>
 *
 * released under GNU GPL v2 licence
 */

#include <string.h>
#include "Key.h"
#include "Logger.h"
extern "C" {
#include "base64.h"
}

TSIGKey::TSIGKey(const std::string& name)
    : Digest_(DIGEST_NONE), Name_(name), Fudge_(300) {

}
std::string TSIGKey::getAlgorithmText() {
    switch (Digest_) {
    case DIGEST_HMAC_MD5:
        return std::string("HMAC-MD5.SIG-ALG.REG.INT");
    case DIGEST_HMAC_SHA1:
        return std::string("HMAC-SHA1.SIG-ALG.REG.INT");
    case DIGEST_HMAC_SHA256:
        return std::string("HMAC-SHA256.SIG-ALG.REG.INT");
    default:
        return std::string("Unsupported algorithm");
    }
}

bool TSIGKey::setData(const std::string& base64encoded) {
    Base64Data_ = base64encoded;

    char raw[100];
    size_t raw_len = 100;
    memset(raw, 0, 100);

    base64_decode_context ctx = {0};
    base64_decode_ctx_init(&ctx);

    bool status = base64_decode(&ctx, Base64Data_.c_str(),
                                Base64Data_.length(),
                                raw, &raw_len);
    if (!status) {
        Log(Warning) << "Failed to base64-decode key. There are trailing unparsed characters."
                     << LogEnd;
    }

    Data_.resize(raw_len);
    for (unsigned i=0; i<raw_len; i++) {
        Data_[i] = raw[i];
    }

    return status;
}

std::string TSIGKey::getPackedData() {
    return Data_;
}

std::string TSIGKey::getBase64Data() {
    return Base64Data_;
}