Skip to content

Commit

Permalink
Expose CSR Nonce to Android/iOS layer (#7987)
Browse files Browse the repository at this point in the history
* Expose csrNonce param to pairDevice() in Android

* Expose csrNonce param to pairDevice() in Darwin
  • Loading branch information
austinh0 authored and pull[bot] committed Aug 20, 2021
1 parent 49039f8 commit 1256160
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self)
return result;
}

JNI_METHOD(void, pairDevice)(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jint connObj, jlong pinCode)
JNI_METHOD(void, pairDevice)
(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jint connObj, jlong pinCode, jbyteArray csrNonce)
{
StackLockGuard lock(JniReferences::GetInstance().GetStackLock());
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -271,6 +272,11 @@ JNI_METHOD(void, pairDevice)(JNIEnv * env, jobject self, jlong handle, jlong dev
.SetConnectionObject(reinterpret_cast<BLE_CONNECTION_OBJECT>(connObj))
.SetBleLayer(&sBleLayer)
.SetPeerAddress(Transport::PeerAddress::BLE());
if (csrNonce != nullptr)
{
JniByteArray jniCsrNonce(env, csrNonce);
params = params.SetCSRNonce(jniCsrNonce.byteSpan());
}
err = wrapper->Controller()->PairDevice(deviceId, params);

if (err != CHIP_NO_ERROR)
Expand Down
2 changes: 2 additions & 0 deletions src/controller/java/JniTypeWrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <jni.h>
#include <lib/support/Span.h>

/// Exposes the underlying UTF string from a jni string
class JniUtfString
Expand All @@ -44,6 +45,7 @@ class JniByteArray
~JniByteArray() { mEnv->ReleaseByteArrayElements(mArray, mData, 0); }

const jbyte * data() const { return mData; }
chip::ByteSpan byteSpan() const { return chip::ByteSpan(reinterpret_cast<const uint8_t *>(data()), size()); }
jsize size() const { return mDataLength; }

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ public BluetoothGattCallback getCallback() {
}

public void pairDevice(BluetoothGatt bleServer, long deviceId, long setupPincode) {
pairDevice(bleServer, deviceId, setupPincode, null);
}

/**
* Pair a device connected through BLE.
*
* <p>TODO(#7985): Annotate csrNonce as Nullable.
*
* @param bleServer the BluetoothGatt representing the BLE connection to the device
* @param deviceId the node ID to assign to the device
* @param setupPincode the pincode for the device
* @param csrNonce the 32-byte CSR nonce to use, or null if we want to use an internally randomly
* generated CSR nonce.
*/
public void pairDevice(
BluetoothGatt bleServer, long deviceId, long setupPincode, byte[] csrNonce) {
if (connectionId == 0) {
bleGatt = bleServer;

Expand All @@ -59,7 +75,7 @@ public void pairDevice(BluetoothGatt bleServer, long deviceId, long setupPincode

Log.d(TAG, "Bluetooth connection added with ID: " + connectionId);
Log.d(TAG, "Pairing device with ID: " + deviceId);
pairDevice(deviceControllerPtr, deviceId, connectionId, setupPincode);
pairDevice(deviceControllerPtr, deviceId, connectionId, setupPincode, csrNonce);
} else {
Log.e(TAG, "Bluetooth connection already in use.");
completionListener.onError(new Exception("Bluetooth connection already in use."));
Expand All @@ -74,10 +90,6 @@ public void pairTestDeviceWithoutSecurity(String ipAddress) {
pairTestDeviceWithoutSecurity(deviceControllerPtr, ipAddress);
}

public void pairDevice(long deviceId, int connectionId, long pinCode) {
pairDevice(deviceControllerPtr, deviceId, connectionId, pinCode);
}

public long getDevicePointer(long deviceId) {
return getDevicePointer(deviceControllerPtr, deviceId);
}
Expand Down Expand Up @@ -199,7 +211,7 @@ public boolean isActive(long deviceId) {
private native long newDeviceController();

private native void pairDevice(
long deviceControllerPtr, long deviceId, int connectionId, long pinCode);
long deviceControllerPtr, long deviceId, int connectionId, long pinCode, byte[] csrNonce);

private native void unpairDevice(long deviceControllerPtr, long deviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ - (void)handleRendezVousBLE:(uint16_t)discriminator setupPINCode:(uint32_t)setup
{
NSError * error;
uint64_t deviceID = CHIPGetNextAvailableDeviceID();
if ([self.chipController pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode error:&error]) {
if ([self.chipController pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode csrNonce:nil error:&error]) {
deviceID++;
CHIPSetNextAvailableDeviceID(deviceID);
}
Expand Down
1 change: 1 addition & 0 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)pairDevice:(uint64_t)deviceID
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
csrNonce:(nullable NSData *)csrNonce
error:(NSError * __autoreleasing *)error;

- (BOOL)pairDevice:(uint64_t)deviceID
Expand Down
7 changes: 6 additions & 1 deletion src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ - (NSNumber *)_getControllerNodeId
- (BOOL)pairDevice:(uint64_t)deviceID
discriminator:(uint16_t)discriminator
setupPINCode:(uint32_t)setupPINCode
csrNonce:(nullable NSData *)csrNonce
error:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
Expand All @@ -224,6 +225,10 @@ - (BOOL)pairDevice:(uint64_t)deviceID
chip::RendezvousParameters params
= chip::RendezvousParameters().SetSetupPINCode(setupPINCode).SetDiscriminator(discriminator);

if (csrNonce != nil) {
params = params.SetCSRNonce(chip::ByteSpan((const uint8_t *) csrNonce.bytes, csrNonce.length));
}

if ([self isRunning]) {
_operationalCredentialsDelegate->SetDeviceID(deviceID);
errorCode = self.cppCommissioner->PairDevice(deviceID, params);
Expand Down Expand Up @@ -306,7 +311,7 @@ - (BOOL)pairDevice:(uint64_t)deviceID
uint16_t discriminator = setupPayload.discriminator.unsignedShortValue;
uint32_t setupPINCode = setupPayload.setUpPINCode.unsignedIntValue;
_operationalCredentialsDelegate->SetDeviceID(deviceID);
didSucceed = [self pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode error:error];
didSucceed = [self pairDevice:deviceID discriminator:discriminator setupPINCode:setupPINCode csrNonce:nil error:error];
} else {
CHIP_LOG_ERROR("Failed to create CHIPSetupPayload for pairing with error %@", *error);
}
Expand Down

0 comments on commit 1256160

Please sign in to comment.