Skip to content

Commit

Permalink
tv-casting-app: Decouple setDAC* API from tv-casting initialization (#…
Browse files Browse the repository at this point in the history
…25990)

* tv-casting-app: Decouple setDAC* API from tv-casting initialization

* Updating README with NDK version requirements

* Update examples/tv-casting-app/darwin/TvCasting/TvCasting/ExampleDAC.swift

Co-authored-by: Cliff Chung <[email protected]>

* Restyling changes

* Resolving build errors/warns

---------

Co-authored-by: Cliff Chung <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Mar 11, 2024
1 parent 904d989 commit 2434944
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.chip.casting.app;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.chip.casting.AppParameters;
import com.chip.casting.DACProviderStub;
import com.chip.casting.DiscoveredNodeData;
import com.chip.casting.TvCastingApp;
import com.chip.casting.util.DACProviderStub;
import com.chip.casting.util.GlobalCastingConstants;
import com.chip.casting.util.PreferencesConfigurationManager;
import java.util.Random;
Expand Down Expand Up @@ -79,7 +78,7 @@ public void handleDisconnect() {
private boolean initJni() {
tvCastingApp = new TvCastingApp();

Context applicationContext = this.getApplicationContext();
tvCastingApp.setDACProvider(new DACProviderStub());

AppParameters appParameters = new AppParameters();
appParameters.setConfigurationManager(
Expand All @@ -89,10 +88,9 @@ private boolean initJni() {
new byte[AppParameters.MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH];
new Random().nextBytes(rotatingDeviceIdUniqueId);
appParameters.setRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueId);
appParameters.setDacProvider(new DACProviderStub());
appParameters.setSetupPasscode(GlobalCastingConstants.SetupPasscode);
appParameters.setDiscriminator(GlobalCastingConstants.Discriminator);
return tvCastingApp.initApp(applicationContext, appParameters);
return tvCastingApp.initApp(this.getApplicationContext(), appParameters);
}

private void showFragment(Fragment fragment, boolean showOnBack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chip.casting;
package com.chip.casting.util;

import android.util.Base64;
import com.chip.casting.DACProvider;

public class DACProviderStub implements DACProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ public class AppParameters {
public static final int MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH = 16;
private static final int TEST_SETUP_PASSCODE = 20202021;
private static final int TEST_DISCRIMINATOR = 0xF00;
private DACProvider TEST_DAC_PROVIDER = new DACProviderStub();

private ConfigurationManager configurationManager;
private byte[] rotatingDeviceIdUniqueId;
private DACProvider dacProvider = TEST_DAC_PROVIDER;
private String spake2pVerifierBase64;
private String Spake2pSaltBase64;
private int spake2pIterationCount;
Expand Down Expand Up @@ -63,14 +61,6 @@ public byte[] getRotatingDeviceIdUniqueId() {
return Arrays.copyOf(rotatingDeviceIdUniqueId, rotatingDeviceIdUniqueId.length);
}

public DACProvider getDacProvider() {
return dacProvider;
}

public void setDacProvider(DACProvider dacProvider) {
this.dacProvider = dacProvider;
}

public String getSpake2pVerifierBase64() {
return spake2pVerifierBase64;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ public boolean initApp(Context applicationContext, AppParameters appParameters)
return ret;
}

setDACProvider(appParameters.getDacProvider());
return initJni(appParameters);
}

private native void setDACProvider(DACProvider provider);
public native void setDACProvider(DACProvider provider);

private native boolean preInitJni(AppParameters appParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ JNI_METHOD(jboolean, initJni)(JNIEnv *, jobject, jobject jAppParameters)

JNI_METHOD(void, setDACProvider)(JNIEnv *, jobject, jobject provider)
{
if (!chip::Credentials::IsDeviceAttestationCredentialsProviderSet())
{
JNIDACProvider * p = new JNIDACProvider(provider);
chip::Credentials::SetDeviceAttestationCredentialsProvider(p);
}
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "JNI_METHOD setDACProvider called");

JNIDACProvider * p = new JNIDACProvider(provider);
chip::Credentials::SetDeviceAttestationCredentialsProvider(p);
}

JNI_METHOD(jboolean, openBasicCommissioningWindow)
Expand Down
1 change: 0 additions & 1 deletion examples/tv-casting-app/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ android_library("java") {
"App/app/src/main/jni/com/chip/casting/ContentApp.java",
"App/app/src/main/jni/com/chip/casting/ContentLauncherTypes.java",
"App/app/src/main/jni/com/chip/casting/DACProvider.java",
"App/app/src/main/jni/com/chip/casting/DACProviderStub.java",
"App/app/src/main/jni/com/chip/casting/DiscoveredNodeData.java",
"App/app/src/main/jni/com/chip/casting/FailureCallback.java",
"App/app/src/main/jni/com/chip/casting/MatterCallbackHandler.java",
Expand Down
6 changes: 3 additions & 3 deletions examples/tv-casting-app/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ the TV.
- [Matter TV Casting Android App Example](#matter-tv-casting-android-app-example)
- [Requirements for building](#requirements-for-building)
- [ABIs and TARGET_CPU](#abis-and-target_cpu)
- [Gradle & JDK Version](#gradle--jdk-version)
- [Gradle \& JDK Version](#gradle--jdk-version)
- [Preparing for build](#preparing-for-build)
- [Building & Installing the app](#building--installing-the-app)
- [Building \& Installing the app](#building--installing-the-app)

<hr>

## Requirements for building

You need Android SDK 21 & NDK downloaded to your machine. Set the
You need Android SDK 21 & NDK 23.2.8568313 downloaded to your machine. Set the
`$ANDROID_HOME` environment variable to where the SDK is downloaded and the
`$ANDROID_NDK_HOME` environment variable to point to where the NDK package is
downloaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

@property NSData * spake2pVerifierBase64;

@property DeviceAttestationCredentialsHolder * deviceAttestationCredentials;

@end

#endif /* AppParameters_h */
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
initAppStatusHandler:(nullable void (^)(bool))initAppStatusHandler;

- (void)setDacHolder:(DeviceAttestationCredentialsHolder * _Nonnull)deviceAttestationCredentials
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
setDacHolderStatus:(void (^_Nonnull)(MatterError * _Nonnull))setDacHolderStatus;

/*!
@brief Browse for on-network commissioner TVs
Expand Down Expand Up @@ -192,7 +196,7 @@
@param responseHandler Called when purgeCache completes
*/
- (void)purgeCache:(dispatch_queue_t _Nonnull)clientQueue responseHandler:(void (^)(MatterError * _Nonnull))responseHandler;
- (void)purgeCache:(dispatch_queue_t _Nonnull)clientQueue responseHandler:(void (^_Nonnull)(MatterError * _Nonnull))responseHandler;

/*!
@brief Start the Matter server and reconnect to a previously connected Video Player (if any). This API is async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/attestation_verifier/DefaultDeviceAttestationVerifier.h>
#include <credentials/attestation_verifier/DeviceAttestationVerifier.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <lib/support/CHIPListUtils.h>
#include <lib/support/CHIPMem.h>
#include <platform/PlatformManager.h>
Expand Down Expand Up @@ -129,7 +128,6 @@ - (MatterError *)initializeApp:(AppParameters * _Nullable)appParameters

CHIP_ERROR err = CHIP_NO_ERROR;
_commissionableDataProvider = new CommissionableDataProviderImpl();
_deviceAttestationCredentialsProvider = chip::Credentials::Examples::GetExampleDACProvider();

_appParameters = appParameters;
AppParams cppAppParams;
Expand Down Expand Up @@ -171,55 +169,13 @@ - (MatterError *)initializeApp:(AppParameters * _Nullable)appParameters
ChipLogError(AppServer, "Failed to initialize CommissionableDataProvider: %s", ErrorStr(err));
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

if (_appParameters.deviceAttestationCredentials != nil) {
NSData * certificationDeclarationNsData = _appParameters.deviceAttestationCredentials.getCertificationDeclaration;
chip::MutableByteSpan certificationDeclaration
= chip::MutableByteSpan(const_cast<uint8_t *>(static_cast<const uint8_t *>(certificationDeclarationNsData.bytes)),
certificationDeclarationNsData.length);

NSData * firmwareInformationNsData = _appParameters.deviceAttestationCredentials.getFirmwareInformation;
chip::MutableByteSpan firmwareInformation
= chip::MutableByteSpan(const_cast<uint8_t *>(static_cast<const uint8_t *>(firmwareInformationNsData.bytes)),
firmwareInformationNsData.length);

NSData * deviceAttestationCertNsData = _appParameters.deviceAttestationCredentials.getDeviceAttestationCert;
chip::MutableByteSpan deviceAttestationCert
= chip::MutableByteSpan(const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertNsData.bytes)),
deviceAttestationCertNsData.length);

NSData * productAttestationIntermediateCertNsData
= _appParameters.deviceAttestationCredentials.getProductAttestationIntermediateCert;
chip::MutableByteSpan productAttestationIntermediateCert = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(productAttestationIntermediateCertNsData.bytes)),
productAttestationIntermediateCertNsData.length);

NSData * deviceAttestationCertPrivateKeyNsData
= _appParameters.deviceAttestationCredentials.getDeviceAttestationCertPrivateKey;
chip::MutableByteSpan deviceAttestationCertPrivateKey = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertPrivateKeyNsData.bytes)),
deviceAttestationCertPrivateKeyNsData.length);

NSData * deviceAttestationCertPublicKeyKeyNsData
= _appParameters.deviceAttestationCredentials.getDeviceAttestationCertPublicKeyKey;
chip::MutableByteSpan deviceAttestationCertPublicKeyKey = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertPublicKeyKeyNsData.bytes)),
deviceAttestationCertPublicKeyKeyNsData.length);

_deviceAttestationCredentialsProvider = new DeviceAttestationCredentialsProviderImpl(&certificationDeclaration,
&firmwareInformation, &deviceAttestationCert, &productAttestationIntermediateCert, &deviceAttestationCertPrivateKey,
&deviceAttestationCertPublicKeyKey);
}
}
chip::DeviceLayer::SetCommissionableDataProvider(_commissionableDataProvider);

_commissionableDataProvider->GetSetupPasscode(setupPasscode);
_commissionableDataProvider->GetSetupDiscriminator(setupDiscriminator);
_onboardingPayload = [[OnboardingPayload alloc] initWithSetupPasscode:setupPasscode setupDiscriminator:setupDiscriminator];

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(_deviceAttestationCredentialsProvider);

// Initialize device attestation verifier from a constant version
{
// TODO: Replace testingRootStore with a AttestationTrustStore that has the necessary official PAA roots available
Expand Down Expand Up @@ -280,6 +236,53 @@ - (MatterError *)initializeApp:(AppParameters * _Nullable)appParameters
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]];
}

- (void)setDacHolder:(DeviceAttestationCredentialsHolder * _Nonnull)deviceAttestationCredentials
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
setDacHolderStatus:(void (^_Nonnull)(MatterError * _Nonnull))setDacHolderStatus
{
dispatch_sync(_chipWorkQueue, ^{
NSData * certificationDeclarationNsData = deviceAttestationCredentials.getCertificationDeclaration;
chip::MutableByteSpan certificationDeclaration
= chip::MutableByteSpan(const_cast<uint8_t *>(static_cast<const uint8_t *>(certificationDeclarationNsData.bytes)),
certificationDeclarationNsData.length);

NSData * firmwareInformationNsData = deviceAttestationCredentials.getFirmwareInformation;
chip::MutableByteSpan firmwareInformation = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(firmwareInformationNsData.bytes)), firmwareInformationNsData.length);

NSData * deviceAttestationCertNsData = deviceAttestationCredentials.getDeviceAttestationCert;
chip::MutableByteSpan deviceAttestationCert
= chip::MutableByteSpan(const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertNsData.bytes)),
deviceAttestationCertNsData.length);

NSData * productAttestationIntermediateCertNsData = deviceAttestationCredentials.getProductAttestationIntermediateCert;
chip::MutableByteSpan productAttestationIntermediateCert = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(productAttestationIntermediateCertNsData.bytes)),
productAttestationIntermediateCertNsData.length);

NSData * deviceAttestationCertPrivateKeyNsData = deviceAttestationCredentials.getDeviceAttestationCertPrivateKey;
chip::MutableByteSpan deviceAttestationCertPrivateKey = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertPrivateKeyNsData.bytes)),
deviceAttestationCertPrivateKeyNsData.length);

NSData * deviceAttestationCertPublicKeyKeyNsData = deviceAttestationCredentials.getDeviceAttestationCertPublicKey;
chip::MutableByteSpan deviceAttestationCertPublicKeyKey = chip::MutableByteSpan(
const_cast<uint8_t *>(static_cast<const uint8_t *>(deviceAttestationCertPublicKeyKeyNsData.bytes)),
deviceAttestationCertPublicKeyKeyNsData.length);

self->_deviceAttestationCredentialsProvider
= new DeviceAttestationCredentialsProviderImpl(&certificationDeclaration, &firmwareInformation, &deviceAttestationCert,
&productAttestationIntermediateCert, &deviceAttestationCertPrivateKey, &deviceAttestationCertPublicKeyKey);

SetDeviceAttestationCredentialsProvider(self->_deviceAttestationCredentialsProvider);

dispatch_async(clientQueue, ^{
setDacHolderStatus([[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]);
});
});
}

- (void)discoverCommissioners:(dispatch_queue_t _Nonnull)clientQueue
discoveryRequestSentHandler:(nullable void (^)(bool))discoveryRequestSentHandler
discoveredCommissionerHandler:(nullable void (^)(DiscoveredNodeData *))discoveredCommissionerHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

- (NSData * _Nonnull)getDeviceAttestationCertPrivateKey;

- (NSData * _Nonnull)getDeviceAttestationCertPublicKeyKey;
- (NSData * _Nonnull)getDeviceAttestationCertPublicKey;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ @interface DeviceAttestationCredentialsHolder ()

@property NSData * deviceAttestationCertPrivateKey;

@property NSData * deviceAttestationCertPublicKeyKey;
@property NSData * deviceAttestationCertPublicKey;

@end

@implementation DeviceAttestationCredentialsHolder

- (DeviceAttestationCredentialsHolder * _Nonnull)
initWithCertificationDeclaration:(NSData * _Nonnull)certificationDeclaration
firmwareInformation:(NSData * _Nonnull)firmwareInformation
deviceAttestationCert:(NSData * _Nonnull)deviceAttestationCert
productAttestationIntermediateCert:(NSData * _Nonnull)productAttestationIntermediateCert
deviceAttestationCertPrivateKey:(NSData * _Nonnull)deviceAttestationCertPrivateKey
deviceAttestationCertPublicKeyKey:(NSData * _Nonnull)deviceAttestationCertPublicKeyKey
- (DeviceAttestationCredentialsHolder * _Nonnull)initWithCertificationDeclaration:(NSData * _Nonnull)certificationDeclaration
firmwareInformation:(NSData * _Nonnull)firmwareInformation
deviceAttestationCert:(NSData * _Nonnull)deviceAttestationCert
productAttestationIntermediateCert:
(NSData * _Nonnull)productAttestationIntermediateCert
deviceAttestationCertPrivateKey:(NSData * _Nonnull)deviceAttestationCertPrivateKey
deviceAttestationCertPublicKeyKey:(NSData * _Nonnull)deviceAttestationCertPublicKey
{
self = [super init];
if (self) {
Expand All @@ -52,7 +52,7 @@ @implementation DeviceAttestationCredentialsHolder
_deviceAttestationCert = deviceAttestationCert;
_productAttestationIntermediateCert = productAttestationIntermediateCert;
_deviceAttestationCertPrivateKey = deviceAttestationCertPrivateKey;
_deviceAttestationCertPublicKeyKey = deviceAttestationCertPublicKeyKey;
_deviceAttestationCertPublicKey = deviceAttestationCertPublicKey;
}
return self;
}
Expand Down Expand Up @@ -82,8 +82,8 @@ - (NSData * _Nonnull)getDeviceAttestationCertPrivateKey;
return _deviceAttestationCertPrivateKey;
}

- (NSData * _Nonnull)getDeviceAttestationCertPublicKeyKey;
- (NSData * _Nonnull)getDeviceAttestationCertPublicKey;
{
return _deviceAttestationCertPublicKeyKey;
return _deviceAttestationCertPublicKey;
}
@end
Loading

0 comments on commit 2434944

Please sign in to comment.