Skip to content

Commit

Permalink
Persist keypair used by example opcert signer (#6821)
Browse files Browse the repository at this point in the history
* Persist keypair used by example opcert signer

* Fix Android build
  • Loading branch information
pan-apple authored May 14, 2021
1 parent e5cb796 commit d7c56ff
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 44 deletions.
8 changes: 5 additions & 3 deletions examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ CHIP_ERROR ModelCommand::Run(PersistentStorage & storage, NodeId localId, NodeId
{
CHIP_ERROR err = CHIP_NO_ERROR;

mOpCredsIssuer.Initialize();

chip::Controller::CommissionerInitParams initParams;
initParams.storageDelegate = &storage;
initParams.storageDelegate = &storage;

err = mOpCredsIssuer.Initialize(storage);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Operational Cred Issuer: %s", ErrorStr(err)));

initParams.operationalCredentialsDelegate = &mOpCredsIssuer;

err = mCommissioner.SetUdpListenPort(storage.GetListenPort());
Expand Down
12 changes: 7 additions & 5 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ CHIP_ERROR PairingCommand::Run(PersistentStorage & storage, NodeId localId, Node
{
CHIP_ERROR err = CHIP_NO_ERROR;

mOpCredsIssuer.Initialize();

chip::Controller::CommissionerInitParams params;
params.storageDelegate = &storage;
params.mDeviceAddressUpdateDelegate = this;
params.pairingDelegate = this;
params.storageDelegate = &storage;
params.mDeviceAddressUpdateDelegate = this;
params.pairingDelegate = this;

err = mOpCredsIssuer.Initialize(storage);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Operational Cred Issuer: %s", ErrorStr(err)));

params.operationalCredentialsDelegate = &mOpCredsIssuer;

err = mCommissioner.SetUdpListenPort(storage.GetListenPort());
Expand Down
1 change: 0 additions & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class PairingCommand : public Command,
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
break;
}
mOpCredsIssuer.Initialize();
}

/////////// Command Interface /////////
Expand Down
8 changes: 5 additions & 3 deletions examples/chip-tool/commands/reporting/ReportingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ CHIP_ERROR ReportingCommand::Run(PersistentStorage & storage, NodeId localId, No
{
CHIP_ERROR err = CHIP_NO_ERROR;

mOpCredsIssuer.Initialize();

chip::Controller::BasicCluster cluster;
chip::Controller::CommissionerInitParams initParams;

initParams.storageDelegate = &storage;
initParams.storageDelegate = &storage;

err = mOpCredsIssuer.Initialize(storage);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Operational Cred Issuer: %s", ErrorStr(err)));

initParams.operationalCredentialsDelegate = &mOpCredsIssuer;

err = mCommissioner.SetUdpListenPort(storage.GetListenPort());
Expand Down
26 changes: 26 additions & 0 deletions src/controller/ExampleOperationalCredentialsIssuer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,35 @@
namespace chip {
namespace Controller {

constexpr const char kOperationalCredentialsIssuerKeypairStorage[] = "ExampleOpCredsCAKey";

using namespace Credentials;
using namespace Crypto;

CHIP_ERROR ExampleOperationalCredentialsIssuer::Initialize(PersistentStorageDelegate & storage)
{
Crypto::P256SerializedKeypair serializedKey;
uint16_t keySize = static_cast<uint16_t>(serializedKey.Capacity());

if (storage.SyncGetKeyValue(kOperationalCredentialsIssuerKeypairStorage, serializedKey, keySize) != CHIP_NO_ERROR)
{
// Storage doesn't have an existing keypair. Let's create one and add it to the storage.
ReturnErrorOnFailure(mIssuer.Initialize());
ReturnErrorOnFailure(mIssuer.Serialize(serializedKey));

keySize = static_cast<uint16_t>(serializedKey.Length());
ReturnErrorOnFailure(storage.SyncSetKeyValue(kOperationalCredentialsIssuerKeypairStorage, serializedKey, keySize));
}
else
{
// Use the keypair from the storage
ReturnErrorOnFailure(mIssuer.Deserialize(serializedKey));
}

mInitialized = true;
return CHIP_NO_ERROR;
}

CHIP_ERROR ExampleOperationalCredentialsIssuer::GenerateNodeOperationalCertificate(const PeerId & peerId, const ByteSpan & csr,
int64_t serialNumber, uint8_t * certBuf,
uint32_t certBufSize, uint32_t & outCertLen)
Expand Down
41 changes: 13 additions & 28 deletions src/controller/ExampleOperationalCredentialsIssuer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
* issuer for CHIP devices. The class can be used as a guideline on how to
* construct your own certificate issuer. It can also be used in tests and tools
* if a specific signing authority is not required.
*
* NOTE: This class stores the encryption key in clear storage. This is not suited
* for production use. This should only be used in test tools.
*/

#pragma once

#include <controller/OperationalCredentialsDelegate.h>
#include <core/CHIPError.h>
#include <core/CHIPPersistentStorageDelegate.h>
#include <crypto/CHIPCryptoPAL.h>
#include <support/CodeUtils.h>

Expand All @@ -45,36 +49,17 @@ class DLL_EXPORT ExampleOperationalCredentialsIssuer : public OperationalCredent
CHIP_ERROR GetRootCACertificate(FabricId fabricId, uint8_t * certBuf, uint32_t certBufSize, uint32_t & outCertLen) override;

/**
* @brief Serialize the issuer's keypair.
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR Serialize(Crypto::P256SerializedKeypair & issuer)
{
VerifyOrReturnError(mInitialized, CHIP_ERROR_INCORRECT_STATE);
return mIssuer.Serialize(issuer);
}

/**
* @brief Deserialize the keypair as issuer's keypair.
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR Deserialize(Crypto::P256SerializedKeypair & issuer)
{
ReturnErrorOnFailure(mIssuer.Deserialize(issuer));
mInitialized = true;
return CHIP_NO_ERROR;
}

/**
* @brief Initialize the issuer with a new keypair.
* @brief Initialize the issuer with the keypair in the storage.
* If the storage doesn't have one, it'll create one, and it to the storage.
*
* @param[in] storage A reference to the storage, where the keypair is stored.
* The object of ExampleOperationalCredentialsIssuer doesn't hold
* on the reference of storage.
*
* @return Returns a CHIP_ERROR on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR Initialize()
{
ReturnErrorOnFailure(mIssuer.Initialize());
mInitialized = true;
return CHIP_NO_ERROR;
}
[[deprecated("This class stores the encryption key in clear storage. Don't use it for production code.")]] CHIP_ERROR
Initialize(PersistentStorageDelegate & storage);

void SetIssuerId(uint32_t id) { mIssuerId = id; }

Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew(Jav
initParams.inetLayer = inetLayer;
initParams.bleLayer = GetJNIBleLayer();

*errInfoOnFailure = wrapper->OpCredsIssuer().Initialize();
*errInfoOnFailure = wrapper->OpCredsIssuer().Initialize(*initParams.storageDelegate);
if (*errInfoOnFailure != CHIP_NO_ERROR)
{
return nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ CHIP_ERROR pychip_DeviceController_NewDeviceController(chip::Controller::DeviceC
localDeviceId = kDefaultLocalDeviceId;
}

ReturnErrorOnFailure(sOperationalCredentialsIssuer.Initialize());
ReturnErrorOnFailure(sOperationalCredentialsIssuer.Initialize(sStorageDelegate));

initParams.storageDelegate = &sStorageDelegate;
initParams.mDeviceAddressUpdateDelegate = &sDeviceAddressUpdateDelegate;
Expand Down
3 changes: 1 addition & 2 deletions src/controller/python/chip/internal/CommissionerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ extern "C" chip::Controller::DeviceCommissioner * pychip_internal_Commissioner_N
params.inetLayer = &chip::DeviceLayer::InetLayer;
params.pairingDelegate = &gPairingDelegate;

err = gOperationalCredentialsIssuer.Initialize();

err = gOperationalCredentialsIssuer.Initialize(gServerStorage);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Operational credentials issuer initialization failed: %s", chip::ErrorStr(err));
Expand Down

0 comments on commit d7c56ff

Please sign in to comment.