Skip to content

Commit ddbbdcb

Browse files
[Android] Fix onNOCChainGeneration terminate issue (project-chip#32235)
* Fix onNOCChainGeneration terminate issue * Restyled by clang-format * update to static_cast --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 1160f97 commit ddbbdcb

File tree

1 file changed

+52
-36
lines changed

1 file changed

+52
-36
lines changed

src/controller/java/CHIPDeviceController-JNI.cpp

+52-36
Original file line numberDiff line numberDiff line change
@@ -171,44 +171,54 @@ JNI_METHOD(jint, onNOCChainGeneration)
171171
ChipLogProgress(Controller, "setNOCChain() called");
172172

173173
jmethodID getRootCertificate;
174+
jmethodID getIntermediateCertificate;
175+
jmethodID getOperationalCertificate;
176+
jmethodID getIpk;
177+
jmethodID getAdminSubject;
178+
179+
jbyteArray rootCertificate = nullptr;
180+
jbyteArray intermediateCertificate = nullptr;
181+
jbyteArray operationalCertificate = nullptr;
182+
jbyteArray ipk = nullptr;
183+
184+
Optional<NodeId> adminSubjectOptional;
185+
uint64_t adminSubject = chip::kUndefinedNodeId;
186+
187+
CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
188+
189+
Optional<Crypto::IdentityProtectionKeySpan> ipkOptional;
190+
uint8_t ipkValue[CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES];
191+
Crypto::IdentityProtectionKeySpan ipkTempSpan(ipkValue);
192+
193+
VerifyOrExit(controllerParams != nullptr, ChipLogError(Controller, "controllerParams is null!"));
194+
174195
err = chip::JniReferences::GetInstance().FindMethod(env, controllerParams, "getRootCertificate", "()[B", &getRootCertificate);
175-
VerifyOrReturnValue(err == CHIP_NO_ERROR, static_cast<jint>(err.AsInteger()));
196+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Find getRootCertificate method fail!"));
176197

177-
jmethodID getIntermediateCertificate;
178198
err = chip::JniReferences::GetInstance().FindMethod(env, controllerParams, "getIntermediateCertificate", "()[B",
179199
&getIntermediateCertificate);
180-
VerifyOrReturnValue(err == CHIP_NO_ERROR, static_cast<jint>(err.AsInteger()));
200+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Find getIntermediateCertificate method fail!"));
181201

182-
jmethodID getOperationalCertificate;
183202
err = chip::JniReferences::GetInstance().FindMethod(env, controllerParams, "getOperationalCertificate", "()[B",
184203
&getOperationalCertificate);
185-
VerifyOrReturnValue(err == CHIP_NO_ERROR, static_cast<jint>(err.AsInteger()));
204+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Find getOperationalCertificate method fail!"));
186205

187-
jmethodID getIpk;
188206
err = chip::JniReferences::GetInstance().FindMethod(env, controllerParams, "getIpk", "()[B", &getIpk);
189-
VerifyOrReturnValue(err == CHIP_NO_ERROR, static_cast<jint>(err.AsInteger()));
207+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Find getIpk method fail!"));
190208

191-
jmethodID getAdminSubject;
192209
err = chip::JniReferences::GetInstance().FindMethod(env, controllerParams, "getAdminSubject", "()J", &getAdminSubject);
193-
VerifyOrReturnValue(err == CHIP_NO_ERROR, static_cast<jint>(err.AsInteger()));
194-
195-
jbyteArray rootCertificate = (jbyteArray) env->CallObjectMethod(controllerParams, getRootCertificate);
196-
VerifyOrReturnValue(rootCertificate != nullptr, static_cast<jint>(CHIP_ERROR_BAD_REQUEST.AsInteger()));
210+
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Find getAdminSubject method fail!"));
197211

198-
jbyteArray intermediateCertificate = (jbyteArray) env->CallObjectMethod(controllerParams, getIntermediateCertificate);
199-
VerifyOrReturnValue(intermediateCertificate != nullptr, static_cast<jint>(CHIP_ERROR_BAD_REQUEST.AsInteger()));
212+
rootCertificate = static_cast<jbyteArray>(env->CallObjectMethod(controllerParams, getRootCertificate));
213+
VerifyOrExit(rootCertificate != nullptr, err = CHIP_ERROR_BAD_REQUEST);
200214

201-
jbyteArray operationalCertificate = (jbyteArray) env->CallObjectMethod(controllerParams, getOperationalCertificate);
202-
VerifyOrReturnValue(operationalCertificate != nullptr, static_cast<jint>(CHIP_ERROR_BAD_REQUEST.AsInteger()));
215+
intermediateCertificate = static_cast<jbyteArray>(env->CallObjectMethod(controllerParams, getIntermediateCertificate));
216+
VerifyOrExit(intermediateCertificate != nullptr, err = CHIP_ERROR_BAD_REQUEST);
203217

204-
// use ipk and adminSubject from CommissioningParameters if not set in ControllerParams
205-
CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
218+
operationalCertificate = static_cast<jbyteArray>(env->CallObjectMethod(controllerParams, getOperationalCertificate));
219+
VerifyOrExit(operationalCertificate != nullptr, err = CHIP_ERROR_BAD_REQUEST);
206220

207-
Optional<Crypto::IdentityProtectionKeySpan> ipkOptional;
208-
uint8_t ipkValue[CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES];
209-
Crypto::IdentityProtectionKeySpan ipkTempSpan(ipkValue);
210-
211-
jbyteArray ipk = (jbyteArray) env->CallObjectMethod(controllerParams, getIpk);
221+
ipk = static_cast<jbyteArray>(env->CallObjectMethod(controllerParams, getIpk));
212222
if (ipk != nullptr)
213223
{
214224
JniByteArray jByteArrayIpk(env, ipk);
@@ -217,7 +227,7 @@ JNI_METHOD(jint, onNOCChainGeneration)
217227
{
218228
ChipLogError(Controller, "Invalid IPK size %u and expect %u", static_cast<unsigned>(jByteArrayIpk.byteSpan().size()),
219229
static_cast<unsigned>(sizeof(ipkValue)));
220-
return CHIP_ERROR_INVALID_IPK.AsInteger();
230+
ExitNow(err = CHIP_ERROR_INVALID_IPK);
221231
}
222232
memcpy(&ipkValue[0], jByteArrayIpk.byteSpan().data(), jByteArrayIpk.byteSpan().size());
223233

@@ -229,8 +239,7 @@ JNI_METHOD(jint, onNOCChainGeneration)
229239
ipkOptional.SetValue(commissioningParams.GetIpk().Value());
230240
}
231241

232-
Optional<NodeId> adminSubjectOptional;
233-
uint64_t adminSubject = static_cast<uint64_t>(env->CallLongMethod(controllerParams, getAdminSubject));
242+
adminSubject = static_cast<uint64_t>(env->CallLongMethod(controllerParams, getAdminSubject));
234243
if (adminSubject == kUndefinedNodeId)
235244
{
236245
// if no value pass in ControllerParams, use value from CommissioningParameters
@@ -243,20 +252,27 @@ JNI_METHOD(jint, onNOCChainGeneration)
243252
// NOTE: we are allowing adminSubject to not be set since the OnNOCChainGeneration callback makes this field
244253
// optional and includes logic to handle the case where it is not set. It would also make sense to return
245254
// an error here since that use case may not be realistic.
246-
247-
JniByteArray jByteArrayRcac(env, rootCertificate);
248-
JniByteArray jByteArrayIcac(env, intermediateCertificate);
249-
JniByteArray jByteArrayNoc(env, operationalCertificate);
255+
{
256+
JniByteArray jByteArrayRcac(env, rootCertificate);
257+
JniByteArray jByteArrayIcac(env, intermediateCertificate);
258+
JniByteArray jByteArrayNoc(env, operationalCertificate);
250259

251260
#ifndef JAVA_MATTER_CONTROLLER_TEST
252-
err = wrapper->GetAndroidOperationalCredentialsIssuer()->NOCChainGenerated(CHIP_NO_ERROR, jByteArrayNoc.byteSpan(),
253-
jByteArrayIcac.byteSpan(), jByteArrayRcac.byteSpan(),
254-
ipkOptional, adminSubjectOptional);
261+
err = wrapper->GetAndroidOperationalCredentialsIssuer()->NOCChainGenerated(
262+
CHIP_NO_ERROR, jByteArrayNoc.byteSpan(), jByteArrayIcac.byteSpan(), jByteArrayRcac.byteSpan(), ipkOptional,
263+
adminSubjectOptional);
255264

256-
if (err != CHIP_NO_ERROR)
257-
{
258-
ChipLogError(Controller, "Failed to SetNocChain for the device: %" CHIP_ERROR_FORMAT, err.Format());
265+
if (err != CHIP_NO_ERROR)
266+
{
267+
ChipLogError(Controller, "Failed to SetNocChain for the device: %" CHIP_ERROR_FORMAT, err.Format());
268+
}
269+
return static_cast<jint>(err.AsInteger());
270+
#endif // JAVA_MATTER_CONTROLLER_TEST
259271
}
272+
exit:
273+
#ifndef JAVA_MATTER_CONTROLLER_TEST
274+
err = wrapper->GetAndroidOperationalCredentialsIssuer()->NOCChainGenerated(err, ByteSpan(), ByteSpan(), ByteSpan(), ipkOptional,
275+
adminSubjectOptional);
260276
#endif // JAVA_MATTER_CONTROLLER_TEST
261277
return static_cast<jint>(err.AsInteger());
262278
}

0 commit comments

Comments
 (0)