summaryrefslogtreecommitdiff
path: root/libandroid_net_lowpan/LowpanCredential.cpp
blob: f0c6109fae8f3d49c3f9144a95da43f5ac80ff9d (plain)
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "LowpanCredential"

#include <android/net/lowpan/LowpanCredential.h>

#include <binder/Parcel.h>
#include <log/log.h>
#include <utils/Errors.h>

using android::BAD_TYPE;
using android::BAD_VALUE;
using android::NO_ERROR;
using android::Parcel;
using android::status_t;
using android::UNEXPECTED_NULL;
using android::net::lowpan::LowpanCredential;
using namespace ::android::binder;

namespace android {

namespace net {

namespace lowpan {

#define RETURN_IF_FAILED(calledOnce)                                     \
    {                                                                    \
        status_t returnStatus = calledOnce;                              \
        if (returnStatus) {                                              \
            ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
            return returnStatus;                                         \
         }                                                               \
    }

LowpanCredential::LowpanCredential() : mMasterKeyIndex(UNSPECIFIED_MASTER_KEY_INDEX) { }

status_t LowpanCredential::initMasterKey(LowpanCredential& out, const uint8_t* masterKeyBytes, int masterKeyLen, int masterKeyIndex)
{
    if (masterKeyLen < 0) {
        return BAD_INDEX;
    } else if (masterKeyLen > MASTER_KEY_MAX_SIZE) {
        return BAD_INDEX;
    } else if (masterKeyBytes == NULL) {
        return BAD_VALUE;
    }

    out.mMasterKey.clear();
    out.mMasterKey.insert(out.mMasterKey.end(), masterKeyBytes, masterKeyBytes + masterKeyLen);
    out.mMasterKeyIndex = masterKeyIndex;

    return NO_ERROR;
}

status_t LowpanCredential::initMasterKey(LowpanCredential& out, const uint8_t* masterKeyBytes, int masterKeyLen)
{
    return LowpanCredential::initMasterKey(out, masterKeyBytes, masterKeyLen, 0);
}

status_t LowpanCredential::initMasterKey(LowpanCredential& out, const std::vector<uint8_t>& masterKey, int masterKeyIndex)
{
    return LowpanCredential::initMasterKey(out, &masterKey.front(), masterKey.size(), masterKeyIndex);
}

status_t LowpanCredential::initMasterKey(LowpanCredential& out, const std::vector<uint8_t>& masterKey)
{
    return LowpanCredential::initMasterKey(out, masterKey, 0);
}

bool LowpanCredential::isMasterKey() const {
    return mMasterKey.size() > 0;
}

bool LowpanCredential::getMasterKey(std::vector<uint8_t>* masterKey) const {
    if (isMasterKey()) {
        *masterKey = mMasterKey;
        return true;
    }
    return false;
}

bool LowpanCredential::getMasterKey(const uint8_t** masterKey, int* masterKeyLen) const {
    if (isMasterKey()) {
        if (masterKey) {
            *masterKey = &mMasterKey.front();
        }
        if (masterKeyLen) {
            *masterKeyLen = mMasterKey.size();
        }
        return true;
    }
    return false;
}

int LowpanCredential::getMasterKeyIndex() const {
    return mMasterKeyIndex;
}

status_t LowpanCredential::writeToParcel(Parcel* parcel) const {
    /*
     * Keep implementation in sync with writeToParcel() in
     * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanCredential.java.
     */
    RETURN_IF_FAILED(parcel->writeByteVector(mMasterKey));
    RETURN_IF_FAILED(parcel->writeInt32(mMasterKeyIndex));
    return NO_ERROR;
}

status_t LowpanCredential::readFromParcel(const Parcel* parcel) {
    /*
     * Keep implementation in sync with readFromParcel() in
     * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanCredential.java.
     */
    RETURN_IF_FAILED(parcel->readByteVector(&mMasterKey));
    RETURN_IF_FAILED(parcel->readInt32(&mMasterKeyIndex));
    return NO_ERROR;
}

bool LowpanCredential::operator==(const LowpanCredential& rhs)
{
    if (mMasterKey != rhs.mMasterKey) {
        return false;
    }

    if (mMasterKeyIndex != rhs.mMasterKeyIndex) {
        return false;
    }

    return true;
}

}  // namespace lowpan

}  // namespace net

}  // namespace android