user2807654
user2807654

Reputation: 122

How do I connect to AWS IoT with Local Certificate in an iOS app?

What I am trying to do is use the AWS SDK ( which can be found here: https://github.com/aws-amplify/aws-sdk-ios/blob/main/README.md#aws-sdk-for-ios ) to connect to AWS IoT so that I can subcribe to and publish data over MQTT.

So far, what I have done, is go to the AWS Console and in IoT Core I have created a new "Thing" as IoT calls it. I created a certificate for this Thing and when creating the certificate I downloaded the .crt file, the private and public .key files, and the CA1 and CA3 .pem files.

I then used this command in the Mac Terminal to create a .p12 file from the private key, CA1.pem file, and device certificate:

openssl pkcs12 -export -in myCert.crt -inkey privateKey.key -CAfile AmazonRootCA1.pem -out awsiot-identity.p12

It asked for a password. I typed in a password and confirmed it. This resulted in the creation of a .p12 file as expected. I then dragged that .p12 file into my Xcode project at the root level (same level your assets catalog and code files would be) and selected to copy it and add it to my Target.

In my code, in my class' init, I have this:

AWSMobileClient.initialize()
initializeControlPlane(credentialsProvider: AWSMobileClient.default())
initializeDataPlane(credentialsProvider: AWSMobileClient.default())

The code for those two functions looks like this:

    func initializeControlPlane(credentialsProvider: AWSCredentialsProvider) {
        let controlPlaneServiceConfiguration = AWSServiceConfiguration(region:AWS_REGION, credentialsProvider:credentialsProvider)
        
        AWSServiceManager.default().defaultServiceConfiguration = controlPlaneServiceConfiguration
        iotManager = AWSIoTManager.default()
        iot = AWSIoT.default()
    }
    
    func initializeDataPlane(credentialsProvider: AWSCredentialsProvider) {
        let iotEndPoint = AWSEndpoint(urlString: IOT_ENDPOINT)
        
        let iotDataConfiguration = AWSServiceConfiguration(region: AWS_REGION,
                                                           endpoint: iotEndPoint,
                                                           credentialsProvider: credentialsProvider)
        
        AWSIoTDataManager.register(with: iotDataConfiguration!, forKey: AWS_IOT_DATA_MANAGER_KEY)
        iotDataManager = AWSIoTDataManager(forKey: AWS_IOT_DATA_MANAGER_KEY)
    }

AWS_REGION is a constant defined like this: let AWS_REGION: AWSRegionType = .USEast2, IOT_ENDPOINT is a constant that is equal to the iot endpoint as shown in AWS. I found it by going to IoT Core and clicking settings near the bottom of the left pane. It is in this format: ```"xxxxxxxxxxxxxx-ats.iot.us-east-2.amazonaws.com"

Then, after those two functions are called, I call a function that looks basically like this:

    func importCert() {
        let myBundle = Bundle.main
        let myImages = myBundle.paths(forResourcesOfType: "p12" as String, inDirectory:nil)

        let certId = myImages.first
        
        let data = try! Data(contentsOf: URL(fileURLWithPath: certId!))
        
        if AWSIoTManager.importIdentity(fromPKCS12Data: data, passPhrase: PASSPHARASE, certificateId: CERT_ID) {
            self.iotDataManager.connect(withClientId: UUID().uuidString, cleanSession: true, certificateId: CERT_ID, statusCallback: self.mqttEventCallback)
        } else {
            print("failure")
        }
    }

PASSPHRASE is a string equal to what I set as the password for the .p12 file. CERT_ID is also a string that is equal to the CertificateID in AWS IoT (if it should be something else or if it even matters let me know).

What happens when I run this code is that the importIdentity function is always returning false, implying it failed to import my certificate into the Amazon keychain. According to the documentation on the connect function, the certificateId must be in the keychain.

I am very new to AWS and especially to their SDK. So, any help would be appreciated. I know they have an example app (found here: https://github.com/awslabs/aws-sdk-ios-samples/tree/main/IoT-Sample/Swift), but the sample app seems to be using Authentication through Cognito to create a certificate. What I want to do is use an existing certificate. The example app README and documentation both seem to mention this as an option. When using a local certificate, I wouldn't think I would need Cognito authentication.

Upvotes: 0

Views: 82

Answers (0)

Related Questions