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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật