@@ -171,44 +171,54 @@ JNI_METHOD(jint, onNOCChainGeneration)
171
171
ChipLogProgress (Controller, " setNOCChain() called" );
172
172
173
173
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
+
174
195
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! " ));
176
197
177
- jmethodID getIntermediateCertificate;
178
198
err = chip::JniReferences::GetInstance ().FindMethod (env, controllerParams, " getIntermediateCertificate" , " ()[B" ,
179
199
&getIntermediateCertificate);
180
- VerifyOrReturnValue (err == CHIP_NO_ERROR, static_cast <jint>(err. AsInteger () ));
200
+ VerifyOrExit (err == CHIP_NO_ERROR, ChipLogError (Controller, " Find getIntermediateCertificate method fail! " ));
181
201
182
- jmethodID getOperationalCertificate;
183
202
err = chip::JniReferences::GetInstance ().FindMethod (env, controllerParams, " getOperationalCertificate" , " ()[B" ,
184
203
&getOperationalCertificate);
185
- VerifyOrReturnValue (err == CHIP_NO_ERROR, static_cast <jint>(err. AsInteger () ));
204
+ VerifyOrExit (err == CHIP_NO_ERROR, ChipLogError (Controller, " Find getOperationalCertificate method fail! " ));
186
205
187
- jmethodID getIpk;
188
206
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! " ));
190
208
191
- jmethodID getAdminSubject;
192
209
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!" ));
197
211
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);
200
214
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);
203
217
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 );
206
220
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));
212
222
if (ipk != nullptr )
213
223
{
214
224
JniByteArray jByteArrayIpk (env, ipk);
@@ -217,7 +227,7 @@ JNI_METHOD(jint, onNOCChainGeneration)
217
227
{
218
228
ChipLogError (Controller, " Invalid IPK size %u and expect %u" , static_cast <unsigned >(jByteArrayIpk.byteSpan ().size ()),
219
229
static_cast <unsigned >(sizeof (ipkValue)));
220
- return CHIP_ERROR_INVALID_IPK. AsInteger ( );
230
+ ExitNow (err = CHIP_ERROR_INVALID_IPK);
221
231
}
222
232
memcpy (&ipkValue[0 ], jByteArrayIpk.byteSpan ().data (), jByteArrayIpk.byteSpan ().size ());
223
233
@@ -229,8 +239,7 @@ JNI_METHOD(jint, onNOCChainGeneration)
229
239
ipkOptional.SetValue (commissioningParams.GetIpk ().Value ());
230
240
}
231
241
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));
234
243
if (adminSubject == kUndefinedNodeId )
235
244
{
236
245
// if no value pass in ControllerParams, use value from CommissioningParameters
@@ -243,20 +252,27 @@ JNI_METHOD(jint, onNOCChainGeneration)
243
252
// NOTE: we are allowing adminSubject to not be set since the OnNOCChainGeneration callback makes this field
244
253
// optional and includes logic to handle the case where it is not set. It would also make sense to return
245
254
// 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);
250
259
251
260
#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);
255
264
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
259
271
}
272
+ exit :
273
+ #ifndef JAVA_MATTER_CONTROLLER_TEST
274
+ err = wrapper->GetAndroidOperationalCredentialsIssuer ()->NOCChainGenerated (err, ByteSpan (), ByteSpan (), ByteSpan (), ipkOptional,
275
+ adminSubjectOptional);
260
276
#endif // JAVA_MATTER_CONTROLLER_TEST
261
277
return static_cast <jint>(err.AsInteger ());
262
278
}
0 commit comments