codesign_util.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2023 Microsoft, Inc.
  2. // Copyright 2013 The Chromium Authors
  3. // Use of this source code is governed by the MIT license that can be
  4. // found in the LICENSE file.
  5. #include "shell/common/mac/codesign_util.h"
  6. #include <optional>
  7. #include "base/apple/foundation_util.h"
  8. #include "base/apple/osstatus_logging.h"
  9. #include "base/apple/scoped_cftyperef.h"
  10. #include <CoreFoundation/CoreFoundation.h>
  11. #include <Security/Security.h>
  12. namespace electron {
  13. std::optional<bool> IsUnsignedOrAdHocSigned(SecCodeRef code) {
  14. base::apple::ScopedCFTypeRef<SecStaticCodeRef> static_code;
  15. OSStatus status = SecCodeCopyStaticCode(code, kSecCSDefaultFlags,
  16. static_code.InitializeInto());
  17. if (status == errSecCSUnsigned) {
  18. return true;
  19. }
  20. if (status != errSecSuccess) {
  21. OSSTATUS_LOG(ERROR, status) << "SecCodeCopyStaticCode";
  22. return {};
  23. }
  24. // Copy the signing info from the SecStaticCodeRef.
  25. base::apple::ScopedCFTypeRef<CFDictionaryRef> signing_info;
  26. status =
  27. SecCodeCopySigningInformation(static_code.get(), kSecCSSigningInformation,
  28. signing_info.InitializeInto());
  29. if (status != errSecSuccess) {
  30. OSSTATUS_LOG(ERROR, status) << "SecCodeCopySigningInformation";
  31. return {};
  32. }
  33. // Look up the code signing flags. If the flags are absent treat this as
  34. // unsigned. This decision is consistent with the StaticCode source:
  35. // https://github.com/apple-oss-distributions/Security/blob/Security-60157.40.30.0.1/OSX/libsecurity_codesigning/lib/StaticCode.cpp#L2270
  36. CFNumberRef signing_info_flags =
  37. base::apple::GetValueFromDictionary<CFNumberRef>(signing_info.get(),
  38. kSecCodeInfoFlags);
  39. if (!signing_info_flags) {
  40. return true;
  41. }
  42. // Using a long long to extract the value from the CFNumberRef to be
  43. // consistent with how it was packed by Security.framework.
  44. // https://github.com/apple-oss-distributions/Security/blob/Security-60157.40.30.0.1/OSX/libsecurity_utilities/lib/cfutilities.h#L262
  45. long long flags;
  46. if (!CFNumberGetValue(signing_info_flags, kCFNumberLongLongType, &flags)) {
  47. LOG(ERROR) << "CFNumberGetValue";
  48. return {};
  49. }
  50. if (static_cast<uint32_t>(flags) & kSecCodeSignatureAdhoc) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. bool ProcessSignatureIsSameWithCurrentApp(pid_t pid) {
  56. // Get and check the code signature of current app.
  57. base::apple::ScopedCFTypeRef<SecCodeRef> self_code;
  58. OSStatus status =
  59. SecCodeCopySelf(kSecCSDefaultFlags, self_code.InitializeInto());
  60. if (status != errSecSuccess) {
  61. OSSTATUS_LOG(ERROR, status) << "SecCodeCopyGuestWithAttributes";
  62. return false;
  63. }
  64. std::optional<bool> not_signed = IsUnsignedOrAdHocSigned(self_code.get());
  65. if (!not_signed.has_value()) {
  66. // Error happened.
  67. return false;
  68. }
  69. if (not_signed.value()) {
  70. // Current app is not signed.
  71. return true;
  72. }
  73. // Get the code signature of process.
  74. base::apple::ScopedCFTypeRef<CFNumberRef> process_cf(
  75. CFNumberCreate(nullptr, kCFNumberIntType, &pid));
  76. const void* attribute_keys[] = {kSecGuestAttributePid};
  77. const void* attribute_values[] = {process_cf.get()};
  78. base::apple::ScopedCFTypeRef<CFDictionaryRef> attributes(CFDictionaryCreate(
  79. nullptr, attribute_keys, attribute_values, std::size(attribute_keys),
  80. &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
  81. base::apple::ScopedCFTypeRef<SecCodeRef> process_code;
  82. status = SecCodeCopyGuestWithAttributes(nullptr, attributes.get(),
  83. kSecCSDefaultFlags,
  84. process_code.InitializeInto());
  85. if (status != errSecSuccess) {
  86. OSSTATUS_LOG(ERROR, status) << "SecCodeCopyGuestWithAttributes";
  87. return false;
  88. }
  89. // Get the requirement of current app's code signature.
  90. base::apple::ScopedCFTypeRef<SecRequirementRef> self_requirement;
  91. status = SecCodeCopyDesignatedRequirement(self_code.get(), kSecCSDefaultFlags,
  92. self_requirement.InitializeInto());
  93. if (status != errSecSuccess) {
  94. OSSTATUS_LOG(ERROR, status) << "SecCodeCopyDesignatedRequirement";
  95. return false;
  96. }
  97. DCHECK(self_requirement.get());
  98. // Check whether the process meets the signature requirement of current app.
  99. status = SecCodeCheckValidity(process_code.get(), kSecCSDefaultFlags,
  100. self_requirement.get());
  101. if (status != errSecSuccess && status != errSecCSReqFailed) {
  102. OSSTATUS_LOG(ERROR, status) << "SecCodeCheckValidity";
  103. return false;
  104. }
  105. return status == errSecSuccess;
  106. }
  107. } // namespace electron