简体   繁体   English

Android 应用程序使用 X.509 证书连接到 AWS IoT

[英]Android app to connect to AWS IoT using X.509 certificate

I am writing an Android application to connect a sensor to an AWS IoT service.我正在编写一个 Android 应用程序来将传感器连接到 AWS IoT 服务。

I have been provided with the X.509 certificate, a pair of public-private key, clientEndpoint , etc.我已获得 X.509 证书、一对公私钥、 clientEndpoint等。

I am trying to follow the AWS's Sample code ( see here ).我正在尝试遵循 AWS 的示例代码( 请参阅此处)。

The instructions are clear, but I don't want to generate the certificate and the keys (I already have them).说明很清楚,但我不想生成证书和密钥(我已经有了它们)。

Below is the code snippet:下面是代码片段:

// 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);
Log.i(LOG_TAG,"Cert ID: " +createKeysAndCertificateResult.getCertificateId() +" created.");

// 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(certificateId,createKeysAndCertificateResult.getCertificatePem(),createKeysAndCertificateResult.getKeyPair().getPrivateKey(),
keystorePath, keystoreName, keystorePassword);
// load keystore from file into memory to pass on
// connection
clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,keystorePath, keystoreName, keystorePassword);

How can I use the existing certificates files instead of generating new certificate and keys?如何使用现有的证书文件而不是生成新的证书和密钥?

Thank you谢谢

  1. Use AWSIotKeystoreHelper.isKeystorePresent(mKeystorePath, mKeystoreName) to check if keystore is already on you device使用AWSIotKeystoreHelper.isKeystorePresent(mKeystorePath, mKeystoreName)检查您的设备上是否已存在密钥库

  2. Check Alias using AWSIotKeystoreHelper.keystoreContainsAlias(mCertificateId, mKeystorePath, mKeystorePassword)使用AWSIotKeystoreHelper.keystoreContainsAlias(mCertificateId, mKeystorePath, mKeystorePassword)检查别名

  3. Get keystore using keystore = AWSIotKeystoreHelper.getIotKeystore(mCertificateId, mKeystoreName, mKeystorePassword)使用keystore = AWSIotKeystoreHelper.getIotKeystore(mCertificateId, mKeystoreName, mKeystorePassword)获取密钥库

  4. Use keystore on mqttManager to connect使用 mqttManager 上的 keystore 进行连接

Here's a full snip that I've successfully used with some test code.这是一个完整的片段,我已经成功地使用了一些测试代码。

    String tempFilePath = context.getFilesDir().getAbsolutePath();

    if (!AWSIotKeystoreHelper.isKeystorePresent(tempFilePath, "iotkeystore")) {

        Resources resources = context.getResources();
        int certId = resources.getIdentifier("foo_device_cert", "raw", context.getPackageName());
        int privkeyId = resources.getIdentifier("foo_device_private_key", "raw", context.getPackageName());

        String cert = TestUtils.loadTestResource(context, certId);
        String privKey = TestUtils.loadTestResource(context, privkeyId);

        AWSIotKeystoreHelper.saveCertificateAndPrivateKey("iotcert", cert, privKey, tempFilePath, "iotkeystore", "iotpasswd");
    }

    KeyStore keystore = AWSIotKeystoreHelper.getIotKeystore("iotcert", tempFilePath, "iotkeystore", "iotpasswd");
    AWSIotMqttManager mqttManager = new AWSIotMqttManager("sdk-java", "xxxxxxxxxxxx-ats.iot.us-east-2.amazonaws.com");
    mqttManager.connect(keystore, new LocalMqttStatusCallback());
    mqttManager.subscribeToTopic("sdk/test/java", AWSIotMqttQos.QOS0, new LocalMessageCallback());
    logger.info("testLoadCertificate()...");

In this case, I simply saved the certificates in the res/raw folder and loaded them at run time as shown above.在这种情况下,我只是将证书保存在 res/raw 文件夹中,并在运行时加载它们,如上所示。 This prob.这个概率。 isn't the best from a security standpoint, but should help you get something working.从安全的角度来看,这不是最好的,但应该可以帮助您获得一些工作。 I put this entire snip into a Robolectric test case and the keystore gets reloaded each time.我将整个片段放入 Robolectric 测试用例中,每次都会重新加载密钥库。 It properly connects and receives messages though.它虽然正确连接并接收消息。

The AWS documentation is really poor here, I wasn't able to find any working sample code from them (bad links) and I'm not wanting to just turn over my entire project to Amplify and it's automatic reconfiguration. AWS 文档在这里真的很差,我无法从中找到任何工作示例代码(错误链接),而且我不想将我的整个项目交给Amplify并且它是自动重新配置的。

Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM