Skip to content

Provisioning Android devices to connect to AWS IoT MQTT message broker without manual provisioning steps

0

Hi, In 2020, we tried a PoC for our Android Devices to use AWS IoT. At that time, we could provision the required cert/keys right from our Android Apps and using AWS Cognito and the aws-iot-java-SDKV1 library.

mIotAndroidClient = new AWSIotClient(new CognitoCachingCredentialsProvider(context, mCognitoPoolId, mRegion));
mIotAndroidClient.setRegion(Region.getRegion(mRegion));

// Create a new private key and certificate. This call
// creates both on the server and returns them to the
// device.
CreateKeysAndCertificateRequest createKeysAndCertificateRequest =
    new CreateKeysAndCertificateRequest();
createKeysAndCertificateRequest.setSetAsActive(true);
final CreateKeysAndCertificateResult createKeysAndCertificateResult;
createKeysAndCertificateResult =
    mIotAndroidClient.createKeysAndCertificate(createKeysAndCertificateRequest);

// store in keystore for use in MQTT client
// saved as alias "default" so a new certificate isn't
// generated each run of this application
AWSIotKeystoreHelper.saveCertificateAndPrivateKey(mCertificateId,
    createKeysAndCertificateResult.getCertificatePem(),
    createKeysAndCertificateResult.getKeyPair().getPrivateKey(),
    mKeystorePath, mKeystoreName, mKeystorePassword);

// load keystore from file into memory to pass on
// connection
mClientKeyStore = AWSIotKeystoreHelper.getIotKeystore(mCertificateId,
    mKeystorePath, mKeystoreName, mKeystorePassword);

// Attach a policy to the newly created certificate.
// This flow assumes the policy was already created in
// AWS IoT and we are now just attaching it to the
// certificate.
AttachPrincipalPolicyRequest policyAttachRequest =
    new AttachPrincipalPolicyRequest();
policyAttachRequest.setPolicyName(mAWSIoTPolicyName);
policyAttachRequest.setPrincipal(createKeysAndCertificateResult.getCertificateArn());
mIotAndroidClient.attachPrincipalPolicy(policyAttachRequest);

mMqttManager.connect(mClientKeyStore, new AWSIotMqttClientStatusCallback() {
        @Override
        public void onStatusChanged(final AWSIotMqttClientStatus status,
                                    final Throwable throwable) {
            mStatus = String.valueOf(status);
            if (status == AWSIotMqttClientStatusCallback.AWSIotMqttClientStatus.Connected) {
                    subscribe(mTopics);
            }
        }
    });

Now we are again trying to connect to AWS IoT from the Android Apps using SDK V2, we've gone through this documentation - https://docs.aws.amazon.com/iot/latest/developerguide/iot-connect-devices.html#iot-connect-device-sdks

We've understood from this doc that we need to define a Provisioning Template in AWS IoT. Then on the Android device, we need to generate a certificate and key set using the provisioning claim step in advance. Using this claim, the IoT Fleet Provisioning server will provide the certificate and key. We CAN NOT perform this additional step of generating the certificate/key in person on these Android devices as these are customer's devices -

https://github.com/aws/aws-iot-device-sdk-java-v2/tree/main/samples/FleetProvisioning#creating-a-certificate-key-set-from-a-provisioning-claim

Question Are there any connection methods we can use that will allow us to have the required provisioning to connect to the IoT Server done programmatically right from the customer's Android devices where our App is installed? This was possible with sdkv1. How can we achieve this with SDK v2 ??

asked 2 years ago588 views

1 Answer
0

Hello,

Yes, there are methods to provision Android devices to connect to AWS IoT MQTT message broker without manual provisioning steps, even with the newer SDK v2. AWS IoT Core offers a feature called fleet provisioning that allows you to generate and securely deliver device certificates and private keys to your devices when they connect to AWS IoT for the first time.

With fleet provisioning, you can implement a process similar to what you were doing with SDK v1, but in a more secure and scalable manner. Here's how you can approach this:

  1. Set up a fleet provisioning template in AWS IoT Core. This template defines the resources and permissions that will be assigned to your devices.

  2. In your Android app, you'll need to include a provisioning claim certificate. This is a temporary certificate that allows the device to connect to AWS IoT Core for the purpose of exchanging it for a unique certificate.

  3. When your app runs for the first time on a customer's device, it can use this claim certificate to connect to AWS IoT Core and request a unique certificate for that device.

  4. AWS IoT Core will then generate a new certificate and private key for the device and send it back securely.

  5. Your app can then store this new certificate and private key securely on the device (e.g., in the Android Keystore) and use it for all future connections to AWS IoT Core.

This process allows you to provision devices programmatically without any manual steps on the customer's device. It's more secure than the previous method because each device gets a unique certificate, and you have more control over the provisioning process through the fleet provisioning template.

To implement this with SDK v2, you would use the AWS IoT Device SDK for Java v2, which includes support for fleet provisioning. The exact code would be different from your SDK v1 example, but the overall process remains similar - connecting with a temporary credential, requesting a permanent credential, and then using that for future connections.

Remember to handle error cases and implement retry logic in case of network issues during the provisioning process. Also, ensure that you're following best practices for storing sensitive information like certificates and private keys on Android devices.

Resources
Device provisioning - IoT Lens

Provisioning by claim

answered 2 years ago

AWS
SUPPORT ENGINEER

revised 2 years ago

AWS
SUPPORT ENGINEER

revised 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.