|
25 | 25 | #include "AndroidICDClient.h"
|
26 | 26 |
|
27 | 27 | #include <app/icd/client/ICDClientInfo.h>
|
| 28 | +#include <lib/support/JniTypeWrappers.h> |
28 | 29 |
|
29 | 30 | chip::app::DefaultICDClientStorage sICDClientStorage;
|
| 31 | +static CHIP_ERROR ParseICDClientInfo(JNIEnv * env, jint jFabricIndex, jobject jIcdClientInfo, |
| 32 | + chip::app::ICDClientInfo & icdClientInfo); |
30 | 33 |
|
31 | 34 | jobject getICDClientInfo(JNIEnv * env, const char * icdClientInfoSign, jint jFabricIndex)
|
32 | 35 | {
|
@@ -93,6 +96,105 @@ jobject getICDClientInfo(JNIEnv * env, const char * icdClientInfoSign, jint jFab
|
93 | 96 | return jInfo;
|
94 | 97 | }
|
95 | 98 |
|
| 99 | +CHIP_ERROR StoreICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo, jbyteArray jKey) |
| 100 | +{ |
| 101 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 102 | + |
| 103 | + chip::app::ICDClientInfo clientInfo; |
| 104 | + chip::JniByteArray jniKey(env, jKey); |
| 105 | + |
| 106 | + err = ParseICDClientInfo(env, jFabricIndex, jicdClientInfo, clientInfo); |
| 107 | + VerifyOrReturnValue(err == CHIP_NO_ERROR, err, |
| 108 | + ChipLogError(Controller, "Failed to parse ICD Client info: %" CHIP_ERROR_FORMAT, err.Format())); |
| 109 | + |
| 110 | + err = getICDClientStorage()->SetKey(clientInfo, jniKey.byteSpan()); |
| 111 | + |
| 112 | + if (err == CHIP_NO_ERROR) |
| 113 | + { |
| 114 | + err = getICDClientStorage()->StoreEntry(clientInfo); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + getICDClientStorage()->RemoveKey(clientInfo); |
| 119 | + ChipLogError(Controller, "Failed to persist symmetric key with error: %" CHIP_ERROR_FORMAT, err.Format()); |
| 120 | + } |
| 121 | + |
| 122 | + return err; |
| 123 | +} |
| 124 | + |
| 125 | +CHIP_ERROR RemoveICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo) |
| 126 | +{ |
| 127 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 128 | + |
| 129 | + chip::app::ICDClientInfo info; |
| 130 | + err = ParseICDClientInfo(env, jFabricIndex, jicdClientInfo, info); |
| 131 | + VerifyOrReturnValue(err == CHIP_NO_ERROR, err, |
| 132 | + ChipLogError(Controller, "Failed to parse ICD Client info: %" CHIP_ERROR_FORMAT, err.Format())); |
| 133 | + |
| 134 | + getICDClientStorage()->RemoveKey(info); |
| 135 | + |
| 136 | + return err; |
| 137 | +} |
| 138 | + |
| 139 | +CHIP_ERROR ClearICDClientInfo(JNIEnv * env, jint jFabricIndex, jlong jNodeId) |
| 140 | +{ |
| 141 | + CHIP_ERROR err = CHIP_NO_ERROR; |
| 142 | + |
| 143 | + chip::ScopedNodeId scopedNodeId(static_cast<chip::NodeId>(jNodeId), static_cast<chip::FabricIndex>(jFabricIndex)); |
| 144 | + err = getICDClientStorage()->DeleteEntry(scopedNodeId); |
| 145 | + if (err != CHIP_NO_ERROR) |
| 146 | + { |
| 147 | + ChipLogError(Controller, "ClearICDClientInfo error!: %" CHIP_ERROR_FORMAT, err.Format()); |
| 148 | + } |
| 149 | + return err; |
| 150 | +} |
| 151 | + |
| 152 | +CHIP_ERROR ParseICDClientInfo(JNIEnv * env, jint jFabricIndex, jobject jIcdClientInfo, chip::app::ICDClientInfo & icdClientInfo) |
| 153 | +{ |
| 154 | + VerifyOrReturnError(jIcdClientInfo != nullptr, CHIP_ERROR_INVALID_ARGUMENT); |
| 155 | + |
| 156 | + jmethodID getPeerNodeIdMethod = nullptr; |
| 157 | + jmethodID getStartCounterMethod = nullptr; |
| 158 | + jmethodID getOffsetMethod = nullptr; |
| 159 | + jmethodID getMonitoredSubjectMethod = nullptr; |
| 160 | + jmethodID getIcdAesKeyMethod = nullptr; |
| 161 | + jmethodID getIcdHmacKeyMethod = nullptr; |
| 162 | + |
| 163 | + ReturnErrorOnFailure( |
| 164 | + chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getPeerNodeId", "()J", &getPeerNodeIdMethod)); |
| 165 | + ReturnErrorOnFailure( |
| 166 | + chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getStartCounter", "()J", &getStartCounterMethod)); |
| 167 | + ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getOffset", "()J", &getOffsetMethod)); |
| 168 | + ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getMonitoredSubject", "()J", |
| 169 | + &getMonitoredSubjectMethod)); |
| 170 | + ReturnErrorOnFailure( |
| 171 | + chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getIcdAesKey", "()[B", &getIcdAesKeyMethod)); |
| 172 | + ReturnErrorOnFailure( |
| 173 | + chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getIcdHmacKey", "()[B", &getIcdHmacKeyMethod)); |
| 174 | + |
| 175 | + jlong jPeerNodeId = env->CallLongMethod(jIcdClientInfo, getPeerNodeIdMethod); |
| 176 | + jlong jStartCounter = env->CallLongMethod(jIcdClientInfo, getStartCounterMethod); |
| 177 | + jlong jOffset = env->CallLongMethod(jIcdClientInfo, getOffsetMethod); |
| 178 | + jlong jMonitoredSubject = env->CallLongMethod(jIcdClientInfo, getMonitoredSubjectMethod); |
| 179 | + jbyteArray jIcdAesKey = static_cast<jbyteArray>(env->CallObjectMethod(jIcdClientInfo, getIcdAesKeyMethod)); |
| 180 | + jbyteArray jIcdHmacKey = static_cast<jbyteArray>(env->CallObjectMethod(jIcdClientInfo, getIcdHmacKeyMethod)); |
| 181 | + |
| 182 | + chip::ScopedNodeId scopedNodeId(static_cast<chip::NodeId>(jPeerNodeId), static_cast<chip::FabricIndex>(jFabricIndex)); |
| 183 | + chip::JniByteArray jniIcdAesKey(env, jIcdAesKey); |
| 184 | + chip::JniByteArray jniIcdHmacKey(env, jIcdHmacKey); |
| 185 | + |
| 186 | + icdClientInfo.peer_node = scopedNodeId; |
| 187 | + icdClientInfo.start_icd_counter = static_cast<uint32_t>(jStartCounter); |
| 188 | + icdClientInfo.offset = static_cast<uint32_t>(jOffset); |
| 189 | + icdClientInfo.monitored_subject = static_cast<uint64_t>(jMonitoredSubject); |
| 190 | + memcpy(icdClientInfo.aes_key_handle.AsMutable<chip::Crypto::Symmetric128BitsKeyByteArray>(), jniIcdAesKey.data(), |
| 191 | + sizeof(chip::Crypto::Symmetric128BitsKeyByteArray)); |
| 192 | + memcpy(icdClientInfo.hmac_key_handle.AsMutable<chip::Crypto::Symmetric128BitsKeyByteArray>(), jniIcdHmacKey.data(), |
| 193 | + sizeof(chip::Crypto::Symmetric128BitsKeyByteArray)); |
| 194 | + |
| 195 | + return CHIP_NO_ERROR; |
| 196 | +} |
| 197 | + |
96 | 198 | chip::app::DefaultICDClientStorage * getICDClientStorage()
|
97 | 199 | {
|
98 | 200 | return &sICDClientStorage;
|
|
0 commit comments