@Before public void setUp() throws Exception { // generate keys KeyPair testKP = AWSIotKeystoreHelper.generatePrivateAndPublicKeys(); // save in keystore AWSIotKeystoreHelper.saveCertificateAndPrivateKey(CERT_ID, TestCert, testKP.getPrivate(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); }
byte[] certBytes = parseDERFromPEM(certPem, AWS_IOT_PEM_BEGIN_CERT_TAG, AWS_IOT_PEM_END_CERT_TAG); X509Certificate cert = generateCertificateFromDER(certBytes); if (!keystoreFile.exists()) { createKeystore(keystorePath, keystoreName, keystorePassword);
@Config(manifest = Config.NONE, reportSdk = 15) @Test(expected = UnsupportedOperationException.class) public void testConnectSdkNotSufficient() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(null); TestClientStatusCallback csb = new TestClientStatusCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); assertEquals(0, mockClient.connectCalls); }
CreateKeysAndCertificateResult certResult = iotClient.createKeysAndCertificate(certRequest); AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certResult.getCertificateId(), certResult.getCertificatePem(), certResult.getKeyPair().getPrivateKey(), iotClient.attachPrincipalPolicy(policyRequest); KeyStore ks = AWSIotKeystoreHelper.getIotKeystore(certResult.getCertificateId(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD);
/** * Save private key and public key and certificate in keystore. * * @param certId The certificate ID or alias in the keystore. * @param certPem The certificate in PEM format. * @param keyPem The private key in PEM format. Can handle with or without * BEGIN RSA/END RSA strings. * @param keystorePath The path to keystore. * @param keystoreName The name of the keystore. * @param keystorePassword The password for the keystore. */ public static void saveCertificateAndPrivateKey(String certId, String certPem, String keyPem, String keystorePath, String keystoreName, String keystorePassword) { PrivateKeyReader privateKeyReader = new PrivateKeyReader(keyPem); PrivateKey privateKey; try { privateKey = privateKeyReader.getPrivateKey(); } catch (IOException e) { throw new AmazonClientException("An error occurred saving the certificate and key.", e); } catch (InvalidKeySpecException e) { throw new AWSIotCertificateException( "An error occurred saving the certificate and key.", e); } saveCertificateAndPrivateKey(certId, certPem, privateKey, keystorePath, keystoreName, keystorePassword); }
if (AWSIotKeystoreHelper.isKeystorePresent(keystorePath, keystoreName)) { if (AWSIotKeystoreHelper.keystoreContainsAlias(certificateId, keystorePath, keystoreName, keystorePassword)) { Log.i(LOG_TAG, "Certificate " + certificateId + " found in keystore - using for MQTT."); clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId, keystorePath, keystoreName, keystorePassword); btnConnect.setEnabled(true);
/** * Get certificate and private key from stream. * * @param certId The certificate Id or alias. * @param keyStoreInputStream an InputStream of a Keystore. * @param keyStorePassword The password for the keystore. * @return KeyStore with with private key and certificate. */ public static KeyStore getIotKeystore(String certId, InputStream keyStoreInputStream, String keyStorePassword) { try { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(keyStoreInputStream, keyStorePassword.toCharArray()); return getTempKeystore(keyStore, certId, keyStorePassword); } catch (CertificateException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (NoSuchAlgorithmException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (KeyStoreException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (IOException e) { throw new AmazonClientException("Error retrieving certificate and key.", e); } }
CreateKeysAndCertificateResult certResult = iotClient.createKeysAndCertificate(certRequest); AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certResult.getCertificateId(), certResult.getCertificatePem(), certResult.getKeyPair().getPrivateKey(), iotClient.attachPrincipalPolicy(policyRequest); KeyStore ks = AWSIotKeystoreHelper.getIotKeystore(certResult.getCertificateId(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD);
/** * Save private key and public key and certificate in keystore. * * @param certId The certificate ID or alias in the keystore. * @param certPem The certificate in PEM format. * @param keyPem The private key in PEM format. Can handle with or without * BEGIN RSA/END RSA strings. * @param keystorePath The path to keystore. * @param keystoreName The name of the keystore. * @param keystorePassword The password for the keystore. */ public static void saveCertificateAndPrivateKey(String certId, String certPem, String keyPem, String keystorePath, String keystoreName, String keystorePassword) { PrivateKeyReader privateKeyReader = new PrivateKeyReader(keyPem); PrivateKey privateKey; try { privateKey = privateKeyReader.getPrivateKey(); } catch (IOException e) { throw new AmazonClientException("An error occurred saving the certificate and key.", e); } catch (InvalidKeySpecException e) { throw new AWSIotCertificateException( "An error occurred saving the certificate and key.", e); } saveCertificateAndPrivateKey(certId, certPem, privateKey, keystorePath, keystoreName, keystorePassword); }
fis.close(); return getTempKeystore(keyStore, certId, keyStorePassword);
@Test(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackEmptyTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); testClient.publishData("test1".getBytes(StringUtils.UTF8), "", AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
CreateKeysAndCertificateResult certResult = iotClient.createKeysAndCertificate(certRequest); AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certResult.getCertificateId(), certResult.getCertificatePem(), certResult.getKeyPair().getPrivateKey(), iotClient.attachPrincipalPolicy(policyRequest); KeyStore ks = AWSIotKeystoreHelper.getIotKeystore(certResult.getCertificateId(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD);
byte[] certBytes = parseDERFromPEM(certPem, AWS_IOT_PEM_BEGIN_CERT_TAG, AWS_IOT_PEM_END_CERT_TAG); X509Certificate cert = generateCertificateFromDER(certBytes); if (!keystoreFile.exists()) { createKeystore(keystorePath, keystoreName, keystorePassword);
@Before public void setUp() throws Exception { // generate keys KeyPair testKP = AWSIotKeystoreHelper.generatePrivateAndPublicKeys(); // save in keystore AWSIotKeystoreHelper.saveCertificateAndPrivateKey(CERT_ID, TestCert, testKP.getPrivate(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); }
fis.close(); return getTempKeystore(keyStore, certId, keyStorePassword);
@Test(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackNullTopic() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); testClient.publishData("test1".getBytes(StringUtils.UTF8), null, AWSIotMqttQos.QOS0, mdcb, "TEST_TOKEN"); }
CreateKeysAndCertificateResult certResult = iotClient.createKeysAndCertificate(certRequest); AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certResult.getCertificateId(), certResult.getCertificatePem(), certResult.getKeyPair().getPrivateKey(), iotClient.attachPrincipalPolicy(policyRequest); KeyStore ks = AWSIotKeystoreHelper.getIotKeystore(certResult.getCertificateId(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD);
/** * Get certificate and private key from stream. * * @param certId The certificate Id or alias. * @param keyStoreInputStream an InputStream of a Keystore. * @param keyStorePassword The password for the keystore. * @return KeyStore with with private key and certificate. */ public static KeyStore getIotKeystore(String certId, InputStream keyStoreInputStream, String keyStorePassword) { try { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(keyStoreInputStream, keyStorePassword.toCharArray()); return getTempKeystore(keyStore, certId, keyStorePassword); } catch (CertificateException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (NoSuchAlgorithmException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (KeyStoreException e) { throw new AWSIotCertificateException("Error retrieving certificate and key.", e); } catch (IOException e) { throw new AmazonClientException("Error retrieving certificate and key.", e); } }
@Test(expected = IllegalArgumentException.class) public void testPublishDataWithCallbackNullQos() throws Exception { MockMqttClient mockClient = new MockMqttClient(); AWSIotMqttManager testClient = new AWSIotMqttManager("test-client", Region.getRegion(Regions.US_EAST_1), TEST_ENDPOINT_PREFIX); testClient.setMqttClient(mockClient); TestClientStatusCallback csb = new TestClientStatusCallback(); TestMessageDeliveryCallback mdcb = new TestMessageDeliveryCallback(); KeyStore testKeystore = AWSIotKeystoreHelper.getIotKeystore(CERT_ID, KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD); testClient.connect(testKeystore, csb); testClient.publishData("test1".getBytes(StringUtils.UTF8), "unit/test/topic", null, mdcb, "TEST_TOKEN"); }
CreateKeysAndCertificateResult certResult = iotClient.createKeysAndCertificate(certRequest); AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certResult.getCertificateId(), certResult.getCertificatePem(), certResult.getKeyPair().getPrivateKey(), iotClient.attachPrincipalPolicy(policyRequest); KeyStore ks = AWSIotKeystoreHelper.getIotKeystore(certResult.getCertificateId(), KEYSTORE_PATH, KEYSTORE_NAME, KEYSTORE_PASSWORD);