InitializeCerts_Android.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/PlatformDef.h>
  9. // The AWS Native SDK AWSAllocator triggers a warning due to accessing members of std::allocator directly.
  10. // AWSAllocator.h(70): warning C4996: 'std::allocator<T>::pointer': warning STL4010: Various members of std::allocator are deprecated in
  11. // C++17. Use std::allocator_traits instead of accessing these members directly. You can define
  12. // _SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING or _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS to acknowledge that you have received
  13. // this warning.
  14. AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
  15. #include <aws/core/utils/memory/stl/AWSString.h>
  16. AZ_POP_DISABLE_WARNING
  17. #include <AzCore/Android/Utils.h>
  18. #include <AzCore/IO/FileIO.h>
  19. #include <AzCore/IO/SystemFile.h>
  20. #include <AzCore/std/containers/vector.h>
  21. namespace AWSNativeSDKInit
  22. {
  23. namespace Platform
  24. {
  25. void CopyCaCertBundle()
  26. {
  27. AZStd::vector<char> contents;
  28. AZStd::string certificatePath = "@products@/certificates/aws/cacert.pem";
  29. AZStd::string publicStoragePath = AZ::Android::Utils::GetAppPublicStoragePath();
  30. publicStoragePath.append("/certificates/aws/cacert.pem");
  31. AZ::IO::FileIOBase* fileBase = AZ::IO::FileIOBase::GetInstance();
  32. if (!fileBase->Exists(certificatePath.c_str()))
  33. {
  34. AZ_Error("AWSNativeSDKInit", false, "Certificate File(%s) does not exist.\n", certificatePath.c_str());
  35. }
  36. AZ::IO::HandleType fileHandle;
  37. AZ::IO::Result fileResult = fileBase->Open(certificatePath.c_str(), AZ::IO::OpenMode::ModeRead, fileHandle);
  38. if (!fileResult)
  39. {
  40. AZ_Error("AWSNativeSDKInit", false, "Failed to open certificate file with result %i\n", fileResult.GetResultCode());
  41. }
  42. AZ::u64 fileSize = 0;
  43. fileBase->Size(fileHandle, fileSize);
  44. if (fileSize == 0)
  45. {
  46. AZ_Error("AWSNativeSDKInit", false, "Given empty file(%s) as the certificate bundle.\n", certificatePath.c_str());
  47. }
  48. contents.resize(fileSize + 1);
  49. fileResult = fileBase->Read(fileHandle, contents.data(), fileSize);
  50. if (!fileResult)
  51. {
  52. AZ_Error(
  53. "AWSNativeSDKInit", false, "Failed to read from the certificate bundle(%s) with result code(%i).\n", certificatePath.c_str(),
  54. fileResult.GetResultCode());
  55. }
  56. AZ_Printf("AWSNativeSDKInit", "Certificate bundle is read successfully from %s", certificatePath.c_str());
  57. AZ::IO::HandleType outFileHandle;
  58. AZ::IO::Result outFileResult = fileBase->Open(publicStoragePath.c_str(), AZ::IO::OpenMode::ModeWrite, outFileHandle);
  59. if (!outFileResult)
  60. {
  61. AZ_Error("AWSNativeSDKInit", false, "Failed to open the certificate bundle with result %i\n", fileResult.GetResultCode());
  62. }
  63. AZ::IO::Result writeFileResult = fileBase->Write(outFileHandle, contents.data(), fileSize);
  64. if (!writeFileResult)
  65. {
  66. AZ_Error("AWSNativeSDKInit", false, "Failed to write the certificate bundle with result %i\n", writeFileResult.GetResultCode());
  67. }
  68. fileBase->Close(fileHandle);
  69. fileBase->Close(outFileHandle);
  70. AZ_Printf("AWSNativeSDKInit", "Certificate bundle successfully copied to %s", publicStoragePath.c_str());
  71. }
  72. } // namespace Platform
  73. }